-
Notifications
You must be signed in to change notification settings - Fork 650
add calc_server #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YYYYimo
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
YYYYimo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add calc_server #66
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import asyncio | ||
| import json | ||
| from pathlib import Path | ||
| from typing import List, Dict, Any | ||
| import pandas as pd | ||
|
|
||
| from agentlightning.server import AgentLightningServer | ||
| from agentlightning.types import NamedResources, PromptTemplate, LLM | ||
| from agentlightning import configure_logger | ||
|
|
||
| VLLM_ENDPOINT = "http://localhost:8000/v1" | ||
| MODEL_NAME = "Qwen/Qwen2-1.5B-Instruct" | ||
|
|
||
| SERVER_HOST = "127.0.0.1" | ||
| SERVER_PORT = 9999 | ||
| SERVER_URL = f"http://{SERVER_HOST}:{SERVER_PORT}" | ||
| # Define the path for the output file where results will be stored. | ||
| OUTPUT_PATH = Path(__file__).parent / "rollouts.jsonl" | ||
|
|
||
| configure_logger() | ||
|
|
||
| async def load_dataset() -> List[Dict[str, Any]]: | ||
| """ | ||
| Loads the dataset for the tasks. | ||
| It first tries to load from .parquet files in the 'data' directory. | ||
| If that fails or the directory doesn't exist, it falls back to a default demo sample. | ||
| """ | ||
| data_dir = Path(__file__).parent / "data" | ||
|
|
||
| if not data_dir.exists(): | ||
| print("Data directory not found. Using default sample.") | ||
| return [{"question": "What is 2 + 2?", "result": "4"}] | ||
|
|
||
| try: | ||
| samples = [] | ||
| for pf in sorted(data_dir.glob("*.parquet")): | ||
| df = pd.read_parquet(pf) | ||
| for _, row in df.iterrows(): | ||
| q = row.get("question") | ||
| r = row.get("result") | ||
| if q is None or r is None: | ||
| continue | ||
| samples.append({"question": str(q), "result": str(r)}) | ||
| if samples: | ||
| print(f"Loaded {len(samples)} samples from parquet files.") | ||
| return samples | ||
| except Exception as e: | ||
| print(f"Failed to load from parquet files: {e}. Using default sample.") | ||
| pass | ||
|
|
||
| return [{"question": "What is 2 + 2?", "result": "4"}] | ||
|
|
||
|
|
||
| async def main(timeout_per_task: int = 30): | ||
| # 1. Prepare the output file: ensure it's clean and empty before the run. | ||
| OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True) | ||
| if OUTPUT_PATH.exists(): | ||
| OUTPUT_PATH.unlink() | ||
| OUTPUT_PATH.touch() | ||
| print(f"Output will be saved to: {OUTPUT_PATH}") | ||
|
|
||
| # 2. Initialize and start the AgentLightning server. | ||
| server = AgentLightningServer(host=SERVER_HOST, port=SERVER_PORT) | ||
| await server.start() | ||
| print(f"[Server] Started and listening at {SERVER_URL}") | ||
|
|
||
| # 3. Define and broadcast shared resources to all workers. | ||
| # This is a core concept: the server dictates the configuration for all connecting workers. | ||
| # Here, we instruct all workers to use our local vLLM instance. | ||
| resources = { | ||
| "main_llm": LLM( | ||
| endpoint=VLLM_ENDPOINT, | ||
| model=MODEL_NAME, | ||
| sampling_parameters={"temperature": 0.0}, # Use 0 for deterministic results in calculation tasks. | ||
| ) | ||
| } | ||
|
|
||
| await server.update_resources(resources) | ||
| print(f"[Server] Broadcasted resources: All workers will use LLM at {VLLM_ENDPOINT}") | ||
|
|
||
| # 4. Load the dataset and queue all samples as tasks. | ||
| samples = await load_dataset() | ||
| print(f"[Server] Loaded {len(samples)} samples.") | ||
| task_ids = [] | ||
| for s in samples: | ||
| tid = await server.queue_task(sample=s, mode="train") | ||
| task_ids.append(tid) | ||
| print(f"[Server] Queued {len(task_ids)} tasks. Waiting for workers...") | ||
|
|
||
| # 5. Poll for and collect the results (rollouts) for each task. | ||
| rollouts = [] | ||
| for tid in task_ids: | ||
| try: | ||
| # This will block until the task is completed by a worker or the timeout is reached. | ||
| rollout = await server.poll_completed_rollout(tid, timeout=timeout_per_task) | ||
| except asyncio.TimeoutError: | ||
| print(f"[Server] Warning: Timed out waiting for task {tid}.") | ||
| rollout = None | ||
| except Exception as e: | ||
| print(f"[Server] Error waiting for task {tid}: {e}") | ||
| rollout = None | ||
|
|
||
| if rollout: | ||
| # The rollout object is a Pydantic model; convert it to a dictionary for JSON serialization. | ||
| rdict = rollout.model_dump() | ||
| rollouts.append(rdict) | ||
| with open(OUTPUT_PATH, "a", encoding="utf-8") as f: | ||
| f.write(json.dumps(rdict, ensure_ascii=False) + "\n") | ||
| print(f"[Server] Received and saved rollout for task {tid}") | ||
| else: | ||
| print(f"[Server] No rollout received for task {tid}") | ||
|
|
||
| # 6. Stop the server gracefully after all tasks have been processed. | ||
| await server.stop() | ||
| print("\n[Server] Stopped.") | ||
| print(f"[Server] Collected {len(rollouts)} rollouts -> {OUTPUT_PATH}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # The n_workers argument here is for demonstration; in the server-client model, | ||
| # the number of workers is determined by how many worker scripts you run. | ||
| asyncio.run(main(timeout_per_task=30)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from vllm.entrypoints.cli.main import main | ||
| from agentlightning.instrumentation.vllm import instrument_vllm | ||
|
|
||
| if __name__ == "__main__": | ||
| instrument_vllm() | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After
proc.join(timeout=5), there's no check if the process actually terminated. If the timeout expires, the process may still be running. Consider adding a force kill if the process doesn't terminate gracefully.