@@ -43,14 +43,14 @@ def wrapper(fn: Union[Tensor, str, pathlib.Path]) -> T: return func(Tensor(pathl
43
43
@accept_filename
44
44
def safe_load_metadata (t :Tensor ) -> tuple [Tensor , int , dict [str , Any ]]:
45
45
"""
46
- Loads a .safetensor file from disk , returning the data, metadata length , and metadata.
46
+ Loads a .safetensor file, returning the source tensor, data start position , and metadata.
47
47
"""
48
48
data_start = int .from_bytes (t [0 :8 ].data (), "little" ) + 8
49
49
return t , data_start , json .loads (t [8 :data_start ].data ().tobytes ())
50
50
51
51
def safe_load (fn :Union [Tensor , str , pathlib .Path ]) -> dict [str , Tensor ]:
52
52
"""
53
- Loads a .safetensor file from disk , returning the state_dict.
53
+ Loads a .safetensor file, returning the ` state_dict` .
54
54
55
55
```python
56
56
state_dict = nn.state.safe_load("test.safetensor")
@@ -63,7 +63,7 @@ def safe_load(fn:Union[Tensor, str, pathlib.Path]) -> dict[str, Tensor]:
63
63
64
64
def safe_save (tensors :dict [str , Tensor ], fn :str , metadata :Optional [dict [str , Any ]]= None ):
65
65
"""
66
- Saves a state_dict to disk in a .safetensor file with optional metadata.
66
+ Saves a ` state_dict` to disk in a .safetensor file with optional metadata.
67
67
68
68
```python
69
69
t = Tensor([1, 2, 3])
@@ -87,7 +87,7 @@ def safe_save(tensors:dict[str, Tensor], fn:str, metadata:Optional[dict[str, Any
87
87
88
88
def get_state_dict (obj , prefix :str = '' , tensor_type = Tensor ) -> dict [str , Tensor ]:
89
89
"""
90
- Returns a state_dict of the object, with optional prefix.
90
+ Returns a ` state_dict` of the object, with optional prefix.
91
91
92
92
```python exec="true" source="above" session="tensor" result="python"
93
93
class Net:
@@ -126,7 +126,7 @@ def __init__(self):
126
126
127
127
def load_state_dict (model , state_dict :dict [str , Tensor ], strict = True , verbose = True , consume = False , realize = True ) -> None :
128
128
"""
129
- Loads a state_dict into a model.
129
+ Loads a ` state_dict` into a model.
130
130
131
131
```python
132
132
class Net:
@@ -162,7 +162,11 @@ def __init__(self):
162
162
@accept_filename
163
163
def tar_extract (t : Tensor ) -> dict [str , Tensor ]:
164
164
"""
165
- Extracts files from a tar archive and returns them as dictionary of names (keys) and tensors (values).
165
+ ```python
166
+ tar_extract(fn: Tensor | str | Path) -> dict[str, Tensor]
167
+ ```
168
+
169
+ Extracts files from a tar archive and returns them as a dictionary of names (keys) and tensors (values).
166
170
167
171
```python
168
172
tensors = nn.state.tar_extract(Tensor(pathlib.Path("archive.tar")))
@@ -176,7 +180,11 @@ def tar_extract(t: Tensor) -> dict[str, Tensor]:
176
180
@accept_filename
177
181
def torch_load (t :Tensor ) -> dict [str , Tensor ]:
178
182
"""
179
- Loads a torch .pth file from disk.
183
+ ```python
184
+ torch_load(fn: Tensor | str | Path) -> dict[str, Tensor]
185
+ ```
186
+
187
+ Loads a torch .pth file, returning the `state_dict`.
180
188
181
189
```python
182
190
state_dict = nn.state.torch_load("test.pth")
@@ -294,13 +302,14 @@ def q_to_uint8(t: Tensor, b: int) -> Tensor:
294
302
@accept_filename
295
303
def gguf_load (tensor : Tensor ) -> tuple [dict , dict [str , Tensor ]]:
296
304
"""
297
- Loads a gguf file from a tensor .
305
+ Loads a . gguf file, returning the `kv_data` and `state_dict` .
298
306
299
307
```python
300
- fn = "Meta-Llama-3-8B-Instruct.Q4_0.gguf"
301
- gguf_tensor = Tensor.empty(os.stat(fn).st_size, dtype=dtypes.uint8, device=f"disk:{fn}").to(Device.DEFAULT)
302
- kv_data, state_dict = gguf_load(gguf_tensor)
308
+ gguf_tensor = Tensor(pathlib.Path("Meta-Llama-3-8B-Instruct.Q4_0.gguf")).to(Device.DEFAULT)
309
+ kv_data, state_dict = nn.state.gguf_load(gguf_tensor)
303
310
```
311
+
312
+ NOTE: The provided tensor must be on a device that supports execution.
304
313
"""
305
314
reader , kv_data , state_dict = io .BufferedReader (TensorIO (tensor ), 1_000_000 ), {}, {}
306
315
def read_unpack (fmt : str , n : int ): return struct .unpack (fmt , reader .read (n ))[0 ]
0 commit comments