File size: 4,836 Bytes
ea1b5d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Support-preserving controls and explicit irreversible masked decoding.

Research sampler is deliberately labeled separately from unmodified upstream.
Random numbers are attached to complete global position arrays at each step.
"""
import math
import torch

def gate(strength, progress, gap, soft=True):
    if not soft:
        return ((strength > 0) & (progress <= strength)).to(strength.dtype)
    # Ramp ends at release threshold. g(0)=0 and limit alpha->0+ is 0 for p>=0.
    return ((strength-progress)/max(gap, 1e-6)).clamp(0, 1) * (strength > 0)

def target_mask(shape, task, device='cuda'):
    mask = torch.zeros(shape, device=device, dtype=torch.float32)
    if len(shape) == 1:
        a,b=task.get('target_time',[.5,.8]); mask[int(a*shape[0]):max(int(b*shape[0]),int(a*shape[0])+1)]=1
    elif len(shape) == 2:
        x0,y0,x1,y1=task.get('target_box',[.25,.25,.75,.75])
        h,w=shape; mask[int(y0*h):max(int(y1*h),int(y0*h)+1),int(x0*w):max(int(x1*w),int(x0*w)+1)]=1
    else:
        raise ValueError(f'Unsupported mask shape {shape}')
    return mask

def categorical_position_sample(logits, generator):
    # Inverse CDF, one uniform draw per global position; no RNG draw per group.
    probs=logits.float().softmax(-1)
    uniforms=torch.rand(probs.shape[:-1],device=probs.device,generator=generator)
    sampled=(probs.cumsum(-1)<uniforms[...,None]).sum(-1).clamp_max(probs.shape[-1]-1)
    return sampled,probs.gather(-1,sampled[...,None]).squeeze(-1)

@torch.no_grad()
def masked_decode(predict, reference, mask, mask_id, steps, job, trace):
    """predict(ids) -> [1,N,V]. Temporary reference is never the commit state."""
    ref=reference.reshape(1,-1).long(); support=mask.reshape(1,-1)>0
    n=ref.shape[1]; device=ref.device
    ids=torch.full_like(ref,mask_id)
    committed=torch.zeros_like(ids,dtype=torch.bool)
    generator=torch.Generator(device=device).manual_seed(job['seed'])
    reference_uniform=torch.rand(ref.shape,device=device,generator=torch.Generator(device=device).manual_seed(job.get('prior_seed',1234)))
    order_generator=torch.Generator(device=device).manual_seed(job['seed']+5678)
    random_priority=torch.rand(ref.shape,device=device,generator=order_generator)
    committed_at=torch.full_like(ids,-1)
    for step in range(steps):
        p=1-math.cos(step/steps*math.pi/2)
        reveal=1-math.cos((step+1)/steps*math.pi/2)
        delay=job.get('commitment_delay',0.)
        reveal=max(0.,(reveal-delay)/(1-delay))
        g=gate(mask.reshape(1,-1)*job['alpha'],p,1/steps,job.get('soft_release',True))
        temporary=(reference_uniform<g)&~committed
        if job['arm']=='logit_bias': temporary=torch.zeros_like(temporary)
        visible=torch.where(temporary,ref,ids)
        fraction=job.get('context_fraction',1.)
        # Explicit context masking intervention; this is not a model-maturity claim.
        visible=torch.where(committed & (random_priority>=fraction),torch.full_like(ids,mask_id),visible)
        logits=predict(visible)
        if logits.ndim != 3 or logits.shape[:2] != ids.shape:
            raise ValueError(f'Expected [1,N,V], got {logits.shape}')
        if job['arm'] in ('guide','logit_bias'):
            # Discrete guide: logit correction, no arithmetic on token IDs.
            lead=torch.arange(n,device=device)[None,:]<support.nonzero()[:,1].min()
            guide_g=gate(torch.full_like(mask.reshape(1,-1),job.get('guide_duration',.05))*lead,p,1/steps)
            if job['arm']=='logit_bias':
                guide_g=support.float()*job['alpha']
            logits=logits.clone()
            logits.scatter_add_(-1,ref[...,None],(guide_g*job.get('guide_amplitude',.1)*8)[...,None].to(logits.dtype))
        sampled,confidence=categorical_position_sample(logits,generator)
        sampled=torch.where(temporary,ref,sampled)
        current=int(committed.sum())
        wanted=n if step==steps-1 else min(n,max(current,int(n*reveal)))
        k=wanted-current
        score=confidence.clone()
        arm=job['arm']
        if arm=='random': score=random_priority.clone()
        elif arm=='target_first': score=support.float()*2+random_priority
        elif arm=='target_last': score=(~support).float()*2+random_priority
        score[committed]=-float('inf')
        newly=torch.zeros_like(committed)
        if k: newly.scatter_(1,score.topk(k,dim=1).indices,True)
        ids=torch.where(newly,sampled,ids)
        committed_at[newly]=step
        committed|=newly
        trace.append({'step':step,'progress':p,'gate_mean':float(g.mean()),
                      'gate_outside_max':float(g[~support].max()) if (~support).any() else 0.,
                      'committed_fraction':float(committed.float().mean()),
                      'confidence_mean':float(confidence.mean()),'newly_committed':k})
    return ids,committed_at