TRACE-VLA code snapshot 2026-11-22 11:47 +07: 456 canonical files + SHA256 manifest + sync report (part 2)
ae1ed83 verified Download code/trace_vla/pkg/sync.py from phammminhhieu/trace-vla-code: direct link, hf CLI and curl.
- Browser
- Download file 3.73 kB
-
https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/trace_vla/pkg/sync.py
- Command line
-
hf download hf://phammminhhieu/trace-vla-code/code/trace_vla/pkg/sync.py
-
curl -L -o sync.py https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/trace_vla/pkg/sync.py
3.73 kB
| """SYNC - Context Synchronization and Retrieval (spec v1.1, section 9). | |
| Label-free retrieval. 64 learned context queries read live visual | |
| patches, the text token, the state token, history tokens, and the | |
| DELTA demonstration memory. The initial retrieval mechanism is global | |
| soft cross-attention: the queries can read ALL valid demonstration | |
| tokens (at most 16 windows x 4 tokens = 64 positions), any number of | |
| times, at any temporal position. There is no progress pointer and no | |
| window pre-selection in the v1.1 initial configuration; PULSE does | |
| not control SYNC (spec 10.1). Two blocks of query self-attention, | |
| context cross-attention, and FFN. | |
| """ | |
| import torch | |
| import torch.nn as nn | |
| def _decoder_block(d: int, heads: int, ffn: int): | |
| return nn.ModuleDict({ | |
| "ln1": nn.LayerNorm(d), | |
| "self_attn": nn.MultiheadAttention(d, heads, batch_first=True), | |
| "ln2": nn.LayerNorm(d), | |
| "cross_attn": nn.MultiheadAttention(d, heads, batch_first=True), | |
| "ln3": nn.LayerNorm(d), | |
| "ff": nn.Sequential(nn.Linear(d, ffn), nn.GELU(), nn.Linear(ffn, d)), | |
| }) | |
| def run_decoder_block(blk, x, memory, mem_pad_mask=None): | |
| n = blk["ln1"](x) | |
| a, _ = blk["self_attn"](n, n, n, need_weights=False) | |
| x = x + a | |
| n = blk["ln2"](x) | |
| a, _ = blk["cross_attn"](n, memory, memory, | |
| key_padding_mask=mem_pad_mask, need_weights=False) | |
| x = x + a | |
| x = x + blk["ff"](blk["ln3"](x)) | |
| return x | |
| class Sync(nn.Module): | |
| def __init__(self, cfg): | |
| super().__init__() | |
| self.cfg = cfg | |
| d = cfg.d_model | |
| self.queries = nn.Parameter(torch.zeros(cfg.context_tokens, d)) | |
| nn.init.normal_(self.queries, std=0.02) | |
| # Learned null demo token for text-only operation (spec 9/7.3 | |
| # lineage: video-only and text-only modes must both work). | |
| self.null_demo_token = nn.Parameter(torch.zeros(d)) | |
| nn.init.normal_(self.null_demo_token, std=0.02) | |
| self.blocks = nn.ModuleList( | |
| _decoder_block(d, cfg.n_heads, cfg.ffn_width) | |
| for _ in range(cfg.sync_blocks) | |
| ) | |
| self.final_ln = nn.LayerNorm(d) | |
| def forward(self, live_tokens, text_token, state_token, history_tokens, | |
| history_mask, demo_memory=None, window_mask=None): | |
| """All token tensors are [B, n, 512]. history_mask bool True=valid. | |
| demo_memory [B, S, 4, 512] with window_mask [B, S] bool | |
| True=valid, or None for text-only operation. | |
| Returns context [B, 64, 512]. | |
| """ | |
| b = live_tokens.shape[0] | |
| device = live_tokens.device | |
| parts = [live_tokens, text_token, state_token, history_tokens] | |
| pads = [ | |
| torch.zeros(b, live_tokens.shape[1], dtype=torch.bool, device=device), | |
| torch.zeros(b, 1, dtype=torch.bool, device=device), | |
| torch.zeros(b, 1, dtype=torch.bool, device=device), | |
| ~history_mask, | |
| ] | |
| if demo_memory is not None: | |
| s = demo_memory.shape[1] | |
| demo_tokens = demo_memory.reshape(b, s * 4, -1) | |
| demo_pad = ~window_mask.repeat_interleave(4, dim=1) | |
| demo_tokens = demo_tokens * (~demo_pad).unsqueeze(-1).to( | |
| demo_tokens.dtype) | |
| else: | |
| demo_tokens = self.null_demo_token.view(1, 1, -1).expand(b, 1, -1) | |
| demo_pad = torch.zeros(b, 1, dtype=torch.bool, device=device) | |
| parts.append(demo_tokens) | |
| pads.append(demo_pad) | |
| memory = torch.cat(parts, dim=1) | |
| mem_pad = torch.cat(pads, dim=1) | |
| q = self.queries[None].expand(b, -1, -1) | |
| for blk in self.blocks: | |
| q = run_decoder_block(blk, q, memory, mem_pad) | |
| return self.final_ln(q) | |