尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
5. fastwam 模型 video expert pre dit过程
代码原始代码defpre_dit(self,x:torch.Tensor,timestep:torch.Tensor,context:torch.Tensor,context_mask:Optional[torch.Tensor]None,action:Optional[torch.Tensor]None,fuse_vae_embedding_in_latents:boolFalse,control_camera_latents_input:Optional[torch.Tensor]None,)-Dict[str,Any]:x,timestep,context_maskself._validate_forward_inputs(xx,timesteptimestep,contextcontext,context_maskcontext_mask,actionaction,)batch_sizex.shape[0]patch_hint(self.patch_size[1])patch_wint(self.patch_size[2])ifx.shape[3]%patch_h!0orx.shape[4]%patch_w!0:raiseValueError(Latent spatial shape must be divisible by DiT patch size, fgot HxW({x.shape[3]},{x.shape[4]}), patch({patch_h},{patch_w}))tokens_per_frame(x.shape[3]//patch_h)*(x.shape[4]//patch_w)ifself.seperated_timestepandfuse_vae_embedding_in_latents:ifnothasattr(self,patch_size)orlen(self.patch_size)3:raiseValueError(fInvalid dit.patch_size:{getattr(self,patch_size,None)})token_timestepstorch.ones((batch_size,x.shape[2],tokens_per_frame),dtypetimestep.dtype,devicetimestep.device,)*timestep.view(batch_size,1,1)token_timesteps[:,0,:]0token_timestepstoken_timesteps.reshape(batch_size,-1)token_t_embsinusoidal_embedding_1d(self.freq_dim,token_timesteps.reshape(-1))tself.time_embedding(token_t_emb).reshape(batch_size,-1,self.hidden_dim)t_modself.time_projection(t).unflatten(2,(6,self.hidden_dim))else:raiseNotImplementedError(Only support seperated_timestep with fuse_vae_embedding_in_latents for now.)tself.time_embedding(sinusoidal_embedding_1d(self.freq_dim,timestep))t_modself.time_projection(t).unflatten(1,(6,self.hidden_dim))print(patchify前的x:,x.shape)xself.patchify(x,control_camera_latents_inputcontrol_camera_latents_input)print(patchify后的x:,x.shape)f,h,wx.shape[2:]print(text_embedding前的维度:,context.shape)contextself.text_embedding(context)# (B, L, dim)print(text_embedding后的维度:,context.shape)context_lencontext.shape[1]ifself.action_conditionedandactionisnotNone:action_lenaction.shape[1]action_embself.action_embedding(action)# (B, action_len, dim)action_pos_embedsinusoidal_embedding_1d(self.hidden_dim,torch.arange(action_len,deviceaction_emb.device))# (action_len, dim)action_embaction_embaction_pos_embed.unsqueeze(0)# (B, action_len, dim)contexttorch.cat([context,action_emb],dim1)# (B, context_len action_len, dim)# new masknum_temporal_groupsf-1# first latent frame do not attend to actionsifnum_temporal_groups0:raiseValueError(Action-conditioned context mask requires at least 2 latent frames when action is provided.)assertaction_emb.shape[1]%num_temporal_groups0,\fAction embedding length{action_emb.shape[1]}must be divisible by number of temporal groups{num_temporal_groups}# Each latent frame (from the 2nd one) attends to the corresponding group of action tokensaction_group_maskcreate_group_causal_attn_mask(num_temporal_groupsnum_temporal_groups,num_query_per_grouptokens_per_frame,num_key_per_groupaction_len//num_temporal_groups,modeself.action_group_causal_mask_mode,).to(context.device)# ((f-1)*tokens_per_frame, action_len)seq_lenf*h*w# query lengthfinal_context_masktorch.zeros((batch_size,seq_len,context.shape[1]),dtypetorch.bool,devicecontext.device)# (B, seq_len, L action_len)# all latent frames attend to text tokensfinal_context_mask[:,:,:context_len]context_mask.unsqueeze(1).expand(-1,seq_len,-1)# (B, seq_len, L)# latent frames from the 2nd one attend to action tokensfinal_context_mask[:,tokens_per_frame:,context_len:]action_group_mask.unsqueeze(0).expand(batch_size,-1,-1)# (B, seq_len, action_len)context_maskfinal_context_maskelifself.action_conditionedandactionisNone:iff!1:raiseValueError(Action-conditioned model requires action unless running single-frame text-only mode with num_latent_frames1.)context_maskcontext_mask.unsqueeze(1).expand(-1,f*h*w,-1)# (B, seq_len, L)else:context_maskcontext_mask.unsqueeze(1).expand(-1,f*h*w,-1)# (B, seq_len, L)x_tokensrearrange(x,b c f h w - b (f h w) c).contiguous()freqstorch.cat([self.freqs[0][:f].view(f,1,1,-1).expand(f,h,w,-1),self.freqs[1][:h].view(1,h,1,-1).expand(f,h,w,-1),self.freqs[2][:w].view(1,1,w,-1).expand(f,h,w,-1)],dim-1).reshape(f*h*w,1,-1).to(x_tokens.device)return{tokens:x_tokens,freqs:freqs,t:t,t_mod:t_mod,context:context,context_mask:context_mask,meta:{grid_size:(f,h,w),tokens_per_frame:tokens_per_frame,batch_size:batch_size,},}运行结果--------before video_expert.pre_dit-------------- first_frame_latents:(1,48,1,14,28)torch.bfloat16 cuda:0 timestep_video:(1,)torch.bfloat16 cuda:0 context:(1,129,4096)torch.bfloat16 cuda:0 context_mask:(1,129)torch.bool cuda:0 fuse_flag: True patchify前的x: torch.Size([1,48,1,14,28])patchify后的x: torch.Size([1,3072,1,7,14])text_embedding前的维度: torch.Size([1,129,4096])text_embedding后的维度: torch.Size([1,129,3072])--------after video_expert.pre_dit--------------[Processing]Key: tokens|Shape: torch.Size([1,98,3072])|Dtype: torch.bfloat16[Processing]Key: freqs|Shape: torch.Size([98,1,64])|Dtype: torch.complex128[Processing]Key: t|Shape: torch.Size([1,98,3072])|Dtype: torch.bfloat16[Processing]Key: t_mod|Shape: torch.Size([1,98,6,3072])|Dtype: torch.bfloat16[Processing]Key: context|Shape: torch.Size([1,129,3072])|Dtype: torch.bfloat16[Processing]Key: context_mask|Shape: torch.Size([1,98,129])|Dtype: torch.bool[Processing]Key: meta|Type:classdict流程图pre_dit inputsx (first_frame_latents): (1,48,1,14,28) bf16 cudatimestep: (1,) bf16 cudacontext: (1,129,4096) bf16 cudacontext_mask: (1,129) bool cudafuse_vae_embedding_in_latents: TrueValidate shapes_validate_forward_inputsCompute tokens_per_framepatch_size(1,2,2)H14,W28patchify grid: h7,w14tokens_per_frame 7*14 98Separated timestep (per-token)seperated_timestepTrue fuseTrueBuild token_timesteps:shape (B,F,tokens_per_frame)(1,1,98)set frame0 timestep0flatten - (1,98)sinusoidal_embedding - token_t_emb(time_embedding)- t: (1,98,3072)(time_projection)- t_mod: (1,98,6,3072)Patchify video latentsBefore: x (1,48,1,14,28)After: x (1,3072,1,7,14)Flatten to token sequencex_tokens rearrange(b,c,f,h,w)-(b,f*h*w,c)tokens: (1,98,3072)Text embeddingtext_embedding: 4096 - 3072context: (1,129,4096) - (1,129,3072)Expand context_mask to per-querycontext_mask: (1,129)- (1,seq_len,129)seq_len f*h*w 98context_mask: (1,98,129)Build rotary freqsfreqs: (seq_len,1,64) (98,1,64) complex128pre_dit outputs dictReturn payload(tokens, freqs, t, t_mod,context, context_mask,meta{grid_size(1,7,14), tokens_per_frame98, batch_size1})
RELATED

相关推荐

在OpenCloudOS上部署OpenClaw与Ollama:构建自主可控的本地大模型服务

在OpenCloudOS上部署OpenClaw与Ollama:构建自主可控的本地大模型服务

1. 项目概述:为什么要在OpenCloudOS上折腾OpenClaw和Ollama?最近在折腾本地大模型应用,发现了一个挺有意思的组合:在OpenCloudOS上部署OpenClaw来配置和管理本地的Ollama服务。这听起来可能有点绕,但如果你也受够了公有…

📅 2026/8/9 10:55:44
稳流稳压系统在48小时老化测试中的工程实践与自动化实现

稳流稳压系统在48小时老化测试中的工程实践与自动化实现

在实际硬件开发、嵌入式系统测试和电源管理项目中,我们经常需要对设备进行长时间、高强度的稳定性验证。“48小时老化”就是一种典型的压力测试方法,它要求被测设备在特定负载下连续运行48小时,以暴露早期失效、温升异常、性能漂移等潜在问题…

📅 2026/8/9 3:13:10
Verilog组合逻辑电路设计:从门级到行为级的编码实践与陷阱规避

Verilog组合逻辑电路设计:从门级到行为级的编码实践与陷阱规避

1. 组合逻辑电路与Verilog描述入门在数字电路设计的领域里,Verilog HDL(硬件描述语言)是我们将抽象逻辑转化为实际电路图的桥梁。而组合逻辑电路,作为数字系统的基石,其描述是每个硬件工程师必须掌握的核心技能。简单来…

📅 2026/8/9 15:10:04
MORE NEWS

更多资讯

📰

Python上位机开发实战:PyCharm+PyQt5工业级配置与避坑指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

技术复盘:多智能体上下文压缩与KV缓存命中取舍,TaoToken统一Key配置实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

WordPress MCP 实战:用 TaoToken 统一 Key 打通 AI 代理与向量数据库的 RAG 配置

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

Appian 引入 MCP 协议并与 Snowflake 合作:为智能体提供强管控能力的配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

Cursor IDE Rules / Skills / Subagents 前端项目配置全指南:TaoToken 统一 Key 接入 settings.json 骨架

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

OpenClaw 配 TaoToken:settings.json 骨架与报错排查

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬