pub struct ClipTextEncoder<T: Float> {
pub embeddings: ClipTextEmbeddings<T>,
pub encoder: ClipEncoder<T>,
pub final_layer_norm: LayerNorm<T>,
pub config: ClipTextConfig,
/* private fields */
}Expand description
The full SD-1.5 CLIP text encoder. Wraps ClipTextEmbeddings +
ClipEncoder + a final LayerNorm (final_layer_norm).
Mirrors CLIPTextTransformer in transformers. The HF
CLIPTextModel wrapper sits one prefix above
(text_model.embeddings.*, text_model.encoder.*,
text_model.final_layer_norm.*) — see
crate::safetensors_loader::load_clip_text_encoder for the
text_model. strip.
Output is the per-token last_hidden_state [B, S, hidden_size].
SD-1.5 consumes this directly as encoder_hidden_states for the
UNet’s cross-attention (no pooling).
Fields§
§embeddings: ClipTextEmbeddings<T>Token + position embedding sum.
encoder: ClipEncoder<T>12 × ClipEncoderLayer for SD-1.5.
final_layer_norm: LayerNorm<T>Final LayerNorm over the last hidden state.
config: ClipTextConfigFrozen copy of the configuration used to build the module.
Implementations§
Source§impl<T: Float> ClipTextEncoder<T>
impl<T: Float> ClipTextEncoder<T>
Sourcepub fn new(cfg: ClipTextConfig) -> FerrotorchResult<Self>
pub fn new(cfg: ClipTextConfig) -> FerrotorchResult<Self>
Build a randomly-initialized text encoder.
§Errors
Returns the underlying FerrotorchError from any sub-module
constructor.
Sourcepub fn forward_from_ids(&self, input_ids: &[u32]) -> FerrotorchResult<Tensor<T>>
pub fn forward_from_ids(&self, input_ids: &[u32]) -> FerrotorchResult<Tensor<T>>
Run the encoder on a token-id sequence and return the per-token
last_hidden_state [1, S, hidden_size].
input_ids is the verbatim CLIP-BPE token-id vector (length
S). For SD-1.5 the canonical inference call is S = 77
(already-padded with EOS to the max length).
§Errors
FerrotorchError::InvalidArgumentifinput_idsis empty or longer thanmax_position_embeddings.- Propagates downstream Embedding / LayerNorm errors.
Sourcepub fn forward_from_id_tensor(
&self,
ids: &Tensor<T>,
) -> FerrotorchResult<Tensor<T>>
pub fn forward_from_id_tensor( &self, ids: &Tensor<T>, ) -> FerrotorchResult<Tensor<T>>
Run the encoder on a pre-built float-encoded token-id tensor of
shape [S]. Returns [1, S, hidden_size].
ids carries u32 token ids losslessly cast to T
(numeric_cast::cast::<u32, T>). Mirrors what the dump example
reads off disk.
§Errors
Propagates downstream lookup / LayerNorm errors and converts
invalid (negative / NaN / overflow) ids to
FerrotorchError::InvalidArgument.
Sourcepub fn load_hf_state_dict(
&mut self,
hf_state: &StateDict<T>,
strict: bool,
) -> FerrotorchResult<DropReport>
pub fn load_hf_state_dict( &mut self, hf_state: &StateDict<T>, strict: bool, ) -> FerrotorchResult<DropReport>
Load a HuggingFace CLIPTextModel state dict into this module.
Accepts both:
- bare-
text_modellayout (no prefix; what the pin script normalises to). - full
text_model.<rest>prefix (what the upstream HF checkpoint ships).
The HF safetensors also ships a non-parameter buffer we explicitly drop:
text_model.embeddings.position_ids— a[1, max_pos]arange(max_pos)buffer regenerated each forward pass. Recorded in thecrate::safetensors_loader::DropReport.
§Errors
Forwards whatever each sub-module’s load_state_dict returns
(shape mismatch / strict-mode missing key). Strict mode will
surface text_model.embeddings.position_ids and any unknown
key as errors; callers with a full HF checkpoint must pass
strict=false.
Trait Implementations§
Source§impl<T: Float> Module<T> for ClipTextEncoder<T>
impl<T: Float> Module<T> for ClipTextEncoder<T>
Source§fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>>
fn forward(&self, input: &Tensor<T>) -> FerrotorchResult<Tensor<T>>
Module::forward treats input as already-summed embeddings
[1, S, hidden] and only runs the encoder + final LayerNorm.
Real callers should use Self::forward_from_ids /
Self::forward_from_id_tensor.
Source§fn parameters(&self) -> Vec<&Parameter<T>>
fn parameters(&self) -> Vec<&Parameter<T>>
Source§fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>>
fn parameters_mut(&mut self) -> Vec<&mut Parameter<T>>
Source§fn named_parameters(&self) -> Vec<(String, &Parameter<T>)>
fn named_parameters(&self) -> Vec<(String, &Parameter<T>)>
Source§fn is_training(&self) -> bool
fn is_training(&self) -> bool
Source§fn state_dict(&self) -> StateDict<T>
fn state_dict(&self) -> StateDict<T>
Source§fn load_state_dict(
&mut self,
state: &StateDict<T>,
strict: bool,
) -> FerrotorchResult<()>
fn load_state_dict( &mut self, state: &StateDict<T>, strict: bool, ) -> FerrotorchResult<()>
Source§fn to_device(&mut self, device: Device) -> Result<(), FerrotorchError>
fn to_device(&mut self, device: Device) -> Result<(), FerrotorchError>
Source§fn buffers(&self) -> Vec<&Buffer<T>>
fn buffers(&self) -> Vec<&Buffer<T>>
Source§fn buffers_mut(&mut self) -> Vec<&mut Buffer<T>>
fn buffers_mut(&mut self) -> Vec<&mut Buffer<T>>
Source§fn named_buffers(&self) -> Vec<(String, &Buffer<T>)>
fn named_buffers(&self) -> Vec<(String, &Buffer<T>)>
Source§fn as_any(&self) -> Option<&(dyn Any + 'static)>
fn as_any(&self) -> Option<&(dyn Any + 'static)>
Source§fn children(&self) -> Vec<&dyn Module<T>>
fn children(&self) -> Vec<&dyn Module<T>>
Source§fn named_children(&self) -> Vec<(String, &dyn Module<T>)>
fn named_children(&self) -> Vec<(String, &dyn Module<T>)>
Source§fn modules(&self) -> Vec<&dyn Module<T>>where
Self: Sized,
fn modules(&self) -> Vec<&dyn Module<T>>where
Self: Sized,
Source§fn descendants_dyn(&self) -> Vec<&dyn Module<T>>
fn descendants_dyn(&self) -> Vec<&dyn Module<T>>
self in depth-first order. Object-safe.Source§fn named_modules(&self) -> Vec<(String, &dyn Module<T>)>where
Self: Sized,
fn named_modules(&self) -> Vec<(String, &dyn Module<T>)>where
Self: Sized,
""; children paths are joined with ..Source§fn named_descendants_dyn(&self) -> Vec<(String, &dyn Module<T>)>
fn named_descendants_dyn(&self) -> Vec<(String, &dyn Module<T>)>
Source§fn with_forward_hook(
self,
hook: Box<dyn Fn(&Tensor<T>, &Tensor<T>) + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
fn with_forward_hook(
self,
hook: Box<dyn Fn(&Tensor<T>, &Tensor<T>) + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
HookedModule and register a forward hook.
Returns the wrapper paired with a HookHandle that can be used to
remove the hook later. The wrapper implements Module<T> itself, so
it slots into any place the original module did. Mirrors
torch.nn.Module.register_forward_hook.Source§fn with_forward_pre_hook(
self,
hook: Box<dyn Fn(&Tensor<T>) -> Result<Tensor<T>, FerrotorchError> + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
fn with_forward_pre_hook(
self,
hook: Box<dyn Fn(&Tensor<T>) -> Result<Tensor<T>, FerrotorchError> + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
HookedModule and register a forward
pre-hook. See Self::with_forward_hook. Mirrors
torch.nn.Module.register_forward_pre_hook.Source§fn with_backward_hook(
self,
hook: Box<dyn Fn(&Tensor<T>, &Tensor<T>) + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
fn with_backward_hook(
self,
hook: Box<dyn Fn(&Tensor<T>, &Tensor<T>) + Send + Sync>,
) -> (HookedModule<Self, T>, HookHandle)where
Self: Sized,
HookedModule and register a backward hook.
See Self::with_forward_hook. Mirrors
torch.nn.Module.register_backward_hook.Source§fn zero_grad(&self) -> Result<(), FerrotorchError>
fn zero_grad(&self) -> Result<(), FerrotorchError>
None. Read moreSource§fn requires_grad_(&mut self, requires_grad: bool)
fn requires_grad_(&mut self, requires_grad: bool)
requires_grad on every parameter (freeze / unfreeze the
module). Mirrors torch.nn.Module.requires_grad_.Source§fn apply_to_parameters(&mut self, f: &mut dyn FnMut(&mut Parameter<T>))
fn apply_to_parameters(&mut self, f: &mut dyn FnMut(&mut Parameter<T>))
torch.nn.Module.apply for the parameter case (true apply recurses
over all submodules; the recursive form requires &mut dyn Module
which conflicts with this trait’s &mut self borrow). Read moreAuto Trait Implementations§
impl<T> !Freeze for ClipTextEncoder<T>
impl<T> !RefUnwindSafe for ClipTextEncoder<T>
impl<T> Send for ClipTextEncoder<T>
impl<T> Sync for ClipTextEncoder<T>
impl<T> Unpin for ClipTextEncoder<T>
impl<T> UnsafeUnpin for ClipTextEncoder<T>
impl<T> !UnwindSafe for ClipTextEncoder<T>
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
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
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>
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