-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix tune-visual multi gpu finetuning and provide http server impl #257
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,96 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| GR00T HTTP Server Module | ||
|
|
||
| This module provides HTTP server functionality for GR00T model inference. | ||
| It exposes a REST API for easy integration with web applications and other services. | ||
|
|
||
| Dependencies: | ||
| => Server: `pip install uvicorn fastapi json-numpy` | ||
| => Client: `pip install requests json-numpy` | ||
| """ | ||
|
|
||
| import json | ||
| import logging | ||
| import traceback | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| import json_numpy | ||
| import uvicorn | ||
| from fastapi import FastAPI, HTTPException | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| from gr00t.model.policy import Gr00tPolicy | ||
|
|
||
| # Patch json to handle numpy arrays | ||
| json_numpy.patch() | ||
|
|
||
|
|
||
| class HTTPInferenceServer: | ||
| def __init__( | ||
| self, policy: Gr00tPolicy, port: int, host: str = "0.0.0.0", api_token: Optional[str] = None | ||
| ): | ||
| """ | ||
| A simple HTTP server for GR00T models; exposes `/act` to predict an action for a given observation. | ||
| => Takes in observation dict with numpy arrays | ||
| => Returns action dict with numpy arrays | ||
| """ | ||
| self.policy = policy | ||
| self.port = port | ||
| self.host = host | ||
| self.api_token = api_token | ||
| self.app = FastAPI(title="GR00T Inference Server", version="1.0.0") | ||
|
|
||
| # Register endpoints | ||
| self.app.post("/act")(self.predict_action) | ||
| self.app.get("/health")(self.health_check) | ||
|
|
||
| def predict_action(self, payload: Dict[str, Any]) -> JSONResponse: | ||
| """Predict action from observation.""" | ||
| try: | ||
| # Handle double-encoded payloads (for compatibility) | ||
| if "encoded" in payload: | ||
| assert len(payload.keys()) == 1, "Only uses encoded payload!" | ||
| payload = json.loads(payload["encoded"]) | ||
|
|
||
| # Validate required fields | ||
| if "observation" not in payload: | ||
| raise HTTPException( | ||
| status_code=400, detail="Missing 'observation' field in payload" | ||
| ) | ||
|
|
||
| obs = payload["observation"] | ||
|
|
||
| # Run inference | ||
| action = self.policy.get_action(obs) | ||
|
|
||
| # Return action as JSON with numpy arrays | ||
| return JSONResponse(content=action) | ||
|
|
||
| except Exception as e: | ||
| logging.error(traceback.format_exc()) | ||
| logging.warning( | ||
| "Your request threw an error; make sure your request complies with the expected format:\n" | ||
| "{'observation': dict} where observation contains the required modalities.\n" | ||
| "Example observation keys: video.ego_view, state.left_arm, state.right_arm, etc." | ||
| ) | ||
| raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}") | ||
|
|
||
| def health_check(self) -> Dict[str, str]: | ||
| """Health check endpoint.""" | ||
| return {"status": "healthy", "model": "GR00T"} | ||
|
|
||
| def run(self) -> None: | ||
| """Start the HTTP server.""" | ||
| print(f"Starting GR00T HTTP server on {self.host}:{self.port}") | ||
| print("Available endpoints:") | ||
| print(" POST /act - Get action prediction from observation") | ||
| print(" GET /health - Health check") | ||
| uvicorn.run(self.app, host=self.host, port=self.port) | ||
|
|
||
|
|
||
| def create_http_server( | ||
| policy: Gr00tPolicy, port: int, host: str = "0.0.0.0", api_token: Optional[str] = None | ||
| ) -> HTTPInferenceServer: | ||
| """Factory function to create an HTTP inference server.""" | ||
| return HTTPInferenceServer(policy, port, host, api_token) |
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
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
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,28 @@ | ||
| import time | ||
|
|
||
| import json_numpy | ||
| import numpy as np | ||
| import requests | ||
|
|
||
| json_numpy.patch() | ||
|
|
||
| obs = { | ||
| "video.ego_view": np.zeros((1, 256, 256, 3), dtype=np.uint8), | ||
| "state.left_arm": np.random.rand(1, 7), | ||
| "state.right_arm": np.random.rand(1, 7), | ||
| "state.left_hand": np.random.rand(1, 6), | ||
| "state.right_hand": np.random.rand(1, 6), | ||
| "state.waist": np.random.rand(1, 3), | ||
| "annotation.human.action.task_description": ["do your thing!"], | ||
| } | ||
|
|
||
|
|
||
| t = time.time() | ||
| response = requests.post( | ||
| "http://0.0.0.0:8000/act", | ||
| # "http://159.223.171.199:44989/act", # Bore tunnel | ||
| json={"observation": obs}, | ||
| ) | ||
| print(f"used time {time.time() - t}") | ||
| action = response.json() | ||
| print(action) |
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
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.
This is currently a hack to unblock things. Need better solution
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.
Issue is now tracked here: #265