当前位置: 首页 > news >正文

Kronos解析

模型结构

<bound method Module.parameters of Kronos(
(token_drop): Dropout(p=0.0, inplace=False)
(embedding): HierarchicalEmbedding(
(emb_s1): Embedding(1024, 832)
(emb_s2): Embedding(1024, 832)
(fusion_proj): Linear(in_features=1664, out_features=832, bias=True)
)
(time_emb): TemporalEmbedding(
(minute_embed): Embedding(60, 832)
(hour_embed): Embedding(24, 832)
(weekday_embed): Embedding(7, 832)
(day_embed): Embedding(32, 832)
(month_embed): Embedding(13, 832)
)
(transformer): ModuleList(
(0-11): 12 x TransformerBlock(
(norm1): RMSNorm()
(self_attn): MultiHeadAttentionWithRoPE(
(q_proj): Linear(in_features=832, out_features=832, bias=True)
(k_proj): Linear(in_features=832, out_features=832, bias=True)
(v_proj): Linear(in_features=832, out_features=832, bias=True)
(out_proj): Linear(in_features=832, out_features=832, bias=True)
(rotary): RotaryPositionalEmbedding()
(resid_dropout): Dropout(p=0.2, inplace=False)
)
(norm2): RMSNorm()
(ffn): FeedForward(
(w1): Linear(in_features=832, out_features=2048, bias=False)
(w3): Linear(in_features=832, out_features=2048, bias=False)
(w2): Linear(in_features=2048, out_features=832, bias=False)
(ffn_dropout): Dropout(p=0.2, inplace=False)
)
)
)
(norm): RMSNorm()
(dep_layer): DependencyAwareLayer(
(cross_attn): MultiHeadCrossAttentionWithRoPE(
(q_proj): Linear(in_features=832, out_features=832, bias=True)
(k_proj): Linear(in_features=832, out_features=832, bias=True)
(v_proj): Linear(in_features=832, out_features=832, bias=True)
(out_proj): Linear(in_features=832, out_features=832, bias=True)
(rotary): RotaryPositionalEmbedding()
(resid_dropout): Dropout(p=0.0, inplace=False)
)
(norm): RMSNorm()
)
(head): DualHead(
(proj_s1): Linear(in_features=832, out_features=1024, bias=True)
(proj_s2): Linear(in_features=832, out_features=1024, bias=True)
)
)>

def forward(self, s1_ids, s2_ids, stamp=None, padding_mask=None, use_teacher_forcing=False, s1_targets=None):

输入 token后的 s1_ids, s2_ids shape为 [1,400] [1,400]

x = self.embedding([s1_ids, s2_ids])

HierarchicalEmbedding

token_ids (torch.Tensor): Composite token IDs of shape [batch_size, seq_len] or [N], each in range [0, 2^(s1_bits + s2_bits) - 1]. 2^(s1_bits + s2_bits) - 1 这个哪里来的? token我找找
BSQuantizer
def bits_to_indices(self, bits): bits = (bits >= 0).to(torch.long) indices = 2 ** torch.arange( 0, bits.shape[-1], 1, dtype=torch.long, device=bits.device, ) return (bits * indices).sum(-1)

bits_to_indices(bits) ∈ [0, 2^N − 1]

s1_emb = self.emb_s1(s1_ids) * math.sqrt(self.d_model) s2_emb = self.emb_s2(s2_ids) * math.sqrt(self.d_model) return self.fusion_proj(torch.cat([s1_emb, s2_emb], dim=-1))
if stamp is not None: time_embedding = self.time_emb(stamp) x = x + time_embedding
TemporalEmbedding
x = self.token_drop(x)
for layer in self.transformer: x = layer(x, key_padding_mask=padding_mask) x = self.norm(x)
s1_logits = self.head(x)
DualHead
if use_teacher_forcing: sibling_embed = self.embedding.emb_s1(s1_targets) else: s1_probs = F.softmax(s1_logits.detach(), dim=-1) sample_s1_ids = torch.multinomial(s1_probs.view(-1, self.s1_vocab_size), 1).view(s1_ids.shape) sibling_embed = self.embedding.emb_s1(sample_s1_ids) x2 = self.dep_layer(x, sibling_embed, key_padding_mask=padding_mask) # Dependency Aware Layer: Condition on s1 embeddings 这个DependencyAwareLayer跨注意力(cross-attention)让一个子表示(sibling/subtoken)去感知并注入主序列 hidden states 的依赖信息,从而显式建模不同子表示之间的结构依赖关系。 s2_logits = self.head.cond_forward(x2) return s1_logits, s2_logits
计算损失 def compute_loss(self, s1_logits, s2_logits, s1_targets, s2_targets, padding_mask=None): if padding_mask is not None: valid_mask = (padding_mask == 0) s1_logits = s1_logits[valid_mask] s2_logits = s2_logits[valid_mask] s1_targets = s1_targets[valid_mask] s2_targets = s2_targets[valid_mask] ce_s1 = F.cross_entropy(s1_logits, s1_targets) ce_s2 = F.cross_entropy(s2_logits, s2_targets) else: ce_s1 = F.cross_entropy(s1_logits.reshape(-1, self.vocab_s1), s1_targets.reshape(-1)) ce_s2 = F.cross_entropy(s2_logits.reshape(-1, self.vocab_s2), s2_targets.reshape(-1)) ce_loss = (ce_s1 + ce_s2) / 2 return ce_loss, ce_s1, ce_s2
decode_s1
def decode_s1(self, s1_ids, s2_ids, stamp=None, padding_mask=None): """ Decodes only the s1 tokens. This method performs a forward pass to predict only s1 tokens. It returns the s1 logits and the context representation from the Transformer, which can be used for subsequent s2 decoding. Args: s1_ids (torch.Tensor): Input tensor of s1 token IDs. Shape: [batch_size, seq_len] s2_ids (torch.Tensor): Input tensor of s2 token IDs. Shape: [batch_size, seq_len] stamp (torch.Tensor, optional): Temporal stamp tensor. Shape: [batch_size, seq_len]. Defaults to None. padding_mask (torch.Tensor, optional): Mask for padding tokens. Shape: [batch_size, seq_len]. Defaults to None. Returns: Tuple[torch.Tensor, torch.Tensor]: - s1 logits: Logits for s1 token predictions. Shape: [batch_size, seq_len, s1_vocab_size] - context: Context representation from the Transformer. Shape: [batch_size, seq_len, d_model] """ x = self.embedding([s1_ids, s2_ids]) if stamp is not None: time_embedding = self.time_emb(stamp) x = x + time_embedding x = self.token_drop(x) for layer in self.transformer: x = layer(x, key_padding_mask=padding_mask) x = self.norm(x) s1_logits = self.head(x) return s1_logits, x
decode_s2
def decode_s2(self, context, s1_ids, padding_mask=None): """ Decodes the s2 tokens, conditioned on the context and s1 tokens. This method decodes s2 tokens based on a pre-computed context representation (typically from `decode_s1`) and the s1 token IDs. It uses the dependency-aware layer and the conditional s2 head to predict s2 tokens. Args: context (torch.Tensor): Context representation from the transformer (output of decode_s1). Shape: [batch_size, seq_len, d_model] s1_ids (torch.torch.Tensor): Input tensor of s1 token IDs. Shape: [batch_size, seq_len] padding_mask (torch.Tensor, optional): Mask for padding tokens. Shape: [batch_size, seq_len]. Defaults to None. Returns: torch.Tensor: s2 logits. Shape: [batch_size, seq_len, s2_vocab_size] """ sibling_embed = self.embedding.emb_s1(s1_ids) x2 = self.dep_layer(context, sibling_embed, key_padding_mask=padding_mask) return self.head.cond_forward(x2)
http://www.cnnetsun.cn/news/64626.html

相关文章:

  • AutoGPT提示词工程技巧:提升任务拆解准确性
  • Stable Diffusion AIGC 视觉设计实战教程之 07-图生图
  • 当毕业论文不再是“一个人的深夜战场”:一位研究生眼中的AI科研协作者如何重塑写作流程
  • 统计提交svn代码行数,文件以及文档
  • 解锁学术新次元:书匠策AI科研工具为毕业论文注入智慧动能
  • GPT-5.2全面解析:AI“打工能力“大提升,程序员职场必备技能
  • vue-springboot基于Java医院药品管理系统的设计与实现_8z88u88g
  • 深圳 CNC 加工哪家强?慧闻智造!精密零件加工的靠谱专家
  • Java中高级面试题详解(十五):彻底搞懂 Spring Boot 启动流程与扩展点,别再只会写 main 方法!
  • CTF 解题核心思维 + 新手入门全攻略
  • 2026PCB产业高端化浪潮与慕尼黑上海电子展的连接枢纽
  • 揭秘Web组件的隐形守护者:影子DOM如何彻底改变前端开发格局!
  • AI基于Springboot的图书馆在线占座系统_s58324g1
  • 从零构建Agent:大模型智能代理的六步落地指南!
  • 股票历史分时BOLL数据之Python、Java等多种主流语言实例代码演示通过股票数据接口获取数据
  • 25 岁转行不迷茫!网安工程师手把手带学,入门到精通
  • springboot个人任务管理系统-计算机毕业设计源码63521
  • 别瞎学了!2025 网安工程师入门全流程,零基础也能会,收藏即上岸
  • 把AI大模型想象成一个“超级猜词游戏”!非专业也能看懂的工作原理,原来这么简单!
  • 企业级智能体终极指南!从定义到落地,一篇彻底解决你的所有疑问!
  • AI大乱斗!当GPT-5.2遇上Claude-4.5-opus,谁会先“认怂”?史上最硬核模型PK赛!
  • 如何实现员工网站管控?这六款软件来帮您管理员工
  • 护网蓝队初级岗位薪资真相:从 0 学网安,小白参与护网也能日入 2000+
  • 【商城系统】
  • 商城系统的开发语言选择
  • 电脑配置路由,如何选择最适合的方案?
  • 哪些企业适合适用黄金专线宽带?
  • 计算机毕业设计springboot基于spring+vue的在线考试系统 基于 Spring Boot 和 Vue.js 的在线考试平台设计与实现 Spring Boot + Vue 技术栈构建的在线
  • Docker网络【20251215】003篇
  • 一张学术海报10分钟搞定:PPT手把手攻略+97套免抠素材随领