MCPcopy Create free account
hub / github.com/AlayaLab/Hive / get_scheduler

Function get_scheduler

models/flowsep/diffusers/optimization.py:282–354  ·  view source on GitHub ↗

Unified API to get any scheduler from its name. Args: name (`str` or `SchedulerType`): The name of the scheduler to use. optimizer (`torch.optim.Optimizer`): The optimizer that will be used during training. step_rules (`str`, *optiona

(
    name: Union[str, SchedulerType],
    optimizer: Optimizer,
    step_rules: Optional[str] = None,
    num_warmup_steps: Optional[int] = None,
    num_training_steps: Optional[int] = None,
    num_cycles: int = 1,
    power: float = 1.0,
    last_epoch: int = -1,
)

Source from the content-addressed store, hash-verified

280
281
282def get_scheduler(
283 name: Union[str, SchedulerType],
284 optimizer: Optimizer,
285 step_rules: Optional[str] = None,
286 num_warmup_steps: Optional[int] = None,
287 num_training_steps: Optional[int] = None,
288 num_cycles: int = 1,
289 power: float = 1.0,
290 last_epoch: int = -1,
291):
292 """
293 Unified API to get any scheduler from its name.
294
295 Args:
296 name (`str` or `SchedulerType`):
297 The name of the scheduler to use.
298 optimizer (`torch.optim.Optimizer`):
299 The optimizer that will be used during training.
300 step_rules (`str`, *optional*):
301 A string representing the step rules to use. This is only used by the `PIECEWISE_CONSTANT` scheduler.
302 num_warmup_steps (`int`, *optional*):
303 The number of warmup steps to do. This is not required by all schedulers (hence the argument being
304 optional), the function will raise an error if it's unset and the scheduler type requires it.
305 num_training_steps (`int``, *optional*):
306 The number of training steps to do. This is not required by all schedulers (hence the argument being
307 optional), the function will raise an error if it's unset and the scheduler type requires it.
308 num_cycles (`int`, *optional*):
309 The number of hard restarts used in `COSINE_WITH_RESTARTS` scheduler.
310 power (`float`, *optional*, defaults to 1.0):
311 Power factor. See `POLYNOMIAL` scheduler
312 last_epoch (`int`, *optional*, defaults to -1):
313 The index of the last epoch when resuming training.
314 """
315 name = SchedulerType(name)
316 schedule_func = TYPE_TO_SCHEDULER_FUNCTION[name]
317 if name == SchedulerType.CONSTANT:
318 return schedule_func(optimizer, last_epoch=last_epoch)
319
320 if name == SchedulerType.PIECEWISE_CONSTANT:
321 return schedule_func(optimizer, rules=step_rules, last_epoch=last_epoch)
322
323 # All other schedulers require `num_warmup_steps`
324 if num_warmup_steps is None:
325 raise ValueError(f"{name} requires `num_warmup_steps`, please provide that argument.")
326
327 if name == SchedulerType.CONSTANT_WITH_WARMUP:
328 return schedule_func(optimizer, num_warmup_steps=num_warmup_steps, last_epoch=last_epoch)
329
330 # All other schedulers require `num_training_steps`
331 if num_training_steps is None:
332 raise ValueError(f"{name} requires `num_training_steps`, please provide that argument.")
333
334 if name == SchedulerType.COSINE_WITH_RESTARTS:
335 return schedule_func(
336 optimizer,
337 num_warmup_steps=num_warmup_steps,
338 num_training_steps=num_training_steps,
339 num_cycles=num_cycles,

Callers

nothing calls this directly

Calls 1

SchedulerTypeClass · 0.85

Tested by

no test coverage detected