bitsandbytes 0.50.0__py3-none-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. bitsandbytes/__init__.py +78 -0
  2. bitsandbytes/__main__.py +4 -0
  3. bitsandbytes/_ops.py +510 -0
  4. bitsandbytes/autograd/__init__.py +0 -0
  5. bitsandbytes/autograd/_functions.py +491 -0
  6. bitsandbytes/backends/__init__.py +0 -0
  7. bitsandbytes/backends/cpu/__init__.py +0 -0
  8. bitsandbytes/backends/cpu/ops.py +580 -0
  9. bitsandbytes/backends/cuda/__init__.py +0 -0
  10. bitsandbytes/backends/cuda/ops.py +1199 -0
  11. bitsandbytes/backends/default/__init__.py +0 -0
  12. bitsandbytes/backends/default/ops.py +632 -0
  13. bitsandbytes/backends/hpu/__init__.py +0 -0
  14. bitsandbytes/backends/hpu/ops.py +53 -0
  15. bitsandbytes/backends/mps/__init__.py +0 -0
  16. bitsandbytes/backends/mps/ops.py +277 -0
  17. bitsandbytes/backends/triton/__init__.py +0 -0
  18. bitsandbytes/backends/triton/kernels_4bit.py +577 -0
  19. bitsandbytes/backends/triton/kernels_8bit_quant.py +195 -0
  20. bitsandbytes/backends/triton/kernels_optim.py +1177 -0
  21. bitsandbytes/backends/triton/ops.py +304 -0
  22. bitsandbytes/backends/utils.py +94 -0
  23. bitsandbytes/backends/xpu/__init__.py +0 -0
  24. bitsandbytes/backends/xpu/ops.py +305 -0
  25. bitsandbytes/cextension.py +405 -0
  26. bitsandbytes/consts.py +12 -0
  27. bitsandbytes/cuda_specs.py +111 -0
  28. bitsandbytes/diagnostics/__init__.py +0 -0
  29. bitsandbytes/diagnostics/cuda.py +193 -0
  30. bitsandbytes/diagnostics/main.py +134 -0
  31. bitsandbytes/diagnostics/utils.py +12 -0
  32. bitsandbytes/functional.py +1810 -0
  33. bitsandbytes/libbitsandbytes_cpu.dll +0 -0
  34. bitsandbytes/nn/__init__.py +19 -0
  35. bitsandbytes/nn/modules.py +1220 -0
  36. bitsandbytes/nn/parametrize.py +206 -0
  37. bitsandbytes/optim/__init__.py +22 -0
  38. bitsandbytes/optim/adagrad.py +187 -0
  39. bitsandbytes/optim/adam.py +346 -0
  40. bitsandbytes/optim/adamw.py +337 -0
  41. bitsandbytes/optim/ademamix.py +410 -0
  42. bitsandbytes/optim/lamb.py +190 -0
  43. bitsandbytes/optim/lars.py +259 -0
  44. bitsandbytes/optim/lion.py +266 -0
  45. bitsandbytes/optim/optimizer.py +756 -0
  46. bitsandbytes/optim/rmsprop.py +170 -0
  47. bitsandbytes/optim/sgd.py +152 -0
  48. bitsandbytes/py.typed +0 -0
  49. bitsandbytes/utils.py +208 -0
  50. bitsandbytes-0.50.0.dist-info/METADATA +288 -0
  51. bitsandbytes-0.50.0.dist-info/RECORD +54 -0
  52. bitsandbytes-0.50.0.dist-info/WHEEL +5 -0
  53. bitsandbytes-0.50.0.dist-info/licenses/LICENSE +21 -0
  54. bitsandbytes-0.50.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,259 @@
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
+ 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
@@ -0,0 +1,266 @@
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
+ )