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