-
public abstract class Tensor
Representation of an ExecuTorch Tensor. Behavior is similar to PyTorch's tensor objects.
Most tensors will be constructed as
{@code Tensor.fromBlob(data, shape)}
, where{@code data}
can be an array or a direct Buffer (of the proper subclass). Helper methods are provided to allocate buffers properly.To access Tensor data, see dtype, shape, and various
{@code getDataAs*}
methods.When constructing
{@code Tensor}
objects with{@code data}
as an array, it is not specified whether this data is copied or retained as a reference so it is recommended not to modify it after constructing.{@code data}
passed as a Buffer is not copied, so it can be modified between Module calls to avoid reallocation. Data retrieved from{@code Tensor}
objects may be copied or may be a reference to the{@code Tensor}
's internal data buffer.{@code shape}
is always copied.Warning: These APIs are experimental and subject to change without notice
-
-
Method Summary
Modifier and Type Method Description static ByteBuffer
allocateByteBuffer(int numElements)
Allocates a new direct ByteBuffer with native byte order with specified capacity thatcan be used in fromBlob, . static IntBuffer
allocateIntBuffer(int numElements)
Allocates a new direct IntBuffer with native byte order with specified capacity thatcan be used in fromBlob. static FloatBuffer
allocateFloatBuffer(int numElements)
Allocates a new direct FloatBuffer with native byte order with specified capacity thatcan be used in fromBlob. static LongBuffer
allocateLongBuffer(int numElements)
Allocates a new direct LongBuffer with native byte order with specified capacity thatcan be used in fromBlob. static DoubleBuffer
allocateDoubleBuffer(int numElements)
Allocates a new direct DoubleBuffer with native byte order with specified capacity thatcan be used in fromBlob. static Tensor
fromBlobUnsigned(Array<byte> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array ofbytes. static Tensor
fromBlob(Array<byte> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data as array ofbytes. static Tensor
fromBlob(Array<int> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data as array ofints. static Tensor
fromBlob(Array<float> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data as arrayof floats. static Tensor
fromBlob(Array<long> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data as array oflongs. static Tensor
fromBlob(Array<double> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data as arrayof doubles. static Tensor
fromBlobUnsigned(ByteBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data. static Tensor
fromBlob(ByteBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data. static Tensor
fromBlob(IntBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data. static Tensor
fromBlob(FloatBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data. static Tensor
fromBlob(LongBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data. static Tensor
fromBlob(DoubleBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data. long
numel()
Returns the number of elements in this tensor. static long
numel(Array<long> shape)
Calculates the number of elements in a tensor with the specified shape. Array<long>
shape()
Returns the shape of this tensor. abstract DType
dtype()
Array<byte>
getDataAsByteArray()
Array<byte>
getDataAsUnsignedByteArray()
Array<int>
getDataAsIntArray()
Array<float>
getDataAsFloatArray()
Array<long>
getDataAsLongArray()
Array<double>
getDataAsDoubleArray()
Array<byte>
toByteArray()
Serializes a {@code Tensor}
into a byte array.static Tensor
fromByteArray(Array<byte> bytes)
Deserializes a {@code Tensor}
from a byte[].-
-
Method Detail
-
allocateByteBuffer
static ByteBuffer allocateByteBuffer(int numElements)
Allocates a new direct ByteBuffer with native byte order with specified capacity thatcan be used in fromBlob, .
- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateIntBuffer
static IntBuffer allocateIntBuffer(int numElements)
Allocates a new direct IntBuffer with native byte order with specified capacity thatcan be used in fromBlob.
- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateFloatBuffer
static FloatBuffer allocateFloatBuffer(int numElements)
Allocates a new direct FloatBuffer with native byte order with specified capacity thatcan be used in fromBlob.
- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateLongBuffer
static LongBuffer allocateLongBuffer(int numElements)
Allocates a new direct LongBuffer with native byte order with specified capacity thatcan be used in fromBlob.
- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateDoubleBuffer
static DoubleBuffer allocateDoubleBuffer(int numElements)
Allocates a new direct DoubleBuffer with native byte order with specified capacity thatcan be used in fromBlob.
- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
fromBlobUnsigned
static Tensor fromBlobUnsigned(Array<byte> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array ofbytes.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
static Tensor fromBlob(Array<byte> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data as array ofbytes.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
static Tensor fromBlob(Array<int> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data as array ofints.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
static Tensor fromBlob(Array<float> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data as arrayof floats.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
static Tensor fromBlob(Array<long> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data as array oflongs.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
static Tensor fromBlob(Array<double> data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data as arrayof doubles.
- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlobUnsigned
static Tensor fromBlobUnsigned(ByteBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
fromBlob
static Tensor fromBlob(ByteBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
fromBlob
static Tensor fromBlob(IntBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
fromBlob
static Tensor fromBlob(FloatBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
fromBlob
static Tensor fromBlob(LongBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
fromBlob
static Tensor fromBlob(DoubleBuffer data, Array<long> shape)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data.
- Parameters:
data
- Direct buffer with native byte order that contains{@code Tensor.numel(shape)}
elements.shape
- Tensor shape
-
numel
long numel()
Returns the number of elements in this tensor.
-
numel
static long numel(Array<long> shape)
Calculates the number of elements in a tensor with the specified shape.
-
getDataAsByteArray
Array<byte> getDataAsByteArray()
-
getDataAsUnsignedByteArray
Array<byte> getDataAsUnsignedByteArray()
-
getDataAsIntArray
Array<int> getDataAsIntArray()
-
getDataAsFloatArray
Array<float> getDataAsFloatArray()
-
getDataAsLongArray
Array<long> getDataAsLongArray()
-
getDataAsDoubleArray
Array<double> getDataAsDoubleArray()
-
toByteArray
Array<byte> toByteArray()
Serializes a
{@code Tensor}
into a byte array. Note: This method is experimental and subject tochange without notice. This does NOT supoprt list type.
-
fromByteArray
static Tensor fromByteArray(Array<byte> bytes)
Deserializes a
{@code Tensor}
from a byte[]. Note: This method is experimental and subject tochange without notice. This does NOT supoprt list type.- Parameters:
bytes
- The byte array to deserialize from.
-
-
-
-