pub struct Transformer { /* private fields */ }Expand description
Neural Transformer language model matching nn.py.
Implementations§
Source§impl Transformer
impl Transformer
Sourcepub fn new(
config: &ModelConfig,
device: &Device,
rng: &mut DeterministicRng,
) -> Result<Self, CodecError>
pub fn new( config: &ModelConfig, device: &Device, rng: &mut DeterministicRng, ) -> Result<Self, CodecError>
Initializes a new Transformer with deterministic weights on device.
§Errors
Returns CodecError on invalid model configuration or tensor allocation error.
Sourcepub fn from_prior(
config: &ModelConfig,
archive_path: impl AsRef<Path>,
vocab_path: impl AsRef<Path>,
seed: u64,
device: &Device,
rng: &mut DeterministicRng,
) -> Result<(Self, PriorLoadInfo), CodecError>
pub fn from_prior( config: &ModelConfig, archive_path: impl AsRef<Path>, vocab_path: impl AsRef<Path>, seed: u64, device: &Device, rng: &mut DeterministicRng, ) -> Result<(Self, PriorLoadInfo), CodecError>
Constructs a Transformer and immediately loads a QPI1 prior archive.
For loop-untying priors (where every target block is copied from the archive),
parameter buffers are allocated zeroed instead of sampling discarded normal
initialization weights. For depth-growth priors (where unmapped layers retain
random initialization), full deterministic initialization is performed via rng.
§Errors
Returns CodecError if reading, validating, or loading the prior archive fails.
Sourcepub fn forward(
&self,
idx: &Tensor,
output_slice: OutputSlice,
mode: &mut ForwardMode<'_>,
compute_dtype: DType,
) -> Result<Tensor, CodecError>
pub fn forward( &self, idx: &Tensor, output_slice: OutputSlice, mode: &mut ForwardMode<'_>, compute_dtype: DType, ) -> Result<Tensor, CodecError>
Runs a forward pass over token IDs idx ([batch, seq], U32), returning logits.
§Errors
Returns CodecError if tensor shapes, sequence lengths, or Candle operations fail.
Sourcepub fn named_vars(&self) -> &[(String, Var)]
pub fn named_vars(&self) -> &[(String, Var)]
Returns all unique trainable (name, Var) pairs in canonical order.
Sourcepub fn param_counts(&self) -> (usize, usize)
pub fn param_counts(&self) -> (usize, usize)
Returns (total_params, non_embedding_params).
Sourcepub fn state_shapes(&self) -> BTreeMap<String, Vec<usize>>
pub fn state_shapes(&self) -> BTreeMap<String, Vec<usize>>
Returns the expected parameter shape map matching PyTorch state_dict().
Sourcepub fn state_sha256(&self) -> Result<String, CodecError>
pub fn state_sha256(&self) -> Result<String, CodecError>
Computes a deterministic SHA-256 digest over all parameter names, shapes, dtypes, and f32 bytes.
§Errors
Returns CodecError::Candle if reading parameter tensors from device fails.
Sourcepub fn load_prior(
&mut self,
archive_path: impl AsRef<Path>,
vocab_path: impl AsRef<Path>,
seed: u64,
) -> Result<PriorLoadInfo, CodecError>
pub fn load_prior( &mut self, archive_path: impl AsRef<Path>, vocab_path: impl AsRef<Path>, seed: u64, ) -> Result<PriorLoadInfo, CodecError>
Loads and validates a QPI1 depth-growth or loop-untying prior archive.
§Errors
Returns CodecError if reading, validating, or transferring tensors fails.
Sourcepub fn export_tensors(
&self,
out: &mut HashMap<String, Tensor>,
) -> Result<(), CodecError>
pub fn export_tensors( &self, out: &mut HashMap<String, Tensor>, ) -> Result<(), CodecError>
Exports all trainable parameters into out under "model.<name>" keys for safetensors checkpoints.
§Errors
Returns CodecError::Candle if converting tensors to contiguous CPU tensors fails.
Sourcepub fn import_tensors(
&mut self,
tensors: &HashMap<String, Tensor>,
) -> Result<(), CodecError>
pub fn import_tensors( &mut self, tensors: &HashMap<String, Tensor>, ) -> Result<(), CodecError>
Restores trainable parameters from checkpoint tensors loaded by load_checkpoint.
§Errors
Returns CodecError if any expected parameter is missing or has an incompatible shape.
Sourcepub fn weight_norm(&self) -> Result<f64, CodecError>
pub fn weight_norm(&self) -> Result<f64, CodecError>
Computes global L2 parameter norm across all trainable parameters.
§Errors
Returns CodecError::Candle on tensor read failures.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Transformer
impl !UnwindSafe for Transformer
impl Freeze for Transformer
impl Send for Transformer
impl Sync for Transformer
impl Unpin for Transformer
impl UnsafeUnpin for Transformer
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
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
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