dist_attr.cc 23.0 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 26 27 28 29 30 31
/* Copyright (c) 2022 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 <algorithm>
#include <iostream>
#include <iterator>

#include "paddle/fluid/distributed/auto_parallel/dist_attr.h"
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/var_desc.h"

namespace paddle {
namespace distributed {
namespace auto_parallel {

std::vector<std::string> TensorDistAttr::fields_{
    "process_mesh", "dims_mapping", "batch_dim", "dynamic_dims"};

32 33 34 35 36 37 38 39 40 41 42
static inline std::vector<int64_t> get_tensor_shape(const VarDesc* tensor) {
  if (tensor == nullptr) return std::vector<int64_t>();
  switch (tensor->GetType()) {
    case framework::proto::VarType::READER:
    case framework::proto::VarType::LOD_TENSOR_ARRAY:
    case framework::proto::VarType::STEP_SCOPES:
    case framework::proto::VarType::FEED_MINIBATCH:
    case framework::proto::VarType::FETCH_LIST:
      return std::vector<int64_t>();
    default:
      return tensor->GetShape();
43 44 45
  }
}

46 47 48 49 50 51 52
TensorDistAttr::TensorDistAttr(const VarDesc& tensor) {
  VLOG(4) << "[TensorDistAttr constructor] tensor name: " << tensor.Name();
  std::vector<int64_t> tensor_shape = get_tensor_shape(&tensor);
  set_default_dims_mapping(tensor_shape);
  set_default_dynamic_dims(tensor_shape);
}

53
TensorDistAttr::TensorDistAttr(const TensorDistAttr& dist_attr) {
54
  copy_from(dist_attr);
55 56 57
}

TensorDistAttr& TensorDistAttr::operator=(const TensorDistAttr& dist_attr) {
58 59 60 61 62 63 64
  if (this == &dist_attr) return *this;
  TensorDistAttr tmp(dist_attr);
  std::swap(this->process_mesh_, tmp.process_mesh_);
  std::swap(this->dims_mapping_, tmp.dims_mapping_);
  std::swap(this->batch_dim_, tmp.batch_dim_);
  std::swap(this->dynamic_dims_, tmp.dynamic_dims_);
  std::swap(this->annotated_, tmp.annotated_);
65 66 67 68
  return *this;
}

void TensorDistAttr::copy_from(const TensorDistAttr& dist_attr) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  set_process_mesh(dist_attr.process_mesh());
  set_dims_mapping(dist_attr.dims_mapping());
  set_batch_dim(dist_attr.batch_dim());
  set_dynamic_dims(dist_attr.dynamic_dims());
  set_annotated(dist_attr.annotated());
}

void TensorDistAttr::set_process_mesh(const ProcessMesh& process_mesh) {
  process_mesh_ = process_mesh;
}

void TensorDistAttr::set_dims_mapping(
    const std::vector<int64_t>& dims_mapping) {
  dims_mapping_ = dims_mapping;
}

void TensorDistAttr::set_batch_dim(int64_t batch_dim) {
86
  batch_dim_ = batch_dim;
87 88 89 90 91 92 93 94 95 96 97
}

void TensorDistAttr::set_dynamic_dims(const std::vector<bool>& dynamic_dims) {
  dynamic_dims_ = dynamic_dims;
}

void TensorDistAttr::set_annotated(
    const std::map<std::string, bool>& annotated) {
  annotated_ = annotated;
}

98 99 100 101 102 103 104 105 106 107 108
void TensorDistAttr::set_default_dims_mapping(
    const std::vector<int64_t>& tensor_shape) {
  if (tensor_shape.size() != 0) {
    dims_mapping_ = std::vector<int64_t>(tensor_shape.size(), -1);
  }
}

void TensorDistAttr::set_default_dynamic_dims(
    const std::vector<int64_t>& tensor_shape) {
  if (tensor_shape.size() != 0) {
    dynamic_dims_ = std::vector<bool>(tensor_shape.size(), false);
109 110 111
  }
}

112
void TensorDistAttr::mark_annotated(const std::string& name) {
113 114 115 116 117 118 119 120
  auto result = std::find(std::begin(fields_), std::end(fields_), name);
  if (result != std::end(fields_)) {
    annotated_[name] = true;
  }
}

bool TensorDistAttr::verify_process_mesh(
    const ProcessMesh& process_mesh) const {
121 122
  VLOG(4) << "[TensorDistAttr verify_process_mesh] "
          << process_mesh.to_string();
123 124
  if (!process_mesh_.empty()) {
    for (int64_t dim_mapping : dims_mapping_) {
125
      if (dim_mapping >= process_mesh_.ndim()) {
126 127 128 129 130 131 132 133
        return false;
      }
    }
  }
  return true;
}

bool TensorDistAttr::verify_dims_mapping(
134 135
    const std::vector<int64_t>& dims_mapping,
    const std::vector<int64_t>& tensor_shape) const {
136
  VLOG(4) << "[TensorDistAttr verify_dims_mapping] " << str_join(dims_mapping);
137
  if (dims_mapping.size() != tensor_shape.size()) {
138
    return false;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  }
  std::unordered_map<int64_t, int64_t> map;
  if (!process_mesh_.empty()) {
    for (int64_t i : dims_mapping) {
      if (i < -1 || i >= process_mesh_.ndim()) {
        return false;
      }
      ++map[i];
      if (i != -1 && map[i] > 1) {
        return false;
      }
    }
  } else {
    for (int64_t i : dims_mapping) {
      ++map[i];
      if (i != -1 && map[i] > 1) {
        return false;
      }
    }
  }
  return true;
}

162 163
bool TensorDistAttr::verify_batch_dim(
    int64_t dim, const std::vector<int64_t>& tensor_shape) const {
164
  VLOG(4) << "[TensorDistAttr verify_batch_dim] " << dim;
165 166
  int64_t ndim = tensor_shape.size();
  if (ndim > 0) {
167 168 169 170 171 172 173 174 175 176 177
    if (dim < 0) {
      dim = dim + ndim;
    }
    if (dim < 0 || dim >= ndim) {
      return false;
    }
  }
  return true;
}

bool TensorDistAttr::verify_dynamic_dims(
178 179
    const std::vector<bool>& dynamic_dims,
    const std::vector<int64_t>& tensor_shape) const {
180
  VLOG(4) << "[TensorDistAttr verify_dynamic_dims] " << str_join(dynamic_dims);
181
  if (dynamic_dims.size() > 0 && dynamic_dims.size() != tensor_shape.size()) {
182
    return false;
183 184 185 186 187 188
  }
  return true;
}

bool TensorDistAttr::verify_annotated(
    const std::map<std::string, bool>& annotated) const {
189
  VLOG(4) << "[TensorDistAttr verify_annotated] " << str_join(annotated);
190 191 192 193 194 195 196 197 198
  for (const auto& item : annotated) {
    auto result = std::find(std::begin(fields_), std::end(fields_), item.first);
    if (result == std::end(fields_)) {
      return false;
    }
  }
  return true;
}

199 200
bool TensorDistAttr::verify(const VarDesc* tensor) const {
  auto tensor_shape = get_tensor_shape(tensor);
201 202 203
  if (!verify_process_mesh(process_mesh_)) {
    return false;
  }
204
  if (!verify_dims_mapping(dims_mapping_, tensor_shape)) {
205 206
    return false;
  }
207
  if (!verify_batch_dim(batch_dim_, tensor_shape)) {
208 209
    return false;
  }
210
  if (!verify_dynamic_dims(dynamic_dims_, tensor_shape)) {
211 212 213 214 215 216 217 218 219 220
    return false;
  }
  if (!verify_annotated(annotated_)) {
    return false;
  }
  return true;
}

std::string TensorDistAttr::to_string() const {
  std::string dist_str;
221
  dist_str += "{process_mesh: " + process_mesh_.to_string() + ", ";
222 223 224 225 226 227 228
  dist_str += "dims_mappings: [" + str_join(dims_mapping_) + "], ";
  dist_str += "batch_dim: " + std::to_string(batch_dim_) + ", ";
  dist_str += "dynamic_dims: [" + str_join(dynamic_dims_) + "], ";
  dist_str += "annotated: [" + str_join(annotated_) + "]}";
  return dist_str;
}

229 230 231
void TensorDistAttr::from_proto(const TensorDistAttrProto& proto) {
  process_mesh_ = ProcessMesh::from_proto(proto.process_mesh());
  dims_mapping_.resize(proto.dims_mapping_size());
232
  for (int64_t i = 0; i < proto.dims_mapping_size(); ++i) {
233
    dims_mapping_[i] = proto.dims_mapping(i);
234
  }
235 236
  batch_dim_ = proto.batch_dim();
  dynamic_dims_.resize(proto.dynamic_dims_size());
237
  for (int64_t i = 0; i < proto.dynamic_dims_size(); ++i) {
238
    dynamic_dims_[i] = proto.dynamic_dims(i);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  }
}

TensorDistAttrProto TensorDistAttr::to_proto() const {
  TensorDistAttrProto proto;
  proto.mutable_process_mesh()->CopyFrom(process_mesh_.to_proto());
  for (const auto& i : dims_mapping_) {
    proto.add_dims_mapping(i);
  }
  proto.set_batch_dim(batch_dim_);
  for (const auto& i : dynamic_dims_) {
    proto.add_dynamic_dims(i);
  }
  return proto;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
std::string TensorDistAttr::serialize_to_string() {
  std::string data;
  auto proto = to_proto();
  proto.SerializeToString(&data);
  PADDLE_ENFORCE_EQ(to_proto().SerializeToString(&data),
                    true,
                    platform::errors::InvalidArgument(
                        "Failed to serialize tensor dist attr to string."));
  return data;
}

void TensorDistAttr::parse_from_string(const std::string& data) {
  TensorDistAttrProto proto;
  PADDLE_ENFORCE_EQ(proto.ParseFromString(data),
                    true,
                    platform::errors::InvalidArgument(
                        "Failed to parse tensor dist attr from string."));
  from_proto(proto);
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
bool operator==(const TensorDistAttr& lhs, const TensorDistAttr& rhs) {
  if (lhs.process_mesh() != rhs.process_mesh()) {
    return false;
  }
  if (lhs.dims_mapping() != rhs.dims_mapping()) {
    return false;
  }
  if (lhs.batch_dim() != rhs.batch_dim()) {
    return false;
  }
  if (lhs.dynamic_dims() != rhs.dynamic_dims()) {
    return false;
  }
  return true;
}

291 292 293
std::vector<std::string> OperatorDistAttr::fields_{"process_mesh",
                                                   "impl_type",
                                                   "impl_idx",
294
                                                   "is_recompute",
295
                                                   "execution_stream",
296
                                                   "stream_priority",
297
                                                   "scheduling_priority"};
298

299 300 301
OperatorDistAttr::OperatorDistAttr(const OpDesc& op) {
  VLOG(4) << "[OperatorDistAttr constructor] op type: " << op.Type();
  initialize(&op);
302 303 304
}

OperatorDistAttr::OperatorDistAttr(const OperatorDistAttr& dist_attr) {
305
  VLOG(4) << "[OperatorDistAttr copy constructor]";
306 307 308 309 310
  copy_from(dist_attr);
}

OperatorDistAttr& OperatorDistAttr::operator=(
    const OperatorDistAttr& dist_attr) {
311 312 313 314 315 316 317 318 319 320 321
  VLOG(4) << "[OperatorDistAttr assign constructor]";
  if (this == &dist_attr) return *this;
  OperatorDistAttr tmp(dist_attr);
  std::swap(this->input_dist_attrs_, tmp.input_dist_attrs_);
  std::swap(this->output_dist_attrs_, tmp.output_dist_attrs_);
  std::swap(this->process_mesh_, tmp.process_mesh_);
  std::swap(this->op_type_, tmp.op_type_);
  std::swap(this->impl_type_, tmp.impl_type_);
  std::swap(this->impl_idx_, tmp.impl_idx_);
  std::swap(this->is_recompute_, tmp.is_recompute_);
  std::swap(this->execution_stream_, tmp.execution_stream_);
322 323
  std::swap(this->stream_priority_, tmp.stream_priority_);
  std::swap(this->scheduling_priority_, tmp.scheduling_priority_);
324 325 326
  std::swap(this->annotated_, tmp.annotated_);
  // Note: Make sure all tensor dist attr has the same process_mesh
  set_process_mesh(this->process_mesh_);
327 328 329
  return *this;
}

330 331 332 333
void OperatorDistAttr::initialize(const OpDesc* op) {
  if (op == nullptr) return;
  for (std::string name : op->InputArgumentNames()) {
    VarDesc* input = op->Block()->FindVarRecursive(name);
334
    VLOG(4) << "[OperatorDistAttr create input dist attr] " << name;
335
    if (input == nullptr || op->Type() == "create_py_reader") {
336 337 338 339
      input_dist_attrs_[name] = TensorDistAttr();
    } else {
      input_dist_attrs_[name] = TensorDistAttr(*input);
    }
340
  }
341 342
  for (std::string name : op->OutputArgumentNames()) {
    VarDesc* output = op->Block()->FindVarRecursive(name);
343 344 345 346 347 348
    VLOG(4) << "[OperatorDistAttr create output dist attr] " << name;
    if (output == nullptr) {
      output_dist_attrs_[name] = TensorDistAttr();
    } else {
      output_dist_attrs_[name] = TensorDistAttr(*output);
    }
349
  }
350
  op_type_ = op->Type();
351
  impl_type_ = kDefault;
352
  impl_idx_ = 0;
353
  is_recompute_ = false;
354
  execution_stream_ = kDefault;
355
  stream_priority_ = 0;
356
  scheduling_priority_ = 0;
357 358
}

359 360 361
void OperatorDistAttr::copy_from(const OperatorDistAttr& dist_attr) {
  set_input_dist_attrs(dist_attr.input_dist_attrs());
  set_output_dist_attrs(dist_attr.output_dist_attrs());
362
  set_process_mesh(dist_attr.process_mesh());
363
  set_op_type(dist_attr.op_type());
364 365
  set_impl_type(dist_attr.impl_type());
  set_impl_idx(dist_attr.impl_idx());
366
  set_is_recompute(dist_attr.is_recompute());
367
  set_execution_stream(dist_attr.execution_stream());
368
  set_stream_priority(dist_attr.stream_priority());
369
  set_scheduling_priority(dist_attr.scheduling_priority());
370 371 372
  set_annotated(dist_attr.annotated());
}

373 374
void OperatorDistAttr::set_input_dist_attrs(
    const std::map<std::string, TensorDistAttr>& dist_attrs) {
375 376
  for (const auto& item : dist_attrs) {
    set_input_dist_attr(item.first, item.second);
377
  }
378 379 380 381
}

void OperatorDistAttr::set_output_dist_attrs(
    const std::map<std::string, TensorDistAttr>& dist_attrs) {
382 383
  for (const auto& item : dist_attrs) {
    set_output_dist_attr(item.first, item.second);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  }
}

void OperatorDistAttr::set_input_dist_attr(const std::string& name,
                                           const TensorDistAttr& dist_attr) {
  input_dist_attrs_[name] = dist_attr;
  // Make sure the process mesh of input be same as that of the op
  input_dist_attrs_[name].set_process_mesh(process_mesh_);
}

void OperatorDistAttr::set_output_dist_attr(const std::string& name,
                                            const TensorDistAttr& dist_attr) {
  output_dist_attrs_[name] = dist_attr;
  // Make sure the process mesh of output be same as that of the op
  output_dist_attrs_[name].set_process_mesh(process_mesh_);
}

void OperatorDistAttr::set_process_mesh(const ProcessMesh& process_mesh) {
  for (auto& item : input_dist_attrs_) {
    item.second.set_process_mesh(process_mesh);
  }
  for (auto& item : output_dist_attrs_) {
    item.second.set_process_mesh(process_mesh);
  }
  process_mesh_ = process_mesh;
}

411 412 413 414 415 416
void OperatorDistAttr::set_annotated(
    const std::map<std::string, bool>& annotated) {
  annotated_ = annotated;
}

void OperatorDistAttr::mark_annotated(const std::string& name) {
417 418 419 420 421 422
  auto result = std::find(std::begin(fields_), std::end(fields_), name);
  if (result != std::end(fields_)) {
    annotated_[name] = true;
  }
  if (name == "process_mesh") {
    for (auto& item : input_dist_attrs_) {
423
      item.second.mark_annotated(name);
424 425
    }
    for (auto& item : output_dist_attrs_) {
426
      item.second.mark_annotated(name);
427 428 429 430
    }
  }
}

431 432 433 434 435 436 437 438
void OperatorDistAttr::clear_annotated() {
  annotated_.clear();
  for (auto& item : input_dist_attrs_) {
    item.second.clear_annotated();
  }
  for (auto& item : output_dist_attrs_) {
    item.second.clear_annotated();
  }
439 440
}

441 442 443 444 445 446 447
const std::vector<int64_t>& OperatorDistAttr::input_dims_mapping(
    const std::string& name) const {
  return input_dist_attr(name).dims_mapping();
}

void OperatorDistAttr::set_input_dims_mapping(
    const std::string& name, const std::vector<int64_t>& dims_mapping) {
448 449
  input_dist_attrs_[name].set_dims_mapping(dims_mapping);
  input_dist_attrs_[name].set_process_mesh(process_mesh_);
450 451 452 453 454 455 456 457 458
}

const std::vector<int64_t>& OperatorDistAttr::output_dims_mapping(
    const std::string& name) {
  return output_dist_attr(name).dims_mapping();
}

void OperatorDistAttr::set_output_dims_mapping(
    const std::string& name, const std::vector<int64_t>& dims_mapping) {
459 460
  output_dist_attrs_[name].set_dims_mapping(dims_mapping);
  output_dist_attrs_[name].set_process_mesh(process_mesh_);
461 462
}

463 464 465
bool OperatorDistAttr::verify_input_dist_attr(const std::string& name,
                                              const TensorDistAttr& dist_attr,
                                              const VarDesc* tensor) const {
466 467
  VLOG(4) << "[OperatorDistAttr verify_input_dist_attr] " << name << " "
          << dist_attr.to_string();
468
  if (!dist_attr.verify(tensor)) {
469 470
    return false;
  }
471 472
  if (tensor != nullptr) {
    if (name != tensor->Name()) {
473 474 475
      return false;
    }
  }
476 477 478
  if (input_dist_attrs_.count(name) == 0) {
    return false;
  }
479 480 481
  return true;
}

482 483 484
bool OperatorDistAttr::verify_output_dist_attr(const std::string& name,
                                               const TensorDistAttr& dist_attr,
                                               const VarDesc* tensor) const {
485 486
  VLOG(4) << "[OperatorDistAttr verify_output_dist_attr] " << name << " "
          << dist_attr.to_string();
487
  if (!dist_attr.verify(tensor)) {
488 489
    return false;
  }
490 491
  if (tensor != nullptr) {
    if (name != tensor->Name()) {
492 493 494
      return false;
    }
  }
495 496 497
  if (output_dist_attrs_.count(name) == 0) {
    return false;
  }
498 499 500 501 502
  return true;
}

bool OperatorDistAttr::verify_process_mesh(
    const ProcessMesh& process_mesh) const {
503 504
  VLOG(4) << "[OperatorDistAttr verify_process_mesh] "
          << process_mesh.to_string();
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  if (process_mesh != process_mesh_) {
    return false;
  }
  for (auto& item : input_dist_attrs_) {
    if (item.second.process_mesh() != process_mesh) {
      return false;
    }
  }
  for (auto& item : output_dist_attrs_) {
    if (item.second.process_mesh() != process_mesh) {
      return false;
    }
  }
  return true;
}

bool OperatorDistAttr::verify_annotated(
    const std::map<std::string, bool>& annotated) const {
523
  VLOG(4) << "[OperatorDistAttr verify_annotated] " << str_join(annotated);
524 525 526 527 528 529 530
  for (const auto& item : annotated) {
    auto result = std::find(std::begin(fields_), std::end(fields_), item.first);
    if (result == std::end(fields_)) {
      return false;
    }
  }
  for (auto& item : input_dist_attrs_) {
531 532
    VLOG(4) << "[OperatorDistAttr verify_annotated input] "
            << str_join(item.second.annotated());
533 534 535 536 537
    if (!item.second.verify_annotated(item.second.annotated())) {
      return false;
    }
  }
  for (auto& item : output_dist_attrs_) {
538 539
    VLOG(4) << "[OperatorDistAttr verify_annotated output] "
            << str_join(item.second.annotated());
540 541 542 543 544 545 546
    if (!item.second.verify_annotated(item.second.annotated())) {
      return false;
    }
  }
  return true;
}

547
bool OperatorDistAttr::verify(const OpDesc* op) const {
548 549 550 551
  if (!verify_process_mesh(process_mesh_)) {
    return false;
  }
  for (auto const& item : input_dist_attrs_) {
552
    auto input_names = op->InputArgumentNames();
553 554 555 556 557
    auto found =
        std::find(std::begin(input_names), std::end(input_names), item.first);
    if (found == std::end(input_names)) {
      return false;
    }
558 559
    auto tensor = op->Block()->FindVarRecursive(item.first);
    if (!verify_input_dist_attr(item.first, item.second, tensor)) {
560 561 562 563
      return false;
    }
  }
  for (auto const& item : output_dist_attrs_) {
564
    auto output_names = op->OutputArgumentNames();
565 566 567 568 569
    auto found =
        std::find(std::begin(output_names), std::end(output_names), item.first);
    if (found == std::end(output_names)) {
      return false;
    }
570 571
    auto tensor = op->Block()->FindVarRecursive(item.first);
    if (!verify_output_dist_attr(item.first, item.second, tensor)) {
572 573 574 575 576 577
      return false;
    }
  }
  return true;
}

578 579
void OperatorDistAttr::rename_input(const std::string& old_name,
                                    const std::string& new_name) {
580
  if (old_name == new_name) return;
581 582
  for (auto& item : input_dist_attrs_) {
    if (item.first == old_name) {
583
      input_dist_attrs_[new_name].copy_from(input_dist_attrs_[old_name]);
584 585 586 587 588 589 590 591
      input_dist_attrs_.erase(old_name);
      break;
    }
  }
}

void OperatorDistAttr::rename_output(const std::string& old_name,
                                     const std::string& new_name) {
592
  if (old_name == new_name) return;
593 594
  for (auto& item : output_dist_attrs_) {
    if (item.first == old_name) {
595
      output_dist_attrs_[new_name].copy_from(output_dist_attrs_[old_name]);
596 597 598 599 600 601
      output_dist_attrs_.erase(old_name);
      break;
    }
  }
}

602 603
std::string OperatorDistAttr::to_string() const {
  std::string str;
604
  str += "{impl_type: " + impl_type_ + ", ";
605
  str += "impl_idx: " + std::to_string(impl_idx_) + ", ";
606
  str += "execution_stream: " + execution_stream_ + ", ";
607
  str += "stream_priority: " + std::to_string(stream_priority_) + ", ";
608
  str += "scheduling_priority: " + std::to_string(scheduling_priority_) + ", ";
609 610 611 612
  str += "annotated: [" + str_join(annotated_) + "], ";
  str += "\nprocess_mesh: " + process_mesh_.to_string() + ", ";
  str += "\ninput_dist_attrs: [\n";
  for (auto const& item : input_dist_attrs_) {
613
    str += "  " + item.first + ": " + item.second.to_string() + ",\n";
614 615 616 617
  }
  str.replace(str.size() - 2, 2, "]");
  str += "\noutput_dist_attrs: [\n";
  for (auto const& item : output_dist_attrs_) {
618
    str += "  " + item.first + ": " + item.second.to_string() + ",\n";
619 620 621 622 623
  }
  str.replace(str.size() - 2, 2, "]}");
  return str;
}

624
void OperatorDistAttr::from_proto(const OperatorDistAttrProto& proto) {
625
  for (int64_t i = 0; i < proto.input_dist_attrs_size(); ++i) {
626 627 628 629
    TensorDistAttr dist_attr;
    std::string name = proto.input_dist_attrs(i).name();
    dist_attr.from_proto(proto.input_dist_attrs(i).tensor_dist_attr());
    input_dist_attrs_[name] = dist_attr;
630 631
  }
  for (int64_t i = 0; i < proto.output_dist_attrs_size(); ++i) {
632 633 634 635
    TensorDistAttr dist_attr;
    std::string name = proto.output_dist_attrs(i).name();
    dist_attr.from_proto(proto.output_dist_attrs(i).tensor_dist_attr());
    output_dist_attrs_[name] = dist_attr;
636
  }
637 638 639
  process_mesh_ = ProcessMesh::from_proto(proto.process_mesh());
  impl_type_ = proto.impl_type();
  impl_idx_ = proto.impl_idx();
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
}

OperatorDistAttrProto OperatorDistAttr::to_proto() const {
  OperatorDistAttrProto proto;
  for (const auto& item : input_dist_attrs_) {
    auto proto_item = proto.mutable_input_dist_attrs()->Add();
    proto_item->set_name(item.first);
    proto_item->mutable_tensor_dist_attr()->CopyFrom(item.second.to_proto());
  }
  for (const auto& item : output_dist_attrs_) {
    auto proto_item = proto.mutable_output_dist_attrs()->Add();
    proto_item->set_name(item.first);
    proto_item->mutable_tensor_dist_attr()->CopyFrom(item.second.to_proto());
  }
  proto.mutable_process_mesh()->CopyFrom(process_mesh_.to_proto());
  proto.set_impl_type(impl_type_);
  proto.set_impl_idx(impl_idx_);
  return proto;
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
std::string OperatorDistAttr::serialize_to_string() {
  std::string data;
  auto proto = to_proto();
  proto.SerializeToString(&data);
  PADDLE_ENFORCE_EQ(to_proto().SerializeToString(&data),
                    true,
                    platform::errors::InvalidArgument(
                        "Failed to serialize op dist attr to string."));
  return data;
}

void OperatorDistAttr::parse_from_string(const std::string& data) {
  OperatorDistAttrProto proto;
  PADDLE_ENFORCE_EQ(proto.ParseFromString(data),
                    true,
                    platform::errors::InvalidArgument(
                        "Failed to parse op dist attr from string."));
  from_proto(proto);
}

680 681 682 683 684 685 686 687 688 689
bool operator==(const OperatorDistAttr& lhs, const OperatorDistAttr& rhs) {
  if (lhs.process_mesh() != rhs.process_mesh()) {
    return false;
  }
  if (lhs.impl_type() != rhs.impl_type()) {
    return false;
  }
  if (lhs.impl_idx() != rhs.impl_idx()) {
    return false;
  }
690 691 692
  if (lhs.execution_stream() != rhs.execution_stream()) {
    return false;
  }
693 694 695
  if (lhs.stream_priority() != rhs.stream_priority()) {
    return false;
  }
696 697 698
  if (lhs.scheduling_priority() != rhs.scheduling_priority()) {
    return false;
  }
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  for (auto const& item : lhs.input_dist_attrs()) {
    if (rhs.input_dist_attrs().count(item.first) != 1) {
      return false;
    }
    if (rhs.input_dist_attrs().at(item.first) !=
        lhs.input_dist_attrs().at(item.first)) {
      return false;
    }
  }
  for (auto const& item : lhs.output_dist_attrs()) {
    if (rhs.output_dist_attrs().count(item.first) != 1) {
      return false;
    }
    if (rhs.output_dist_attrs().at(item.first) !=
        lhs.output_dist_attrs().at(item.first)) {
      return false;
    }
  }
  return true;
}

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