rust_bert/models/longformer/mod.rs
1//! # Longformer: The Long-Document Transformer (Betalgy et al.)
2//!
3//! Implementation of the Longformer language model ([Longformer: The Long-Document Transformer](https://arxiv.org/abs/2001.04063) Betalgy, Peters, Cohan, 2020).
4//! The base model is implemented in the `longformer_model::LongformerModel` struct. Several language model heads have also been implemented, including:
5//! - Masked language model: `longformer_model::LongformerForMaskedLM`
6//! - Multiple choices: `longformer_model:LongformerForMultipleChoice`
7//! - Question answering: `longformer_model::LongformerForQuestionAnswering`
8//! - Sequence classification: `longformer_model::LongformerForSequenceClassification`
9//! - Token classification (e.g. NER, POS tagging): `longformer_model::LongformerForTokenClassification`
10//!
11//! # Model set-up and pre-trained weights loading
12//!
13//! A full working example (question answering) is provided in `examples/question_answering_longformer`, run with `cargo run --example question_answering_longformer`.
14//! All models expect the following resources:
15//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
16//! - 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.
17//! - `RobertaTokenizer` using a `vocab.json` vocabulary and `merges.txt` byte pair encoding merges
18//!
19//! # Question answering example below:
20//!
21//! ```no_run
22//! use rust_bert::longformer::{
23//! LongformerConfigResources, LongformerMergesResources, LongformerModelResources,
24//! LongformerVocabResources,
25//! };
26//! use rust_bert::pipelines::common::ModelType;
27//! use rust_bert::pipelines::question_answering::{
28//! QaInput, QuestionAnsweringConfig, QuestionAnsweringModel,
29//! };
30//! use rust_bert::resources::{RemoteResource};
31//!
32//! fn main() -> anyhow::Result<()> {
33//! // Set-up Question Answering model
34//! use rust_bert::pipelines::common::ModelResource;
35//! let config = QuestionAnsweringConfig::new(
36//! ModelType::Longformer,
37//! ModelResource::Torch(Box::new(RemoteResource::from_pretrained(
38//! LongformerModelResources::LONGFORMER_BASE_SQUAD1,
39//! ))),
40//! RemoteResource::from_pretrained(
41//! LongformerConfigResources::LONGFORMER_BASE_SQUAD1,
42//! ),
43//! RemoteResource::from_pretrained(
44//! LongformerVocabResources::LONGFORMER_BASE_SQUAD1,
45//! ),
46//! Some(RemoteResource::from_pretrained(
47//! LongformerMergesResources::LONGFORMER_BASE_SQUAD1,
48//! )),
49//! false,
50//! None,
51//! false,
52//! );
53//!
54//! let qa_model = QuestionAnsweringModel::new(config)?;
55//!
56//! // Define input
57//! let question_1 = String::from("Where does Amy live ?");
58//! let context_1 = String::from("Amy lives in Amsterdam");
59//! let question_2 = String::from("Where does Eric live");
60//! let context_2 = String::from("While Amy lives in Amsterdam, Eric is in The Hague.");
61//! let qa_input_1 = QaInput {
62//! question: question_1,
63//! context: context_1,
64//! };
65//! let qa_input_2 = QaInput {
66//! question: question_2,
67//! context: context_2,
68//! };
69//!
70//! // Get answer
71//! let answers = qa_model.predict(&[qa_input_1, qa_input_2], 1, 32);
72//! println!("{:?}", answers);
73//! Ok(())
74//! }
75//! ```
76
77mod attention;
78mod embeddings;
79mod encoder;
80mod longformer_model;
81
82pub use longformer_model::{
83 LongformerConfig, LongformerConfigResources, LongformerForMaskedLM,
84 LongformerForMultipleChoice, LongformerForQuestionAnswering,
85 LongformerForSequenceClassification, LongformerForTokenClassification,
86 LongformerMergesResources, LongformerModel, LongformerModelResources,
87 LongformerTokenClassificationOutput, LongformerVocabResources,
88};