rust_bert/models/bart/
mod.rs

1//! # BART (Lewis et al.)
2//!
3//! Implementation of the BART language model ([BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/abs/1910.13461) Lewis, Liu, Goyal, Ghazvininejad, Mohamed, Levy, Stoyanov, Zettlemoyer, 2019).
4//! The base model is implemented in the `bart_model::BartModel` struct. The model also includes a language model head: `bart_model::BartForConditionalGeneration`
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 summarization capabilities are illustrated in `examples/summarization_bart`, run with `cargo run --example summarization_bart`.
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//! - `RobertaTokenizer` using a `vocab.txt` vocabulary and `merges.txt` 2-gram merges
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::bart::{BartConfig, BartModel};
23//! use rust_bert::resources::{LocalResource, ResourceProvider};
24//! use rust_bert::Config;
25//! use rust_tokenizers::tokenizer::RobertaTokenizer;
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 merges_resource = LocalResource {
34//!     local_path: PathBuf::from("path/to/vocab.txt"),
35//! };
36//! let weights_resource = LocalResource {
37//!     local_path: PathBuf::from("path/to/model.ot"),
38//! };
39//! let config_path = config_resource.get_local_path()?;
40//! let vocab_path = vocab_resource.get_local_path()?;
41//! let merges_path = merges_resource.get_local_path()?;
42//! let weights_path = weights_resource.get_local_path()?;
43//!
44//! let device = Device::cuda_if_available();
45//! let mut vs = nn::VarStore::new(device);
46//! let tokenizer: RobertaTokenizer = RobertaTokenizer::from_file(
47//!     vocab_path.to_str().unwrap(),
48//!     merges_path.to_str().unwrap(),
49//!     true,
50//!     false,
51//! )?;
52//! let config = BartConfig::from_file(config_path);
53//! let bart_model = BartModel::new(&vs.root(), &config);
54//! vs.load(weights_path)?;
55//!
56//! # Ok(())
57//! # }
58//! ```
59
60mod attention;
61mod bart_model;
62mod decoder;
63mod embeddings;
64mod encoder;
65
66pub use attention::LayerState;
67pub use bart_model::{
68    BartConfig, BartConfigResources, BartForConditionalGeneration, BartForSequenceClassification,
69    BartGenerator, BartMergesResources, BartModel, BartModelOutput, BartModelResources,
70    BartVocabResources,
71};
72
73pub(crate) use attention::BartAttention;
74pub(crate) use bart_model::{_expand_mask, _make_causal_mask, _prepare_decoder_attention_mask};
75pub(crate) use decoder::BartDecoderOutput;
76pub(crate) use embeddings::LearnedPositionalEmbedding;
77pub(crate) use encoder::BartEncoderOutput;