File size: 4,455 Bytes
6361ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Run the existing experiment on Modal; no local GPU or model upload needed."""
import json
import os
from pathlib import Path
import re

import modal

ROOT = Path(__file__).resolve().parent
APP_ROOT = Path('/opt/qrl')
STATE = Path('/qrl-state')
LLAMA_REVISION = '620d2225cc436ab0b001e3200e590e3b286ef9ab'
app = modal.App('qwen-reasoning-lab')
volume = modal.Volume.from_name('qwen-reasoning-lab', create_if_missing=True)

image = (
    modal.Image.from_registry('nvidia/cuda:12.8.1-devel-ubuntu22.04', add_python='3.12')
    .entrypoint([])
    .apt_install('git', 'cmake', 'build-essential', 'libcurl4-openssl-dev')
    .run_commands(
        f'git clone https://github.com/ggml-org/llama.cpp.git /opt/llama.cpp && '
        f'cd /opt/llama.cpp && git checkout {LLAMA_REVISION}',
        'cmake -S /opt/llama.cpp -B /opt/llama.cpp/build '
        '-DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=86 -DLLAMA_CURL=ON '
        '-DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF',
        'cmake --build /opt/llama.cpp/build --target llama-server -j 4',
    )
    .pip_install('torch==2.10.0', index_url='https://download.pytorch.org/whl/cu128')
    .pip_install_from_requirements(str(ROOT / 'requirements.txt'))
    .env({'QRL_PORTABLE_CUDA': '1', 'TOKENIZERS_PARALLELISM': 'false',
          'OMP_NUM_THREADS': '4', 'HF_HUB_DISABLE_XET': '1'})
)
# Explicit allowlist: never send local credentials, models, datasets or old runs.
for source in sorted(ROOT.iterdir()):
    if source.is_file() and (source.suffix == '.py' or
                            source.name == 'manifest.json' or
                            re.fullmatch(r'config(?:_[a-z]+)?\.json', source.name)):
        image = image.add_local_file(str(source), str(APP_ROOT / source.name))


def validate(config, run, mode):
    if not re.fullmatch(r'config(?:_[a-z]+)?\.json', config):
        raise ValueError('Use a checked-in config*.json filename')
    if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]{0,79}', run):
        raise ValueError('Run must be a simple directory name (letters, numbers, _ and -)')
    if mode not in ('smoke', 'splits', 'experiment'):
        raise ValueError('Mode must be smoke, splits or experiment')


@app.function(image=image, gpu='A10G', cpu=4, memory=16384,
              volumes={str(STATE): volume}, timeout=24 * 60 * 60,
              max_containers=1)
def execute(config: str, run: str, mode: str):
    import fcntl
    import shutil
    import subprocess
    import sys

    validate(config, run, mode)
    volume.reload()
    # Protect shared downloads and the manifest as well as individual runs.
    with (STATE / '.runner.lock').open('w') as lock:
        fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
        for name in ('models', 'data', '.cache', 'runs'):
            (STATE / name).mkdir(exist_ok=True)
            target = APP_ROOT / name
            if not target.is_symlink():
                target.symlink_to(STATE / name, target_is_directory=True)
        manifest = STATE / 'manifest.json'
        if not manifest.exists():
            shutil.copy2(APP_ROOT / 'manifest.json', manifest)
        (APP_ROOT / 'manifest.json').unlink()
        (APP_ROOT / 'manifest.json').symlink_to(manifest)
        settings = json.loads((APP_ROOT / config).read_text())
        settings.update(llama_server='/opt/llama.cpp/build/bin/llama-server',
                        experimental_attention=False, cpu_threads=4)
        remote_config = APP_ROOT / 'config_modal.json'
        remote_config.write_text(json.dumps(settings, indent=2) + '\n')
        env = dict(os.environ, QRL_CONFIG=remote_config.name, QRL_RUN=run)
        try:
            subprocess.run([sys.executable, '-u', 'prepare.py', '--config', remote_config.name],
                           cwd=APP_ROOT, env=env, check=True)
            volume.commit()
            command = ([sys.executable, '-u', 'smoke.py'] if mode == 'smoke' else
                       [sys.executable, '-u', 'experiment.py'] +
                       (['--prepare-splits-only'] if mode == 'splits' else []))
            subprocess.run(command, cwd=APP_ROOT, env=env, check=True)
        finally:
            volume.commit()
    return f'Saved to Modal volume qwen-reasoning-lab: runs/{run}'


@app.local_entrypoint()
def main(config: str = 'config_generalized.json', run: str = 'modal-generalized',
         mode: str = 'experiment'):
    validate(config, run, mode)
    print(execute.spawn(config, run, mode).get())