-
Notifications
You must be signed in to change notification settings - Fork 97
introduce a versatile sparse tensor type to MatX (experimental) #821
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// BSD 3-Clause License | ||
// | ||
// Copyright (c) 2025, NVIDIA Corporation | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this | ||
// list of conditions and the following disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
///////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "matx.h" | ||
|
||
using namespace matx; | ||
|
||
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) | ||
{ | ||
MATX_ENTER_HANDLER(); | ||
|
||
cudaStream_t stream = 0; | ||
cudaExecutor exec{stream}; | ||
|
||
// | ||
// Creates a COO matrix for the following 4x8 dense matrix with 5 nonzero | ||
// elements, using the factory method that uses MatX tensors for the 1-dim | ||
// buffers. The sparse matrix resides in the same memory space as its buffer | ||
// constituents. | ||
// | ||
// | 1, 2, 0, 0, 0, 0, 0, 0 | | ||
// | 0, 0, 0, 0, 0, 0, 0, 0 | | ||
// | 0, 0, 0, 0, 0, 0, 0, 0 | | ||
// | 0, 0, 3, 4, 0, 5, 0, 0 | | ||
// | ||
|
||
constexpr index_t m = 4; | ||
constexpr index_t n = 8; | ||
constexpr index_t nse = 5; | ||
|
||
tensor_t<float, 1> values{{nse}}; | ||
tensor_t<int, 1> row_idx{{nse}}; | ||
tensor_t<int, 1> col_idx{{nse}}; | ||
|
||
values.SetVals({ 1, 2, 3, 4, 5 }); | ||
row_idx.SetVals({ 0, 0, 3, 3, 3 }); | ||
col_idx.SetVals({ 0, 1, 2, 3, 5 }); | ||
|
||
// Note that sparse tensor support in MatX is still experimental. | ||
auto Acoo = experimental::make_tensor_coo(values, row_idx, col_idx, {m, n}); | ||
|
||
// | ||
// This shows: | ||
// | ||
// tensor_impl_2_f32: Tensor{float} Rank: 2, Sizes:[4, 8], Levels:[4, 8] | ||
// nse = 5 | ||
// format = ( d0, d1 ) -> ( d0 : compressed(non-unique), d1 : singleton ) | ||
// crd[0] = ( 0 0 3 3 3 ) | ||
// crd[1] = ( 0 1 2 3 5 ) | ||
// values = ( 1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 ) | ||
// space = CUDA managed memory | ||
// | ||
print(Acoo); | ||
|
||
// TODO: operations on Acoo | ||
|
||
MATX_EXIT_HANDLER(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// BSD 3-Clause License | ||
// | ||
// Copyright (c) 2025, NVIDIA Corporation | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this | ||
// list of conditions and the following disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
///////////////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
|
||
#include "matx/core/sparse_tensor.h" | ||
|
||
namespace matx { | ||
namespace experimental { | ||
|
||
// | ||
// MatX uses a single versatile sparse tensor type that uses a tensor format | ||
// DSL (Domain Specific Language) to describe a vast space of storage formats. | ||
// This file provides a number of convenience factory methods that construct | ||
// sparse tensors in well-known storage formats, like COO, CSR, and CSC, | ||
// directly from the constituent buffers. More factory methods can easily be | ||
// added as the need arises. | ||
// | ||
|
||
// Constructs a sparse matrix in COO format directly from the values and | ||
// the two coordinates vectors. The entries should be sorted by row, then | ||
// column. Duplicate entries should not occur. Explicit zeros may be stored. | ||
template <typename ValTensor, typename CrdTensor> | ||
auto make_tensor_coo(ValTensor &val, CrdTensor &row, CrdTensor &col, | ||
const index_t (&shape)[2], bool owning = false) { | ||
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1); | ||
using VAL = typename ValTensor::value_type; | ||
using CRD = typename CrdTensor::value_type; | ||
using POS = int; // no positions, although some forms use [0,nse] | ||
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning}; | ||
basic_storage<decltype(emptyp)> ep{std::move(emptyp)}; | ||
return sparse_tensor_t<VAL, CRD, POS, COO>( | ||
shape, val.GetStorage(), {row.GetStorage(), col.GetStorage()}, {ep, ep}); | ||
} | ||
|
||
// Constructs a sparse matrix in CSR format directly from the values, the row | ||
// positions, and column coordinates vectors. The entries should be sorted by | ||
// row, then column. Explicit zeros may be stored. | ||
template <typename ValTensor, typename PosTensor, typename CrdTensor> | ||
auto make_tensor_csr(ValTensor &val, PosTensor &rowp, CrdTensor &col, | ||
const index_t (&shape)[2], bool owning = false) { | ||
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1 && | ||
PosTensor::Rank() == 1); | ||
using VAL = typename ValTensor::value_type; | ||
using CRD = typename CrdTensor::value_type; | ||
using POS = typename PosTensor::value_type; | ||
raw_pointer_buffer<CRD, matx_allocator<CRD>> emptyc{nullptr, 0, owning}; | ||
basic_storage<decltype(emptyc)> ec{std::move(emptyc)}; | ||
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning}; | ||
basic_storage<decltype(emptyp)> ep{std::move(emptyp)}; | ||
return sparse_tensor_t<VAL, CRD, POS, CSR>( | ||
shape, val.GetStorage(), {ec, col.GetStorage()}, {ep, rowp.GetStorage()}); | ||
} | ||
|
||
// Constructs a sparse matrix in CSC format directly from the values, | ||
// the row coordinates, and column position vectors. The entries should | ||
// be sorted by column, then row. Explicit zeros may be stored. | ||
template <typename ValTensor, typename PosTensor, typename CrdTensor> | ||
auto make_tensor_csc(ValTensor &val, CrdTensor &row, PosTensor &colp, | ||
const index_t (&shape)[2], bool owning = false) { | ||
static_assert(ValTensor::Rank() == 1 && CrdTensor::Rank() == 1 && | ||
PosTensor::Rank() == 1); | ||
using VAL = typename ValTensor::value_type; | ||
using CRD = typename CrdTensor::value_type; | ||
using POS = typename PosTensor::value_type; | ||
raw_pointer_buffer<CRD, matx_allocator<CRD>> emptyc{nullptr, 0, owning}; | ||
basic_storage<decltype(emptyc)> ec{std::move(emptyc)}; | ||
raw_pointer_buffer<POS, matx_allocator<POS>> emptyp{nullptr, 0, owning}; | ||
basic_storage<decltype(emptyp)> ep{std::move(emptyp)}; | ||
return sparse_tensor_t<VAL, CRD, POS, CSC>( | ||
shape, val.GetStorage(), {ec, row.GetStorage()}, {ep, colp.GetStorage()}); | ||
} | ||
|
||
} // namespace experimental | ||
} // namespace matx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.