rust_bert/models/openai_gpt/mod.rs
1//! # GPT (Radford et al.)
2//!
3//! Implementation of the GPT2 language model ([Improving Language Understanding by Generative Pre-Training](https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf) Radford, Narasimhan, Salimans, Sutskever 2018).
4//! The base model is implemented in the `openai_gpt_model::OpenAiGptModel` struct. The model also includes a language model head: `openai_gpt_model::OpenAIGPTLMHeadModel`
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//! - `GptTokenizer` using a `vocab.txt` vocabulary and `merges.txt` 2-gram merges
13//!
14//! Pretrained models are available and can be downloaded using RemoteResources.
15//!
16//! ```no_run
17//! # fn main() -> anyhow::Result<()> {
18//! use tch::{nn, Device};
19//! # use std::path::PathBuf;
20//! use rust_bert::gpt2::Gpt2Config;
21//! use rust_bert::openai_gpt::OpenAiGptModel;
22//! use rust_bert::resources::{LocalResource, ResourceProvider};
23//! use rust_bert::Config;
24//! use rust_tokenizers::tokenizer::OpenAiGptTokenizer;
25//!
26//! let config_resource = LocalResource {
27//! local_path: PathBuf::from("path/to/config.json"),
28//! };
29//! let vocab_resource = LocalResource {
30//! local_path: PathBuf::from("path/to/vocab.txt"),
31//! };
32//! let merges_resource = LocalResource {
33//! local_path: PathBuf::from("path/to/vocab.txt"),
34//! };
35//! let weights_resource = LocalResource {
36//! local_path: PathBuf::from("path/to/model.ot"),
37//! };
38//! let config_path = config_resource.get_local_path()?;
39//! let vocab_path = vocab_resource.get_local_path()?;
40//! let merges_path = merges_resource.get_local_path()?;
41//! let weights_path = weights_resource.get_local_path()?;
42//!
43//! let device = Device::cuda_if_available();
44//! let mut vs = nn::VarStore::new(device);
45//! let tokenizer: OpenAiGptTokenizer = OpenAiGptTokenizer::from_file(
46//! vocab_path.to_str().unwrap(),
47//! merges_path.to_str().unwrap(),
48//! true,
49//! )?;
50//! let config = Gpt2Config::from_file(config_path);
51//! let gpt_model = OpenAiGptModel::new(&vs.root(), &config);
52//! vs.load(weights_path)?;
53//!
54//! # Ok(())
55//! # }
56//! ```
57
58mod openai_gpt_model;
59mod transformer;
60
61pub use openai_gpt_model::{
62 OpenAIGPTLMHeadModel, OpenAIGenerator, OpenAiGptConfig, OpenAiGptConfigResources,
63 OpenAiGptMergesResources, OpenAiGptModel, OpenAiGptModelOutput, OpenAiGptModelResources,
64 OpenAiGptVocabResources,
65};