rust_bert/models/pegasus/mod.rs
1//! # Pegasus (Zhang et al.)
2//!
3//! Implementation of the Pegasus language model ([PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://arxiv.org/abs/1912.08777) Zhang, Zhao, Saleh, Liu, 2019).
4//! The base model is implemented in the `pegasus_model::PegasusModel` struct and leverages an implementation that is broadly similar to BART. The model also includes a language model head: `pegasus_model::PegasusForConditionalGeneration`
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//! A full working example is provided in `examples/summarization_pegasus`, run with `cargo run --example summarization_pegasus`.
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//! - `PegasusTokenizer` using a `spiece.model` vocabulary and unigram 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::pegasus::{PegasusConfig, PegasusModel};
23//! use rust_bert::resources::{LocalResource, ResourceProvider};
24//! use rust_bert::Config;
25//! use rust_tokenizers::tokenizer::PegasusTokenizer;
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/spiece.model"),
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: PegasusTokenizer =
43//! PegasusTokenizer::from_file(vocab_path.to_str().unwrap(), false)?;
44//! let config = PegasusConfig::from_file(config_path);
45//! let pegasus_model = PegasusModel::new(&vs.root(), &config);
46//! vs.load(weights_path)?;
47//!
48//! # Ok(())
49//! # }
50//! ```
51
52mod attention;
53mod decoder;
54mod embeddings;
55mod encoder;
56mod pegasus_model;
57
58pub use attention::LayerState;
59pub use pegasus_model::{
60 PegasusConditionalGenerator, PegasusConfig, PegasusConfigResources,
61 PegasusForConditionalGeneration, PegasusModel, PegasusModelResources, PegasusVocabResources,
62};