pub struct TransformerBlock { /* private fields */ }
Expand description
[Listing 4.6] The transformer block component of GPT
Implementations§
Source§impl TransformerBlock
impl TransformerBlock
Sourcepub fn new(cfg: Config, vb: VarBuilder<'_>) -> Result<Self>
pub fn new(cfg: Config, vb: VarBuilder<'_>) -> Result<Self>
Creates a new TransformerBlock
use candle_core::{Device, DType};
use candle_nn::{VarBuilder, VarMap};
use llms_from_scratch_rs::listings::ch04::{Config, TransformerBlock};
let dev = Device::cuda_if_available(0).unwrap();
let varmap = VarMap::new();
let vb = VarBuilder::from_varmap(&varmap, DType::F32, &dev);
let cfg = Config::gpt_sm_test();
let transformer_block = TransformerBlock::new(cfg, vb.pp("transformer")).unwrap();
pub fn from_fields( att: MultiHeadAttention, ff: FeedForward, norm1: LayerNorm, norm2: LayerNorm, drop_shortcut: Dropout, ) -> Result<Self>
Sourcepub fn forward(&self, xs: &Tensor) -> Result<Tensor>
pub fn forward(&self, xs: &Tensor) -> Result<Tensor>
Manual implementation of forward
Note: that blanket implementation of ModuleT
when a type implements
Module
prevents having forward
being overrided. Thus, this type
is ModuleT
but technicall not Module
.
pub fn att(&self) -> &MultiHeadAttention
pub fn ff(&self) -> &FeedForward
pub fn norm1(&self) -> &LayerNorm
pub fn norm2(&self) -> &LayerNorm
pub fn drop_shortcut(&self) -> &Dropout
Trait Implementations§
Source§impl Clone for TransformerBlock
impl Clone for TransformerBlock
Source§fn clone(&self) -> TransformerBlock
fn clone(&self) -> TransformerBlock
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for TransformerBlock
impl Debug for TransformerBlock
Auto Trait Implementations§
impl Freeze for TransformerBlock
impl !RefUnwindSafe for TransformerBlock
impl Send for TransformerBlock
impl Sync for TransformerBlock
impl Unpin for TransformerBlock
impl !UnwindSafe for TransformerBlock
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> 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> ⓘ
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 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> ⓘ
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