rust_bert/models/electra/mod.rs
1//! # Electra: Pre-training Text Encoders as Discriminators Rather Than Generators (Clark et al.)
2//!
3//! Implementation of the Electra language model ([https://openreview.net/pdf?id=r1xMH1BtvB](https://openreview.net/pdf?id=r1xMH1BtvB) Clark, Luong, Le, Manning, 2020).
4//! The base model is implemented in the `electra_model::ElectraModel` struct. Both generator and discriminator are available via specialized heads:
5//! - Generator head: `electra_model::ElectraGeneratorHead`
6//! - Discriminator head: `electra_model::ElectraDiscriminatorHead`
7//!
8//! The generator and discriminator models are built from these:
9//! - Generator (masked language model): `electra_model::ElectraForMaskedLM`
10//! - Discriminator: `electra_model::ElectraDiscriminator`
11//!
12//! An additional sequence token classification model is available for reference
13//! - Token classification (e.g. NER, POS tagging): `electra_model::ElectraForTokenClassification`
14//!
15//! # Model set-up and pre-trained weights loading
16//!
17//! The example below illustrate a Masked language model example, the structure is similar for other models (e.g. discriminator).
18//! All models expect the following resources:
19//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
20//! - 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.
21//! - `BertTokenizer` using a `vocab.txt` vocabulary
22//!
23//! Pretrained models are available and can be downloaded using RemoteResources.
24//!
25//! ```no_run
26//! # fn main() -> anyhow::Result<()> {
27//! #
28//! use tch::{nn, Device};
29//! # use std::path::PathBuf;
30//! use rust_bert::electra::{ElectraConfig, ElectraForMaskedLM};
31//! use rust_bert::resources::{LocalResource, ResourceProvider};
32//! use rust_bert::Config;
33//! use rust_tokenizers::tokenizer::BertTokenizer;
34//!
35//! let config_resource = LocalResource {
36//! local_path: PathBuf::from("path/to/config.json"),
37//! };
38//! let vocab_resource = LocalResource {
39//! local_path: PathBuf::from("path/to/vocab.txt"),
40//! };
41//! let weights_resource = LocalResource {
42//! local_path: PathBuf::from("path/to/model.ot"),
43//! };
44//! let config_path = config_resource.get_local_path()?;
45//! let vocab_path = vocab_resource.get_local_path()?;
46//! let weights_path = weights_resource.get_local_path()?;
47//! let device = Device::cuda_if_available();
48//! let mut vs = nn::VarStore::new(device);
49//! let tokenizer: BertTokenizer =
50//! BertTokenizer::from_file(vocab_path.to_str().unwrap(), true, true)?;
51//! let config = ElectraConfig::from_file(config_path);
52//! let electra_model = ElectraForMaskedLM::new(&vs.root(), &config);
53//! vs.load(weights_path)?;
54//!
55//! # Ok(())
56//! # }
57//! ```
58
59mod electra_model;
60mod embeddings;
61
62pub use electra_model::{
63 ElectraConfig, ElectraConfigResources, ElectraDiscriminator, ElectraDiscriminatorHead,
64 ElectraDiscriminatorOutput, ElectraForMaskedLM, ElectraForTokenClassification,
65 ElectraGeneratorHead, ElectraMaskedLMOutput, ElectraModel, ElectraModelOutput,
66 ElectraModelResources, ElectraTokenClassificationOutput, ElectraVocabResources,
67};