Allow 0-dimensional inputs for var_mean#121
Conversation
|
|
||
| // 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)}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)|
@IvanYashchuk any reason you didn't merge this in? |
|
!test |
|
|
||
| int correction = 0; | ||
| bool keepdim = false; | ||
|
|
There was a problem hiding this comment.
| 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.
|
!test |
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>
Reducing 0-dimensional inputs is allowed since csarofeen/pytorch#1771.
In this PR I added a fallback path for the
var_meanop, so that it doesn't hit this checkFuser/csrc/ops/arith.cpp
Line 1724 in 4483f51
The change in the
numFeaturesfunction is required to avoid segfaults.