Home | History | Annotate | Download | only in optimized
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_OPTIMIZED_CBLAS_CONV_H_
     17 #define TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_OPTIMIZED_CBLAS_CONV_H_
     18 
     19 // The Conv implementation based on CBLAS interface. This is only used on iOS
     20 // for now, utilizing Apple's Accelerate framework.
     21 
     22 #if TFLITE_USE_APPLE_ACCELERATE_FOR_CONV
     23 #include <Accelerate/Accelerate.h>
     24 #else
     25 #include "tensorflow/contrib/lite/kernels/internal/optimized/cblas_reference.h"
     26 #endif
     27 
     28 #include "tensorflow/contrib/lite/kernels/internal/optimized/multithreaded_conv.h"
     29 #include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h"
     30 
     31 namespace tflite {
     32 namespace cblas_ops {
     33 
     34 inline void Conv(const float* input_data, const Dims<4>& input_dims,
     35                  const float* filter_data, const Dims<4>& filter_dims,
     36                  const float* bias_data, const Dims<4>& bias_dims,
     37                  int stride_width, int stride_height, int pad_width,
     38                  int pad_height, float output_activation_min,
     39                  float output_activation_max, float* output_data,
     40                  const Dims<4>& output_dims, float* im2col_data,
     41                  const Dims<4>& im2col_dims) {
     42   gemmlowp::ScopedProfilingLabel label("Conv/cblas");
     43 
     44   const float* gemm_input_data = nullptr;
     45   const Dims<4>* gemm_input_dims = nullptr;
     46   const int filter_width = ArraySize(filter_dims, 1);
     47   const int filter_height = ArraySize(filter_dims, 2);
     48   const bool need_im2col = stride_width != 1 || stride_height != 1 ||
     49                            filter_width != 1 || filter_height != 1;
     50   if (need_im2col) {
     51     TFLITE_DCHECK(im2col_data);
     52     optimized_ops::Im2col(input_data, input_dims, stride_width, stride_height,
     53                           pad_width, pad_height, filter_height, filter_width, 0,
     54                           im2col_data, im2col_dims);
     55     gemm_input_data = im2col_data;
     56     gemm_input_dims = &im2col_dims;
     57   } else {
     58     TFLITE_DCHECK(!im2col_data);
     59     gemm_input_data = input_data;
     60     gemm_input_dims = &input_dims;
     61   }
     62 
     63   // The following code computes matrix multiplication c = a * transponse(b)
     64   // with CBLAS, where:
     65   // * `a` is a matrix with dimensions (m, k).
     66   // * `b` is a matrix with dimensions (n, k), so transpose(b) is (k, n).
     67   // * `c` is a matrix with dimensions (m, n).
     68   // The naming of variables are aligned with CBLAS specification here.
     69   const float* a = gemm_input_data;
     70   const float* b = filter_data;
     71   float* c = output_data;
     72   int m = gemm_input_dims->sizes[1] * gemm_input_dims->sizes[2] *
     73           gemm_input_dims->sizes[3];
     74   int n = output_dims.sizes[0];
     75   int k = gemm_input_dims->sizes[0];
     76   // The stride of matrix a, b and c respectively.
     77   int stride_a = k;
     78   int stride_b = k;
     79   int stride_c = n;
     80 
     81   cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, n, k, 1.0f, a,
     82               stride_a, b, stride_b, 0.0f, c, stride_c);
     83 
     84   optimized_ops::AddBiasAndEvalActivationFunction(
     85       bias_data, bias_dims, output_data, output_dims, output_activation_min,
     86       output_activation_max);
     87 }
     88 
     89 }  // namespace cblas_ops
     90 }  // namespace tflite
     91 
     92 #endif  // TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_OPTIMIZED_CBLAS_CONV_H_
     93