| 470 | } |
| 471 | |
| 472 | CompiledProgram::CompiledProgram(const std::vector<phi::Place> &places, |
| 473 | const std::vector<std::string> &bcast_vars, |
| 474 | const std::string &loss_var_name, |
| 475 | Scope *scope, |
| 476 | const std::vector<Scope *> &local_scopes, |
| 477 | const BuildStrategy &build_strategy, |
| 478 | ir::Graph *graph) |
| 479 | : member_(new CompiledProgramPrivate(places, scope)) { |
| 480 | PADDLE_ENFORCE_EQ( |
| 481 | !places.empty(), |
| 482 | true, |
| 483 | common::errors::Unavailable("NPU is not supported in CompiledProgram.")); |
| 484 | InitP2P(places); |
| 485 | InitReaderQueueDeviceCount( |
| 486 | graph, *(member_->global_scope_), member_->places_.size()); |
| 487 | // Initialize necessary info of member_ with strategy. |
| 488 | InitProgramPrivateMemberInfo(build_strategy, places.size()); |
| 489 | |
| 490 | // Step 1. Create local scopes and Clone graph into multi device |
| 491 | CreateLocalScopes(scope, local_scopes, /*create_new*/ true); |
| 492 | std::vector<ir::Graph *> graphs = CloneGraphToMultiDevices(graph); |
| 493 | PrepareNCCLCommunicator(scope); |
| 494 | |
| 495 | // broadcast parameters from the 0th device to others: |
| 496 | auto need_broadcast = [&]() -> bool { |
| 497 | if (member_->build_strategy_.num_trainers_ > 1) { // NOLINT |
| 498 | // 1. num_tariners would be grater than 1 for nccl distributed training. |
| 499 | return true; |
| 500 | } else if (member_->local_scopes_.size() != 1 && local_scopes.empty()) { |
| 501 | // 2. Only one trainer process, but CompiledProgram hold multiple |
| 502 | // devices. |
| 503 | return true; |
| 504 | } |
| 505 | return false; |
| 506 | }; |
| 507 | if (need_broadcast()) { |
| 508 | BCastParamsToDevices(bcast_vars, member_->build_strategy_.trainer_id_); |
| 509 | } |
| 510 | |
| 511 | // Step 2. Convert main_program to SSA form and dependency graph. Also, insert |
| 512 | // ncclOp |
| 513 | std::vector<ir::Graph *> async_graphs = |
| 514 | CompileGraphWithBuildStrategy(graph, &graphs, loss_var_name); |
| 515 | graph = member_->ApplyMemoryOptimizePass(graph); |
| 516 | } |
| 517 | |
| 518 | void CompiledProgram::BCastParamsToDevices(const std::vector<std::string> &vars, |
| 519 | int trainer_id) const { |