Skip to content

[Ops][addmv] Missing shape and dimension validation for self in torch.addmv #6010

Closed

Description

@Kaap10

Summary

In csrc/ops/addmv/addmv_aten_kernels.cc, ValidateAddmvInputs(self, mat, vec, beta, alpha) validates input matrix and vector dimensions:

TT_RET_CHECK(mat.dim() == 2, error::kInvalidArgument)
    << "expected the second argument to be a matrix (2D tensor), got "
    << mat.dim() << "D tensor";

TT_RET_CHECK(vec.dim() == 1, error::kInvalidArgument)
    << "expected the third argument to be a vector (1D tensor), got "
    << vec.dim() << "D tensor";

TT_RET_CHECK(mat.size(1) == vec.size(0), error::kInvalidArgument)
    << "expected the last dimension of the second argument (matrix of size ["
    << absl::StrJoin(mat.sizes(), /* separator= */ ", ")
    << "]) to match the first dimension of the third argument (vector of size ["
    << absl::StrJoin(vec.sizes(), /* separator= */ ", ") << "]), got "
    << mat.size(1) << " vs " << vec.size(0);

However, it performs no dimension or shape validation on self:

  1. Dimension constraint: PyTorch requires self to be either a 0D scalar or 1D vector (self.dim() <= 1). Multidimensional tensors (2D, 3D, etc.) are invalid and raise RuntimeError: vector + matrix @ vector expected, got ....
  2. Length constraint: When self.dim() == 1, its length must either be 1 (which broadcasts) or equal to mat.size(0). Incompatible lengths raise RuntimeError: size mismatch, got input (K), mat (MxN), vec (N).

Impact:

  • Silent correctness bug on beta=0: When beta == 0, Addmv executes the fast-path optimization (if (beta.IsZero())), which completely drops self and delegates to BuildAddmvShloBetaZero(mat_op, vec_op, alpha_op, ...) without ever building or validating self. Because ValidateAddmvInputs never checked self's dimensions, invalid inputs such as self = torch.ones(5) or self = torch.ones(2, 2) against a (2, 3) matrix silently succeed on TPU and return an output of shape [2], whereas PyTorch CPU/CUDA raises RuntimeError.
  • Unhandled StableHLO failure on beta != 0: When beta != 0, BroadcastIfNeeded(bias_op, mv_op) fails deep inside the StableHLO builder with failed to broadcast tensor: ... instead of raising a clean, PyTorch-compliant RuntimeError.

Minimal Reproducible Example (MRE)

import torch

device = torch.device("tpu")
mat = torch.ones(2, 3, device=device)
vec = torch.ones(3, device=device)

# Case 1: Incompatible 1D self length with beta=0 silently succeeds
self_mismatch = torch.ones(5, device=device)
# On CPU / CUDA:
#   RuntimeError: size mismatch, got input (5), mat (2x3), vec (3)
# On TPU:
#   Silently succeeds and returns a tensor of shape [2]!
res1 = torch.addmv(self_mismatch, mat, vec, beta=0)

# Case 2: 2D self tensor with beta=0 silently succeeds
self_2d = torch.ones(2, 2, device=device)
# On CPU / CUDA:
#   RuntimeError: vector + matrix @ vector expected, got 2, 2, 1
# On TPU:
#   Silently succeeds and returns a tensor of shape [2]!
res2 = torch.addmv(self_2d, mat, vec, beta=0)

# Case 3: Incompatible shape with beta != 0 triggers internal StableHLO broadcast failure
# On TPU:
#   RuntimeError: addmv(): failed to broadcast tensor: ...
res3 = torch.addmv(self_mismatch, mat, vec, beta=1)

What Happened vs What Was Expected

  • What happened:
    • When beta=0, invalid self shapes bypass execution and silently produce a [2] output.
    • When beta != 0, mismatched shapes fail with an internal failed to broadcast tensor: ... StableHLO error.
  • What was expected:
    • ValidateAddmvInputs should validate that self.dim() <= 1, and if self.dim() == 1, that self.size(0) == 1 || self.size(0) == mat.size(0).
    • Incompatible shapes should raise a clear Python RuntimeError matching standard PyTorch error behavior:
      RuntimeError: addmv(): expected the first argument to be a vector or scalar (0D or 1D tensor), got 2D tensor
      RuntimeError: addmv(): size mismatch, got input (5), mat (2x3), vec (3)
      

Root Cause

In csrc/ops/addmv/addmv_aten_kernels.cc:

  1. ValidateAddmvInputs validates mat and vec, but only checks !IsBool(self) for self.
  2. Addmv() skips self entirely when beta.IsZero():
    if (beta.IsZero()) {
      auto op_builder = [out_dtype, precision](FixedSizeSpan<mlir::MlirOp, 3> inputs)
          -> absl::StatusOr<mlir::MlirOp> {
        auto& [mat_op, vec_op, alpha_op] = inputs;
        return BuildAddmvShloBetaZero(mat_op, vec_op, alpha_op, out_dtype, precision);
      };
      return DispatchOpOut<3>(std::move(op_builder), {mat, vec, alpha_tensor},
                              out, std::move(options));
    }

Proposed Fix

  1. In csrc/ops/addmv/addmv_aten_kernels.cc, update ValidateAddmvInputs to check self's rank and size compatibility with mat.size(0):
    TT_RET_CHECK(self.dim() <= 1, error::kInvalidArgument)
        << "expected the first argument to be a vector or scalar (0D or 1D tensor), got "
        << self.dim() << "D tensor";
    
    if (self.dim() == 1) {
      TT_RET_CHECK(self.size(0) == 1 || self.size(0) == mat.size(0),
                   error::kInvalidArgument)
          << "size mismatch, got input (" << self.size(0) << "), mat ("
          << mat.size(0) << "x" << mat.size(1) << "), vec (" << vec.size(0) << ")";
    }
  2. Add corresponding error tests in tests/errors_test.py:
    • test_addmv_self_not_a_vector
    • test_addmv_self_size_mismatch (testing both beta != 0 and beta = 0)

Environment

  • PyTorch Version: 2.15 / nightly
  • TorchTPU: main (0.2.1.dev)
  • Hardware: TPU v5e / v6e / Cloud TPU VM

Activity

  1. Kaap10 commented on Oct 29, 2026

    @Kaap10
    ContributorAuthor

    Opened PR #6011 with the validation fix and comprehensive error test coverage.

  2. added a commit that references this issue on Nov 11, 2026
    d6a8dbc
  3. added a commit that references this issue on Nov 12, 2026
    9218ffd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Fields

Priority

Medium

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions