Class Tensor

Inheritance Relationships

Base Type

  • public mindspore::tensor::MetaTensor

Class Documentation

class Tensor : public mindspore::tensor::MetaTensor

Public Functions

explicit Tensor(const Tensor &tensor)

Create tensor from another tensor, data is shared.

Parameters

tensor[in] [Tensor] The input tensor.

Tensor(const Tensor &tensor, TypeId data_type)

Create tensor with given data type from another tensor.

Parameters
  • tensor[in] [Tensor] The input tensor.

  • data_type[in] [TypeId] The new tensor data type.

Tensor(TypeId data_type, const ShapeVector &shape, TensorDataPtr data)

Create tensor with the given shared tensor data.

Parameters
  • data_type[in] [TypeId] Data type of the tensor.

  • shape[in] The shape represented by ShapeVector of the tensor.

  • data[in] The shared tensor data.

Tensor(TypeId data_type, const ShapeVector &shape)

Create a lazy allocated tensor.

Parameters
  • data_type[in] [TypeId] Data type of the tensor.

  • shape[in] The shape represented by ShapeVector of the tensor.

Tensor(TypeId data_type, const ShapeVector &shape, void *data, size_t data_len)

Create a tensor with input data buffer.

Parameters
  • data_type[in] [TypeId] Data type of the tensor.

  • shape[in] The shape represented by ShapeVector of the tensor.

  • data[in] The input data to be copied into tensor.

  • data_len[in] The length of data in bytes.

Tensor(TypeId data_type, const ShapeVector &shape, void *data, TypeId src_data_type)

Create a tensor with input data buffer and given source data type.

Parameters
  • data_type[in] [TypeId] Data type of the tensor.

  • shape[in] The shape represented by ShapeVector of the tensor.

  • data[in] The input data to be copied into tensor.

  • src_data_type[in] The source data type.

explicit Tensor(const std::vector<int64_t> &input, const TypePtr &data_type = nullptr)

Create 1 dimension tensor from an int vector.

Parameters
  • input[in] [std::vector<int64_t>] the data for tensor.

  • data_type[in] [TypeId] data type.

explicit Tensor(const std::vector<double> &input, const TypePtr &data_type = nullptr)

Create 1 dimension tensor from a float vector.

Parameters
  • input[in] [std::vector<double>] the data for tensor.

  • data_type[in] [TypeId] data type.

explicit Tensor(int64_t input, const TypePtr &data_type = nullptr)

Create 0 dimension tensor from an int64_t scalar.

Parameters
  • input[in] [int64] the data for tensor.

  • data_type[in] [TypeId] data type.

explicit Tensor(double input, const TypePtr &data_type = nullptr)

Create 0 dimension tensor from a float scalar.

Parameters
  • input[in] [double] the data for tensor.

  • data_type[in] [TypeId] data type.

explicit Tensor(uint64_t input, const TypePtr &data_type = nullptr)

Create 0 dimension tensor from a uint scalar.

Parameters
  • input[in] [uint] the data for tensor.

  • data_type[in] [TypeId] data type.

explicit Tensor(bool input, const TypePtr &data_type = nullptr)

Create 0 dimension tensor from a bool scalar.

Parameters
  • input[in] [bool] the data for tensor.

  • data_type[in] [TypeId] data type.

~Tensor() override = default

Destructor of Tensor.

bool ValueEqual(const Tensor &tensor) const

It is different from ‘operator==’ which just compares shape/type/address, it does real value comparison.

Parameters

tensor[in] The Tensor object to be compared.

Returns

True if it has the same value, otherwise false.

Tensor &AssignValue(const Tensor &tensor)

Assign value to this tensor.

Parameters

tensor[in] The input tensor.

Returns

Tensor with new value.

inline int DataDim() const

Gets tensor’s dimension.

Returns

The number of dimensions of the tensor data.

inline size_t DataSize() const

Getting tensor data size.

Returns

The total number of elements of the tensor data.

inline int data_type_c() const

Get the data type of the tensor for C++.

Returns

[int] The tensor’s data type will be cast to int to return.

inline ShapeVector shape_c(void) const

Get the tensor’s shape for C++.

Returns

[ShapeVector]

inline void *data_c()

Get Tensor data pointer for c++ type.

Returns

The pointer to the object

inline size_t Size() const

Get Tensor data byte-size for c++ type.

Returns

byte size of Tensor data

inline void *data_c() const

The pointer to the object.

void data_sync(bool need_wait = true) const

To synchronize data with the device, you need to wait for the data to be valid.

inline TensorData &data()

Get the internal data object.

Returns

The reference to internal data object.

inline const TensorDataPtr &data_ptr() const

Get the internal data shared pointer.

return The reference to internal data object.

inline const TensorData &data() const

Get the internal data object.

Returns

The reference to internal data object.

std::string GetShapeAndDataTypeInfo() const

Get information about shape and data type.

Returns

Information about shape and data type.

std::string ToStringInternal(size_t limit_size) const

Get display information of limit size.

Parameters

limit_size[in] The limit size.

Returns

The display information of limit size.

std::string ToStringNoLimit() const

Get display information with unlimited size.

Returns

The display information with unlimited size.

std::string ToString() const override

Get display information of this Tensor.

Returns

The display information of this Tensor.

std::string ToStringRepr() const

Get display information in repr form.

Returns

The display information in repr form.

void CheckShape(const ShapeVector &shape) const

Check the shape of this Tensor.

Parameters

shape[in] The input shape.

inline bool is_init() const

Check if this Tensor is initialized.

Returns

Whether this Tensor is initialized.

inline void set_init_flag(bool flag)

Set the initialization flag of this Tensor.

Parameters

flag[in] Whether this Tensor is initialized.

inline DeviceSyncPtr device_address() const

Get the device address.

Returns

The device address.

inline void set_device_address(const DeviceSyncPtr &device_sync, bool need_update_ref_count = true)

Set the device address.

Parameters
  • device_sync[in] The input Device synchronization.

  • need_update_ref_count[in] If need_update_ref_count is true, the device address cannot be released and reused, so the feature map should set false when set device address of tensor.

inline bool need_release_device_mem() const

Check whether to release device memory.

Returns

Ture if need to release device memory, otherwise false.

inline void set_need_release_device_mem(bool release_device_mem)

Set the flag to determine whether the device memory needs to be released.

Parameters

release_device_mem[in] If release_device_mem is ture, the device memory will to be released.

inline void set_padding_type(const std::string padding_type)

Set the padding type of this Tensor.

Parameters

padding_type[in] The input padding type.

inline std::string padding_type() const

Get the padding type of this Tensor.

Returns

The padding type.

inline std::string id() const

Get the id of this Tensor.

Returns

The id of this Tensor.

inline TypePtr cast_dtype()

Get the cast dtype of this Tensor.

Returns

The cast dtype of this Tensor.

inline void set_cast_dtype(const TypePtr &dtype = nullptr)

Set the cast dtype of this Tensor.

Parameters

dtype[in] The input cast dtype.

inline bool cache_enable() const

Used cache_enable to update the tensor from the cache to the host.

Returns

True if caching is enabled, otherwise false.

inline void set_cache_enable(bool cache_enable = true)

Set cache_enable.

Parameters

cache_enable[in] Whether to enable caching.

inline std::shared_ptr<Tensor> hashmap_tensor_ptr() const

Get the pointer of hashmap tensor.

Returns

The pointer of hashmap tensor.

inline void set_hashmap_tensor_ptr(const std::shared_ptr<Tensor> &hashmap_tensor_ptr = nullptr)

Set the pointer of hashmap tensor.

Parameters

hashmap_tensor_ptr[in] The input pointer of hashmap tensor.

inline std::shared_ptr<Tensor> cache_tensor_ptr() const

Get the pointer of cache tensor.

Returns

The pointer of cache tensor.

inline void set_cache_tensor_ptr(const std::shared_ptr<Tensor> &cache_tensor_ptr = nullptr)

Set the pointer of cache tensor.

Parameters

cache_tensor_ptr[in] The input pointer of cache tensor.

inline void SetNeedWait(bool need_wait)

Set whether the event needs to wait.

Parameters

need_wait[in] Whether the event needs to wait.

inline bool NeedWait() const

Check whether the event needs to wait.

Returns

Whether the event needs to wait.

inline void Wait() const

Require the event to wait.

inline void SetDeviceEvent(const std::shared_ptr<DeviceEvent> &device_event)

Set device event.

Parameters

device_event[in] The input device event.

inline void WaitDevice()

Require the device event to wait.

inline bool NeedWaitDevice() const

Set whether the device needs to wait.

Returns

Whether the device needs to wait.

inline void set_sync_status(TensorSyncStatus sync_status) const

Set synchronization status.

Parameters

sync_status[in] The input synchronization status.

inline TensorSyncStatus sync_status() const

Get synchronization status.

Returns

The synchronization status.

inline bool NeedSyncDeviceToHostImmediately() const

Check the value of sync_status_.

Returns

Ture if sync_status_ is kNeedSyncDeviceToHostImmediately.

inline bool NeedSyncDeviceToHost() const

Check the value of sync_status_.

Returns

Ture if sync_status_ is kNeedSyncDeviceToHost.

inline bool NeedSyncHostToDevice() const

Check the value of sync_status_.

Returns

Ture if sync_status_ is kNeedSyncHostToDevice.

inline bool IsGraphOutput() const

Check if this Tensor is the output of graph.

Returns

Whether this Tensor is the output of graph

inline void SetIsGraphOutput()

Set whether this Tensor is the output of graph.

inline bool IsUpdatedByDevice() const

Get whether this Tensor is updated by the device.

Returns

Whether this Tensor is updated by the device.

inline void SetIsUpdateByDevice()

Set whether this Tensor is updated by the device.