Assemble memory cost
| 415 | |
| 416 | // Assemble memory cost |
| 417 | void SbpEdge::InitializeMemory(const HashMap<LogicalBlobId, int32_t>& lbi2id, |
| 418 | const std::vector<int32_t>& id2count, |
| 419 | const std::vector<int64_t>& producer_nd_sbp_sig2memory) { |
| 420 | const auto& consumer_operator = end_node_->op_node_->op(); |
| 421 | const auto& end_sbp_sig_list = end_node_->sbp_sig_list_; |
| 422 | std::vector<int64_t> consumer_nd_sbp_sig2memory(end_sbp_sig_list.size(), 0); |
| 423 | // Compute and store the memory for consumer |
| 424 | for (const auto& ibn : consumer_operator.input_bns()) { |
| 425 | // Match the ibn to find the hierarchy |
| 426 | const auto& lbi = consumer_operator.BnInOp2Lbi(ibn); |
| 427 | if (SearchLbi(lbi) && id2count.at(lbi2id.at(lbi)) > 0) { |
| 428 | const auto& consumer_hierarchy = |
| 429 | *CHECK_JUST(consumer_operator.GetParallelDesc4BnInOp(ibn))->hierarchy(); |
| 430 | const auto& logical_blob_desc = start_node_->op_node_->LogicalBlobDesc4Lbi(lbi); |
| 431 | HashMap<NdSbp, int64_t> consumer_nd_sbp2memory; |
| 432 | for (int32_t sbp_sig_id = 0; sbp_sig_id < end_sbp_sig_list.size(); sbp_sig_id++) { |
| 433 | const NdSbp& nd_sbp = end_sbp_sig_list[sbp_sig_id].bn_in_op2nd_sbp().at(ibn); |
| 434 | auto it = consumer_nd_sbp2memory.find(nd_sbp); |
| 435 | if (it == consumer_nd_sbp2memory.end()) { |
| 436 | // This compute the memory at rank 0, the largest one. |
| 437 | // We could be faster if we just compute the average memory. |
| 438 | it = consumer_nd_sbp2memory |
| 439 | .insert({nd_sbp, |
| 440 | MaxByteSize4BlobDescSbp(logical_blob_desc, nd_sbp, consumer_hierarchy)}) |
| 441 | .first; |
| 442 | } |
| 443 | consumer_nd_sbp_sig2memory[sbp_sig_id] += it->second; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | // Avoid negative value for memory |
| 448 | // For example, B -> S might reduce memory but we still consider 0 memory increment instead of |
| 449 | // negative memory increment. |
| 450 | if (*std::max_element(consumer_nd_sbp_sig2memory.begin(), consumer_nd_sbp_sig2memory.end()) |
| 451 | > *std::min_element(producer_nd_sbp_sig2memory.begin(), producer_nd_sbp_sig2memory.end())) { |
| 452 | in_memory_support_ = true; |
| 453 | memory_.resize(producer_nd_sbp_sig2memory.size()); |
| 454 | int32_t consumer_sbp_sig_size = consumer_nd_sbp_sig2memory.size(); |
| 455 | for (int32_t i = 0; i < producer_nd_sbp_sig2memory.size(); i++) { |
| 456 | auto& memory_i = memory_[i]; |
| 457 | memory_i.resize(consumer_sbp_sig_size, 0); |
| 458 | for (int32_t j = 0; j < consumer_sbp_sig_size; j++) { |
| 459 | int64_t memory_difference = consumer_nd_sbp_sig2memory[j] - producer_nd_sbp_sig2memory[i]; |
| 460 | // Only accept positive memory change |
| 461 | if (memory_difference > 0) { memory_i[j] = memory_difference; } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Set the cut ratio |
| 468 | double SbpEdge::GetCutRatio() const { |
nothing calls this directly
no test coverage detected