Skip to content

Fix aten::any.dim truthiness for numeric tensors#4353

Open
fallintoplace wants to merge 1 commit into
pytorch:mainfrom
fallintoplace:fix-any-dim-numeric-truthiness
Open

Fix aten::any.dim truthiness for numeric tensors#4353
fallintoplace wants to merge 1 commit into
pytorch:mainfrom
fallintoplace:fix-any-dim-numeric-truthiness

Conversation

@fallintoplace

Copy link
Copy Markdown

Summary

  • Convert numeric aten::any.dim inputs into an explicit nonzero mask before reducing.
  • Keep bool inputs on the existing bool-to-int reduction path.
  • Add deterministic regression tests for cancelling numeric values and negative nonzero values.

Testing

  • git diff --check
  • bazelisk test //tests/core/conversion/converters:test_reduce was attempted locally. The first run was blocked by missing PyTorch; after installing PyTorch in a local venv and setting TORCH_PATH, Bazel stayed in analysis for several minutes on this local machine and was interrupted before running tests.

@meta-cla meta-cla Bot added the cla signed label Jun 20, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters labels Jun 20, 2026
@github-actions github-actions Bot requested a review from zewenli98 June 20, 2026 21:54
@zewenli98

Copy link
Copy Markdown
Collaborator

@fallintoplace Thanks for the contribution. Torchscript is being removed soon (like pytorch 2.14). Please use Dynamo instead.

@zewenli98 zewenli98 closed this Jun 23, 2026
@fallintoplace

Copy link
Copy Markdown
Author

@zewenli98 What about this one? Thank you for your attention.

@zewenli98 zewenli98 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@fallintoplace Thanks for the contribution. Torchscript is being removed soon (like pytorch 2.14). Please use Dynamo instead.

Pytorch decided not to remove Torchscript support in 2.14.

This PR fixes the correctness bug in aten::any on numeric tensors (cases like [1, -1] that cancel to 0). Can you fix the comments and rebase? Thanks!

in_tensor = castITensor(ctx, in_tensor, nvinfer1::DataType::kINT32, (util::node_info(n) + "_in").c_str());
} else {
// Numeric truthiness is based on nonzero elements, not the reduced sum of raw values.
auto zero_tensor = tensor_to_const(ctx, torch::tensor({0}, torch::kInt32), util::node_info(n) + "_zero");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

zero_tensor with torch::kInt32 is created here and then compared with in_tensor. If in_tensor is float/half or lives on a different device, the elementwise EQUAL may fail at network construction or behave incorrectly.
Can you create the zero constant to match in_tensor's dtype and device?

Comment on lines +361 to +362
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you test different dtypes like fp32 and fp16?
Can you also test if NaN works? e.g.:

>>> torch.any(torch.tensor([0, torch.nan]))
tensor(True)

@zewenli98 zewenli98 reopened this Jul 1, 2026
@fallintoplace fallintoplace force-pushed the fix-any-dim-numeric-truthiness branch from 910ffd0 to e5ecc75 Compare July 1, 2026 17:02

@zewenli98 zewenli98 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@zewenli98

Copy link
Copy Markdown
Collaborator

@fallintoplace Can you fix the lint issue? please refer to https://github.com/pytorch/TensorRT/blob/main/CONTRIBUTING.md#coding-guidelines. Thanks

@fallintoplace fallintoplace force-pushed the fix-any-dim-numeric-truthiness branch from e5ecc75 to 1254a03 Compare July 1, 2026 21:52
@fallintoplace fallintoplace force-pushed the fix-any-dim-numeric-truthiness branch from 1254a03 to 7e03ae2 Compare July 1, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants