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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Chutes Deployment for LoRASA Policy
|
| 3 |
+
|
| 4 |
+
This chute wraps the LoRASA agent for deployment on the Chutes platform
|
| 5 |
+
for evaluation in the kinitro robotics subnet.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
# Set environment variables
|
| 9 |
+
export CHUTE_USER="your-username"
|
| 10 |
+
export CHUTES_API_KEY="your-api-key"
|
| 11 |
+
|
| 12 |
+
# Deploy directly
|
| 13 |
+
chutes deploy chute:chute --accept-fee
|
| 14 |
+
|
| 15 |
+
# Or use kinitro CLI (recommended)
|
| 16 |
+
kinitro chutes-push --repo YOUR_HF_REPO --revision YOUR_REVISION
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
from typing import Optional
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
from pydantic import BaseModel
|
| 24 |
+
|
| 25 |
+
from chutes.chute import Chute, NodeSelector
|
| 26 |
+
from chutes.image import Image
|
| 27 |
+
|
| 28 |
+
# =============================================================================
|
| 29 |
+
# Configuration
|
| 30 |
+
# =============================================================================
|
| 31 |
+
|
| 32 |
+
CHUTES_USER = os.environ.get("CHUTE_USER", "shr1ftyy")
|
| 33 |
+
CHUTE_NAME = f"{CHUTES_USER}-lorasa-policy"
|
| 34 |
+
|
| 35 |
+
# HuggingFace repo (set by CLI or environment)
|
| 36 |
+
HF_REPO = os.environ.get("HF_REPO", "shr1ftyy/lorasa-policy")
|
| 37 |
+
HF_REVISION = os.environ.get("HF_REVISION", "main")
|
| 38 |
+
|
| 39 |
+
# GPU requirements
|
| 40 |
+
GPU_COUNT = 1
|
| 41 |
+
MIN_VRAM_GB = 16
|
| 42 |
+
|
| 43 |
+
# =============================================================================
|
| 44 |
+
# Image Configuration
|
| 45 |
+
# =============================================================================
|
| 46 |
+
|
| 47 |
+
image_tag = f"{HF_REVISION[:8]}" if HF_REVISION else "latest"
|
| 48 |
+
image = Image(
|
| 49 |
+
CHUTES_USER,
|
| 50 |
+
CHUTE_NAME,
|
| 51 |
+
image_tag,
|
| 52 |
+
readme=f"LoRASA policy for kinitro robotics subnet from {HF_REPO}",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Install dependencies
|
| 56 |
+
image.run_command(
|
| 57 |
+
"pip install --no-cache-dir "
|
| 58 |
+
"torch "
|
| 59 |
+
"numpy "
|
| 60 |
+
"gymnasium "
|
| 61 |
+
"pillow "
|
| 62 |
+
"pydantic "
|
| 63 |
+
"huggingface_hub"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Download model from HuggingFace
|
| 67 |
+
if HF_REPO:
|
| 68 |
+
image.run_command(
|
| 69 |
+
f'python -c "from huggingface_hub import snapshot_download; '
|
| 70 |
+
f"snapshot_download('{HF_REPO}', revision='{HF_REVISION}', local_dir='/app')\""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
image.set_workdir("/app")
|
| 74 |
+
|
| 75 |
+
# =============================================================================
|
| 76 |
+
# Chute Definition
|
| 77 |
+
# =============================================================================
|
| 78 |
+
|
| 79 |
+
chute = Chute(
|
| 80 |
+
username=CHUTES_USER,
|
| 81 |
+
name=CHUTE_NAME,
|
| 82 |
+
image=image,
|
| 83 |
+
readme=f"LoRASA policy for kinitro robotics subnet from {HF_REPO}",
|
| 84 |
+
node_selector=NodeSelector(
|
| 85 |
+
gpu_count=GPU_COUNT,
|
| 86 |
+
min_vram_gb_per_gpu=MIN_VRAM_GB,
|
| 87 |
+
),
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# =============================================================================
|
| 91 |
+
# Request/Response Models
|
| 92 |
+
# =============================================================================
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class TaskConfig(BaseModel):
|
| 96 |
+
"""Task configuration for episode reset."""
|
| 97 |
+
|
| 98 |
+
env_id: Optional[str] = None
|
| 99 |
+
env_name: Optional[str] = None
|
| 100 |
+
task_name: Optional[str] = None
|
| 101 |
+
seed: Optional[int] = None
|
| 102 |
+
task_id: Optional[int] = None
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
class ResetRequest(BaseModel):
|
| 106 |
+
"""Request body for /reset endpoint."""
|
| 107 |
+
|
| 108 |
+
task_config: TaskConfig
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class ResetResponse(BaseModel):
|
| 112 |
+
"""Response body for /reset endpoint."""
|
| 113 |
+
|
| 114 |
+
status: str = "ok"
|
| 115 |
+
episode_id: Optional[str] = None
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class ActRequest(BaseModel):
|
| 119 |
+
"""Request body for /act endpoint."""
|
| 120 |
+
|
| 121 |
+
observation: list[float]
|
| 122 |
+
images: Optional[dict[str, list]] = None
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
class ActResponse(BaseModel):
|
| 126 |
+
"""Response body for /act endpoint."""
|
| 127 |
+
|
| 128 |
+
action: list[float]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class HealthResponse(BaseModel):
|
| 132 |
+
"""Response body for /health endpoint."""
|
| 133 |
+
|
| 134 |
+
status: str
|
| 135 |
+
model_loaded: bool
|
| 136 |
+
uptime_seconds: float
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# =============================================================================
|
| 140 |
+
# Global State
|
| 141 |
+
# =============================================================================
|
| 142 |
+
|
| 143 |
+
_agent = None
|
| 144 |
+
_current_task_config = None
|
| 145 |
+
_start_time = None
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
@chute.on_startup()
|
| 149 |
+
def initialize(app):
|
| 150 |
+
"""Initialize the LoRASA agent on startup."""
|
| 151 |
+
import time
|
| 152 |
+
from pathlib import Path
|
| 153 |
+
|
| 154 |
+
global _agent, _start_time
|
| 155 |
+
_start_time = time.time()
|
| 156 |
+
|
| 157 |
+
# Import agent code (downloaded to /app from HuggingFace)
|
| 158 |
+
import sys
|
| 159 |
+
|
| 160 |
+
if "/app" not in sys.path:
|
| 161 |
+
sys.path.insert(0, "/app")
|
| 162 |
+
|
| 163 |
+
from agent import LoRASAAgent
|
| 164 |
+
|
| 165 |
+
agent_dir = Path("/app")
|
| 166 |
+
backbone_path = agent_dir / "checkpoints" / "lorasa_shared_backbone.pt"
|
| 167 |
+
adapter_dir = agent_dir / "checkpoints"
|
| 168 |
+
|
| 169 |
+
import torch
|
| 170 |
+
|
| 171 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 172 |
+
|
| 173 |
+
_agent = LoRASAAgent(
|
| 174 |
+
backbone_path=str(backbone_path),
|
| 175 |
+
adapter_dir=str(adapter_dir),
|
| 176 |
+
device=device,
|
| 177 |
+
)
|
| 178 |
+
print(f"LoRASA Agent initialized on {device}")
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# =============================================================================
|
| 182 |
+
# Endpoints
|
| 183 |
+
# =============================================================================
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
@chute.cord(
|
| 187 |
+
public_api_path="/health",
|
| 188 |
+
public_api_method="GET",
|
| 189 |
+
)
|
| 190 |
+
async def health() -> HealthResponse:
|
| 191 |
+
"""Health check endpoint."""
|
| 192 |
+
import time
|
| 193 |
+
|
| 194 |
+
global _agent, _start_time
|
| 195 |
+
|
| 196 |
+
return HealthResponse(
|
| 197 |
+
status="healthy" if _agent is not None else "unhealthy",
|
| 198 |
+
model_loaded=_agent is not None,
|
| 199 |
+
uptime_seconds=time.time() - _start_time if _start_time else 0,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@chute.cord(
|
| 204 |
+
public_api_path="/reset",
|
| 205 |
+
public_api_method="POST",
|
| 206 |
+
)
|
| 207 |
+
async def reset(request: ResetRequest) -> ResetResponse:
|
| 208 |
+
"""Reset policy for a new episode."""
|
| 209 |
+
import uuid
|
| 210 |
+
|
| 211 |
+
global _agent, _current_task_config
|
| 212 |
+
|
| 213 |
+
if _agent is None:
|
| 214 |
+
return ResetResponse(status="error", episode_id=None)
|
| 215 |
+
|
| 216 |
+
_agent.reset()
|
| 217 |
+
_current_task_config = request.task_config.model_dump()
|
| 218 |
+
episode_id = uuid.uuid4().hex
|
| 219 |
+
|
| 220 |
+
return ResetResponse(status="ok", episode_id=episode_id)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
@chute.cord(
|
| 224 |
+
public_api_path="/act",
|
| 225 |
+
public_api_method="POST",
|
| 226 |
+
)
|
| 227 |
+
async def act(request: ActRequest) -> ActResponse:
|
| 228 |
+
"""Get action for current observation."""
|
| 229 |
+
global _agent, _current_task_config
|
| 230 |
+
|
| 231 |
+
if _agent is None:
|
| 232 |
+
return ActResponse(action=[0.0, 0.0, 0.0, 0.0])
|
| 233 |
+
|
| 234 |
+
# Import helper functions
|
| 235 |
+
import sys
|
| 236 |
+
|
| 237 |
+
if "/app" not in sys.path:
|
| 238 |
+
sys.path.insert(0, "/app")
|
| 239 |
+
from model_loader import get_task_index
|
| 240 |
+
|
| 241 |
+
# Build observation dict for LoRASAAgent
|
| 242 |
+
obs_dict = {}
|
| 243 |
+
|
| 244 |
+
# Proprioceptive observation
|
| 245 |
+
proprio = np.array(request.observation, dtype=np.float32)
|
| 246 |
+
|
| 247 |
+
# Get task name for one-hot encoding
|
| 248 |
+
task_name = None
|
| 249 |
+
if _current_task_config:
|
| 250 |
+
task_name = _current_task_config.get("task_name")
|
| 251 |
+
if not task_name and _current_task_config.get("env_id"):
|
| 252 |
+
env_id = _current_task_config.get("env_id")
|
| 253 |
+
if env_id and "/" in env_id:
|
| 254 |
+
task_name = env_id.split("/")[-1]
|
| 255 |
+
|
| 256 |
+
# Create one-hot encoding
|
| 257 |
+
one_hot = np.zeros(50, dtype=np.float32)
|
| 258 |
+
if task_name:
|
| 259 |
+
task_idx = get_task_index(task_name)
|
| 260 |
+
if 0 <= task_idx < 50:
|
| 261 |
+
one_hot[task_idx] = 1.0
|
| 262 |
+
|
| 263 |
+
# observation.state = [proprio, one_hot_50]
|
| 264 |
+
obs_dict["observation.state"] = np.concatenate([proprio, one_hot])
|
| 265 |
+
|
| 266 |
+
# Include task_name for the agent
|
| 267 |
+
if task_name:
|
| 268 |
+
obs_dict["task_name"] = task_name
|
| 269 |
+
|
| 270 |
+
# Convert images if provided
|
| 271 |
+
if request.images:
|
| 272 |
+
for i, (cam_name, img_data) in enumerate(request.images.items()):
|
| 273 |
+
img = np.array(img_data, dtype=np.uint8)
|
| 274 |
+
# Convert HWC -> CHW if needed
|
| 275 |
+
if img.ndim == 3 and img.shape[-1] == 3:
|
| 276 |
+
img = img.transpose(2, 0, 1)
|
| 277 |
+
if i == 0:
|
| 278 |
+
obs_dict["observation.image"] = img
|
| 279 |
+
else:
|
| 280 |
+
obs_dict[f"observation.image{i+1}"] = img
|
| 281 |
+
else:
|
| 282 |
+
# Provide dummy images if not provided (agent expects 2 views)
|
| 283 |
+
obs_dict["observation.image"] = np.zeros((3, 84, 84), dtype=np.uint8)
|
| 284 |
+
obs_dict["observation.image2"] = np.zeros((3, 84, 84), dtype=np.uint8)
|
| 285 |
+
|
| 286 |
+
# Get action from agent
|
| 287 |
+
action_tensor = _agent.act(obs_dict)
|
| 288 |
+
|
| 289 |
+
# Convert to list
|
| 290 |
+
if hasattr(action_tensor, "numpy"):
|
| 291 |
+
action = action_tensor.numpy()
|
| 292 |
+
else:
|
| 293 |
+
action = np.array(action_tensor)
|
| 294 |
+
|
| 295 |
+
action = action.flatten().tolist()
|
| 296 |
+
|
| 297 |
+
return ActResponse(action=action)
|
requirements.txt
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
|
|
| 1 |
torch>=2.0.0
|
| 2 |
numpy>=1.24.0
|
| 3 |
gymnasium>=0.29.0
|
| 4 |
-
metaworld>=1.0.0
|
| 5 |
Pillow>=9.0.0
|
| 6 |
-
|
| 7 |
-
|
| 8 |
fastapi>=0.100.0
|
| 9 |
uvicorn>=0.23.0
|
| 10 |
pydantic>=2.0.0
|
| 11 |
|
|
|
|
|
|
|
|
|
| 1 |
+
# LoRASA Policy Dependencies
|
| 2 |
torch>=2.0.0
|
| 3 |
numpy>=1.24.0
|
| 4 |
gymnasium>=0.29.0
|
|
|
|
| 5 |
Pillow>=9.0.0
|
| 6 |
+
|
| 7 |
+
# Server dependencies (for local testing)
|
| 8 |
fastapi>=0.100.0
|
| 9 |
uvicorn>=0.23.0
|
| 10 |
pydantic>=2.0.0
|
| 11 |
|
| 12 |
+
# HuggingFace (for model hosting)
|
| 13 |
+
huggingface_hub>=0.16.0
|
server.py
CHANGED
|
@@ -153,10 +153,10 @@ async def act(request: ActRequest):
|
|
| 153 |
task_name = None
|
| 154 |
if _current_task_config:
|
| 155 |
task_name = _current_task_config.get("task_name")
|
| 156 |
-
if not task_name
|
| 157 |
# Extract from env_id like "metaworld/pick-place-v3"
|
| 158 |
env_id = _current_task_config.get("env_id")
|
| 159 |
-
if "/" in env_id:
|
| 160 |
task_name = env_id.split("/")[-1]
|
| 161 |
|
| 162 |
# Create one-hot encoding
|
|
|
|
| 153 |
task_name = None
|
| 154 |
if _current_task_config:
|
| 155 |
task_name = _current_task_config.get("task_name")
|
| 156 |
+
if not task_name:
|
| 157 |
# Extract from env_id like "metaworld/pick-place-v3"
|
| 158 |
env_id = _current_task_config.get("env_id")
|
| 159 |
+
if env_id and "/" in env_id:
|
| 160 |
task_name = env_id.split("/")[-1]
|
| 161 |
|
| 162 |
# Create one-hot encoding
|