Visible to Intel only — GUID: GUID-57AF9CC8-33BC-4D13-A9EB-8AB62E4CD6A9
Visible to Intel only — GUID: GUID-57AF9CC8-33BC-4D13-A9EB-8AB62E4CD6A9
Memory
Overview
A container that describes and stores data. More…
// typedefs
typedef struct dnnl_memory_desc* dnnl_memory_desc_t;
typedef const struct dnnl_memory_desc* const_dnnl_memory_desc_t;
typedef struct dnnl_memory* dnnl_memory_t;
typedef const struct dnnl_memory* const_dnnl_memory_t;
// enums
enum dnnl_format_kind_t;
enum dnnl_format_tag_t;
// structs
struct dnnl_memory;
struct dnnl_memory_desc;
struct dnnl::memory;
// global functions
bool dnnl::operator == (dnnl_data_type_t a, memory::data_type b);
bool dnnl::operator != (dnnl_data_type_t a, memory::data_type b);
bool dnnl::operator == (memory::data_type a, dnnl_data_type_t b);
bool dnnl::operator != (memory::data_type a, dnnl_data_type_t b);
bool dnnl::operator == (dnnl_format_tag_t a, memory::format_tag b);
bool dnnl::operator != (dnnl_format_tag_t a, memory::format_tag b);
bool dnnl::operator == (memory::format_tag a, dnnl_format_tag_t b);
bool dnnl::operator != (memory::format_tag a, dnnl_format_tag_t b);
dnnl_status_t DNNL_API dnnl_memory_desc_destroy(dnnl_memory_desc_t memory_desc);
dnnl_status_t DNNL_API dnnl_memory_desc_clone(
dnnl_memory_desc_t* memory_desc,
const_dnnl_memory_desc_t existing_memory_desc
);
dnnl_status_t DNNL_API dnnl_memory_desc_create_with_strides(
dnnl_memory_desc_t* memory_desc,
int ndims,
const dnnl_dims_t dims,
dnnl_data_type_t data_type,
const dnnl_dims_t strides
);
dnnl_status_t DNNL_API dnnl_memory_desc_create_with_tag(
dnnl_memory_desc_t* memory_desc,
int ndims,
const dnnl_dims_t dims,
dnnl_data_type_t data_type,
dnnl_format_tag_t tag
);
dnnl_status_t DNNL_API dnnl_memory_desc_create_submemory(
dnnl_memory_desc_t* memory_desc,
const_dnnl_memory_desc_t parent_memory_desc,
const dnnl_dims_t dims,
const dnnl_dims_t offsets
);
dnnl_status_t DNNL_API dnnl_memory_desc_reshape(
dnnl_memory_desc_t* out_memory_desc,
const_dnnl_memory_desc_t in_memory_desc,
int ndims,
const dnnl_dims_t dims
);
dnnl_status_t DNNL_API dnnl_memory_desc_permute_axes(
dnnl_memory_desc_t* out_memory_desc,
const_dnnl_memory_desc_t in_memory_desc,
const int* permutation
);
dnnl_status_t DNNL_API dnnl_memory_desc_query(
const_dnnl_memory_desc_t memory_desc,
dnnl_query_t what,
void* result
);
int DNNL_API dnnl_memory_desc_equal(
const_dnnl_memory_desc_t lhs,
const_dnnl_memory_desc_t rhs
);
size_t DNNL_API dnnl_memory_desc_get_size(const_dnnl_memory_desc_t memory_desc);
size_t DNNL_API dnnl_data_type_size(dnnl_data_type_t data_type);
dnnl_status_t DNNL_API dnnl_memory_create(
dnnl_memory_t* memory,
const_dnnl_memory_desc_t memory_desc,
dnnl_engine_t engine,
void* handle
);
dnnl_status_t DNNL_API dnnl_memory_get_memory_desc(
const_dnnl_memory_t memory,
const_dnnl_memory_desc_t* memory_desc
);
dnnl_status_t DNNL_API dnnl_memory_get_engine(
const_dnnl_memory_t memory,
dnnl_engine_t* engine
);
dnnl_status_t DNNL_API dnnl_memory_map_data(
const_dnnl_memory_t memory,
void** mapped_ptr
);
dnnl_status_t DNNL_API dnnl_memory_unmap_data(
const_dnnl_memory_t memory,
void* mapped_ptr
);
dnnl_status_t DNNL_API dnnl_memory_get_data_handle(
const_dnnl_memory_t memory,
void** handle
);
dnnl_status_t DNNL_API dnnl_memory_set_data_handle(
dnnl_memory_t memory,
void* handle
);
dnnl_status_t DNNL_API dnnl_memory_destroy(dnnl_memory_t memory);
// macros
#define DNNL_MEMORY_ALLOCATE
#define DNNL_MEMORY_NONE
#define DNNL_RUNTIME_DIM_VAL
#define DNNL_RUNTIME_F32_VAL
#define DNNL_RUNTIME_S32_VAL
#define DNNL_RUNTIME_SIZE_VAL
Detailed Documentation
A container that describes and stores data.
Memory objects can contain data of various types and formats. There are two levels of abstraction:
Memory descriptor engine-agnostic logical description of data (number of dimensions, dimension sizes, and data type), and, optionally, the information about the physical format of data in memory. If this information is not known yet, a memory descriptor can be created with dnnl::memory::format_tag::any. This allows compute-intensive primitives to choose the best format for computation. The user is responsible for reordering the data into the chosen format when formats do not match.
A memory descriptor can be initialized either by specifying dimensions and a memory format tag or strides for each of them, or by manipulating the dnnl_memory_desc_t structure directly.
WARNING:The latter approach requires understanding how the physical data representation is mapped to the structure and is discouraged. This topic is discussed in Understanding Memory Formats.The user can query the amount of memory required by a memory descriptor using the dnnl::memory::desc::get_size() function. The size of data in general cannot be computed as the product of dimensions multiplied by the size of the data type. So users are required to use this function for better code portability.
Two memory descriptors can be compared using the equality and inequality operators. The comparison is especially useful when checking whether it is necessary to reorder data from the user’s data format to a primitive’s format.
Memory object an engine-specific object that handles the memory buffer and its description (a memory descriptor). For the CPU engine or with USM, the memory buffer handle is simply a pointer to void. The memory buffer can be queried using dnnl::memory::get_data_handle() and set using dnnl::memory::set_data_handle(). The underlying SYCL buffer, when used, can be queried using dnnl::sycl_interop::get_buffer and set using dnnl::sycl_interop::set_buffer. A memory object can also be queried for the underlying memory descriptor and for its engine using dnnl::memory::get_desc() and dnnl::memory::get_engine().
Along with ordinary memory descriptors with all dimensions being positive, the library supports zero-volume memory descriptors with one or more dimensions set to zero. This is used to support the NumPy* convention. If a zero-volume memory is passed to a primitive, the primitive typically does not perform any computations with this memory. For example:
A concatenation primitive would ignore all memory object with zeroes in the concat dimension / axis.
A forward convolution with a source memory object with zero in the minibatch dimension would always produce a destination memory object with a zero in the minibatch dimension and perform no computations.
However, a forward convolution with a zero in one of the weights dimensions is ill-defined and is considered to be an error by the library because there is no clear definition of what the output values should be.
Memory buffer of a zero-volume memory is never accessed.
Typedefs
typedef struct dnnl_memory_desc* dnnl_memory_desc_t
A memory descriptor handle.
typedef const struct dnnl_memory_desc* const_dnnl_memory_desc_t
A memory descriptor handle.
typedef struct dnnl_memory* dnnl_memory_t
A memory handle.
typedef const struct dnnl_memory* const_dnnl_memory_t
A constant memory handle.
Global Functions
dnnl_status_t DNNL_API dnnl_memory_desc_destroy(dnnl_memory_desc_t memory_desc)
Destroys a memory descriptor.
Parameters:
memory_desc |
Memory descriptor to destroy. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_clone(
dnnl_memory_desc_t* memory_desc,
const_dnnl_memory_desc_t existing_memory_desc
)
Clones a memory descriptor.
The resulting memory descriptor must be destroyed separately.
Parameters:
memory_desc |
Output memory descriptor. |
existing_memory_desc |
Memory descriptor to clone. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_create_with_strides(
dnnl_memory_desc_t* memory_desc,
int ndims,
const dnnl_dims_t dims,
dnnl_data_type_t data_type,
const dnnl_dims_t strides
)
Creates a memory descriptor using dimensions and strides.
Parameters:
memory_desc |
Output memory descriptor. |
ndims |
Number of dimensions |
dims |
Array of dimensions. |
data_type |
Elements data type. |
strides |
Strides in each dimension. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_create_with_tag(
dnnl_memory_desc_t* memory_desc,
int ndims,
const dnnl_dims_t dims,
dnnl_data_type_t data_type,
dnnl_format_tag_t tag
)
Creates a memory descriptor using dimensions and memory format tag.
Parameters:
memory_desc |
Output memory descriptor. |
ndims |
Number of dimensions |
dims |
Array of dimensions. |
data_type |
Elements data type. |
tag |
Memory format tag. Can be dnnl_format_tag_any which would allow a primitive to chose the final memory format. In this case the format_kind field of the memory descriptor would be set to dnnl_format_kind_any. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_create_submemory(
dnnl_memory_desc_t* memory_desc,
const_dnnl_memory_desc_t parent_memory_desc,
const dnnl_dims_t dims,
const dnnl_dims_t offsets
)
Creates a memory descriptor for a region inside an area described by an existing memory descriptor.
Parameters:
memory_desc |
Output memory descriptor. |
parent_memory_desc |
An existing memory descriptor. |
dims |
Sizes of the region. |
offsets |
Offsets to the region from the encompassing memory object in each dimension |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_reshape(
dnnl_memory_desc_t* out_memory_desc,
const_dnnl_memory_desc_t in_memory_desc,
int ndims,
const dnnl_dims_t dims
)
Creates a memory descriptor by reshaping an existing one.
The new memory descriptor inherits the data type. This operation is valid only for memory descriptors that have format_kind dnnl_blocked or dnnl_format_kind_any.
The resulting memory descriptor must be destroyed separately.
The operation ensures the transformation of the physical memory format corresponds to the transformation of the logical dimensions. If such transformation is impossible, the function returns dnnl_invalid_arguments.
The reshape operation can be described as a combination of the following basic operations:
Add a dimension of size 1. This is always possible.
Remove a dimension of size 1. This is possible only if the dimension has no padding (i.e. padded_dims[dim] == dims[dim] && dims[dim] == 1).
Split a dimension into multiple ones. This is possible only if the size of the dimension is exactly equal to the product of the split ones and the dimension does not have padding (i.e. padded_dims[dim] = dims[dim]).
Joining multiple consecutive dimensions into a single one. As in the cases above, this requires that the dimensions do not have padding and that the memory format is such that in physical memory these dimensions are dense and have the same order as their logical counterparts. This also assumes that these dimensions are not blocked.
Here, dense means: stride for dim[i] == (stride for dim[i + 1]) * dim[i + 1];
And same order means: i < j if and only if stride for dim[j] <= stride for dim[i].
Parameters:
out_memory_desc |
Output memory descriptor. |
in_memory_desc |
An existing memory descriptor. Must have format_kind set to dnnl_blocked or dnnl_format_kind_any. |
ndims |
Number of dimensions for the output memory descriptor. |
dims |
Dimensions for the output memory descriptor. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_permute_axes(
dnnl_memory_desc_t* out_memory_desc,
const_dnnl_memory_desc_t in_memory_desc,
const int* permutation
)
Creates a memory descriptor by permuting axes in an existing one.
The physical memory layout representation is adjusted accordingly to maintain the consistency between the logical and physical parts of the memory descriptor.
The resulting memory descriptor must be destroyed separately.
The new memory descriptor inherits the data type. This operation is valid only for memory descriptors that have format_kind set to dnnl_blocked or dnnl_format_kind_any.
The logical axes will be permuted in the following manner:
for (i: 0 .. in_memory_desc->ndims)
out_memory_desc->dims[permutation[i]] = in_memory_desc->dims[i];
Example:
dnnl_memory_desc_t in_md, out_md, expect_out_md;
const int permutation[] = {1, 0}; // swap the first and the second axes
dnnl_dims_t in_dims = {2, 3}, out_dims = {3, 2};
dnnl_format_tag_t in_tag = dnnl_ab, out_tag = dnnl_ba;
dnnl_memory_desc_create_with_tag(
&in_md, 2, in_dims, data_type, in_tag);
dnnl_memory_desc_create_with_tag(
&expect_out_md, 2, out_dims, data_type, out_tag);
dnnl_memory_desc_permute_axes(&out_md, in_md, permutation);
assert(dnnl_memory_desc_equal(out_md, expect_out_md));
dnnl_memory_desc_destroy(in_md);
dnnl_memory_desc_destroy(out_md);
dnnl_memory_desc_destroy(expect_out_md);
Parameters:
out_memory_desc |
Output memory descriptor. |
in_memory_desc |
An existing memory descriptor. Must have format_kind set to dnnl_blocked or dnnl_format_kind_any. |
permutation |
Axes permutation (of size in_memory_desc->ndims). |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_desc_query(
const_dnnl_memory_desc_t memory_desc,
dnnl_query_t what,
void* result
)
Queries a memory descriptor for various pieces of information.
The following information can be queried:
Number of dimensions (dnnl_query_ndims_s32)
Dimensions (dnnl_query_dims) in the following order:
CNN data tensors: mini-batch, channel, spatial ({N, C, [[D,] H,] W})
CNN weight tensors: group (optional), output channel, input channel, spatial ({[G,] O, I, [[D,] H,] W})
RNN data tensors: time, mini-batch, channels ({T, N, C}) or layers, directions, states, mini-batch, channels ({L, D, S, N, C})
RNN weight tensor: layers, directions, input channel, gates, output channels ({L, D, I, G, O})
Data type of the tensor elements (dnnl_query_data_type)
Padded dimensions (dnnl_query_padded_dims) - size of the data including padding in each dimension
Padded offsets (dnnl_query_padded_offsets) - per-dimension offset from the padding to actual data, the top-level tensor with offsets applied must lie within the padding area.
Submemory offset (dnnl_query_submemory_offset_s64) - offset from memory origin to the current block, non-zero only in a description of a memory sub-block.
Format kind (dnnl_query_format_kind) - memory format kind
The following queries are applicable only to format kind dnnl_blocked.
Strides (dnnl_query_strides) between the outermost blocks or in case of plain (non-blocked) formats the strides between dimensions
Number of innermost blocks (dnnl_query_inner_nblks_s32), e.g. {4, 16, 4} in case of OIhw_4i16o4i
Size of the innermost blocks (dnnl_query_inner_blks), e.g. 3 in case of OIhw_4i16o4i_
Logical indices of the blocks (dnnl_query_inner_idxs), e.g. {1, 0, 1} in case of 4i16o4i, because i is the 1st dim and o is the 0st dim
Parameters:
memory_desc |
Memory descriptor. |
what |
Parameter to query. |
result |
Output result. The type depends on the query. For example, it must be a dnnl_dims_t** if querying for a strides. |
Returns:
dnnl_success on success and a status describing the error otherwise.
int DNNL_API dnnl_memory_desc_equal(
const_dnnl_memory_desc_t lhs,
const_dnnl_memory_desc_t rhs
)
Compares two memory descriptors.
Use this function to identify whether a reorder is required between the two memories
Parameters:
lhs |
Left-hand side of the comparison. |
rhs |
Right-hand side of the comparison. |
Returns:
1 if the descriptors are the same.
0 if the descriptors are different.
size_t DNNL_API dnnl_memory_desc_get_size(const_dnnl_memory_desc_t memory_desc)
Returns the size of a memory descriptor.
Parameters:
memory_desc |
Memory descriptor. |
Returns:
The number of bytes required for memory described by a memory descriptor.
size_t DNNL_API dnnl_data_type_size(dnnl_data_type_t data_type)
Returns the size of data type.
Parameters:
data_type |
Data type. |
Returns:
The number of bytes occupied by data type.
dnnl_status_t DNNL_API dnnl_memory_create(
dnnl_memory_t* memory,
const_dnnl_memory_desc_t memory_desc,
dnnl_engine_t engine,
void* handle
)
Creates a memory object.
Unless handle is equal to DNNL_MEMORY_NONE, the constructed memory object will have the underlying buffer set. In this case, the buffer will be initialized as if dnnl_memory_set_data_handle() had been called.
Parameters:
memory |
Output memory object. |
memory_desc |
Memory descriptor. |
engine |
Engine to use. |
handle |
Handle of the memory buffer to use as an underlying storage.
|
Returns:
dnnl_success on success and a status describing the error otherwise.
See also:
dnnl_status_t DNNL_API dnnl_memory_get_memory_desc(
const_dnnl_memory_t memory,
const_dnnl_memory_desc_t* memory_desc
)
Returns the memory descriptor for a memory object.
Parameters:
memory |
Memory object. |
memory_desc |
Output memory descriptor (a copy). |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_get_engine(
const_dnnl_memory_t memory,
dnnl_engine_t* engine
)
Returns the engine of a memory object.
Parameters:
memory |
Memory object. |
engine |
Output engine on which the memory is located. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_map_data(
const_dnnl_memory_t memory,
void** mapped_ptr
)
Maps a memory object and returns a host-side pointer to a memory buffer with a copy of its contents.
Mapping enables explicit direct access to memory contents for the engines that do not support it implicitly.
Mapping is an exclusive operation - a memory object cannot be used in other operations until this memory object is unmapped.
Parameters:
memory |
Memory object. |
mapped_ptr |
Output pointer to the mapped buffer. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_unmap_data(
const_dnnl_memory_t memory,
void* mapped_ptr
)
Unmaps a memory object and writes back any changes made to the previously mapped memory buffer.
The pointer to the mapped buffer must be obtained via the dnnl_memory_map_data() call.
Parameters:
memory |
Memory object. |
mapped_ptr |
Pointer to the mapped buffer that must have been obtained using the dnnl_memory_map_data() function. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_get_data_handle(
const_dnnl_memory_t memory,
void** handle
)
Returns memory object’s data handle.
Parameters:
memory |
Memory object. |
handle |
Output data handle. For the CPU engine, the data handle is a pointer to the actual data. For OpenCL it is a cl_mem. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_set_data_handle(
dnnl_memory_t memory,
void* handle
)
Sets the underlying memory buffer.
Parameters:
memory |
Memory object. |
handle |
Data handle. For the CPU engine or when USM is used, the memory buffer is a pointer to the actual data. For OpenCL it is a cl_mem. |
Returns:
dnnl_success on success and a status describing the error otherwise.
dnnl_status_t DNNL_API dnnl_memory_destroy(dnnl_memory_t memory)
Destroys a memory object.
Parameters:
memory |
Memory object to destroy. |
Returns:
dnnl_success on success and a status describing the error otherwise.
Macros
#define DNNL_MEMORY_ALLOCATE
Special pointer value that indicates that the library needs to allocate an underlying buffer for a memory object.
#define DNNL_MEMORY_NONE
Special pointer value that indicates that a memory object should not have an underlying buffer.
#define DNNL_RUNTIME_DIM_VAL
A wildcard value for dimensions that are unknown at a primitive creation time.
#define DNNL_RUNTIME_F32_VAL
A wildcard value for floating point values that are unknown at a primitive creation time.
#define DNNL_RUNTIME_S32_VAL
A wildcard value for int32_t values that are unknown at a primitive creation time.
#define DNNL_RUNTIME_SIZE_VAL
A size_t counterpart of the DNNL_RUNTIME_DIM_VAL.
For instance, this value is returned by dnnl_memory_desc_get_size() if either of the dimensions or strides equal to DNNL_RUNTIME_DIM_VAL.