-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcross_process_demo.py
More file actions
219 lines (179 loc) · 7.99 KB
/
Copy pathcross_process_demo.py
File metadata and controls
219 lines (179 loc) · 7.99 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
"""Cross-process AVP demo: two separate processes communicate via HTTP.
Demonstrates AVP latent transfer between independent processes, each running
their own model instance. The client (Researcher agent) produces latent
context via think(), serializes it to AVP binary format, and sends it over
HTTP. The server (Solver agent) deserializes the context and generates an
answer using its own model instance.
Usage:
# Terminal 1 — start the solver server
python examples/cross_process_demo.py server --model Qwen/Qwen2.5-1.5B-Instruct
# Terminal 2 — run the researcher client
python examples/cross_process_demo.py client --model Qwen/Qwen2.5-1.5B-Instruct \
--question "What is 15% of 240?"
"""
import argparse
import sys
import os
# Ensure avp-python root is on sys.path
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
_PROJECT_ROOT = os.path.dirname(_SCRIPT_DIR)
if _PROJECT_ROOT not in sys.path:
sys.path.insert(0, _PROJECT_ROOT)
def run_server(model_name: str, device: str, port: int, latent_steps: int):
"""Start a FastAPI server that receives AVP context and generates answers."""
import json
import torch
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from avp import HuggingFaceConnector
from avp.context import AVPContext
print(f"Loading model: {model_name} on {device}...")
connector = HuggingFaceConnector.from_pretrained(model_name, device=device)
identity = connector.get_model_identity()
print(f"Model loaded. Identity: {identity.model_family}, "
f"hidden_dim={identity.hidden_dim}, layers={identity.num_layers}")
print(f"Model hash: {connector._model_hash[:16]}...")
app = FastAPI(title="AVP Cross-Process Demo — Solver Agent")
@app.get("/health")
async def health():
return {"status": "ok", "model": model_name, "model_hash": connector._model_hash}
@app.post("/avp/solve")
async def solve(request: Request):
"""Receive AVP context bytes, generate answer, return it."""
body = await request.body()
question = request.headers.get("X-Question", "")
print(f"\n--- Received AVP context ({len(body):,} bytes) ---")
print(f"Question: {question}")
# Deserialize AVP context
context = AVPContext.from_bytes(body, device=device)
print(f"Context: seq_len={context.seq_len}, steps={context.num_steps}, "
f"model_family={context.model_family}")
# Generate answer using the received context
prompt = (
f"You received analysis from a Researcher agent. "
f"Using their work, solve this problem.\n\n"
f"Question: {question}\n\n"
f"Compute the final answer and put it inside \\boxed{{}}."
)
answer = connector.generate(prompt, context=context)
print(f"Generated answer ({len(answer)} chars): {answer[:200]}...")
return JSONResponse({
"answer": answer,
"context_bytes": len(body),
"context_seq_len": context.seq_len,
})
print(f"\nStarting server on port {port}...")
print(f"Waiting for AVP context from client...\n")
uvicorn.run(app, host="0.0.0.0", port=port, log_level="warning")
def run_client(model_name: str, device: str, port: int, question: str, latent_steps: int):
"""Run think(), serialize context, POST to server, print answer."""
import time
import httpx
from avp import HuggingFaceConnector
print(f"Loading model: {model_name} on {device}...")
connector = HuggingFaceConnector.from_pretrained(model_name, device=device)
identity = connector.get_model_identity()
print(f"Model loaded. Hash: {connector._model_hash[:16]}...")
# Step 1: Think (produce latent context)
print(f"\n--- Researcher Agent: thinking ({latent_steps} steps) ---")
print(f"Question: {question}")
t0 = time.perf_counter()
research_prompt = (
f"You are a Researcher Agent. Analyze this math problem. "
f"Identify the key quantities, relationships, and approach.\n\n"
f"Question: {question}"
)
context = connector.think(research_prompt, steps=latent_steps)
think_time = time.perf_counter() - t0
print(f"Think complete: seq_len={context.seq_len}, time={think_time:.2f}s")
# Step 2: Serialize to AVP binary
t0 = time.perf_counter()
avp_bytes = context.to_bytes(
session_id="cross-process-demo",
source_agent_id="researcher",
target_agent_id="solver",
model_id=model_name,
)
serialize_time = time.perf_counter() - t0
print(f"Serialized: {len(avp_bytes):,} bytes in {serialize_time*1000:.1f}ms")
# Step 3: POST to server
server_url = f"http://localhost:{port}/avp/solve"
print(f"\n--- Sending to server at {server_url} ---")
t0 = time.perf_counter()
with httpx.Client(timeout=120.0) as client:
resp = client.post(
server_url,
content=avp_bytes,
headers={
"Content-Type": "application/octet-stream",
"X-Question": question,
},
)
roundtrip_time = time.perf_counter() - t0
if resp.status_code != 200:
print(f"Error: {resp.status_code} {resp.text}")
sys.exit(1)
result = resp.json()
# Step 4: Print results
print(f"\n{'=' * 60}")
print("CROSS-PROCESS AVP DEMO RESULTS")
print(f"{'=' * 60}")
print(f"Question: {question}")
print(f"Answer: {result['answer']}")
print(f"\nMetrics:")
print(f" Think time: {think_time:.2f}s ({latent_steps} latent steps)")
print(f" Serialize time: {serialize_time*1000:.1f}ms")
print(f" Wire size: {result['context_bytes']:,} bytes")
print(f" Context seq_len: {result['context_seq_len']}")
print(f" Server roundtrip: {roundtrip_time:.2f}s (includes generation)")
print(f"\nProcess isolation:")
print(f" - Client PID: {os.getpid()}")
print(f" - Separate model instances in client and server")
print(f" - Transport: HTTP + AVP binary format")
print(f" - Context serialized/deserialized across process boundary")
print(f"{'=' * 60}")
def auto_device() -> str:
"""Auto-detect best available device."""
import torch
if torch.cuda.is_available():
return "cuda"
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
return "cpu"
def main():
parser = argparse.ArgumentParser(
description="Cross-process AVP demo: server + client"
)
subparsers = parser.add_subparsers(dest="command", required=True)
# Server subcommand
server_parser = subparsers.add_parser("server", help="Start the solver server")
server_parser.add_argument(
"--model", type=str, default="Qwen/Qwen2.5-1.5B-Instruct",
help="Model to load (default: Qwen/Qwen2.5-1.5B-Instruct)",
)
server_parser.add_argument("--device", type=str, default=None)
server_parser.add_argument("--port", type=int, default=9200)
server_parser.add_argument("--latent_steps", type=int, default=20)
# Client subcommand
client_parser = subparsers.add_parser("client", help="Run the researcher client")
client_parser.add_argument(
"--model", type=str, default="Qwen/Qwen2.5-1.5B-Instruct",
help="Model to load (default: Qwen/Qwen2.5-1.5B-Instruct)",
)
client_parser.add_argument("--device", type=str, default=None)
client_parser.add_argument("--port", type=int, default=9200)
client_parser.add_argument(
"--question", type=str, default="What is 15% of 240?",
help="Math question to solve",
)
client_parser.add_argument("--latent_steps", type=int, default=20)
args = parser.parse_args()
device = args.device or auto_device()
if args.command == "server":
run_server(args.model, device, args.port, args.latent_steps)
elif args.command == "client":
run_client(args.model, device, args.port, args.question, args.latent_steps)
if __name__ == "__main__":
main()