tensorflow/compiler/mlir/tensorflow/transforms/tpu_sharding_identification_pass.cc
// Extracts sharding configurations for all outputs by parsing XlaSharding/
// TPUPartitionedOutput op connected to the retvals/results.
Extracts sharding configurations for all outputs by parsing XlaSharding
TPUPartitionedOutput op connected to the retvals/results.
Extracts sharding configurations for all outputs by parsing XlaSharding TPUPartitionedOutput op connected to the retvals / results .
void IdentifyXlaShardingForComputationOutputs(
    StringRef logical_core_0_sharding, bool use_spmd,
    bool infer_from_computation, tf_device::ClusterFuncOp cluster_func,
    FuncOp func, Builder* builder,
    const llvm::SmallVectorImpl<llvm::StringRef>& sharding_for_args,
    llvm::SmallVectorImpl<llvm::StringRef>& sharding_for_rets) {
  Block& function_block = func.front();
  Operation* terminator = function_block.getTerminator();
  sharding_for_rets.reserve(terminator->getNumOperands());
  llvm::SmallVector<int, 8> aliases;  
  ExtractAliases(func, aliases);
   Replicate sharding is used if `use_spmd` is set.
   Iterate through operands of the terminator. If the preceding op is
   XlaShardingOp, then the provided sharding configuration is added to the
   tf_device.ClusterFunc as an attribute and the function as a result
   attribute.
  for (auto result_and_retval :
       llvm::zip(cluster_func.results(), terminator->getOpOperands())) {
    Value result = std::get<0>(result_and_retval);
    OpOperand& retval = std::get<1>(result_and_retval);
    if (auto result_sharding = GetXlaShardingFromResult(result)) {
      sharding_for_rets.push_back(result_sharding.getValue());
      continue;
    }
    if (auto from_alias =
            GetXlaShardingFromAlias(result, aliases, sharding_for_args)) {
      sharding_for_rets.push_back(from_alias.getValue());
      continue;
    }
    if (infer_from_computation) {
      if (auto retval_sharding = GetXlaShardingFromRetval(retval.get())) {
        sharding_for_rets.push_back(retval_sharding.getValue());
        continue;
      }
    }
    if (use_spmd) {
       If XLA SPMD is enabled, we default to replicate sharding. This way,
       all devices get the whole tensor(s), but if there's an XlaSharding op
       deeper in the function, they can use dynamic-slice to slice off their
       part of the computation.
      sharding_for_rets.push_back(kReplicateSharding);
      continue;
    }
     Otherwise, default to maximal sharding core 0.
    sharding_for_rets.push_back(logical_core_0_sharding);
  }
}
void IdentifyXlaShardingForComputationOutputs ( StringRef logical_core_0_sharding , bool use_spmd , bool infer_from_computation , tf_device :: ClusterFuncOp cluster_func , FuncOp func , Builder * builder , const llvm :: SmallVectorImpl < llvm :: StringRef > & sharding_for_args , llvm :: SmallVectorImpl < llvm :: StringRef > & sharding_for_rets ) { Block & function_block = func . front ( ) ; Operation * terminator = function_block . getTerminator ( ) ; sharding_for_rets . reserve ( terminator -> getNumOperands ( ) ) ; llvm :: SmallVector < int , 8 > aliases ; ExtractAliases ( func , aliases ) ; for ( auto result_and_retval : llvm :: zip ( cluster_func . results ( ) , terminator -> getOpOperands ( ) ) ) { Value result = std :: get < 0 > ( result_and_retval ) ; OpOperand & retval = std :: get < 1 > ( result_and_retval ) ; if ( auto result_sharding = GetXlaShardingFromResult ( result ) ) { sharding_for_rets . push_back ( result_sharding . getValue ( ) ) ; continue ; } if ( auto from_alias = GetXlaShardingFromAlias ( result , aliases , sharding_for_args ) ) { sharding_for_rets . push_back ( from_alias . getValue ( ) ) ; continue ; } if ( infer_from_computation ) { if ( auto retval_sharding = GetXlaShardingFromRetval ( retval . get ( ) ) ) { sharding_for_rets . push_back ( retval_sharding . getValue ( ) ) ; continue ; } } if ( use_spmd ) { sharding_for_rets . push_back ( kReplicateSharding ) ; continue ; } sharding_for_rets . push_back ( logical_core_0_sharding ) ; } }
Extracts sharding configurations for all outputs by parsing XlaSharding
TPUPartitionedOutput op connected to the retvals/results.
Extracts sharding configurations for all outputs by parsing XlaSharding TPUPartitionedOutput op connected to the retvals / results .
// maps return value index to arg index // Iterate through results of `cluster_func`. For output ops, look for // TPUPartitionedOutput ops. // // Replicate sharding is used if `use_spmd` is set. // // Iterate through operands of the terminator. If the preceding op is // XlaShardingOp, then the provided sharding configuration is added to the // tf_device.ClusterFunc as an attribute and the function as a result // attribute. // If XLA SPMD is enabled, we default to replicate sharding. This way, // all devices get the whole tensor(s), but if there's an XlaSharding op // deeper in the function, they can use dynamic-slice to slice off their // part of the computation. // Otherwise, default to maximal sharding core 0.