-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervene.py
688 lines (577 loc) · 24.9 KB
/
intervene.py
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
"""Add-ins to a model that modify its state operations.
Intervenors are intended to be used with instances of types of `AbstractStagedModel`, to
patch state operations onto existing models. For example, it is common to take
an existing task and apply a certain force field on the biomechanical effector.
Instead of rewriting the biomechanical model itself, which would lead to a
proliferation of model classes with slightly different task conditions, we can
instead use `add_intervenor` to do surgery on the model using only a few lines
of code.
Likewise, since the exact parameters of an intervention often change between
(or within) task trials, it is convenient to be able to specify the distribution
of these parameters across trials. The function `schedule_intervenors` does
simultaneous surgery on task and model instances so that the model interventions
are paired with their trial-by-trial parameters, as provided by the task.
TODO:
- Could just use `operation` to distinguish `NetworkClamp` from
`NetworkConstantInput`. Though might need to rethink the NaN unit spec thing
- Could enforce that `out_where = in_where` for some intervenors, in particular
`AddNoise`, `NetworkClamp`, and `NetworkConstantInput`. These are intended to
make modifications to a part of the state, not to transform between parts.
:copyright: Copyright 2023-2024 by Matt Laporte.
:license: Apache 2.0. See LICENSE for details.
"""
from abc import abstractclassmethod, abstractmethod
from collections.abc import Mapping, Sequence, Callable
import copy
from dataclasses import fields
import logging
import operator as op
from typing import (
TYPE_CHECKING,
Any,
Generic,
Optional,
Tuple,
Type,
TypeVar,
Union,
)
import equinox as eqx
from equinox import AbstractVar, field
import jax
import jax.numpy as jnp
from jaxtyping import Array, ArrayLike, Float, PRNGKeyArray, PyTree
from feedbax.misc import get_unique_label
from feedbax._model import AbstractModel
from feedbax.state import AbstractState, StateT
from feedbax._tree import tree_call
if TYPE_CHECKING:
# from feedbax.model import AbstractModel
from feedbax.mechanics.mechanics import MechanicsState
from feedbax.nn import NetworkState
from feedbax._staged import AbstractStagedModel
from feedbax.task import AbstractTask
logger = logging.getLogger(__name__)
class AbstractIntervenorInput(eqx.Module):
"""Base class for PyTrees of intervention parameters.
Attributes:
active: Whether the intervention is active.
"""
active: AbstractVar[bool]
InputT = TypeVar("InputT", bound=AbstractIntervenorInput)
class AbstractIntervenor(eqx.Module, Generic[StateT, InputT]):
"""Base class for modules that intervene on a model's state.
Attributes:
params: Default intervention parameters.
in_where: Takes an instance of the model state, and returns the substate
corresponding to the intervenor's input.
out_where: Takes an instance of the model state, and returns the substate
corresponding to the intervenor's output. In many cases, `out_where` will
be the same as `in_where`.
operation: Which operation to use to combine the original and altered
`out_where` substates. For example, an intervenor that clamps a state
variable to a particular value should use an operation like `lambda x, y: y`
to replace the original with the altered state. On the other hand, an
additive intervenor would use the equivalent of `lambda x, y: x + y`.
label: The intervenor's label.
"""
params: AbstractVar[InputT]
in_where: AbstractVar[Callable[[StateT], PyTree[ArrayLike, "T"]]]
out_where: AbstractVar[Callable[[StateT], PyTree[ArrayLike, "S"]]]
operation: AbstractVar[Callable[[ArrayLike, ArrayLike], ArrayLike]]
label: AbstractVar[str]
@classmethod
def with_params(cls, **kwargs) -> "AbstractIntervenor[StateT, InputT]":
"""Constructor that accepts field names of `InputT` as keywords.
This is a convenience so we don't need to import the parameter class,
to instantiate an intervenor class it is associated with.
!!! Example
```python
CurlField.with_params(amplitude=10.0, label="MyCurlField")
```
"""
param_cls = next((f for f in fields(cls) if f.name == "params")).type
param_fields = [f.name for f in fields(param_cls)]
return cls(
param_cls(**{k: v for k, v in kwargs.items() if k in param_fields}),
**{k: v for k, v in kwargs.items() if k not in param_fields},
)
def __call__(self, input: InputT, state: StateT, *, key: PRNGKeyArray) -> StateT:
"""Return a state PyTree modified by the intervention.
Arguments:
input: PyTree of intervention parameters. If any leaves are `None`, they
will be replaced by the corresponding leaves of `self.params`.
state: The model state to be intervened upon.
key: A key to provide randomness for the intervention.
"""
params: InputT = eqx.combine(input, self.params)
return jax.lax.cond(
params.active,
lambda: eqx.tree_at( # Replace the `out_where` substate
self.out_where,
state,
jax.tree_map( # With the combined original and altered substates
lambda x, y: self.operation(x, y),
self.out_where(state),
self.transform(
params,
self.in_where(state),
key=key,
),
),
),
lambda: state,
)
@abstractmethod
def transform(
self,
params: InputT,
substate_in: PyTree[ArrayLike, "T"],
*,
key: Optional[PRNGKeyArray],
) -> PyTree[ArrayLike, "S"]:
"""Transforms the input substate to produce an altered output substate."""
...
class CurlFieldParams(AbstractIntervenorInput):
"""Parameters for a curl force field.
Attributes:
amplitude: The amplitude of the force field. Negative is clockwise, positive
is counterclockwise.
active: Whether the force field is active.
"""
amplitude: float = 0.0
active: bool = True
class CurlField(AbstractIntervenor["MechanicsState", CurlFieldParams]):
"""Apply a curl force field to a mechanical effector.
Attributes:
params: Default curl field parameters.
in_where: Returns the substate corresponding to the effector's velocity.
out_where: Returns the substate corresponding to the force on the effector.
operation: How to combine the effector force due to the curl field,
with the existing force on the effector. Default is addition.
label: The intervenor's label.
"""
params: CurlFieldParams = CurlFieldParams()
in_where: Callable[["MechanicsState"], Float[Array, "... ndim=2"]] = (
lambda state: state.effector.vel
)
out_where: Callable[["MechanicsState"], Float[Array, "... ndim=2"]] = (
lambda state: state.effector.force
)
operation: Callable[[ArrayLike, ArrayLike], ArrayLike] = op.add
label: str = "CurlField"
def transform(
self,
params: CurlFieldParams,
substate_in: Float[Array, "ndim=2"],
*,
key: Optional[PRNGKeyArray] = None,
) -> Float[Array, "ndim=2"]:
"""Transform velocity into curl force."""
scale = params.amplitude * jnp.array([-1, 1])
return scale * substate_in[..., ::-1]
class AddNoiseParams(AbstractIntervenorInput):
"""Parameters for adding noise to a state.
Attributes:
scale: Constant factor to multiply the noise samples.
noise_func: A function that returns noise samples.
active: Whether the intervention is active.
"""
scale: float = 1.0
noise_func: Callable = jax.random.normal
active: bool = True
class AddNoise(AbstractIntervenor[StateT, AddNoiseParams]):
"""Add noise to a part of the state.
Attributes:
params: Default intervention parameters.
out_where: Returns the substate to which noise is added.
operation: How to combine the noise with the substate. Default is
addition.
label: The intervenor's label.
"""
params: AddNoiseParams = AddNoiseParams()
in_where: Callable[[StateT], PyTree[Array, "T"]] = lambda state: state
out_where: Callable[[StateT], PyTree[Array, "T"]] = lambda state: state
operation: Callable[[ArrayLike, ArrayLike], ArrayLike] = op.add
label: str = "AddNoise"
def transform(
self,
params: AddNoiseParams,
substate_in: PyTree[Array, "T"],
*,
key: Optional[PRNGKeyArray],
) -> PyTree[Array, "T"]:
"""Return a PyTree of scaled noise arrays with the same structure/shapes as
`substate_in`."""
return jax.tree_map(
lambda x: params.scale * params.noise_func(
key,
shape=x.shape,
dtype=x.dtype,
),
substate_in,
)
class NetworkIntervenorParams(AbstractIntervenorInput):
"""Parameters for interventions on network unit activity.
Attributes:
unit_spec: A PyTree of arrays with the same tree structure and array shapes
as the substate of the network to be intervened upon, specifying the
unit-wise activities that constitute the perturbation.
active: Whether the intervention is active.
!!! Note ""
Note that `unit_spec` may be a single array—which is just a single-leaf PyTree
of arrays—when `out_where` of the intervenor is also a single array.
"""
unit_spec: Optional[PyTree] = None
active: bool = True
class NetworkClamp(AbstractIntervenor["NetworkState", NetworkIntervenorParams]):
"""Clamps some of a network's units' activities to given values.
Attributes:
params: Default intervention parameters.
out_where: Returns the substate of arrays giving the activity of the units
whose activities may be clamped.
operation: How to combine the original and clamped unit activities. Default
is to replace the original with the altered.
label: The intervenor's label.
"""
params: NetworkIntervenorParams = NetworkIntervenorParams()
in_where: Callable[["NetworkState"], PyTree[Array, "T"]] = (
lambda state: state.hidden
)
out_where: Callable[["NetworkState"], PyTree[Array, "T"]] = (
lambda state: state.hidden
)
operation: Callable[[ArrayLike, ArrayLike], ArrayLike] = lambda x, y: y
label: str = "NetworkClamp"
def transform(
self,
params: NetworkIntervenorParams,
substate_in: PyTree[Array, "T"],
*,
key: Optional[PRNGKeyArray] = None,
) -> PyTree[Array, "T"]:
return jax.tree_map(
lambda x, y: jnp.where(jnp.isnan(y), x, y),
substate_in,
params.unit_spec,
)
class NetworkConstantInput(AbstractIntervenor["NetworkState", NetworkIntervenorParams]):
"""Adds a constant input to some network units.
Attributes:
params: Default intervention parameters.
out_where: Returns the substate of arrays giving the activity of the units
to which a constant input may be added.
operation: How to combine the original and altered unit activities. Default
is addition.
label: The intervenor's label.
"""
params: NetworkIntervenorParams = NetworkIntervenorParams()
in_where: Callable[["NetworkState"], PyTree[Array, "T"]] = (
lambda state: state.hidden
)
out_where: Callable[["NetworkState"], PyTree[Array, "T"]] = (
lambda state: state.hidden
)
operation: Callable[[ArrayLike, ArrayLike], ArrayLike] = op.add
label: str = "NetworkConstantInput"
def transform(
self,
params: NetworkIntervenorParams,
substate_in: "NetworkState",
*,
key: Optional[PRNGKeyArray] = None,
) -> PyTree[Array, "T"]:
return jax.tree_map(jnp.nan_to_num, params.unit_spec)
class ConstantInputParams(AbstractIntervenorInput):
"""Parameters for adding a constant input to a state array.
Attributes:
scale: Constant factor to multiply the input.
arrays: A PyTree of arrays with the same tree structure and array shapes
as the substate of the state to be intervened upon, specifying the
constant input to be added.
active: Whether the intervention is active.
"""
scale: float = 1.0
arrays: Optional[PyTree] = ()
active: bool = True
class ConstantInput(AbstractIntervenor[StateT, ConstantInputParams]):
"""Adds a constant input to a state array.
Attributes:
params: Default intervention parameters.
out_where: Returns the substate of arrays to which a constant input is added.
operation: How to combine the original and altered substates. Default is addition.
label: The intervenor's label.
"""
params: ConstantInputParams = ConstantInputParams()
in_where: Callable[[StateT], PyTree[Array, "T"]] = lambda state: state
out_where: Callable[[StateT], PyTree[Array, "T"]] = lambda state: state
operation: Callable[[ArrayLike, ArrayLike], ArrayLike] = op.add
label: str = "ConstantInput"
def transform(
self,
params: ConstantInputParams,
substate_in: PyTree[Array, "T"],
*,
key: Optional[PRNGKeyArray] = None,
) -> PyTree[Array, "T"]:
return jax.tree_map(
lambda array: params.scale * array,
params.arrays,
)
def add_intervenor(
model: "AbstractStagedModel[StateT]",
intervenor: AbstractIntervenor,
stage_name: Optional[str] = None,
**kwargs: Any,
) -> "AbstractStagedModel[StateT]":
"""Return an updated model with an added intervenor.
!!! Note ""
This is a helper for calling `add_intervenors` with a single intervenor.
Arguments:
model: The model to which the intervenor will be added.
intervenor: The intervenor to add.
stage_name: The stage named in `model.model_spec` to which the intervenor will
be added.
kwargs: Additional keyword arguments to
[`add_intervenors`][feedbax.intervene.add_intervenors].
"""
if stage_name is not None:
return add_intervenors(model, {stage_name: [intervenor]}, **kwargs)
else:
return add_intervenors(model, [intervenor], **kwargs)
StateS = TypeVar("StateS", AbstractState, Array)
def add_intervenors(
model: "AbstractStagedModel[StateT]",
intervenors: Union[
Sequence[AbstractIntervenor[StateS, InputT]],
Mapping[str, Sequence[AbstractIntervenor[StateS, InputT]]],
],
where: Callable[
[AbstractModel[StateT]], "AbstractStagedModel[StateS]"
] = lambda model: model.step,
keep_existing: bool = True,
) -> "AbstractStagedModel[StateT]":
"""Return an updated model with added intervenors.
Arguments:
model: The model to which the intervenors will be added.
intervenors: The intervenors to add. May be a sequence of intervenors to
add to the first stage in `model.model_spec`, or a mapping from stage
name to a sequence of intervenors to add to that stage.
where: Takes `model` and returns the instance of `AbstractStagedModel` within
it (which may be `model` itself) to which to add the intervenors.
keep_existing: Whether to keep the existing intervenors belonging directly to
the instance of `AbstractStagedModel` to which the new intervenors are added.
If `True`, the new intervenors are appended to the existing ones; if `False`,
the old intervenors are replaced.
"""
if keep_existing:
existing_intervenors = where(model).intervenors
if isinstance(intervenors, Sequence):
# If a sequence is given, append to the first stage.
first_stage_label = next(iter(existing_intervenors))
intervenors_dict = eqx.tree_at(
lambda intervenors: intervenors[first_stage_label],
existing_intervenors,
existing_intervenors[first_stage_label] + list(intervenors),
)
elif isinstance(intervenors, dict):
intervenors_dict = copy.deepcopy(existing_intervenors)
for label, new_intervenors in intervenors.items():
intervenors_dict[label] += list(new_intervenors)
else:
raise ValueError("intervenors not a sequence or dict of sequences")
for k in intervenors_dict:
if k not in where(model).model_spec:
raise ValueError(f"{k} is not a valid model stage for intervention")
return eqx.tree_at(
lambda model: where(model).intervenors,
model,
intervenors_dict,
)
def remove_intervenors(
model: AbstractModel,
where: Callable[[AbstractModel], PyTree] = lambda model: model,
) -> AbstractModel:
"""Return a model with all intervenors removed."""
return eqx.tree_at(
where,
model,
jax.tree_map(
lambda model: add_intervenors(
model,
intervenors={stage: [] for stage in model.model_spec},
keep_existing=False,
),
where(model),
# Can't do `isinstance(x, AbstractModel)` because of circular import
is_leaf=lambda x: getattr(x, "model_spec", None) is not None,
),
)
class TimeSeriesParam(eqx.Module):
"""Wraps intervenor parameters that should be interpreted as time series.
Attributes:
param: The parameter to interpret as a time series.
"""
param: Array
def __call__(self):
"""Return the wrapped parameter."""
return self.param
def schedule_intervenor(
tasks: PyTree["AbstractTask"],
models: PyTree[AbstractModel[StateT]],
intervenor: AbstractIntervenor | Type[AbstractIntervenor],
# TODO: intervenor_validation
where: Callable[[AbstractModel[StateT]], Any] = lambda model: model,
stage_name: Optional[str] = None,
validation_same_schedule: bool = True,
intervention_spec: Optional[
AbstractIntervenorInput
] = None, #! wrong! distribution functions are allowed. only the PyTree structure is the same
intervention_spec_validation: Optional[AbstractIntervenorInput] = None,
default_active: bool = False,
) -> Tuple["AbstractTask", AbstractModel[StateT]]:
"""Adds an intervention to a model and a task.
!!! Note ""
Accepts either an intervenor instance, or an intervenor class. Passing
an intervenor instance but no `intervention_spec`, the instance's
`params` attribute is used as `intervention_spec`. This can be combined
with the intervenor's `with_params` constructor to define the
schedule. For example:
```python
schedule_intervenor(
tasks,
models,
CurlField.with_params(
amplitude=lambda trial_spec, *, key: jr.normal(key, (1,)),
active=True,
),
...
)
```
Passing an intervenor class and an `intervention_spec`, an instance
will be constructed from the two.
Passing an intervenor instance *and* an `intervention_spec`, the
instance's `params` will be replaced with the `intervention_spec`
before adding to the model.
Passing an intervenor class but no `intervention_spec`, an error is
raised due to insufficient information to schedule the intervention.
Passing a value for `intervention_spec_validation` allows for separate
control over the intervention schedule for the task's validation set.
Arguments:
tasks: The task(s) in whose trials the intervention will be scheduled
models: The model(s) to which the intervention will be added
intervenor: The intervenor (or intervenor class) to schedule.
where: Takes `model` and returns the instance of `AbstractStagedModel` within
it (which may be `model` itself) to which to add the intervenors.
stage_name: The name of the stage in `where(model).model_spec` to which to
add the intervenor. Defaults to the first stage.
validation_same_schedule: Whether the interventions should be scheduled
in the same way for the validation set as for the training set.
intervention_spec: The input to the intervenor, which may be
a constant, or a callable that is used by `task` to construct the
intervention parameters for each trial.
intervention_spec_validation: Same as `intervention_spec`, but for the
task's validation set. Overrides `validation_same_schedule`.
default_active: If the intervenor added to the model should have
`active=True` by default, so that the intervention will be
turned on even if the intervenor doesn't explicitly receive values
for its parameters.
"""
# A unique label is needed because `AbstractTask` uses a single dict to
# pass intervention parameters for all intervenors in an `AbstractStagedModel`,
# regardless of where they appear in the model hierarchy.
#
# The label should be unique across all models and tasks that the intervenor
# will be registered with.
invalid_labels_models = jax.tree_util.tree_reduce(
lambda x, y: x + y,
jax.tree_map(
lambda model: model.step._all_intervenor_labels,
models,
is_leaf=lambda x: isinstance(x, eqx.Module), # AbstractModel
),
is_leaf=lambda x: isinstance(x, tuple),
)
invalid_labels_tasks = jax.tree_util.tree_reduce(
lambda x, y: x + y,
jax.tree_map(
lambda task: tuple(task.intervention_specs.keys()),
tasks,
is_leaf=lambda x: isinstance(x, eqx.Module), # AbstractTask
),
is_leaf=lambda x: isinstance(x, tuple),
)
invalid_labels = set(invalid_labels_models + invalid_labels_tasks)
label = get_unique_label(intervenor.label, invalid_labels)
# Construct training and validation intervention specs
if intervention_spec is not None:
intervention_specs = {label: intervention_spec}
else:
if isinstance(intervenor, type(AbstractIntervenor)):
raise ValueError("Must pass intervention_spec if intervenor is a class")
intervention_specs = {label: intervenor.params}
if intervention_spec_validation is not None:
intervention_specs_validation = {label: intervention_spec_validation}
elif validation_same_schedule:
intervention_specs_validation = intervention_specs
else:
intervention_specs_validation = dict()
# Add the intervention specs to every task in `tasks`
tasks = jax.tree_map(
lambda task: eqx.tree_at(
lambda task: (task.intervention_specs, task.intervention_specs_validation),
task,
(
task.intervention_specs | intervention_specs,
task.intervention_specs_validation | intervention_specs_validation,
),
),
tasks,
is_leaf=lambda x: isinstance(x, eqx.Module), # AbstractTask
)
# Instantiate the intervenor if necessary, give it the unique label,
# and make sure it has a single set of default param values.
if isinstance(intervenor, type(AbstractIntervenor)):
intervenor = intervenor(params=intervention_specs[label])
intervenor_relabeled = eqx.tree_at(lambda x: x.label, intervenor, label)
# TODO: Should we let the user pass a `default_intervention_spec`?
key_example = jax.random.PRNGKey(0)
task_example = jax.tree_leaves(
tasks, is_leaf=lambda x: isinstance(x, eqx.Module) # AbstractTask
)[0]
trial_spec_example = task_example.get_train_trial(key_example)
# Use the validation spec to construct the defaults.
intervenor_defaults = eqx.tree_at(
lambda intervenor: intervenor.params,
intervenor_relabeled,
# We apply `tree_call` twice:
# 1. evaluate any lambdas;
# 2. unwrap any `TimeSeriesParam` instances.
tree_call(
tree_call(
intervention_specs_validation[label],
trial_spec_example,
key=key_example,
)
),
)
intervenor_final = eqx.tree_at(
lambda intervenor: intervenor.params.active,
intervenor_defaults,
default_active,
)
if stage_name is None:
intervenors = [intervenor_final]
else:
intervenors = {stage_name: [intervenor_final]}
# Add the intervenor to all of the models.
models = jax.tree_map(
lambda model: add_intervenors(
model,
intervenors,
where=where,
),
models,
is_leaf=lambda x: isinstance(x, AbstractModel),
)
return tasks, models