rust_bert/models/distilbert/mod.rs
1//! # DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter (Sanh et al.)
2//!
3//! Implementation of the DistilBERT language model ([https://arxiv.org/abs/1910.01108](https://arxiv.org/abs/1910.01108) Sanh, Debut, Chaumond, Wolf, 2019).
4//! The base model is implemented in the `distilbert_model::DistilBertModel` struct. Several language model heads have also been implemented, including:
5//! - Masked language model: `distilbert_model::DistilBertForMaskedLM`
6//! - Question answering: `distilbert_model::DistilBertForQuestionAnswering`
7//! - Sequence classification: `distilbert_model::DistilBertForSequenceClassification`
8//! - Token classification (e.g. NER, POS tagging): `distilbert_model::DistilBertForTokenClassification`
9//!
10//! # Model set-up and pre-trained weights loading
11//!
12//! The example below illustrate a DistilBERT Masked language model example, the structure is similar for other models.
13//! All models expect the following resources:
14//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
15//! - 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.
16//! - `BertTokenizer` using a `vocab.txt` vocabulary
17//!
18//! Pretrained models are available and can be downloaded using RemoteResources.
19//!
20//! ```no_run
21//! # fn main() -> anyhow::Result<()> {
22//! #
23//! use tch::{nn, Device};
24//! # use std::path::PathBuf;
25//! use rust_bert::distilbert::{
26//! DistilBertConfig, DistilBertConfigResources, DistilBertModelMaskedLM,
27//! DistilBertModelResources, DistilBertVocabResources,
28//! };
29//! use rust_bert::resources::{LocalResource, ResourceProvider};
30//! use rust_bert::Config;
31//! use rust_tokenizers::tokenizer::BertTokenizer;
32//!
33//! let config_resource = LocalResource {
34//! local_path: PathBuf::from("path/to/config.json"),
35//! };
36//! let vocab_resource = LocalResource {
37//! local_path: PathBuf::from("path/to/vocab.txt"),
38//! };
39//! let weights_resource = LocalResource {
40//! local_path: PathBuf::from("path/to/model.ot"),
41//! };
42//! let config_path = config_resource.get_local_path()?;
43//! let vocab_path = vocab_resource.get_local_path()?;
44//! let weights_path = weights_resource.get_local_path()?;
45//! let device = Device::cuda_if_available();
46//! let mut vs = nn::VarStore::new(device);
47//! let tokenizer: BertTokenizer =
48//! BertTokenizer::from_file(vocab_path.to_str().unwrap(), true, true)?;
49//! let config = DistilBertConfig::from_file(config_path);
50//! let bert_model = DistilBertModelMaskedLM::new(&vs.root(), &config);
51//! vs.load(weights_path)?;
52//!
53//! # Ok(())
54//! # }
55//! ```
56
57mod attention;
58mod distilbert_model;
59mod embeddings;
60mod transformer;
61
62pub use distilbert_model::{
63 DistilBertConfig, DistilBertConfigResources, DistilBertForQuestionAnswering,
64 DistilBertForSentenceEmbeddings, DistilBertForTokenClassification, DistilBertMaskedLMOutput,
65 DistilBertModel, DistilBertModelClassifier, DistilBertModelMaskedLM, DistilBertModelResources,
66 DistilBertQuestionAnsweringOutput, DistilBertSequenceClassificationOutput,
67 DistilBertTokenClassificationOutput, DistilBertVocabResources,
68};