MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / TEST

Function TEST

src/tensorrt/test/tensorrt.cpp:20–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18using namespace opr;
19
20TEST(TestOprTensorRT, Basic) {
21 REQUIRE_GPU(1);
22 intl::SimpleTensorRTNetwork net;
23
24 auto p = net.create_trt_network(true);
25 auto trt_net = TensorRTOpr::to_shared_ptr_network(p.second);
26 auto y2 = TensorRTOpr::make(
27 TensorRTOpr::to_shared_ptr_builder(p.first), trt_net,
28 intl::TensorRTGraphFeatureBits::NCHW_FLOAT, {}, {net.x})[0];
29
30 HostTensorND host_z1;
31 HostTensorND host_z2;
32 auto func = net.graph->compile(
33 {make_callback_copy(net.y, host_z1), make_callback_copy(y2, host_z2)});
34 func->execute();
35 MGB_ASSERT_TENSOR_NEAR(host_z1, host_z2, 2e-4);
36
37 auto&& host_x = net.host_x;
38
39// Fixme: support dynamic shape
40#if NV_TENSOR_RT_VERSION < 8500
41 auto&& gen = net.gen;
42 *host_x = *gen({1, 23, 43, 43});
43 func->execute();
44 MGB_ASSERT_TENSOR_NEAR(host_z1, host_z2, 2e-4);
45 *host_x = *gen({10, 23, 12, 12});
46 func->execute();
47 MGB_ASSERT_TENSOR_NEAR(host_z1, host_z2, 1e-3);
48
49 *host_x = *gen({10, 23, 12, 12});
50 func->execute();
51 MGB_ASSERT_TENSOR_NEAR(host_z1, host_z2, 1e-3);
52#endif
53
54 // write to file so in python test we can have an engine file
55 TensorRTUniquePtr<IBuilder> builder{
56 createInferBuilder(TensorRTOpr::Logger::instance()), {}};
57 builder->setMaxBatchSize(10);
58
59#if NV_TENSOR_RT_VERSION >= 6001
60 TensorRTUniquePtr<IBuilderConfig> build_config{builder->createBuilderConfig()};
61 TensorRTUniquePtr<ICudaEngine> cuda_engine{
62 builder->buildEngineWithConfig(*trt_net, *build_config)};
63#else
64 TensorRTUniquePtr<ICudaEngine> cuda_engine{builder->buildCudaEngine(*trt_net)};
65#endif
66 TensorRTUniquePtr<IHostMemory> mem{cuda_engine->serialize(), {}};
67 FILE* fout = fopen(output_file("trt_cuda_engine_test").c_str(), "wb");
68 auto wr = fwrite(mem->data(), 1, mem->size(), fout);
69 mgb_assert(wr == mem->size());
70 fclose(fout);
71 debug::write_to_file(
72 output_file("trt_cuda_engine_test.input").c_str(),
73 debug::dump_tensor(*host_x, "x"));
74 debug::write_to_file(
75 output_file("trt_cuda_engine_test.output").c_str(),
76 debug::dump_tensor(host_z1, "x"));
77}

Callers

nothing calls this directly

Calls 15

to_shared_ptr_networkFunction · 0.85
to_shared_ptr_builderFunction · 0.85
make_callback_copyFunction · 0.85
output_fileFunction · 0.85
dump_tensorFunction · 0.85
create_trt_networkMethod · 0.80
makeFunction · 0.50
genFunction · 0.50
loadFunction · 0.50
compileMethod · 0.45
executeMethod · 0.45
serializeMethod · 0.45

Tested by

no test coverage detected