cjm-substrate-hf-utils 0.0.12__tar.gz
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.
- cjm_substrate_hf_utils-0.0.12/LICENSE +201 -0
- cjm_substrate_hf_utils-0.0.12/MANIFEST.in +5 -0
- cjm_substrate_hf_utils-0.0.12/PKG-INFO +169 -0
- cjm_substrate_hf_utils-0.0.12/README.md +149 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils/__init__.py +1 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils/_modidx.py +13 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils/cache_config.py +64 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils/download.py +56 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils/loading.py +40 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/PKG-INFO +169 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/SOURCES.txt +15 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/dependency_links.txt +1 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/entry_points.txt +2 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/requires.txt +4 -0
- cjm_substrate_hf_utils-0.0.12/cjm_substrate_hf_utils.egg-info/top_level.txt +1 -0
- cjm_substrate_hf_utils-0.0.12/pyproject.toml +33 -0
- cjm_substrate_hf_utils-0.0.12/setup.cfg +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2022, fastai
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cjm-substrate-hf-utils
|
|
3
|
+
Version: 0.0.12
|
|
4
|
+
Summary: Hugging Face Hub helpers for cjm-substrate capabilities: cache-config mixin, progress-reporting snapshot downloads, and typed-OOM model loading.
|
|
5
|
+
Author-email: "Christian J. Mills" <9126128+cj-mills@users.noreply.github.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/cj-mills/cjm-substrate-hf-utils
|
|
8
|
+
Project-URL: Documentation, https://cj-mills.github.io/cjm-substrate-hf-utils/
|
|
9
|
+
Keywords: nbdev
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Requires-Python: >=3.12
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: cjm-substrate>=0.0.47
|
|
16
|
+
Requires-Dist: cjm-substrate-torch-utils>=0.0.11
|
|
17
|
+
Requires-Dist: huggingface_hub
|
|
18
|
+
Requires-Dist: tqdm
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# cjm-substrate-hf-utils
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
``` bash
|
|
29
|
+
pip install cjm_substrate_hf_utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Project Structure
|
|
33
|
+
|
|
34
|
+
nbs/
|
|
35
|
+
├── cache_config.ipynb # A dataclass mixin adding HuggingFace Hub cache / revision / air-gap / security fields to a capability's config, with `RELOAD_TRIGGER` + JSON-schema metadata pre-set.
|
|
36
|
+
├── download.ipynb # Wrap `huggingface_hub.snapshot_download` so per-file progress flows to the capability's `report_progress`, defeating the substrate's stall detector during downloads.
|
|
37
|
+
└── loading.ipynb # Call `model_class.from_pretrained(...)` and convert CUDA OOM into the substrate's typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
38
|
+
|
|
39
|
+
Total: 3 notebooks
|
|
40
|
+
|
|
41
|
+
## Module Dependencies
|
|
42
|
+
|
|
43
|
+
``` mermaid
|
|
44
|
+
graph LR
|
|
45
|
+
cache_config["cache_config<br/>HF cache config mixin"]
|
|
46
|
+
download["download<br/>Snapshot download with progress"]
|
|
47
|
+
loading["loading<br/>Pretrained loading with typed OOM"]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
No cross-module dependencies detected.
|
|
51
|
+
|
|
52
|
+
## CLI Reference
|
|
53
|
+
|
|
54
|
+
No CLI commands found in this project.
|
|
55
|
+
|
|
56
|
+
## Module Overview
|
|
57
|
+
|
|
58
|
+
Detailed documentation for each module in the project:
|
|
59
|
+
|
|
60
|
+
### HF cache config mixin (`cache_config.ipynb`)
|
|
61
|
+
|
|
62
|
+
> A dataclass mixin adding HuggingFace Hub cache / revision / air-gap /
|
|
63
|
+
> security fields to a capability’s config, with `RELOAD_TRIGGER` +
|
|
64
|
+
> JSON-schema metadata pre-set.
|
|
65
|
+
|
|
66
|
+
#### Import
|
|
67
|
+
|
|
68
|
+
``` python
|
|
69
|
+
from cjm_substrate_hf_utils.cache_config import (
|
|
70
|
+
HFCacheConfig
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### Classes
|
|
75
|
+
|
|
76
|
+
``` python
|
|
77
|
+
@dataclass
|
|
78
|
+
class HFCacheConfig:
|
|
79
|
+
"""
|
|
80
|
+
Mixin adding HuggingFace Hub cache/revision/air-gap/security fields to a capability config.
|
|
81
|
+
|
|
82
|
+
Compose by inheritance:
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class MyCapabilityConfig(HFCacheConfig):
|
|
86
|
+
model_id: str = field(default="org/model", metadata={RELOAD_TRIGGER: "model"})
|
|
87
|
+
# ... capability-specific fields (all defaulted) ...
|
|
88
|
+
|
|
89
|
+
Each field is `RELOAD_TRIGGER`-tagged `"model"` (a change invalidates a loaded
|
|
90
|
+
model) and carries `SCHEMA_TITLE`/`SCHEMA_DESC` so the capability-config UI renders
|
|
91
|
+
it. `trust_remote_code` defaults to False and is flagged DANGER in its help text.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
cache_dir: Optional[str] = field(...)
|
|
95
|
+
revision: Optional[str] = field(...)
|
|
96
|
+
local_files_only: bool = field(...)
|
|
97
|
+
trust_remote_code: bool = field(...)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Snapshot download with progress (`download.ipynb`)
|
|
101
|
+
|
|
102
|
+
> Wrap `huggingface_hub.snapshot_download` so per-file progress flows to
|
|
103
|
+
> the capability’s `report_progress`, defeating the substrate’s stall
|
|
104
|
+
> detector during downloads.
|
|
105
|
+
|
|
106
|
+
#### Import
|
|
107
|
+
|
|
108
|
+
``` python
|
|
109
|
+
from cjm_substrate_hf_utils.download import (
|
|
110
|
+
snapshot_download_with_progress
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Functions
|
|
115
|
+
|
|
116
|
+
``` python
|
|
117
|
+
def snapshot_download_with_progress(
|
|
118
|
+
"""
|
|
119
|
+
Download an HF Hub snapshot, streaming per-file progress to `report_progress`.
|
|
120
|
+
|
|
121
|
+
When `report_progress` is given, a `tqdm_class` subclass forwards each update
|
|
122
|
+
as `report_progress(downloaded / total, "downloading <file>")`. When it is
|
|
123
|
+
None, the default HF Hub progress bars are used unchanged.
|
|
124
|
+
|
|
125
|
+
Returns the local snapshot directory; subsequent `from_pretrained` calls with
|
|
126
|
+
the same `cache_dir` + `local_files_only=True` hit the populated cache.
|
|
127
|
+
|
|
128
|
+
Adoption: the per-file tqdm callback is real progress but NOT a reliable
|
|
129
|
+
stall-detector floor on its own (it can be sparse / silent on one large file).
|
|
130
|
+
Wrap BOTH this call and the subsequent `from_pretrained` in a single
|
|
131
|
+
`with self.heartbeat(...)` block so the substrate always sees the tuple advance.
|
|
132
|
+
"""
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Pretrained loading with typed OOM (`loading.ipynb`)
|
|
136
|
+
|
|
137
|
+
> Call `model_class.from_pretrained(...)` and convert CUDA OOM into the
|
|
138
|
+
> substrate’s typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
139
|
+
|
|
140
|
+
#### Import
|
|
141
|
+
|
|
142
|
+
``` python
|
|
143
|
+
from cjm_substrate_hf_utils.loading import (
|
|
144
|
+
load_pretrained_with_oom
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### Functions
|
|
149
|
+
|
|
150
|
+
``` python
|
|
151
|
+
def load_pretrained_with_oom(
|
|
152
|
+
model_class: Type, # A class exposing `.from_pretrained(repo_id, **kwargs)`
|
|
153
|
+
repo_id: str, # Model repo id or local path passed to from_pretrained
|
|
154
|
+
*,
|
|
155
|
+
label: Optional[str] = None, # OOM-message context (default: f"loading {repo_id!r}")
|
|
156
|
+
**kwargs, # Forwarded verbatim to model_class.from_pretrained
|
|
157
|
+
) -> Any: # The loaded model
|
|
158
|
+
"""
|
|
159
|
+
Call `model_class.from_pretrained(repo_id, **kwargs)`, converting CUDA OOM to a typed error.
|
|
160
|
+
|
|
161
|
+
On `torch.cuda.OutOfMemoryError`, re-raises as `CapabilityResourceError` (via
|
|
162
|
+
`cuda_oom_to_capability_resource_error`) so the substrate's CR-7 reactive-retry
|
|
163
|
+
path can evict + reload + retry. All other exceptions propagate unchanged.
|
|
164
|
+
|
|
165
|
+
Wrap the call in `self.heartbeat(...)` at the capability site to cover the silent
|
|
166
|
+
construction phase; pre-download with `snapshot_download_with_progress` for
|
|
167
|
+
real download progress.
|
|
168
|
+
"""
|
|
169
|
+
```
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# cjm-substrate-hf-utils
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
``` bash
|
|
9
|
+
pip install cjm_substrate_hf_utils
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Project Structure
|
|
13
|
+
|
|
14
|
+
nbs/
|
|
15
|
+
├── cache_config.ipynb # A dataclass mixin adding HuggingFace Hub cache / revision / air-gap / security fields to a capability's config, with `RELOAD_TRIGGER` + JSON-schema metadata pre-set.
|
|
16
|
+
├── download.ipynb # Wrap `huggingface_hub.snapshot_download` so per-file progress flows to the capability's `report_progress`, defeating the substrate's stall detector during downloads.
|
|
17
|
+
└── loading.ipynb # Call `model_class.from_pretrained(...)` and convert CUDA OOM into the substrate's typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
18
|
+
|
|
19
|
+
Total: 3 notebooks
|
|
20
|
+
|
|
21
|
+
## Module Dependencies
|
|
22
|
+
|
|
23
|
+
``` mermaid
|
|
24
|
+
graph LR
|
|
25
|
+
cache_config["cache_config<br/>HF cache config mixin"]
|
|
26
|
+
download["download<br/>Snapshot download with progress"]
|
|
27
|
+
loading["loading<br/>Pretrained loading with typed OOM"]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
No cross-module dependencies detected.
|
|
31
|
+
|
|
32
|
+
## CLI Reference
|
|
33
|
+
|
|
34
|
+
No CLI commands found in this project.
|
|
35
|
+
|
|
36
|
+
## Module Overview
|
|
37
|
+
|
|
38
|
+
Detailed documentation for each module in the project:
|
|
39
|
+
|
|
40
|
+
### HF cache config mixin (`cache_config.ipynb`)
|
|
41
|
+
|
|
42
|
+
> A dataclass mixin adding HuggingFace Hub cache / revision / air-gap /
|
|
43
|
+
> security fields to a capability’s config, with `RELOAD_TRIGGER` +
|
|
44
|
+
> JSON-schema metadata pre-set.
|
|
45
|
+
|
|
46
|
+
#### Import
|
|
47
|
+
|
|
48
|
+
``` python
|
|
49
|
+
from cjm_substrate_hf_utils.cache_config import (
|
|
50
|
+
HFCacheConfig
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Classes
|
|
55
|
+
|
|
56
|
+
``` python
|
|
57
|
+
@dataclass
|
|
58
|
+
class HFCacheConfig:
|
|
59
|
+
"""
|
|
60
|
+
Mixin adding HuggingFace Hub cache/revision/air-gap/security fields to a capability config.
|
|
61
|
+
|
|
62
|
+
Compose by inheritance:
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class MyCapabilityConfig(HFCacheConfig):
|
|
66
|
+
model_id: str = field(default="org/model", metadata={RELOAD_TRIGGER: "model"})
|
|
67
|
+
# ... capability-specific fields (all defaulted) ...
|
|
68
|
+
|
|
69
|
+
Each field is `RELOAD_TRIGGER`-tagged `"model"` (a change invalidates a loaded
|
|
70
|
+
model) and carries `SCHEMA_TITLE`/`SCHEMA_DESC` so the capability-config UI renders
|
|
71
|
+
it. `trust_remote_code` defaults to False and is flagged DANGER in its help text.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
cache_dir: Optional[str] = field(...)
|
|
75
|
+
revision: Optional[str] = field(...)
|
|
76
|
+
local_files_only: bool = field(...)
|
|
77
|
+
trust_remote_code: bool = field(...)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Snapshot download with progress (`download.ipynb`)
|
|
81
|
+
|
|
82
|
+
> Wrap `huggingface_hub.snapshot_download` so per-file progress flows to
|
|
83
|
+
> the capability’s `report_progress`, defeating the substrate’s stall
|
|
84
|
+
> detector during downloads.
|
|
85
|
+
|
|
86
|
+
#### Import
|
|
87
|
+
|
|
88
|
+
``` python
|
|
89
|
+
from cjm_substrate_hf_utils.download import (
|
|
90
|
+
snapshot_download_with_progress
|
|
91
|
+
)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Functions
|
|
95
|
+
|
|
96
|
+
``` python
|
|
97
|
+
def snapshot_download_with_progress(
|
|
98
|
+
"""
|
|
99
|
+
Download an HF Hub snapshot, streaming per-file progress to `report_progress`.
|
|
100
|
+
|
|
101
|
+
When `report_progress` is given, a `tqdm_class` subclass forwards each update
|
|
102
|
+
as `report_progress(downloaded / total, "downloading <file>")`. When it is
|
|
103
|
+
None, the default HF Hub progress bars are used unchanged.
|
|
104
|
+
|
|
105
|
+
Returns the local snapshot directory; subsequent `from_pretrained` calls with
|
|
106
|
+
the same `cache_dir` + `local_files_only=True` hit the populated cache.
|
|
107
|
+
|
|
108
|
+
Adoption: the per-file tqdm callback is real progress but NOT a reliable
|
|
109
|
+
stall-detector floor on its own (it can be sparse / silent on one large file).
|
|
110
|
+
Wrap BOTH this call and the subsequent `from_pretrained` in a single
|
|
111
|
+
`with self.heartbeat(...)` block so the substrate always sees the tuple advance.
|
|
112
|
+
"""
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Pretrained loading with typed OOM (`loading.ipynb`)
|
|
116
|
+
|
|
117
|
+
> Call `model_class.from_pretrained(...)` and convert CUDA OOM into the
|
|
118
|
+
> substrate’s typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
119
|
+
|
|
120
|
+
#### Import
|
|
121
|
+
|
|
122
|
+
``` python
|
|
123
|
+
from cjm_substrate_hf_utils.loading import (
|
|
124
|
+
load_pretrained_with_oom
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Functions
|
|
129
|
+
|
|
130
|
+
``` python
|
|
131
|
+
def load_pretrained_with_oom(
|
|
132
|
+
model_class: Type, # A class exposing `.from_pretrained(repo_id, **kwargs)`
|
|
133
|
+
repo_id: str, # Model repo id or local path passed to from_pretrained
|
|
134
|
+
*,
|
|
135
|
+
label: Optional[str] = None, # OOM-message context (default: f"loading {repo_id!r}")
|
|
136
|
+
**kwargs, # Forwarded verbatim to model_class.from_pretrained
|
|
137
|
+
) -> Any: # The loaded model
|
|
138
|
+
"""
|
|
139
|
+
Call `model_class.from_pretrained(repo_id, **kwargs)`, converting CUDA OOM to a typed error.
|
|
140
|
+
|
|
141
|
+
On `torch.cuda.OutOfMemoryError`, re-raises as `CapabilityResourceError` (via
|
|
142
|
+
`cuda_oom_to_capability_resource_error`) so the substrate's CR-7 reactive-retry
|
|
143
|
+
path can evict + reload + retry. All other exceptions propagate unchanged.
|
|
144
|
+
|
|
145
|
+
Wrap the call in `self.heartbeat(...)` at the capability site to cover the silent
|
|
146
|
+
construction phase; pre-download with `snapshot_download_with_progress` for
|
|
147
|
+
real download progress.
|
|
148
|
+
"""
|
|
149
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.12"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Autogenerated by nbdev
|
|
2
|
+
|
|
3
|
+
d = { 'settings': { 'branch': 'main',
|
|
4
|
+
'doc_baseurl': '/cjm-substrate-hf-utils',
|
|
5
|
+
'doc_host': 'https://cj-mills.github.io',
|
|
6
|
+
'git_url': 'https://github.com/cj-mills/cjm-substrate-hf-utils',
|
|
7
|
+
'lib_path': 'cjm_substrate_hf_utils'},
|
|
8
|
+
'syms': { 'cjm_substrate_hf_utils.cache_config': { 'cjm_substrate_hf_utils.cache_config.HFCacheConfig': ( 'cache_config.html#hfcacheconfig',
|
|
9
|
+
'cjm_substrate_hf_utils/cache_config.py')},
|
|
10
|
+
'cjm_substrate_hf_utils.download': { 'cjm_substrate_hf_utils.download.snapshot_download_with_progress': ( 'download.html#snapshot_download_with_progress',
|
|
11
|
+
'cjm_substrate_hf_utils/download.py')},
|
|
12
|
+
'cjm_substrate_hf_utils.loading': { 'cjm_substrate_hf_utils.loading.load_pretrained_with_oom': ( 'loading.html#load_pretrained_with_oom',
|
|
13
|
+
'cjm_substrate_hf_utils/loading.py')}}}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""A dataclass mixin adding HuggingFace Hub cache / revision / air-gap / security fields to a capability's config, with `RELOAD_TRIGGER` + JSON-schema metadata pre-set.
|
|
2
|
+
|
|
3
|
+
Docs: https://cj-mills.github.io/cjm-substrate-hf-utilscache_config.html.md"""
|
|
4
|
+
|
|
5
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/cache_config.ipynb.
|
|
6
|
+
|
|
7
|
+
# %% auto #0
|
|
8
|
+
__all__ = ['HFCacheConfig']
|
|
9
|
+
|
|
10
|
+
# %% ../nbs/cache_config.ipynb #exports
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Optional
|
|
13
|
+
|
|
14
|
+
from cjm_substrate.core.capability import RELOAD_TRIGGER
|
|
15
|
+
from cjm_substrate.utils.validation import SCHEMA_TITLE, SCHEMA_DESC
|
|
16
|
+
|
|
17
|
+
# %% ../nbs/cache_config.ipynb #hf-cache-config
|
|
18
|
+
@dataclass
|
|
19
|
+
class HFCacheConfig:
|
|
20
|
+
"""Mixin adding HuggingFace Hub cache/revision/air-gap/security fields to a capability config.
|
|
21
|
+
|
|
22
|
+
Compose by inheritance:
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class MyCapabilityConfig(HFCacheConfig):
|
|
26
|
+
model_id: str = field(default="org/model", metadata={RELOAD_TRIGGER: "model"})
|
|
27
|
+
# ... capability-specific fields (all defaulted) ...
|
|
28
|
+
|
|
29
|
+
Each field is `RELOAD_TRIGGER`-tagged `"model"` (a change invalidates a loaded
|
|
30
|
+
model) and carries `SCHEMA_TITLE`/`SCHEMA_DESC` so the capability-config UI renders
|
|
31
|
+
it. `trust_remote_code` defaults to False and is flagged DANGER in its help text.
|
|
32
|
+
"""
|
|
33
|
+
cache_dir: Optional[str] = field(
|
|
34
|
+
default=None,
|
|
35
|
+
metadata={
|
|
36
|
+
RELOAD_TRIGGER: "model",
|
|
37
|
+
SCHEMA_TITLE: "HF Cache Directory",
|
|
38
|
+
SCHEMA_DESC: "Override the HF Hub cache location for model downloads (default: HF_HOME).",
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
revision: Optional[str] = field(
|
|
42
|
+
default=None,
|
|
43
|
+
metadata={
|
|
44
|
+
RELOAD_TRIGGER: "model",
|
|
45
|
+
SCHEMA_TITLE: "Model Revision",
|
|
46
|
+
SCHEMA_DESC: "Git revision / tag / commit to pin (default: the repo's main branch).",
|
|
47
|
+
},
|
|
48
|
+
)
|
|
49
|
+
local_files_only: bool = field(
|
|
50
|
+
default=False,
|
|
51
|
+
metadata={
|
|
52
|
+
RELOAD_TRIGGER: "model",
|
|
53
|
+
SCHEMA_TITLE: "Local Files Only",
|
|
54
|
+
SCHEMA_DESC: "Reject network access and use only cached files (air-gapped / reproducible).",
|
|
55
|
+
},
|
|
56
|
+
)
|
|
57
|
+
trust_remote_code: bool = field(
|
|
58
|
+
default=False,
|
|
59
|
+
metadata={
|
|
60
|
+
RELOAD_TRIGGER: "model",
|
|
61
|
+
SCHEMA_TITLE: "Trust Remote Code",
|
|
62
|
+
SCHEMA_DESC: "DANGER: allow execution of model-author code shipped in the repo.",
|
|
63
|
+
},
|
|
64
|
+
)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Wrap `huggingface_hub.snapshot_download` so per-file progress flows to the capability's `report_progress`, defeating the substrate's stall detector during downloads.
|
|
2
|
+
|
|
3
|
+
Docs: https://cj-mills.github.io/cjm-substrate-hf-utilsdownload.html.md"""
|
|
4
|
+
|
|
5
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/download.ipynb.
|
|
6
|
+
|
|
7
|
+
# %% auto #0
|
|
8
|
+
__all__ = ['snapshot_download_with_progress']
|
|
9
|
+
|
|
10
|
+
# %% ../nbs/download.ipynb #exports
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Callable, Optional
|
|
13
|
+
|
|
14
|
+
from huggingface_hub import snapshot_download
|
|
15
|
+
from tqdm.auto import tqdm as _tqdm
|
|
16
|
+
|
|
17
|
+
# %% ../nbs/download.ipynb #snapshot-download
|
|
18
|
+
def snapshot_download_with_progress(
|
|
19
|
+
repo_id: str, # HF Hub repo id, e.g. "mistralai/Voxtral-Mini-3B-2507"
|
|
20
|
+
*,
|
|
21
|
+
report_progress: Optional[Callable[[float, str], None]] = None, # Capability's report_progress(fraction, message)
|
|
22
|
+
cache_dir: Optional[str] = None, # HF cache override (HFCacheConfig.cache_dir)
|
|
23
|
+
revision: Optional[str] = None, # Git rev / tag / commit to pin
|
|
24
|
+
local_files_only: bool = False, # Use cached files only; no network
|
|
25
|
+
**kwargs, # Forwarded to huggingface_hub.snapshot_download
|
|
26
|
+
) -> Path: # Local path to the downloaded snapshot
|
|
27
|
+
"""Download an HF Hub snapshot, streaming per-file progress to `report_progress`.
|
|
28
|
+
|
|
29
|
+
When `report_progress` is given, a `tqdm_class` subclass forwards each update
|
|
30
|
+
as `report_progress(downloaded / total, "downloading <file>")`. When it is
|
|
31
|
+
None, the default HF Hub progress bars are used unchanged.
|
|
32
|
+
|
|
33
|
+
Returns the local snapshot directory; subsequent `from_pretrained` calls with
|
|
34
|
+
the same `cache_dir` + `local_files_only=True` hit the populated cache.
|
|
35
|
+
|
|
36
|
+
Adoption: the per-file tqdm callback is real progress but NOT a reliable
|
|
37
|
+
stall-detector floor on its own (it can be sparse / silent on one large file).
|
|
38
|
+
Wrap BOTH this call and the subsequent `from_pretrained` in a single
|
|
39
|
+
`with self.heartbeat(...)` block so the substrate always sees the tuple advance.
|
|
40
|
+
"""
|
|
41
|
+
tqdm_class = None
|
|
42
|
+
if report_progress is not None:
|
|
43
|
+
class _ReportingTqdm(_tqdm):
|
|
44
|
+
def update(self, n=1):
|
|
45
|
+
super().update(n)
|
|
46
|
+
if self.total:
|
|
47
|
+
report_progress(self.n / self.total, f"downloading {self.desc or repo_id}")
|
|
48
|
+
tqdm_class = _ReportingTqdm
|
|
49
|
+
return Path(snapshot_download(
|
|
50
|
+
repo_id,
|
|
51
|
+
cache_dir=cache_dir,
|
|
52
|
+
revision=revision,
|
|
53
|
+
local_files_only=local_files_only,
|
|
54
|
+
tqdm_class=tqdm_class,
|
|
55
|
+
**kwargs,
|
|
56
|
+
))
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Call `model_class.from_pretrained(...)` and convert CUDA OOM into the substrate's typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
2
|
+
|
|
3
|
+
Docs: https://cj-mills.github.io/cjm-substrate-hf-utilsloading.html.md"""
|
|
4
|
+
|
|
5
|
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/loading.ipynb.
|
|
6
|
+
|
|
7
|
+
# %% auto #0
|
|
8
|
+
__all__ = ['load_pretrained_with_oom']
|
|
9
|
+
|
|
10
|
+
# %% ../nbs/loading.ipynb #exports
|
|
11
|
+
from typing import Any, Optional, Type
|
|
12
|
+
|
|
13
|
+
import torch
|
|
14
|
+
|
|
15
|
+
from cjm_substrate_torch_utils.oom import cuda_oom_to_capability_resource_error
|
|
16
|
+
|
|
17
|
+
# %% ../nbs/loading.ipynb #load-pretrained
|
|
18
|
+
def load_pretrained_with_oom(
|
|
19
|
+
model_class: Type, # A class exposing `.from_pretrained(repo_id, **kwargs)`
|
|
20
|
+
repo_id: str, # Model repo id or local path passed to from_pretrained
|
|
21
|
+
*,
|
|
22
|
+
label: Optional[str] = None, # OOM-message context (default: f"loading {repo_id!r}")
|
|
23
|
+
**kwargs, # Forwarded verbatim to model_class.from_pretrained
|
|
24
|
+
) -> Any: # The loaded model
|
|
25
|
+
"""Call `model_class.from_pretrained(repo_id, **kwargs)`, converting CUDA OOM to a typed error.
|
|
26
|
+
|
|
27
|
+
On `torch.cuda.OutOfMemoryError`, re-raises as `CapabilityResourceError` (via
|
|
28
|
+
`cuda_oom_to_capability_resource_error`) so the substrate's CR-7 reactive-retry
|
|
29
|
+
path can evict + reload + retry. All other exceptions propagate unchanged.
|
|
30
|
+
|
|
31
|
+
Wrap the call in `self.heartbeat(...)` at the capability site to cover the silent
|
|
32
|
+
construction phase; pre-download with `snapshot_download_with_progress` for
|
|
33
|
+
real download progress.
|
|
34
|
+
"""
|
|
35
|
+
try:
|
|
36
|
+
return model_class.from_pretrained(repo_id, **kwargs)
|
|
37
|
+
except torch.cuda.OutOfMemoryError as e:
|
|
38
|
+
raise cuda_oom_to_capability_resource_error(
|
|
39
|
+
e, label=label or f"loading {repo_id!r}",
|
|
40
|
+
) from e
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cjm-substrate-hf-utils
|
|
3
|
+
Version: 0.0.12
|
|
4
|
+
Summary: Hugging Face Hub helpers for cjm-substrate capabilities: cache-config mixin, progress-reporting snapshot downloads, and typed-OOM model loading.
|
|
5
|
+
Author-email: "Christian J. Mills" <9126128+cj-mills@users.noreply.github.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/cj-mills/cjm-substrate-hf-utils
|
|
8
|
+
Project-URL: Documentation, https://cj-mills.github.io/cjm-substrate-hf-utils/
|
|
9
|
+
Keywords: nbdev
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Requires-Python: >=3.12
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: cjm-substrate>=0.0.47
|
|
16
|
+
Requires-Dist: cjm-substrate-torch-utils>=0.0.11
|
|
17
|
+
Requires-Dist: huggingface_hub
|
|
18
|
+
Requires-Dist: tqdm
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# cjm-substrate-hf-utils
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
``` bash
|
|
29
|
+
pip install cjm_substrate_hf_utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Project Structure
|
|
33
|
+
|
|
34
|
+
nbs/
|
|
35
|
+
├── cache_config.ipynb # A dataclass mixin adding HuggingFace Hub cache / revision / air-gap / security fields to a capability's config, with `RELOAD_TRIGGER` + JSON-schema metadata pre-set.
|
|
36
|
+
├── download.ipynb # Wrap `huggingface_hub.snapshot_download` so per-file progress flows to the capability's `report_progress`, defeating the substrate's stall detector during downloads.
|
|
37
|
+
└── loading.ipynb # Call `model_class.from_pretrained(...)` and convert CUDA OOM into the substrate's typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
38
|
+
|
|
39
|
+
Total: 3 notebooks
|
|
40
|
+
|
|
41
|
+
## Module Dependencies
|
|
42
|
+
|
|
43
|
+
``` mermaid
|
|
44
|
+
graph LR
|
|
45
|
+
cache_config["cache_config<br/>HF cache config mixin"]
|
|
46
|
+
download["download<br/>Snapshot download with progress"]
|
|
47
|
+
loading["loading<br/>Pretrained loading with typed OOM"]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
No cross-module dependencies detected.
|
|
51
|
+
|
|
52
|
+
## CLI Reference
|
|
53
|
+
|
|
54
|
+
No CLI commands found in this project.
|
|
55
|
+
|
|
56
|
+
## Module Overview
|
|
57
|
+
|
|
58
|
+
Detailed documentation for each module in the project:
|
|
59
|
+
|
|
60
|
+
### HF cache config mixin (`cache_config.ipynb`)
|
|
61
|
+
|
|
62
|
+
> A dataclass mixin adding HuggingFace Hub cache / revision / air-gap /
|
|
63
|
+
> security fields to a capability’s config, with `RELOAD_TRIGGER` +
|
|
64
|
+
> JSON-schema metadata pre-set.
|
|
65
|
+
|
|
66
|
+
#### Import
|
|
67
|
+
|
|
68
|
+
``` python
|
|
69
|
+
from cjm_substrate_hf_utils.cache_config import (
|
|
70
|
+
HFCacheConfig
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### Classes
|
|
75
|
+
|
|
76
|
+
``` python
|
|
77
|
+
@dataclass
|
|
78
|
+
class HFCacheConfig:
|
|
79
|
+
"""
|
|
80
|
+
Mixin adding HuggingFace Hub cache/revision/air-gap/security fields to a capability config.
|
|
81
|
+
|
|
82
|
+
Compose by inheritance:
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class MyCapabilityConfig(HFCacheConfig):
|
|
86
|
+
model_id: str = field(default="org/model", metadata={RELOAD_TRIGGER: "model"})
|
|
87
|
+
# ... capability-specific fields (all defaulted) ...
|
|
88
|
+
|
|
89
|
+
Each field is `RELOAD_TRIGGER`-tagged `"model"` (a change invalidates a loaded
|
|
90
|
+
model) and carries `SCHEMA_TITLE`/`SCHEMA_DESC` so the capability-config UI renders
|
|
91
|
+
it. `trust_remote_code` defaults to False and is flagged DANGER in its help text.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
cache_dir: Optional[str] = field(...)
|
|
95
|
+
revision: Optional[str] = field(...)
|
|
96
|
+
local_files_only: bool = field(...)
|
|
97
|
+
trust_remote_code: bool = field(...)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Snapshot download with progress (`download.ipynb`)
|
|
101
|
+
|
|
102
|
+
> Wrap `huggingface_hub.snapshot_download` so per-file progress flows to
|
|
103
|
+
> the capability’s `report_progress`, defeating the substrate’s stall
|
|
104
|
+
> detector during downloads.
|
|
105
|
+
|
|
106
|
+
#### Import
|
|
107
|
+
|
|
108
|
+
``` python
|
|
109
|
+
from cjm_substrate_hf_utils.download import (
|
|
110
|
+
snapshot_download_with_progress
|
|
111
|
+
)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Functions
|
|
115
|
+
|
|
116
|
+
``` python
|
|
117
|
+
def snapshot_download_with_progress(
|
|
118
|
+
"""
|
|
119
|
+
Download an HF Hub snapshot, streaming per-file progress to `report_progress`.
|
|
120
|
+
|
|
121
|
+
When `report_progress` is given, a `tqdm_class` subclass forwards each update
|
|
122
|
+
as `report_progress(downloaded / total, "downloading <file>")`. When it is
|
|
123
|
+
None, the default HF Hub progress bars are used unchanged.
|
|
124
|
+
|
|
125
|
+
Returns the local snapshot directory; subsequent `from_pretrained` calls with
|
|
126
|
+
the same `cache_dir` + `local_files_only=True` hit the populated cache.
|
|
127
|
+
|
|
128
|
+
Adoption: the per-file tqdm callback is real progress but NOT a reliable
|
|
129
|
+
stall-detector floor on its own (it can be sparse / silent on one large file).
|
|
130
|
+
Wrap BOTH this call and the subsequent `from_pretrained` in a single
|
|
131
|
+
`with self.heartbeat(...)` block so the substrate always sees the tuple advance.
|
|
132
|
+
"""
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Pretrained loading with typed OOM (`loading.ipynb`)
|
|
136
|
+
|
|
137
|
+
> Call `model_class.from_pretrained(...)` and convert CUDA OOM into the
|
|
138
|
+
> substrate’s typed `CapabilityResourceError` for CR-7 reactive retry.
|
|
139
|
+
|
|
140
|
+
#### Import
|
|
141
|
+
|
|
142
|
+
``` python
|
|
143
|
+
from cjm_substrate_hf_utils.loading import (
|
|
144
|
+
load_pretrained_with_oom
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### Functions
|
|
149
|
+
|
|
150
|
+
``` python
|
|
151
|
+
def load_pretrained_with_oom(
|
|
152
|
+
model_class: Type, # A class exposing `.from_pretrained(repo_id, **kwargs)`
|
|
153
|
+
repo_id: str, # Model repo id or local path passed to from_pretrained
|
|
154
|
+
*,
|
|
155
|
+
label: Optional[str] = None, # OOM-message context (default: f"loading {repo_id!r}")
|
|
156
|
+
**kwargs, # Forwarded verbatim to model_class.from_pretrained
|
|
157
|
+
) -> Any: # The loaded model
|
|
158
|
+
"""
|
|
159
|
+
Call `model_class.from_pretrained(repo_id, **kwargs)`, converting CUDA OOM to a typed error.
|
|
160
|
+
|
|
161
|
+
On `torch.cuda.OutOfMemoryError`, re-raises as `CapabilityResourceError` (via
|
|
162
|
+
`cuda_oom_to_capability_resource_error`) so the substrate's CR-7 reactive-retry
|
|
163
|
+
path can evict + reload + retry. All other exceptions propagate unchanged.
|
|
164
|
+
|
|
165
|
+
Wrap the call in `self.heartbeat(...)` at the capability site to cover the silent
|
|
166
|
+
construction phase; pre-download with `snapshot_download_with_progress` for
|
|
167
|
+
real download progress.
|
|
168
|
+
"""
|
|
169
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
cjm_substrate_hf_utils/__init__.py
|
|
6
|
+
cjm_substrate_hf_utils/_modidx.py
|
|
7
|
+
cjm_substrate_hf_utils/cache_config.py
|
|
8
|
+
cjm_substrate_hf_utils/download.py
|
|
9
|
+
cjm_substrate_hf_utils/loading.py
|
|
10
|
+
cjm_substrate_hf_utils.egg-info/PKG-INFO
|
|
11
|
+
cjm_substrate_hf_utils.egg-info/SOURCES.txt
|
|
12
|
+
cjm_substrate_hf_utils.egg-info/dependency_links.txt
|
|
13
|
+
cjm_substrate_hf_utils.egg-info/entry_points.txt
|
|
14
|
+
cjm_substrate_hf_utils.egg-info/requires.txt
|
|
15
|
+
cjm_substrate_hf_utils.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cjm_substrate_hf_utils
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cjm-substrate-hf-utils"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Hugging Face Hub helpers for cjm-substrate capabilities: cache-config mixin, progress-reporting snapshot downloads, and typed-OOM model loading."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = {text = "Apache-2.0"}
|
|
12
|
+
authors = [{name = "Christian J. Mills", email = "9126128+cj-mills@users.noreply.github.com"}]
|
|
13
|
+
keywords = ['nbdev']
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
17
|
+
]
|
|
18
|
+
dependencies = ['cjm-substrate>=0.0.47', 'cjm-substrate-torch-utils>=0.0.11', 'huggingface_hub', 'tqdm']
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Repository = "https://github.com/cj-mills/cjm-substrate-hf-utils"
|
|
22
|
+
Documentation = "https://cj-mills.github.io/cjm-substrate-hf-utils/"
|
|
23
|
+
|
|
24
|
+
[project.entry-points.nbdev]
|
|
25
|
+
cjm_substrate_hf_utils = "cjm_substrate_hf_utils._modidx:d"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.dynamic]
|
|
28
|
+
version = {attr = "cjm_substrate_hf_utils.__version__"}
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["cjm_substrate_hf_utils"]
|
|
32
|
+
|
|
33
|
+
[tool.nbdev]
|