Browse Source

IR Unify

tags/v1.1.1
chenjianping 5 years ago
parent
commit
1a58723c65
100 changed files with 4010 additions and 327 deletions
  1. +2
    -2
      mindspore/lite/CMakeLists.txt
  2. +2
    -2
      mindspore/lite/include/model.h
  3. +1
    -0
      mindspore/lite/nnacl/CMakeLists.txt
  4. +102
    -0
      mindspore/lite/nnacl/arithmetic.c
  5. +69
    -0
      mindspore/lite/nnacl/arithmetic.h
  6. +0
    -102
      mindspore/lite/nnacl/arithmetic_common.c
  7. +0
    -68
      mindspore/lite/nnacl/arithmetic_common.h
  8. +1
    -1
      mindspore/lite/nnacl/batch_to_space.c
  9. +8
    -0
      mindspore/lite/nnacl/common_func.h
  10. +1
    -0
      mindspore/lite/nnacl/conv_parameter.h
  11. +2
    -0
      mindspore/lite/nnacl/errorcode.h
  12. +1
    -1
      mindspore/lite/nnacl/fp16/arithmetic_fp16.c
  13. +2
    -2
      mindspore/lite/nnacl/fp16/arithmetic_fp16.h
  14. +1
    -1
      mindspore/lite/nnacl/fp16/stack_fp16.c
  15. +1
    -1
      mindspore/lite/nnacl/fp32/arithmetic_fp32.h
  16. +1
    -1
      mindspore/lite/nnacl/fp32/broadcast_to_fp32.h
  17. +1
    -0
      mindspore/lite/nnacl/fp32/concat_fp32.c
  18. +57
    -130
      mindspore/lite/nnacl/fp32/resize_fp32.c
  19. +9
    -9
      mindspore/lite/nnacl/fp32/resize_fp32.h
  20. +1
    -1
      mindspore/lite/nnacl/fp32/space_to_batch_fp32.c
  21. +1
    -1
      mindspore/lite/nnacl/fp32/space_to_depth_fp32.c
  22. +1
    -1
      mindspore/lite/nnacl/fp32/stack_fp32.c
  23. +2
    -0
      mindspore/lite/nnacl/fp32/tile_fp32.h
  24. +1
    -1
      mindspore/lite/nnacl/fp32/topk_fp32.h
  25. +0
    -1
      mindspore/lite/nnacl/fp32_grad/batch_norm.h
  26. +1
    -1
      mindspore/lite/nnacl/fp32_grad/softmax_grad.h
  27. +0
    -1
      mindspore/lite/nnacl/gather_parameter.h
  28. +42
    -0
      mindspore/lite/nnacl/infer/adam_infer.c
  29. +31
    -0
      mindspore/lite/nnacl/infer/adam_infer.h
  30. +79
    -0
      mindspore/lite/nnacl/infer/addn_infer.c
  31. +31
    -0
      mindspore/lite/nnacl/infer/addn_infer.h
  32. +45
    -0
      mindspore/lite/nnacl/infer/apply_momentum_infer.c
  33. +31
    -0
      mindspore/lite/nnacl/infer/apply_momentum_infer.h
  34. +49
    -0
      mindspore/lite/nnacl/infer/argmax_infer.c
  35. +32
    -0
      mindspore/lite/nnacl/infer/argmax_infer.h
  36. +51
    -0
      mindspore/lite/nnacl/infer/argmin_infer.c
  37. +32
    -0
      mindspore/lite/nnacl/infer/argmin_infer.h
  38. +28
    -0
      mindspore/lite/nnacl/infer/arithmetic_compare_infer.c
  39. +31
    -0
      mindspore/lite/nnacl/infer/arithmetic_compare_infer.h
  40. +112
    -0
      mindspore/lite/nnacl/infer/arithmetic_grad_infer.c
  41. +45
    -0
      mindspore/lite/nnacl/infer/arithmetic_grad_infer.h
  42. +118
    -0
      mindspore/lite/nnacl/infer/arithmetic_infer.c
  43. +32
    -0
      mindspore/lite/nnacl/infer/arithmetic_infer.h
  44. +22
    -0
      mindspore/lite/nnacl/infer/assert_op_infer.c
  45. +31
    -0
      mindspore/lite/nnacl/infer/assert_op_infer.h
  46. +34
    -0
      mindspore/lite/nnacl/infer/assign_add_infer.c
  47. +31
    -0
      mindspore/lite/nnacl/infer/assign_add_infer.h
  48. +37
    -0
      mindspore/lite/nnacl/infer/assign_infer.c
  49. +31
    -0
      mindspore/lite/nnacl/infer/assign_infer.h
  50. +68
    -0
      mindspore/lite/nnacl/infer/audio_spectrogram_infer.c
  51. +37
    -0
      mindspore/lite/nnacl/infer/audio_spectrogram_infer.h
  52. +83
    -0
      mindspore/lite/nnacl/infer/batch_to_space_infer.c
  53. +32
    -0
      mindspore/lite/nnacl/infer/batch_to_space_infer.h
  54. +39
    -0
      mindspore/lite/nnacl/infer/bias_grad_infer.c
  55. +31
    -0
      mindspore/lite/nnacl/infer/bias_grad_infer.h
  56. +33
    -0
      mindspore/lite/nnacl/infer/binary_cross_entropy_infer.c
  57. +32
    -0
      mindspore/lite/nnacl/infer/binary_cross_entropy_infer.h
  58. +38
    -0
      mindspore/lite/nnacl/infer/bn_grad_infer.c
  59. +31
    -0
      mindspore/lite/nnacl/infer/bn_grad_infer.h
  60. +68
    -0
      mindspore/lite/nnacl/infer/broadcast_to_infer.c
  61. +32
    -0
      mindspore/lite/nnacl/infer/broadcast_to_infer.h
  62. +41
    -0
      mindspore/lite/nnacl/infer/cast_infer.c
  63. +32
    -0
      mindspore/lite/nnacl/infer/cast_infer.h
  64. +453
    -0
      mindspore/lite/nnacl/infer/common_infer.c
  65. +210
    -0
      mindspore/lite/nnacl/infer/common_infer.h
  66. +68
    -0
      mindspore/lite/nnacl/infer/concat_infer.c
  67. +32
    -0
      mindspore/lite/nnacl/infer/concat_infer.h
  68. +41
    -0
      mindspore/lite/nnacl/infer/constant_of_shape_infer.c
  69. +32
    -0
      mindspore/lite/nnacl/infer/constant_of_shape_infer.h
  70. +28
    -0
      mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.c
  71. +38
    -0
      mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.h
  72. +35
    -0
      mindspore/lite/nnacl/infer/conv2d_grad_input_infer.c
  73. +38
    -0
      mindspore/lite/nnacl/infer/conv2d_grad_input_infer.h
  74. +95
    -0
      mindspore/lite/nnacl/infer/conv2d_infer.c
  75. +32
    -0
      mindspore/lite/nnacl/infer/conv2d_infer.h
  76. +31
    -0
      mindspore/lite/nnacl/infer/crop_infer.c
  77. +32
    -0
      mindspore/lite/nnacl/infer/crop_infer.h
  78. +46
    -0
      mindspore/lite/nnacl/infer/custom_extract_features_infer.c
  79. +31
    -0
      mindspore/lite/nnacl/infer/custom_extract_features_infer.h
  80. +39
    -0
      mindspore/lite/nnacl/infer/custom_normalize_infer.c
  81. +32
    -0
      mindspore/lite/nnacl/infer/custom_normalize_infer.h
  82. +39
    -0
      mindspore/lite/nnacl/infer/custom_predict_infer.c
  83. +36
    -0
      mindspore/lite/nnacl/infer/custom_predict_infer.h
  84. +93
    -0
      mindspore/lite/nnacl/infer/deconv2d_infer.c
  85. +32
    -0
      mindspore/lite/nnacl/infer/deconv2d_infer.h
  86. +58
    -0
      mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.c
  87. +32
    -0
      mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.h
  88. +54
    -0
      mindspore/lite/nnacl/infer/depth_to_space_infer.c
  89. +32
    -0
      mindspore/lite/nnacl/infer/depth_to_space_infer.h
  90. +72
    -0
      mindspore/lite/nnacl/infer/depthwise_conv2d_infer.c
  91. +32
    -0
      mindspore/lite/nnacl/infer/depthwise_conv2d_infer.h
  92. +76
    -0
      mindspore/lite/nnacl/infer/detection_post_process_infer.c
  93. +32
    -0
      mindspore/lite/nnacl/infer/detection_post_process_infer.h
  94. +33
    -0
      mindspore/lite/nnacl/infer/dropout_grad_infer.c
  95. +31
    -0
      mindspore/lite/nnacl/infer/dropout_grad_infer.h
  96. +39
    -0
      mindspore/lite/nnacl/infer/dropout_infer.c
  97. +31
    -0
      mindspore/lite/nnacl/infer/dropout_infer.h
  98. +58
    -0
      mindspore/lite/nnacl/infer/embedding_lookup_infer.c
  99. +31
    -0
      mindspore/lite/nnacl/infer/embedding_lookup_infer.h
  100. +46
    -0
      mindspore/lite/nnacl/infer/expand_dims_infer.c

+ 2
- 2
mindspore/lite/CMakeLists.txt View File

@@ -161,8 +161,8 @@ if (SUPPORT_GPU)
if (OFFLINE_COMPILE)
add_compile_definitions(PROGRAM_WITH_IL)
endif ()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/build/_deps/opencl-headers-src/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/build/_deps/opencl-clhpp-src/include)
include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-headers-src/)
include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-clhpp-src/include)
endif ()

if (WIN32)


+ 2
- 2
mindspore/lite/include/model.h View File

@@ -19,14 +19,14 @@
#include "include/lite_utils.h"

namespace mindspore::lite {
class PrimitiveC;
struct MS_API Model {
struct Node {
String name_;
NodeType node_type_;
PrimitiveC *primitive_;
const void *primitive_;
Uint32Vector input_indices_;
Uint32Vector output_indices_;
int quant_type_;
};
using NodePtrVector = std::vector<Node *>;
struct SubGraph {


+ 1
- 0
mindspore/lite/nnacl/CMakeLists.txt View File

@@ -15,6 +15,7 @@ file(GLOB KERNEL_SRC
${NNACL_DIR}/*.c
${NNACL_DIR}/fp32/*.c
${NNACL_DIR}/int8/*.c
${NNACL_DIR}/infer/*.c
${NNACL_DIR}/quantization/*.c
)



+ 102
- 0
mindspore/lite/nnacl/arithmetic.c View File

@@ -0,0 +1,102 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/arithmetic.h"
#include "nnacl/nnacl_utils.h"

void TileOneDimension(const float *inData, float *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple) {
int srcDimSize = inShape[dim];
if (dim == ndim - 1) {
for (int i = 0; i < multiple[dim]; i++) {
memcpy(outData, inData, srcDimSize * sizeof(float));
outData += srcDimSize;
}
return;
}
for (size_t i = 0; i < srcDimSize; i++) {
for (size_t j = 0; j < multiple[dim]; j++) {
TileOneDimension(inData + inStrides[dim] * i, outData + outStrides[dim] * (i + j * srcDimSize), dim + 1, ndim,
inShape, inStrides, outStrides, multiple);
}
}
}

void TileOneDimensionUint8(const uint8_t *inData, uint8_t *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple) {
int srcDimSize = inShape[dim];
if (dim == ndim - 1) {
for (int i = 0; i < multiple[dim]; i++) {
memcpy(outData, inData, srcDimSize * sizeof(uint8_t));
outData += srcDimSize;
}
return;
}
for (size_t i = 0; i < srcDimSize; i++) {
for (size_t j = 0; j < multiple[dim]; j++) {
TileOneDimensionUint8(inData + inStrides[dim] * i, outData + outStrides[dim] * (i + j * srcDimSize), dim + 1,
ndim, inShape, inStrides, outStrides, multiple);
}
}
}

void ComputeStrides(const int *shape, int *strides, const int ndim) {
int stride = 1;
for (int i = ndim - 1; i >= 0; i--) {
strides[i] = stride;
stride *= shape[i];
}
}

void CalcMultiplesAndStrides(ArithmeticParameter *param) {
NNACL_ASSERT(param->in_shape0_[i] != 0);
NNACL_ASSERT(param->in_shape1_[i] != 0);
for (size_t i = 0; i < param->ndim_; i++) {
param->multiples0_[i] = param->out_shape_[i] / param->in_shape0_[i];
param->multiples1_[i] = param->out_shape_[i] / param->in_shape1_[i];
}
// cal strides
ComputeStrides(param->in_shape0_, param->in_strides0_, param->ndim_);
ComputeStrides(param->in_shape1_, param->in_strides1_, param->ndim_);
ComputeStrides(param->out_shape_, param->out_strides_, param->ndim_);
}

void TileDimensions(const float *data0, const float *data1, float *tile_data0, float *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimension(data0, tile_data0, 0, param->ndim_, param->in_shape0_, param->in_strides0_, param->out_strides_,
param->multiples0_);
TileOneDimension(data1, tile_data1, 0, param->ndim_, param->in_shape1_, param->in_strides1_, param->out_strides_,
param->multiples1_);
}

void TileDimensionsUint8(const uint8_t *data0, const uint8_t *data1, uint8_t *tile_data0, uint8_t *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimensionUint8(data0, tile_data0, 0, param->ndim_, param->in_shape0_, param->in_strides0_, param->out_strides_,
param->multiples0_);
TileOneDimensionUint8(data1, tile_data1, 0, param->ndim_, param->in_shape1_, param->in_strides1_, param->out_strides_,
param->multiples1_);
}

void TileDimensionsInt8(const int8_t *data0, const int8_t *data1, int8_t *tile_data0, int8_t *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimensionUint8((uint8_t *)(data0), (uint8_t *)(tile_data0), 0, param->ndim_, param->in_shape0_,
param->in_strides0_, param->out_strides_, param->multiples0_);
TileOneDimensionUint8((uint8_t *)(data1), (uint8_t *)(tile_data1), 0, param->ndim_, param->in_shape1_,
param->in_strides1_, param->out_strides_, param->multiples1_);
}

+ 69
- 0
mindspore/lite/nnacl/arithmetic.h View File

@@ -0,0 +1,69 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_
#define MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_

#ifdef ENABLE_NEON
#include <arm_neon.h>
#endif
#include <string.h>
#include "nnacl/op_base.h"
#include "nnacl/arithmetic.h"

typedef struct ArithmeticParameter {
OpParameter op_parameter_;
bool broadcasting_;
size_t ndim_;
int activation_type_;
int in_shape0_[10];
int in_elements_num0_;
int in_shape1_[10];
int in_elements_num1_;

int out_shape_[10];
int out_elements_num_;

int in_strides0_[10];
int in_strides1_[10];
int out_strides_[10];

int multiples0_[10];
int multiples1_[10];
int eltwise_mode_; // eltwise need
} ArithmeticParameter;

#ifdef __cplusplus
extern "C" {
#endif
void TileOneDimension(const float *inData, float *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple);
void ComputeStrides(const int *shape, int *strides, const int ndim);

void CalcMultiplesAndStrides(ArithmeticParameter *param);

void TileOneDimensionUint8(const uint8_t *inData, uint8_t *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple);
void TileDimensions(const float *data0, const float *data1, float *tile_data0, float *tile_data1,
ArithmeticParameter *param);
void TileDimensionsUint8(const uint8_t *data0, const uint8_t *data1, uint8_t *tile_data0, uint8_t *tile_data1,
ArithmeticParameter *param);
void TileDimensionsInt8(const int8_t *data0, const int8_t *data1, int8_t *tile_data0, int8_t *tile_data1,
ArithmeticParameter *param);
#ifdef __cplusplus
}
#endif

#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_

+ 0
- 102
mindspore/lite/nnacl/arithmetic_common.c View File

@@ -1,102 +0,0 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/arithmetic_common.h"
#include "nnacl/nnacl_utils.h"

void TileOneDimension(const float *inData, float *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple) {
int srcDimSize = inShape[dim];
if (dim == ndim - 1) {
for (int i = 0; i < multiple[dim]; i++) {
memcpy(outData, inData, srcDimSize * sizeof(float));
outData += srcDimSize;
}
return;
}
for (size_t i = 0; i < srcDimSize; i++) {
for (size_t j = 0; j < multiple[dim]; j++) {
TileOneDimension(inData + inStrides[dim] * i, outData + outStrides[dim] * (i + j * srcDimSize), dim + 1, ndim,
inShape, inStrides, outStrides, multiple);
}
}
}

void TileOneDimensionUint8(const uint8_t *inData, uint8_t *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple) {
int srcDimSize = inShape[dim];
if (dim == ndim - 1) {
for (int i = 0; i < multiple[dim]; i++) {
memcpy(outData, inData, srcDimSize * sizeof(uint8_t));
outData += srcDimSize;
}
return;
}
for (size_t i = 0; i < srcDimSize; i++) {
for (size_t j = 0; j < multiple[dim]; j++) {
TileOneDimensionUint8(inData + inStrides[dim] * i, outData + outStrides[dim] * (i + j * srcDimSize), dim + 1,
ndim, inShape, inStrides, outStrides, multiple);
}
}
}

void ComputeStrides(const int *shape, int *strides, const int ndim) {
int stride = 1;
for (int i = ndim - 1; i >= 0; i--) {
strides[i] = stride;
stride *= shape[i];
}
}

void CalcMultiplesAndStrides(ArithmeticParameter *param) {
NNACL_ASSERT(param->in_shape0_[i] != 0);
NNACL_ASSERT(param->in_shape1_[i] != 0);
for (size_t i = 0; i < param->ndim_; i++) {
param->multiples0_[i] = param->out_shape_[i] / param->in_shape0_[i];
param->multiples1_[i] = param->out_shape_[i] / param->in_shape1_[i];
}
// cal strides
ComputeStrides(param->in_shape0_, param->in_strides0_, param->ndim_);
ComputeStrides(param->in_shape1_, param->in_strides1_, param->ndim_);
ComputeStrides(param->out_shape_, param->out_strides_, param->ndim_);
}

void TileDimensions(const float *data0, const float *data1, float *tile_data0, float *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimension(data0, tile_data0, 0, param->ndim_, param->in_shape0_, param->in_strides0_, param->out_strides_,
param->multiples0_);
TileOneDimension(data1, tile_data1, 0, param->ndim_, param->in_shape1_, param->in_strides1_, param->out_strides_,
param->multiples1_);
}

void TileDimensionsUint8(const uint8_t *data0, const uint8_t *data1, uint8_t *tile_data0, uint8_t *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimensionUint8(data0, tile_data0, 0, param->ndim_, param->in_shape0_, param->in_strides0_, param->out_strides_,
param->multiples0_);
TileOneDimensionUint8(data1, tile_data1, 0, param->ndim_, param->in_shape1_, param->in_strides1_, param->out_strides_,
param->multiples1_);
}

void TileDimensionsInt8(const int8_t *data0, const int8_t *data1, int8_t *tile_data0, int8_t *tile_data1,
ArithmeticParameter *param) {
CalcMultiplesAndStrides(param);
TileOneDimensionUint8((uint8_t *)(data0), (uint8_t *)(tile_data0), 0, param->ndim_, param->in_shape0_,
param->in_strides0_, param->out_strides_, param->multiples0_);
TileOneDimensionUint8((uint8_t *)(data1), (uint8_t *)(tile_data1), 0, param->ndim_, param->in_shape1_,
param->in_strides1_, param->out_strides_, param->multiples1_);
}

+ 0
- 68
mindspore/lite/nnacl/arithmetic_common.h View File

@@ -1,68 +0,0 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_
#define MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_

#ifdef ENABLE_NEON
#include <arm_neon.h>
#endif
#include <string.h>
#include "nnacl/op_base.h"
#include "nnacl/arithmetic_common.h"

typedef struct ArithmeticParameter {
OpParameter op_parameter_;
bool broadcasting_;
size_t ndim_;
int activation_type_;
int in_shape0_[10];
int in_elements_num0_;
int in_shape1_[10];
int in_elements_num1_;

int out_shape_[10];
int out_elements_num_;

int in_strides0_[10];
int in_strides1_[10];
int out_strides_[10];

int multiples0_[10];
int multiples1_[10];
} ArithmeticParameter;

#ifdef __cplusplus
extern "C" {
#endif
void TileOneDimension(const float *inData, float *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple);
void ComputeStrides(const int *shape, int *strides, const int ndim);

void CalcMultiplesAndStrides(ArithmeticParameter *param);

void TileOneDimensionUint8(const uint8_t *inData, uint8_t *outData, int dim, size_t ndim, const int *inShape,
const int *inStrides, const int *outStrides, const int *multiple);
void TileDimensions(const float *data0, const float *data1, float *tile_data0, float *tile_data1,
ArithmeticParameter *param);
void TileDimensionsUint8(const uint8_t *data0, const uint8_t *data1, uint8_t *tile_data0, uint8_t *tile_data1,
ArithmeticParameter *param);
void TileDimensionsInt8(const int8_t *data0, const int8_t *data1, int8_t *tile_data0, int8_t *tile_data1,
ArithmeticParameter *param);
#ifdef __cplusplus
}
#endif

#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_COMMON_H_

+ 1
- 1
mindspore/lite/nnacl/batch_to_space.c View File

@@ -15,7 +15,7 @@
*/

#include "nnacl/batch_to_space.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"

void BatchToSpaceNoCropForNHWC(const void *input, void *output, const int *in_shape, int out_n, const int *block,
int data_size) {


+ 8
- 0
mindspore/lite/nnacl/common_func.h View File

@@ -63,6 +63,14 @@ static inline int GetStride(int *strides, const int *shape, int length) {
return stride;
}

inline void ComputeStrides(const int *shape, int *strides, const int ndim) {
int stride = 1;
for (int i = ndim - 1; i >= 0; i--) {
strides[i] = stride;
stride *= shape[i];
}
}

#ifdef ENABLE_ARM64
void BiasAdd(const float *bias, float *data, size_t oc4, size_t plan_size);
void BiasAddRelu6(const float *bias, float *data, size_t oc4, size_t plan_size);


+ 1
- 0
mindspore/lite/nnacl/conv_parameter.h View File

@@ -51,6 +51,7 @@ typedef struct ConvParameter {
int output_unit_;
PadMode pad_mode_;
ActType act_type_;
int channel_multiplie_;
} ConvParameter;

typedef struct SlidingWindowParam {


+ 2
- 0
mindspore/lite/nnacl/errorcode.h View File

@@ -22,6 +22,8 @@ typedef enum ErrorCodeCommonEnum {
NNACL_ERR = 1,
NNACL_NULL_PTR,
NNACL_PARAM_INVALID,
NNACL_INFER_INVALID,
NNACL_INPUT_TENSOR_ERROR,
NNACL_COMMON_END = 9999
} ErrorCodeCommonEnum;



+ 1
- 1
mindspore/lite/nnacl/fp16/arithmetic_fp16.c View File

@@ -16,7 +16,7 @@

#include "nnacl/fp16/arithmetic_fp16.h"
#include <math.h>
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"
#include "nnacl/nnacl_utils.h"

void TileOneDimensionFp16(float16_t *inData, float16_t *outData, int dim, size_t ndim, int *inShape, int *inStrides,


+ 2
- 2
mindspore/lite/nnacl/fp16/arithmetic_fp16.h View File

@@ -20,7 +20,7 @@
#include <arm_neon.h>
#endif
#include "nnacl/op_base.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"
#include "nnacl/errorcode.h"

#ifdef __cplusplus
@@ -107,7 +107,7 @@ int ElementMinimumFp16(float16_t *input0, float16_t *input1, float16_t *output,
int ElementNotEqualFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementEqualFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementLessFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementLessEqual(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementLessEqualFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementGreaterFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);
int ElementGreaterEqualFp16(float16_t *input0, float16_t *input1, uint8_t *output, int element_size);



+ 1
- 1
mindspore/lite/nnacl/fp16/stack_fp16.c View File

@@ -15,7 +15,7 @@
*/

#include "nnacl/fp16/stack_fp16.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"

size_t Fp16GetStackCopyNum(int axis, int *in_shape, size_t shape_size) {
size_t one_input_size = 1;


+ 1
- 1
mindspore/lite/nnacl/fp32/arithmetic_fp32.h View File

@@ -20,7 +20,7 @@
#include <arm_neon.h>
#endif
#include "nnacl/op_base.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"
#include "nnacl/errorcode.h"

#ifdef __cplusplus


+ 1
- 1
mindspore/lite/nnacl/fp32/broadcast_to_fp32.h View File

@@ -21,7 +21,7 @@
#endif
#include "nnacl/op_base.h"

#define BROADCAST_TO_SHAPE_MAX_SIZE 4
#define BROADCAST_TO_SHAPE_MAX_SIZE 8

typedef struct BroadcastToParameter {
OpParameter op_parameter_;


+ 1
- 0
mindspore/lite/nnacl/fp32/concat_fp32.c View File

@@ -37,6 +37,7 @@ void Concat(const void **input, int input_num, int axis, const int **inputs_outp
int offset = UP_DIV(input_stride, thread_num);
int count = input_stride - offset * task_id;
if (count <= 0) {
axis_offset += inputs_output_shape[i][axis];
continue;
}
count = MSMIN(offset, count);


+ 57
- 130
mindspore/lite/nnacl/fp32/resize_fp32.c View File

@@ -17,8 +17,29 @@
#include "nnacl/fp32/resize_fp32.h"
#include "nnacl/common_func.h"
#include "nnacl/errorcode.h"
int PrepareResizeBilinear(const int *input_shape, const int *output_shape, bool align_corners, int *y_bottoms,
int *y_tops, int *x_lefts, int *x_rights, float *y_bottom_weights, float *x_left_weights) {

float CalculateOriginalCoordinate(int x_resized, int length_original, int length_resized,
int coordinate_transform_mode) {
float scale;
switch (coordinate_transform_mode) {
case 0: // ASYMMETRIC
scale = (float)(length_resized) / length_original;
return (float)x_resized / scale;
case 1: // ALIGN_CORNERS
scale = (float)(length_resized - 1) / (length_original - 1);
return (float)x_resized / scale;
case 2: // HALF_PIXEL
scale = (float)(length_resized) / length_original;
float actual = (float)(x_resized + 0.5) / scale - 0.5;
return actual > 0 ? actual : 0;
default:
return -1;
}
}

int PrepareResizeBilinear(const int *input_shape, const int *output_shape, int coordinate_transform_mode,
int *y_bottoms, int *y_tops, int *x_lefts, int *x_rights, float *y_bottom_weights,
float *x_left_weights) {
if (input_shape == NULL || output_shape == NULL || y_bottoms == NULL || y_tops == NULL || x_lefts == NULL ||
x_rights == NULL || y_bottom_weights == NULL || x_left_weights == NULL) {
return NNACL_NULL_PTR;
@@ -29,18 +50,13 @@ int PrepareResizeBilinear(const int *input_shape, const int *output_shape, bool

int new_height = output_shape[1];
int new_width = output_shape[2];
float height_scale = (float)(in_h) / new_height;
float width_scale = (float)(in_w) / new_width;
if (align_corners && new_height > 1) {
height_scale = (float)(in_h - 1) / (new_height - 1);
}
if (align_corners && new_width > 1) {
width_scale = (float)(in_w - 1) / (new_width - 1);
}

int h, w;
for (h = 0; h < new_height; h++) {
float actual_y = (float)h * height_scale;
float actual_y = CalculateOriginalCoordinate(h, in_h, new_height, coordinate_transform_mode);
if (actual_y == -1) {
return NNACL_ERR;
}
int y_bottom = (int)(floor(actual_y));
int y_top = y_bottom + 1 < in_h ? (y_bottom + 1) : (in_h - 1);
float y_top_weight = actual_y - (float)(y_bottom);
@@ -51,7 +67,10 @@ int PrepareResizeBilinear(const int *input_shape, const int *output_shape, bool
y_bottom_weights[h] = y_bottom_weight;
}
for (w = 0; w < new_width; w++) {
float actual_x = (float)(w)*width_scale;
float actual_x = CalculateOriginalCoordinate(w, in_w, new_width, coordinate_transform_mode);
if (actual_x == -1) {
return NNACL_ERR;
}
int x_left = (int)(floor(actual_x));
int x_right = x_left + 1 < in_w ? (x_left + 1) : (in_w - 1);
float x_right_weight = actual_x - (float)(x_left);
@@ -64,96 +83,6 @@ int PrepareResizeBilinear(const int *input_shape, const int *output_shape, bool
return NNACL_OK;
}

int ResizeBilinear(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *y_bottoms, const int *y_tops, const int *x_lefts, const int *x_rights,
const float *y_bottom_weights, const float *x_left_weights, const int n_h_begin, const int n_h_end) {
if (input_data == NULL || output_data == NULL || input_shape == NULL || output_shape == NULL || y_bottoms == NULL ||
y_tops == NULL || x_lefts == NULL || x_rights == NULL || y_bottom_weights == NULL || x_left_weights == NULL) {
return NNACL_NULL_PTR;
}

int in_w = input_shape[2];
int in_c = input_shape[3];

int new_height = output_shape[1];
int new_width = output_shape[2];

int n_h, n, h, w, c;
n = n_h_begin / new_height;
h = n_h_begin % new_height;
int n_h_stride = new_width * in_c;
int out_offset = n_h_begin * n_h_stride;
for (n_h = n_h_begin; n_h < n_h_end; n_h++, h++) {
if (h == new_height) {
h = 0;
n++;
}
int y_bottom = y_bottoms[h];
int y_top = y_tops[h];
float y_bottom_weight = y_bottom_weights[h];
const float y_top_weight = 1.0f - y_bottom_weight;

for (w = 0; w < new_width; w++) {
int x_left = x_lefts[w];
int x_right = x_rights[w];
float x_left_weight = x_left_weights[w];
const float x_right_weight = 1.0f - x_left_weight;
float top_left_weight = y_top_weight * x_left_weight;
float top_right_weight = y_top_weight * x_right_weight;
float bottom_left_weight = y_bottom_weight * x_left_weight;
float bottom_right_weight = y_bottom_weight * x_right_weight;

c = 0;
int in_bottom_left_offset = offset(input_shape, n, y_bottom, x_left, c);
int in_bottom_right_offset = in_bottom_left_offset + (x_right - x_left) * in_c;
int in_top_left_offset = in_bottom_left_offset + (y_top - y_bottom) * in_w * in_c;
int in_top_right_offset = in_bottom_right_offset + (y_top - y_bottom) * in_w * in_c;

#ifdef ENABLE_NEON
float32x4_t top_left_w = vdupq_n_f32(top_left_weight);
float32x4_t top_right_w = vdupq_n_f32(top_right_weight);
float32x4_t bottom_left_w = vdupq_n_f32(bottom_left_weight);
float32x4_t bottom_right_w = vdupq_n_f32(bottom_right_weight);

for (; c <= in_c - 4; c += 4) {
float32x4_t bottom_left = vld1q_f32(input_data + in_bottom_left_offset + c);
float32x4_t bottom_right = vld1q_f32(input_data + in_bottom_right_offset + c);
float32x4_t top_left = vld1q_f32(input_data + in_top_left_offset + c);
float32x4_t top_right = vld1q_f32(input_data + in_top_right_offset + c);

float32x4_t interp_value = vdupq_n_f32(0.0);

float32x4_t tmp = vmulq_f32(bottom_left, bottom_left_w);
interp_value = vaddq_f32(interp_value, tmp);

tmp = vmulq_f32(bottom_right, bottom_right_w);
interp_value = vaddq_f32(interp_value, tmp);

tmp = vmulq_f32(top_left, top_left_w);
interp_value = vaddq_f32(interp_value, tmp);

tmp = vmulq_f32(top_right, top_right_w);
interp_value = vaddq_f32(interp_value, tmp);
vst1q_f32(output_data + out_offset, interp_value);
out_offset += 4;
}
#endif
for (; c < in_c; c++) {
float bottom_left = input_data[in_bottom_left_offset + c];
float bottom_right = input_data[in_bottom_right_offset + c];
float top_left = input_data[in_top_left_offset + c];
float top_right = input_data[in_top_right_offset + c];
float interp_value = bottom_left * bottom_left_weight + bottom_right * bottom_right_weight +
top_left * top_left_weight + top_right * top_right_weight;
output_data[out_offset] = interp_value;
out_offset++;
}
}
}

return NNACL_OK;
}

int InterpRow(const float *src_line, float *linear_output, int new_width, const float *x_left_weights,
const int *x_lefts, const int *x_rights, int in_c) {
int w;
@@ -207,10 +136,10 @@ int InterpCol(const float *bottom_line, const float *top_line, float *output, in
return 0;
}

int ResizeBilinear2(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *y_bottoms, const int *y_tops, const int *x_lefts, const int *x_rights,
const float *y_bottom_weights, const float *x_left_weights, float *line0, float *line1,
const int n_h_begin, const int n_h_end) {
int ResizeBilinear(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *y_bottoms, const int *y_tops, const int *x_lefts, const int *x_rights,
const float *y_bottom_weights, const float *x_left_weights, float *line0, float *line1,
const int n_h_begin, const int n_h_end) {
if (input_data == NULL || output_data == NULL || input_shape == NULL || output_shape == NULL || y_bottoms == NULL ||
y_tops == NULL || x_lefts == NULL || x_rights == NULL || y_bottom_weights == NULL || x_left_weights == NULL) {
return NNACL_NULL_PTR;
@@ -278,36 +207,34 @@ int ResizeBilinear2(const float *input_data, float *output_data, const int *inpu
return NNACL_OK;
}

int CalcNearestNeighbor(const int out_position, const int in_size, const float scale, const bool align_corners) {
int actual_v;
if (align_corners) {
actual_v = (int)(round((float)out_position * scale));
} else {
actual_v = (int)(floor((float)out_position * scale));
}
int input_position = actual_v < in_size ? actual_v : in_size - 1;
return input_position;
}

int ResizeNearestNeighbor(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
bool align_corners, int tid, int thread_num) {
int coordinate_transform_mode, int tid, int thread_num) {
int batch, y, x, c;
c = input_shape[3];

float height_scale = (float)(input_shape[1]) / (float)(output_shape[1]);
float width_scale = (float)(input_shape[2]) / (float)(output_shape[2]);
if (align_corners && output_shape[1] > 1) {
height_scale = (float)(input_shape[1] - 1) / (output_shape[1] - 1);
}
if (align_corners && output_shape[2] > 1) {
width_scale = (float)(input_shape[2] - 1) / (output_shape[2] - 1);
}

bool align_corners = coordinate_transform_mode == 1;
for (batch = 0; batch < output_shape[0]; batch++) {
for (y = tid; y < output_shape[1]; y += thread_num) {
int input_y = CalcNearestNeighbor(y, input_shape[1], height_scale, align_corners);
float actual_y = CalculateOriginalCoordinate(y, input_shape[1], output_shape[1], coordinate_transform_mode);
if (actual_y == -1) {
return NNACL_ERR;
}
int input_y;
if (align_corners) {
input_y = (int)(round(actual_y));
} else {
input_y = (int)(floor(actual_y));
}
for (x = 0; x < output_shape[2]; x++) {
int input_x = CalcNearestNeighbor(x, input_shape[2], width_scale, align_corners);
float actual_x = CalculateOriginalCoordinate(x, input_shape[2], output_shape[2], coordinate_transform_mode);
if (actual_x == -1) {
return NNACL_ERR;
}
int input_x;
if (align_corners) {
input_x = (int)(round(actual_x));
} else {
input_x = (int)(floor(actual_x));
}
int in_offset = offset(input_shape, batch, input_y, input_x, 0);
int out_offset = offset(output_shape, batch, y, x, 0);
memcpy(output_data + out_offset, input_data + in_offset, c * sizeof(float));


+ 9
- 9
mindspore/lite/nnacl/fp32/resize_fp32.h View File

@@ -26,20 +26,20 @@
extern "C" {
#endif

int PrepareResizeBilinear(const int *input_shape, const int *output_shape, bool align_corners, int *y_bottoms,
int *y_tops, int *x_lefts, int *x_rights, float *y_bottom_weights, float *x_left_weights);
int PrepareResizeBilinear(const int *input_shape, const int *output_shape, int coordinate_transform_mode,
int *y_bottoms, int *y_tops, int *x_lefts, int *x_rights, float *y_bottom_weights,
float *x_left_weights);

int ResizeBilinear(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *y_bottoms, const int *y_tops, const int *x_lefts, const int *x_rights,
const float *y_bottom_weights, const float *x_left_weights, const int n_h_begin, const int n_h_end);

int ResizeBilinear2(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *y_bottoms, const int *y_tops, const int *x_lefts, const int *x_rights,
const float *y_bottom_weights, const float *x_left_weights, float *line0, float *line1,
const int n_h_begin, const int n_h_end);
const float *y_bottom_weights, const float *x_left_weights, float *line0, float *line1,
const int n_h_begin, const int n_h_end);

int ResizeNearestNeighbor(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
bool align_corners, int tid, int thread_num);
int coordinate_transform_mode, int tid, int thread_num);

float CalculateOriginalCoordinate(int x_resized, int length_original, int length_resized,
int coordinate_transform_mode);
#ifdef __cplusplus
}
#endif


+ 1
- 1
mindspore/lite/nnacl/fp32/space_to_batch_fp32.c View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/fp32/space_to_batch_fp32.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"

void DoSpaceToBatch(const float *input, float *output, const int *in_shape, const int *out_shape, const int *in_stride,
const int *out_stride, const int *blocks, const int *paddings, int thread, int task_id) {


+ 1
- 1
mindspore/lite/nnacl/fp32/space_to_depth_fp32.c View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "nnacl/fp32/space_to_depth_fp32.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"
#include "nnacl/errorcode.h"
#include "nnacl/op_base.h"



+ 1
- 1
mindspore/lite/nnacl/fp32/stack_fp32.c View File

@@ -15,7 +15,7 @@
*/

#include "nnacl/fp32/stack_fp32.h"
#include "nnacl/arithmetic_common.h"
#include "nnacl/arithmetic.h"

size_t GetStackCopyNum(int axis, const int *in_shape, size_t shape_size) {
size_t one_input_size = 1;


+ 2
- 0
mindspore/lite/nnacl/fp32/tile_fp32.h View File

@@ -24,6 +24,8 @@ typedef struct TileParameter {
OpParameter op_parameter_;
int multiples_[5];
int dims_[5];
size_t dims_size_;
size_t multiples_size_;

// shape correlative
int in_shape_[5];


+ 1
- 1
mindspore/lite/nnacl/fp32/topk_fp32.h View File

@@ -27,10 +27,10 @@ typedef struct TopkNode {
typedef struct TopkParameter {
// primitive parameter
OpParameter op_parameter_;
int k_;
bool sorted_;

// other parameter
int k_;
int last_dim_size_;
int loop_num_;
void *topk_node_list_;


+ 0
- 1
mindspore/lite/nnacl/fp32_grad/batch_norm.h View File

@@ -22,7 +22,6 @@
typedef struct BNGradParameter {
OpParameter op_parameter_;
float epsilon_;
float momentum_;
} BNGradParameter;

#ifdef __cplusplus


+ 1
- 1
mindspore/lite/nnacl/fp32_grad/softmax_grad.h View File

@@ -35,7 +35,7 @@ typedef struct SoftmaxCrossEntropyParameter {
// other parameter
int32_t batch_size_;
unsigned int number_of_classes_;
int is_grad;
bool is_grad;
} SoftmaxCrossEntropyParameter;

void SoftmaxGrad(const float *input_ptr, const float *yt_ptr, float *output_ptr, float *sum_data, float *sum_mul,


+ 0
- 1
mindspore/lite/nnacl/gather_parameter.h View File

@@ -23,7 +23,6 @@ typedef struct GatherParameter {
// Primitive parameter
OpParameter op_parameter_;
int axis_;
int batchDims_;
} GatherParameter;

#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_NNACL_GATHER_PARAMETER_H_

+ 42
- 0
mindspore/lite/nnacl/infer/adam_infer.c View File

@@ -0,0 +1,42 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nnacl/infer/adam_infer.h"

int AdamInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}
if (10 != inputs_size) {
return NNACL_ERR;
}

if (GetElementNum(inputs[0]) != GetElementNum(inputs[1]) || GetElementNum(inputs[0]) != GetElementNum(inputs[2]) ||
GetElementNum(inputs[0]) != GetElementNum(inputs[9]) || GetElementNum(inputs[3]) != 1 ||
GetElementNum(inputs[4]) != 1 || GetElementNum(inputs[5]) != 1 || GetElementNum(inputs[6]) != 1 ||
GetElementNum(inputs[7]) != 1 || GetElementNum(inputs[8]) != 1) {
return NNACL_ERR;
}
if (outputs_size != 0) {
TensorC *out = outputs[0];
SetDataTypeFormat(out, inputs[0]);
out->shape_size_ = 1;
out->shape_[0] = 1;
}

return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/adam_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ADAM_INFER_H
#define MINDSPORE_LITE_NNACL_ADAM_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int AdamInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ADAM_INFER_H

+ 79
- 0
mindspore/lite/nnacl/infer/addn_infer.c View File

@@ -0,0 +1,79 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/addn_infer.h"

int AddnInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret == NNACL_NULL_PTR) {
return NNACL_NULL_PTR;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
if (inputs_size < 2) {
return NNACL_ERR;
}
SetDataTypeFormat(output, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}

size_t max_dims = input->shape_size_;
size_t max_dims_idx = 0;

// determine max_dims
for (size_t i = 1; i < inputs_size; ++i) {
if (inputs[i]->shape_size_ > max_dims) {
max_dims = inputs[i]->shape_size_;
max_dims_idx = 0;
}
}
ShapeSet(output->shape_, &output->shape_size_, inputs[max_dims_idx]->shape_, inputs[max_dims_idx]->shape_size_);

// make sure all elements have the same size or 1 (broadcasting) in all dimensions
for (size_t i = 1; i < inputs_size; ++i) {
if ((inputs[i]->shape_size_ != max_dims) && (GetElementNum(inputs[i]) != GetElementNum(inputs[max_dims_idx]))) {
return NNACL_ERR;
}
if (inputs[i]->data_type_ != inputs[0]->data_type_) {
return NNACL_ERR;
}
}

for (size_t d = 0; d < input->shape_size_; ++d) {
size_t max_dim = 0;
for (size_t i = 0; i < inputs_size; ++i) {
size_t shift = max_dims - inputs[i]->shape_size_;
size_t dim = (i < shift) ? 1 : inputs[i]->shape_[d];
if (dim > max_dim) {
max_dim = dim;
}
}
#ifndef SUPPORT_TRAIN
for (size_t i = 0; i < inputs_size; ++i) {
size_t shift = max_dims - inputs[i]->shape_size_;
size_t dim = (i < shift) ? 1 : inputs[i]->shape_[d];
if ((dim != max_dim) && (dim != 1)) {
return NNACL_ERR;
}
}
#endif
output->shape_[d] = max_dim; // set the biggest dimension in the output tensor
}

return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/addn_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ADDN_INFER_H
#define MINDSPORE_LITE_NNACL_ADDN_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int AddnInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ADDN_INFER_H

+ 45
- 0
mindspore/lite/nnacl/infer/apply_momentum_infer.c View File

@@ -0,0 +1,45 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/apply_momentum_infer.h"

int ApplyMomentumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}
if (inputs_size != 5) {
return NNACL_INPUT_TENSOR_ERROR;
}

if (GetElementNum(inputs[0]) != GetElementNum(inputs[1]) || GetElementNum(inputs[0]) != GetElementNum(inputs[3]) ||
GetElementNum(inputs[2]) != 1 || GetElementNum(inputs[4]) != 1) {
return NNACL_INPUT_TENSOR_ERROR;
}
if (outputs_size != 0) {
TensorC *out = outputs[0];
if (out == NULL) {
return NNACL_NULL_PTR;
}
out->data_type_ = inputs[0]->data_type_;
out->format_ = inputs[0]->format_;
out->shape_size_ = 1;
out->shape_[0] = 1;
}

return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/apply_momentum_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H
#define MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int ApplyMomentumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H

+ 49
- 0
mindspore/lite/nnacl/infer/argmax_infer.c View File

@@ -0,0 +1,49 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/argmax_infer.h"

int ArgmaxInferShape(const TensorC *const *inputs, const size_t inputs_size, TensorC **outputs,
const size_t outputs_size, OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];

SetDataTypeFormat(output, input);
ArgMinMaxParameter *param = (ArgMinMaxParameter *)parameter;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = 0;
ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_);
size_t input_shape_size = input->shape_size_;
int axis = param->axis_ < 0 ? param->axis_ + input_shape_size : param->axis_;
if (axis >= input_shape_size || axis < 0) {
return NNACL_PARAM_INVALID;
}
if (param->topk_ == 1 && !param->keep_dims_) {
ShapeErase(output_shape, &output_shape_size, axis);
} else {
output_shape[axis] = param->topk_;
}

SetShapeArray(output, output_shape, output_shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/argmax_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARGMAX_INFER_H
#define MINDSPORE_LITE_NNACL_ARGMAX_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/arg_min_max_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int ArgmaxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ARGMAX_INFER_H

+ 51
- 0
mindspore/lite/nnacl/infer/argmin_infer.c View File

@@ -0,0 +1,51 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/argmin_infer.h"

int ArgminInferShape(const TensorC *const *inputs, const size_t inputs_size, TensorC **outputs,
const size_t outputs_size, OpParameter *parameter) {
if (inputs_size != 1 || outputs_size > 2) {
return NNACL_ERR;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
ArgMinMaxParameter *param = (ArgMinMaxParameter *)parameter;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_shape_size = input->shape_size_;
int axis = param->axis_ < 0 ? param->axis_ + input_shape_size : param->axis_;
if (axis >= input_shape_size || axis < 0) {
return NNACL_PARAM_INVALID;
}
int output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = 0;
ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_);
if (param->topk_ == 1 && !param->keep_dims_) {
ShapeErase(output_shape, &output_shape_size, axis);
} else {
output_shape[axis] = param->topk_;
}

SetShapeArray(output, output_shape, output_shape_size);
if (outputs_size == 2) {
SetDataTypeFormat(outputs[1], input);
SetShapeTensor(outputs[1], output);
}
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/argmin_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARGMAIN_INFER_H
#define MINDSPORE_LITE_NNACL_ARGMAIN_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/arg_min_max_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int ArgminInferShape(const TensorC *const *inputs, const size_t inputs_size, TensorC **outputs,
const size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ARGMAIN_INFER_H

+ 28
- 0
mindspore/lite/nnacl/infer/arithmetic_compare_infer.c View File

@@ -0,0 +1,28 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/arithmetic_compare_infer.h"

int ArithmeticCompareInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter) {
int res = ArithmeticInferShape(inputs, inputs_size, outputs, outputs_size, parameter);
TensorC *output = outputs[0];
if (output == NULL) {
return NNACL_NULL_PTR;
}
output->data_type_ = kNumberTypeBool;
return res;
}

+ 31
- 0
mindspore/lite/nnacl/infer/arithmetic_compare_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H
#define MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H

#include "nnacl/infer/arithmetic_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int ArithmeticCompareInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H

+ 112
- 0
mindspore/lite/nnacl/infer/arithmetic_grad_infer.c View File

@@ -0,0 +1,112 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/arithmetic_grad_infer.h"

int ArithmeticGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 2);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *dy = inputs[0];
const TensorC *x1 = inputs[1];
const TensorC *x2 = inputs[2];
TensorC *dx1 = outputs[0];
TensorC *dx2 = outputs[1];

ArithmeticGradParameter *param = (ArithmeticGradParameter *)parameter;
if ((param->type_ == PrimitiveType_MaximumGrad) || (param->type_ == PrimitiveType_MinimumGrad)) {
x1 = inputs[0];
x2 = inputs[1];
dy = inputs[2];
}

int inShape0[MAX_SHAPE_SIZE];
size_t inShape0_size = 0;
ShapeSet(inShape0, &inShape0_size, x1->shape_, x1->shape_size_);
int inShape1[MAX_SHAPE_SIZE];
size_t inShape1_size = 0;
ShapeSet(inShape1, &inShape1_size, x2->shape_, x2->shape_size_);
int outShape[MAX_SHAPE_SIZE];
size_t outShape_size = 0;
ShapeSet(outShape, &outShape_size, dy->shape_, dy->shape_size_);

if ((param->type_ == PrimitiveType_AddGrad) || (param->type_ == PrimitiveType_SubGrad) ||
(param->type_ == PrimitiveType_MaximumGrad) || (param->type_ == PrimitiveType_MinimumGrad)) {
param->ndim_ = outShape_size;
param->x1_shape_size_ = param->ndim_;
param->x2_shape_size_ = param->ndim_;
param->dy_shape_size_ = param->ndim_;
int fillDimNum0 = outShape_size - inShape0_size;
int fillDimNum1 = outShape_size - inShape1_size;
int j0 = 0;
int j1 = 0;
for (unsigned int i = 0; i < outShape_size; i++) {
param->x1_shape_[i] = (i < fillDimNum0) ? 1 : inShape0[j0++];
param->x2_shape_[i] = (i < fillDimNum1) ? 1 : inShape1[j1++];
param->dy_shape_[i] = outShape[i];
}
} else {
if (GetElementNum(dx1) < GetElementNum(dx2)) {
param->ndim_ = inShape1_size;
param->x1_shape_size_ = param->ndim_;
param->x2_shape_size_ = param->ndim_;
param->dy_shape_size_ = param->ndim_;
int fillDimNum = inShape1_size - inShape0_size; // This will not work for batch!
int j = 0;
for (unsigned int i = 0; i < inShape1_size; i++) {
if (i < fillDimNum) {
param->x2_shape_[i] = 1;
} else {
param->x2_shape_[i] = inShape0[j++];
}
param->x1_shape_[i] = inShape1[i];
param->dy_shape_[i] = outShape[i];
}
} else if (GetElementNum(dx2) < GetElementNum(dx1)) { // if (inShape0.size() > inShape1.size())
param->ndim_ = inShape0_size;
param->x1_shape_size_ = param->ndim_;
param->x2_shape_size_ = param->ndim_;
param->dy_shape_size_ = param->ndim_;
param->broadcasting_ = true;
int j = 0;
int fillDimNum = inShape0_size - inShape1_size;
for (unsigned int i = 0; i < inShape0_size; i++) {
if (i < fillDimNum) {
param->x2_shape_[i] = 1;
} else {
param->x2_shape_[i] = inShape1[j++];
}
param->x1_shape_[i] = inShape0[i];
param->dy_shape_[i] = outShape[i];
}
} else {
param->broadcasting_ = false;
for (unsigned int i = 0; i < inShape0_size; i++) {
param->x2_shape_[i] = inShape1[i];
param->x1_shape_[i] = inShape0[i];
param->dy_shape_[i] = outShape[i];
}
}
}

SetShapeTensor(dx1, x1);
SetShapeTensor(dx2, x2);
dx1->data_type_ = dy->data_type_;
dx2->data_type_ = dy->data_type_;
return NNACL_OK;
}

+ 45
- 0
mindspore/lite/nnacl/infer/arithmetic_grad_infer.h View File

@@ -0,0 +1,45 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H
#define MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ArithmeticGradParameter {
OpParameter op_parameter_;
int type_;
bool broadcasting_; // default false
int ndim_;
// std::vector<int> dy_shape_;
int dy_shape_[MAX_SHAPE_SIZE];
size_t dy_shape_size_;
int x1_shape_[MAX_SHAPE_SIZE];
size_t x1_shape_size_;
int x2_shape_[MAX_SHAPE_SIZE];
size_t x2_shape_size_;
} ArithmeticGradParameter;

int ArithmeticGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H

+ 118
- 0
mindspore/lite/nnacl/infer/arithmetic_infer.c View File

@@ -0,0 +1,118 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/arithmetic_infer.h"

int ArithmeticInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

ArithmeticParameter *param = (ArithmeticParameter *)parameter;
param->broadcasting_ = false;

const TensorC *input0 = inputs[0];
const TensorC *input1 = inputs[1];
TensorC *output = outputs[0];

const int *input_shape0 = input0->shape_;
size_t input_shape0_size = input0->shape_size_;
const int *input_shape1 = input1->shape_;
size_t input_shape1_size = input1->shape_size_;
output->format_ = input0->format_;
output->data_type_ = input0->data_type_;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
if (input_shape0_size > 10 || input_shape1_size > 10) {
// int wrong_dim = input_shape0_size > input_shape1_size ? input_shape0_size : input_shape1_size;
return NNACL_ERR;
}
int in_shape0_[10];
int in_shape1_[10];
int out_shape_[10];

int ndim_ = input_shape0_size;
if (input_shape0_size < input_shape1_size) {
ndim_ = input_shape1_size;
int fill_dim_num = input_shape1_size - input_shape0_size;
int j = 0;
for (size_t i = 0; i < input_shape1_size; i++) {
if (i < fill_dim_num) {
in_shape0_[i] = 1;
} else {
in_shape0_[i] = input_shape0[j++];
}
in_shape1_[i] = input_shape1[i];
}
// format = input0->format();
} else if (input_shape0_size > input_shape1_size) {
ndim_ = input_shape0_size;
int fill_dim_num = input_shape0_size - input_shape1_size;
int j = 0;
for (size_t i = 0; i < input_shape0_size; i++) {
if (i < fill_dim_num) {
in_shape1_[i] = 1;
} else {
in_shape1_[i] = input_shape1[j++];
}
in_shape0_[i] = input_shape0[i];
}
} else {
for (size_t i = 0; i < input_shape0_size; i++) {
in_shape1_[i] = input_shape1[i];
in_shape0_[i] = input_shape0[i];
}
}

int output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = 0;
for (int i = 0; i < ndim_; i++) {
if (in_shape0_[i] != in_shape1_[i]) {
if (in_shape0_[i] == 1) {
out_shape_[i] = in_shape1_[i];
} else if (in_shape1_[i] == 1) {
out_shape_[i] = in_shape0_[i];
} else {
return NNACL_ERR;
}
param->broadcasting_ = true;
} else {
out_shape_[i] = in_shape0_[i];
}
output_shape[output_shape_size] = out_shape_[i];
output_shape_size++;
}

SetShapeArray(output, output_shape, output_shape_size);

param->ndim_ = ndim_;
memcpy(param->in_shape0_, in_shape0_, ndim_ * sizeof(int));
memcpy(param->in_shape1_, in_shape1_, ndim_ * sizeof(int));
memcpy(param->out_shape_, out_shape_, ndim_ * sizeof(int));

param->in_elements_num0_ = 1;
param->in_elements_num1_ = 1;
param->out_elements_num_ = 1;
for (int i = 0; i < ndim_; i++) {
param->in_elements_num0_ *= param->in_shape0_[i];
param->in_elements_num1_ *= param->in_shape1_[i];
param->out_elements_num_ *= param->out_shape_[i];
}
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/arithmetic_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H
#define MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/arithmetic.h"

#ifdef __cplusplus
extern "C" {
#endif

int ArithmeticInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outpus_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H

+ 22
- 0
mindspore/lite/nnacl/infer/assert_op_infer.c View File

@@ -0,0 +1,22 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/assert_op_infer.h"

int AssertOpInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/assert_op_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H
#define MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int AssertOpInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H

+ 34
- 0
mindspore/lite/nnacl/infer/assign_add_infer.c View File

@@ -0,0 +1,34 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/assign_add_infer.h"

int AssignAddInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *x = inputs[0];
const TensorC *y = inputs[1];
TensorC *out = outputs[0];
if (x->data_type_ != y->data_type_) {
return NNACL_ERR;
}
SetDataTypeFormat(out, x);
SetShapeTensor(out, x);
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/assign_add_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H
#define MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int AssignAddInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H

+ 37
- 0
mindspore/lite/nnacl/infer/assign_infer.c View File

@@ -0,0 +1,37 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/assign_infer.h"

int AssignInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2);
if (check_ret != NNACL_OK) {
return check_ret;
}

if (GetElementNum(inputs[0]) != GetElementNum(inputs[1])) {
return NNACL_ERR;
}

if (outputs_size != 0) {
TensorC *out = outputs[0];
SetDataTypeFormat(out, inputs[0]);
out->shape_size_ = 1;
out->shape_[0] = 1;
}
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/assign_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_ASSIGN_INFER_H
#define MINDSPORE_LITE_NNACL_ASSIGN_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int AssignInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_ASSIGN_INFER_H

+ 68
- 0
mindspore/lite/nnacl/infer/audio_spectrogram_infer.c View File

@@ -0,0 +1,68 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/audio_spectrogram_infer.h"

int Log2Ceil(uint32_t length) {
if (length == 0) {
return -1;
}
int floor = 0;
for (int i = 4; i >= 0; --i) {
const int shift = (1 << i);
uint32_t tmp = length >> shift;
if (tmp != 0) {
length = tmp;
floor += shift;
}
}
return length == (length & ~(length - 1)) ? floor : floor + 1;
}

uint32_t GetFftLength(uint32_t length) {
int shift = Log2Ceil(length);
return 1 << shift;
}

int AudioSpectrogramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
AudioSpectrogramParameter *param = (AudioSpectrogramParameter *)parameter;
if (param->window_size_ < 2) {
return NNACL_ERR;
}
if (param->stride_ < 1) {
return NNACL_ERR;
}
int output_shape[3];
output_shape[0] = input->shape_[1];
int sample_sub_window = input->shape_[0] - param->window_size_;
output_shape[1] = sample_sub_window < 0 ? 0 : 1 + sample_sub_window / param->stride_;
// compute fft length
int fft_length = GetFftLength(param->window_size_);
output_shape[2] = fft_length / 2 + 1;
SetShapeArray(output, output_shape, 3);
return NNACL_OK;
}

+ 37
- 0
mindspore/lite/nnacl/infer/audio_spectrogram_infer.h View File

@@ -0,0 +1,37 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H
#define MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct AudioSpectrogramParameter {
OpParameter op_parameter_;
int window_size_;
int stride_;
} AudioSpectrogramParameter;

int AudioSpectrogramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H

+ 83
- 0
mindspore/lite/nnacl/infer/batch_to_space_infer.c View File

@@ -0,0 +1,83 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/batch_to_space_infer.h"

int BatchToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
if (input->format_ != Format_NHWC) {
return NNACL_ERR;
}
SetDataTypeFormat(outputs[0], input);
BatchToSpaceParameter *param = (BatchToSpaceParameter *)parameter;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_shape[MAX_SHAPE_SIZE];
size_t input_shape_size = 0;
ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_);
if (input_shape_size != 4) {
return NNACL_PARAM_INVALID;
}

int32_t *block_shape = param->block_shape_;
// if (block_shape.size() != kBlockShapeSize) {
// MS_LOG(ERROR) << "Block shape size should be " << kBlockShapeSize;
// return RET_PARAM_INVALID;
// return NNACL_PARAM_INVALID;
//}
int32_t *crops = param->crops_;
// if (crops.size() != kCropsSize) {
// MS_LOG(ERROR) << "Crops size should be " << kCropsSize;
// return RET_PARAM_INVALID;
// return NNACL_PARAM_INVALID;
//}
int mul_block_shape = 1;

for (size_t i = 0; i < 2; ++i) {
if (block_shape[i] <= 0) {
return NNACL_PARAM_INVALID;
}
if (input_shape[kNHWC_N] % block_shape[i]) {
return NNACL_ERR;
}
mul_block_shape *= block_shape[i];
}

if (input_shape[kNHWC_N] < mul_block_shape) {
return NNACL_PARAM_INVALID;
}
for (size_t i = 0; i < 4; ++i) {
if (crops[i] < 0) {
return NNACL_PARAM_INVALID;
}
}
int32_t output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = input_shape_size;
output_shape[kNHWC_N] = input_shape[kNHWC_N] / mul_block_shape;
output_shape[kNHWC_H] = input_shape[kNHWC_H] * block_shape[0] - crops[0] - crops[1];
output_shape[kNHWC_W] = input_shape[kNHWC_W] * block_shape[1] - crops[2] - crops[3];
output_shape[kNHWC_C] = input_shape[kNHWC_C];

SetShapeArray(outputs[0], output_shape, output_shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/batch_to_space_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H
#define MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/batch_to_space.h"

#ifdef __cplusplus
extern "C" {
#endif

int BatchToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H

+ 39
- 0
mindspore/lite/nnacl/infer/bias_grad_infer.c View File

@@ -0,0 +1,39 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/bias_grad_infer.h"

int BiasGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *in0 = inputs[0];
TensorC *out = outputs[0];

int inshape[MAX_SHAPE_SIZE];
size_t inshape_size = 0;
ShapeSet(inshape, &inshape_size, in0->shape_, in0->shape_size_);
int ndim = inshape_size;
for (int i = 0; i < ndim - 1; i++) {
inshape[i] = 1;
}
SetDataTypeFormat(out, in0);
SetShapeArray(out, inshape, inshape_size);

return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/bias_grad_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H
#define MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int BiasGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H

+ 33
- 0
mindspore/lite/nnacl/infer/binary_cross_entropy_infer.c View File

@@ -0,0 +1,33 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/binary_cross_entropy_infer.h"

int BinaryCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter) {
const TensorC *x = inputs[0];
TensorC *out = outputs[0];
SetDataTypeFormat(out, x);
BinaryCrossEntropyParameter *param = (BinaryCrossEntropyParameter *)parameter;
int reduction = param->reduction;
if (reduction == 1 || reduction == 2) {
out->shape_size_ = 1;
out->shape_[0] = 1;
} else {
SetShapeTensor(out, x);
}
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/binary_cross_entropy_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H
#define MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/fp32_grad/binary_cross_entropy.h"

#ifdef __cplusplus
extern "C" {
#endif

int BinaryCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H

+ 38
- 0
mindspore/lite/nnacl/infer/bn_grad_infer.c View File

@@ -0,0 +1,38 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/bn_grad_infer.h"

int BnGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 6, 3);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *in = inputs[1];
const TensorC *scale = inputs[2];
if (in->shape_size_ != 4) {
return NNACL_INPUT_TENSOR_ERROR;
}

SetShapeTensor(outputs[0], in);
SetDataTypeFormat(outputs[0], in);
SetShapeTensor(outputs[1], scale);
SetDataTypeFormat(outputs[1], scale);
SetShapeTensor(outputs[2], scale);
SetDataTypeFormat(outputs[2], scale);
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/bn_grad_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H
#define MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int BnGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H

+ 68
- 0
mindspore/lite/nnacl/infer/broadcast_to_infer.c View File

@@ -0,0 +1,68 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/broadcast_to_infer.h"

int BroadcastToInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
if (inputs_size != 1 && inputs_size != 2) {
return NNACL_ERR;
}
if (outputs_size != 1) {
return NNACL_ERR;
}

const TensorC *input = inputs[0];
SetDataTypeFormat(outputs[0], input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
BroadcastToParameter *param = (BroadcastToParameter *)parameter;
int32_t dst_shape[MAX_SHAPE_SIZE];
size_t dst_shape_size = param->shape_size_;
for (size_t i = 0; i < dst_shape_size; i++) {
dst_shape[i] = param->shape_[i];
}
for (size_t i = 0; i < dst_shape_size; ++i) {
if (dst_shape[i] == -1) {
dst_shape[i] = inputs[0]->shape_[i];
}
}
const int *input_shape = input->shape_;
size_t input_shape_size = input->shape_size_;
int shape[MAX_SHAPE_SIZE];
size_t shape_size = dst_shape_size;
int input_shape_index = input_shape_size - 1;
if (input_shape_size > dst_shape_size) {
return NNACL_ERR;
}

for (int i = dst_shape_size - 1; i >= 0; --i) {
if (dst_shape[i] < 0) {
return NNACL_ERR;
}
if (input_shape_index >= 0) {
int dim = input_shape[input_shape_index];
if (dim != dst_shape[i] && dim != 1) {
return NNACL_ERR;
}
}
shape[i] = dst_shape[i];
--input_shape_index;
}
SetShapeArray(outputs[0], shape, shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/broadcast_to_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H
#define MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/fp32/broadcast_to_fp32.h"

#ifdef __cplusplus
extern "C" {
#endif

int BroadcastToInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outpus_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H

+ 41
- 0
mindspore/lite/nnacl/infer/cast_infer.c View File

@@ -0,0 +1,41 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/cast_infer.h"

int CastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
output->format_ = input->format_;
const TensorC *dst_type = inputs[1];
output->data_type_ = *((int *)dst_type->data_);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
if (input->data_type_ != kNumberTypeBool && input->data_type_ != kNumberTypeUInt8 &&
input->data_type_ != kNumberTypeInt8 && input->data_type_ != kNumberTypeInt32 &&
input->data_type_ != kNumberTypeFloat32 && input->data_type_ != kNumberTypeFloat16) {
return NNACL_INPUT_TENSOR_ERROR;
}

SetShapeTensor(output, input);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/cast_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CAST_INFER_H
#define MINDSPORE_LITE_NNACL_CAST_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/fp32/cast_fp32.h"

#ifdef __cplusplus
extern "C" {
#endif

int CastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CAST_INFER_H

+ 453
- 0
mindspore/lite/nnacl/infer/common_infer.c View File

@@ -0,0 +1,453 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use tensor file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nnacl/infer/common_infer.h"
#include <stdlib.h>
#include <string.h>

int FreeTensorListData(TensorListC *tensor_list) {
// del each tensor in tensors_ and clear tensors_
if (tensor_list->element_num_ == 0) {
return NNACL_OK;
}
for (int i = 0; i < tensor_list->element_num_; ++i) {
// if (tensor_list->tensors_[i] != NULL) {
// delete this->tensors_[i];
// free(tensor_list->tensors_[i]); note: maybe need
tensor_list->tensors_[i] = NULL;
// }
}
// tensors_.clear(); //note: correct?
// tensor_list->element_num_ = 0; //note: maybe need
return NNACL_OK;
}

int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, vvector *tensor_shape) {
// This function will create a new tensors_
// Your must to set shape(param2: tensor_shape) and data_type_(tensors_data_type_ = param1: dtype) of each tensor in
// tensors_. After that, you need to call function:MallocData to malloc data buf of each tensor in tensors_.
if (tensor_list->element_num_ != 0) {
// If tensors_ is not empty then clear this tensors_ and rebuild a new tensors_.
int ret = FreeTensorListData(tensor_list);
if (ret != NNACL_OK) {
return NNACL_ERR;
}
}
if (((size_t)(tensor_list->element_num_)) != tensor_shape->size_) {
return NNACL_ERR;
}
tensor_list->tensors_data_type_ = dtype;
for (int i = 0; i < tensor_list->element_num_; ++i) {
TensorC *tensor_ptr = (TensorC *)malloc(sizeof(TensorC));
if (tensor_ptr == NULL) {
return NNACL_ERR;
}
tensor_ptr->data_type_ = dtype;
ShapeSet(tensor_ptr->shape_, &(tensor_ptr->shape_size_), tensor_shape->shape_[i], tensor_shape->shape_size_[i]);
tensor_list->tensors_[i] = tensor_ptr;
}
return NNACL_OK;
}

int TensorListMergeShape(int *element_shape, size_t element_shape_size, const int *tmp, size_t tmp_size) {
if (element_shape_size != tmp_size) {
return NNACL_ERR;
}
for (size_t j = 0; j < tmp_size; ++j) {
if (element_shape[j] >= 0 && tmp[j] >= 0 && element_shape[j] != tmp[j]) {
return NNACL_ERR;
}
element_shape[j] = element_shape[j] >= 0 ? element_shape[j] : tmp[j];
}
return NNACL_OK;
}

bool TensorListIsFullyDefined(int *shape, size_t shape_size) {
for (size_t i = 0; i < shape_size; ++i) {
if (shape[i] < 0) {
return false;
}
}
return true;
}

int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
for (size_t i = 0; i < inputs_size; i++) {
if (inputs[i] == NULL) {
return NNACL_NULL_PTR;
}
}
for (size_t i = 0; i < outputs_size; i++) {
if (outputs[i] == NULL) {
return NNACL_NULL_PTR;
}
}
if (parameter == NULL) {
return NNACL_NULL_PTR;
}
return NNACL_OK;
}

int CheckAugmentNullSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret == NNACL_NULL_PTR) {
return NNACL_NULL_PTR;
}
if (inputs_size != inputs_size_obj || outputs_size != outputs_size_obj) {
return NNACL_INPUT_TENSOR_ERROR;
}
return NNACL_OK;
}

int CheckAugmentNullSizeInputTwo(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter, size_t inputs_size_obj_0,
size_t inputs_size_obj_1, size_t outputs_size_obj) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret == NNACL_NULL_PTR) {
return NNACL_NULL_PTR;
}
if ((inputs_size != inputs_size_obj_0 && inputs_size != inputs_size_obj_1) || outputs_size != outputs_size_obj) {
return NNACL_INPUT_TENSOR_ERROR;
}
return NNACL_OK;
}

int CheckAugmentNullInputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t inputs_size_obj) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret == NNACL_NULL_PTR) {
return NNACL_NULL_PTR;
}
if (inputs_size != inputs_size_obj) {
return NNACL_INPUT_TENSOR_ERROR;
}
return NNACL_OK;
}

int CheckAugmentNullOutputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t outputs_size_obj) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret == NNACL_NULL_PTR) {
return NNACL_NULL_PTR;
}
if (outputs_size != outputs_size_obj) {
return NNACL_INPUT_TENSOR_ERROR;
}
return NNACL_OK;
}

int SetShapeTensor(TensorC *dst, const TensorC *src) {
for (size_t i = 0; i < src->shape_size_; i++) {
dst->shape_[i] = src->shape_[i];
}
dst->shape_size_ = src->shape_size_;
return NNACL_OK;
}

int SetShapeArray(TensorC *dst, int *src, size_t src_size) {
for (size_t i = 0; i < src_size; i++) {
dst->shape_[i] = src[i];
}
dst->shape_size_ = src_size;
return NNACL_OK;
}

void SetDataTypeFormat(TensorC *dst, const TensorC *src) {
dst->format_ = src->format_;
dst->data_type_ = src->data_type_;
}

int GetBatch(const TensorC *tensor) {
if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) {
return -1;
}
switch (tensor->format_) {
case Format_NHWC:
case Format_NHWC4:
case Format_NCHW:
case Format_NC4HW4:
case Format_KCHW:
case Format_KHWC:
case Format_NC:
case Format_NC4:
return tensor->shape_[0];
case Format_HWCK:
case Format_CHWK:
return tensor->shape_[3];
case Format_HWKC:
return tensor->shape_[2];
case Format_CKHW:
return tensor->shape_[1];
default:
return -1;
}
}
int GetHeight(const TensorC *tensor) {
if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) {
return -1;
}
switch (tensor->format_) {
case Format_NCHW:
case Format_KCHW:
case Format_CKHW:
return tensor->shape_[2];
case Format_NHWC:
case Format_NHWC4:
case Format_NC4HW4:
case Format_KHWC:
case Format_CHWK:
return tensor->shape_[1];
case Format_HWCK:
case Format_HWKC:
case Format_HW:
case Format_HW4:
return tensor->shape_[0];
default:
return -1;
}
}
int GetWidth(const TensorC *tensor) {
if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) {
return -1;
}
switch (tensor->format_) {
case Format_NCHW:
case Format_KCHW:
case Format_CKHW:
return tensor->shape_[3];
case Format_KHWC:
case Format_NHWC:
case Format_NHWC4:
case Format_NC4HW4:
case Format_CHWK:
return tensor->shape_[2];
case Format_HWCK:
case Format_HWKC:
case Format_HW:
case Format_HW4:
return tensor->shape_[1];
default:
return -1;
}
}
int GetChannel(const TensorC *tensor) {
if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) {
return -1;
}
switch (tensor->format_) {
case Format_NCHW:
case Format_KCHW:
case Format_NC:
case Format_NC4:
return tensor->shape_[1];
case Format_HWCK:
return tensor->shape_[2];
case Format_HWKC:
case Format_NHWC:
case Format_NHWC4:
case Format_NC4HW4:
case Format_KHWC:
return tensor->shape_[3];
case Format_CKHW:
case Format_CHWK:
return tensor->shape_[0];
default:
return -1;
}
}

int GetElementNum(const TensorC *tensor) {
if (tensor->shape_size_ == 0) {
return 1; // scalar mode
}
int res = 1;
for (size_t i = 0; i < tensor->shape_size_; i++) {
res = res * tensor->shape_[i];
}
return res;
}
int GetDimensionSize(const TensorC *tensor, const size_t index) {
int dim_size = -1;
if (index < tensor->shape_size_) {
dim_size = tensor->shape_[index];
}
return dim_size;
}

int ShapeSet(int *dst_shape, size_t *dst_shape_size, const int *src_shape, size_t src_shape_size) {
for (size_t i = 0; i < src_shape_size; i++) {
dst_shape[i] = src_shape[i];
}
*dst_shape_size = src_shape_size;
return NNACL_OK;
}

int ShapePush(int *shape, size_t *shape_size, int value) {
shape[*shape_size] = value;
*shape_size = *shape_size + 1;
return NNACL_OK;
}

int ShapeInsert(int *shape, size_t *shape_size, int index, int value) {
if (index < 0 || index > *shape_size) {
return NNACL_ERR;
}
for (int i = *shape_size; i > index; i--) {
shape[i] = shape[i - 1];
}
shape[index] = value;
*shape_size = *shape_size + 1;
return NNACL_OK;
}

int ShapeErase(int *shape, size_t *shape_size, int index) {
if (index < 0 && index >= *shape_size) {
return NNACL_ERR;
}

for (int i = index; i < *shape_size - 1; i++) {
shape[i] = shape[i + 1];
}
*shape_size = *shape_size - 1;
return NNACL_OK;
}

bool ShapeEqual(const int *shape0, size_t shape0_size, const int *shape1, size_t shape1_size) {
if (shape0_size != shape1_size) {
return false;
}
for (int i = 0; i < shape0_size; i++) {
if (shape0[i] != shape1[i]) {
return false;
}
}
return true;
}

void iswap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}

int imin(int a, int b) { return a > b ? b : a; }

int imax(int a, int b) { return a < b ? b : a; }

// input == output completely refer to
// 1. zeros_like
int CommonInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
if (parameter == NULL || inputs[0] == NULL || outputs[0] == NULL) {
return NNACL_NULL_PTR;
}
SetDataTypeFormat(outputs[0], inputs[0]);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
SetShapeTensor(outputs[0], inputs[0]);
return NNACL_OK;
}

int FftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
output->data_type_ = kNumberTypeFloat32;
output->format_ = input->format_;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_shape[MAX_SHAPE_SIZE];
size_t input_shape_size = 0;
ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_);
input_shape_size--;
SetShapeArray(output, input_shape, input_shape_size);
return NNACL_OK;
}

int VectorCInit(VectorC *vc, size_t per_malloc_size) {
vc->data_ = (int *)malloc(per_malloc_size * sizeof(int));
if (vc->data_ == NULL) {
return NNACL_ERR;
}
vc->size_ = 0;
vc->max_size_ = per_malloc_size;
vc->per_malloc_size_ = per_malloc_size;
return NNACL_OK;
}

void VectorCSet(VectorC *vc, const int *src_shape, size_t src_shape_size) {
if (src_shape_size == 0) {
vc->size_ = 0;
} else {
free(vc->data_);
vc->max_size_ = (src_shape_size / vc->per_malloc_size_ + 1) * vc->per_malloc_size_;
vc->data_ = (int *)malloc(sizeof(int) * vc->max_size_);
for (size_t i = 0; i < src_shape_size; i++) {
vc->data_[i] = src_shape[i];
}
vc->size_ = src_shape_size;
}
}

void VectorCPush(VectorC *vc, int value) {
if (vc->size_ + 1 > vc->max_size_) {
int *tmp = (int *)malloc(vc->per_malloc_size_ * sizeof(int) + vc->max_size_ * sizeof(int));
memcpy(tmp, vc->data_, vc->size_ * sizeof(int));
free(vc->data_);
vc->data_ = tmp;
vc->max_size_ = vc->max_size_ + vc->per_malloc_size_;
}
vc->data_[vc->size_] = value;
vc->size_++;
}

void VectorCInsert(VectorC *vc, int index, int value) {
if (vc->size_ + 1 > vc->max_size_) {
int *tmp = (int *)malloc(vc->per_malloc_size_ * sizeof(int) + vc->max_size_ * sizeof(int));
memcpy(tmp, vc->data_, vc->size_ * sizeof(int));
free(vc->data_);
vc->data_ = tmp;
vc->max_size_ = vc->max_size_ + vc->per_malloc_size_;
}
memmove(vc->data_ + index + 1, vc->data_ + index, (vc->size_ - index) * sizeof(int));
vc->data_[index] = value;
vc->size_++;
}

void VectorCErase(VectorC *vc, int index) {
memmove(vc->data_ + index, vc->data_ + index + 1, (vc->size_ - index - 1) * sizeof(int));
vc->size_--;
}

bool VectorCEqual(VectorC *vc1, VectorC *vc2) {
if (vc1->size_ != vc2->size_) {
return false;
}
for (size_t i = 0; i < vc1->size_; i++) {
if (vc1->data_[i] != vc2->data_[i]) {
return false;
}
}
return true;
}

void VectorCFree(VectorC *vc) {
free(vc->data_);
vc->data_ = NULL;
}

+ 210
- 0
mindspore/lite/nnacl/infer/common_infer.h View File

@@ -0,0 +1,210 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MINDSPORE_LITE_NNACL_COMMON_H_
#define MINDSPORE_LITE_NNACL_COMMON_H_

#include <stddef.h>
#include "nnacl/errorcode.h"
#include "nnacl/op_base.h"
#include "nnacl/tensor_c.h"

#ifdef __cplusplus
extern "C" {
#endif

#define kNCHW_N 0
#define kNCHW_C 1
#define kNCHW_H 2
#define kNCHW_W 3

typedef enum FormatC {
Format_NCHW = 0,
Format_NHWC = 1,
Format_NHWC4 = 2,
Format_HWKC = 3,
Format_HWCK = 4,
Format_KCHW = 5,
Format_CKHW = 6,
Format_KHWC = 7,
Format_CHWK = 8,
Format_HW = 9,
Format_HW4 = 10,
Format_NC = 11,
Format_NC4 = 12,
Format_NC4HW4 = 100,
Format_NUM_OF_FORMAT = 101,
Format_MIN = Format_NCHW,
Format_MAX = Format_NUM_OF_FORMAT
} FormatC;

typedef enum TypeIdC {
kTypeUnknown = 0,
kMetaTypeBegin = kTypeUnknown,
kMetaTypeType, // Type
kMetaTypeAnything,
kMetaTypeObject,
kMetaTypeTypeType, // TypeType
kMetaTypeProblem,
kMetaTypeExternal,
kMetaTypeNone,
kMetaTypeNull,
kMetaTypeEllipsis,
kMetaTypeEnd,
//
// Object types
//
kObjectTypeBegin = kMetaTypeEnd,
kObjectTypeNumber,
kObjectTypeString,
kObjectTypeList,
kObjectTypeTuple,
kObjectTypeSlice,
kObjectTypeKeyword,
kObjectTypeTensorType,
kObjectTypeRowTensorType,
kObjectTypeSparseTensorType,
kObjectTypeUndeterminedType,
kObjectTypeClass,
kObjectTypeDictionary,
kObjectTypeFunction,
kObjectTypeJTagged,
kObjectTypeSymbolicKeyType,
kObjectTypeEnvType,
kObjectTypeRefKey,
kObjectTypeRef,
kObjectTypeEnd,
//
// Number Types
//
kNumberTypeBegin = kObjectTypeEnd,
kNumberTypeBool,
kNumberTypeInt,
kNumberTypeInt8,
kNumberTypeInt16,
kNumberTypeInt32,
kNumberTypeInt64,
kNumberTypeUInt,
kNumberTypeUInt8,
kNumberTypeUInt16,
kNumberTypeUInt32,
kNumberTypeUInt64,
kNumberTypeFloat,
kNumberTypeFloat16,
kNumberTypeFloat32,
kNumberTypeFloat64,
kNumberTypeComplex64,
kNumberTypeEnd
} TypeIdC;

enum PrimitiveType {
PrimitiveType_MaximumGrad,
PrimitiveType_MinimumGrad,
PrimitiveType_AddGrad,
PrimitiveType_SubGrad,
};

enum NNACLLshProjectionType {
LshProjectionType_UNKNOWN = 0,
LshProjectionType_SPARSE = 1,
LshProjectionType_DENSE = 2,
LshProjectionType_MIN = LshProjectionType_UNKNOWN,
LshProjectionType_MAX = LshProjectionType_DENSE
};

#define MAX_PTR_ELEMENT 20
typedef struct vvector {
int *shape_[MAX_PTR_ELEMENT]; // note:
int shape_size_[MAX_PTR_ELEMENT];
size_t size_;
} vvector;

typedef struct TensorListC {
int data_type_;
TensorC *tensors_[MAX_PTR_ELEMENT];
size_t element_num_;

TypeIdC tensors_data_type_; // note: element_data_type_ ?
int element_shape_[MAX_SHAPE_SIZE];
size_t element_shape_size_;

int format_;

int max_elements_num_;
} TensorListC;

typedef struct VectorC {
int *data_;
size_t size_;
size_t max_size_;
size_t per_malloc_size_;
} VectorC;

int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, vvector *tensor_shape);
int TensorListMergeShape(int *element_shape, size_t element_shape_size, const int *tmp, size_t tmp_size);
bool TensorListIsFullyDefined(int *shape, size_t shape_size);

int GetBatch(const TensorC *tensor);
int GetHeight(const TensorC *tensor);
int GetWidth(const TensorC *tensor);
int GetChannel(const TensorC *tensor);
int GetElementNum(const TensorC *tensor);
int GetDimensionSize(const TensorC *tensor, const size_t index);

int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);
int CheckAugmentNullSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj);
int CheckAugmentNullSizeInputTwo(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter, size_t inputs_size_obj_0,
size_t inputs_size_obj_1, size_t outputs_size_obj);
int CheckAugmentNullInputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t inputs_size_obj);
int CheckAugmentNullOutputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter, size_t outputs_size_obj);
void SetDataTypeFormat(TensorC *dst, const TensorC *src);

int SetShapeTensor(TensorC *dst, const TensorC *src);
int SetShapeArray(TensorC *dst, int *src, size_t src_size);
int ShapeSet(int *dst_shape, size_t *dst_shape_size, const int *src_shape, size_t src_shape_size);
int ShapePush(int *shape, size_t *shape_size, int value);
int ShapeInsert(int *shape, size_t *shape_size, int index, int value);
int ShapeErase(int *shape, size_t *shape_size, int index);
bool ShapeEqual(const int *shape0, size_t shape0_size, const int *shape1, size_t shape1_size);

void iswap(int *a, int *b);

int imin(int a, int b);
int imax(int a, int b);

int CommonInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);
int FftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

int VectorCInit(VectorC *vc, size_t per_malloc_size);
void VectorCSet(VectorC *vc, const int *src_shape, size_t src_shape_size);
void VectorCPush(VectorC *vc, int value);
void VectorCInsert(VectorC *vc, int index, int value);
void VectorCErase(VectorC *vc, int index);
bool VectorCEqual(VectorC *vc1, VectorC *vc2);
void VectorCFree(VectorC *vc);

#ifdef __cplusplus
}
#endif

#endif // MINDSPORE_LITE_NNACL_COMMON__H_

+ 68
- 0
mindspore/lite/nnacl/infer/concat_infer.c View File

@@ -0,0 +1,68 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/concat_infer.h"

int ConcatInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input0 = inputs[0];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input0);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}

const int *input0_shape = inputs[0]->shape_;
size_t input0_shape_size = inputs[0]->shape_size_;

ConcatParameter *param = (ConcatParameter *)parameter;
int axis = param->axis_ < 0 ? param->axis_ + input0_shape_size : param->axis_;
if (axis < 0 || axis >= input0_shape_size) {
return NNACL_ERR;
}
int input0_shape_without_axis[MAX_SHAPE_SIZE];
size_t input0_shape_without_axis_size = 0;
ShapeSet(input0_shape_without_axis, &input0_shape_without_axis_size, input0_shape, input0_shape_size);
ShapeErase(input0_shape_without_axis, &input0_shape_without_axis_size, axis);
int output_axis_dim = input0_shape[axis];
for (size_t i = 1; i < inputs_size; ++i) {
int shape_tmp[MAX_SHAPE_SIZE];
size_t shape_tmp_size = 0;
ShapeSet(shape_tmp, &shape_tmp_size, inputs[i]->shape_, inputs[i]->shape_size_);
if (shape_tmp_size != input0_shape_size) {
return NNACL_ERR;
}
int axis_tmp = shape_tmp[axis];
ShapeErase(shape_tmp, &shape_tmp_size, axis);
if (!ShapeEqual(input0_shape_without_axis, input0_shape_without_axis_size, shape_tmp, shape_tmp_size)) {
return NNACL_ERR;
}
output_axis_dim += axis_tmp;
}
int output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = input0_shape_size;
for (size_t i = 0; i < input0_shape_size; i++) {
output_shape[i] = input0_shape[i];
}
output_shape[axis] = output_axis_dim;
SetShapeArray(outputs[0], output_shape, output_shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/concat_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CONCAT_INFER_H
#define MINDSPORE_LITE_NNACL_CONCAT_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/concat_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int ConcatInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CONCAT_INFER_H

+ 41
- 0
mindspore/lite/nnacl/infer/constant_of_shape_infer.c View File

@@ -0,0 +1,41 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/constant_of_shape_infer.h"

int ConstantOfShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *in_tensor = inputs[0];
TensorC *out_tensor = outputs[0];
ConstantOfShapeParameter *param = (ConstantOfShapeParameter *)parameter;
out_tensor->data_type_ = (TypeIdC)(param->data_type_);
out_tensor->format_ = in_tensor->format_;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int *in_data = (int *)(in_tensor->data_);
if (in_data == NULL) {
return NNACL_INFER_INVALID;
}
int size = GetElementNum(in_tensor);
SetShapeArray(out_tensor, in_data, size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/constant_of_shape_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H
#define MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/constant_of_shape.h"

#ifdef __cplusplus
extern "C" {
#endif

int ConstantOfShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H

+ 28
- 0
mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.c View File

@@ -0,0 +1,28 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/conv2d_grad_filter_infer.h"

int Conv2dGradFilterInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
if (inputs_size < 2 || outputs_size != 1) {
return NNACL_ERR;
}
Conv2dGradFilterParameter *param = (Conv2dGradFilterParameter *)parameter;
SetDataTypeFormat(outputs[0], inputs[0]);
SetShapeArray(outputs[0], param->filter_shape_, param->filter_shape_size_);
return NNACL_OK;
}

+ 38
- 0
mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.h View File

@@ -0,0 +1,38 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H
#define MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Conv2dGradFilterParameter {
ConvParameter op_parameter_;
int filter_shape_[MAX_SHAPE_SIZE];
size_t filter_shape_size_;
} Conv2dGradFilterParameter;

int Conv2dGradFilterInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H

+ 35
- 0
mindspore/lite/nnacl/infer/conv2d_grad_input_infer.c View File

@@ -0,0 +1,35 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/conv2d_grad_input_infer.h"

int Conv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
if (inputs_size < 2 || outputs_size != 1) {
return NNACL_ERR;
}
const TensorC *in0 = inputs[0];
TensorC *out = outputs[0];

if (in0 == NULL || out == NULL) {
return NNACL_NULL_PTR;
}
SetDataTypeFormat(out, in0);
Conv2dGradInputParameter *param = (Conv2dGradInputParameter *)parameter;
SetShapeArray(out, param->input_shape_, param->input_shape_size_);

return NNACL_OK;
}

+ 38
- 0
mindspore/lite/nnacl/infer/conv2d_grad_input_infer.h View File

@@ -0,0 +1,38 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H
#define MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Conv2dGradInputParameter {
ConvParameter op_parameter_;
int input_shape_[MAX_SHAPE_SIZE];
size_t input_shape_size_;
} Conv2dGradInputParameter;

int Conv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H

+ 95
- 0
mindspore/lite/nnacl/infer/conv2d_infer.c View File

@@ -0,0 +1,95 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nnacl/infer/conv2d_infer.h"

void ConvInferShape(int input_h, int input_w, int *output_h, int *output_w, ConvParameter *param) {
int kernel_w = param->kernel_w_;
int kernel_h = param->kernel_h_;
int stride_w = param->stride_w_;
int stride_h = param->stride_h_;
int dilate_w = param->dilation_w_;
int dilate_h = param->dilation_h_;

if (param->pad_mode_ == Pad_same) { // maybe error
*output_w = ceil((float)(input_w) / (float)(stride_w));
*output_h = ceil((float)(input_h) / (float)(stride_h));
int pad_h_all = ((*output_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - input_h);
int pad_w_all = ((*output_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - input_w);
if (pad_h_all < 0) {
param->pad_u_ = param->pad_d_ = 0;
} else {
param->pad_u_ = pad_h_all / 2;
param->pad_d_ = pad_h_all - param->pad_u_;
}
if (pad_w_all < 0) {
param->pad_l_ = param->pad_r_ = 0;
} else {
param->pad_l_ = pad_w_all / 2;
param->pad_r_ = pad_w_all - param->pad_l_;
}
} else {
*output_w = ceil(((float)(input_w) + param->pad_l_ + param->pad_r_ - ((float)(kernel_w)-1) * (float)(dilate_w)) /
(float)(stride_w));
*output_h = ceil(((float)(input_h) + param->pad_u_ + param->pad_d_ - ((float)(kernel_h)-1) * (float)(dilate_h)) /
(float)(stride_h));
}
}

int Conv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input_tensor = inputs[0];
const TensorC *weight_tensor = inputs[1];
TensorC *out_tensor = outputs[0];

out_tensor->format_ = input_tensor->format_;
out_tensor->data_type_ = input_tensor->data_type_;
ConvParameter *param = (ConvParameter *)parameter;
if (param->group_ == 0) {
param->group_ = weight_tensor->shape_[0];
}
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
const int *in_shape = input_tensor->shape_;
int input_h = in_shape[1];
int input_w = in_shape[2];
int output_w = 0, output_h = 0;

ConvInferShape(input_h, input_w, &output_h, &output_w, param);

int out_shape[MAX_SHAPE_SIZE];
size_t out_shape_size = 0;
ShapeSet(out_shape, &out_shape_size, input_tensor->shape_, input_tensor->shape_size_);
out_shape[1] = output_h > 0 ? output_h : 1;
out_shape[2] = output_w > 0 ? output_w : 1;
out_shape[3] = weight_tensor->shape_[0];
SetShapeArray(out_tensor, out_shape, out_shape_size);

param->input_batch_ = in_shape[0];
param->input_h_ = in_shape[1];
param->input_w_ = in_shape[2];
param->input_channel_ = in_shape[3];
param->output_batch_ = out_shape[0];
param->output_h_ = out_shape[1];
param->output_w_ = out_shape[2];
param->output_channel_ = out_shape[3];

return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/conv2d_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CONV2D_INFER_H
#define MINDSPORE_LITE_NNACL_CONV2D_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int Conv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CONV2D_INFER_H

+ 31
- 0
mindspore/lite/nnacl/infer/crop_infer.c View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/crop_infer.h"

int CropInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
SetDataTypeFormat(outputs[0], inputs[0]);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
SetShapeTensor(outputs[0], inputs[1]);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/crop_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CROP_INFER_H
#define MINDSPORE_LITE_NNACL_CROP_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/crop_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int CropInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CROP_INFER_H

+ 46
- 0
mindspore/lite/nnacl/infer/custom_extract_features_infer.c View File

@@ -0,0 +1,46 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/custom_extract_features_infer.h"

int CustomExtractFeaturesInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
TensorC *output0 = outputs[0];
TensorC *output1 = outputs[1];

output0->data_type_ = kNumberTypeInt32;
output0->format_ = input->format_;
output1->data_type_ = kNumberTypeFloat32;
output1->format_ = input->format_;

if (input->data_ == NULL) {
return NNACL_INFER_INVALID;
}
int string_num = *((const int32_t *)(input->data_)); // maybe error

int res = (string_num == 0 ? 1 : string_num);
output0->shape_size_ = 1;
output0->shape_[0] = res;
output1->shape_size_ = 1;
output1->shape_[0] = res;
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/custom_extract_features_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H
#define MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int CustomExtractFeaturesInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H

+ 39
- 0
mindspore/lite/nnacl/infer/custom_normalize_infer.c View File

@@ -0,0 +1,39 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/custom_normalize_infer.h"

int CustomNormalizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
TensorC *output = outputs[0];

SetDataTypeFormat(output, input);

if (input->data_ == NULL) {
return NNACL_INFER_INVALID;
}
int string_num = *((const int32_t *)(input->data_)); // also look custom_extract_features

output->shape_size_ = 1;
output->shape_[0] = (string_num == 0 ? 1 : string_num);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/custom_normalize_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H
#define MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/softmax_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int CustomNormalizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H

+ 39
- 0
mindspore/lite/nnacl/infer/custom_predict_infer.c View File

@@ -0,0 +1,39 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/custom_predict_infer.h"

int CustomPredictInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
TensorC *output0 = outputs[0];
TensorC *output1 = outputs[1];

CustomPredictParameter *param = (CustomPredictParameter *)parameter;
output0->shape_size_ = 1;
output0->shape_[0] = param->output_num;
output0->data_type_ = kNumberTypeInt32;
output0->format_ = input->format_;
output1->shape_size_ = 1;
output1->shape_[0] = param->output_num;
output1->data_type_ = kNumberTypeFloat32;
output1->format_ = input->format_;
return NNACL_OK;
}

+ 36
- 0
mindspore/lite/nnacl/infer/custom_predict_infer.h View File

@@ -0,0 +1,36 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H
#define MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct CustomPredictParameter {
OpParameter op_parameter_;
int output_num;
} CustomPredictParameter;

int CustomPredictInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H

+ 93
- 0
mindspore/lite/nnacl/infer/deconv2d_infer.c View File

@@ -0,0 +1,93 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/deconv2d_infer.h"

int Deconv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
const TensorC *weight = inputs[1];
TensorC *output = outputs[0];
output->format_ = input->format_;
output->data_type_ = input->data_type_;

ConvParameter *param = (ConvParameter *)parameter;
if (param->group_ == 0) {
param->group_ = weight->shape_[0];
}
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int32_t input_h = GetHeight(input);
int32_t input_w = GetWidth(input);

int32_t output_n = GetBatch(input);
int32_t output_h = 0;
int32_t output_w = 0;
int32_t output_c = GetChannel(weight);
if (param->group_ == GetChannel(input) && param->group_ == GetBatch(weight) && 1 == GetChannel(weight)) {
output_c = GetBatch(weight); /* depthwise */
}

int kernel_w = param->kernel_w_;
int kernel_h = param->kernel_h_;
int stride_w = param->stride_w_;
int stride_h = param->stride_h_;
int dilate_w = param->dilation_w_;
int dilate_h = param->dilation_h_;
int pad_mode = param->pad_mode_;
if (pad_mode == Pad_pad) {
output_h = (input_h - 1) * stride_h + ((kernel_h - 1) * dilate_h + 1) - param->pad_u_ - param->pad_d_;
output_w = (input_w - 1) * stride_w + ((kernel_w - 1) * dilate_w + 1) - param->pad_l_ - param->pad_r_;
} else if (pad_mode == Pad_same) {
output_h = input_h * stride_h;
output_w = input_w * stride_w;
} else if (pad_mode == Pad_valid) {
output_h = (input_h - 1) * stride_h + kernel_h;
output_w = (input_w - 1) * stride_w + kernel_w;
} else {
return NNACL_ERR;
}
output->shape_size_ = 4;
output->shape_[0] = output_n;
output->shape_[1] = output_h;
output->shape_[2] = output_w;
output->shape_[3] = output_c;

if (pad_mode == Pad_same) {
param->pad_u_ = ((input_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - output_h) / 2;
param->pad_l_ = ((input_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - output_w) / 2;
} else if (pad_mode == Pad_valid) {
param->pad_u_ = 0;
param->pad_l_ = 0;
}

const int *in_shape = input->shape_;
param->input_batch_ = in_shape[0];
param->input_h_ = in_shape[1];
param->input_w_ = in_shape[2];
param->input_channel_ = in_shape[3];
param->output_batch_ = output_n;
param->output_h_ = output_h;
param->output_w_ = output_w;
param->output_channel_ = output_c;
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/deconv2d_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DECONV2D_INFER_H
#define MINDSPORE_LITE_NNACL_DECONV2D_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int Deconv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DECONV2D_INFER_H

+ 58
- 0
mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.c View File

@@ -0,0 +1,58 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/dedepthwise_conv2d_infer.h"

int DeDepthwiseConv2DInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter) {
int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
// const TensorC *weight = inputs[1];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_h = input->shape_[1];
int input_w = input->shape_[2];
int input_channel = input->shape_[3];
int output_w = 0, output_h = 0;

ConvParameter *param = (ConvParameter *)parameter;
output_h = param->stride_h_ * (input_h - 1) + param->kernel_h_ - param->pad_u_ - param->pad_d_;
output_w = param->stride_w_ * (input_w - 1) + param->kernel_w_ - param->pad_l_ - param->pad_r_;
if ((output_h + param->pad_u_ + param->pad_d_ - param->kernel_h_) % param->stride_h_ != 0) {
output_h += (output_h + param->pad_l_ + param->pad_r_ - param->kernel_h_) % param->stride_h_;
}
if ((output_w + param->pad_l_ + param->pad_r_ - param->kernel_w_) % param->stride_w_ != 0) {
output_w += (output_w + param->pad_l_ + param->pad_r_ - param->kernel_w_) % param->stride_w_;
}
int out_shape[MAX_SHAPE_SIZE];
size_t out_shape_size = 0;
ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_);
out_shape[1] = output_h;
out_shape[2] = output_w;
if (param->channel_multiplie_ != 1) {
return NNACL_ERR;
}
out_shape[3] = input_channel; // in_channel * out_channel

SetShapeArray(output, out_shape, out_shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H
#define MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int DeDepthwiseConv2DInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H

+ 54
- 0
mindspore/lite/nnacl/infer/depth_to_space_infer.c View File

@@ -0,0 +1,54 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/depth_to_space_infer.h"

int DepthToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
if (input->format_ != Format_NHWC) {
return NNACL_ERR;
}
SetDataTypeFormat(outputs[0], input);
DepthToSpaceParameter *param = (DepthToSpaceParameter *)parameter;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_shape[MAX_SHAPE_SIZE];
size_t input_shape_size = 0;
ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_);
if (input_shape_size != 4) {
return NNACL_PARAM_INVALID;
}

int32_t block_size = param->block_size_;
if (input_shape[kNHWC_C] % (block_size * block_size) != 0 || input_shape[kNHWC_C] == 0) {
return NNACL_PARAM_INVALID;
}
int32_t output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = input_shape_size;
output_shape[kNHWC_N] = input_shape[kNHWC_N];
output_shape[kNHWC_H] = input_shape[kNHWC_H] * block_size;
output_shape[kNHWC_W] = input_shape[kNHWC_W] * block_size;
output_shape[kNHWC_C] = input_shape[kNHWC_C] / (block_size * block_size);
SetShapeArray(outputs[0], output_shape, output_shape_size);
return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/depth_to_space_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H
#define MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/depth_to_space_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int DepthToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H

+ 72
- 0
mindspore/lite/nnacl/infer/depthwise_conv2d_infer.c View File

@@ -0,0 +1,72 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/depthwise_conv2d_infer.h"

int DepthwiseConv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
// const TensorC *weight = inputs[1];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
ConvParameter *param = (ConvParameter *)parameter;

if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
int input_h = input->shape_[1];
int input_w = input->shape_[2];
int input_channel = input->shape_[3];
int output_w = 0, output_h = 0;
param->input_channel_ = input_channel;

if (param->pad_mode_ == Pad_same) { // maybe error
output_h = ceil((float)(input_h) / (float)(param->stride_h_));
output_w = ceil((float)(input_w) / (float)(param->stride_w_));
int pad_h_all = ((output_h - 1) * param->stride_h_ + (param->kernel_h_ - 1) * param->dilation_h_ + 1 - input_h);
int pad_w_all = ((output_w - 1) * param->stride_w_ + (param->kernel_w_ - 1) * param->dilation_w_ + 1 - input_w);
if (pad_h_all > 0) {
param->pad_u_ = pad_h_all / 2;
param->pad_d_ = pad_h_all - param->pad_u_;
}
if (pad_w_all > 0) {
param->pad_l_ = pad_w_all / 2;
param->pad_r_ = pad_w_all - param->pad_l_;
}
} else {
output_h = ceil(((float)(input_h) + param->pad_u_ + param->pad_d_ -
((float)(param->kernel_h_) - 1) * (float)(param->dilation_h_)) /
(float)(param->stride_h_));
output_w = ceil(((float)(input_w) + param->pad_l_ + param->pad_r_ -
((float)(param->kernel_w_) - 1) * (float)(param->dilation_w_)) /
(float)(param->stride_w_));
}
int out_shape[MAX_SHAPE_SIZE];
size_t out_shape_size = 0;
ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_);
out_shape[1] = output_h;
out_shape[2] = output_w;
if (param->channel_multiplie_ != 1) {
return NNACL_ERR;
}
out_shape[3] = input_channel; // in_channel * out_channel
SetShapeArray(output, out_shape, out_shape_size);
return 0;
}

+ 32
- 0
mindspore/lite/nnacl/infer/depthwise_conv2d_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H
#define MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/conv_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int DepthwiseConv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H

+ 76
- 0
mindspore/lite/nnacl/infer/detection_post_process_infer.c View File

@@ -0,0 +1,76 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/detection_post_process_infer.h"

int DetectionPostProcessInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter) {
int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 4);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *boxes = inputs[0];
const TensorC *scores = inputs[1];
const TensorC *anchors = inputs[2];

DetectionPostProcessParameter *param = (DetectionPostProcessParameter *)parameter;
if (scores->shape_[2] < param->num_classes_) {
return NNACL_ERR;
}
if (scores->shape_[2] - param->num_classes_ > 1) {
return NNACL_ERR;
}
if (boxes->shape_[1] != scores->shape_[1]) {
return NNACL_ERR;
}
if (boxes->shape_[1] != anchors->shape_[0]) {
return NNACL_ERR;
}

TensorC *detected_boxes = outputs[0];
TensorC *detected_classes = outputs[1];
TensorC *detected_scores = outputs[2];
TensorC *num_det = outputs[3];

detected_boxes->format_ = boxes->format_;
detected_boxes->data_type_ = kNumberTypeFloat32;
detected_classes->format_ = boxes->format_;
detected_classes->data_type_ = kNumberTypeFloat32;
detected_scores->format_ = boxes->format_;
detected_scores->data_type_ = kNumberTypeFloat32;
num_det->format_ = boxes->format_;
num_det->data_type_ = kNumberTypeFloat32;
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
const int max_detections = param->max_detections_;
const int max_classes_per_detection = param->max_classes_per_detection_;
const int num_detected_boxes = (int)(max_detections * max_classes_per_detection);
detected_boxes->shape_size_ = 3;
detected_boxes->shape_[0] = 1;
detected_boxes->shape_[1] = num_detected_boxes;
detected_boxes->shape_[2] = 4;
detected_classes->shape_size_ = 2;
detected_classes->shape_[0] = 1;
detected_classes->shape_[1] = num_detected_boxes;
detected_scores->shape_size_ = 2;
detected_scores->shape_[0] = 1;
detected_scores->shape_[1] = num_detected_boxes;
num_det->shape_size_ = 1;
num_det->shape_[0] = 1;

return NNACL_OK;
}

+ 32
- 0
mindspore/lite/nnacl/infer/detection_post_process_infer.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H
#define MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H

#include "nnacl/infer/common_infer.h"
#include "nnacl/detection_post_process_parameter.h"

#ifdef __cplusplus
extern "C" {
#endif

int DetectionPostProcessInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
size_t outputs_size, OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H

+ 33
- 0
mindspore/lite/nnacl/infer/dropout_grad_infer.c View File

@@ -0,0 +1,33 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/dropout_grad_infer.h"

int DropoutGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2);
if (check_ret != NNACL_OK) {
return check_ret;
}
const TensorC *input = inputs[0];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
SetShapeTensor(output, input);
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/dropout_grad_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H
#define MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int DropoutGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H

+ 39
- 0
mindspore/lite/nnacl/infer/dropout_infer.c View File

@@ -0,0 +1,39 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/dropout_infer.h"

int DropoutInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
TensorC *output0 = outputs[0];
SetDataTypeFormat(output0, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}
SetShapeTensor(output0, input);
if (outputs_size > 1) {
TensorC *output1 = outputs[1];
SetDataTypeFormat(output1, input);
SetShapeTensor(output1, input);
}
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/dropout_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_DROPOUT_INFER_H
#define MINDSPORE_LITE_NNACL_DROPOUT_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int DropoutInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_DROPOUT_INFER_H

+ 58
- 0
mindspore/lite/nnacl/infer/embedding_lookup_infer.c View File

@@ -0,0 +1,58 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/embedding_lookup_infer.h"

int EmbeddingLookupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
if (check_ret != NNACL_OK) {
return check_ret;
}
if (inputs_size < 2 || outputs_size != 1) {
return NNACL_INPUT_TENSOR_ERROR;
}
const TensorC *params_ = inputs[0];
const TensorC *ids = inputs[inputs_size - 1];
TensorC *output = outputs[0];
SetDataTypeFormat(output, params_);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}

int embedding_shape[MAX_SHAPE_SIZE];
size_t embedding_shape_size = 0;
ShapeSet(embedding_shape, &embedding_shape_size, params_->shape_, params_->shape_size_);
ShapeErase(embedding_shape, &embedding_shape_size, 0);
int output_shape[MAX_SHAPE_SIZE];
size_t output_shape_size = 0;
ShapeSet(output_shape, &output_shape_size, ids->shape_, ids->shape_size_);
for (size_t i = 0; i < embedding_shape_size; ++i) {
ShapePush(output_shape, &output_shape_size, embedding_shape[i]);
}
for (size_t i = 1; i < inputs_size - 1; ++i) {
int embedding_shape_t[MAX_SHAPE_SIZE];
size_t embedding_shape_t_size = 0;
ShapeSet(embedding_shape_t, &embedding_shape_t_size, inputs[i]->shape_, inputs[i]->shape_size_);
ShapeErase(embedding_shape_t, &embedding_shape_t_size, 0);
bool t_equal = ShapeEqual(embedding_shape_t, embedding_shape_t_size, embedding_shape, embedding_shape_size);
if (!t_equal) {
return NNACL_INPUT_TENSOR_ERROR;
}
}
SetShapeArray(output, output_shape, output_shape_size);
return NNACL_OK;
}

+ 31
- 0
mindspore/lite/nnacl/infer/embedding_lookup_infer.h View File

@@ -0,0 +1,31 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H
#define MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H

#include "nnacl/infer/common_infer.h"

#ifdef __cplusplus
extern "C" {
#endif

int EmbeddingLookupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter);

#ifdef __cplusplus
}
#endif
#endif // MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H

+ 46
- 0
mindspore/lite/nnacl/infer/expand_dims_infer.c View File

@@ -0,0 +1,46 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nnacl/infer/expand_dims_infer.h"

int ExpandDimsInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1);
if (check_ret != NNACL_OK) {
return check_ret;
}

const TensorC *input = inputs[0];
TensorC *output = outputs[0];
SetDataTypeFormat(output, input);
if (!parameter->infer_flag_) {
return NNACL_INFER_INVALID;
}

ExpandDimsParameter *param = (ExpandDimsParameter *)parameter;
param->dim_ = ((int32_t *)(inputs[1]->data_))[0];
int dim = param->dim_;
if (dim < 0) {
dim += input->shape_size_ + 1;
}
if (dim > (int)(input->shape_size_)) {
return NNACL_INPUT_TENSOR_ERROR;
}

ShapeSet(output->shape_, &(output->shape_size_), input->shape_, input->shape_size_);
ShapeInsert(output->shape_, &(output->shape_size_), dim, 1);
return NNACL_OK;
}

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save