trailofbits.python.pytorch-tensor.pytorch-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: pytorch-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
      technology:
        - pytorch
      description: Possible parsing issues and inefficiency from improper tensor creation
      references:
        - https://pytorch.org/docs/stable/tensors.html
      license: AGPL-3.0 license
    pattern: torch.Tensor(...)

Examples

pytorch-tensor.py

import torch 

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

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

import torch as t

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

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


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