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:

DataType

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[hidet.graph.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. None indicates the compact row major layout.

Return type:

Optional[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

to_device(device, /, *, stream=None)[source]

Move the tensor to the specified device.

Parameters:
  • device (Device or str) – The device to move the tensor to.

  • stream (Stream or None) – The stream to use for the copy. If None, the current stream is used.

Returns:

ret – The tensor on the specified device.

Return type:

Tensor

astype(dtype)[source]

Cast the data type of current tensor.

Parameters:

dtype (DataType or str) – The target data type to convert to.

Returns:

ret – The tensor with the new data type.

Return type:

Tensor

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:

Tensor

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:

Tensor

copy()[source]

Create a copy of current tensor.

Returns:

ret – A new tensor with the same contents as the current one.

Return type:

Tensor

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:

Tensor

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:

Tensor

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:

Tensor

detach()[source]

Detach the current tensor from tracing.

Returns:

ret – The detached tensor.

Return type:

Tensor

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:

torch.Tensor

to(dtype=None, device=None)[source]

Cast the data type of current tensor or/and move it to another device.

Parameters:
  • dtype (DataType or str, optional) – The target data type to convert to. None indicates unchanged.

  • device (Device or str, optional) – The target device to copy the tensor. None indicates unchanged.

Returns:

ret – The tensor with the new data type on target device.

Return type:

Tensor

item()[source]

Convert the tensor to a scalar value.

Return type:

int | float | bool

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:

Tensor

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:

Tensor

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:

Tensor

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:

Tensor

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:

Tensor

sum(dims, keep_dim=False)[source]

Create a sum reduced tensor.

See Also hidet.graph.ops.reduce_sum().

Parameters:
  • dims (Union[int, List[int]]) – The dimensions to sum up.

  • keep_dim (bool) – Whether to keep the reduced dimensions.

Returns:

ret – The reduced tensor.

Return type:

Tensor

mean(dims, keep_dim=False)[source]

Create a mean reduced tensor.

See Also hidet.graph.ops.reduce_mean().

Parameters:
  • dims (Union[int, List[int]]) – The dimensions to average up.

  • keep_dim (bool) – Whether to keep the reduced dimensions.

Returns:

ret – The reduced tensor.

Return type:

Tensor