hidet.Tensor
hidet.Tensor¶
- class hidet.Tensor(shape, dtype, device, storage, layout=None, trace=None)¶
An n-dimension array, could be symbolic or concrete.
This class defines an n-dimension array.
- Parameters
shape (Sequence[int]) – The shape of the tensor.
dtype (DataType or str) – The data type of the tensor.
device (Device or str) – The device of the tensor.
storage (Storage, optional) – The storage of the tensor. None indicates it is a symbolic tensor.
layout (DataLayout, optional) – The data layout of the tensor.
trace (Tuple[Operator, int], optional) – Where this tensor is derived from. A trace = (op, i) indicates that this tensor is the i-th output of the op operator.
- shape¶
The shape of the tensor.
The shape is a tuple of integers indicating the size of the tensor along each dimension.
- Returns
shape – The shape of the tensor.
- Return type
Tuple[int, …]
- dtype¶
The data type of the tensor.
- Returns
dtype – The data type of the tensor.
- Return type
- device¶
The device of the tensor.
- Returns
device – The device of the tensor.
- Return type
Device
- size¶
The number of elements in the tensor.
- Returns
size – The number of elements in the tensor.
- Return type
int
- nbytes¶
The number of bytes of the tensor.
- Returns
ret – The number of bytes.
- Return type
int
- storage¶
The storage of the tensor.
- Returns
storage – The storage of the tensor.
- Return type
Storage
- trace¶
The producer and the index of outputs of the producer of this tensor.
This attribute is used to track how this tensor is computed. None indicates this is a leaf tensor where the value will be given by the user. Otherwise, it will be a tuple with (operator, index) where operator is the producer of this tensor and index is the index of the output of the operator.
- Returns
trace – The trace of this tensor.
- Return type
Tuple[Operator, int]
- op¶
The operator that produces this tensor.
- Returns
ret – The operator that produces this tensor. None indicates it is not traced.
- Return type
hidet.graph.operator.Operator, optional
- layout¶
The data layout of the tensor.
Note
This attribute is experimental and might change in the future.
- Returns
layout – The data layout of the tensor.
- Return type
DataLayout
- tolist()[source]¶
Convert the tensor to a nested list of numbers.
- Returns
ret – The nested list of numbers. The number of nested levels is equal to the rank of the tensor.
- Return type
the nested list of numbers
- cpu()[source]¶
Create a copy of self tensor on cpu device.
If the current tensor is already on cpu device, self is returned.
- Returns
ret – The new tensor or self.
- Return type
- cuda(device=None)[source]¶
Create a copy of self tensor on cuda device.
If the current tensor is already on cuda device, self is returned.
- Parameters
device (Device, optional) – The target cuda device. None indicates the current cuda device.
- Returns
ret – The new tensor or self.
- Return type
- copy()[source]¶
Create a copy of current tensor.
- Returns
ret – A new tensor with the same contents as the current one.
- Return type
- cpu_async(stream=None)[source]¶
Copy the tensor to CPU asynchronously.
- Parameters
stream (hidet.cuda.Stream, optional) – The stream to copy the tensor to CPU on.
- Returns
ret – The tensor on CPU.
- Return type
- cuda_async(device=None, stream=None)[source]¶
Copy the tensor to GPU asynchronously.
- Parameters
device (Device, optional) – The target cuda device. None indicates the current cuda device.
stream (hidet.cuda.Stream, optional) – The stream to copy the tensor to GPU on. None indicates the current stream.
- Returns
ret – The tensor on GPU.
- Return type
- copy_async(stream=None)[source]¶
Create a copy of current tensor asynchronously.
- Parameters
stream (hidet.cuda.Stream, optional) – The stream to copy the tensor. None indicates the current stream of the device where self tensor is on.
- Returns
ret – A new tensor with the same contents as the current one.
- Return type
- detach()[source]¶
Detach the current tensor from tracing.
- Returns
ret – The detached tensor.
- Return type
- numpy()[source]¶
Convert the tensor to a numpy array.
The tensor must be on CPU device. Otherwise, a RuntimeError will be raised. The returned numpy array will share the same memory with the tensor.
- Returns
ret – The numpy array.
- Return type
np.ndarray
- torch()[source]¶
Convert to a torch tensor.
- Returns
ret – The torch tensor that shares the memory with the hidet tensor.
- Return type
- to(dtype=None, device=None)[source]¶
Cast the data type of current tensor or/and move it to another device.
- signature()[source]¶
Get the signature of the tensor.
- Returns
ret – The signature of the tensor.
- Return type
str
- is_symbolic()[source]¶
Check if the tensor is symbolic.
A tensor is symbolic if it is not backed by any storage (i.e.,
self.storage is None
).- Returns
ret – True if the tensor is symbolic, False otherwise.
- Return type
bool
- contiguous()[source]¶
Create a tensor with contiguous row-major layout.
If the tensor already has the continuous row-major layout, this tensor is returned directly.
- Returns
ret – The tensor with contiguous row-major layout.
- Return type
- reshape(shape)[source]¶
Create a reshaped tensor.
See Also
hidet.graph.ops.reshape()
.- Parameters
shape (Sequence[int]) – The new shape.
- Returns
ret – The reshaped tensor.
- Return type
- squeeze(dims)[source]¶
Create a squeezed tensor.
See Also
hidet.graph.ops.squeeze()
.- Parameters
dims (Union[int, Sequence[int]]) – The dimension(s) to squeeze.
- Returns
ret – The squeezed tensor.
- Return type
- unsqueeze(dims)[source]¶
Create a unsqueezed tensor.
See Also
hidet.graph.ops.unsqueeze()
.- Parameters
dims (Union[int, Sequence[int]]) – The dimensions to unsqueeze.
- Returns
ret – The unsqueezed tensor.
- Return type
- rearrange(plan)[source]¶
Create a rearranged tensor.
See Also
hidet.graph.ops.rearrange()
.- Parameters
plan (List[List[int]]) – The rearrange plan.
- Returns
ret – The rearranged tensor.
- Return type