"""Current-residual weighted A selection. Only selected residual codes are written. The base pass emits 16-row residual summaries; no full residual-code materialization or selector GEMM. A second pass re-encodes selected blocks with the same factor. """ import torch import triton import triton.language as tl from ._linear_kernels import scaled_amax,finish_amax from ._calibrated import quantize_calibrated @triton.jit def base_summary(X,D,F,SENS,C,S,E,M:tl.constexpr,K:tl.constexpr,MP:tl.constexpr,BASE:tl.constexpr, RES:tl.constexpr=1.,GAIN_SCORE:tl.constexpr=False): rb=tl.program_id(0);kb=tl.program_id(1) mi=rb*16+tl.arange(0,16);ki=kb*128+tl.arange(0,128) x=tl.load(X+mi[:,None]*K+ki[None,:],mi[:,None]>4),(16,128)).to(tl.int32) mag=code&7 value=tl.where(mag<4,mag*.5,tl.where(mag<6,mag-2.,2.*mag-8.)) value=tl.where(code>=8,-value,value) decoded=tl.reshape(tl.reshape(value,(16,8,16))*s.to(tl.float32)[:,:,None],(16,128))*f residual=x-decoded benefit=residual*residual if GAIN_SCORE: # Evaluate the represented residual, not a hypothetical exact repair. # Codes stay in registers; only selected residual codes are written. rc=tl.reshape(tl.join(r&15,r>>4),(16,128)).to(tl.int32) rm=rc&7 rv=tl.where(rm<4,rm*.5,tl.where(rm<6,rm-2.,2.*rm-8.)) rv=tl.where(rc>=8,-rv,rv) represented=tl.reshape(tl.reshape(rv,(16,8,16))*rs.to(tl.float32)[:,:,None],(16,128))*f remaining=residual-represented benefit=tl.maximum(benefit-remaining*remaining,0.) sensitivity=tl.load(SENS+ki) score=tl.sum(tl.sum(benefit*sensitivity[None,:],1),0) tl.store(E+rb*(K//128)+kb,score) tl.store(C+mi[:,None]*(K//2)+kb*64+tl.arange(0,64)[None,:],p,mi[:,None]energy[:,None])|((energy[None,:]==energy[:,None])&(kt[None,:]