rust_bert/models/albert/mod.rs
1//! # ALBERT: A Lite BERT for Self-supervised Learning of Language Representations (Lan et al.)
2//!
3//! Implementation of the ALBERT language model ([https://arxiv.org/abs/1909.11942](https://arxiv.org/abs/1909.11942) Lan, Chen, Goodman, Gimpel, Sharma, Soricut, 2019).
4//! This model offers a greatly reduced memory footprint for similar effective size (number and size of layers). The computational cost remains however similar to the original BERT model.
5//! The base model is implemented in the `albert_model::AlbertModel` struct. Several language model heads have also been implemented, including:
6//! - Masked language model: `albert_model::AlbertForMaskedLM`
7//! - Multiple choices: `albert_model:AlbertForMultipleChoice`
8//! - Question answering: `albert_model::AlbertForQuestionAnswering`
9//! - Sequence classification: `albert_model::AlbertForSequenceClassification`
10//! - Token classification (e.g. NER, POS tagging): `albert_model::AlbertForTokenClassification`
11//!
12//! # Model set-up and pre-trained weights loading
13//!
14//! The example below illustrate a Masked language model example, the structure is similar for other models.
15//! All models expect the following resources:
16//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
17//! - 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.
18//! - `BertTokenizer` using a `vocab.txt` vocabulary
19//!
20//! Pretrained models are available and can be downloaded using RemoteResources.
21//!
22//! ```no_run
23//! # fn main() -> anyhow::Result<()> {
24//! #
25//! use tch::{nn, Device};
26//! # use std::path::PathBuf;
27//! use rust_bert::albert::{AlbertConfig, AlbertForMaskedLM};
28//! use rust_bert::resources::{LocalResource, ResourceProvider};
29//! use rust_bert::Config;
30//! use rust_tokenizers::tokenizer::AlbertTokenizer;
31//!
32//! let config_resource = LocalResource {
33//! local_path: PathBuf::from("path/to/config.json"),
34//! };
35//! let vocab_resource = LocalResource {
36//! local_path: PathBuf::from("path/to/vocab.txt"),
37//! };
38//! let weights_resource = LocalResource {
39//! local_path: PathBuf::from("path/to/model.ot"),
40//! };
41//! let config_path = config_resource.get_local_path()?;
42//! let vocab_path = vocab_resource.get_local_path()?;
43//! let weights_path = weights_resource.get_local_path()?;
44//! let device = Device::cuda_if_available();
45//! let mut vs = nn::VarStore::new(device);
46//! let tokenizer: AlbertTokenizer =
47//! AlbertTokenizer::from_file(vocab_path.to_str().unwrap(), true, true)?;
48//! let config = AlbertConfig::from_file(config_path);
49//! let bert_model = AlbertForMaskedLM::new(&vs.root(), &config);
50//! vs.load(weights_path)?;
51//!
52//! # Ok(())
53//! # }
54//! ```
55
56mod albert_model;
57mod attention;
58mod embeddings;
59mod encoder;
60
61pub use albert_model::{
62 AlbertConfig, AlbertConfigResources, AlbertForMaskedLM, AlbertForMultipleChoice,
63 AlbertForQuestionAnswering, AlbertForSentenceEmbeddings, AlbertForSequenceClassification,
64 AlbertForTokenClassification, AlbertMaskedLMOutput, AlbertModel, AlbertModelResources,
65 AlbertOutput, AlbertQuestionAnsweringOutput, AlbertSequenceClassificationOutput,
66 AlbertTokenClassificationOutput, AlbertVocabResources,
67};