iamthinbaker commited on
Commit
71b02e7
·
verified ·
1 Parent(s): 90c11be

Add model code for trust_remote_code

Browse files
Files changed (1) hide show
  1. conditioned_gpt2.py +7 -7
conditioned_gpt2.py CHANGED
@@ -82,7 +82,11 @@ class ConditionedGPT2(GPT2LMHeadModel):
82
  ):
83
  nn.init.normal_(emb.weight, std=0.02)
84
 
85
- # Per-token loss weights — downweights background tokens to focus on color pixels
 
 
 
 
86
  self.register_buffer("token_weights", token_weights)
87
 
88
  # Store row marker token ids as a buffer so they're saved with the model
@@ -227,11 +231,7 @@ class ConditionedGPT2(GPT2LMHeadModel):
227
  shift_labels = (
228
  labels[..., 1:].contiguous().to(outputs.logits.device)
229
  )
230
- weights = (
231
- self.token_weights.to(outputs.logits.device)
232
- if self.token_weights is not None
233
- else None
234
- )
235
  loss = F.cross_entropy(
236
  shift_logits.view(-1, self.config.vocab_size),
237
  shift_labels.view(-1),
@@ -424,7 +424,7 @@ class ConditionedGPT2(GPT2LMHeadModel):
424
 
425
  class _RowLengthLogitsProcessor(LogitsProcessor):
426
  def __init__(
427
- self, tokenizer, row_marker_ids: list[int], row_width: int = 63
428
  ):
429
  vocab = tokenizer.get_vocab()
430
  self.row_marker_set = set(row_marker_ids)
 
82
  ):
83
  nn.init.normal_(emb.weight, std=0.02)
84
 
85
+ # Per-token loss weights — downweights background tokens to focus on color pixels.
86
+ # Never None so PyTorch always includes it in state_dict and from_pretrained
87
+ # can load it from checkpoint without an "unexpected key" warning.
88
+ if token_weights is None:
89
+ token_weights = torch.ones(config.vocab_size)
90
  self.register_buffer("token_weights", token_weights)
91
 
92
  # Store row marker token ids as a buffer so they're saved with the model
 
231
  shift_labels = (
232
  labels[..., 1:].contiguous().to(outputs.logits.device)
233
  )
234
+ weights = self.token_weights.to(outputs.logits.device)
 
 
 
 
235
  loss = F.cross_entropy(
236
  shift_logits.view(-1, self.config.vocab_size),
237
  shift_labels.view(-1),
 
424
 
425
  class _RowLengthLogitsProcessor(LogitsProcessor):
426
  def __init__(
427
+ self, tokenizer, row_marker_ids: list[int], row_width: int = 63,
428
  ):
429
  vocab = tokenizer.get_vocab()
430
  self.row_marker_set = set(row_marker_ids)