rust_bert/models/marian/mod.rs
1//! # Marian
2//!
3//! Implementation of the Marian language model ([Marian: Fast Neural Machine Translation in {C++}](http://www.aclweb.org/anthology/P18-4020) Junczys-Dowmunt, Grundkiewicz, Dwojak, Hoang, Heafield, Neckermann, Seide, Germann, Fikri Aji, Bogoychev, Martins, Birch, 2018).
4//! The base model is implemented in the `bart_model::BartModel` struct. This model includes a language model head: `marian_model::MarianForConditionalGeneration`
5//! implementing the common `generation_utils::LanguageGenerator` trait shared between the models used for generation (see `pipelines` for more information).
6//!
7//! # Model set-up and pre-trained weights loading
8//!
9//! A full working example is provided in `examples/translation_marian`, run with `cargo run --example translation_marian`.
10//! All models expect the following resources:
11//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
12//! - 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.
13//! - `MarianTokenizer` using a `vocab.json` vocabulary and `spiece.model` sentence piece model
14//!
15//! Pretrained models for a number of language pairs are available and can be downloaded using RemoteResources. These are shared under Creative Commons Attribution 4.0 International License license by the Opus-MT team from Language Technology at the University of Helsinki at <https://github.com/Helsinki-NLP/Opus-MT>.
16//!
17//! ```no_run
18//! # fn main() -> anyhow::Result<()> {
19//! #
20//! use tch::{nn, Device};
21//! # use std::path::PathBuf;
22//! use rust_bert::bart::{BartConfig, BartModel};
23//! use rust_bert::marian::MarianForConditionalGeneration;
24//! use rust_bert::resources::{LocalResource, ResourceProvider};
25//! use rust_bert::Config;
26//! use rust_tokenizers::tokenizer::MarianTokenizer;
27//!
28//! let config_resource = LocalResource {
29//! local_path: PathBuf::from("path/to/config.json"),
30//! };
31//! let vocab_resource = LocalResource {
32//! local_path: PathBuf::from("path/to/vocab.json"),
33//! };
34//! let sentence_piece_resource = LocalResource {
35//! local_path: PathBuf::from("path/to/spiece.model"),
36//! };
37//! let weights_resource = LocalResource {
38//! local_path: PathBuf::from("path/to/model.ot"),
39//! };
40//! let config_path = config_resource.get_local_path()?;
41//! let vocab_path = vocab_resource.get_local_path()?;
42//! let spiece_path = sentence_piece_resource.get_local_path()?;
43//! let weights_path = weights_resource.get_local_path()?;
44//!
45//! let device = Device::cuda_if_available();
46//! let mut vs = nn::VarStore::new(device);
47//! let tokenizer = MarianTokenizer::from_files(
48//! vocab_path.to_str().unwrap(),
49//! spiece_path.to_str().unwrap(),
50//! true,
51//! );
52//! let config = BartConfig::from_file(config_path);
53//! let marian_model = MarianForConditionalGeneration::new(&vs.root(), &config);
54//! vs.load(weights_path)?;
55//!
56//! # Ok(())
57//! # }
58//! ```
59
60mod marian_model;
61
62pub use marian_model::{
63 MarianConfig, MarianConfigResources, MarianForConditionalGeneration, MarianGenerator,
64 MarianModelPreset, MarianModelResources, MarianSourceLanguages, MarianSpmResources,
65 MarianTargetLanguages, MarianVocabResources,
66};