Expose Storage.data_ptr() as public API#1554
Expose Storage.data_ptr() as public API#1554alinpahontu2912 wants to merge 2 commits intodotnet:mainfrom
Conversation
|
Hey @LittleLittleCloud does this help you? |
Change the access modifier of Storage.data_ptr() from protected to public, allowing external consumers to obtain the raw data pointer for interop with other native libraries (e.g. bitsandbytes). Fixes dotnet#1454 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
696fe86 to
70b1a4e
Compare
There was a problem hiding this comment.
Pull request overview
This PR exposes torch.Storage.data_ptr() as a public API so external consumers can obtain the underlying storage pointer for native interop scenarios (e.g., CUDA libraries like bitsandbytes).
Changes:
- Change
Storage.data_ptr()visibility fromprotectedtopublic. - Add a unit test validating
storage<T>().data_ptr()returns a non-zero pointer.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/TorchSharp/Tensor/Storage.cs |
Makes data_ptr() public to enable external interop access to the underlying storage pointer. |
test/TorchSharpTest/TestTorchTensor.cs |
Adds coverage ensuring Storage.data_ptr() returns a non-zero pointer for a basic tensor-backed storage. |
Comments suppressed due to low confidence (1)
src/TorchSharp/Tensor/Storage.cs:151
- Now that data_ptr() is public, calling it after Storage.Dispose() will currently throw a NullReferenceException because Dispose() sets _tensor = null and data_ptr() unconditionally dereferences _tensor.Handle. Consider adding an explicit guard (e.g., throw InvalidOperationException) when _tensor is null so the public API fails predictably instead of NRE.
public IntPtr data_ptr()
{
if (_tensor_data_ptr != IntPtr.Zero)
return _tensor_data_ptr;
var res = THSStorage_data_ptr(_tensor.Handle);
if (res == IntPtr.Zero) { CheckForErrors(); }
_tensor_data_ptr = res;
return res;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/TorchSharp/Tensor/Storage.cs:172
Storage.data_ptr()can throw a NullReferenceException when called on a scalarStorage<T>instance (created via the public implicit operator) or afterStorage<T>.Dispose(), because_tensoris null and the method callsTHSStorage_data_ptr(_tensor.Handle). Since this method is now public API, add an explicit guard (e.g., throwInvalidOperationException/ObjectDisposedException) when_tensoris null.
public IntPtr data_ptr()
{
if (_tensor_data_ptr != IntPtr.Zero)
return _tensor_data_ptr;
var res = THSStorage_data_ptr(_tensor.Handle);
if (res == IntPtr.Zero) { CheckForErrors(); }
_tensor_data_ptr = res;
return res;
src/TorchSharp/Tensor/Storage.cs:172
- The XML docs state callers must not cache the returned pointer and that it becomes invalid if the underlying storage is moved/reallocated/resized, but the implementation caches the pointer in
_tensor_data_ptrand always returns the cached value. This can make repeated calls return a stale/invalid pointer after a reallocation; consider either (1) always queryingTHSStorage_data_ptr(or providing a refresh option) or (2) updating the contract/docs to explicitly state that the pointer is cached and will not update.
public IntPtr data_ptr()
{
if (_tensor_data_ptr != IntPtr.Zero)
return _tensor_data_ptr;
var res = THSStorage_data_ptr(_tensor.Handle);
if (res == IntPtr.Zero) { CheckForErrors(); }
_tensor_data_ptr = res;
return res;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Change the access modifier of Storage.data_ptr() from protected to public, allowing external consumers to obtain the raw data pointer for interop with other native libraries (e.g. bitsandbytes).
Fixes #1454