[tensor-shapes] torch.maximum rejects an inline torch.where result with the same variadic shape

Đang mở
#5,709 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
30/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch, rust
Lĩnh vực
devtools

Hướng nghiên cứu

Reproduce first via the sandbox link or the repro.py commands: the inline torch.where(...) and the named selected form must both report Tensor[S]. Then find the Rust checker's call-argument inference where a generic function's type-var tuple is solved — the named form solves it when binding selected, the inline form does so mid-argument. Done means pyrefly check passes on repro.py and the type-checker tests stay green. The payload names no source files, so start from call inference.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

pytorch tensor-shapes typechecking
Describe the Bug

Hej,

With Pyrefly and the PyTorch shape stubs at 1.5.0, I get a false positive when passing a torch.where result directly to torch.maximum. Giving that same result a local name makes the error disappear. Both forms should return Tensor[S]: the boolean mask and the two real-valued tensors all have shape S.

Reproduction

Save this as repro.py in an empty directory.

from __future__ import annotations

from typing import TYPE_CHECKING

import torch

if TYPE_CHECKING:
    from shape_extensions import IntTuple
    from torch import Tensor


def example[S: IntTuple](mask: Tensor[S], x: Tensor[S], y: Tensor[S]) -> Tensor[S]:
    return torch.maximum(torch.where(mask, x, y), y)

Run the following:

touch pyrefly.toml
uv venv --python 3.13.18 .venv
uv pip install --python .venv/bin/python --index https://download.pytorch.org/whl/cpu 'torch==2.14.0 cpu'
uv pip install --python .venv/bin/python pyrefly==1.5.0 pyrefly-torch-stubs==1.5.0
.venv/bin/pyrefly check --python-interpreter-path .venv/bin/python --output-format min-text repro.py

The checker exits with status 1 and underlines torch.where(mask, x, y).

ERROR repro.py:13:26-49: Argument `Tensor[S]` is not assignable to parameter `input` with type `Tensor[S]` in function `torch.maximum` [bad-argument-type]

Replacing the return statement with these two lines passes. reveal_type(selected) and reveal_type(torch.maximum(selected, y)) both report Tensor[S].

    selected = torch.where(mask, x, y)
    return torch.maximum(selected, y)

The inline form also passes when the rank is fixed, including Tensor[[N, M]] with two symbolic extents. A direct maximum on incompatible shapes (2, 3) and (3, 2) correctly fails. Calling the original function with same-shaped CPU tensors succeeds and gives the expected elementwise values.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.zVdda9swFP0rgr44I3U3xl4CG4VQtjC6lSUvwzNGdZTY1HZCbC_pYP995-pKluU66x72UEqJLV3dz3PPlXuUeVD7w65HmEmyaYEAqohtbHJNmkY21ODT6ur73U0y_3Qz_7z48rFHCeCXNDN19mRMQrWqOpN7lahToypKYEc1i6pZtejYnqjW1xmFPMOPG0hxg0fLWXc0DkpZP8yMaLSMp-LkvT323jRquzeLA-4GbTgs5Skv2zLgt2OmDkobgFaomtC_c-eYN1lS7FJZ_F-PQDCAj1oDF2f8-Ivn9jCL_SuW3xrosa79Yw7JCzHf4XC-zRoRpBNxqxop7grZoE1LTIdFlYZAzlrIDaCWSwAzFLeLFag5RUQq_FFdiG9q3aYIhYubKbFv70HdGZbehO_C14wN9F36ILcYQkQBTQaqA4M1eWpLDl3nZr2dl0biOaxNwReQpllLdaThKpKlduJ9D5A8UWxlaHvmxPghnokwhGMMBpN_K-vMfUXUh8EqdERxHBiCr_Zt4zChVSMsOtatOiVgmj5oumACvds3N7F4Qm3xd1OoUlXN5ZG43XgbCg4przZAGCaG-JlLseUrEsb_iWhC1GAwSU8ha2KtJvZrl3_dExqq6a5a50QnXQAoFYO9ofgjXp2SEpvGMUURm5pbdcPk8vaCEji-NZZ8bNnUP_XTNxUbNX6JnD27f7ZYZv-VlXNa_LGP_fGq-v70wBs4L7yio-qcT3TzsBeuesPkpXZ42N1D64JGdoJfd8WzNTQx-ou4FfYakoNvDrKqKZxoFQcbqjPnuRO9Hsjx4W1Ll7XEZbveq1RfQnGbogOw1ZnVGjvP-j0335UoktJpQe3xbLKxVnV6yO-Ro_tHYTpOFvkvLGjLIz3ncT5lJUxGvbQOmkE1Hp87UqgNMOnoSqPBLYzFRlb04Agm02By-QGXN8MJbJkYkvzrEhRoK0b3xJ9ez8UwBmNS_nIRfOYTzX6YDXHezQQCu31ZppkqJflvUujt1rz9tC3qYChj7_38NVSpIz6G0gKZ0bd_QrFxKxrYZrYeqvM6bBx_gy4ZapiKK1-zp_jJlba7u_7-Aw

(Only applicable for extension issues) IDE Information

No response

Ngôn ngữ chính
Rust
Star
7.2k
Fork
578
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của facebook/pyrefly

Tất cả issue của facebook/pyrefly

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.