declearn.model.torch.TorchOptiModule
Bases: OptiModule
Hacky OptiModule subclass to wrap a torch Optimizer.
This torch-only OptiModule enables wrapping a torch.nn.Optimizer
to make it part of a declearn Optimizer pipeline, where it may be
combined with other framework-agnostic tools (notably FL-specific
ones such as the FedProx loss regularizer).
The wrapped torch Optimizer states will be placed on a device (CPU
or GPU) selected automatically based on the first input gradients'
placement OR on the global device policy when set_state
is used.
The reset
method may be used to drop internal optimizer states
and device-placement choices at once.
Please note that this relies on a hack that may have unforeseen side effects on the optimization algorithm if used carelessly and will at any rate cause some memory overhead. Thus it should be used sparingly, taking into account the following constraints and limitations:
- The wrapped optimizer class should have a "lr" (learning-rate) parameter, that will be forced to 1.0, so that updates' scaling remains the responsibility of the wrapping declearn Optimizer.
- The wrapped optimizer class should not make use of the watched parameters' values, only of their gradients, because it will in fact monitor artificial, zero-valued parameters at each step.
- If the module is to be used by the clients, the wrapped optimizer class must have been imported from a third-party package that is also available to the clients (e.g. torch).
Also note that passing a string input as optim_cls
(as is always done
when deserializing the module from its auto-generated config) may raise
security concerns due to its resulting in importing external code. As a
consequence, users will be asked to validate any non-torch import before
it is executed. This may be disabled when instantiating the module from
its init constructor but not when using from_config
, from_specs
or
deserialize
.
This class is mostly provided for experimental use of algorithms that are not natively available in declearn, for users that do not want to put in (or reserve for later) the effort of writing a custom, dedicated, framework-agnostic OptiModule subclass implementing that algorithm. If you encounter issues, please report to the declearn developers, and we will be happy to assist with debugging the present module and/or implementing the desired algorithm as a proper OptiModule.
Source code in declearn/model/torch/_optim.py
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 |
|
__init__(optim_cls, validate=True, **kwargs)
Instantiate a hacky torch optimizer plug-in module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optim_cls |
Union[Type[torch.optim.Optimizer], str]
|
Class constructor of the torch Optimizer that needs wrapping. A string containing its import path may be provided instead. |
required |
validate |
bool
|
Whether the user should be prompted to validate the module-
import action triggered in case |
True
|
**kwargs |
Any
|
Keyword arguments to |
{}
|
Source code in declearn/model/torch/_optim.py
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 |
|
reset()
Reset this module to its uninitialized state.
Discard the wrapped torch parameters (that define a required
specification of input gradients) and torch Optimizer. As a
consequence, the next call to run
will result in creating
a new Optimizer from scratch and setting a new specification.
Source code in declearn/model/torch/_optim.py
233 234 235 236 237 238 239 240 241 242 |
|
run(gradients)
Run input gradients through the wrapped torch Optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gradients |
Vector
|
Input gradients that are to be processed and updated. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If |
KeyError
|
If |
Returns:
Name | Type | Description |
---|---|---|
gradients |
TorchVector
|
Modified input gradients. The output Vector should be fully compatible with the input one - only the values of the wrapped coefficients may have changed. |
Source code in declearn/model/torch/_optim.py
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 |
|