|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from typing import Any, Dict, List, Optional, Union |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +from fastapi import FastAPI, HTTPException, Request |
| 21 | +from pydantic import BaseModel, Field |
| 22 | + |
| 23 | +from nemo_gym.base_resources_server import ( |
| 24 | + BaseResourcesServerConfig, |
| 25 | + BaseSeedSessionRequest, |
| 26 | + BaseSeedSessionResponse, |
| 27 | + BaseVerifyRequest, |
| 28 | + BaseVerifyResponse, |
| 29 | + SimpleResourcesServer, |
| 30 | +) |
| 31 | +from nemo_gym.server_utils import SESSION_ID_KEY, ServerClient |
| 32 | +from resources_servers.grl_tetris.tetris_env import TetrisEnv |
| 33 | + |
| 34 | + |
| 35 | +DEFAULT_GRID_LOOKUP = {0: "_", 1: "#", 2: "X"} |
| 36 | +DEFAULT_ACTION_LOOKUP = {0: "Left", 1: "Right", 2: "Down"} |
| 37 | + |
| 38 | + |
| 39 | +class GrlTetrisResourcesServerConfig(BaseResourcesServerConfig): |
| 40 | + env_config: Dict[str, Any] = Field( |
| 41 | + default_factory=lambda: { |
| 42 | + "grid_lookup": DEFAULT_GRID_LOOKUP, |
| 43 | + "action_lookup": DEFAULT_ACTION_LOOKUP, |
| 44 | + "render_mode": "text", |
| 45 | + "dim_x": 4, |
| 46 | + "dim_y": 4, |
| 47 | + "box_type": 3, |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class GrlTetrisSeedSessionRequest(BaseSeedSessionRequest): |
| 53 | + seed: Optional[int] = None |
| 54 | + |
| 55 | + |
| 56 | +class GrlTetrisSeedSessionResponse(BaseSeedSessionResponse): |
| 57 | + observation: str |
| 58 | + |
| 59 | + |
| 60 | +class GrlTetrisStepRequest(BaseModel): |
| 61 | + actions: List[Union[str, int]] = Field(default_factory=list) |
| 62 | + |
| 63 | + |
| 64 | +class GrlTetrisStepTrace(BaseModel): |
| 65 | + action_id: int |
| 66 | + action_label: str |
| 67 | + reward: float |
| 68 | + done: bool |
| 69 | + info: Dict[str, Any] |
| 70 | + |
| 71 | + |
| 72 | +class GrlTetrisStepResponse(BaseModel): |
| 73 | + observation: str |
| 74 | + reward: float |
| 75 | + total_reward: float |
| 76 | + done: bool |
| 77 | + steps: List[GrlTetrisStepTrace] |
| 78 | + history: List[GrlTetrisStepTrace] = Field(default_factory=list) |
| 79 | + |
| 80 | + |
| 81 | +class GrlTetrisVerifyResponse(BaseVerifyResponse): |
| 82 | + success: bool |
| 83 | + |
| 84 | + |
| 85 | +@dataclass |
| 86 | +class TetrisSessionState: |
| 87 | + env: Any |
| 88 | + observation: str |
| 89 | + total_reward: float = 0.0 |
| 90 | + done: bool = False |
| 91 | + last_info: Dict[str, Any] = field(default_factory=dict) |
| 92 | + history: List[GrlTetrisStepTrace] = field(default_factory=list) |
| 93 | + |
| 94 | + |
| 95 | +class GrlTetrisResourcesServer(SimpleResourcesServer): |
| 96 | + config: GrlTetrisResourcesServerConfig |
| 97 | + server_client: ServerClient |
| 98 | + session_id_to_state: Dict[str, TetrisSessionState] = Field(default_factory=dict) |
| 99 | + |
| 100 | + def setup_webserver(self) -> FastAPI: |
| 101 | + app = super().setup_webserver() |
| 102 | + app.post("/step")(self.step) |
| 103 | + return app |
| 104 | + |
| 105 | + def _create_env(self) -> TetrisEnv: |
| 106 | + return TetrisEnv(self.config.env_config) |
| 107 | + |
| 108 | + async def seed_session(self, request: Request, body: GrlTetrisSeedSessionRequest) -> GrlTetrisSeedSessionResponse: |
| 109 | + session_id = request.session[SESSION_ID_KEY] |
| 110 | + env = self._create_env() |
| 111 | + observation = env.reset(seed=body.seed) |
| 112 | + |
| 113 | + self.session_id_to_state[session_id] = TetrisSessionState( |
| 114 | + env=env, |
| 115 | + observation=observation, |
| 116 | + ) |
| 117 | + return GrlTetrisSeedSessionResponse(observation=observation) |
| 118 | + |
| 119 | + async def step(self, request: Request, body: GrlTetrisStepRequest) -> GrlTetrisStepResponse: |
| 120 | + session_id = request.session.get(SESSION_ID_KEY) |
| 121 | + if session_id is None or session_id not in self.session_id_to_state: |
| 122 | + raise HTTPException(status_code=400, detail="Session not initialized. Call /seed_session first.") |
| 123 | + |
| 124 | + session_state = self.session_id_to_state[session_id] |
| 125 | + env = session_state.env |
| 126 | + |
| 127 | + reverse_lookup = {label.lower(): idx for idx, label in env.ACTION_LOOKUP.items()} |
| 128 | + total_step_reward = 0.0 |
| 129 | + steps: List[GrlTetrisStepTrace] = [] |
| 130 | + |
| 131 | + if session_state.done: |
| 132 | + return GrlTetrisStepResponse( |
| 133 | + observation=session_state.observation, |
| 134 | + reward=0.0, |
| 135 | + total_reward=session_state.total_reward, |
| 136 | + done=True, |
| 137 | + steps=[], |
| 138 | + history=list(session_state.history), |
| 139 | + ) |
| 140 | + |
| 141 | + for action in body.actions: |
| 142 | + action_id = self._parse_action(action, reverse_lookup) |
| 143 | + if action_id not in env.ACTION_LOOKUP: |
| 144 | + raise HTTPException(status_code=400, detail=f"Invalid action identifier: {action}") |
| 145 | + |
| 146 | + next_obs, reward, done, info = env.step(action_id) |
| 147 | + info = self._to_python_types(info) |
| 148 | + total_step_reward += reward |
| 149 | + session_state.total_reward += reward |
| 150 | + session_state.observation = next_obs |
| 151 | + session_state.last_info = info |
| 152 | + session_state.done = bool(done) |
| 153 | + |
| 154 | + step = GrlTetrisStepTrace( |
| 155 | + action_id=action_id, |
| 156 | + action_label=env.ACTION_LOOKUP[action_id], |
| 157 | + reward=reward, |
| 158 | + done=session_state.done, |
| 159 | + info=info, |
| 160 | + ) |
| 161 | + session_state.history.append(step) |
| 162 | + steps.append(step) |
| 163 | + |
| 164 | + if session_state.done: |
| 165 | + break |
| 166 | + |
| 167 | + return GrlTetrisStepResponse( |
| 168 | + observation=session_state.observation, |
| 169 | + reward=total_step_reward, |
| 170 | + total_reward=session_state.total_reward, |
| 171 | + done=session_state.done, |
| 172 | + steps=steps, |
| 173 | + history=list(session_state.history), |
| 174 | + ) |
| 175 | + |
| 176 | + async def verify(self, request: Request, body: BaseVerifyRequest) -> GrlTetrisVerifyResponse: |
| 177 | + session_id = request.session.get(SESSION_ID_KEY) |
| 178 | + session_state = self.session_id_to_state.get(session_id) |
| 179 | + |
| 180 | + success = False |
| 181 | + reward = 0.0 |
| 182 | + if session_state is not None: |
| 183 | + success = bool(session_state.last_info.get("success")) |
| 184 | + reward = session_state.total_reward |
| 185 | + |
| 186 | + if session_id in self.session_id_to_state: |
| 187 | + try: |
| 188 | + session_state.env.close() # type: ignore[union-attr] |
| 189 | + except Exception: # pragma: no cover - defensive cleanup |
| 190 | + pass |
| 191 | + del self.session_id_to_state[session_id] |
| 192 | + |
| 193 | + return GrlTetrisVerifyResponse( |
| 194 | + **body.model_dump(), |
| 195 | + reward=reward, |
| 196 | + success=success, |
| 197 | + ) |
| 198 | + |
| 199 | + @staticmethod |
| 200 | + def _parse_action(action: Union[str, int], reverse_lookup: Dict[str, int]) -> int: |
| 201 | + if isinstance(action, int): |
| 202 | + return action |
| 203 | + |
| 204 | + candidate = action.strip() |
| 205 | + lower_candidate = candidate.lower() |
| 206 | + if lower_candidate in reverse_lookup: |
| 207 | + return reverse_lookup[lower_candidate] |
| 208 | + |
| 209 | + try: |
| 210 | + return int(candidate) |
| 211 | + except ValueError as exc: |
| 212 | + raise HTTPException(status_code=400, detail=f"Unable to parse action: {action}") from exc |
| 213 | + |
| 214 | + @staticmethod |
| 215 | + def _to_python_types(obj: Any) -> Any: |
| 216 | + if isinstance(obj, dict): |
| 217 | + return {k: GrlTetrisResourcesServer._to_python_types(v) for k, v in obj.items()} |
| 218 | + if isinstance(obj, list): |
| 219 | + return [GrlTetrisResourcesServer._to_python_types(v) for v in obj] |
| 220 | + if isinstance(obj, np.generic): |
| 221 | + return obj.item() |
| 222 | + return obj |
| 223 | + |
| 224 | + |
| 225 | +if __name__ == "__main__": |
| 226 | + GrlTetrisResourcesServer.run_webserver() |
0 commit comments