1 /* Copyright 2020 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_COMPILER_MLIR_TENSORFLOW_UTILS_XLA_SHARDING_UTIL_H_ 17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_XLA_SHARDING_UTIL_H_ 18 19 #include "absl/strings/string_view.h" 20 #include "llvm/ADT/ArrayRef.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "mlir/IR/Builders.h" // from @llvm-project 23 #include "mlir/IR/Operation.h" // from @llvm-project 24 #include "mlir/IR/Types.h" // from @llvm-project 25 #include "mlir/IR/Value.h" // from @llvm-project 26 #include "mlir/Support/LogicalResult.h" // from @llvm-project 27 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h" 28 #include "tensorflow/compiler/xla/xla_data.pb.h" 29 #include "tensorflow/core/protobuf/tpu/compile_metadata.pb.h" 30 31 namespace tensorflow { 32 33 inline constexpr absl::string_view kInputShardingAttr = 34 "input_sharding_configuration"; 35 inline constexpr absl::string_view kOutputShardingAttr = 36 "output_sharding_configuration"; 37 38 // Parses "input_sharding_configuration" attribute and returns a list where i-th 39 // element is a list of mlir::Value's which represent inputs for the TPU 40 // computation correponding to i-th logical device. If the attribute does not 41 // exist, the all inputs are placed on logical core 0. 42 mlir::LogicalResult ExtractInputsForLogicalDevices( 43 const int num_cores_per_replica, 44 mlir::tf_device::ClusterFuncOp cluster_func, mlir::OpBuilder* builder, 45 llvm::SmallVectorImpl<llvm::SmallVector<mlir::Value, 4>>* input_list); 46 47 // Extracts a list of OpSharding that represent output sharding configuration of 48 // `tf_device.cluster`. 49 mlir::LogicalResult ParseAndValidateOutputSharding( 50 const int num_cores_per_replica, 51 mlir::tf_device::ClusterFuncOp cluster_func, 52 mlir::SmallVector<xla::OpSharding, 4>* output_sharding_list); 53 54 // Retrieves output types for TPUExecute op representing execution for provided 55 // logical device id. TPUExecute op for different logical device may have 56 // different outputs depending on the output sharding configuration. 57 mlir::LogicalResult GetOutputTypesForLogicalDeviceComputation( 58 const int core_id, llvm::ArrayRef<xla::OpSharding> output_sharding_config, 59 mlir::tf_device::ClusterFuncOp cluster_func, 60 llvm::SmallVectorImpl<mlir::Type>* output_types, 61 llvm::SmallVectorImpl<int>* cluster_to_core_index); 62 63 // Remaps outputs of `new_parallel_execute` op that represent concurrent 64 // execution of the `tf_device.cluster_func` at index `cluster_idx` of 65 // `old_parallel_execute` with its users. 66 mlir::LogicalResult RemapOutputsFromLogicalDevices( 67 const mlir::Location& location, 68 llvm::ArrayRef<xla::OpSharding> output_sharding_config, 69 llvm::SmallVector<llvm::SmallVector<int, 4>, 4> cluster_to_core_index, 70 mlir::tf_device::ParallelExecuteOp old_parallel_execute, int cluster_idx, 71 mlir::tf_device::ParallelExecuteOp new_parallel_execute, 72 mlir::OpBuilder* builder); 73 74 // Determines each logical core argument to metadata argument index mapping, 75 // based on sharding. The return value is indexed first by logical core then by 76 // argument index. 77 llvm::SmallVector<llvm::SmallVector<int64_t, 4>, 4> GetMetadataArgumentMapping( 78 const tpu::TPUCompileMetadataProto& metadata); 79 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_XLA_SHARDING_UTIL_H_ 83