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