Clearly different results on different machines

AspenDove: <p>I implemented YOLOv3 using libtorch and trained with Telsa V100 on the server several times until the recall level was high enough. However, when I turned the model into a CPU version and saved it, I used the same code on my laptop and found that the recall was extremely low. Therefore, I designed a comparison experiment. Using the model, code and training set exactly the same as those in the laptop on the server, the running result display was quite different from the result on my laptop in recall. As is shown in <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5af3f944ba1acf784c8d46e060e547deb08c6b0e.png" data-download-href="https://discuss.pytorch.org/uploads/default/5af3f944ba1acf784c8d46e060e547deb08c6b0e" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5af3f944ba1acf784c8d46e060e547deb08c6b0e.png" alt="image" data-base62-sha1="cYBGnJeVpHqjM4U1uRWejYBmvvM" width="402" height="500" data-dominant-color="141111"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">661×821 19.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
which is the result on server, running ubuntu with CPU mode, libtorch 1.5.0,<br>
and the result on my laptop<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/c/acbc85dc12b0d1240b8dfc480ace8eff8609094b.png" data-download-href="https://discuss.pytorch.org/uploads/default/acbc85dc12b0d1240b8dfc480ace8eff8609094b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/c/acbc85dc12b0d1240b8dfc480ace8eff8609094b.png" alt="image" data-base62-sha1="oE624aZfJACoVBtepHIZTvNTV6X" width="303" height="500" data-dominant-color="1D1A1A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">611×1006 12 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
running windows10 with also CPU mode, libtorch 1.5.0.</p>
<p>You can see the apparent difference in metrics.</p>
<p>This is a completely incomprehensible result, and I’ve managed to control the variables as much as possible, except for the difference between the two operating systems and the hardware systems.I don’t know if this is a normal phenomenon, but if there is such a huge difference between different systems, how do you transplant pre-training weights?</p>

ptrblck: <p>There might be several different issues, so let’s split them up.</p>
<ol>
<li>
<p>The pretrained model should give you approx. the same outputs up to the limited floating point precision. Especially while using different hardware, you might not be able to overcome the absolute errors introduces by the floating point precision. If you load the <code>state_dict</code> properly, set <code>model.eval()</code>, and still get largely different results, we would need to see the code to debug further.</p>
</li>
<li>
<p>Training on different platform might also yield slightly different results due to the aforementioned reasons. However, it would be interesting to see how reliable the difference is. I.e. out of e.g. 10 runs on each platform with different seeds, how often does one model converge while the other diverges?</p>
</li>
</ol>

AspenDove: <p>Thank you for your prompt reply.</p>
<p>I can certainly understand the floating point differences between platforms, but it seems that I have encountered more than I expected. I thought about it for a while, but I still didn’t know how to solve the problem. For example, I would eventually deploy to a laptop, so would I eventually have to train on a laptop? In my experience, this doesn’t make much sense.</p>
<p>On the other hand, you mentioned the need to see the code to debug further. so how exactly to do that?</p>

ptrblck: <aside class="quote no-group" data-username="AspenDove" data-post="3" data-topic="81768">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/aspendove/48/22951_2.png" class="avatar"> AspenDove:</div>
<blockquote>
<p>For example, I would eventually deploy to a laptop, so would I eventually have to train on a laptop? In my experience, this doesn’t make much sense.</p>
</blockquote>
</aside>
<p>No, as mentioned above, the pretrained model should give you the same outputs up the the floating point precision.</p>
<aside class="quote no-group" data-username="AspenDove" data-post="3" data-topic="81768">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/aspendove/48/22951_2.png" class="avatar"> AspenDove:</div>
<blockquote>
<p>On the other hand, you mentioned the need to see the code to debug further. so how exactly to do that?</p>
</blockquote>
</aside>
<p>You could write an evluation script, which loads the model, uses a constant tensor, applies the forward pass, and prints the output.<br>
Post this code here by wrapping it into three backticks ``` and also post the outputs from the Linux server and your laptop.</p>

AspenDove: <p>This is the code snippet I used to test:</p>
<pre><code class="lang-auto">device_type = torch::kCPU;
torch::Device device(device_type);

Yolo net(device);
/*
auto train_set = VOCDataset(device).map(Collate{});//.map(torch::data::transforms::Stack&lt;&gt;());
auto train_size = train_set.size().value();
auto train_loader = torch::data::make_data_loader&lt;torch::data::samplers::SequentialSampler&gt;
		(std::move(train_set), 
			torch::data::DataLoaderOptions().workers(4).batch_size(train_opts.batch_size));

torch::optim::Adam optimizer(net-&gt;parameters(), 
	torch::optim::AdamOptions(train_opts.learn_rate)
	.weight_decay(train_opts.weight_decay));
*/
torch::load(net, train_opts.model_path);

cv::Mat origin_image = cv::imread("./imgs/dog.jpg"), resized_image;
cv::cvtColor(origin_image, resized_image, cv::COLOR_BGR2RGB);
resized_image = resizeKeepAspectRatio(origin_image, cv::Size(416, 416), { 128,128,128 });

cv::Mat img_float;
resized_image.convertTo(img_float, CV_32F, 1.0 / 255);

auto img_tensor = torch::from_blob(img_float.data, { 1, 416, 416, 3 }).to(device);
img_tensor = img_tensor.permute({ 0,3,1,2 }).contiguous();
auto output = net-&gt;forward(img_tensor);

std::cout &lt;&lt; non_max_suppression(output, 20, 0.1, 0.9);
</code></pre>
<p>Here is what I got on server:</p>
<pre><code class="lang-auto">   0.0000   66.9942  147.0321  173.0608  340.8369    0.1800    0.9806    0.0000
[ CPUFloatType{1,8} ]
</code></pre>
<p>while here is what I got on my laptop:</p>
<pre><code class="lang-auto">   0.0000  222.2365  106.0619  242.9099  132.0313    0.1007    0.9915    0.0000
   0.0000  136.0336  192.0411  292.1751  281.3036    0.1112    0.9894    0.0000
   0.0000   66.4557  148.0490  172.6351  341.0895    0.2453    0.9815    0.0000
[ CPUFloatType{3,8} ]
</code></pre>

ptrblck: <p>Assuming you are running this code snippet on both machines, I would recommend to check the intermediate tensors such as the <code>output</code> as well as the output activations using hooks.<br>
If I’m not mistaken, forward hooks are not available in <code>libtorch</code> yet, so you could use the Python frontend.</p>