Skip to content

fix: parse integer MTX files and fix tensor buffer ownership#580

Open
urays wants to merge 1 commit into
tensor-compiler:masterfrom
urays:master
Open

fix: parse integer MTX files and fix tensor buffer ownership#580
urays wants to merge 1 commit into
tensor-compiler:masterfrom
urays:master

Conversation

@urays

@urays urays commented Jul 9, 2026

Copy link
Copy Markdown

This PR fixes two sparse tensor correctness issues:

  • Allow MatrixMarket files with integer fields to be parsed in addition to real.
  • Fix heap memory leaks when packed or generated sparse tensor buffers are transferred back into TACO storage.

Problem

TACO previously rejected valid MatrixMarket files whose field type was integer, even though those files can be
handled by the existing MatrixMarket read path.

TACO also leaked heap memory after packing or reading sparse tensors. The affected allocations came from generated
pack/assemble kernels and from taco_tensor_t fill-value handling.

There were two ownership issues:

  • init_taco_tensor_t allocated a separate fill_value, but deinit_taco_tensor_t never released it. In practice,
    TensorStorage::operator taco_tensor_t*() may also replace tensorData->fill_value with
    content->fillValue.getValPtr(), so treating fill_value as owned storage is unsafe.
  • unpackTensorData and Kernel::unpackResults wrapped generated sparse pos, idx, and vals buffers with
    Array::UserOwns. These buffers are allocated by generated TACO code using malloc/realloc; after transfer into
    TensorStorage, there is no other owner, so they must be released by the receiving Array.

Changes

  • Accept MatrixMarket integer fields alongside real fields.
  • Wrap transferred sparse position, index, singleton index, and value buffers with Array::Free instead of
    Array::UserOwns.
  • Update Kernel::unpackResults to use the same ownership behavior for generated sparse result buffers.
  • Change init_taco_tensor_t so fill_value aliases the supplied fill_ptr rather than allocating a separate copy.
  • Allocate the expected index pointer storage for taco_mode_dia1.

Validation

Valgrind was run on a MatrixMarket CSR read + SpMM case that previously reported definitely-lost allocations from
generated pack code and taco_tensor_t initialization.

After this change:

definitely lost: 0 bytes in 0 blocks
indirectly lost: 0 bytes in 0 blocks
possibly lost: 0 bytes in 0 blocks

Notes

The ownership model is now explicit: generated sparse buffers are transferred into TACO storage and released through
Array::Free, while taco_tensor_t::fill_value remains a non-owning reference to storage managed by the TensorStorage/
Literal path.

  Accept MatrixMarket integer fields alongside real fields, transfer unpacked tensor index/value buffers to Array
  ownership, and avoid leaking copied fill values.
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.

1 participant