.. index:: pair: struct; dnnl::memory::desc .. _doxid-structdnnl_1_1memory_1_1desc: struct dnnl::memory::desc ========================= .. toctree:: :hidden: Overview ~~~~~~~~ A memory descriptor. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include struct desc { // fields :ref:`dnnl_memory_desc_t` :ref:`data`; // construction :ref:`desc`(); :ref:`desc`( const :ref:`dims`& adims, :ref:`data_type` adata_type, :ref:`format_tag` aformat_tag, bool allow_empty = false ); :ref:`desc`( const :ref:`dims`& adims, :ref:`data_type` adata_type, const :ref:`dims`& strides, bool allow_empty = false ); :ref:`desc`(const :ref:`dnnl_memory_desc_t`& data); // methods desc :ref:`submemory_desc`( const :ref:`dims`& adims, const :ref:`dims`& offsets, bool allow_empty = false ) const; desc :ref:`reshape`(const :ref:`dims`& adims, bool allow_empty = false) const; desc :ref:`permute_axes`(const std::vector& permutation, bool allow_empty = false) const; :ref:`memory::data_type` :ref:`data_type`() const; :ref:`memory::dims` :ref:`dims`() const; size_t :ref:`get_size`() const; bool :ref:`is_zero`() const; bool :ref:`operator ==` (const desc& other) const; bool :ref:`operator !=` (const desc& other) const; :ref:`operator bool` () const; }; .. _details-structdnnl_1_1memory_1_1desc: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ A memory descriptor. Fields ------ .. index:: pair: variable; data .. _doxid-structdnnl_1_1memory_1_1desc_1afea646e8777c2341509a61f4ae17dcf3: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`dnnl_memory_desc_t` data The underlying C API data structure. Construction ------------ .. index:: pair: function; desc .. _doxid-structdnnl_1_1memory_1_1desc_1a2a12f9b43aae8c214d695b321b543b5c: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc() Constructs a zero (empty) memory descriptor. Such a memory descriptor can be used to indicate absence of an argument. .. index:: pair: function; desc .. _doxid-structdnnl_1_1memory_1_1desc_1a03f068d5a2e5b2d043d2f203717a0ceb: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc( const :ref:`dims`& adims, :ref:`data_type` adata_type, :ref:`format_tag` aformat_tag, bool allow_empty = false ) Constructs a memory descriptor. .. note:: The logical order of dimensions corresponds to the ``abc...`` format tag, and the physical meaning of the dimensions depends both on the primitive that would operate on this memory and the operation context. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - adims - Tensor dimensions. * - adata_type - Data precision/type. * - aformat_tag - Memory format tag. * - allow_empty - A flag signifying whether construction is allowed to fail without throwing an exception. In this case a zero memory descriptor will be constructed. This flag is optional and defaults to false. .. index:: pair: function; desc .. _doxid-structdnnl_1_1memory_1_1desc_1ac0471538db2d230492a07356929c859c: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc( const :ref:`dims`& adims, :ref:`data_type` adata_type, const :ref:`dims`& strides, bool allow_empty = false ) Constructs a memory descriptor by strides. .. note:: The logical order of dimensions corresponds to the ``abc...`` format tag, and the physical meaning of the dimensions depends both on the primitive that would operate on this memory and the operation context. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - adims - Tensor dimensions. * - adata_type - Data precision/type. * - strides - Strides for each dimension. * - allow_empty - A flag signifying whether construction is allowed to fail without throwing an exception. In this case a zero memory descriptor will be constructed. This flag is optional and defaults to false. .. index:: pair: function; desc .. _doxid-structdnnl_1_1memory_1_1desc_1adc5a3eb4dbd8aef650bf6823f13167e3: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc(const :ref:`dnnl_memory_desc_t`& data) Constructs a memory descriptor from a C API data structure. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - data - A C API :ref:`dnnl_memory_desc_t ` structure. Methods ------- .. index:: pair: function; submemory_desc .. _doxid-structdnnl_1_1memory_1_1desc_1a7de2abef3b34e94c5dfa16e1fc3f3aab: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc submemory_desc( const :ref:`dims`& adims, const :ref:`dims`& offsets, bool allow_empty = false ) const Constructs a memory descriptor for a region inside an area described by this memory descriptor. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - adims - Sizes of the region. * - offsets - Offsets to the region from the encompassing memory object in each dimension. * - allow_empty - A flag signifying whether construction is allowed to fail without throwing an exception. In this case a zero memory descriptor will be returned. This flag is optional and defaults to false. .. rubric:: Returns: A memory descriptor for the region. .. index:: pair: function; reshape .. _doxid-structdnnl_1_1memory_1_1desc_1ab95a6fbd16dd8b7c421611d39d49fe3f: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc reshape(const :ref:`dims`& adims, bool allow_empty = false) const Constructs 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 set to :ref:`dnnl::memory::format_kind::blocked ` or :ref:`dnnl::memory::format_kind::any `. The operation ensures that the transformation of the physical memory format corresponds to the transformation of the logical dimensions. If such transformation is impossible, the function either throws an exception (default) or returns a zero memory descriptor depending on the ``allow_empty`` flag. 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 product of all tensor dimensions stays constant and the dimension being split does not have padding (i.e. ``padded_dims[dim] = dims[dim]``). #. Join 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]``. .. warning:: Some combinations of physical memory layout and/or offsets or dimensions may result in a failure to make a reshape. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - adims - New dimensions. The product of dimensions must remain constant. * - allow_empty - A flag signifying whether construction is allowed to fail without throwing an exception. In this case a zero memory descriptor will be returned. This flag is optional and defaults to false. .. rubric:: Returns: A new memory descriptor with new dimensions. .. index:: pair: function; permute_axes .. _doxid-structdnnl_1_1memory_1_1desc_1a70e831b990a91d61bc44e57f6e895a83: .. ref-code-block:: cpp :class: doxyrest-title-code-block desc permute_axes(const std::vector& permutation, bool allow_empty = false) const Constructs 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 new memory descriptor inherits the data type. The new memory descriptor inherits the data type. This operation is valid only for memory descriptors that have format_kind set to :ref:`dnnl::memory::format_kind::blocked ` or :ref:`dnnl::memory::format_kind::any `. The logical axes will be permuted in the following manner: .. ref-code-block:: cpp for (i = 0; i < ndims(); i++) new_desc.dims()[permutation[i]] = dims()[i]; Example: .. ref-code-block:: cpp std::vector permutation = {1, 0}; // swap the first and // the second axes dnnl::memory::desc in_md( {2, 3}, data_type, memory::format_tag::ab); dnnl::memory::desc expect_out_md( {3, 2}, data_type, memory::format_tag::ba); assert(in_md.permute_axes(permutation) == expect_out_md); .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - permutation - Axes permutation. * - allow_empty - A flag signifying whether construction is allowed to fail without throwing an exception. In this case a zero memory descriptor will be returned. This flag is optional and defaults to false. .. rubric:: Returns: A new memory descriptor with new dimensions. .. index:: pair: function; data_type .. _doxid-structdnnl_1_1memory_1_1desc_1ab3cb5e5f28d660a0105eb7580e5ab213: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`memory::data_type` data_type() const Returns the data type of the memory descriptor. .. rubric:: Returns: The data type. .. index:: pair: function; dims .. _doxid-structdnnl_1_1memory_1_1desc_1aaf95c174b7d7f5ea59646886719d0a2b: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`memory::dims` dims() const Returns dimensions of the memory descriptor. Potentially expensive due to the data copy involved. .. rubric:: Returns: A copy of the dimensions vector. .. index:: pair: function; get_size .. _doxid-structdnnl_1_1memory_1_1desc_1ac20108bc192912382aa4a95ae27df804: .. ref-code-block:: cpp :class: doxyrest-title-code-block size_t get_size() const Returns size of the memory descriptor in bytes. .. rubric:: Returns: The number of bytes required to allocate a memory buffer for the memory object described by this memory descriptor including the padding area. .. index:: pair: function; is_zero .. _doxid-structdnnl_1_1memory_1_1desc_1aa162a1ba5621a799c8a909c726f021a2: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool is_zero() const Checks whether the memory descriptor is zero (empty). .. rubric:: Returns: ``true`` if the memory descriptor describes an empty memory and ``false`` otherwise. .. index:: pair: function; operator== .. _doxid-structdnnl_1_1memory_1_1desc_1a9d623dab6f8a8ebc34b0da95814e3728: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool operator == (const desc& other) const An equality operator. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - other - Another memory descriptor. .. rubric:: Returns: Whether this and the other memory descriptors have the same format tag, dimensions, strides, blocking, etc. .. index:: pair: function; operator!= .. _doxid-structdnnl_1_1memory_1_1desc_1a5a1bc8d3b88a2c304fd9535ea45fbb72: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool operator != (const desc& other) const An inequality operator. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - other - Another memory descriptor. .. rubric:: Returns: Whether this and the other memory descriptors describe different memory. .. index:: pair: function; operator bool .. _doxid-structdnnl_1_1memory_1_1desc_1adf628c4bec9060fa2e779ca148a32956: .. ref-code-block:: cpp :class: doxyrest-title-code-block operator bool () const Checks whether the object is not empty. .. rubric:: Returns: Whether the object is not empty.