rust_bert/models/prophetnet/
mod.rs

1//! # ProphetNet (ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training)
2//!
3//! Implementation of the ProphetNet language model ([ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://arxiv.org/abs/2001.04063) Qi, Yan, Gong, Liu, Duan, Chen, Zhang, Zhou, 2020).
4//! The base model is implemented in the `prophetnet_model::ProphetNetModel` struct. Two language model heads have also been implemented:
5//! - Conditional language generation (encoder-decoder architecture): `prophetnet_model::ProphetNetForConditionalGeneration` implementing the common `generation_utils::LanguageGenerator` trait shared between the models used for generation (see `pipelines` for more information)
6//! - Causal language generation (decoder architecture): `prophetnet_model::ProphetNetForCausalGeneration`
7//!
8//! # Model set-up and pre-trained weights loading
9//!
10//! A full working example (summarization) is provided in `examples/summarization_prophetnet`, run with `cargo run --example summarization_prophetnet`.
11//! All models expect the following resources:
12//! - Configuration file expected to have a structure following the [Transformers library](https://github.com/huggingface/transformers)
13//! - 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.
14//! - `ProphetNetTokenizer` using a `vocab.txt` vocabulary
15//!
16//!
17//! ```no_run
18//! use rust_bert::pipelines::common::ModelType;
19//! use rust_bert::pipelines::summarization::{SummarizationConfig, SummarizationModel};
20//! use rust_bert::prophetnet::{
21//!     ProphetNetConfigResources, ProphetNetModelResources, ProphetNetVocabResources,
22//! };
23//! use rust_bert::resources::RemoteResource;
24//! use tch::Device;
25//!
26//! fn main() -> anyhow::Result<()> {
27//!     use rust_bert::pipelines::common::ModelResource;
28//!     let config_resource = Box::new(RemoteResource::from_pretrained(
29//!         ProphetNetConfigResources::PROPHETNET_LARGE_CNN_DM,
30//!     ));
31//!     let vocab_resource = Box::new(RemoteResource::from_pretrained(
32//!         ProphetNetVocabResources::PROPHETNET_LARGE_CNN_DM,
33//!     ));
34//!     let weights_resource = Box::new(RemoteResource::from_pretrained(
35//!         ProphetNetModelResources::PROPHETNET_LARGE_CNN_DM,
36//!     ));
37//!
38//!     let summarization_config = SummarizationConfig {
39//!         model_type: ModelType::ProphetNet,
40//!         model_resource: ModelResource::Torch(weights_resource),
41//!         config_resource,
42//!         vocab_resource,
43//!         merges_resource: None,
44//!         length_penalty: 1.2,
45//!         num_beams: 4,
46//!         no_repeat_ngram_size: 3,
47//!         device: Device::cuda_if_available(),
48//!         ..Default::default()
49//!     };
50//!     let summarization_model = SummarizationModel::new(summarization_config)?;
51//!
52//!     let input = ["In findings published Tuesday in Cornell University's arXiv by a team of scientists \
53//! from the University of Montreal and a separate report published Wednesday in Nature Astronomy by a team \
54//! from University College London (UCL), the presence of water vapour was confirmed in the atmosphere of K2-18b, \
55//! a planet circling a star in the constellation Leo. This is the first such discovery in a planet in its star's \
56//! habitable zone — not too hot and not too cold for liquid water to exist. The Montreal team, led by Björn Benneke, \
57//! used data from the NASA's Hubble telescope to assess changes in the light coming from K2-18b's star as the planet \
58//! passed between it and Earth. They found that certain wavelengths of light, which are usually absorbed by water, \
59//! weakened when the planet was in the way, indicating not only does K2-18b have an atmosphere, but the atmosphere \
60//! contains water in vapour form. The team from UCL then analyzed the Montreal team's data using their own software \
61//! and confirmed their conclusion. This was not the first time scientists have found signs of water on an exoplanet, \
62//! but previous discoveries were made on planets with high temperatures or other pronounced differences from Earth. \
63//! \"This is the first potentially habitable planet where the temperature is right and where we now know there is water,\" \
64//! said UCL astronomer Angelos Tsiaras. \"It's the best candidate for habitability right now.\" \"It's a good sign\", \
65//! said Ryan Cloutier of the Harvard–Smithsonian Center for Astrophysics, who was not one of either study's authors. \
66//! \"Overall,\" he continued, \"the presence of water in its atmosphere certainly improves the prospect of K2-18b being \
67//! a potentially habitable planet, but further observations will be required to say for sure. \" \
68//! K2-18b was first identified in 2015 by the Kepler space telescope. It is about 110 light-years from Earth and larger \
69//! but less dense. Its star, a red dwarf, is cooler than the Sun, but the planet's orbit is much closer, such that a year \
70//! on K2-18b lasts 33 Earth days. According to The Guardian, astronomers were optimistic that NASA's James Webb space \
71//! telescope — scheduled for launch in 2021 — and the European Space Agency's 2028 ARIEL program, could reveal more \
72//! about exoplanets like K2-18b."];
73//!
74//!     //    Credits: WikiNews, CC BY 2.5 license (https://en.wikinews.org/wiki/Astronomers_find_water_vapour_in_atmosphere_of_exoplanet_K2-18b)
75//!     let _output = summarization_model.summarize(&input)?;
76//!     for sentence in _output {
77//!         println!("{}", sentence);
78//!     }
79//!
80//!     Ok(())
81//! }
82//! ```
83
84mod attention;
85mod decoder;
86mod embeddings;
87mod encoder;
88mod prophetnet_model;
89
90pub use attention::LayerState;
91pub use prophetnet_model::{
92    ProphetNetConditionalGenerator, ProphetNetConfig, ProphetNetConfigResources,
93    ProphetNetForCausalGeneration, ProphetNetForConditionalGeneration, ProphetNetGenerationOutput,
94    ProphetNetModel, ProphetNetModelResources, ProphetNetOutput, ProphetNetVocabResources,
95};