File size: 10,410 Bytes
a64600b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""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 numbers
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)


def _validate_masked_job(steps, job):
    """Reject misspelled policies and invalid new controls before any prediction."""
    if isinstance(steps, bool) or not isinstance(steps, numbers.Integral) or steps <= 0:
        raise ValueError('steps must be a positive integer')
    arms = {'confidence', 'random', 'target_first', 'target_last',
            'guide', 'logit_bias', 'balanced', 'control_aware'}
    if not isinstance(job.get('arm'), str) or job.get('arm') not in arms:
        raise ValueError(f"Unknown masked arm {job.get('arm')!r}; expected one of {sorted(arms)}")
    schedule = job.get('control_schedule', 'duration')
    if not isinstance(schedule, str) or schedule not in {'duration', 'amplitude'}:
        raise ValueError('control_schedule must be duration or amplitude')
    if not isinstance(job.get('reference_copy', True), bool):
        raise ValueError('reference_copy must be a boolean')
    for key, default, upper, inclusive in (
            ('alpha', None, 1., True), ('authority_weight', .5, 1., True),
            ('context_fraction', 1., 1., True), ('commitment_delay', 0., 1., False)):
        value = job.get(key, default)
        if (isinstance(value, bool) or not isinstance(value, numbers.Real)
                or not math.isfinite(value) or value < 0
                or (value > upper if inclusive else value >= upper)):
            end = ']' if inclusive else ')'
            raise ValueError(f'{key} must be a finite number in [0, {upper}{end}')


def _quota_commit(score, support, committed, wanted):
    """Reveal floor(T * wanted / N) target tokens cumulatively, when feasible.

    A proportional target quota is shared by balanced and control_aware. Clamp
    the next target increment to the available capacity of both regions; this
    also makes the final step reveal every remaining token. The reveal count
    and number of predictor calls are unchanged from the legacy cosine policy.
    """
    current = int(committed.sum())
    k = wanted - current
    newly = torch.zeros_like(committed)
    if not k:
        return newly
    target_done = int((committed & support).sum())
    target_left = int((~committed & support).sum())
    outside_left = int((~committed & ~support).sum())
    desired = int(support.sum()) * wanted // support.numel()
    target_k = max(max(0, k - outside_left),
                   min(min(k, target_left), desired - target_done))
    for region, count in ((support, target_k), (~support, k - target_k)):
        if count:
            region_score = score.masked_fill(committed | ~region, -float('inf'))
            newly.scatter_(1, region_score.topk(count, dim=1).indices, True)
    return newly

@torch.no_grad()
def masked_decode(predict, reference, mask, mask_id, steps, job, trace):
    """predict(ids) -> [1,N,V]; defaults reproduce the legacy sampler.

    duration controls the reference release time. amplitude fixes the window
    over the full decode and varies its probability only. reference_copy=False
    still exposes reference tokens to the predictor, but never copies them into
    sampled candidates. control_aware is an experimental conflict penalty, not
    a validated authority estimator; it uses no additional predictor calls.

    Trace counts use flattened token positions. temporary/exposure/conflict are
    counted before the update; committed is cumulative after it; newly/direct
    copy refer only to this step's commits. A direct copy counts every committed
    position covered by the reference override, including coincident samples.
    """
    _validate_masked_job(steps, job)
    ref=reference.reshape(1,-1).long(); support=mask.reshape(1,-1)>0
    n=ref.shape[1]; device=ref.device
    if not n or support.shape != ref.shape:
        raise ValueError('reference and mask must have the same positive number of tokens')
    if mask.device != device or not bool(torch.isfinite(mask).all()) or bool(((mask < 0) | (mask > 1)).any()):
        raise ValueError('mask must be finite, in [0, 1], and on the reference device')
    if job['arm'] in ('guide', 'logit_bias') and not bool(support.any()):
        raise ValueError('guide and logit_bias require a nonempty target region')
    control_schedule = job.get('control_schedule', 'duration')
    reference_copy = job.get('reference_copy', True)
    control_mask = mask.reshape(1, -1)
    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))
        if control_schedule == 'duration':
            g=gate(control_mask*job['alpha'],p,1/steps,job.get('soft_release',True))
        else:
            # A fixed duration of one; alpha does not change the release time.
            g=gate(torch.ones_like(control_mask,dtype=torch.float32),p,1/steps,job.get('soft_release',True))*control_mask*job['alpha']
        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)
        conflict = temporary & (sampled != ref)
        if reference_copy:
            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
        elif arm=='control_aware':
            # Keep likely reference-copy conflicts mutable within each quota.
            # At alpha=0 temporary/conflict are zero, exactly matching balanced.
            score=score-job.get('authority_weight',.5)*conflict.to(score.dtype)
        score[committed]=-float('inf')
        if arm in ('balanced', 'control_aware'):
            newly=_quota_commit(score,support,committed,wanted)
        else:
            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,
                      'target_count':int(support.sum()),
                      'target_temporary_count':int((support & temporary).sum()),
                      'target_committed_count':int((support & committed).sum()),
                      'target_newly_count':int((support & newly).sum()),
                      'target_direct_copy_count':int((support & newly & temporary).sum()) if reference_copy else 0,
                      'outside_direct_copy_count':int((~support & newly & temporary).sum()) if reference_copy else 0,
                      'reference_exposure_count':int(temporary.sum()),
                      'target_conflict_count':int((support & conflict).sum()),
                      'control_schedule':control_schedule})
    return ids,committed_at