22
22
ConstraintInitializer: Function signature for constraint factory.
23
23
"""
24
24
25
+ from __future__ import annotations
26
+
25
27
import time
26
- from typing import Any , Optional , Protocol , Union , runtime_checkable
28
+ from typing import Any , Protocol , runtime_checkable
27
29
28
30
from pydantic import Field
29
31
32
34
ScheduledRequestInfo ,
33
35
SchedulerState ,
34
36
SchedulerUpdateAction ,
37
+ SchedulerUpdateActionProgress ,
35
38
)
36
39
from guidellm .utils import RegistryMixin
37
40
@@ -127,7 +130,7 @@ def resolve(
127
130
cls ,
128
131
initializers : dict [
129
132
str ,
130
- Union [ Any , dict [str , Any ], Constraint , ConstraintInitializer ] ,
133
+ Any | dict [str , Any ] | Constraint | ConstraintInitializer ,
131
134
],
132
135
) -> dict [str , Constraint ]:
133
136
"""
@@ -154,7 +157,7 @@ def resolve(
154
157
@classmethod
155
158
def resolve_constraints (
156
159
cls ,
157
- constraints : dict [str , Union [ Any , dict [str , Any ], Constraint ] ],
160
+ constraints : dict [str , Any | dict [str , Any ] | Constraint ],
158
161
) -> dict [str , Constraint ]:
159
162
"""
160
163
Resolve constraints from mixed constraint specifications.
@@ -177,9 +180,7 @@ def resolve_constraints(
177
180
178
181
179
182
class _MaxNumberBase (StandardBaseModel ):
180
- max_num : Union [int , float ] = Field (
181
- gt = 0 , description = "Maximum number of requests allowed"
182
- )
183
+ max_num : int | float = Field (gt = 0 , description = "Maximum number of requests allowed" )
183
184
184
185
185
186
class MaxNumberConstraint (_MaxNumberBase ):
@@ -208,12 +209,12 @@ def __call__(
208
209
"created_requests" : state .created_requests ,
209
210
"processed_requests" : state .processed_requests ,
210
211
},
211
- progress = {
212
- " remaining_fraction" : max (
212
+ progress = SchedulerUpdateActionProgress (
213
+ remaining_fraction = max (
213
214
0.0 , 1.0 - state .processed_requests / float (self .max_num )
214
215
),
215
- " remaining_requests" : max (0 , self .max_num - state .processed_requests ),
216
- } ,
216
+ remaining_requests = max (0 , self .max_num - state .processed_requests ),
217
+ ) ,
217
218
)
218
219
219
220
@@ -222,9 +223,7 @@ class MaxNumberConstraintInitializer(_MaxNumberBase):
222
223
"""Factory for creating MaxNumberConstraint instances."""
223
224
224
225
@classmethod
225
- def from_simple_value (
226
- cls , value : Union [int , float ]
227
- ) -> "MaxNumberConstraintInitializer" :
226
+ def from_simple_value (cls , value : int | float ) -> MaxNumberConstraintInitializer :
228
227
"""
229
228
Create a MaxNumberConstraintInitializer from a simple scalar value.
230
229
@@ -246,9 +245,7 @@ def create_constraint(self, **_kwargs) -> Constraint:
246
245
247
246
248
247
class _MaxDurationBase (StandardBaseModel ):
249
- max_duration : Union [int , float ] = Field (
250
- gt = 0 , description = "Maximum duration in seconds"
251
- )
248
+ max_duration : int | float = Field (gt = 0 , description = "Maximum duration in seconds" )
252
249
253
250
254
251
class MaxDurationConstraint (_MaxDurationBase ):
@@ -278,12 +275,10 @@ def __call__(
278
275
"start_time" : state .start_time ,
279
276
"current_time" : current_time ,
280
277
},
281
- progress = {
282
- "remaining_fraction" : max (
283
- 0.0 , 1.0 - elapsed / float (self .max_duration )
284
- ),
285
- "remaining_duration" : max (0.0 , self .max_duration - elapsed ),
286
- },
278
+ progress = SchedulerUpdateActionProgress (
279
+ remaining_fraction = max (0.0 , 1.0 - elapsed / float (self .max_duration )),
280
+ remaining_duration = max (0.0 , self .max_duration - elapsed ),
281
+ ),
287
282
)
288
283
289
284
@@ -292,9 +287,7 @@ class MaxDurationConstraintInitializer(_MaxDurationBase):
292
287
"""Factory for creating MaxDurationConstraint instances."""
293
288
294
289
@classmethod
295
- def from_simple_value (
296
- cls , value : Union [int , float ]
297
- ) -> "MaxDurationConstraintInitializer" :
290
+ def from_simple_value (cls , value : int | float ) -> MaxDurationConstraintInitializer :
298
291
"""
299
292
Create a MaxDurationConstraintInitializer from a simple scalar value.
300
293
@@ -316,7 +309,7 @@ def create_constraint(self, **_kwargs) -> Constraint:
316
309
317
310
318
311
class _MaxErrorsBase (StandardBaseModel ):
319
- max_errors : Union [ int , float ] = Field (
312
+ max_errors : int | float = Field (
320
313
gt = 0 , description = "Maximum number of errors allowed"
321
314
)
322
315
@@ -352,9 +345,7 @@ class MaxErrorsConstraintInitializer(_MaxErrorsBase):
352
345
"""Factory for creating MaxErrorsConstraint instances."""
353
346
354
347
@classmethod
355
- def from_simple_value (
356
- cls , value : Union [int , float ]
357
- ) -> "MaxErrorsConstraintInitializer" :
348
+ def from_simple_value (cls , value : int | float ) -> MaxErrorsConstraintInitializer :
358
349
"""
359
350
Create a MaxErrorsConstraintInitializer from a simple scalar value.
360
351
@@ -376,10 +367,10 @@ def create_constraint(self, **_kwargs) -> Constraint:
376
367
377
368
378
369
class _MaxErrorRateBase (StandardBaseModel ):
379
- max_error_rate : Union [ int , float ] = Field (
370
+ max_error_rate : int | float = Field (
380
371
gt = 0 , le = 1 , description = "Maximum error rate allowed (0.0 to 1.0)"
381
372
)
382
- window_size : Union [ int , float ] = Field (
373
+ window_size : int | float = Field (
383
374
default = 50 ,
384
375
gt = 0 ,
385
376
description = "Size of sliding window for calculating error rate" ,
@@ -444,9 +435,7 @@ class MaxErrorRateConstraintInitializer(_MaxErrorRateBase):
444
435
"""Factory for creating MaxErrorRateConstraint instances."""
445
436
446
437
@classmethod
447
- def from_simple_value (
448
- cls , value : Union [int , float ]
449
- ) -> "MaxErrorRateConstraintInitializer" :
438
+ def from_simple_value (cls , value : int | float ) -> MaxErrorRateConstraintInitializer :
450
439
"""
451
440
Create a MaxErrorRateConstraintInitializer from a simple scalar value.
452
441
@@ -469,10 +458,10 @@ def create_constraint(self, **_kwargs) -> Constraint:
469
458
470
459
471
460
class _MaxGlobalErrorRateBase (StandardBaseModel ):
472
- max_error_rate : Union [ int , float ] = Field (
461
+ max_error_rate : int | float = Field (
473
462
gt = 0 , le = 1 , description = "Maximum error rate allowed (0.0 to 1.0)"
474
463
)
475
- min_processed : Optional [ Union [ int , float ]] = Field (
464
+ min_processed : int | float | None = Field (
476
465
default = 50 ,
477
466
gt = 30 ,
478
467
description = (
@@ -524,8 +513,8 @@ class MaxGlobalErrorRateConstraintInitializer(_MaxGlobalErrorRateBase):
524
513
525
514
@classmethod
526
515
def from_simple_value (
527
- cls , value : Union [ int , float ]
528
- ) -> " MaxGlobalErrorRateConstraintInitializer" :
516
+ cls , value : int | float
517
+ ) -> MaxGlobalErrorRateConstraintInitializer :
529
518
"""
530
519
Create a MaxGlobalErrorRateConstraintInitializer from a simple scalar value.
531
520
0 commit comments