Skip to content
Snippets Groups Projects

Add transpose function for 4D tensors and related templates

Merged Wissam Boussella requested to merge wboussella/aidge_export_cpp:transpose_op into dev

Context

New transpose OP for export_cpp

Modified files

  • transpose.hpp new kernel transpose;
  • operator.py, to take in case an export of node Transpose;
  • transpose_ND_forward.jinja and 'transpose_ND_config.jinja' to generate the code.

TODO

  • Unit-Tests : waiting for the feature to compare results between the export, onnx_runtime and aidge from @pineapple
Edited by Cyril Moineau

Merge request reports

Merge request pipeline #67214 passed

Merge request pipeline passed for b7b395f3

Test coverage 35.00% (0.00%) from 1 job
Code Quality is loading
Test summary results are being parsed

Merged by Cyril MoineauCyril Moineau 2 months ago (Mar 5, 2025 8:56am UTC)

Loading

Pipeline #67216 passed

Pipeline passed for f3a546b9 on dev

Test coverage 35.00% (0.00%) from 1 job

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
12 * dimensions reordered as specified by the permute array.
13 *
14 * @tparam T Data type of the tensor elements (e.g., float, double).
15 * @param[in] inputs Pointer to the input tensor data stored in contiguous memory.
16 * @param[out] outputs Pointer to the pre-allocated memory for the transposed tensor.
17 * Ensure this memory is appropriately sized to hold the transposed data.
18 * @param[in] in_dim1 Size of the first dimension of the input tensor.
19 * @param[in] in_dim2 Size of the second dimension of the input tensor.
20 * @param[in] in_dim3 Size of the third dimension of the input tensor.
21 * @param[in] in_dim4 Size of the fourth dimension of the input tensor.
22 * @param[in] permute Array of four unsigned integers specifying the desired permutation
23 * of dimensions. Each value should be in the range [0, 3], defining
24 * the new order of dimensions for the output tensor.
25 */
26 template <typename T>
27 void transpose_4D_forward(const T* inputs,
  • 18 * @param[in] in_dim1 Size of the first dimension of the input tensor.
    19 * @param[in] in_dim2 Size of the second dimension of the input tensor.
    20 * @param[in] in_dim3 Size of the third dimension of the input tensor.
    21 * @param[in] in_dim4 Size of the fourth dimension of the input tensor.
    22 * @param[in] permute Array of four unsigned integers specifying the desired permutation
    23 * of dimensions. Each value should be in the range [0, 3], defining
    24 * the new order of dimensions for the output tensor.
    25 */
    26 template <typename T>
    27 void transpose_4D_forward(const T* inputs,
    28 T* outputs,
    29 unsigned int in_dim1,
    30 unsigned int in_dim2,
    31 unsigned int in_dim3,
    32 unsigned int in_dim4,
    33 const unsigned int* permute)
  • 12 * dimensions reordered as specified by the permute array.
    13 *
    14 * @tparam T Data type of the tensor elements (e.g., float, double).
    15 * @param[in] inputs Pointer to the input tensor data stored in contiguous memory.
    16 * @param[out] outputs Pointer to the pre-allocated memory for the transposed tensor.
    17 * Ensure this memory is appropriately sized to hold the transposed data.
    18 * @param[in] in_dim1 Size of the first dimension of the input tensor.
    19 * @param[in] in_dim2 Size of the second dimension of the input tensor.
    20 * @param[in] in_dim3 Size of the third dimension of the input tensor.
    21 * @param[in] in_dim4 Size of the fourth dimension of the input tensor.
    22 * @param[in] permute Array of four unsigned integers specifying the desired permutation
    23 * of dimensions. Each value should be in the range [0, 3], defining
    24 * the new order of dimensions for the output tensor.
    25 */
    26 template <typename T>
    27 void transpose_4D_forward(const T* inputs,
  • 26 template <typename T>
    27 void transpose_4D_forward(const T* inputs,
    28 T* outputs,
    29 unsigned int in_dim1,
    30 unsigned int in_dim2,
    31 unsigned int in_dim3,
    32 unsigned int in_dim4,
    33 const unsigned int* permute)
    34 {
    35 unsigned int in_dims[4] = {in_dim1, in_dim2, in_dim3, in_dim4};
    36 unsigned int out_dims[4];
    37 for (unsigned int i = 0; i < 4; ++i) {
    38 out_dims[i] = in_dims[permute[i]];
    39 }
    40
    41 unsigned int in_strides[4] = {
  • 295 295 str(ROOT / "kernels" / "activation.hpp"),
    296 296 str(ROOT / "kernels" / "rescaling.hpp")
    297 297 ]
    298
    299 @ExportLibCpp.register("Transpose", aidge_core.ImplSpec(aidge_core.IOSpec(aidge_core.dtype.float32)))
  • The proposed kernel does not leverage the fact that it is an export. I added some proposition to enhance it. You may want to try to benchmark your kernel before and after the changes I proposed.

    Dangerous comment of a function withtout providing replacement in operators.py!

  • Cyril Moineau requested changes

    requested changes

  • 20 * @param[in] in_dim3 Size of the third dimension of the input tensor.
    21 * @param[in] in_dim4 Size of the fourth dimension of the input tensor.
    22 * @param[in] permute Array of four unsigned integers specifying the desired permutation
    23 * of dimensions. Each value should be in the range [0, 3], defining
    24 * the new order of dimensions for the output tensor.
    25 */
    26 template <typename T>
    27 void transpose_4D_forward(const T* inputs,
    28 T* outputs,
    29 unsigned int in_dim1,
    30 unsigned int in_dim2,
    31 unsigned int in_dim3,
    32 unsigned int in_dim4,
    33 const unsigned int* permute)
    34 {
    35 unsigned int in_dims[4] = {in_dim1, in_dim2, in_dim3, in_dim4};
  • added 1 commit

    • ac47d3e6 - keep transpose in nhwc in operator.py, but need to be changed

    Compare with previous version

  • added 1 commit

    • b7b395f3 - now NB_DIMS is tezmplate's parameter

    Compare with previous version

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading