Skip to content

Comments

Allow 0-dimensional inputs for var_mean#121

Merged
jjsjann123 merged 9 commits intoNVIDIA:mainfrom
IvanYashchuk:var_mean_0d
Jul 25, 2025
Merged

Allow 0-dimensional inputs for var_mean#121
jjsjann123 merged 9 commits intoNVIDIA:mainfrom
IvanYashchuk:var_mean_0d

Conversation

@IvanYashchuk
Copy link
Collaborator

Reducing 0-dimensional inputs is allowed since csarofeen/pytorch#1771.
In this PR I added a fallback path for the var_mean op, so that it doesn't hit this check

TORCH_CHECK(tv->nDims() > 0, "Tried to reduce a 0-dim tensor");

The change in the numFeatures function is required to avoid segfaults.

@IvanYashchuk IvanYashchuk requested a review from jjsjann123 April 4, 2023 13:11
Copy link
Collaborator

@jjsjann123 jjsjann123 left a comment

Choose a reason for hiding this comment

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

LGTM


// Welford op can't handle 0-dim tensors, so we need to handle them separately
if (x->nDims() == 0) {
return {variance(x, dims, correction, keepdim), mean(x, dims, keepdim)};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Feels a little bit awkward here, since variance for a scalar tensor is just nan and mean is just returning itself. Having said that, we might want to keep it like this in the definition, that feels like an optimization pass's job to simplify that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree, but functions in FusidionDefinition.ops should still mostly work with edge cases like that. Otherwise, a higher-level project using these functions would need to have more and more wrappers that fix problematic cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not just nan, it's also 0.0 😄

In [1]: import torch

In [2]: a = torch.randn(())

In [3]: torch.var(a, dim=0, correction=0)
Out[3]: tensor(0.)

In [4]: torch.var(a, dim=0, correction=1)
Out[4]: tensor(nan)

@csarofeen
Copy link
Collaborator

@IvanYashchuk any reason you didn't merge this in?

@jjsjann123
Copy link
Collaborator

!test


int correction = 0;
bool keepdim = false;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
at::ScalarType dtype = at::kFloat; // Declare dtype before use

[Suggested by AI] One line was added to declare and initialize the variable dtype:

  • at::ScalarType dtype = at::kFloat; // Declare dtype before use

This ensures that dtype is defined (as a float scalar type) before it is referenced elsewhere in the code, eliminating the previous build error caused by an undeclared or uninitialized variable.

@jjsjann123
Copy link
Collaborator

!test

@jjsjann123 jjsjann123 merged commit 70ab277 into NVIDIA:main Jul 25, 2025
50 of 52 checks passed
nsarka pushed a commit to nsarka/Fuser that referenced this pull request Jul 28, 2025
Reducing 0-dimensional inputs is allowed since
csarofeen/pytorch#1771.
In this PR I added a fallback path for the `var_mean` op, so that it
doesn't hit this check
https://github.com/NVIDIA/Fuser/blob/4483f51a396a8c594caae8078e3cb82c7d3caa44/csrc/ops/arith.cpp#L1724
The change in the `numFeatures` function is required to avoid segfaults.

---------

Co-authored-by: Christian Sarofeen <csarofeen@nvidia.com>
Co-authored-by: jjsjann123 <jiej@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants