Closed solaoi closed 4 months ago
MarianMT models are compatible with the current implementation using the sentencepiece
tokenizer. Below is a sample code:
use sentencepiece::SentencePieceProcessor;
use ct2rs::config::Config;
use ct2rs::translator::Translator;
let t = Translator::new("./data/opus-mt-en-jap", Config::default())?;
let encoder = SentencePieceProcessor::open("./data/opus-mt-en-jap/source.spm")?;
let decoder = SentencePieceProcessor::open("./data/opus-mt-en-jap/target.spm")?;
let source: Vec<String> = encoder.encode(
"Hello world! This library provides Rust bindings for CTranslate2.",
)?.iter().map(|v| v.piece.to_string()).collect();
let res = t.translate_batch(&*vec![source], &*vec![vec![""]], &Default::default())?;
for r in res {
if let Some(h) = r.hypotheses.get(0) {
println!("{:?}", decoder.decode_pieces(h)?);
}
}
Note that the model can be converted using the following command:
ct2-transformers-converter --model Helsinki-NLP/opus-mt-en-jap --output_dir data/opus-mt-en-jap
Additionally, ensure that source.spm
and target.spm
are copied from the repository to the directory data/opus-mt-en-jap
.
I am considering providing detailed instructions; however, I encourage you to experiment with the code provided above.
Thank you, it seems to work with the script you provided.
However, compared to when I run it in Python, it feels like the translation accuracy has degraded for all models based on MarianMT.
Do I need to set parameters like beam_size
in TranslationOptions?
I tried using the MarianMT model in Python, but the results were not good either. As you mentioned, the default translation options may not be optimal for this model.
Below is the code I used (taken from CTranslate2's docs):
import ctranslate2
import transformers
translator = ctranslate2.Translator("data/opus-mt-en-jap")
tokenizer = transformers.AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-jap")
source = tokenizer.convert_ids_to_tokens(tokenizer.encode("Hello world! This library provides Rust bindings for CTranslate2."))
results = translator.translate_batch([source])
target = results[0].hypotheses[0]
print(tokenizer.decode(tokenizer.convert_tokens_to_ids(target)))
The output I obtained was:
世界 は 上 を は き た. この 築 い た 荷 は, クミン を 描 く.
Could you share the specific parameters or settings you are using for the MarianMT models?
I compared the following three MarianMT-based models by varying the beam_size and repetition_penalty, using the provided Python code and ctranslate2-rs:
Helsinki-NLP/opus-mt-en-jap
Hoax0930/marian-finetuned-kde4-en-to-ja_kftt
skata/fugumt-en-ja
世界 は 上 を は き た. この 築 い た 荷 は, クミン を 描 く.
世 に は 陰府 を も つ. この 築 い た 荷 は, クミン に 対 し て 用意 を さ れ て い る.
世界 に は 希望 を 下 す. この 築 き 建て る 荷 は, クミン に 属 する もの で あ る.
世 に は 長 い もの で あ る. この 築 い た 所 は, クプロ を 描 き, 帆 を 造 る もの で あ る.
世界 に は 希望 を 下 す. この 築 き 建て る 荷 は, クミン に 属 する もの で あ る.
世界 を おお う もの で あ る. この 築 い た 荷 は, クミン に と っ て, 帆 を つく る もの で あ る.
世界 を おお う もの で あ る. この 築 い た 荷 は, クミン に 対 し て くい もの で あ る.
その 勢夫 マケナテ は 去 っ て ザア と い い, クプラび と ボアカデ の ひとり で あ る. 彼 は ▁shal を 避け て い る 者 で あ り, 威厳 が あ る. あなた は い と 高 き 者 の 最初 の 年 に, 警戒 の 年 に あ っ て, 受 く こと が でき る.
その 勢夫 マケナ の よう に 行 き, ボヅパ と い う 人 の 姿 が 知 ら れ て い る. 彼 は スカル の 人々 で あ り, 岩 を 打 つ. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 と い う 者 の 証言 と で あ る.
その 頭 を 打 つ こと は 細か く, クプラ の 女 の ひとり で あ っ て, 人 に 属 する 男 や ぎ, 岩 だ ぬき を 知 る 者 が あ る. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 の 初め で あ っ て, その 堅 く 立 つ.
その 足 の 響き を 知 る よう に, 寄 っ て 行 き なさ い. しゃこ に は 姿 が あ り, 威厳 が あ り, 岩 の 頂 は 岩 で あ る. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 の 初め で あ っ て, その 堅 く 立 つ こと が でき る.
その 足 の 響き を 知 る よう に, 寄 っ て 行 き なさ い. しゃこ に は 姿 が あ り, 威厳 が あ り, 岩 の 人々 で あ る. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 の 初め で あ っ て, その 堅 く 立 つ こと が でき る.
その 頂 を 過ぎ る こと は, ボヅパテ に も 知 ら れ る. 人 に 属 する 者, 寄 る べ な き 者 の ひとり で あ る. しえ たげ る 者, 岩 だ から で あ る. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 と い う 者 の 最初 の 年 で あ る.
その 足 の 響き を 知 る よう に, 寄 っ て 行 き なさ い. しゃこ に は 姿 が あ り, 威厳 が あ り, 岩 の 人々 で あ る. これ は い と 高 き 者 の 最初 の 年 で あ り, 警戒 の 初め で あ っ て, その 堅 く 立 つ こと が でき る.
⁇ . この築きは,ものマンナをくもの,クミンにくいくものであって,それにアスファルトをつくり,
⁇ . この世界はめいくもの,クミンにくいもの,クミンにくいもの備えて置くものがそれである.
⁇ . この築きはめるべくもの,クミンにくいものがあり, 道具で備えられ,
⁇ . この築きはめるべくもの,クミンにくいれるもの,クミンにくいものがそれであるが備えられる.
⁇ . この築きはめるべくもの,クミンがそこに置くものであって,それにアスファルト ん だ ものである.
⁇ . この築きはめるべくもの,クミンにくいれるもの,クミンにくいものがそれであるが備えられる.
⁇ . この築きはめるべくもの,クミンにくい入りがあり, 道具で備えられ,
⁇ ナ の よう に 行 き , ボヅパテ に は 姿 が あ り , 人 に は 姿 が あ る . 岩 , ボケル ・ アビム の 年 に は , 威勢深 い 者 が あ る . これ は い と 高 き 者 の 最初 の 年 で あ り , 警戒 の 初め で あ っ て , その 堅 く 立 つ .
⁇ ナ の よう に 行 き , ボヅパテ に は 姿 が あ り , 人 に は 姿 が あ る . 岩 に は 岩 が あ り , 威厳 が あ る . これ は い と 高 き 者 の 最初 の 年 で あ り , つり とげ の 初め で あ る .
⁇ ナマイ の よう に , そこ に 行 き , ボヅパ と い う 人 の うち の ベネケル は 知 ら れ て い る . 彼 は ▁shal を 打 つ 者 , 岩 を 失 う 者 で あ る . これ は い と 高 き 者 の 最初 の 年 で あ り , 警戒 の 初め で あ っ て , その 堅 く 立 つ .
⁇ エル の よう に 行 っ て 威厳 が あ り , ボヅパ の 男 , ボヅパ の 男 で , 人 の 姿 が あ る . あなた は この 岩 を 静め る . これ は い と 高 き 者 の 最初 の 年 で あ り , つり とげ の あ る .
⁇ ナマイ の よう に , そこ に 行 き , ボヅパ と い う 人 の うち の ベネケル は 知 ら れ て い る . 彼 は ▁shal を 打 つ 者 , 岩 を 失 う 者 で あ る . これ は い と 高 き 者 の 最初 の 年 で あ り , 警戒 の 初め で あ っ て , その 堅 く 立 つ .
⁇ エル の よう に 行 っ て 驚 き , さとげ の よう に 人 , ボヅパ の 人 で あ る . あなた は 言 う , \" メケル ・ アビム , スケル \" と い う . この 事 は い と 高 き 者 の 最初 の 年 で あ り , つや さ とげ の 年 で あ る .
⁇ ナマイ の よう に , そこ に 行 き , ボヅパ と い う 人 の うち の ベネケル は 知 ら れ て い る . 彼 は ▁shal を 打 つ 者 , 岩 を 失 う 者 で あ る . これ は い と 高 き 者 の 最初 の 年 で あ り , 警戒 の 初め で あ っ て , その 堅 く 立 つ .
Hello World! このライブラリは CTranstelt2 のラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
Hello World! このライブラリは CTranslate2にラプターバインドを提供します。
azerbaijan@kde.gr.jp,support@pluto.dti.ne.jp,support@pluto.dti.ne.jp,sgtom@pluto.dti.ne.jp,support@pluto.dti.ne.jp,sgtom@pluto.dti.ne.jp,sgtom@pluto.dion.ne.jp
azerbaijan@kde.gr.jp,support@pluto.dti.ne.jp,support@pluto.dti.ne.jp,ybando@k6.dion.ne.jp,ybando@k6.dion.ne.jp
azerbaijan@kde.gr.jp,shinobo@leo.bekkoame.ne.jp,tsuno@ngy.1st.ne.jp
azerbaijan@kde.gr.jp,ybando@k6.dion.ne.jp,ybando@k6.dion.ne.jp,ybando@k6.dion.ne.jpEMAIL OF TRANSLATORS
azerbaijan@kde.gr.jp,shinobo@leo.bekkoame.ne.jp,tsuno@ngy.1st.ne.jp
City name (optional, probably does not need a translation). http://europa.eu.int/europa.int/eur-lex/lex/LexUriServ/LexUriServ.do?uri=CELEX:32001L0059:EN:HTML
azerbaijan@kde.gr.jp,shinobo@leo.bekkoame.ne.jp,tsuno@ngy.1st.ne.jp
⁇ Hello Woro! このライブラリは CTCrrassrate2.CTCTranslate2.CTClasslat のスルバフメントを提供します。
⁇ Hello Woro! このライブラリは CTCrrassrate2.CTCTransslate2.CTClasslatra2.sslバインドを提供します。
⁇ Hello Work! このライブラリは CTCrransslate2.CTclassrat2.CTRAranslad のスラップバインドを提供します。
⁇ Hello Woro! このライブラリは CTCrrassrate2.CTCTranslate2.CTClasslat2.sslバインドを提供します。
⁇ Hello Work! このライブラリは CTCrasslatrad 2.CTcranslate2.
⁇ Hello Woro! このライブラリは CTCrrassrate2.CTCTranslate2.CTClasslat のスラップバインドを提供します。
⁇ Hello Work! このライブラリは CTCrasslatrad 2.CTcranslate2.
⁇ 尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚一一の始に在りししに属して大のギターのギターとの音楽を担当との音楽を担当とい.
⁇ 尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚尚一一年に在りて,Kesokuroboard.Kosoroboard.Kosoroboard
⁇ 南ゴホハは、またボクチチャンとして知られていますが、マンガやアニメシリーズの中の主役の一人です。Kuiki Kochhiro Okul!
⁇ 南ゴホハは、またボクチチャンとして知られていますが、マンガやアニメシリーズ、ボクチ・ロックの主要キャラの1人です。Schka高等大学1年目になり、バンドのギターと歌詞を担当しています。KesokuRand.KesokuRand.KosoRand.
⁇ 南ゴホハは、またボクチチャンとして知られていますが、マンガやアニメシリーズの中の主役の一人です。Kuoki Kusoroboard.
⁇ 南ゴホハは、またボクチチャンとして知られていますが、マンガやアニメシリーズ、ボクチ・ロックの主要キャラの1つで、Kochichi Kochi Kochi Kochi Hochi-Goh はシュカ高等学1年目になり、バンドのギターと歌詞を担当しています。KesokuRand.KesokuRand.
⁇ 南ゴホハは、またボクチチャンとして知られていますが、マンガやアニメシリーズの中の主役の一人です。Kuoki Kusoroboard. http://echika.kdeia.jp/artichi.ne.jpEMAIL OF TRANSLATORS
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
このライブラリはCTranslate2にRustバインディングを提供します。
漫画・アニメシリーズ「ボッキ・ザ・ロック!」の主人公のひとりで、修歌高校1年生で、バンド「けっそくバンド」のギターと歌詞を担当。
漫画・アニメシリーズ「Bocchi the Rock!」の主人公のひとりで、修歌高校1年生の時、バンド「けっそくバンド」のギターと歌詞を担当。
漫画・アニメシリーズ「ボッキザロック!」の主人公の一人である「ボッチちゃん」は、修歌高校1年生でギターと歌詞を担当している。
漫画・アニメシリーズ「Bocchi the Rock!」の主人公のひとりで、修歌高校1年生の時、バンド「けっそくバンド」のギターと歌詞を担当。
漫画・アニメシリーズ『ボッキザロック!』の主人公である「ボッチちゃん」は、修歌高校1年生でギターと歌詞を担当している。
漫画・アニメシリーズ「Bocchi the Rock!」の主人公のひとりで、修歌高校1年生の時、バンド「けっそくバンド」のギターと歌詞を担当。
漫画・アニメシリーズ「ボッキザロック!」の主人公の一人である「ボッチちゃん」は、修歌高校1年生でギターと歌詞を担当している。
⁇ 、こんにちは、世界!こんにちは、こんにちは、世界!このライブラリは、このライブラリがCTranslate2.....にCTranslate2.....にCTranslate2.....にCTranslate2....にRustバインディングを提供するCTranslate2..にRustバインディングを提供するRust bindings for CTranslate2 for CTranslate2.にRust Rustを提供するライブラリです。このライブラリは、CTranslate2.
⁇ 、こんにちは、世界!こんにちは、こんにちは、世界!このライブラリは、このライブラリは、CTranslate2...にCTranslate2...にCTranslate2.....にCTranslate2...にCTranslate2..にRustバインディングのRust バインディングを提供している。このライブラリは、このライブラリが提供する。このライブラリは、CTranslate2にRustバインディングを提供する。
⁇ こんにちは、世界!このライブラリが提供するRustバインディング for CTranslate 2.2.
⁇ このライブラリライブラリはCTranslate2...にCTranslate2....にCTranslate2...にRustバインディングを提供する。このライブラリはCTranslate2..にRustバインディングを提供する。このライブラリはCTranslate2..にRustバインディングを提供する。
⁇ このライブラリは、CTranslate 2.2.
⁇ このライブラリライブラリはCTranslate2...にCTranslate2....にCTranslate2...にRustバインディングを提供する。このライブラリはCTranslate2..にRustバインディングを提供する。このライブラリはCTranslate2..にRustバインディングを提供する。
⁇ このライブラリは、CTranslate 2.2.
⁇ ちちゃんとしても知られるボッチちゃんとしても知られるボッチチちゃんとしても知られるボッチちゃんのキャラクターの1人で、漫画・アニメシリーズの主人公の1人である漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッチ・ザ・ロック!ボッチ・ザ・ロック!」の主人公の1人である。彼女は修化高校1年生の修化高校1年生で、シューカ高校の1年生で、バンド「けそくバンド」のギターと歌詞とバンドのギターを担当している。
⁇ ちちゃんとしても知られるボッチチちゃんとしても知られるボッチチちゃんとしても知られるボッチチ・ヒトリ・ヒトリ・ヒトリ・ヒトリ・ヒトリ・ゴトー(ボッチチ・ヒトリ・ヒトリ・ヒトリ・ゴトー)は、漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッチ・ボッチ・ザ・ロック!」の主人公の1人であり、漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッキ・ザ・ボッチ・ザ・ロック!」の主人公の主人公の1人である。
⁇ 知ちゃんとしても知られるボッチチチャン(ボッキちちゃ)として知られる「ボッキー・ヒトリひとり日鳥ごとう」は、漫画やアニメシリーズ『Bocchi the Rock!』の主人公の一人でマンガンおよびアニメ連作アニメーション系の主要キャラクターの一つである。彼女は修加高校初年度に就き、シュカ高学園1年生でありバンドケソクバントのギター&歌詞担当、キーソック帯けそく Band.のギタリング/歌詞を担当している他、シューッカハイスクールのギター兼首高校一年に入っていて、バンドK雪石石石楽帯域をバンドする血 ⁇ 組気 ⁇ グループであるバンド、「けそくうか束」「けっ独バンド」(けそくば...
⁇ 知ちゃんとしても知られるボッチちゃんとしても知られるボッチチちゃんとしても知られるボッチチ・ヒトリ・ヒトリ・ヒトリ・ヒトリ・ヒトリ・ゴトー(ボッチチ・ヒトリ・ヒトリ・ヒトリ・ゴトー)は、漫画・アニメシリーズ『ボッチ・ザ・ロック!ボッチ・ボッチ・ザ・ロック!ボッチ・ザ・ロック!』の主人公の1人である漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッチ・ボッチ・ザ・ロック!ボッチ・ザ・ロック!」の主人公で、漫画・アニメシリーズ「ボッチ・ザロック!ボッチ・ボッチ・ザ・ロック!
⁇ 知ちゃんとしても知られるボッチチチャン(ボッキちちゃ)として知られる「ボッキー・ヒトリひとり日鳥ごとう」は、漫画やアニメシリーズの主人公である『Bocchi the Rock!』の主要キャラクター1人です。彼女はシュカ高校初年で修化高学年の1年生でありバンドケソクバントのギター&歌詞を担当している。
⁇ 知ちゃんとしても知られるボッチちゃんとしても知られるボッチチちゃんとしても知られるボッチチ・ヒトリ・ヒトリ・ヒトリ・ヒトリ・ゴトー(ボッチチ・ヒトリ・ヒトリ・ヒトリ・ゴトー)は、漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッチ・ザ・ロック!ボッチ・ザ・ロック!」の主人公の1人である漫画・アニメシリーズ「ボッチ・ザ・ザ・ロック!ボッチ・ザ・ボッチ・ザ・ロック!」の主人公の主人公の1人である漫画・アニメシリーズの主人公で、漫画・アニメシリーズ「ボッチ・ザ・ロック!ボッチ・ボッチ・ザ・ロック!
⁇ 知ちゃんとしても知られるボッチ・ヒトリひとり日鳥ゴトー(ボッチチャン)の1人で、漫画やアニメシリーズ「Bocchi the Rock!BOCCHI THEロック!」の主要キャラクターの一人である。シュカ高校初年度に就き、酒華高等学校の1年生でありバンドケソクバントのギター&歌詞を担当しているKecsoku Band.をギターおよび作詞担当する。
Thank you for sharing the data. I also tested the models you mentioned and discovered that the sentencepiece tokenizer does not append </s>
to the end of the token list. When I manually add </s>
, the output no longer contains repetitive text. Additionally, I have implemented a modification that omits the target prefix if the model does not require it (#40). With these updates, the outputs are more similar to those produced by Python.
Here is my newest code, btw:
use sentencepiece::SentencePieceProcessor;
use ct2rs::config::Config;
use ct2rs::translator::Translator;
let t = Translator::new("./data/opus-mt-en-jap", Config::default())?;
let encoder = SentencePieceProcessor::open("./data/opus-mt-en-jap/source.spm")?;
let decoder = SentencePieceProcessor::open("./data/opus-mt-en-jap/target.spm")?;
let mut source: Vec<String> = encoder.encode(
"Hello world! This library provides Rust bindings for CTranslate2.",
)?.iter().map(|v| v.piece.to_string()).collect();
source.push("</s>".to_string());
let res = t.translate_batch(vec![source], &Default::default())?;
for r in res {
if let Some(h) = r.hypotheses.get(0) {
println!("{:?}", decoder.decode_pieces(h)?);
}
}
Thank you! Everything worked perfectly with the main branch. The code you provided had just one typo below, but everything else was perfect!
I really appreciate your prompt response. I'm looking forward to future updates!
- let res = t.translate_batch(vec![source], &Default::default())?;
+ let res = t.translate_batch(&vec![source], &Default::default())?;
@jkawamoto Thank you for adding the sample document below. https://github.com/jkawamoto/ctranslate2-rs/blob/main/examples/marian-mt.rs
I tried using version 0.7.3, but it seems that Translator::with_tokenizer
does not exist.
It works when I use new
as shown below. Is there something wrong with my implementation?
use ct2rs::config::Config;
use ct2rs::sentencepiece::Tokenizer;
use ct2rs::TranslationOptions;
use ct2rs::Translator;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let text = std::env::args()
.nth(1)
.unwrap_or("Hello world! This library provides Rust bindings for CTranslate2.".to_string());
let model_path = "./mymodel";
let t = Translator::new(
&model_path,
Tokenizer::new(&model_path)?,
&Config::default(),
)?;
let sources: Vec<String> = text.lines().map(String::from).collect();
let res = t.translate_batch(
&sources,
&TranslationOptions {
beam_size: 5,
..Default::default()
},
)?;
for (r, _) in res {
print!("{}", r);
}
Ok(())
}
Your code looks good with v0.7.3.
Please refer to the example at v0.7.3 instead of the one on the main
branch. The main
branch is currently aimed at v0.8.0, which includes some breaking changes.
Thank you for your quick response! I’ll check the v0.7.3 example as suggested.
Thank you for your great work! I would like to use this with the MarianMT Model as described here: https://opennmt.net/CTranslate2/guides/transformers.html#marianmt
I would appreciate your support on this.