"""Research sampler companion for the merged LFM2.5 masked-diffusion checkpoint.""" import torch from torch import nn class DiffusionLFM(nn.Module): """Use bidirectional canvas attention and the saved learned mask embedding.""" def __init__(self, causal_model, mask_embedding): super().__init__() self.model = causal_model self.mask_embedding = nn.Parameter(mask_embedding.detach().clone(), requires_grad=False) for layer in self.model.model.layers: if layer.is_attention_layer: layer.self_attn.is_causal = False @property def causal_model(self): return self.model def hidden(self, ids, masked, valid, prefix_lengths): m=self.model.model hidden=m.embed_tokens(ids) hidden=torch.where(masked[...,None],self.mask_embedding.to(hidden.dtype),hidden) positions=torch.arange(ids.shape[1],device=ids.device)[None] rotary=m.pos_emb(hidden,positions) length=ids.shape[1] q=torch.arange(length,device=ids.device)[None,:,None] k=torch.arange(length,device=ids.device)[None,None,:] p=prefix_lengths[:,None,None] allowed=torch.where(q