# mBART for translation truncating result

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429%3E%5C
**Category:** Beginners\
**Created:** [March 15, 2021, 7:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429 "2021-03-15T19:45:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Buckeyes2019](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@Buckeyes2019](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBuckeyes2019)%5C
**Post date:** [March 15, 2021, 7:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429%2F1 "2021-03-15T19:45:50Z")

</div>

I am using mBART (specifically “mrm8488/mbart-large-finetuned-opus-es-en-translation”) for translation and the model seems to be truncating the output. Below is the code and the result. Has anyone used this model successfully? Can you see an error in my code? Any suggestions on how I might get a better translation with this model?

`<< Original Text: "Esta investigación presenta un análisis del gasto público federal asignado a los hogares en condición de marginación con enfoque asistencial. Se sustenta en que éste debe orientarse a facilitar la inversión y el impulso a los procesos de trabajo productivo, generadores de crecimiento y empleo. Para esto se presenta una propuesta de evaluación cuantitativa basada en el modelo de contabilidad social que formula el Sistema de Cuentas Nacionales, en su revisión de 1993 y actualizada con la misma perspectiva en 2008. Los resultados se analizan con el modelo de multiplicador keynesiano.">>`

**model\_name1 = "mrm8488/mbart-large-finetuned-opus-es-en-translation"**  
**tokenizer1 = AutoTokenizer.from\_pretrained(model\_name1)**  
**model1 = AutoModelForSeq2SeqLM.from\_pretrained(model\_name1)**

**input\_ids1 = tokenizer1(text, return\_tensors=“pt”).input\_ids**  
**outputs1 = model1.generate(input\_ids1, num\_return\_sequences=4, num\_beams=6, do\_sample=True, early\_stopping=True)**  
**print(tokenizer1.decode(outputs1[0]))**

`<s> This Research presents a three-year review of the federal public expenditure model, the same as the nationally-allotment model,</s>`

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 15, 2021, 10:31pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429%2F2 "2021-03-15T22:31:52Z")

</div>

Hi @Buckeyes2019, looking at the [docs](https://huggingface.co/transformers/model_doc/mbart.html?highlight=translation#training) for MBart, it seems that you need to prepare the data in a special format and define `decoder_start_token_id`

I’m not sure whether that will solve your truncation issue (I wonder if you can set `max_length` in `model.generate` to a larger value?) and looking at your example it seems like the model is “summarising” the input text instead of translating it which seems odd to me …

---

<div class="post-metadata">

**Author:** ![infinitejoy](https://avatars.discourse-cdn.com/v4/letter/i/ea666f/32.png) [@infinitejoy](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Finfinitejoy)%5C
**Post date:** [March 16, 2021, 5:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429%2F3 "2021-03-16T05:18:40Z")

</div>

`max_length` cannot be set to a number greater than 32 which is the batch len

---

<div class="post-metadata">

**Author:** ![valhalla](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/valhalla/32/4179_2.png) [@valhalla](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fvalhalla)%5C
**Post date:** [March 16, 2021, 10:21am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fmbart-for-translation-truncating-result%2F4429%2F4 "2021-03-16T10:21:59Z")

</div>

hi @infinitejoy

As @lewtun said, MBart needs the data in a special format. You should pass the `src_lang` when initializing the tokenizer or set the `tokenizer.src_lang` attribute, so the tokenizer will add the correct language token to encoded text. Also `MBart` expects the target language id as the `decoder_start_token_id` , so you need to pass that argument to `generate`. Here’s a simple code snippet

```python
tokenizer = MBartTokenizer(..., src_lang="es_XX")
model.generate(..., decoder_start_token_id=tokenizer.get_lang_code_to_id["en_XX"]

```

also I’m not sure what you mean by

> `max_length` cannot be set to a number greater than 32 which is the batch len

you can set `max_length` to any value you want.

also there are other models on the hub that can do `es` to `en` translation like [mbart50](https://huggingface.co/transformers/master/model_doc/mbart.html#overview-of-mbart-50) or [m2m100](https://huggingface.co/transformers/master/model_doc/m2m_100.html)
