<!--Copyright 2021 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->
*This model was released on 2021-11-17 and added to Hugging Face Transformers on 2023-06-20.*

# XLS-R

[XLS-R](https://huggingface.co/papers/2111.09296) is a large-scale model for cross-lingual speech representation learning built on wav2vec 2.0. Trained with up to 2B parameters on nearly half a million hours of speech audio in 128 languages, XLS-R significantly outperforms previous models across various tasks and languages. It achieves state-of-the-art results on CoVoST-2 speech translation, improving BLEU scores by 7.4 on average across 21 translation directions. XLS-R also lowers speech recognition error rates by 14-34% on benchmarks like BABEL, MLS, CommonVoice, and VoxPopuli, and sets a new standard on VoxLingua107 for language identification. Notably, the model demonstrates that cross-lingual pretraining can surpass English-only pretraining for translating English speech into other languages.

<hfoptions id="usage">
<hfoption id="Pipeline">

```py
import torch
from transformers import pipeline

pipeline = pipeline(task="automatic-speech-recognition", model="facebook/wav2vec2-xls-r-300m", dtype="auto")
pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac")
```

</hfoption>
<hfoption id="AutoModel">

```py
import torch
from datasets import load_dataset
from transformers import AutoProcessor, AutoModelForCTC

dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation").sort("id")
sampling_rate = dataset.features["audio"].sampling_rate

processor = AutoProcessor.from_pretrained("facebook/wav2vec2-xls-r-300m")
model = AutoModelForCTC.from_pretrained("facebook/wav2vec2-xls-r-300m", dtype="auto")

inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt")
with torch.no_grad():
    logits = model(**inputs).logits
predicted_ids = torch.argmax(logits, dim=-1)
print(f"Transcription: {processor.batch_decode(predicted_ids)[0]}")
```

</hfoption>
</hfoptions>

