rust_bert/models/mbart/
mod.rs

1//! # MBart (Liu et al.)
2//!
3//! Implementation of the MBart language model ([Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) Liu, Gu, Goyal, Li, Edunov, Ghazvininejad, Lewis, Zettlemoyer, 2020).
4//! The base model is implemented in the `mbart_model::MBartModel` struct. The model also includes a language model head: `mbart_model::MBartForConditionalGeneration`
5//! implementing the common `generation_utils::LanguageGenerator` trait shared between the models used for generation (see `pipelines` for more information).
6//!
7//! # Model set-up and pre-trained weights loading
8//!
9//! The translation capabilities are illustrated in `examples/translation_mbart`, run with `cargo run --example translation_mbart`.
10//! All models expect the following resources:
11//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
12//! - Model weights are expected to have a structure and parameter names following the [Transformers library](https://github.com/huggingface/transformers). A conversion using the Python utility scripts is required to convert the `.bin` weights to the `.ot` format.
13//! - `MBart50Tokenizer` using a `spiece.model` SentencePiece model
14//!
15//! Pretrained models are available and can be downloaded using RemoteResources.
16//!
17//! ```no_run
18//! # fn main() -> anyhow::Result<()> {
19//! #
20//! use tch::{nn, Device};
21//! # use std::path::PathBuf;
22//! use rust_bert::mbart::{MBartConfig, MBartModel};
23//! use rust_bert::resources::{LocalResource, ResourceProvider};
24//! use rust_bert::Config;
25//! use rust_tokenizers::tokenizer::MBart50Tokenizer;
26//!
27//! let config_resource = LocalResource {
28//!     local_path: PathBuf::from("path/to/config.json"),
29//! };
30//! let vocab_resource = LocalResource {
31//!     local_path: PathBuf::from("path/to/vocab.txt"),
32//! };
33//! let weights_resource = LocalResource {
34//!     local_path: PathBuf::from("path/to/model.ot"),
35//! };
36//! let config_path = config_resource.get_local_path()?;
37//! let vocab_path = vocab_resource.get_local_path()?;
38//! let weights_path = weights_resource.get_local_path()?;
39//!
40//! let device = Device::cuda_if_available();
41//! let mut vs = nn::VarStore::new(device);
42//! let tokenizer: MBart50Tokenizer =
43//!     MBart50Tokenizer::from_file(vocab_path.to_str().unwrap(), false)?;
44//! let config = MBartConfig::from_file(config_path);
45//! let mbart_model = MBartModel::new(&vs.root(), &config);
46//! vs.load(weights_path)?;
47//!
48//! # Ok(())
49//! # }
50//! ```
51
52mod attention;
53mod decoder;
54mod embeddings;
55mod encoder;
56mod mbart_model;
57
58pub use mbart_model::{
59    MBartConfig, MBartConfigResources, MBartForConditionalGeneration,
60    MBartForSequenceClassification, MBartGenerator, MBartModel, MBartModelOutput,
61    MBartModelResources, MBartSourceLanguages, MBartTargetLanguages, MBartVocabResources,
62};
63
64pub use attention::LayerState;
65pub(crate) use decoder::MBartDecoderLayer;
66pub(crate) use encoder::MBartEncoderLayer;