# BartForConditionalGeneration "logits" shape is wrong/unexpected

**URL:** <https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000>\
**Category:** 🤗Transformers\
**Created:** [November 11, 2020, 8:34pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000 "2020-11-11T20:34:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wgpubs](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/wgpubs/32/303_2.png) [@wgpubs](https://discuss.huggingface.co/u/wgpubs)\
**Post date:** [November 11, 2020, 8:34pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000/1 "2020-11-11T20:34:22Z")

</div>

Using `BartForConditionalGeneration` with a batch\_size = 2 … all the inputs look right but when I examine the `logits` the shape is `torch.Size([2, 1, 50264]) `

The inputs are:

```auto
x['input_ids'].shape => torch.Size([2, 256])
x['attention_mask'].shape => torch.Size([2, 256])
x['decoder_input_ids'].shape => torch.Size([2, 68])

```

What may I be doing wrong? Or is there a bug in the model?

---

<div class="post-metadata">

**Author:** ![Jung](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jung/32/21989_2.png) [@Jung](https://discuss.huggingface.co/u/Jung)\
**Post date:** [November 11, 2020, 8:52pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000/2 "2020-11-11T20:52:29Z")

</div>

Could you give us the command you produce the logits? Did you just call the model or using model.generate?

Btw, 50264 is Bart vocab\_size

---

<div class="post-metadata">

**Author:** ![wgpubs](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/wgpubs/32/303_2.png) [@wgpubs](https://discuss.huggingface.co/u/wgpubs)\
**Post date:** [November 11, 2020, 8:55pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000/3 "2020-11-11T20:55:48Z")

</div>

It seems the problem occurs when passing `decoder_input_ids`. Here’s my code:

```auto
logits = self.hf_model(x['input_ids'], x['attention_mask'], x['decoder_input_ids'], labels=None, return_dict=True).logits

```

where `self.hf_model` is an instance of `BartForConditionalGeneration`.

returns a tensor with shape `torch.Size([2, 1, 50264])` when the expected is `torch.Size([2, 68, 50264])`

---

<div class="post-metadata">

**Author:** ![wgpubs](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/wgpubs/32/303_2.png) [@wgpubs](https://discuss.huggingface.co/u/wgpubs)\
**Post date:** [November 11, 2020, 9:33pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000/4 "2020-11-11T21:33:50Z")

</div>

**SOLVED**

Apparently, if even you are calculating the loss on your own, you have to pass in `labels` if you pass ing `decoder_input_ids`.

Ya’ll may want to add this to the docs or update the behavior to not require the `labels` argument.

Thanks - wg

---

<div class="post-metadata">

**Author:** ![Jung](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jung/32/21989_2.png) [@Jung](https://discuss.huggingface.co/u/Jung)\
**Post date:** [November 11, 2020, 9:57pm UTC](https://discuss.huggingface.co/t/bartforconditionalgeneration-logits-shape-is-wrong-unexpected/2000/5 "2020-11-11T21:57:15Z")

</div>

Great that you solved it!  
If you want to compute loss on your own, perhaps you can use `BartModel` (does not require labels) instead of `BartForConditionalGeneration`

=======================  
About the original question, to me, the following command gave the correct outputs & shape (I used TF). But it seems we need ‘max\_length’ padding

```
model = TFBartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')

src_texts = ['My friends are cool but they eat too many carbs. I really want them to be healthy, so I buy them vegetable.']
tgt_texts = ['I buy them vegetable.']
x = tokenizer.prepare_seq2seq_batch(src_texts, tgt_texts, return_tensors='tf',padding='max_length')

out = model(x)

```

Note In my case, padding has to be specified as ‘max\_length’ , where in the other case, model calling failed.
