Repository navigation
Closed
Labels
Description
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:
- Dimension constraint: PyTorch requires
selfto be either a 0D scalar or 1D vector (self.dim() <= 1). Multidimensional tensors (2D, 3D, etc.) are invalid and raiseRuntimeError: vector + matrix @ vector expected, got .... - Length constraint: When
self.dim() == 1, its length must either be1(which broadcasts) or equal tomat.size(0). Incompatible lengths raiseRuntimeError: size mismatch, got input (K), mat (MxN), vec (N).
Impact:
- Silent correctness bug on
beta=0: Whenbeta == 0,Addmvexecutes the fast-path optimization (if (beta.IsZero())), which completely dropsselfand delegates toBuildAddmvShloBetaZero(mat_op, vec_op, alpha_op, ...)without ever building or validatingself. BecauseValidateAddmvInputsnever checkedself's dimensions, invalid inputs such asself = torch.ones(5)orself = torch.ones(2, 2)against a(2, 3)matrix silently succeed on TPU and return an output of shape[2], whereas PyTorch CPU/CUDA raisesRuntimeError. - Unhandled StableHLO failure on
beta != 0: Whenbeta != 0,BroadcastIfNeeded(bias_op, mv_op)fails deep inside the StableHLO builder withfailed to broadcast tensor: ...instead of raising a clean, PyTorch-compliantRuntimeError.
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, invalidselfshapes bypass execution and silently produce a[2]output. - When
beta != 0, mismatched shapes fail with an internalfailed to broadcast tensor: ...StableHLO error.
- When
- What was expected:
ValidateAddmvInputsshould validate thatself.dim() <= 1, and ifself.dim() == 1, thatself.size(0) == 1 || self.size(0) == mat.size(0).- Incompatible shapes should raise a clear Python
RuntimeErrormatching 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:
ValidateAddmvInputsvalidatesmatandvec, but only checks!IsBool(self)forself.Addmv()skipsselfentirely whenbeta.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
- In
csrc/ops/addmv/addmv_aten_kernels.cc, updateValidateAddmvInputsto checkself's rank and size compatibility withmat.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) << ")"; }
- Add corresponding error tests in
tests/errors_test.py:test_addmv_self_not_a_vectortest_addmv_self_size_mismatch(testing bothbeta != 0andbeta = 0)
Environment
- PyTorch Version: 2.15 / nightly
- TorchTPU:
main(0.2.1.dev) - Hardware: TPU v5e / v6e / Cloud TPU VM
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Medium
Activity
Kaap10 commentedon Oct 29, 2026
Opened PR #6011 with the validation fix and comprehensive error test coverage.
[Ops][addmv] Validate self shape and dimension in torch.addmv
[Ops][addmv] Validate self shape and dimension in torch.addmv