|
| 1 | +"""Cross-model text pipeline: 2-agent chain where Researcher (model A) passes text to Solver (model B). |
| 2 | +
|
| 3 | +This is the text baseline for cross-model comparison. Both agents communicate via |
| 4 | +text (like pipeline_text.py), but each agent runs on a different model. This lets |
| 5 | +us measure whether rosetta projection adds value over simply piping text between |
| 6 | +different models. |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | +from typing import Any, Dict, List |
| 11 | + |
| 12 | +from benchmarks.shared.generation import generate_text, render_prompt, tokenize_prompt |
| 13 | +from benchmarks.shared.metrics import gpu_memory_tracker |
| 14 | +from .agents import AGENTS, build_text_prompt |
| 15 | +from .evaluate import extract_gold, extract_gsm8k_answer, check_correct |
| 16 | + |
| 17 | + |
| 18 | +def run_text_cross_model_pipeline( |
| 19 | + model_a: Any, |
| 20 | + tokenizer_a: Any, |
| 21 | + model_b: Any, |
| 22 | + tokenizer_b: Any, |
| 23 | + device: str, |
| 24 | + question: str, |
| 25 | + gold_solution: str, |
| 26 | + max_new_tokens: int = 512, |
| 27 | + temperature: float = 0.7, |
| 28 | + top_p: float = 0.95, |
| 29 | + verbose: bool = False, |
| 30 | +) -> Dict: |
| 31 | + """Run the 2-agent cross-model text pipeline on a single GSM8K problem. |
| 32 | +
|
| 33 | + Researcher (model A) generates text analysis. |
| 34 | + Solver (model B) receives that text in its prompt and generates the answer. |
| 35 | + """ |
| 36 | + with gpu_memory_tracker(device) as mem: |
| 37 | + t0 = time.perf_counter() |
| 38 | + agent_traces: List[Dict] = [] |
| 39 | + total_prompt_tokens = 0 |
| 40 | + total_output_tokens = 0 |
| 41 | + total_context_tokens = 0 |
| 42 | + |
| 43 | + researcher = AGENTS[0] |
| 44 | + solver = AGENTS[1] |
| 45 | + |
| 46 | + # --- Agent 1: Researcher on model A --- |
| 47 | + agent_t0 = time.perf_counter() |
| 48 | + messages = build_text_prompt(researcher.role, question) |
| 49 | + prompt_text = render_prompt(tokenizer_a, messages) |
| 50 | + input_ids, attention_mask = tokenize_prompt(tokenizer_a, prompt_text, device) |
| 51 | + prompt_tokens = int(input_ids.shape[-1]) |
| 52 | + total_prompt_tokens += prompt_tokens |
| 53 | + |
| 54 | + researcher_text, _ = generate_text( |
| 55 | + model_a, tokenizer_a, input_ids, attention_mask, device, |
| 56 | + max_new_tokens=max_new_tokens, |
| 57 | + temperature=temperature, |
| 58 | + top_p=top_p, |
| 59 | + ) |
| 60 | + |
| 61 | + output_encoded = tokenizer_a(researcher_text, add_special_tokens=False) |
| 62 | + output_tokens = len(output_encoded["input_ids"]) |
| 63 | + total_output_tokens += output_tokens |
| 64 | + agent_time_ms = (time.perf_counter() - agent_t0) * 1000 |
| 65 | + |
| 66 | + agent_traces.append({ |
| 67 | + "name": researcher.name, |
| 68 | + "role": researcher.role, |
| 69 | + "model": "model_a", |
| 70 | + "prompt_tokens": prompt_tokens, |
| 71 | + "output_tokens": output_tokens, |
| 72 | + "context_tokens": 0, |
| 73 | + "agent_time_ms": agent_time_ms, |
| 74 | + "output": researcher_text, |
| 75 | + }) |
| 76 | + |
| 77 | + if verbose: |
| 78 | + print(f" [{researcher.name} (A)] output ({len(researcher_text)} chars): " |
| 79 | + f"{researcher_text[:200]}...") |
| 80 | + |
| 81 | + # --- Agent 2: Solver on model B --- |
| 82 | + agent_t0 = time.perf_counter() |
| 83 | + |
| 84 | + # Count context tokens — Researcher's text re-tokenized by model B's tokenizer |
| 85 | + context_encoded = tokenizer_b(researcher_text, add_special_tokens=False) |
| 86 | + context_token_count = len(context_encoded["input_ids"]) |
| 87 | + total_context_tokens += context_token_count |
| 88 | + |
| 89 | + messages = build_text_prompt(solver.role, question, researcher_text) |
| 90 | + prompt_text = render_prompt(tokenizer_b, messages) |
| 91 | + input_ids, attention_mask = tokenize_prompt(tokenizer_b, prompt_text, device) |
| 92 | + prompt_tokens = int(input_ids.shape[-1]) |
| 93 | + total_prompt_tokens += prompt_tokens |
| 94 | + |
| 95 | + solver_text, _ = generate_text( |
| 96 | + model_b, tokenizer_b, input_ids, attention_mask, device, |
| 97 | + max_new_tokens=max_new_tokens, |
| 98 | + temperature=temperature, |
| 99 | + top_p=top_p, |
| 100 | + ) |
| 101 | + |
| 102 | + output_encoded = tokenizer_b(solver_text, add_special_tokens=False) |
| 103 | + output_tokens = len(output_encoded["input_ids"]) |
| 104 | + total_output_tokens += output_tokens |
| 105 | + agent_time_ms = (time.perf_counter() - agent_t0) * 1000 |
| 106 | + |
| 107 | + agent_traces.append({ |
| 108 | + "name": solver.name, |
| 109 | + "role": solver.role, |
| 110 | + "model": "model_b", |
| 111 | + "prompt_tokens": prompt_tokens, |
| 112 | + "output_tokens": output_tokens, |
| 113 | + "context_tokens": context_token_count, |
| 114 | + "agent_time_ms": agent_time_ms, |
| 115 | + "output": solver_text, |
| 116 | + }) |
| 117 | + |
| 118 | + if verbose: |
| 119 | + print(f" [{solver.name} (B)] output ({len(solver_text)} chars): " |
| 120 | + f"{solver_text[:200]}...") |
| 121 | + |
| 122 | + wall_time = time.perf_counter() - t0 |
| 123 | + |
| 124 | + total_tokens = total_prompt_tokens + total_output_tokens |
| 125 | + tokens_per_sec = total_tokens / wall_time if wall_time > 0 else 0 |
| 126 | + |
| 127 | + gold = extract_gold(gold_solution) |
| 128 | + prediction = extract_gsm8k_answer(agent_traces[-1]["output"]) |
| 129 | + correct = check_correct(prediction, gold) |
| 130 | + |
| 131 | + return { |
| 132 | + "question": question, |
| 133 | + "gold": gold, |
| 134 | + "prediction": prediction, |
| 135 | + "raw_output": agent_traces[-1]["output"], |
| 136 | + "correct": correct, |
| 137 | + "wall_time": wall_time, |
| 138 | + "total_prompt_tokens": total_prompt_tokens, |
| 139 | + "total_output_tokens": total_output_tokens, |
| 140 | + "total_tokens": total_tokens, |
| 141 | + "total_context_tokens": total_context_tokens, |
| 142 | + "tokens_per_sec": tokens_per_sec, |
| 143 | + "peak_memory_mb": mem["peak_memory_mb"], |
| 144 | + "agents": agent_traces, |
| 145 | + "mode": "text_cross_model", |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +def run_text_cross_model_benchmark( |
| 150 | + model_a: Any, |
| 151 | + tokenizer_a: Any, |
| 152 | + model_b: Any, |
| 153 | + tokenizer_b: Any, |
| 154 | + device: str, |
| 155 | + dataset: List[Dict], |
| 156 | + max_new_tokens: int = 512, |
| 157 | + temperature: float = 0.7, |
| 158 | + top_p: float = 0.95, |
| 159 | + verbose: bool = False, |
| 160 | +) -> List[Dict]: |
| 161 | + """Run cross-model text pipeline on a list of GSM8K samples.""" |
| 162 | + results = [] |
| 163 | + for i, sample in enumerate(dataset): |
| 164 | + if verbose: |
| 165 | + print(f"\n[TextCrossModel] Sample {i + 1}/{len(dataset)}: " |
| 166 | + f"{sample['question'][:80]}...") |
| 167 | + |
| 168 | + result = run_text_cross_model_pipeline( |
| 169 | + model_a, tokenizer_a, model_b, tokenizer_b, device, |
| 170 | + question=sample["question"], |
| 171 | + gold_solution=sample["answer"], |
| 172 | + max_new_tokens=max_new_tokens, |
| 173 | + temperature=temperature, |
| 174 | + top_p=top_p, |
| 175 | + verbose=verbose, |
| 176 | + ) |
| 177 | + results.append(result) |
| 178 | + |
| 179 | + if verbose: |
| 180 | + status = "CORRECT" if result["correct"] else "WRONG" |
| 181 | + print(f" => {status} (pred={result['prediction']}, gold={result['gold']}, " |
| 182 | + f"time={result['wall_time']:.1f}s)") |
| 183 | + else: |
| 184 | + correct = sum(1 for r in results if r["correct"]) |
| 185 | + print(f" [TextCrossModel] {i + 1}/{len(dataset)} " |
| 186 | + f"({correct}/{i + 1} correct, {result['wall_time']:.1f}s)", |
| 187 | + flush=True) |
| 188 | + |
| 189 | + return results |
0 commit comments