| 476 | } |
| 477 | |
| 478 | void CostModel::AddToCostGraphDef(const Graph* graph, |
| 479 | CostGraphDef* cost_graph) const { |
| 480 | std::vector<const Edge*> inputs; |
| 481 | std::vector<const Edge*> control_inputs; |
| 482 | for (const Node* n : graph->nodes()) { |
| 483 | CostGraphDef::Node* cnode = cost_graph->add_node(); |
| 484 | cnode->set_name(n->name()); |
| 485 | cnode->set_device(n->assigned_device_name()); |
| 486 | cnode->set_id(Id(n)); |
| 487 | |
| 488 | inputs.clear(); |
| 489 | inputs.resize(n->num_inputs(), nullptr); |
| 490 | control_inputs.clear(); |
| 491 | for (const Edge* e : n->in_edges()) { |
| 492 | if (e->IsControlEdge()) { |
| 493 | control_inputs.push_back(e); |
| 494 | } else { |
| 495 | inputs[e->dst_input()] = e; |
| 496 | } |
| 497 | } |
| 498 | std::sort(control_inputs.begin(), control_inputs.end(), |
| 499 | [this](Edge const* a, Edge const* b) { |
| 500 | return Id(a->src()) < Id(b->src()); |
| 501 | }); |
| 502 | |
| 503 | for (const Edge* e : inputs) { |
| 504 | CostGraphDef::Node::InputInfo* input_info = cnode->add_input_info(); |
| 505 | input_info->set_preceding_node(Id(e->src())); |
| 506 | input_info->set_preceding_port(e->src_output()); |
| 507 | } |
| 508 | |
| 509 | for (int i = 0; i < n->num_outputs(); i++) { |
| 510 | CostGraphDef::Node::OutputInfo* output_info = cnode->add_output_info(); |
| 511 | int64 alloc_id = AllocationId(n, i); |
| 512 | int64 alias_to_input = -1; |
| 513 | for (const Edge* e : inputs) { |
| 514 | int64 input_alloc_id = AllocationId(e->src(), e->src_output()); |
| 515 | if (input_alloc_id == alloc_id) { |
| 516 | alias_to_input = e->dst_input(); |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | output_info->set_alias_input_port(alias_to_input); |
| 521 | output_info->set_dtype(MaxMemoryType(n, i)); |
| 522 | *output_info->mutable_shape() = MaxMemoryShape(n, i); |
| 523 | if (alias_to_input < 0 && IsPersistentTensor(n, alloc_id)) { |
| 524 | output_info->set_size(0); |
| 525 | } else { |
| 526 | output_info->set_size(MaxMemorySize(n, i).value()); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | for (const Edge* e : control_inputs) { |
| 531 | cnode->add_control_input(Id(e->src())); |
| 532 | } |
| 533 | |
| 534 | cnode->set_temporary_memory_size(TempMemorySize(n).value()); |
| 535 | cnode->set_persistent_memory_size(PersistentMemorySize(n).value()); |
no test coverage detected