|
Mila
Deep Neural Network Library
|
Tensor transfer utilities – copy/dispatch helpers for tensor data movement. More...
#include <concepts>#include <string>#include <memory>#include <span>#include <type_traits>#include <stdexcept>import Serialization.Tensor;import Compute.CpuMemoryResource;import Compute.IExecutionContext;import Compute.ExecutionContext;import Compute.DeviceType;import Compute.DeviceId;import Compute.Device;import Dnn.TensorDataTypeMap;import Dnn.TensorOps.Base;import Dnn.TensorDataTypeTraits;import Dnn.TensorDataType;import Dnn.Tensor;Namespaces | |
| namespace | Mila |
| Mila main API namespace. | |
Functions | |
| template<TensorDataType TSrcDataType, typename TSrcMemoryResource, TensorDataType TDstDataType, typename TDstMemoryResource> requires isValidTensor<TSrcDataType, TSrcMemoryResource> && isValidTensor<TDstDataType, TDstMemoryResource> | |
| void | Mila::Dnn::copy (const Tensor< TSrcDataType, TSrcMemoryResource > &src, Tensor< TDstDataType, TDstMemoryResource > &dst, IExecutionContext *exec_context=nullptr) |
| Copies tensor data from source to destination tensor with optional ExecutionContext. | |
|
template<TensorDataType TDstDataType, typename TDstMemoryResource> requires isValidTensor<TDstDataType, TDstMemoryResource> | |
| void | Mila::Dnn::copyFromBlob (const Serialization::ITensorBlob &blob, Tensor< TDstDataType, TDstMemoryResource > &dst, IExecutionContext *exec_context=nullptr) |
| template<TensorDataType TSrcDataType, TensorDataType TDstDataType, typename TDstMemoryResource> requires isValidTensor<TDstDataType, TDstMemoryResource> | |
| void | Mila::Dnn::copyFromBlobWithConversion (const Serialization::ITensorBlob &blob, Tensor< TDstDataType, TDstMemoryResource > &dst, IExecutionContext *exec_context=nullptr) |
| Copy a serialized blob into a destination tensor, converting element types. | |
| template<TensorDataType TDstDataType, TensorDataType TSrcDataType, typename TSrcMemoryResource> requires isValidTensor<TSrcDataType, TSrcMemoryResource> && isValidTensor<TDstDataType, CpuMemoryResource> | |
| Tensor< TDstDataType, CpuMemoryResource > | Mila::Dnn::toHost (const Tensor< TSrcDataType, TSrcMemoryResource > &src, IExecutionContext *exec_context=nullptr) |
| Create a host (CPU) tensor from src and copy data into it. | |
Tensor transfer utilities – copy/dispatch helpers for tensor data movement.
Provides an exported generic copy() template that validates shapes, handles host/device accessibility, and dispatches to device-specific TensorOps implementations. Supports an optional execution context for stream control.
|
export |
Copies tensor data from source to destination tensor with optional ExecutionContext.
Transfers data from source tensor to pre-allocated destination tensor. Both tensors must have compatible shapes (same dimensions). Supports type conversion and cross-device transfers with explicit stream control.
Device compatibility rules:
ExecutionContext handling:
| TSrcDataType | Source tensor data type |
| TSrcMemoryResource | Source memory resource type |
| TDstDataType | Destination tensor data type |
| TDstMemoryResource | Destination memory resource type |
| src | Source tensor to copy from |
| dst | Destination tensor to copy to (must be pre-allocated) |
| exec_context | Optional execution context for stream control (borrowed, not owned) |
| std::runtime_error | If device-only tensors are on incompatible device types |
Example:
|
export |
Copy a serialized blob into a destination tensor, converting element types.
Intended for quantize-on-load paths where the checkpoint dtype (TSrcDataType) differs from the weight storage dtype (TDstDataType). Shape is validated against the destination tensor. Dispatches to the device-specific backend.
| TSrcDataType | Blob element dtype (e.g. BF16). |
| TDstDataType | Destination tensor dtype (e.g. FP8_E4M3). |
| TDstMemoryResource | Destination memory resource. |
| blob | Source tensor blob. |
| dst | Pre-allocated destination tensor. |
| exec_context | Optional execution context for stream control (borrowed). |
| std::invalid_argument | if blob shape != dst shape. |
|
export |
Create a host (CPU) tensor from src and copy data into it.
By default the destination data type matches the source data type. The destination tensor preserves the source shape. An optional execution context may be supplied for device-side stream control when the source is device-resident.
| TSrcDataType | Source tensor data type |
| TSrcMemoryResource | Source memory resource type |
| TDstDataType | Destination tensor data type (defaults to source type) |
| src | Source tensor to copy from |
| exec_context | Optional execution context for stream control (borrowed) |