Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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 #ifdef INTEL_MKL
     17 
     18 // See docs in ../ops/math_ops.cc.
     19 
     20 #define EIGEN_USE_THREADS
     21 #include <iostream>
     22 #include <vector>
     23 
     24 #include "tensorflow/core/kernels/cwise_ops_common.h"
     25 
     26 #include "tensorflow/core/util/mkl_util.h"
     27 
     28 namespace tensorflow {
     29 
     30 typedef Eigen::ThreadPoolDevice CPUDevice;
     31 
     32 template <typename Device, typename Functor>
     33 class MklBinaryOp : public BinaryOp<Device, Functor> {
     34  public:
     35   explicit MklBinaryOp(OpKernelConstruction* context)
     36       : BinaryOp<Device, Functor>(context) {}
     37 
     38   void Compute(OpKernelContext* context) override {
     39     auto in0 = context->input(0);
     40     auto in1 = context->input(1);
     41     VLOG(1) << "Shapes (start mklbinaryop compute): "
     42             << in0.shape().DebugString() << " _and_ "
     43             << in1.shape().DebugString();
     44 
     45     // Call the TensorFlow BinaryOp Compute method
     46     BinaryOp<Device, Functor>::Compute(context);
     47 
     48     auto out = context->mutable_output(0);
     49     VLOG(1) << "Shapes (output): " << out->shape().DebugString();
     50 
     51     // Pass input shape through to output shape
     52     ForwardMklMetaDataInToOut(context, 0, 0);
     53 
     54     out = context->mutable_output(0);
     55     VLOG(1) << "Shapes (output): " << out->shape().DebugString();
     56   }
     57 };
     58 
     59 //---------- Registration macros for various element-wise ops -----------
     60 // We will need to redefine "REGISTER" to include the mkl_op_registry flag
     61 #pragma push_macro("REGISTER")
     62 #undef REGISTER
     63 #define REGISTER(OP, D, N, F, T)                                    \
     64   REGISTER_KERNEL_BUILDER(Name(N)                                   \
     65                               .Device(DEVICE_##D)                   \
     66                               .TypeConstraint<T>("T")               \
     67                               .Label(mkl_op_registry::kMklOpLabel), \
     68                           OP<D##Device, F<T>>);
     69 
     70 REGISTER5(MklBinaryOp, CPU, "_MklAdd", functor::add, float, Eigen::half, double,
     71           int32, int64);
     72 REGISTER7(MklBinaryOp, CPU, "_MklSub", functor::sub, float, Eigen::half, double,
     73           int32, int64, complex64, complex128);
     74 REGISTER5(MklBinaryOp, CPU, "_MklMul", functor::mul, float, Eigen::half, double,
     75           uint8, int32);
     76 REGISTER5(MklBinaryOp, CPU, "_MklMaximum", functor::maximum, float, Eigen::half,
     77           double, int32, int64);
     78 REGISTER5(MklBinaryOp, CPU, "_MklSquaredDifference",
     79           functor::squared_difference, float, Eigen::half, double, int32,
     80           int64);
     81 
     82 #undef REGISTER
     83 #pragma pop_macro("REGISTER")
     84 //-----------------------------------------------------------------------
     85 
     86 }  // end namespace tensorflow
     87 
     88 #endif  // INTEL_MKL
     89