Skip to content

[Ops][addmv] Validate self shape and dimension in torch.addmv - #6011

Closed
Kaap10 wants to merge 4 commits into
google-pytorch:mainfrom
Kaap10:fix-addmv-self-validation
Closed

Kaap10 wants to merge 4 commits into
google-pytorch:mainfrom
Kaap10:fix-addmv-self-validation

Conversation

@Kaap10

@Kaap10 Kaap10 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #6010

torch.addmv(self, mat, vec) validates that mat.dim() == 2, vec.dim() == 1, and mat.size(1) == vec.size(0) in csrc/ops/addmv/addmv_aten_kernels.cc. However, ValidateAddmvInputs performed no dimension or shape validation on self:

  1. It omitted validating that self is a scalar or vector (self.dim() <= 1).
  2. When self is 1-D, it omitted validating that its length matches mat.size(0) (or is 1 for singleton broadcast).

This caused two issues:

  • Silent execution with beta=0: Addmv executes the if (beta.IsZero()) branch which completely drops self and delegates to BuildAddmvShloBetaZero. Because self was never validated in ValidateAddmvInputs, invalid inputs (e.g. 2D/3D tensors or mismatched 1D lengths) silently succeeded on TPU, whereas PyTorch CPU/CUDA raises RuntimeError.
  • Unhandled StableHLO failure with beta != 0: Incompatible shapes failed deep inside StableHLO lowering with failed to broadcast tensor: ... instead of a standard PyTorch RuntimeError.

This PR:

  1. Adds self.dim() <= 1 validation in ValidateAddmvInputs.
  2. Adds self.size(0) == 1 || self.size(0) == mat.size(0) length validation for 1-D self in ValidateAddmvInputs.
  3. Adds comprehensive error tests in tests/errors_test.py (test_addmv_not_a_vector_or_scalar and test_addmv_size_mismatch) covering both beta=1 and beta=0.

Test Plan

  • Verified Python formatting and linting: ruff check tests/errors_test.py (all checks passed with 0 errors).
  • Verified Google C++ formatting: all modified lines in csrc/ops/addmv/addmv_aten_kernels.cc strictly adhere to <= 80 columns.
  • Verified test coverage for both rank and length checks under default beta=1 and beta=0.

@github-actions

Copy link
Copy Markdown

Thanks for contributing to TorchTPU, @Kaap10! 🔥

To ensure smooth code reviews, we kindly ask authors and reviewers to follow the process documented in https://github.com/google-pytorch/torch_tpu/blob/main/CONTRIBUTING.md#code-reviews.

Happy hacking!

@ghpvnist ghpvnist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Added a few comments.

Comment thread tests/errors_test.py Resolved
Comment thread tests/errors_test.py Resolved
Comment thread tests/errors_test.py Resolved
Comment thread tests/errors_test.py Resolved
Comment thread tests/errors_test.py Resolved
Kaap10 added a commit to Kaap10/torch_tpu that referenced this pull request Oct 30, 2026
Refine the size mismatch error message in addmv_aten_kernels.cc to include all operand shapes, and update reviewer attribution to gunhyun in tests/errors_test.py per review comments on google-pytorch#6011.
@Kaap10

Kaap10 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review - @ghpvnist

I have addressed all the comments:

  1. Updated the TPU error message in addmv_aten_kernels.cc to display all operand shapes: expected arguments shapes [5], [2, 3], [3] to have compatible shapes, got 5 vs 2 which is not compatible.
  2. Updated message_reviewed_by="gunhyun" across all newly added assertions in tests/errors_test.py.
  3. Verified with ruff check and confirmed all checks are passing.

Ready for another look whenever you have time!

@Kaap10
Kaap10 requested a review from ghpvnist October 30, 2026 02:46

@ghpvnist ghpvnist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@zhanyong-wan zhanyong-wan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread tests/errors_test.py Outdated

with et.assert_raises_message(
RuntimeError,
tpu="""addmv(): expected the first argument to be a vector or scalar (0D or 1D tensor), got 2D tensor""",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we swap "vector" and "scalar" to match the order of "0D or 1D"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, swapped to "scalar or vector".

Comment thread tests/errors_test.py Outdated

with et.assert_raises_message(
RuntimeError,
tpu="""addmv(): expected arguments shapes [5], [2, 3], [3] to have compatible shapes, got 5 vs 2 which is not compatible""",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar nit: "arguments shapes" => "argument shapes"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, updated to "argument shapes".

Comment thread tests/errors_test.py Outdated

with et.assert_raises_message(
RuntimeError,
tpu="""addmv(): expected arguments shapes [5], [2, 3], [3] to have compatible shapes, got 5 vs 2 which is not compatible""",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: simplify "have compatible shapes" to "be compatible"?

Then we can drop " which is not compatible" as it's obvious.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, simplified to "to be compatible, got 5 vs 2".

@Kaap10
Kaap10 requested a review from zhanyong-wan November 3, 2026 03:34

@zhanyong-wan zhanyong-wan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the CI errors.

@Kaap10

Kaap10 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Please fix the CI errors.

Fixed.

Before:

        << " to be compatible, got " << self.size(0) << " vs "
        << mat.size(0);

After:

        << " to be compatible, got " << self.size(0) << " vs " << mat.size(0);

Verified locally with clang-format.

@zhanyong-wan

Copy link
Copy Markdown
Contributor

@ghpvnist , the imported CL fails in internal testing. Could you fix up cl/1010873988 to get this landed? Thanks.

Comment thread tests/errors_test.py Outdated
RuntimeError,
tpu="""addmv(): expected the first argument to be a scalar or vector (0D or 1D tensor), got 2D tensor""",
gpu="""vector + matrix @ vector expected, got 2, 2, 1""",
message_reviewed_by="gunhyun",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a rebase conflict here and recently there was an api change to remove the message_reviewd_by parameter. Could you rebase to head and remove this field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, rebased onto latest head and removed the message_reviewed_by parameter from the error tests.
Also updated the error macro registry in tests/error_coverage_data.yaml.

Fixes google-pytorch#6010

Validate that self is a scalar or vector (dim <= 1) and that when dim == 1, its length matches mat.size(0) or is 1 in ValidateAddmvInputs before dispatching to Addmv. This prevents silent execution on invalid shapes when beta=0 and unhandled StableHLO broadcast failures when beta!=0. Add test coverage in tests/errors_test.py.
Refine the size mismatch error message in addmv_aten_kernels.cc to include all operand shapes, and update reviewer attribution to gunhyun in tests/errors_test.py per review comments on google-pytorch#6011.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants