huggingface-hub 0.29.0rc2__py3-none-any.whl → 1.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- huggingface_hub/__init__.py +160 -46
- huggingface_hub/_commit_api.py +277 -71
- huggingface_hub/_commit_scheduler.py +15 -15
- huggingface_hub/_inference_endpoints.py +33 -22
- huggingface_hub/_jobs_api.py +301 -0
- huggingface_hub/_local_folder.py +18 -3
- huggingface_hub/_login.py +31 -63
- huggingface_hub/_oauth.py +460 -0
- huggingface_hub/_snapshot_download.py +241 -81
- huggingface_hub/_space_api.py +18 -10
- huggingface_hub/_tensorboard_logger.py +15 -19
- huggingface_hub/_upload_large_folder.py +196 -76
- huggingface_hub/_webhooks_payload.py +3 -3
- huggingface_hub/_webhooks_server.py +15 -25
- huggingface_hub/{commands → cli}/__init__.py +1 -15
- huggingface_hub/cli/_cli_utils.py +173 -0
- huggingface_hub/cli/auth.py +147 -0
- huggingface_hub/cli/cache.py +841 -0
- huggingface_hub/cli/download.py +189 -0
- huggingface_hub/cli/hf.py +60 -0
- huggingface_hub/cli/inference_endpoints.py +377 -0
- huggingface_hub/cli/jobs.py +772 -0
- huggingface_hub/cli/lfs.py +175 -0
- huggingface_hub/cli/repo.py +315 -0
- huggingface_hub/cli/repo_files.py +94 -0
- huggingface_hub/{commands/env.py → cli/system.py} +10 -13
- huggingface_hub/cli/upload.py +294 -0
- huggingface_hub/cli/upload_large_folder.py +117 -0
- huggingface_hub/community.py +20 -12
- huggingface_hub/constants.py +83 -59
- huggingface_hub/dataclasses.py +609 -0
- huggingface_hub/errors.py +99 -30
- huggingface_hub/fastai_utils.py +30 -41
- huggingface_hub/file_download.py +606 -346
- huggingface_hub/hf_api.py +2445 -1132
- huggingface_hub/hf_file_system.py +269 -152
- huggingface_hub/hub_mixin.py +61 -66
- huggingface_hub/inference/_client.py +501 -630
- huggingface_hub/inference/_common.py +133 -121
- huggingface_hub/inference/_generated/_async_client.py +536 -722
- huggingface_hub/inference/_generated/types/__init__.py +6 -1
- huggingface_hub/inference/_generated/types/automatic_speech_recognition.py +5 -6
- huggingface_hub/inference/_generated/types/base.py +10 -7
- huggingface_hub/inference/_generated/types/chat_completion.py +77 -31
- huggingface_hub/inference/_generated/types/depth_estimation.py +2 -2
- huggingface_hub/inference/_generated/types/document_question_answering.py +2 -2
- huggingface_hub/inference/_generated/types/feature_extraction.py +2 -2
- huggingface_hub/inference/_generated/types/fill_mask.py +2 -2
- huggingface_hub/inference/_generated/types/image_to_image.py +8 -2
- huggingface_hub/inference/_generated/types/image_to_text.py +2 -3
- huggingface_hub/inference/_generated/types/image_to_video.py +60 -0
- huggingface_hub/inference/_generated/types/sentence_similarity.py +3 -3
- huggingface_hub/inference/_generated/types/summarization.py +2 -2
- huggingface_hub/inference/_generated/types/table_question_answering.py +5 -5
- huggingface_hub/inference/_generated/types/text2text_generation.py +2 -2
- huggingface_hub/inference/_generated/types/text_generation.py +11 -11
- huggingface_hub/inference/_generated/types/text_to_audio.py +1 -2
- huggingface_hub/inference/_generated/types/text_to_speech.py +1 -2
- huggingface_hub/inference/_generated/types/text_to_video.py +2 -2
- huggingface_hub/inference/_generated/types/token_classification.py +2 -2
- huggingface_hub/inference/_generated/types/translation.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_classification.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_image_classification.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_object_detection.py +1 -3
- huggingface_hub/inference/_mcp/__init__.py +0 -0
- huggingface_hub/inference/_mcp/_cli_hacks.py +88 -0
- huggingface_hub/inference/_mcp/agent.py +100 -0
- huggingface_hub/inference/_mcp/cli.py +247 -0
- huggingface_hub/inference/_mcp/constants.py +81 -0
- huggingface_hub/inference/_mcp/mcp_client.py +395 -0
- huggingface_hub/inference/_mcp/types.py +45 -0
- huggingface_hub/inference/_mcp/utils.py +128 -0
- huggingface_hub/inference/_providers/__init__.py +149 -20
- huggingface_hub/inference/_providers/_common.py +160 -37
- huggingface_hub/inference/_providers/black_forest_labs.py +12 -9
- huggingface_hub/inference/_providers/cerebras.py +6 -0
- huggingface_hub/inference/_providers/clarifai.py +13 -0
- huggingface_hub/inference/_providers/cohere.py +32 -0
- huggingface_hub/inference/_providers/fal_ai.py +231 -22
- huggingface_hub/inference/_providers/featherless_ai.py +38 -0
- huggingface_hub/inference/_providers/fireworks_ai.py +22 -1
- huggingface_hub/inference/_providers/groq.py +9 -0
- huggingface_hub/inference/_providers/hf_inference.py +143 -33
- huggingface_hub/inference/_providers/hyperbolic.py +9 -5
- huggingface_hub/inference/_providers/nebius.py +47 -5
- huggingface_hub/inference/_providers/novita.py +48 -5
- huggingface_hub/inference/_providers/nscale.py +44 -0
- huggingface_hub/inference/_providers/openai.py +25 -0
- huggingface_hub/inference/_providers/publicai.py +6 -0
- huggingface_hub/inference/_providers/replicate.py +46 -9
- huggingface_hub/inference/_providers/sambanova.py +37 -1
- huggingface_hub/inference/_providers/scaleway.py +28 -0
- huggingface_hub/inference/_providers/together.py +34 -5
- huggingface_hub/inference/_providers/wavespeed.py +138 -0
- huggingface_hub/inference/_providers/zai_org.py +17 -0
- huggingface_hub/lfs.py +33 -100
- huggingface_hub/repocard.py +34 -38
- huggingface_hub/repocard_data.py +79 -59
- huggingface_hub/serialization/__init__.py +0 -1
- huggingface_hub/serialization/_base.py +12 -15
- huggingface_hub/serialization/_dduf.py +8 -8
- huggingface_hub/serialization/_torch.py +69 -69
- huggingface_hub/utils/__init__.py +27 -8
- huggingface_hub/utils/_auth.py +7 -7
- huggingface_hub/utils/_cache_manager.py +92 -147
- huggingface_hub/utils/_chunk_utils.py +2 -3
- huggingface_hub/utils/_deprecation.py +1 -1
- huggingface_hub/utils/_dotenv.py +55 -0
- huggingface_hub/utils/_experimental.py +7 -5
- huggingface_hub/utils/_fixes.py +0 -10
- huggingface_hub/utils/_git_credential.py +5 -5
- huggingface_hub/utils/_headers.py +8 -30
- huggingface_hub/utils/_http.py +399 -237
- huggingface_hub/utils/_pagination.py +6 -6
- huggingface_hub/utils/_parsing.py +98 -0
- huggingface_hub/utils/_paths.py +5 -5
- huggingface_hub/utils/_runtime.py +74 -22
- huggingface_hub/utils/_safetensors.py +21 -21
- huggingface_hub/utils/_subprocess.py +13 -11
- huggingface_hub/utils/_telemetry.py +4 -4
- huggingface_hub/{commands/_cli_utils.py → utils/_terminal.py} +4 -4
- huggingface_hub/utils/_typing.py +25 -5
- huggingface_hub/utils/_validators.py +55 -74
- huggingface_hub/utils/_verification.py +167 -0
- huggingface_hub/utils/_xet.py +235 -0
- huggingface_hub/utils/_xet_progress_reporting.py +162 -0
- huggingface_hub/utils/insecure_hashlib.py +3 -5
- huggingface_hub/utils/logging.py +8 -11
- huggingface_hub/utils/tqdm.py +33 -4
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/METADATA +94 -82
- huggingface_hub-1.1.3.dist-info/RECORD +155 -0
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/WHEEL +1 -1
- huggingface_hub-1.1.3.dist-info/entry_points.txt +6 -0
- huggingface_hub/commands/delete_cache.py +0 -428
- huggingface_hub/commands/download.py +0 -200
- huggingface_hub/commands/huggingface_cli.py +0 -61
- huggingface_hub/commands/lfs.py +0 -200
- huggingface_hub/commands/repo_files.py +0 -128
- huggingface_hub/commands/scan_cache.py +0 -181
- huggingface_hub/commands/tag.py +0 -159
- huggingface_hub/commands/upload.py +0 -299
- huggingface_hub/commands/upload_large_folder.py +0 -129
- huggingface_hub/commands/user.py +0 -304
- huggingface_hub/commands/version.py +0 -37
- huggingface_hub/inference_api.py +0 -217
- huggingface_hub/keras_mixin.py +0 -500
- huggingface_hub/repository.py +0 -1477
- huggingface_hub/serialization/_tensorflow.py +0 -95
- huggingface_hub/utils/_hf_folder.py +0 -68
- huggingface_hub-0.29.0rc2.dist-info/RECORD +0 -131
- huggingface_hub-0.29.0rc2.dist-info/entry_points.txt +0 -6
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info/licenses}/LICENSE +0 -0
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/top_level.txt +0 -0
|
@@ -20,7 +20,7 @@ import re
|
|
|
20
20
|
from collections import defaultdict, namedtuple
|
|
21
21
|
from functools import lru_cache
|
|
22
22
|
from pathlib import Path
|
|
23
|
-
from typing import TYPE_CHECKING, Any,
|
|
23
|
+
from typing import TYPE_CHECKING, Any, Iterable, NamedTuple, Optional, Union
|
|
24
24
|
|
|
25
25
|
from packaging import version
|
|
26
26
|
|
|
@@ -43,10 +43,10 @@ def save_torch_model(
|
|
|
43
43
|
filename_pattern: Optional[str] = None,
|
|
44
44
|
force_contiguous: bool = True,
|
|
45
45
|
max_shard_size: Union[int, str] = MAX_SHARD_SIZE,
|
|
46
|
-
metadata: Optional[
|
|
46
|
+
metadata: Optional[dict[str, str]] = None,
|
|
47
47
|
safe_serialization: bool = True,
|
|
48
48
|
is_main_process: bool = True,
|
|
49
|
-
shared_tensors_to_discard: Optional[
|
|
49
|
+
shared_tensors_to_discard: Optional[list[str]] = None,
|
|
50
50
|
):
|
|
51
51
|
"""
|
|
52
52
|
Saves a given torch model to disk, handling sharding and shared tensors issues.
|
|
@@ -63,18 +63,12 @@ def save_torch_model(
|
|
|
63
63
|
|
|
64
64
|
Before saving the model, the `save_directory` is cleaned from any previous shard files.
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
> [!WARNING]
|
|
67
|
+
> If one of the model's tensor is bigger than `max_shard_size`, it will end up in its own shard which will have a
|
|
68
|
+
> size greater than `max_shard_size`.
|
|
67
69
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
</Tip>
|
|
72
|
-
|
|
73
|
-
<Tip warning={true}>
|
|
74
|
-
|
|
75
|
-
If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.
|
|
76
|
-
|
|
77
|
-
</Tip>
|
|
70
|
+
> [!WARNING]
|
|
71
|
+
> If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.
|
|
78
72
|
|
|
79
73
|
Args:
|
|
80
74
|
model (`torch.nn.Module`):
|
|
@@ -92,7 +86,7 @@ def save_torch_model(
|
|
|
92
86
|
that reason. Defaults to `True`.
|
|
93
87
|
max_shard_size (`int` or `str`, *optional*):
|
|
94
88
|
The maximum size of each shard, in bytes. Defaults to 5GB.
|
|
95
|
-
metadata (`
|
|
89
|
+
metadata (`dict[str, str]`, *optional*):
|
|
96
90
|
Extra information to save along with the model. Some metadata will be added for each dropped tensors.
|
|
97
91
|
This information will not be enough to recover the entire shared structure but might help understanding
|
|
98
92
|
things.
|
|
@@ -104,7 +98,7 @@ def save_torch_model(
|
|
|
104
98
|
Whether the process calling this is the main process or not. Useful when in distributed training like
|
|
105
99
|
TPUs and need to call this function from all processes. In this case, set `is_main_process=True` only on
|
|
106
100
|
the main process to avoid race conditions. Defaults to True.
|
|
107
|
-
shared_tensors_to_discard (`
|
|
101
|
+
shared_tensors_to_discard (`list[str]`, *optional*):
|
|
108
102
|
List of tensor names to drop when saving shared tensors. If not provided and shared tensors are
|
|
109
103
|
detected, it will drop the first name alphabetically.
|
|
110
104
|
|
|
@@ -137,16 +131,16 @@ def save_torch_model(
|
|
|
137
131
|
|
|
138
132
|
|
|
139
133
|
def save_torch_state_dict(
|
|
140
|
-
state_dict:
|
|
134
|
+
state_dict: dict[str, "torch.Tensor"],
|
|
141
135
|
save_directory: Union[str, Path],
|
|
142
136
|
*,
|
|
143
137
|
filename_pattern: Optional[str] = None,
|
|
144
138
|
force_contiguous: bool = True,
|
|
145
139
|
max_shard_size: Union[int, str] = MAX_SHARD_SIZE,
|
|
146
|
-
metadata: Optional[
|
|
140
|
+
metadata: Optional[dict[str, str]] = None,
|
|
147
141
|
safe_serialization: bool = True,
|
|
148
142
|
is_main_process: bool = True,
|
|
149
|
-
shared_tensors_to_discard: Optional[
|
|
143
|
+
shared_tensors_to_discard: Optional[list[str]] = None,
|
|
150
144
|
) -> None:
|
|
151
145
|
"""
|
|
152
146
|
Save a model state dictionary to the disk, handling sharding and shared tensors issues.
|
|
@@ -163,21 +157,15 @@ def save_torch_state_dict(
|
|
|
163
157
|
|
|
164
158
|
Before saving the model, the `save_directory` is cleaned from any previous shard files.
|
|
165
159
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
size greater than `max_shard_size`.
|
|
170
|
-
|
|
171
|
-
</Tip>
|
|
160
|
+
> [!WARNING]
|
|
161
|
+
> If one of the model's tensor is bigger than `max_shard_size`, it will end up in its own shard which will have a
|
|
162
|
+
> size greater than `max_shard_size`.
|
|
172
163
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.
|
|
176
|
-
|
|
177
|
-
</Tip>
|
|
164
|
+
> [!WARNING]
|
|
165
|
+
> If your model is a `transformers.PreTrainedModel`, you should pass `model._tied_weights_keys` as `shared_tensors_to_discard` to properly handle shared tensors saving. This ensures the correct duplicate tensors are discarded during saving.
|
|
178
166
|
|
|
179
167
|
Args:
|
|
180
|
-
state_dict (`
|
|
168
|
+
state_dict (`dict[str, torch.Tensor]`):
|
|
181
169
|
The state dictionary to save.
|
|
182
170
|
save_directory (`str` or `Path`):
|
|
183
171
|
The directory in which the model will be saved.
|
|
@@ -192,7 +180,7 @@ def save_torch_state_dict(
|
|
|
192
180
|
that reason. Defaults to `True`.
|
|
193
181
|
max_shard_size (`int` or `str`, *optional*):
|
|
194
182
|
The maximum size of each shard, in bytes. Defaults to 5GB.
|
|
195
|
-
metadata (`
|
|
183
|
+
metadata (`dict[str, str]`, *optional*):
|
|
196
184
|
Extra information to save along with the model. Some metadata will be added for each dropped tensors.
|
|
197
185
|
This information will not be enough to recover the entire shared structure but might help understanding
|
|
198
186
|
things.
|
|
@@ -204,7 +192,7 @@ def save_torch_state_dict(
|
|
|
204
192
|
Whether the process calling this is the main process or not. Useful when in distributed training like
|
|
205
193
|
TPUs and need to call this function from all processes. In this case, set `is_main_process=True` only on
|
|
206
194
|
the main process to avoid race conditions. Defaults to True.
|
|
207
|
-
shared_tensors_to_discard (`
|
|
195
|
+
shared_tensors_to_discard (`list[str]`, *optional*):
|
|
208
196
|
List of tensor names to drop when saving shared tensors. If not provided and shared tensors are
|
|
209
197
|
detected, it will drop the first name alphabetically.
|
|
210
198
|
|
|
@@ -246,7 +234,7 @@ def save_torch_state_dict(
|
|
|
246
234
|
shared_tensors_to_discard=shared_tensors_to_discard,
|
|
247
235
|
)
|
|
248
236
|
else:
|
|
249
|
-
from torch import save as save_file_fn # type: ignore[assignment]
|
|
237
|
+
from torch import save as save_file_fn # type: ignore[assignment, no-redef]
|
|
250
238
|
|
|
251
239
|
logger.warning(
|
|
252
240
|
"You are using unsafe serialization. Due to security reasons, it is recommended not to load "
|
|
@@ -278,7 +266,7 @@ def save_torch_state_dict(
|
|
|
278
266
|
safe_file_kwargs = {"metadata": per_file_metadata} if safe_serialization else {}
|
|
279
267
|
for filename, tensors in state_dict_split.filename_to_tensors.items():
|
|
280
268
|
shard = {tensor: state_dict[tensor] for tensor in tensors}
|
|
281
|
-
save_file_fn(shard, os.path.join(save_directory, filename), **safe_file_kwargs)
|
|
269
|
+
save_file_fn(shard, os.path.join(save_directory, filename), **safe_file_kwargs) # ty: ignore[invalid-argument-type]
|
|
282
270
|
logger.debug(f"Shard saved to {filename}")
|
|
283
271
|
|
|
284
272
|
# Save the index (if any)
|
|
@@ -300,7 +288,7 @@ def save_torch_state_dict(
|
|
|
300
288
|
|
|
301
289
|
|
|
302
290
|
def split_torch_state_dict_into_shards(
|
|
303
|
-
state_dict:
|
|
291
|
+
state_dict: dict[str, "torch.Tensor"],
|
|
304
292
|
*,
|
|
305
293
|
filename_pattern: str = constants.SAFETENSORS_WEIGHTS_FILE_PATTERN,
|
|
306
294
|
max_shard_size: Union[int, str] = MAX_SHARD_SIZE,
|
|
@@ -314,22 +302,16 @@ def split_torch_state_dict_into_shards(
|
|
|
314
302
|
[6+2+2GB], [6+2GB], [6GB].
|
|
315
303
|
|
|
316
304
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
`split_torch_state_dict_into_shards` under the hood.
|
|
321
|
-
|
|
322
|
-
</Tip>
|
|
323
|
-
|
|
324
|
-
<Tip warning={true}>
|
|
305
|
+
> [!TIP]
|
|
306
|
+
> To save a model state dictionary to the disk, see [`save_torch_state_dict`]. This helper uses
|
|
307
|
+
> `split_torch_state_dict_into_shards` under the hood.
|
|
325
308
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
</Tip>
|
|
309
|
+
> [!WARNING]
|
|
310
|
+
> If one of the model's tensor is bigger than `max_shard_size`, it will end up in its own shard which will have a
|
|
311
|
+
> size greater than `max_shard_size`.
|
|
330
312
|
|
|
331
313
|
Args:
|
|
332
|
-
state_dict (`
|
|
314
|
+
state_dict (`dict[str, torch.Tensor]`):
|
|
333
315
|
The state dictionary to save.
|
|
334
316
|
filename_pattern (`str`, *optional*):
|
|
335
317
|
The pattern to generate the files names in which the model will be saved. Pattern must be a string that
|
|
@@ -348,7 +330,7 @@ def split_torch_state_dict_into_shards(
|
|
|
348
330
|
>>> from safetensors.torch import save_file as safe_save_file
|
|
349
331
|
>>> from huggingface_hub import split_torch_state_dict_into_shards
|
|
350
332
|
|
|
351
|
-
>>> def save_state_dict(state_dict:
|
|
333
|
+
>>> def save_state_dict(state_dict: dict[str, torch.Tensor], save_directory: str):
|
|
352
334
|
... state_dict_split = split_torch_state_dict_into_shards(state_dict)
|
|
353
335
|
... for filename, tensors in state_dict_split.filename_to_tensors.items():
|
|
354
336
|
... shard = {tensor: state_dict[tensor] for tensor in tensors}
|
|
@@ -560,7 +542,7 @@ def load_state_dict_from_file(
|
|
|
560
542
|
map_location: Optional[Union[str, "torch.device"]] = None,
|
|
561
543
|
weights_only: bool = False,
|
|
562
544
|
mmap: bool = False,
|
|
563
|
-
) -> Union[
|
|
545
|
+
) -> Union[dict[str, "torch.Tensor"], Any]:
|
|
564
546
|
"""
|
|
565
547
|
Loads a checkpoint file, handling both safetensors and pickle checkpoint formats.
|
|
566
548
|
|
|
@@ -580,7 +562,7 @@ def load_state_dict_from_file(
|
|
|
580
562
|
loading safetensors files, as the `safetensors` library uses memory mapping by default.
|
|
581
563
|
|
|
582
564
|
Returns:
|
|
583
|
-
`Union[
|
|
565
|
+
`Union[dict[str, "torch.Tensor"], Any]`: The loaded checkpoint.
|
|
584
566
|
- For safetensors files: always returns a dictionary mapping parameter names to tensors.
|
|
585
567
|
- For pickle files: returns any Python object that was pickled (commonly a state dict, but could be
|
|
586
568
|
an entire model, optimizer state, or any other Python object).
|
|
@@ -700,12 +682,21 @@ def _validate_keys_for_strict_loading(
|
|
|
700
682
|
raise RuntimeError(error_message)
|
|
701
683
|
|
|
702
684
|
|
|
703
|
-
def _get_unique_id(tensor: "torch.Tensor") -> Union[int,
|
|
685
|
+
def _get_unique_id(tensor: "torch.Tensor") -> Union[int, tuple[Any, ...]]:
|
|
704
686
|
"""Returns a unique id for plain tensor
|
|
705
687
|
or a (potentially nested) Tuple of unique id for the flattened Tensor
|
|
706
688
|
if the input is a wrapper tensor subclass Tensor
|
|
707
689
|
"""
|
|
708
690
|
|
|
691
|
+
try:
|
|
692
|
+
from torch.distributed.tensor import DTensor
|
|
693
|
+
|
|
694
|
+
if isinstance(tensor, DTensor):
|
|
695
|
+
local_tensor = tensor.to_local()
|
|
696
|
+
return local_tensor.storage().data_ptr()
|
|
697
|
+
except ImportError:
|
|
698
|
+
pass
|
|
699
|
+
|
|
709
700
|
try:
|
|
710
701
|
# for torch 2.1 and above we can also handle tensor subclasses
|
|
711
702
|
from torch.utils._python_dispatch import is_traceable_wrapper_subclass
|
|
@@ -715,11 +706,11 @@ def _get_unique_id(tensor: "torch.Tensor") -> Union[int, Tuple[Any, ...]]:
|
|
|
715
706
|
return tuple(_get_unique_id(getattr(tensor, attr)) for attr in attrs)
|
|
716
707
|
|
|
717
708
|
except ImportError:
|
|
718
|
-
# for torch version less than 2.1, we can
|
|
709
|
+
# for torch version less than 2.1, we can fall back to original implementation
|
|
719
710
|
pass
|
|
720
711
|
|
|
721
712
|
if tensor.device.type == "xla" and is_torch_tpu_available():
|
|
722
|
-
# NOTE: xla tensors
|
|
713
|
+
# NOTE: xla tensors don't have storage
|
|
723
714
|
# use some other unique id to distinguish.
|
|
724
715
|
# this is a XLA tensor, it must be created using torch_xla's
|
|
725
716
|
# device. So the following import is safe:
|
|
@@ -732,7 +723,7 @@ def _get_unique_id(tensor: "torch.Tensor") -> Union[int, Tuple[Any, ...]]:
|
|
|
732
723
|
return unique_id
|
|
733
724
|
|
|
734
725
|
|
|
735
|
-
def get_torch_storage_id(tensor: "torch.Tensor") -> Optional[
|
|
726
|
+
def get_torch_storage_id(tensor: "torch.Tensor") -> Optional[tuple["torch.device", Union[int, tuple[Any, ...]], int]]:
|
|
736
727
|
"""
|
|
737
728
|
Return unique identifier to a tensor storage.
|
|
738
729
|
|
|
@@ -753,6 +744,15 @@ def get_torch_storage_size(tensor: "torch.Tensor") -> int:
|
|
|
753
744
|
"""
|
|
754
745
|
Taken from https://github.com/huggingface/safetensors/blob/08db34094e9e59e2f9218f2df133b7b4aaff5a99/bindings/python/py_src/safetensors/torch.py#L31C1-L41C59
|
|
755
746
|
"""
|
|
747
|
+
try:
|
|
748
|
+
from torch.distributed.tensor import DTensor
|
|
749
|
+
|
|
750
|
+
if isinstance(tensor, DTensor):
|
|
751
|
+
# this returns the size of the FULL tensor in bytes
|
|
752
|
+
return tensor.nbytes
|
|
753
|
+
except ImportError:
|
|
754
|
+
pass
|
|
755
|
+
|
|
756
756
|
try:
|
|
757
757
|
# for torch 2.1 and above we can also handle tensor subclasses
|
|
758
758
|
from torch.utils._python_dispatch import is_traceable_wrapper_subclass
|
|
@@ -761,7 +761,7 @@ def get_torch_storage_size(tensor: "torch.Tensor") -> int:
|
|
|
761
761
|
attrs, _ = tensor.__tensor_flatten__() # type: ignore[attr-defined]
|
|
762
762
|
return sum(get_torch_storage_size(getattr(tensor, attr)) for attr in attrs)
|
|
763
763
|
except ImportError:
|
|
764
|
-
# for torch version less than 2.1, we can
|
|
764
|
+
# for torch version less than 2.1, we can fall back to original implementation
|
|
765
765
|
pass
|
|
766
766
|
|
|
767
767
|
try:
|
|
@@ -797,7 +797,7 @@ def is_torch_tpu_available(check_device=True):
|
|
|
797
797
|
return False
|
|
798
798
|
|
|
799
799
|
|
|
800
|
-
def storage_ptr(tensor: "torch.Tensor") -> Union[int,
|
|
800
|
+
def storage_ptr(tensor: "torch.Tensor") -> Union[int, tuple[Any, ...]]:
|
|
801
801
|
"""
|
|
802
802
|
Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L11.
|
|
803
803
|
"""
|
|
@@ -808,7 +808,7 @@ def storage_ptr(tensor: "torch.Tensor") -> Union[int, Tuple[Any, ...]]:
|
|
|
808
808
|
if is_traceable_wrapper_subclass(tensor):
|
|
809
809
|
return _get_unique_id(tensor) # type: ignore
|
|
810
810
|
except ImportError:
|
|
811
|
-
# for torch version less than 2.1, we can
|
|
811
|
+
# for torch version less than 2.1, we can fall back to original implementation
|
|
812
812
|
pass
|
|
813
813
|
|
|
814
814
|
try:
|
|
@@ -823,10 +823,10 @@ def storage_ptr(tensor: "torch.Tensor") -> Union[int, Tuple[Any, ...]]:
|
|
|
823
823
|
|
|
824
824
|
|
|
825
825
|
def _clean_state_dict_for_safetensors(
|
|
826
|
-
state_dict:
|
|
827
|
-
metadata:
|
|
826
|
+
state_dict: dict[str, "torch.Tensor"],
|
|
827
|
+
metadata: dict[str, str],
|
|
828
828
|
force_contiguous: bool = True,
|
|
829
|
-
shared_tensors_to_discard: Optional[
|
|
829
|
+
shared_tensors_to_discard: Optional[list[str]] = None,
|
|
830
830
|
):
|
|
831
831
|
"""Remove shared tensors from state_dict and update metadata accordingly (for reloading).
|
|
832
832
|
|
|
@@ -860,7 +860,7 @@ def _end_ptr(tensor: "torch.Tensor") -> int:
|
|
|
860
860
|
return stop
|
|
861
861
|
|
|
862
862
|
|
|
863
|
-
def _filter_shared_not_shared(tensors:
|
|
863
|
+
def _filter_shared_not_shared(tensors: list[set[str]], state_dict: dict[str, "torch.Tensor"]) -> list[set[str]]:
|
|
864
864
|
"""
|
|
865
865
|
Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L44
|
|
866
866
|
"""
|
|
@@ -888,7 +888,7 @@ def _filter_shared_not_shared(tensors: List[Set[str]], state_dict: Dict[str, "to
|
|
|
888
888
|
return filtered_tensors
|
|
889
889
|
|
|
890
890
|
|
|
891
|
-
def _find_shared_tensors(state_dict:
|
|
891
|
+
def _find_shared_tensors(state_dict: dict[str, "torch.Tensor"]) -> list[set[str]]:
|
|
892
892
|
"""
|
|
893
893
|
Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L69.
|
|
894
894
|
"""
|
|
@@ -916,7 +916,7 @@ def _is_complete(tensor: "torch.Tensor") -> bool:
|
|
|
916
916
|
attrs, _ = tensor.__tensor_flatten__() # type: ignore[attr-defined]
|
|
917
917
|
return all(_is_complete(getattr(tensor, attr)) for attr in attrs)
|
|
918
918
|
except ImportError:
|
|
919
|
-
# for torch version less than 2.1, we can
|
|
919
|
+
# for torch version less than 2.1, we can fall back to original implementation
|
|
920
920
|
pass
|
|
921
921
|
|
|
922
922
|
return tensor.data_ptr() == storage_ptr(tensor) and tensor.nelement() * _get_dtype_size(
|
|
@@ -925,11 +925,11 @@ def _is_complete(tensor: "torch.Tensor") -> bool:
|
|
|
925
925
|
|
|
926
926
|
|
|
927
927
|
def _remove_duplicate_names(
|
|
928
|
-
state_dict:
|
|
928
|
+
state_dict: dict[str, "torch.Tensor"],
|
|
929
929
|
*,
|
|
930
|
-
preferred_names: Optional[
|
|
931
|
-
discard_names: Optional[
|
|
932
|
-
) ->
|
|
930
|
+
preferred_names: Optional[list[str]] = None,
|
|
931
|
+
discard_names: Optional[list[str]] = None,
|
|
932
|
+
) -> dict[str, list[str]]:
|
|
933
933
|
"""
|
|
934
934
|
Taken from https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/py_src/safetensors/torch.py#L80
|
|
935
935
|
"""
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
# limitations under the License
|
|
15
15
|
|
|
16
16
|
# ruff: noqa: F401
|
|
17
|
-
|
|
18
17
|
from huggingface_hub.errors import (
|
|
19
18
|
BadRequestError,
|
|
20
19
|
CacheNotFound,
|
|
@@ -43,6 +42,7 @@ from ._cache_manager import (
|
|
|
43
42
|
CachedRevisionInfo,
|
|
44
43
|
DeleteCacheStrategy,
|
|
45
44
|
HFCacheInfo,
|
|
45
|
+
_format_size,
|
|
46
46
|
scan_cache_dir,
|
|
47
47
|
)
|
|
48
48
|
from ._chunk_utils import chunk_iterable
|
|
@@ -51,14 +51,18 @@ from ._experimental import experimental
|
|
|
51
51
|
from ._fixes import SoftTemporaryDirectory, WeakFileLock, yaml_dump
|
|
52
52
|
from ._git_credential import list_credential_helpers, set_git_credential, unset_git_credential
|
|
53
53
|
from ._headers import build_hf_headers, get_token_to_send
|
|
54
|
-
from ._hf_folder import HfFolder
|
|
55
54
|
from ._http import (
|
|
56
|
-
|
|
55
|
+
ASYNC_CLIENT_FACTORY_T,
|
|
56
|
+
CLIENT_FACTORY_T,
|
|
57
|
+
close_session,
|
|
57
58
|
fix_hf_endpoint_in_url,
|
|
59
|
+
get_async_session,
|
|
58
60
|
get_session,
|
|
59
61
|
hf_raise_for_status,
|
|
60
62
|
http_backoff,
|
|
61
|
-
|
|
63
|
+
http_stream_backoff,
|
|
64
|
+
set_async_client_factory,
|
|
65
|
+
set_client_factory,
|
|
62
66
|
)
|
|
63
67
|
from ._pagination import paginate
|
|
64
68
|
from ._paths import DEFAULT_IGNORE_PATTERNS, FORBIDDEN_FOLDERS, filter_repo_objects
|
|
@@ -71,7 +75,6 @@ from ._runtime import (
|
|
|
71
75
|
get_gradio_version,
|
|
72
76
|
get_graphviz_version,
|
|
73
77
|
get_hf_hub_version,
|
|
74
|
-
get_hf_transfer_version,
|
|
75
78
|
get_jinja_version,
|
|
76
79
|
get_numpy_version,
|
|
77
80
|
get_pillow_version,
|
|
@@ -81,6 +84,7 @@ from ._runtime import (
|
|
|
81
84
|
get_tensorboard_version,
|
|
82
85
|
get_tf_version,
|
|
83
86
|
get_torch_version,
|
|
87
|
+
installation_method,
|
|
84
88
|
is_aiohttp_available,
|
|
85
89
|
is_colab_enterprise,
|
|
86
90
|
is_fastai_available,
|
|
@@ -89,7 +93,6 @@ from ._runtime import (
|
|
|
89
93
|
is_google_colab,
|
|
90
94
|
is_gradio_available,
|
|
91
95
|
is_graphviz_available,
|
|
92
|
-
is_hf_transfer_available,
|
|
93
96
|
is_jinja_available,
|
|
94
97
|
is_notebook,
|
|
95
98
|
is_numpy_available,
|
|
@@ -105,6 +108,22 @@ from ._runtime import (
|
|
|
105
108
|
from ._safetensors import SafetensorsFileMetadata, SafetensorsRepoMetadata, TensorInfo
|
|
106
109
|
from ._subprocess import capture_output, run_interactive_subprocess, run_subprocess
|
|
107
110
|
from ._telemetry import send_telemetry
|
|
111
|
+
from ._terminal import ANSI, tabulate
|
|
108
112
|
from ._typing import is_jsonable, is_simple_optional_type, unwrap_simple_optional_type
|
|
109
|
-
from ._validators import
|
|
110
|
-
from .
|
|
113
|
+
from ._validators import validate_hf_hub_args, validate_repo_id
|
|
114
|
+
from ._xet import (
|
|
115
|
+
XetConnectionInfo,
|
|
116
|
+
XetFileData,
|
|
117
|
+
XetTokenType,
|
|
118
|
+
fetch_xet_connection_info_from_repo_info,
|
|
119
|
+
parse_xet_file_data_from_response,
|
|
120
|
+
refresh_xet_connection_info,
|
|
121
|
+
)
|
|
122
|
+
from .tqdm import (
|
|
123
|
+
are_progress_bars_disabled,
|
|
124
|
+
disable_progress_bars,
|
|
125
|
+
enable_progress_bars,
|
|
126
|
+
is_tqdm_disabled,
|
|
127
|
+
tqdm,
|
|
128
|
+
tqdm_stream_file,
|
|
129
|
+
)
|
huggingface_hub/utils/_auth.py
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
-
"""Contains
|
|
14
|
+
"""Contains a helper to get the token from machine (env variable, secret or config file)."""
|
|
15
15
|
|
|
16
16
|
import configparser
|
|
17
17
|
import logging
|
|
@@ -19,7 +19,7 @@ import os
|
|
|
19
19
|
import warnings
|
|
20
20
|
from pathlib import Path
|
|
21
21
|
from threading import Lock
|
|
22
|
-
from typing import
|
|
22
|
+
from typing import Optional
|
|
23
23
|
|
|
24
24
|
from .. import constants
|
|
25
25
|
from ._runtime import is_colab_enterprise, is_google_colab
|
|
@@ -41,7 +41,7 @@ def get_token() -> Optional[str]:
|
|
|
41
41
|
|
|
42
42
|
Token is retrieved in priority from the `HF_TOKEN` environment variable. Otherwise, we read the token file located
|
|
43
43
|
in the Hugging Face home folder. Returns None if user is not logged in. To log in, use [`login`] or
|
|
44
|
-
`
|
|
44
|
+
`hf auth login`.
|
|
45
45
|
|
|
46
46
|
Returns:
|
|
47
47
|
`str` or `None`: The token, `None` if it doesn't exist.
|
|
@@ -125,13 +125,13 @@ def _get_token_from_file() -> Optional[str]:
|
|
|
125
125
|
return None
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
def get_stored_tokens() ->
|
|
128
|
+
def get_stored_tokens() -> dict[str, str]:
|
|
129
129
|
"""
|
|
130
130
|
Returns the parsed INI file containing the access tokens.
|
|
131
131
|
The file is located at `HF_STORED_TOKENS_PATH`, defaulting to `~/.cache/huggingface/stored_tokens`.
|
|
132
132
|
If the file does not exist, an empty dictionary is returned.
|
|
133
133
|
|
|
134
|
-
Returns: `
|
|
134
|
+
Returns: `dict[str, str]`
|
|
135
135
|
Key is the token name and value is the token.
|
|
136
136
|
"""
|
|
137
137
|
tokens_path = Path(constants.HF_STORED_TOKENS_PATH)
|
|
@@ -147,12 +147,12 @@ def get_stored_tokens() -> Dict[str, str]:
|
|
|
147
147
|
return stored_tokens
|
|
148
148
|
|
|
149
149
|
|
|
150
|
-
def _save_stored_tokens(stored_tokens:
|
|
150
|
+
def _save_stored_tokens(stored_tokens: dict[str, str]) -> None:
|
|
151
151
|
"""
|
|
152
152
|
Saves the given configuration to the stored tokens file.
|
|
153
153
|
|
|
154
154
|
Args:
|
|
155
|
-
stored_tokens (`
|
|
155
|
+
stored_tokens (`dict[str, str]`):
|
|
156
156
|
The stored tokens to save. Key is the token name and value is the token.
|
|
157
157
|
"""
|
|
158
158
|
stored_tokens_path = Path(constants.HF_STORED_TOKENS_PATH)
|