• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_CONV2D_IMPL_H_
16 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_CONV2D_IMPL_H_
17 
18 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
19 #include "tensorflow/core/kernels/eigen_spatial_convolutions.h"
20 #include "tensorflow/core/platform/types.h"
21 
22 #if defined(TENSORFLOW_USE_CUSTOM_CONTRACTION_KERNEL)
23 #include "tensorflow/core/kernels/eigen_contraction_kernel.h"
24 #endif
25 
26 // 'tensorflow' namespace is used so that int64 and other types don't require
27 // qualification.
28 namespace tensorflow {
29 namespace xla {
30 
31 template <typename EigenDevice, typename ScalarType>
EigenConvImpl(const EigenDevice & device,ScalarType * out,ScalarType * lhs,ScalarType * rhs,Eigen::Index input_batch,Eigen::Index input_rows,Eigen::Index input_cols,Eigen::Index input_channels,Eigen::Index kernel_rows,Eigen::Index kernel_cols,Eigen::Index kernel_channels,Eigen::Index kernel_filters,Eigen::Index output_rows,Eigen::Index output_cols,Eigen::Index row_stride,Eigen::Index col_stride,Eigen::Index padding_top,Eigen::Index padding_bottom,Eigen::Index padding_left,Eigen::Index padding_right,Eigen::Index lhs_row_dilation,Eigen::Index lhs_col_dilation,Eigen::Index rhs_row_dilation,Eigen::Index rhs_col_dilation)32 void EigenConvImpl(const EigenDevice& device, ScalarType* out, ScalarType* lhs,
33                    ScalarType* rhs, Eigen::Index input_batch,
34                    Eigen::Index input_rows, Eigen::Index input_cols,
35                    Eigen::Index input_channels, Eigen::Index kernel_rows,
36                    Eigen::Index kernel_cols, Eigen::Index kernel_channels,
37                    Eigen::Index kernel_filters, Eigen::Index output_rows,
38                    Eigen::Index output_cols, Eigen::Index row_stride,
39                    Eigen::Index col_stride, Eigen::Index padding_top,
40                    Eigen::Index padding_bottom, Eigen::Index padding_left,
41                    Eigen::Index padding_right, Eigen::Index lhs_row_dilation,
42                    Eigen::Index lhs_col_dilation, Eigen::Index rhs_row_dilation,
43                    Eigen::Index rhs_col_dilation) {
44   const Eigen::TensorMap<Eigen::Tensor<const ScalarType, 4, Eigen::RowMajor>,
45                          Eigen::Aligned>
46       input(lhs, input_batch, input_rows, input_cols, input_channels);
47 
48   const Eigen::TensorMap<Eigen::Tensor<const ScalarType, 4, Eigen::RowMajor>,
49                          Eigen::Aligned>
50       kernel(rhs, kernel_rows, kernel_cols, kernel_channels, kernel_filters);
51 
52   Eigen::TensorMap<Eigen::Tensor<ScalarType, 4, Eigen::RowMajor>,
53                    Eigen::Aligned>
54       output(out, input_batch, output_rows, output_cols, kernel_filters);
55 
56   Eigen::array<Eigen::IndexPair<int64>, 1> contract_dims;
57   contract_dims[0] = Eigen::IndexPair<int64>(1, 0);
58 
59   // Molds the output of the patch extraction code into a 2d tensor:
60   // - the first dimension (dims[0]): the patch values to be multiplied with the
61   //   kernels
62   // - the second dimension (dims[1]): everything else
63   Eigen::DSizes<int64, 2> pre_contract_dims;
64   pre_contract_dims[0] = output_cols * output_rows * input_batch;
65   pre_contract_dims[1] = kernel_channels * kernel_cols * kernel_rows;
66 
67   // Molds the output of the contraction into the shape expected by the user:
68   Eigen::DSizes<int64, 4> post_contract_dims;
69   post_contract_dims[0] = input_batch;
70   post_contract_dims[1] = output_rows;
71   post_contract_dims[2] = output_cols;
72   post_contract_dims[3] = kernel_filters;
73 
74   Eigen::DSizes<int64, 2> kernel_dims;
75   kernel_dims[0] = kernel_channels * kernel_cols * kernel_rows;
76   kernel_dims[1] = kernel_filters;
77 
78   // The row and column dimensions must be flipped when passed to Eigen.
79   output.device(device) =
80       input
81           .extract_image_patches(kernel_cols, kernel_rows, col_stride,
82                                  row_stride, rhs_col_dilation, rhs_row_dilation,
83                                  lhs_col_dilation, lhs_row_dilation,
84                                  padding_left, padding_right, padding_top,
85                                  padding_bottom, static_cast<ScalarType>(0.0f))
86           .reshape(pre_contract_dims)
87           .contract(kernel.reshape(kernel_dims), contract_dims)
88           .reshape(post_contract_dims);
89 }
90 
91 }  // namespace xla
92 }  // namespace tensorflow
93 
94 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_CONV2D_IMPL_H_
95