Skip to content

Commit b050c76

Browse files
committed
Update precision_config.py
(cherry picked from commit 16a6434) Signed-off-by: nathon-lee <leejianwoo@gmail.com>
1 parent 6c59d54 commit b050c76

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

deepspeed/runtime/precision_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# DeepSpeed Team
55

6+
import math
7+
from pydantic import field_validator
68
from deepspeed.runtime.config_utils import DeepSpeedConfigModel
79
from .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

0 commit comments

Comments
 (0)