]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Clippy fix. (#171)
authorNicolas Patry <patry.nicolas@protonmail.com>
Mon, 30 Jan 2023 12:59:40 +0000 (13:59 +0100)
committerGitHub <noreply@github.com>
Mon, 30 Jan 2023 12:59:40 +0000 (13:59 +0100)
* Clippy fix.

* And tests.

bindings/python/src/lib.rs
safetensors/benches/benchmark.rs
safetensors/src/tensor.rs

index f017ad4e74e64e7e1b501379ce19a296a9c1adca..c1ab974757f0d03e683092365b44813f99698afd 100644 (file)
@@ -72,7 +72,7 @@ fn prepare(tensor_dict: HashMap<String, &PyDict>) -> PyResult<BTreeMap<String, T
                     }
                 }
                 "data" => data = value.extract()?,
-                _ => println!("Ignored unknown kwarg option {}", key),
+                _ => println!("Ignored unknown kwarg option {key}"),
             };
         }
         let tensor = TensorView::new(dtype, shape, data);
@@ -95,15 +95,15 @@ fn prepare(tensor_dict: HashMap<String, &PyDict>) -> PyResult<BTreeMap<String, T
 ///         The serialized content.
 #[pyfunction]
 #[pyo3(text_signature = "(tensor_dict, metadata=None)")]
-fn serialize<'a, 'b>(
+fn serialize<'b>(
     py: Python<'b>,
-    tensor_dict: HashMap<String, &'a PyDict>,
+    tensor_dict: HashMap<String, &PyDict>,
     metadata: Option<HashMap<String, String>>,
 ) -> PyResult<&'b PyBytes> {
     let tensors = prepare(tensor_dict)?;
     let metadata_btreemap = metadata.map(|data| BTreeMap::from_iter(data.into_iter()));
     let out = safetensors::tensor::serialize(&tensors, &metadata_btreemap)
-        .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {:?}", e)))?;
+        .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {e:?}")))?;
     let pybytes = PyBytes::new(py, &out);
     Ok(pybytes)
 }
@@ -132,7 +132,7 @@ fn serialize_file(
     let tensors = prepare(tensor_dict)?;
     let metadata_btreemap = metadata.map(|data| BTreeMap::from_iter(data.into_iter()));
     safetensors::tensor::serialize_to_file(&tensors, &metadata_btreemap, filename.as_path())
-        .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {:?}", e)))?;
+        .map_err(|e| SafetensorError::new_err(format!("Error while serializing: {e:?}")))?;
     Ok(())
 }
 
@@ -150,7 +150,7 @@ fn serialize_file(
 #[pyo3(text_signature = "(bytes)")]
 fn deserialize(py: Python, bytes: &[u8]) -> PyResult<Vec<(String, HashMap<String, PyObject>)>> {
     let safetensor = SafeTensors::deserialize(bytes)
-        .map_err(|e| SafetensorError::new_err(format!("Error while deserializing: {:?}", e)))?;
+        .map_err(|e| SafetensorError::new_err(format!("Error while deserializing: {e:?}")))?;
     let mut items = vec![];
 
     for (tensor_name, tensor) in safetensor.tensors() {
@@ -374,7 +374,7 @@ fn check_cuda(cuda_memcpy: &Symbol<MemcpyFn>, dst: usize, src: &[u8], module: &P
     if out != 0 {
         let string = match get_error_string(module, out) {
             Ok(string) => string,
-            Err(_) => format!("{}", out),
+            Err(_) => format!("{out}"),
         };
         println!(
                 "We tried to set your tensor fast, but there was a cuda error, This could have corrupted your GPU ram, aborting to prevent further errors {string:?}"
@@ -490,7 +490,7 @@ impl Open {
         let buffer = unsafe { MmapOptions::new().map(&file)? };
 
         let (n, metadata) = SafeTensors::read_metadata(&buffer).map_err(|e| {
-            SafetensorError::new_err(format!("Error while deserializing header: {:?}", e))
+            SafetensorError::new_err(format!("Error while deserializing header: {e:?}"))
         })?;
 
         let offset = n + 8;
@@ -1093,8 +1093,7 @@ fn get_pydtype(module: &PyModule, dtype: Dtype) -> PyResult<PyObject> {
             Dtype::BOOL => module.getattr(intern!(py, "bool"))?.into(),
             dtype => {
                 return Err(SafetensorError::new_err(format!(
-                    "Dtype not understood: {:?}",
-                    dtype
+                    "Dtype not understood: {dtype:?}"
                 )))
             }
         };
index 769888e3e9f71f751d404882e37878a1235a4fa7..c49660edd7478fb229076f6e0a41a1b9911b6750 100644 (file)
@@ -20,7 +20,7 @@ pub fn bench_serialize(c: &mut Criterion) {
     // 2_MB x 5 = 10_MB
     for i in 0..n_layers {
         let tensor = TensorView::new(dtype, shape.clone(), &data[..]);
-        metadata.insert(format!("weight{}", i), tensor);
+        metadata.insert(format!("weight{i}"), tensor);
     }
 
     c.bench_function("Serlialize 10_MB", |b| {
@@ -38,7 +38,7 @@ pub fn bench_deserialize(c: &mut Criterion) {
     // 2_MB x 5 = 10_MB
     for i in 0..n_layers {
         let tensor = TensorView::new(dtype, shape.clone(), &data[..]);
-        metadata.insert(format!("weight{}", i), tensor);
+        metadata.insert(format!("weight{i}"), tensor);
     }
 
     let out = serialize(&metadata, &None).unwrap();
index 654f6769c087b5112ea30858228278f2389a8fc6..b870bd5795d0cc44a7f5bb669d4d79abbef272f6 100644 (file)
@@ -40,7 +40,7 @@ impl From<std::io::Error> for SafeTensorError {
 
 impl std::fmt::Display for SafeTensorError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "{:?}", self)
+        write!(f, "{self:?}")
     }
 }
 
@@ -458,19 +458,19 @@ mod tests {
         tensors_desc.push(("wte".to_string(), vec![50257, 768]));
         tensors_desc.push(("wpe".to_string(), vec![1024, 768]));
         for i in 0..n_heads {
-            tensors_desc.push((format!("h.{}.ln_1.weight", i), vec![768]));
-            tensors_desc.push((format!("h.{}.ln_1.bias", i), vec![768]));
-            tensors_desc.push((format!("h.{}.attn.bias", i), vec![1, 1, 1024, 1024]));
-            tensors_desc.push((format!("h.{}.attn.c_attn.weight", i), vec![768, 2304]));
-            tensors_desc.push((format!("h.{}.attn.c_attn.bias", i), vec![2304]));
-            tensors_desc.push((format!("h.{}.attn.c_proj.weight", i), vec![768, 768]));
-            tensors_desc.push((format!("h.{}.attn.c_proj.bias", i), vec![768]));
-            tensors_desc.push((format!("h.{}.ln_2.weight", i), vec![768]));
-            tensors_desc.push((format!("h.{}.ln_2.bias", i), vec![768]));
-            tensors_desc.push((format!("h.{}.mlp.c_fc.weight", i), vec![768, 3072]));
-            tensors_desc.push((format!("h.{}.mlp.c_fc.bias", i), vec![3072]));
-            tensors_desc.push((format!("h.{}.mlp.c_proj.weight", i), vec![3072, 768]));
-            tensors_desc.push((format!("h.{}.mlp.c_proj.bias", i), vec![768]));
+            tensors_desc.push((format!("h.{i}.ln_1.weight"), vec![768]));
+            tensors_desc.push((format!("h.{i}.ln_1.bias"), vec![768]));
+            tensors_desc.push((format!("h.{i}.attn.bias"), vec![1, 1, 1024, 1024]));
+            tensors_desc.push((format!("h.{i}.attn.c_attn.weight"), vec![768, 2304]));
+            tensors_desc.push((format!("h.{i}.attn.c_attn.bias"), vec![2304]));
+            tensors_desc.push((format!("h.{i}.attn.c_proj.weight"), vec![768, 768]));
+            tensors_desc.push((format!("h.{i}.attn.c_proj.bias"), vec![768]));
+            tensors_desc.push((format!("h.{i}.ln_2.weight"), vec![768]));
+            tensors_desc.push((format!("h.{i}.ln_2.bias"), vec![768]));
+            tensors_desc.push((format!("h.{i}.mlp.c_fc.weight"), vec![768, 3072]));
+            tensors_desc.push((format!("h.{i}.mlp.c_fc.bias"), vec![3072]));
+            tensors_desc.push((format!("h.{i}.mlp.c_proj.weight"), vec![3072, 768]));
+            tensors_desc.push((format!("h.{i}.mlp.c_proj.bias"), vec![768]));
         }
         tensors_desc.push(("ln_f.weight".to_string(), vec![768]));
         tensors_desc.push(("ln_f.bias".to_string(), vec![768]));
@@ -492,7 +492,7 @@ mod tests {
             offset += n;
         }
 
-        let filename = format!("./out_{}.bin", model_id);
+        let filename = format!("./out_{model_id}.bin");
 
         let out = serialize(&metadata, &None).unwrap();