__init__.pyi 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Generated content DO NOT EDIT
  2. @staticmethod
  3. def deserialize(bytes):
  4. """
  5. Opens a safetensors lazily and returns tensors as asked
  6. Args:
  7. data (`bytes`):
  8. The byte content of a file
  9. Returns:
  10. (`List[str, Dict[str, Dict[str, any]]]`):
  11. The deserialized content is like:
  12. [("tensor_name", {"shape": [2, 3], "dtype": "F32", "data": b"\0\0.." }), (...)]
  13. """
  14. pass
  15. @staticmethod
  16. def serialize(tensor_dict, metadata=None):
  17. """
  18. Serializes raw data.
  19. Args:
  20. tensor_dict (`Dict[str, Dict[Any]]`):
  21. The tensor dict is like:
  22. {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
  23. metadata (`Dict[str, str]`, *optional*):
  24. The optional purely text annotations
  25. Returns:
  26. (`bytes`):
  27. The serialized content.
  28. """
  29. pass
  30. @staticmethod
  31. def serialize_file(tensor_dict, filename, metadata=None):
  32. """
  33. Serializes raw data into file.
  34. Args:
  35. tensor_dict (`Dict[str, Dict[Any]]`):
  36. The tensor dict is like:
  37. {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
  38. filename (`str`, or `os.PathLike`):
  39. The name of the file to write into.
  40. metadata (`Dict[str, str]`, *optional*):
  41. The optional purely text annotations
  42. Returns:
  43. (`NoneType`):
  44. On success return None
  45. """
  46. pass
  47. class safe_open:
  48. """
  49. Opens a safetensors lazily and returns tensors as asked
  50. Args:
  51. filename (`str`, or `os.PathLike`):
  52. The filename to open
  53. framework (`str`):
  54. The framework you want you tensors in. Supported values:
  55. `pt`, `tf`, `flax`, `numpy`.
  56. device (`str`, defaults to `"cpu"`):
  57. The device on which you want the tensors.
  58. """
  59. def __init__(self, filename, framework, device=...):
  60. pass
  61. def __enter__(self):
  62. """
  63. Start the context manager
  64. """
  65. pass
  66. def __exit__(self, _exc_type, _exc_value, _traceback):
  67. """
  68. Exits the context manager
  69. """
  70. pass
  71. def get_slice(self, name):
  72. """
  73. Returns a full slice view object
  74. Args:
  75. name (`str`):
  76. The name of the tensor you want
  77. Returns:
  78. (`PySafeSlice`):
  79. A dummy object you can slice into to get a real tensor
  80. Example:
  81. ```python
  82. from safetensors import safe_open
  83. with safe_open("model.safetensors", framework="pt", device=0) as f:
  84. tensor_part = f.get_slice("embedding")[:, ::8]
  85. ```
  86. """
  87. pass
  88. def get_tensor(self, name):
  89. """
  90. Returns a full tensor
  91. Args:
  92. name (`str`):
  93. The name of the tensor you want
  94. Returns:
  95. (`Tensor`):
  96. The tensor in the framework you opened the file for.
  97. Example:
  98. ```python
  99. from safetensors import safe_open
  100. with safe_open("model.safetensors", framework="pt", device=0) as f:
  101. tensor = f.get_tensor("embedding")
  102. ```
  103. """
  104. pass
  105. def keys(self):
  106. """
  107. Returns the names of the tensors in the file.
  108. Returns:
  109. (`List[str]`):
  110. The name of the tensors contained in that file
  111. """
  112. pass
  113. def metadata(self):
  114. """
  115. Return the special non tensor information in the header
  116. Returns:
  117. (`Dict[str, str]`):
  118. The freeform metadata.
  119. """
  120. pass
  121. def offset_keys(self):
  122. """
  123. Returns the names of the tensors in the file, ordered by offset.
  124. Returns:
  125. (`List[str]`):
  126. The name of the tensors contained in that file
  127. """
  128. pass
  129. class SafetensorError(Exception):
  130. """
  131. Custom Python Exception for Safetensor errors.
  132. """