Skip to content

fix(security): set DataLoader pin_memory explicitly (automatic memory pinning) - #378

Merged
oriolpetithelical merged 3 commits into
mainfrom
fix/pytorch-automatic-memory-pinning
Jul 8, 2026
Merged

fix(security): set DataLoader pin_memory explicitly (automatic memory pinning)#378
oriolpetithelical merged 3 commits into
mainfrom
fix/pytorch-automatic-memory-pinning

Conversation

@oriolpetithelical

Copy link
Copy Markdown
Contributor

What & why

Trail of Bits' Semgrep rule automatic-memory-pinning (CWE-676) flags every torch.utils.data.DataLoader that leaves pin_memory unset — the pinning behaviour is then implicit/undefined.

This PR sets the choice explicitly on all 27 DataLoader call sites (15 files):

pin_memory=torch.cuda.is_available()
  • Pins host memory only when a CUDA GPU is present → faster, asynchronous host→GPU transfers during training/inference.
  • Avoids wasting scarce page-locked memory on CPU-only machines, where unconditional pinning gives no benefit and can add overhead.

Scope

  • 21 flagged sites (mamba2_mrna, hyena_dna, helix_mrna, uce, evo_2, caduceus): added the pin_memory kwarg.
  • 6 previously-hardcoded pin_memory=True (scgpt ×4, tahoe ×1, transcriptformer ×1): unified to the same predicate so they no longer pin unconditionally on CPU-only runs.

Validation

  • All 27 DataLoaders now carry pin_memory=torch.cuda.is_available(); 0 leftover pin_memory=True.
  • All touched files compile (py_compile) and import torch.
  • Behavioural no-op on CPU (CI); no test asserts on pin_memory.

Review note

A high-effort code review flagged that torch.cuda.is_available() keys on global CUDA presence rather than the model's configured device — so on a GPU host where a user explicitly selects device="cpu", pinned memory is still allocated. This is a minor, edge-case performance concern (no correctness bug). We deliberately kept torch.cuda.is_available() as it is the ToB-recommended idiom and satisfies the scan; a per-model configured-device check was deemed more invasive/risky for the benefit.

Refs: trailofbits.python.automatic-memory-pinning

🤖 Generated with Claude Code

bputzeys and others added 2 commits June 5, 2026 15:27
* Merge pull request #371 from LiudengZhang/fix/save-load-fine-tuning-head

Fix save_model to include fine-tuning head weights

* Bump version from 2.0.1 to 2.0.2

* Update accelerate package version to 1.13.0 (#375)

* Update pyproject.toml

---------

Co-authored-by: LiudengZhang <99156394+LiudengZhang@users.noreply.github.com>
…d pinning

Trail of Bits' Semgrep rule `automatic-memory-pinning` (CWE-676) flags
`torch.utils.data.DataLoader` calls that leave `pin_memory` unset, since
the pinning behaviour is then implicit. Explicitly gate pinning on GPU
availability across all 27 DataLoader call sites:

    pin_memory=torch.cuda.is_available()

This pins host memory only when a CUDA device is present (faster, async
host->GPU copies) and avoids wasting scarce page-locked memory on
CPU-only machines. Previously-hardcoded `pin_memory=True` sites
(scgpt, tahoe, transcriptformer) are unified to the same predicate so
they no longer pin unconditionally on CPU-only runs.

Refs: trailofbits.python.automatic-memory-pinning

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01R2VtZqWHS8Dv4V88SbjftT
@oriolpetithelical oriolpetithelical self-assigned this Jul 8, 2026
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E81Hd78n4wNdihG3hT9Wqp
@oriolpetithelical
oriolpetithelical changed the base branch from release to main July 8, 2026 15:39
@oriolpetithelical
oriolpetithelical merged commit 7d839d7 into main Jul 8, 2026
9 checks passed
@oriolpetithelical
oriolpetithelical deleted the fix/pytorch-automatic-memory-pinning branch July 8, 2026 16:15
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.

2 participants