|
| 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 | +// See docs in ../ops/array_ops.cc. |
| 17 | + |
| 18 | +#ifdef INTEL_MKL |
| 19 | +#define EIGEN_USE_THREADS |
| 20 | + |
| 21 | +#include "tensorflow/core/kernels/transpose_op.h" |
| 22 | +#include "tensorflow/core/kernels/transpose_functor.h" |
| 23 | +#include "third_party/mkl/include/mkl_trans.h" |
| 24 | + |
| 25 | +namespace tensorflow { |
| 26 | + |
| 27 | +// output = TransposeOp(T<any> input, T<int32> perm) takes a tensor |
| 28 | +// of type T and rank N, and a permutation of 0, 1, ..., N-1. It |
| 29 | +// shuffles the dimensions of the input tensor according to permutation. |
| 30 | +// |
| 31 | +// Specifically, the returned tensor output meets the following condition: |
| 32 | +// 1) output.dims() == input.dims(); |
| 33 | +// 2) output.dim_size(i) == input.dim_size(perm[i]); |
| 34 | +// 3) output.tensor<T, N>(i_0, i_1, ..., i_N-1) == |
| 35 | +// input.tensor<T, N>(j_0, j_1, ..., j_N-1), |
| 36 | +// where i_s == j_{perm[s]} |
| 37 | +// |
| 38 | +// REQUIRES: perm is a vector of int32. |
| 39 | +// REQUIRES: input.dims() == perm.size(). |
| 40 | +// REQUIRES: perm is a permutation. |
| 41 | + |
| 42 | +Status MklTransposeCpuOp::DoTranspose(OpKernelContext* ctx, const Tensor& in, |
| 43 | + gtl::ArraySlice<int32> perm, |
| 44 | + Tensor* out) { |
| 45 | + if (in.dims() == 2 && in.dtype() == DT_FLOAT) { |
| 46 | + float* user_o = out->flat<float>().data(); |
| 47 | + const float* user_i = in.flat<float>().data(); |
| 48 | + |
| 49 | + // Documentation here: https://software.intel.com/en-us/node/520863 |
| 50 | + // Parameters: (ordering:row-major, operation:transpose, num_rows, num_cols, |
| 51 | + // alpha (for scaling), array, dist_bet_adjacent_cols/rows |
| 52 | + // (source), array, dist_bet_adjacent_cols/rows (dest)) |
| 53 | + mkl_somatcopy('R', 'T', in.dim_size(0), in.dim_size(1), 1, |
| 54 | + user_i, in.dim_size(1), |
| 55 | + user_o, in.dim_size(0)); |
| 56 | + |
| 57 | + return Status::OK(); |
| 58 | + } |
| 59 | + |
| 60 | + // Fallback to eigen if transpose parameters not supported by MKL |
| 61 | + typedef Eigen::ThreadPoolDevice CPUDevice; |
| 62 | + return ::tensorflow::DoTranspose(ctx->eigen_device<CPUDevice>(), in, perm, |
| 63 | + out); |
| 64 | +} // MklTransposeCpuOp::DoTranspose |
| 65 | +} // namespace tensorflow |
| 66 | + |
| 67 | +#endif // INTEL_MKL |
0 commit comments