# Problem with returning decoder cross attentions through generate function

**URL:** <https://discuss.huggingface.co/t/problem-with-returning-decoder-cross-attentions-through-generate-function/113886>\
**Category:** 🤗Transformers\
**Created:** [October 25, 2024, 3:29am UTC](https://discuss.huggingface.co/t/problem-with-returning-decoder-cross-attentions-through-generate-function/113886 "2024-10-25T03:29:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrinalr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mrinalr/32/33400_2.png) [@mrinalr](https://discuss.huggingface.co/u/mrinalr)\
**Post date:** [October 25, 2024, 3:29am UTC](https://discuss.huggingface.co/t/problem-with-returning-decoder-cross-attentions-through-generate-function/113886/1 "2024-10-25T03:29:56Z")

</div>

I’m using TFBartBase model with generate() with the default beam search setting. I have set the “output\_attentions”, “return\_dict”, and “return\_dict\_in\_generate” flags to True. I noticed that the cross\_attention object is an empty list. After a bit of digging around in beam\_search\_body\_fn(), I found the below snippet that explains why this is happening.

The below snippet from [tf\_utils.py](https://github.com/huggingface/transformers/blob/v4.45.2/src/transformers/generation/tf_utils.py#L2344) file.

```auto
 # Store scores, attentions and hidden_states when required
            if not use_xla and return_dict_in_generate:
                if output_scores:
                    all_scores.append(
                        logits_warper(
                            flatten_beam_dim(running_sequences),
                            flatten_beam_dim(log_probs_processed),
                            cur_len,
                        )
                    )
                if output_attentions and self.config.is_encoder_decoder:
                    decoder_attentions.append(model_outputs.decoder_attentions)
                elif output_attentions and not self.config.is_encoder_decoder:
                    decoder_attentions.append(model_outputs.attentions)
                    if self.config.is_encoder_decoder:
                        cross_attentions.append(model_outputs.cross_attentions)

```

why is `cross_attentions.append(model_outputs.cross_attentions)` not part of the first `if output_attentions and self.config.is_encoder_decoder`?  
I believe this is causing cross\_attentions object to not return anything. Is this a bug or is there a reason behind the if…elif… statement being formed the way it is?

I’d really appreciate soem insights. Thank you!
