WEBVTT

1
00:00:00.000 --> 00:00:02.871
Part 11 of ML Libraries with a Bot.

2
00:00:02.871 --> 00:00:07.218
Read ML Error Messages and Profile Code:
Cheat Sheet.

3
00:00:07.218 --> 00:00:14.108
Machine learning errors look scary, but
most of them say exactly what's wrong in
the

4
00:00:14.108 --> 00:00:15.011
first line.

5
00:00:15.011 --> 00:00:21.327
This cheat sheet causes four common
PyTorch errors on purpose, shows the real

6
00:00:21.327 --> 00:00:23.952
messages, and explains each one.

7
00:00:23.952 --> 00:00:30.432
Then it uses Python's built-in profiler to
find which part of a script is slow.

8
00:00:30.432 --> 00:00:36.994
Throughout, the aim is to get a bot to fix
the cause, not just make the error go

9
00:00:36.994 --> 00:00:37.404
away.

10
00:00:38.250 --> 00:00:40.705
What the bot can and cannot do.

11
00:00:40.705 --> 00:00:45.060
Decode an error message and point to the
line at fault.

12
00:00:45.060 --> 00:00:48.148
Suggest a fix and explain why it works.

13
00:00:48.148 --> 00:00:52.029
Read profiler output and say where the
time goes.

14
00:00:52.029 --> 00:00:53.929
Debug code it can't see.

15
00:00:53.929 --> 00:00:57.889
Paste the code, the full traceback and the
shapes.

16
00:00:57.889 --> 00:00:59.947
Know your intended shapes.

17
00:00:59.947 --> 00:01:05.649
If it "fixes" a shape error by reshaping,
check the reshape makes sense.

18
00:01:05.649 --> 00:01:07.154
Promise a speed-up.

19
00:01:07.154 --> 00:01:09.608
Profile again after the change.

20
00:01:10.208 --> 00:01:15.523
Read the first and last lines of the
traceback before asking anyone.

21
00:01:15.523 --> 00:01:18.805
Then print the shapes and dtypes involved.

22
00:01:18.805 --> 00:01:21.775
Half the time that's the whole answer.

23
00:01:22.375 --> 00:01:29.177
Real output: addmm: mat1 and mat2 shapes
cannot be multiplied (8x3 and 4x2).

24
00:01:29.177 --> 00:01:35.353
A Linear(4, 2) layer expects 4 features
per row, but the input has 3.

25
00:01:35.353 --> 00:01:37.859
The batch size of 8 is fine.

26
00:01:37.859 --> 00:01:42.692
Fix the data or the layer so the feature
counts match.

27
00:01:43.292 --> 00:01:48.878
The prompt: Here's my code, the full
traceback and the shapes and dtypes of the

28
00:01:48.878 --> 00:01:50.717
tensors involved (pasted).

29
00:01:50.717 --> 00:01:56.586
Explain the first line of the error in
plain English, point to the line that
causes

30
00:01:56.586 --> 00:02:00.758
it, and suggest a fix that keeps the data
meaning the same.

31
00:02:00.758 --> 00:02:05.425
If you'd need to know my intended shapes,
ask instead of guessing.

32
00:02:06.167 --> 00:02:12.765
Python's documentation describes cProfile
as a profiler suitable for most users.

33
00:02:12.765 --> 00:02:19.445
We profiled two ways of computing the
length of each row in a 20,000 by 16
array:

34
00:02:19.445 --> 00:02:26.208
Real output from one run on our box: Both
functions give the same answer, which we

35
00:02:26.208 --> 00:02:27.198
check first.

36
00:02:27.198 --> 00:02:33.961
The Python loop took about 0.061 seconds,
against about 0.0013 seconds for NumPy's

37
00:02:33.961 --> 00:02:35.528
vectorized version.

38
00:02:36.375 --> 00:02:42.806
For PyTorch models, torch.profiler can
show time per operator and on the GPU.

39
00:02:42.806 --> 00:02:46.146
Its docs page explains how to set it up.

40
00:02:46.146 --> 00:02:47.983
We didn't run it here.

41
00:02:48.583 --> 00:02:51.909
Shape errors: print .shape for each input.

42
00:02:51.909 --> 00:02:54.047
Dtype errors: print .dtype.

43
00:02:54.047 --> 00:02:57.768
Labels for classification are usually
integers.

44
00:02:57.768 --> 00:03:02.994
Device errors: print .device, and check
torch.cuda.is_available().

45
00:03:02.994 --> 00:03:07.587
Out of memory: lower the batch size first,
and see Part 2.

46
00:03:07.587 --> 00:03:13.525
Slow code: profile before you optimize,
and check the answer didn't change.

47
00:03:14.125 --> 00:03:16.208
Tested vs documented.

48
00:03:16.208 --> 00:03:24.045
Tested by us: all four errors and the
cProfile run, with PyTorch 2.15.0 CPU and

49
00:03:24.045 --> 00:03:25.236
NumPy 2.5.4.

50
00:03:25.236 --> 00:03:32.874
Documented, not tested by us:
torch.profiler and the device-selection
advice.

51
00:03:32.874 --> 00:03:36.842
Not claimed: the timings as a benchmark.

52
00:03:37.542 --> 00:03:42.358
The last part of the series reviews
training code with a bot and recaps
everything.

53
00:03:42.958 --> 00:03:49.002
Before you run anything: check every bot
answer against the official docs, work in
a

54
00:03:49.002 --> 00:03:54.902
virtual environment, load model files only
from sources you trust, respect dataset

55
00:03:54.902 --> 00:04:00.154
and model licenses, and keep personal data
out of bots and training sets.

56
00:04:00.154 --> 00:04:02.673
Every source is linked on the page.

