27 | {%- endif %}
--------------------------------------------------------------------------------
/docs/api/modules.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify
17 | ========
18 |
19 | .. toctree::
20 | :maxdepth: 4
21 |
22 | sparsify
23 |
--------------------------------------------------------------------------------
/docs/api/sparsify.blueprints.code_samples.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.blueprints.code\_samples package
17 | =========================================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.blueprints.code\_samples.pytorch\_\_integration module
23 | ---------------------------------------------------------------
24 |
25 | .. automodule:: sparsify.blueprints.code_samples.pytorch__integration
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | sparsify.blueprints.code\_samples.pytorch\_\_training module
31 | ------------------------------------------------------------
32 |
33 | .. automodule:: sparsify.blueprints.code_samples.pytorch__training
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
38 | sparsify.blueprints.code\_samples.tensorflow\_\_integration module
39 | ------------------------------------------------------------------
40 |
41 | .. automodule:: sparsify.blueprints.code_samples.tensorflow__integration
42 | :members:
43 | :undoc-members:
44 | :show-inheritance:
45 |
46 | Module contents
47 | ---------------
48 |
49 | .. automodule:: sparsify.blueprints.code_samples
50 | :members:
51 | :undoc-members:
52 | :show-inheritance:
53 |
--------------------------------------------------------------------------------
/docs/api/sparsify.blueprints.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.blueprints package
17 | ===========================
18 |
19 | Subpackages
20 | -----------
21 |
22 | .. toctree::
23 | :maxdepth: 4
24 |
25 | sparsify.blueprints.code_samples
26 | sparsify.blueprints.utils
27 |
28 | Submodules
29 | ----------
30 |
31 | sparsify.blueprints.errors module
32 | ---------------------------------
33 |
34 | .. automodule:: sparsify.blueprints.errors
35 | :members:
36 | :undoc-members:
37 | :show-inheritance:
38 |
39 | sparsify.blueprints.jobs module
40 | -------------------------------
41 |
42 | .. automodule:: sparsify.blueprints.jobs
43 | :members:
44 | :undoc-members:
45 | :show-inheritance:
46 |
47 | sparsify.blueprints.model\_repo module
48 | --------------------------------------
49 |
50 | .. automodule:: sparsify.blueprints.model_repo
51 | :members:
52 | :undoc-members:
53 | :show-inheritance:
54 |
55 | sparsify.blueprints.projects module
56 | -----------------------------------
57 |
58 | .. automodule:: sparsify.blueprints.projects
59 | :members:
60 | :undoc-members:
61 | :show-inheritance:
62 |
63 | sparsify.blueprints.projects\_benchmarks module
64 | -----------------------------------------------
65 |
66 | .. automodule:: sparsify.blueprints.projects_benchmarks
67 | :members:
68 | :undoc-members:
69 | :show-inheritance:
70 |
71 | sparsify.blueprints.projects\_data module
72 | -----------------------------------------
73 |
74 | .. automodule:: sparsify.blueprints.projects_data
75 | :members:
76 | :undoc-members:
77 | :show-inheritance:
78 |
79 | sparsify.blueprints.projects\_model module
80 | ------------------------------------------
81 |
82 | .. automodule:: sparsify.blueprints.projects_model
83 | :members:
84 | :undoc-members:
85 | :show-inheritance:
86 |
87 | sparsify.blueprints.projects\_optimizations module
88 | --------------------------------------------------
89 |
90 | .. automodule:: sparsify.blueprints.projects_optimizations
91 | :members:
92 | :undoc-members:
93 | :show-inheritance:
94 |
95 | sparsify.blueprints.projects\_profiles module
96 | ---------------------------------------------
97 |
98 | .. automodule:: sparsify.blueprints.projects_profiles
99 | :members:
100 | :undoc-members:
101 | :show-inheritance:
102 |
103 | sparsify.blueprints.system module
104 | ---------------------------------
105 |
106 | .. automodule:: sparsify.blueprints.system
107 | :members:
108 | :undoc-members:
109 | :show-inheritance:
110 |
111 | sparsify.blueprints.ui module
112 | -----------------------------
113 |
114 | .. automodule:: sparsify.blueprints.ui
115 | :members:
116 | :undoc-members:
117 | :show-inheritance:
118 |
119 | Module contents
120 | ---------------
121 |
122 | .. automodule:: sparsify.blueprints
123 | :members:
124 | :undoc-members:
125 | :show-inheritance:
126 |
--------------------------------------------------------------------------------
/docs/api/sparsify.blueprints.utils.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.blueprints.utils package
17 | =================================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.blueprints.utils.helpers module
23 | ----------------------------------------
24 |
25 | .. automodule:: sparsify.blueprints.utils.helpers
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | sparsify.blueprints.utils.projects module
31 | -----------------------------------------
32 |
33 | .. automodule:: sparsify.blueprints.utils.projects
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
38 | sparsify.blueprints.utils.projects\_benchmark module
39 | ----------------------------------------------------
40 |
41 | .. automodule:: sparsify.blueprints.utils.projects_benchmark
42 | :members:
43 | :undoc-members:
44 | :show-inheritance:
45 |
46 | sparsify.blueprints.utils.projects\_data module
47 | -----------------------------------------------
48 |
49 | .. automodule:: sparsify.blueprints.utils.projects_data
50 | :members:
51 | :undoc-members:
52 | :show-inheritance:
53 |
54 | sparsify.blueprints.utils.projects\_optimizations module
55 | --------------------------------------------------------
56 |
57 | .. automodule:: sparsify.blueprints.utils.projects_optimizations
58 | :members:
59 | :undoc-members:
60 | :show-inheritance:
61 |
62 | sparsify.blueprints.utils.projects\_optimizations\_pruning module
63 | -----------------------------------------------------------------
64 |
65 | .. automodule:: sparsify.blueprints.utils.projects_optimizations_pruning
66 | :members:
67 | :undoc-members:
68 | :show-inheritance:
69 |
70 | Module contents
71 | ---------------
72 |
73 | .. automodule:: sparsify.blueprints.utils
74 | :members:
75 | :undoc-members:
76 | :show-inheritance:
77 |
--------------------------------------------------------------------------------
/docs/api/sparsify.models.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.models package
17 | =======================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.models.base module
23 | ---------------------------
24 |
25 | .. automodule:: sparsify.models.base
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | sparsify.models.jobs module
31 | ---------------------------
32 |
33 | .. automodule:: sparsify.models.jobs
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
38 | sparsify.models.projects module
39 | -------------------------------
40 |
41 | .. automodule:: sparsify.models.projects
42 | :members:
43 | :undoc-members:
44 | :show-inheritance:
45 |
46 | sparsify.models.projects\_benchmark module
47 | ------------------------------------------
48 |
49 | .. automodule:: sparsify.models.projects_benchmark
50 | :members:
51 | :undoc-members:
52 | :show-inheritance:
53 |
54 | sparsify.models.projects\_data module
55 | -------------------------------------
56 |
57 | .. automodule:: sparsify.models.projects_data
58 | :members:
59 | :undoc-members:
60 | :show-inheritance:
61 |
62 | sparsify.models.projects\_model module
63 | --------------------------------------
64 |
65 | .. automodule:: sparsify.models.projects_model
66 | :members:
67 | :undoc-members:
68 | :show-inheritance:
69 |
70 | sparsify.models.projects\_optimizations module
71 | ----------------------------------------------
72 |
73 | .. automodule:: sparsify.models.projects_optimizations
74 | :members:
75 | :undoc-members:
76 | :show-inheritance:
77 |
78 | sparsify.models.projects\_profiles module
79 | -----------------------------------------
80 |
81 | .. automodule:: sparsify.models.projects_profiles
82 | :members:
83 | :undoc-members:
84 | :show-inheritance:
85 |
86 | sparsify.models.utils module
87 | ----------------------------
88 |
89 | .. automodule:: sparsify.models.utils
90 | :members:
91 | :undoc-members:
92 | :show-inheritance:
93 |
94 | Module contents
95 | ---------------
96 |
97 | .. automodule:: sparsify.models
98 | :members:
99 | :undoc-members:
100 | :show-inheritance:
101 |
--------------------------------------------------------------------------------
/docs/api/sparsify.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify package
17 | ================
18 |
19 | Subpackages
20 | -----------
21 |
22 | .. toctree::
23 | :maxdepth: 4
24 |
25 | sparsify.blueprints
26 | sparsify.models
27 | sparsify.schemas
28 | sparsify.utils
29 | sparsify.workers
30 |
31 | Submodules
32 | ----------
33 |
34 | sparsify.app module
35 | -------------------
36 |
37 | .. automodule:: sparsify.app
38 | :members:
39 | :undoc-members:
40 | :show-inheritance:
41 |
42 | sparsify.log module
43 | -------------------
44 |
45 | .. automodule:: sparsify.log
46 | :members:
47 | :undoc-members:
48 | :show-inheritance:
49 |
50 | sparsify.version module
51 | -----------------------
52 |
53 | .. automodule:: sparsify.version
54 | :members:
55 | :undoc-members:
56 | :show-inheritance:
57 |
58 | Module contents
59 | ---------------
60 |
61 | .. automodule:: sparsify
62 | :members:
63 | :undoc-members:
64 | :show-inheritance:
65 |
--------------------------------------------------------------------------------
/docs/api/sparsify.schemas.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.schemas package
17 | ========================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.schemas.errors module
23 | ------------------------------
24 |
25 | .. automodule:: sparsify.schemas.errors
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | sparsify.schemas.helpers module
31 | -------------------------------
32 |
33 | .. automodule:: sparsify.schemas.helpers
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
38 | sparsify.schemas.jobs module
39 | ----------------------------
40 |
41 | .. automodule:: sparsify.schemas.jobs
42 | :members:
43 | :undoc-members:
44 | :show-inheritance:
45 |
46 | sparsify.schemas.model\_repo module
47 | -----------------------------------
48 |
49 | .. automodule:: sparsify.schemas.model_repo
50 | :members:
51 | :undoc-members:
52 | :show-inheritance:
53 |
54 | sparsify.schemas.projects module
55 | --------------------------------
56 |
57 | .. automodule:: sparsify.schemas.projects
58 | :members:
59 | :undoc-members:
60 | :show-inheritance:
61 |
62 | sparsify.schemas.projects\_benchmarks module
63 | --------------------------------------------
64 |
65 | .. automodule:: sparsify.schemas.projects_benchmarks
66 | :members:
67 | :undoc-members:
68 | :show-inheritance:
69 |
70 | sparsify.schemas.projects\_data module
71 | --------------------------------------
72 |
73 | .. automodule:: sparsify.schemas.projects_data
74 | :members:
75 | :undoc-members:
76 | :show-inheritance:
77 |
78 | sparsify.schemas.projects\_model module
79 | ---------------------------------------
80 |
81 | .. automodule:: sparsify.schemas.projects_model
82 | :members:
83 | :undoc-members:
84 | :show-inheritance:
85 |
86 | sparsify.schemas.projects\_optimizations module
87 | -----------------------------------------------
88 |
89 | .. automodule:: sparsify.schemas.projects_optimizations
90 | :members:
91 | :undoc-members:
92 | :show-inheritance:
93 |
94 | sparsify.schemas.projects\_profiles module
95 | ------------------------------------------
96 |
97 | .. automodule:: sparsify.schemas.projects_profiles
98 | :members:
99 | :undoc-members:
100 | :show-inheritance:
101 |
102 | sparsify.schemas.system module
103 | ------------------------------
104 |
105 | .. automodule:: sparsify.schemas.system
106 | :members:
107 | :undoc-members:
108 | :show-inheritance:
109 |
110 | Module contents
111 | ---------------
112 |
113 | .. automodule:: sparsify.schemas
114 | :members:
115 | :undoc-members:
116 | :show-inheritance:
117 |
--------------------------------------------------------------------------------
/docs/api/sparsify.utils.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.utils package
17 | ======================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.utils.system module
23 | ----------------------------
24 |
25 | .. automodule:: sparsify.utils.system
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | Module contents
31 | ---------------
32 |
33 | .. automodule:: sparsify.utils
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
--------------------------------------------------------------------------------
/docs/api/sparsify.workers.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | sparsify.workers package
17 | ========================
18 |
19 | Submodules
20 | ----------
21 |
22 | sparsify.workers.base module
23 | ----------------------------
24 |
25 | .. automodule:: sparsify.workers.base
26 | :members:
27 | :undoc-members:
28 | :show-inheritance:
29 |
30 | sparsify.workers.manager module
31 | -------------------------------
32 |
33 | .. automodule:: sparsify.workers.manager
34 | :members:
35 | :undoc-members:
36 | :show-inheritance:
37 |
38 | sparsify.workers.projects\_benchmark module
39 | -------------------------------------------
40 |
41 | .. automodule:: sparsify.workers.projects_benchmark
42 | :members:
43 | :undoc-members:
44 | :show-inheritance:
45 |
46 | sparsify.workers.projects\_data module
47 | --------------------------------------
48 |
49 | .. automodule:: sparsify.workers.projects_data
50 | :members:
51 | :undoc-members:
52 | :show-inheritance:
53 |
54 | sparsify.workers.projects\_model module
55 | ---------------------------------------
56 |
57 | .. automodule:: sparsify.workers.projects_model
58 | :members:
59 | :undoc-members:
60 | :show-inheritance:
61 |
62 | sparsify.workers.projects\_profiles module
63 | ------------------------------------------
64 |
65 | .. automodule:: sparsify.workers.projects_profiles
66 | :members:
67 | :undoc-members:
68 | :show-inheritance:
69 |
70 | Module contents
71 | ---------------
72 |
73 | .. automodule:: sparsify.workers
74 | :members:
75 | :undoc-members:
76 | :show-inheritance:
77 |
--------------------------------------------------------------------------------
/docs/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/favicon.ico
--------------------------------------------------------------------------------
/docs/index.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | .. mdinclude:: ../README.md
17 |
18 | .. toctree::
19 | :maxdepth: 3
20 | :caption: General
21 |
22 | source/userguide/index
23 |
24 | .. toctree::
25 | :maxdepth: 2
26 | :caption: API
27 |
28 | api/sparsify
29 |
30 | .. toctree::
31 | :maxdepth: 3
32 | :caption: Connect Online
33 |
34 | Bugs, Feature Requests
35 | Deep Sparse Community Slack
36 | Neural Magic GitHub
37 | Neural Magic Docs
38 |
--------------------------------------------------------------------------------
/docs/models-guide.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Sparsify Models Guide
18 |
19 | For most Sparsify Experiments, you will need to provide a base model to create a sparse model from.
20 | Due to the varied ML pipelines and implementations, Sparsify standardizes on a few popular formats for models.
21 | You will need to make sure that your model is formatted properly according to the standards listed below.
22 |
23 | ## Table of Contents
24 |
25 | 1. [Image Classification](#image-classification)
26 | 2. [Object Detection](#object-detection)
27 | 3. [Image Segmentation](#image-segmentation)
28 | 4. [NLP](#nlp)
29 | 5. [ONNX](#onnx)
30 | 6. [Custom](#custom)
31 |
32 | ## Image Classification
33 |
34 | For image classification tasks, Sparsify relies on the PTH format generated from SparseML.
35 | Specifically, the PTH format generated from the `ModuleExporter` class in SparseML.
36 | This will save a model in the PTH format with the following structure:
37 |
38 | ### Structure
39 | ```text
40 | {
41 | "state_dict": model.state_dict(),
42 | "optimizer": optimizer.state_dict(),
43 | "recipe": recipe,
44 | "epoch": epoch,
45 | "arch_key": arch_key,
46 | }
47 | ```
48 |
49 | ### Example
50 | ```python
51 | from sparseml.pytorch.image_classification.utils import ModuleExporter
52 | from torchvision.models import resnet18
53 |
54 | model = resnet18()
55 | exporter = ModuleExporter(model, "./")
56 | exporter.export_pytorch(
57 | optimizer=None,
58 | epoch=-1,
59 | recipe=None,
60 | name=f"{model}.pth",
61 | arch_key="resnet18",
62 | )
63 | ```
64 |
65 | ## Object Detection
66 |
67 | For object detection tasks, Sparsify utilizes the YOLO format for models.
68 | This is the same format used by Ultralytics [YOLOv5/YOLOv8](https://docs.ultralytics.com/)
69 | This is the default format that is saved from training within the YOLOv5 or YOLOv8 repos.
70 |
71 | More information on the YOLO format can be found [here](https://docs.ultralytics.com/tasks/detect/#models).
72 |
73 | ## Image Segmentation
74 |
75 | For image segmentation tasks, Sparsify utilizes the YOLO format for models.
76 | This is the same format used by Ultralytics [YOLOv5/YOLOv8](https://docs.ultralytics.com/)
77 | This is the default format that is saved from training within the YOLOv5 or YOLOv8 repos.
78 |
79 | More information on the YOLO format can be found [here](https://docs.ultralytics.com/tasks/segment/#models).
80 |
81 | ## NLP
82 |
83 | For NLP tasks, Sparsify utilizes the HuggingFace Models format and expectations.
84 | This includes the standard tokenizer.json, config.json, and bin files.
85 | If using any of the standard transformers pathways externally or through SparseML, then this is the default format models are saved in.
86 |
87 | More information on the HuggingFace Models format can be found [here](https://huggingface.co/transformers/model_sharing.html).
88 |
89 | ## ONNX
90 |
91 | For One-Shot Experiments, Sparsify utilizes the `.ONNX` format for models.
92 | In the future, more formats will be added for support with One-Shot Experiments.
93 |
94 | For more information on the ONNX format, see the [ONNX website](https://onnx.ai/).
95 | For more information on exporting to the ONNX format, see our docs page [here](https://docs.neuralmagic.com/user-guides/onnx-export).
96 |
--------------------------------------------------------------------------------
/docs/source/icon-sparsify.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/icon-sparsify.png
--------------------------------------------------------------------------------
/docs/source/userguide/01-intro.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Welcome to Sparsify
18 |
19 | Sparsify improves model performance for deployment at scale using the latest model compression techniques. Use Sparsify to analyze, optimize, and integrate your neural network for production.
20 |
21 | Sparsify is a deep learning model acceleration tool suite that simplifies the model optimization process to rapidly achieve the best combination of size, speed, and accuracy on any deep learning model. Sparsify is an easy way to optimize and benchmark models informed by industry research insights for ML practitioners, including ML engineers and operators, who need to deploy performant deep learning models at scale. Sparsify provides visual performance potential for your model, including a sliding scale between performance and loss.
22 |
23 | ## About This Guide
24 |
25 | This guide provides the following for you to use Sparsify:
26 |
27 | - Learning path and goals
28 |
29 | - Basic examples
30 |
31 | - Details of features
32 |
33 | - Advanced options
34 |
35 | ## Feedback and Help
36 |
37 | Sparsify is currently an alpha release and, as such, the software may not contain all of the features that are planned for the final version. Neural Magic, the software's maintainer, welcomes all feedback and suggestions, from user experience to model optimization errors.
38 |
39 | For support, sign up or log in to our [**Deep Sparse Community Slack**.](https://join.slack.com/t/discuss-neuralmagic/shared_invite/zt-q1a1cnvo-YBoICSIw3L1dmQpjBeDurQ)
40 |
41 | Post software issues requiring support. Include a light summary, the steps to reproduce, screenshots, files used, or onscreen messages. Be sure to share your environment details and framework versions.
42 |
43 | [**GitHub Issue Queue**](https://github.com/neuralmagic/sparsify/issues/)
44 |
45 | Submit bugs and feature requests. Check existing issues to avoid duplication.
46 |
47 | ---
48 | **Next step...**
49 |
50 | Begin by [installing Sparsify.](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/02-install-sparsify.html)
51 |
--------------------------------------------------------------------------------
/docs/source/userguide/02-install-sparsify.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Installing and Launching Sparsify
18 |
19 | To install Sparsify, run:
20 |
21 | ```bash
22 | pip install sparsify
23 | ```
24 |
25 | To launch Sparsify, type the following command:
26 |
27 | ```bash
28 | sparsify
29 | ```
30 |
31 | Optionally, you can run `sparsify -h` to get more options.
32 |
33 | ## From the Start screen, you can
34 |
35 |
36 |
37 | ### Start a new project
38 |
39 | The New Project button initiates the start of a new project during which you will analyze, optimize, and integrate your model to maximize performance and ensure compatibility with optionally, Neural Magic's DeepSparse Engine.
40 |
41 | ### Open an existing project
42 |
43 | Projects are listed in a navigation bar (black area) on the left of the Start page. You can create a single or multiple projects for your analysis.
44 |
45 | ### Display additional information
46 |
47 | An information button is available in the upper right of the navigation bar to display information about Sparsify.
48 |
49 |
50 |
51 | ---
52 | **Next step...**
53 |
54 | Go deeper on details with the [Sparsify Overview](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/03-sparsify-overview.html).
55 |
--------------------------------------------------------------------------------
/docs/source/userguide/03-sparsify-overview.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Sparsify Overview
18 |
19 | Sparsify involves three simple steps:
20 |
21 | ## 1. Analyze
22 |
23 | Create a project and upload your model. Sparsify will estimate performance improvements and model compression potential. During analysis, your goals are to:
24 |
25 | - Open an existing project, or create a new project by uploading an ONNX version of your model.
26 |
27 | - Profile your model for the effects of model optimizations on performance and loss.
28 |
29 | ## 2. Optimize
30 |
31 | Use the Sparsify automatic model optimization algorithms to apply the latest techniques to make your model smaller and run faster. Sparsify enables you to optimize using pruning, and quantization (future), and sparse transfer learning (future). During optimization, your goals are to:
32 |
33 | - Create and modify a model optimization configuration.
34 |
35 | - Optionally, benchmark the model to get measured (rather than estimated) values.
36 |
37 | ## 3. Integrate
38 |
39 | Export a configuration (yml) file and integrate code into your training workflow. This minimizes the work needed for you to quickly retrain your model to put Sparsify optimizations into practice. During integration, your goals are to:
40 |
41 | - Export the configuration and integrate it into your current training flow.
42 |
43 | - Include Sparsify-generated integration and/or training code into your current training flow.
44 |
45 | While *analyzing* and *optimizing* a model, five work sections are provided in the navigation bar for each project:
46 |
47 |
48 |
49 | When a section is active, it changes color. For example:
50 |
51 |
52 |
53 | **Performance Profiles** provide an indication of the effect of model optimizations on the inference performance of the model. In addition, they provide detailed performance information for the model without any optimizations.
54 |
55 | **Loss Profiles** provide an indication of the effect of optimization on the loss. A loss profile shows 1) how each layer responds to and is affected by optimization sensitivity such as pruning and 2) how many parameters there are in the model.
56 |
57 | **Optimization** applies the latest techniques to make your model run faster. Once satisfied with the optimization, you can export a configuration (yml) file and integrate code into your training.
58 |
59 | **Benchmarks** provide measured (rather than estimated) values. After running a benchmark, you might want to change optimization values and run an optimization profile again.
60 |
61 | **Settings** for your model project are listed. You can review/change settings at any time.
62 |
63 | ---
64 | **Next step...**
65 |
66 | Begin by [analyzing](https://docs.neuralmagic.com/archive/sparsify/source/userguide/04-analyze.html) your model.
67 |
--------------------------------------------------------------------------------
/docs/source/userguide/04-analyze.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Analyze
18 |
19 | During analysis, you will profile (understand) your model. Profiling is a unique Sparsify feature that does a layer-by-layer analysis of the model by looking at loss and performance. Rather than simply estimating, profiling determines which parts of your model contribute to performance and which parts contribute to loss. For example, if you optimize the model and cut out one layer completely, how much will that affect the loss? And, how much is that going to affect the performance? This information is valuable to know before you begin to optimize. It guides the automatic optimization algorithms.
20 |
21 | During analysis, your goals are to:
22 |
23 | - Open an existing project, or create a new project by uploading an ONNX version of your model.
24 |
25 | - Profile your model for the effects of model optimizations on performance and loss.
26 |
27 | ## Opening an Existing Project
28 |
29 | Existing projects are listed by name in the navigation bar on the left side of the screen. Just click on a project name to open it.
30 |
31 |
32 |
33 | ## Creating a New Project
34 |
35 | Follow this procedure to set up a new project. You will select an ONNX* model configuration file, name the project, and profile the model (based on batch size and cores).
36 |
37 | - [ONNX](https://onnx.ai/) (Open Neural Network Exchange) is an open-source artificial intelligence ecosystem. Model data that is the ONNX format is easily passed to the DeepSparse Emgine to get inference results. Sparsify accepts only ONNX files.
38 |
39 | If further instructions are needed, consult the [SparseML's documentation on Exporting to ONNX](https://docs.neuralmagic.com/sparseml/) for PyTorch and TensorFlow.
40 |
41 | 1. Click **NEW PROJECT**.
42 |
43 | 2. Start a new project in one of several ways:
44 |
45 | - Click to browse or drag a local ONNX file of the model to load. You can browse to or drag from the computer that is currently running Sparsify in the browser.
46 |
47 | - Click to enter a remote server path or URL for an ONNX file. A remote server path is anywhere that the "sparsify" command was originally entered to start the server.
48 |
49 |
50 |
51 | **Note**: Click **CANCEL** at any time if you want to end the new project procedure. You will be returned to the Start page.
52 |
53 | 3. Once you select the model (base.onnx, in this example)....
54 |
55 |
56 |
57 | click **UPLOAD** to upload the model file.
58 |
59 |
60 |
61 | 4. When a local file is uploaded, the name of the file is automatically displayed as the project name. If you upload a model file from a URL, the name field will be empty and the project will be listed as "unspecified" in the project list. In either case, you can change this name and/or enter a project description. You can use alphanumeric characters, special characters, and spaces.
62 |
63 | The project name is used throughout Sparsify as a reference to your model. For example, if you create multiple projects using the same model, unique project names will enable you to differentiate.
64 |
65 | **Note:** You can change the project name at any time by returning to this dialog or by changing the name in the [Settings](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/07-settings.html).
66 |
67 |
68 |
69 | **Note:** Click BACK if you want to return to the previous dialog.
70 |
71 | 5. Click **NEXT**.
72 |
73 | 6. Continue by [Profiling Your Model](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04a-profiling-your-model.html).
74 |
--------------------------------------------------------------------------------
/docs/source/userguide/04a-profiling-your-model.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Profiling Your Model
18 |
19 | Profiling is used to determine how optimization will affect performance and loss.
20 |
21 | - A performance profile provides an indication of the effect of model optimizations on the inference performance of the model. In addition, it provides detailed performance information for the model without any optimizations. The profile looks at a detailed view of how fast each layer is running and how much time it is taking as well as how much time can be optimized.
22 |
23 | - A loss profile is an indication of the effect of optimization on the loss. It shows how each layer is likely to affect the loss when model optimizations such as pruning are applied to it. The loss profile also shows how many parameters there are in the model.
24 |
25 |
26 |
27 | **Note:** If you change information on this dialog and then click **BACK**, the changes are retained when you return to this dialog.
28 |
29 | 1. You can select both Performance and Loss for profiling. In general, both should be run and, as such, both are selected by default.
30 |
31 | You may opt to disable the Performance option if the model is large and will require numerous executions that will take considerable time. In this case, the performance profile will be approximated from the architecture as a FLOPS (floating point operations per second) profile. A FLOPS profile is a theoretical measure of performance.
32 |
33 | If you are just [benchmarking](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/05a-benchmark.html) the model, you may disable both the Performance and Loss options. In this case, both profiles will be approximated from the architecture.
34 |
35 | 2. Enter names for the performance and loss profiles. These names will be referenced throughout Sparsify to quickly identify your profiles.
36 |
37 | 3. Further describe the Performance profile for the target deployment:
38 |
39 | - Batch size of the inputs to be used with the model. For example, you might enter 1 if you are running real-time object detection. If you are running a throughput use case, batch size depends on your domain and use case (64 is common for image classification).
40 |
41 | - Number of CPU cores on which to run the model.
42 |
43 | You should enter the batch size and core count to which you will deploy. Sparsify will run with this batch size and core count and apply the optimizations to the model to see how the performance is being changed. Batch size and core count can affect how Sparsify runs the model and how fast it is run; therefore, they affect how Sparsify optimizes the model.
44 |
45 | **Note:** Eventually, you might want to run multiple performance profiles by [Adding a New Performance Profile](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04b-reviewing-performance-profiles.html#adding-a-new-performance-profile). For example, if you are unsure of how you will deploy, you might want to consider various optimization schemes. Or, you may want multiple performance profiles because you deploy a model in multiple ways (such as a throughput offline and latency online).
46 |
47 | 4. Click **RUN** and a message will indicate that the system is profiling the performance and/or loss.
48 |
49 |
50 |
51 | When all metrics are generated, the New Project Setup dialog is grayed out. For example:
52 |
53 |
54 |
55 | 5. Click **COMPLETE**. The performance profile will be displayed, as described in [Reviewing Performance Profiles](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04b-reviewing-performance-profiles.html).
56 |
57 | ---
58 | **Next steps...**
59 |
60 | Continue by reviewing your [Performance Profiles](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04b-reviewing-performance-profiles.html) and [Loss Profiles](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04c-reviewing-loss-profiles.html).
61 | Then, you will be ready to [Optimize](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/05-optimize.html).
62 |
--------------------------------------------------------------------------------
/docs/source/userguide/04c-reviewing-loss-profiles.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Reviewing Loss Profiles
18 |
19 | A loss profile is an indication of the effect of model optimizations on the loss. It shows how each layer responds to and is affected by optimization sensitivity such as pruning. The loss profile also shows how many parameters there are in the model.
20 |
21 |
22 |
23 | You might see two types of loss profiles listed in the Loss Profiles section of the navigation bar:
24 |
25 | - "baseline" (the name entered on the New Project Setup dialog)—the profile that you defined, which indicates loss sensitivity and the number of parameters in the model.
26 |
27 | - Approximated—a profile that is approximated from the architecture if you did not specify Loss during project setup.
28 |
29 | The Loss Profile consists of three sections:
30 |
31 | - Model Summary
32 |
33 | - Layer Sensitivities
34 |
35 | - Layer Params Counts
36 |
37 | ## Model Summary
38 |
39 | The Model Summary provides information about the loss profile, including the pruning and quantization (future release) settings that were used when the profile was run.
40 |
41 |
42 |
43 | The Model Summary also shows the total number of parameters and how much reduction Sparsify estimates as well as the loss sensitivity (an indication of how the model is affected by optimization) and estimated recovery (the estimated confidence in recovering the original loss of the model after retraining with the current model optimization).
44 |
45 | Average sensitivity indicates the loss sensitivity of the entire model. The estimated recovery value shows the chance of recovering the loss completely.
46 |
47 |
48 |
49 | The number of parameters (params) in a model corresponds to how well the model can be optimized. For example, if a model has a significant number of parameters, it will be very prunable because it is very redundant. The estimated parameters reduction shows how much model compression will result from optimization.
50 |
51 |
52 |
53 | ## Layer Sensitivities
54 |
55 | The Layer Sensitivities graph shows pruning sensitivity measure values at each layer. You will see which layer is the most sensitive to pruning and which is the least sensitive. (The value for each layer is relative and not in an exact range.) When the graph shows that a layer affects the loss significantly, you may not want to prune that layer.
56 |
57 |
58 |
59 | **Note:** When a model has more weights that are further from zero, the model is more sensitive to pruning.
60 |
61 | ## Layer Params Counts
62 |
63 | This graph shows how many parameters are in each layer, which is important when you are trying to reduce the size of the model. In the following example, the last two layers make up many of the parameters in the network. So, you would want to include these layers (with the most parameters) for optimization as those are the layers that will have the most effect on size-reduction.
64 |
65 | **Note:** There will be a rough correlation between the sensitivity and the number of parameters in a layer. A layer with fewer parameters will be more sensitive.
66 |
67 |
68 |
69 | ### Adding a New Loss Profile
70 |
71 | You can identify a new loss profile:
72 |
73 | 1. Click the icon in the Loss Profiles section of the navigation bar.
74 |
75 | 2. Enter the profile name.
76 |
77 | 3. Click **ADD**.
78 |
79 |
80 |
81 | ---
82 | **Next steps...**
83 |
84 | Continue by reviewing your [Performance Profiles](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/04b-reviewing-performance-profiles.html) if you have not already done so.
85 | Then, you will be ready to [Optimize](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/05-optimize.html).
86 |
--------------------------------------------------------------------------------
/docs/source/userguide/06-integrate.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Integrate
18 |
19 | After you optimize your model, click the **EXPORT** button on the Optimization screen to initiate the integration process.
20 |
21 |
22 |
23 | During integration, you will generate a configuration (yml) file and integrate code into your training. This minimizes the work needed for you to train. The configuration file includes descriptions ("modifiers") of how the model will be modified during training. These modifiers correspond to modifiers in the [SparseML code base](https://github.com/neuralmagic/sparseml/). You can integrate the configuration file with your existing training platform and do not need to rewrite any part of your platform for optimization.
24 |
25 |
26 |
27 | Your goals are to:
28 |
29 | - Export the configuration file.
30 |
31 | - Include Sparsify-generated integration and/or training code into your current training flow.
32 |
33 | You can choose the PyTorch or TensorFlow framework, and the file code will change accordingly.
34 |
35 | ---
36 | **Next step...**
37 |
38 | Explore the [Optimization Config File and Code for Optimization](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/06a-optimize-config.html).
39 |
--------------------------------------------------------------------------------
/docs/source/userguide/06a-optimize-config.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Optimization Config File and Code for Optimization
18 |
19 | The **optimization configuration file** is used during training. You can save this file to disk or copy it to your clipboard.
20 |
21 |
22 |
23 | The optimization configuration file (config.yml) includes all information that was described during optimization. The configuration file encodes all Sparsify processing into a format that your system can use to override the training process (such as to prune and control learning rate).
24 |
25 | After exporting, you would integrate the configuration file into your training flow. Then, run the training. If the results are not acceptable for your business criteria, return to model optimization and try different values. For example, perhaps the model did not recover with the pruning modifier and the sparsity should be lowered.
26 |
27 | **Optimization code** is provided for training and for integration. You can save this code to disk or copy it to your clipboard.
28 |
29 |
30 |
31 | ---
32 | **Next step...**
33 |
34 | Explore your project's [Settings](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/07-settings.html) or review general [Key Concepts, Features, and Terms](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/08-key-terms.html) mentioned throughout Sparsify.
35 |
--------------------------------------------------------------------------------
/docs/source/userguide/07-settings.md:
--------------------------------------------------------------------------------
1 |
16 |
17 | # Settings
18 |
19 | Settings for a saved project can be accessed from your project's left navigation bar. This dialog reflects any information that you entered for the project. Clicking into editable sections will display a "save button" and allow you to make changes. Settings includes three sections:
20 |
21 | - Project Settings
22 |
23 |
24 |
25 | - Original Training Settings
26 |
27 |
28 |
29 | - Delete Project
30 |
31 |
32 |
33 | ---
34 | **Next step...**
35 |
36 | Review general [Key Concepts, Features, and Terms](https://docs.neuralmagic.com/archive/sparsify/main/source/userguide/08-key-terms.html) mentioned throughout Sparsify.
37 |
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_0.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_0.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_1.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_10.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_10.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_11.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_11.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_12.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_12.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_13.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_13.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_14.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_14.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_15.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_15.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_15a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_15a.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_15b.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_15b.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_16.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_16.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_17.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_17.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_18.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_18.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_19.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_19.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_2.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_20.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_20.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_21.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_21.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_21a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_21a.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_21b.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_21b.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_22.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_22.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_23.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_23.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_24.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_24.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_25.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_25.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_26.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_26.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_27.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_27.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_28.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_28.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29a.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29b.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29b.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29c.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29c.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29d.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29d.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_29e.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_29e.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_3.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_30.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_30.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_31.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_31.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_31a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_31a.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_32.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_32.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_33.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_33.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_34.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_34.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_35.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_35.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_36.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_36.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_36a.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_36a.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_37.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_37.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_38.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_38.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_39.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_39.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_4.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_40.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_40.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_41.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_41.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_42.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_42.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_43.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_43.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_44.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_44.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_45.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_45.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_46.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_46.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_47.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_47.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_48.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_48.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_49.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_49.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_5.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_50.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_50.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_51.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_51.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_52.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_52.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_53.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_53.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_54.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_54.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_55.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_55.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_56.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_56.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_57.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_57.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_58.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_58.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_59.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_59.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_6.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_60.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_60.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_61.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_61.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_62.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_62.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_63.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_63.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_64.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_64.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_65.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_65.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_7.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_8.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_8.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/images/image_9.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/docs/source/userguide/images/image_9.jpg
--------------------------------------------------------------------------------
/docs/source/userguide/index.rst:
--------------------------------------------------------------------------------
1 | ..
2 | Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing,
11 | software distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 |
16 | User Guide
17 | ==========
18 |
19 | .. toctree::
20 | :maxdepth: 3
21 |
22 | 01-intro
23 | 02-install-sparsify
24 | 03-sparsify-overview
25 | 04-analyze
26 | 04a-profiling-your-model
27 | 04b-reviewing-performance-profiles
28 | 04c-reviewing-loss-profiles
29 | 05-optimize
30 | 05a-benchmark
31 | 06-integrate
32 | 06a-optimize-config
33 | 07-settings
34 | 08-key-terms
35 |
--------------------------------------------------------------------------------
/examples/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/examples/.gitkeep
--------------------------------------------------------------------------------
/public/assets/favicon/android-chrome-192x192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/android-chrome-192x192.png
--------------------------------------------------------------------------------
/public/assets/favicon/android-chrome-512x512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/android-chrome-512x512.png
--------------------------------------------------------------------------------
/public/assets/favicon/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/apple-touch-icon.png
--------------------------------------------------------------------------------
/public/assets/favicon/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/favicon-16x16.png
--------------------------------------------------------------------------------
/public/assets/favicon/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/favicon-32x32.png
--------------------------------------------------------------------------------
/public/assets/favicon/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/public/assets/favicon/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
27 |
28 |
29 | Sparsify
30 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "assets/favicon/favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "assets/favicon/android-chrome-192x192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "assets/favicon/android-chrome-512x512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [tool.black]
2 | line-length = 88
3 | target-version = ['py36']
4 |
--------------------------------------------------------------------------------
/scripts/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/scripts/.gitkeep
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [isort]
2 | profile = black
3 | default_section = FIRSTPARTY
4 | ensure_newline_before_comments = True
5 | force_grid_wrap = 0
6 | include_trailing_comma = True
7 | known_first_party = deepsparse,sparsezoo,sparseml,sparsify,tests
8 | known_third_party = bs4,requests,packaging,yaml,tqdm,numpy,onnx,pytest,flasgger,flask,flask_cors,pkg_resources,torch,tensorflow,keras,marshmallow,werkzeug,peewee,playhouse
9 | sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
10 |
11 | line_length = 88
12 | lines_after_imports = 2
13 | multi_line_output = 3
14 | use_parentheses = True
15 |
16 | [flake8]
17 | ignore = E203, E251, E701, W503
18 | max-line-length = 88
--------------------------------------------------------------------------------
/src/sparsify/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .login import *
19 |
--------------------------------------------------------------------------------
/src/sparsify/auto/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Library for automated sparsification runs using the SparseML package and
17 | its integrations
18 | """
19 |
20 | # flake8: noqa
21 | # isort: skip_file
22 |
23 | from .utils import *
24 | from .tasks import *
25 | from .scripts.main import main
26 |
--------------------------------------------------------------------------------
/src/sparsify/auto/samples/finetune_llmfoundry_sample.yaml:
--------------------------------------------------------------------------------
1 | max_seq_len: 2048
2 | global_seed: 17
3 | model_name_or_path: mosaicml/mpt-7b-instruct
4 | load_path: /storage/dsikka/mpt_7b_instruct_oneshot_sp70.pt
5 | precision: amp_bf16
6 |
7 | max_duration: 1ep
8 | eval_interval: 1ep
9 | # eval_subset_num_batches: 3 # use this for quick testing
10 | eval_first: true
11 | seed: ${global_seed}
12 |
13 | global_train_batch_size: 1
14 | # for mpt-7b dense:
15 | # 4 x A100_80GB = "device_train_microbatch_size: 12"
16 | # 8 x A6000_48GB = "device_train_microbatch_size: 6"
17 |
18 | # for mpt-7b sparse (with masks):
19 | # 8 x A6000_48GB = "device_train_microbatch_size: 4"
20 | device_train_batch_size: 1
21 | device_train_microbatch_size: 1
22 | device_eval_batch_size: 1
23 |
24 | # Run Name
25 | run_name: test_run
26 |
27 | model:
28 | name: hf_causal_lm
29 | pretrained: true
30 | pretrained_model_name_or_path: mosaicml/mpt-7b-instruct
31 | max_seq_len: ${max_seq_len}
32 | config_overrides:
33 | attn_config:
34 | attn_impl: torch
35 | # Set this to `true` if using `train_loader.dataset.packing_ratio` below
36 | attn_uses_sequence_id: true
37 |
38 | # Tokenizer
39 | tokenizer:
40 | name: EleutherAI/gpt-neox-20b
41 | kwargs:
42 | model_max_length: ${max_seq_len}
43 |
44 | # Dataloaders
45 | train_loader:
46 | name: finetuning
47 | dataset:
48 | hf_name: mosaicml/dolly_hhrlhf
49 | split: train
50 | max_seq_len: ${max_seq_len}
51 | allow_pad_trimming: false
52 | decoder_only_format: true
53 | # # Use `python llmfoundry/data/packing.py --yaml-path /path/to/this/yaml/ ...`
54 | # # to profile this run's optimal packing_ratio as it depends on GPU count,
55 | # # batch size, sequence length
56 | packing_ratio: 13 # padding=0.36%, waste=0.79%
57 | shuffle: true
58 | drop_last: false
59 | num_workers: 8
60 | pin_memory: false
61 | prefetch_factor: 2
62 | persistent_workers: true
63 | timeout: 0
64 |
65 | eval_loader:
66 | name: finetuning
67 | dataset:
68 | hf_name: mosaicml/dolly_hhrlhf
69 | split: test
70 | max_seq_len: ${max_seq_len}
71 | allow_pad_trimming: false
72 | decoder_only_format: true
73 | packing_ratio: 13
74 | shuffle: false
75 | drop_last: false
76 | num_workers: 8
77 | pin_memory: false
78 | prefetch_factor: 2
79 | persistent_workers: true
80 | timeout: 0
81 |
82 | # Optimization
83 | scheduler:
84 | name: linear_decay_with_warmup
85 | t_warmup: 20ba
86 | alpha_f: 0
87 |
88 | optimizer:
89 | name: decoupled_adamw
90 | lr: 1e-4
91 | betas:
92 | - 0.9
93 | - 0.999
94 | eps: 1.0e-8
95 | weight_decay: 0.0
96 |
97 | # we can't use gradient clipping for sparse training runs because we don't have
98 | # a way to mask gradients of pruned weights, and thus the global gradient norm
99 | # will be incorrect
100 | # algorithms:
101 | # gradient_clipping:
102 | # clipping_type: norm
103 | # clipping_threshold: 1.0
104 |
105 | # FSDP
106 | fsdp_config:
107 | sharding_strategy: FULL_SHARD
108 | mixed_precision: FULL
109 | activation_checkpointing: true
110 | activation_checkpointing_reentrant: false
111 | activation_cpu_offload: false
112 | limit_all_gathers: true
113 | verbose: false
114 |
115 | # Logging
116 | progress_bar: false
117 | log_to_console: true
118 | console_log_interval: 1ba
119 |
120 | callbacks:
121 | speed_monitor:
122 | window_size: 10
123 | lr_monitor: {}
124 | memory_monitor: {}
125 | runtime_estimator: {}
126 |
127 | loggers:
128 | tensorboard: {}
129 |
130 | # Checkpoint to local filesystem or remote object store
131 | save_interval: 1ep
132 | save_num_checkpoints_to_keep: 1 # Important, this cleans up checkpoints saved to DISK
133 | save_folder: output_dir/{run_name}/checkpoints
134 | save_overwrite: true
--------------------------------------------------------------------------------
/src/sparsify/auto/scripts/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
--------------------------------------------------------------------------------
/src/sparsify/auto/scripts/main.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import logging
16 | from pathlib import Path
17 |
18 | import yaml
19 |
20 | from sparsify.auto.tasks import TaskRunner
21 | from sparsify.auto.utils import (
22 | api_request_config,
23 | create_save_directory,
24 | initialize_banner_logger,
25 | )
26 | from sparsify.schemas import APIArgs
27 | from sparsify.schemas.auto_api import SparsificationTrainingConfig
28 | from sparsify.utils import get_task_info
29 | from tensorboard.program import TensorBoard
30 | from tensorboard.util import tb_logging
31 |
32 |
33 | _LOGGER = logging.getLogger("auto_banner")
34 |
35 |
36 | def main(api_args: APIArgs):
37 | initialize_banner_logger()
38 |
39 | # Set up directory for saving
40 | (
41 | train_directory,
42 | log_directory,
43 | deploy_directory,
44 | ) = create_save_directory(api_args)
45 |
46 | if api_args.task in get_task_info("finetune").aliases:
47 | _LOGGER.info(
48 | "Running finetuning. "
49 | "Currently only arguments passed for use-case and data will be considered"
50 | )
51 | config = SparsificationTrainingConfig(
52 | task=api_args.task, dataset=api_args.dataset, base_model=None, recipe=None
53 | )
54 | runner = TaskRunner.create(config)
55 | runner.train(train_directory=train_directory, log_directory=log_directory)
56 | return
57 |
58 | _suppress_tensorboard_logs()
59 |
60 | # Launch tensorboard server
61 | tensorboard_server = TensorBoard()
62 | tensorboard_server.configure(argv=[None, "--logdir", log_directory])
63 | url = tensorboard_server.launch()
64 | _LOGGER.info(f"TensorBoard listening on {url}")
65 |
66 | # Request config from api and instantiate runner
67 |
68 | raw_config = api_request_config(api_args)
69 | config = SparsificationTrainingConfig(**raw_config)
70 |
71 | runner = TaskRunner.create(config)
72 | # Execute integration run and return metrics
73 | metrics = runner.train(train_directory=train_directory, log_directory=log_directory)
74 |
75 | yaml.safe_dump(
76 | metrics.dict(), (Path(train_directory).parent / "metrics.yaml").open("w")
77 | )
78 | runner.export(model_directory=train_directory)
79 | runner.create_deployment_directory(
80 | train_directory=train_directory, deploy_directory=deploy_directory
81 | )
82 |
83 |
84 | def _suppress_tensorboard_logs():
85 | # set tensorboard logger to warning level
86 | # avoids a constant stream of logs from tensorboard
87 | tb_logger = tb_logging.get_logger()
88 | tb_logger.setLevel(logging.WARNING)
89 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .runner import *
19 | from .args import *
20 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/args.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from typing import List, Optional
16 |
17 | from pydantic import BaseModel
18 |
19 |
20 | __all__ = ["BaseArgs"]
21 |
22 |
23 | class BaseArgs(BaseModel):
24 | """
25 | Base class for representing integration stage args (train or export)
26 | """
27 |
28 | def serialize_to_cli_string(self, underscores_to_dashes) -> List[str]:
29 | """
30 | Handles logic for converting pydantic classes into valid argument strings.
31 |
32 | :return: string of the full CLI command
33 | """
34 | args_string_list = []
35 | for key, value in self.dict().items():
36 | key = key.replace("_", "-") if underscores_to_dashes else key
37 | # Handles bool type args (e.g. --do-train)
38 | if isinstance(value, bool):
39 | bool_str = self._serialize_bool(key, value)
40 | if bool_str is not None:
41 | args_string_list.append(bool_str)
42 | elif isinstance(value, List):
43 | list_str = self._serialize_list(key, value)
44 | if list_str is not None:
45 | args_string_list.extend(list_str)
46 | # Handles the most straightforward case of keyword followed by value
47 | # e.g. --epochs 30
48 | else:
49 | value_str = self._serialize_value(key, value)
50 | if value_str is not None:
51 | args_string_list.extend(value_str)
52 | return args_string_list
53 |
54 | def _serialize_value(self, key: str, value: str) -> Optional[List[str]]:
55 | """
56 | Handles logic for converting most values to valid argument strings.
57 |
58 | :return: list of serialized strings or None
59 | """
60 | if value is None or value == "":
61 | return None
62 | return [f"--{key}", str(value)]
63 |
64 | def _serialize_bool(self, key: str, value: bool) -> Optional[str]:
65 | """
66 | Handles logic for converting bools to valid argument strings.
67 |
68 | :return: serialized string or None
69 | """
70 | if value:
71 | return "--" + key
72 | return None
73 |
74 | def _serialize_list(self, key: str, value: List) -> Optional[List[str]]:
75 | """
76 | Handles logic for converting lists to valid argument strings.
77 |
78 | :return: list of serialized strings
79 | """
80 | if len(value) < 2:
81 | raise ValueError(
82 | "List arguments must have more one entry. " f"Received {key}:{value}"
83 | )
84 | # Handles args that are both bool and value based (see evolve in yolov5)
85 | if isinstance(value[0], bool):
86 | if value[0]:
87 | return [f"--{key}", str(value[1])]
88 | return None
89 | # Handles args that have multiple values after the keyword.
90 | # e.g. --freeze-layers 0 10 15
91 | else:
92 | return [f"--{key}", map(str, value)]
93 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/deployment_instructions.md:
--------------------------------------------------------------------------------
1 | # Sparsify Deployment Guide
2 |
3 | Deploying with Neural Magic's inference runtime, [DeepSparse](https://github.com/neuralmagic/deepsparse), is recommended for the best performance with sparsified models on CPUs.
4 | The deployment folder contains everything necessary to benchmark and deploy a sparsified model with DeepSparse.
5 |
6 | ## Requirements
7 |
8 | A Linux-based CPU system with Python versions 3.8-3.10 installed and AVX2 or greater instruction set is required to run DeepSparse.
9 | DeepSparse is not currently supported on Windows or MacOS.
10 | To install DeepSparse, its dependencies, and check your system, run the following commands:
11 |
12 | ```bash
13 | pip install deepsparse[server]
14 | deepsparse.check_hardware
15 | ```
16 |
17 | Other installation options may be needed, depending on your use case.
18 | For more details and other installation options, see the [Installation Guide](https://github.com/neuralmagic/deepsparse).
19 |
20 | For the latest hardware support and system requirements, see the [Support and Requirements Guide](https://github.com/neuralmagic/deepsparse).
21 |
22 | ## Benchmarking
23 |
24 | The `deepsparse.benchmark` command enables benchmarking of an ONNX model on your system.
25 | The command takes a model path as a minimum argument and will run the model through a series of inference runs using random data.
26 | For example:
27 |
28 | ```bash
29 | deepsparse.benchmark model.onnx
30 | ```
31 |
32 | For more information on the `deepsparse.benchmark` command, see the [Benchmarking Guide](https://github.com/neuralmagic/deepsparse/blob/main/docs/user-guide/deepsparse-benchmarking.md).
33 |
34 | ## Pipeline Deployments
35 |
36 | DeepSparse contains many pipeline deployments for different use cases.
37 | These pipelines package up the model inference and any pre- and post-processing steps into a single, optimized callable for deployment.
38 | Additionally, custom pipelines are supported.
39 | For example, a sample custom pipeline for ImageNet is provided below:
40 |
41 | ```python
42 | from deepsparse.pipelines.custom_pipeline import CustomTaskPipeline
43 | from torchvision import transforms
44 | from PIL import Image
45 | import torch
46 |
47 | preprocess_transforms = transforms.Compose([
48 | transforms.Resize(256),
49 | transforms.CenterCrop(224),
50 | transforms.ToTensor(),
51 | transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
52 | ])
53 |
54 | def preprocess(img_file):
55 | with open(img_file, "rb") as img_file:
56 | img = Image.open(img_file)
57 | img = img.convert("RGB")
58 | img = preprocess_transforms(img)
59 | batch = torch.stack([img])
60 | return [batch.numpy()]
61 |
62 | custom_pipeline = CustomTaskPipeline(
63 | model_path="zoo:cv/classification/resnet_v1-50/pytorch/sparseml/imagenet/pruned90_quant-none",
64 | process_inputs_fn=preprocess,
65 | )
66 |
67 | scores, probs = custom_pipeline("buddy.jpeg")
68 | ```
69 | (Note: Download [buddy.jpeg](https://raw.githubusercontent.com/neuralmagic/deepsparse/main/tests/deepsparse/pipelines/sample_images/buddy.jpeg))
70 |
71 |
72 | For more information on the available pipelines and how to create custom pipelines, see the [Pipeline Deployment Guide](https://github.com/neuralmagic/deepsparse/blob/main/docs/user-guide/deepsparse-benchmarking.md).
73 |
74 | ## Server Deployments
75 |
76 | DeepSparse additionally contains a performant server deployment for different use cases.
77 | The server deployment packages up the model inference and any pre- and post-processing steps into a single, optimized HTTP request for deployment.
78 | To start the server, run the following command with the appropriate arguments:
79 |
80 | ```bash
81 | deepsparse.server --task TASK --model_path ./deployment/model.onnx
82 | ```
83 |
84 | For more information on the `deepsparse.server` command, see the [Server Deployment Guide](https://github.com/neuralmagic/deepsparse/blob/main/docs/user-guide/deepsparse-server.md).
85 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/finetune/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
17 | from .args import *
18 |
19 |
20 | try:
21 | from .finetune import *
22 | from .runner import *
23 | except ImportError as exception:
24 | raise ImportError(
25 | "To use the llm finetuning pathway, please install sparsify[llm]"
26 | ) from exception
27 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/finetune/args.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from pydantic import Field
16 | from sparsify.auto.tasks import BaseArgs
17 |
18 |
19 | __all__ = ["FineTuneTrainArgs"]
20 |
21 |
22 | class FineTuneTrainArgs(BaseArgs):
23 | yaml: str = Field(
24 | default=None,
25 | description="path to the training yaml",
26 | )
27 | checkpoints: str = Field(
28 | default=None,
29 | description="path to the directory to store checkpoints",
30 | )
31 | logging: str = Field(
32 | default=None,
33 | description="path to store logs",
34 | )
35 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/finetune/helpers.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import torch
16 |
17 | from composer.core import Algorithm, Event
18 |
19 |
20 | all = ["attach_masks", "MaskPrunedWeights"]
21 |
22 |
23 | class MaskPrunedWeights(Algorithm):
24 | """
25 | Composer specific hook which allows us to mask weights after a specific event,
26 | in this case at the end of the batch. Provided as input to the Trainer while
27 | finetuning. Note: can also mask weights before the forward pass by adding
28 | `or event == Event.BATCH_START`
29 | """
30 |
31 | def match(self, event, state):
32 | return event == Event.BATCH_END
33 |
34 | @torch.no_grad()
35 | def apply(self, event, state, logger):
36 | def mask_weights(module):
37 | if hasattr(module, "constant_pruning_mask"):
38 | module.weight *= module.constant_pruning_mask
39 |
40 | state.model.apply(mask_weights)
41 |
42 |
43 | def attach_masks(model: torch.nn.Module):
44 | """
45 | Recursively attach masks to weights which have already been pruned to avoid
46 | finetuning them further.
47 |
48 | :param model: torch.nnn.Module to recursively attach masks to if the weights are
49 | already pruned
50 | """
51 | for _, module in model.named_children():
52 | if isinstance(module, torch.nn.Linear):
53 | constant_pruning_mask = torch.where(
54 | module.weight == 0,
55 | torch.tensor(0, dtype=torch.uint8),
56 | torch.tensor(1, dtype=torch.uint8),
57 | )
58 | module.register_buffer(
59 | "constant_pruning_mask", constant_pruning_mask, persistent=False
60 | )
61 | else:
62 | attach_masks(module)
63 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/finetune/runner.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from typing import Tuple
16 |
17 | from pydantic import BaseModel
18 | from sparsify.auto.tasks.finetune.args import FineTuneTrainArgs
19 | from sparsify.auto.tasks.finetune.finetune import main as train_hook
20 | from sparsify.auto.tasks.runner import TaskRunner
21 | from sparsify.auto.utils import HardwareSpecs
22 | from sparsify.schemas import Metrics, SparsificationTrainingConfig
23 | from sparsify.utils import TASK_REGISTRY
24 |
25 |
26 | __all__ = [
27 | "LLMFinetuner",
28 | ]
29 |
30 |
31 | @TaskRunner.register_task(task=TASK_REGISTRY["finetune"])
32 | class LLMFinetuner(TaskRunner):
33 | """
34 | TaskRunner for LLM finetuning. Currently set-up as a shell to leverage TaskRunner's
35 | ddp functionality for finetuning. Function definitions will be completed as
36 | functionality is further supported.
37 | """
38 |
39 | train_hook = staticmethod(train_hook)
40 | export_model_kwarg = "None"
41 |
42 | def __init__(self, config: SparsificationTrainingConfig):
43 | super().__init__(config)
44 |
45 | @classmethod
46 | def config_to_args(
47 | cls, config: SparsificationTrainingConfig
48 | ) -> Tuple[BaseModel, BaseModel]:
49 | train_args = FineTuneTrainArgs(yaml=config.dataset)
50 |
51 | return train_args, None
52 |
53 | def update_run_directory_args(self):
54 | pass
55 |
56 | def _train_completion_check(self) -> bool:
57 | pass
58 |
59 | def _export_completion_check(self) -> bool:
60 | pass
61 |
62 | def _update_train_args_post_failure(self, error_type: Exception):
63 | pass
64 |
65 | def _update_export_args_post_failure(self, error_type: Exception):
66 | pass
67 |
68 | def _get_metrics(self) -> Metrics:
69 | pass
70 |
71 | def _get_default_deployment_directory(self, train_directory: str) -> str:
72 | pass
73 |
74 | def tune_args_for_hardware(self, hardware_specs: HardwareSpecs):
75 | pass
76 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/image_classification/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
17 | from .args import *
18 | from .runner import *
19 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/object_detection/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
17 | from .yolov5 import *
18 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/object_detection/yolov5/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
17 | from .args import *
18 | from .runner import *
19 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/transformers/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | try:
19 | from .args import *
20 | from .runner import *
21 | except ImportError as exception:
22 | raise ImportError("Please install sparsify[nm] to use this pathway.") from exception
23 |
--------------------------------------------------------------------------------
/src/sparsify/auto/tasks/transformers/llama.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import logging
16 |
17 | import click
18 | from sparseml.transformers.export import export as export_hook
19 | from sparsify.auto.tasks.transformers import TransformersExportArgs
20 |
21 |
22 | _LOGGER = logging.getLogger()
23 | _LOGGER.setLevel(logging.INFO)
24 |
25 | """
26 | Usage: sparisfy.llama_export [OPTIONS]
27 |
28 | Exports a LLAMA model given a path to model directory, sequence length, and
29 | name of the exported pathway.
30 |
31 | Example: sparisfy.llama_export --model_path
32 | --sequence_length
33 |
34 | Output: Produces a deployment directory with the exported model
35 |
36 |
37 | Options:
38 | --model_path TEXT Path to directory where model files for weights,
39 | config, and tokenizer are stored
40 | --sequence_length INTEGER Sequence length to use. [default: 2048]
41 | --onnx_file_name TEXT Name of the exported model. [default:
42 | model.onnx]
43 | --help Show this message and exit. [default: False]
44 | """
45 |
46 |
47 | @click.command(context_settings={"show_default": True})
48 | @click.option(
49 | "--model_path",
50 | default=None,
51 | type=str,
52 | help="Path to directory where model files for weights, config, and "
53 | "tokenizer are stored",
54 | )
55 | @click.option(
56 | "--sequence_length",
57 | default=2048,
58 | type=int,
59 | help="Sequence length to use.",
60 | )
61 | @click.option(
62 | "--onnx_file_name",
63 | default="model.onnx",
64 | type=str,
65 | help="Name of the exported model.",
66 | )
67 | def llama_export(model_path: str, sequence_length: int, onnx_file_name: str):
68 | """
69 | Exports a LLAMA model given a path to model directory, sequence length, and name
70 | of the exported pathway.
71 |
72 | Example:
73 | sparisfy.llama_export --model_path --sequence_length
74 |
75 | Output:
76 | Produces a deployment directory with the exported model
77 |
78 | """
79 | export_args = TransformersExportArgs(
80 | task="text-generation",
81 | model_path=model_path,
82 | sequence_length=sequence_length,
83 | no_convert_qat=True,
84 | onnx_file_name=onnx_file_name,
85 | )
86 | _LOGGER.info("Exporting LLAMA model")
87 | export_hook(**export_args.dict())
88 |
--------------------------------------------------------------------------------
/src/sparsify/auto/utils/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .helpers import *
19 | from .error_handler import *
20 | from .hardware_analyzer import *
21 | from .nm_api import *
22 |
--------------------------------------------------------------------------------
/src/sparsify/auto/utils/hardware_analyzer.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | import warnings
15 | from typing import List
16 |
17 | import torch
18 |
19 | from pydantic import BaseModel, Field
20 |
21 |
22 | __ALL__ = ["HardwareSpecs", "analyze_hardware"]
23 |
24 |
25 | class HardwareSpecs(BaseModel):
26 | """
27 | Class containing information gleaned from local machine
28 | """
29 |
30 | cuda_available: bool = Field(description="Whether cuda is available on the system")
31 | device_count: int = Field(description="Number of devices detected")
32 | device_names: List[str] = Field(description="Names of devices available")
33 | fp16_available: bool = Field(description="True if mixed precision available")
34 |
35 |
36 | def analyze_hardware() -> HardwareSpecs:
37 | """
38 | Return a HardwareSpecs class filled with information on the local machine
39 | """
40 | return HardwareSpecs(
41 | cuda_available=_cuda_available(),
42 | device_count=torch.cuda.device_count() or 1,
43 | device_names=[f"cuda:{idx}" for idx in range(torch.cuda.device_count())]
44 | or (["CPU"]),
45 | fp16_available=torch.cuda.has_half,
46 | )
47 |
48 |
49 | def _cuda_available() -> bool:
50 | """
51 | Check that cuda is available and cuda operations pass as expected
52 | """
53 | if torch.cuda.is_available():
54 | device = torch.device("cuda:0")
55 | try:
56 | tensor = torch.rand(4, 4).to(device) # noqa: F841
57 | del tensor
58 | except RuntimeError:
59 | warnings.warn(
60 | "Detected CUDA drivers installed on system but could not perform "
61 | "Tensor operation on cuda:0 device"
62 | )
63 | return False
64 | return True
65 | return False
66 |
--------------------------------------------------------------------------------
/src/sparsify/auto/utils/nm_api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Helper functions for communicating with the Neural Magic API
17 | """
18 | import os
19 | from typing import Tuple
20 |
21 | import requests
22 |
23 | from sparsify.login import import_sparsifyml_authenticated
24 | from sparsify.schemas import APIArgs, Metrics, RunMode, SparsificationTrainingConfig
25 | from sparsify.utils import get_base_url, strtobool
26 |
27 |
28 | sparsifyml = import_sparsifyml_authenticated()
29 |
30 | from sparsifyml.auto import ( # noqa: E402
31 | auto_training_config_initial,
32 | auto_training_config_tune,
33 | )
34 |
35 |
36 | __all__ = ["api_request_config", "api_request_tune", "request_student_teacher_configs"]
37 |
38 | _CONFIG_REQUEST_END_POINT = "/v1/sparsify/auto/training-config"
39 | _CONFIG_TUNE_END_POINT = "/v1/sparsify/auto/training-config/tune"
40 |
41 | SPARSIFY_SERVER: bool = strtobool(os.getenv(key="SPARSIFY_SERVER", default="False"))
42 |
43 |
44 | def api_request_config(api_args: APIArgs) -> dict:
45 | """
46 | Make a request for the initial training configs
47 |
48 | :return: dictionary of SparsificationTrainingConfig objects
49 | """
50 | response = (
51 | requests.post(
52 | f"{get_base_url()}{_CONFIG_REQUEST_END_POINT}", json=api_args.dict()
53 | ).json()
54 | if SPARSIFY_SERVER
55 | else auto_training_config_initial(user_args=api_args).dict()
56 | )
57 | return response
58 |
59 |
60 | def api_request_tune(history: Tuple[SparsificationTrainingConfig, Metrics]) -> dict:
61 | """
62 | Make a server request for updated hyperparameters to run
63 |
64 | :return: dictionary of SparsificationTrainingConfig object
65 | """
66 |
67 | response = (
68 | requests.post(
69 | f"{get_base_url()}{_CONFIG_TUNE_END_POINT}",
70 | json=[(config.dict(), metrics.dict()) for config, metrics in history],
71 | ).json()
72 | if SPARSIFY_SERVER
73 | else auto_training_config_tune(
74 | trial_history=[(config, metrics) for config, metrics in history]
75 | ).dict()
76 | )
77 | return response
78 |
79 |
80 | def request_student_teacher_configs(
81 | api_args: APIArgs,
82 | ) -> Tuple[SparsificationTrainingConfig, SparsificationTrainingConfig]:
83 | """
84 | Request student and/or teacher sparsification configs from the NM API
85 | """
86 |
87 | student_config, teacher_config = None, None
88 |
89 | if RunMode(api_args.run_mode) == RunMode.teacher_only:
90 | teacher_config = SparsificationTrainingConfig(**api_request_config(api_args))
91 |
92 | else:
93 | student_config = SparsificationTrainingConfig(**api_request_config(api_args))
94 | if student_config.distill_teacher == "auto":
95 | teacher_input_args = api_args.copy(deep=True)
96 | teacher_input_args.run_mode = RunMode.teacher_only
97 | teacher_config = SparsificationTrainingConfig(
98 | **api_request_config(teacher_input_args)
99 | )
100 |
101 | return student_config, teacher_config
102 |
--------------------------------------------------------------------------------
/src/sparsify/check_environment/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .gpu_device import *
19 | from .ort_health import *
20 | from .pathway_checks import *
21 |
--------------------------------------------------------------------------------
/src/sparsify/check_environment/gpu_device.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import logging
16 |
17 | import torch
18 |
19 |
20 | _LOGGER = logging.getLogger(__name__)
21 |
22 | __all__ = ["check_for_gpu"]
23 |
24 |
25 | def check_for_gpu():
26 | """
27 | Check for GPU and warn if not found
28 | """
29 | _LOGGER.warning("Checking for GPU...")
30 | if not torch.cuda.is_available():
31 | _LOGGER.warn(
32 | "*************************** NO GPU DETECTED ***************************\n"
33 | "No GPU(s) detected on machine. The use of a GPU for training-aware "
34 | "sparsification, sparse-transfer learning, and one-shot sparsification is "
35 | "highly recommended.\n"
36 | "************************************************************************"
37 | )
38 | else:
39 | _LOGGER.warning("GPU check completed successfully")
40 |
--------------------------------------------------------------------------------
/src/sparsify/check_environment/main.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | from sparsify.check_environment import check_for_gpu, check_ort_health
15 |
16 |
17 | def main():
18 | """
19 | Check the environment for compatibility with the sparsifyml package
20 | """
21 | check_for_gpu()
22 | check_ort_health()
23 |
24 |
25 | if __name__ == "__main__":
26 | main()
27 |
--------------------------------------------------------------------------------
/src/sparsify/check_environment/pathway_checks.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | from sparsify.check_environment import check_for_gpu, check_ort_health
17 |
18 |
19 | __all__ = ["one_shot_checks", "auto_checks"]
20 |
21 |
22 | def one_shot_checks():
23 | """
24 | Check environment for compatibility with one-shot sparsification
25 | """
26 | check_for_gpu()
27 | check_ort_health()
28 |
29 |
30 | def auto_checks():
31 | """
32 | Check environment for compatibility with training-aware sparsification and
33 | sparse-transfer learning
34 | """
35 | check_for_gpu()
36 |
--------------------------------------------------------------------------------
/src/sparsify/cli/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
--------------------------------------------------------------------------------
/src/sparsify/create/README.MD:
--------------------------------------------------------------------------------
1 | # Sparsify Create
2 |
3 | Sparsify Create is a tool to automatically generate sparsification recipes and apply them to
4 | custom training aware scenarios.
5 |
6 | This document outlines basic usage of `sparsify.create`. For a full list of arguments, check the docstring.
7 |
8 |
9 | ## Usage
10 | The main entrypoint of `sparsify.create` is the `initialize` function. `initialize` takes a pytorch
11 | model, optimizer, and number of steps in each epoch, creates a sparsification recipe, and wraps the
12 | training script to incorporate sparsification in the existing training process.
13 |
14 | `sparsify.create.initialize` should be called **after** the model, optimizer, and dataloader
15 | (for determining number of steps per epoch) have been created but **before** the training process starts.
16 |
17 |
18 | ```python
19 | import sparsify
20 |
21 | # EXISTING TRAINING VARIABLES
22 | MODEL = ...
23 | OPTIMIZER = ...
24 | STEPS_PER_EPOCH = ... # len(DATALOADER)
25 |
26 | # initialize sparsify.create
27 | sparsification_manager, OPTIMIZER = sparsify.create.initialize(
28 | MODEL, OPTIMIZER, STEPS_PER_EPOCH
29 | )
30 |
31 | # if the script has an end epoch, update it from the manager
32 | END_EPOCH = sparsification_manager.max_epochs
33 | ```
34 |
35 |
36 | ## Recipe Variables
37 | The `recipe_variables` argument may be used to override particular values related to the sparsification
38 | recipe. This may include, epochs, pruning type, target hardware, and distillation hyper parameters.
39 |
40 | `recipe_variables` must be specified as a `RecipeVariables` schema or dictionary containing the valid
41 | variables. `RecipeVariables` schema and help text is included in `schemas.py`.
42 |
43 |
44 | ## Additional Integration Notes
45 | This section contains notes on additional updates that are useful for best practice with `sparsify.create`.
46 |
47 | ### Max Epochs
48 | As noted above, once sparsificaiton starts, it sets a new internal max epoch. To account for this
49 | any end epoch that exists in the script should be updated according to the `sparsification_manager`:
50 |
51 | ```python
52 | END_EPOCH = sparsification_manager.max_epochs
53 | ```
54 |
55 | ### Learning Rate Schedulers
56 | `sparsify.create` creates a sparsification recipe that has its own learning rate scheduler. To avoid
57 | learning rate conflicts, any existing learning rate schedulers should be disabled. The kind of learning
58 | rate scheudule used may be set with the `lr` parameter of `initialize()` and start and end LRs may
59 | be set with `RecipeVariables`.
60 |
61 | ### Mixed Precision Training
62 | If the integrated script supports mixed precision (FP16) training, it is likley using a torch `Scaler` object.
63 | Since this is the object that will be used for `.step()` updates instead of the `optimizer` it should also
64 | be passed to initialize and will instead be returned as an updated object:
65 |
66 | ```python
67 | # extending the original example
68 | SCALER = ...
69 |
70 | # initialize sparsify.create and override the SCALER
71 | sparsification_manager, SCALER = sparsify.create.initialize(
72 | MODEL, OPTIMIZER, STEPS_PER_EPOCH, scaler=SCALER
73 | )
74 | ```
75 |
76 | #### Quantization and Mixed Precision
77 | PyTorch does not support FP16 training with quantization, thus when quantization begins, mixed precision should
78 | be disabled. This can be done by setting `enabled=False` in the scaler and any context managers used in training
79 | for autocast.
80 |
81 | This will also cause an increase in GPU memory usage so the batch size should be adjusted to accommodate.
82 |
83 | The epoch that quantization can starts can be found with the following snippet, if it exists, mixed precision
84 | should be disabled after the `quantization_start_epoch`
85 |
86 | ```python
87 | if sparsification_manager.quantization_modifiers:
88 | quantization_start_epoch = min(
89 | modifier.start_epoch
90 | for modifier in sparsification_manager.quantization_modifiers
91 | )
92 | ```
93 |
94 | ### Distillation
95 | `sparsify.create` supports model distillation - to enable `distillation` should be set to `True`
96 | in `initialize`. Self distillation will be used by default, however a distillation teacher may
97 | be passed to `initialize`.
98 |
99 | Example of distillation with a custom teacher:
100 | ```python
101 | # extending the original example
102 | DISTILLATION_TEACHER = ...
103 |
104 | # initialize sparsify.create and override the SCALER
105 | sparsification_manager, OPTIMIZER = sparsify.create.initialize(
106 | MODEL,
107 | OPTIMIZER,
108 | STEPS_PER_EPOCH,
109 | distillation=True,
110 | distillation_teacher=DISTILLATION_TEACHER,
111 | )
112 | ```
113 |
114 |
115 | ### Sparse Transfer Learning
116 | Sparse transfer learning may be enabled by setting pruning to `transfer` in `initialize`:
117 | ```python
118 | # extending the original example
119 | sparsification_manager, OPTIMIZER = sparsify.create.initialize(
120 | MODEL, OPTIMIZER, STEPS_PER_EPOCH, pruning="constant"
121 | )
122 | ```
123 |
--------------------------------------------------------------------------------
/src/sparsify/create/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Interface and utilities for sparsify.create
17 | """
18 |
19 | # flake8: noqa
20 | from .api import *
21 | from .schemas import *
22 |
--------------------------------------------------------------------------------
/src/sparsify/create/api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | User facing API entrypoints for sparsify.create
17 | """
18 |
19 | import logging
20 | from typing import Any, Dict, Optional, Union
21 |
22 | import torch
23 | from torch.nn import Module
24 |
25 | from sparseml.pytorch.optim import ScheduledModifierManager
26 | from sparsify.create.schemas import RecipeVariables
27 | from sparsifyml.create import create_recipe
28 |
29 |
30 | _LOGGER = logging.getLogger(__name__)
31 |
32 |
33 | def initialize(
34 | model: Module,
35 | optimizer: torch.optim.Optimizer,
36 | steps_per_epoch: int,
37 | pruning: Union[str, bool, None] = True,
38 | quantization: Union[bool, str] = True,
39 | lr: str = "linear",
40 | distillation: bool = False,
41 | distillation_teacher: Optional[Union[str, Module]] = None,
42 | scaler: Optional[torch.cuda.amp.GradScaler] = None,
43 | recipe_variables: Union[RecipeVariables, Dict[str, Any], None] = None,
44 | ):
45 | """
46 | Prepares a pytorch model and optimizer for sparsification. Under the hood,
47 | a sparsification recipe will be created and initialized with SparseML to
48 | apply the desired optimizations
49 |
50 | :param model: PyTorch Module to sparsify
51 | :param optimizer: the optimizer used in model training
52 | :param steps_per_epoch: number of steps (batches) in each epoch
53 | :param pruning: optional pruning algorithm to use in the recipe, can be any of the
54 | following, `true` (represents Magnitude/Global-Magnitude pruning according to
55 | global_sparsity), `false` (No pruning), `acdc`, `mfac`, `movement`, `obs`,
56 | `constant`, `transfer`, or `sparse-transfer`. Defaults to `true`
57 | :param quantization: `True` if quantization needs to be applied else `False`.
58 | Defaults to `True`
59 | :param lr: the learning rate schedule function. Defaults to `linear`
60 | :param distillation: if `True` distillation will be added to the recipe.
61 | distillation_teacher should also be provided to initialize distillation if
62 | using a custom teacher. Will default to 'self' distillation if distillation
63 | is enabled with no teacher set
64 | :param scaler: scaler used to wrap optimizer for mix precision training, may be
65 | any object that wraps the optimizer `step` call
66 | :param distillation_teacher: teacher Module to use with distillation if enabled,
67 | defaults to self distillation if None provided
68 | :param recipe_variables: additional variables to override training with
69 | :return: tuple of the sparsification manager
70 | (`sparseml.pytorch.optim.ScheduledModifierManager`) and the wrapped optimizer
71 | object (scaler if provided otherwise optimizer)
72 | """
73 |
74 | # create recipe from sparsifyml
75 | _LOGGER.info(
76 | f"[sparsify] Creating recipe with pruning={pruning}, "
77 | f"quantization={quantization}, distilation={distillation}"
78 | )
79 | if pruning in ["transfer", "sparse-transfer"]:
80 | pruning = "constant"
81 | recipe_variables = recipe_variables or RecipeVariables() # get defaults
82 | recipe = create_recipe(
83 | model=model,
84 | pruning=pruning,
85 | quantization=quantization,
86 | distillation=distillation,
87 | lr=lr,
88 | recipe_variables=recipe_variables,
89 | )
90 |
91 | _LOGGER.info(
92 | "Initializing sparsification by wrapping "
93 | f"{scaler.__class__.__name__ or 'optimizer'}"
94 | )
95 | manager = ScheduledModifierManager.from_yaml(recipe)
96 | manager.initialize(
97 | module=model,
98 | epoch=0.0,
99 | distillation_teacher=(
100 | distillation_teacher or "self" if distillation else "disable"
101 | ),
102 | )
103 |
104 | wrapped_optim = manager.modify(
105 | model,
106 | optimizer,
107 | steps_per_epoch=steps_per_epoch,
108 | wrap_optim=scaler,
109 | )
110 |
111 | return manager, wrapped_optim
112 |
--------------------------------------------------------------------------------
/src/sparsify/create/schemas.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Schemas for supporting sparsify.create
17 | """
18 |
19 | from typing import Optional
20 |
21 | from pydantic import BaseModel, Field
22 |
23 |
24 | __all__ = [
25 | "RecipeVariables",
26 | ]
27 |
28 |
29 | class RecipeVariables(BaseModel):
30 | """
31 | Standardized recipe variables that may be overwritten in auto-generated recipes
32 | """
33 |
34 | # Training Vars
35 | num_epochs: float = Field(default=20.0, description="total epochs")
36 | init_lr: float = Field(default=1e-3, description="initial LR before scheduling")
37 | final_lr: float = Field(default=1e-7, description="target LR for end of scheduler")
38 | target: Optional[str] = Field(
39 | default=None, description="target hardware for deployment. ('vnni', 'tensorrt')"
40 | )
41 | # Sparsification Vars
42 | sparsity: float = Field(default=0.8, description="target pruning final sparsity")
43 | mask_type: str = Field(default="unstructured", description="pruning type/structure")
44 | global_sparsity: bool = Field(
45 | default=True, description="global/layerwise sparsity when set to True/False"
46 | )
47 | # Distillation Vars
48 | hardness: float = Field(default=0.5, description="distillation hardness")
49 | temperature: float = Field(default=2.0, description="distillation temperature")
50 |
--------------------------------------------------------------------------------
/src/sparsify/one_shot/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Module for functionality related to ONNX one shot sparsification
17 | """
18 |
19 | # flake8: noqa
20 | # isort: skip_file
21 | from .api import *
22 |
--------------------------------------------------------------------------------
/src/sparsify/one_shot/api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | import argparse
17 | from pathlib import Path
18 |
19 | from sparsezoo import Model
20 | from sparsify.login import import_sparsifyml_authenticated
21 | from sparsify.utils import constants
22 |
23 |
24 | sparsifyml = import_sparsifyml_authenticated()
25 | from sparsifyml import one_shot # noqa: E402
26 |
27 |
28 | __all__ = [
29 | "one_shot",
30 | ]
31 |
32 | _SUPPORTED_MODEL_FORMATS = [".onnx"]
33 |
34 |
35 | def main():
36 | parser = argparse.ArgumentParser(
37 | formatter_class=argparse.ArgumentDefaultsHelpFormatter
38 | )
39 |
40 | info = parser.add_argument_group("Project")
41 | info.add_argument(
42 | "--task",
43 | required=True,
44 | choices=sorted(constants.TASK_REGISTRY.keys()),
45 | help="The task this model is for",
46 | )
47 | info.add_argument(
48 | "--deploy-dir",
49 | default=None,
50 | type=str,
51 | help="Path to save the deployment ready model to",
52 | )
53 |
54 | model = parser.add_argument_group("Model")
55 | model.add_argument(
56 | "--model",
57 | required=True,
58 | type=str,
59 | help=(
60 | "Path to model file. "
61 | f"The following formats are supported: {_SUPPORTED_MODEL_FORMATS}"
62 | ),
63 | )
64 | model.add_argument(
65 | "--recipe", default=None, type=str, help="Recipe to override automatic recipe."
66 | )
67 |
68 | data = parser.add_argument_group("Data")
69 | data.add_argument(
70 | "--dataset", required=True, type=str, help="Path to dataset folder"
71 | )
72 | data.add_argument(
73 | "--num-samples",
74 | default=-1,
75 | type=int,
76 | help=(
77 | "Number of samples to use from the dataset for processing. "
78 | "-1 means the entire dataset."
79 | ),
80 | )
81 | data.add_argument(
82 | "--eval-metric",
83 | default="kl",
84 | choices=["kl", "accuracy", "mAP", "recall", "f1"],
85 | help="Metric that the model is evaluated against on the task.",
86 | )
87 |
88 | optim = parser.add_argument_group("Optimization")
89 | optim.add_argument(
90 | "--optim-level",
91 | default=0.5,
92 | type=int,
93 | help=(
94 | "Preferred tradeoff between accuracy and performance. Float value in the "
95 | "range [0, 1]. Default 0.5"
96 | ),
97 | )
98 |
99 | args = parser.parse_args()
100 |
101 | one_shot.one_shot(
102 | task=args.task,
103 | model_file=Path(_maybe_unwrap_zoo_stub(args.model)),
104 | dataset_dir=Path(args.dataset),
105 | num_samples=args.num_samples,
106 | deploy_dir=Path(args.deploy_dir),
107 | eval_metric=args.eval_metric,
108 | optim_level=args.optim_level,
109 | recipe_file=Path(args.recipe) if args.recipe is not None else None,
110 | )
111 |
112 |
113 | def _maybe_unwrap_zoo_stub(model_path: str) -> str:
114 | if model_path.startswith("zoo:"):
115 | return Model(model_path).onnx_model.path
116 | return model_path
117 |
118 |
119 | if __name__ == "__main__":
120 | main()
121 |
--------------------------------------------------------------------------------
/src/sparsify/schemas/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .tuning_hyperparameters import *
19 | from .auto_api import *
20 |
--------------------------------------------------------------------------------
/src/sparsify/schemas/tuning_hyperparameters.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Templates for tunable hyperparameters, to be tuned by the Neural Magic API
17 | """
18 | from typing import Any, List, Union
19 |
20 | from pydantic import BaseModel, Field, validator
21 | from pydantic.typing import Literal
22 |
23 |
24 | __all__ = [
25 | "BaseParameterDistribution",
26 | "SampledHyperparameter",
27 | "NumericParameterDistribution",
28 | "CategoricalParameterDistribution",
29 | ]
30 |
31 |
32 | class BaseParameterDistribution(BaseModel):
33 | """
34 | Base class for a hyperparameter distribution, which can be sampled and tuned via
35 | Neural Magic API calls
36 | """
37 |
38 | value_type: Literal["float", "int", "categorical"] = Field(
39 | description="Value type, from float, int, or categorical"
40 | )
41 |
42 |
43 | class NumericParameterDistribution(BaseParameterDistribution):
44 | """
45 | Numeric distribution which can represent a float or integer hyperparameter
46 | distribution
47 | """
48 |
49 | low: Union[float, int] = Field(
50 | description="Minimum of value range",
51 | )
52 | high: Union[float, int] = Field(
53 | description="Maximum of value range",
54 | )
55 | step: Union[float, int, None] = Field(
56 | description=("Option size of the discretization step"),
57 | default=None,
58 | )
59 | log: bool = Field(
60 | description="When true, parameter is sampled on a log scale",
61 | default=False,
62 | )
63 |
64 | @validator("value_type")
65 | def valid_type(cls, value_type):
66 | valid_types = ["float", "int"]
67 | if value_type not in valid_types:
68 | raise ValueError(
69 | f"type for must be one of {valid_types} for class {cls}. Received "
70 | f"{value_type}"
71 | )
72 | return value_type
73 |
74 |
75 | class CategoricalParameterDistribution(BaseParameterDistribution):
76 | """
77 | Categorical distribution constructed from pre-defined, non-ordered values
78 | """
79 |
80 | choices: List[Union[None, bool, int, float, str]] = Field(
81 | description="Set of possible value choices to sample from"
82 | )
83 |
84 | @validator("value_type")
85 | def valid_type(cls, value_type):
86 | valid_types = ["categorical"]
87 | if value_type not in valid_types:
88 | raise ValueError(
89 | f"type for must be one of {valid_types} for class {cls}. Received "
90 | f"{value_type}"
91 | )
92 | return value_type
93 |
94 |
95 | class SampledHyperparameter(BaseModel):
96 | """
97 | Represents an instance of a sampled hyperparameter, with distribution and value
98 | """
99 |
100 | name: str = Field(description="Parameter name")
101 | source: str = Field(
102 | description=(
103 | "Source of where the parameter is controlled. Either 'recipe' or 'CLI', "
104 | "case insensitive."
105 | )
106 | )
107 | distribution: Union[
108 | NumericParameterDistribution, CategoricalParameterDistribution
109 | ] = Field(description="Distribution from which the parameter may be sampled")
110 | value: Any = Field(description="Samples parameter value")
111 |
112 | @validator("source")
113 | def lowercase_source(cls, source):
114 | if source.lower() not in ["recipe", "cli"]:
115 | raise ValueError(
116 | f"Source must be 'recipe' or 'cli', case-insensitive. Got {source}"
117 | )
118 | return source.lower()
119 |
--------------------------------------------------------------------------------
/src/sparsify/utils/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 | # isort: skip_file
17 |
18 | from .task_name import *
19 | from .constants import *
20 | from .nm_api import *
21 | from .helpers import *
22 |
--------------------------------------------------------------------------------
/src/sparsify/utils/exceptions.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | class InvalidAPIKey(Exception):
17 | """The API key was invalid"""
18 |
19 |
20 | class SparsifyLoginRequired(Exception):
21 | """Run `sparsify.login`"""
22 |
--------------------------------------------------------------------------------
/src/sparsify/utils/helpers.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | import json
17 | import logging
18 | from pathlib import Path
19 |
20 | import requests
21 |
22 | from sparsify.utils.exceptions import InvalidAPIKey
23 |
24 |
25 | __all__ = [
26 | "credentials_exists",
27 | "get_access_token",
28 | "get_authenticated_pypi_url",
29 | "get_sparsify_credentials_path",
30 | "get_token_url",
31 | "overwrite_credentials",
32 | "set_log_level",
33 | "strtobool",
34 | ]
35 | _MAP = {
36 | "y": True,
37 | "yes": True,
38 | "t": True,
39 | "true": True,
40 | "on": True,
41 | "1": True,
42 | "n": False,
43 | "no": False,
44 | "f": False,
45 | "false": False,
46 | "off": False,
47 | "0": False,
48 | }
49 |
50 | _LOGGER = logging.getLogger(__name__)
51 |
52 |
53 | def strtobool(value):
54 | """
55 | Emulation of distutils.strtobool since it is deprecated and will be removed
56 | by Python3.12
57 |
58 | :param value: a str convertible value to be converted to bool
59 | :returns: a bool representation of the value
60 | """
61 | try:
62 | return _MAP[str(value).lower()]
63 | except KeyError:
64 | raise ValueError('"{}" is not a valid bool value'.format(value))
65 |
66 |
67 | def get_token_url():
68 | """
69 | :return: The url to use for getting an access token
70 | """
71 | return "https://accounts.neuralmagic.com/v1/connect/token"
72 |
73 |
74 | def get_sparsify_credentials_path() -> Path:
75 | """
76 | :return: The path to the neuralmagic credentials file
77 | """
78 | return Path.home().joinpath(".config", "neuralmagic", "credentials.json")
79 |
80 |
81 | def credentials_exists() -> bool:
82 | """
83 | :return: True if the credentials file exists, False otherwise
84 | """
85 | return get_sparsify_credentials_path().exists()
86 |
87 |
88 | def overwrite_credentials(api_key: str) -> None:
89 | """
90 | Overwrite the credentials file with the given api key
91 | or create a new file if it does not exist
92 |
93 | :param api_key: The api key to write to the credentials file
94 | """
95 | credentials_path = get_sparsify_credentials_path()
96 | credentials_path.parent.mkdir(parents=True, exist_ok=True)
97 | credentials = {"api_key": api_key}
98 |
99 | with credentials_path.open("w") as fp:
100 | json.dump(credentials, fp)
101 |
102 |
103 | def get_access_token(api_key: str) -> str:
104 | """
105 | Get the access token for the given api key
106 |
107 | :param api_key: The api key to use for authentication
108 | :return: The requested access token
109 | """
110 | response = requests.post(
111 | get_token_url(),
112 | data={
113 | "grant_type": "password",
114 | "username": "api-key",
115 | "client_id": "ee910196-cd8a-11ed-b74d-bb563cd16e9d",
116 | "password": api_key,
117 | "scope": "pypi:read",
118 | },
119 | )
120 |
121 | try:
122 | response.raise_for_status()
123 | except requests.HTTPError as http_error:
124 | error_message = (
125 | "Sorry, we were unable to authenticate your Neural Magic Account API key. "
126 | "If you believe this is a mistake, contact support@neuralmagic.com "
127 | "to help remedy this issue."
128 | )
129 | raise InvalidAPIKey(error_message) from http_error
130 |
131 | if response.status_code != 200:
132 | raise ValueError(f"Unknown response code {response.status_code}")
133 |
134 | _LOGGER.info("Successfully authenticated with Neural Magic Account API key")
135 | return response.json()["access_token"]
136 |
137 |
138 | def get_authenticated_pypi_url(access_token: str) -> str:
139 | """
140 | Get the authenticated pypi url for the given access token
141 |
142 | :return: The authenticated pypi url
143 | """
144 | pypi_url_template = "https://nm:{}@pypi.neuralmagic.com"
145 | return pypi_url_template.format(access_token)
146 |
147 |
148 | def set_log_level(logger: logging.Logger, level: int) -> None:
149 | """
150 | Set the log level for the given logger and all of its handlers
151 |
152 | :param logger: The logger to set the level for
153 | :param level: The level to set the logger to
154 | """
155 | logging.basicConfig(level=level)
156 | for handler in logger.handlers:
157 | handler.setLevel(level=level)
158 |
--------------------------------------------------------------------------------
/src/sparsify/utils/nm_api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import os
16 |
17 |
18 | __all__ = ["get_base_url"]
19 | _DEFAULT_BASE_URL = "https://api.neuralmagic.com"
20 |
21 |
22 | def get_base_url() -> str:
23 | """
24 | :return The base url for sparsify backend server
25 | """
26 | return os.getenv("SPARSIFY_BACKEND_URL", default=_DEFAULT_BASE_URL)
27 |
--------------------------------------------------------------------------------
/src/sparsify/utils/system.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Utilities for ONNX based ML system info and validation
17 | """
18 |
19 | import socket
20 | from typing import Any, Dict, List, Union
21 |
22 | from pkg_resources import get_distribution
23 |
24 | import psutil
25 |
26 |
27 | try:
28 | import deepsparse
29 |
30 | deepsparse_import_err = None
31 | except Exception as deepsparse_err:
32 | deepsparse = None
33 | deepsparse_import_err = deepsparse_err
34 |
35 | try:
36 | import onnxruntime
37 |
38 | ort_import_err = None
39 | except Exception as ort_err:
40 | onnxruntime = None
41 | ort_import_err = ort_err
42 |
43 |
44 | __all__ = [
45 | "available_ml_engines",
46 | "ml_engines_errors",
47 | "get_ml_sys_info",
48 | ]
49 |
50 |
51 | def available_ml_engines() -> List[str]:
52 | """
53 | :return: List of available inference providers on current system. Potential values
54 | include ['deepsparse', 'ort_cpu', 'ort_gpu']
55 | """
56 | # ORT availability
57 | engines = []
58 |
59 | if deepsparse is not None:
60 | engines.append("deepsparse")
61 |
62 | if onnxruntime is not None:
63 | ort_providers = onnxruntime.get_available_providers()
64 | if "CPUExecutionProvider" in ort_providers:
65 | engines.append("ort_cpu")
66 | if "CUDAExecutionProvider" in ort_providers:
67 | engines.append("ort_gpu")
68 |
69 | return engines
70 |
71 |
72 | def ml_engines_errors() -> Dict[str, Exception]:
73 | """
74 | :return: a dictionary containing any errors encountered when importing ML engines
75 | on the current system
76 | """
77 | return {
78 | "deepsparse": deepsparse_import_err,
79 | "onnxruntime": ort_import_err,
80 | }
81 |
82 |
83 | def _get_package_version(package: str) -> Union[None, str]:
84 | try:
85 | return get_distribution(package).version
86 | except Exception:
87 | return None
88 |
89 |
90 | def get_ml_version_info() -> Dict[str, str]:
91 | """
92 | :return: a dictionary containing the version information of sparseml,
93 | deepsparse, onnxruntime, and onnx if installed.
94 | """
95 | sparseml_version = _get_package_version("sparseml") or _get_package_version(
96 | "sparseml-nightly"
97 | )
98 | deepsparse_version = _get_package_version("deepsparse") or _get_package_version(
99 | "deepsparse-nightly"
100 | )
101 | onnxruntime_version = _get_package_version("onnxruntime")
102 | onnx_version = _get_package_version("onnx")
103 |
104 | return {
105 | "sparseml": sparseml_version,
106 | "onnxruntime": onnxruntime_version,
107 | "deepsparse": deepsparse_version,
108 | "onnx": onnx_version,
109 | }
110 |
111 |
112 | def get_ml_sys_info() -> Dict[str, Any]:
113 | """
114 | :return: a dictionary containing info for the system and ML engines on the system.
115 | Such as number of cores, instruction sets, available engines, etc
116 | """
117 | sys_info = {
118 | "available_engines": available_ml_engines(),
119 | "ip_address": None,
120 | "version_info": get_ml_version_info(),
121 | }
122 |
123 | try:
124 | # sometimes this isn't available, wrap to protect and not fail
125 | sys_info["ip_address"] = socket.gethostbyname(socket.gethostname())
126 | except Exception:
127 | pass
128 |
129 | # get sys info from deepsparse.cpu
130 | if deepsparse is not None:
131 | deepsparse_info = deepsparse.cpu.cpu_architecture()
132 | deepsparse_info = {
133 | k.lower(): v for k, v in deepsparse_info.items()
134 | } # standardize case
135 | sys_info.update(deepsparse_info) # add to main dict
136 |
137 | sys_info["available_instructions"] = (
138 | deepsparse_info["isa"]
139 | if isinstance(deepsparse_info["isa"], list)
140 | else [deepsparse_info["isa"]]
141 | )
142 | sys_info["available_instructions"] = [
143 | ins.upper() for ins in sys_info["available_instructions"]
144 | ]
145 | else:
146 | sys_info["cores_per_socket"] = psutil.cpu_count(logical=False)
147 |
148 | return sys_info
149 |
--------------------------------------------------------------------------------
/src/sparsify/utils/task_name.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from typing import List, Optional, Union
16 |
17 |
18 | class TaskName:
19 | """
20 | Immutable class for streamlined handling of task names and their aliases
21 |
22 | Behavior:
23 | - Current automatic explicit aliases include swapping of the `-` and `_` characters.
24 | - Current automatic implicit aliases include any case-changes to the task name.
25 | i.e. task name is case-insensitive.
26 | - When an additional alias is added to a task name, all of its automatic aliases
27 | are added as well.
28 | - Class display and hashing are done on the `.name` string using the default
29 | string behavior.
30 | - Class comparison checks that the target object (str or TaskName) is an alias of
31 | this TaskName. e.g. TaskName("yolov5") == "YOLOv5" -> True.
32 | """
33 |
34 | __slots__ = ("name", "aliases", "domain", "sub_domain")
35 |
36 | def __init__(
37 | self,
38 | name: str,
39 | domain: str,
40 | sub_domain: str,
41 | aliases: Optional[List[str]] = None,
42 | ):
43 | aliases = aliases or []
44 | for field in [name, domain, sub_domain]:
45 | if not isinstance(field, str):
46 | raise ValueError(f"'{field}' must be a string")
47 |
48 | super(TaskName, self).__setattr__("name", name.lower().replace("-", "_"))
49 | super(TaskName, self).__setattr__("aliases", self._get_supported_aliases(name))
50 | super(TaskName, self).__setattr__("domain", domain)
51 | super(TaskName, self).__setattr__("sub_domain", sub_domain)
52 |
53 | for alias in aliases:
54 | if not isinstance(alias, str):
55 | raise ValueError("'alias' must be a string")
56 | self._add_alias(alias)
57 |
58 | def __setattr__(self, name, value):
59 | """
60 | Prevent modification of attributes
61 | """
62 | raise AttributeError("TaskName cannot be modified")
63 |
64 | def __repr__(self):
65 | return f"TaskName({self.pretty_print()})"
66 |
67 | def __str__(self):
68 | return self.name
69 |
70 | def pretty_print(self):
71 | return (
72 | f'name="{self.name}", domain="{self.domain}", '
73 | f'sub_domain="{self.sub_domain}", aliases={self.aliases}',
74 | )
75 |
76 | def __eq__(self, other: Union[str, "TaskName"]):
77 | if isinstance(other, TaskName):
78 | return _task_name_eq(self.name, other.name)
79 | elif isinstance(other, str):
80 | return any(_task_name_eq(other, alias) for alias in self.aliases)
81 | else:
82 | return False
83 |
84 | def __hash__(self):
85 | return hash(self.name)
86 |
87 | def _add_alias(self, alias):
88 | self.aliases.extend(self._get_supported_aliases(alias))
89 |
90 | def _get_supported_aliases(self, task: str):
91 | task = task.lower().replace("-", "_")
92 | aliases = [task]
93 | if "_" in task:
94 | aliases.append(task.replace("_", "-"))
95 | return aliases
96 |
97 |
98 | def _task_name_eq(str_1: str, str_2: str) -> bool:
99 | # ignore case, ignore separators (-, _, ' ')
100 | def _strip_separators(string):
101 | return string.lower().replace(" ", "").replace("-", "").replace("_", "")
102 |
103 | return _strip_separators(str_1) == _strip_separators(str_2)
104 |
--------------------------------------------------------------------------------
/src/sparsify/version.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """
16 | Functionality for storing and setting the version info for Sparsify
17 | """
18 |
19 | from datetime import date
20 |
21 |
22 | version_base = "1.7.0"
23 | is_release = False # change to True to set the generated version as a release version
24 |
25 |
26 | def _generate_version():
27 | return (
28 | version_base
29 | if is_release
30 | else f"{version_base}.{date.today().strftime('%Y%m%d')}"
31 | )
32 |
33 |
34 | __all__ = [
35 | "__version__",
36 | "version_base",
37 | "is_release",
38 | "version",
39 | "version_major",
40 | "version_minor",
41 | "version_bug",
42 | "version_major_minor",
43 | ]
44 | __version__ = _generate_version()
45 |
46 | version = __version__
47 | version_major, version_minor, version_bug, version_build = version.split(".") + (
48 | [None] if len(version.split(".")) < 4 else []
49 | ) # handle conditional for version being 3 parts or 4 (4 containing build date)
50 | version_major_minor = f"{version_major}.{version_minor}"
51 |
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuralmagic/sparsify/1dfa3e8e3598f91aa01514dbb8486dd03e429e49/tests/.gitkeep
--------------------------------------------------------------------------------
/tests/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
--------------------------------------------------------------------------------
/tests/sparsify/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | # flake8: noqa
16 |
--------------------------------------------------------------------------------
/tests/sparsify/auto/test_main.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | import importlib
15 | import os
16 | from contextlib import suppress
17 | from pathlib import Path
18 | from tempfile import TemporaryDirectory
19 |
20 | import pytest
21 |
22 |
23 | with suppress(ModuleNotFoundError):
24 | from sparsify.schemas import Metrics
25 |
26 | _SPARSIFYML_INSTALLED: bool = importlib.util.find_spec("sparsifyml") is not None
27 |
28 | """
29 | Tests for sparsify.auto main process logic, with emphasis on on file creation, saving,
30 | and tracking flow
31 | """
32 |
33 | # Run definition
34 | _TEMPORARY_DIRECTORY = TemporaryDirectory()
35 | _SAVE_DIRECTORY = _TEMPORARY_DIRECTORY.name
36 | _NUM_TRIALS = 10
37 | _MAXIMUM_SAVES = 3
38 | _TEST_CONFIG = {
39 | "task": "object_detection",
40 | "dataset": "coco128.yaml",
41 | "base_model": "zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94", # noqa E501
42 | "recipe": "zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94", # noqa E501
43 | "save_directory": _SAVE_DIRECTORY,
44 | "num_trials": _NUM_TRIALS,
45 | "maximum_trial_saves": _MAXIMUM_SAVES,
46 | }
47 | _TEST_TEACHER_CONFIG = None
48 | _ACCURACY_LIST = [0.1, 0.9, 0.3, 0.4, 0.5, 0.6, 0.5, 0.8, 0.4, 0.3]
49 | _METRICS_LIST = [
50 | Metrics(metrics={"map0.5": acc}, objective_key="map0.5") for acc in _ACCURACY_LIST
51 | ]
52 | _RUNNER_MOCK_PATH = "sparsify.auto.tasks.object_detection.yolov5.runner.Yolov5Runner"
53 |
54 | # Testing helper function
55 |
56 |
57 | def _train_side_effect_mock(self):
58 | # add a dummy file and sub_directory
59 | output_directory = self._get_model_artifact_directory()
60 | os.makedirs(output_directory)
61 | Path(os.path.join(output_directory, "output.txt")).touch()
62 | os.mkdir(os.path.join(output_directory, "artifact_subdirectory"))
63 |
64 |
65 | def _export_side_effect_mock(self, model_directory):
66 | _test_trial_artifact_directory(model_directory)
67 | deployment_path = os.path.join(model_directory, "deployment")
68 | os.makedirs(deployment_path) # create deployment dir
69 | # add dummy onnx file
70 | Path(os.path.join(deployment_path, "export_output.onnx")).touch()
71 |
72 |
73 | @pytest.fixture(autouse=True)
74 | def _cleanup_directory():
75 | yield
76 | _TEMPORARY_DIRECTORY.cleanup()
77 | assert not os.path.exists(_SAVE_DIRECTORY)
78 |
79 |
80 | def _test_trial_artifact_directory(directory_path: str) -> bool:
81 | assert os.path.exists(directory_path), f"Directory not found: {directory_path}"
82 | assert os.listdir(directory_path), f"Directory empty: {directory_path}"
83 |
--------------------------------------------------------------------------------
/tests/sparsify/auto/test_registered_tasks.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | import ast
15 | from contextlib import suppress
16 |
17 | import pytest
18 | import requests
19 | from requests.exceptions import ConnectionError
20 |
21 |
22 | with suppress(ModuleNotFoundError):
23 | from sparsify.auto.tasks import TaskRunner
24 | from sparsify.utils import TASK_REGISTRY, get_base_url
25 |
26 |
27 | def _is_service_online():
28 | try:
29 | response = requests.get(get_base_url())
30 | if not response.ok:
31 | return False
32 | return True
33 | except ConnectionError:
34 | return False
35 |
36 |
37 | @pytest.mark.skipif(not _is_service_online(), reason="NM API service not online")
38 | def test_task_names_runner_and_config():
39 | # Test that the config creator and task runner have the same tasks registered
40 | response = requests.get(
41 | f"{get_base_url()}/v1/sparsify/auto/training-config/supported-tasks",
42 | )
43 | assert response.status_code
44 |
45 | assert [char in response.text for char in ["[]"]]
46 | config_tasks = [TASK_REGISTRY[task] for task in ast.literal_eval(response.text)]
47 |
48 | runner_tasks = TaskRunner.supported_tasks()
49 | assert set(config_tasks) == set(runner_tasks)
50 |
51 | # Test that the config creator and task runner recognize the same task name aliases
52 | config_task_aliases = {task.name: task.aliases for task in config_tasks}
53 | runner_task_aliases = TaskRunner.supported_task_aliases()
54 |
55 | for task, config_aliases in config_task_aliases.items():
56 | assert set(config_aliases) == set(runner_task_aliases[task])
57 |
--------------------------------------------------------------------------------
/tests/sparsify/create/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
--------------------------------------------------------------------------------
/tests/sparsify/create/test_api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import torch
16 | from torch.nn import Module
17 |
18 | from sparseml.pytorch.models import mobilenet
19 | from sparsify.create import initialize
20 |
21 |
22 | def _module_is_quantized(module: Module) -> bool:
23 | return any(
24 | submodule.__class__.__name__ == "FakeQuantize" for submodule in module.modules()
25 | )
26 |
27 |
28 | def _module_sparsity(module: Module):
29 | total_params = 0.0
30 | total_nonzero = 0.0
31 |
32 | for name, parameter in module.named_parameters():
33 | if ".weight" not in name or "conv" not in name:
34 | continue
35 | total_params += parameter.data.numel()
36 | total_nonzero += parameter.data.count_nonzero()
37 |
38 | if total_params == 0:
39 | return 0
40 |
41 | return 1 - (total_nonzero / total_params)
42 |
43 |
44 | def test_create_initialize_and_recipe_apply_mobilenet():
45 | model = mobilenet(pretrained=True)
46 | optim = torch.optim.SGD(model.parameters(), lr=0.1)
47 |
48 | # sanity check
49 | assert not _module_is_quantized(model)
50 | assert _module_sparsity(model) < 0.01
51 |
52 | manager, optim = initialize(model, optim, 4)
53 | # check manager content
54 | assert manager.pruning_modifiers
55 | assert manager.quantization_modifiers
56 | assert not manager.distillation_modifiers
57 | assert optim.__class__.__name__ == "RecipeManagerStepWrapper"
58 |
59 | # emulate entire range of training steps
60 | for _ in range(20 * 4 + 1):
61 | optim.step()
62 |
63 | # check expected structure post training
64 | assert _module_is_quantized(model)
65 | assert abs(_module_sparsity(model) - 0.8) < 0.05
66 |
--------------------------------------------------------------------------------
/tests/sparsify/schemas/test_api.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import pytest
16 |
17 | from sparsify.schemas import APIArgs
18 |
19 |
20 | @pytest.mark.parametrize(
21 | "target_api_args",
22 | [
23 | APIArgs(task="question-answering", dataset="squad"),
24 | APIArgs(
25 | task="image-classification",
26 | dataset="imagenette",
27 | base_model="zoo:resnet50",
28 | recipe="zoo:resnet50-pruned_quant",
29 | recipe_args=dict(target_sparsity=0.8),
30 | distill_teacher="zoo:resnet50",
31 | kwargs=dict(model_tag="resnet50-imagenette"),
32 | ),
33 | ],
34 | )
35 | def test_api_args_from_cli(target_api_args):
36 | api_fields = APIArgs.__fields__
37 |
38 | # build cli args
39 | cli_args = []
40 | for field_name, field in api_fields.items():
41 | target_val = getattr(target_api_args, field_name)
42 | default_val = (
43 | field.default_factory() if field.default_factory else field.default
44 | )
45 |
46 | if target_val == default_val:
47 | # unfilled default
48 | continue
49 |
50 | if isinstance(target_val, dict):
51 | target_val = str(target_val)
52 | cli_args.append(f"--{field_name}")
53 | cli_args.append(target_val)
54 |
55 | parsed_api_args = APIArgs.from_cli(args=cli_args)
56 |
57 | for field_name in api_fields.keys():
58 | target_val = getattr(target_api_args, field_name)
59 | parsed_val = getattr(parsed_api_args, field_name)
60 | assert isinstance(parsed_val, type(target_val))
61 | assert target_val == parsed_val
62 |
--------------------------------------------------------------------------------
/tests/sparsify/test_version.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | from sparsify.version import (
16 | __version__,
17 | version,
18 | version_bug,
19 | version_major,
20 | version_major_minor,
21 | version_minor,
22 | )
23 |
24 |
25 | def test_version():
26 | assert __version__
27 | assert version
28 | assert version_major == version.split(".")[0]
29 | assert version_minor == version.split(".")[1]
30 | assert version_bug == version.split(".")[2]
31 | assert version_major_minor == f"{version_major}.{version_minor}"
32 |
--------------------------------------------------------------------------------
/tests/sparsify/utils/test_constants.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 |
16 | from sparsify.utils.constants import TaskName, get_dataset_info, get_task_info
17 |
18 |
19 | _IC_TASK = TaskName(
20 | name="image_classification",
21 | aliases=["ic", "classification"],
22 | domain="cv",
23 | sub_domain="classification",
24 | )
25 |
26 | _TEXT_CLASSIFICATION_TASK = TaskName(
27 | name="text_classification",
28 | aliases=["glue"],
29 | domain="nlp",
30 | sub_domain="text_classification",
31 | )
32 |
33 |
34 | def test_get_task_info():
35 | assert get_task_info("ic") == _IC_TASK
36 | assert get_task_info("image_Classification") == _IC_TASK
37 | assert get_task_info("classification") == _IC_TASK
38 | assert get_task_info(None) is None
39 |
40 |
41 | def test_get_dataset_info():
42 | assert get_dataset_info("mnli") == _TEXT_CLASSIFICATION_TASK
43 | assert get_dataset_info("MnLi") == _TEXT_CLASSIFICATION_TASK
44 | assert get_dataset_info(" mNli ") == _TEXT_CLASSIFICATION_TASK
45 | assert get_dataset_info(None) is None
46 |
--------------------------------------------------------------------------------
/tests/sparsify/utils/test_task_name.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing,
10 | # software distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | import pytest
16 |
17 | from sparsify.utils import TASK_REGISTRY, TaskName
18 |
19 |
20 | @pytest.mark.parametrize(
21 | "name, aliases, domain, sub_domain, test_aliases, test_not_aliases",
22 | [
23 | (
24 | "image_classification",
25 | ["ic", "classification"],
26 | "cv",
27 | "classification",
28 | ["Image-Classification", "ic", "IC", "classification", "CLASSIFICATION"],
29 | ["cv"],
30 | ),
31 | (
32 | "object_detection",
33 | ["od", "detection"],
34 | "cv",
35 | "detection",
36 | [
37 | "Object-Detection",
38 | "od",
39 | "OD",
40 | "detection",
41 | "DETECTION",
42 | ],
43 | ["yolo"],
44 | ),
45 | (
46 | "segmentation",
47 | [],
48 | "cv",
49 | "segmentation",
50 | [
51 | "Segmentation",
52 | "SEGMENTATION",
53 | ],
54 | ["object-segmentation"],
55 | ),
56 | (
57 | "question_answering",
58 | ["qa"],
59 | "nlp",
60 | "question_answering",
61 | [
62 | "Question-Answering",
63 | "qa",
64 | "QA",
65 | ],
66 | ["questionanswer"],
67 | ),
68 | (
69 | "text_classification",
70 | ["glue"],
71 | "nlp",
72 | "text_classification",
73 | [
74 | "Text-Classification",
75 | "glue",
76 | "GLUE",
77 | ],
78 | ["text"],
79 | ),
80 | (
81 | "sentiment_analysis",
82 | ["sentiment"],
83 | "nlp",
84 | "sentiment_analysis",
85 | [
86 | "Sentiment-Analysis",
87 | "sentiment",
88 | "SENTIMENT",
89 | ],
90 | ["imdb"],
91 | ),
92 | (
93 | "token_classification",
94 | ["ner", "named_entity_recognition"],
95 | "nlp",
96 | "token_classification",
97 | ["Token-Classification", "ner", "NER", "Named-Entity-Recognition"],
98 | ["token"],
99 | ),
100 | ],
101 | )
102 | class TestTaskName:
103 | def test_task_create(
104 | self,
105 | name,
106 | aliases,
107 | domain,
108 | sub_domain,
109 | test_aliases,
110 | test_not_aliases,
111 | ):
112 | task = TaskName(name, domain, sub_domain, aliases)
113 |
114 | assert task == name
115 | assert task.domain == domain
116 | assert task.sub_domain == sub_domain
117 | assert TASK_REGISTRY[name] == task
118 |
119 | for alias in aliases + test_aliases:
120 | assert alias == task
121 |
122 | for alias in test_not_aliases:
123 | assert alias != task
124 |
125 | def test_task_add_alias(
126 | self,
127 | name,
128 | aliases,
129 | domain,
130 | sub_domain,
131 | test_aliases,
132 | test_not_aliases,
133 | ):
134 | task = TaskName(name, domain, sub_domain, aliases)
135 |
136 | for alias in test_not_aliases:
137 | task._add_alias(alias)
138 | assert alias == task
139 | assert alias.upper() == task
140 | assert alias.lower() == task
141 | assert alias.replace("_", "-") == task
142 | assert alias.replace("-", "_") == task
143 |
144 | for alias in aliases + test_aliases:
145 | assert alias == task
146 |
147 | def test_task_immutability(
148 | self,
149 | name,
150 | aliases,
151 | domain,
152 | sub_domain,
153 | test_aliases,
154 | test_not_aliases,
155 | ):
156 | task = TaskName(name, domain, sub_domain, aliases)
157 |
158 | for field in ["name", "domain", "sub_domain", "aliases"]:
159 | try:
160 | setattr(task, field, "")
161 | pytest.fail(
162 | f"TaskName is not immutable. '{field}' was successfully modified"
163 | )
164 | except AttributeError:
165 | continue
166 |
--------------------------------------------------------------------------------