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

[BUG] sum function produces incorrect results #723

Closed
HugoPhibbs opened this issue Aug 19, 2024 · 2 comments · May be fixed by #724
Closed

[BUG] sum function produces incorrect results #723

HugoPhibbs opened this issue Aug 19, 2024 · 2 comments · May be fixed by #724

Comments

@HugoPhibbs
Copy link
Contributor

Describe the Bug
I'm using the sum function over a 2D tensor, and instead of doing a reduction along the cols. It doesn't seem to be adding values together properly.

To Reproduce
Steps to reproduce the behavior:

Basically I run the code:

template<typename T>
inline int *constructQueryVectorDegreeArrayMatx(matx::tensor_t<T, 2> &distances, T eps) {
    std::cout << eps << std::endl;
    print(distances);

    auto lt = distances < eps;
    auto lt_int = matx::as_type<int>(lt);

    print(lt_int);
    auto res = matx::make_tensor<int>({1, distances.Shape()[1]}, matx::MATX_MANAGED_MEMORY);
    (res = matx::sum(lt_int, {0})).run();

    print(res);
    return res.Data();
}

With input/output:

2.1
Tensor{float} Rank: 2, Sizes:[4, 4], Strides:[4,1]
000000:  0.0000e+00  1.0000e+00  2.0000e+00  3.0000e+00 
000001:  0.0000e+00  2.0000e+00  1.0000e+00  0.0000e+00 
000002:  1.0000e+00  8.0000e+00  9.0000e+00  1.1000e+01 
000003:  1.5000e+01  2.0000e+00  6.0000e+00  7.0000e+00 
Operator{int32_t} Rank: 2, Sizes:[4, 4]
000000:  1  1  1  0 
000001:  1  1  1  1 
000002:  1  0  0  0 
000003:  0  1  0  0 
Tensor{int32_t} Rank: 2, Sizes:[1, 4], Strides:[4,1]
000000:  9  9  9  9 // This should be different

Expected Behavior
You would expect res to be:

{3, 4, 1, 1}

But it instead full of 9s - which is coincidentally the total sum of the expected result.

System Details (please complete the following information):

  • OS: Ubuntu 20.04
  • CUDA version: 12.6
  • g++ version: 9.4.0
@cliffburdick
Copy link
Collaborator

Hi @HugoPhibbs, my first thought is you cannot reduce to a rank that's the same as the input, even if one of the dimensions is 1. I need to check if we error on that. Instead you would do something like:

    auto res = matx::make_tensor<int>({distances.Shape()[1]}, matx::MATX_MANAGED_MEMORY);
    (res = matx::sum(lt_int, {0})).run();
    auto res2d = clone<2>(res, {1, matxKeepDim});

Will test this later.

@cliffburdick
Copy link
Collaborator

I can confirm with the correct rank output it works fine. We are pushing a fix to enforce that:

    auto t = make_tensor<float>({4,4});
    t.SetVals({{0.f,1.f,2.f,3.f},
              {0.f,2.f,1.f,0.f},
              {1.f,8.f,9.f,11.f},
              {15.f,2.f,6.f,7.f}});

    auto lt = t < 5.f;
    auto lt_int = matx::as_type<int>(lt);
    auto t2 = make_tensor<int>({4});
    (t2 = sum(lt_int, {0})).run();
    print(t);
    print(lt_int);
    print(t2);
000000:  0.0000e+00  1.0000e+00  2.0000e+00  3.0000e+00
000001:  0.0000e+00  2.0000e+00  1.0000e+00  0.0000e+00
000002:  1.0000e+00  8.0000e+00  9.0000e+00  1.1000e+01
000003:  1.5000e+01  2.0000e+00  6.0000e+00  7.0000e+00
Operator{int32_t} Rank: 2, Sizes:[4, 4]
000000:  1  1  1  1
000001:  1  1  1  1
000002:  1  0  0  0
000003:  0  1  0  0
Tensor{int32_t} Rank: 1, Sizes:[4], Strides:[1]
000000:  3
000001:  3
000002:  2
000003:  2

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 a pull request may close this issue.

2 participants