Skip to content

lingbotworld2_t5

LingBot World 2 UMT5 encoder with the released checkpoint's module names.

Classes

fastvideo.models.encoders.lingbotworld2_t5.GELU

Bases: Module

T5 gated GELU approximation used by the source checkpoint.

Methods:

fastvideo.models.encoders.lingbotworld2_t5.GELU.forward
forward(x: Tensor) -> Tensor

Apply the tanh GELU approximation.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Apply the tanh GELU approximation."""
    return 0.5 * x * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0))))

fastvideo.models.encoders.lingbotworld2_t5.LingBotWorld2T5EncoderModel

LingBotWorld2T5EncoderModel(config: LingBotWorld2UMT5Config, prefix: str = '')

Bases: TextEncoder

FastVideo-native LingBot World 2 UMT5 encoder for the released .pth weights.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(self, config: LingBotWorld2UMT5Config, prefix: str = ""):
    super().__init__(config)
    del prefix
    arch = config.arch_config
    self.token_embedding = nn.Embedding(arch.vocab_size, arch.dim)
    self.dropout = nn.Dropout(arch.dropout)
    self.blocks = nn.ModuleList(
        [
            T5SelfAttention(
                arch.dim,
                arch.dim_attn,
                arch.dim_ffn,
                arch.num_heads,
                arch.num_buckets,
                shared_pos=False,
                dropout=arch.dropout,
            )
            for _ in range(arch.num_layers)
        ]
    )
    self.norm = T5LayerNorm(arch.dim)

Methods:

fastvideo.models.encoders.lingbotworld2_t5.LingBotWorld2T5EncoderModel.forward
forward(input_ids: Tensor | None, position_ids: Tensor | None = None, attention_mask: Tensor | None = None, inputs_embeds: Tensor | None = None, output_hidden_states: bool | None = None, **kwargs) -> BaseEncoderOutput

Encode token ids and return source-compatible hidden states.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(
    self,
    input_ids: torch.Tensor | None,
    position_ids: torch.Tensor | None = None,
    attention_mask: torch.Tensor | None = None,
    inputs_embeds: torch.Tensor | None = None,
    output_hidden_states: bool | None = None,
    **kwargs,
) -> BaseEncoderOutput:
    """Encode token ids and return source-compatible hidden states."""
    del position_ids, inputs_embeds, output_hidden_states, kwargs
    assert input_ids is not None
    x = self.dropout(self.token_embedding(input_ids))
    for block in self.blocks:
        x = block(x, attention_mask)
    x = self.dropout(self.norm(x))
    return BaseEncoderOutput(last_hidden_state=x, attention_mask=attention_mask)
fastvideo.models.encoders.lingbotworld2_t5.LingBotWorld2T5EncoderModel.load_weights
load_weights(weights: Iterable[tuple[str, Tensor]]) -> set[str]

Load source .pth weights whose names already match this module.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
    """Load source `.pth` weights whose names already match this module."""
    params_dict = dict(self.named_parameters())
    loaded_params: set[str] = set()
    for name, loaded_weight in weights:
        if name not in params_dict:
            continue
        param = params_dict[name]
        weight_loader = getattr(param, "weight_loader", default_weight_loader)
        weight_loader(param, loaded_weight)
        loaded_params.add(name)
    return loaded_params

fastvideo.models.encoders.lingbotworld2_t5.T5Attention

T5Attention(dim: int, dim_attn: int, num_heads: int, dropout: float = 0.1)

Bases: Module

LingBot World 2 source T5 attention block.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(self, dim: int, dim_attn: int, num_heads: int, dropout: float = 0.1):
    super().__init__()
    assert dim_attn % num_heads == 0
    self.dim = dim
    self.dim_attn = dim_attn
    self.num_heads = num_heads
    self.head_dim = dim_attn // num_heads
    self.q = nn.Linear(dim, dim_attn, bias=False)
    self.k = nn.Linear(dim, dim_attn, bias=False)
    self.v = nn.Linear(dim, dim_attn, bias=False)
    self.o = nn.Linear(dim_attn, dim, bias=False)
    self.dropout = nn.Dropout(dropout)

Methods:

fastvideo.models.encoders.lingbotworld2_t5.T5Attention.forward
forward(x: Tensor, context: Tensor | None = None, mask: Tensor | None = None, pos_bias: Tensor | None = None) -> Tensor

Project QKV, add relative bias/mask, and return attended states.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(
    self,
    x: torch.Tensor,
    context: torch.Tensor | None = None,
    mask: torch.Tensor | None = None,
    pos_bias: torch.Tensor | None = None,
) -> torch.Tensor:
    """Project QKV, add relative bias/mask, and return attended states."""
    context = x if context is None else context
    b, n, c = x.size(0), self.num_heads, self.head_dim
    q = self.q(x).view(b, -1, n, c)
    k = self.k(context).view(b, -1, n, c)
    v = self.v(context).view(b, -1, n, c)
    attn_bias = x.new_zeros(b, n, q.size(1), k.size(1))
    if pos_bias is not None:
        attn_bias += pos_bias
    if mask is not None:
        assert mask.ndim in (2, 3)
        mask = mask.view(b, 1, 1, -1) if mask.ndim == 2 else mask.unsqueeze(1)
        attn_bias.masked_fill_(mask == 0, torch.finfo(x.dtype).min)
    attn = torch.einsum("binc,bjnc->bnij", q, k) + attn_bias
    attn = F.softmax(attn.float(), dim=-1).type_as(attn)
    x = torch.einsum("bnij,bjnc->binc", attn, v)
    return self.dropout(self.o(x.reshape(b, -1, n * c)))

fastvideo.models.encoders.lingbotworld2_t5.T5FeedForward

T5FeedForward(dim: int, dim_ffn: int, dropout: float = 0.1)

Bases: Module

LingBot World 2 source T5 gated feed-forward block.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(self, dim: int, dim_ffn: int, dropout: float = 0.1):
    super().__init__()
    self.dim = dim
    self.dim_ffn = dim_ffn
    self.gate = nn.Sequential(nn.Linear(dim, dim_ffn, bias=False), GELU())
    self.fc1 = nn.Linear(dim, dim_ffn, bias=False)
    self.fc2 = nn.Linear(dim_ffn, dim, bias=False)
    self.dropout = nn.Dropout(dropout)

Methods:

fastvideo.models.encoders.lingbotworld2_t5.T5FeedForward.forward
forward(x: Tensor) -> Tensor

Apply the gated feed-forward projection.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Apply the gated feed-forward projection."""
    x = self.fc1(x) * self.gate(x)
    x = self.dropout(x)
    x = self.fc2(x)
    return self.dropout(x)

fastvideo.models.encoders.lingbotworld2_t5.T5LayerNorm

T5LayerNorm(dim: int, eps: float = 1e-06)

Bases: Module

T5 RMS-style layer norm with source-compatible parameter name.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(self, dim: int, eps: float = 1e-6):
    super().__init__()
    self.dim = dim
    self.eps = eps
    self.weight = nn.Parameter(torch.ones(dim))

Methods:

fastvideo.models.encoders.lingbotworld2_t5.T5LayerNorm.forward
forward(x: Tensor) -> Tensor

Normalize in fp32 and apply the learned scale.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Normalize in fp32 and apply the learned scale."""
    x = x * torch.rsqrt(x.float().pow(2).mean(dim=-1, keepdim=True) + self.eps)
    if self.weight.dtype in (torch.float16, torch.bfloat16):
        x = x.type_as(self.weight)
    return self.weight * x

fastvideo.models.encoders.lingbotworld2_t5.T5RelativeEmbedding

T5RelativeEmbedding(num_buckets: int, num_heads: int, bidirectional: bool, max_dist: int = 128)

Bases: Module

Per-block relative position embedding used by LingBot World 2 UMT5.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(self, num_buckets: int, num_heads: int, bidirectional: bool, max_dist: int = 128):
    super().__init__()
    self.num_buckets = num_buckets
    self.num_heads = num_heads
    self.bidirectional = bidirectional
    self.max_dist = max_dist
    self.embedding = nn.Embedding(num_buckets, num_heads)

Methods:

fastvideo.models.encoders.lingbotworld2_t5.T5RelativeEmbedding.forward
forward(lq: int, lk: int) -> Tensor

Build a relative-position bias tensor for attention logits.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(self, lq: int, lk: int) -> torch.Tensor:
    """Build a relative-position bias tensor for attention logits."""
    device = self.embedding.weight.device
    rel_pos = torch.arange(lk, device=device).unsqueeze(0) - torch.arange(lq, device=device).unsqueeze(1)
    rel_pos = self._relative_position_bucket(rel_pos)
    rel_pos_embeds = self.embedding(rel_pos)
    return rel_pos_embeds.permute(2, 0, 1).unsqueeze(0).contiguous()

fastvideo.models.encoders.lingbotworld2_t5.T5SelfAttention

T5SelfAttention(dim: int, dim_attn: int, dim_ffn: int, num_heads: int, num_buckets: int, shared_pos: bool = False, dropout: float = 0.1)

Bases: Module

One source-compatible UMT5 encoder block.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def __init__(
    self,
    dim: int,
    dim_attn: int,
    dim_ffn: int,
    num_heads: int,
    num_buckets: int,
    shared_pos: bool = False,
    dropout: float = 0.1,
):
    super().__init__()
    self.norm1 = T5LayerNorm(dim)
    self.attn = T5Attention(dim, dim_attn, num_heads, dropout)
    self.norm2 = T5LayerNorm(dim)
    self.ffn = T5FeedForward(dim, dim_ffn, dropout)
    self.pos_embedding = None if shared_pos else T5RelativeEmbedding(num_buckets, num_heads, bidirectional=True)

Methods:

fastvideo.models.encoders.lingbotworld2_t5.T5SelfAttention.forward
forward(x: Tensor, mask: Tensor | None = None, pos_bias: Tensor | None = None) -> Tensor

Run self-attention and feed-forward residual updates.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def forward(self, x: torch.Tensor, mask: torch.Tensor | None = None, pos_bias: torch.Tensor | None = None) -> torch.Tensor:
    """Run self-attention and feed-forward residual updates."""
    e = pos_bias if self.pos_embedding is None else self.pos_embedding(x.size(1), x.size(1))
    x = self._fp16_clamp(x + self.attn(self.norm1(x), mask=mask, pos_bias=e))
    return self._fp16_clamp(x + self.ffn(self.norm2(x)))

Functions:

fastvideo.models.encoders.lingbotworld2_t5.basic_clean

basic_clean(text: str) -> str

Apply LingBot World 2's ftfy/html cleanup before tokenization.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def basic_clean(text: str) -> str:
    """Apply LingBot World 2's ftfy/html cleanup before tokenization."""
    text = ftfy.fix_text(text)
    text = html.unescape(html.unescape(text))
    return text.strip()

fastvideo.models.encoders.lingbotworld2_t5.canonicalize

canonicalize(text: str, keep_punctuation_exact_string: str | None = None) -> str

Normalize prompts with LingBot World 2's optional punctuation handling.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def canonicalize(text: str, keep_punctuation_exact_string: str | None = None) -> str:
    """Normalize prompts with LingBot World 2's optional punctuation handling."""
    text = text.replace("_", " ")
    if keep_punctuation_exact_string:
        text = keep_punctuation_exact_string.join(
            part.translate(str.maketrans("", "", string.punctuation))
            for part in text.split(keep_punctuation_exact_string)
        )
    else:
        text = text.translate(str.maketrans("", "", string.punctuation))
    return " ".join(text.lower().split())

fastvideo.models.encoders.lingbotworld2_t5.lingbotworld2_whitespace_preprocess

lingbotworld2_whitespace_preprocess(prompt: str) -> str

Match the LingBot World 2 source tokenizer's clean='whitespace' behavior.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def lingbotworld2_whitespace_preprocess(prompt: str) -> str:
    """Match the LingBot World 2 source tokenizer's `clean='whitespace'` behavior."""
    return whitespace_clean(basic_clean(prompt))

fastvideo.models.encoders.lingbotworld2_t5.whitespace_clean

whitespace_clean(text: str) -> str

Collapse all whitespace runs to single spaces.

Source code in fastvideo/models/encoders/lingbotworld2_t5.py
def whitespace_clean(text: str) -> str:
    """Collapse all whitespace runs to single spaces."""
    return " ".join(text.split())