| 1430 | } |
| 1431 | |
| 1432 | void TensorRTReplacePass::Impl::update_graph() { |
| 1433 | using GpuAllocator = opr::TensorRTOpr::GpuAllocator; |
| 1434 | using TensorRTOpr = opr::TensorRTOpr; |
| 1435 | |
| 1436 | std::shared_ptr<GpuAllocator> gpu_allocator; |
| 1437 | std::shared_ptr<ExtraDep> extra_dep = std::make_shared<ExtraDep>(); |
| 1438 | |
| 1439 | // construct trt network |
| 1440 | auto construct_network = [this, &gpu_allocator, &extra_dep](OperatorNodeBase* opr) { |
| 1441 | if (!has_fail_msg(opr).valid()) { |
| 1442 | auto cn = opr->output(0)->comp_node(); |
| 1443 | auto trt_graph = m_tensorrt_graphs[m_graph_map[opr] - 1]; |
| 1444 | auto b = trt_graph->builder; |
| 1445 | mgb_assert(b != nullptr); |
| 1446 | if (!gpu_allocator) { |
| 1447 | gpu_allocator = std::make_shared<GpuAllocator>(cn); |
| 1448 | b->setGpuAllocator(gpu_allocator.get()); |
| 1449 | } else { |
| 1450 | auto cn0 = gpu_allocator->comp_node(); |
| 1451 | mgb_assert( |
| 1452 | cn0 == cn, |
| 1453 | "multiple comp nodes for trt graph are not " |
| 1454 | "supported: %s %s", |
| 1455 | cn0.to_string().c_str(), cn.to_string().c_str()); |
| 1456 | } |
| 1457 | |
| 1458 | if (!trt_graph->network) { |
| 1459 | #if NV_TENSOR_RT_VERSION >= 6001 |
| 1460 | nvinfer1::NetworkDefinitionCreationFlags flags; |
| 1461 | flags = 1 << static_cast<int>(nvinfer1::NetworkDefinitionCreationFlag:: |
| 1462 | kEXPLICIT_BATCH); |
| 1463 | trt_graph->network = b->createNetworkV2(flags); |
| 1464 | #else |
| 1465 | trt_graph->network = b->createNetwork(); |
| 1466 | #endif |
| 1467 | } |
| 1468 | // make extra dep |
| 1469 | for (auto&& inp : trt_graph->inputs) { |
| 1470 | extra_dep->operator[](opr).push_back(inp); |
| 1471 | } |
| 1472 | |
| 1473 | auto iter = m_opr_trait.find(opr->dyn_typeinfo()); |
| 1474 | if (iter != m_opr_trait.end()) { |
| 1475 | if (iter->second.add_to_nvinfer) { |
| 1476 | iter->second.add_to_nvinfer(trt_graph->network, opr); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | }; |
| 1481 | m_opt_state.graph().iter(construct_network); |
| 1482 | |
| 1483 | // trt network markOutput |
| 1484 | for (auto trt_graph : m_tensorrt_graphs) { |
| 1485 | // record traverse order |
| 1486 | size_t idx = 0; |
| 1487 | auto&& varnode2itensor = trt_graph->varnode2itensor; |
| 1488 | for (auto output : trt_graph->outputs) { |
| 1489 | trt_graph->output2idx[output] = idx++; |
nothing calls this directly
no test coverage detected