Vendor pinned MeZO, AGZO, LOZO and LoREN sources with independent environments. Add direct in-place fix/adjust/node training, derived history-free basis gradients, development-best checkpoints and experiment tooling. Keep models, datasets, caches and credentials outside Git. Validation: 93 project tests passed; 521 vendored author files verified.
27 lines
1.6 KiB
Python
27 lines
1.6 KiB
Python
"""Diagnostics around the unchanged MeZO option-loss wrapper; its scalar loss is authoritative."""
|
|
import torch
|
|
import torch.nn.functional as F
|
|
|
|
class MeZOLoss:
|
|
def __init__(self,model):self.model=model;self.last_weight=None
|
|
def __call__(self,inputs):
|
|
output=self.model(**inputs,return_dict=True)
|
|
labels=inputs['input_ids'][:,1:].clone();labels[labels==self.model.config.pad_token_id]=-100
|
|
for index,length in enumerate(inputs['option_len']):
|
|
if int(length)<=0:raise ValueError('Empty MeZO option after encoding/truncation')
|
|
labels[index,:-int(length)]=-100
|
|
valid=labels!=-100
|
|
if 'num_options' in inputs:
|
|
options=int(inputs['num_options'][0])
|
|
if options!=2 or not bool((inputs['num_options']==2).all()):raise ValueError('Current node/diagnostic integration supports binary SST2 options only')
|
|
log_probs=output.logits[:,:-1].log_softmax(-1)
|
|
selected=log_probs.gather(-1,labels.masked_fill(~valid,0).unsqueeze(-1)).squeeze(-1)
|
|
scores=((selected*valid).sum(-1)/valid.sum(-1)).view(-1,2)
|
|
# Repeat per-example scores for the two candidate activation rows.
|
|
logits=scores.repeat_interleave(2,dim=0);self.last_weight=len(scores)
|
|
else:
|
|
logits=output.logits.new_empty((len(labels),0));self.last_weight=int(valid.sum())
|
|
# Preserve the author's scalar/token-weighted objective, rather than
|
|
# replacing it with a mean of per-example token losses.
|
|
return output.loss.expand(len(inputs['input_ids'])),logits
|