"""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) [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=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,:]