1 /* Copyright 2020 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_CORE_KERNELS_MKL_MKL_BATCH_MATMUL_HELPER_H_ 16 #define TENSORFLOW_CORE_KERNELS_MKL_MKL_BATCH_MATMUL_HELPER_H_ 17 #ifdef INTEL_MKL 18 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 #include "tensorflow/core/framework/op.h" 21 #include "tensorflow/core/framework/op_kernel.h" 22 #include "tensorflow/core/framework/register_types.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/framework/tensor_shape.h" 25 #include "tensorflow/core/framework/type_traits.h" 26 #include "tensorflow/core/framework/types.h" 27 #include "tensorflow/core/kernels/mkl/mkl_matmul_ops_common.h" 28 #include "tensorflow/core/platform/logging.h" 29 #include "tensorflow/core/platform/types.h" 30 #include "tensorflow/core/util/matmul_bcast.h" 31 #include "tensorflow/core/util/mkl_util.h" 32 33 namespace tensorflow { 34 35 struct MklBatchMatMulHelper { 36 using dims = dnnl::memory::dims; 37 // This method makes the rank (ndims) of input same as the output by creating 38 // new axes to the input. For example, if input shape is [a, b, c, d] and 39 // output shape is [e, f, g, h, i, j], then the reshaped input would have a 40 // shape of [1, 1, a, b, c, d]. ExpandInputDimsToOutputShapeMklBatchMatMulHelper41 void ExpandInputDimsToOutputShape(const TensorShape& input_shape, 42 const TensorShape& output_shape, 43 dims* reshaped_dims) { 44 auto ndims_input = input_shape.dims(); 45 auto ndims_output = output_shape.dims(); 46 auto dim_offset = ndims_output - ndims_input; 47 DCHECK(dim_offset > 0); 48 reshaped_dims->clear(); 49 reshaped_dims->resize(ndims_output, 1); 50 auto input_dims = input_shape.dim_sizes(); 51 for (int dim_idx = 0; dim_idx < ndims_input; ++dim_idx) 52 reshaped_dims->at(dim_idx + dim_offset) = input_dims[dim_idx]; 53 } 54 CreateMatMulParamsMklBatchMatMulHelper55 std::unique_ptr<MklMatMulParams> CreateMatMulParams( 56 const TensorShape& lhs_shape, const TensorShape& rhs_shape, 57 const TensorShape& out_shape, bool& adj_x, bool& adj_y) { 58 const auto ndims_lhs = lhs_shape.dims(); 59 const auto ndims_rhs = rhs_shape.dims(); 60 const auto ndims_out = out_shape.dims(); 61 auto lhs_dims = TFShapeToMklDnnDims(lhs_shape); 62 auto rhs_dims = TFShapeToMklDnnDims(rhs_shape); 63 auto out_dims = TFShapeToMklDnnDims(out_shape); 64 65 // DNNL matmul_primitive requires ranks of inputs and output to be same. 66 // Create dnnl::memory::dims for inputs and output of same rank. 67 // It is assumed here that MatMulBCast object creates output_batch_shape as 68 // a conforming superset of input batch shapes, i.e., ndims_out >= 69 // ndims_lhs and ndims_out >= ndims_rhs. 70 if (ndims_lhs < ndims_out) { 71 ExpandInputDimsToOutputShape(lhs_shape, out_shape, &lhs_dims); 72 } 73 if (ndims_rhs < ndims_out) { 74 ExpandInputDimsToOutputShape(rhs_shape, out_shape, &rhs_dims); 75 } 76 using dim = dnnl::memory::dim; 77 dim m; // Number of rows in x 78 dim k; // Number of columns in x 79 dim n; // Number of columns in y 80 auto lhs_strides = CalculateTFStrides(lhs_dims); 81 auto rhs_strides = CalculateTFStrides(rhs_dims); 82 auto out_strides = CalculateTFStrides(out_dims); 83 84 if (adj_x) { 85 int m_idx = ndims_out - 1; 86 int k_idx = ndims_out - 2; 87 m = lhs_dims[m_idx]; 88 k = lhs_dims[k_idx]; 89 std::swap(lhs_dims[m_idx], lhs_dims[k_idx]); 90 lhs_strides[m_idx] = m; 91 lhs_strides[k_idx] = 1; 92 } 93 94 if (adj_y) { 95 int k_idx = ndims_out - 1; 96 int n_idx = ndims_out - 2; 97 k = rhs_dims[k_idx]; 98 n = rhs_dims[n_idx]; 99 std::swap(rhs_dims[k_idx], rhs_dims[n_idx]); 100 rhs_strides[k_idx] = k; 101 rhs_strides[n_idx] = 1; 102 } 103 104 return std::make_unique<MklMatMulParams>( 105 lhs_dims, rhs_dims, out_dims, lhs_strides, rhs_strides, out_strides); 106 } 107 }; 108 109 } // namespace tensorflow 110 111 #endif // INTEL_MKL 112 #endif // TENSORFLOW_CORE_KERNELS_MKL_MKL_BATCH_MATMUL_HELPER_H_ 113