Spaces:
Sleeping
Sleeping
File size: 5,090 Bytes
aedefd2 | 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 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
GpuScheduler Environment Client
================================
WebSocket-based client that connects to the running GpuScheduler server.
Handles action serialization (Python β JSON) and result deserialization
(JSON β typed Pydantic models) for the full scheduler action/observation space.
Usage:
# Against a running local server
env = GpuSchedulerEnv(base_url="http://localhost:8000")
result = await env.reset()
result = await env.step(GpuSchedulerAction(action_type="WAIT"))
await env.close()
# Against a Docker container (auto-starts, auto-cleans up)
env = await GpuSchedulerEnv.from_docker_image(os.getenv("IMAGE_NAME"))
"""
from typing import Dict, List, Optional
from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State
# NOTE: models.py is built in Step 2 β these imports become valid after that.
from .models import (
GpuSchedulerAction,
GpuSchedulerObservation,
JobInfo,
NodeInfo,
)
class GpuSchedulerEnv(
EnvClient[GpuSchedulerAction, GpuSchedulerObservation, State]
):
"""
Typed WebSocket client for the GpuScheduler Environment.
Extends OpenEnv's EnvClient with GPU-scheduler-specific serialization.
Each instance maintains a single persistent WebSocket session on the server,
so state is fully isolated between concurrent agent runs.
The client handles three action types:
SCHEDULE(job_id, node_id) β place a queued job onto a node
PREEMPT(job_id) β evict a running job back to queue
WAIT β advance the simulation clock one step
"""
def _step_payload(self, action: GpuSchedulerAction) -> Dict:
"""
Serialize a GpuSchedulerAction into the JSON body sent to POST /step.
Only includes fields relevant to the chosen action_type so the server
can cleanly validate partial actions (e.g. WAIT has no job_id).
Args:
action: Typed action from the agent.
Returns:
Dict that will be JSON-encoded and sent to the server.
"""
payload: Dict = {"action_type": action.action_type}
# Conditionally include optional fields β avoids None noise in JSON
if action.job_id is not None:
payload["job_id"] = action.job_id
if action.node_id is not None:
payload["node_id"] = action.node_id
return payload
def _parse_result(self, payload: Dict) -> StepResult[GpuSchedulerObservation]:
"""
Deserialize the server's JSON response into a typed StepResult.
The server returns a flat dict; we reconstruct the nested Pydantic
models (NodeInfo, JobInfo) that make up the observation.
Args:
payload: Raw JSON dict from the server step/reset response.
Returns:
StepResult containing a fully-typed GpuSchedulerObservation.
"""
obs_data = payload.get("observation", {})
# --- Reconstruct nested node objects ---
nodes = [
NodeInfo(**n) for n in obs_data.get("nodes", [])
]
# --- Reconstruct active jobs and queue ---
active_jobs = [
JobInfo(**j) for j in obs_data.get("active_jobs", [])
]
queue = [
JobInfo(**j) for j in obs_data.get("queue", [])
]
# --- Build the full observation object ---
observation = GpuSchedulerObservation(
cluster_grid=obs_data.get("cluster_grid", [[-1] * 8 for _ in range(8)]),
nodes=nodes,
active_jobs=active_jobs,
queue=queue,
current_hour=obs_data.get("current_hour", 0.0),
total_hours=obs_data.get("total_hours", 24.0),
compute_burn_so_far=obs_data.get("compute_burn_so_far", 0.0),
task_name=obs_data.get("task_name", "unknown"),
last_action_result=obs_data.get("last_action_result", ""),
# score field: populated by the environment when done=True
score=obs_data.get("score"),
done=payload.get("done", False),
reward=payload.get("reward", 0.0),
metadata=obs_data.get("metadata", {}),
)
return StepResult(
observation=observation,
reward=payload.get("reward", 0.0),
done=payload.get("done", False),
)
def _parse_state(self, payload: Dict) -> State:
"""
Deserialize the GET /state response into an OpenEnv State object.
Args:
payload: Raw JSON dict from the server state response.
Returns:
State with episode_id and current step_count.
"""
return State(
episode_id=payload.get("episode_id"),
step_count=payload.get("step_count", 0),
)
|