rust_bert/models/mobilebert/mod.rs
1//! # MobileBERT (A Compact Task-agnostic BERT for Resource-Limited Devices)
2//!
3//! Implementation of the MobileBERT language model ([MobileBERT: A Compact Task-agnostic BERT for Resource-Limited Devices](https://arxiv.org/abs/2004.02984) Sun, Yu, Song, Liu, Yang, Zhou, 2020).
4//! The base model is implemented in the `mobilebert_model::MobileBertModel` struct. Several language model heads have also been implemented, including:
5//! - Multiple choices: `mobilebert_model:MobileBertForMultipleChoice`
6//! - Question answering: `mobilebert_model::MobileBertForQuestionAnswering`
7//! - Sequence classification: `mobilebert_model::MobileBertForSequenceClassification`
8//! - Token classification (e.g. NER, POS tagging): `mobilebert_model::MobileBertForTokenClassification`.
9//!
10//! # Model set-up and pre-trained weights loading
11//!
12//! All models expect the following resources:
13//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
14//! - 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.
15//! - `BertTokenizer` using a `vocab.txt` vocabulary
16//!
17//! Pretrained models for a number of language pairs are available and can be downloaded using RemoteResources.
18//!
19//! ```no_run
20//! # fn main() -> anyhow::Result<()> {
21//! #
22//! use tch::{nn, Device};
23//! # use std::path::PathBuf;
24//! use rust_bert::mobilebert::{
25//! MobileBertConfig, MobileBertConfigResources, MobileBertForMaskedLM,
26//! MobileBertModelResources, MobileBertVocabResources,
27//! };
28//! use rust_bert::resources::{RemoteResource, ResourceProvider};
29//! use rust_bert::Config;
30//! use rust_tokenizers::tokenizer::BertTokenizer;
31//!
32//! let config_resource =
33//! RemoteResource::from_pretrained(MobileBertConfigResources::MOBILEBERT_UNCASED);
34//! let vocab_resource =
35//! RemoteResource::from_pretrained(MobileBertVocabResources::MOBILEBERT_UNCASED);
36//! let weights_resource =
37//! RemoteResource::from_pretrained(MobileBertModelResources::MOBILEBERT_UNCASED);
38//! let config_path = config_resource.get_local_path()?;
39//! let vocab_path = vocab_resource.get_local_path()?;
40//! let weights_path = weights_resource.get_local_path()?;
41//! let device = Device::cuda_if_available();
42//! let mut vs = nn::VarStore::new(device);
43//! let tokenizer: BertTokenizer =
44//! BertTokenizer::from_file(vocab_path.to_str().unwrap(), true, true)?;
45//! let config = MobileBertConfig::from_file(config_path);
46//! let bert_model = MobileBertForMaskedLM::new(&vs.root(), &config);
47//! vs.load(weights_path)?;
48//!
49//! # Ok(())
50//! # }
51//! ```
52
53mod attention;
54mod embeddings;
55mod encoder;
56mod mobilebert_model;
57
58pub use mobilebert_model::{
59 MobileBertConfig, MobileBertConfigResources, MobileBertForMaskedLM,
60 MobileBertForMultipleChoice, MobileBertForQuestionAnswering,
61 MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertModel,
62 MobileBertModelResources, MobileBertVocabResources, NoNorm, NormalizationType,
63};