File size: 4,618 Bytes
6c5f29f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import annotations

import json
import os
import subprocess
from pathlib import Path

import modal


ROOT = Path(__file__).resolve().parent.parent
REMOTE_ROOT = "/root/project"
REMOTE_RESULTS = "/results"
REMOTE_HF_CACHE = "/root/.cache/huggingface"
IGNORE = [
    ".git",
    ".git-archives",
    "__pycache__",
    "dreamerv3/.venv",
    "dreamerv3/pilot_logs",
    "dreamerv3/smoke_logs",
    "dreamerv3/cw_modal_runs",
    "dreamerv3/paper_runs_smoke",
    "results*",
    "seq_results",
]

app = modal.App("llm-memory-longmemeval")
results_volume = modal.Volume.from_name("llm-memory-longmemeval-results", create_if_missing=True)
hf_cache_volume = modal.Volume.from_name("llm-memory-longmemeval-hf-cache", create_if_missing=True)

image = (
    modal.Image.debian_slim(python_version="3.11")
    .apt_install("git")
    .pip_install(
        "torch>=2.4.0",
        "transformers>=4.51.0",
        "accelerate>=1.6.0",
        "scikit-learn>=1.5.0",
        "matplotlib>=3.9.0",
        "sentencepiece>=0.2.0",
        "safetensors>=0.4.5",
        "huggingface_hub[hf_transfer]>=0.30.2",
    )
    .env(
        {
            "PYTHONUNBUFFERED": "1",
            "HF_HUB_ENABLE_HF_TRANSFER": "1",
            "TOKENIZERS_PARALLELISM": "false",
        }
    )
    .add_local_dir(ROOT, REMOTE_ROOT, copy=True, ignore=IGNORE)
)


def _stream_subprocess(command: list[str], cwd: str, env: dict[str, str], logfile: str) -> None:
    Path(logfile).parent.mkdir(parents=True, exist_ok=True)
    with open(logfile, "w", encoding="utf-8") as stream:
        process = subprocess.Popen(
            command,
            cwd=cwd,
            env=env,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
            bufsize=1,
        )
        assert process.stdout is not None
        for line in process.stdout:
            print(line, end="")
            stream.write(line)
        return_code = process.wait()
    if return_code:
        raise subprocess.CalledProcessError(return_code, command)


@app.function(
    image=image,
    gpu="L4",
    cpu=8,
    memory=32768,
    timeout=60 * 60 * 4,
    volumes={
        REMOTE_RESULTS: results_volume,
        REMOTE_HF_CACHE: hf_cache_volume,
    },
)
def run_validation(
    budget_frac: float = 0.20,
    run_generation: bool = True,
    generation_per_type: int = 20,
    reader_model: str = "Qwen/Qwen2.5-1.5B-Instruct",
    prompt_word_budget: int = 1600,
    max_new_tokens: int = 48,
) -> dict:
    env = os.environ.copy()
    env["PYTHONPATH"] = REMOTE_ROOT + os.pathsep + env.get("PYTHONPATH", "")
    env["HF_HOME"] = REMOTE_HF_CACHE
    env["HF_HUB_CACHE"] = str(Path(REMOTE_HF_CACHE) / "hub")
    env["TRANSFORMERS_CACHE"] = str(Path(REMOTE_HF_CACHE) / "hub")

    run_name = f"longmemeval_budget_{str(budget_frac).replace('.', 'p')}"
    if run_generation:
        run_name += "_gen"
    output_dir = f"{REMOTE_RESULTS}/{run_name}"
    logfile = f"{output_dir}/stdout.log"
    command = [
        "python",
        "llm_memory_validation/bsc_longmemeval.py",
        "--output-dir",
        output_dir,
        "--budget-frac",
        str(budget_frac),
        "--topk",
        "5",
        "--generation-per-type",
        str(generation_per_type),
        "--prompt-word-budget",
        str(prompt_word_budget),
        "--max-new-tokens",
        str(max_new_tokens),
        "--reader-model",
        reader_model,
    ]
    if run_generation:
        command.append("--run-generation")

    _stream_subprocess(command, cwd=REMOTE_ROOT, env=env, logfile=logfile)
    results_volume.commit()
    hf_cache_volume.commit()

    summary_path = Path(output_dir) / "summary.json"
    report_path = Path(output_dir) / "REPORT.md"
    payload = {
        "run_name": run_name,
        "output_dir": output_dir,
        "summary": json.loads(summary_path.read_text(encoding="utf-8")),
        "report_md": report_path.read_text(encoding="utf-8"),
        "stdout_log": logfile,
    }
    return payload


@app.local_entrypoint()
def main(
    budget_frac: float = 0.20,
    run_generation: bool = True,
    generation_per_type: int = 20,
    reader_model: str = "Qwen/Qwen2.5-1.5B-Instruct",
    prompt_word_budget: int = 1600,
    max_new_tokens: int = 48,
) -> None:
    payload = run_validation.remote(
        budget_frac=budget_frac,
        run_generation=run_generation,
        generation_per_type=generation_per_type,
        reader_model=reader_model,
        prompt_word_budget=prompt_word_budget,
        max_new_tokens=max_new_tokens,
    )
    print(json.dumps(payload, indent=2))