Skip to content

Commit

Permalink
[Feature](bangc-ops): add transform binary oprator. (#839)
Browse files Browse the repository at this point in the history
Co-authored-by: zhangtingyu <[email protected]>
  • Loading branch information
TingyuZhangSie and zhangtingyu authored Oct 16, 2023
1 parent add2576 commit 4d2653e
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bangc-ops/kernels/kernel_wrapper/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@
const mluOpTensorDescriptor_t, const void *, \
const mluOpTensorDescriptor_t, void *diff_x

#define TRANSFORM_PARAM_TYPE \
mluOpHandle_t, \
const mluOpPointerMode_t, \
const void *, \
const mluOpTensorDescriptor_t, \
const void *, \
const void *, \
const mluOpTensorDescriptor_t, \
void *

#define STRIDEDSLICE_PARAM_TYPE \
mluOpHandle_t, const mluOpTensorDescriptor_t, const void *, \
const int *, const int *, const int *, \
Expand Down Expand Up @@ -306,7 +316,6 @@ KERNEL_REGISTER(RoiAlignBackward, ROIALIGNBACKWARD_PARAM_TYPE);
KERNEL_REGISTER(RoiAlignBackwardV2, ROIALIGNBACKWARD_V2_PARAM_TYPE);
KERNEL_REGISTER(RoiPoolingForward, ROIPOOLINGFORWARD_PARAM_TYPE);
KERNEL_REGISTER(RoiPoolingBackward, ROIPOOLINGBACKWARD_PARAM_TYPE);
KERNEL_REGISTER(transform, TRANSFORM_PARAM_TYPE);
KERNEL_REGISTER(SyncBatchNormStats, SYNCBATCHNORMSTATS_PARAM_TYPE);
KERNEL_REGISTER(SyncBatchNormStatsV2, SYNCBATCHNORMSTATS_V2_PARAM_TYPE);
KERNEL_REGISTER(SyncBatchNormGatherStatsWithCounts,
Expand All @@ -320,5 +329,6 @@ KERNEL_REGISTER(SyncBatchNormBackwardElemt,
SYNCBATCHNORMBACKWARDELEMT_PARAM_TYPE);
KERNEL_REGISTER(SyncBatchNormBackwardElemtV2,
SYNCBATCHNORMBACKWARDELEMT_V2_PARAM_TYPE);
KERNEL_REGISTER(transform, TRANSFORM_PARAM_TYPE);
KERNEL_REGISTER(StridedSlice, STRIDEDSLICE_PARAM_TYPE);
#endif // KERNELS_KERNEL_WRAPPER_WRAPPER_H
40 changes: 40 additions & 0 deletions bangc-ops/kernels/transform/transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*************************************************************************
* 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 mluOpTransform(
mluOpHandle_t handle,
const mluOpPointerMode_t pointer_mode,
const void *alpha,
const mluOpTensorDescriptor_t input_desc,
const void *input,
const void *beta,
const mluOpTensorDescriptor_t output_desc,
void *output) {
transformWrapper wrapper;
mluOpStatus_t ret = wrapper.invoke(
handle, pointer_mode, alpha, input_desc, input,
beta, output_desc, output);
return ret;
}

95 changes: 95 additions & 0 deletions bangc-ops/mlu_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -15096,6 +15096,101 @@ mluOpSyncBatchNormBackwardElemtV2(mluOpHandle_t handle,
const mluOpTensorDescriptor_t diff_x_desc,
void *diff_x);

// Group:Transform
/*!
* @brief Linearly transforms an input tensor using the following formula:
*
* output = alpha * input + beta
*
* Parameters \b input and \b output represent tensor input and output,
* and \b alpha and \b beta represent scale factors.
* \b mluOpTransform supports scale pointer
* from both host and device for \b alpha and \b beta parameter.
* @param[in] handle
* Handle to a Cambricon MLUOP context used to manage MLU
* devices and queues.
* For detailed information, please refer to ::mluOpHandle_t.
* @param[in] input_desc
* Descriptor of the input tensor \b.
* Refer to ::mluOpTensorDescriptor_t for further detail.
* @param[in] input
* Device pointer to the MLU memory that stores the input tensor.
* @param[in] pointer_mode
* An enum value that indicates which mode scalar values \b alpha
* and \b beta would be passed by on the host or device.
* Mode type is defined in ::mluOpPointerMode_t.
* @param[in] alpha
* Pointer to scaling factor of tensor input.
* If \b pointer_mode is \b MLUOP_POINTER_MODE_DEVICE, \b alpha
* should be a device pointer.
* If \b pointer_mode is \b MLUOP_POINTER_MODE_HOST, \b alpha
* should be a host pointer.
* @param[in] beta
* Pointer to scaling factor of tensor input.
* If \b pointer_mode is \b MLUOP_POINTER_MODE_DEVICE, \b beta
* should be a device pointer.
* If \b pointer_mode is \b MLUOP_POINTER_MODE_HOST, \b beta
* should be a host pointer.
* @param[in] output_desc
* Descriptor of the output tensor \b output. For detailed
* information, please refer to ::mluOpTensorDescriptor_t.
* @param[out] output
* Descriptor of the output tensor. For detailed information, see
* ::mluOpTensorDescriptor_t.
*
* @par Return
* - ::MLUOP_STATUS_SUCCESS, ::MLUOP_STATUS_BAD_PARAM
*
* @par Formula
* - See "Transform Operator" section in "Cambricon MLUOP User Guide" for
* details.
*
* @par Data Type
* - The combinations of the data type for input tensor \b input
* and output tensor
* \b output must be half-half, float-float or int32-int32.
* - \b alpha and \b beta: If data type of tensors is float or half, the data
* type of \b alpha and \b beta should be float pointer.
* If data type of tensors is
* int32, the data type of \b alpha and \b beta should be int pointer.
*
* @par Scale Limitation
* - Tensor descriptors of input and output tensors must be the same.
* - Dimension number should not exceeds \p MLUOP_DIM_MAX.
*
* @note
* - None.
*
* @par Requirements
* - None.
*
* @par Example
@verbatim
Input tensor : [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

alpha : 2

beta : 1

Output tensor : [[3, 5, 7],
[9, 11, 13],
[15, 17, 19]]
@endverbatim
*
*/

mluOpStatus_t MLUOP_WIN_API
mluOpTransform(mluOpHandle_t handle,
const mluOpPointerMode_t pointer_mode,
const void *alpha,
const mluOpTensorDescriptor_t input_desc,
const void *input,
const void *beta,
const mluOpTensorDescriptor_t output_desc,
void *output);

// Group:StridedSlice
/*!
* @brief Extracts a slice of size ``(end - begin) / stride`` from the given \p input tensor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
op_name: "transform"
op_type: "TRANSFORM"
input {
id: "input1"
shape: {
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1000
dims: 2000
}
layout: LAYOUT_ARRAY
dtype: DTYPE_FLOAT
random_data: {
seed: 23
upper_bound: 100
lower_bound: 0
distribution: UNIFORM
}
}
output {
id: "output"
shape: {
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1
dims: 1000
dims: 2000
}
layout: LAYOUT_ARRAY
dtype: DTYPE_FLOAT
}
transform_param: {
alpha: 1
beta: 1
}
test_param: {
error_func: DIFF1
error_func: DIFF2
error_threshold: 0.003
error_threshold: 0.003
baseline_device: CPU
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
op_name: "transform"
op_type: "TRANSFORM"
input {
id: "input1"
shape: {
dims: 13
dims: 17
dims: 100
dims: 70
}
layout: LAYOUT_ARRAY
dtype: DTYPE_FLOAT
random_data: {
seed: 23
upper_bound: 100
lower_bound: 0
distribution: UNIFORM
}
}
output {
id: "output"
shape: {
dims: 13
dims: 17
dims: 100
dims: 70
}
layout: LAYOUT_ARRAY
dtype: DTYPE_FLOAT
}
transform_param: {
alpha: 1
beta: 1
}
test_param: {
error_func: DIFF1
error_func: DIFF2
error_threshold: 0.003
error_threshold: 0.003
baseline_device: CPU
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
op_name: "transform"
op_type: "TRANSFORM"
input {
id: "input1"
shape: {
dims: 1000
}
layout: LAYOUT_ARRAY
dtype: DTYPE_COMPLEX_FLOAT
random_data: {
seed: 23
upper_bound: 100
lower_bound: 0
distribution: UNIFORM
}
}
output {
id: "output"
shape: {
dims: 1000
}
layout: LAYOUT_ARRAY
dtype: DTYPE_COMPLEX_FLOAT
}
transform_param: {
alpha: 1.1
alpha_imag:1.1
beta: 1.2
beta_imag:1.1
}
test_param: {
error_func: DIFF1
error_func: DIFF2
error_threshold: 0.003
error_threshold: 0.003
baseline_device: CPU
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
op_name: "transform"
op_type: "TRANSFORM"
input {
id: "input1"
shape: {
dims: 1078
dims: 201
}
layout: LAYOUT_ARRAY
dtype: DTYPE_HALF
random_data: {
seed: 23
upper_bound: 100
lower_bound: 0
distribution: UNIFORM
}
}
output {
id: "output"
shape: {
dims: 1078
dims: 201
}
layout: LAYOUT_ARRAY
dtype: DTYPE_HALF
}
transform_param: {
alpha: 1.5
beta: 0.8
}
test_param: {
error_func: DIFF1
error_func: DIFF2
error_threshold: 0.003
error_threshold: 0.003
baseline_device: CPU
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
op_name: "transform"
op_type: "TRANSFORM"
input {
id: "input1"
shape: {
dims: 1078
dims: 201
}
layout: LAYOUT_ARRAY
dtype: DTYPE_INT32
random_data: {
seed: 23
upper_bound: 100
lower_bound: 0
distribution: UNIFORM
}
}
output {
id: "output"
shape: {
dims: 1078
dims: 201
}
layout: LAYOUT_ARRAY
dtype: DTYPE_INT32
}
transform_param: {
alpha: 1.5
beta: 0.8
}
test_param: {
error_func: DIFF1
error_func: DIFF2
error_threshold: 0.003
error_threshold: 0.003
baseline_device: CPU
}
Loading

0 comments on commit 4d2653e

Please sign in to comment.