rust_bert/models/gpt2/
mod.rs

1//! # GPT2 (Radford et al.)
2//!
3//! Implementation of the GPT2 language model ([Language Models are Unsupervised Multitask Learners](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) Radford, Wu, Child, Luan, Amodei, Sutskever 2019).
4//! The base model is implemented in the `gpt2_model::Gpt2Model` struct. The model also includes a language model head: `gpt2_model::GPT2LMHeadModel`
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_gpt2`, run with `cargo run --example generation_gpt2`.
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//! - `Gpt2Tokenizer` 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::gpt2::{GPT2LMHeadModel, Gpt2Config};
23//! use rust_bert::resources::{LocalResource, ResourceProvider};
24//! use rust_bert::Config;
25//! use rust_tokenizers::tokenizer::Gpt2Tokenizer;
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: Gpt2Tokenizer = Gpt2Tokenizer::from_file(
47//!     vocab_path.to_str().unwrap(),
48//!     merges_path.to_str().unwrap(),
49//!     true,
50//! )?;
51//! let config = Gpt2Config::from_file(config_path);
52//! let gpt2_model = GPT2LMHeadModel::new(&vs.root(), &config);
53//! vs.load(weights_path)?;
54//!
55//! # Ok(())
56//! # }
57//! ```
58
59pub(crate) mod attention;
60mod gpt2_model;
61pub(crate) mod transformer;
62
63pub use gpt2_model::{
64    GPT2Generator, GPT2LMHeadModel, Gpt2Config, Gpt2ConfigResources, Gpt2MergesResources,
65    Gpt2Model, Gpt2ModelOutput, Gpt2ModelResources, Gpt2VocabResources,
66};