rust_bert/models/reformer/mod.rs
1//! # Reformer: The Efficient Transformer (Kitaev et al.)
2//!
3//! Implementation of the Reformer language model ([Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451) Kitaev, kaiser, Levskaya, 2020).
4//! The base model is implemented in the `reformer_model::ReformerModel` struct. The model also includes a language model head: `reformer_model::ReformerModelWithLMHead`
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/generation_reformer`, run with `cargo run --example generation_reformer`.
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//! - `ReformerTokenizer` using a `spiece.model` BPE model
14//!
15//! Pretrained models on "Crime and Punishment" (Dostoevsky) 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::reformer::{ReformerConfig, ReformerModel};
23//! use rust_bert::resources::{LocalResource, ResourceProvider};
24//! use rust_bert::Config;
25//! use rust_tokenizers::tokenizer::ReformerTokenizer;
26//!
27//! let config_resource = LocalResource {
28//! local_path: PathBuf::from("path/to/config.json"),
29//! };
30//! let weights_resource = LocalResource {
31//! local_path: PathBuf::from("path/to/weights.ot"),
32//! };
33//! let vocab_resource = LocalResource {
34//! local_path: PathBuf::from("path/to/spiece.model"),
35//! };
36//! let config_path = config_resource.get_local_path()?;
37//! let weights_path = weights_resource.get_local_path()?;
38//! let vocab_path = vocab_resource.get_local_path()?;
39//!
40//! let device = Device::cuda_if_available();
41//! let mut vs = nn::VarStore::new(device);
42//! let tokenizer: ReformerTokenizer =
43//! ReformerTokenizer::from_file(vocab_path.to_str().unwrap(), true)?;
44//! let config = ReformerConfig::from_file(config_path);
45//! let bart_model = ReformerModel::new(&vs.root(), &config);
46//! vs.load(weights_path)?;
47//!
48//! # Ok(())
49//! # }
50//! ```
51
52mod attention;
53mod attention_utils;
54mod embeddings;
55mod encoder;
56mod reformer_model;
57
58pub use attention::LayerState;
59pub use reformer_model::{
60 ReformerClassificationOutput, ReformerConfig, ReformerConfigResources,
61 ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerGenerator,
62 ReformerModel, ReformerModelResources, ReformerModelWithLMHead,
63 ReformerQuestionAnsweringModelOutput, ReformerVocabResources,
64};