# ML training

> Record a PyTorch training loop and read where NaN or inf first appeared, which tensors each step received, and which files were opened.

When your program has imported numpy or torch, each call of your code that receives or returns an array or a tensor has one more line per such value: shape, dtype, device, how many NaN, how many inf, the smallest and the largest number. The example is [ml-training](/examples/ml-training.md): a small model trained on CPU, where a batch whose loss is not a finite number is left out.

## Record the loop

```bash
ormaos run -- python train.py
```

```console
$ ormaos run -- python train.py
trained 40 steps, last loss 379.2, checkpoint model.pt
ormaos: r1 exit 0 in …: 1 Python process, 203 calls, 0 exceptions, 10 io
ormaos: not collected: logs with no line of user code: 5 logging records were asked for with no frame of user code waiting (library threads, the end of the interpreter): they are counted here and not recorded; the first loggers: torch._dynamo.eval_frame, torch._dynamo.utils, torch._subclasses.fake_tensor
ormaos: saved in .ormaos/r1, next: ormaos tree r1:c1
```

## Where was NaN or inf first seen, in which tensor?

`ormaos show` counts the arrays and tensors at the calls and names the first call where one had NaN or inf:

```console
$ ormaos show
r1 exit 0 in …: 1 Python process, 203 calls, 0 exceptions (0 swallowed, 0 uncaught)
command: python train.py
code: git commit not read (the working directory is not inside a git repository)
io: 10 events (file 10)
tensors: an array or a tensor was an argument or a result 400 times, 10 with NaN or inf: return of standardize at train.py:32  [r1:c56]
not collected: logs with no line of user code: 5 logging records were asked for with no frame of user code waiting (library threads, the end of the interpreter): they are counted here and not recorded; the first loggers: torch._dynamo.eval_frame, torch._dynamo.utils, torch._subclasses.fake_tensor
never recorded: values, and 20 more kinds of things (`ormaos show --full` lists them with the reason)
next: ormaos tree r1:c1 | ormaos io r1 | ormaos tree r1:c56
```

```console
$ ormaos tree r1:c56
inside: r1:c1 <module> > r1:c2 main > r1:c55 train_step
r1:c56 standardize(batch: Tensor[8x4 float32 cpu]) -> Tensor[8x4 float32 cpu]  train.py:32  …
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 26.9506
      returned: shape 8x4, float32, cpu, nan 8, inf 0, min -1.41653, max 2.45451
next: ormaos tree r1:c55 | ormaos exceptions r1:c56
```

`standardize` received a batch with no NaN and returned one with 8 NaN. The program printed `trained 40 steps`; the tensors say what one of those steps was given.

## What did each step receive?

```console
$ ormaos tree r1 --function train_step --limit 3
r1: 40 of the 203 calls match --function train_step (40 train_step; 0 raised)
r1:c5 train_step(model: torch.nn.modules.container.Sequential, optimizer: torch.optim.adam.Adam, ...) -> float  train.py:50  …  calls below: 4
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 27.7474
      minutes: shape 8, float32, cpu, nan 0, inf 0, min 28.5324, max 123.528
r1:c10 train_step(model: torch.nn.modules.container.Sequential, optimizer: torch.optim.adam.Adam, ...) -> float  train.py:50  …  calls below: 4
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 29.0304
      minutes: shape 8, float32, cpu, nan 0, inf 0, min 30.2604, max 134.456
r1:c15 train_step(model: torch.nn.modules.container.Sequential, optimizer: torch.optim.adam.Adam, ...) -> float  train.py:50  …  calls below: 4
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 20.893
      minutes: shape 8, float32, cpu, nan 0, inf 0, min 33.8778, max 95.9466
... 37 more: --offset 3
next: ormaos tree r1:c5 | ormaos tree r1:c10 | ormaos tree r1 --offset 3 --function train_step | ormaos tree r1 | ormaos exceptions r1
```

```console
$ ormaos tree r1 --function standardize --limit 4
r1: 40 of the 203 calls match --function standardize (40 standardize; 0 raised)
r1:c6 standardize(batch: Tensor[8x4 float32 cpu]) -> Tensor[8x4 float32 cpu]  train.py:32  …
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 27.7474
      returned: shape 8x4, float32, cpu, nan 0, inf 0, min -1.39879, max 1.71224
r1:c11 standardize(batch: Tensor[8x4 float32 cpu]) -> Tensor[8x4 float32 cpu]  train.py:32  …
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 29.0304
      returned: shape 8x4, float32, cpu, nan 0, inf 0, min -1.44715, max 1.59425
r1:c16 standardize(batch: Tensor[8x4 float32 cpu]) -> Tensor[8x4 float32 cpu]  train.py:32  …
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 20.893
      returned: shape 8x4, float32, cpu, nan 0, inf 0, min -2.00666, max 1.62019
r1:c21 standardize(batch: Tensor[8x4 float32 cpu]) -> Tensor[8x4 float32 cpu]  train.py:32  …
      batch: shape 8x4, float32, cpu, nan 0, inf 0, min 0, max 26.9984
      returned: shape 8x4, float32, cpu, nan 0, inf 0, min -1.74082, max 1.40948
... 36 more: --offset 4
next: ormaos tree r1:c6 | ormaos tree r1:c11 | ormaos tree r1 --offset 4 --function standardize | ormaos tree r1 | ormaos exceptions r1
```

## Which files were opened?

```console
$ ormaos io r1
r1: 10 io events (file 10)
  1x file open /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/cuda/random.py (read)  at train.py:63 in main  [r1:i1]
  1x file read /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/cuda/random.py  at train.py:63 in main  [r1:i2]
  1x file open /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/random.py (read)  at train.py:63 in main  [r1:i3]
  1x file read /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/random.py  at train.py:63 in main  [r1:i4]
  1x file open /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/xpu/__init__.py (read)  at train.py:63 in main  [r1:i5]
  1x file read /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/xpu/__init__.py  at train.py:63 in main  [r1:i6]
  1x file open /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/xpu/random.py (read)  at train.py:63 in main  [r1:i7]
  1x file read /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/xpu/random.py  at train.py:63 in main  [r1:i8]
  1x file open /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/mtia/__init__.py (read)  at train.py:63 in main  [r1:i9]
  1x file read /tmp/ormaos-docs-generate/venv/lib/python3.14/site-packages/torch/mtia/__init__.py  at train.py:63 in main  [r1:i10]
next: ormaos io r1:i1 | ormaos io r1:i2 | ormaos io r1:c2 | ormaos exceptions r1
```

These are source files of torch, opened and read by torch itself, at line 63 of `train.py`, the line of your code that called it.

## A loop with very many calls

The recording keeps at most 200,000 calls per process. For a long training run, record the context, the exceptions and the I/O only:

```bash
ormaos run --only context,exceptions,io -- python train.py
```

```console
$ ormaos show r2
r2 exit 0 in …: 1 Python process, 0 calls, 0 exceptions (0 swallowed, 0 uncaught)
command: python train.py
code: git commit not read (the working directory is not inside a git repository)
recorded only: context, exceptions, io
io: 10 events (file 10)
never recorded: values, and 17 more kinds of things (`ormaos show --full` lists them with the reason)
next: ormaos io r2
```

`ormaos diff` then says what it can compare and what it cannot:

```console
$ ormaos diff r1 r2
r1 -> r2, matched by place in the code and order, not by address
  r1: exit 0, 203 calls, 0 exceptions, 10 io events
  r2: exit 0, 0 calls, 0 exceptions, 10 io events
capabilities: calls context dataframes exceptions io logs tensors in r1, context exceptions io in r2
calls: not compared (calls not recorded in r2)
exceptions: the same in both (0 groups)
io: the same in both (10 groups)
logs: not compared (logs not recorded in r2)
data: not compared (dataframes and tensors not recorded in r2)
not collected only in r1: logs with no line of user code
not compared: values, shapes of arguments and results, durations, cpu and memory, the lines of `path`, which process or thread an event was in, the versions of the code
next: ormaos show r1 | ormaos show r2
```

## Notes for ML code

- Health (NaN, inf, smallest, largest) is computed on the CPU only: for a tensor on another device it is not computed, because the tensor would be copied. Shape, dtype and device are always there.
- Health is not computed for an array of more than 1,000,000 elements, while `torch.jit` traces, or after the counting has taken 2 seconds in one process. Each is said in `not_collected`.
- The smallest and the largest number are numbers of your data; they are the only ones written.
- Worker processes started with `multiprocessing` (a `DataLoader` with workers, for example) are processes of the same recording; `ormaos show --full` lists each one and how it ended.

---

Source: https://docs.ormaos.com/python/guides/ml-training/ · ormaos 0.0.1 · ormaos-python 968f69776c · ormaos-examples 6e27be5922
