Skip to content

Commit 37fdf62

Browse files
ziywang50claude
andauthored
Apply Cohere logit_scale in AyaVisionForConditionalGeneration (#50723)
* Apply Cohere logit_scale in AyaVisionForConditionalGeneration Co-authored-by: Claude <noreply@anthropic.com> * Update AyaVision forward expectations for logits scaling * AyaVision: use image_outputs in the modular forward to match main --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e35e1a5 commit 37fdf62

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎src/transformers/models/aya_vision/modeling_aya_vision.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ def forward(
364364
hidden_states = outputs[0]
365365
hidden_states = slice_logits_to_keep(hidden_states, logits_to_keep)
366366
logits = self.lm_head(hidden_states)
367+
logits = logits * self.config.text_config.logit_scale
367368

368369
loss = None
369370
if labels is not None:

‎src/transformers/models/aya_vision/modular_aya_vision.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ...activations import ACT2FN
2828
from ...cache_utils import Cache
2929
from ...modeling_outputs import BaseModelOutputWithPooling
30+
from ...modeling_utils import slice_logits_to_keep
3031
from ...processing_utils import Unpack
3132
from ...utils import TransformersKwargs, auto_docstring, logging
3233
from ...utils.deprecation import deprecate_kwarg
@@ -245,7 +246,7 @@ def forward(
245246
>>> gen_tokens = model.generate(**inputs, max_new_tokens=300, do_sample=True, temperature=0.3)
246247
>>> processor.tokenizer.decode(gen_tokens[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
247248
```"""
248-
super().forward(
249+
outputs = self.model(
249250
input_ids=input_ids,
250251
pixel_values=pixel_values,
251252
attention_mask=attention_mask,
@@ -254,12 +255,30 @@ def forward(
254255
inputs_embeds=inputs_embeds,
255256
vision_feature_layer=vision_feature_layer,
256257
vision_feature_select_strategy=vision_feature_select_strategy,
257-
labels=labels,
258-
logits_to_keep=logits_to_keep,
259258
image_sizes=image_sizes,
260259
mm_encoder_outputs=mm_encoder_outputs,
261260
**kwargs,
262261
)
263262

263+
hidden_states = outputs[0]
264+
hidden_states = slice_logits_to_keep(hidden_states, logits_to_keep)
265+
logits = self.lm_head(hidden_states)
266+
logits = logits * self.config.text_config.logit_scale
267+
268+
loss = None
269+
if labels is not None:
270+
loss = self.loss_function(
271+
logits=logits, labels=labels, vocab_size=self.config.text_config.vocab_size, **kwargs
272+
)
273+
274+
return AyaVisionCausalLMOutputWithPast(
275+
loss=loss,
276+
logits=logits,
277+
cache=outputs.cache,
278+
hidden_states=outputs.hidden_states,
279+
attentions=outputs.attentions,
280+
image_outputs=outputs.image_outputs,
281+
)
282+
264283

265284
__all__ = ["AyaVisionForConditionalGeneration", "AyaVisionPreTrainedModel", "AyaVisionModel"]

‎tests/models/aya_vision/test_modeling_aya_vision.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ def test_small_model_integration_forward(self):
271271

272272
EXPECTED_LOGITS = Expectations(
273273
{
274-
("xpu", 3): [1.6699, 0.6260, 3.2266, 8.5547, 2.209],
274+
("xpu", 3): [0.4175, 0.1565, 0.8066, 2.1387, 0.5522],
275275
# 4-bit
276-
("cuda", 7): [0.1097, 0.3481, 3.8340, 9.7969, 2.0488],
277-
("cuda", 8): [1.6396, 0.6094, 3.1992, 8.5234, 2.1875],
276+
("cuda", 7): [0.0274, 0.0870, 0.9585, 2.4492, 0.5122],
277+
("cuda", 8): [0.4219, 0.1578, 0.8120, 2.1465, 0.5576],
278278
}
279279
) # fmt: skip
280280
expected_logits = torch.tensor(EXPECTED_LOGITS.get_expectation(), dtype=torch.float16)

0 commit comments

Comments
 (0)