feat: ORT Local Translate

This commit is contained in:
xkeyC
2025-11-15 17:58:42 +08:00
parent 58da84c0a6
commit 3219129094
19 changed files with 619 additions and 232 deletions

134
rust/Cargo.lock generated
View File

@@ -17,6 +17,20 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"getrandom 0.3.4",
"once_cell",
"serde",
"version_check",
"zerocopy",
]
[[package]]
name = "aho-corasick"
version = "1.1.3"
@@ -401,6 +415,15 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
[[package]]
name = "castaway"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a"
dependencies = [
"rustversion",
]
[[package]]
name = "cc"
version = "1.2.43"
@@ -508,6 +531,21 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "compact_str"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb1325a1cece981e8a296ab8f0f9b63ae357bd0784a9faaf548cc7b480707a"
dependencies = [
"castaway",
"cfg-if",
"itoa",
"rustversion",
"ryu",
"serde",
"static_assertions",
]
[[package]]
name = "compression-codecs"
version = "0.4.31"
@@ -756,6 +794,15 @@ dependencies = [
"cc",
]
[[package]]
name = "dary_heap"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06d2e3287df1c007e74221c49ca10a95d557349e54b3a75dc2fb14712c751f04"
dependencies = [
"serde",
]
[[package]]
name = "dashmap"
version = "5.5.3"
@@ -1322,7 +1369,7 @@ dependencies = [
"idna",
"ipnet",
"once_cell",
"rand 0.9.2",
"rand",
"ring",
"thiserror 2.0.17",
"tinyvec",
@@ -1344,7 +1391,7 @@ dependencies = [
"moka",
"once_cell",
"parking_lot",
"rand 0.9.2",
"rand",
"resolv-conf",
"smallvec 1.15.1",
"thiserror 2.0.17",
@@ -1693,9 +1740,9 @@ dependencies = [
[[package]]
name = "itertools"
version = "0.12.1"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
@@ -1946,6 +1993,21 @@ dependencies = [
"rawpointer",
]
[[package]]
name = "ndarray"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c7c9125e8f6f10c9da3aad044cc918cf8784fa34de857b1aa68038eb05a50a9"
dependencies = [
"matrixmultiply",
"num-complex",
"num-integer",
"num-traits",
"portable-atomic",
"portable-atomic-util",
"rawpointer",
]
[[package]]
name = "nix"
version = "0.30.1"
@@ -2172,7 +2234,7 @@ version = "2.0.0-rc.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa7e49bd669d32d7bc2a15ec540a527e7764aec722a45467814005725bcd721"
dependencies = [
"ndarray",
"ndarray 0.16.1",
"ort-sys",
"smallvec 2.0.0-alpha.10",
"tracing",
@@ -2421,7 +2483,7 @@ dependencies = [
"bytes",
"getrandom 0.3.4",
"lru-slab",
"rand 0.9.2",
"rand",
"ring",
"rustc-hash",
"rustls",
@@ -2462,35 +2524,14 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha 0.9.0",
"rand_core 0.9.3",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.4",
"rand_chacha",
"rand_core",
]
[[package]]
@@ -2500,16 +2541,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core 0.9.3",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom 0.2.16",
"rand_core",
]
[[package]]
@@ -2539,12 +2571,12 @@ dependencies = [
[[package]]
name = "rayon-cond"
version = "0.3.0"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "059f538b55efd2309c9794130bc149c6a553db90e9d99c2030785c82f0bd7df9"
checksum = "2964d0cf57a3e7a06e8183d14a8b527195c706b7983549cd5462d5aa3747438f"
dependencies = [
"either",
"itertools 0.11.0",
"itertools 0.14.0",
"rayon",
]
@@ -2695,7 +2727,7 @@ dependencies = [
"flutter_rust_bridge",
"futures",
"hickory-resolver",
"ndarray",
"ndarray 0.17.1",
"notify-rust",
"once_cell",
"ort",
@@ -3301,22 +3333,24 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokenizers"
version = "0.20.4"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b08cc37428a476fc9e20ac850132a513a2e1ce32b6a31addf2b74fa7033b905"
checksum = "6475a27088c98ea96d00b39a9ddfb63780d1ad4cceb6f48374349a96ab2b7842"
dependencies = [
"ahash",
"aho-corasick",
"compact_str",
"dary_heap",
"derive_builder",
"esaxx-rs",
"getrandom 0.2.16",
"itertools 0.12.1",
"lazy_static",
"getrandom 0.3.4",
"itertools 0.14.0",
"log",
"macro_rules_attribute",
"monostate",
"onig",
"paste",
"rand 0.8.5",
"rand",
"rayon",
"rayon-cond",
"regex",
@@ -3324,7 +3358,7 @@ dependencies = [
"serde",
"serde_json",
"spm_precompiled",
"thiserror 1.0.69",
"thiserror 2.0.17",
"unicode-normalization-alignments",
"unicode-segmentation",
"unicode_categories",

View File

@@ -24,8 +24,8 @@ notify-rust = "4"
asar = "0.3.0"
walkdir = "2.5.0"
ort = { version = "2.0.0-rc.10", features = ["xnnpack", "download-binaries", "ndarray"] }
tokenizers = { version = "0.20", default-features = false, features = ["onig"] }
ndarray = "0.16"
tokenizers = { version = "0.22", default-features = false, features = ["onig"] }
ndarray = "0.17"
serde_json = "1.0"
[target.'cfg(windows)'.dependencies]

View File

@@ -245,10 +245,16 @@ impl OpusMtModel {
.context("Failed to create attention_mask array")?;
// 3. 运行 encoder
let input_ids_value =
Value::from_array(input_ids_array).context("Failed to create input_ids value")?;
let attention_mask_value = Value::from_array(attention_mask_array.clone())
.context("Failed to create attention_mask value")?;
let input_ids_value = Value::from_array((
input_ids_array.shape().to_vec(),
input_ids_array.into_raw_vec_and_offset().0,
))
.context("Failed to create input_ids value")?;
let attention_mask_value = Value::from_array((
attention_mask_array.shape().to_vec(),
attention_mask_array.clone().into_raw_vec_and_offset().0,
))
.context("Failed to create attention_mask value")?;
let encoder_inputs = ort::inputs![
"input_ids" => input_ids_value,
@@ -303,12 +309,21 @@ impl OpusMtModel {
.context("Failed to create decoder input_ids")?;
// 创建 ORT Value
let decoder_input_value = Value::from_array(decoder_input_ids)
.context("Failed to create decoder input value")?;
let encoder_hidden_value = Value::from_array(encoder_hidden_states.clone())
.context("Failed to create encoder hidden value")?;
let encoder_mask_value = Value::from_array(encoder_attention_mask.clone())
.context("Failed to create encoder mask value")?;
let decoder_input_value = Value::from_array((
decoder_input_ids.shape().to_vec(),
decoder_input_ids.into_raw_vec_and_offset().0,
))
.context("Failed to create decoder input value")?;
let encoder_hidden_value = Value::from_array((
encoder_hidden_states.shape().to_vec(),
encoder_hidden_states.clone().into_raw_vec_and_offset().0,
))
.context("Failed to create encoder hidden value")?;
let encoder_mask_value = Value::from_array((
encoder_attention_mask.shape().to_vec(),
encoder_attention_mask.clone().into_raw_vec_and_offset().0,
))
.context("Failed to create encoder mask value")?;
// 运行 decoder
let decoder_inputs = ort::inputs![