1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Operations.h"
18#include "CpuOperationUtils.h"
19
20#include "tensorflow/contrib/lite/kernels/internal/optimized/depthwiseconv_float.h"
21#include "tensorflow/contrib/lite/kernels/internal/optimized/depthwiseconv_uint8.h"
22
23namespace android {
24namespace nn {
25
26#define ANDROID_NN_DEPTHWISE_CONV_PARAMETERS                                    \
27    uint32_t height       = getSizeOfDimension(inputShape, 1);                  \
28    uint32_t width        = getSizeOfDimension(inputShape, 2);                  \
29    uint32_t filterHeight = getSizeOfDimension(filterShape, 1);                 \
30    uint32_t filterWidth  = getSizeOfDimension(filterShape, 2);                 \
31    uint32_t outHeight    = getSizeOfDimension(outputShape, 1);                 \
32    uint32_t outWidth     = getSizeOfDimension(outputShape, 2);                 \
33                                                                                \
34    uint32_t paddingHeight = (uint32_t)padding_top;                             \
35    uint32_t paddingWidth = (uint32_t)padding_left;
36
37bool depthwiseConvFloat32(const float* inputData, const Shape& inputShape,
38                          const float* filterData, const Shape& filterShape,
39                          const float* biasData, const Shape& biasShape,
40                          int32_t padding_left, int32_t padding_right,
41                          int32_t padding_top, int32_t padding_bottom,
42                          int32_t stride_width, int32_t stride_height,
43                          int32_t depth_multiplier, int32_t activation,
44                          float* outputData, const Shape& outputShape) {
45
46    ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
47
48    float output_activation_min, output_activation_max;
49    CalculateActivationRangeFloat(activation, &output_activation_min,
50                                  &output_activation_max);
51
52    tflite::optimized_ops::DepthwiseConv(
53            inputData, convertShapeToDims(inputShape),
54            filterData, convertShapeToDims(filterShape),
55            biasData, convertShapeToDims(biasShape),
56            stride_width, stride_height,
57            paddingWidth, paddingHeight, depth_multiplier,
58            output_activation_min, output_activation_max,
59            outputData, convertShapeToDims(outputShape));
60
61    return true;
62}
63
64
65bool depthwiseConvQuant8(const uint8_t* inputData, const Shape& inputShape,
66                         const uint8_t* filterData, const Shape& filterShape,
67                         const int32_t* biasData, const Shape& biasShape,
68                         int32_t padding_left, int32_t padding_right,
69                         int32_t padding_top, int32_t padding_bottom,
70                         int32_t stride_width, int32_t stride_height,
71                         int32_t depth_multiplier, int32_t activation,
72                         uint8_t* outputData, const Shape& outputShape) {
73
74    ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
75
76    float real_multiplier = 0.0;
77    int32_t output_multiplier = 0;
78    int32_t output_shift = 0;
79    int32_t output_activation_min = 0;
80    int32_t output_activation_max = 0;
81
82
83    if (!GetQuantizedConvolutionMultipler(inputShape, filterShape, biasShape,
84                                          outputShape, &real_multiplier) ||
85            !QuantizeMultiplierSmallerThanOne(real_multiplier, &output_multiplier,
86                                              &output_shift)) {
87        return false;
88    }
89    CalculateActivationRangeUint8(activation, outputShape,
90                                  &output_activation_min,
91                                  &output_activation_max);
92
93    uint32_t inputOffset = -inputShape.offset;
94    uint32_t filterOffset = -filterShape.offset;
95    uint32_t outputOffset = outputShape.offset;
96
97    tflite::optimized_ops::DepthwiseConv(
98            inputData, convertShapeToDims(inputShape), inputOffset,
99            filterData, convertShapeToDims(filterShape), filterOffset,
100            biasData, convertShapeToDims(biasShape),
101            stride_width, stride_height,
102            paddingWidth, paddingHeight, depth_multiplier,
103            outputOffset, output_multiplier, output_shift,
104            output_activation_min, output_activation_max,
105            outputData, convertShapeToDims(outputShape));
106
107    return true;
108}
109
110#undef ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
111}  // namespace nn
112}  // namespace android
113