rust_bert/models/bert/mod.rs
1//! # BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (Devlin et al.)
2//!
3//! Implementation of the BERT language model ([https://arxiv.org/abs/1810.04805](https://arxiv.org/abs/1810.04805) Devlin, Chang, Lee, Toutanova, 2018).
4//! The base model is implemented in the `bert_model::BertModel` struct. Several language model heads have also been implemented, including:
5//! - Masked language model: `bert_model::BertForMaskedLM`
6//! - Multiple choices: `bert_model:BertForMultipleChoice`
7//! - Question answering: `bert_model::BertForQuestionAnswering`
8//! - Sequence classification: `bert_model::BertForSequenceClassification`
9//! - Token classification (e.g. NER, POS tagging): `bert_model::BertForTokenClassification`
10//!
11//! # Model set-up and pre-trained weights loading
12//!
13//! A full working example is provided in `examples/masked_language_model_bert`, run with `cargo run --example masked_language_model_bert`.
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::bert::{BertConfig, BertForMaskedLM};
28//! use rust_bert::resources::{LocalResource, ResourceProvider};
29//! use rust_bert::Config;
30//! use rust_tokenizers::tokenizer::BertTokenizer;
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: BertTokenizer =
47//! BertTokenizer::from_file(vocab_path.to_str().unwrap(), true, true)?;
48//! let config = BertConfig::from_file(config_path);
49//! let bert_model = BertForMaskedLM::new(&vs.root(), &config);
50//! vs.load(weights_path)?;
51//!
52//! # Ok(())
53//! # }
54//! ```
55
56mod attention;
57mod bert_model;
58mod embeddings;
59pub(crate) mod encoder;
60
61pub use bert_model::{
62 BertConfig, BertConfigResources, BertForMaskedLM, BertForMultipleChoice,
63 BertForQuestionAnswering, BertForSentenceEmbeddings, BertForSequenceClassification,
64 BertForTokenClassification, BertMaskedLMOutput, BertModel, BertModelOutput, BertModelResources,
65 BertQuestionAnsweringOutput, BertSequenceClassificationOutput, BertTokenClassificationOutput,
66 BertVocabResources,
67};
68pub use embeddings::{BertEmbedding, BertEmbeddings};
69pub use encoder::{BertEncoder, BertEncoderOutput, BertLayer, BertLayerOutput, BertPooler};