transpose_spmd_rule.cc 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.h"
#include "paddle/phi/core/distributed/auto_parallel/utils.h"

namespace paddle {
namespace distributed {
namespace auto_parallel {
using phi::distributed::auto_parallel::str_join;
std::pair<std::vector<TensorDistAttr>, std::vector<TensorDistAttr>>
TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
                                const paddle::framework::AttributeMap& attrs) {
  // step0: Verify Input Args Based on Transpose Logic
26
  int64_t ninputs = input_specs.size();
27 28 29 30 31 32 33 34 35 36 37
  PADDLE_ENFORCE_EQ(
      ninputs,
      1,
      phi::errors::InvalidArgument("The size of InputSpec in transpose must "
                                   "be equal to 1, but got [%d].",
                                   ninputs));
  VerifySpecs(input_specs, "transpose");

  // step1: Build Einsum Notation
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  // get einsum notation for input
38
  int64_t ndim = input_specs[0].shape().size();
39 40 41 42 43
  std::vector<std::string> input_axes_vec;
  std::string input_axes = alphabet.substr(0, ndim);
  input_axes_vec.emplace_back(input_axes);

  // get einsum notation for output
44
  std::string output_axes = GetOutputNotation(ndim, input_axes, attrs);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  // step2: Sharding Propogation
  // step2.1: merge input shardings
  std::vector<std::pair<std::string, std::vector<int64_t>>> axes_sharding_info;
  axes_sharding_info = GetAxesDimsMappingPair(input_axes_vec, input_specs);
  std::unordered_map<std::string, int64_t> axis_to_dim_map =
      ShardingMergeForTensors(axes_sharding_info);

  // step2.2: infer output dimsmapping from merged input dimsmapping
  std::vector<int64_t> output_dims_mapping =
      GetDimsMappingForAxes(output_axes, axis_to_dim_map);

  // initialize output dist_attr's process_mesh, batch_dim and dynamic dims with
  // input dist_attr.
  TensorDistAttr output_dist_attr =
      CopyTensorDistAttrForOutput(input_specs[0].dist_attr());
  output_dist_attr.set_dims_mapping(output_dims_mapping);

63 64
  // step3  Handle partial (TODO)

65 66 67 68 69 70 71 72 73
  VLOG(4) << "TransposeSPMDRule InferForward:";
  for (int64_t i = 0; i < ninputs; i++) {
    VLOG(4) << "Input" << std::to_string(i) << " shape: ["
            << str_join(input_specs[i].shape()) << "] "
            << "src_dims_mapping: [" << str_join(input_specs[i].dims_mapping())
            << "] "
            << "dst_dims_mapping: [" << str_join(input_specs[i].dims_mapping())
            << "]";
  }
74 75
  VLOG(4) << "Perm: ["
          << str_join(ExtractAttr<std::vector<int64_t>>("perm", attrs)) << "]";
76 77 78 79 80 81 82
  VLOG(4) << "Output dims_mapping: [" + str_join(output_dims_mapping) + "]\n\n";

  return {{input_specs[0].dist_attr()}, {output_dist_attr}};
}

std::pair<std::vector<TensorDistAttr>, std::vector<TensorDistAttr>>
TransposeSPMDRule::InferBackward(
83
    const std::vector<DistTensorSpec>& input_specs,
84 85
    const std::vector<DistTensorSpec>& output_specs,
    const paddle::framework::AttributeMap& attrs) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  // step0: Verify Input Args Based on Transpose Logic
  int64_t ninputs = input_specs.size();
  int64_t noutputs = output_specs.size();
  PADDLE_ENFORCE_EQ(
      ninputs,
      1,
      phi::errors::InvalidArgument("The size of InputSpec in transpose must "
                                   "be equal to 1, but got [%d].",
                                   ninputs));
  PADDLE_ENFORCE_EQ(
      noutputs,
      1,
      phi::errors::InvalidArgument("The size of OutputSpec in transpose must "
                                   "be equal to 1, but got [%d].",
                                   noutputs));
  VerifySpecs(output_specs, "transpose_backward");

  // step1: Build Einsum Notation
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  // get einsum notation for input
  int64_t ndim = input_specs[0].shape().size();
  std::string input_axes = alphabet.substr(0, ndim);

  // get einsum notation for output
  std::string output_axes = GetOutputNotation(ndim, input_axes, attrs);
  std::vector<std::string> output_axes_vec;
  output_axes_vec.emplace_back(output_axes);

  // step2: Sharding Propogation
  // step2.1: merge input shardings
  std::vector<std::pair<std::string, std::vector<int64_t>>> axes_sharding_info;
  axes_sharding_info = GetAxesDimsMappingPair(output_axes_vec, output_specs);
  std::unordered_map<std::string, int64_t> axis_to_dim_map =
      ShardingMergeForTensors(axes_sharding_info);

  // step2.2: infer output dimsmapping from merged input dimsmapping
  std::vector<int64_t> input_dims_mapping =
      GetDimsMappingForAxes(input_axes, axis_to_dim_map);

  // initialize output dist_attr's process_mesh, batch_dim and dynamic dims with
  // input dist_attr.
  TensorDistAttr input_dist_attr =
      CopyTensorDistAttrForOutput(input_specs[0].dist_attr());
  input_dist_attr.set_dims_mapping(input_dims_mapping);

  // Step3  Handle partial (TODO)

  VLOG(4) << "TransposeSPMDRule InferBackward:";
  VLOG(4) << "Output shape: [" << str_join(output_specs[0].shape()) << "] "
          << "dims_mapping: [" << str_join(output_specs[0].dims_mapping())
          << "]";
  VLOG(4) << "Perm: ["
          << str_join(ExtractAttr<std::vector<int64_t>>("perm", attrs)) << "]";
  for (int64_t i = 0; i < ninputs; i++) {
    VLOG(4) << "Input" << std::to_string(i) << " shape: ["
            << str_join(input_specs[i].shape()) << "] "
            << "dims_mapping: [" << str_join(input_dims_mapping) << "]";
  }
  VLOG(4) << std::endl;

  return {{input_dist_attr}, {output_specs[0].dist_attr()}};
}

std::string TransposeSPMDRule::GetOutputNotation(
    int64_t input_ndim,
    const std::string& input_axes,
    const paddle::framework::AttributeMap& attrs) {
  std::vector<int64_t> perm_dims =
      ExtractAttr<std::vector<int64_t>>("perm", attrs);

  // convert the negative dim value to normal dim value
  for (int64_t i = 0, n = perm_dims.size(); i < n; ++i) {
    if (perm_dims[i] < 0) {
      perm_dims[i] = input_ndim + perm_dims[i];
    }
  }

  std::string output_axes = "";
  for (int64_t i = 0; i < input_ndim; i++) {
    output_axes.append(1, input_axes[perm_dims[i]]);
  }
167

168
  return output_axes;
169 170 171 172 173
}

}  // namespace auto_parallel
}  // namespace distributed
}  // namespace paddle