Skip to content
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

Add shape() convenience method #1161

Merged
merged 3 commits into from
Mar 27, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `long[] pytorch.Tensor.shape()` method for convenience ([pull #1161](https://github.com/bytedeco/javacpp-presets/pull/1161))
* Enable DNNL codegen as BYOC backend in presets for TVM
* Allow passing raw pointer as deleter to `from_blob()`, etc functions of PyTorch ([discussion #1160](https://github.com/bytedeco/javacpp-presets/discussions/1160))
* Include `cudnn_backend.h` header file in presets for CUDA ([issue #1158](https://github.com/bytedeco/javacpp-presets/issues/1158))
Expand Down
14 changes: 14 additions & 0 deletions pytorch/src/main/java/org/bytedeco/pytorch/AbstractTensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public static Tensor create(byte[] data, boolean signed, long... shape) {
public abstract long nbytes();
public abstract Pointer data_ptr();

/**
* Convenience method, similar to {@code sizes().vec().get()}.
*
* Returns a new {@code long[]} with each call since e.g. transpose_() and squeeze_() can change the shape of the tensor,
* and the caller could otherwise modify the contents, surprising subsequent callers.
*
* Please memoize externally if you're concerned about performance.
*/
public long[] shape() {
long[] out = new long[(int) ndimension()];
for (int i = 0; i < out.length; i++) out[i] = size(i);
return out;
}

/** Returns {@code createBuffer(0)}. */
public <B extends Buffer> B createBuffer() {
return (B)createBuffer(0);
Expand Down