Home | History | Annotate | Download | only in kernels
      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 // XLA-specific MatMul Op.
     17 
     18 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
     19 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     20 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 
     23 namespace tensorflow {
     24 namespace {
     25 
     26 constexpr std::array<DataType, 5> kMatmulTypes = {
     27     {DT_HALF, DT_BFLOAT16, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64}};
     28 
     29 class MatMulOp : public XlaOpKernel {
     30  public:
     31   explicit MatMulOp(OpKernelConstruction* ctx, bool is_sparse = false)
     32       : XlaOpKernel(ctx), is_sparse_(is_sparse) {
     33     OP_REQUIRES_OK(ctx, ctx->GetAttr("transpose_a", &transpose_a_));
     34     OP_REQUIRES_OK(ctx, ctx->GetAttr("transpose_b", &transpose_b_));
     35     if (is_sparse) {
     36       OP_REQUIRES_OK(ctx, ctx->GetAttr("Ta", &a_type_));
     37       OP_REQUIRES_OK(ctx, ctx->GetAttr("Tb", &b_type_));
     38       // SparseMatMul is actually dense matmul with a hint that one or
     39       // both of the inputs may contain a lot of zeroes. On CPU these
     40       // inputs are dynamically converted to sparse representation
     41       // before multiplication. For now in XLA we ignore the hints
     42       // and always do dense multiplication.
     43       bool dummy_is_sparse;
     44       OP_REQUIRES_OK(ctx, ctx->GetAttr("a_is_sparse", &dummy_is_sparse));
     45       OP_REQUIRES_OK(ctx, ctx->GetAttr("b_is_sparse", &dummy_is_sparse));
     46     }
     47   }
     48 
     49   ~MatMulOp() override = default;
     50 
     51   void Compile(XlaOpKernelContext* ctx) override {
     52     const TensorShape a_shape = ctx->InputShape(0);
     53     const TensorShape b_shape = ctx->InputShape(1);
     54 
     55     // Check that the dimensions of the two matrices are valid.
     56     OP_REQUIRES(ctx, TensorShapeUtils::IsMatrix(a_shape),
     57                 errors::InvalidArgument("In[0] is not a matrix"));
     58     OP_REQUIRES(ctx, TensorShapeUtils::IsMatrix(b_shape),
     59                 errors::InvalidArgument("In[1] is not a matrix"));
     60     int first_index = transpose_a_ ? 0 : 1;
     61     int second_index = transpose_b_ ? 1 : 0;
     62 
     63     OP_REQUIRES(ctx,
     64                 a_shape.dim_size(first_index) == b_shape.dim_size(second_index),
     65                 errors::InvalidArgument("Matrix size-compatible: In[0]: ",
     66                                         a_shape.DebugString(), ", In[1]: ",
     67                                         b_shape.DebugString()));
     68 
     69     xla::ComputationDataHandle a = ctx->Input(0);
     70     xla::ComputationDataHandle b = ctx->Input(1);
     71     if (is_sparse_) {
     72       if (a_type_ == DT_BFLOAT16) {
     73         a = ctx->builder()->ConvertElementType(a, xla::F32);
     74       }
     75       if (b_type_ == DT_BFLOAT16) {
     76         b = ctx->builder()->ConvertElementType(b, xla::F32);
     77       }
     78     }
     79     auto lhs = (transpose_a_) ? ctx->builder()->Transpose(a, {1, 0}) : a;
     80     auto rhs = (transpose_b_) ? ctx->builder()->Transpose(b, {1, 0}) : b;
     81     ctx->SetOutput(0, ctx->builder()->Dot(lhs, rhs));
     82   }
     83 
     84  private:
     85   bool is_sparse_;
     86   bool transpose_a_;
     87   bool transpose_b_;
     88   DataType a_type_;
     89   DataType b_type_;
     90 };
     91 
     92 REGISTER_XLA_OP(Name("MatMul").TypeConstraint("T", kMatmulTypes), MatMulOp);
     93 
     94 class SparseMatMulOp : public MatMulOp {
     95  public:
     96   explicit SparseMatMulOp(OpKernelConstruction* ctx) : MatMulOp(ctx, true) {}
     97 
     98   ~SparseMatMulOp() override = default;
     99 };
    100 
    101 REGISTER_XLA_OP(Name("SparseMatMul"), SparseMatMulOp);
    102 
    103 }  // namespace
    104 }  // namespace tensorflow
    105