cast

Tensor.cast(dtype: Union[DataType, type], auto_cast: bool = True)

Creates a new tensor with the specified target data type, and optionally casts the data of original tensor with the target data type, stores casted data in the new tenor.

The new tensor is created with a contiguous memory layout in host memory. The new tensor does not contain any quantization information (QuantInfo).

Note

  • If auto_cast is set to false, the new tensor is created without storing any data. To store data from original Tensor object, set auto_cast to true, which will automatically cast data to the target data type, and stores data in the new tensor.

  • This function is only valid for non-image (multi-dimensional) tensors.

Parameters:
  • dtype (Union[DataType, type]) -- The target data type for the new tensor. If a Python type, such as np.float32 is provided, it will be mapped to the corresponding internal DataType using TensorInfo.numpy_tcim_map. See TensorInfo for details.

  • auto_cast (bool) -- Specifies if to automatically cast the data of original Tensor object to the target data type specified in dtype. If set to true, the data will be automatically cast to the target data type with tcim_lite.runtime.Tensor.cast_to(), and stored in the new tensor. If set to false, the new tensor will be created with target data type without performing data casting. Defaults to true.

Returns:

A new object with the specified data type without quantization information.

Return type:

Tensor

Raises:

RuntimeError -- If the operation fails, such as due to unsupported data type conversion or invalid tensor type (image tensor).