| 63 | // Transpose of N-dimensional tensor using OneDNN |
| 64 | template <typename T> |
| 65 | Status MKLTransposeND(OpKernelContext* context, const Tensor& in_tensor, |
| 66 | Tensor* out_tensor, const gtl::ArraySlice<int32>& perm) { |
| 67 | try { |
| 68 | engine cpu_engine = engine(ENGINE_CPU, 0); |
| 69 | MklDnnData<T> in(&cpu_engine); |
| 70 | MklDnnData<T> out(&cpu_engine); |
| 71 | |
| 72 | memory::dims in_dims = TFShapeToMklDnnDims(in_tensor.shape()); |
| 73 | memory::dims out_dims = TFShapeToMklDnnDims(out_tensor->shape()); |
| 74 | memory::dims in_strides = CalculateTFStrides(in_dims); |
| 75 | // Reorder output strides based on permutation requested. |
| 76 | memory::dims out_strides = |
| 77 | ReorderStrides(CalculateTFStrides(out_dims), perm); |
| 78 | |
| 79 | std::shared_ptr<stream> transpose_stream; |
| 80 | in.SetUsrMem(in_dims, in_strides, &in_tensor); |
| 81 | // Output dimensions are same as input dimensions. We adjust the layout |
| 82 | // using strides. |
| 83 | out.SetUsrMem(in_dims, out_strides, out_tensor); |
| 84 | |
| 85 | std::vector<primitive> net; |
| 86 | MklDnnThreadPool eigen_tp(context); |
| 87 | auto* prim = FindOrCreateReorder<T>(in.GetUsrMem(), out.GetUsrMem()); |
| 88 | transpose_stream.reset(CreateStream(&eigen_tp, prim->GetEngine())); |
| 89 | in.SetUsrMemDataHandle(&in_tensor, transpose_stream); |
| 90 | out.SetUsrMemDataHandle(out_tensor, transpose_stream); |
| 91 | net.push_back(*(prim->GetPrimitive())); |
| 92 | std::vector<MemoryArgsMap> net_args; |
| 93 | net_args.push_back({{DNNL_ARG_FROM, *in.GetUsrMem()}, |
| 94 | {DNNL_ARG_TO, *out.GetUsrMem()}}); |
| 95 | execute_primitives(net, transpose_stream, net_args); |
| 96 | |
| 97 | return Status::OK(); |
| 98 | } catch (dnnl::error& e) { |
| 99 | string error_msg = "Status: " + std::to_string(e.status) + |
| 100 | ", message: " + std::string(e.message) + ", in file " + |
| 101 | std::string(__FILE__) + ":" + std::to_string(__LINE__); |
| 102 | return errors::Aborted("Operation received an exception:", error_msg); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 |
nothing calls this directly
no test coverage detected