Skip to main content

GradScaler

Struct GradScaler 

Source
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

Source

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 で成長する定義は 意味を持たない)
Source

pub fn scale(&self) -> f32

現在のスケール値。

Source

pub fn growth_tracker(&self) -> u64

直近の backoff/growth からの連続 clean step 数 (growth_interval に到達すると growth し 0 へリセットされる)。

Source

pub fn scale_loss<'t>(&self, loss: &Var<'t>) -> Result<Var<'t>, AutodiffError>

scale_loss へ現在のスケール値を渡す薄いラッパー。

Source

pub fn unscale( &self, grads: &[&Tensor<f32>], ) -> Result<UnscaleResult, AutodiffError>

unscale_grads へ現在のスケール値を渡す薄いラッパー。

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V