Cache the Modal sandbox install chain in image layers (#8536)
## Why
The `modal-torch-latest` sandbox installs its whole dependency chain at
runtime, after the sandbox clock starts: requirements (~3.6 min) plus
the forced Torch reinstall (~3 min) — roughly 8 of the fixed sandbox
lifetime, every run. The v1 GPU suite now runs close to that lifetime
(#8519's merge-queue run was SIGKILLed at the sandbox deadline with the
suite at 90%), so the fixed install overhead is directly eating test
budget.
## What
- Bake the static chain into content-addressed Modal image layers:
- a requirements layer keyed by the **merge-group base SHA** (bases move
slowly, so the layer cache stays hot; the raw-file URLs embed the SHA,
so a requirements change rebuilds the layer),
- a Torch layer keyed by the **preset**, applied **after** the
requirements layer.
- Layer order preserves the previous guarantee: the Torch pin lands
last, so a transitive dependency cannot displace the intended CUDA build
variant.
- The runtime `pip install -r` commands are kept unchanged as
correctness guards: they are a near no-op when the baked layers are
current, and install the difference when a candidate branch changed a
requirements file — caching only speeds things up, it never changes the
final environment.
- The runtime `reinstall Torch packages` command is removed (now the
image layer).
- `DS_CI_BASE_SHA` is wired from the workflow (`pull_request.base.sha ||
merge_group.base_sha`), falling back to the candidate SHA on events
without a base — that only lowers the cache hit rate, never correctness.
Net effect: warm runs hand ~6.5 min back to pytest; cold runs (new
preset / new base SHA) pay a one-time layer build that is shared across
subsequent runs.
## Verification
- `python3 ci/test_tests_fetcher.py`: 16/16 passed.
- `pre-commit run --files ci/torch_latest.py
.github/workflows/modal-torch-latest.yml`: all hooks passed.
- Exercised `_build_sandbox_image` and `build_remote_commands` against a
stub Modal module: layer order is requirements → Torch pin; the runtime
chain keeps the guard commands and no longer reinstalls Torch;
`base_sha` falls back to the candidate SHA on push events.
- Sandbox-equivalent execution still needs a real Modal run (first
merge-queue entry after merge will build the layers).
Signed-off-by: Ma, Guokai <guokai.ma@intel.com>