Skip to main content

Generator

Struct Generator 

Source
pub struct Generator { /* private fields */ }
Expand description

プロセスグローバルな RNG(manual_seed/with_global_rng)とは 完全に独立した状態を持つ乱数源(PyTorch torch.Generator 相当。 イシュー #2156・親 #2131)。

用途は「グローバル状態を汚さない再現可能なサンプリング」。 Generator::new(seed) と manual_seed(seed) の直後に グローバル自由関数(bernoulli/multinomial/normal)を 呼んだ結果は bit 完全一致する(いずれも Xorshift64Star::new(seed) から始まるため。crates/autodiff/tests/ random_parity.rs で固定)。

&mut self で操作するためロックは不要(呼び出し元が所有権・借用で 排他制御を担う。with_global_rng の Mutex とは異なる設計)。

§用途限定

xorshift64* は暗号学的に安全な PRNG ではない(モジュール冒頭コメント 参照。OWASP A02)。鍵・トークン生成などセキュリティ用途には使用 しないこと。

Implementations§

Source§

impl Generator

Source

pub fn new(seed: u64) -> Generator

seed から新規の独立した RNG 状態を作る (Xorshift64Star::new(seed) と同じ 0 補正を継承する)。

Source

pub fn manual_seed(&mut self, seed: u64)

状態を seed からやり直す(manual_seed の Generator 版。 グローバル状態には一切触れない)。

Source

pub fn initial_seed(&self) -> u64

Self::new/Self::manual_seed に渡された、補正前のシード値を 返す(PyTorch Generator.initial_seed() と同じ意味論)。

Source

pub fn bernoulli( &mut self, probs: &Tensor<f32>, ) -> Result<Tensor<f32>, RngError>

bernoulli の Generator 版。グローバル RNG は消費しない。

Source

pub fn normal( &mut self, mean: f32, std: f32, shape: &[usize], ) -> Result<Tensor<f32>, RngError>

normal の Generator 版。グローバル RNG は消費しない。

Source

pub fn multinomial( &mut self, weights: &Tensor<f32>, num_samples: usize, replacement: bool, ) -> Result<Tensor<i32>, RngError>

multinomial の Generator 版。グローバル RNG は消費しない。 検証本体は自由関数版と validate_multinomial を共有する (イシュー #2156。ドリフト防止)。

Trait Implementations§

Source§

impl Clone for Generator

Source§

fn clone(&self) -> Generator

内部状態を複製する(Xorshift64Star 自体に Clone を derive しない設計判断を保つため、フィールド単位で複製する。同一系列を 独立に分岐させる用途——例えば複数の Generator を並行してドリフト なく使い比べるテスト——を想定する)。

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Generator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

内部状態(Xorshift64Star の state)は表示しない。initial_seed のみを表示する(内部実装詳細を Debug 出力経由で公開面へ漏らさない ための最小限の情報開示)。

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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