summaryrefslogtreecommitdiff
path: root/python-higher.spec
blob: 58ab97d1fc4981c2051d473dfb8035e4033b4a40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
%global _empty_manifest_terminate_build 0
Name:		python-higher
Version:	0.2.1
Release:	1
Summary:	A pytorch library allowing users to obtain higher order gradients over losses spanning training loops rather than individual training steps.
License:	Apache
URL:		https://github.com/facebookresearch/higher
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/fc/07/6b15c672bf1c0e561b695bb54a67eb0c94ffe7f898a1f8927ac37f96de90/higher-0.2.1.tar.gz
BuildArch:	noarch

Requires:	python3-torch

%description
`higher` is a library providing support for higher-order optimization, e.g. through unrolled first-order optimization loops, of "meta" aspects of these loops. It provides tools for turning existing `torch.nn.Module` instances "stateless", meaning that changes to the parameters thereof can be tracked, and gradient with regard to intermediate parameters can be taken. It also provides a suite of differentiable optimizers, to facilitate the implementation of various meta-learning approaches.
Full documentation is available at https://higher.readthedocs.io/en/latest/.
# Requirements and Installation
* Python version >= 3.5
* PyTorch version >= 1.3
To install `higher` from [PyPi](https://pypi.org/project/higher/):
```bash
pip install higher
```
To install `higher` from source:
```bash
git clone git@github.com:facebookresearch/higher.git
cd higher
pip install .
```
Alternatively `python setup.py install` will do the same thing.
# Citation
If you use `higher` in your research and found it helpful, please consider citing the following paper:
```bib
@article{grefenstette2019generalized,
  title={Generalized Inner Loop Meta-Learning},
  author={Grefenstette, Edward and Amos, Brandon and Yarats, Denis and Htut, Phu Mon and Molchanov, Artem and Meier, Franziska and Kiela, Douwe and Cho, Kyunghyun and Chintala, Soumith},
  journal={arXiv preprint arXiv:1910.01727},
  year={2019}
}
```
# Use case
## Your needs
You have a `model` with parameters `P`, where `P[t]` denotes the parameters at update timestep `t`.
You want to update the model through `k` steps of optimization, and compute gradients through the optimization process,
i.e. compute `torch.autograd.grad(P[k], P[0])` or obtain gradients that depend on this gradient pathway existing.
## Your obstacles
You are using some existing code for your `model`, so the parameters are stateful, preventing you from forming a graph with `P[t]` as nodes.
Even if you roll your own solution, you want to use optimization techniques beyond normal SGD, and `torch.optim` optimizers don't let you optimize "through" them.
## Your solution
Good news: `higher` has got you covered! Using our growing set of tools and utility functions, you can backpropagate through an unbounded number of model update steps for all your meta-learning needs.
This library includes:
* Helper functions for monkey-patching `torch.nn` modules to make them functional (non-stateful), i.e. feed their parameters as an extra argument during the forward pass.
* Classes implementing differentiable versions of `torch.optim.Adam` (and SGD), designed to track or branch out from the state of a "normal" `Adam` instance.
# Example Usage
Say your training code looks like this:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
for xs, ys in data:
    opt.zero_grad()
    logits = model(xs)
    loss = loss_function(logits, ys)
    loss.backward()
    opt.step()
```
To turn this into a differentiable version, the following changes should be introduced:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
# When you want to branch from the current state of your model and unroll
# optimization, follow this example. This context manager gets a snapshot of the
# current version of the model and optimizer at the point where you want to
# start unrolling and create a functional version `fmodel` which executes the
# forward pass of `model` with implicit fast weights which can be read by doing
# `fmodel.parameters()`, and a differentiable optimizer `diffopt` which ensures
# that at each step, gradient of `fmodel.parameters()` with regard to initial
# fast weights `fmodel.parameters(time=0)` (or any other part of the unrolled
# model history) is defined.
with higher.innerloop_ctx(model, opt) as (fmodel, diffopt):
    for xs, ys in data:
        logits = fmodel(xs)  # modified `params` can also be passed as a kwarg
        loss = loss_function(logits, ys)  # no need to call loss.backwards()
        diffopt.step(loss)  # note that `step` must take `loss` as an argument!
        # The line above gets P[t+1] from P[t] and loss[t]. `step` also returns
        # these new parameters, as an alternative to getting them from
        # `fmodel.fast_params` or `fmodel.parameters()` after calling
        # `diffopt.step`.
        # At this point, or at any point in the iteration, you can take the
        # gradient of `fmodel.parameters()` (or equivalently
        # `fmodel.fast_params`) w.r.t. `fmodel.parameters(time=0)` (equivalently
        # `fmodel.init_fast_params`). i.e. `fast_params` will always have
        # `grad_fn` as an attribute, and be part of the gradient tape.
    # At the end of your inner loop you can obtain these e.g. ...
    grad_of_grads = torch.autograd.grad(
        meta_loss_fn(fmodel.parameters()), fmodel.parameters(time=0))
```
**Beware** that when unrolling your optimisation like this for `k`, all gradients and all activations of your model at each step is kept in memory,
meaning the memory footprint of your model is `k` times greater.
# Adding your own optimizers
It is possible to use optimizers other that those found in `torch.optim`. A differentiable version must be implemented first. This can be done by subclassing `higher.optim.DifferentiableOptimizer` and overriding the `_update` method, following the arguments of the original. Assuming the logic of the optimizer being added follows the logic of those found in `torch.optim`, the steps to follow are more or less:
1. Remove the following code (no support for closures).
    ```
    loss = None
    if closure is not None:
        loss = closure()
    ```
2. Replace
    ```
    for group in self.param_groups:
        for p in group['params']:
            if p.grad is None:
                continue
            grad = p.grad.data
    ```
    with
    ```
    zipped = zip(self.param_groups, grouped_grads)
    for group_idx, (group, grads) in enumerate(zipped):
        for p_idx, (p, g) in enumerate(zip(group['params'], grads)):
          if g is None:
              continue
    ```
3. Replace `state = self.state[p]` with `state = self.state[group_idx][p_idx]`.
4. Replace any in-place op with a non in-place op, e.g. `t.add_(a, x).mul_(y)` should become `t = t.add(a, x).mul(y)` (note the assignment). Be careful to also track where dictionaries are being implicitly updated by such ops, e.g. if there is code of the form:
    ```
    p = state['k']
    p.add_(a, x)
    ```
    in the original optimizer, this code should be converted to
    ```
    p = state['k']
    state['k'] = p = p.add(a, x)
    ```
    to ensure the corresponding dictionary is.
5. Except where used for shape inference, replace instances of `t.data` with `t` for all `t`.
6. Be sure to update `group['params'][p_idx]` for each `p_idx` in need of update (those ignored will yield the original parameters in the fast weight collection). The latest fast weights will be returned by the inherited `step` function.
7. **Importantly**, you need to register your new differentiable optimizer with `higher` using `higher.register_optim` to ensure that it is recognized as an option by the library's methods. You can do this at any point after the definition of an optimizer, and before any `higher` code involving that optimizer is called. For example, if you have implemented `MyDiffOpt` as a differentiable version of some optimizer `MyOpt`, register it by adding the line `higher.register_optim(MyOpt, MyDiffOpt)` after the classes are defined.
You can find examples of how to test for gradient correctness using finite difference methods in `tests/test_optim.py`. Please note that some stability tricks may be needed to avoid `nan`s in the gradients. See the `higher.optim.DifferentiableAdam` implementation for examples of mitigation strategies, e.g. identify operations that yield exploding gradients, e.g. typically those taking the square roots of moving averages (which are intially zero), and register a backward hook using `x.register_hook` on the inputs `x` to those functions, using the helper function `_get_mask_closure` from `higher.optim`.
# Release Notes
See the [changelog](https://github.com/facebookresearch/higher/blob/master/CHANGELOG.md) for release notes.
# Known/Possible Issues
* See the [issues tracker](https://github.com/facebookresearch/higher/issues) for an up-to-date list.
* No support (or planned support) for `torch.nn.DataParallel` at this time. This would require a rewrite of `DataParallel`. Please raise an issue on the pytorch issue tracker if this matters to you.
* Some of the adaptative gradient-style differentiable optimizers may be unstable and yield NaNs when taking higher order gradients. Some tricks have been used to mitigate this risk. Please raise an issue if these are not sufficient in practice.
* Second-order gradients may not work with some CUDNN modules (mostly RNNs). From PyTorch v1.3 onwards, wrapping the code where models are used with `higher` using the following context manager should solve the issue:
```python
with torch.backends.cudnn.flags(enabled=False):
    # Your meta-learning code here...
```
# License
`higher` is released under Apache License Version 2.0.
# Thanks
Thanks to [Adam Paszke](https://gist.github.com/apaszke)
whose [gist](https://gist.github.com/apaszke/4c8ead6f17a781d589f6655692e7f6f0)
was the source of inspiration (and starting point) for our method for monkey
patching arbitrary `torch.nn` modules.
Thanks for the many interns, researchers, and engineers who helped road-test early versions of this library.

%package -n python3-higher
Summary:	A pytorch library allowing users to obtain higher order gradients over losses spanning training loops rather than individual training steps.
Provides:	python-higher
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-higher
`higher` is a library providing support for higher-order optimization, e.g. through unrolled first-order optimization loops, of "meta" aspects of these loops. It provides tools for turning existing `torch.nn.Module` instances "stateless", meaning that changes to the parameters thereof can be tracked, and gradient with regard to intermediate parameters can be taken. It also provides a suite of differentiable optimizers, to facilitate the implementation of various meta-learning approaches.
Full documentation is available at https://higher.readthedocs.io/en/latest/.
# Requirements and Installation
* Python version >= 3.5
* PyTorch version >= 1.3
To install `higher` from [PyPi](https://pypi.org/project/higher/):
```bash
pip install higher
```
To install `higher` from source:
```bash
git clone git@github.com:facebookresearch/higher.git
cd higher
pip install .
```
Alternatively `python setup.py install` will do the same thing.
# Citation
If you use `higher` in your research and found it helpful, please consider citing the following paper:
```bib
@article{grefenstette2019generalized,
  title={Generalized Inner Loop Meta-Learning},
  author={Grefenstette, Edward and Amos, Brandon and Yarats, Denis and Htut, Phu Mon and Molchanov, Artem and Meier, Franziska and Kiela, Douwe and Cho, Kyunghyun and Chintala, Soumith},
  journal={arXiv preprint arXiv:1910.01727},
  year={2019}
}
```
# Use case
## Your needs
You have a `model` with parameters `P`, where `P[t]` denotes the parameters at update timestep `t`.
You want to update the model through `k` steps of optimization, and compute gradients through the optimization process,
i.e. compute `torch.autograd.grad(P[k], P[0])` or obtain gradients that depend on this gradient pathway existing.
## Your obstacles
You are using some existing code for your `model`, so the parameters are stateful, preventing you from forming a graph with `P[t]` as nodes.
Even if you roll your own solution, you want to use optimization techniques beyond normal SGD, and `torch.optim` optimizers don't let you optimize "through" them.
## Your solution
Good news: `higher` has got you covered! Using our growing set of tools and utility functions, you can backpropagate through an unbounded number of model update steps for all your meta-learning needs.
This library includes:
* Helper functions for monkey-patching `torch.nn` modules to make them functional (non-stateful), i.e. feed their parameters as an extra argument during the forward pass.
* Classes implementing differentiable versions of `torch.optim.Adam` (and SGD), designed to track or branch out from the state of a "normal" `Adam` instance.
# Example Usage
Say your training code looks like this:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
for xs, ys in data:
    opt.zero_grad()
    logits = model(xs)
    loss = loss_function(logits, ys)
    loss.backward()
    opt.step()
```
To turn this into a differentiable version, the following changes should be introduced:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
# When you want to branch from the current state of your model and unroll
# optimization, follow this example. This context manager gets a snapshot of the
# current version of the model and optimizer at the point where you want to
# start unrolling and create a functional version `fmodel` which executes the
# forward pass of `model` with implicit fast weights which can be read by doing
# `fmodel.parameters()`, and a differentiable optimizer `diffopt` which ensures
# that at each step, gradient of `fmodel.parameters()` with regard to initial
# fast weights `fmodel.parameters(time=0)` (or any other part of the unrolled
# model history) is defined.
with higher.innerloop_ctx(model, opt) as (fmodel, diffopt):
    for xs, ys in data:
        logits = fmodel(xs)  # modified `params` can also be passed as a kwarg
        loss = loss_function(logits, ys)  # no need to call loss.backwards()
        diffopt.step(loss)  # note that `step` must take `loss` as an argument!
        # The line above gets P[t+1] from P[t] and loss[t]. `step` also returns
        # these new parameters, as an alternative to getting them from
        # `fmodel.fast_params` or `fmodel.parameters()` after calling
        # `diffopt.step`.
        # At this point, or at any point in the iteration, you can take the
        # gradient of `fmodel.parameters()` (or equivalently
        # `fmodel.fast_params`) w.r.t. `fmodel.parameters(time=0)` (equivalently
        # `fmodel.init_fast_params`). i.e. `fast_params` will always have
        # `grad_fn` as an attribute, and be part of the gradient tape.
    # At the end of your inner loop you can obtain these e.g. ...
    grad_of_grads = torch.autograd.grad(
        meta_loss_fn(fmodel.parameters()), fmodel.parameters(time=0))
```
**Beware** that when unrolling your optimisation like this for `k`, all gradients and all activations of your model at each step is kept in memory,
meaning the memory footprint of your model is `k` times greater.
# Adding your own optimizers
It is possible to use optimizers other that those found in `torch.optim`. A differentiable version must be implemented first. This can be done by subclassing `higher.optim.DifferentiableOptimizer` and overriding the `_update` method, following the arguments of the original. Assuming the logic of the optimizer being added follows the logic of those found in `torch.optim`, the steps to follow are more or less:
1. Remove the following code (no support for closures).
    ```
    loss = None
    if closure is not None:
        loss = closure()
    ```
2. Replace
    ```
    for group in self.param_groups:
        for p in group['params']:
            if p.grad is None:
                continue
            grad = p.grad.data
    ```
    with
    ```
    zipped = zip(self.param_groups, grouped_grads)
    for group_idx, (group, grads) in enumerate(zipped):
        for p_idx, (p, g) in enumerate(zip(group['params'], grads)):
          if g is None:
              continue
    ```
3. Replace `state = self.state[p]` with `state = self.state[group_idx][p_idx]`.
4. Replace any in-place op with a non in-place op, e.g. `t.add_(a, x).mul_(y)` should become `t = t.add(a, x).mul(y)` (note the assignment). Be careful to also track where dictionaries are being implicitly updated by such ops, e.g. if there is code of the form:
    ```
    p = state['k']
    p.add_(a, x)
    ```
    in the original optimizer, this code should be converted to
    ```
    p = state['k']
    state['k'] = p = p.add(a, x)
    ```
    to ensure the corresponding dictionary is.
5. Except where used for shape inference, replace instances of `t.data` with `t` for all `t`.
6. Be sure to update `group['params'][p_idx]` for each `p_idx` in need of update (those ignored will yield the original parameters in the fast weight collection). The latest fast weights will be returned by the inherited `step` function.
7. **Importantly**, you need to register your new differentiable optimizer with `higher` using `higher.register_optim` to ensure that it is recognized as an option by the library's methods. You can do this at any point after the definition of an optimizer, and before any `higher` code involving that optimizer is called. For example, if you have implemented `MyDiffOpt` as a differentiable version of some optimizer `MyOpt`, register it by adding the line `higher.register_optim(MyOpt, MyDiffOpt)` after the classes are defined.
You can find examples of how to test for gradient correctness using finite difference methods in `tests/test_optim.py`. Please note that some stability tricks may be needed to avoid `nan`s in the gradients. See the `higher.optim.DifferentiableAdam` implementation for examples of mitigation strategies, e.g. identify operations that yield exploding gradients, e.g. typically those taking the square roots of moving averages (which are intially zero), and register a backward hook using `x.register_hook` on the inputs `x` to those functions, using the helper function `_get_mask_closure` from `higher.optim`.
# Release Notes
See the [changelog](https://github.com/facebookresearch/higher/blob/master/CHANGELOG.md) for release notes.
# Known/Possible Issues
* See the [issues tracker](https://github.com/facebookresearch/higher/issues) for an up-to-date list.
* No support (or planned support) for `torch.nn.DataParallel` at this time. This would require a rewrite of `DataParallel`. Please raise an issue on the pytorch issue tracker if this matters to you.
* Some of the adaptative gradient-style differentiable optimizers may be unstable and yield NaNs when taking higher order gradients. Some tricks have been used to mitigate this risk. Please raise an issue if these are not sufficient in practice.
* Second-order gradients may not work with some CUDNN modules (mostly RNNs). From PyTorch v1.3 onwards, wrapping the code where models are used with `higher` using the following context manager should solve the issue:
```python
with torch.backends.cudnn.flags(enabled=False):
    # Your meta-learning code here...
```
# License
`higher` is released under Apache License Version 2.0.
# Thanks
Thanks to [Adam Paszke](https://gist.github.com/apaszke)
whose [gist](https://gist.github.com/apaszke/4c8ead6f17a781d589f6655692e7f6f0)
was the source of inspiration (and starting point) for our method for monkey
patching arbitrary `torch.nn` modules.
Thanks for the many interns, researchers, and engineers who helped road-test early versions of this library.

%package help
Summary:	Development documents and examples for higher
Provides:	python3-higher-doc
%description help
`higher` is a library providing support for higher-order optimization, e.g. through unrolled first-order optimization loops, of "meta" aspects of these loops. It provides tools for turning existing `torch.nn.Module` instances "stateless", meaning that changes to the parameters thereof can be tracked, and gradient with regard to intermediate parameters can be taken. It also provides a suite of differentiable optimizers, to facilitate the implementation of various meta-learning approaches.
Full documentation is available at https://higher.readthedocs.io/en/latest/.
# Requirements and Installation
* Python version >= 3.5
* PyTorch version >= 1.3
To install `higher` from [PyPi](https://pypi.org/project/higher/):
```bash
pip install higher
```
To install `higher` from source:
```bash
git clone git@github.com:facebookresearch/higher.git
cd higher
pip install .
```
Alternatively `python setup.py install` will do the same thing.
# Citation
If you use `higher` in your research and found it helpful, please consider citing the following paper:
```bib
@article{grefenstette2019generalized,
  title={Generalized Inner Loop Meta-Learning},
  author={Grefenstette, Edward and Amos, Brandon and Yarats, Denis and Htut, Phu Mon and Molchanov, Artem and Meier, Franziska and Kiela, Douwe and Cho, Kyunghyun and Chintala, Soumith},
  journal={arXiv preprint arXiv:1910.01727},
  year={2019}
}
```
# Use case
## Your needs
You have a `model` with parameters `P`, where `P[t]` denotes the parameters at update timestep `t`.
You want to update the model through `k` steps of optimization, and compute gradients through the optimization process,
i.e. compute `torch.autograd.grad(P[k], P[0])` or obtain gradients that depend on this gradient pathway existing.
## Your obstacles
You are using some existing code for your `model`, so the parameters are stateful, preventing you from forming a graph with `P[t]` as nodes.
Even if you roll your own solution, you want to use optimization techniques beyond normal SGD, and `torch.optim` optimizers don't let you optimize "through" them.
## Your solution
Good news: `higher` has got you covered! Using our growing set of tools and utility functions, you can backpropagate through an unbounded number of model update steps for all your meta-learning needs.
This library includes:
* Helper functions for monkey-patching `torch.nn` modules to make them functional (non-stateful), i.e. feed their parameters as an extra argument during the forward pass.
* Classes implementing differentiable versions of `torch.optim.Adam` (and SGD), designed to track or branch out from the state of a "normal" `Adam` instance.
# Example Usage
Say your training code looks like this:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
for xs, ys in data:
    opt.zero_grad()
    logits = model(xs)
    loss = loss_function(logits, ys)
    loss.backward()
    opt.step()
```
To turn this into a differentiable version, the following changes should be introduced:
```python
model = MyModel()
opt = torch.optim.Adam(model.parameters())
# When you want to branch from the current state of your model and unroll
# optimization, follow this example. This context manager gets a snapshot of the
# current version of the model and optimizer at the point where you want to
# start unrolling and create a functional version `fmodel` which executes the
# forward pass of `model` with implicit fast weights which can be read by doing
# `fmodel.parameters()`, and a differentiable optimizer `diffopt` which ensures
# that at each step, gradient of `fmodel.parameters()` with regard to initial
# fast weights `fmodel.parameters(time=0)` (or any other part of the unrolled
# model history) is defined.
with higher.innerloop_ctx(model, opt) as (fmodel, diffopt):
    for xs, ys in data:
        logits = fmodel(xs)  # modified `params` can also be passed as a kwarg
        loss = loss_function(logits, ys)  # no need to call loss.backwards()
        diffopt.step(loss)  # note that `step` must take `loss` as an argument!
        # The line above gets P[t+1] from P[t] and loss[t]. `step` also returns
        # these new parameters, as an alternative to getting them from
        # `fmodel.fast_params` or `fmodel.parameters()` after calling
        # `diffopt.step`.
        # At this point, or at any point in the iteration, you can take the
        # gradient of `fmodel.parameters()` (or equivalently
        # `fmodel.fast_params`) w.r.t. `fmodel.parameters(time=0)` (equivalently
        # `fmodel.init_fast_params`). i.e. `fast_params` will always have
        # `grad_fn` as an attribute, and be part of the gradient tape.
    # At the end of your inner loop you can obtain these e.g. ...
    grad_of_grads = torch.autograd.grad(
        meta_loss_fn(fmodel.parameters()), fmodel.parameters(time=0))
```
**Beware** that when unrolling your optimisation like this for `k`, all gradients and all activations of your model at each step is kept in memory,
meaning the memory footprint of your model is `k` times greater.
# Adding your own optimizers
It is possible to use optimizers other that those found in `torch.optim`. A differentiable version must be implemented first. This can be done by subclassing `higher.optim.DifferentiableOptimizer` and overriding the `_update` method, following the arguments of the original. Assuming the logic of the optimizer being added follows the logic of those found in `torch.optim`, the steps to follow are more or less:
1. Remove the following code (no support for closures).
    ```
    loss = None
    if closure is not None:
        loss = closure()
    ```
2. Replace
    ```
    for group in self.param_groups:
        for p in group['params']:
            if p.grad is None:
                continue
            grad = p.grad.data
    ```
    with
    ```
    zipped = zip(self.param_groups, grouped_grads)
    for group_idx, (group, grads) in enumerate(zipped):
        for p_idx, (p, g) in enumerate(zip(group['params'], grads)):
          if g is None:
              continue
    ```
3. Replace `state = self.state[p]` with `state = self.state[group_idx][p_idx]`.
4. Replace any in-place op with a non in-place op, e.g. `t.add_(a, x).mul_(y)` should become `t = t.add(a, x).mul(y)` (note the assignment). Be careful to also track where dictionaries are being implicitly updated by such ops, e.g. if there is code of the form:
    ```
    p = state['k']
    p.add_(a, x)
    ```
    in the original optimizer, this code should be converted to
    ```
    p = state['k']
    state['k'] = p = p.add(a, x)
    ```
    to ensure the corresponding dictionary is.
5. Except where used for shape inference, replace instances of `t.data` with `t` for all `t`.
6. Be sure to update `group['params'][p_idx]` for each `p_idx` in need of update (those ignored will yield the original parameters in the fast weight collection). The latest fast weights will be returned by the inherited `step` function.
7. **Importantly**, you need to register your new differentiable optimizer with `higher` using `higher.register_optim` to ensure that it is recognized as an option by the library's methods. You can do this at any point after the definition of an optimizer, and before any `higher` code involving that optimizer is called. For example, if you have implemented `MyDiffOpt` as a differentiable version of some optimizer `MyOpt`, register it by adding the line `higher.register_optim(MyOpt, MyDiffOpt)` after the classes are defined.
You can find examples of how to test for gradient correctness using finite difference methods in `tests/test_optim.py`. Please note that some stability tricks may be needed to avoid `nan`s in the gradients. See the `higher.optim.DifferentiableAdam` implementation for examples of mitigation strategies, e.g. identify operations that yield exploding gradients, e.g. typically those taking the square roots of moving averages (which are intially zero), and register a backward hook using `x.register_hook` on the inputs `x` to those functions, using the helper function `_get_mask_closure` from `higher.optim`.
# Release Notes
See the [changelog](https://github.com/facebookresearch/higher/blob/master/CHANGELOG.md) for release notes.
# Known/Possible Issues
* See the [issues tracker](https://github.com/facebookresearch/higher/issues) for an up-to-date list.
* No support (or planned support) for `torch.nn.DataParallel` at this time. This would require a rewrite of `DataParallel`. Please raise an issue on the pytorch issue tracker if this matters to you.
* Some of the adaptative gradient-style differentiable optimizers may be unstable and yield NaNs when taking higher order gradients. Some tricks have been used to mitigate this risk. Please raise an issue if these are not sufficient in practice.
* Second-order gradients may not work with some CUDNN modules (mostly RNNs). From PyTorch v1.3 onwards, wrapping the code where models are used with `higher` using the following context manager should solve the issue:
```python
with torch.backends.cudnn.flags(enabled=False):
    # Your meta-learning code here...
```
# License
`higher` is released under Apache License Version 2.0.
# Thanks
Thanks to [Adam Paszke](https://gist.github.com/apaszke)
whose [gist](https://gist.github.com/apaszke/4c8ead6f17a781d589f6655692e7f6f0)
was the source of inspiration (and starting point) for our method for monkey
patching arbitrary `torch.nn` modules.
Thanks for the many interns, researchers, and engineers who helped road-test early versions of this library.

%prep
%autosetup -n higher-0.2.1

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-higher -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Mon Apr 10 2023 Python_Bot <Python_Bot@openeuler.org> - 0.2.1-1
- Package Spec generated