Home | History | Annotate | Download | only in reference
      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 #ifndef ANDROID_ML_NN_COMMON_OPERATIONS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
     18 #define ANDROID_ML_NN_COMMON_OPERATIONS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
     19 
     20 #include "../common.h"
     21 #include "../types.h"
     22 
     23 namespace android {
     24 namespace nn {
     25 namespace reference_ops {
     26 
     27 template <FusedActivationFunctionType Ac>
     28 void DepthwiseConv(const float* input_data, const Dims<4>& input_dims,
     29                    const float* filter_data, const Dims<4>& filter_dims,
     30                    const float* bias_data, const Dims<4>& bias_dims,
     31                    int stride_width, int stride_height,
     32                    int pad_width, int pad_height, int depth_multiplier,
     33                    float* output_data, const Dims<4>& output_dims) {
     34   const int batches = MatchingArraySize(input_dims, 3, output_dims, 3);
     35   const int output_depth = MatchingArraySize(filter_dims, 0, output_dims, 0);
     36   const int input_height = ArraySize(input_dims, 2);
     37   const int input_width = ArraySize(input_dims, 1);
     38   const int input_depth = ArraySize(input_dims, 0);
     39   const int filter_height = ArraySize(filter_dims, 2);
     40   const int filter_width = ArraySize(filter_dims, 1);
     41   const int output_height = ArraySize(output_dims, 2);
     42   const int output_width = ArraySize(output_dims, 1);
     43   DCHECK(output_depth == input_depth * depth_multiplier);
     44 
     45   for (int b = 0; b < batches; ++b) {
     46     for (int out_y = 0; out_y < output_height; ++out_y) {
     47       for (int out_x = 0; out_x < output_width; ++out_x) {
     48         for (int ic = 0; ic < input_depth; ++ic) {
     49           for (int m = 0; m < depth_multiplier; m++) {
     50             const int oc = m + ic * depth_multiplier;
     51             const int in_x_origin = (out_x * stride_width) - pad_width;
     52             const int in_y_origin = (out_y * stride_height) - pad_height;
     53             float total = 0.f;
     54             for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
     55               for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
     56                 const int in_x = in_x_origin + filter_x;
     57                 const int in_y = in_y_origin + filter_y;
     58                 // If the location is outside the bounds of the input image,
     59                 // use zero as a default value.
     60                 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
     61                     (in_y < input_height)) {
     62                   float input_value =
     63                       input_data[Offset(input_dims, ic, in_x, in_y, b)];
     64                   float filter_value = filter_data[Offset(
     65                       filter_dims, oc, filter_x, filter_y, 0)];
     66                   total += (input_value * filter_value);
     67                 }
     68               }
     69             }
     70             float bias_value = 0.0f;
     71             if (bias_data) {
     72               bias_value = bias_data[Offset(bias_dims, oc, 0, 0, 0)];
     73             }
     74             output_data[Offset(output_dims, oc, out_x, out_y, b)] =
     75                 ActivationFunction<Ac>(total + bias_value);
     76           }
     77         }
     78       }
     79     }
     80   }
     81 }
     82 
     83 }  // end namespace reference_ops
     84 }  // namespace nn
     85 }  // namespace android
     86 
     87 #endif  // ANDROID_ML_NN_COMMON_OPERATIONS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
     88