tensorflow/compiler/mlir/tensorflow/transforms/tpu_sharding_identification_pass.cc
// Extracts sharding configurations for all inputs by parsing XlaSharding/
// TPUPartitionedInput op connected to the operands/arguments. If argument to
// the `cluster_func` directly feeds into another function call op, then
// recursively walk the function definition to find the connected XlaSharding
// op.
Extracts sharding configurations for all inputs by parsing XlaSharding
TPUPartitionedInput op connected to the operands/arguments. If argument to
the `cluster_func` directly feeds into another function call op, then
recursively walk the function definition to find the connected XlaSharding
op.
Extracts sharding configurations for all inputs by parsing XlaSharding TPUPartitionedInput op connected to the operands / arguments . If argument to the ` cluster_func ` directly feeds into another function call op then recursively walk the function definition to find the connected XlaSharding op .
void IdentifyXlaShardingForComputationInputs(
    StringRef logical_core_0_sharding, bool use_spmd,
    bool infer_from_computation, tf_device::ClusterFuncOp cluster_func,
    FuncOp func, Builder* builder,
    llvm::SmallVectorImpl<llvm::StringRef>& sharding_for_args) {
  Block& function_block = func.front();
  sharding_for_args.reserve(function_block.getNumArguments());
   Replicate sharding is used if `use_spmd` is set.
   Iterate through input arguments to the entry block of
   tf_device.ClusterFunc. For input ops, look for XlaSharding ops.
   XlaSharding ops can:
     1) Directly follow the input argument if input argument has non-resource
        types.
     2) Follow ReadVariableOp if the input type is of resource type.
     3) Follow IdentityOp or CastOp after above cases (1), (2).
   Sharding configurations are added to the tf_device.ClusterFunc as an
   attribute and the function as an argument attribute.
  for (auto operand_and_arg :
       llvm::zip(cluster_func.operands(), function_block.getArguments())) {
    Value operand = std::get<0>(operand_and_arg);
    BlockArgument arg = std::get<1>(operand_and_arg);
    if (auto operand_sharding = GetXlaShardingFromOperand(operand)) {
      sharding_for_args.push_back(operand_sharding.getValue());
      continue;
    }
    if (infer_from_computation) {
      auto arg_sharding = GetXlaShardingFromArg(arg);
      if (arg_sharding) {
        sharding_for_args.push_back(arg_sharding.getValue());
        continue;
      }
    }
    if (use_spmd && !IsMaximalVariable(operand)) {
       If XLA SPMD is enabled, host variables or non-variable per-replica
       inputs should take on replicate sharding, so that every device gets the
       whole tensor(s) (and can slice them up later). Exclude device
       variables, which always should take maximal sharding.
      sharding_for_args.push_back(kReplicateSharding);
      continue;
    }
     Otherwise, default to maximal sharding core 0.
    sharding_for_args.push_back(logical_core_0_sharding);
  }
}
void IdentifyXlaShardingForComputationInputs ( StringRef logical_core_0_sharding , bool use_spmd , bool infer_from_computation , tf_device :: ClusterFuncOp cluster_func , FuncOp func , Builder * builder , llvm :: SmallVectorImpl < llvm :: StringRef > & sharding_for_args ) { Block & function_block = func . front ( ) ; sharding_for_args . reserve ( function_block . getNumArguments ( ) ) ; for ( auto operand_and_arg : llvm :: zip ( cluster_func . operands ( ) , function_block . getArguments ( ) ) ) { Value operand = std :: get < 0 > ( operand_and_arg ) ; BlockArgument arg = std :: get < 1 > ( operand_and_arg ) ; if ( auto operand_sharding = GetXlaShardingFromOperand ( operand ) ) { sharding_for_args . push_back ( operand_sharding . getValue ( ) ) ; continue ; } if ( infer_from_computation ) { auto arg_sharding = GetXlaShardingFromArg ( arg ) ; if ( arg_sharding ) { sharding_for_args . push_back ( arg_sharding . getValue ( ) ) ; continue ; } } if ( use_spmd && ! IsMaximalVariable ( operand ) ) { sharding_for_args . push_back ( kReplicateSharding ) ; continue ; } sharding_for_args . push_back ( logical_core_0_sharding ) ; } }
Extracts sharding configurations for all inputs by parsing XlaSharding
TPUPartitionedInput op connected to the operands/arguments.
Extracts sharding configurations for all inputs by parsing XlaSharding TPUPartitionedInput op connected to the operands / arguments .
// Look up function definition from module. // Iterate through operands of `cluster_func`. // The computation operand can either be: //   1) a TPUPartitionedInput Op if the input has a non-resource type; //   2) a ReadVariableOp else. // // Replicate sharding is used if `use_spmd` is set. // // Iterate through input arguments to the entry block of // tf_device.ClusterFunc. For input ops, look for XlaSharding ops. // XlaSharding ops can: //   1) Directly follow the input argument if input argument has non-resource //      types. //   2) Follow ReadVariableOp if the input type is of resource type. //   3) Follow IdentityOp or CastOp after above cases (1), (2). // // Sharding configurations are added to the tf_device.ClusterFunc as an // attribute and the function as an argument attribute. // If XLA SPMD is enabled, host variables or non-variable per-replica // inputs should take on replicate sharding, so that every device gets the // whole tensor(s) (and can slice them up later). Exclude device // variables, which always should take maximal sharding. // Otherwise, default to maximal sharding core 0.