#!/usr/bin/env bash # Phase 1a: install ms-swift (Megatron backend) on the pod WITHOUT touching NGC torch. # Source: swift.readthedocs.io/en/latest/BestPractices/deepseek-v4.html (fetched 2026-08-03) set -euo pipefail WS=${WS:-/workspace} SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) if [[ -f "$WS/env.sh" ]]; then # shellcheck disable=SC1090 source "$WS/env.sh" fi if [[ -n "${DSV4VL_VENV:-}" ]]; then test -x "$DSV4VL_VENV/bin/python" || { echo "Missing requested persistent venv: $DSV4VL_VENV" >&2 exit 2 } # shellcheck disable=SC1090 source "$DSV4VL_VENV/bin/activate" fi # Guard: record torch before, verify unchanged after (hard rule 1). TORCH_BEFORE=$(python -c "import torch; print(torch.__version__)") # Constraint file pins torch so no dependency can replace it. python - <<'EOF' import torch, pathlib pathlib.Path("/tmp/constraints.txt").write_text(f"torch=={torch.__version__}\n") EOF export PIP_CONSTRAINT=/tmp/constraints.txt # Megatron commit fd1121b8 requires the NVRx 0.6 async-checkpoint API. NGC # 25.06 bundles 0.4.0, whose namespace package also lacks __version__. pip install "nvidia-resiliency-ext==0.6.0" # NGC 25.06 ships safetensors 0.5.3, which cannot deserialize the # DeepSeek-V4 FP4-scale dtype (F8_E8M0). 0.8.0 opens all 48 source shards. pip install "safetensors==0.8.0" # Transformers 5.12's pre-quantized DSV4 attention dispatches through the # public kernels-community/finegrained-fp8 implementation. Pin inside the # release's declared >=0.12,<0.13 range and prohibit dependency resolution so # this optional runtime cannot replace the NGC torch stack. # Install the Hub client first: Python packaging enumerates every installed # entry point while Megatron builds its metadata, which imports `kernels` and # therefore its optional Hub module even before ms-swift installs Transformers. pip install "huggingface_hub==1.26.0" pip install --no-deps "kernels==0.12.3" # Megatron-LM dev branch — tested commit from the swift best-practices doc. MEGATRON_COMMIT=fd1121b8ff7e3a4f83a28d35aed172d7bc0260e1 MCORE_BRIDGE_COMMIT=1f0b2c4ead2d607e9c90af64070a67597cd0f360 MS_SWIFT_COMMIT=49efcbfe59e480d4fa9a8bdecdb76c743d0af37d pip install "git+https://github.com/NVIDIA/Megatron-LM.git@${MEGATRON_COMMIT}" pip install "git+https://github.com/modelscope/mcore-bridge.git@${MCORE_BRIDGE_COMMIT}" pip install "ms-swift[megatron] @ git+https://github.com/modelscope/ms-swift.git@${MS_SWIFT_COMMIT}" # ms-swift's broad dataset requirement currently selects PyArrow 25, which # conflicts with the NGC 25.06 cuDF stack. Reveal the image's PyArrow 19.0.1 # again and restore the exact data versions used to build/verify the Stage-2 # mix. These pins satisfy ms-swift's declared datasets>=3,<4.8.5 range. pip uninstall -y pyarrow pip install --no-deps --force-reinstall -r "$SCRIPT_DIR/../requirements-stage2-data.txt" pip install "packaging==24.2" python "$SCRIPT_DIR/verify_stage2_data_runtime.py" # DeepSeek-V4 compressed/heavily-compressed attention rotates the DSA indexer # activations with this CUDA kernel. Build only Hopper (H100/H200 = SM90) and # reuse the pinned NGC Torch instead of creating a build-isolation environment. MAX_JOBS=16 TORCH_CUDA_ARCH_LIST=9.0 \ pip install --no-build-isolation \ "git+https://github.com/Dao-AILab/fast-hadamard-transform.git@1cc807efbd6cc001df359822d60bf6052dd66859" # Apply idempotent compatibility fixes for NGC TE, PEFT/TorchAO discovery, and # the fail-closed attention-only trainable audit. python "$SCRIPT_DIR/patch_runtime_compat.py" TORCH_AFTER=$(python -c "import torch; print(torch.__version__)") if [ "$TORCH_BEFORE" != "$TORCH_AFTER" ]; then echo "FATAL: torch changed ($TORCH_BEFORE -> $TORCH_AFTER) — NGC/TE stack broken. Rebuild container." >&2 exit 1 fi python - <<'PY' import importlib.metadata import megatron.core import swift import transformer_engine assert importlib.metadata.version("kernels") == "0.12.3" assert importlib.metadata.version("packaging") == "24.2" print("TE/mcore/swift/kernels import OK") PY python "$SCRIPT_DIR/verify_stage2_training_runtime.py" pip check # Record environment for reproducibility. { echo "## ENV ($(date -u +%F))" echo "torch: $TORCH_AFTER" echo "Megatron-LM: $MEGATRON_COMMIT" echo "mcore-bridge: $MCORE_BRIDGE_COMMIT" echo "ms-swift: $MS_SWIFT_COMMIT" echo "venv: $(python -c 'import sys; print(sys.prefix)')" pip freeze | grep -iE 'ms-swift|mcore-bridge|megatron|transformer.engine|flash|kernels' || true nvidia-smi --query-gpu=name,memory.total --format=csv } >> "$WS/ENV.md" echo "Install OK. ENV recorded in $WS/ENV.md"