pub struct GradScaler { /* private fields */ }Expand description
損失スケーリングのスケール値更新契約を保持する状態機械
(PyTorch torch.cuda.amp.GradScaler 相当)。
1 step の使い方:
scaler.scale_loss(&loss) → Tape::backward →
scaler.unscale(&grads) で UnscaleResult を得る →
should_skip_step() が true ならこの step の clip・optimizer step
を 両方スキップする(clip_grad_norm は非有限勾配で Err を
返すため、非有限検出より前に clip を呼ぶと学習ループ全体が失敗する。
nn/optim/mod.rs doc「適用順序契約」参照)→ false なら
clip_grad_norm → optimizer step の順に進める → 最後に必ず
scaler.update(found_non_finite) を呼びスケールを更新する。
Implementations§
Source§impl GradScaler
impl GradScaler
Sourcepub fn new(config: GradScalerConfig) -> Result<GradScaler, AutodiffError>
pub fn new(config: GradScalerConfig) -> Result<GradScaler, AutodiffError>
GradScalerConfig を検証して構築する(AdamW::new と同様、
テンソル生成に進む前に構築不可能な引数を弾く。
error.rs::AutodiffError::InvalidArgument doc 参照)。
§Errors
init_scaleが有限かつ正でない、または非正規化数 (稼働中 scale・grad_scaler_from_stateと同一基準)growth_factorが有限かつ1.0より大きくない (1.0以下では成長条件が意味を持たない)backoff_factorが(0.0, 1.0)の範囲外 (1.0以上では backoff が縮小にならず、0.0以下では scale が即座に潰れる)growth_intervalが0(0回連続 clean で成長する定義は 意味を持たない)
Sourcepub fn growth_tracker(&self) -> u64
pub fn growth_tracker(&self) -> u64
直近の backoff/growth からの連続 clean step 数
(growth_interval に到達すると growth し 0 へリセットされる)。
Sourcepub fn scale_loss<'t>(&self, loss: &Var<'t>) -> Result<Var<'t>, AutodiffError>
pub fn scale_loss<'t>(&self, loss: &Var<'t>) -> Result<Var<'t>, AutodiffError>
scale_loss へ現在のスケール値を渡す薄いラッパー。
Sourcepub fn unscale(
&self,
grads: &[&Tensor<f32>],
) -> Result<UnscaleResult, AutodiffError>
pub fn unscale( &self, grads: &[&Tensor<f32>], ) -> Result<UnscaleResult, AutodiffError>
unscale_grads へ現在のスケール値を渡す薄いラッパー。
Sourcepub fn update(&mut self, found_non_finite: bool) -> Result<(), AutodiffError>
pub fn update(&mut self, found_non_finite: bool) -> Result<(), AutodiffError>
この step の unscale 結果(UnscaleResult::found_non_finite)に
基づき、次 step のスケール値を更新する(PyTorch GradScaler.update
相当)。
found_non_finite == true:scale *= backoff_factor・growth_trackerを0へリセットする。backoff 後の scale が0.0・非正規化数・非有限になる場合は以後の unscale が 0 除算・無意味な値になるためErrを返す(fail-closed)。found_non_finite == false:growth_trackerを 1 増やし、growth_intervalに達したらscale *= growth_factorしてgrowth_trackerを0へリセットする。ただし成長後の scale が 非有限になる場合は 成長をスキップして scale を据え置く (growth_trackerは0へリセットする。無限に近い scale へ 増殖させて次 step で即座に backoff させるより、据え置いて 様子を見るほうが安全側の挙動であるため)。
§Errors
backoff の結果 scale が 0.0・非正規化数・非有限になった場合。
Auto Trait Implementations§
impl Freeze for GradScaler
impl RefUnwindSafe for GradScaler
impl Send for GradScaler
impl Sync for GradScaler
impl Unpin for GradScaler
impl UnsafeUnpin for GradScaler
impl UnwindSafe for GradScaler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more