1 /* Copyright 2019 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 #ifndef TENSORFLOW_CORE_UTIL_MATMUL_BCAST_H_ 17 #define TENSORFLOW_CORE_UTIL_MATMUL_BCAST_H_ 18 19 #include <vector> 20 21 #include "tensorflow/core/framework/tensor_shape.h" 22 #include "tensorflow/core/lib/gtl/inlined_vector.h" 23 #include "tensorflow/core/util/bcast.h" 24 25 namespace tensorflow { 26 27 // Simple wrapper over BCast specialized for MatMul. 28 // Provides utilities for broadcasting across batch dimensions for binary 29 // MatMul-like operations. 30 class MatMulBCast { 31 public: 32 using Vec = BCast::Vec; 33 34 MatMulBCast(Vec x, Vec y); 35 IsValid()36 bool IsValid() const { return batch_bcast_ && batch_bcast_->IsValid(); } IsBroadcastingRequired()37 bool IsBroadcastingRequired() const { return broadcasting_required_; } 38 output_batch_size()39 const int64 output_batch_size() const { return output_batch_size_; } x_batch_size()40 const int64 x_batch_size() const { return x_batch_size_; } y_batch_size()41 const int64 y_batch_size() const { return y_batch_size_; } output_batch_shape()42 const TensorShape& output_batch_shape() const { return output_shape_; } 43 44 // Returns the mapping from the flattened output batch indices to x's 45 // flattened batch indices. The result is a vector of length 46 // output_batch_size(). To compute the i'th batch output, a binary matmul-like 47 // operation should use the `x_batch_indices()[i]`th batch index of `x`. 48 // Note: Returns an empty vector if broadcasting is not required. Callers 49 // should only use this when IsBroadcastingRequired() returns true. x_batch_indices()50 const std::vector<int64>& x_batch_indices() const { return x_batch_indices_; } 51 // Returns the mapping from the flattened output batch indices to y's 52 // flattened batch indices. Similar to x_batch_indices(). 53 // Note: Returns an empty vector if broadcasting is not required. Callers 54 // should only use this when IsBroadcastingRequired() returns true. y_batch_indices()55 const std::vector<int64>& y_batch_indices() const { return y_batch_indices_; } 56 57 private: 58 std::unique_ptr<BCast> batch_bcast_; 59 bool broadcasting_required_ = false; 60 int64 x_batch_size_; 61 int64 y_batch_size_; 62 TensorShape output_shape_; 63 int64 output_batch_size_; 64 std::vector<int64> x_batch_indices_; 65 std::vector<int64> y_batch_indices_; 66 }; 67 68 } // namespace tensorflow 69 70 #endif // TENSORFLOW_CORE_UTIL_MATMUL_BCAST_H_ 71