RuVI-AI
/

ArushCodes commited on
Commit
eaefd58
·
verified ·
1 Parent(s): c306148

Update modeling_pragya.py

Browse files
Files changed (1) hide show
  1. modeling_pragya.py +24 -5
modeling_pragya.py CHANGED
@@ -1,6 +1,7 @@
1
  import dataclasses
2
 
3
  import torch
 
4
  from transformers import GenerationMixin, PreTrainedModel
5
  from transformers.modeling_outputs import CausalLMOutputWithPast
6
 
@@ -86,7 +87,8 @@ class PragyaForCausalLM(PreTrainedModel, GenerationMixin):
86
  def get_output_embeddings(self):
87
  return self.model.lm_head
88
 
89
- def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
 
90
  if attention_mask is not None and not bool(torch.all(attention_mask == 1)):
91
  # The model has no attention-mask support. Padded batches would silently
92
  # attend to pad tokens, so refuse rather than return wrong results.
@@ -94,10 +96,27 @@ class PragyaForCausalLM(PreTrainedModel, GenerationMixin):
94
  "attention_mask with padding is not supported. Run one sequence at a time "
95
  "or pass unpadded batches of equal length."
96
  )
97
- # With labels: full logits (B, T, V) and loss.
98
- # Without labels: logits for the last position only, (B, 1, V).
99
- logits, loss = self.model(input_ids, targets=labels)
100
- return CausalLMOutputWithPast(loss=loss, logits=logits)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  def prepare_inputs_for_generation(self, input_ids, **kwargs):
103
  # No KV cache: GPT keeps its own cache format, so every step runs on the full sequence.
 
1
  import dataclasses
2
 
3
  import torch
4
+ import torch.nn.functional as F
5
  from transformers import GenerationMixin, PreTrainedModel
6
  from transformers.modeling_outputs import CausalLMOutputWithPast
7
 
 
87
  def get_output_embeddings(self):
88
  return self.model.lm_head
89
 
90
+ def forward(self, input_ids=None, attention_mask=None, labels=None,
91
+ logits_to_keep=0, **kwargs):
92
  if attention_mask is not None and not bool(torch.all(attention_mask == 1)):
93
  # The model has no attention-mask support. Padded batches would silently
94
  # attend to pad tokens, so refuse rather than return wrong results.
 
96
  "attention_mask with padding is not supported. Run one sequence at a time "
97
  "or pass unpadded batches of equal length."
98
  )
99
+ if labels is not None:
100
+ # Training path: GPT computes the loss itself.
101
+ logits, loss = self.model(input_ids, targets=labels)
102
+ return CausalLMOutputWithPast(loss=loss, logits=logits)
103
+
104
+ # Inference path. logits_to_keep=0 means logits for every position (B, T, V),
105
+ # which lm-evaluation-harness and loglikelihood scoring need. logits_to_keep=k
106
+ # returns the last k positions. Hidden states are captured after ln_f, and the
107
+ # projection is the same F.linear(x, lm_head.weight) GPT uses internally.
108
+ captured = {}
109
+ handle = self.model.transformer.ln_f.register_forward_hook(
110
+ lambda module, inp, out: captured.__setitem__("x", out))
111
+ try:
112
+ self.model(input_ids)
113
+ finally:
114
+ handle.remove()
115
+ x = captured["x"]
116
+ if logits_to_keep:
117
+ x = x[:, -logits_to_keep:, :]
118
+ logits = F.linear(x, self.model.lm_head.weight)
119
+ return CausalLMOutputWithPast(logits=logits)
120
 
121
  def prepare_inputs_for_generation(self, input_ids, **kwargs):
122
  # No KV cache: GPT keeps its own cache format, so every step runs on the full sequence.