Skip to content

PyTorch efficiency lint misses .to() after torch.randint #5666

Description

@dillydill123

Describe the Bug

The pytorch-efficiency-lint-redundant-to-call diagnostic detects .to(device) after torch.rand and others, but does not detect .to(device) or .to(torch.bool) after torch.randint. torch.randint accepts both device and dtype arguments, so these conversions can be specified when constructing the tensor.

import torch

def randint_bool_dtype_via_to() -> torch.Tensor:
    return torch.randint(0, 2, (2, 2, 1)).to(torch.bool)

def randint_bool_dtype_direct() -> torch.Tensor:
    return torch.randint(0, 2, (2, 2, 1), dtype=torch.bool)

def randint_to_device(device: torch.device) -> torch.Tensor:
    return torch.randint(0, 2, (2, 2, 1)).to(device)

def rand_to_device(device: torch.device) -> torch.Tensor:
    return torch.rand((2, 2, 1)).to(device)

Pyrefly should report pytorch-efficiency-lint-redundant-to-call for the torch.randint(...).to(torch.bool) and torch.randint(...).to(device) cases. Right now it only flags rand(...).

Environment:

  • Pyrefly 1.4.0
  • PyTorch 2.11.0
  • numpy 1.26.4

I'm not sure what other torch factories are also missed.

Sandbox Link

No sandbox due to torch requriement

(Only applicable for extension issues) IDE Information

No response

Activity

  1. jay23606 commented on Nov 17, 2026

    @jay23606

    I'd like to work on this. My plan is to extend the efficiency lint's factory list so torch.randint is handled like torch.rand, likely in the redundant-to-call check, and add test cases for the .to(torch.bool) and .to(device) forms. Could you assign this to me?

  2. added theissue type on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issue

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions