rust_bert/models/longt5/
mod.rs

1//! # LongT5 (Efficient Text-To-Text Transformer for Long Sequences)
2//!
3//! Implementation of the LongT5 language model ([LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://arxiv.org/abs/2112.07916) Guo, Ainslie, Uthus, Ontanon, Ni, Sung, Yang, 2021).
4//! The base model is implemented in the `longt5_model::LongT5Model` struct. This model includes a language model head: `longt5_model::LongT5ForConditionalGeneration`
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//! All models expect the following resources:
10//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
11//! - 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.
12//! - `T5Tokenizer` using a `spiece.model` sentence piece model
13//!
14//! Pretrained models for a number of language pairs are available and can be downloaded using RemoteResources.
15//!
16//! ```no_run
17//! # fn main() -> anyhow::Result<()> {
18//! #
19//! use tch::{nn, Device};
20//! # use std::path::PathBuf;
21//! use rust_bert::longt5::{LongT5Config, LongT5ForConditionalGeneration};
22//! use rust_bert::resources::{LocalResource, ResourceProvider};
23//! use rust_bert::Config;
24//! use rust_tokenizers::tokenizer::T5Tokenizer;
25//!
26//! let config_resource = LocalResource {
27//!     local_path: PathBuf::from("path/to/config.json"),
28//! };
29//! let sentence_piece_resource = LocalResource {
30//!     local_path: PathBuf::from("path/to/spiece.model"),
31//! };
32//! let weights_resource = LocalResource {
33//!     local_path: PathBuf::from("path/to/model.ot"),
34//! };
35//! let config_path = config_resource.get_local_path()?;
36//! let spiece_path = sentence_piece_resource.get_local_path()?;
37//! let weights_path = weights_resource.get_local_path()?;
38//!
39//! let device = Device::cuda_if_available();
40//! let mut vs = nn::VarStore::new(device);
41//! let tokenizer = T5Tokenizer::from_file(spiece_path.to_str().unwrap(), true);
42//! let config = LongT5Config::from_file(config_path);
43//! let longt5_model = LongT5ForConditionalGeneration::new(&vs.root(), &config);
44//! vs.load(weights_path)?;
45//!
46//! # Ok(())
47//! # }
48//! ```
49
50mod attention;
51mod encoder;
52mod layer_norm;
53mod longt5_model;
54
55pub use attention::LayerState;
56pub use longt5_model::{
57    LongT5Config, LongT5ConfigResources, LongT5ForConditionalGeneration, LongT5Generator,
58    LongT5Model, LongT5ModelResources, LongT5VocabResources,
59};