Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Native/LibTorchSharp/THSTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,16 @@ Tensor THSTensor_to_dense(Tensor tensor)
CATCH_TENSOR(tensor->to_dense());
}

Tensor THSTensor_to_sparse(Tensor tensor)
{
CATCH_TENSOR(tensor->to_sparse());
}

Tensor THSTensor_to_sparse_with_dims(Tensor tensor, const int64_t sparse_dim)
{
CATCH_TENSOR(tensor->to_sparse(sparse_dim));
}

void THSTensor_set_(Tensor tensor, const Tensor source)
{
CATCH(tensor->set_(*source););
Expand Down
3 changes: 3 additions & 0 deletions src/Native/LibTorchSharp/THSTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,9 @@ EXPORT_API(Tensor) THSTensor_trapezoid_dx(const Tensor y, const double dx, int64

EXPORT_API(Tensor) THSTensor_to_dense(Tensor tensor);

EXPORT_API(Tensor) THSTensor_to_sparse(Tensor tensor);
EXPORT_API(Tensor) THSTensor_to_sparse_with_dims(Tensor tensor, const int64_t sparse_dim);

EXPORT_API(Tensor) THSTensor_to_device(const Tensor tensor, const int device_type, const int device_index, const bool copy, const bool non_blocking);

EXPORT_API(Tensor) THSTensor_to_type(const Tensor tensor, int8_t scalar_type, const bool copy, const bool non_blocking);
Expand Down
6 changes: 6 additions & 0 deletions src/TorchSharp/PInvoke/LibTorchSharp.THSTensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ internal static extern IntPtr THSTensor_upsample_nearest3d(IntPtr input,
[DllImport("LibTorchSharp")]
internal static extern IntPtr THSTensor_to_dense(IntPtr handle);

[DllImport("LibTorchSharp")]
internal static extern IntPtr THSTensor_to_sparse(IntPtr handle);

[DllImport("LibTorchSharp")]
internal static extern IntPtr THSTensor_to_sparse_with_dims(IntPtr handle, long sparse_dim);

[DllImport("LibTorchSharp")]
internal static extern IntPtr THSTensor_clone(IntPtr handle);

Expand Down
23 changes: 23 additions & 0 deletions src/TorchSharp/Tensor/Tensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,29 @@ public Tensor to_dense()
return new Tensor(res);
}

/// <summary>
/// Converts a dense tensor to a sparse COO tensor.
/// </summary>
public Tensor to_sparse()
{
var res = NativeMethods.THSTensor_to_sparse(Handle);
if (res == IntPtr.Zero)
CheckForErrors();
return new Tensor(res);
}

/// <summary>
/// Converts a dense tensor to a sparse COO tensor with the specified number of sparse dimensions.
/// </summary>
/// <param name="sparse_dim">The number of sparse dimensions.</param>
public Tensor to_sparse(int sparse_dim)
{
var res = NativeMethods.THSTensor_to_sparse_with_dims(Handle, sparse_dim);
if (res == IntPtr.Zero)
CheckForErrors();
return new Tensor(res);
}

/// <summary>
/// Returns a copy of the tensor input.
/// </summary>
Expand Down
Loading