news 2026/7/18 3:24:52

Kronos解析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/16 18:43:09

朱炳仁、朱军岷14件铜艺捐赠入藏中国国家博物馆

2025年12月12日&#xff0c;朱炳仁、朱军岷作品捐赠入藏仪式在中国国家博物馆白玉厅隆重举行。这对国内罕见的“一门双国遗”父子传承人&#xff0c;将14件艺术珍品悉数捐赠给国博。这标志着朱炳仁首创的熔铜艺术&#xff0c;自2007年首件作品入藏国博后&#xff0c;历经18年沉…

作者头像 李华
网站建设 2026/7/18 6:18:45

数字化饮食闭环管理新趋势:AI技术如何重塑个性化营养方案

随着健康中国战略的深入推进&#xff0c;企业健康管理正迎来数字化转型升级的重要机遇。在员工健康管理领域&#xff0c;传统的饮食指导方式已难以满足精准化、个性化的需求。在此背景下&#xff0c;数字化饮食闭环管理作为一种创新模式&#xff0c;通过数据采集、智能分析、方…

作者头像 李华
网站建设 2026/7/18 14:29:07

AutoGPT+GPU云服务无限扩展的智能执行能力

AutoGPT 与 GPU 云服务&#xff1a;构建无限扩展的智能执行系统 在生成式 AI 的浪潮中&#xff0c;我们正经历一场从“对话工具”到“自主代理”的深刻变革。过去&#xff0c;用户需要一步步指导 AI 完成任务——“写一段介绍”、“搜索某项数据”、“总结这篇文档”。而今天&a…

作者头像 李华
网站建设 2026/7/18 3:02:50

Vim 标签页(Tab)操作详解

Vim 标签页&#xff08;Tab&#xff09;操作详解&#x1f4da; 标签页基础1. 创建标签页:tabnew [文件名] " 在新标签页打开文件 :tabedit [文件名] " 同上&#xff0c;在新标签页编辑文件 :tabe [文件名] " 简写形式" 从命令行直接…

作者头像 李华
网站建设 2026/7/16 14:12:16

学术突围新路径:书匠策AI如何成为毕业论文的“隐形导师“?

在高校图书馆的深夜灯光下&#xff0c;总有一群人对着电脑屏幕抓耳挠腮&#xff1a;文献综述像一团乱麻&#xff0c;实验数据在表格里打架&#xff0c;参考文献格式总在APA和GB之间反复横跳。这些场景&#xff0c;构成了无数毕业生挥之不去的"论文焦虑"。而今&#x…

作者头像 李华
网站建设 2026/7/17 16:38:35

K8s-1.29.2二进制安装-第一章

从本章来完成安装k8s学习的最后一种安装方式(二进制安装)&#xff0c;系统使用Rockly9.6&#xff0c;K8s版本1.29.2&#xff0c;一共会分成几张进行编写。1. 安装Topo2.环境初始化 1、镜像下载(所有节点) # 官方下载地址 https://rockylinux.org/download # 阿里云镜像下载地址…

作者头像 李华