Chapter 5: running train_model_simple() on CPU is really slow, why? #1037
|
Hello, However, the book says that a MacBook Air completed the training in about 5 minutes. Running on Debian GNU/Linux forky, with PyTorch 2.12.0 . What I am seeing is that PyTorch seems to run with 6 threads (equal to the number of physical cores, by default): But, during the training, I can see (with What am I missing? |
Replies: 1 comment 1 reply
|
Those five idle threads are the whole story — PyTorch is configured for six but only actually using one. Which also means the M3 comparison is a red herring. Your CPU shouldn't be 36× slower than a MacBook Air here; a working build lands in the tens of minutes at worst. Three hours means something's broken, not that the hardware is weak. Quickest way to confirm, with the training loop out of the picture entirely: Those five idle threads are the whole story — PyTorch is configured for six but only actually using one. Which also means the M3 comparison is a red herring. Your CPU shouldn't be 36× slower than a MacBook Air here; a working build lands in the tens of minutes at worst. Three hours means something's broken, not that the hardware is weak. Quickest way to confirm, with the training loop out of the picture entirely: import time, torch
a = torch.randn(4096, 4096)
b = torch.randn(4096, 4096)
torch.mm(a, b)
t0 = time.perf_counter()
for _ in range(5):
torch.mm(a, b)
dt = (time.perf_counter() - t0) / 5
print(f"{dt*1000:.0f} ms {2*4096**3/dt/1e9:.0f} GFLOP/s")Watch If that's what you get, check where torch came from — Re-run the benchmark after and compare. Also worth a quick Your losses match the book, so the model code is fine. It's purely a threading problem. |
Hello @darekwojciechowski, thanks a lot for your answer. It helped me to pinpoint the root cause of the performance issue.
TL;DR: Everything is fine with Debian packages, after switching to OpenMP-enabled OpenBLAS (see below for details).
I tried the small benchmark you suggested:
I ran this benchmark with the Debia…