Skip to content

fix: TG013 downloads and device restores, and preprocessors are not models - #51

Merged
HimanshuJanbandhu merged 1 commit into
mainfrom
fix/tg013-triage
Aug 19, 2026
Merged

fix: TG013 downloads and device restores, and preprocessors are not models#51
HimanshuJanbandhu merged 1 commit into
mainfrom
fix/tg013-triage

Conversation

@HimanshuJanbandhu

Copy link
Copy Markdown
Contributor

Finishes the triage. TG013 was the one rule left without a verdict, and TG002 had two causes still standing.

TG013 — 3 false of 10, two narrow causes

A download is not a redundant upload. _device_argument accepted a "cpu" literal as a destination, so this was reported:

tensor = torch.arange(1, 1_000_000, device="cuda")...
for i in range(100):
    cpu_tensor = tensor.to("cpu", non_blocking=True)

That's pinmem_nonblock.py — a tutorial whose entire subject is measuring transfer behaviour, copying to the host, on a tensor already on the device. Wrong twice over.

Restoring the device after a deliberate .cpu() is required. fast_neural_style does:

transformer.eval().cpu()
torch.save(transformer.state_dict(), ckpt_model_path)
transformer.to(device).train()      # we said: hoist this out of the loop

Hoisting it would leave the model on the host for the rest of training.

The other 7 are true — the mild device= advice the rule exists to give (a host factory or torch.tensor(python_list) inside a loop), plus one genuinely hoistable constant, self.STOP_TOKENS_TENSOR.to(self._device) in torchtune.

TG002 — from_pretrained made a tokenizer a model

from_pretrained sits in MODEL_WRAPPERS and matched before anything could object, so AutoTokenizer.from_pretrained(...) was a model and this was a forward pass:

dataset.map(lambda x: tokenizer(x["question"]), batched=True)

We reported a missing no_grad around tokenisation. Feature extractors and image processors the same. The exclusion is by class name and runs first; a guard test asserts AutoModelForCausalLM.from_pretrained still counts as a model.

Result

errors warnings notes
before 13 49 207
after 11 46 207

Five removed, zero new findings.

Where the triage ends up

Every rule now has a verdict. Remaining: TG004 207 (verified accurate, notes), TG008 31, TG003 3 (true, fixed upstream in pytorch/examples#1424), TG013 7, TG001 10, TG002 6.

Two items are left as decisions rather than fixes, recorded in the TODO:

  • TG008 (31) — factually right, but mostly synthetic-data example scripts where seeding changes nothing observable. Same profile that moved TG004 to note under RFC 0003, so it deserves the same test: is this code defective, or merely untuned?
  • Two TG001 causes with one finding each — the label naming heuristic on non-tensor lists, and container element types. One finding is not yet a pattern.

Testing

447 tests, 4 new — two per fix, each including the guard for the case the fix must not silence.

Stacks on #49 and #50.

…odels

Finishes the triage: TG013 was the one rule left without a verdict, and
TG002 had two causes still standing.

TG013, 3 false of 10, from two narrow causes.

A download is not a redundant upload. `_device_argument` accepted a
"cpu" literal as a destination, so `pinmem_nonblock.py` was reported for
looping 100 times over `tensor.to("cpu", non_blocking=True)` -- on a
tensor created with `device="cuda"`, in a tutorial whose whole subject is
measuring transfer behaviour. Wrong twice over.

Restoring the device after a deliberate `.cpu()` is required, not
redundant. `fast_neural_style` does `transformer.eval().cpu()`, writes a
checkpoint, then `transformer.to(device).train()`. Hoisting that out
would leave the model on the host for the rest of training.

The other 7 are the mild `device=` advice the rule exists to give, plus
one genuinely hoistable constant in torchtune.

TG002: `from_pretrained` sits in MODEL_WRAPPERS and matched before
anything could object, so `AutoTokenizer.from_pretrained(...)` was a
model and `tokenizer(x["question"])` was a forward pass -- we reported a
missing `no_grad` around tokenisation. Feature extractors and image
processors the same. The exclusion is by class name and runs first; a
guard test asserts `AutoModelForCausalLM.from_pretrained` still counts.

Across the seven repos: errors 13 -> 11, warnings 49 -> 46, five removed,
no new findings.
@HimanshuJanbandhu
HimanshuJanbandhu merged commit 86b6458 into main Aug 19, 2026
8 checks passed
@HimanshuJanbandhu
HimanshuJanbandhu deleted the fix/tg013-triage branch August 19, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant