Add test scripts
This commit is contained in:
parent
6c1d57f9cd
commit
8943455447
4 changed files with 60 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.safetensors
|
30
gentest.py
Normal file
30
gentest.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
# /// script
|
||||
# dependencies = ["numpy", "safetensors"]
|
||||
# ///
|
||||
|
||||
import numpy as np
|
||||
from safetensors.numpy import save_file
|
||||
|
||||
|
||||
arrs = {
|
||||
"arr_bool": np.array([[True, False], [True, False]], dtype=bool),
|
||||
"arr_f2": np.array([[1, 2, 3], [-139.20182, 5, 6]], dtype="<f2"),
|
||||
"arr_f4": np.array([[1, 2, 3], [-139.20182, 5, 6]], dtype="<f4"),
|
||||
"arr_f8": np.array([[1, 2, 3], [-139.20182, 5, 6]], dtype="<f8"),
|
||||
"arr_i1": np.array([[1, 2, 3], [-4, 5, 6]], dtype="<i1"),
|
||||
"arr_i2": np.array([[1, 2, 3], [-167, 5, 6]], dtype="<i2"),
|
||||
"arr_i4": np.array([[1, 2, 3], [-1393645220, 5, 6]], dtype="<i4"),
|
||||
"arr_i8": np.array([[1, 2, 3], [-1678172369384593271, 5, 6]], dtype="<i8"),
|
||||
"arr_u1": np.array([[1, 2, 3], [4, 5, 6]], dtype="<u1"),
|
||||
"arr_u2": np.array([[1, 2, 3], [167, 5, 6]], dtype="<u2"),
|
||||
"arr_u4": np.array([[1, 2, 3], [1393645220, 5, 6]], dtype="<u4"),
|
||||
"arr_u8": np.array([[1, 2, 3], [16781723693845932719, 5, 6]], dtype="<u8"),
|
||||
"multidim": np.random.rand(3, 4, 5),
|
||||
"scalar": np.asarray(42),
|
||||
"simple": np.random.rand(10),
|
||||
}
|
||||
|
||||
for name, arr in arrs.items():
|
||||
print(name, arr.dtype)
|
||||
print(arr)
|
||||
save_file(tensor_dict=arrs, filename="arrs.safetensors", metadata={"hello": "world"})
|
10
loadtest.py
Normal file
10
loadtest.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
# /// script
|
||||
# dependencies = ["numpy", "safetensors"]
|
||||
# ///
|
||||
|
||||
from safetensors.numpy import load_file
|
||||
|
||||
arrs = load_file("test.safetensors")
|
||||
for name, arr in arrs.items():
|
||||
print(name, arr.dtype)
|
||||
print(arr)
|
19
test.bqn
Normal file
19
test.bqn
Normal file
|
@ -0,0 +1,19 @@
|
|||
⟨ExtractMetadata,GetArrayNames,GetArray,SerializeArrays⟩←•Import"safetensors.bqn"
|
||||
|
||||
# Read a safetensors file generated from Python
|
||||
≠bytes←•file.MapBytes"arrs.safetensors"
|
||||
{•Out𝕩⋄•Show bytes⊸GetArray𝕩⋄⟨⟩}¨∧GetArrayNames bytes
|
||||
|
||||
# Serialize BQN arrays to a safetensors file
|
||||
a←3‿2⥊1‿0‿1‿1‿0‿0
|
||||
b←3‿4‿5⥊↕60
|
||||
c←2‿3⥊3.14×↕6
|
||||
"test.safetensors"•file.Bytes "arr_a"‿"arr_b"‿"arr_c" SerializeArrays a‿b‿c
|
||||
|
||||
# Read the file that was created
|
||||
≠bytes↩•file.MapBytes"test.safetensors"
|
||||
names←∧GetArrayNames bytes
|
||||
{•Out𝕩⋄•Show bytes⊸GetArray𝕩⋄⟨⟩}¨names
|
||||
! a≡bytes GetArray "arr_a"
|
||||
! b≡bytes GetArray "arr_b"
|
||||
! c≡bytes GetArray "arr_c"
|
Loading…
Add table
Add a link
Reference in a new issue