Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix scalar tensor.mean #8970

Merged
merged 18 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions oneflow/core/common/wrap_dim_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static inline Maybe<std::bitset<dim_bitset_size>> dim_list_to_bitset(
std::bitset<dim_bitset_size> seen;
for (int32_t i = 0; i < dims.size(); i++) {
size_t dim = JUST(maybe_wrap_dim(dims[i], ndims));
CHECK_OR_RETURN(!seen[dim]) << Error::RuntimeError() << "The dim " << dim
<< " appears multiple times in the list of dims";
CHECK_OR_RETURN_ERROR(!seen[dim]) << Error::RuntimeError() << "The dim " << dim
<< " appears multiple times in the list of dims";
seen[dim] = true;
}
return seen;
Expand Down
17 changes: 6 additions & 11 deletions oneflow/core/functional/impl/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,18 @@ bool IsScalarTensor(const std::shared_ptr<Tensor>& x) {

Maybe<std::vector<int32_t>> CheckAxis(const std::vector<int32_t>& axis, const int32_t& ndim) {
const int32_t naxis = axis.size();

int32_t reduce_ndim = naxis;
if (naxis == 0 || ndim == 0) { reduce_ndim = ndim; };
std::vector<int32_t> reduce_axis(reduce_ndim);
if (naxis == 0) {
std::vector<int32_t> reduce_axis(ndim);
std::iota(reduce_axis.begin(), reduce_axis.end(), 0);
return reduce_axis;
} else {
std::vector<int32_t> reduce_axis(naxis);
std::vector<int32_t> axis_num(ndim);
JUST(dim_list_to_bitset(axis, ndim)); // checking axis[dim]'s validation
for (int32_t i = 0; i < naxis; i++) {
reduce_axis[i] = JUST(maybe_wrap_dim(axis[i], ndim));
axis_num[reduce_axis[i]]++;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bug的原因在这一行,当x.mean(-1)里的tensor x是scalar tensor时,则参数axis:{1}; ndim=0,reduce_axis为空vector,reduce_axis[i]会引发segment fault。

CHECK_OR_RETURN(axis_num[reduce_axis[i]] < 2)
<< Error::RuntimeError() << "dim " << reduce_axis[i]
<< " appears multiple times in the list of dims";
if (i < reduce_ndim) { reduce_axis[i] = JUST(maybe_wrap_dim(axis[i], ndim)); };
}
return reduce_axis;
}
return reduce_axis;
}

Maybe<void> CheckInplaceValid(const std::shared_ptr<Tensor>& x) {
Expand Down
7 changes: 7 additions & 0 deletions python/oneflow/test/modules/test_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def test_mean_with_random_data(test_case):
x = random_tensor(ndim=4, dtype=float).to(device)
return torch.mean(x, dim)

@autotest(n=5)
def test_mean_with_scalar_data(test_case):
device = random_device()
x = random_tensor(ndim=4, dtype=float).to(device).mean()
y = x.mean(-1)
return y


if __name__ == "__main__":
unittest.main()