Upload folder using huggingface_hub (part 5)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +27 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/INSTALLER +1 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/METADATA +288 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/RECORD +70 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/REQUESTED +0 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/WHEEL +5 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/licenses/LICENSE +21 -0
- venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/top_level.txt +1 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/__init__.py +22 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/adagrad.py +187 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/adam.py +346 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/adamw.py +337 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/ademamix.py +410 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/lamb.py +190 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/lars.py +259 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/lion.py +266 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/optimizer.py +756 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/rmsprop.py +170 -0
- venv/lib/python3.11/site-packages/bitsandbytes/optim/sgd.py +152 -0
- venv/lib/python3.11/site-packages/bitsandbytes/py.typed +0 -0
- venv/lib/python3.11/site-packages/bitsandbytes/utils.py +208 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/INSTALLER +1 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/METADATA +158 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/RECORD +9 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/WHEEL +6 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/licenses/LICENSE +19 -0
- venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/top_level.txt +2 -0
- venv/lib/python3.11/site-packages/brotli.py +57 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/INSTALLER +1 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/METADATA +78 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/RECORD +18 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/WHEEL +5 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/licenses/LICENSE +20 -0
- venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/top_level.txt +1 -0
- venv/lib/python3.11/site-packages/certifi/__init__.py +4 -0
- venv/lib/python3.11/site-packages/certifi/__main__.py +12 -0
- venv/lib/python3.11/site-packages/certifi/cacert.pem +0 -0
- venv/lib/python3.11/site-packages/certifi/core.py +83 -0
- venv/lib/python3.11/site-packages/certifi/py.typed +0 -0
- venv/lib/python3.11/site-packages/certifi/tests/__init__.py +0 -0
- venv/lib/python3.11/site-packages/certifi/tests/test_certify.py +18 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/INSTALLER +1 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/METADATA +827 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/RECORD +36 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/WHEEL +7 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/entry_points.txt +2 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/licenses/LICENSE +21 -0
- venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/top_level.txt +2 -0
- venv/lib/python3.11/site-packages/charset_normalizer/__init__.py +48 -0
- venv/lib/python3.11/site-packages/charset_normalizer/__main__.py +6 -0
.gitattributes
CHANGED
|
@@ -143,3 +143,30 @@ venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_rocm714.so filter
|
|
| 143 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_rocm72.so filter=lfs diff=lfs merge=lfs -text
|
| 144 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_xpu2025.so filter=lfs diff=lfs merge=lfs -text
|
| 145 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_xpu2026.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_rocm72.so filter=lfs diff=lfs merge=lfs -text
|
| 144 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_xpu2025.so filter=lfs diff=lfs merge=lfs -text
|
| 145 |
venv/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_xpu2026.so filter=lfs diff=lfs merge=lfs -text
|
| 146 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_bindings/cydriver.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 147 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_bindings/cyruntime.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 148 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_bindings/cyruntime_ptds.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 149 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_internal/cufile.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 150 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_internal/nvml.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 151 |
+
venv/lib/python3.11/site-packages/cuda/bindings/_internal/utils.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 152 |
+
venv/lib/python3.11/site-packages/cuda/bindings/cudla.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 153 |
+
venv/lib/python3.11/site-packages/cuda/bindings/cufile.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 154 |
+
venv/lib/python3.11/site-packages/cuda/bindings/cydriver.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 155 |
+
venv/lib/python3.11/site-packages/cuda/bindings/cynvml.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 156 |
+
venv/lib/python3.11/site-packages/cuda/bindings/cyruntime.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 157 |
+
venv/lib/python3.11/site-packages/cuda/bindings/driver.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 158 |
+
venv/lib/python3.11/site-packages/cuda/bindings/nvfatbin.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 159 |
+
venv/lib/python3.11/site-packages/cuda/bindings/nvjitlink.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 160 |
+
venv/lib/python3.11/site-packages/cuda/bindings/nvml.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 161 |
+
venv/lib/python3.11/site-packages/cuda/bindings/nvrtc.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 162 |
+
venv/lib/python3.11/site-packages/cuda/bindings/runtime.cpython-311-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 163 |
+
venv/lib/python3.11/site-packages/cv2/cv2.abi3.so filter=lfs diff=lfs merge=lfs -text
|
| 164 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSans-Bold.ttf filter=lfs diff=lfs merge=lfs -text
|
| 165 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSans-BoldOblique.ttf filter=lfs diff=lfs merge=lfs -text
|
| 166 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSans-ExtraLight.ttf filter=lfs diff=lfs merge=lfs -text
|
| 167 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSans-Oblique.ttf filter=lfs diff=lfs merge=lfs -text
|
| 168 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSans.ttf filter=lfs diff=lfs merge=lfs -text
|
| 169 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSansCondensed-Bold.ttf filter=lfs diff=lfs merge=lfs -text
|
| 170 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSansCondensed-BoldOblique.ttf filter=lfs diff=lfs merge=lfs -text
|
| 171 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSansCondensed-Oblique.ttf filter=lfs diff=lfs merge=lfs -text
|
| 172 |
+
venv/lib/python3.11/site-packages/cv2/qt/fonts/DejaVuSansCondensed.ttf filter=lfs diff=lfs merge=lfs -text
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
uv
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/METADATA
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: bitsandbytes
|
| 3 |
+
Version: 0.50.0
|
| 4 |
+
Summary: k-bit optimizers and matrix multiplication routines.
|
| 5 |
+
Author-email: Tim Dettmers <dettmers@cs.washington.edu>
|
| 6 |
+
Maintainer-email: Titus von Köller <titus@huggingface.co>, Matthew Douglas <matthew.douglas@huggingface.co>
|
| 7 |
+
License-Expression: MIT
|
| 8 |
+
Project-URL: homepage, https://github.com/bitsandbytes-foundation/bitsandbytes
|
| 9 |
+
Project-URL: changelog, https://github.com/bitsandbytes-foundation/bitsandbytes/blob/main/CHANGELOG.md
|
| 10 |
+
Project-URL: docs, https://huggingface.co/docs/bitsandbytes/main
|
| 11 |
+
Project-URL: issues, https://github.com/bitsandbytes-foundation/bitsandbytes/issues
|
| 12 |
+
Keywords: gpu,optimizers,optimization,8-bit,quantization,compression
|
| 13 |
+
Classifier: Development Status :: 4 - Beta
|
| 14 |
+
Classifier: Environment :: GPU :: NVIDIA CUDA :: 11.8
|
| 15 |
+
Classifier: Environment :: GPU :: NVIDIA CUDA :: 12
|
| 16 |
+
Classifier: Environment :: GPU :: NVIDIA CUDA :: 13
|
| 17 |
+
Classifier: Intended Audience :: Developers
|
| 18 |
+
Classifier: Intended Audience :: Science/Research
|
| 19 |
+
Classifier: Operating System :: POSIX :: Linux
|
| 20 |
+
Classifier: Operating System :: MacOS
|
| 21 |
+
Classifier: Operating System :: Microsoft :: Windows
|
| 22 |
+
Classifier: Programming Language :: C++
|
| 23 |
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
| 24 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 25 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 26 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 27 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 28 |
+
Classifier: Programming Language :: Python :: 3.14
|
| 29 |
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
| 30 |
+
Requires-Python: >=3.10
|
| 31 |
+
Description-Content-Type: text/markdown
|
| 32 |
+
License-File: LICENSE
|
| 33 |
+
Requires-Dist: torch<3,>=2.4
|
| 34 |
+
Requires-Dist: numpy>=1.17
|
| 35 |
+
Requires-Dist: packaging>=20.9
|
| 36 |
+
Provides-Extra: benchmark
|
| 37 |
+
Requires-Dist: pandas; extra == "benchmark"
|
| 38 |
+
Requires-Dist: matplotlib; extra == "benchmark"
|
| 39 |
+
Provides-Extra: docs
|
| 40 |
+
Requires-Dist: hf-doc-builder==0.5.0; extra == "docs"
|
| 41 |
+
Provides-Extra: dev
|
| 42 |
+
Requires-Dist: bitsandbytes[test]; extra == "dev"
|
| 43 |
+
Requires-Dist: build<2,>=1.0.0; extra == "dev"
|
| 44 |
+
Requires-Dist: ruff~=0.14.3; extra == "dev"
|
| 45 |
+
Requires-Dist: pre-commit<4,>=3.5.0; extra == "dev"
|
| 46 |
+
Requires-Dist: wheel<1,>=0.42; extra == "dev"
|
| 47 |
+
Provides-Extra: test
|
| 48 |
+
Requires-Dist: einops~=0.8.0; extra == "test"
|
| 49 |
+
Requires-Dist: lion-pytorch==0.2.3; extra == "test"
|
| 50 |
+
Requires-Dist: pytest~=8.3; extra == "test"
|
| 51 |
+
Requires-Dist: scipy<2,>=1.11.4; extra == "test"
|
| 52 |
+
Requires-Dist: transformers<5,>=4.30.1; extra == "test"
|
| 53 |
+
Dynamic: license-file
|
| 54 |
+
|
| 55 |
+
<p align="center"><img src="https://avatars.githubusercontent.com/u/175231607?s=200&v=4" alt=""></p>
|
| 56 |
+
<h1 align="center">bitsandbytes</h1>
|
| 57 |
+
<p align="center">
|
| 58 |
+
<a href="https://github.com/bitsandbytes-foundation/bitsandbytes/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/bitsandbytes-foundation/bitsandbytes.svg?color=blue"></a>
|
| 59 |
+
<a href="https://pepy.tech/project/bitsandbytes"><img alt="Downloads" src="https://static.pepy.tech/badge/bitsandbytes/month"></a>
|
| 60 |
+
<a href="https://github.com/bitsandbytes-foundation/bitsandbytes/actions/workflows/tests-nightly.yml"><img alt="Nightly Unit Tests" src="https://img.shields.io/github/actions/workflow/status/bitsandbytes-foundation/bitsandbytes/tests-nightly.yml?logo=github&label=Nightly%20Tests"></a>
|
| 61 |
+
<a href="https://github.com/bitsandbytes-foundation/bitsandbytes/releases"><img alt="GitHub Release" src="https://img.shields.io/github/v/release/bitsandbytes-foundation/bitsandbytes"></a>
|
| 62 |
+
<a href="https://pypi.org/project/bitsandbytes/"><img alt="PyPI - Python Version" src="https://img.shields.io/pypi/pyversions/bitsandbytes"></a>
|
| 63 |
+
</p>
|
| 64 |
+
|
| 65 |
+
`bitsandbytes` enables accessible large language models via k-bit quantization for PyTorch. We provide three main features for dramatically reducing memory consumption for inference and training:
|
| 66 |
+
|
| 67 |
+
* 8-bit optimizers uses block-wise quantization to maintain 32-bit performance at a small fraction of the memory cost.
|
| 68 |
+
* LLM.int8() or 8-bit quantization enables large language model inference with only half the required memory and without any performance degradation. This method is based on vector-wise quantization to quantize most features to 8-bits and separately treating outliers with 16-bit matrix multiplication.
|
| 69 |
+
* QLoRA or 4-bit quantization enables large language model training with several memory-saving techniques that don't compromise performance. This method quantizes a model to 4-bits and inserts a small set of trainable low-rank adaptation (LoRA) weights to allow training.
|
| 70 |
+
|
| 71 |
+
The library includes quantization primitives for 8-bit & 4-bit operations, through `bitsandbytes.nn.Linear8bitLt` and `bitsandbytes.nn.Linear4bit` and 8-bit optimizers through `bitsandbytes.optim` module.
|
| 72 |
+
|
| 73 |
+
## System Requirements
|
| 74 |
+
bitsandbytes has the following minimum requirements for all platforms:
|
| 75 |
+
|
| 76 |
+
* Python 3.10+
|
| 77 |
+
* [PyTorch](https://pytorch.org/get-started/locally/) 2.4+
|
| 78 |
+
* _Note: While we aim to provide wide backwards compatibility, we recommend using the latest version of PyTorch for the best experience._
|
| 79 |
+
|
| 80 |
+
#### Accelerator support:
|
| 81 |
+
|
| 82 |
+
<small>Note: this table reflects the status of the current development branch. For the latest stable release, see the
|
| 83 |
+
[document in the 0.49.2 tag](https://github.com/bitsandbytes-foundation/bitsandbytes/blob/0.49.2/README.md#accelerator-support).
|
| 84 |
+
</small>
|
| 85 |
+
|
| 86 |
+
##### Legend:
|
| 87 |
+
🚧 = Planned |
|
| 88 |
+
〰️ = Partially Supported |
|
| 89 |
+
✅ = Supported |
|
| 90 |
+
❌ = Not Supported
|
| 91 |
+
|
| 92 |
+
<table>
|
| 93 |
+
<thead>
|
| 94 |
+
<tr>
|
| 95 |
+
<th>Platform</th>
|
| 96 |
+
<th>Accelerator</th>
|
| 97 |
+
<th>Hardware Requirements</th>
|
| 98 |
+
<th>LLM.int8()</th>
|
| 99 |
+
<th>QLoRA 4-bit</th>
|
| 100 |
+
<th>8-bit Optimizers</th>
|
| 101 |
+
</tr>
|
| 102 |
+
</thead>
|
| 103 |
+
<tbody>
|
| 104 |
+
<tr>
|
| 105 |
+
<td colspan="6">🐧 <strong>Linux, glibc >= 2.24</strong></td>
|
| 106 |
+
</tr>
|
| 107 |
+
<tr>
|
| 108 |
+
<td align="right">x86-64</td>
|
| 109 |
+
<td>◻️ CPU</td>
|
| 110 |
+
<td>Minimum: AVX2<br>Optimized: AVX512F, AVX512BF16</td>
|
| 111 |
+
<td>✅</td>
|
| 112 |
+
<td>✅</td>
|
| 113 |
+
<td>✅</td>
|
| 114 |
+
</tr>
|
| 115 |
+
<tr>
|
| 116 |
+
<td></td>
|
| 117 |
+
<td>🟩 NVIDIA GPU <br><code>cuda</code></td>
|
| 118 |
+
<td>SM60+ minimum<br>SM75+ recommended</td>
|
| 119 |
+
<td>✅</td>
|
| 120 |
+
<td>✅</td>
|
| 121 |
+
<td>✅</td>
|
| 122 |
+
</tr>
|
| 123 |
+
<tr>
|
| 124 |
+
<td></td>
|
| 125 |
+
<td>🟥 AMD GPU <br><code>cuda</code></td>
|
| 126 |
+
<td>
|
| 127 |
+
CDNA: gfx908, gfx90a, gfx942, gfx950<br>
|
| 128 |
+
RDNA: gfx103X, gfx110X, gfx115X, gfx120X
|
| 129 |
+
</td>
|
| 130 |
+
<td>✅</td>
|
| 131 |
+
<td>✅</td>
|
| 132 |
+
<td>✅</td>
|
| 133 |
+
</tr>
|
| 134 |
+
<tr>
|
| 135 |
+
<td></td>
|
| 136 |
+
<td>🟦 Intel GPU <br><code>xpu</code></td>
|
| 137 |
+
<td>
|
| 138 |
+
Data Center GPU Max Series<br>
|
| 139 |
+
Arc A-Series (Alchemist)<br>
|
| 140 |
+
Arc B-Series (Battlemage)
|
| 141 |
+
</td>
|
| 142 |
+
<td>✅</td>
|
| 143 |
+
<td>✅</td>
|
| 144 |
+
<td>✅</td>
|
| 145 |
+
</tr>
|
| 146 |
+
<tr>
|
| 147 |
+
<td></td>
|
| 148 |
+
<td>🟪 Intel Gaudi <br><code>hpu</code></td>
|
| 149 |
+
<td>Gaudi2, Gaudi3</td>
|
| 150 |
+
<td>✅</td>
|
| 151 |
+
<td>〰️</td>
|
| 152 |
+
<td>❌</td>
|
| 153 |
+
</tr>
|
| 154 |
+
<tr>
|
| 155 |
+
<td align="right">aarch64</td>
|
| 156 |
+
<td>◻️ CPU</td>
|
| 157 |
+
<td></td>
|
| 158 |
+
<td>✅ *</td>
|
| 159 |
+
<td>✅</td>
|
| 160 |
+
<td>✅</td>
|
| 161 |
+
</tr>
|
| 162 |
+
<tr>
|
| 163 |
+
<td></td>
|
| 164 |
+
<td>🟩 NVIDIA GPU <br><code>cuda</code></td>
|
| 165 |
+
<td>SM75+</td>
|
| 166 |
+
<td>✅</td>
|
| 167 |
+
<td>✅</td>
|
| 168 |
+
<td>✅</td>
|
| 169 |
+
</tr>
|
| 170 |
+
<tr>
|
| 171 |
+
<td colspan="6">🪟 <strong>Windows 11 / Windows Server 2022+</strong></td>
|
| 172 |
+
</tr>
|
| 173 |
+
<tr>
|
| 174 |
+
<td align="right">x86-64</td>
|
| 175 |
+
<td>◻️ CPU</td>
|
| 176 |
+
<td>AVX2</td>
|
| 177 |
+
<td>✅</td>
|
| 178 |
+
<td>✅</td>
|
| 179 |
+
<td>✅</td>
|
| 180 |
+
</tr>
|
| 181 |
+
<tr>
|
| 182 |
+
<td></td>
|
| 183 |
+
<td>🟩 NVIDIA GPU <br><code>cuda</code></td>
|
| 184 |
+
<td>SM60+ minimum<br>SM75+ recommended</td>
|
| 185 |
+
<td>✅</td>
|
| 186 |
+
<td>✅</td>
|
| 187 |
+
<td>✅</td>
|
| 188 |
+
</tr>
|
| 189 |
+
<tr>
|
| 190 |
+
<td></td>
|
| 191 |
+
<td>🟥 AMD GPU <br><code>cuda</code></td>
|
| 192 |
+
<td>
|
| 193 |
+
RDNA: gfx103X, gfx110X, gfx115X, gfx120X
|
| 194 |
+
</td>
|
| 195 |
+
<td>✅</td>
|
| 196 |
+
<td>✅</td>
|
| 197 |
+
<td>✅</td>
|
| 198 |
+
</tr>
|
| 199 |
+
<tr>
|
| 200 |
+
<td></td>
|
| 201 |
+
<td>🟦 Intel GPU <br><code>xpu</code></td>
|
| 202 |
+
<td>
|
| 203 |
+
Arc A-Series (Alchemist) <br>
|
| 204 |
+
Arc B-Series (Battlemage)
|
| 205 |
+
</td>
|
| 206 |
+
<td>✅</td>
|
| 207 |
+
<td>✅</td>
|
| 208 |
+
<td>✅</td>
|
| 209 |
+
</tr>
|
| 210 |
+
<tr>
|
| 211 |
+
<td align="right">arm64</td>
|
| 212 |
+
<td>◻️ CPU</td>
|
| 213 |
+
<td></td>
|
| 214 |
+
<td>✅</td>
|
| 215 |
+
<td>✅</td>
|
| 216 |
+
<td>✅</td>
|
| 217 |
+
</tr>
|
| 218 |
+
<tr>
|
| 219 |
+
<td colspan="6">🍎 <strong>macOS 14+</strong></td>
|
| 220 |
+
</tr>
|
| 221 |
+
<tr>
|
| 222 |
+
<td align="right">arm64</td>
|
| 223 |
+
<td>◻️ CPU</td>
|
| 224 |
+
<td>Apple M1+</td>
|
| 225 |
+
<td>✅ *</td>
|
| 226 |
+
<td>✅</td>
|
| 227 |
+
<td>✅</td>
|
| 228 |
+
</tr>
|
| 229 |
+
<tr>
|
| 230 |
+
<td></td>
|
| 231 |
+
<td>⬜ Metal <br><code>mps</code></td>
|
| 232 |
+
<td>Apple M1+</td>
|
| 233 |
+
<td>✅ *</td>
|
| 234 |
+
<td>✅</td>
|
| 235 |
+
<td>🚧</td>
|
| 236 |
+
</tbody>
|
| 237 |
+
</table>
|
| 238 |
+
<sup>* While supported, these marked features may lack in performance optimizations.</sup>
|
| 239 |
+
|
| 240 |
+
## :book: Documentation
|
| 241 |
+
* [Official Documentation](https://huggingface.co/docs/bitsandbytes/main)
|
| 242 |
+
* 🤗 [Transformers](https://huggingface.co/docs/transformers/quantization/bitsandbytes)
|
| 243 |
+
* 🤗 [Diffusers](https://huggingface.co/docs/diffusers/quantization/bitsandbytes)
|
| 244 |
+
* 🤗 [PEFT](https://huggingface.co/docs/peft/developer_guides/quantization#quantize-a-model)
|
| 245 |
+
|
| 246 |
+
## :heart: Sponsors
|
| 247 |
+
The continued maintenance and development of `bitsandbytes` is made possible thanks to the generous support of our sponsors. Their contributions help ensure that we can keep improving the project and delivering valuable updates to the community.
|
| 248 |
+
|
| 249 |
+
<kbd><a href="https://hf.co" target="_blank"><img width="100" src="https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg" alt="Hugging Face"></a></kbd>
|
| 250 |
+
|
| 251 |
+
## License
|
| 252 |
+
`bitsandbytes` is MIT licensed.
|
| 253 |
+
|
| 254 |
+
## How to cite us
|
| 255 |
+
If you found this library useful, please consider citing our work:
|
| 256 |
+
|
| 257 |
+
### QLoRA
|
| 258 |
+
|
| 259 |
+
```bibtex
|
| 260 |
+
@article{dettmers2023qlora,
|
| 261 |
+
title={Qlora: Efficient finetuning of quantized llms},
|
| 262 |
+
author={Dettmers, Tim and Pagnoni, Artidoro and Holtzman, Ari and Zettlemoyer, Luke},
|
| 263 |
+
journal={arXiv preprint arXiv:2305.14314},
|
| 264 |
+
year={2023}
|
| 265 |
+
}
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
### LLM.int8()
|
| 269 |
+
|
| 270 |
+
```bibtex
|
| 271 |
+
@article{dettmers2022llmint8,
|
| 272 |
+
title={LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale},
|
| 273 |
+
author={Dettmers, Tim and Lewis, Mike and Belkada, Younes and Zettlemoyer, Luke},
|
| 274 |
+
journal={arXiv preprint arXiv:2208.07339},
|
| 275 |
+
year={2022}
|
| 276 |
+
}
|
| 277 |
+
```
|
| 278 |
+
|
| 279 |
+
### 8-bit Optimizers
|
| 280 |
+
|
| 281 |
+
```bibtex
|
| 282 |
+
@article{dettmers2022optimizers,
|
| 283 |
+
title={8-bit Optimizers via Block-wise Quantization},
|
| 284 |
+
author={Dettmers, Tim and Lewis, Mike and Shleifer, Sam and Zettlemoyer, Luke},
|
| 285 |
+
journal={9th International Conference on Learning Representations, ICLR},
|
| 286 |
+
year={2022}
|
| 287 |
+
}
|
| 288 |
+
```
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/RECORD
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bitsandbytes-0.50.0.dist-info/INSTALLER,sha256=5hhM4Q4mYTT9z6QB6PGpUAW81PGNFrYrdXMj4oM_6ak,2
|
| 2 |
+
bitsandbytes-0.50.0.dist-info/METADATA,sha256=otWVmUGy3scc_PFYdPBhHAhjQloToVV2HHKTcoS2cvM,10535
|
| 3 |
+
bitsandbytes-0.50.0.dist-info/RECORD,,
|
| 4 |
+
bitsandbytes-0.50.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 5 |
+
bitsandbytes-0.50.0.dist-info/WHEEL,sha256=vzvwQmedic5-XvNrhk7dqzH_Ci-T7aNTCjvRUpPnYBk,110
|
| 6 |
+
bitsandbytes-0.50.0.dist-info/licenses/LICENSE,sha256=UkEte8fOQVfqYou6rLiCngqcs8WPV_mRdhJryM8r_IU,1086
|
| 7 |
+
bitsandbytes-0.50.0.dist-info/top_level.txt,sha256=bK-Zzu-JyIIh4njm8jTYcbuqX-Z80XTcDal4lXCG0-M,13
|
| 8 |
+
bitsandbytes/__init__.py,sha256=Ti1jq8tz_-G89r96rPzkH_N93OhjYLL3-2SycnpOmOw,2222
|
| 9 |
+
bitsandbytes/__main__.py,sha256=A3iA9QH8yfn-60uN6w_7WUiuRIM777rZ_pf_HsZtse4,90
|
| 10 |
+
bitsandbytes/_ops.py,sha256=5tntTCaOUgRKj_X4lflMqIvwLRVrJcRhYUHPqdqvkkw,19919
|
| 11 |
+
bitsandbytes/autograd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 12 |
+
bitsandbytes/autograd/_functions.py,sha256=d8pUyHtprUcKxz5dfd6UlI8g3p3ouHZJV3KLwpgx838,17700
|
| 13 |
+
bitsandbytes/backends/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 14 |
+
bitsandbytes/backends/cpu/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 15 |
+
bitsandbytes/backends/cpu/ops.py,sha256=1L_Ki0zmpqNorszdFRb5j1Bb83T25m1k-yLmIp6x_2c,20911
|
| 16 |
+
bitsandbytes/backends/cuda/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 17 |
+
bitsandbytes/backends/cuda/ops.py,sha256=mChNXwxzlLwXkv9f7eLYYia139s2GkACMjuBSsLhWBM,41773
|
| 18 |
+
bitsandbytes/backends/default/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 19 |
+
bitsandbytes/backends/default/ops.py,sha256=45r9Ftym5vNKFDBa3gukrLtJ3Cx6m6-Tc0HX4ztoKBk,19802
|
| 20 |
+
bitsandbytes/backends/hpu/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 21 |
+
bitsandbytes/backends/hpu/ops.py,sha256=GgFzY-1rOgqmH7ccXiK_aeo9yKTitvj7-WbOqGgG9uM,1457
|
| 22 |
+
bitsandbytes/backends/mps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 23 |
+
bitsandbytes/backends/mps/ops.py,sha256=2Tps6y0DNZ0GH2Gr1w0VL_7Urya_JpPXxtR_hHC9B3A,9105
|
| 24 |
+
bitsandbytes/backends/triton/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 25 |
+
bitsandbytes/backends/triton/kernels_4bit.py,sha256=oolPfXxCwpPxZ3ekwiC68Nv-b1So6PtvI48RsSFPIAM,22150
|
| 26 |
+
bitsandbytes/backends/triton/kernels_8bit_quant.py,sha256=kndNWcj0_-FDtSmXUpyaxfxPfHODGq_Wz51CXemjrfw,6542
|
| 27 |
+
bitsandbytes/backends/triton/kernels_optim.py,sha256=TgRYrQxMG9kx3Mt3LZLB3VPb_5otpe2ZkVUBl_IHcbE,38685
|
| 28 |
+
bitsandbytes/backends/triton/ops.py,sha256=cjz-MTTMjVmEd-I4qEu2l95D45VvZI4fqo6RoameKrg,10276
|
| 29 |
+
bitsandbytes/backends/utils.py,sha256=3FZPL78T26gTiKI9TIcWfdVNoEr48XsNtSaF_kKxHIQ,2322
|
| 30 |
+
bitsandbytes/backends/xpu/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 31 |
+
bitsandbytes/backends/xpu/ops.py,sha256=D_zelk4b8jxLSunzm3O6ilC5Ci2aApCURvFGcHRDJZY,9845
|
| 32 |
+
bitsandbytes/cextension.py,sha256=p85Ne8tQS_fSaM6WZY2-gygEtvbtmZ9h6vK-65OFyb0,17174
|
| 33 |
+
bitsandbytes/consts.py,sha256=xfDadQAax-maxqkHrVNbZ6UITJoS0XgcUfopmjLgilM,380
|
| 34 |
+
bitsandbytes/cuda_specs.py,sha256=akFTR2ZxQ5CFpFLeg5QKFbVOZ4xz3USax43RxazP22k,3236
|
| 35 |
+
bitsandbytes/diagnostics/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 36 |
+
bitsandbytes/diagnostics/cuda.py,sha256=O3907PxFrWr5gN34nDHgY7dWCaYcP2P5D35DYIlapGM,7256
|
| 37 |
+
bitsandbytes/diagnostics/main.py,sha256=08pFuKjQrltzVyj7F3ch31SPJcI6PGiSyVQts0GtTJo,4202
|
| 38 |
+
bitsandbytes/diagnostics/utils.py,sha256=ECiI279jh78977RWZFIOkmTJG4g1O_YiQLwP4iSa7Tw,284
|
| 39 |
+
bitsandbytes/functional.py,sha256=UgqenTr7oREkc032Dkiifji2OJMF34iINEBbQ14oWBc,65287
|
| 40 |
+
bitsandbytes/libbitsandbytes_cpu.so,sha256=r5-vKRTVSSoweJLDl2nnNAuLZJwhSQXvwNGYnIvJIAw,63464
|
| 41 |
+
bitsandbytes/libbitsandbytes_cuda118.so,sha256=yrv4eh-7j55wrKzWZR9OGfMm-eO6VVtJGoQV6mMLb1E,21879184
|
| 42 |
+
bitsandbytes/libbitsandbytes_cuda121.so,sha256=mxt0V9l2tHCeq39DY7nJcZv2a1Nf0yg_mL7MvrwGue0,21580176
|
| 43 |
+
bitsandbytes/libbitsandbytes_cuda124.so,sha256=Ts7llW-_81ckbEQayIoVoDxakYvpfiHsUa_biXJbOGE,21294432
|
| 44 |
+
bitsandbytes/libbitsandbytes_cuda126.so,sha256=ZIP8lwEsECWkt4MQgclbCwC-usm68rJupbY89KCk6Cc,21382936
|
| 45 |
+
bitsandbytes/libbitsandbytes_cuda128.so,sha256=0aA588rukPlafkbpWo9MlxS6VwhLF9NA1bZxC-cPIko,26535848
|
| 46 |
+
bitsandbytes/libbitsandbytes_cuda130.so,sha256=ZoWczGZSOxUGk5amVAd3NmpDd0jasIAdrDIM_WvV5io,3901168
|
| 47 |
+
bitsandbytes/libbitsandbytes_cuda132.so,sha256=0sWf_5Kb7VaVJlw5fhXqvZ6L5PLrFYseS_I7ePJAi74,3984920
|
| 48 |
+
bitsandbytes/libbitsandbytes_rocm64.so,sha256=R6cl1d5pf3zQj8peD6k6fcp2rJebonUUj4d6TP6S7gM,894216
|
| 49 |
+
bitsandbytes/libbitsandbytes_rocm70.so,sha256=iIn4KqIdATqIbIae6_HG55E14hj5_KiW4bjsr1jCM5g,922896
|
| 50 |
+
bitsandbytes/libbitsandbytes_rocm71.so,sha256=dP2hhc1o7jYQq_CbFLi2u7ZqRaXenXIWwKsG3L4gtx8,922888
|
| 51 |
+
bitsandbytes/libbitsandbytes_rocm714.so,sha256=l3on6_3-VBON4quE3o3Dw1tGkq_VZZDQ3hx89KfOty8,1164704
|
| 52 |
+
bitsandbytes/libbitsandbytes_rocm72.so,sha256=sA5PU10TbvJBLvAGCItiJmtnMI5N7lzEUFodyQ0Lg2I,972048
|
| 53 |
+
bitsandbytes/libbitsandbytes_xpu2025.so,sha256=jZIyTVv0zWgq4dxI1GKlTPHwRoZr_xYvme90k9j0Sks,320024
|
| 54 |
+
bitsandbytes/libbitsandbytes_xpu2026.so,sha256=mfnUrfBG2VMLNIOgu_0H6t0qGYdg-DnLnG95EwmeWDg,303496
|
| 55 |
+
bitsandbytes/nn/__init__.py,sha256=aTONEw0rCr5xTt9xw2X-sbpXJxcFg-MXNksFxXtgL_4,432
|
| 56 |
+
bitsandbytes/nn/modules.py,sha256=96shYGgeLwicqugNb9KDrGqbnxuCJp5vOVw7BG-vYG4,44262
|
| 57 |
+
bitsandbytes/nn/parametrize.py,sha256=tSc2Iij4q1J2evGdD7nVF8NY9dVdaR678kKW1Ve7bAM,8365
|
| 58 |
+
bitsandbytes/optim/__init__.py,sha256=0RqM1U6jwU8FCeePDch4QEu2AZEYVrtDI89lF2d1IUA,881
|
| 59 |
+
bitsandbytes/optim/adagrad.py,sha256=vPtp0M9opv2I42RdSweQQiJa9qu4ddYgbzKagGIl-W4,6774
|
| 60 |
+
bitsandbytes/optim/adam.py,sha256=UIbjVFTmQZU5EzpV4-0jzAn4blbOYnRQr3aPShlRdVc,12832
|
| 61 |
+
bitsandbytes/optim/adamw.py,sha256=2mtW2oR1zy61WR5oEUUB0Vm05A-TZ8RjozJNwgKqJjs,12449
|
| 62 |
+
bitsandbytes/optim/ademamix.py,sha256=BSAckQJywUa3UDWiztJjqN5joGLzU06r8kASrNT2PMo,12750
|
| 63 |
+
bitsandbytes/optim/lamb.py,sha256=amOaYeNIwU7R6Bennj-OS7QNCakHHEre8VXM5JvnMLM,7315
|
| 64 |
+
bitsandbytes/optim/lars.py,sha256=o32s1FUiE49GRpc-WdTmRu_FeB7922ZBMh3UNBxcaWg,8326
|
| 65 |
+
bitsandbytes/optim/lion.py,sha256=OUu1aysIM85NNHNFu-LDTCLI6HoqPNMp-bLVgZAGtHk,8309
|
| 66 |
+
bitsandbytes/optim/optimizer.py,sha256=LgegbqBzbk5s57uCEgcmSqXC5wRHGMiAIbe646Fd3y4,28127
|
| 67 |
+
bitsandbytes/optim/rmsprop.py,sha256=2sHSY1hfTr83aZvBmIEfzoTiKER8LZc_BDkmyJG3lPs,6134
|
| 68 |
+
bitsandbytes/optim/sgd.py,sha256=gVjbjjYrm197RiOt6vLoXaMxaWVK6De91Ap_KL6WFtg,4916
|
| 69 |
+
bitsandbytes/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 70 |
+
bitsandbytes/utils.py,sha256=G1oZbgIAt07m9-7SpLBRtIjsipI8kCptdfTWKz96g9I,6928
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/REQUESTED
ADDED
|
File without changes
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (83.0.0)
|
| 3 |
+
Root-Is-Purelib: false
|
| 4 |
+
Tag: py3-none-manylinux_2_24_x86_64
|
| 5 |
+
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/licenses/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) Facebook, Inc. and its affiliates.
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
venv/lib/python3.11/site-packages/bitsandbytes-0.50.0.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
bitsandbytes
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
from .adagrad import Adagrad, Adagrad8bit, Adagrad32bit
|
| 7 |
+
from .adam import Adam, Adam8bit, Adam32bit, PagedAdam, PagedAdam8bit, PagedAdam32bit
|
| 8 |
+
from .adamw import (
|
| 9 |
+
AdamW,
|
| 10 |
+
AdamW8bit,
|
| 11 |
+
AdamW32bit,
|
| 12 |
+
PagedAdamW,
|
| 13 |
+
PagedAdamW8bit,
|
| 14 |
+
PagedAdamW32bit,
|
| 15 |
+
)
|
| 16 |
+
from .ademamix import AdEMAMix, AdEMAMix8bit, AdEMAMix32bit, PagedAdEMAMix, PagedAdEMAMix8bit, PagedAdEMAMix32bit
|
| 17 |
+
from .lamb import LAMB, LAMB8bit, LAMB32bit
|
| 18 |
+
from .lars import LARS, LARS8bit, LARS32bit, PytorchLARS
|
| 19 |
+
from .lion import Lion, Lion8bit, Lion32bit, PagedLion, PagedLion8bit, PagedLion32bit
|
| 20 |
+
from .optimizer import GlobalOptimManager
|
| 21 |
+
from .rmsprop import RMSprop, RMSprop8bit, RMSprop32bit
|
| 22 |
+
from .sgd import SGD, SGD8bit, SGD32bit
|
venv/l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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from bitsandbytes.optim.optimizer import Optimizer1State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Adagrad(Optimizer1State):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
params,
|
| 12 |
+
lr=1e-2,
|
| 13 |
+
lr_decay=0,
|
| 14 |
+
weight_decay=0,
|
| 15 |
+
initial_accumulator_value=0,
|
| 16 |
+
eps=1e-10,
|
| 17 |
+
optim_bits=32,
|
| 18 |
+
args=None,
|
| 19 |
+
min_8bit_size=4096,
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
Base Adagrad optimizer.
|
| 23 |
+
|
| 24 |
+
Arguments:
|
| 25 |
+
params (`torch.tensor`):
|
| 26 |
+
The input parameters to optimize.
|
| 27 |
+
lr (`float`, defaults to 1e-2):
|
| 28 |
+
The learning rate.
|
| 29 |
+
lr_decay (`int`, defaults to 0):
|
| 30 |
+
The learning rate decay.
|
| 31 |
+
weight_decay (`float`, defaults to 0.0):
|
| 32 |
+
The weight decay value for the optimizer.
|
| 33 |
+
initial_accumulator_value (`int`, defaults to 0):
|
| 34 |
+
The initial momemtum values.
|
| 35 |
+
eps (`float`, defaults to 1e-10):
|
| 36 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 37 |
+
optim_bits (`int`, defaults to 32):
|
| 38 |
+
The number of bits of the optimizer state.
|
| 39 |
+
args (`object`, defaults to `None`):
|
| 40 |
+
An object with additional arguments.
|
| 41 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 42 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 43 |
+
"""
|
| 44 |
+
if not 0.0 <= lr:
|
| 45 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 46 |
+
if not 0.0 <= weight_decay:
|
| 47 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 48 |
+
if not 0.0 <= eps:
|
| 49 |
+
raise ValueError(f"Invalid epsilon value: {eps}")
|
| 50 |
+
if initial_accumulator_value != 0.0:
|
| 51 |
+
raise ValueError("Initial accumulator value != 0.0 not supported!")
|
| 52 |
+
if lr_decay != 0.0:
|
| 53 |
+
raise ValueError("Lr Decay != 0.0 not supported!")
|
| 54 |
+
super().__init__(
|
| 55 |
+
"adagrad",
|
| 56 |
+
params,
|
| 57 |
+
lr,
|
| 58 |
+
(0.0, 0.0),
|
| 59 |
+
eps,
|
| 60 |
+
weight_decay,
|
| 61 |
+
optim_bits,
|
| 62 |
+
args,
|
| 63 |
+
min_8bit_size,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class Adagrad8bit(Optimizer1State):
|
| 68 |
+
def __init__(
|
| 69 |
+
self,
|
| 70 |
+
params,
|
| 71 |
+
lr=1e-2,
|
| 72 |
+
lr_decay=0,
|
| 73 |
+
weight_decay=0,
|
| 74 |
+
initial_accumulator_value=0,
|
| 75 |
+
eps=1e-10,
|
| 76 |
+
optim_bits=8,
|
| 77 |
+
args=None,
|
| 78 |
+
min_8bit_size=4096,
|
| 79 |
+
):
|
| 80 |
+
"""
|
| 81 |
+
8-bit Adagrad optimizer.
|
| 82 |
+
|
| 83 |
+
Arguments:
|
| 84 |
+
params (`torch.tensor`):
|
| 85 |
+
The input parameters to optimize.
|
| 86 |
+
lr (`float`, defaults to 1e-2):
|
| 87 |
+
The learning rate.
|
| 88 |
+
lr_decay (`int`, defaults to 0):
|
| 89 |
+
The learning rate decay.
|
| 90 |
+
weight_decay (`float`, defaults to 0.0):
|
| 91 |
+
The weight decay value for the optimizer.
|
| 92 |
+
initial_accumulator_value (`int`, defaults to 0):
|
| 93 |
+
The initial momemtum values.
|
| 94 |
+
eps (`float`, defaults to 1e-10):
|
| 95 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 96 |
+
optim_bits (`int`, defaults to 8):
|
| 97 |
+
The number of bits of the optimizer state.
|
| 98 |
+
Note: This parameter is not used in Adagrad8bit as it always uses 8-bit optimization.
|
| 99 |
+
args (`object`, defaults to `None`):
|
| 100 |
+
An object with additional arguments.
|
| 101 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 102 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 103 |
+
"""
|
| 104 |
+
if not 0.0 <= lr:
|
| 105 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 106 |
+
if not 0.0 <= weight_decay:
|
| 107 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 108 |
+
if not 0.0 <= eps:
|
| 109 |
+
raise ValueError(f"Invalid epsilon value: {eps}")
|
| 110 |
+
if initial_accumulator_value != 0.0:
|
| 111 |
+
raise ValueError("Initial accumulator value != 0.0 not supported!")
|
| 112 |
+
if lr_decay != 0.0:
|
| 113 |
+
raise ValueError("Lr Decay != 0.0 not supported!")
|
| 114 |
+
if optim_bits != 8:
|
| 115 |
+
# We allow the default value of 8 to maintain compatibility with the function signature,
|
| 116 |
+
# but any other value is invalid since Adagrad8bit always uses 8-bit optimization
|
| 117 |
+
raise ValueError("Adagrad8bit only supports optim_bits=8 (default value for compatibility)")
|
| 118 |
+
super().__init__(
|
| 119 |
+
"adagrad",
|
| 120 |
+
params,
|
| 121 |
+
lr,
|
| 122 |
+
(0.0, 0.0),
|
| 123 |
+
eps,
|
| 124 |
+
weight_decay,
|
| 125 |
+
8,
|
| 126 |
+
args,
|
| 127 |
+
min_8bit_size,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class Adagrad32bit(Optimizer1State):
|
| 132 |
+
def __init__(
|
| 133 |
+
self,
|
| 134 |
+
params,
|
| 135 |
+
lr=1e-2,
|
| 136 |
+
lr_decay=0,
|
| 137 |
+
weight_decay=0,
|
| 138 |
+
initial_accumulator_value=0,
|
| 139 |
+
eps=1e-10,
|
| 140 |
+
optim_bits=32,
|
| 141 |
+
args=None,
|
| 142 |
+
min_8bit_size=4096,
|
| 143 |
+
):
|
| 144 |
+
"""
|
| 145 |
+
32-bit Adagrad optimizer.
|
| 146 |
+
|
| 147 |
+
Arguments:
|
| 148 |
+
params (`torch.tensor`):
|
| 149 |
+
The input parameters to optimize.
|
| 150 |
+
lr (`float`, defaults to 1e-2):
|
| 151 |
+
The learning rate.
|
| 152 |
+
lr_decay (`int`, defaults to 0):
|
| 153 |
+
The learning rate decay.
|
| 154 |
+
weight_decay (`float`, defaults to 0.0):
|
| 155 |
+
The weight decay value for the optimizer.
|
| 156 |
+
initial_accumulator_value (`int`, defaults to 0):
|
| 157 |
+
The initial momemtum values.
|
| 158 |
+
eps (`float`, defaults to 1e-10):
|
| 159 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 160 |
+
optim_bits (`int`, defaults to 32):
|
| 161 |
+
The number of bits of the optimizer state.
|
| 162 |
+
args (`object`, defaults to `None`):
|
| 163 |
+
An object with additional arguments.
|
| 164 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 165 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 166 |
+
"""
|
| 167 |
+
if not 0.0 <= lr:
|
| 168 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 169 |
+
if not 0.0 <= weight_decay:
|
| 170 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 171 |
+
if not 0.0 <= eps:
|
| 172 |
+
raise ValueError(f"Invalid epsilon value: {eps}")
|
| 173 |
+
if initial_accumulator_value != 0.0:
|
| 174 |
+
raise ValueError("Initial accumulator value != 0.0 not supported!")
|
| 175 |
+
if lr_decay != 0.0:
|
| 176 |
+
raise ValueError("Lr Decay != 0.0 not supported!")
|
| 177 |
+
super().__init__(
|
| 178 |
+
"adagrad",
|
| 179 |
+
params,
|
| 180 |
+
lr,
|
| 181 |
+
(0.0, 0.0),
|
| 182 |
+
eps,
|
| 183 |
+
weight_decay,
|
| 184 |
+
32,
|
| 185 |
+
args,
|
| 186 |
+
min_8bit_size,
|
| 187 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/adam.py
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
from bitsandbytes.optim.optimizer import Optimizer2State
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Adam(Optimizer2State):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
params,
|
| 13 |
+
lr=1e-3,
|
| 14 |
+
betas=(0.9, 0.999),
|
| 15 |
+
eps=1e-8,
|
| 16 |
+
weight_decay=0,
|
| 17 |
+
amsgrad=False,
|
| 18 |
+
optim_bits=32,
|
| 19 |
+
args=None,
|
| 20 |
+
min_8bit_size=4096,
|
| 21 |
+
is_paged=False,
|
| 22 |
+
):
|
| 23 |
+
"""
|
| 24 |
+
Base Adam optimizer.
|
| 25 |
+
|
| 26 |
+
Arguments:
|
| 27 |
+
params (`torch.tensor`):
|
| 28 |
+
The input parameters to optimize.
|
| 29 |
+
lr (`float`, defaults to 1e-3):
|
| 30 |
+
The learning rate.
|
| 31 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 32 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 33 |
+
eps (`float`, defaults to 1e-8):
|
| 34 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 35 |
+
weight_decay (`float`, defaults to 0.0):
|
| 36 |
+
The weight decay value for the optimizer.
|
| 37 |
+
amsgrad (`bool`, defaults to `False`):
|
| 38 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 39 |
+
optim_bits (`int`, defaults to 32):
|
| 40 |
+
The number of bits of the optimizer state.
|
| 41 |
+
args (`object`, defaults to `None`):
|
| 42 |
+
An object with additional arguments.
|
| 43 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 44 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 45 |
+
is_paged (`bool`, defaults to `False`):
|
| 46 |
+
Whether the optimizer is a paged optimizer or not.
|
| 47 |
+
"""
|
| 48 |
+
super().__init__(
|
| 49 |
+
"adam",
|
| 50 |
+
params,
|
| 51 |
+
lr,
|
| 52 |
+
betas,
|
| 53 |
+
eps,
|
| 54 |
+
weight_decay,
|
| 55 |
+
optim_bits,
|
| 56 |
+
args,
|
| 57 |
+
min_8bit_size,
|
| 58 |
+
is_paged=is_paged,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class Adam8bit(Optimizer2State):
|
| 63 |
+
def __init__(
|
| 64 |
+
self,
|
| 65 |
+
params,
|
| 66 |
+
lr=1e-3,
|
| 67 |
+
betas=(0.9, 0.999),
|
| 68 |
+
eps=1e-8,
|
| 69 |
+
weight_decay=0,
|
| 70 |
+
amsgrad=False,
|
| 71 |
+
optim_bits=32,
|
| 72 |
+
args=None,
|
| 73 |
+
min_8bit_size=4096,
|
| 74 |
+
is_paged=False,
|
| 75 |
+
):
|
| 76 |
+
"""
|
| 77 |
+
8-bit Adam optimizer.
|
| 78 |
+
|
| 79 |
+
Arguments:
|
| 80 |
+
params (`torch.tensor`):
|
| 81 |
+
The input parameters to optimize.
|
| 82 |
+
lr (`float`, defaults to 1e-3):
|
| 83 |
+
The learning rate.
|
| 84 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 85 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 86 |
+
eps (`float`, defaults to 1e-8):
|
| 87 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 88 |
+
weight_decay (`float`, defaults to 0.0):
|
| 89 |
+
The weight decay value for the optimizer.
|
| 90 |
+
amsgrad (`bool`, defaults to `False`):
|
| 91 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 92 |
+
Note: This parameter is not supported in Adam8bit and must be False.
|
| 93 |
+
optim_bits (`int`, defaults to 32):
|
| 94 |
+
The number of bits of the optimizer state.
|
| 95 |
+
Note: This parameter is not used in Adam8bit as it always uses 8-bit optimization.
|
| 96 |
+
args (`object`, defaults to `None`):
|
| 97 |
+
An object with additional arguments.
|
| 98 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 99 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 100 |
+
is_paged (`bool`, defaults to `False`):
|
| 101 |
+
Whether the optimizer is a paged optimizer or not.
|
| 102 |
+
"""
|
| 103 |
+
# Validate unsupported parameters
|
| 104 |
+
if amsgrad:
|
| 105 |
+
raise ValueError("Adam8bit does not support amsgrad=True")
|
| 106 |
+
|
| 107 |
+
if optim_bits != 32:
|
| 108 |
+
# We allow the default value of 32 to maintain compatibility with the function signature,
|
| 109 |
+
# but any other value is invalid since Adam8bit always uses 8-bit optimization
|
| 110 |
+
raise ValueError("Adam8bit only supports optim_bits=32 (default value for compatibility)")
|
| 111 |
+
|
| 112 |
+
super().__init__(
|
| 113 |
+
"adam",
|
| 114 |
+
params,
|
| 115 |
+
lr,
|
| 116 |
+
betas,
|
| 117 |
+
eps,
|
| 118 |
+
weight_decay,
|
| 119 |
+
8, # Hardcoded to 8 bits
|
| 120 |
+
args,
|
| 121 |
+
min_8bit_size,
|
| 122 |
+
is_paged=is_paged,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class Adam32bit(Optimizer2State):
|
| 127 |
+
def __init__(
|
| 128 |
+
self,
|
| 129 |
+
params,
|
| 130 |
+
lr=1e-3,
|
| 131 |
+
betas=(0.9, 0.999),
|
| 132 |
+
eps=1e-8,
|
| 133 |
+
weight_decay=0,
|
| 134 |
+
amsgrad=False,
|
| 135 |
+
optim_bits=32,
|
| 136 |
+
args=None,
|
| 137 |
+
min_8bit_size=4096,
|
| 138 |
+
is_paged=False,
|
| 139 |
+
):
|
| 140 |
+
"""
|
| 141 |
+
32-bit Adam optimizer.
|
| 142 |
+
|
| 143 |
+
Arguments:
|
| 144 |
+
params (`torch.tensor`):
|
| 145 |
+
The input parameters to optimize.
|
| 146 |
+
lr (`float`, defaults to 1e-3):
|
| 147 |
+
The learning rate.
|
| 148 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 149 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 150 |
+
eps (`float`, defaults to 1e-8):
|
| 151 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 152 |
+
weight_decay (`float`, defaults to 0.0):
|
| 153 |
+
The weight decay value for the optimizer.
|
| 154 |
+
amsgrad (`bool`, defaults to `False`):
|
| 155 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 156 |
+
optim_bits (`int`, defaults to 32):
|
| 157 |
+
The number of bits of the optimizer state.
|
| 158 |
+
args (`object`, defaults to `None`):
|
| 159 |
+
An object with additional arguments.
|
| 160 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 161 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 162 |
+
is_paged (`bool`, defaults to `False`):
|
| 163 |
+
Whether the optimizer is a paged optimizer or not.
|
| 164 |
+
"""
|
| 165 |
+
super().__init__(
|
| 166 |
+
"adam",
|
| 167 |
+
params,
|
| 168 |
+
lr,
|
| 169 |
+
betas,
|
| 170 |
+
eps,
|
| 171 |
+
weight_decay,
|
| 172 |
+
32,
|
| 173 |
+
args,
|
| 174 |
+
min_8bit_size,
|
| 175 |
+
is_paged=is_paged,
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
class PagedAdam(Optimizer2State):
|
| 180 |
+
def __init__(
|
| 181 |
+
self,
|
| 182 |
+
params,
|
| 183 |
+
lr=1e-3,
|
| 184 |
+
betas=(0.9, 0.999),
|
| 185 |
+
eps=1e-8,
|
| 186 |
+
weight_decay=0,
|
| 187 |
+
amsgrad=False,
|
| 188 |
+
optim_bits=32,
|
| 189 |
+
args=None,
|
| 190 |
+
min_8bit_size=4096,
|
| 191 |
+
is_paged=False,
|
| 192 |
+
):
|
| 193 |
+
"""
|
| 194 |
+
Paged Adam optimizer.
|
| 195 |
+
|
| 196 |
+
Arguments:
|
| 197 |
+
params (`torch.tensor`):
|
| 198 |
+
The input parameters to optimize.
|
| 199 |
+
lr (`float`, defaults to 1e-3):
|
| 200 |
+
The learning rate.
|
| 201 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 202 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 203 |
+
eps (`float`, defaults to 1e-8):
|
| 204 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 205 |
+
weight_decay (`float`, defaults to 0.0):
|
| 206 |
+
The weight decay value for the optimizer.
|
| 207 |
+
amsgrad (`bool`, defaults to `False`):
|
| 208 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 209 |
+
optim_bits (`int`, defaults to 32):
|
| 210 |
+
The number of bits of the optimizer state.
|
| 211 |
+
args (`object`, defaults to `None`):
|
| 212 |
+
An object with additional arguments.
|
| 213 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 214 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 215 |
+
is_paged (`bool`, defaults to `False`):
|
| 216 |
+
Whether the optimizer is a paged optimizer or not.
|
| 217 |
+
"""
|
| 218 |
+
super().__init__(
|
| 219 |
+
"adam",
|
| 220 |
+
params,
|
| 221 |
+
lr,
|
| 222 |
+
betas,
|
| 223 |
+
eps,
|
| 224 |
+
weight_decay,
|
| 225 |
+
optim_bits,
|
| 226 |
+
args,
|
| 227 |
+
min_8bit_size,
|
| 228 |
+
is_paged=True,
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
class PagedAdam8bit(Optimizer2State):
|
| 233 |
+
def __init__(
|
| 234 |
+
self,
|
| 235 |
+
params,
|
| 236 |
+
lr=1e-3,
|
| 237 |
+
betas=(0.9, 0.999),
|
| 238 |
+
eps=1e-8,
|
| 239 |
+
weight_decay=0,
|
| 240 |
+
amsgrad=False,
|
| 241 |
+
optim_bits=32,
|
| 242 |
+
args=None,
|
| 243 |
+
min_8bit_size=4096,
|
| 244 |
+
is_paged=False,
|
| 245 |
+
):
|
| 246 |
+
"""
|
| 247 |
+
8-bit paged Adam optimizer.
|
| 248 |
+
|
| 249 |
+
Arguments:
|
| 250 |
+
params (`torch.tensor`):
|
| 251 |
+
The input parameters to optimize.
|
| 252 |
+
lr (`float`, defaults to 1e-3):
|
| 253 |
+
The learning rate.
|
| 254 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 255 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 256 |
+
eps (`float`, defaults to 1e-8):
|
| 257 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 258 |
+
weight_decay (`float`, defaults to 0.0):
|
| 259 |
+
The weight decay value for the optimizer.
|
| 260 |
+
amsgrad (`bool`, defaults to `False`):
|
| 261 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 262 |
+
Note: This parameter is not supported in PagedAdam8bit and must be False.
|
| 263 |
+
optim_bits (`int`, defaults to 32):
|
| 264 |
+
The number of bits of the optimizer state.
|
| 265 |
+
Note: This parameter is not used in PagedAdam8bit as it always uses 8-bit optimization.
|
| 266 |
+
args (`object`, defaults to `None`):
|
| 267 |
+
An object with additional arguments.
|
| 268 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 269 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 270 |
+
is_paged (`bool`, defaults to `False`):
|
| 271 |
+
Whether the optimizer is a paged optimizer or not.
|
| 272 |
+
"""
|
| 273 |
+
# Validate unsupported parameters
|
| 274 |
+
if amsgrad:
|
| 275 |
+
raise ValueError("PagedAdam8bit does not support amsgrad=True")
|
| 276 |
+
|
| 277 |
+
if optim_bits != 32:
|
| 278 |
+
# We allow the default value of 32 to maintain compatibility with the function signature,
|
| 279 |
+
# but any other value is invalid since PagedAdam8bit always uses 8-bit optimization
|
| 280 |
+
raise ValueError("PagedAdam8bit only supports optim_bits=32 (default value for compatibility)")
|
| 281 |
+
|
| 282 |
+
super().__init__(
|
| 283 |
+
"adam",
|
| 284 |
+
params,
|
| 285 |
+
lr,
|
| 286 |
+
betas,
|
| 287 |
+
eps,
|
| 288 |
+
weight_decay,
|
| 289 |
+
8, # Hardcoded to 8 bits
|
| 290 |
+
args,
|
| 291 |
+
min_8bit_size,
|
| 292 |
+
is_paged=True,
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
class PagedAdam32bit(Optimizer2State):
|
| 297 |
+
def __init__(
|
| 298 |
+
self,
|
| 299 |
+
params,
|
| 300 |
+
lr=1e-3,
|
| 301 |
+
betas=(0.9, 0.999),
|
| 302 |
+
eps=1e-8,
|
| 303 |
+
weight_decay=0,
|
| 304 |
+
amsgrad=False,
|
| 305 |
+
optim_bits=32,
|
| 306 |
+
args=None,
|
| 307 |
+
min_8bit_size=4096,
|
| 308 |
+
is_paged=False,
|
| 309 |
+
):
|
| 310 |
+
"""
|
| 311 |
+
Paged 32-bit Adam optimizer.
|
| 312 |
+
|
| 313 |
+
Arguments:
|
| 314 |
+
params (`torch.tensor`):
|
| 315 |
+
The input parameters to optimize.
|
| 316 |
+
lr (`float`, defaults to 1e-3):
|
| 317 |
+
The learning rate.
|
| 318 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 319 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 320 |
+
eps (`float`, defaults to 1e-8):
|
| 321 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 322 |
+
weight_decay (`float`, defaults to 0.0):
|
| 323 |
+
The weight decay value for the optimizer.
|
| 324 |
+
amsgrad (`bool`, defaults to `False`):
|
| 325 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 326 |
+
optim_bits (`int`, defaults to 32):
|
| 327 |
+
The number of bits of the optimizer state.
|
| 328 |
+
args (`object`, defaults to `None`):
|
| 329 |
+
An object with additional arguments.
|
| 330 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 331 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 332 |
+
is_paged (`bool`, defaults to `False`):
|
| 333 |
+
Whether the optimizer is a paged optimizer or not.
|
| 334 |
+
"""
|
| 335 |
+
super().__init__(
|
| 336 |
+
"adam",
|
| 337 |
+
params,
|
| 338 |
+
lr,
|
| 339 |
+
betas,
|
| 340 |
+
eps,
|
| 341 |
+
weight_decay,
|
| 342 |
+
32,
|
| 343 |
+
args,
|
| 344 |
+
min_8bit_size,
|
| 345 |
+
is_paged=True,
|
| 346 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/adamw.py
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
from bitsandbytes.optim.optimizer import Optimizer2State
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class AdamW(Optimizer2State):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
params,
|
| 13 |
+
lr=1e-3,
|
| 14 |
+
betas=(0.9, 0.999),
|
| 15 |
+
eps=1e-8,
|
| 16 |
+
weight_decay=1e-2,
|
| 17 |
+
amsgrad=False,
|
| 18 |
+
optim_bits=32,
|
| 19 |
+
args=None,
|
| 20 |
+
min_8bit_size=4096,
|
| 21 |
+
is_paged=False,
|
| 22 |
+
):
|
| 23 |
+
"""
|
| 24 |
+
Base AdamW optimizer.
|
| 25 |
+
|
| 26 |
+
Arguments:
|
| 27 |
+
params (`torch.Tensor`):
|
| 28 |
+
The input parameters to optimize.
|
| 29 |
+
lr (`float`, defaults to 1e-3):
|
| 30 |
+
The learning rate.
|
| 31 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 32 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 33 |
+
eps (`float`, defaults to 1e-8):
|
| 34 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 35 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 36 |
+
The weight decay value for the optimizer.
|
| 37 |
+
amsgrad (`bool`, defaults to `False`):
|
| 38 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 39 |
+
optim_bits (`int`, defaults to 32):
|
| 40 |
+
The number of bits of the optimizer state.
|
| 41 |
+
args (`object`, defaults to `None`):
|
| 42 |
+
An object with additional arguments.
|
| 43 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 44 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 45 |
+
is_paged (`bool`, defaults to `False`):
|
| 46 |
+
Whether the optimizer is a paged optimizer or not.
|
| 47 |
+
"""
|
| 48 |
+
super().__init__(
|
| 49 |
+
"adam",
|
| 50 |
+
params,
|
| 51 |
+
lr,
|
| 52 |
+
betas,
|
| 53 |
+
eps,
|
| 54 |
+
weight_decay,
|
| 55 |
+
optim_bits,
|
| 56 |
+
args,
|
| 57 |
+
min_8bit_size,
|
| 58 |
+
is_paged=is_paged,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class AdamW8bit(Optimizer2State):
|
| 63 |
+
def __init__(
|
| 64 |
+
self,
|
| 65 |
+
params,
|
| 66 |
+
lr=1e-3,
|
| 67 |
+
betas=(0.9, 0.999),
|
| 68 |
+
eps=1e-8,
|
| 69 |
+
weight_decay=1e-2,
|
| 70 |
+
amsgrad=False,
|
| 71 |
+
optim_bits=32,
|
| 72 |
+
args=None,
|
| 73 |
+
min_8bit_size=4096,
|
| 74 |
+
is_paged=False,
|
| 75 |
+
):
|
| 76 |
+
"""
|
| 77 |
+
8-bit AdamW optimizer.
|
| 78 |
+
|
| 79 |
+
Arguments:
|
| 80 |
+
params (`torch.Tensor`):
|
| 81 |
+
The input parameters to optimize.
|
| 82 |
+
lr (`float`, defaults to 1e-3):
|
| 83 |
+
The learning rate.
|
| 84 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 85 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 86 |
+
eps (`float`, defaults to 1e-8):
|
| 87 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 88 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 89 |
+
The weight decay value for the optimizer.
|
| 90 |
+
amsgrad (`bool`, defaults to `False`):
|
| 91 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 92 |
+
Note: This parameter is not supported in AdamW8bit and must be False.
|
| 93 |
+
optim_bits (`int`, defaults to 32):
|
| 94 |
+
The number of bits of the optimizer state.
|
| 95 |
+
Note: This parameter is not used in AdamW8bit as it always uses 8-bit optimization.
|
| 96 |
+
args (`object`, defaults to `None`):
|
| 97 |
+
An object with additional arguments.
|
| 98 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 99 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 100 |
+
is_paged (`bool`, defaults to `False`):
|
| 101 |
+
Whether the optimizer is a paged optimizer or not.
|
| 102 |
+
"""
|
| 103 |
+
# Validate unsupported parameters
|
| 104 |
+
if amsgrad:
|
| 105 |
+
raise ValueError("AdamW8bit does not support amsgrad=True")
|
| 106 |
+
|
| 107 |
+
if optim_bits != 32:
|
| 108 |
+
# We allow the default value of 32 to maintain compatibility with the function signature,
|
| 109 |
+
# but any other value is invalid since AdamW8bit always uses 8-bit optimization
|
| 110 |
+
raise ValueError("AdamW8bit only supports optim_bits=32 (default value for compatibility)")
|
| 111 |
+
|
| 112 |
+
super().__init__(
|
| 113 |
+
"adam",
|
| 114 |
+
params,
|
| 115 |
+
lr,
|
| 116 |
+
betas,
|
| 117 |
+
eps,
|
| 118 |
+
weight_decay,
|
| 119 |
+
8, # Hardcoded to 8 bits
|
| 120 |
+
args,
|
| 121 |
+
min_8bit_size,
|
| 122 |
+
is_paged=is_paged,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class AdamW32bit(Optimizer2State):
|
| 127 |
+
def __init__(
|
| 128 |
+
self,
|
| 129 |
+
params,
|
| 130 |
+
lr=1e-3,
|
| 131 |
+
betas=(0.9, 0.999),
|
| 132 |
+
eps=1e-8,
|
| 133 |
+
weight_decay=1e-2,
|
| 134 |
+
amsgrad=False,
|
| 135 |
+
optim_bits=32,
|
| 136 |
+
args=None,
|
| 137 |
+
min_8bit_size=4096,
|
| 138 |
+
is_paged=False,
|
| 139 |
+
):
|
| 140 |
+
"""
|
| 141 |
+
32-bit AdamW optimizer.
|
| 142 |
+
|
| 143 |
+
Arguments:
|
| 144 |
+
params (`torch.Tensor`):
|
| 145 |
+
The input parameters to optimize.
|
| 146 |
+
lr (`float`, defaults to 1e-3):
|
| 147 |
+
The learning rate.
|
| 148 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 149 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 150 |
+
eps (`float`, defaults to 1e-8):
|
| 151 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 152 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 153 |
+
The weight decay value for the optimizer.
|
| 154 |
+
amsgrad (`bool`, defaults to `False`):
|
| 155 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 156 |
+
optim_bits (`int`, defaults to 32):
|
| 157 |
+
The number of bits of the optimizer state.
|
| 158 |
+
args (`object`, defaults to `None`):
|
| 159 |
+
An object with additional arguments.
|
| 160 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 161 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 162 |
+
is_paged (`bool`, defaults to `False`):
|
| 163 |
+
Whether the optimizer is a paged optimizer or not.
|
| 164 |
+
"""
|
| 165 |
+
super().__init__(
|
| 166 |
+
"adam",
|
| 167 |
+
params,
|
| 168 |
+
lr,
|
| 169 |
+
betas,
|
| 170 |
+
eps,
|
| 171 |
+
weight_decay,
|
| 172 |
+
32,
|
| 173 |
+
args,
|
| 174 |
+
min_8bit_size,
|
| 175 |
+
is_paged=is_paged,
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
class PagedAdamW(Optimizer2State):
|
| 180 |
+
def __init__(
|
| 181 |
+
self,
|
| 182 |
+
params,
|
| 183 |
+
lr=1e-3,
|
| 184 |
+
betas=(0.9, 0.999),
|
| 185 |
+
eps=1e-8,
|
| 186 |
+
weight_decay=1e-2,
|
| 187 |
+
amsgrad=False,
|
| 188 |
+
optim_bits=32,
|
| 189 |
+
args=None,
|
| 190 |
+
min_8bit_size=4096,
|
| 191 |
+
):
|
| 192 |
+
"""
|
| 193 |
+
Paged AdamW optimizer.
|
| 194 |
+
|
| 195 |
+
Arguments:
|
| 196 |
+
params (`torch.Tensor`):
|
| 197 |
+
The input parameters to optimize.
|
| 198 |
+
lr (`float`, defaults to 1e-3):
|
| 199 |
+
The learning rate.
|
| 200 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 201 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 202 |
+
eps (`float`, defaults to 1e-8):
|
| 203 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 204 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 205 |
+
The weight decay value for the optimizer.
|
| 206 |
+
amsgrad (`bool`, defaults to `False`):
|
| 207 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 208 |
+
optim_bits (`int`, defaults to 32):
|
| 209 |
+
The number of bits of the optimizer state.
|
| 210 |
+
args (`object`, defaults to `None`):
|
| 211 |
+
An object with additional arguments.
|
| 212 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 213 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 214 |
+
"""
|
| 215 |
+
super().__init__(
|
| 216 |
+
"adam",
|
| 217 |
+
params,
|
| 218 |
+
lr,
|
| 219 |
+
betas,
|
| 220 |
+
eps,
|
| 221 |
+
weight_decay,
|
| 222 |
+
optim_bits,
|
| 223 |
+
args,
|
| 224 |
+
min_8bit_size,
|
| 225 |
+
is_paged=True,
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
class PagedAdamW8bit(Optimizer2State):
|
| 230 |
+
def __init__(
|
| 231 |
+
self,
|
| 232 |
+
params,
|
| 233 |
+
lr=1e-3,
|
| 234 |
+
betas=(0.9, 0.999),
|
| 235 |
+
eps=1e-8,
|
| 236 |
+
weight_decay=1e-2,
|
| 237 |
+
amsgrad=False,
|
| 238 |
+
optim_bits=32,
|
| 239 |
+
args=None,
|
| 240 |
+
min_8bit_size=4096,
|
| 241 |
+
):
|
| 242 |
+
"""
|
| 243 |
+
Paged 8-bit AdamW optimizer.
|
| 244 |
+
|
| 245 |
+
Arguments:
|
| 246 |
+
params (`torch.Tensor`):
|
| 247 |
+
The input parameters to optimize.
|
| 248 |
+
lr (`float`, defaults to 1e-3):
|
| 249 |
+
The learning rate.
|
| 250 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 251 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 252 |
+
eps (`float`, defaults to 1e-8):
|
| 253 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 254 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 255 |
+
The weight decay value for the optimizer.
|
| 256 |
+
amsgrad (`bool`, defaults to `False`):
|
| 257 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 258 |
+
Note: This parameter is not supported in PagedAdamW8bit and must be False.
|
| 259 |
+
optim_bits (`int`, defaults to 32):
|
| 260 |
+
The number of bits of the optimizer state.
|
| 261 |
+
Note: This parameter is not used in PagedAdamW8bit as it always uses 8-bit optimization.
|
| 262 |
+
args (`object`, defaults to `None`):
|
| 263 |
+
An object with additional arguments.
|
| 264 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 265 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 266 |
+
"""
|
| 267 |
+
# Validate unsupported parameters
|
| 268 |
+
if amsgrad:
|
| 269 |
+
raise ValueError("PagedAdamW8bit does not support amsgrad=True")
|
| 270 |
+
|
| 271 |
+
if optim_bits != 32:
|
| 272 |
+
# We allow the default value of 32 to maintain compatibility with the function signature,
|
| 273 |
+
# but any other value is invalid since PagedAdamW8bit always uses 8-bit optimization
|
| 274 |
+
raise ValueError("PagedAdamW8bit only supports optim_bits=32 (default value for compatibility)")
|
| 275 |
+
|
| 276 |
+
super().__init__(
|
| 277 |
+
"adam",
|
| 278 |
+
params,
|
| 279 |
+
lr,
|
| 280 |
+
betas,
|
| 281 |
+
eps,
|
| 282 |
+
weight_decay,
|
| 283 |
+
8, # Hardcoded to 8 bits
|
| 284 |
+
args,
|
| 285 |
+
min_8bit_size,
|
| 286 |
+
is_paged=True,
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
class PagedAdamW32bit(Optimizer2State):
|
| 291 |
+
def __init__(
|
| 292 |
+
self,
|
| 293 |
+
params,
|
| 294 |
+
lr=1e-3,
|
| 295 |
+
betas=(0.9, 0.999),
|
| 296 |
+
eps=1e-8,
|
| 297 |
+
weight_decay=1e-2,
|
| 298 |
+
amsgrad=False,
|
| 299 |
+
optim_bits=32,
|
| 300 |
+
args=None,
|
| 301 |
+
min_8bit_size=4096,
|
| 302 |
+
):
|
| 303 |
+
"""
|
| 304 |
+
Paged 32-bit AdamW optimizer.
|
| 305 |
+
|
| 306 |
+
Arguments:
|
| 307 |
+
params (`torch.Tensor`):
|
| 308 |
+
The input parameters to optimize.
|
| 309 |
+
lr (`float`, defaults to 1e-3):
|
| 310 |
+
The learning rate.
|
| 311 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 312 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 313 |
+
eps (`float`, defaults to 1e-8):
|
| 314 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 315 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 316 |
+
The weight decay value for the optimizer.
|
| 317 |
+
amsgrad (`bool`, defaults to `False`):
|
| 318 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 319 |
+
optim_bits (`int`, defaults to 32):
|
| 320 |
+
The number of bits of the optimizer state.
|
| 321 |
+
args (`object`, defaults to `None`):
|
| 322 |
+
An object with additional arguments.
|
| 323 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 324 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 325 |
+
"""
|
| 326 |
+
super().__init__(
|
| 327 |
+
"adam",
|
| 328 |
+
params,
|
| 329 |
+
lr,
|
| 330 |
+
betas,
|
| 331 |
+
eps,
|
| 332 |
+
weight_decay,
|
| 333 |
+
32,
|
| 334 |
+
args,
|
| 335 |
+
min_8bit_size,
|
| 336 |
+
is_paged=True,
|
| 337 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/ademamix.py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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Iterable
|
| 2 |
+
import math
|
| 3 |
+
from typing import Literal, Optional
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
import bitsandbytes.functional as F
|
| 8 |
+
from bitsandbytes.optim.optimizer import Optimizer2State
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class _ReferenceAdEMAMix(torch.optim.Optimizer):
|
| 12 |
+
"""
|
| 13 |
+
Reference: https://hf.co/papers/2409.03137
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def __init__(
|
| 17 |
+
self,
|
| 18 |
+
params: Iterable[torch.nn.Parameter],
|
| 19 |
+
lr: float = 1e-3,
|
| 20 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 21 |
+
alpha: float = 5.0,
|
| 22 |
+
eps: float = 1e-8,
|
| 23 |
+
weight_decay: float = 1e-2, # default 0.0 or 1e-2?
|
| 24 |
+
t_beta3: Optional[int] = None,
|
| 25 |
+
t_alpha: Optional[int] = None,
|
| 26 |
+
):
|
| 27 |
+
defaults = dict(
|
| 28 |
+
lr=lr, betas=betas, alpha=alpha, eps=eps, weight_decay=weight_decay, t_beta3=t_beta3, t_alpha=t_alpha
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
super().__init__(params, defaults)
|
| 32 |
+
|
| 33 |
+
@torch.no_grad()
|
| 34 |
+
def step(self, closure=None):
|
| 35 |
+
loss = None
|
| 36 |
+
|
| 37 |
+
if closure is not None:
|
| 38 |
+
with torch.enable_grad():
|
| 39 |
+
loss = closure()
|
| 40 |
+
|
| 41 |
+
for group in self.param_groups:
|
| 42 |
+
if "step" in group:
|
| 43 |
+
group["step"] += 1
|
| 44 |
+
else:
|
| 45 |
+
group["step"] = 1
|
| 46 |
+
|
| 47 |
+
lr = group["lr"]
|
| 48 |
+
eps = group["eps"]
|
| 49 |
+
beta1, beta2, beta3 = group["betas"]
|
| 50 |
+
alpha = group["alpha"]
|
| 51 |
+
t_alpha = group["t_alpha"]
|
| 52 |
+
t_beta3 = group["t_beta3"]
|
| 53 |
+
weight_decay = group["weight_decay"]
|
| 54 |
+
|
| 55 |
+
for p in group["params"]:
|
| 56 |
+
if p.grad is None:
|
| 57 |
+
continue
|
| 58 |
+
|
| 59 |
+
grad = p.grad
|
| 60 |
+
state = self.state[p]
|
| 61 |
+
|
| 62 |
+
# State initialization
|
| 63 |
+
if len(state) == 0:
|
| 64 |
+
# For parity with bnb implementation we combine both fast
|
| 65 |
+
# and slow EMA stats into one stacked tensor.
|
| 66 |
+
state["m1_m2"] = p.new_zeros((2, *p.size()))
|
| 67 |
+
state["nu"] = torch.zeros_like(p) # second moment estimate
|
| 68 |
+
|
| 69 |
+
m1, m2, nu = state["m1_m2"][0], state["m1_m2"][1], state["nu"]
|
| 70 |
+
|
| 71 |
+
bias_correction1 = 1 - beta1 ** group["step"]
|
| 72 |
+
|
| 73 |
+
bias_correction2 = 1 - beta2 ** group["step"]
|
| 74 |
+
|
| 75 |
+
# Apply scheduler for alpha
|
| 76 |
+
if t_alpha is not None:
|
| 77 |
+
alpha = min(group["step"] * alpha / t_alpha, alpha)
|
| 78 |
+
|
| 79 |
+
# Apply scheduler for beta3
|
| 80 |
+
if t_beta3 is not None:
|
| 81 |
+
ln_beta1 = math.log(beta1)
|
| 82 |
+
ln_beta3 = math.log(beta3)
|
| 83 |
+
step_scale = group["step"] / t_beta3
|
| 84 |
+
beta3 = min(
|
| 85 |
+
math.exp((ln_beta1 * ln_beta3) / (((1 - step_scale) * ln_beta3) + (step_scale * ln_beta1))),
|
| 86 |
+
beta3,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Update the EMAs
|
| 90 |
+
m1.mul_(beta1).add_(grad, alpha=1 - beta1)
|
| 91 |
+
m2.mul_(beta3).add_(grad, alpha=1 - beta3)
|
| 92 |
+
nu.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
|
| 93 |
+
|
| 94 |
+
# Compute step
|
| 95 |
+
denom = (nu.sqrt() / (bias_correction2**0.5)).add(eps)
|
| 96 |
+
update = (m1.div(bias_correction1) + alpha * m2) / denom
|
| 97 |
+
|
| 98 |
+
# Add weight decay
|
| 99 |
+
update.add_(p, alpha=weight_decay)
|
| 100 |
+
|
| 101 |
+
# Apply update scaled by learning rate
|
| 102 |
+
p.add_(-lr * update)
|
| 103 |
+
|
| 104 |
+
return loss
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class AdEMAMix(Optimizer2State):
|
| 108 |
+
def __init__(
|
| 109 |
+
self,
|
| 110 |
+
params: Iterable[torch.nn.Parameter],
|
| 111 |
+
lr: float = 1e-3,
|
| 112 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 113 |
+
alpha: float = 5.0,
|
| 114 |
+
t_alpha: Optional[int] = None,
|
| 115 |
+
t_beta3: Optional[int] = None,
|
| 116 |
+
eps: float = 1e-8,
|
| 117 |
+
weight_decay: float = 1e-2,
|
| 118 |
+
optim_bits: Literal[8, 32] = 32,
|
| 119 |
+
min_8bit_size: int = 4096,
|
| 120 |
+
is_paged: bool = False,
|
| 121 |
+
):
|
| 122 |
+
super().__init__(
|
| 123 |
+
"ademamix",
|
| 124 |
+
params=params,
|
| 125 |
+
lr=lr,
|
| 126 |
+
betas=betas,
|
| 127 |
+
eps=eps,
|
| 128 |
+
weight_decay=weight_decay,
|
| 129 |
+
optim_bits=optim_bits,
|
| 130 |
+
args=None,
|
| 131 |
+
min_8bit_size=min_8bit_size,
|
| 132 |
+
is_paged=is_paged,
|
| 133 |
+
alpha=alpha,
|
| 134 |
+
t_alpha=t_alpha,
|
| 135 |
+
t_beta3=t_beta3,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
@torch.no_grad()
|
| 139 |
+
def init_state(self, group, p, gindex, pindex):
|
| 140 |
+
# In our AdEMAMix implementation, we use `state` to hold
|
| 141 |
+
# both the fast and slow EMAs. Here we override the base
|
| 142 |
+
# `Optimizer2State` to allocate a buffer twice as large.
|
| 143 |
+
|
| 144 |
+
config = self.get_config(gindex, pindex, group)
|
| 145 |
+
|
| 146 |
+
if config["optim_bits"] == 32:
|
| 147 |
+
dtype = torch.float32
|
| 148 |
+
elif config["optim_bits"] == 8:
|
| 149 |
+
dtype = torch.uint8
|
| 150 |
+
else:
|
| 151 |
+
raise NotImplementedError(f"Amount of optimizer bits not supported: {config['optim_bits']}")
|
| 152 |
+
|
| 153 |
+
if p.numel() < config["min_8bit_size"]:
|
| 154 |
+
dtype = torch.float32
|
| 155 |
+
|
| 156 |
+
state = self.state[p]
|
| 157 |
+
state["step"] = 0
|
| 158 |
+
|
| 159 |
+
if dtype == torch.uint8:
|
| 160 |
+
if "dynamic" not in self.name2qmap:
|
| 161 |
+
self.fill_qmap()
|
| 162 |
+
self.name2qmap["dynamic"] = state["qmap1"] = self.name2qmap["dynamic"].to(p.device)
|
| 163 |
+
self.name2qmap["udynamic"] = state["qmap2"] = self.name2qmap["udynamic"].to(p.device)
|
| 164 |
+
|
| 165 |
+
blocksize = 256
|
| 166 |
+
n = p.numel()
|
| 167 |
+
blocks = (n // blocksize) + bool(n % blocksize)
|
| 168 |
+
|
| 169 |
+
state["absmax1"] = torch.zeros((2, blocks), dtype=torch.float32, device=p.device)
|
| 170 |
+
state["absmax2"] = torch.zeros((blocks,), dtype=torch.float32, device=p.device)
|
| 171 |
+
|
| 172 |
+
state["state1"] = self._get_state_double_buffer(p, dtype=dtype)
|
| 173 |
+
state["state2"] = self.get_state_buffer(p, dtype=dtype)
|
| 174 |
+
|
| 175 |
+
@torch.no_grad()
|
| 176 |
+
def update_step(self, group, p, gindex, pindex):
|
| 177 |
+
config = self.get_config(gindex, pindex, group)
|
| 178 |
+
|
| 179 |
+
if not config["t_alpha"] and not config["t_beta3"]:
|
| 180 |
+
# Not using alpha/beta3 scheduler; we can fall through.
|
| 181 |
+
super().update_step(group, p, gindex, pindex)
|
| 182 |
+
return
|
| 183 |
+
|
| 184 |
+
# Ensure contiguous memory layout
|
| 185 |
+
p.data = p.data.contiguous()
|
| 186 |
+
p.grad = p.grad.contiguous()
|
| 187 |
+
|
| 188 |
+
state = self.state[p]
|
| 189 |
+
grad = p.grad
|
| 190 |
+
|
| 191 |
+
state["step"] += 1
|
| 192 |
+
step = state["step"]
|
| 193 |
+
|
| 194 |
+
beta1, beta2, beta3 = config["betas"]
|
| 195 |
+
alpha = config["alpha"]
|
| 196 |
+
t_alpha = config["t_alpha"]
|
| 197 |
+
t_beta3 = config["t_beta3"]
|
| 198 |
+
|
| 199 |
+
# Apply scheduler for alpha
|
| 200 |
+
if t_alpha:
|
| 201 |
+
alpha_t = min(step * alpha / t_alpha, alpha)
|
| 202 |
+
else:
|
| 203 |
+
alpha_t = alpha
|
| 204 |
+
|
| 205 |
+
# Apply scheduler for beta3
|
| 206 |
+
if t_beta3:
|
| 207 |
+
ln_beta1 = math.log(beta1)
|
| 208 |
+
ln_beta3 = math.log(beta3)
|
| 209 |
+
step_scale = step / t_beta3
|
| 210 |
+
beta3_t = min(
|
| 211 |
+
math.exp((ln_beta1 * ln_beta3) / (((1 - step_scale) * ln_beta3) + (step_scale * ln_beta1))), beta3
|
| 212 |
+
)
|
| 213 |
+
else:
|
| 214 |
+
beta3_t = beta3
|
| 215 |
+
|
| 216 |
+
# Apply updates
|
| 217 |
+
if state["state1"].dtype == torch.float32:
|
| 218 |
+
F.optimizer_update_32bit(
|
| 219 |
+
self.optimizer_name,
|
| 220 |
+
grad,
|
| 221 |
+
p,
|
| 222 |
+
state["state1"],
|
| 223 |
+
beta1,
|
| 224 |
+
config["eps"],
|
| 225 |
+
step,
|
| 226 |
+
config["lr"],
|
| 227 |
+
state["state2"],
|
| 228 |
+
beta2,
|
| 229 |
+
beta3_t,
|
| 230 |
+
alpha_t,
|
| 231 |
+
config["weight_decay"],
|
| 232 |
+
gnorm_scale=1.0,
|
| 233 |
+
unorm_vec=state["unorm_vec"] if config["max_unorm"] > 0.0 else None,
|
| 234 |
+
max_unorm=config["max_unorm"],
|
| 235 |
+
skip_zeros=config["skip_zeros"],
|
| 236 |
+
)
|
| 237 |
+
elif state["state1"].dtype == torch.uint8:
|
| 238 |
+
F.optimizer_update_8bit_blockwise(
|
| 239 |
+
self.optimizer_name,
|
| 240 |
+
grad,
|
| 241 |
+
p,
|
| 242 |
+
state["state1"],
|
| 243 |
+
state["state2"],
|
| 244 |
+
config["betas"][0],
|
| 245 |
+
config["betas"][1],
|
| 246 |
+
beta3_t,
|
| 247 |
+
alpha_t,
|
| 248 |
+
config["eps"],
|
| 249 |
+
step,
|
| 250 |
+
config["lr"],
|
| 251 |
+
state["qmap1"],
|
| 252 |
+
state["qmap2"],
|
| 253 |
+
state["absmax1"],
|
| 254 |
+
state["absmax2"],
|
| 255 |
+
config["weight_decay"],
|
| 256 |
+
gnorm_scale=1.0,
|
| 257 |
+
skip_zeros=config["skip_zeros"],
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
def _get_state_double_buffer(self, p, dtype=torch.float32):
|
| 261 |
+
if not self.is_paged or p.numel() < 1e5:
|
| 262 |
+
return torch.zeros((2, *p.size()), dtype=dtype, device=p.device)
|
| 263 |
+
else:
|
| 264 |
+
buff = F.get_paged(*(2, *p.size()), dtype=dtype, device=p.device)
|
| 265 |
+
F.fill(buff, 0)
|
| 266 |
+
self.page_mng.paged_tensors.append(buff)
|
| 267 |
+
return buff
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
class AdEMAMix8bit(AdEMAMix):
|
| 271 |
+
def __init__(
|
| 272 |
+
self,
|
| 273 |
+
params: Iterable[torch.nn.Parameter],
|
| 274 |
+
lr: float = 1e-3,
|
| 275 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 276 |
+
alpha: float = 5.0,
|
| 277 |
+
t_alpha: Optional[int] = None,
|
| 278 |
+
t_beta3: Optional[int] = None,
|
| 279 |
+
eps: float = 1e-8,
|
| 280 |
+
weight_decay: float = 1e-2,
|
| 281 |
+
min_8bit_size: int = 4096,
|
| 282 |
+
is_paged: bool = False,
|
| 283 |
+
):
|
| 284 |
+
super().__init__(
|
| 285 |
+
params,
|
| 286 |
+
lr=lr,
|
| 287 |
+
betas=betas,
|
| 288 |
+
alpha=alpha,
|
| 289 |
+
t_alpha=t_alpha,
|
| 290 |
+
t_beta3=t_beta3,
|
| 291 |
+
eps=eps,
|
| 292 |
+
weight_decay=weight_decay,
|
| 293 |
+
optim_bits=8,
|
| 294 |
+
min_8bit_size=min_8bit_size,
|
| 295 |
+
is_paged=is_paged,
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
class PagedAdEMAMix8bit(AdEMAMix8bit):
|
| 300 |
+
def __init__(
|
| 301 |
+
self,
|
| 302 |
+
params: Iterable[torch.nn.Parameter],
|
| 303 |
+
lr: float = 1e-3,
|
| 304 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 305 |
+
alpha: float = 5.0,
|
| 306 |
+
t_alpha: Optional[int] = None,
|
| 307 |
+
t_beta3: Optional[int] = None,
|
| 308 |
+
eps: float = 1e-8,
|
| 309 |
+
weight_decay: float = 1e-2,
|
| 310 |
+
min_8bit_size: int = 4096,
|
| 311 |
+
):
|
| 312 |
+
super().__init__(
|
| 313 |
+
params,
|
| 314 |
+
lr=lr,
|
| 315 |
+
betas=betas,
|
| 316 |
+
alpha=alpha,
|
| 317 |
+
t_alpha=t_alpha,
|
| 318 |
+
t_beta3=t_beta3,
|
| 319 |
+
eps=eps,
|
| 320 |
+
weight_decay=weight_decay,
|
| 321 |
+
min_8bit_size=min_8bit_size,
|
| 322 |
+
is_paged=True,
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
class PagedAdEMAMix(AdEMAMix):
|
| 327 |
+
def __init__(
|
| 328 |
+
self,
|
| 329 |
+
params: Iterable[torch.nn.Parameter],
|
| 330 |
+
lr: float = 1e-3,
|
| 331 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 332 |
+
alpha: float = 5.0,
|
| 333 |
+
t_alpha: Optional[int] = None,
|
| 334 |
+
t_beta3: Optional[int] = None,
|
| 335 |
+
eps: float = 1e-8,
|
| 336 |
+
weight_decay: float = 1e-2,
|
| 337 |
+
optim_bits: Literal[8, 32] = 32,
|
| 338 |
+
min_8bit_size: int = 4096,
|
| 339 |
+
):
|
| 340 |
+
super().__init__(
|
| 341 |
+
params,
|
| 342 |
+
lr=lr,
|
| 343 |
+
betas=betas,
|
| 344 |
+
alpha=alpha,
|
| 345 |
+
t_alpha=t_alpha,
|
| 346 |
+
t_beta3=t_beta3,
|
| 347 |
+
eps=eps,
|
| 348 |
+
weight_decay=weight_decay,
|
| 349 |
+
optim_bits=optim_bits,
|
| 350 |
+
min_8bit_size=min_8bit_size,
|
| 351 |
+
is_paged=True,
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
class AdEMAMix32bit(Optimizer2State):
|
| 356 |
+
def __init__(
|
| 357 |
+
self,
|
| 358 |
+
params: Iterable[torch.nn.Parameter],
|
| 359 |
+
lr: float = 1e-3,
|
| 360 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 361 |
+
alpha: float = 5.0,
|
| 362 |
+
t_alpha: Optional[int] = None,
|
| 363 |
+
t_beta3: Optional[int] = None,
|
| 364 |
+
eps: float = 1e-8,
|
| 365 |
+
weight_decay: float = 1e-2,
|
| 366 |
+
min_8bit_size: int = 4096,
|
| 367 |
+
is_paged: bool = False,
|
| 368 |
+
):
|
| 369 |
+
super().__init__(
|
| 370 |
+
"ademamix",
|
| 371 |
+
params=params,
|
| 372 |
+
lr=lr,
|
| 373 |
+
betas=betas,
|
| 374 |
+
eps=eps,
|
| 375 |
+
weight_decay=weight_decay,
|
| 376 |
+
optim_bits=32,
|
| 377 |
+
args=None,
|
| 378 |
+
min_8bit_size=min_8bit_size,
|
| 379 |
+
is_paged=is_paged,
|
| 380 |
+
alpha=alpha,
|
| 381 |
+
t_alpha=t_alpha,
|
| 382 |
+
t_beta3=t_beta3,
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
class PagedAdEMAMix32bit(AdEMAMix32bit):
|
| 387 |
+
def __init__(
|
| 388 |
+
self,
|
| 389 |
+
params: Iterable[torch.nn.Parameter],
|
| 390 |
+
lr: float = 1e-3,
|
| 391 |
+
betas: tuple[float, float, float] = (0.9, 0.999, 0.9999),
|
| 392 |
+
alpha: float = 5.0,
|
| 393 |
+
t_alpha: Optional[int] = None,
|
| 394 |
+
t_beta3: Optional[int] = None,
|
| 395 |
+
eps: float = 1e-8,
|
| 396 |
+
weight_decay: float = 1e-2,
|
| 397 |
+
min_8bit_size: int = 4096,
|
| 398 |
+
):
|
| 399 |
+
super().__init__(
|
| 400 |
+
params,
|
| 401 |
+
lr=lr,
|
| 402 |
+
betas=betas,
|
| 403 |
+
alpha=alpha,
|
| 404 |
+
t_alpha=t_alpha,
|
| 405 |
+
t_beta3=t_beta3,
|
| 406 |
+
eps=eps,
|
| 407 |
+
weight_decay=weight_decay,
|
| 408 |
+
min_8bit_size=min_8bit_size,
|
| 409 |
+
is_paged=True,
|
| 410 |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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from bitsandbytes.optim.optimizer import Optimizer2State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class LAMB(Optimizer2State):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
params,
|
| 12 |
+
lr=1e-3,
|
| 13 |
+
bias_correction=True,
|
| 14 |
+
betas=(0.9, 0.999),
|
| 15 |
+
eps=1e-8,
|
| 16 |
+
weight_decay=0,
|
| 17 |
+
amsgrad=False,
|
| 18 |
+
adam_w_mode=True,
|
| 19 |
+
optim_bits=32,
|
| 20 |
+
args=None,
|
| 21 |
+
min_8bit_size=4096,
|
| 22 |
+
max_unorm=1.0,
|
| 23 |
+
):
|
| 24 |
+
"""
|
| 25 |
+
Base LAMB optimizer.
|
| 26 |
+
|
| 27 |
+
Arguments:
|
| 28 |
+
params (`torch.tensor`):
|
| 29 |
+
The input parameters to optimize.
|
| 30 |
+
lr (`float`, defaults to 1e-3):
|
| 31 |
+
The learning rate.
|
| 32 |
+
bias_correction (`bool`, defaults to `True`):
|
| 33 |
+
Whether to apply bias correction to the first and second-order moments.
|
| 34 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 35 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 36 |
+
eps (`float`, defaults to 1e-8):
|
| 37 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 38 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 39 |
+
The weight decay value for the optimizer.
|
| 40 |
+
amsgrad (`bool`, defaults to `False`):
|
| 41 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 42 |
+
adam_w_mode (`bool`, defaults to `True`):
|
| 43 |
+
Whether to use the AdamW variant.
|
| 44 |
+
optim_bits (`int`, defaults to 32):
|
| 45 |
+
The number of bits of the optimizer state.
|
| 46 |
+
args (`object`, defaults to `None`):
|
| 47 |
+
An object with additional arguments.
|
| 48 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 49 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 50 |
+
max_unorm (`float`, defaults to 1.0):
|
| 51 |
+
The maximum gradient norm.
|
| 52 |
+
"""
|
| 53 |
+
super().__init__(
|
| 54 |
+
"lamb",
|
| 55 |
+
params,
|
| 56 |
+
lr,
|
| 57 |
+
betas,
|
| 58 |
+
eps,
|
| 59 |
+
weight_decay,
|
| 60 |
+
optim_bits,
|
| 61 |
+
args,
|
| 62 |
+
min_8bit_size,
|
| 63 |
+
max_unorm=max_unorm,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class LAMB8bit(Optimizer2State):
|
| 68 |
+
def __init__(
|
| 69 |
+
self,
|
| 70 |
+
params,
|
| 71 |
+
lr=1e-3,
|
| 72 |
+
bias_correction=True,
|
| 73 |
+
betas=(0.9, 0.999),
|
| 74 |
+
eps=1e-8,
|
| 75 |
+
weight_decay=0,
|
| 76 |
+
amsgrad=False,
|
| 77 |
+
adam_w_mode=True,
|
| 78 |
+
args=None,
|
| 79 |
+
min_8bit_size=4096,
|
| 80 |
+
max_unorm=1.0,
|
| 81 |
+
):
|
| 82 |
+
"""
|
| 83 |
+
8-bit LAMB optimizer.
|
| 84 |
+
|
| 85 |
+
Arguments:
|
| 86 |
+
params (`torch.tensor`):
|
| 87 |
+
The input parameters to optimize.
|
| 88 |
+
lr (`float`, defaults to 1e-3):
|
| 89 |
+
The learning rate.
|
| 90 |
+
bias_correction (`bool`, defaults to `True`):
|
| 91 |
+
Whether to apply bias correction to the first and second-order moments.
|
| 92 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 93 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 94 |
+
eps (`float`, defaults to 1e-8):
|
| 95 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 96 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 97 |
+
The weight decay value for the optimizer.
|
| 98 |
+
amsgrad (`bool`, defaults to `False`):
|
| 99 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 100 |
+
Note: This parameter is not supported in LAMB8bit and must be False.
|
| 101 |
+
adam_w_mode (`bool`, defaults to `True`):
|
| 102 |
+
Whether to use the AdamW variant.
|
| 103 |
+
args (`object`, defaults to `None`):
|
| 104 |
+
An object with additional arguments.
|
| 105 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 106 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 107 |
+
max_unorm (`float`, defaults to 1.0):
|
| 108 |
+
The maximum update norm for trust-ratio clipping.
|
| 109 |
+
Note: This parameter is not supported in LAMB8bit and must be left at the
|
| 110 |
+
default 1.0. The 8-bit blockwise update does not implement update-norm
|
| 111 |
+
clipping; it is honored by the 32-bit LAMB / LAMB32bit optimizers.
|
| 112 |
+
"""
|
| 113 |
+
# Validate unsupported parameters
|
| 114 |
+
if amsgrad:
|
| 115 |
+
raise ValueError("LAMB8bit does not support amsgrad=True")
|
| 116 |
+
|
| 117 |
+
if max_unorm != 1.0:
|
| 118 |
+
# We allow the default value of 1.0 to maintain compatibility with the function
|
| 119 |
+
# signature, but the 8-bit blockwise update does not implement update-norm
|
| 120 |
+
# clipping, so any other value would be silently ignored.
|
| 121 |
+
raise ValueError("LAMB8bit only supports max_unorm=1.0 (default value for compatibility)")
|
| 122 |
+
|
| 123 |
+
super().__init__(
|
| 124 |
+
"lamb",
|
| 125 |
+
params,
|
| 126 |
+
lr,
|
| 127 |
+
betas,
|
| 128 |
+
eps,
|
| 129 |
+
weight_decay,
|
| 130 |
+
8,
|
| 131 |
+
args,
|
| 132 |
+
min_8bit_size,
|
| 133 |
+
max_unorm=max_unorm,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
class LAMB32bit(Optimizer2State):
|
| 138 |
+
def __init__(
|
| 139 |
+
self,
|
| 140 |
+
params,
|
| 141 |
+
lr=1e-3,
|
| 142 |
+
bias_correction=True,
|
| 143 |
+
betas=(0.9, 0.999),
|
| 144 |
+
eps=1e-8,
|
| 145 |
+
weight_decay=0,
|
| 146 |
+
amsgrad=False,
|
| 147 |
+
adam_w_mode=True,
|
| 148 |
+
args=None,
|
| 149 |
+
min_8bit_size=4096,
|
| 150 |
+
max_unorm=1.0,
|
| 151 |
+
):
|
| 152 |
+
"""
|
| 153 |
+
32-bit LAMB optimizer.
|
| 154 |
+
|
| 155 |
+
Arguments:
|
| 156 |
+
params (`torch.tensor`):
|
| 157 |
+
The input parameters to optimize.
|
| 158 |
+
lr (`float`, defaults to 1e-3):
|
| 159 |
+
The learning rate.
|
| 160 |
+
bias_correction (`bool`, defaults to `True`):
|
| 161 |
+
Whether to apply bias correction to the first and second-order moments.
|
| 162 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 163 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 164 |
+
eps (`float`, defaults to 1e-8):
|
| 165 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 166 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 167 |
+
The weight decay value for the optimizer.
|
| 168 |
+
amsgrad (`bool`, defaults to `False`):
|
| 169 |
+
Whether to use the [AMSGrad](https://hf.co/papers/1904.09237) variant of Adam that uses the maximum of past squared gradients instead.
|
| 170 |
+
adam_w_mode (`bool`, defaults to `True`):
|
| 171 |
+
Whether to use the AdamW variant.
|
| 172 |
+
args (`object`, defaults to `None`):
|
| 173 |
+
An object with additional arguments.
|
| 174 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 175 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 176 |
+
max_unorm (`float`, defaults to 1.0):
|
| 177 |
+
The maximum gradient norm.
|
| 178 |
+
"""
|
| 179 |
+
super().__init__(
|
| 180 |
+
"lamb",
|
| 181 |
+
params,
|
| 182 |
+
lr,
|
| 183 |
+
betas,
|
| 184 |
+
eps,
|
| 185 |
+
weight_decay,
|
| 186 |
+
32,
|
| 187 |
+
args,
|
| 188 |
+
min_8bit_size,
|
| 189 |
+
max_unorm=max_unorm,
|
| 190 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/lars.py
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
import torch
|
| 6 |
+
from torch.optim import Optimizer
|
| 7 |
+
|
| 8 |
+
from bitsandbytes.optim.optimizer import Optimizer1State
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class LARS(Optimizer1State):
|
| 12 |
+
def __init__(
|
| 13 |
+
self,
|
| 14 |
+
params,
|
| 15 |
+
lr,
|
| 16 |
+
momentum=0,
|
| 17 |
+
dampening=0,
|
| 18 |
+
weight_decay=0,
|
| 19 |
+
nesterov=False,
|
| 20 |
+
optim_bits=32,
|
| 21 |
+
args=None,
|
| 22 |
+
min_8bit_size=4096,
|
| 23 |
+
max_unorm=0.02,
|
| 24 |
+
):
|
| 25 |
+
"""
|
| 26 |
+
Base LARS optimizer.
|
| 27 |
+
|
| 28 |
+
Arguments:
|
| 29 |
+
params (`torch.tensor`):
|
| 30 |
+
The input parameters to optimize.
|
| 31 |
+
lr (`float`):
|
| 32 |
+
The learning rate.
|
| 33 |
+
momentum (`float`, defaults to 0):
|
| 34 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 35 |
+
dampening (`float`, defaults to 0):
|
| 36 |
+
The dampening value reduces the momentum of the optimizer.
|
| 37 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 38 |
+
The weight decay value for the optimizer.
|
| 39 |
+
nesterov (`bool`, defaults to `False`):
|
| 40 |
+
Whether to use Nesterov momentum.
|
| 41 |
+
optim_bits (`int`, defaults to 32):
|
| 42 |
+
The number of bits of the optimizer state.
|
| 43 |
+
args (`object`, defaults to `None`):
|
| 44 |
+
An object with additional arguments.
|
| 45 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 46 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 47 |
+
max_unorm (`float`, defaults to 0.02):
|
| 48 |
+
The maximum gradient norm.
|
| 49 |
+
"""
|
| 50 |
+
if momentum == 0:
|
| 51 |
+
raise NotImplementedError("LARS without momentum is not supported!")
|
| 52 |
+
super().__init__(
|
| 53 |
+
"lars",
|
| 54 |
+
params,
|
| 55 |
+
lr,
|
| 56 |
+
(momentum, dampening),
|
| 57 |
+
0.0,
|
| 58 |
+
weight_decay,
|
| 59 |
+
optim_bits,
|
| 60 |
+
args,
|
| 61 |
+
min_8bit_size,
|
| 62 |
+
max_unorm=max_unorm,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class LARS8bit(Optimizer1State):
|
| 67 |
+
def __init__(
|
| 68 |
+
self,
|
| 69 |
+
params,
|
| 70 |
+
lr,
|
| 71 |
+
momentum=0,
|
| 72 |
+
dampening=0,
|
| 73 |
+
weight_decay=0,
|
| 74 |
+
nesterov=False,
|
| 75 |
+
args=None,
|
| 76 |
+
min_8bit_size=4096,
|
| 77 |
+
max_unorm=0.02,
|
| 78 |
+
):
|
| 79 |
+
"""
|
| 80 |
+
8-bit LARS optimizer.
|
| 81 |
+
|
| 82 |
+
Arguments:
|
| 83 |
+
params (`torch.tensor`):
|
| 84 |
+
The input parameters to optimize.
|
| 85 |
+
lr (`float`):
|
| 86 |
+
The learning rate.
|
| 87 |
+
momentum (`float`, defaults to 0):
|
| 88 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 89 |
+
dampening (`float`, defaults to 0):
|
| 90 |
+
The dampening value reduces the momentum of the optimizer.
|
| 91 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 92 |
+
The weight decay value for the optimizer.
|
| 93 |
+
nesterov (`bool`, defaults to `False`):
|
| 94 |
+
Whether to use Nesterov momentum.
|
| 95 |
+
args (`object`, defaults to `None`):
|
| 96 |
+
An object with additional arguments.
|
| 97 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 98 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 99 |
+
max_unorm (`float`, defaults to 0.02):
|
| 100 |
+
The maximum gradient norm.
|
| 101 |
+
"""
|
| 102 |
+
if momentum == 0:
|
| 103 |
+
raise NotImplementedError("LARS without momentum is not supported!")
|
| 104 |
+
super().__init__(
|
| 105 |
+
"lars",
|
| 106 |
+
params,
|
| 107 |
+
lr,
|
| 108 |
+
(momentum, dampening),
|
| 109 |
+
0.0,
|
| 110 |
+
weight_decay,
|
| 111 |
+
8,
|
| 112 |
+
args,
|
| 113 |
+
min_8bit_size,
|
| 114 |
+
max_unorm=max_unorm,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class LARS32bit(Optimizer1State):
|
| 119 |
+
def __init__(
|
| 120 |
+
self,
|
| 121 |
+
params,
|
| 122 |
+
lr,
|
| 123 |
+
momentum=0,
|
| 124 |
+
dampening=0,
|
| 125 |
+
weight_decay=0,
|
| 126 |
+
nesterov=False,
|
| 127 |
+
args=None,
|
| 128 |
+
min_8bit_size=4096,
|
| 129 |
+
max_unorm=0.02,
|
| 130 |
+
):
|
| 131 |
+
"""
|
| 132 |
+
32-bit LARS optimizer.
|
| 133 |
+
|
| 134 |
+
Arguments:
|
| 135 |
+
params (`torch.tensor`):
|
| 136 |
+
The input parameters to optimize.
|
| 137 |
+
lr (`float`):
|
| 138 |
+
The learning rate.
|
| 139 |
+
momentum (`float`, defaults to 0):
|
| 140 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 141 |
+
dampening (`float`, defaults to 0):
|
| 142 |
+
The dampening value reduces the momentum of the optimizer.
|
| 143 |
+
weight_decay (`float`, defaults to 1e-2):
|
| 144 |
+
The weight decay value for the optimizer.
|
| 145 |
+
nesterov (`bool`, defaults to `False`):
|
| 146 |
+
Whether to use Nesterov momentum.
|
| 147 |
+
args (`object`, defaults to `None`):
|
| 148 |
+
An object with additional arguments.
|
| 149 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 150 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 151 |
+
max_unorm (`float`, defaults to 0.02):
|
| 152 |
+
The maximum gradient norm.
|
| 153 |
+
"""
|
| 154 |
+
if momentum == 0:
|
| 155 |
+
raise NotImplementedError("LARS without momentum is not supported!")
|
| 156 |
+
super().__init__(
|
| 157 |
+
"lars",
|
| 158 |
+
params,
|
| 159 |
+
lr,
|
| 160 |
+
(momentum, dampening),
|
| 161 |
+
0.0,
|
| 162 |
+
weight_decay,
|
| 163 |
+
32,
|
| 164 |
+
args,
|
| 165 |
+
min_8bit_size,
|
| 166 |
+
max_unorm=max_unorm,
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
class PytorchLARS(Optimizer):
|
| 171 |
+
def __init__(
|
| 172 |
+
self,
|
| 173 |
+
params,
|
| 174 |
+
lr=0.01,
|
| 175 |
+
momentum=0,
|
| 176 |
+
dampening=0,
|
| 177 |
+
weight_decay=0,
|
| 178 |
+
nesterov=False,
|
| 179 |
+
max_unorm=0.02,
|
| 180 |
+
):
|
| 181 |
+
if lr < 0.0:
|
| 182 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 183 |
+
if momentum < 0.0:
|
| 184 |
+
raise ValueError(f"Invalid momentum value: {momentum}")
|
| 185 |
+
if weight_decay < 0.0:
|
| 186 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 187 |
+
|
| 188 |
+
defaults = dict(
|
| 189 |
+
lr=lr,
|
| 190 |
+
momentum=momentum,
|
| 191 |
+
dampening=dampening,
|
| 192 |
+
weight_decay=weight_decay,
|
| 193 |
+
nesterov=nesterov,
|
| 194 |
+
max_unorm=max_unorm,
|
| 195 |
+
)
|
| 196 |
+
if nesterov and (momentum <= 0 or dampening != 0):
|
| 197 |
+
raise ValueError("Nesterov momentum requires a momentum and zero dampening")
|
| 198 |
+
super().__init__(params, defaults)
|
| 199 |
+
|
| 200 |
+
def __setstate__(self, state):
|
| 201 |
+
super().__setstate__(state)
|
| 202 |
+
for group in self.param_groups:
|
| 203 |
+
group.setdefault("nesterov", False)
|
| 204 |
+
|
| 205 |
+
@torch.no_grad()
|
| 206 |
+
def step(self, closure=None):
|
| 207 |
+
"""Performs a single optimization step.
|
| 208 |
+
|
| 209 |
+
Args:
|
| 210 |
+
closure (callable, optional): A closure that reevaluates the model
|
| 211 |
+
and returns the loss.
|
| 212 |
+
"""
|
| 213 |
+
loss = None
|
| 214 |
+
if closure is not None:
|
| 215 |
+
with torch.enable_grad():
|
| 216 |
+
loss = closure()
|
| 217 |
+
|
| 218 |
+
for group in self.param_groups:
|
| 219 |
+
weight_decay = group["weight_decay"]
|
| 220 |
+
momentum = group["momentum"]
|
| 221 |
+
dampening = group["dampening"]
|
| 222 |
+
nesterov = group["nesterov"]
|
| 223 |
+
max_unorm = group["max_unorm"]
|
| 224 |
+
lr = group["lr"]
|
| 225 |
+
|
| 226 |
+
for p in group["params"]:
|
| 227 |
+
if p.grad is None:
|
| 228 |
+
continue
|
| 229 |
+
|
| 230 |
+
state = self.state[p]
|
| 231 |
+
d_p = p.grad
|
| 232 |
+
if weight_decay != 0:
|
| 233 |
+
d_p = d_p.add(p, alpha=weight_decay)
|
| 234 |
+
|
| 235 |
+
if momentum != 0:
|
| 236 |
+
buf = state.get("momentum_buffer", None)
|
| 237 |
+
|
| 238 |
+
if buf is None:
|
| 239 |
+
buf = torch.clone(d_p).detach()
|
| 240 |
+
state["momentum_buffer"] = buf
|
| 241 |
+
else:
|
| 242 |
+
buf.mul_(momentum).add_(d_p, alpha=1 - dampening)
|
| 243 |
+
|
| 244 |
+
if nesterov:
|
| 245 |
+
update = d_p + buf * momentum
|
| 246 |
+
else:
|
| 247 |
+
update = buf
|
| 248 |
+
|
| 249 |
+
update_scale = 1.0
|
| 250 |
+
if max_unorm > 0.0:
|
| 251 |
+
assert p.dtype == torch.float32
|
| 252 |
+
pnorm = torch.norm(p.detach())
|
| 253 |
+
unorm = torch.norm(update)
|
| 254 |
+
if unorm > max_unorm * pnorm:
|
| 255 |
+
update_scale = max_unorm * pnorm / unorm
|
| 256 |
+
|
| 257 |
+
p.add_(update, alpha=-lr * update_scale)
|
| 258 |
+
|
| 259 |
+
return loss
|
venv/lib/python3.11/site-packages/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from bitsandbytes.optim.optimizer import Optimizer1State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Lion(Optimizer1State):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
params,
|
| 12 |
+
lr=1e-4,
|
| 13 |
+
betas=(0.9, 0.99),
|
| 14 |
+
weight_decay=0,
|
| 15 |
+
optim_bits=32,
|
| 16 |
+
args=None,
|
| 17 |
+
min_8bit_size=4096,
|
| 18 |
+
is_paged=False,
|
| 19 |
+
):
|
| 20 |
+
"""
|
| 21 |
+
Base Lion optimizer.
|
| 22 |
+
|
| 23 |
+
Arguments:
|
| 24 |
+
params (`torch.tensor`):
|
| 25 |
+
The input parameters to optimize.
|
| 26 |
+
lr (`float`, defaults to 1e-4):
|
| 27 |
+
The learning rate.
|
| 28 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 29 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 30 |
+
weight_decay (`float`, defaults to 0):
|
| 31 |
+
The weight decay value for the optimizer.
|
| 32 |
+
optim_bits (`int`, defaults to 32):
|
| 33 |
+
The number of bits of the optimizer state.
|
| 34 |
+
args (`object`, defaults to `None`):
|
| 35 |
+
An object with additional arguments.
|
| 36 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 37 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 38 |
+
is_paged (`bool`, defaults to `False`):
|
| 39 |
+
Whether the optimizer is a paged optimizer or not.
|
| 40 |
+
"""
|
| 41 |
+
super().__init__(
|
| 42 |
+
"lion",
|
| 43 |
+
params,
|
| 44 |
+
lr,
|
| 45 |
+
betas,
|
| 46 |
+
0.0,
|
| 47 |
+
weight_decay,
|
| 48 |
+
optim_bits,
|
| 49 |
+
args,
|
| 50 |
+
min_8bit_size,
|
| 51 |
+
is_paged=is_paged,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class Lion8bit(Optimizer1State):
|
| 56 |
+
def __init__(
|
| 57 |
+
self,
|
| 58 |
+
params,
|
| 59 |
+
lr=1e-4,
|
| 60 |
+
betas=(0.9, 0.99),
|
| 61 |
+
weight_decay=0,
|
| 62 |
+
args=None,
|
| 63 |
+
min_8bit_size=4096,
|
| 64 |
+
is_paged=False,
|
| 65 |
+
):
|
| 66 |
+
"""
|
| 67 |
+
8-bit Lion optimizer.
|
| 68 |
+
|
| 69 |
+
Arguments:
|
| 70 |
+
params (`torch.tensor`):
|
| 71 |
+
The input parameters to optimize.
|
| 72 |
+
lr (`float`, defaults to 1e-4):
|
| 73 |
+
The learning rate.
|
| 74 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 75 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 76 |
+
weight_decay (`float`, defaults to 0):
|
| 77 |
+
The weight decay value for the optimizer.
|
| 78 |
+
args (`object`, defaults to `None`):
|
| 79 |
+
An object with additional arguments.
|
| 80 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 81 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 82 |
+
is_paged (`bool`, defaults to `False`):
|
| 83 |
+
Whether the optimizer is a paged optimizer or not.
|
| 84 |
+
"""
|
| 85 |
+
super().__init__(
|
| 86 |
+
"lion",
|
| 87 |
+
params,
|
| 88 |
+
lr,
|
| 89 |
+
betas,
|
| 90 |
+
0.0,
|
| 91 |
+
weight_decay,
|
| 92 |
+
8,
|
| 93 |
+
args,
|
| 94 |
+
min_8bit_size,
|
| 95 |
+
is_paged=is_paged,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class Lion32bit(Optimizer1State):
|
| 100 |
+
def __init__(
|
| 101 |
+
self,
|
| 102 |
+
params,
|
| 103 |
+
lr=1e-4,
|
| 104 |
+
betas=(0.9, 0.99),
|
| 105 |
+
weight_decay=0,
|
| 106 |
+
args=None,
|
| 107 |
+
min_8bit_size=4096,
|
| 108 |
+
is_paged=False,
|
| 109 |
+
):
|
| 110 |
+
"""
|
| 111 |
+
32-bit Lion optimizer.
|
| 112 |
+
|
| 113 |
+
Arguments:
|
| 114 |
+
params (`torch.tensor`):
|
| 115 |
+
The input parameters to optimize.
|
| 116 |
+
lr (`float`, defaults to 1e-4):
|
| 117 |
+
The learning rate.
|
| 118 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 119 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 120 |
+
weight_decay (`float`, defaults to 0):
|
| 121 |
+
The weight decay value for the optimizer.
|
| 122 |
+
args (`object`, defaults to `None`):
|
| 123 |
+
An object with additional arguments.
|
| 124 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 125 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 126 |
+
is_paged (`bool`, defaults to `False`):
|
| 127 |
+
Whether the optimizer is a paged optimizer or not.
|
| 128 |
+
"""
|
| 129 |
+
super().__init__(
|
| 130 |
+
"lion",
|
| 131 |
+
params,
|
| 132 |
+
lr,
|
| 133 |
+
betas,
|
| 134 |
+
0.0,
|
| 135 |
+
weight_decay,
|
| 136 |
+
32,
|
| 137 |
+
args,
|
| 138 |
+
min_8bit_size,
|
| 139 |
+
is_paged=is_paged,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class PagedLion(Optimizer1State):
|
| 144 |
+
def __init__(
|
| 145 |
+
self,
|
| 146 |
+
params,
|
| 147 |
+
lr=1e-4,
|
| 148 |
+
betas=(0.9, 0.99),
|
| 149 |
+
weight_decay=0,
|
| 150 |
+
optim_bits=32,
|
| 151 |
+
args=None,
|
| 152 |
+
min_8bit_size=4096,
|
| 153 |
+
):
|
| 154 |
+
"""
|
| 155 |
+
Paged Lion optimizer.
|
| 156 |
+
|
| 157 |
+
Arguments:
|
| 158 |
+
params (`torch.tensor`):
|
| 159 |
+
The input parameters to optimize.
|
| 160 |
+
lr (`float`, defaults to 1e-4):
|
| 161 |
+
The learning rate.
|
| 162 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 163 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 164 |
+
weight_decay (`float`, defaults to 0):
|
| 165 |
+
The weight decay value for the optimizer.
|
| 166 |
+
optim_bits (`int`, defaults to 32):
|
| 167 |
+
The number of bits of the optimizer state.
|
| 168 |
+
args (`object`, defaults to `None`):
|
| 169 |
+
An object with additional arguments.
|
| 170 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 171 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 172 |
+
"""
|
| 173 |
+
super().__init__(
|
| 174 |
+
"lion",
|
| 175 |
+
params,
|
| 176 |
+
lr,
|
| 177 |
+
betas,
|
| 178 |
+
0.0,
|
| 179 |
+
weight_decay,
|
| 180 |
+
optim_bits,
|
| 181 |
+
args,
|
| 182 |
+
min_8bit_size,
|
| 183 |
+
is_paged=True,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
class PagedLion8bit(Optimizer1State):
|
| 188 |
+
def __init__(
|
| 189 |
+
self,
|
| 190 |
+
params,
|
| 191 |
+
lr=1e-4,
|
| 192 |
+
betas=(0.9, 0.99),
|
| 193 |
+
weight_decay=0,
|
| 194 |
+
args=None,
|
| 195 |
+
min_8bit_size=4096,
|
| 196 |
+
):
|
| 197 |
+
"""
|
| 198 |
+
Paged 8-bit Lion optimizer.
|
| 199 |
+
|
| 200 |
+
Arguments:
|
| 201 |
+
params (`torch.tensor`):
|
| 202 |
+
The input parameters to optimize.
|
| 203 |
+
lr (`float`, defaults to 1e-4):
|
| 204 |
+
The learning rate.
|
| 205 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 206 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 207 |
+
weight_decay (`float`, defaults to 0):
|
| 208 |
+
The weight decay value for the optimizer.
|
| 209 |
+
args (`object`, defaults to `None`):
|
| 210 |
+
An object with additional arguments.
|
| 211 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 212 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 213 |
+
"""
|
| 214 |
+
super().__init__(
|
| 215 |
+
"lion",
|
| 216 |
+
params,
|
| 217 |
+
lr,
|
| 218 |
+
betas,
|
| 219 |
+
0.0,
|
| 220 |
+
weight_decay,
|
| 221 |
+
8,
|
| 222 |
+
args,
|
| 223 |
+
min_8bit_size,
|
| 224 |
+
is_paged=True,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
class PagedLion32bit(Optimizer1State):
|
| 229 |
+
def __init__(
|
| 230 |
+
self,
|
| 231 |
+
params,
|
| 232 |
+
lr=1e-4,
|
| 233 |
+
betas=(0.9, 0.99),
|
| 234 |
+
weight_decay=0,
|
| 235 |
+
args=None,
|
| 236 |
+
min_8bit_size=4096,
|
| 237 |
+
):
|
| 238 |
+
"""
|
| 239 |
+
Paged 32-bit Lion optimizer.
|
| 240 |
+
|
| 241 |
+
Arguments:
|
| 242 |
+
params (`torch.tensor`):
|
| 243 |
+
The input parameters to optimize.
|
| 244 |
+
lr (`float`, defaults to 1e-4):
|
| 245 |
+
The learning rate.
|
| 246 |
+
betas (`tuple(float, float)`, defaults to (0.9, 0.999)):
|
| 247 |
+
The beta values are the decay rates of the first and second-order moment of the optimizer.
|
| 248 |
+
weight_decay (`float`, defaults to 0):
|
| 249 |
+
The weight decay value for the optimizer.
|
| 250 |
+
args (`object`, defaults to `None`):
|
| 251 |
+
An object with additional arguments.
|
| 252 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 253 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 254 |
+
"""
|
| 255 |
+
super().__init__(
|
| 256 |
+
"lion",
|
| 257 |
+
params,
|
| 258 |
+
lr,
|
| 259 |
+
betas,
|
| 260 |
+
0.0,
|
| 261 |
+
weight_decay,
|
| 262 |
+
32,
|
| 263 |
+
args,
|
| 264 |
+
min_8bit_size,
|
| 265 |
+
is_paged=True,
|
| 266 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/optimizer.py
ADDED
|
@@ -0,0 +1,756 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from collections import abc as container_abcs, defaultdict
|
| 6 |
+
from copy import deepcopy
|
| 7 |
+
from itertools import chain
|
| 8 |
+
import logging
|
| 9 |
+
from typing import Optional
|
| 10 |
+
import warnings
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
import bitsandbytes.functional as F
|
| 15 |
+
from bitsandbytes.utils import sync_gpu
|
| 16 |
+
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class MockArgs:
|
| 21 |
+
def __init__(self, initial_data):
|
| 22 |
+
for key in initial_data:
|
| 23 |
+
setattr(self, key, initial_data[key])
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class GlobalOptimManager:
|
| 27 |
+
"""
|
| 28 |
+
A global optimizer manager for enabling custom optimizer configs.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
_instance = None
|
| 32 |
+
|
| 33 |
+
def __init__(self):
|
| 34 |
+
raise RuntimeError("Call get_instance() instead")
|
| 35 |
+
|
| 36 |
+
def initialize(self):
|
| 37 |
+
self.pid2config = {}
|
| 38 |
+
self.index2config = {}
|
| 39 |
+
self.optimizer = None
|
| 40 |
+
self.uses_config_override = False
|
| 41 |
+
self.module_weight_config_triple = []
|
| 42 |
+
|
| 43 |
+
@classmethod
|
| 44 |
+
def get_instance(cls):
|
| 45 |
+
if cls._instance is None:
|
| 46 |
+
cls._instance = cls.__new__(cls)
|
| 47 |
+
cls._instance.initialize()
|
| 48 |
+
return cls._instance
|
| 49 |
+
|
| 50 |
+
def register_parameters(self, params):
|
| 51 |
+
param_groups = list(params)
|
| 52 |
+
if not isinstance(param_groups[0], dict):
|
| 53 |
+
param_groups = [{"params": param_groups}]
|
| 54 |
+
|
| 55 |
+
for group_index, group in enumerate(param_groups):
|
| 56 |
+
for p_index, p in enumerate(group["params"]):
|
| 57 |
+
if id(p) in self.pid2config:
|
| 58 |
+
self.index2config[(group_index, p_index)] = self.pid2config[id(p)]
|
| 59 |
+
|
| 60 |
+
def override_config(self, parameters, key=None, value=None, key_value_dict=None):
|
| 61 |
+
"""
|
| 62 |
+
Override initial optimizer config with specific hyperparameters.
|
| 63 |
+
|
| 64 |
+
The key-values of the optimizer config for the input parameters are overridden
|
| 65 |
+
This can be both, optimizer parameters like `betas` or `lr`, or it can be
|
| 66 |
+
8-bit specific parameters like `optim_bits`.
|
| 67 |
+
|
| 68 |
+
Arguments:
|
| 69 |
+
parameters (`torch.Tensor` or `list(torch.Tensors)`):
|
| 70 |
+
The input parameters.
|
| 71 |
+
key (`str`):
|
| 72 |
+
The hyperparameter to override.
|
| 73 |
+
value:
|
| 74 |
+
The hyperparameter value.
|
| 75 |
+
key_value_dict (`dict`):
|
| 76 |
+
A dictionary with multiple key-values to override.
|
| 77 |
+
|
| 78 |
+
Example:
|
| 79 |
+
|
| 80 |
+
```py
|
| 81 |
+
import torch
|
| 82 |
+
import bitsandbytes as bnb
|
| 83 |
+
|
| 84 |
+
mng = bnb.optim.GlobalOptimManager.get_instance()
|
| 85 |
+
|
| 86 |
+
model = MyModel()
|
| 87 |
+
mng.register_parameters(model.parameters()) # 1. register parameters while still on CPU
|
| 88 |
+
|
| 89 |
+
model = model.cuda()
|
| 90 |
+
# use 8-bit optimizer states for all parameters
|
| 91 |
+
adam = bnb.optim.Adam(model.parameters(), lr=0.001, optim_bits=8)
|
| 92 |
+
|
| 93 |
+
# 2. override: the parameter model.fc1.weight now uses 32-bit Adam
|
| 94 |
+
mng.override_config(model.fc1.weight, 'optim_bits', 32)
|
| 95 |
+
```
|
| 96 |
+
"""
|
| 97 |
+
self.uses_config_override = True
|
| 98 |
+
if isinstance(parameters, torch.nn.Parameter):
|
| 99 |
+
parameters = [parameters]
|
| 100 |
+
if isinstance(parameters, torch.Tensor):
|
| 101 |
+
parameters = [parameters]
|
| 102 |
+
if key is not None and value is not None:
|
| 103 |
+
assert key_value_dict is None
|
| 104 |
+
key_value_dict = {key: value}
|
| 105 |
+
|
| 106 |
+
if key_value_dict is not None:
|
| 107 |
+
for p in parameters:
|
| 108 |
+
if id(p) in self.pid2config:
|
| 109 |
+
self.pid2config[id(p)].update(key_value_dict)
|
| 110 |
+
else:
|
| 111 |
+
self.pid2config[id(p)] = key_value_dict
|
| 112 |
+
|
| 113 |
+
def register_module_override(self, module, param_name, config):
|
| 114 |
+
self.module_weight_config_triple.append((module, param_name, config))
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class Optimizer8bit(torch.optim.Optimizer):
|
| 118 |
+
_FSDP_WRAPPED_QUANT_STATE_KEY = "__bnb_optimizer_quant_state__"
|
| 119 |
+
|
| 120 |
+
def __init__(self, params, defaults, optim_bits=32, is_paged=False):
|
| 121 |
+
"""
|
| 122 |
+
Base 8-bit optimizer class.
|
| 123 |
+
|
| 124 |
+
Arguments:
|
| 125 |
+
params (`torch.Tensor`):
|
| 126 |
+
The input parameters to optimize.
|
| 127 |
+
optim_bits (`int`, defaults to 32):
|
| 128 |
+
The number of bits of the optimizer state.
|
| 129 |
+
is_paged (`bool`, defaults to `False`):
|
| 130 |
+
Whether the optimizer is a paged optimizer or not.
|
| 131 |
+
"""
|
| 132 |
+
super().__init__(params, defaults)
|
| 133 |
+
self.initialized = False
|
| 134 |
+
self.name2qmap = {}
|
| 135 |
+
self.is_paged = is_paged
|
| 136 |
+
self.page_mng = F.GlobalPageManager.get_instance()
|
| 137 |
+
|
| 138 |
+
self.mng = GlobalOptimManager.get_instance()
|
| 139 |
+
self.non_castable_tensor_keys = {
|
| 140 |
+
"qmap1",
|
| 141 |
+
"qmap2",
|
| 142 |
+
"max1",
|
| 143 |
+
"max2",
|
| 144 |
+
"new_max1",
|
| 145 |
+
"new_max2",
|
| 146 |
+
"state1",
|
| 147 |
+
"state2",
|
| 148 |
+
"gnorm_vec",
|
| 149 |
+
"absmax1",
|
| 150 |
+
"absmax2",
|
| 151 |
+
"unorm_vec",
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
if optim_bits == 8:
|
| 155 |
+
self.fill_qmap()
|
| 156 |
+
|
| 157 |
+
def fill_qmap(self):
|
| 158 |
+
self.name2qmap["dynamic"] = F.create_dynamic_map(signed=True)
|
| 159 |
+
self.name2qmap["udynamic"] = F.create_dynamic_map(signed=False)
|
| 160 |
+
|
| 161 |
+
def state_dict(self):
|
| 162 |
+
"""Return optimizer state, wrapping quantization tensors for FSDP compatibility.
|
| 163 |
+
|
| 164 |
+
FSDP's full_optim_state_dict gathers all tensor states across ranks.
|
| 165 |
+
Quantization states (state1, state2, absmax, etc.) have different shapes
|
| 166 |
+
than model parameters, causing gather operations to fail. By wrapping
|
| 167 |
+
these tensors in a nested dict, FSDP skips them during gathering.
|
| 168 |
+
"""
|
| 169 |
+
state_dict = super().state_dict()
|
| 170 |
+
|
| 171 |
+
# Deep copy the state to avoid modifying the original optimizer state
|
| 172 |
+
# PyTorch's state_dict() only does a shallow copy
|
| 173 |
+
state_dict["state"] = {
|
| 174 |
+
k: {kk: vv for kk, vv in v.items()} if isinstance(v, dict) else v for k, v in state_dict["state"].items()
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
# Wrap quantization-specific tensors in a nested dict to hide from FSDP
|
| 178 |
+
for param_state in state_dict["state"].values():
|
| 179 |
+
if isinstance(param_state, dict):
|
| 180 |
+
quant_state = {}
|
| 181 |
+
keys_to_wrap = [k for k in param_state if k in self.non_castable_tensor_keys]
|
| 182 |
+
for key in keys_to_wrap:
|
| 183 |
+
quant_state[key] = param_state.pop(key)
|
| 184 |
+
if quant_state:
|
| 185 |
+
param_state[self._FSDP_WRAPPED_QUANT_STATE_KEY] = quant_state
|
| 186 |
+
|
| 187 |
+
return state_dict
|
| 188 |
+
|
| 189 |
+
def __setstate__(self, state):
|
| 190 |
+
super().__setstate__(state)
|
| 191 |
+
|
| 192 |
+
def load_state_dict(self, state_dict, move_to_device=True):
|
| 193 |
+
"""Load an optimizer state.
|
| 194 |
+
|
| 195 |
+
Arguments:
|
| 196 |
+
state_dict (`dict`):
|
| 197 |
+
An optimizer state (should be returned from a call to `state_dict`) to load.
|
| 198 |
+
move_to_device (`bool`, defaults to `True`):
|
| 199 |
+
Whether to move the optimizer's state to the device.
|
| 200 |
+
"""
|
| 201 |
+
# deepcopy, to be consistent with module API
|
| 202 |
+
state_dict = deepcopy(state_dict)
|
| 203 |
+
|
| 204 |
+
# Unwrap quantization states that were wrapped for FSDP compatibility
|
| 205 |
+
for param_state in state_dict["state"].values():
|
| 206 |
+
if isinstance(param_state, dict) and self._FSDP_WRAPPED_QUANT_STATE_KEY in param_state:
|
| 207 |
+
quant_state = param_state.pop(self._FSDP_WRAPPED_QUANT_STATE_KEY)
|
| 208 |
+
param_state.update(quant_state)
|
| 209 |
+
|
| 210 |
+
# Validate the state_dict
|
| 211 |
+
groups = self.param_groups
|
| 212 |
+
saved_groups = state_dict["param_groups"]
|
| 213 |
+
|
| 214 |
+
if len(groups) != len(saved_groups):
|
| 215 |
+
raise ValueError("loaded state dict has a different number of parameter groups")
|
| 216 |
+
param_lens = (len(g["params"]) for g in groups)
|
| 217 |
+
saved_lens = (len(g["params"]) for g in saved_groups)
|
| 218 |
+
if any(p_len != s_len for p_len, s_len in zip(param_lens, saved_lens)):
|
| 219 |
+
raise ValueError(
|
| 220 |
+
"loaded state dict contains a parameter group that doesn't match the size of optimizer's group",
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
# Update the state
|
| 224 |
+
id_map = {
|
| 225 |
+
old_id: p
|
| 226 |
+
for old_id, p in zip(
|
| 227 |
+
chain.from_iterable(g["params"] for g in saved_groups),
|
| 228 |
+
chain.from_iterable(g["params"] for g in groups),
|
| 229 |
+
)
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
def cast(param, value):
|
| 233 |
+
r"""Make a deep copy of value, casting all tensors to device of param."""
|
| 234 |
+
if isinstance(value, torch.Tensor):
|
| 235 |
+
# Floating-point types are a bit special here. They are the only ones
|
| 236 |
+
# that are assumed to always match the type of params.
|
| 237 |
+
if param.is_floating_point() and value.dtype != torch.uint8:
|
| 238 |
+
value = value.to(param.dtype)
|
| 239 |
+
return value
|
| 240 |
+
elif isinstance(value, dict):
|
| 241 |
+
for k, v in value.items():
|
| 242 |
+
if k in self.non_castable_tensor_keys:
|
| 243 |
+
if move_to_device:
|
| 244 |
+
value[k] = v.to(param.device)
|
| 245 |
+
else:
|
| 246 |
+
value[k] = cast(param, v)
|
| 247 |
+
|
| 248 |
+
return value
|
| 249 |
+
elif isinstance(value, container_abcs.Iterable):
|
| 250 |
+
return type(value)(cast(param, v) for v in value)
|
| 251 |
+
else:
|
| 252 |
+
return value
|
| 253 |
+
|
| 254 |
+
# Copy state assigned to params (and cast tensors to appropriate types).
|
| 255 |
+
# State that is not assigned to params is copied as is (needed for
|
| 256 |
+
# backward compatibility).
|
| 257 |
+
state = defaultdict(dict)
|
| 258 |
+
for k, v in state_dict["state"].items():
|
| 259 |
+
if k in id_map:
|
| 260 |
+
param = id_map[k]
|
| 261 |
+
state[param] = cast(param, v)
|
| 262 |
+
else:
|
| 263 |
+
state[k] = v
|
| 264 |
+
|
| 265 |
+
# Update parameter groups, setting their 'params' value
|
| 266 |
+
def update_group(group, new_group):
|
| 267 |
+
new_group["params"] = group["params"]
|
| 268 |
+
return new_group
|
| 269 |
+
|
| 270 |
+
param_groups = [update_group(g, ng) for g, ng in zip(groups, saved_groups)]
|
| 271 |
+
self.__setstate__({"state": state, "param_groups": param_groups})
|
| 272 |
+
|
| 273 |
+
def to_gpu(self):
|
| 274 |
+
for gindex, group in enumerate(self.param_groups):
|
| 275 |
+
for pindex, p in enumerate(group["params"]):
|
| 276 |
+
if p.device.type == "cpu":
|
| 277 |
+
continue
|
| 278 |
+
if p in self.state:
|
| 279 |
+
values = self.state[p]
|
| 280 |
+
for k, v in values.items():
|
| 281 |
+
if isinstance(v, torch.Tensor):
|
| 282 |
+
is_paged = getattr(v, "is_paged", False)
|
| 283 |
+
if not is_paged:
|
| 284 |
+
self.state[p][k] = v.to(p.device)
|
| 285 |
+
|
| 286 |
+
def check_overrides(self):
|
| 287 |
+
for module, attr, config in self.mng.module_weight_config_triple:
|
| 288 |
+
pmodule = getattr(module, attr)
|
| 289 |
+
assert pmodule is not None
|
| 290 |
+
assert isinstance(pmodule, torch.Tensor) or isinstance(pmodule, torch.Parameter)
|
| 291 |
+
found = False
|
| 292 |
+
for gindex, group in enumerate(self.param_groups):
|
| 293 |
+
if found:
|
| 294 |
+
break
|
| 295 |
+
for pindex, p in enumerate(group["params"]):
|
| 296 |
+
if found:
|
| 297 |
+
break
|
| 298 |
+
if id(p) == id(pmodule):
|
| 299 |
+
# found the matching parameter
|
| 300 |
+
# init override
|
| 301 |
+
self.mng.pid2config[id(p)] = config
|
| 302 |
+
self.mng.index2config[(gindex, pindex)] = self.mng.pid2config[id(p)]
|
| 303 |
+
found = True
|
| 304 |
+
|
| 305 |
+
@torch.no_grad()
|
| 306 |
+
def step(self, closure=None):
|
| 307 |
+
"""Perform a single optimization step.
|
| 308 |
+
|
| 309 |
+
Arguments:
|
| 310 |
+
closure (`Callable`, *optional*, defaults to `None`):
|
| 311 |
+
A closure that reevaluates the model and returns the loss.
|
| 312 |
+
"""
|
| 313 |
+
loss = None
|
| 314 |
+
if closure is not None:
|
| 315 |
+
with torch.enable_grad():
|
| 316 |
+
loss = closure()
|
| 317 |
+
|
| 318 |
+
if not self.initialized:
|
| 319 |
+
self.check_overrides()
|
| 320 |
+
self.to_gpu() # needed for fairseq pure fp16 training
|
| 321 |
+
self.initialized = True
|
| 322 |
+
|
| 323 |
+
# if self.is_paged: self.page_mng.prefetch_all()
|
| 324 |
+
p = None
|
| 325 |
+
for gindex, group in enumerate(self.param_groups):
|
| 326 |
+
for pindex, p in enumerate(group["params"]):
|
| 327 |
+
if p.grad is None:
|
| 328 |
+
continue
|
| 329 |
+
state = self.state[p]
|
| 330 |
+
if len(state) == 0:
|
| 331 |
+
self.init_state(group, p, gindex, pindex)
|
| 332 |
+
|
| 333 |
+
self.prefetch_state(p)
|
| 334 |
+
self.update_step(group, p, gindex, pindex)
|
| 335 |
+
sync_gpu(p)
|
| 336 |
+
if self.is_paged and p is not None:
|
| 337 |
+
# all paged operations are asynchronous, we need
|
| 338 |
+
# to sync to make sure all tensors are in the right state
|
| 339 |
+
sync_gpu(p)
|
| 340 |
+
|
| 341 |
+
return loss
|
| 342 |
+
|
| 343 |
+
def get_config(self, gindex, pindex, group):
|
| 344 |
+
config = {}
|
| 345 |
+
config["betas"] = group["betas"]
|
| 346 |
+
config["eps"] = group["eps"]
|
| 347 |
+
config["weight_decay"] = group["weight_decay"]
|
| 348 |
+
config["lr"] = group["lr"]
|
| 349 |
+
config["alpha"] = group.get("alpha", 0.0)
|
| 350 |
+
config["t_alpha"] = group.get("t_alpha", None)
|
| 351 |
+
config["t_beta3"] = group.get("t_beta3", None)
|
| 352 |
+
config["optim_bits"] = self.args.optim_bits
|
| 353 |
+
config["min_8bit_size"] = self.args.min_8bit_size
|
| 354 |
+
config["max_unorm"] = self.args.max_unorm
|
| 355 |
+
config["skip_zeros"] = self.args.skip_zeros
|
| 356 |
+
|
| 357 |
+
if (gindex, pindex) in self.mng.index2config:
|
| 358 |
+
config.update(self.mng.index2config[(gindex, pindex)])
|
| 359 |
+
|
| 360 |
+
# Also check pid2config as a fallback so that override_config works
|
| 361 |
+
# regardless of whether it was called before or after register_parameters.
|
| 362 |
+
p = self.param_groups[gindex]["params"][pindex]
|
| 363 |
+
if id(p) in self.mng.pid2config:
|
| 364 |
+
config.update(self.mng.pid2config[id(p)])
|
| 365 |
+
|
| 366 |
+
return config
|
| 367 |
+
|
| 368 |
+
def init_state(self, group, p, gindex, pindex):
|
| 369 |
+
raise NotImplementedError("init_state method needs to be overridden")
|
| 370 |
+
|
| 371 |
+
def update_step(self, group, p, gindex, pindex):
|
| 372 |
+
raise NotImplementedError("The update_step method needs to be overridden")
|
| 373 |
+
|
| 374 |
+
def get_state_buffer(self, p, dtype=torch.float32):
|
| 375 |
+
if p.device.type == "cpu":
|
| 376 |
+
if self.is_paged and not getattr(self, "_cpu_paged_warned", False):
|
| 377 |
+
warnings.warn(
|
| 378 |
+
"Paged optimizers are not supported on CPU. Falling back to non-paged optimizer behavior.",
|
| 379 |
+
stacklevel=2,
|
| 380 |
+
)
|
| 381 |
+
self._cpu_paged_warned = True
|
| 382 |
+
return torch.zeros_like(p, dtype=dtype, device=p.device)
|
| 383 |
+
if not self.is_paged or p.numel() < 1e5:
|
| 384 |
+
return torch.zeros_like(p, dtype=dtype, device=p.device)
|
| 385 |
+
else:
|
| 386 |
+
# > 1 MB
|
| 387 |
+
buff = F.get_paged(*p.shape, dtype=dtype, device=p.device)
|
| 388 |
+
F.fill(buff, 0)
|
| 389 |
+
self.page_mng.paged_tensors.append(buff)
|
| 390 |
+
return buff
|
| 391 |
+
|
| 392 |
+
def prefetch_state(self, p):
|
| 393 |
+
if self.is_paged:
|
| 394 |
+
state = self.state[p]
|
| 395 |
+
s1 = state["state1"]
|
| 396 |
+
is_paged = getattr(s1, "is_paged", False)
|
| 397 |
+
if is_paged:
|
| 398 |
+
F.prefetch_tensor(state["state1"])
|
| 399 |
+
if "state2" in state:
|
| 400 |
+
F.prefetch_tensor(state["state2"])
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
class Optimizer2State(Optimizer8bit):
|
| 404 |
+
def __init__(
|
| 405 |
+
self,
|
| 406 |
+
optimizer_name,
|
| 407 |
+
params,
|
| 408 |
+
lr=1e-3,
|
| 409 |
+
betas=(0.9, 0.999),
|
| 410 |
+
eps=1e-8,
|
| 411 |
+
weight_decay=0.0,
|
| 412 |
+
optim_bits=32,
|
| 413 |
+
args=None,
|
| 414 |
+
min_8bit_size=4096,
|
| 415 |
+
max_unorm=0.0,
|
| 416 |
+
skip_zeros=False,
|
| 417 |
+
is_paged=False,
|
| 418 |
+
alpha=0.0,
|
| 419 |
+
t_alpha: Optional[int] = None,
|
| 420 |
+
t_beta3: Optional[int] = None,
|
| 421 |
+
):
|
| 422 |
+
"""
|
| 423 |
+
Base 2-state update optimizer class.
|
| 424 |
+
|
| 425 |
+
Arguments:
|
| 426 |
+
optimizer_name (`str`):
|
| 427 |
+
The name of the optimizer.
|
| 428 |
+
params (`torch.Tensor`):
|
| 429 |
+
The input parameters to optimize.
|
| 430 |
+
lr (`float`, defaults to 1e-3):
|
| 431 |
+
The learning rate.
|
| 432 |
+
betas (`tuple`, defaults to (0.9, 0.999)):
|
| 433 |
+
The beta values for the optimizer.
|
| 434 |
+
eps (`float`, defaults to 1e-8):
|
| 435 |
+
The epsilon value for the optimizer.
|
| 436 |
+
weight_decay (`float`, defaults to 0.0):
|
| 437 |
+
The weight decay value for the optimizer.
|
| 438 |
+
optim_bits (`int`, defaults to 32):
|
| 439 |
+
The number of bits of the optimizer state.
|
| 440 |
+
args (`object`, defaults to `None`):
|
| 441 |
+
An object with additional arguments.
|
| 442 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 443 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 444 |
+
max_unorm (`float`, defaults to 0.0):
|
| 445 |
+
The maximum value to normalize each block with.
|
| 446 |
+
skip_zeros (`bool`, defaults to `False`):
|
| 447 |
+
Whether to skip zero values for sparse gradients and models to ensure correct updates.
|
| 448 |
+
is_paged (`bool`, defaults to `False`):
|
| 449 |
+
Whether the optimizer is a paged optimizer or not.
|
| 450 |
+
alpha (`float`, defaults to 0.0):
|
| 451 |
+
The alpha value for the AdEMAMix optimizer.
|
| 452 |
+
t_alpha (`Optional[int]`, defaults to `None`):
|
| 453 |
+
Number of iterations for alpha scheduling with AdEMAMix.
|
| 454 |
+
t_beta3 (`Optional[int]`, defaults to `None`):
|
| 455 |
+
Number of iterations for beta scheduling with AdEMAMix.
|
| 456 |
+
|
| 457 |
+
"""
|
| 458 |
+
if not 0.0 <= lr:
|
| 459 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 460 |
+
if not 0.0 <= eps:
|
| 461 |
+
raise ValueError(f"Invalid epsilon value: {eps}")
|
| 462 |
+
if isinstance(betas, str):
|
| 463 |
+
# format: '(beta1, beta2)'
|
| 464 |
+
betas = betas.replace("(", "").replace(")", "").strip().split(",")
|
| 465 |
+
betas = [float(b) for b in betas]
|
| 466 |
+
for i in range(len(betas)):
|
| 467 |
+
if not 0.0 <= betas[i] < 1.0:
|
| 468 |
+
raise ValueError(f"Invalid beta parameter at index {i}: {betas[i]}")
|
| 469 |
+
if not 0.0 <= weight_decay:
|
| 470 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 471 |
+
|
| 472 |
+
defaults = dict(
|
| 473 |
+
lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, alpha=alpha, t_alpha=t_alpha, t_beta3=t_beta3
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
super().__init__(params, defaults, optim_bits, is_paged)
|
| 477 |
+
|
| 478 |
+
if args is None:
|
| 479 |
+
args = {}
|
| 480 |
+
args["optim_bits"] = optim_bits
|
| 481 |
+
args["min_8bit_size"] = min_8bit_size
|
| 482 |
+
args["max_unorm"] = max_unorm
|
| 483 |
+
args["skip_zeros"] = skip_zeros
|
| 484 |
+
|
| 485 |
+
self.args = MockArgs(args)
|
| 486 |
+
else:
|
| 487 |
+
self.args = args
|
| 488 |
+
|
| 489 |
+
self.optimizer_name = optimizer_name
|
| 490 |
+
|
| 491 |
+
@torch.no_grad()
|
| 492 |
+
def init_state(self, group, p, gindex, pindex):
|
| 493 |
+
config = self.get_config(gindex, pindex, group)
|
| 494 |
+
|
| 495 |
+
if config["optim_bits"] == 32:
|
| 496 |
+
dtype = torch.float32
|
| 497 |
+
elif config["optim_bits"] == 8:
|
| 498 |
+
dtype = torch.uint8
|
| 499 |
+
else:
|
| 500 |
+
raise NotImplementedError(f"Amount of optimizer bits not supported: {config['optim_bits']}")
|
| 501 |
+
|
| 502 |
+
if p.numel() < config["min_8bit_size"]:
|
| 503 |
+
dtype = torch.float32
|
| 504 |
+
|
| 505 |
+
state = self.state[p]
|
| 506 |
+
state["step"] = 0
|
| 507 |
+
|
| 508 |
+
if dtype == torch.float32:
|
| 509 |
+
state["state1"] = self.get_state_buffer(p, dtype=torch.float32)
|
| 510 |
+
state["state2"] = self.get_state_buffer(p, dtype=torch.float32)
|
| 511 |
+
elif dtype == torch.uint8:
|
| 512 |
+
if state["step"] == 0:
|
| 513 |
+
if "dynamic" not in self.name2qmap:
|
| 514 |
+
self.fill_qmap()
|
| 515 |
+
self.name2qmap["dynamic"] = self.name2qmap["dynamic"].to(p.device)
|
| 516 |
+
self.name2qmap["udynamic"] = self.name2qmap["udynamic"].to(p.device)
|
| 517 |
+
|
| 518 |
+
state["state1"] = self.get_state_buffer(p, dtype=torch.uint8)
|
| 519 |
+
state["qmap1"] = self.name2qmap["dynamic"]
|
| 520 |
+
|
| 521 |
+
state["state2"] = self.get_state_buffer(p, dtype=torch.uint8)
|
| 522 |
+
state["qmap2"] = self.name2qmap["udynamic"]
|
| 523 |
+
|
| 524 |
+
blocksize = 256
|
| 525 |
+
n = p.numel()
|
| 526 |
+
blocks = (n // blocksize) + bool(n % blocksize)
|
| 527 |
+
|
| 528 |
+
state["absmax1"] = torch.zeros((blocks,), dtype=torch.float32, device=p.device)
|
| 529 |
+
state["absmax2"] = torch.zeros((blocks,), dtype=torch.float32, device=p.device)
|
| 530 |
+
|
| 531 |
+
if config["max_unorm"] > 0.0:
|
| 532 |
+
state["unorm_vec"] = torch.zeros((1,), device=p.device)
|
| 533 |
+
|
| 534 |
+
@torch.no_grad()
|
| 535 |
+
def update_step(self, group, p, gindex, pindex):
|
| 536 |
+
# avoid update error from non-contiguous memory layout
|
| 537 |
+
p.data = p.data.contiguous()
|
| 538 |
+
p.grad = p.grad.contiguous()
|
| 539 |
+
|
| 540 |
+
state = self.state[p]
|
| 541 |
+
grad = p.grad
|
| 542 |
+
|
| 543 |
+
config = self.get_config(gindex, pindex, group)
|
| 544 |
+
|
| 545 |
+
state["step"] += 1
|
| 546 |
+
step = state["step"]
|
| 547 |
+
|
| 548 |
+
if state["state1"].dtype == torch.float:
|
| 549 |
+
F.optimizer_update_32bit(
|
| 550 |
+
self.optimizer_name,
|
| 551 |
+
grad,
|
| 552 |
+
p,
|
| 553 |
+
state["state1"],
|
| 554 |
+
config["betas"][0],
|
| 555 |
+
config["eps"],
|
| 556 |
+
step,
|
| 557 |
+
config["lr"],
|
| 558 |
+
state["state2"],
|
| 559 |
+
config["betas"][1],
|
| 560 |
+
config["betas"][2] if len(config["betas"]) >= 3 else 0.0,
|
| 561 |
+
config.get("alpha", 0.0),
|
| 562 |
+
config["weight_decay"],
|
| 563 |
+
1.0,
|
| 564 |
+
state["unorm_vec"] if config["max_unorm"] > 0.0 else None,
|
| 565 |
+
max_unorm=config["max_unorm"],
|
| 566 |
+
skip_zeros=config["skip_zeros"],
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
+
elif state["state1"].dtype == torch.uint8:
|
| 570 |
+
F.optimizer_update_8bit_blockwise(
|
| 571 |
+
self.optimizer_name,
|
| 572 |
+
grad,
|
| 573 |
+
p,
|
| 574 |
+
state["state1"],
|
| 575 |
+
state["state2"],
|
| 576 |
+
config["betas"][0],
|
| 577 |
+
config["betas"][1],
|
| 578 |
+
config["betas"][2] if len(config["betas"]) >= 3 else 0.0,
|
| 579 |
+
config.get("alpha", 0.0),
|
| 580 |
+
config["eps"],
|
| 581 |
+
step,
|
| 582 |
+
config["lr"],
|
| 583 |
+
state["qmap1"],
|
| 584 |
+
state["qmap2"],
|
| 585 |
+
state["absmax1"],
|
| 586 |
+
state["absmax2"],
|
| 587 |
+
config["weight_decay"],
|
| 588 |
+
gnorm_scale=1.0,
|
| 589 |
+
skip_zeros=config["skip_zeros"],
|
| 590 |
+
)
|
| 591 |
+
|
| 592 |
+
|
| 593 |
+
class Optimizer1State(Optimizer8bit):
|
| 594 |
+
def __init__(
|
| 595 |
+
self,
|
| 596 |
+
optimizer_name,
|
| 597 |
+
params,
|
| 598 |
+
lr=1e-3,
|
| 599 |
+
betas=(0.9, 0.0),
|
| 600 |
+
eps=1e-8,
|
| 601 |
+
weight_decay=0.0,
|
| 602 |
+
optim_bits=32,
|
| 603 |
+
args=None,
|
| 604 |
+
min_8bit_size=4096,
|
| 605 |
+
max_unorm=0.0,
|
| 606 |
+
skip_zeros=False,
|
| 607 |
+
is_paged=False,
|
| 608 |
+
):
|
| 609 |
+
"""
|
| 610 |
+
Base 1-state update optimizer class.
|
| 611 |
+
|
| 612 |
+
Arguments:
|
| 613 |
+
optimizer_name (`str`):
|
| 614 |
+
The name of the optimizer.
|
| 615 |
+
params (`torch.Tensor`):
|
| 616 |
+
The input parameters to optimize.
|
| 617 |
+
lr (`float`, defaults to 1e-3):
|
| 618 |
+
The learning rate.
|
| 619 |
+
betas (`tuple`, defaults to (0.9, 0.0)):
|
| 620 |
+
The beta values for the optimizer.
|
| 621 |
+
eps (`float`, defaults to 1e-8):
|
| 622 |
+
The epsilon value for the optimizer.
|
| 623 |
+
weight_decay (`float`, defaults to 0.0):
|
| 624 |
+
The weight decay value for the optimizer.
|
| 625 |
+
optim_bits (`int`, defaults to 32):
|
| 626 |
+
The number of bits of the optimizer state.
|
| 627 |
+
args (`object`, defaults to `None`):
|
| 628 |
+
An object with additional arguments.
|
| 629 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 630 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 631 |
+
max_unorm (`float`, defaults to 0.0):
|
| 632 |
+
The maximum value to normalize each block with.
|
| 633 |
+
skip_zeros (`bool`, defaults to `False`):
|
| 634 |
+
Whether to skip zero values for sparse gradients and models to ensure correct updates.
|
| 635 |
+
is_paged (`bool`, defaults to `False`):
|
| 636 |
+
Whether the optimizer is a paged optimizer or not.
|
| 637 |
+
"""
|
| 638 |
+
if not 0.0 <= lr:
|
| 639 |
+
raise ValueError(f"Invalid learning rate: {lr}")
|
| 640 |
+
if not 0.0 <= eps:
|
| 641 |
+
raise ValueError(f"Invalid epsilon value: {eps}")
|
| 642 |
+
for i in range(len(betas)):
|
| 643 |
+
if not 0.0 <= betas[i] < 1.0:
|
| 644 |
+
raise ValueError(f"Invalid beta parameter at index {i}: {betas[i]}")
|
| 645 |
+
if not 0.0 <= weight_decay:
|
| 646 |
+
raise ValueError(f"Invalid weight_decay value: {weight_decay}")
|
| 647 |
+
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
|
| 648 |
+
super().__init__(params, defaults, optim_bits, is_paged)
|
| 649 |
+
|
| 650 |
+
if args is None:
|
| 651 |
+
args = {}
|
| 652 |
+
args["optim_bits"] = optim_bits
|
| 653 |
+
args["min_8bit_size"] = min_8bit_size
|
| 654 |
+
args["max_unorm"] = max_unorm
|
| 655 |
+
args["skip_zeros"] = skip_zeros
|
| 656 |
+
|
| 657 |
+
self.args = MockArgs(args)
|
| 658 |
+
else:
|
| 659 |
+
self.args = args
|
| 660 |
+
|
| 661 |
+
self.optimizer_name = optimizer_name
|
| 662 |
+
|
| 663 |
+
@torch.no_grad()
|
| 664 |
+
def init_state(self, group, p, gindex, pindex):
|
| 665 |
+
config = self.get_config(gindex, pindex, group)
|
| 666 |
+
|
| 667 |
+
if config["optim_bits"] == 32:
|
| 668 |
+
dtype = torch.float32
|
| 669 |
+
elif config["optim_bits"] == 8:
|
| 670 |
+
dtype = torch.uint8
|
| 671 |
+
else:
|
| 672 |
+
raise NotImplementedError(f"Amount of optimizer bits not supported: {config['optim_bits']}")
|
| 673 |
+
|
| 674 |
+
if p.numel() < config["min_8bit_size"]:
|
| 675 |
+
dtype = torch.float32
|
| 676 |
+
|
| 677 |
+
state = self.state[p]
|
| 678 |
+
state["step"] = 0
|
| 679 |
+
|
| 680 |
+
if dtype == torch.float32:
|
| 681 |
+
state["state1"] = self.get_state_buffer(p, dtype=torch.float32)
|
| 682 |
+
elif dtype == torch.uint8:
|
| 683 |
+
if state["step"] == 0:
|
| 684 |
+
if "dynamic" not in self.name2qmap:
|
| 685 |
+
self.fill_qmap()
|
| 686 |
+
self.name2qmap["dynamic"] = self.name2qmap["dynamic"].to(p.device)
|
| 687 |
+
|
| 688 |
+
state["state1"] = self.get_state_buffer(p, dtype=torch.uint8)
|
| 689 |
+
state["qmap1"] = self.name2qmap["dynamic"]
|
| 690 |
+
|
| 691 |
+
blocksize = 256
|
| 692 |
+
n = p.numel()
|
| 693 |
+
blocks = (n // blocksize) + bool(n % blocksize)
|
| 694 |
+
|
| 695 |
+
state["absmax1"] = torch.zeros((blocks,), dtype=torch.float32, device=p.device)
|
| 696 |
+
|
| 697 |
+
if config["max_unorm"] > 0.0:
|
| 698 |
+
state["unorm_vec"] = torch.zeros((1,), device=p.device)
|
| 699 |
+
|
| 700 |
+
@torch.no_grad()
|
| 701 |
+
def update_step(self, group, p, gindex, pindex):
|
| 702 |
+
# avoid update error from non-contiguous memory layout
|
| 703 |
+
p.data = p.data.contiguous()
|
| 704 |
+
p.grad = p.grad.contiguous()
|
| 705 |
+
|
| 706 |
+
state = self.state[p]
|
| 707 |
+
grad = p.grad
|
| 708 |
+
|
| 709 |
+
config = self.get_config(gindex, pindex, group)
|
| 710 |
+
|
| 711 |
+
state["step"] += 1
|
| 712 |
+
step = state["step"]
|
| 713 |
+
|
| 714 |
+
if state["state1"].dtype == torch.float:
|
| 715 |
+
F.optimizer_update_32bit(
|
| 716 |
+
self.optimizer_name,
|
| 717 |
+
grad,
|
| 718 |
+
p,
|
| 719 |
+
state["state1"],
|
| 720 |
+
config["betas"][0],
|
| 721 |
+
config["eps"],
|
| 722 |
+
step,
|
| 723 |
+
config["lr"],
|
| 724 |
+
None,
|
| 725 |
+
config["betas"][1],
|
| 726 |
+
0.0,
|
| 727 |
+
0.0,
|
| 728 |
+
config["weight_decay"],
|
| 729 |
+
1.0,
|
| 730 |
+
state["unorm_vec"] if config["max_unorm"] > 0.0 else None,
|
| 731 |
+
max_unorm=config["max_unorm"],
|
| 732 |
+
skip_zeros=config["skip_zeros"],
|
| 733 |
+
)
|
| 734 |
+
|
| 735 |
+
elif state["state1"].dtype == torch.uint8:
|
| 736 |
+
F.optimizer_update_8bit_blockwise(
|
| 737 |
+
self.optimizer_name,
|
| 738 |
+
grad,
|
| 739 |
+
p,
|
| 740 |
+
state["state1"],
|
| 741 |
+
None,
|
| 742 |
+
config["betas"][0],
|
| 743 |
+
config["betas"][1],
|
| 744 |
+
0.0,
|
| 745 |
+
0.0,
|
| 746 |
+
config["eps"],
|
| 747 |
+
step,
|
| 748 |
+
config["lr"],
|
| 749 |
+
state["qmap1"],
|
| 750 |
+
None,
|
| 751 |
+
state["absmax1"],
|
| 752 |
+
None,
|
| 753 |
+
config["weight_decay"],
|
| 754 |
+
gnorm_scale=1.0,
|
| 755 |
+
skip_zeros=config["skip_zeros"],
|
| 756 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/rmsprop.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from bitsandbytes.optim.optimizer import Optimizer1State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class RMSprop(Optimizer1State):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
params,
|
| 12 |
+
lr=1e-2,
|
| 13 |
+
alpha=0.99,
|
| 14 |
+
eps=1e-8,
|
| 15 |
+
weight_decay=0,
|
| 16 |
+
momentum=0,
|
| 17 |
+
centered=False,
|
| 18 |
+
optim_bits=32,
|
| 19 |
+
args=None,
|
| 20 |
+
min_8bit_size=4096,
|
| 21 |
+
):
|
| 22 |
+
"""
|
| 23 |
+
Base RMSprop optimizer.
|
| 24 |
+
|
| 25 |
+
Arguments:
|
| 26 |
+
params (`torch.tensor`):
|
| 27 |
+
The input parameters to optimize.
|
| 28 |
+
lr (`float`, defaults to 1e-2):
|
| 29 |
+
The learning rate.
|
| 30 |
+
alpha (`float`, defaults to 0.99):
|
| 31 |
+
The alpha value is the decay rate of the squared gradients of the optimizer.
|
| 32 |
+
eps (`float`, defaults to 1e-8):
|
| 33 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 34 |
+
weight_decay (`float`, defaults to 0.0):
|
| 35 |
+
The weight decay value for the optimizer.
|
| 36 |
+
momentum (`float`, defaults to 0):
|
| 37 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 38 |
+
centered (`bool`, defaults to `False`):
|
| 39 |
+
Whether the gradients are normalized by the variance. If `True`, it can help training at the expense of additional compute.
|
| 40 |
+
optim_bits (`int`, defaults to 32):
|
| 41 |
+
The number of bits of the optimizer state.
|
| 42 |
+
args (`object`, defaults to `None`):
|
| 43 |
+
An object with additional arguments.
|
| 44 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 45 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 46 |
+
"""
|
| 47 |
+
if alpha == 0:
|
| 48 |
+
raise NotImplementedError("RMSprop with alpha==0.0 is not supported!")
|
| 49 |
+
if centered:
|
| 50 |
+
raise NotImplementedError("Centered RMSprop is not supported!")
|
| 51 |
+
super().__init__(
|
| 52 |
+
"rmsprop",
|
| 53 |
+
params,
|
| 54 |
+
lr,
|
| 55 |
+
(alpha, momentum),
|
| 56 |
+
eps,
|
| 57 |
+
weight_decay,
|
| 58 |
+
optim_bits,
|
| 59 |
+
args,
|
| 60 |
+
min_8bit_size,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class RMSprop8bit(Optimizer1State):
|
| 65 |
+
def __init__(
|
| 66 |
+
self,
|
| 67 |
+
params,
|
| 68 |
+
lr=1e-2,
|
| 69 |
+
alpha=0.99,
|
| 70 |
+
eps=1e-8,
|
| 71 |
+
weight_decay=0,
|
| 72 |
+
momentum=0,
|
| 73 |
+
centered=False,
|
| 74 |
+
args=None,
|
| 75 |
+
min_8bit_size=4096,
|
| 76 |
+
):
|
| 77 |
+
"""
|
| 78 |
+
8-bit RMSprop optimizer.
|
| 79 |
+
|
| 80 |
+
Arguments:
|
| 81 |
+
params (`torch.tensor`):
|
| 82 |
+
The input parameters to optimize.
|
| 83 |
+
lr (`float`, defaults to 1e-2):
|
| 84 |
+
The learning rate.
|
| 85 |
+
alpha (`float`, defaults to 0.99):
|
| 86 |
+
The alpha value is the decay rate of the squared gradients of the optimizer.
|
| 87 |
+
eps (`float`, defaults to 1e-8):
|
| 88 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 89 |
+
weight_decay (`float`, defaults to 0.0):
|
| 90 |
+
The weight decay value for the optimizer.
|
| 91 |
+
momentum (`float`, defaults to 0):
|
| 92 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 93 |
+
centered (`bool`, defaults to `False`):
|
| 94 |
+
Whether the gradients are normalized by the variance. If `True`, it can help training at the expense of additional compute.
|
| 95 |
+
args (`object`, defaults to `None`):
|
| 96 |
+
An object with additional arguments.
|
| 97 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 98 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 99 |
+
"""
|
| 100 |
+
if alpha == 0:
|
| 101 |
+
raise NotImplementedError("RMSprop with alpha==0.0 is not supported!")
|
| 102 |
+
if centered:
|
| 103 |
+
raise NotImplementedError("Centered RMSprop is not supported!")
|
| 104 |
+
super().__init__(
|
| 105 |
+
"rmsprop",
|
| 106 |
+
params,
|
| 107 |
+
lr,
|
| 108 |
+
(alpha, momentum),
|
| 109 |
+
eps,
|
| 110 |
+
weight_decay,
|
| 111 |
+
8,
|
| 112 |
+
args,
|
| 113 |
+
min_8bit_size,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class RMSprop32bit(Optimizer1State):
|
| 118 |
+
def __init__(
|
| 119 |
+
self,
|
| 120 |
+
params,
|
| 121 |
+
lr=1e-2,
|
| 122 |
+
alpha=0.99,
|
| 123 |
+
eps=1e-8,
|
| 124 |
+
weight_decay=0,
|
| 125 |
+
momentum=0,
|
| 126 |
+
centered=False,
|
| 127 |
+
args=None,
|
| 128 |
+
min_8bit_size=4096,
|
| 129 |
+
):
|
| 130 |
+
"""
|
| 131 |
+
32-bit RMSprop optimizer.
|
| 132 |
+
|
| 133 |
+
Arguments:
|
| 134 |
+
params (`torch.tensor`):
|
| 135 |
+
The input parameters to optimize.
|
| 136 |
+
lr (`float`, defaults to 1e-2):
|
| 137 |
+
The learning rate.
|
| 138 |
+
alpha (`float`, defaults to 0.99):
|
| 139 |
+
The alpha value is the decay rate of the squared gradients of the optimizer.
|
| 140 |
+
eps (`float`, defaults to 1e-8):
|
| 141 |
+
The epsilon value prevents division by zero in the optimizer.
|
| 142 |
+
weight_decay (`float`, defaults to 0.0):
|
| 143 |
+
The weight decay value for the optimizer.
|
| 144 |
+
momentum (`float`, defaults to 0):
|
| 145 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 146 |
+
centered (`bool`, defaults to `False`):
|
| 147 |
+
Whether the gradients are normalized by the variance. If `True`, it can help training at the expense of additional compute.
|
| 148 |
+
optim_bits (`int`, defaults to 32):
|
| 149 |
+
The number of bits of the optimizer state.
|
| 150 |
+
args (`object`, defaults to `None`):
|
| 151 |
+
An object with additional arguments.
|
| 152 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 153 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
if alpha == 0:
|
| 157 |
+
raise NotImplementedError("RMSprop with alpha==0.0 is not supported!")
|
| 158 |
+
if centered:
|
| 159 |
+
raise NotImplementedError("Centered RMSprop is not supported!")
|
| 160 |
+
super().__init__(
|
| 161 |
+
"rmsprop",
|
| 162 |
+
params,
|
| 163 |
+
lr,
|
| 164 |
+
(alpha, momentum),
|
| 165 |
+
eps,
|
| 166 |
+
weight_decay,
|
| 167 |
+
32,
|
| 168 |
+
args,
|
| 169 |
+
min_8bit_size,
|
| 170 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/optim/sgd.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the MIT license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
from bitsandbytes.optim.optimizer import Optimizer1State
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class SGD(Optimizer1State):
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
params,
|
| 12 |
+
lr,
|
| 13 |
+
momentum=0,
|
| 14 |
+
dampening=0,
|
| 15 |
+
weight_decay=0,
|
| 16 |
+
nesterov=False,
|
| 17 |
+
optim_bits=32,
|
| 18 |
+
args=None,
|
| 19 |
+
min_8bit_size=4096,
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
Base SGD optimizer.
|
| 23 |
+
|
| 24 |
+
Arguments:
|
| 25 |
+
params (`torch.tensor`):
|
| 26 |
+
The input parameters to optimize.
|
| 27 |
+
lr (`float`):
|
| 28 |
+
The learning rate.
|
| 29 |
+
momentum (`float`, defaults to 0):
|
| 30 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 31 |
+
dampening (`float`, defaults to 0):
|
| 32 |
+
The dampening value reduces the momentum of the optimizer.
|
| 33 |
+
weight_decay (`float`, defaults to 0.0):
|
| 34 |
+
The weight decay value for the optimizer.
|
| 35 |
+
nesterov (`bool`, defaults to `False`):
|
| 36 |
+
Whether to use Nesterov momentum.
|
| 37 |
+
optim_bits (`int`, defaults to 32):
|
| 38 |
+
The number of bits of the optimizer state.
|
| 39 |
+
args (`object`, defaults to `None`):
|
| 40 |
+
An object with additional arguments.
|
| 41 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 42 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 43 |
+
"""
|
| 44 |
+
if momentum == 0:
|
| 45 |
+
raise NotImplementedError("SGD without momentum is not supported!")
|
| 46 |
+
super().__init__(
|
| 47 |
+
"momentum",
|
| 48 |
+
params,
|
| 49 |
+
lr,
|
| 50 |
+
(momentum, dampening),
|
| 51 |
+
0.0,
|
| 52 |
+
weight_decay,
|
| 53 |
+
optim_bits,
|
| 54 |
+
args,
|
| 55 |
+
min_8bit_size,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class SGD8bit(Optimizer1State):
|
| 60 |
+
def __init__(
|
| 61 |
+
self,
|
| 62 |
+
params,
|
| 63 |
+
lr,
|
| 64 |
+
momentum=0,
|
| 65 |
+
dampening=0,
|
| 66 |
+
weight_decay=0,
|
| 67 |
+
nesterov=False,
|
| 68 |
+
args=None,
|
| 69 |
+
min_8bit_size=4096,
|
| 70 |
+
):
|
| 71 |
+
"""
|
| 72 |
+
8-bit SGD optimizer.
|
| 73 |
+
|
| 74 |
+
Arguments:
|
| 75 |
+
params (`torch.tensor`):
|
| 76 |
+
The input parameters to optimize.
|
| 77 |
+
lr (`float`):
|
| 78 |
+
The learning rate.
|
| 79 |
+
momentum (`float`, defaults to 0):
|
| 80 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 81 |
+
dampening (`float`, defaults to 0):
|
| 82 |
+
The dampening value reduces the momentum of the optimizer.
|
| 83 |
+
weight_decay (`float`, defaults to 0.0):
|
| 84 |
+
The weight decay value for the optimizer.
|
| 85 |
+
nesterov (`bool`, defaults to `False`):
|
| 86 |
+
Whether to use Nesterov momentum.
|
| 87 |
+
args (`object`, defaults to `None`):
|
| 88 |
+
An object with additional arguments.
|
| 89 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 90 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 91 |
+
"""
|
| 92 |
+
if momentum == 0:
|
| 93 |
+
raise NotImplementedError("SGD without momentum is not supported!")
|
| 94 |
+
super().__init__(
|
| 95 |
+
"momentum",
|
| 96 |
+
params,
|
| 97 |
+
lr,
|
| 98 |
+
(momentum, dampening),
|
| 99 |
+
0.0,
|
| 100 |
+
weight_decay,
|
| 101 |
+
8,
|
| 102 |
+
args,
|
| 103 |
+
min_8bit_size,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class SGD32bit(Optimizer1State):
|
| 108 |
+
def __init__(
|
| 109 |
+
self,
|
| 110 |
+
params,
|
| 111 |
+
lr,
|
| 112 |
+
momentum=0,
|
| 113 |
+
dampening=0,
|
| 114 |
+
weight_decay=0,
|
| 115 |
+
nesterov=False,
|
| 116 |
+
args=None,
|
| 117 |
+
min_8bit_size=4096,
|
| 118 |
+
):
|
| 119 |
+
"""
|
| 120 |
+
32-bit SGD optimizer.
|
| 121 |
+
|
| 122 |
+
Arguments:
|
| 123 |
+
params (`torch.tensor`):
|
| 124 |
+
The input parameters to optimize.
|
| 125 |
+
lr (`float`):
|
| 126 |
+
The learning rate.
|
| 127 |
+
momentum (`float`, defaults to 0):
|
| 128 |
+
The momentum value speeds up the optimizer by taking bigger steps.
|
| 129 |
+
dampening (`float`, defaults to 0):
|
| 130 |
+
The dampening value reduces the momentum of the optimizer.
|
| 131 |
+
weight_decay (`float`, defaults to 0.0):
|
| 132 |
+
The weight decay value for the optimizer.
|
| 133 |
+
nesterov (`bool`, defaults to `False`):
|
| 134 |
+
Whether to use Nesterov momentum.
|
| 135 |
+
args (`object`, defaults to `None`):
|
| 136 |
+
An object with additional arguments.
|
| 137 |
+
min_8bit_size (`int`, defaults to 4096):
|
| 138 |
+
The minimum number of elements of the parameter tensors for 8-bit optimization.
|
| 139 |
+
"""
|
| 140 |
+
if momentum == 0:
|
| 141 |
+
raise NotImplementedError("SGD without momentum is not supported!")
|
| 142 |
+
super().__init__(
|
| 143 |
+
"momentum",
|
| 144 |
+
params,
|
| 145 |
+
lr,
|
| 146 |
+
(momentum, dampening),
|
| 147 |
+
0.0,
|
| 148 |
+
weight_decay,
|
| 149 |
+
32,
|
| 150 |
+
args,
|
| 151 |
+
min_8bit_size,
|
| 152 |
+
)
|
venv/lib/python3.11/site-packages/bitsandbytes/py.typed
ADDED
|
File without changes
|
venv/lib/python3.11/site-packages/bitsandbytes/utils.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
import shlex
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def outlier_hook(module, input):
|
| 12 |
+
assert isinstance(module, torch.nn.Linear)
|
| 13 |
+
tracer = OutlierTracer.get_instance()
|
| 14 |
+
hvalue = tracer.get_hvalue(module.weight)
|
| 15 |
+
if hvalue not in tracer.hvalue2outlier_idx:
|
| 16 |
+
outlier_idx = find_outlier_dims(module.weight)
|
| 17 |
+
tracer.outliers.append(outlier_idx)
|
| 18 |
+
tracer.hvalues.append(hvalue)
|
| 19 |
+
if len(tracer.outliers) > 1:
|
| 20 |
+
# assign the current layer the outlier idx found from the weight
|
| 21 |
+
# of the previous linear layer
|
| 22 |
+
if tracer.outliers[-1].numel() > 0:
|
| 23 |
+
assert tracer.outliers[-1].max() < module.weight.shape[1]
|
| 24 |
+
tracer.hvalue2outlier_idx[hvalue] = tracer.outliers[-1]
|
| 25 |
+
|
| 26 |
+
else:
|
| 27 |
+
# first layer, we cannot use the weight for outlier detection
|
| 28 |
+
# we follow a mixed approach:
|
| 29 |
+
# (1) zscore test of std of hidden dimension
|
| 30 |
+
# (2) magnitude > 6 test
|
| 31 |
+
merged = input[0].view(-1, input[0].shape[-1])
|
| 32 |
+
# (1) zscore test of std of hidden dimension
|
| 33 |
+
outlier_idx = find_outlier_dims(merged, reduction_dim=1, zscore=3)
|
| 34 |
+
# (2) magnitude > 6 test
|
| 35 |
+
dims = (torch.abs(input[0]) > 6).sum(dim=list(range(len(input[0].shape) - 1)))
|
| 36 |
+
outlier_idx2 = torch.where(dims > 0)[0]
|
| 37 |
+
outlier_idx = torch.cat([outlier_idx, outlier_idx2]).unique()
|
| 38 |
+
tracer.hvalue2outlier_idx[hvalue] = outlier_idx
|
| 39 |
+
else:
|
| 40 |
+
for hook in tracer.hooks:
|
| 41 |
+
hook.remove()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class OutlierTracer:
|
| 45 |
+
_instance = None
|
| 46 |
+
|
| 47 |
+
def __init__(self):
|
| 48 |
+
raise RuntimeError("Call get_instance() instead")
|
| 49 |
+
|
| 50 |
+
def initialize(self, model):
|
| 51 |
+
self.last_w = None
|
| 52 |
+
self.current_outlier_dims = None
|
| 53 |
+
self.hvalues = []
|
| 54 |
+
self.outliers = []
|
| 55 |
+
self.hvalue2outlier_idx = {}
|
| 56 |
+
self.initialized = True
|
| 57 |
+
self.hooks = []
|
| 58 |
+
|
| 59 |
+
for n, m in model.named_modules():
|
| 60 |
+
if isinstance(m, torch.nn.Linear):
|
| 61 |
+
self.hooks.append(m.register_forward_pre_hook(outlier_hook))
|
| 62 |
+
|
| 63 |
+
def is_initialized(self):
|
| 64 |
+
return getattr(self, "initialized", False)
|
| 65 |
+
|
| 66 |
+
def get_hvalue(self, weight):
|
| 67 |
+
return weight.data.storage().data_ptr()
|
| 68 |
+
|
| 69 |
+
def get_outliers(self, weight):
|
| 70 |
+
if not self.is_initialized():
|
| 71 |
+
logger.warning("Outlier tracer is not initialized...")
|
| 72 |
+
return None
|
| 73 |
+
hvalue = self.get_hvalue(weight)
|
| 74 |
+
if hvalue in self.hvalue2outlier_idx:
|
| 75 |
+
return self.hvalue2outlier_idx[hvalue]
|
| 76 |
+
else:
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
@classmethod
|
| 80 |
+
def get_instance(cls):
|
| 81 |
+
if cls._instance is None:
|
| 82 |
+
cls._instance = cls.__new__(cls)
|
| 83 |
+
return cls._instance
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def find_outlier_dims(weight, reduction_dim=0, zscore=4.0, topk=None, rdm=False):
|
| 87 |
+
if rdm:
|
| 88 |
+
return torch.randint(0, weight.shape[1], size=(topk,), device=weight.device).long()
|
| 89 |
+
|
| 90 |
+
std = weight.std(reduction_dim)
|
| 91 |
+
stdm = std.mean()
|
| 92 |
+
stdstd = std.std()
|
| 93 |
+
|
| 94 |
+
zstd = (std - stdm) / stdstd
|
| 95 |
+
|
| 96 |
+
if topk is not None:
|
| 97 |
+
_, idx = torch.topk(std.abs(), k=topk, dim=0)
|
| 98 |
+
else:
|
| 99 |
+
idx = torch.where(zstd > zscore)[0]
|
| 100 |
+
|
| 101 |
+
return idx
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def execute_and_return(command_string: str) -> tuple[str, str]:
|
| 105 |
+
def _decode(subprocess_err_out_tuple):
|
| 106 |
+
return tuple(to_decode.decode("UTF-8").strip() for to_decode in subprocess_err_out_tuple)
|
| 107 |
+
|
| 108 |
+
def execute_and_return_decoded_std_streams(command_string):
|
| 109 |
+
return _decode(
|
| 110 |
+
subprocess.Popen(
|
| 111 |
+
shlex.split(command_string),
|
| 112 |
+
stdout=subprocess.PIPE,
|
| 113 |
+
stderr=subprocess.PIPE,
|
| 114 |
+
).communicate(),
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
std_out, std_err = execute_and_return_decoded_std_streams(command_string)
|
| 118 |
+
return std_out, std_err
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def replace_linear(
|
| 122 |
+
model,
|
| 123 |
+
linear_replacement,
|
| 124 |
+
skip_modules=("lm_head",),
|
| 125 |
+
copy_weights=False,
|
| 126 |
+
post_processing_function=None,
|
| 127 |
+
):
|
| 128 |
+
"""
|
| 129 |
+
Replace linear modules with a new Linear module.
|
| 130 |
+
Parameters:
|
| 131 |
+
model (`torch.nn.Module`):
|
| 132 |
+
Input model or `torch.nn.Module` as the function is run recursively.
|
| 133 |
+
linear_replacement (`torch.nn.Module`):
|
| 134 |
+
The linear module that replaces the old one. Only expects standard arguments.
|
| 135 |
+
If other arguments need to be passed, use a lambda.
|
| 136 |
+
skip_modules (`List[str]`, *optional*, defaults to `lm_head`):
|
| 137 |
+
List of modules names not to convert. Defaults to `lm_head`.
|
| 138 |
+
copy_weights (`bool`):
|
| 139 |
+
Copy the weights from the old linear module to the new one
|
| 140 |
+
post_processing_function (`str`):
|
| 141 |
+
A function name of the replacement linear class that is called
|
| 142 |
+
after processing.
|
| 143 |
+
"""
|
| 144 |
+
for name, module in model.named_children():
|
| 145 |
+
if len(list(module.children())) > 0:
|
| 146 |
+
replace_linear(module, linear_replacement, skip_modules, copy_weights, post_processing_function)
|
| 147 |
+
|
| 148 |
+
if isinstance(module, torch.nn.Linear) and name not in skip_modules:
|
| 149 |
+
old_module = model._modules[name]
|
| 150 |
+
model._modules[name] = linear_replacement(
|
| 151 |
+
module.in_features,
|
| 152 |
+
module.out_features,
|
| 153 |
+
module.bias is not None,
|
| 154 |
+
)
|
| 155 |
+
if copy_weights:
|
| 156 |
+
model._modules[name].weight = old_module.weight
|
| 157 |
+
model._modules[name].bias = old_module.bias
|
| 158 |
+
|
| 159 |
+
if post_processing_function is not None:
|
| 160 |
+
func = getattr(module, post_processing_function, None)
|
| 161 |
+
if func is not None:
|
| 162 |
+
func(module)
|
| 163 |
+
return model
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def pack_dict_to_tensor(source_dict):
|
| 167 |
+
"""
|
| 168 |
+
Pack a dictionary into a torch tensor for storing quant_state items in state_dict.
|
| 169 |
+
|
| 170 |
+
Parameters:
|
| 171 |
+
- source_dict: The dictionary to be packed.
|
| 172 |
+
|
| 173 |
+
Returns:
|
| 174 |
+
A torch tensor containing the packed data.
|
| 175 |
+
"""
|
| 176 |
+
json_str = json.dumps(source_dict)
|
| 177 |
+
json_bytes = json_str.encode("utf-8")
|
| 178 |
+
tensor_data = torch.tensor(list(json_bytes), dtype=torch.uint8)
|
| 179 |
+
|
| 180 |
+
return tensor_data
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def unpack_tensor_to_dict(tensor_data):
|
| 184 |
+
"""
|
| 185 |
+
Unpack a torch tensor into a Python dictionary.
|
| 186 |
+
|
| 187 |
+
Parameters:
|
| 188 |
+
- tensor_data: The torch tensor containing the packed data.
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
A Python dictionary containing the unpacked data.
|
| 192 |
+
"""
|
| 193 |
+
json_bytes = bytes(tensor_data.cpu().numpy())
|
| 194 |
+
json_str = json_bytes.decode("utf-8")
|
| 195 |
+
unpacked_dict = json.loads(json_str)
|
| 196 |
+
|
| 197 |
+
return unpacked_dict
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
LINEAR_8BIT_WEIGHTS_FORMAT_MAPPING = {"row": 0, "col32": 1, "col_turing": 2, "col_ampere": 3}
|
| 201 |
+
INVERSE_LINEAR_8BIT_WEIGHTS_FORMAT_MAPPING = {val: name for (name, val) in LINEAR_8BIT_WEIGHTS_FORMAT_MAPPING.items()}
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def sync_gpu(t: torch.Tensor):
|
| 205 |
+
if t.device.type == "cuda":
|
| 206 |
+
torch.cuda.synchronize()
|
| 207 |
+
elif t.device.type == "xpu":
|
| 208 |
+
torch.xpu.synchronize()
|
venv/lib/python3.11/site-packages/brotli-1.2.0.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: brotli
|
| 3 |
+
Version: 1.2.0
|
| 4 |
+
Summary: Python bindings for the Brotli compression library
|
| 5 |
+
Home-page: https://github.com/google/brotli
|
| 6 |
+
Author: The Brotli Authors
|
| 7 |
+
License: MIT
|
| 8 |
+
Platform: Posix
|
| 9 |
+
Platform: MacOS X
|
| 10 |
+
Platform: Windows
|
| 11 |
+
Classifier: Development Status :: 4 - Beta
|
| 12 |
+
Classifier: Environment :: Console
|
| 13 |
+
Classifier: Intended Audience :: Developers
|
| 14 |
+
Classifier: Operating System :: MacOS :: MacOS X
|
| 15 |
+
Classifier: Operating System :: Microsoft :: Windows
|
| 16 |
+
Classifier: Operating System :: POSIX :: Linux
|
| 17 |
+
Classifier: Programming Language :: C
|
| 18 |
+
Classifier: Programming Language :: C++
|
| 19 |
+
Classifier: Programming Language :: Python
|
| 20 |
+
Classifier: Programming Language :: Python :: 2
|
| 21 |
+
Classifier: Programming Language :: Python :: 2.7
|
| 22 |
+
Classifier: Programming Language :: Python :: 3
|
| 23 |
+
Classifier: Programming Language :: Python :: 3.3
|
| 24 |
+
Classifier: Programming Language :: Python :: 3.4
|
| 25 |
+
Classifier: Programming Language :: Python :: 3.5
|
| 26 |
+
Classifier: Programming Language :: Unix Shell
|
| 27 |
+
Classifier: Topic :: Software Development :: Libraries
|
| 28 |
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
| 29 |
+
Classifier: Topic :: System :: Archiving
|
| 30 |
+
Classifier: Topic :: System :: Archiving :: Compression
|
| 31 |
+
Classifier: Topic :: Text Processing :: Fonts
|
| 32 |
+
Classifier: Topic :: Utilities
|
| 33 |
+
Description-Content-Type: text/markdown
|
| 34 |
+
License-File: LICENSE
|
| 35 |
+
Dynamic: author
|
| 36 |
+
Dynamic: classifier
|
| 37 |
+
Dynamic: description
|
| 38 |
+
Dynamic: description-content-type
|
| 39 |
+
Dynamic: home-page
|
| 40 |
+
Dynamic: license
|
| 41 |
+
Dynamic: license-file
|
| 42 |
+
Dynamic: platform
|
| 43 |
+
Dynamic: summary
|
| 44 |
+
|
| 45 |
+
<p align="center">
|
| 46 |
+
<img src="https://github.com/google/brotli/actions/workflows/build_test.yml/badge.svg" alt="GitHub Actions Build Status" href="https://github.com/google/brotli/actions?query=branch%3Amaster">
|
| 47 |
+
<img src="https://oss-fuzz-build-logs.storage.googleapis.com/badges/brotli.svg" alt="Fuzzing Status" href="https://oss-fuzz-build-logs.storage.googleapis.com/index.html#brotli">
|
| 48 |
+
</p>
|
| 49 |
+
<p align="center"><img src="https://brotli.org/brotli.svg" alt="Brotli" width="64"></p>
|
| 50 |
+
|
| 51 |
+
### Introduction
|
| 52 |
+
|
| 53 |
+
Brotli is a generic-purpose lossless compression algorithm that compresses data
|
| 54 |
+
using a combination of a modern variant of the LZ77 algorithm, Huffman coding
|
| 55 |
+
and 2nd order context modeling, with a compression ratio comparable to the best
|
| 56 |
+
currently available general-purpose compression methods. It is similar in speed
|
| 57 |
+
with deflate but offers more dense compression.
|
| 58 |
+
|
| 59 |
+
The specification of the Brotli Compressed Data Format is defined in
|
| 60 |
+
[RFC 7932](https://datatracker.ietf.org/doc/html/rfc7932).
|
| 61 |
+
|
| 62 |
+
Brotli is open-sourced under the MIT License, see the LICENSE file.
|
| 63 |
+
|
| 64 |
+
> **Please note:** brotli is a "stream" format; it does not contain
|
| 65 |
+
> meta-information, like checksums or uncompressed data length. It is possible
|
| 66 |
+
> to modify "raw" ranges of the compressed stream and the decoder will not
|
| 67 |
+
> notice that.
|
| 68 |
+
|
| 69 |
+
### Installation
|
| 70 |
+
|
| 71 |
+
In most Linux distributions, installing `brotli` is just a matter of using
|
| 72 |
+
the package management system. For example in Debian-based distributions:
|
| 73 |
+
`apt install brotli` will install `brotli`. On MacOS, you can use
|
| 74 |
+
[Homebrew](https://brew.sh/): `brew install brotli`.
|
| 75 |
+
|
| 76 |
+
[](https://repology.org/project/brotli/versions)
|
| 77 |
+
|
| 78 |
+
Of course you can also build brotli from sources.
|
| 79 |
+
|
| 80 |
+
### Build instructions
|
| 81 |
+
|
| 82 |
+
#### Vcpkg
|
| 83 |
+
|
| 84 |
+
You can download and install brotli using the
|
| 85 |
+
[vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
|
| 86 |
+
|
| 87 |
+
git clone https://github.com/Microsoft/vcpkg.git
|
| 88 |
+
cd vcpkg
|
| 89 |
+
./bootstrap-vcpkg.sh
|
| 90 |
+
./vcpkg integrate install
|
| 91 |
+
./vcpkg install brotli
|
| 92 |
+
|
| 93 |
+
The brotli port in vcpkg is kept up to date by Microsoft team members and
|
| 94 |
+
community contributors. If the version is out of date, please [create an issue
|
| 95 |
+
or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
|
| 96 |
+
|
| 97 |
+
#### Bazel
|
| 98 |
+
|
| 99 |
+
See [Bazel](https://www.bazel.build/)
|
| 100 |
+
|
| 101 |
+
#### CMake
|
| 102 |
+
|
| 103 |
+
The basic commands to build and install brotli are:
|
| 104 |
+
|
| 105 |
+
$ mkdir out && cd out
|
| 106 |
+
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./installed ..
|
| 107 |
+
$ cmake --build . --config Release --target install
|
| 108 |
+
|
| 109 |
+
You can use other [CMake](https://cmake.org/) configuration.
|
| 110 |
+
|
| 111 |
+
#### Python
|
| 112 |
+
|
| 113 |
+
To install the latest release of the Python module, run the following:
|
| 114 |
+
|
| 115 |
+
$ pip install brotli
|
| 116 |
+
|
| 117 |
+
To install the tip-of-the-tree version, run:
|
| 118 |
+
|
| 119 |
+
$ pip install --upgrade git+https://github.com/google/brotli
|
| 120 |
+
|
| 121 |
+
See the [Python readme](python/README.md) for more details on installing
|
| 122 |
+
from source, development, and testing.
|
| 123 |
+
|
| 124 |
+
### Contributing
|
| 125 |
+
|
| 126 |
+
We glad to answer/library related questions in
|
| 127 |
+
[brotli mailing list](https://groups.google.com/g/brotli).
|
| 128 |
+
|
| 129 |
+
Regular issues / feature requests should be reported in
|
| 130 |
+
[issue tracker](https://github.com/google/brotli/issues).
|
| 131 |
+
|
| 132 |
+
For reporting vulnerability please read [SECURITY](SECURITY.md).
|
| 133 |
+
|
| 134 |
+
For contributing changes please read [CONTRIBUTING](CONTRIBUTING.md).
|
| 135 |
+
|
| 136 |
+
### Benchmarks
|
| 137 |
+
* [Squash Compression Benchmark](https://quixdb.github.io/squash-benchmark/) / [Unstable Squash Compression Benchmark](https://quixdb.github.io/squash-benchmark/unstable/)
|
| 138 |
+
* [Large Text Compression Benchmark](https://mattmahoney.net/dc/text.html)
|
| 139 |
+
* [Lzturbo Benchmark](https://sites.google.com/site/powturbo/home/benchmark)
|
| 140 |
+
|
| 141 |
+
### Related projects
|
| 142 |
+
> **Disclaimer:** Brotli authors take no responsibility for the third party projects mentioned in this section.
|
| 143 |
+
|
| 144 |
+
Independent [decoder](https://github.com/madler/brotli) implementation
|
| 145 |
+
by Mark Adler, based entirely on format specification.
|
| 146 |
+
|
| 147 |
+
JavaScript port of brotli [decoder](https://github.com/devongovett/brotli.js).
|
| 148 |
+
Could be used directly via `npm install brotli`
|
| 149 |
+
|
| 150 |
+
Hand ported [decoder / encoder](https://github.com/dominikhlbg/BrotliHaxe)
|
| 151 |
+
in haxe by Dominik Homberger.
|
| 152 |
+
Output source code: JavaScript, PHP, Python, Java and C#
|
| 153 |
+
|
| 154 |
+
7Zip [plugin](https://github.com/mcmilk/7-Zip-Zstd)
|
| 155 |
+
|
| 156 |
+
Dart compression framework with
|
| 157 |
+
[fast FFI-based Brotli implementation](https://pub.dev/documentation/es_compression/latest/brotli/)
|
| 158 |
+
with ready-to-use prebuilt binaries for Win/Linux/Mac
|
venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/RECORD
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/brotli.cpython-311.pyc,,
|
| 2 |
+
_brotli.cpython-311-x86_64-linux-gnu.so,sha256=oNkoKKdbC8N6n3NI5g1V4MFmzEX8vlGAiWeInBs6jxs,5173360
|
| 3 |
+
brotli-1.2.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 4 |
+
brotli-1.2.0.dist-info/METADATA,sha256=-dEtUYS1CQsUay5fDWKTFSGysUNepGZDOiObt8QfyWI,6116
|
| 5 |
+
brotli-1.2.0.dist-info/RECORD,,
|
| 6 |
+
brotli-1.2.0.dist-info/WHEEL,sha256=_CFvICYDmZlAYHt8L7Zn3n-BGLj8dkZLQPp22Piy5JE,151
|
| 7 |
+
brotli-1.2.0.dist-info/licenses/LICENSE,sha256=PRgACONpIqTo2uwRw0x68mT-1ZYtB5JK6pKMOOhmPJQ,1084
|
| 8 |
+
brotli-1.2.0.dist-info/top_level.txt,sha256=gsS54HrhO3ZveFxeMrKo_7qH4Sm4TbQ7jGLVBEqJ4NI,15
|
| 9 |
+
brotli.py,sha256=gTYQvw10ppmNe0fZEuiJLDiW5urBnLeyJXXgT8KUtj8,1970
|
venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (80.9.0)
|
| 3 |
+
Root-Is-Purelib: false
|
| 4 |
+
Tag: cp311-cp311-manylinux_2_17_x86_64
|
| 5 |
+
Tag: cp311-cp311-manylinux2014_x86_64
|
| 6 |
+
|
venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/licenses/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (c) 2009, 2010, 2013-2016 by the Brotli Authors.
|
| 2 |
+
|
| 3 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 4 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 5 |
+
in the Software without restriction, including without limitation the rights
|
| 6 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 7 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 8 |
+
furnished to do so, subject to the following conditions:
|
| 9 |
+
|
| 10 |
+
The above copyright notice and this permission notice shall be included in
|
| 11 |
+
all copies or substantial portions of the Software.
|
| 12 |
+
|
| 13 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 14 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 15 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 16 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 17 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 18 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 19 |
+
THE SOFTWARE.
|
venv/lib/python3.11/site-packages/brotli-1.2.0.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
_brotli
|
| 2 |
+
brotli
|
venv/lib/python3.11/site-packages/brotli.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2016 The Brotli Authors. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Distributed under MIT license.
|
| 4 |
+
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
| 5 |
+
|
| 6 |
+
"""Functions to compress and decompress data using the Brotli library."""
|
| 7 |
+
|
| 8 |
+
import _brotli
|
| 9 |
+
|
| 10 |
+
# The library version.
|
| 11 |
+
version = __version__ = _brotli.__version__
|
| 12 |
+
|
| 13 |
+
# The compression mode.
|
| 14 |
+
MODE_GENERIC = _brotli.MODE_GENERIC
|
| 15 |
+
MODE_TEXT = _brotli.MODE_TEXT
|
| 16 |
+
MODE_FONT = _brotli.MODE_FONT
|
| 17 |
+
|
| 18 |
+
# The Compressor object.
|
| 19 |
+
Compressor = _brotli.Compressor
|
| 20 |
+
|
| 21 |
+
# The Decompressor object.
|
| 22 |
+
Decompressor = _brotli.Decompressor
|
| 23 |
+
|
| 24 |
+
# Compress a byte string.
|
| 25 |
+
def compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0):
|
| 26 |
+
"""Compress a byte string.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
string (bytes): The input data.
|
| 30 |
+
mode (int, optional): The compression mode; value 0 should be used for
|
| 31 |
+
generic input (MODE_GENERIC); value 1 might be beneficial for UTF-8 text
|
| 32 |
+
input (MODE_TEXT); value 2 tunes encoder for WOFF 2.0 data (MODE_FONT).
|
| 33 |
+
Defaults to 0.
|
| 34 |
+
quality (int, optional): Controls the compression-speed vs compression-
|
| 35 |
+
density tradeoff. The higher the quality, the slower the compression.
|
| 36 |
+
Range is 0 to 11. Defaults to 11.
|
| 37 |
+
lgwin (int, optional): Base 2 logarithm of the sliding window size. Range
|
| 38 |
+
is 10 to 24. Defaults to 22.
|
| 39 |
+
lgblock (int, optional): Base 2 logarithm of the maximum input block size.
|
| 40 |
+
Range is 16 to 24. If set to 0, the value will be set based on the
|
| 41 |
+
quality. Defaults to 0.
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
The compressed byte string.
|
| 45 |
+
|
| 46 |
+
Raises:
|
| 47 |
+
brotli.error: If arguments are invalid, or compressor fails.
|
| 48 |
+
"""
|
| 49 |
+
compressor = Compressor(mode=mode, quality=quality, lgwin=lgwin,
|
| 50 |
+
lgblock=lgblock)
|
| 51 |
+
return compressor.process(string) + compressor.finish()
|
| 52 |
+
|
| 53 |
+
# Decompress a compressed byte string.
|
| 54 |
+
decompress = _brotli.decompress
|
| 55 |
+
|
| 56 |
+
# Raised if compression or decompression fails.
|
| 57 |
+
error = _brotli.error
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/METADATA
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: certifi
|
| 3 |
+
Version: 2026.7.22
|
| 4 |
+
Summary: Python package for providing Mozilla's CA Bundle.
|
| 5 |
+
Home-page: https://github.com/certifi/python-certifi
|
| 6 |
+
Author: Kenneth Reitz
|
| 7 |
+
Author-email: me@kennethreitz.com
|
| 8 |
+
License: MPL-2.0
|
| 9 |
+
Project-URL: Source, https://github.com/certifi/python-certifi
|
| 10 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 11 |
+
Classifier: Intended Audience :: Developers
|
| 12 |
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
| 13 |
+
Classifier: Natural Language :: English
|
| 14 |
+
Classifier: Programming Language :: Python
|
| 15 |
+
Classifier: Programming Language :: Python :: 3
|
| 16 |
+
Classifier: Programming Language :: Python :: 3 :: Only
|
| 17 |
+
Classifier: Programming Language :: Python :: 3.7
|
| 18 |
+
Classifier: Programming Language :: Python :: 3.8
|
| 19 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 20 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 21 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 22 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 23 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 24 |
+
Classifier: Programming Language :: Python :: 3.14
|
| 25 |
+
Requires-Python: >=3.7
|
| 26 |
+
License-File: LICENSE
|
| 27 |
+
Dynamic: author
|
| 28 |
+
Dynamic: author-email
|
| 29 |
+
Dynamic: classifier
|
| 30 |
+
Dynamic: description
|
| 31 |
+
Dynamic: home-page
|
| 32 |
+
Dynamic: license
|
| 33 |
+
Dynamic: license-file
|
| 34 |
+
Dynamic: project-url
|
| 35 |
+
Dynamic: requires-python
|
| 36 |
+
Dynamic: summary
|
| 37 |
+
|
| 38 |
+
Certifi: Python SSL Certificates
|
| 39 |
+
================================
|
| 40 |
+
|
| 41 |
+
Certifi provides Mozilla's carefully curated collection of Root Certificates for
|
| 42 |
+
validating the trustworthiness of SSL certificates while verifying the identity
|
| 43 |
+
of TLS hosts. It has been extracted from the `Requests`_ project.
|
| 44 |
+
|
| 45 |
+
Installation
|
| 46 |
+
------------
|
| 47 |
+
|
| 48 |
+
``certifi`` is available on PyPI. Simply install it with ``pip``::
|
| 49 |
+
|
| 50 |
+
$ pip install certifi
|
| 51 |
+
|
| 52 |
+
Usage
|
| 53 |
+
-----
|
| 54 |
+
|
| 55 |
+
To reference the installed certificate authority (CA) bundle, you can use the
|
| 56 |
+
built-in function::
|
| 57 |
+
|
| 58 |
+
>>> import certifi
|
| 59 |
+
|
| 60 |
+
>>> certifi.where()
|
| 61 |
+
'/usr/local/lib/python3.7/site-packages/certifi/cacert.pem'
|
| 62 |
+
|
| 63 |
+
Or from the command line::
|
| 64 |
+
|
| 65 |
+
$ python -m certifi
|
| 66 |
+
/usr/local/lib/python3.7/site-packages/certifi/cacert.pem
|
| 67 |
+
|
| 68 |
+
Enjoy!
|
| 69 |
+
|
| 70 |
+
.. _`Requests`: https://requests.readthedocs.io/en/latest/
|
| 71 |
+
|
| 72 |
+
Addition/Removal of Certificates
|
| 73 |
+
--------------------------------
|
| 74 |
+
|
| 75 |
+
Certifi does not support any addition/removal or other modification of the
|
| 76 |
+
CA trust store content. This project is intended to provide a reliable and
|
| 77 |
+
highly portable root of trust to python deployments. Look to upstream projects
|
| 78 |
+
for methods to use alternate trust.
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/RECORD
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
certifi-2026.7.22.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 2 |
+
certifi-2026.7.22.dist-info/METADATA,sha256=71rxY4-7I2dqw8V3ffz8LNnDSP5Bcu1bo9J3ZVskgJA,2474
|
| 3 |
+
certifi-2026.7.22.dist-info/RECORD,,
|
| 4 |
+
certifi-2026.7.22.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
| 5 |
+
certifi-2026.7.22.dist-info/licenses/LICENSE,sha256=6TcW2mucDVpKHfYP5pWzcPBpVgPSH2-D8FPkLPwQyvc,989
|
| 6 |
+
certifi-2026.7.22.dist-info/top_level.txt,sha256=KMu4vUCfsjLrkPbSNdgdekS-pVJzBAJFO__nI8NF6-U,8
|
| 7 |
+
certifi/__init__.py,sha256=elLl9CBfmz0SoxiYzqp-P2g30ZzCMHALyuGp2R0UhsE,94
|
| 8 |
+
certifi/__main__.py,sha256=xBBoj905TUWBLRGANOcf7oi6e-3dMP4cEoG9OyMs11g,243
|
| 9 |
+
certifi/__pycache__/__init__.cpython-311.pyc,,
|
| 10 |
+
certifi/__pycache__/__main__.cpython-311.pyc,,
|
| 11 |
+
certifi/__pycache__/core.cpython-311.pyc,,
|
| 12 |
+
certifi/cacert.pem,sha256=nMKndLUZjc_xTZvh5mCR9TiXXYZ84Cmpa84VpV39cw8,240216
|
| 13 |
+
certifi/core.py,sha256=XFXycndG5pf37ayeF8N32HUuDafsyhkVMbO4BAPWHa0,3394
|
| 14 |
+
certifi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 15 |
+
certifi/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 16 |
+
certifi/tests/__pycache__/__init__.cpython-311.pyc,,
|
| 17 |
+
certifi/tests/__pycache__/test_certify.cpython-311.pyc,,
|
| 18 |
+
certifi/tests/test_certify.py,sha256=fBedHt839-kSsTNmNzX7RsYzjV0OX4kV0Rw7wofSJ7g,467
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (83.0.0)
|
| 3 |
+
Root-Is-Purelib: true
|
| 4 |
+
Tag: py3-none-any
|
| 5 |
+
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/licenses/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This package contains a modified version of ca-bundle.crt:
|
| 2 |
+
|
| 3 |
+
ca-bundle.crt -- Bundle of CA Root Certificates
|
| 4 |
+
|
| 5 |
+
This is a bundle of X.509 certificates of public Certificate Authorities
|
| 6 |
+
(CA). These were automatically extracted from Mozilla's root certificates
|
| 7 |
+
file (certdata.txt). This file can be found in the mozilla source tree:
|
| 8 |
+
https://hg.mozilla.org/mozilla-central/file/tip/security/nss/lib/ckfw/builtins/certdata.txt
|
| 9 |
+
It contains the certificates in PEM format and therefore
|
| 10 |
+
can be directly used with curl / libcurl / php_curl, or with
|
| 11 |
+
an Apache+mod_ssl webserver for SSL client authentication.
|
| 12 |
+
Just configure this file as the SSLCACertificateFile.#
|
| 13 |
+
|
| 14 |
+
***** BEGIN LICENSE BLOCK *****
|
| 15 |
+
This Source Code Form is subject to the terms of the Mozilla Public License,
|
| 16 |
+
v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain
|
| 17 |
+
one at http://mozilla.org/MPL/2.0/.
|
| 18 |
+
|
| 19 |
+
***** END LICENSE BLOCK *****
|
| 20 |
+
@(#) $RCSfile: certdata.txt,v $ $Revision: 1.80 $ $Date: 2011/11/03 15:11:58 $
|
venv/lib/python3.11/site-packages/certifi-2026.7.22.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
certifi
|
venv/lib/python3.11/site-packages/certifi/__init__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .core import contents, where
|
| 2 |
+
|
| 3 |
+
__all__ = ["contents", "where"]
|
| 4 |
+
__version__ = "2026.07.22"
|
venv/lib/python3.11/site-packages/certifi/__main__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
|
| 3 |
+
from certifi import contents, where
|
| 4 |
+
|
| 5 |
+
parser = argparse.ArgumentParser()
|
| 6 |
+
parser.add_argument("-c", "--contents", action="store_true")
|
| 7 |
+
args = parser.parse_args()
|
| 8 |
+
|
| 9 |
+
if args.contents:
|
| 10 |
+
print(contents())
|
| 11 |
+
else:
|
| 12 |
+
print(where())
|
venv/lib/python3.11/site-packages/certifi/cacert.pem
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
venv/lib/python3.11/site-packages/certifi/core.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
certifi.py
|
| 3 |
+
~~~~~~~~~~
|
| 4 |
+
|
| 5 |
+
This module returns the installation location of cacert.pem or its contents.
|
| 6 |
+
"""
|
| 7 |
+
import sys
|
| 8 |
+
import atexit
|
| 9 |
+
|
| 10 |
+
def exit_cacert_ctx() -> None:
|
| 11 |
+
_CACERT_CTX.__exit__(None, None, None) # type: ignore[union-attr]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
if sys.version_info >= (3, 11):
|
| 15 |
+
|
| 16 |
+
from importlib.resources import as_file, files
|
| 17 |
+
|
| 18 |
+
_CACERT_CTX = None
|
| 19 |
+
_CACERT_PATH = None
|
| 20 |
+
|
| 21 |
+
def where() -> str:
|
| 22 |
+
# This is slightly terrible, but we want to delay extracting the file
|
| 23 |
+
# in cases where we're inside of a zipimport situation until someone
|
| 24 |
+
# actually calls where(), but we don't want to re-extract the file
|
| 25 |
+
# on every call of where(), so we'll do it once then store it in a
|
| 26 |
+
# global variable.
|
| 27 |
+
global _CACERT_CTX
|
| 28 |
+
global _CACERT_PATH
|
| 29 |
+
if _CACERT_PATH is None:
|
| 30 |
+
# This is slightly janky, the importlib.resources API wants you to
|
| 31 |
+
# manage the cleanup of this file, so it doesn't actually return a
|
| 32 |
+
# path, it returns a context manager that will give you the path
|
| 33 |
+
# when you enter it and will do any cleanup when you leave it. In
|
| 34 |
+
# the common case of not needing a temporary file, it will just
|
| 35 |
+
# return the file system location and the __exit__() is a no-op.
|
| 36 |
+
#
|
| 37 |
+
# We also have to hold onto the actual context manager, because
|
| 38 |
+
# it will do the cleanup whenever it gets garbage collected, so
|
| 39 |
+
# we will also store that at the global level as well.
|
| 40 |
+
_CACERT_CTX = as_file(files("certifi").joinpath("cacert.pem"))
|
| 41 |
+
_CACERT_PATH = str(_CACERT_CTX.__enter__())
|
| 42 |
+
atexit.register(exit_cacert_ctx)
|
| 43 |
+
|
| 44 |
+
return _CACERT_PATH
|
| 45 |
+
|
| 46 |
+
def contents() -> str:
|
| 47 |
+
return files("certifi").joinpath("cacert.pem").read_text(encoding="ascii")
|
| 48 |
+
|
| 49 |
+
else:
|
| 50 |
+
|
| 51 |
+
from importlib.resources import path as get_path, read_text
|
| 52 |
+
|
| 53 |
+
_CACERT_CTX = None
|
| 54 |
+
_CACERT_PATH = None
|
| 55 |
+
|
| 56 |
+
def where() -> str:
|
| 57 |
+
# This is slightly terrible, but we want to delay extracting the
|
| 58 |
+
# file in cases where we're inside of a zipimport situation until
|
| 59 |
+
# someone actually calls where(), but we don't want to re-extract
|
| 60 |
+
# the file on every call of where(), so we'll do it once then store
|
| 61 |
+
# it in a global variable.
|
| 62 |
+
global _CACERT_CTX
|
| 63 |
+
global _CACERT_PATH
|
| 64 |
+
if _CACERT_PATH is None:
|
| 65 |
+
# This is slightly janky, the importlib.resources API wants you
|
| 66 |
+
# to manage the cleanup of this file, so it doesn't actually
|
| 67 |
+
# return a path, it returns a context manager that will give
|
| 68 |
+
# you the path when you enter it and will do any cleanup when
|
| 69 |
+
# you leave it. In the common case of not needing a temporary
|
| 70 |
+
# file, it will just return the file system location and the
|
| 71 |
+
# __exit__() is a no-op.
|
| 72 |
+
#
|
| 73 |
+
# We also have to hold onto the actual context manager, because
|
| 74 |
+
# it will do the cleanup whenever it gets garbage collected, so
|
| 75 |
+
# we will also store that at the global level as well.
|
| 76 |
+
_CACERT_CTX = get_path("certifi", "cacert.pem")
|
| 77 |
+
_CACERT_PATH = str(_CACERT_CTX.__enter__())
|
| 78 |
+
atexit.register(exit_cacert_ctx)
|
| 79 |
+
|
| 80 |
+
return _CACERT_PATH
|
| 81 |
+
|
| 82 |
+
def contents() -> str:
|
| 83 |
+
return read_text("certifi", "cacert.pem", encoding="ascii")
|
venv/lib/python3.11/site-packages/certifi/py.typed
ADDED
|
File without changes
|
venv/lib/python3.11/site-packages/certifi/tests/__init__.py
ADDED
|
File without changes
|
venv/lib/python3.11/site-packages/certifi/tests/test_certify.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import unittest
|
| 3 |
+
|
| 4 |
+
import certifi
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestCertifi(unittest.TestCase):
|
| 8 |
+
def test_cabundle_exists(self) -> None:
|
| 9 |
+
assert os.path.exists(certifi.where())
|
| 10 |
+
|
| 11 |
+
def test_read_contents(self) -> None:
|
| 12 |
+
content = certifi.contents()
|
| 13 |
+
assert "-----BEGIN CERTIFICATE-----" in content
|
| 14 |
+
|
| 15 |
+
def test_py_typed_exists(self) -> None:
|
| 16 |
+
assert os.path.exists(
|
| 17 |
+
os.path.join(os.path.dirname(certifi.__file__), 'py.typed')
|
| 18 |
+
)
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/INSTALLER
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pip
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/METADATA
ADDED
|
@@ -0,0 +1,827 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.4
|
| 2 |
+
Name: charset-normalizer
|
| 3 |
+
Version: 3.4.9
|
| 4 |
+
Summary: The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet.
|
| 5 |
+
Author-email: "Ahmed R. TAHRI" <tahri.ahmed@proton.me>
|
| 6 |
+
Maintainer-email: "Ahmed R. TAHRI" <tahri.ahmed@proton.me>
|
| 7 |
+
License: MIT
|
| 8 |
+
Project-URL: Changelog, https://github.com/jawah/charset_normalizer/blob/master/CHANGELOG.md
|
| 9 |
+
Project-URL: Documentation, https://charset-normalizer.readthedocs.io/
|
| 10 |
+
Project-URL: Code, https://github.com/jawah/charset_normalizer
|
| 11 |
+
Project-URL: Issue tracker, https://github.com/jawah/charset_normalizer/issues
|
| 12 |
+
Keywords: encoding,charset,charset-detector,detector,normalization,unicode,chardet,detect
|
| 13 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 14 |
+
Classifier: Intended Audience :: Developers
|
| 15 |
+
Classifier: Operating System :: OS Independent
|
| 16 |
+
Classifier: Programming Language :: Python
|
| 17 |
+
Classifier: Programming Language :: Python :: 3
|
| 18 |
+
Classifier: Programming Language :: Python :: 3.7
|
| 19 |
+
Classifier: Programming Language :: Python :: 3.8
|
| 20 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 21 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 22 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 23 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 24 |
+
Classifier: Programming Language :: Python :: 3.13
|
| 25 |
+
Classifier: Programming Language :: Python :: 3.14
|
| 26 |
+
Classifier: Programming Language :: Python :: 3 :: Only
|
| 27 |
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
| 28 |
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
| 29 |
+
Classifier: Programming Language :: Python :: Free Threading :: 4 - Resilient
|
| 30 |
+
Classifier: Topic :: Text Processing :: Linguistic
|
| 31 |
+
Classifier: Topic :: Utilities
|
| 32 |
+
Classifier: Typing :: Typed
|
| 33 |
+
Requires-Python: >=3.7
|
| 34 |
+
Description-Content-Type: text/markdown
|
| 35 |
+
License-File: LICENSE
|
| 36 |
+
Provides-Extra: unicode-backport
|
| 37 |
+
Dynamic: license-file
|
| 38 |
+
|
| 39 |
+
<h1 align="center">Charset Detection, for Everyone 👋</h1>
|
| 40 |
+
|
| 41 |
+
<p align="center">
|
| 42 |
+
<sup>The Real First Universal Charset Detector</sup><br>
|
| 43 |
+
<a href="https://pypi.org/project/charset-normalizer">
|
| 44 |
+
<img src="https://img.shields.io/pypi/pyversions/charset_normalizer.svg?orange=blue" />
|
| 45 |
+
</a>
|
| 46 |
+
<a href="https://pepy.tech/project/charset-normalizer/">
|
| 47 |
+
<img alt="Download Count Total" src="https://static.pepy.tech/badge/charset-normalizer/month" />
|
| 48 |
+
</a>
|
| 49 |
+
<a href="https://bestpractices.coreinfrastructure.org/projects/7297">
|
| 50 |
+
<img src="https://bestpractices.coreinfrastructure.org/projects/7297/badge">
|
| 51 |
+
</a>
|
| 52 |
+
</p>
|
| 53 |
+
<p align="center">
|
| 54 |
+
<sup><i>Featured Packages</i></sup><br>
|
| 55 |
+
<a href="https://github.com/jawah/niquests">
|
| 56 |
+
<img alt="Static Badge" src="https://img.shields.io/badge/Niquests-Most_Advanced_HTTP_Client-cyan">
|
| 57 |
+
</a>
|
| 58 |
+
<a href="https://github.com/jawah/wassima">
|
| 59 |
+
<img alt="Static Badge" src="https://img.shields.io/badge/Wassima-Certifi_Replacement-cyan">
|
| 60 |
+
</a>
|
| 61 |
+
</p>
|
| 62 |
+
<p align="center">
|
| 63 |
+
<sup><i>In other language (unofficial port - by the community)</i></sup><br>
|
| 64 |
+
<a href="https://github.com/nickspring/charset-normalizer-rs">
|
| 65 |
+
<img alt="Static Badge" src="https://img.shields.io/badge/Rust-red">
|
| 66 |
+
</a>
|
| 67 |
+
</p>
|
| 68 |
+
|
| 69 |
+
> A library that helps you read text from an unknown charset encoding.<br /> Motivated by `chardet`,
|
| 70 |
+
> I'm trying to resolve the issue by taking a new approach.
|
| 71 |
+
> All IANA character set names for which the Python core library provides codecs are supported.
|
| 72 |
+
> You can also register your own set of codecs, and yes, it would work as-is.
|
| 73 |
+
|
| 74 |
+
This project offers you an alternative to **Universal Charset Encoding Detector**, also known as **Chardet**.
|
| 75 |
+
|
| 76 |
+
| Feature | [Chardet](https://github.com/chardet/chardet) | Charset Normalizer | [cChardet](https://github.com/PyYoshi/cChardet) |
|
| 77 |
+
|--------------------------------------------------|:---------------------------------------------:|:-----------------------------------------------------------------------------------------------:|:-----------------------------------------------:|
|
| 78 |
+
| `Fast` | ✅ | ✅ | ✅ |
|
| 79 |
+
| `Universal`[^1] | ❌ | ✅ | ❌ |
|
| 80 |
+
| `Reliable` **without** distinguishable standards | ✅ | ✅ | ✅ |
|
| 81 |
+
| `Reliable` **with** distinguishable standards | ✅ | ✅ | ✅ |
|
| 82 |
+
| `License` | _Disputed_[^2]<br>_restrictive_ | MIT | MPL-1.1<br>_restrictive_ |
|
| 83 |
+
| `Native Python` | ✅ | ✅ | ❌ |
|
| 84 |
+
| `Detect spoken language` | ✅ | ✅ | N/A |
|
| 85 |
+
| `UnicodeDecodeError Safety` | ✅ | ✅ | ❌ |
|
| 86 |
+
| `Whl Size (min)` | 500 kB | 150 kB | ~200 kB |
|
| 87 |
+
| `Supported Encoding` | 99 | [99](https://charset-normalizer.readthedocs.io/en/latest/user/support.html#supported-encodings) | 40 |
|
| 88 |
+
| `Can register custom encoding` | ❌ | ✅ | ❌ |
|
| 89 |
+
|
| 90 |
+
<p align="center">
|
| 91 |
+
<img src="https://i.imgflip.com/373iay.gif" alt="Reading Normalized Text" width="226"/><img src="https://media.tenor.com/images/c0180f70732a18b4965448d33adba3d0/tenor.gif" alt="Cat Reading Text" width="200"/>
|
| 92 |
+
</p>
|
| 93 |
+
|
| 94 |
+
[^1]: They are clearly using specific code for a specific encoding even if covering most of used one.
|
| 95 |
+
[^2]: Chardet 7.0+ was relicensed from LGPL-2.1 to MIT following an AI-assisted rewrite. This relicensing is disputed on two independent grounds: **(a)** the original author [contests](https://github.com/chardet/chardet/issues/327) that the maintainer had the right to relicense, arguing the rewrite is a derivative work of the LGPL-licensed codebase since it was not a clean room implementation; **(b)** the copyright claim itself is [questionable](https://github.com/chardet/chardet/issues/334) given the code was primarily generated by an LLM, and AI-generated output may not be copyrightable under most jurisdictions. Either issue alone could undermine the MIT license. Beyond licensing, the rewrite raises questions about responsible use of AI in open source: key architectural ideas pioneered by charset-normalizer - notably decode-first validity filtering (our foundational approach since v1) and encoding pairwise similarity with the same algorithm and threshold — surfaced in chardet 7 without acknowledgment. The project also imported test files from charset-normalizer to train and benchmark against it, then claimed superior accuracy on those very files. Charset-normalizer has always been MIT-licensed, encoding-agnostic by design, and built on a verifiable human-authored history.
|
| 96 |
+
|
| 97 |
+
## ⚡ Performance
|
| 98 |
+
|
| 99 |
+
This package offer better performances against Chardet. Here are some numbers.
|
| 100 |
+
|
| 101 |
+
| Package | Accuracy | Mean per file (ms) | File per sec (est) |
|
| 102 |
+
|---------------------------------------------------|:--------:|:------------------:|:------------------:|
|
| 103 |
+
| [chardet 7.4](https://github.com/chardet/chardet) | 89 % | 3 ms | 333 file/sec |
|
| 104 |
+
| charset-normalizer | **97 %** | 1 ms | 1000 file/sec |
|
| 105 |
+
|
| 106 |
+
| Package | 99th percentile | 95th percentile | 50th percentile |
|
| 107 |
+
|---------------------------------------------------|:---------------:|:---------------:|:---------------:|
|
| 108 |
+
| [chardet 7.4](https://github.com/chardet/chardet) | 28 ms | 16 ms | < 1 ms |
|
| 109 |
+
| charset-normalizer | 8 ms | 5 ms | 1 ms |
|
| 110 |
+
|
| 111 |
+
_updated as of July 2026 using CPython 3.12, Charset-Normalizer 3.4.8, and Chardet 7.4.3_
|
| 112 |
+
|
| 113 |
+
~Chardet's performance on larger file (1MB+) are very poor. Expect huge difference on large payload.~ No longer the case since Chardet 7.0+
|
| 114 |
+
|
| 115 |
+
> Stats are generated using 400+ files using default parameters. More details on used files, see GHA workflows.
|
| 116 |
+
> And yes, these results might change at any time. The dataset can be updated to include more files.
|
| 117 |
+
> The actual delays heavily depends on your CPU capabilities. The factors should remain the same.
|
| 118 |
+
> Chardet claims on his documentation to have a greater accuracy than us based on the dataset they trained Chardet on(...)
|
| 119 |
+
> Well, it's normal, the opposite would have been worrying. Whereas charset-normalizer don't train on anything, our solution
|
| 120 |
+
> is based on a completely different algorithm, still heuristic through, it does not need weights across every encoding tables.
|
| 121 |
+
|
| 122 |
+
## ✨ Installation
|
| 123 |
+
|
| 124 |
+
Using pip:
|
| 125 |
+
|
| 126 |
+
```sh
|
| 127 |
+
pip install charset-normalizer -U
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## 🚀 Basic Usage
|
| 131 |
+
|
| 132 |
+
### CLI
|
| 133 |
+
This package comes with a CLI.
|
| 134 |
+
|
| 135 |
+
```
|
| 136 |
+
usage: normalizer [-h] [-v] [-a] [-n] [-m] [-r] [-f] [-t THRESHOLD]
|
| 137 |
+
file [file ...]
|
| 138 |
+
|
| 139 |
+
The Real First Universal Charset Detector. Discover originating encoding used
|
| 140 |
+
on text file. Normalize text to unicode.
|
| 141 |
+
|
| 142 |
+
positional arguments:
|
| 143 |
+
files File(s) to be analysed
|
| 144 |
+
|
| 145 |
+
optional arguments:
|
| 146 |
+
-h, --help show this help message and exit
|
| 147 |
+
-v, --verbose Display complementary information about file if any.
|
| 148 |
+
Stdout will contain logs about the detection process.
|
| 149 |
+
-a, --with-alternative
|
| 150 |
+
Output complementary possibilities if any. Top-level
|
| 151 |
+
JSON WILL be a list.
|
| 152 |
+
-n, --normalize Permit to normalize input file. If not set, program
|
| 153 |
+
does not write anything.
|
| 154 |
+
-m, --minimal Only output the charset detected to STDOUT. Disabling
|
| 155 |
+
JSON output.
|
| 156 |
+
-r, --replace Replace file when trying to normalize it instead of
|
| 157 |
+
creating a new one.
|
| 158 |
+
-f, --force Replace file without asking if you are sure, use this
|
| 159 |
+
flag with caution.
|
| 160 |
+
-t THRESHOLD, --threshold THRESHOLD
|
| 161 |
+
Define a custom maximum amount of chaos allowed in
|
| 162 |
+
decoded content. 0. <= chaos <= 1.
|
| 163 |
+
--version Show version information and exit.
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
```bash
|
| 167 |
+
normalizer ./data/sample.1.fr.srt
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
or
|
| 171 |
+
|
| 172 |
+
```bash
|
| 173 |
+
python -m charset_normalizer ./data/sample.1.fr.srt
|
| 174 |
+
```
|
| 175 |
+
|
| 176 |
+
🎉 Since version 1.4.0 the CLI produce easily usable stdout result in JSON format.
|
| 177 |
+
|
| 178 |
+
```json
|
| 179 |
+
{
|
| 180 |
+
"path": "/home/default/projects/charset_normalizer/data/sample.1.fr.srt",
|
| 181 |
+
"encoding": "cp1252",
|
| 182 |
+
"encoding_aliases": [
|
| 183 |
+
"1252",
|
| 184 |
+
"windows_1252"
|
| 185 |
+
],
|
| 186 |
+
"alternative_encodings": [
|
| 187 |
+
"cp1254",
|
| 188 |
+
"cp1256",
|
| 189 |
+
"cp1258",
|
| 190 |
+
"iso8859_14",
|
| 191 |
+
"iso8859_15",
|
| 192 |
+
"iso8859_16",
|
| 193 |
+
"iso8859_3",
|
| 194 |
+
"iso8859_9",
|
| 195 |
+
"latin_1",
|
| 196 |
+
"mbcs"
|
| 197 |
+
],
|
| 198 |
+
"language": "French",
|
| 199 |
+
"alphabets": [
|
| 200 |
+
"Basic Latin",
|
| 201 |
+
"Latin-1 Supplement"
|
| 202 |
+
],
|
| 203 |
+
"has_sig_or_bom": false,
|
| 204 |
+
"chaos": 0.149,
|
| 205 |
+
"coherence": 97.152,
|
| 206 |
+
"unicode_path": null,
|
| 207 |
+
"is_preferred": true
|
| 208 |
+
}
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
### Python
|
| 212 |
+
*Just print out normalized text*
|
| 213 |
+
```python
|
| 214 |
+
from charset_normalizer import from_path
|
| 215 |
+
|
| 216 |
+
results = from_path('./my_subtitle.srt')
|
| 217 |
+
|
| 218 |
+
print(str(results.best()))
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
*Upgrade your code without effort*
|
| 222 |
+
```python
|
| 223 |
+
from charset_normalizer import detect
|
| 224 |
+
```
|
| 225 |
+
|
| 226 |
+
The above code will behave the same as **chardet**. We ensure that we offer the best (reasonable) BC result possible.
|
| 227 |
+
|
| 228 |
+
See the docs for advanced usage : [readthedocs.io](https://charset-normalizer.readthedocs.io/en/latest/)
|
| 229 |
+
|
| 230 |
+
## 😇 Why
|
| 231 |
+
|
| 232 |
+
When I started using Chardet, I noticed that it was not suited to my expectations, and I wanted to propose a
|
| 233 |
+
reliable alternative using a completely different method. Also! I never back down on a good challenge!
|
| 234 |
+
|
| 235 |
+
I **don't care** about the **originating charset** encoding, because **two different tables** can
|
| 236 |
+
produce **two identical rendered string.**
|
| 237 |
+
What I want is to get readable text, the best I can.
|
| 238 |
+
|
| 239 |
+
In a way, **I'm brute forcing text decoding.** How cool is that ? 😎
|
| 240 |
+
|
| 241 |
+
Don't confuse package **ftfy** with charset-normalizer or chardet. ftfy goal is to repair Unicode string whereas charset-normalizer to convert raw file in unknown encoding to unicode.
|
| 242 |
+
|
| 243 |
+
## 🍰 How
|
| 244 |
+
|
| 245 |
+
- Discard all charset encoding table that could not fit the binary content.
|
| 246 |
+
- Measure noise, or the mess once opened (by chunks) with a corresponding charset encoding.
|
| 247 |
+
- Extract matches with the lowest mess detected.
|
| 248 |
+
- Additionally, we measure coherence / probe for a language.
|
| 249 |
+
|
| 250 |
+
**Wait a minute**, what is noise/mess and coherence according to **YOU ?**
|
| 251 |
+
|
| 252 |
+
*Noise :* I opened hundred of text files, **written by humans**, with the wrong encoding table. **I observed**, then
|
| 253 |
+
**I established** some ground rules about **what is obvious** when **it seems like** a mess (aka. defining noise in rendered text).
|
| 254 |
+
I know that my interpretation of what is noise is probably incomplete, feel free to contribute in order to
|
| 255 |
+
improve or rewrite it.
|
| 256 |
+
|
| 257 |
+
*Coherence :* For each language there is on earth, we have computed ranked letter appearance occurrences (the best we can). So I thought
|
| 258 |
+
that intel is worth something here. So I use those records against decoded text to check if I can detect intelligent design.
|
| 259 |
+
|
| 260 |
+
## ⚡ Known limitations
|
| 261 |
+
|
| 262 |
+
- Language detection is unreliable when text contains two or more languages sharing identical letters. (eg. HTML (english tags) + Turkish content (Sharing Latin characters))
|
| 263 |
+
- Every charset detector heavily depends on sufficient content. In common cases, do not bother run detection on very tiny content.
|
| 264 |
+
|
| 265 |
+
## ⚠️ About Python EOLs
|
| 266 |
+
|
| 267 |
+
**If you are running:**
|
| 268 |
+
|
| 269 |
+
- Python >=2.7,<3.5: Unsupported
|
| 270 |
+
- Python 3.5: charset-normalizer < 2.1
|
| 271 |
+
- Python 3.6: charset-normalizer < 3.1
|
| 272 |
+
|
| 273 |
+
Upgrade your Python interpreter as soon as possible.
|
| 274 |
+
|
| 275 |
+
## 👤 Contributing
|
| 276 |
+
|
| 277 |
+
Contributions, issues and feature requests are very much welcome.<br />
|
| 278 |
+
Feel free to check [issues page](https://github.com/ousret/charset_normalizer/issues) if you want to contribute.
|
| 279 |
+
|
| 280 |
+
## 📝 License
|
| 281 |
+
|
| 282 |
+
Copyright © [Ahmed TAHRI @Ousret](https://github.com/Ousret).<br />
|
| 283 |
+
This project is [MIT](https://github.com/Ousret/charset_normalizer/blob/master/LICENSE) licensed.
|
| 284 |
+
|
| 285 |
+
Characters frequencies used in this project © 2012 [Denny Vrandečić](http://simia.net/letters/)
|
| 286 |
+
|
| 287 |
+
## 💼 For Enterprise
|
| 288 |
+
|
| 289 |
+
Professional support for charset-normalizer is available as part of the [Tidelift
|
| 290 |
+
Subscription][1]. Tidelift gives software development teams a single source for
|
| 291 |
+
purchasing and maintaining their software, with professional grade assurances
|
| 292 |
+
from the experts who know it best, while seamlessly integrating with existing
|
| 293 |
+
tools.
|
| 294 |
+
|
| 295 |
+
[1]: https://tidelift.com/subscription/pkg/pypi-charset-normalizer?utm_source=pypi-charset-normalizer&utm_medium=readme
|
| 296 |
+
|
| 297 |
+
[](https://www.bestpractices.dev/projects/7297)
|
| 298 |
+
|
| 299 |
+
# Changelog
|
| 300 |
+
All notable changes to charset-normalizer will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
| 301 |
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
| 302 |
+
|
| 303 |
+
## [3.4.9](https://github.com/Ousret/charset_normalizer/compare/3.4.8...3.4.9) (2026-07-07)
|
| 304 |
+
|
| 305 |
+
### Fixed
|
| 306 |
+
- Regression in our fallback path leading to a decode error. (#771)
|
| 307 |
+
We've yanked 3.4.8 as a result of that bug.
|
| 308 |
+
|
| 309 |
+
## [3.4.8](https://github.com/Ousret/charset_normalizer/compare/3.4.7...3.4.8) (2026-07-06)
|
| 310 |
+
|
| 311 |
+
### Fixed
|
| 312 |
+
- Wall import time due to cascade codec imports for our multibyte first sort of iana supported codecs (#742)
|
| 313 |
+
- Unnecessary json import at runtime (#753)
|
| 314 |
+
- Inverse capitalization not seen by noise detector (#731)
|
| 315 |
+
|
| 316 |
+
### Changed
|
| 317 |
+
- No longer holding a global cache for our noise / coherence measurements. Relax RSS memory usage.
|
| 318 |
+
- Micro-optimizations in our noise / coherence measurements.
|
| 319 |
+
- No longer using regex search by default for our preemptive charset mark algorithm.
|
| 320 |
+
- Raised upperbound of setuptools to v83.
|
| 321 |
+
- Raised upperbound of mypy(c) to v2.1.
|
| 322 |
+
|
| 323 |
+
### Removed
|
| 324 |
+
- Redundant UTF7 BOM marker (#730)
|
| 325 |
+
|
| 326 |
+
## [3.4.7](https://github.com/Ousret/charset_normalizer/compare/3.4.6...3.4.7) (2026-04-02)
|
| 327 |
+
|
| 328 |
+
### Changed
|
| 329 |
+
- Pre-built optimized version using mypy[c] v1.20.
|
| 330 |
+
- Relax `setuptools` constraint to `setuptools>=68,<82.1`.
|
| 331 |
+
|
| 332 |
+
### Fixed
|
| 333 |
+
- Correctly remove SIG remnant in utf-7 decoded string. (#718) (#716)
|
| 334 |
+
|
| 335 |
+
## [3.4.6](https://github.com/Ousret/charset_normalizer/compare/3.4.5...3.4.6) (2026-03-15)
|
| 336 |
+
|
| 337 |
+
### Changed
|
| 338 |
+
- Flattened the logic in `charset_normalizer.md` for higher performance. Removed `eligible(..)` and `feed(...)`
|
| 339 |
+
in favor of `feed_info(...)`.
|
| 340 |
+
- Raised upper bound for mypy[c] to 1.20, for our optimized version.
|
| 341 |
+
- Updated `UNICODE_RANGES_COMBINED` using Unicode blocks v17.
|
| 342 |
+
|
| 343 |
+
### Fixed
|
| 344 |
+
- Edge case where noise difference between two candidates can be almost insignificant. (#672)
|
| 345 |
+
- CLI `--normalize` writing to wrong path when passing multiple files in. (#702)
|
| 346 |
+
|
| 347 |
+
### Misc
|
| 348 |
+
- Freethreaded pre-built wheels now shipped in PyPI starting with 3.14t. (#616)
|
| 349 |
+
|
| 350 |
+
## [3.4.5](https://github.com/Ousret/charset_normalizer/compare/3.4.4...3.4.5) (2026-03-06)
|
| 351 |
+
|
| 352 |
+
### Changed
|
| 353 |
+
- Update `setuptools` constraint to `setuptools>=68,<=82`.
|
| 354 |
+
- Raised upper bound of mypyc for the optional pre-built extension to v1.19.1
|
| 355 |
+
|
| 356 |
+
### Fixed
|
| 357 |
+
- Add explicit link to lib math in our optimized build. (#692)
|
| 358 |
+
- Logger level not restored correctly for empty byte sequences. (#701)
|
| 359 |
+
- TypeError when passing bytearray to from_bytes. (#703)
|
| 360 |
+
|
| 361 |
+
### Misc
|
| 362 |
+
- Applied safe micro-optimizations in both our noise detector and language detector.
|
| 363 |
+
- Rewrote the `query_yes_no` function (inside CLI) to avoid using ambiguous licensed code.
|
| 364 |
+
- Added `cd.py` submodule into mypyc optional compilation to reduce further the performance impact.
|
| 365 |
+
|
| 366 |
+
## [3.4.4](https://github.com/Ousret/charset_normalizer/compare/3.4.2...3.4.4) (2025-10-13)
|
| 367 |
+
|
| 368 |
+
### Changed
|
| 369 |
+
- Bound `setuptools` to a specific constraint `setuptools>=68,<=81`.
|
| 370 |
+
- Raised upper bound of mypyc for the optional pre-built extension to v1.18.2
|
| 371 |
+
|
| 372 |
+
### Removed
|
| 373 |
+
- `setuptools-scm` as a build dependency.
|
| 374 |
+
|
| 375 |
+
### Misc
|
| 376 |
+
- Enforced hashes in `dev-requirements.txt` and created `ci-requirements.txt` for security purposes.
|
| 377 |
+
- Additional pre-built wheels for riscv64, s390x, and armv7l architectures.
|
| 378 |
+
- Restore ` multiple.intoto.jsonl` in GitHub releases in addition to individual attestation file per wheel.
|
| 379 |
+
|
| 380 |
+
## [3.4.3](https://github.com/Ousret/charset_normalizer/compare/3.4.2...3.4.3) (2025-08-09)
|
| 381 |
+
|
| 382 |
+
### Changed
|
| 383 |
+
- mypy(c) is no longer a required dependency at build time if `CHARSET_NORMALIZER_USE_MYPYC` isn't set to `1`. (#595) (#583)
|
| 384 |
+
- automatically lower confidence on small bytes samples that are not Unicode in `detect` output legacy function. (#391)
|
| 385 |
+
|
| 386 |
+
### Added
|
| 387 |
+
- Custom build backend to overcome inability to mark mypy as an optional dependency in the build phase.
|
| 388 |
+
- Support for Python 3.14
|
| 389 |
+
|
| 390 |
+
### Fixed
|
| 391 |
+
- sdist archive contained useless directories.
|
| 392 |
+
- automatically fallback on valid UTF-16 or UTF-32 even if the md says it's noisy. (#633)
|
| 393 |
+
|
| 394 |
+
### Misc
|
| 395 |
+
- SBOM are automatically published to the relevant GitHub release to comply with regulatory changes.
|
| 396 |
+
Each published wheel comes with its SBOM. We choose CycloneDX as the format.
|
| 397 |
+
- Prebuilt optimized wheel are no longer distributed by default for CPython 3.7 due to a change in cibuildwheel.
|
| 398 |
+
|
| 399 |
+
## [3.4.2](https://github.com/Ousret/charset_normalizer/compare/3.4.1...3.4.2) (2025-05-02)
|
| 400 |
+
|
| 401 |
+
### Fixed
|
| 402 |
+
- Addressed the DeprecationWarning in our CLI regarding `argparse.FileType` by backporting the target class into the package. (#591)
|
| 403 |
+
- Improved the overall reliability of the detector with CJK Ideographs. (#605) (#587)
|
| 404 |
+
|
| 405 |
+
### Changed
|
| 406 |
+
- Optional mypyc compilation upgraded to version 1.15 for Python >= 3.8
|
| 407 |
+
|
| 408 |
+
## [3.4.1](https://github.com/Ousret/charset_normalizer/compare/3.4.0...3.4.1) (2024-12-24)
|
| 409 |
+
|
| 410 |
+
### Changed
|
| 411 |
+
- Project metadata are now stored using `pyproject.toml` instead of `setup.cfg` using setuptools as the build backend.
|
| 412 |
+
- Enforce annotation delayed loading for a simpler and consistent types in the project.
|
| 413 |
+
- Optional mypyc compilation upgraded to version 1.14 for Python >= 3.8
|
| 414 |
+
|
| 415 |
+
### Added
|
| 416 |
+
- pre-commit configuration.
|
| 417 |
+
- noxfile.
|
| 418 |
+
|
| 419 |
+
### Removed
|
| 420 |
+
- `build-requirements.txt` as per using `pyproject.toml` native build configuration.
|
| 421 |
+
- `bin/integration.py` and `bin/serve.py` in favor of downstream integration test (see noxfile).
|
| 422 |
+
- `setup.cfg` in favor of `pyproject.toml` metadata configuration.
|
| 423 |
+
- Unused `utils.range_scan` function.
|
| 424 |
+
|
| 425 |
+
### Fixed
|
| 426 |
+
- Converting content to Unicode bytes may insert `utf_8` instead of preferred `utf-8`. (#572)
|
| 427 |
+
- Deprecation warning "'count' is passed as positional argument" when converting to Unicode bytes on Python 3.13+
|
| 428 |
+
|
| 429 |
+
## [3.4.0](https://github.com/Ousret/charset_normalizer/compare/3.3.2...3.4.0) (2024-10-08)
|
| 430 |
+
|
| 431 |
+
### Added
|
| 432 |
+
- Argument `--no-preemptive` in the CLI to prevent the detector to search for hints.
|
| 433 |
+
- Support for Python 3.13 (#512)
|
| 434 |
+
|
| 435 |
+
### Fixed
|
| 436 |
+
- Relax the TypeError exception thrown when trying to compare a CharsetMatch with anything else than a CharsetMatch.
|
| 437 |
+
- Improved the general reliability of the detector based on user feedbacks. (#520) (#509) (#498) (#407) (#537)
|
| 438 |
+
- Declared charset in content (preemptive detection) not changed when converting to utf-8 bytes. (#381)
|
| 439 |
+
|
| 440 |
+
## [3.3.2](https://github.com/Ousret/charset_normalizer/compare/3.3.1...3.3.2) (2023-10-31)
|
| 441 |
+
|
| 442 |
+
### Fixed
|
| 443 |
+
- Unintentional memory usage regression when using large payload that match several encoding (#376)
|
| 444 |
+
- Regression on some detection case showcased in the documentation (#371)
|
| 445 |
+
|
| 446 |
+
### Added
|
| 447 |
+
- Noise (md) probe that identify malformed arabic representation due to the presence of letters in isolated form (credit to my wife)
|
| 448 |
+
|
| 449 |
+
## [3.3.1](https://github.com/Ousret/charset_normalizer/compare/3.3.0...3.3.1) (2023-10-22)
|
| 450 |
+
|
| 451 |
+
### Changed
|
| 452 |
+
- Optional mypyc compilation upgraded to version 1.6.1 for Python >= 3.8
|
| 453 |
+
- Improved the general detection reliability based on reports from the community
|
| 454 |
+
|
| 455 |
+
## [3.3.0](https://github.com/Ousret/charset_normalizer/compare/3.2.0...3.3.0) (2023-09-30)
|
| 456 |
+
|
| 457 |
+
### Added
|
| 458 |
+
- Allow to execute the CLI (e.g. normalizer) through `python -m charset_normalizer.cli` or `python -m charset_normalizer`
|
| 459 |
+
- Support for 9 forgotten encoding that are supported by Python but unlisted in `encoding.aliases` as they have no alias (#323)
|
| 460 |
+
|
| 461 |
+
### Removed
|
| 462 |
+
- (internal) Redundant utils.is_ascii function and unused function is_private_use_only
|
| 463 |
+
- (internal) charset_normalizer.assets is moved inside charset_normalizer.constant
|
| 464 |
+
|
| 465 |
+
### Changed
|
| 466 |
+
- (internal) Unicode code blocks in constants are updated using the latest v15.0.0 definition to improve detection
|
| 467 |
+
- Optional mypyc compilation upgraded to version 1.5.1 for Python >= 3.8
|
| 468 |
+
|
| 469 |
+
### Fixed
|
| 470 |
+
- Unable to properly sort CharsetMatch when both chaos/noise and coherence were close due to an unreachable condition in \_\_lt\_\_ (#350)
|
| 471 |
+
|
| 472 |
+
## [3.2.0](https://github.com/Ousret/charset_normalizer/compare/3.1.0...3.2.0) (2023-06-07)
|
| 473 |
+
|
| 474 |
+
### Changed
|
| 475 |
+
- Typehint for function `from_path` no longer enforce `PathLike` as its first argument
|
| 476 |
+
- Minor improvement over the global detection reliability
|
| 477 |
+
|
| 478 |
+
### Added
|
| 479 |
+
- Introduce function `is_binary` that relies on main capabilities, and optimized to detect binaries
|
| 480 |
+
- Propagate `enable_fallback` argument throughout `from_bytes`, `from_path`, and `from_fp` that allow a deeper control over the detection (default True)
|
| 481 |
+
- Explicit support for Python 3.12
|
| 482 |
+
|
| 483 |
+
### Fixed
|
| 484 |
+
- Edge case detection failure where a file would contain 'very-long' camel cased word (Issue #289)
|
| 485 |
+
|
| 486 |
+
## [3.1.0](https://github.com/Ousret/charset_normalizer/compare/3.0.1...3.1.0) (2023-03-06)
|
| 487 |
+
|
| 488 |
+
### Added
|
| 489 |
+
- Argument `should_rename_legacy` for legacy function `detect` and disregard any new arguments without errors (PR #262)
|
| 490 |
+
|
| 491 |
+
### Removed
|
| 492 |
+
- Support for Python 3.6 (PR #260)
|
| 493 |
+
|
| 494 |
+
### Changed
|
| 495 |
+
- Optional speedup provided by mypy/c 1.0.1
|
| 496 |
+
|
| 497 |
+
## [3.0.1](https://github.com/Ousret/charset_normalizer/compare/3.0.0...3.0.1) (2022-11-18)
|
| 498 |
+
|
| 499 |
+
### Fixed
|
| 500 |
+
- Multi-bytes cutter/chunk generator did not always cut correctly (PR #233)
|
| 501 |
+
|
| 502 |
+
### Changed
|
| 503 |
+
- Speedup provided by mypy/c 0.990 on Python >= 3.7
|
| 504 |
+
|
| 505 |
+
## [3.0.0](https://github.com/Ousret/charset_normalizer/compare/2.1.1...3.0.0) (2022-10-20)
|
| 506 |
+
|
| 507 |
+
### Added
|
| 508 |
+
- Extend the capability of explain=True when cp_isolation contains at most two entries (min one), will log in details of the Mess-detector results
|
| 509 |
+
- Support for alternative language frequency set in charset_normalizer.assets.FREQUENCIES
|
| 510 |
+
- Add parameter `language_threshold` in `from_bytes`, `from_path` and `from_fp` to adjust the minimum expected coherence ratio
|
| 511 |
+
- `normalizer --version` now specify if current version provide extra speedup (meaning mypyc compilation whl)
|
| 512 |
+
|
| 513 |
+
### Changed
|
| 514 |
+
- Build with static metadata using 'build' frontend
|
| 515 |
+
- Make the language detection stricter
|
| 516 |
+
- Optional: Module `md.py` can be compiled using Mypyc to provide an extra speedup up to 4x faster than v2.1
|
| 517 |
+
|
| 518 |
+
### Fixed
|
| 519 |
+
- CLI with opt --normalize fail when using full path for files
|
| 520 |
+
- TooManyAccentuatedPlugin induce false positive on the mess detection when too few alpha character have been fed to it
|
| 521 |
+
- Sphinx warnings when generating the documentation
|
| 522 |
+
|
| 523 |
+
### Removed
|
| 524 |
+
- Coherence detector no longer return 'Simple English' instead return 'English'
|
| 525 |
+
- Coherence detector no longer return 'Classical Chinese' instead return 'Chinese'
|
| 526 |
+
- Breaking: Method `first()` and `best()` from CharsetMatch
|
| 527 |
+
- UTF-7 will no longer appear as "detected" without a recognized SIG/mark (is unreliable/conflict with ASCII)
|
| 528 |
+
- Breaking: Class aliases CharsetDetector, CharsetDoctor, CharsetNormalizerMatch and CharsetNormalizerMatches
|
| 529 |
+
- Breaking: Top-level function `normalize`
|
| 530 |
+
- Breaking: Properties `chaos_secondary_pass`, `coherence_non_latin` and `w_counter` from CharsetMatch
|
| 531 |
+
- Support for the backport `unicodedata2`
|
| 532 |
+
|
| 533 |
+
## [3.0.0rc1](https://github.com/Ousret/charset_normalizer/compare/3.0.0b2...3.0.0rc1) (2022-10-18)
|
| 534 |
+
|
| 535 |
+
### Added
|
| 536 |
+
- Extend the capability of explain=True when cp_isolation contains at most two entries (min one), will log in details of the Mess-detector results
|
| 537 |
+
- Support for alternative language frequency set in charset_normalizer.assets.FREQUENCIES
|
| 538 |
+
- Add parameter `language_threshold` in `from_bytes`, `from_path` and `from_fp` to adjust the minimum expected coherence ratio
|
| 539 |
+
|
| 540 |
+
### Changed
|
| 541 |
+
- Build with static metadata using 'build' frontend
|
| 542 |
+
- Make the language detection stricter
|
| 543 |
+
|
| 544 |
+
### Fixed
|
| 545 |
+
- CLI with opt --normalize fail when using full path for files
|
| 546 |
+
- TooManyAccentuatedPlugin induce false positive on the mess detection when too few alpha character have been fed to it
|
| 547 |
+
|
| 548 |
+
### Removed
|
| 549 |
+
- Coherence detector no longer return 'Simple English' instead return 'English'
|
| 550 |
+
- Coherence detector no longer return 'Classical Chinese' instead return 'Chinese'
|
| 551 |
+
|
| 552 |
+
## [3.0.0b2](https://github.com/Ousret/charset_normalizer/compare/3.0.0b1...3.0.0b2) (2022-08-21)
|
| 553 |
+
|
| 554 |
+
### Added
|
| 555 |
+
- `normalizer --version` now specify if current version provide extra speedup (meaning mypyc compilation whl)
|
| 556 |
+
|
| 557 |
+
### Removed
|
| 558 |
+
- Breaking: Method `first()` and `best()` from CharsetMatch
|
| 559 |
+
- UTF-7 will no longer appear as "detected" without a recognized SIG/mark (is unreliable/conflict with ASCII)
|
| 560 |
+
|
| 561 |
+
### Fixed
|
| 562 |
+
- Sphinx warnings when generating the documentation
|
| 563 |
+
|
| 564 |
+
## [3.0.0b1](https://github.com/Ousret/charset_normalizer/compare/2.1.0...3.0.0b1) (2022-08-15)
|
| 565 |
+
|
| 566 |
+
### Changed
|
| 567 |
+
- Optional: Module `md.py` can be compiled using Mypyc to provide an extra speedup up to 4x faster than v2.1
|
| 568 |
+
|
| 569 |
+
### Removed
|
| 570 |
+
- Breaking: Class aliases CharsetDetector, CharsetDoctor, CharsetNormalizerMatch and CharsetNormalizerMatches
|
| 571 |
+
- Breaking: Top-level function `normalize`
|
| 572 |
+
- Breaking: Properties `chaos_secondary_pass`, `coherence_non_latin` and `w_counter` from CharsetMatch
|
| 573 |
+
- Support for the backport `unicodedata2`
|
| 574 |
+
|
| 575 |
+
## [2.1.1](https://github.com/Ousret/charset_normalizer/compare/2.1.0...2.1.1) (2022-08-19)
|
| 576 |
+
|
| 577 |
+
### Deprecated
|
| 578 |
+
- Function `normalize` scheduled for removal in 3.0
|
| 579 |
+
|
| 580 |
+
### Changed
|
| 581 |
+
- Removed useless call to decode in fn is_unprintable (#206)
|
| 582 |
+
|
| 583 |
+
### Fixed
|
| 584 |
+
- Third-party library (i18n xgettext) crashing not recognizing utf_8 (PEP 263) with underscore from [@aleksandernovikov](https://github.com/aleksandernovikov) (#204)
|
| 585 |
+
|
| 586 |
+
## [2.1.0](https://github.com/Ousret/charset_normalizer/compare/2.0.12...2.1.0) (2022-06-19)
|
| 587 |
+
|
| 588 |
+
### Added
|
| 589 |
+
- Output the Unicode table version when running the CLI with `--version` (PR #194)
|
| 590 |
+
|
| 591 |
+
### Changed
|
| 592 |
+
- Reuse decoded buffer for single byte character sets from [@nijel](https://github.com/nijel) (PR #175)
|
| 593 |
+
- Fixing some performance bottlenecks from [@deedy5](https://github.com/deedy5) (PR #183)
|
| 594 |
+
|
| 595 |
+
### Fixed
|
| 596 |
+
- Workaround potential bug in cpython with Zero Width No-Break Space located in Arabic Presentation Forms-B, Unicode 1.1 not acknowledged as space (PR #175)
|
| 597 |
+
- CLI default threshold aligned with the API threshold from [@oleksandr-kuzmenko](https://github.com/oleksandr-kuzmenko) (PR #181)
|
| 598 |
+
|
| 599 |
+
### Removed
|
| 600 |
+
- Support for Python 3.5 (PR #192)
|
| 601 |
+
|
| 602 |
+
### Deprecated
|
| 603 |
+
- Use of backport unicodedata from `unicodedata2` as Python is quickly catching up, scheduled for removal in 3.0 (PR #194)
|
| 604 |
+
|
| 605 |
+
## [2.0.12](https://github.com/Ousret/charset_normalizer/compare/2.0.11...2.0.12) (2022-02-12)
|
| 606 |
+
|
| 607 |
+
### Fixed
|
| 608 |
+
- ASCII miss-detection on rare cases (PR #170)
|
| 609 |
+
|
| 610 |
+
## [2.0.11](https://github.com/Ousret/charset_normalizer/compare/2.0.10...2.0.11) (2022-01-30)
|
| 611 |
+
|
| 612 |
+
### Added
|
| 613 |
+
- Explicit support for Python 3.11 (PR #164)
|
| 614 |
+
|
| 615 |
+
### Changed
|
| 616 |
+
- The logging behavior have been completely reviewed, now using only TRACE and DEBUG levels (PR #163 #165)
|
| 617 |
+
|
| 618 |
+
## [2.0.10](https://github.com/Ousret/charset_normalizer/compare/2.0.9...2.0.10) (2022-01-04)
|
| 619 |
+
|
| 620 |
+
### Fixed
|
| 621 |
+
- Fallback match entries might lead to UnicodeDecodeError for large bytes sequence (PR #154)
|
| 622 |
+
|
| 623 |
+
### Changed
|
| 624 |
+
- Skipping the language-detection (CD) on ASCII (PR #155)
|
| 625 |
+
|
| 626 |
+
## [2.0.9](https://github.com/Ousret/charset_normalizer/compare/2.0.8...2.0.9) (2021-12-03)
|
| 627 |
+
|
| 628 |
+
### Changed
|
| 629 |
+
- Moderating the logging impact (since 2.0.8) for specific environments (PR #147)
|
| 630 |
+
|
| 631 |
+
### Fixed
|
| 632 |
+
- Wrong logging level applied when setting kwarg `explain` to True (PR #146)
|
| 633 |
+
|
| 634 |
+
## [2.0.8](https://github.com/Ousret/charset_normalizer/compare/2.0.7...2.0.8) (2021-11-24)
|
| 635 |
+
### Changed
|
| 636 |
+
- Improvement over Vietnamese detection (PR #126)
|
| 637 |
+
- MD improvement on trailing data and long foreign (non-pure latin) data (PR #124)
|
| 638 |
+
- Efficiency improvements in cd/alphabet_languages from [@adbar](https://github.com/adbar) (PR #122)
|
| 639 |
+
- call sum() without an intermediary list following PEP 289 recommendations from [@adbar](https://github.com/adbar) (PR #129)
|
| 640 |
+
- Code style as refactored by Sourcery-AI (PR #131)
|
| 641 |
+
- Minor adjustment on the MD around european words (PR #133)
|
| 642 |
+
- Remove and replace SRTs from assets / tests (PR #139)
|
| 643 |
+
- Initialize the library logger with a `NullHandler` by default from [@nmaynes](https://github.com/nmaynes) (PR #135)
|
| 644 |
+
- Setting kwarg `explain` to True will add provisionally (bounded to function lifespan) a specific stream handler (PR #135)
|
| 645 |
+
|
| 646 |
+
### Fixed
|
| 647 |
+
- Fix large (misleading) sequence giving UnicodeDecodeError (PR #137)
|
| 648 |
+
- Avoid using too insignificant chunk (PR #137)
|
| 649 |
+
|
| 650 |
+
### Added
|
| 651 |
+
- Add and expose function `set_logging_handler` to configure a specific StreamHandler from [@nmaynes](https://github.com/nmaynes) (PR #135)
|
| 652 |
+
- Add `CHANGELOG.md` entries, format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) (PR #141)
|
| 653 |
+
|
| 654 |
+
## [2.0.7](https://github.com/Ousret/charset_normalizer/compare/2.0.6...2.0.7) (2021-10-11)
|
| 655 |
+
### Added
|
| 656 |
+
- Add support for Kazakh (Cyrillic) language detection (PR #109)
|
| 657 |
+
|
| 658 |
+
### Changed
|
| 659 |
+
- Further, improve inferring the language from a given single-byte code page (PR #112)
|
| 660 |
+
- Vainly trying to leverage PEP263 when PEP3120 is not supported (PR #116)
|
| 661 |
+
- Refactoring for potential performance improvements in loops from [@adbar](https://github.com/adbar) (PR #113)
|
| 662 |
+
- Various detection improvement (MD+CD) (PR #117)
|
| 663 |
+
|
| 664 |
+
### Removed
|
| 665 |
+
- Remove redundant logging entry about detected language(s) (PR #115)
|
| 666 |
+
|
| 667 |
+
### Fixed
|
| 668 |
+
- Fix a minor inconsistency between Python 3.5 and other versions regarding language detection (PR #117 #102)
|
| 669 |
+
|
| 670 |
+
## [2.0.6](https://github.com/Ousret/charset_normalizer/compare/2.0.5...2.0.6) (2021-09-18)
|
| 671 |
+
### Fixed
|
| 672 |
+
- Unforeseen regression with the loss of the backward-compatibility with some older minor of Python 3.5.x (PR #100)
|
| 673 |
+
- Fix CLI crash when using --minimal output in certain cases (PR #103)
|
| 674 |
+
|
| 675 |
+
### Changed
|
| 676 |
+
- Minor improvement to the detection efficiency (less than 1%) (PR #106 #101)
|
| 677 |
+
|
| 678 |
+
## [2.0.5](https://github.com/Ousret/charset_normalizer/compare/2.0.4...2.0.5) (2021-09-14)
|
| 679 |
+
### Changed
|
| 680 |
+
- The project now comply with: flake8, mypy, isort and black to ensure a better overall quality (PR #81)
|
| 681 |
+
- The BC-support with v1.x was improved, the old staticmethods are restored (PR #82)
|
| 682 |
+
- The Unicode detection is slightly improved (PR #93)
|
| 683 |
+
- Add syntax sugar \_\_bool\_\_ for results CharsetMatches list-container (PR #91)
|
| 684 |
+
|
| 685 |
+
### Removed
|
| 686 |
+
- The project no longer raise warning on tiny content given for detection, will be simply logged as warning instead (PR #92)
|
| 687 |
+
|
| 688 |
+
### Fixed
|
| 689 |
+
- In some rare case, the chunks extractor could cut in the middle of a multi-byte character and could mislead the mess detection (PR #95)
|
| 690 |
+
- Some rare 'space' characters could trip up the UnprintablePlugin/Mess detection (PR #96)
|
| 691 |
+
- The MANIFEST.in was not exhaustive (PR #78)
|
| 692 |
+
|
| 693 |
+
## [2.0.4](https://github.com/Ousret/charset_normalizer/compare/2.0.3...2.0.4) (2021-07-30)
|
| 694 |
+
### Fixed
|
| 695 |
+
- The CLI no longer raise an unexpected exception when no encoding has been found (PR #70)
|
| 696 |
+
- Fix accessing the 'alphabets' property when the payload contains surrogate characters (PR #68)
|
| 697 |
+
- The logger could mislead (explain=True) on detected languages and the impact of one MBCS match (PR #72)
|
| 698 |
+
- Submatch factoring could be wrong in rare edge cases (PR #72)
|
| 699 |
+
- Multiple files given to the CLI were ignored when publishing results to STDOUT. (After the first path) (PR #72)
|
| 700 |
+
- Fix line endings from CRLF to LF for certain project files (PR #67)
|
| 701 |
+
|
| 702 |
+
### Changed
|
| 703 |
+
- Adjust the MD to lower the sensitivity, thus improving the global detection reliability (PR #69 #76)
|
| 704 |
+
- Allow fallback on specified encoding if any (PR #71)
|
| 705 |
+
|
| 706 |
+
## [2.0.3](https://github.com/Ousret/charset_normalizer/compare/2.0.2...2.0.3) (2021-07-16)
|
| 707 |
+
### Changed
|
| 708 |
+
- Part of the detection mechanism has been improved to be less sensitive, resulting in more accurate detection results. Especially ASCII. (PR #63)
|
| 709 |
+
- According to the community wishes, the detection will fall back on ASCII or UTF-8 in a last-resort case. (PR #64)
|
| 710 |
+
|
| 711 |
+
## [2.0.2](https://github.com/Ousret/charset_normalizer/compare/2.0.1...2.0.2) (2021-07-15)
|
| 712 |
+
### Fixed
|
| 713 |
+
- Empty/Too small JSON payload miss-detection fixed. Report from [@tseaver](https://github.com/tseaver) (PR #59)
|
| 714 |
+
|
| 715 |
+
### Changed
|
| 716 |
+
- Don't inject unicodedata2 into sys.modules from [@akx](https://github.com/akx) (PR #57)
|
| 717 |
+
|
| 718 |
+
## [2.0.1](https://github.com/Ousret/charset_normalizer/compare/2.0.0...2.0.1) (2021-07-13)
|
| 719 |
+
### Fixed
|
| 720 |
+
- Make it work where there isn't a filesystem available, dropping assets frequencies.json. Report from [@sethmlarson](https://github.com/sethmlarson). (PR #55)
|
| 721 |
+
- Using explain=False permanently disable the verbose output in the current runtime (PR #47)
|
| 722 |
+
- One log entry (language target preemptive) was not show in logs when using explain=True (PR #47)
|
| 723 |
+
- Fix undesired exception (ValueError) on getitem of instance CharsetMatches (PR #52)
|
| 724 |
+
|
| 725 |
+
### Changed
|
| 726 |
+
- Public function normalize default args values were not aligned with from_bytes (PR #53)
|
| 727 |
+
|
| 728 |
+
### Added
|
| 729 |
+
- You may now use charset aliases in cp_isolation and cp_exclusion arguments (PR #47)
|
| 730 |
+
|
| 731 |
+
## [2.0.0](https://github.com/Ousret/charset_normalizer/compare/1.4.1...2.0.0) (2021-07-02)
|
| 732 |
+
### Changed
|
| 733 |
+
- 4x to 5 times faster than the previous 1.4.0 release. At least 2x faster than Chardet.
|
| 734 |
+
- Accent has been made on UTF-8 detection, should perform rather instantaneous.
|
| 735 |
+
- The backward compatibility with Chardet has been greatly improved. The legacy detect function returns an identical charset name whenever possible.
|
| 736 |
+
- The detection mechanism has been slightly improved, now Turkish content is detected correctly (most of the time)
|
| 737 |
+
- The program has been rewritten to ease the readability and maintainability. (+Using static typing)+
|
| 738 |
+
- utf_7 detection has been reinstated.
|
| 739 |
+
|
| 740 |
+
### Removed
|
| 741 |
+
- This package no longer require anything when used with Python 3.5 (Dropped cached_property)
|
| 742 |
+
- Removed support for these languages: Catalan, Esperanto, Kazakh, Baque, Volapük, Azeri, Galician, Nynorsk, Macedonian, and Serbocroatian.
|
| 743 |
+
- The exception hook on UnicodeDecodeError has been removed.
|
| 744 |
+
|
| 745 |
+
### Deprecated
|
| 746 |
+
- Methods coherence_non_latin, w_counter, chaos_secondary_pass of the class CharsetMatch are now deprecated and scheduled for removal in v3.0
|
| 747 |
+
|
| 748 |
+
### Fixed
|
| 749 |
+
- The CLI output used the relative path of the file(s). Should be absolute.
|
| 750 |
+
|
| 751 |
+
## [1.4.1](https://github.com/Ousret/charset_normalizer/compare/1.4.0...1.4.1) (2021-05-28)
|
| 752 |
+
### Fixed
|
| 753 |
+
- Logger configuration/usage no longer conflict with others (PR #44)
|
| 754 |
+
|
| 755 |
+
## [1.4.0](https://github.com/Ousret/charset_normalizer/compare/1.3.9...1.4.0) (2021-05-21)
|
| 756 |
+
### Removed
|
| 757 |
+
- Using standard logging instead of using the package loguru.
|
| 758 |
+
- Dropping nose test framework in favor of the maintained pytest.
|
| 759 |
+
- Choose to not use dragonmapper package to help with gibberish Chinese/CJK text.
|
| 760 |
+
- Require cached_property only for Python 3.5 due to constraint. Dropping for every other interpreter version.
|
| 761 |
+
- Stop support for UTF-7 that does not contain a SIG.
|
| 762 |
+
- Dropping PrettyTable, replaced with pure JSON output in CLI.
|
| 763 |
+
|
| 764 |
+
### Fixed
|
| 765 |
+
- BOM marker in a CharsetNormalizerMatch instance could be False in rare cases even if obviously present. Due to the sub-match factoring process.
|
| 766 |
+
- Not searching properly for the BOM when trying utf32/16 parent codec.
|
| 767 |
+
|
| 768 |
+
### Changed
|
| 769 |
+
- Improving the package final size by compressing frequencies.json.
|
| 770 |
+
- Huge improvement over the larges payload.
|
| 771 |
+
|
| 772 |
+
### Added
|
| 773 |
+
- CLI now produces JSON consumable output.
|
| 774 |
+
- Return ASCII if given sequences fit. Given reasonable confidence.
|
| 775 |
+
|
| 776 |
+
## [1.3.9](https://github.com/Ousret/charset_normalizer/compare/1.3.8...1.3.9) (2021-05-13)
|
| 777 |
+
|
| 778 |
+
### Fixed
|
| 779 |
+
- In some very rare cases, you may end up getting encode/decode errors due to a bad bytes payload (PR #40)
|
| 780 |
+
|
| 781 |
+
## [1.3.8](https://github.com/Ousret/charset_normalizer/compare/1.3.7...1.3.8) (2021-05-12)
|
| 782 |
+
|
| 783 |
+
### Fixed
|
| 784 |
+
- Empty given payload for detection may cause an exception if trying to access the `alphabets` property. (PR #39)
|
| 785 |
+
|
| 786 |
+
## [1.3.7](https://github.com/Ousret/charset_normalizer/compare/1.3.6...1.3.7) (2021-05-12)
|
| 787 |
+
|
| 788 |
+
### Fixed
|
| 789 |
+
- The legacy detect function should return UTF-8-SIG if sig is present in the payload. (PR #38)
|
| 790 |
+
|
| 791 |
+
## [1.3.6](https://github.com/Ousret/charset_normalizer/compare/1.3.5...1.3.6) (2021-02-09)
|
| 792 |
+
|
| 793 |
+
### Changed
|
| 794 |
+
- Amend the previous release to allow prettytable 2.0 (PR #35)
|
| 795 |
+
|
| 796 |
+
## [1.3.5](https://github.com/Ousret/charset_normalizer/compare/1.3.4...1.3.5) (2021-02-08)
|
| 797 |
+
|
| 798 |
+
### Fixed
|
| 799 |
+
- Fix error while using the package with a python pre-release interpreter (PR #33)
|
| 800 |
+
|
| 801 |
+
### Changed
|
| 802 |
+
- Dependencies refactoring, constraints revised.
|
| 803 |
+
|
| 804 |
+
### Added
|
| 805 |
+
- Add python 3.9 and 3.10 to the supported interpreters
|
| 806 |
+
|
| 807 |
+
MIT License
|
| 808 |
+
|
| 809 |
+
Copyright (c) 2025 TAHRI Ahmed R.
|
| 810 |
+
|
| 811 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 812 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 813 |
+
in the Software without restriction, including without limitation the rights
|
| 814 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 815 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 816 |
+
furnished to do so, subject to the following conditions:
|
| 817 |
+
|
| 818 |
+
The above copyright notice and this permission notice shall be included in all
|
| 819 |
+
copies or substantial portions of the Software.
|
| 820 |
+
|
| 821 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 822 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 823 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 824 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 825 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 826 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 827 |
+
SOFTWARE.
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/RECORD
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
../../../bin/normalizer,sha256=ieF4ua2t96sQD3N96BFg-0KwnxJ9fWHPzPUsLBvIO9w,244
|
| 2 |
+
ada92cb5d92a588d1b93__mypyc.cpython-311-x86_64-linux-gnu.so,sha256=v_iWTzRsZXn2W3DwYZnauFr4Yf0A8w8xs_8j7mfTbFw,445952
|
| 3 |
+
charset_normalizer-3.4.9.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
| 4 |
+
charset_normalizer-3.4.9.dist-info/METADATA,sha256=DdPeF2CbDbZL0dRkPFvQ3TW16zxb53-LF7wZDy9KKsA,41678
|
| 5 |
+
charset_normalizer-3.4.9.dist-info/RECORD,,
|
| 6 |
+
charset_normalizer-3.4.9.dist-info/WHEEL,sha256=nn5AcWa_wYrb7xwtxEqCCax8LY-Ol14wJNGDK76X20E,190
|
| 7 |
+
charset_normalizer-3.4.9.dist-info/entry_points.txt,sha256=ADSTKrkXZ3hhdOVFi6DcUEHQRS0xfxDIE_pEz4wLIXA,65
|
| 8 |
+
charset_normalizer-3.4.9.dist-info/licenses/LICENSE,sha256=bQ1Bv-FwrGx9wkjJpj4lTQ-0WmDVCoJX0K-SxuJJuIc,1071
|
| 9 |
+
charset_normalizer-3.4.9.dist-info/top_level.txt,sha256=y65Zf_GLs5FHscGHzcUTGAldXcp5mEVXbh8Mo0ZlmWs,47
|
| 10 |
+
charset_normalizer/__init__.py,sha256=OKRxRv2Zhnqk00tqkN0c1BtJjm165fWXLydE52IKuHc,1590
|
| 11 |
+
charset_normalizer/__main__.py,sha256=yzYxMR-IhKRHYwcSlavEv8oGdwxsR89mr2X09qXGdps,109
|
| 12 |
+
charset_normalizer/__pycache__/__init__.cpython-311.pyc,,
|
| 13 |
+
charset_normalizer/__pycache__/__main__.cpython-311.pyc,,
|
| 14 |
+
charset_normalizer/__pycache__/api.cpython-311.pyc,,
|
| 15 |
+
charset_normalizer/__pycache__/cd.cpython-311.pyc,,
|
| 16 |
+
charset_normalizer/__pycache__/constant.cpython-311.pyc,,
|
| 17 |
+
charset_normalizer/__pycache__/legacy.cpython-311.pyc,,
|
| 18 |
+
charset_normalizer/__pycache__/md.cpython-311.pyc,,
|
| 19 |
+
charset_normalizer/__pycache__/models.cpython-311.pyc,,
|
| 20 |
+
charset_normalizer/__pycache__/utils.cpython-311.pyc,,
|
| 21 |
+
charset_normalizer/__pycache__/version.cpython-311.pyc,,
|
| 22 |
+
charset_normalizer/api.py,sha256=kXhFlZNMi6_p0YhbTeGTswoK_DZ6oeAdprPxE8F4yfM,42325
|
| 23 |
+
charset_normalizer/cd.cpython-311-x86_64-linux-gnu.so,sha256=AgR449IbdARrS3y2V8xOUmX_bYkKmVsoJRAGPmMJuwg,16040
|
| 24 |
+
charset_normalizer/cd.py,sha256=6dJOZvd0rrEEWvkx54kAEy9qAP_uIuiWMAi5wX8OZsY,15980
|
| 25 |
+
charset_normalizer/cli/__init__.py,sha256=D8I86lFk2-py45JvqxniTirSj_sFyE6sjaY_0-G1shc,136
|
| 26 |
+
charset_normalizer/cli/__main__.py,sha256=NPK3yYAkXXE7Uvs6IqijkgZzS6G6fguRFuKXzgpf8Uc,11949
|
| 27 |
+
charset_normalizer/cli/__pycache__/__init__.cpython-311.pyc,,
|
| 28 |
+
charset_normalizer/cli/__pycache__/__main__.cpython-311.pyc,,
|
| 29 |
+
charset_normalizer/constant.py,sha256=vp9HvaompccS71BXPZk8lVahonkPbBpeVE3NzKZe2sk,44624
|
| 30 |
+
charset_normalizer/legacy.py,sha256=bgkdEvubTCOPlMKOzuENXHMiarh36VaGbRvWSlIr1KA,2651
|
| 31 |
+
charset_normalizer/md.cpython-311-x86_64-linux-gnu.so,sha256=vXEPVSHEXBrFgbT6a_6DmmBAYZhSFnewl_sVGLNGyaA,16040
|
| 32 |
+
charset_normalizer/md.py,sha256=bwD18o2xLkGxBkuM1HPnYULsLBzNVe09j727Tng2tJw,32641
|
| 33 |
+
charset_normalizer/models.py,sha256=BAYguAENiSKBb7mf3DKzY3qgqjrjBeP0dySCmQSieXE,12830
|
| 34 |
+
charset_normalizer/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
| 35 |
+
charset_normalizer/utils.py,sha256=fMCjgJXzMFegyO-gX-NElZi1rmdF1y1xax2Y-V2eudo,13537
|
| 36 |
+
charset_normalizer/version.py,sha256=FUQoaRCvGW_xwCoesgsGTwzekLTlCkAJr7diNpv-gPI,115
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/WHEEL
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Wheel-Version: 1.0
|
| 2 |
+
Generator: setuptools (83.0.0)
|
| 3 |
+
Root-Is-Purelib: false
|
| 4 |
+
Tag: cp311-cp311-manylinux_2_17_x86_64
|
| 5 |
+
Tag: cp311-cp311-manylinux2014_x86_64
|
| 6 |
+
Tag: cp311-cp311-manylinux_2_28_x86_64
|
| 7 |
+
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/entry_points.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[console_scripts]
|
| 2 |
+
normalizer = charset_normalizer.cli:cli_detect
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/licenses/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 TAHRI Ahmed R.
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
venv/lib/python3.11/site-packages/charset_normalizer-3.4.9.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ada92cb5d92a588d1b93__mypyc
|
| 2 |
+
charset_normalizer
|
venv/lib/python3.11/site-packages/charset_normalizer/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Charset-Normalizer
|
| 3 |
+
~~~~~~~~~~~~~~
|
| 4 |
+
The Real First Universal Charset Detector.
|
| 5 |
+
A library that helps you read text from an unknown charset encoding.
|
| 6 |
+
Motivated by chardet, This package is trying to resolve the issue by taking a new approach.
|
| 7 |
+
All IANA character set names for which the Python core library provides codecs are supported.
|
| 8 |
+
|
| 9 |
+
Basic usage:
|
| 10 |
+
>>> from charset_normalizer import from_bytes
|
| 11 |
+
>>> results = from_bytes('Bсеки човек има право на образование. Oбразованието!'.encode('utf_8'))
|
| 12 |
+
>>> best_guess = results.best()
|
| 13 |
+
>>> str(best_guess)
|
| 14 |
+
'Bсеки човек има право на образование. Oбразованието!'
|
| 15 |
+
|
| 16 |
+
Others methods and usages are available - see the full documentation
|
| 17 |
+
at <https://github.com/Ousret/charset_normalizer>.
|
| 18 |
+
:copyright: (c) 2021 by Ahmed TAHRI
|
| 19 |
+
:license: MIT, see LICENSE for more details.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
import logging
|
| 25 |
+
|
| 26 |
+
from .api import from_bytes, from_fp, from_path, is_binary
|
| 27 |
+
from .legacy import detect
|
| 28 |
+
from .models import CharsetMatch, CharsetMatches
|
| 29 |
+
from .utils import set_logging_handler
|
| 30 |
+
from .version import VERSION, __version__
|
| 31 |
+
|
| 32 |
+
__all__ = (
|
| 33 |
+
"from_fp",
|
| 34 |
+
"from_path",
|
| 35 |
+
"from_bytes",
|
| 36 |
+
"is_binary",
|
| 37 |
+
"detect",
|
| 38 |
+
"CharsetMatch",
|
| 39 |
+
"CharsetMatches",
|
| 40 |
+
"__version__",
|
| 41 |
+
"VERSION",
|
| 42 |
+
"set_logging_handler",
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Attach a NullHandler to the top level logger by default
|
| 46 |
+
# https://docs.python.org/3.3/howto/logging.html#configuring-logging-for-a-library
|
| 47 |
+
|
| 48 |
+
logging.getLogger("charset_normalizer").addHandler(logging.NullHandler())
|
venv/lib/python3.11/site-packages/charset_normalizer/__main__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from .cli import cli_detect
|
| 4 |
+
|
| 5 |
+
if __name__ == "__main__":
|
| 6 |
+
cli_detect()
|