safetensors 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67fbb4b9469dd7bb195c8b89788868f051d80d2537a4d84dba20b91ddd23a79c
4
- data.tar.gz: c9b784821f4821b8db148802e4e78f20b5c53732b8b6a12d089ec4c291cc1986
3
+ metadata.gz: 547c0781cd2c693db2a6605bca61004833aa668876f1512673ed7e5a44b6d30d
4
+ data.tar.gz: be088a6e2c725dfbc32a16c3864f8bc23ff4f58ee86fcf6c3388f64d98fb0223
5
5
  SHA512:
6
- metadata.gz: b1eb7800dfed9d4589d40524825f98bb79d4360ef4a4f7cb69215d8e68b915b5f58ebc7f15b2f8b91e63153f7c1c59fcc0222117c8d04cd2716a9ea44495b54f
7
- data.tar.gz: 0f2112f34fec093f483151fe6307b9983d942ea425b955bb9f8bc6adeef5febbb424f286f8385f3a51e21c0fdd0d1a2ddda30834069a5dd3f985a5b2eeeab8f6
6
+ metadata.gz: 270ea7030272173f04939528d34ae49d8d8a15f7ac51adb62fe6c93990e551257bce124c9bc983bbda70851322e66c4149f4483815f26b504a9528067b61dc1e
7
+ data.tar.gz: 7e4c3681d5443bb85d5226590d4dfe1b42a1c8ecf597ace1f136f89c6456070878cefc4f81ddf8247d8243f5c4fc20897af800c55e16aa4746d271d4cccdda53
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.1 (2025-11-22)
2
+
3
+ - Updated Safetensors to 0.7.0
4
+
1
5
  ## 0.2.0 (2025-06-23)
2
6
 
3
7
  - Updated Safetensors to 0.6.0
data/Cargo.lock CHANGED
@@ -11,6 +11,12 @@ dependencies = [
11
11
  "memchr",
12
12
  ]
13
13
 
14
+ [[package]]
15
+ name = "allocator-api2"
16
+ version = "0.2.21"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
19
+
14
20
  [[package]]
15
21
  name = "bindgen"
16
22
  version = "0.69.5"
@@ -69,12 +75,36 @@ version = "1.15.0"
69
75
  source = "registry+https://github.com/rust-lang/crates.io-index"
70
76
  checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
71
77
 
78
+ [[package]]
79
+ name = "equivalent"
80
+ version = "1.0.2"
81
+ source = "registry+https://github.com/rust-lang/crates.io-index"
82
+ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
83
+
84
+ [[package]]
85
+ name = "foldhash"
86
+ version = "0.2.0"
87
+ source = "registry+https://github.com/rust-lang/crates.io-index"
88
+ checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
89
+
72
90
  [[package]]
73
91
  name = "glob"
74
92
  version = "0.3.2"
75
93
  source = "registry+https://github.com/rust-lang/crates.io-index"
76
94
  checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
77
95
 
96
+ [[package]]
97
+ name = "hashbrown"
98
+ version = "0.16.0"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d"
101
+ dependencies = [
102
+ "allocator-api2",
103
+ "equivalent",
104
+ "foldhash",
105
+ "serde",
106
+ ]
107
+
78
108
  [[package]]
79
109
  name = "itertools"
80
110
  version = "0.12.1"
@@ -120,9 +150,9 @@ dependencies = [
120
150
 
121
151
  [[package]]
122
152
  name = "magnus"
123
- version = "0.7.1"
153
+ version = "0.8.1"
124
154
  source = "registry+https://github.com/rust-lang/crates.io-index"
125
- checksum = "3d87ae53030f3a22e83879e666cb94e58a7bdf31706878a0ba48752994146dab"
155
+ checksum = "bd2ac6e71886be00ac34db92aa732c793c5107c95191805b9a1c7e70e6d342e0"
126
156
  dependencies = [
127
157
  "magnus-macros",
128
158
  "rb-sys",
@@ -132,9 +162,9 @@ dependencies = [
132
162
 
133
163
  [[package]]
134
164
  name = "magnus-macros"
135
- version = "0.6.0"
165
+ version = "0.8.0"
136
166
  source = "registry+https://github.com/rust-lang/crates.io-index"
137
- checksum = "5968c820e2960565f647819f5928a42d6e874551cab9d88d75e3e0660d7f71e3"
167
+ checksum = "47607461fd8e1513cb4f2076c197d8092d921a1ea75bd08af97398f593751892"
138
168
  dependencies = [
139
169
  "proc-macro2",
140
170
  "quote",
@@ -192,18 +222,18 @@ dependencies = [
192
222
 
193
223
  [[package]]
194
224
  name = "rb-sys"
195
- version = "0.9.116"
225
+ version = "0.9.117"
196
226
  source = "registry+https://github.com/rust-lang/crates.io-index"
197
- checksum = "7059846f68396df83155779c75336ca24567741cb95256e6308c9fcc370e8dad"
227
+ checksum = "f900d1ce4629a2ebffaf5de74bd8f9c1188d4c5ed406df02f97e22f77a006f44"
198
228
  dependencies = [
199
229
  "rb-sys-build",
200
230
  ]
201
231
 
202
232
  [[package]]
203
233
  name = "rb-sys-build"
204
- version = "0.9.116"
234
+ version = "0.9.117"
205
235
  source = "registry+https://github.com/rust-lang/crates.io-index"
206
- checksum = "ac217510df41b9ffc041573e68d7a02aaff770c49943c7494441c4b224b0ecd0"
236
+ checksum = "ef1e9c857028f631056bcd6d88cec390c751e343ce2223ddb26d23eb4a151d59"
207
237
  dependencies = [
208
238
  "bindgen",
209
239
  "lazy_static",
@@ -216,9 +246,9 @@ dependencies = [
216
246
 
217
247
  [[package]]
218
248
  name = "rb-sys-env"
219
- version = "0.1.2"
249
+ version = "0.2.2"
220
250
  source = "registry+https://github.com/rust-lang/crates.io-index"
221
- checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
251
+ checksum = "08f8d2924cf136a1315e2b4c7460a39f62ef11ee5d522df9b2750fab55b868b6"
222
252
 
223
253
  [[package]]
224
254
  name = "regex"
@@ -263,20 +293,21 @@ checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
263
293
 
264
294
  [[package]]
265
295
  name = "safetensors"
266
- version = "0.2.0"
296
+ version = "0.2.1"
267
297
  dependencies = [
268
298
  "magnus",
269
299
  "memmap2",
270
- "safetensors 0.6.0",
300
+ "safetensors 0.7.0",
271
301
  "serde_json",
272
302
  ]
273
303
 
274
304
  [[package]]
275
305
  name = "safetensors"
276
- version = "0.6.0"
306
+ version = "0.7.0"
277
307
  source = "registry+https://github.com/rust-lang/crates.io-index"
278
- checksum = "617df84b8360fc910a687afe4be1c4660bf7c090d448d2fc27c781e852205f7c"
308
+ checksum = "675656c1eabb620b921efea4f9199f97fc86e36dd6ffd1fbbe48d0f59a4987f5"
279
309
  dependencies = [
310
+ "hashbrown",
280
311
  "serde",
281
312
  "serde_json",
282
313
  ]
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "safetensors"
3
- version = "0.2.0"
3
+ version = "0.2.1"
4
4
  license = "Apache-2.0"
5
5
  authors = ["Andrew Kane <andrew@ankane.org>"]
6
6
  edition = "2021"
@@ -11,7 +11,7 @@ publish = false
11
11
  crate-type = ["cdylib"]
12
12
 
13
13
  [dependencies]
14
- magnus = "0.7"
14
+ magnus = "0.8"
15
15
  memmap2 = "0.5"
16
- safetensors = "=0.6.0"
16
+ safetensors = "=0.7.0"
17
17
  serde_json = "1"
@@ -79,12 +79,16 @@ fn prepare(tensor_dict: &RHash) -> RbResult<HashMap<String, TensorView<'_>>> {
79
79
  Ok(tensors)
80
80
  }
81
81
 
82
- fn serialize(tensor_dict: RHash, metadata: Option<HashMap<String, String>>) -> RbResult<RString> {
82
+ fn serialize(
83
+ ruby: &Ruby,
84
+ tensor_dict: RHash,
85
+ metadata: Option<HashMap<String, String>>,
86
+ ) -> RbResult<RString> {
83
87
  let tensors = prepare(&tensor_dict)?;
84
88
  let metadata_map = metadata.map(HashMap::from_iter);
85
89
  let out = safetensors::tensor::serialize(&tensors, metadata_map)
86
90
  .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {e:?}")))?;
87
- let rbbytes = RString::from_slice(&out);
91
+ let rbbytes = ruby.str_from_slice(&out);
88
92
  Ok(rbbytes)
89
93
  }
90
94
 
@@ -99,20 +103,20 @@ fn serialize_file(
99
103
  Ok(())
100
104
  }
101
105
 
102
- fn deserialize(bytes: RString) -> RbResult<RArray> {
106
+ fn deserialize(ruby: &Ruby, bytes: RString) -> RbResult<RArray> {
103
107
  let safetensor = SafeTensors::deserialize(unsafe { bytes.as_slice() })
104
108
  .map_err(|e| SafetensorError::new_err(format!("Error while deserializing: {e:?}")))?;
105
109
 
106
110
  let tensors = safetensor.tensors();
107
- let items = RArray::with_capacity(tensors.len());
111
+ let items = ruby.ary_new_capa(tensors.len());
108
112
 
109
113
  for (tensor_name, tensor) in tensors {
110
- let rbshape = RArray::from_vec(tensor.shape().to_vec());
114
+ let rbshape = ruby.ary_from_vec(tensor.shape().to_vec());
111
115
  let rbdtype = format!("{:?}", tensor.dtype());
112
116
 
113
- let rbdata = RString::from_slice(tensor.data());
117
+ let rbdata = ruby.str_from_slice(tensor.data());
114
118
 
115
- let map = RHash::new();
119
+ let map = ruby.hash_new();
116
120
  map.aset("shape", rbshape)?;
117
121
  map.aset("dtype", rbdtype)?;
118
122
  map.aset("data", rbdata)?;
@@ -279,7 +283,7 @@ impl Open {
279
283
  Ok(keys)
280
284
  }
281
285
 
282
- pub fn get_tensor(&self, name: &str) -> RbResult<Value> {
286
+ pub fn get_tensor(&self, ruby: &Ruby, name: &str) -> RbResult<Value> {
283
287
  let info = self.metadata.info(name).ok_or_else(|| {
284
288
  SafetensorError::new_err(format!("File does not contain tensor {name}",))
285
289
  })?;
@@ -289,9 +293,10 @@ impl Open {
289
293
  let data =
290
294
  &mmap[info.data_offsets.0 + self.offset..info.data_offsets.1 + self.offset];
291
295
 
292
- let array: Value = RString::from_slice(data).into_value();
296
+ let array: Value = ruby.str_from_slice(data).as_value();
293
297
 
294
298
  create_tensor(
299
+ ruby,
295
300
  &self.framework,
296
301
  info.dtype,
297
302
  &info.shape,
@@ -332,19 +337,19 @@ impl SafeOpen {
332
337
  self.inner()?.keys()
333
338
  }
334
339
 
335
- pub fn get_tensor(&self, name: String) -> RbResult<Value> {
336
- self.inner()?.get_tensor(&name)
340
+ pub fn get_tensor(ruby: &Ruby, rb_self: &Self, name: String) -> RbResult<Value> {
341
+ rb_self.inner()?.get_tensor(ruby, &name)
337
342
  }
338
343
  }
339
344
 
340
345
  fn create_tensor(
346
+ ruby: &Ruby,
341
347
  framework: &Framework,
342
348
  dtype: Dtype,
343
349
  shape: &[usize],
344
350
  array: Value,
345
351
  device: &Device,
346
352
  ) -> RbResult<Value> {
347
- let ruby = Ruby::get().unwrap();
348
353
  let (module, is_numo): (RModule, bool) = match framework {
349
354
  Framework::Pytorch => (
350
355
  ruby.class_object()
@@ -360,7 +365,7 @@ fn create_tensor(
360
365
  ),
361
366
  };
362
367
 
363
- let dtype = get_rbdtype(module, dtype, is_numo)?;
368
+ let dtype = get_rbdtype(ruby, module, dtype, is_numo)?;
364
369
  let shape = shape.to_vec();
365
370
  let tensor: Value = match framework {
366
371
  Framework::Pytorch => {
@@ -378,19 +383,19 @@ fn create_tensor(
378
383
  Ok(tensor)
379
384
  }
380
385
 
381
- fn get_rbdtype(_module: RModule, dtype: Dtype, is_numo: bool) -> RbResult<Value> {
382
- let dtype: Value = if is_numo {
386
+ fn get_rbdtype(ruby: &Ruby, _module: RModule, dtype: Dtype, is_numo: bool) -> RbResult<Symbol> {
387
+ let dtype: Symbol = if is_numo {
383
388
  match dtype {
384
- Dtype::F64 => Symbol::new("DFloat").into_value(),
385
- Dtype::F32 => Symbol::new("SFloat").into_value(),
386
- Dtype::U64 => Symbol::new("UInt64").into_value(),
387
- Dtype::I64 => Symbol::new("Int64").into_value(),
388
- Dtype::U32 => Symbol::new("UInt32").into_value(),
389
- Dtype::I32 => Symbol::new("Int32").into_value(),
390
- Dtype::U16 => Symbol::new("UInt16").into_value(),
391
- Dtype::I16 => Symbol::new("Int16").into_value(),
392
- Dtype::U8 => Symbol::new("UInt8").into_value(),
393
- Dtype::I8 => Symbol::new("Int8").into_value(),
389
+ Dtype::F64 => ruby.to_symbol("DFloat"),
390
+ Dtype::F32 => ruby.to_symbol("SFloat"),
391
+ Dtype::U64 => ruby.to_symbol("UInt64"),
392
+ Dtype::I64 => ruby.to_symbol("Int64"),
393
+ Dtype::U32 => ruby.to_symbol("UInt32"),
394
+ Dtype::I32 => ruby.to_symbol("Int32"),
395
+ Dtype::U16 => ruby.to_symbol("UInt16"),
396
+ Dtype::I16 => ruby.to_symbol("Int16"),
397
+ Dtype::U8 => ruby.to_symbol("UInt8"),
398
+ Dtype::I8 => ruby.to_symbol("Int8"),
394
399
  dtype => {
395
400
  return Err(SafetensorError::new_err(format!(
396
401
  "Dtype not understood: {dtype:?}"
@@ -399,21 +404,21 @@ fn get_rbdtype(_module: RModule, dtype: Dtype, is_numo: bool) -> RbResult<Value>
399
404
  }
400
405
  } else {
401
406
  match dtype {
402
- Dtype::F64 => Symbol::new("float64").into_value(),
403
- Dtype::F32 => Symbol::new("float32").into_value(),
404
- Dtype::BF16 => Symbol::new("bfloat16").into_value(),
405
- Dtype::F16 => Symbol::new("float16").into_value(),
406
- Dtype::U64 => Symbol::new("uint64").into_value(),
407
- Dtype::I64 => Symbol::new("int64").into_value(),
408
- Dtype::U32 => Symbol::new("uint32").into_value(),
409
- Dtype::I32 => Symbol::new("int32").into_value(),
410
- Dtype::U16 => Symbol::new("uint16").into_value(),
411
- Dtype::I16 => Symbol::new("int16").into_value(),
412
- Dtype::U8 => Symbol::new("uint8").into_value(),
413
- Dtype::I8 => Symbol::new("int8").into_value(),
414
- Dtype::BOOL => Symbol::new("bool").into_value(),
415
- Dtype::F8_E4M3 => Symbol::new("float8_e4m3fn").into_value(),
416
- Dtype::F8_E5M2 => Symbol::new("float8_e5m2").into_value(),
407
+ Dtype::F64 => ruby.to_symbol("float64"),
408
+ Dtype::F32 => ruby.to_symbol("float32"),
409
+ Dtype::BF16 => ruby.to_symbol("bfloat16"),
410
+ Dtype::F16 => ruby.to_symbol("float16"),
411
+ Dtype::U64 => ruby.to_symbol("uint64"),
412
+ Dtype::I64 => ruby.to_symbol("int64"),
413
+ Dtype::U32 => ruby.to_symbol("uint32"),
414
+ Dtype::I32 => ruby.to_symbol("int32"),
415
+ Dtype::U16 => ruby.to_symbol("uint16"),
416
+ Dtype::I16 => ruby.to_symbol("int16"),
417
+ Dtype::U8 => ruby.to_symbol("uint8"),
418
+ Dtype::I8 => ruby.to_symbol("int8"),
419
+ Dtype::BOOL => ruby.to_symbol("bool"),
420
+ Dtype::F8_E4M3 => ruby.to_symbol("float8_e4m3fn"),
421
+ Dtype::F8_E5M2 => ruby.to_symbol("float8_e5m2"),
417
422
  dtype => {
418
423
  return Err(SafetensorError::new_err(format!(
419
424
  "Dtype not understood: {dtype:?}"
@@ -1,3 +1,3 @@
1
1
  module Safetensors
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safetensors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  requirements: []
62
- rubygems_version: 3.6.7
62
+ rubygems_version: 3.6.9
63
63
  specification_version: 4
64
64
  summary: Simple, safe way to store and distribute tensors
65
65
  test_files: []