tensorflow/compiler/xla/service/spmd/dot_handler.cc
// Estimate the number of iterations of a subsequent windowed einsum
// partitioning if its partitioned in the non-contracting dimensions.
// First value returned is the estimate of the number of iterations if LHS is
// matched while the second is the number of iterations if RHS is matched.
Estimate the number of iterations of a subsequent windowed einsum
partitioning if its partitioned in the non-contracting dimensions.
First value returned is the estimate of the number of iterations if LHS is
matched while the second is the number of iterations if RHS is matched.
Estimate the number of iterations of a subsequent windowed einsum partitioning if its partitioned in the non - contracting dimensions . First value returned is the estimate of the number of iterations if LHS is matched while the second is the number of iterations if RHS is matched .
std::pair<absl::optional<int64_t>, absl::optional<int64_t>>
EstimateWindowedEinsumIterationsForNonContractingPartitioning(
    const DotConvDimsMapping& dims_mapping, const PartitionedHlo& lhs,
    const PartitionedHlo& rhs, const Shape& output_base_shape,
    const HloSharding& output_sharding, const SpmdPartitionerOptions& options,
    int64_t num_partitions, int64_t lhs_non_contracting_partitions,
    int64_t rhs_non_contracting_partitions, int64_t lhs_matching_partitions,
    int64_t rhs_matching_partitions, int64_t lhs_contracting_partitions,
    int64_t rhs_contracting_partitions,
    int64_t output_lhs_non_contracting_partitions,
    int64_t output_rhs_non_contracting_partitions, int64_t lhs_batch_partitions,
    int64_t rhs_batch_partitions, const Window& conv_window) {
  const DotDimensionIndexMapping indices_map = ComputeDimensionIndexMapping(
      dims_mapping, lhs.base_shape().rank(), rhs.base_shape().rank(),
      output_base_shape.rank());
  auto subsequent_einsum_iterations_estimate =
      [&](bool assume_lhs_match) -> absl::optional<int64_t> {
    const std::vector<DotConvDimsMapping::DimsMapping>&
        matching_non_contracting_dims =
            assume_lhs_match ? dims_mapping.lhs_non_contracting_dims
                             : dims_mapping.rhs_non_contracting_dims;
    const std::vector<DotConvDimsMapping::DimsMapping>&
        other_non_contracting_dims =
            assume_lhs_match ? dims_mapping.rhs_non_contracting_dims
                             : dims_mapping.lhs_non_contracting_dims;
    const std::vector<int64_t>& output_to_matching_indices =
        assume_lhs_match ? indices_map.output_to_lhs_indices
                         : indices_map.output_to_rhs_indices;
    const std::vector<int64_t>& output_to_other_indices =
        assume_lhs_match ? indices_map.output_to_rhs_indices
                         : indices_map.output_to_lhs_indices;
    const std::vector<int64_t>& matching_to_output_indices =
        assume_lhs_match ? indices_map.lhs_to_output_indices
                         : indices_map.rhs_to_output_indices;
    const std::vector<int64_t>& other_to_output_indices =
        assume_lhs_match ? indices_map.rhs_to_output_indices
                         : indices_map.lhs_to_output_indices;
    const HloSharding& matching_sharding =
        assume_lhs_match ? lhs.sharding() : rhs.sharding();
    const HloSharding& other_sharding =
        assume_lhs_match ? rhs.sharding() : lhs.sharding();
    const PartitionedHlo& matching_partitioned = assume_lhs_match ? lhs : rhs;
    const PartitionedHlo& other_partitioned = assume_lhs_match ? rhs : lhs;
    const int64_t matching_non_contracting_partitions =
        assume_lhs_match ? lhs_non_contracting_partitions
                         : rhs_non_contracting_partitions;
    const int64_t other_non_contracting_partitions =
        assume_lhs_match ? rhs_non_contracting_partitions
                         : lhs_non_contracting_partitions;
    const int64_t matching_contracting_partitions =
        assume_lhs_match ? lhs_contracting_partitions
                         : rhs_contracting_partitions;
    const int64_t other_contracting_partitions =
        assume_lhs_match ? rhs_contracting_partitions
                         : lhs_contracting_partitions;
    const int64_t output_matching_non_contracting_partitions =
        assume_lhs_match ? output_lhs_non_contracting_partitions
                         : output_rhs_non_contracting_partitions;
    const int64_t output_other_non_contracting_partitions =
        assume_lhs_match ? output_rhs_non_contracting_partitions
                         : output_lhs_non_contracting_partitions;
    const int64_t matching_batch_partitions =
        assume_lhs_match ? lhs_batch_partitions : rhs_batch_partitions;
    const int64_t other_batch_partitions =
        assume_lhs_match ? rhs_batch_partitions : lhs_batch_partitions;
    const int64_t matching_matched_non_contracting_partitions =
        assume_lhs_match ? lhs_non_contracting_partitions
                         : rhs_non_contracting_partitions;
    std::vector<int64_t> output_dims;
    output_dims.reserve(matching_non_contracting_dims.size());
    for (const DotConvDimsMapping::DimsMapping& dim :
         matching_non_contracting_dims) {
      output_dims.push_back(dim.output);
    }
    GroupedSharding output_grouped =
        hlo_sharding_util::GroupShardingOnDims(output_sharding, output_dims);
    GroupedSharding matching_grouped =
        GetNonContractingPartitionGroupedShardingForMatchedOperand(
            assume_lhs_match, matching_sharding, output_sharding,
            matching_non_contracting_dims);
    absl::optional<GroupedSharding> other_grouped =
        GetNonContractingPartitionGroupedShardingForOtherOperand(
            assume_lhs_match, output_base_shape,
            other_partitioned.hlo()->shape(), other_contracting_partitions,
            other_non_contracting_partitions, matching_contracting_partitions,
            output_other_non_contracting_partitions, other_sharding,
            output_sharding, matching_non_contracting_dims,
            other_non_contracting_dims, dims_mapping.contracting_dims);
    if (!other_grouped) {
      return absl::nullopt;
    }
    absl::optional<HloSharding> output_sharding_transposed_to_match_matching =
        hlo_sharding_util::TransposeShardingWithCollapsedDims(
            output_grouped.sharding, output_to_matching_indices,
            matching_to_output_indices);
    absl::optional<HloSharding> output_sharding_transposed_to_match_other =
        hlo_sharding_util::TransposeShardingWithCollapsedDims(
            output_grouped.sharding, output_to_other_indices,
            other_to_output_indices);
    const int64_t new_num_partitions =
        num_partitions / matching_non_contracting_partitions;
    absl::optional<WindowedEinsumConfig> e_config =
        GetWindowedEinsumConfiguration(
            new_num_partitions, output_matching_non_contracting_partitions,
            output_other_non_contracting_partitions,
            other_contracting_partitions, other_non_contracting_partitions,
            other_batch_partitions, matching_contracting_partitions,
            matching_non_contracting_partitions /
                matching_matched_non_contracting_partitions,
            matching_batch_partitions,
            ShapeSizeInBytes(other_partitioned.base_shape()),
            ShapeSizeInBytes(matching_partitioned.base_shape()) /
                matching_non_contracting_partitions,
            ShapeSizeInBytes(
                GetPerGroupBaseShape(output_grouped, output_base_shape)),
            options, output_sharding_transposed_to_match_matching,
            output_sharding_transposed_to_match_other,
            matching_grouped.sharding, other_grouped->sharding, conv_window,
            dims_mapping);
    return e_config ? new_num_partitions
                    : absl::optional<int64_t>(absl::nullopt);
  };
  absl::optional<int64_t> lhs_matching_iterations;
  if (lhs_matching_partitions != 0) {
    lhs_matching_iterations = subsequent_einsum_iterations_estimate(true);
  }
  absl::optional<int64_t> rhs_matching_iterations;
  if (rhs_matching_partitions != 0) {
    rhs_matching_iterations = subsequent_einsum_iterations_estimate(false);
  }
  return std::make_pair(lhs_matching_iterations, rhs_matching_iterations);
}
std :: pair < absl :: optional < int64_t > , absl :: optional < int64_t > > EstimateWindowedEinsumIterationsForNonContractingPartitioning ( const DotConvDimsMapping & dims_mapping , const PartitionedHlo & lhs , const PartitionedHlo & rhs , const Shape & output_base_shape , const HloSharding & output_sharding , const SpmdPartitionerOptions & options , int64_t num_partitions , int64_t lhs_non_contracting_partitions , int64_t rhs_non_contracting_partitions , int64_t lhs_matching_partitions , int64_t rhs_matching_partitions , int64_t lhs_contracting_partitions , int64_t rhs_contracting_partitions , int64_t output_lhs_non_contracting_partitions , int64_t output_rhs_non_contracting_partitions , int64_t lhs_batch_partitions , int64_t rhs_batch_partitions , const Window & conv_window ) { const DotDimensionIndexMapping indices_map = ComputeDimensionIndexMapping ( dims_mapping , lhs . base_shape ( ) . rank ( ) , rhs . base_shape ( ) . rank ( ) , output_base_shape . rank ( ) ) ; auto subsequent_einsum_iterations_estimate = [ & ] ( bool assume_lhs_match ) -> absl :: optional < int64_t > { const std :: vector < DotConvDimsMapping :: DimsMapping > & matching_non_contracting_dims = assume_lhs_match ? dims_mapping . lhs_non_contracting_dims : dims_mapping . rhs_non_contracting_dims ; const std :: vector < DotConvDimsMapping :: DimsMapping > & other_non_contracting_dims = assume_lhs_match ? dims_mapping . rhs_non_contracting_dims : dims_mapping . lhs_non_contracting_dims ; const std :: vector < int64_t > & output_to_matching_indices = assume_lhs_match ? indices_map . output_to_lhs_indices : indices_map . output_to_rhs_indices ; const std :: vector < int64_t > & output_to_other_indices = assume_lhs_match ? indices_map . output_to_rhs_indices : indices_map . output_to_lhs_indices ; const std :: vector < int64_t > & matching_to_output_indices = assume_lhs_match ? indices_map . lhs_to_output_indices : indices_map . rhs_to_output_indices ; const std :: vector < int64_t > & other_to_output_indices = assume_lhs_match ? indices_map . rhs_to_output_indices : indices_map . lhs_to_output_indices ; const HloSharding & matching_sharding = assume_lhs_match ? lhs . sharding ( ) : rhs . sharding ( ) ; const HloSharding & other_sharding = assume_lhs_match ? rhs . sharding ( ) : lhs . sharding ( ) ; const PartitionedHlo & matching_partitioned = assume_lhs_match ? lhs : rhs ; const PartitionedHlo & other_partitioned = assume_lhs_match ? rhs : lhs ; const int64_t matching_non_contracting_partitions = assume_lhs_match ? lhs_non_contracting_partitions : rhs_non_contracting_partitions ; const int64_t other_non_contracting_partitions = assume_lhs_match ? rhs_non_contracting_partitions : lhs_non_contracting_partitions ; const int64_t matching_contracting_partitions = assume_lhs_match ? lhs_contracting_partitions : rhs_contracting_partitions ; const int64_t other_contracting_partitions = assume_lhs_match ? rhs_contracting_partitions : lhs_contracting_partitions ; const int64_t output_matching_non_contracting_partitions = assume_lhs_match ? output_lhs_non_contracting_partitions : output_rhs_non_contracting_partitions ; const int64_t output_other_non_contracting_partitions = assume_lhs_match ? output_rhs_non_contracting_partitions : output_lhs_non_contracting_partitions ; const int64_t matching_batch_partitions = assume_lhs_match ? lhs_batch_partitions : rhs_batch_partitions ; const int64_t other_batch_partitions = assume_lhs_match ? rhs_batch_partitions : lhs_batch_partitions ; const int64_t matching_matched_non_contracting_partitions = assume_lhs_match ? lhs_non_contracting_partitions : rhs_non_contracting_partitions ; std :: vector < int64_t > output_dims ; output_dims . reserve ( matching_non_contracting_dims . size ( ) ) ; for ( const DotConvDimsMapping :: DimsMapping & dim : matching_non_contracting_dims ) { output_dims . push_back ( dim . output ) ; } GroupedSharding output_grouped = hlo_sharding_util :: GroupShardingOnDims ( output_sharding , output_dims ) ; GroupedSharding matching_grouped = GetNonContractingPartitionGroupedShardingForMatchedOperand ( assume_lhs_match , matching_sharding , output_sharding , matching_non_contracting_dims ) ; absl :: optional < GroupedSharding > other_grouped = GetNonContractingPartitionGroupedShardingForOtherOperand ( assume_lhs_match , output_base_shape , other_partitioned . hlo ( ) -> shape ( ) , other_contracting_partitions , other_non_contracting_partitions , matching_contracting_partitions , output_other_non_contracting_partitions , other_sharding , output_sharding , matching_non_contracting_dims , other_non_contracting_dims , dims_mapping . contracting_dims ) ; if ( ! other_grouped ) { return absl :: nullopt ; } absl :: optional < HloSharding > output_sharding_transposed_to_match_matching = hlo_sharding_util :: TransposeShardingWithCollapsedDims ( output_grouped . sharding , output_to_matching_indices , matching_to_output_indices ) ; absl :: optional < HloSharding > output_sharding_transposed_to_match_other = hlo_sharding_util :: TransposeShardingWithCollapsedDims ( output_grouped . sharding , output_to_other_indices , other_to_output_indices ) ; const int64_t new_num_partitions = num_partitions / matching_non_contracting_partitions ; absl :: optional < WindowedEinsumConfig > e_config = GetWindowedEinsumConfiguration ( new_num_partitions , output_matching_non_contracting_partitions , output_other_non_contracting_partitions , other_contracting_partitions , other_non_contracting_partitions , other_batch_partitions , matching_contracting_partitions , matching_non_contracting_partitions / matching_matched_non_contracting_partitions , matching_batch_partitions , ShapeSizeInBytes ( other_partitioned . base_shape ( ) ) , ShapeSizeInBytes ( matching_partitioned . base_shape ( ) ) / matching_non_contracting_partitions , ShapeSizeInBytes ( GetPerGroupBaseShape ( output_grouped , output_base_shape ) ) , options , output_sharding_transposed_to_match_matching , output_sharding_transposed_to_match_other , matching_grouped . sharding , other_grouped -> sharding , conv_window , dims_mapping ) ; return e_config ? new_num_partitions : absl :: optional < int64_t > ( absl :: nullopt ) ; } ; absl :: optional < int64_t > lhs_matching_iterations ; if ( lhs_matching_partitions != 0 ) { lhs_matching_iterations = subsequent_einsum_iterations_estimate ( true ) ; } absl :: optional < int64_t > rhs_matching_iterations ; if ( rhs_matching_partitions != 0 ) { rhs_matching_iterations = subsequent_einsum_iterations_estimate ( false ) ; } return std :: make_pair ( lhs_matching_iterations , rhs_matching_iterations ) ; }
Estimate the number of iterations of a subsequent windowed einsum
partitioning if its partitioned in the non-contracting dimensions.
Estimate the number of iterations of a subsequent windowed einsum partitioning if its partitioned in the non - contracting dimensions .