Skip to content

GPU BlockUniTensor Trace test uses wrong locator on rank-0 result (tests/gpu/BlockUniTensor_test.cpp) #1090

Description

@IvanaGyro

Description

BlockUniTensorTest.gpu_Trace (renamed BlockUniTensorTest.GpuTrace on the reorganized codex/clean-up-test-files-and-reorganize-includes branch, same test body) throws in tests/gpu/BlockUniTensor_test.cpp. Reproduced on master@5d2d081 directly, so it predates and is unrelated to #1080 — this is a bug in the GPU test file itself, not in BlockUniTensor.

Reproduction

ctest --test-dir build/debug-openblas-cuda -R "BlockUniTensorTest.gpu_Trace" --output-on-failure

Observed error

[ RUN      ] BlockUniTensorTest.gpu_Trace
unknown file: Failure
C++ exception with description "
# Cytnx error occur at void cytnx::BlockUniTensor::_fx_locate_elem(cytnx::cytnx_int64&, std::vector<long unsigned int>&, const std::vector<long unsigned int>&) const
# error: [ERROR][BlockUniTensor][elem_exists] rank-0 scalar expects no locator indices.

# file : ../../src/BlockUniTensor.cpp (1384)" thrown in the test body.
[  FAILED  ] BlockUniTensorTest.gpu_Trace (308 ms)

Root cause

tests/gpu/BlockUniTensor_test.cpp traces a rank-2 UT_diag down to rank 0 and then indexes it with a non-empty locator:

tmp = UT_diag.Trace(0, 1);
...
EXPECT_DOUBLE_EQ(double(tmp.at({0}).real()), double(ans));   // wrong: tmp is rank-0
EXPECT_DOUBLE_EQ(double(tmp.at({0}).imag()), double(0));

The CPU counterpart in tests/BlockUniTensor_test.cpp (TEST_F(BlockUniTensorTest, Trace)) handles the same UT_diag.Trace(0, 1) result correctly, asserting rank-0-ness explicitly and using an empty locator, matching CLAUDE.md's "rank-0 scalar is not the same as shape {1}" guardrail:

tmp = UT_diag.Trace(0, 1);
...
EXPECT_EQ(tmp.rank(), 0);
EXPECT_TRUE(tmp.bonds().empty());
...
EXPECT_DOUBLE_EQ(double(tmp.at({}).real()), double(ans));
EXPECT_DOUBLE_EQ(double(tmp.at({}).imag()), double(0));

So the GPU test's tmp.at({0}) should be tmp.at({}) — the GPU test file just never picked up this correction that the CPU test file already has. BlockUniTensor::_fx_locate_elem is behaving correctly by rejecting the locator; this is a test bug, not a library bug.

Suggested fix

In tests/gpu/BlockUniTensor_test.cpp, change the two tmp.at({0}) calls following UT_diag.Trace(0, 1) to tmp.at({}), matching the CPU test's already-correct handling.

Environment

  • CUDA 12.6.85 (conda cytnx env), GTX 1660 SUPER / RTX 2080 Ti.
  • debug-openblas-cuda preset (ASan + RUN_TESTS=ON).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions