Spaces:
Sleeping

File size: 10,163 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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 Data Models
=========================
Typed Pydantic models that define every piece of data flowing between
the agent and the environment server.  Three layers:

  ActionType     β€” enum of the three discrete agent actions
  GpuSchedulerAction    β€” the action the agent sends to /step
  JobInfo        β€” snapshot of one job (queued or running)
  NodeInfo       β€” snapshot of one physical node (8 GPUs)
  GpuSchedulerObservation β€” the full state the agent sees after each step

All models inherit from OpenEnv base types so they are automatically
schema-exported, validated at the HTTP boundary, and compatible with
`openenv validate`.
"""

from enum import Enum
from typing import Any, Dict, List, Optional

from openenv.core.env_server.types import Action, Observation
from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# Action space
# ---------------------------------------------------------------------------

class ActionType(str, Enum):
    """
    The three discrete operations an agent can perform each step.

    SCHEDULE β€” place a queued job onto a specific node.
    PREEMPT  β€” immediately evict a running job back to the queue.
               Triggers a large penalty (preemption burn).
    WAIT     β€” advance the simulation clock one step without scheduling.
               Useful when the cluster is full or a better slot is coming.
    """
    SCHEDULE = "SCHEDULE"
    PREEMPT  = "PREEMPT"
    WAIT     = "WAIT"


class GpuSchedulerAction(Action):
    """
    Action submitted by the agent at each decision step.

    Fields are optional depending on action_type:
        SCHEDULE β†’ requires job_id + node_id
        PREEMPT  β†’ requires job_id only
        WAIT     β†’ no additional fields needed

    The environment server validates combinations and returns an error in
    the observation's `last_action_result` field if invalid.
    """

    action_type: ActionType = Field(
        ...,
        description="Which operation to perform: SCHEDULE, PREEMPT, or WAIT",
    )
    job_id: Optional[str] = Field(
        default=None,
        description="ID of the job to schedule or preempt. Required for SCHEDULE/PREEMPT.",
    )
    node_id: Optional[int] = Field(
        default=None,
        ge=0,
        le=7,
        description="Target node index (0–7) for SCHEDULE actions.",
    )


# ---------------------------------------------------------------------------
# Sub-models embedded inside the Observation
# ---------------------------------------------------------------------------

class JobInfo(BaseModel):
    """
    Complete snapshot of one job at a given simulation step.

    Covers all lifecycle states: queued β†’ running β†’ completed / preempted.
    Both the queue preview and the active job list use this same model.
    """

    job_id: str = Field(
        ...,
        description="Unique identifier, e.g. 'job_00042'",
    )
    priority: int = Field(
        ...,
        ge=0,
        le=3,
        description="Integer priority: 0=P0 (highest/critical), 3=P3 (lowest/spot)",
    )
    priority_label: str = Field(
        ...,
        description="Human-readable label: 'P0', 'P1', 'P2', or 'P3'",
    )
    gpu_count: int = Field(
        ...,
        ge=1,
        le=64,
        description=(
            "Number of GPUs required. Values >8 span multiple nodes "
            "(e.g. 32 GPUs = 4 nodes β€” a gang-scheduled job)."
        ),
    )
    duration_hours: float = Field(
        ...,
        gt=0,
        description="Total runtime needed to complete this job, in simulated hours.",
    )
    elapsed_hours: float = Field(
        default=0.0,
        ge=0.0,
        description="Hours of compute already spent on this job (resets after preemption penalty).",
    )
    progress: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description=(
            "Fraction of work completed (0.0 β†’ 1.0). "
            "Degrades below nominal rate when node memory contention is high."
        ),
    )
    status: str = Field(
        default="queued",
        description="Lifecycle state: 'queued' | 'running' | 'completed' | 'preempted'",
    )
    deadline_hour: Optional[float] = Field(
        default=None,
        description=(
            "Absolute simulation hour by which the job must finish. "
            "None = no SLA. Missing deadline triggers SLA violation penalty."
        ),
    )
    hours_in_queue: float = Field(
        default=0.0,
        ge=0.0,
        description="Hours this job has been waiting in queue. Used to compute queueing delay penalty.",
    )
    assigned_nodes: List[int] = Field(
        default_factory=list,
        description="Node IDs the job is currently occupying (empty if queued).",
    )
    arrival_hour: float = Field(
        default=0.0,
        ge=0.0,
        description="Simulated hour at which this job arrived in the cluster queue.",
    )


class NodeInfo(BaseModel):
    """
    Snapshot of one physical node's resource state.

    Each node has 8 physical GPUs.  Memory contention rises as more
    GPU-hours are colocated on the same PCIe bus, degrading job progress
    rates above the 0.7 contention threshold.
    """

    node_id: int = Field(
        ...,
        ge=0,
        le=7,
        description="Node index in the 8-node cluster (0–7).",
    )
    total_gpus: int = Field(
        default=8,
        description="Physical GPU capacity of this node (always 8 in this cluster).",
    )
    used_gpus: int = Field(
        default=0,
        ge=0,
        le=8,
        description="GPUs currently allocated to running jobs.",
    )
    free_gpus: int = Field(
        default=8,
        ge=0,
        le=8,
        description="GPUs available for scheduling (= total_gpus - used_gpus).",
    )
    memory_contention: float = Field(
        default=0.0,
        ge=0.0,
        le=1.0,
        description=(
            "Normalised contention level (0.0 = no contention, 1.0 = fully saturated). "
            "Above 0.7 all jobs on this node suffer a progress rate penalty."
        ),
    )
    running_jobs: List[str] = Field(
        default_factory=list,
        description="job_ids of all jobs currently executing on this node.",
    )


# ---------------------------------------------------------------------------
# Observation β€” what the agent sees after every step / reset
# ---------------------------------------------------------------------------

class GpuSchedulerObservation(Observation):
    """
    Full environment snapshot delivered to the agent after each step.

    Structured into four logical sections:
      1. Cluster topology   β€” 8Γ—8 grid + per-node resource/contention metrics
      2. Job lists          β€” active (running) jobs + lookahead queue
      3. Simulation clock   β€” current hour, episode horizon, burn so far
      4. Action feedback    β€” plain-text result of the last action taken

    The LLM agent should use all four sections to decide its next action.
    """

    # ------------------------------------------------------------------
    # 1. Cluster topology
    # ------------------------------------------------------------------
    cluster_grid: List[List[int]] = Field(
        ...,
        description=(
            "8Γ—8 integer matrix. "
            "Each cell is the GPU slot index within a node row. "
            "Value = -1 (free) or a job index into active_jobs list."
        ),
    )
    nodes: List[NodeInfo] = Field(
        ...,
        description="Per-node resource snapshot for all 8 nodes.",
    )

    # ------------------------------------------------------------------
    # 2. Job lists
    # ------------------------------------------------------------------
    active_jobs: List[JobInfo] = Field(
        default_factory=list,
        description="All jobs currently executing on the cluster.",
    )
    queue: List[JobInfo] = Field(
        default_factory=list,
        description=(
            "Jobs waiting to be scheduled. "
            "Includes a ~4-hour lookahead window of upcoming arrivals "
            "so the agent can plan proactively."
        ),
    )

    # ------------------------------------------------------------------
    # 3. Simulation clock & economics
    # ------------------------------------------------------------------
    current_hour: float = Field(
        default=0.0,
        ge=0.0,
        description="Simulated wall-clock hour elapsed since episode start.",
    )
    total_hours: float = Field(
        default=24.0,
        gt=0,
        description="Total duration of this episode (24 / 72 / 168 hours by task).",
    )
    compute_burn_so_far: float = Field(
        default=0.0,
        ge=0.0,
        description=(
            "Cumulative operational cost in USD accrued this episode. "
            "Cluster burns ~$4,167/hour at full 64-GPU capacity ($100k/day)."
        ),
    )

    # ------------------------------------------------------------------
    # 4. Task metadata & action feedback
    # ------------------------------------------------------------------
    task_name: str = Field(
        default="smooth_sailing",
        description="Which of the three tasks is running: smooth_sailing | deadline_crunch | p0_emergency",
    )
    last_action_result: str = Field(
        default="",
        description=(
            "Plain-English outcome of the previous action, e.g. "
            "'Scheduled job_003 on node 2 (6 GPUs)' or "
            "'INVALID: node 5 has only 2 free GPUs but job needs 4'."
        ),
    )
    score: Optional[float] = Field(
        default=None,
        ge=0.0,
        le=1.0,
        description=(
            "Normalised grader score in [0.0, 1.0] β€” only populated when done=True. "
            "None at all intermediate steps. Used by inference.py to report episode result."
        ),
    )