Conv3DBackpropInput Instructions

Ascend C provides a group of Conv3DBackpropInput high-level APIs for users to quickly implement convolution inverse operations and solve backpropagation errors. The transposed convolution Conv3DTranspose has the same mathematical process as Conv3DBackpropInput. Therefore, you can also use the Conv3DBackpropInput high-order API to implement the transposed convolution operator. Figure 1 Forward and backward propagation of the convolutional layer shows the forward and backward propagation of the convolutional layer. Figure 2 Backpropagation error calculation shows how to calculate the backpropagation error.

The formula for Conv3DBackpropInput is as follows:

  • ∂L/∂Y is the gradient GradOutput of the forward convolution loss function with respect to the output Y, and is used as the input for calculating the backpropagation error ∂L/∂X.
  • W is the forward convolution weight, that is, the matrix kernel, which is also the filter. It is used as the input for calculating the backpropagation error ∂L/∂X. WT represents the transpose of W.
  • ∂L/∂X is the backpropagation error GradInput of the feature matrix.
Figure 1 Forward and backward propagation of the convolutional layer
Figure 2 Backpropagation error calculation

The procedure for implementing the backpropagation error calculation of Conv3DBackpropInput on the kernel side is as follows:

  1. Create a Conv3DBackpropInput object.
  2. Perform the initialization operation.
  3. Set the output gradient GradOutput and input weight of the convolution.
  4. Perform the convolution backpropagation operation.
  5. Finalize the convolution backpropagation operation.

    The M-axis direction mentioned below is the vertical direction of the GradOutput matrix, the K-axis direction is the horizontal direction of the GradOutput matrix or the vertical direction of the Weight matrix, and the N-axis direction is the horizontal direction of the Weight matrix.

The procedure for using the high-level API Conv3DBackpropInput to calculate the backpropagation error is as follows:

  1. Create a Conv3DBackpropInput object.
    1
    2
    3
    4
    5
    6
    7
    #include "lib/conv_backprop/conv3d_bp_input_api.h"
    
    using weightDxType = ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::FRACTAL_Z_3D, weightType>;
    using inputSizeDxType =	ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::ND, int32_t>;
    using gradOutputDxType = ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::NDC1HWC0, gradOutputType>;
    using gradInputDxType = ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::NCDHW, gradInputType>;
    ConvBackpropApi::Conv3DBackpropInput<weightDxType, inputSizeDxType, gradOutputDxType, gradInputDxType> gradInput_;
    

    During object creation, input the weight matrix Weight, the shape information InputSize of the convolution forward feature matrix Input, and the parameter type information of GradOutput and GradInput. The type information is defined by ConvType, including the logical location of memory, data format, and data type.

    1
    2
    3
    4
    5
    6
    template <TPosition POSITION, ConvFormat FORMAT, typename T>
    struct ConvType {
        constexpr static TPosition pos = POSITION;    // Logical position of the Convolution input or output
        constexpr static ConvFormat format = FORMAT; // Convolution input or output data format
        using T = TYPE;                               // Convolution input or output data type
    };
    

    The following briefly describes the data structures used for object creation. Developers can selectively understand these content. The data structure used to create a Conv3DBackpropInput object is defined as follows:

    1
    using Conv3DBackpropInput = Conv3DBpInputIntf<Conv3DBpInputCfg<WEIGHT_TYPE, INPUT_TYPE, GRAD_OUTPUT_TYPE, GRAD_INPUT_TYPE, CONV3D_CFG_DEFAULT>, Conv3DBpInputImpl>;
    

    The Conv3DBpInputIntf and Conv3DBpInputCfg data structures are defined as follows:

    1
    2
    3
    template <class Config_, template <typename, class> class Impl>
    struct Conv3DBpInputIntf {
    }
    
    1
    2
    3
    template <class WEIGHT_TYPE, class INPUT_TYPE, class GRAD_OUTPUT_TYPE, class GRAD_INPUT_TYPE, const Conv3dConfig& CONV3D_CONFIG = CONV3D_CFG_DEFAULT>
    struct Conv3DBpInputCfg : public ConvBpContext<WEIGHT_TYPE, INPUT_TYPE, GRAD_OUTPUT_TYPE, GRAD_INPUT_TYPE> {
    }
    
    Table 1 ConvType parameters

    Parameter

    Description

    POSITION

    Logical memory location.

    • This parameter can be set to TPosition::GM for the weight matrix.
    • This parameter can be set to TPosition::GM for the GradOutput matrix.
    • This parameter can be set to TPosition::GM for the InputSize matrix.
    • This parameter can be set to TPosition::GM for the GradInput matrix.

    ConvFormat

    Data format.

    • This parameter can be set to ConvFormat::FRACTAL_Z_3D for the weight matrix.
    • This parameter can be set to ConvFormat::NDC1HWC0 for the GradOutput matrix.
    • This parameter can be set to ConvFormat::ND for the InputSize matrix.
    • This parameter can be set to ConvFormat::NDC1HWC0 for the GradInput matrix.

    TYPE

    Data type.
    • This parameter can be set to half or bfloat16_t for the weight matrix.
    • This parameter can be set to half or bfloat16_t for the GradOutput matrix.
    • This parameter can be set to int32_t for the InputSize matrix.
    • This parameter can be set to half or bfloat16_t for the GradInput matrix.

    Note: The data types of the GradOutput and Weight matrices must be the same. For details about the data type combinations, see Table 2.

    Table 2 Combinations of Conv3DBackpropInput input and output data types

    Weight

    GradOutput

    InputSize

    GradInput

    Supported Platform

    half

    half

    int32_t

    half

    • Atlas A3 training products / Atlas A3 inference products
    • Atlas A2 training products / Atlas A2 inference products

    bfloat16_t

    bfloat16_t

    int32_t

    bfloat16_t

    • Atlas A3 training products / Atlas A3 inference products
    • Atlas A2 training products / Atlas A2 inference products
  2. Perform the initialization operation.
    1
    2
    3
    // Perform initialization after registration.
    ConvBackpropApi::Conv3DBackpropInput<weightDxType, inputSizeDxType, gradOutputDxType, gradInputDxType> gradInput_;
    gradInput_.Init(&(tilingData->conv3DDxTiling));
    
  3. Set the output gradient GradOutput and input weight of the 3D convolution.
    1
    2
    3
    4
    gradInput_.SetSingleShape(singleShapeM_, singleShapeK_, singleShapeN_); // Set the shape of single-core computation.
    gradInput_.SetStartPosition(dinStartIdx_, curHoStartIdx_); // Set the start position for loading gradOutput on the single core.
    gradInput_.SetGradOutput(gradOutputGm_[offsetA_]);
    gradInput_.SetWeight(weightGm_[offsetB_]); 
    
  4. Perform the convolution backpropagation operation.
    Call Iterate to complete a single iterative computation, and use a while loop to compute the full data on a single core. The Iterate method allows for flexible control over the number of iterations required to compute the desired amount of data.
    1
    2
    3
    while (gradInput_.Iterate()) {
        gradInput_.GetTensorC(gradInputGm_[offsetC_]); 
    }
    
  5. Finalize the convolution backpropagation operation.
    1
    gradInput_.End();
    

Header File to Be Included

1
#include "lib/conv_backprop/conv3d_bp_input_api.h"