|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""E2E check: RemoteRolloutProcessor reads prompt_token_ids trace payloads. |
| 3 | +
|
| 4 | +This starts a tiny local `/init` server, sends one chat completion through the |
| 5 | +Fireworks tracing gateway with `return_token_ids`, and verifies that |
| 6 | +RemoteRolloutProcessor hydrates `assistant_turn_payloads[*].prompt_token_ids`. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import asyncio |
| 13 | +import logging |
| 14 | +import os |
| 15 | +import sys |
| 16 | +import socket |
| 17 | +import threading |
| 18 | +import time |
| 19 | +from pathlib import Path |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 23 | + |
| 24 | +import uvicorn |
| 25 | +from fastapi import FastAPI |
| 26 | +from openai import OpenAI |
| 27 | + |
| 28 | +from eval_protocol import FireworksTracingHttpHandler, InitRequest, RolloutIdFilter, Status |
| 29 | +from eval_protocol.models import EvaluationRow, Message |
| 30 | +from eval_protocol.pytest.remote_rollout_processor import RemoteRolloutProcessor |
| 31 | +from eval_protocol.pytest.types import RolloutProcessorConfig |
| 32 | + |
| 33 | +logger = logging.getLogger("remote_rollout_prompt_token_ids") |
| 34 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") |
| 35 | + |
| 36 | + |
| 37 | +def _free_port() -> int: |
| 38 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 39 | + sock.bind(("127.0.0.1", 0)) |
| 40 | + return int(sock.getsockname()[1]) |
| 41 | + |
| 42 | + |
| 43 | +def _message_to_dict(message: Message | dict[str, Any]) -> dict[str, Any]: |
| 44 | + if isinstance(message, Message): |
| 45 | + return message.dump_mdoel_for_chat_completion_request() |
| 46 | + return {k: v for k, v in dict(message).items() if v is not None} |
| 47 | + |
| 48 | + |
| 49 | +def _make_app(gateway_url: str) -> FastAPI: |
| 50 | + app = FastAPI() |
| 51 | + app_logger = logging.getLogger(f"{__name__}.server") |
| 52 | + app_logger.setLevel(logging.INFO) |
| 53 | + |
| 54 | + @app.get("/") |
| 55 | + def health() -> dict[str, str]: |
| 56 | + return {"status": "ok"} |
| 57 | + |
| 58 | + @app.post("/init") |
| 59 | + def init(req: InitRequest) -> dict[str, str]: |
| 60 | + rollout_logger = logging.getLogger(f"{__name__}.{req.metadata.rollout_id}") |
| 61 | + rollout_logger.addFilter(RolloutIdFilter(req.metadata.rollout_id)) |
| 62 | + if not any(isinstance(handler, FireworksTracingHttpHandler) for handler in rollout_logger.handlers): |
| 63 | + rollout_logger.addHandler(FireworksTracingHttpHandler(gateway_base_url=gateway_url)) |
| 64 | + rollout_logger.setLevel(logging.INFO) |
| 65 | + |
| 66 | + def _worker() -> None: |
| 67 | + try: |
| 68 | + conversation = [_message_to_dict(message) for message in (req.messages or [])] |
| 69 | + params = dict(req.completion_params or {}) |
| 70 | + params.pop("base_url", None) |
| 71 | + params["extra_body"] = { |
| 72 | + **dict(params.get("extra_body") or {}), |
| 73 | + "return_token_ids": True, |
| 74 | + } |
| 75 | + params.setdefault("temperature", 0) |
| 76 | + params.setdefault("max_tokens", 8) |
| 77 | + |
| 78 | + if not req.model_base_url: |
| 79 | + raise ValueError("model_base_url is required") |
| 80 | + if not params.get("model"): |
| 81 | + raise ValueError("completion_params.model is required") |
| 82 | + |
| 83 | + client = OpenAI(base_url=req.model_base_url, api_key=req.api_key) |
| 84 | + response = client.chat.completions.create(messages=conversation, **params) |
| 85 | + content = response.choices[0].message.content or "" |
| 86 | + logger.info("remote server generated content=%r", content) |
| 87 | + |
| 88 | + rollout_logger.info( |
| 89 | + "rollout %s finished", |
| 90 | + req.metadata.rollout_id, |
| 91 | + extra={"status": Status.rollout_finished()}, |
| 92 | + ) |
| 93 | + except Exception as exc: |
| 94 | + rollout_logger.exception( |
| 95 | + "rollout %s failed", |
| 96 | + req.metadata.rollout_id, |
| 97 | + extra={"status": Status.rollout_unknown_error(str(exc))}, |
| 98 | + ) |
| 99 | + |
| 100 | + threading.Thread(target=_worker, daemon=True).start() |
| 101 | + return {"status": "started"} |
| 102 | + |
| 103 | + return app |
| 104 | + |
| 105 | + |
| 106 | +def _wait_ready(url: str, timeout_seconds: float = 30.0) -> None: |
| 107 | + import requests |
| 108 | + |
| 109 | + deadline = time.time() + timeout_seconds |
| 110 | + while time.time() < deadline: |
| 111 | + try: |
| 112 | + resp = requests.get(url, timeout=2) |
| 113 | + if resp.status_code == 200: |
| 114 | + return |
| 115 | + except Exception: |
| 116 | + pass |
| 117 | + time.sleep(0.2) |
| 118 | + raise TimeoutError(f"server not ready: {url}") |
| 119 | + |
| 120 | + |
| 121 | +async def _run(args: argparse.Namespace) -> None: |
| 122 | + api_key = args.api_key or os.getenv("FIREWORKS_DEV_API_KEY") or os.getenv("FIREWORKS_API_KEY") |
| 123 | + if not api_key: |
| 124 | + raise ValueError("Set FIREWORKS_DEV_API_KEY or FIREWORKS_API_KEY") |
| 125 | + |
| 126 | + # FireworksTracingHttpHandler reads FIREWORKS_API_KEY. |
| 127 | + os.environ["FIREWORKS_API_KEY"] = api_key |
| 128 | + os.environ["EP_REMOTE_API_KEY"] = api_key |
| 129 | + |
| 130 | + port = args.port or _free_port() |
| 131 | + remote_base_url = f"http://127.0.0.1:{port}" |
| 132 | + app = _make_app(args.gateway_url) |
| 133 | + config = uvicorn.Config(app, host="127.0.0.1", port=port, log_level="warning") |
| 134 | + server = uvicorn.Server(config) |
| 135 | + thread = threading.Thread(target=server.run, daemon=True) |
| 136 | + thread.start() |
| 137 | + _wait_ready(f"{remote_base_url}/") |
| 138 | + |
| 139 | + rollout_id = f"rrp-prompt-ids-{int(time.time())}" |
| 140 | + row = EvaluationRow( |
| 141 | + messages=[Message(role="user", content="Reply with exactly: ok")], |
| 142 | + ) |
| 143 | + row.input_metadata.row_id = "row-0" |
| 144 | + row.input_metadata.completion_params = { |
| 145 | + "model": args.model, |
| 146 | + "base_url": args.api_base_url, |
| 147 | + "temperature": 0, |
| 148 | + "max_tokens": 8, |
| 149 | + } |
| 150 | + row.execution_metadata.rollout_id = rollout_id |
| 151 | + row.execution_metadata.invocation_id = "inv-0" |
| 152 | + row.execution_metadata.experiment_id = "fir2-1747-rrp-e2e" |
| 153 | + row.execution_metadata.run_id = "run-0" |
| 154 | + |
| 155 | + processor = RemoteRolloutProcessor( |
| 156 | + remote_base_url=remote_base_url, |
| 157 | + model_base_url=args.gateway_url, |
| 158 | + include_payloads=True, |
| 159 | + timeout_seconds=args.timeout_seconds, |
| 160 | + poll_interval=args.poll_interval, |
| 161 | + ) |
| 162 | + try: |
| 163 | + task = processor( |
| 164 | + [row], |
| 165 | + RolloutProcessorConfig( |
| 166 | + completion_params=row.input_metadata.completion_params, |
| 167 | + mcp_config_path="", |
| 168 | + semaphore=asyncio.Semaphore(1), |
| 169 | + steps=1, |
| 170 | + ), |
| 171 | + )[0] |
| 172 | + completed = await task |
| 173 | + finally: |
| 174 | + await processor.acleanup() |
| 175 | + server.should_exit = True |
| 176 | + thread.join(timeout=5) |
| 177 | + |
| 178 | + extra = completed.execution_metadata.extra or {} |
| 179 | + turn_payloads = extra.get("assistant_turn_payloads") or [] |
| 180 | + prompt_ids = None |
| 181 | + if turn_payloads: |
| 182 | + prompt_ids = turn_payloads[0].get("prompt_token_ids") |
| 183 | + if prompt_ids is None: |
| 184 | + prompt_ids = extra.get("prompt_token_ids") |
| 185 | + |
| 186 | + print(f"rollout_id={rollout_id}") |
| 187 | + print(f"messages={len(completed.messages)}") |
| 188 | + print(f"assistant_turn_payloads={turn_payloads}") |
| 189 | + print(f"prompt_token_ids_len={len(prompt_ids) if isinstance(prompt_ids, list) else None}") |
| 190 | + print(f"prompt_token_ids_head={prompt_ids[:8] if isinstance(prompt_ids, list) else None}") |
| 191 | + |
| 192 | + if not isinstance(prompt_ids, list) or not prompt_ids: |
| 193 | + raise AssertionError("RemoteRolloutProcessor did not hydrate prompt_token_ids") |
| 194 | + |
| 195 | + |
| 196 | +def main() -> None: |
| 197 | + parser = argparse.ArgumentParser(description=__doc__) |
| 198 | + parser.add_argument("--gateway-url", default=os.getenv("EP_MODEL_BASE_URL", "https://litellm-gateway-dev-j4kzagdteq-uc.a.run.app")) |
| 199 | + parser.add_argument("--api-base-url", default=os.getenv("FIREWORKS_API_BASE_URL", "https://dev.api.fireworks.ai/inference/v1")) |
| 200 | + parser.add_argument("--model", default=os.getenv("TRACING_E2E_MODEL", "accounts/pyroworks-dev/deployments/malaysia2-intended-butterfly")) |
| 201 | + parser.add_argument("--api-key", default=None) |
| 202 | + parser.add_argument("--port", type=int, default=0) |
| 203 | + parser.add_argument("--timeout-seconds", type=float, default=180.0) |
| 204 | + parser.add_argument("--poll-interval", type=float, default=2.0) |
| 205 | + asyncio.run(_run(parser.parse_args())) |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == "__main__": |
| 209 | + main() |
0 commit comments