pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
82.02k stars 21.99k forks source link

Why C++ version libtorch so slow #35290

Open CaichaoGitHub opened 4 years ago

CaichaoGitHub commented 4 years ago

my code is sample

torch::jit::getProfilingMode() = false; torch::jit::setGraphExecutorOptimize(false); torch::Tensor tensor = torch::zeros({1,3, 1280, 1280},at::kFloat); torch::jit::script::Module model = torch::jit::load("./psenet.pt"); model.eval(); std::vector<torch::jit::IValue> inputs; inputs.push_back(tensor); long long t1 = getSystemTime(); for(int i = 0; i < 8 ;++i) { auto output = model.forward(inputs).toTensor(); }

`long long t2 = getSystemTime();
printf("cost %f \n",(t2 - t1) / 8.0 / 1000.0);`

but the code is 2 times slower than pytorch version ? anyone know the problem?

cc @suo

suo commented 4 years ago

Hi, thank you for the report! Are you able to share the model that is seeing this issue? It's difficult to investigate without a repro. Thanks!

CaichaoGitHub commented 4 years ago

Hi, thank you for the report! Are you able to share the model that is seeing this issue? It's difficult to investigate without a repro. Thanks!

thank you for the reply! my model was the PSENet , which i cloned from https://github.com/whai362/PSENet , i converted it using the torch.jit.trace , and i tested it on my mac-book . the libtorch was downloaded from the official site without any modification

suo commented 4 years ago

One thing that I noticed is that you're running without profiling or optimizations. What happens if you turn it on, warm up the model (3-5 runs should be sufficient), then measure the avg time after those runs?

WANGCHAO1996 commented 3 years ago

One thing that I noticed is that you're running without profiling or optimizations. What happens if you turn it on, warm up the model (3-5 runs should be sufficient), then measure the avg time after those runs?

Hello, my cuda10.2 libtorch1.7.1 model yolov5 was slow in the first two tests and the second one was the slowest and then normal. What is the reason? ![Uploading 图片.png…]()

gemfield commented 3 years ago

libtorch performance issue analysis: https://zhuanlan.zhihu.com/p/363319763