.bind(py),
false,
),
- _ => (
- NUMPY_MODULE
- .get()
- .ok_or_else(|| {
- SafetensorError::new_err(format!("Could not find module {framework:?}",))
- })?
- .bind(py),
- true,
- ),
+ frame => {
+ // Attempt to load the frameworks
+ // Those are needed to prepare the ml dtypes
+ // like bfloat16
+ match frame {
+ Framework::Tensorflow => {
+ let _ = PyModule::import(py, intern!(py, "tensorflow"));
+ }
+ Framework::Flax => {
+ let _ = PyModule::import(py, intern!(py, "flax"));
+ }
+ _ => {}
+ };
+
+ (
+ NUMPY_MODULE
+ .get()
+ .ok_or_else(|| {
+ SafetensorError::new_err(
+ format!("Could not find module {framework:?}",),
+ )
+ })?
+ .bind(py),
+ true,
+ )
+ }
};
let dtype: PyObject = get_pydtype(module, dtype, is_numpy)?;
let count: usize = shape.iter().product();
import platform
import unittest
+import sys
if platform.system() != "Windows":
for k, v in weights.items():
tv = flax_weights[k]
self.assertTrue(jnp.allclose(v, tv))
+
+ def test_loading_without_ml_dtype(self):
+ # This does not work as we cannot unload
+ # modules, copy this into its own file to test.
+ # https://github.com/huggingface/safetensors/issues/598
+ sys.modules.pop("ml_dtypes", None)
+ with safe_open(self.sf_filename, framework="flax") as f:
+ f.get_tensor("test3")