File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33
44# DeepSpeed Team
55
6+ import math
7+ from pydantic import field_validator
68from deepspeed .runtime .config_utils import DeepSpeedConfigModel
79from .fp16 .loss_scaler import (
810 INITIAL_LOSS_SCALE ,
@@ -154,3 +156,27 @@ def dynamic_loss_scale_args(self):
154156 CONSECUTIVE_HYSTERESIS : self .consecutive_hysteresis ,
155157 MIN_LOSS_SCALE : self .min_loss_scale ,
156158 }
159+
160+ loss_scale : float = 0
161+ """
162+ Loss scaling value. Default value of 0 means dynamic loss scaling instead of static loss scale.
163+ """
164+
165+ @field_validator ("loss_scale" )
166+ @classmethod
167+ def _validate_loss_scale (cls , v ):
168+ # Prevent True/False from being treated as 1/0
169+ if isinstance (v , bool ):
170+ raise ValueError ("fp16.loss_scale must be a number, not bool" )
171+
172+ v = float (v )
173+
174+ # Reject inf/-inf/nan
175+ if not math .isfinite (v ):
176+ raise ValueError ("fp16.loss_scale must be a finite number (not inf/-inf/nan)" )
177+
178+ # Reject negative values; 0 still means dynamic loss scaling
179+ if v < 0 :
180+ raise ValueError ("fp16.loss_scale must be >= 0 (0 enables dynamic loss scaling)" )
181+
182+ return v
You can’t perform that action at this time.
0 commit comments