"""Backbone: decoder-only causal Transformer (Qwen3), pretrained or randomly initialised. We only use it on `inputs_embeds`, so text token embeddings come from its own embedding table (resized to include PhotonTTS control tokens) and audio/speaker/clock inputs are projected in. """ from __future__ import annotations import torch.nn as nn from ..config import BackboneConfig def build_backbone(cfg: BackboneConfig, vocab_size: int) -> nn.Module: from transformers import Qwen3Config, Qwen3Model if cfg.pretrained: model = Qwen3Model.from_pretrained(cfg.pretrained, attn_implementation=cfg.attn_implementation) if model.get_input_embeddings().num_embeddings < vocab_size: model.resize_token_embeddings(vocab_size, mean_resizing=True) else: conf = Qwen3Config( vocab_size=vocab_size, hidden_size=cfg.hidden_size, num_hidden_layers=cfg.num_layers, num_attention_heads=cfg.num_heads, num_key_value_heads=cfg.num_kv_heads, head_dim=cfg.hidden_size // cfg.num_heads, intermediate_size=cfg.intermediate_size, max_position_embeddings=cfg.max_position, tie_word_embeddings=False, ) conf._attn_implementation = cfg.attn_implementation model = Qwen3Model(conf) if cfg.gradient_checkpointing: model.gradient_checkpointing_enable() return model