#!/usr/bin/env bash # Run on the rented CUDA host, before starting the campaign. set -euo pipefail set +x campaign_repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" campaign_python=python3 campaign_venv="$campaign_repo/.venv" campaign_cuda_home="${CUDA_HOME:-/usr/local/cuda}" campaign_torch_backend=cu130 usage() { cat <<'EOF' Usage: bash scripts/qwen38_campaign_setup.sh [options] --python PATH Python 3.11+ interpreter (default: python3) --venv DIR Campaign environment (default: REPO/.venv) --cuda-home DIR Matching CUDA toolkit (default: CUDA_HOME or /usr/local/cuda) --torch-backend NAME Official uv CUDA backend (default: cu130) Requires Ubuntu, an installed NVIDIA driver/CUDA toolkit, Python venv/development headers, a C++ compiler, and Internet access. Installs the repository lock plus qwen-hybrid and recovery extras, then replaces CPU Torch with its CUDA variant. Blackwell requires Torch and nvcc CUDA >= 12.8. Select one GPU with CUDA_VISIBLE_DEVICES before setup; finishes with GPU kernel forward self-tests. No credentials are required. Use the environment's Python directly afterward; an ordinary uv sync/run can restore the CPU Torch wheel from uv.lock. EOF } while (($#)); do case "$1" in --python|--venv|--cuda-home|--torch-backend) if (($# < 2)); then printf 'Missing value for %s\n' "$1" >&2 exit 2 fi case "$1" in --python) campaign_python="$2" ;; --venv) campaign_venv="$2" ;; --cuda-home) campaign_cuda_home="$2" ;; --torch-backend) campaign_torch_backend="$2" ;; esac shift 2 ;; -h|--help) usage; exit 0 ;; *) printf 'Unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;; esac done if [[ ! "$campaign_torch_backend" =~ ^cu[0-9]+$ ]]; then printf '%s\n' '--torch-backend must name an official CUDA backend, such as cu130.' >&2 exit 2 fi for campaign_command in "$campaign_python" nvidia-smi c++; do if ! command -v "$campaign_command" >/dev/null 2>&1; then printf 'Required command unavailable: %s\n' "$campaign_command" >&2 exit 1 fi done if [[ ! -x "$campaign_cuda_home/bin/nvcc" ]]; then printf 'CUDA compiler missing: %s/bin/nvcc; set --cuda-home.\n' "$campaign_cuda_home" >&2 exit 1 fi "$campaign_python" - <<'PY' import sys if sys.version_info < (3, 11): raise SystemExit("Setup requires Python 3.11+; use Ubuntu 24.04's Python 3.12") PY cd "$campaign_repo" campaign_venv="$(realpath -m "$campaign_venv")" export CUDA_HOME="$campaign_cuda_home" export MAX_JOBS="${MAX_JOBS:-4}" campaign_tools="$(mktemp -d "${TMPDIR:-/tmp}/qwen38-setup.XXXXXXXX")" trap 'rm -rf "$campaign_tools"' EXIT "$campaign_python" -m venv "$campaign_tools" # Keep the resolver version aligned with .github/workflows/ci.yml. "$campaign_tools/bin/python" -m pip install --disable-pip-version-check 'uv==0.12.4' campaign_uv="$campaign_tools/bin/uv" "$campaign_uv" venv --allow-existing --python "$campaign_python" "$campaign_venv" # The portable lock selects CPU Torch. Build only a temporary kernel-free wheel # here, then replace Torch before rebuilding the extension against its CUDA ABI. CAUSAL_CONV1D_SKIP_CUDA_BUILD=TRUE UV_PROJECT_ENVIRONMENT="$campaign_venv" \ "$campaign_uv" sync --locked --extra qwen-hybrid --extra recovery --no-cache campaign_torch_version="$("$campaign_venv/bin/python" -c \ 'import torch; print(torch.__version__.split("+", 1)[0])')" UV_TORCH_BACKEND="$campaign_torch_backend" "$campaign_uv" pip install \ --python "$campaign_venv/bin/python" --reinstall-package torch \ "torch==$campaign_torch_version" campaign_arch="$("$campaign_venv/bin/python" - <<'PY' import os import re import subprocess import torch if torch.version.cuda is None or not torch.cuda.is_available(): raise SystemExit("CUDA Torch cannot use the NVIDIA device; check the driver and CUDA backend") if torch.cuda.device_count() != 1: raise SystemExit("Select exactly one GPU with CUDA_VISIBLE_DEVICES before setup") capability = torch.cuda.get_device_capability(0) runtime = tuple(int(part) for part in torch.version.cuda.split(".")[:2]) compiler = subprocess.check_output([os.path.join(os.environ["CUDA_HOME"], "bin", "nvcc"), "--version"], text=True) match = re.search(r"release\s+(\d+)\.(\d+)", compiler) if match is None or int(match.group(1)) != runtime[0]: raise SystemExit("CUDA_HOME compiler major must match torch.version.cuda " "before building kernels") toolkit = tuple(int(part) for part in match.groups()) if capability[0] >= 10 and (runtime < (12, 8) or toolkit < (12, 8)): raise SystemExit("Blackwell requires Torch built for CUDA >= 12.8 and nvcc >= 12.8; " "select --torch-backend cu128 or newer and a matching --cuda-home") print(f"{capability[0]}.{capability[1]}") PY )" export TORCH_CUDA_ARCH_LIST="$campaign_arch" printf 'Building kernels for detected compute capability %s\n' "$TORCH_CUDA_ARCH_LIST" # Use the lock's hash-verified source archive, rather than a cached wheel that # might have been compiled against CPU Torch or another CUDA/PyTorch ABI. # This also covers devices with no matching wheel, including Blackwell sm_120. campaign_conv_source="$("$campaign_venv/bin/python" - <<'PY' from pathlib import Path import tomllib lock = tomllib.loads(Path("uv.lock").read_text()) package = next(row for row in lock["package"] if row["name"] == "causal-conv1d") source = package["sdist"] print(source["url"] + "#" + source["hash"].replace(":", "=", 1)) PY )" env -u CAUSAL_CONV1D_SKIP_CUDA_BUILD CAUSAL_CONV1D_FORCE_BUILD=TRUE \ "$campaign_uv" pip install --python "$campaign_venv/bin/python" --no-cache \ --no-build-isolation --no-deps --reinstall-package causal-conv1d \ "causal-conv1d @ $campaign_conv_source" "$campaign_uv" pip check --python "$campaign_venv/bin/python" "$campaign_venv/bin/python" - <<'PY' from importlib.metadata import version import platform import torch import torch.nn.functional as F check = "kernel imports" try: import fla # noqa: F401 import causal_conv1d_cuda # noqa: F401 from causal_conv1d import causal_conv1d_fn from fla.ops.gated_delta_rule import chunk_gated_delta_rule with torch.inference_mode(): torch.manual_seed(0) check = "causal-conv1d forward" x = torch.randn(1, 8, 16, device="cuda", dtype=torch.bfloat16) weight = torch.randn(8, 4, device="cuda", dtype=torch.bfloat16) bias = torch.randn(8, device="cuda", dtype=torch.bfloat16) conv = causal_conv1d_fn(x, weight, bias, activation="silu") torch.cuda.synchronize() reference = F.silu(F.conv1d(x.float(), weight.float().unsqueeze(1), bias.float(), padding=3, groups=8)[..., :x.shape[-1]]) if (conv.device.type != "cuda" or conv.shape != x.shape or not torch.isfinite(conv).all() or not torch.allclose(conv.float(), reference, atol=0.05, rtol=0.02)): raise RuntimeError("output failed shape, finite-value or reference parity check") check = "FLA gated-delta-rule forward" q = F.normalize(torch.randn(1, 16, 2, 32, device="cuda", dtype=torch.bfloat16), dim=-1) k = F.normalize(torch.randn_like(q), dim=-1) v = torch.randn_like(q) g = torch.full((1, 16, 2), -0.1, device="cuda", dtype=torch.float32) beta = torch.full((1, 16, 2), 0.5, device="cuda", dtype=torch.bfloat16) output, state = chunk_gated_delta_rule(q, k, v, g, beta, output_final_state=True) torch.cuda.synchronize() if (output.device.type != "cuda" or output.shape != v.shape or not torch.isfinite(output).all() or not torch.count_nonzero(output) or state is None or not torch.isfinite(state).all()): raise RuntimeError("output failed shape, finite-value or nonzero check") except Exception as error: raise SystemExit(f"GPU kernel self-test failed at {check}: {type(error).__name__}: {error}. " "Check Torch/CUDA/driver compatibility and rebuild causal-conv1d for the " "detected compute capability before loading a model.") from error print("Python:", platform.python_version()) for name in ("obliteratus", "torch", "transformers", "accelerate", "safetensors", "flash-linear-attention", "fla-core", "causal-conv1d", "peft"): print(f"{name}: {version(name)}") print("Torch CUDA:", torch.version.cuda) print("C++11 ABI:", torch.compiled_with_cxx11_abi()) print("GPU:", torch.cuda.get_device_name(0)) print("GPU memory bytes:", torch.cuda.get_device_properties(0).total_memory) print("Compute capability:", torch.cuda.get_device_capability(0)) print("FLA and causal-conv1d imports and GPU forward self-tests: OK") PY nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv "$campaign_cuda_home/bin/nvcc" --version printf '\nSetup complete. Run the campaign with %s/bin/python directly.\n' "$campaign_venv"