red-candle 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +11 -20
- data/ext/candle/Cargo.toml +1 -1
- data/ext/candle/src/ruby/device.rs +8 -7
- data/ext/candle/src/ruby/dtype.rs +3 -2
- data/ext/candle/src/ruby/embedding_model.rs +31 -14
- data/ext/candle/src/ruby/errors.rs +6 -4
- data/ext/candle/src/ruby/llm.rs +78 -68
- data/ext/candle/src/ruby/ner.rs +106 -95
- data/ext/candle/src/ruby/reranker.rs +51 -38
- data/ext/candle/src/ruby/structured.rs +13 -12
- data/ext/candle/src/ruby/tensor.rs +7 -6
- data/ext/candle/src/ruby/tokenizer.rs +101 -84
- data/lib/candle/version.rb +1 -1
- metadata +31 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a3ac57ccd28cb2eb647c5a71f42fdbdf84a9dcaabd906165daca3d57c4a8eb0
|
|
4
|
+
data.tar.gz: 283104e93802ac97f11525226c9e41dc3bebccb8706d5b69a6a648d62cf2ccad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d5be5ca76fe5441ee1fd87ea83bcd02f5a630c3410a4dccc347da28290ef408c4c2cae428134f2a8fba839fe5aacf2b7593fe45274c967600e6684d09f212a01
|
|
7
|
+
data.tar.gz: 72289aaf0c17dea679acfa4f92be5371f2543293e27dab0752b39c77a1c2be98c480c0ce7266bc07519551770abdf53fdbbf0f4d3c1fdf6ba928c2452e598060
|
data/Cargo.lock
CHANGED
|
@@ -167,7 +167,7 @@ dependencies = [
|
|
|
167
167
|
"bitflags 2.9.4",
|
|
168
168
|
"cexpr",
|
|
169
169
|
"clang-sys",
|
|
170
|
-
"itertools 0.
|
|
170
|
+
"itertools 0.11.0",
|
|
171
171
|
"lazy_static",
|
|
172
172
|
"lazycell",
|
|
173
173
|
"proc-macro2",
|
|
@@ -1750,15 +1750,6 @@ dependencies = [
|
|
|
1750
1750
|
"either",
|
|
1751
1751
|
]
|
|
1752
1752
|
|
|
1753
|
-
[[package]]
|
|
1754
|
-
name = "itertools"
|
|
1755
|
-
version = "0.12.1"
|
|
1756
|
-
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1757
|
-
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
|
|
1758
|
-
dependencies = [
|
|
1759
|
-
"either",
|
|
1760
|
-
]
|
|
1761
|
-
|
|
1762
1753
|
[[package]]
|
|
1763
1754
|
name = "itertools"
|
|
1764
1755
|
version = "0.13.0"
|
|
@@ -1890,9 +1881,9 @@ checksum = "670fdfda89751bc4a84ac13eaa63e205cf0fd22b4c9a5fbfa085b63c1f1d3a30"
|
|
|
1890
1881
|
|
|
1891
1882
|
[[package]]
|
|
1892
1883
|
name = "magnus"
|
|
1893
|
-
version = "0.
|
|
1884
|
+
version = "0.8.2"
|
|
1894
1885
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1895
|
-
checksum = "
|
|
1886
|
+
checksum = "3b36a5b126bbe97eb0d02d07acfeb327036c6319fd816139a49824a83b7f9012"
|
|
1896
1887
|
dependencies = [
|
|
1897
1888
|
"magnus-macros",
|
|
1898
1889
|
"rb-sys",
|
|
@@ -1902,9 +1893,9 @@ dependencies = [
|
|
|
1902
1893
|
|
|
1903
1894
|
[[package]]
|
|
1904
1895
|
name = "magnus-macros"
|
|
1905
|
-
version = "0.
|
|
1896
|
+
version = "0.8.0"
|
|
1906
1897
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1907
|
-
checksum = "
|
|
1898
|
+
checksum = "47607461fd8e1513cb4f2076c197d8092d921a1ea75bd08af97398f593751892"
|
|
1908
1899
|
dependencies = [
|
|
1909
1900
|
"proc-macro2",
|
|
1910
1901
|
"quote",
|
|
@@ -2656,18 +2647,18 @@ dependencies = [
|
|
|
2656
2647
|
|
|
2657
2648
|
[[package]]
|
|
2658
2649
|
name = "rb-sys"
|
|
2659
|
-
version = "0.9.
|
|
2650
|
+
version = "0.9.124"
|
|
2660
2651
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2661
|
-
checksum = "
|
|
2652
|
+
checksum = "c85c4188462601e2aa1469def389c17228566f82ea72f137ed096f21591bc489"
|
|
2662
2653
|
dependencies = [
|
|
2663
2654
|
"rb-sys-build",
|
|
2664
2655
|
]
|
|
2665
2656
|
|
|
2666
2657
|
[[package]]
|
|
2667
2658
|
name = "rb-sys-build"
|
|
2668
|
-
version = "0.9.
|
|
2659
|
+
version = "0.9.124"
|
|
2669
2660
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2670
|
-
checksum = "
|
|
2661
|
+
checksum = "568068db4102230882e6d4ae8de6632e224ca75fe5970f6e026a04e91ed635d3"
|
|
2671
2662
|
dependencies = [
|
|
2672
2663
|
"bindgen 0.69.5",
|
|
2673
2664
|
"lazy_static",
|
|
@@ -2680,9 +2671,9 @@ dependencies = [
|
|
|
2680
2671
|
|
|
2681
2672
|
[[package]]
|
|
2682
2673
|
name = "rb-sys-env"
|
|
2683
|
-
version = "0.
|
|
2674
|
+
version = "0.2.3"
|
|
2684
2675
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
2685
|
-
checksum = "
|
|
2676
|
+
checksum = "cca7ad6a7e21e72151d56fe2495a259b5670e204c3adac41ee7ef676ea08117a"
|
|
2686
2677
|
|
|
2687
2678
|
[[package]]
|
|
2688
2679
|
name = "reborrow"
|
data/ext/candle/Cargo.toml
CHANGED
|
@@ -15,7 +15,7 @@ candle-transformers = { version = "0.9.1" }
|
|
|
15
15
|
tokenizers = { version = "0.22.0", default-features = true, features = ["fancy-regex"] }
|
|
16
16
|
hf-hub = "0.4.1"
|
|
17
17
|
half = "2.6.0"
|
|
18
|
-
magnus = "0.
|
|
18
|
+
magnus = "0.8"
|
|
19
19
|
safetensors = "0.3"
|
|
20
20
|
serde_json = "1.0"
|
|
21
21
|
serde = { version = "1.0", features = ["derive"] }
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
use magnus::Error;
|
|
2
|
-
use magnus::{function, method,
|
|
2
|
+
use magnus::{function, method, RModule, Module, Object, Ruby};
|
|
3
3
|
|
|
4
4
|
use ::candle_core::Device as CoreDevice;
|
|
5
5
|
use crate::ruby::Result;
|
|
@@ -101,7 +101,7 @@ impl Device {
|
|
|
101
101
|
#[cfg(not(feature = "cuda"))]
|
|
102
102
|
{
|
|
103
103
|
return Err(Error::new(
|
|
104
|
-
|
|
104
|
+
Ruby::get().unwrap().exception_runtime_error(),
|
|
105
105
|
"CUDA support not compiled in. Rebuild with CUDA available.",
|
|
106
106
|
));
|
|
107
107
|
}
|
|
@@ -115,7 +115,7 @@ impl Device {
|
|
|
115
115
|
#[cfg(not(feature = "metal"))]
|
|
116
116
|
{
|
|
117
117
|
return Err(Error::new(
|
|
118
|
-
|
|
118
|
+
Ruby::get().unwrap().exception_runtime_error(),
|
|
119
119
|
"Metal support not compiled in. Rebuild on macOS.",
|
|
120
120
|
));
|
|
121
121
|
}
|
|
@@ -139,7 +139,7 @@ impl Device {
|
|
|
139
139
|
#[cfg(not(feature = "cuda"))]
|
|
140
140
|
{
|
|
141
141
|
return Err(Error::new(
|
|
142
|
-
|
|
142
|
+
Ruby::get().unwrap().exception_runtime_error(),
|
|
143
143
|
"CUDA support not compiled in. Rebuild with CUDA available.",
|
|
144
144
|
));
|
|
145
145
|
}
|
|
@@ -161,7 +161,7 @@ impl Device {
|
|
|
161
161
|
#[cfg(not(feature = "metal"))]
|
|
162
162
|
{
|
|
163
163
|
return Err(Error::new(
|
|
164
|
-
|
|
164
|
+
Ruby::get().unwrap().exception_runtime_error(),
|
|
165
165
|
"Metal support not compiled in. Rebuild on macOS.",
|
|
166
166
|
));
|
|
167
167
|
}
|
|
@@ -211,14 +211,15 @@ impl magnus::TryConvert for Device {
|
|
|
211
211
|
"cpu" => Device::Cpu,
|
|
212
212
|
"cuda" => Device::Cuda,
|
|
213
213
|
"metal" => Device::Metal,
|
|
214
|
-
_ => return Err(Error::new(
|
|
214
|
+
_ => return Err(Error::new(Ruby::get().unwrap().exception_arg_error(), "invalid device")),
|
|
215
215
|
};
|
|
216
216
|
Ok(device)
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
pub fn init(rb_candle: RModule) -> Result<()> {
|
|
221
|
-
let
|
|
221
|
+
let ruby = Ruby::get().unwrap();
|
|
222
|
+
let rb_device = rb_candle.define_class("Device", ruby.class_object())?;
|
|
222
223
|
rb_device.define_singleton_method("cpu", function!(Device::cpu, 0))?;
|
|
223
224
|
rb_device.define_singleton_method("cuda", function!(Device::cuda, 0))?;
|
|
224
225
|
rb_device.define_singleton_method("metal", function!(Device::metal, 0))?;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
use magnus::value::ReprValue;
|
|
2
|
-
use magnus::{method,
|
|
2
|
+
use magnus::{method, RModule, Module, Ruby};
|
|
3
3
|
|
|
4
4
|
use ::candle_core::DType as CoreDType;
|
|
5
5
|
use crate::ruby::Result;
|
|
@@ -30,7 +30,8 @@ impl DType {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
pub fn init(rb_candle: RModule) -> Result<()> {
|
|
33
|
-
let
|
|
33
|
+
let ruby = Ruby::get().unwrap();
|
|
34
|
+
let rb_dtype = rb_candle.define_class("DType", ruby.class_object())?;
|
|
34
35
|
rb_dtype.define_method("to_s", method!(DType::__str__, 0))?;
|
|
35
36
|
rb_dtype.define_method("inspect", method!(DType::__repr__, 0))?;
|
|
36
37
|
Ok(())
|
|
@@ -13,7 +13,7 @@ use candle_transformers::models::{
|
|
|
13
13
|
jina_bert::{BertModel as JinaBertModel, Config as JinaConfig},
|
|
14
14
|
distilbert::{DistilBertModel, Config as DistilBertConfig}
|
|
15
15
|
};
|
|
16
|
-
use magnus::{
|
|
16
|
+
use magnus::{function, method, prelude::*, Error, RModule, RHash, Ruby};
|
|
17
17
|
use std::path::Path;
|
|
18
18
|
use serde_json;
|
|
19
19
|
|
|
@@ -103,28 +103,30 @@ impl EmbeddingModel {
|
|
|
103
103
|
/// &RETURNS&: Tensor
|
|
104
104
|
/// pooling_method: "pooled", "pooled_normalized", or "cls" (default: "pooled")
|
|
105
105
|
pub fn embedding(&self, input: String, pooling_method: String) -> Result<Tensor> {
|
|
106
|
+
let ruby = Ruby::get().unwrap();
|
|
106
107
|
match &self.0.model {
|
|
107
108
|
Some(model) => {
|
|
108
109
|
match &self.0.tokenizer {
|
|
109
110
|
Some(tokenizer) => Ok(Tensor(self.compute_embedding(input, model, tokenizer, &pooling_method)?)),
|
|
110
|
-
None => Err(magnus::Error::new(
|
|
111
|
+
None => Err(magnus::Error::new(ruby.exception_runtime_error(), "Tokenizer not found"))
|
|
111
112
|
}
|
|
112
113
|
}
|
|
113
|
-
None => Err(magnus::Error::new(
|
|
114
|
+
None => Err(magnus::Error::new(ruby.exception_runtime_error(), "Model not found"))
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
/// Returns the unpooled embedding tensor ([1, SEQLENGTH, DIM]) for the input text
|
|
118
119
|
/// &RETURNS&: Tensor
|
|
119
120
|
pub fn embeddings(&self, input: String) -> Result<Tensor> {
|
|
121
|
+
let ruby = Ruby::get().unwrap();
|
|
120
122
|
match &self.0.model {
|
|
121
123
|
Some(model) => {
|
|
122
124
|
match &self.0.tokenizer {
|
|
123
125
|
Some(tokenizer) => Ok(Tensor(self.compute_embeddings(input, model, tokenizer)?)),
|
|
124
|
-
None => Err(magnus::Error::new(
|
|
126
|
+
None => Err(magnus::Error::new(ruby.exception_runtime_error(), "Tokenizer not found"))
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
|
-
None => Err(magnus::Error::new(
|
|
129
|
+
None => Err(magnus::Error::new(ruby.exception_runtime_error(), "Model not found"))
|
|
128
130
|
}
|
|
129
131
|
}
|
|
130
132
|
|
|
@@ -165,7 +167,10 @@ impl EmbeddingModel {
|
|
|
165
167
|
},
|
|
166
168
|
Err(_) => None
|
|
167
169
|
};
|
|
168
|
-
inferred_emb_dim.ok_or_else(||
|
|
170
|
+
inferred_emb_dim.ok_or_else(|| {
|
|
171
|
+
let ruby = Ruby::get().unwrap();
|
|
172
|
+
magnus::Error::new(ruby.exception_runtime_error(), "Could not infer embedding size from model file. Please specify embedding_size explicitly.")
|
|
173
|
+
})
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
176
|
}
|
|
@@ -178,8 +183,9 @@ impl EmbeddingModel {
|
|
|
178
183
|
EmbeddingModelType::JinaBert => {
|
|
179
184
|
let model_path = api.repo(repo).get("model.safetensors").map_err(wrap_hf_err)?;
|
|
180
185
|
if !std::path::Path::new(&model_path).exists() {
|
|
186
|
+
let ruby = Ruby::get().unwrap();
|
|
181
187
|
return Err(magnus::Error::new(
|
|
182
|
-
|
|
188
|
+
ruby.exception_runtime_error(),
|
|
183
189
|
"model.safetensors not found after download. Only safetensors models are supported. Please ensure your model repo contains model.safetensors."
|
|
184
190
|
));
|
|
185
191
|
}
|
|
@@ -196,8 +202,9 @@ impl EmbeddingModel {
|
|
|
196
202
|
EmbeddingModelType::StandardBert => {
|
|
197
203
|
let model_path = api.repo(repo).get("model.safetensors").map_err(wrap_hf_err)?;
|
|
198
204
|
if !std::path::Path::new(&model_path).exists() {
|
|
205
|
+
let ruby = Ruby::get().unwrap();
|
|
199
206
|
return Err(magnus::Error::new(
|
|
200
|
-
|
|
207
|
+
ruby.exception_runtime_error(),
|
|
201
208
|
"model.safetensors not found after download. Only safetensors models are supported. Please ensure your model repo contains model.safetensors."
|
|
202
209
|
));
|
|
203
210
|
}
|
|
@@ -214,8 +221,9 @@ impl EmbeddingModel {
|
|
|
214
221
|
EmbeddingModelType::DistilBert => {
|
|
215
222
|
let model_path = api.repo(repo.clone()).get("model.safetensors").map_err(wrap_hf_err)?;
|
|
216
223
|
if !std::path::Path::new(&model_path).exists() {
|
|
224
|
+
let ruby = Ruby::get().unwrap();
|
|
217
225
|
return Err(magnus::Error::new(
|
|
218
|
-
|
|
226
|
+
ruby.exception_runtime_error(),
|
|
219
227
|
"model.safetensors not found after download. Only safetensors models are supported. Please ensure your model repo contains model.safetensors."
|
|
220
228
|
));
|
|
221
229
|
}
|
|
@@ -235,8 +243,9 @@ impl EmbeddingModel {
|
|
|
235
243
|
EmbeddingModelType::MiniLM => {
|
|
236
244
|
let model_path = api.repo(repo.clone()).get("model.safetensors").map_err(wrap_hf_err)?;
|
|
237
245
|
if !std::path::Path::new(&model_path).exists() {
|
|
246
|
+
let ruby = Ruby::get().unwrap();
|
|
238
247
|
return Err(magnus::Error::new(
|
|
239
|
-
|
|
248
|
+
ruby.exception_runtime_error(),
|
|
240
249
|
"model.safetensors not found after download. Only safetensors models are supported. Please ensure your model repo contains model.safetensors."
|
|
241
250
|
));
|
|
242
251
|
}
|
|
@@ -357,7 +366,10 @@ impl EmbeddingModel {
|
|
|
357
366
|
"pooled" => Self::pooled_embedding(&result),
|
|
358
367
|
"pooled_normalized" => Self::pooled_normalized_embedding(&result),
|
|
359
368
|
"cls" => Self::pooled_cls_embedding(&result),
|
|
360
|
-
_ =>
|
|
369
|
+
_ => {
|
|
370
|
+
let ruby = Ruby::get().unwrap();
|
|
371
|
+
Err(magnus::Error::new(ruby.exception_runtime_error(), "Unknown pooling method"))
|
|
372
|
+
},
|
|
361
373
|
}
|
|
362
374
|
}
|
|
363
375
|
|
|
@@ -390,7 +402,10 @@ impl EmbeddingModel {
|
|
|
390
402
|
pub fn tokenizer(&self) -> Result<crate::ruby::tokenizer::Tokenizer> {
|
|
391
403
|
match &self.0.tokenizer {
|
|
392
404
|
Some(tokenizer) => Ok(crate::ruby::tokenizer::Tokenizer(tokenizer.clone())),
|
|
393
|
-
None =>
|
|
405
|
+
None => {
|
|
406
|
+
let ruby = Ruby::get().unwrap();
|
|
407
|
+
Err(magnus::Error::new(ruby.exception_runtime_error(), "No tokenizer loaded for this model"))
|
|
408
|
+
}
|
|
394
409
|
}
|
|
395
410
|
}
|
|
396
411
|
|
|
@@ -409,7 +424,8 @@ impl EmbeddingModel {
|
|
|
409
424
|
|
|
410
425
|
/// Get all options as a hash
|
|
411
426
|
pub fn options(&self) -> Result<RHash> {
|
|
412
|
-
let
|
|
427
|
+
let ruby = Ruby::get().unwrap();
|
|
428
|
+
let hash = ruby.hash_new();
|
|
413
429
|
|
|
414
430
|
// Add model_id
|
|
415
431
|
if let Some(model_id) = &self.0.model_id {
|
|
@@ -439,7 +455,8 @@ impl EmbeddingModel {
|
|
|
439
455
|
}
|
|
440
456
|
|
|
441
457
|
pub fn init(rb_candle: RModule) -> Result<()> {
|
|
442
|
-
let
|
|
458
|
+
let ruby = Ruby::get().unwrap();
|
|
459
|
+
let rb_embedding_model = rb_candle.define_class("EmbeddingModel", ruby.class_object())?;
|
|
443
460
|
rb_embedding_model.define_singleton_method("_create", function!(EmbeddingModel::new, 5))?;
|
|
444
461
|
// Expose embedding with an optional pooling_method argument (default: "pooled")
|
|
445
462
|
rb_embedding_model.define_method("_embedding", method!(EmbeddingModel::embedding, 2))?;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
use magnus::Error;
|
|
2
2
|
|
|
3
3
|
pub fn wrap_std_err(err: Box<dyn std::error::Error + Send + Sync>) -> Error {
|
|
4
|
-
|
|
4
|
+
let ruby = magnus::Ruby::get().unwrap();
|
|
5
|
+
Error::new(ruby.exception_runtime_error(), err.to_string())
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
pub fn wrap_candle_err(err: candle_core::Error) -> Error {
|
|
8
|
-
|
|
9
|
+
let ruby = magnus::Ruby::get().unwrap();
|
|
10
|
+
Error::new(ruby.exception_runtime_error(), err.to_string())
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
pub fn wrap_hf_err(err: hf_hub::api::sync::ApiError) -> Error {
|
|
12
|
-
|
|
14
|
+
let ruby = magnus::Ruby::get().unwrap();
|
|
15
|
+
Error::new(ruby.exception_runtime_error(), err.to_string())
|
|
13
16
|
}
|
|
14
|
-
|