trailofbits.python.torch-tensor.torch-tensor

profile photo of trailofbitstrailofbits
Author
unknown
Download Count*

Avoid using torch.Tensor() to directly create a tensor for efficiency and proper parsing

Run Locally

Run in CI

Defintion

rules:
  - id: torch-tensor
    message: Avoid using `torch.Tensor()` to directly create a tensor for efficiency
      and proper parsing
    languages:
      - python
    severity: WARNING
    metadata:
      category: performance
      subcategory:
        - audit
      confidence: HIGH
      references:
        - https://pytorch.org/docs/stable/tensors.html
      license: CC-BY-NC-SA-4.0
    pattern: torch.Tensor(...)

Examples

torch-tensor.py

import torch 

# ruleid: torch-tensor
y = torch.Tensor(x)

def foo(x): 
  # ruleid: torch-tensor
  return torch.Tensor(x)

import torch as t

# ruleid: torch-tensor
y = t.Tensor(x)

def foo(x): 
  # ruleid: torch-tensor
  return t.Tensor(x)


# ok: torch-tensor
y = torch.tensor([0, 1])