Skip to content

Commit dd37d16

Browse files
author
Sara Adkins
committed
Merge branch 'sparsegpt_quant_child' of github.com:neuralmagic/sparseml into sparsegpt_quant_child
2 parents d651ca0 + ae9cc7c commit dd37d16

File tree

12 files changed

+785
-50
lines changed

12 files changed

+785
-50
lines changed

LICENSE-ULTRALYTICS

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
ULTRALYTICS ENTERPRISE SOFTWARE LICENSE AGREEMENT
2+
v0.6.1 - Updated 21 February 2023
3+
4+
This Enterprise Software License Agreement (the “Agreement”) is made between Neuralmagic, Inc., (the “Client”, or "Licensee") and Ultralytics Inc.
5+
(the “Company”), a Delaware corporation with offices at 3616 Barham BLVD X311, Los Angeles CA 90068 United States, (collectively the “Parties”) as
6+
of March 1st, 2023 (the “Effective Date”).
7+
8+
1. Definitions. As used in this Agreement, the following terms have the following specific meanings:
9+
1. Documentation: the documentation for the Software supplied by Company to assist its 
customers in the use of the Software.
10+
2. Licensee: (a) the company or other legal entity on behalf of which this Agreement is signed, if 
the Agreement is signed on behalf of such an
11+
entity (e.g., by an employee, independent contractor, or other authorized representative), or (b) if there is no such entity, the individual who
12+
signs this Agreement. For clarification, “Licensee” refers only to a single, specifically identified legal entity or individual, and does not
13+
include any subsidiary or affiliate of any such legal entity or individual or any other related person.
14+
3. License Term: the period of time in which Licensee shall be entitled to use the Software and Documentation.
15+
4. Services: the Software Updates and Support and any Consulting Services provided by the Company pursuant to this Agreement.
16+
5. Software: all Ultralytics YOLO source code, trained models, project files and scripts maintained at https://github.com/ultralytics/yolov3,
17+
https://github.com/ultralytics/yolov5 and https://github.com/ultralytics/ultralytics provided by Company to Licensee hereunder. 

18+
19+
2. Right to Use Software. Company grants Licensee a non-exclusive, non-transferable, worldwide license to use the Software, as well as the accompanying
20+
Documentation.
21+
1. Licensee will hold the right to use the Software that Company has developed for any purpose, including commercial and for-profit purposes.
22+
2. Licensee's subcontractors, consultants, and vendors will also hold the right to use the Software for purposes of developing or deploying Licensee's
23+
products or services.
24+
3. Licensee will have the ability to make and distribute to its customers and end users an unlimited number of commercial, for-profit products
25+
containing the Software mentioned above.
26+
4. Licensee, and any third party that receives an authorized distribution under Section 2.3, will retain perpetual license rights to Software versions
27+
and updates released during License Term.
28+
5. Licensee, and any third party that receives an authorized distribution under Section 2.3, shall own all rights, title and interest in and to any
29+
Ultralytics YOLO models that they train with the Software. Company shall have no rights in or to such Ultralytics YOLO models. 

30+
31+
3. Restrictions on Use of Software. Except as expressly permitted in this Agreement, Licensee shall not, and shall not permit any third party to:
32+
1. Sublicense, resell, or otherwise transfer the license or any portion thereof to any third party, including but not limited to any subsidiaries
33+
or the affiliates of Licensee.
34+
2. Alter or remove any notices in the Software or within the Documentation included with said Software. All Software included in this source code
35+
license agreement as well as all Documentation included with said Software is provided in an “as is” condition.
36+
37+
4. Software Updates and Support. Company will provide Licensee access to the Software Updates and Support included during the License Term at no additional fee.
38+
1. Updates. Any relevant Updates during the License Term.
39+
2. Support. Communication tools to enable Licensee to communicate efficiently with the 
Company during the License Term.
40+
3. Consulting. Company may provide Consulting Services to Licensee if requested under a 
separate agreement. Such services are made available at Company's
41+
standard time and material charges. 

42+
43+
5. Payment Fees. In consideration of the license granted by the Company under this Agreement, Licensee agrees to pay Company a basic fee of [CONFIDENTIAL],
44+
plus any applicable taxes, for the Software provided under this Agreement. Payment shall be due within thirty (30) days, after which a late fee of one
45+
and a half percent (1.5%) is applied.
46+
47+
6. Term and Termination. This Agreement will begin on the Effective Date for a period of one (1) year (the “License Term”), and will be automatically renewed
48+
for one (1) year terms at the then- current fees and your credit card account (or other payment method account) will be charged without further authorization
49+
from you, absent sixty (60) day written notice of non-renewal prior to the end of the current License Term.
50+
51+
7. Ownership. Ownership of the Software and Documentation, including any copies or modifications of the Software or Documentation (in whole or in part), and
52+
all related copyright, patent, trade secret and other proprietary rights, are and will remain the exclusive property of Company and/or its licensors. Company
53+
reserves all rights not expressly granted by it to Licensee under this Agreement. There are no implied rights.
54+
55+
8. Confidentiality. Company agrees to protect Licensee's confidential information using no less than reasonable care and to avoid disclosure of any confidential
56+
information. To the extent Company is required by law to disclose Licensee's confidential information, Company make such disclosure, provided Company promptly
57+
notifies Licensee of such requirement prior to disclosure (to the extent permitted by law), and reasonably cooperates, at Company's expense, regarding Company's
58+
efforts to avoid and limit disclosure. Upon the reasonable request of Licensee, Company will either return, delete, or destroy all confidential information of
59+
Licensee and certify the same.
60+
61+
9. Limitation of Liability. Excluding a breach of this Agreement, Either Party shall not be liable to the other Party for any indirect, special or consequential
62+
damages or lost profits arising out of or related to this Agreement. Each Party’s total, aggregate liability to the other Party arising out of or in connection
63+
with this Agreement, whether in contract, tort (including negligence) equity or other legal ground, shall not exceed the fees paid or payable by Licensee.
64+
65+
10. Indemnification and Legal Compliance. Company represents and warrants that, to the best of its knowledge, the Software and Documentation provided to Licensee
66+
hereunder do not infringe any intellectual property rights or other rights of any third party. In the event of any claim, suit, or proceeding against Licensee
67+
arising out of an alleged infringement of any intellectual property rights or other rights of any third party, Company shall, at its sole expense, defend or settle
68+
such claim, suit, or proceeding and indemnify and hold harmless Licensee against any damages and costs awarded therein, provided that Licensee promptly notifies
69+
Company in writing of any such claim, suit, or proceeding and provides reasonable cooperation in the defense thereof. This indemnity shall not apply to any claim
70+
to the extent it arises from modifications made to the Software or Documentation by Licensee or a third party.
71+
72+
Both Parties agree to defend, indemnify, and hold harmless the other party from and against any and all damages, injunctive relief, claims, judgments, liabilities,
73+
fines, costs, expenses, penalties, or losses arising out of any third-party claim or action arising out of any breach by that party of any provision of this
74+
Agreement. This indemnification obligation shall survive the termination or expiration of this Agreement.
75+
76+
11. Assignment. Neither Party may assign this Agreement, or the rights and obligations herein, to any third-party without prior written approval from Company. In the
77+
event of assignment this Agreement is binding on the parties’ respective successors and assigns. Notwithstanding the foregoing, each Party is permitted to assign
78+
without consent or notice obligations to any affiliate, or in the event of the sale or merger of all or substantially all of its assets.
79+
80+
12. Severability. If any term, clause or provision herein is held invalid or unenforceable by a court of competent jurisdiction, such invalidity shall not affect the
81+
validity or operation of any other term, clause or provision.
82+
83+
13. Status. The parties’ status hereunder is that of independent contractors and not an employee or agent of the other. Each Party is an independent business and
84+
responsible for their own costs and expenses, including, those relating to federal, state, and local income taxes, unemployment taxes and workers’ compensation,
85+
liability insurance, and including the filing of all returns and reports and the payment of all assessments, taxes and other sums required of their business.
86+
87+
14. Waiver. The waiver of either Party of any breach or failure to enforce any of the terms and conditions of this Agreement at any time shall not in any way affect,
88+
limit, or waive such Party’s right thereafter to enforce and compel strict compliance with every term and condition of this Agreement.
89+
90+
15. Governing Law. This Agreement shall be governed by and construed in accordance with the substantive laws of the State of Delaware in the United States without
91+
regard to conflict of laws and all disputes arising under or relating to this Agreement shall be brought and resolved solely and exclusively in the State Court
92+
located in Delaware.
93+
94+
16. No Limitation. At any time both Parties may contract without limitation with other entities that provide services similar to those to be provided by Company to
95+
Licensee.
96+
97+
17. Final Agreement. This Agreement terminates and supersedes all prior understandings or agreements on the subject matter hereof. This Agreement may be modified
98+
only by a further writing that is duly executed by both parties.
99+
100+
Any changes to this Agreement will be by mutual agreement.
101+
102+
IN WITNESS whereof, the Parties have caused this Agreement to be executed by their duly authorized representatives as of the Effective Date.
103+
104+
Executed by Ultralytics Inc. and Neuralmagic, Inc.

NOTICE

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ PyTorch License https://github.com/pytorch/pytorch/blob/master/LICENSE
3030

3131
PyTorch torchvision and models License https://github.com/pytorch/vision/blob/master/LICENSE
3232

33-
Pytorch Image Models (timm) License https://github.com/rwightman/pytorch-image-models/blob/master/LICENSE
33+
PyTorch Image Models (timm) License https://github.com/rwightman/pytorch-image-models/blob/master/LICENSE
3434

3535
TensorFlow License https://github.com/tensorflow/tensorflow/blob/master/LICENSE
3636

37-
Ultralytics YOLOv3 License https://github.com/ultralytics/yolov3/blob/master/LICENSE
38-
39-
Ultralytics YOLOv5 License https://github.com/ultralytics/yolov5/blob/master/LICENSE
37+
Ultralytics YOLOv* License https://github.com/neuralmagic/sparseml/blob/master/LICENSE-ULTRALYTICS
4038

4139
YOLACT License https://github.com/dbolya/yolact/blob/master/LICENSE
4240

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SparseML is an open-source model optimization toolkit that enables you to create
6262
## Workflows
6363

6464
SparseML enables you to create a sparse model trained on your dataset in two ways:
65-
- **Sparse Transfer Learning** enables you to fine-tune a pre-sparsified model from [SparseZoo](https://sparsezoo.neuralmagic.com/) (an open-source repository of sparse models such as BERT, YOLOv5, and ResNet-50) onto your dataset, while maintaining sparsity. This pathway works just like typical fine-tuning you are used to in training CV and NLP models, and is strongly preferred for if your model architecture is availble in SparseZoo.
65+
- **Sparse Transfer Learning** enables you to fine-tune a pre-sparsified model from [SparseZoo](https://sparsezoo.neuralmagic.com/) (an open-source repository of sparse models such as BERT, YOLOv5, and ResNet-50) onto your dataset, while maintaining sparsity. This pathway works just like typical fine-tuning you are used to in training CV and NLP models, and is strongly preferred for if your model architecture is available in SparseZoo.
6666

6767
- **Sparsification from Scratch** enables you to apply state-of-the-art pruning (like gradual magnitude pruning or OBS pruning) and quantization (like quantization aware training) algorithms to arbitrary PyTorch and Hugging Face models. This pathway requires more experimentation, but allows you to create a sparse version of any model.
6868

@@ -134,7 +134,7 @@ To enable flexibility, ease of use, and repeatability, SparseML uses a declarati
134134

135135
### Python API
136136

137-
Because of the declarative, recipe-based approach, you can add SparseML to your existing PyTorch traing pipelines. The `ScheduleModifierManager` class is responsible for parsing the YAML `recipes` and overriding standard PyTorch model and optimizer objects, encoding the logic of the sparsity algorithms from the recipe. Once you call `manager.modify`, you can then use the model and optimizer as usual, as SparseML abstracts away the complexity of the sparsification algorithms.
137+
Because of the declarative, recipe-based approach, you can add SparseML to your existing PyTorch training pipelines. The `ScheduleModifierManager` class is responsible for parsing the YAML `recipes` and overriding standard PyTorch model and optimizer objects, encoding the logic of the sparsity algorithms from the recipe. Once you call `manager.modify`, you can then use the model and optimizer as usual, as SparseML abstracts away the complexity of the sparsification algorithms.
138138

139139
The workflow looks like this:
140140

integrations/torchvision/modifiers_refactor_example/e2e_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
from torch.utils.data import DataLoader
2525
from torchvision import transforms
2626

27-
import sparseml.core.session as sml
27+
import sparseml.core.session as session_manager
2828
from sparseml.core.event import EventType
2929
from sparseml.core.framework import Framework
3030
from sparseml.pytorch.utils import (
@@ -40,8 +40,8 @@ def main():
4040
device = "cuda:0"
4141

4242
# set up SparseML session
43-
sml.create_session()
44-
session = sml.active_session()
43+
session_manager.create_session()
44+
session = session_manager.active_session()
4545

4646
# download model
4747
model = torchvision.models.mobilenet_v2(

setup.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,11 @@ def _setup_entry_points() -> Dict:
187187
]
188188
)
189189

190-
entry_points["console_scripts"].append(
191-
"sparseml.transformers.export_onnx=sparseml.transformers.export:main"
190+
entry_points["console_scripts"].extend(
191+
[
192+
"sparseml.transformers.export_onnx=sparseml.transformers.export:main",
193+
"sparseml.transformers.export_onnx_refactor=sparseml.transformers.sparsification.obcq.export:main", # noqa 501
194+
]
192195
)
193196

194197
# image classification integration

src/sparseml/modifiers/obcq/utils/helpers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ def ppl_eval_general(
158158

159159
vocabulary_size = logits[0].shape[-1]
160160
logits = [logit[:, :-1, :].view(-1, vocabulary_size) for logit in logits]
161-
logits = torch.concatenate(logits, dim=0).contiguous().to(torch.float32)
161+
logits = torch.cat(logits, dim=0).contiguous().to(torch.float32)
162162

163163
labels = [sample[:, 1:].view(-1) for sample in samples]
164-
labels = torch.concatenate(labels, dim=0).to(dev)
164+
labels = torch.cat(labels, dim=0).to(dev)
165165
neg_log_likelihood += torch.nn.functional.cross_entropy(
166166
logits,
167167
labels,

src/sparseml/modifiers/quantization/base.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from typing import Any, Dict, List, Optional
1616

17-
from sparseml.core import Event, Modifier, State
17+
from sparseml.core import Event, Modifier
1818

1919

2020
__all__ = ["QuantizationModifier"]
@@ -136,6 +136,3 @@ def check_should_disable_observer(self, event: Event) -> bool:
136136
if event.current_index >= disable_epoch:
137137
return True
138138
return False
139-
140-
def on_initialize_structure(self, state: State, **kwargs):
141-
pass # nothing needed for this modifier

src/sparseml/modifiers/quantization/pytorch.py

+27-20
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def __init__(self, **kwargs):
7171
self.scheme_overrides, self.scheme
7272
)
7373

74+
def on_initialize_structure(self, state: State, **kwargs):
75+
module = state.model.model
76+
self._enable_module_qat(module)
77+
state.model.model.apply(torch.quantization.disable_observer)
78+
7479
def on_initialize(self, state: State, **kwargs) -> bool:
7580
raise_if_torch_quantization_not_available()
7681
if self.end and self.end != -1:
@@ -84,6 +89,7 @@ def on_initialize(self, state: State, **kwargs) -> bool:
8489

8590
if self.calculate_start() == -1: # one-shot
8691
self._enable_module_qat(module)
92+
self._calibrate_if_possible(module)
8793
self._disable_quantization_observer(module)
8894

8995
return True
@@ -122,30 +128,31 @@ def _disable_quantization_observer(self, model: Module):
122128
self.quantization_observer_disabled_ = True
123129

124130
def _enable_module_qat(self, module: Module):
125-
# fuse conv-bn-relu blocks prior to quantization emulation
126-
self._fuse(module)
127-
128-
# add quantization_schemes to target submodules
129-
set_quantization_schemes(
130-
module,
131-
scheme=self.scheme,
132-
scheme_overrides=self.scheme_overrides,
133-
ignore=self.ignore,
134-
strict=self.strict,
135-
)
131+
module.apply(torch.quantization.enable_observer)
136132

137-
# fix for freezing batchnorm statistics when not fusing BN with convs.
138-
# pytorch only supports freezing batchnorm statistics for fused modules.
139-
# this fix wraps BN modules adding with a new module class that supports
140-
# methods related to freezing/unfreezing BN statistics.
141-
configure_module_bn_wrappers(module)
133+
if not self.qat_enabled_:
134+
# fuse conv-bn-relu blocks prior to quantization emulation
135+
self._fuse(module)
136+
137+
# add quantization_schemes to target submodules
138+
set_quantization_schemes(
139+
module,
140+
scheme=self.scheme,
141+
scheme_overrides=self.scheme_overrides,
142+
ignore=self.ignore,
143+
strict=self.strict,
144+
)
142145

143-
# convert target qconfig layers to QAT modules with FakeQuantize
144-
convert_module_qat_from_schemes(module)
146+
# fix for freezing batchnorm statistics when not fusing BN with convs.
147+
# pytorch only supports freezing batchnorm statistics for fused modules.
148+
# this fix wraps BN modules adding with a new module class that supports
149+
# methods related to freezing/unfreezing BN statistics.
150+
configure_module_bn_wrappers(module)
145151

146-
self.qat_enabled_ = True
152+
# convert target qconfig layers to QAT modules with FakeQuantize
153+
convert_module_qat_from_schemes(module)
147154

148-
self._calibrate_if_possible(module)
155+
self.qat_enabled_ = True
149156

150157
def _fuse(self, module: Module):
151158
if self.model_fuse_fn_name in [None, "conv_bn_relus"]:

src/sparseml/pytorch/torchvision/train.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def collate_fn(batch):
400400
)
401401

402402
_LOGGER.info("Creating model")
403-
local_rank = args.local_rank if args.distributed else None
403+
local_rank = int(os.environ["LOCAL_RANK"]) if args.distributed else None
404404
model, arch_key, maybe_dp_device = _create_model(
405405
arch_key=args.arch_key,
406406
local_rank=local_rank,
@@ -1256,14 +1256,6 @@ def new_func(*args, **kwargs):
12561256
"Note: Will use ImageNet values if not specified."
12571257
),
12581258
)
1259-
@click.option(
1260-
"--local_rank",
1261-
"--local-rank",
1262-
type=int,
1263-
default=None,
1264-
help="Local rank for distributed training",
1265-
hidden=True, # should not be modified by user
1266-
)
12671259
@click.pass_context
12681260
def cli(ctx, **kwargs):
12691261
"""

src/sparseml/transformers/data/base_llm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _add_end_token(self, tokenized_sample):
100100
if len(tokenized_sample) == self._seqlen:
101101
tokenized_sample[-1] = self.tokenizer.eos_token_id
102102
else:
103-
tokenized_sample = torch.concatenate(
103+
tokenized_sample = torch.cat(
104104
(
105105
tokenized_sample,
106106
torch.tensor((self.tokenizer.eos_token_id,)),

0 commit comments

Comments
 (0)