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