Skip to content

Commit

Permalink
Added safetensors files.
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Nov 17, 2022
1 parent 8a328c3 commit 8450307
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 0 deletions.
Binary file added serialization/safetensors/B.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/B.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/H.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/H.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/I.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/I.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/b.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/b.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/d.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/d.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/e.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/e.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/f.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/f.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
79 changes: 79 additions & 0 deletions serialization/safetensors/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

from pathlib import Path

import numpy as np
import safetensors.numpy

import safetensors


__author__ = "KOLANICH"
__copyright__ = "Public domain"
__license__ = "Unlicense"

__reuse__ = """SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
"""

thisDir = Path(".").absolute()


ambigiousTypes = set("LPQlpq") # these letters result into the same arrays


def makeTypeCodes():
typeCodes = "".join(
sorted(set("".join(np.typecodes[el] for el in ("Complex", "AllInteger", "AllFloat"))))
)
typeCodes = "".join(set(typeCodes) - ambigiousTypes)
return typeCodes


def getPanicExceptionType():
try:
safetensors.safetensors_rust.serialize(
{"tensor_name": {"dtype": "test", "shape": [0, 0], "data": b""}}, None
)
except BaseException as ex: # pylint:disable=broad-except
return ex.__class__
return None


PanicException = getPanicExceptionType()


def makeNumpyDict(typeCodes) -> dict:
npDict = {}
for t in typeCodes:
a = np.array(range(-3, 3), dtype="<" + t)
try:
safetensors.numpy.save({"a": a}) # testing if the type safetensors-serializeable
except PanicException: # pylint:disable=broad-except
print(t, a.dtype)
else:
npDict[t] = a
return npDict


EXT = ".safetensors"


def dumpFile(name: str, data: bytes):
fn = name + EXT
(thisDir / fn).write_bytes(data)
(thisDir / (fn + ".license")).write_text(__reuse__)


def main():
typeCodes = makeTypeCodes()
npDict = makeNumpyDict(typeCodes)
metaDict = {"test": "a"}
dumpFile("overall", safetensors.numpy.save(npDict, metaDict))

for k, v in npDict.items():
dumpFile(k, safetensors.numpy.save({k: v}, metaDict))


if __name__ == "__main__":
main()
Binary file added serialization/safetensors/h.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/h.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/i.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/i.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense
Binary file added serialization/safetensors/overall.safetensors
Binary file not shown.
2 changes: 2 additions & 0 deletions serialization/safetensors/overall.safetensors.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Uncopyrightable
SPDX-License-Identifier: Unlicense

0 comments on commit 8450307

Please sign in to comment.