Skip to content

Commit

Permalink
[Feature](bangc-ops): add pad op
Browse files Browse the repository at this point in the history
  • Loading branch information
gaotianyi544 committed Oct 19, 2023
1 parent 88c2fa7 commit 40186fb
Show file tree
Hide file tree
Showing 13 changed files with 817 additions and 0 deletions.
Binary file modified bangc-ops/kernels/kernel_wrapper/lib/libextops.a
100644 → 100755
Binary file not shown.
9 changes: 9 additions & 0 deletions bangc-ops/kernels/kernel_wrapper/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
const mluOpTensorDescriptor_t, const void *, \
const mluOpTensorDescriptor_t, void *

#define PAD_PARAM_TYPE \
mluOpHandle_t, const mluOpTensorDescriptor_t, \
const void *, \
const void *, \
const void *, \
const mluOpTensorDescriptor_t, \
void *

#define TRANSPOSE_PARAM_TYPE \
mluOpHandle_t, const mluOpTransposeDescriptor_t, \
const mluOpTensorDescriptor_t, const void *, \
Expand Down Expand Up @@ -308,6 +316,7 @@ KERNEL_REGISTER(roiAlignForwardV2, ROIALIGN_FORWARD_V2_PARAM_TYPE);
KERNEL_REGISTER(scatterNd, SCATTER_ND_PARAM_TYPE);
KERNEL_REGISTER(scatterNdV2, SCATTER_ND_V2_PARAM_TYPE);
KERNEL_REGISTER(gatherNd, GATHER_ND_PARAM_TYPE);
KERNEL_REGISTER(pad, PAD_PARAM_TYPE);
KERNEL_REGISTER(transpose, TRANSPOSE_PARAM_TYPE);
KERNEL_REGISTER(transposeV2, TRANSPOSE_V2_PARAM_TYPE);
KERNEL_REGISTER(nms, NMS_PARAM_TYPE);
Expand Down
38 changes: 38 additions & 0 deletions bangc-ops/kernels/pad/pad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*************************************************************************
* Copyright (C) [2023] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#include "kernels/kernel_wrapper/wrapper.h"

mluOpStatus_t MLUOP_WIN_API mluOpPad(
mluOpHandle_t handle,
const mluOpTensorDescriptor_t input_desc,
const void *input,
const void *paddings,
const void *padding_value,
const mluOpTensorDescriptor_t output_desc,
void *output) {
padWrapper wrapper;
mluOpStatus_t ret = wrapper.invoke(
handle, input_desc, input, paddings, padding_value,
output_desc, output);
return ret;
}
85 changes: 85 additions & 0 deletions bangc-ops/mlu_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,91 @@ mluOpAddN(mluOpHandle_t handle,
const mluOpTensorDescriptor_t output_desc,
void *output);

// Group:Pad
/*!
* @brief Pads the input tensor using \b padding_value based on \b paddings that specifies how the tensor is
* padded or cropped. This operation is usually used in ResNet and Transformer networks.
*
* @param[in] handle
* Handle to a Cambricon MLUOP context that is used to manage MLU devices and queues in the pad operation. For
* detailed information, see ::mluOpHandle_t.
* @param[in] input_desc
* The descriptor of the input tensor. For detailed information, see ::mluOpTensorDescriptor_t.
* @param[in] input
* Pointer to the MLU memory that stores the input tensor.
* @param[in] paddings
* A host pointer to the \b paddings data that holds the padding size to be added for each dimension
* of \b input. Positive and negative padding values represent the padding size and the cropping size, respectively.
* @param[in] padding_value
* A host pointer to the \b padding_value that holds the constant value to be added.
* @param[in] output_desc
* The descriptor of the output tensor. For detailed information, see ::mluOpTensorDescriptor_t.
* @param[out] output
* Pointer to the MLU memory that stores the output tensor.
*
* @par Return
* - ::MLUOP_STATUS_SUCCESS, ::MLUOP_STATUS_BAD_PARAM
*
* @par Data Type
* - Data types of input tensor \b input, \b padding_value and output tensor \b output must be the same. The
* supported data types are as follows:
* - input tensor: uint8, int8, uint16, int16, uint32, int32, uint64, int64, bool, half, float.
* - paddings: int32.
* - padding_value: uint8, int8, uint16, int16, uint32, int32, uint64, int64, bool, half, float.
* - output tensor: uint8, int8, uint16, int16, uint32, int32, uint64, int64, bool, half, float.
*
* @par Scale Limitation
* - Only supports padding with a constant value in \b padding_value.
* - The input tensor and output tensor must meet the following requirements:
* - The number of dimensions of \b input is no more than 8.
* - The shape of the integer array of \b paddings is \p [n, 2]. Length of the first dimension of \b paddings is
* \p n, which equals to the number of dimensions of \b input. Length of the second dimension of \b paddings is 2,
* which means the two padding directions of before and after directions for each dimension of \b input. For each
* dimension \p k, \p paddings[k, 0] and \p paddings[k, 1] specify the padding size before and after the input data
* respectively.
* - \p output[k] = \p paddings[k, 0] + \p input[k] + \p paddings[k, 1]. \p k represents each dimension of \b input.
* \p output[k] represents the length of the dimension \p k in the output tensor. \p input[k] represents the length
* of the dimension \p k in the input tensor. \p paddings[k, 0] and \p paddings[k, 1] represent the length to be
* padded before and after the dimension \p k in the input tensor respectively.
* - \p paddings[k, 0] and \p paddings[k, 1] >= \p -input[k]. \p k represents each dimension of \b input.
* \p input[k] represents the length of the dimension \p k in the input tensor.
*
* @par Note
* - None.
*
* @par Example
* - The example of pad is as follows:
@verbatim
For example below, if dimensions of input == 2, then the shape of paddings should be [n, 2] == [2, 2], where n holds
the length of the first dimension of paddings. If paddings == [[len1, len2], [len3, len4]], then for the first
dimension k, paddings[k, 0] == len1 == 1, paddings[k, 1] == len2 == 1. For the second dimension k + 1, paddings[k +
1, 0] == len3 == -1. paddings[k + 1, 1] == len4 == 2. So the paddings is [[1, 1], [-1, 2]].
- len1 is the padding size to be padded before the first dimension k of the input data.
- len2 is the padding size to be padded after the first dimension k of the input data.
- len3 is the padding size to be padded before the second dimension k + 1 of the input data.
- len4 is the padding size to be padded after the second dimension k + 1 of the input data.

input: [[2, 4], [1, 3]]
paddings: [[1, 1], [-1, 2]]
padding_value: 0
output: [[0, 0, 0], [4, 0, 0], [3, 0, 0], [0, 0, 0]]
@endverbatim
*
* @par Reference
* - https://www.tensorflow.org/api_docs/python/tf/pad
* - https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/array_ops.py
* - https://pytorch.org/docs/stable/nn.functional.html?highlight=pad#torch.nn.functional.pad
*/

mluOpStatus_t MLUOP_WIN_API
mluOpPad(mluOpHandle_t handle,
const mluOpTensorDescriptor_t input_desc,
const void *input,
const void *paddings,
const void *padding_value,
const mluOpTensorDescriptor_t output_desc,
void *output);

// Group:Log
/*!
* @brief Computes logarithm of input tensor \b x, and returns the results in
Expand Down
Loading

0 comments on commit 40186fb

Please sign in to comment.