Skip to content

Commit 31eda5c

Browse files
Add Tensor.to_sparse() API for sparse COO tensor conversion
Add to_sparse() and to_sparse(int sparse_dim) methods to convert dense tensors to sparse COO format. This enables GCN and GAT graph neural network examples that require sparse matrix operations. Changes across all 4 binding layers: - THSTensor.h: declarations - THSTensor.cpp: implementations - LibTorchSharp.THSTensor.cs: P/Invoke - Tensor.cs: managed C# methods
1 parent 297f8cb commit 31eda5c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/Native/LibTorchSharp/THSTensor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,16 @@ Tensor THSTensor_to_dense(Tensor tensor)
18941894
CATCH_TENSOR(tensor->to_dense());
18951895
}
18961896

1897+
Tensor THSTensor_to_sparse(Tensor tensor)
1898+
{
1899+
CATCH_TENSOR(tensor->to_sparse());
1900+
}
1901+
1902+
Tensor THSTensor_to_sparse_with_dims(Tensor tensor, const int64_t sparse_dim)
1903+
{
1904+
CATCH_TENSOR(tensor->to_sparse(sparse_dim));
1905+
}
1906+
18971907
void THSTensor_set_(Tensor tensor, const Tensor source)
18981908
{
18991909
CATCH(tensor->set_(*source););

src/Native/LibTorchSharp/THSTensor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,9 @@ EXPORT_API(Tensor) THSTensor_trapezoid_dx(const Tensor y, const double dx, int64
13961396

13971397
EXPORT_API(Tensor) THSTensor_to_dense(Tensor tensor);
13981398

1399+
EXPORT_API(Tensor) THSTensor_to_sparse(Tensor tensor);
1400+
EXPORT_API(Tensor) THSTensor_to_sparse_with_dims(Tensor tensor, const int64_t sparse_dim);
1401+
13991402
EXPORT_API(Tensor) THSTensor_to_device(const Tensor tensor, const int device_type, const int device_index, const bool copy, const bool non_blocking);
14001403

14011404
EXPORT_API(Tensor) THSTensor_to_type(const Tensor tensor, int8_t scalar_type, const bool copy, const bool non_blocking);

src/TorchSharp/PInvoke/LibTorchSharp.THSTensor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ internal static extern IntPtr THSTensor_upsample_nearest3d(IntPtr input,
376376
[DllImport("LibTorchSharp")]
377377
internal static extern IntPtr THSTensor_to_dense(IntPtr handle);
378378

379+
[DllImport("LibTorchSharp")]
380+
internal static extern IntPtr THSTensor_to_sparse(IntPtr handle);
381+
382+
[DllImport("LibTorchSharp")]
383+
internal static extern IntPtr THSTensor_to_sparse_with_dims(IntPtr handle, long sparse_dim);
384+
379385
[DllImport("LibTorchSharp")]
380386
internal static extern IntPtr THSTensor_clone(IntPtr handle);
381387

src/TorchSharp/Tensor/Tensor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,29 @@ public Tensor to_dense()
13761376
return new Tensor(res);
13771377
}
13781378

1379+
/// <summary>
1380+
/// Converts a dense tensor to a sparse COO tensor.
1381+
/// </summary>
1382+
public Tensor to_sparse()
1383+
{
1384+
var res = NativeMethods.THSTensor_to_sparse(Handle);
1385+
if (res == IntPtr.Zero)
1386+
CheckForErrors();
1387+
return new Tensor(res);
1388+
}
1389+
1390+
/// <summary>
1391+
/// Converts a dense tensor to a sparse COO tensor with the specified number of sparse dimensions.
1392+
/// </summary>
1393+
/// <param name="sparse_dim">The number of sparse dimensions.</param>
1394+
public Tensor to_sparse(int sparse_dim)
1395+
{
1396+
var res = NativeMethods.THSTensor_to_sparse_with_dims(Handle, sparse_dim);
1397+
if (res == IntPtr.Zero)
1398+
CheckForErrors();
1399+
return new Tensor(res);
1400+
}
1401+
13791402
/// <summary>
13801403
/// Returns a copy of the tensor input.
13811404
/// </summary>

0 commit comments

Comments
 (0)