-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmode.py
More file actions
531 lines (465 loc) · 18.5 KB
/
Copy pathmode.py
File metadata and controls
531 lines (465 loc) · 18.5 KB
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
"""Definition of a binding mode as a series of layers applied sequentially.
Members are explicitly re-exported in pyprobound.
"""
from __future__ import annotations
import logging
from collections.abc import Iterable, Iterator, Sequence
from typing import Any, Literal, TypeVar, overload
import torch
from torch import Tensor
from torch.nn.modules.module import _addindent
from typing_extensions import Self, override
from .base import Binding, BindingOptim, Call, Component, Spec, Step, Transform
from .containers import TModuleList
from .layers import (
PSAM,
Conv0d,
Conv1d,
Layer,
LengthManager,
ModeKey,
NonSpecific,
)
from .table import Table
from .utils import clear_cache
logger = logging.getLogger(__name__)
T = TypeVar("T", int, Tensor)
Cooperativity = Any
class Mode(Binding, LengthManager):
r"""Scores sequences with a series of layers applied sequentially.
.. math::
\frac{1}{K^{rel}_{\text{D}, a} (S_i)}
= \sum_x \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}
Attributes:
layers (TModuleList[Layer]): The layers to applied sequentially to an
input sequence.
log_hill (Tensor): The Hill coeffient in log space.
"""
unfreezable = Literal[Binding.unfreezable, "hill"]
def __init__(
self, layers: Iterable[Layer], train_hill: bool = False, name: str = ""
) -> None:
"""Initializes the binding mode.
Args:
layers: The layers to be applied sequentially to an input sequence.
train_hill: Whether to train a Hill coefficient.
name: A string used to describe the binding mode.
"""
super().__init__(name=name)
self.layers: TModuleList[Layer] = TModuleList(layers)
self._cooperativities: set["Cooperativity"] = set()
for layer_idx, layer in enumerate(self.layers):
layer._modes.add((self, layer_idx))
if len(self.layers) == 0:
raise ValueError(
"Cannot create binding mode with empty layers argument"
)
# Store model attributes
self.train_hill = train_hill
self.log_hill = torch.nn.Parameter(
torch.tensor(0.0), requires_grad=train_hill
)
# Verify scoring model
self.check_length_consistency()
@override
def __repr__(self) -> str:
if len(self.layers) > 1 or "\n" in repr(self.layers[0]):
return (
f"{type(self).__name__}( [\n "
+ "\n ".join(
_addindent(repr(i), 2) + "," for i in self.layers # type: ignore[no-untyped-call]
)
+ "\n] )"
)
return f"{type(self).__name__}( [ {repr(self.layers[0])} ] )"
@override
def __str__(self) -> str:
if self.name != "":
return super().__str__()
if str(self.key()) != repr(self.key()):
return str(self.key()).replace(
type(self.key()).__name__, type(self).__name__
)
return self.__repr__()
@override
@property
def out_channels(self) -> int:
return self.key().out_channels
@override
@property
def in_channels(self) -> int:
return self.key().in_channels
@property
def input_shape(self) -> int:
"""The number of elements in an input sequence."""
return self.layers[0].input_shape
@property
def min_input_length(self) -> int:
"""The minimum number of finite elements in an input sequence."""
return self.layers[0].min_input_length
@property
def max_input_length(self) -> int:
"""The maximum number of finite elements in an input sequence."""
return self.layers[0].max_input_length
@classmethod
def from_psam(
cls,
psam: PSAM,
prev: Table[Any] | Layer,
train_posbias: bool = False,
bias_mode: Literal["channel", "same", "reverse"] = "channel",
bias_bin: int = 1,
length_specific_bias: bool = True,
out_channel_indexing: Sequence[int] | None = None,
one_hot: bool = False,
unfold: bool = False,
normalize: bool = False,
train_hill: bool = False,
name: str = "",
) -> Self:
r"""Creates a new instance from a PSAM and an input component.
Args:
psam: The specification of the 1d convolution layer.
prev: If used as the first layer, the table that will be passed as
an input; otherwise, the layer that precedes it.
train_posbias: Whether to train a bias :math:`\omega(x)` for each
output position and channel.
bias_mode: Whether to train a separate bias for each output
channel, use the same bias across all output channels, or (if
`score_reverse`) flip it for the reverse output channels.
bias_bin: Applies the constraint
:math:`\omega(x_{i\times\text{bias_bin}}) = \cdots
= \omega(x_{(i+1)\times\text{bias_bin}-1})`.
length_specific_bias: Whether to train a separate bias parameter
for each input length.
out_channel_indexing: Output channel indexing, equivalent to
`Conv1d(seqs)[:,out_channel_indexing]`.
one_hot: Whether to use one-hot scoring instead of dense.
unfold: Whether to score using `unfold` or `conv1d` (if `one_hot`).
normalize: Whether to mean-center `log_posbias` over all windows.
train_hill: Whether to train a Hill coefficient.
name: A string used to describe the binding mode.
"""
if isinstance(prev, Layer):
input_shape = prev.out_len(prev.input_shape, "shape")
min_input_length = prev.out_len(prev.min_input_length, "min")
max_input_length = prev.out_len(prev.max_input_length, "max")
else:
input_shape = prev.input_shape
min_input_length = prev.min_read_length
max_input_length = prev.max_read_length
return cls(
[
Conv1d(
psam=psam,
input_shape=input_shape,
min_input_length=min_input_length,
max_input_length=max_input_length,
train_posbias=train_posbias,
bias_mode=bias_mode,
bias_bin=bias_bin,
length_specific_bias=length_specific_bias,
out_channel_indexing=out_channel_indexing,
one_hot=one_hot,
unfold=unfold,
normalize=normalize,
)
],
train_hill=train_hill,
name=name,
)
@classmethod
def from_nonspecific(
cls,
nonspecific: NonSpecific,
prev: Table[Any] | Layer,
train_posbias: bool = False,
name: str = "",
) -> Self:
"""Creates a new instance from a specification and an input component.
Args:
spec: The specification of the 0d convolution layer.
prev: If used as the first layer, the table that will be passed as
an input; otherwise, the layer that precedes it.
train_posbias: Whether to train a bias for each input length.
name: A string used to describe the binding mode.
"""
if isinstance(prev, Layer):
input_shape = prev.out_len(prev.input_shape, "shape")
min_input_length = prev.out_len(prev.min_input_length, "min")
max_input_length = prev.out_len(prev.max_input_length, "max")
else:
input_shape = prev.input_shape
min_input_length = prev.min_read_length
max_input_length = prev.max_read_length
return cls(
[
Conv0d(
nonspecific=nonspecific,
input_shape=input_shape,
min_input_length=min_input_length,
max_input_length=max_input_length,
train_posbias=train_posbias,
)
],
name=name,
)
@override
def key(self) -> ModeKey:
return ModeKey(layer.layer_spec for layer in self.layers)
@override
def out_len(
self, length: T, mode: Literal["min", "max", "shape"] = "shape"
) -> T:
return self.key().out_len(length=length, mode=mode)
@overload
def in_len(self, length: T, mode: Literal["min"]) -> T: ...
@overload
def in_len(self, length: T, mode: Literal["max"]) -> T | None: ...
@override
def in_len(
self, length: T, mode: Literal["min", "max"] = "max"
) -> T | None:
return self.key().in_len(length=length, mode=mode)
@override
def components(self) -> Iterator[Layer]:
return iter(self.layers)
@override
def check_length_consistency(self) -> None:
for layer in self.layers:
layer.check_length_consistency()
if len(self.layers) <= 1:
return
for layer_idx, (prev_layer, next_layer) in enumerate(
zip(self.layers, self.layers[1:]), start=1
):
if prev_layer.out_channels != next_layer.in_channels:
raise RuntimeError(
f"expected {next_layer.in_channels} in_channels for"
f" layer {layer_idx}, found {prev_layer.out_channels}"
)
prev_layer_out_shape = prev_layer.out_len(
prev_layer.input_shape, "shape"
)
if prev_layer_out_shape != next_layer.input_shape:
raise RuntimeError(
f"expected input_shape {prev_layer_out_shape} for"
f" layer {layer_idx}, found {next_layer.input_shape}"
)
prev_layer_min_out_len = prev_layer.out_len(
prev_layer.min_input_length, "min"
)
if prev_layer_min_out_len != next_layer.min_input_length:
raise RuntimeError(
f"expected min_input_length {prev_layer_min_out_len} for"
f" layer {layer_idx}, found {next_layer.min_input_length}"
)
prev_layer_max_out_len = prev_layer.out_len(
prev_layer.max_input_length, "max"
)
if prev_layer_max_out_len != next_layer.max_input_length:
raise RuntimeError(
f"expected max_input_length {prev_layer_max_out_len} for"
f" layer {layer_idx}, found {next_layer.max_input_length}"
)
@override
def unfreeze(self, parameter: unfreezable = "all") -> None:
if parameter in ("hill", "all") and self.train_hill:
self.log_hill.requires_grad_()
if parameter != "hill":
super().unfreeze(parameter)
@override
def optim_procedure(
self,
ancestry: tuple[Component, ...] | None = None,
current_order: dict[tuple[Spec, ...], BindingOptim] | None = None,
) -> dict[tuple[Spec, ...], BindingOptim]:
if ancestry is None:
ancestry = tuple()
if current_order is None:
current_order = {}
ancestry = ancestry + (self,)
# Check if already in current_order
if self.key() not in current_order:
binding_optim = BindingOptim(
{ancestry},
(
[Step([Call(ancestry[0], "freeze", {})])]
if len(ancestry) > 0
else []
),
)
current_order[self.key()] = binding_optim
else:
binding_optim = current_order[self.key()]
if ancestry in binding_optim.ancestry:
return current_order
binding_optim.ancestry.add(ancestry)
# Unfreeze scoring parameters
for layer in self.layers:
layer.update_binding_optim(binding_optim)
binding_optim.merge_binding_optim()
# Unfreeze all parameters
unfreeze_all = Step(
[Call(ancestry[0], "unfreeze", {"parameter": "all"})]
)
if unfreeze_all not in binding_optim.steps:
binding_optim.steps.append(unfreeze_all)
return current_order
@override
def _apply_block(
self, component: Component | None = None, cache_fun: str | None = None
) -> None:
if component is not None and cache_fun is not None:
logger.info(
"Applying block of %s on %s.%s", component, self, cache_fun
)
self._blocking[cache_fun].add(component)
logger.debug("%s._blocking=%s", self, self._blocking)
@override
def _release_block(
self, component: Component | None = None, cache_fun: str | None = None
) -> None:
if component is not None and cache_fun is not None:
logger.info(
"Releasing block of %s on %s.%s", component, self, cache_fun
)
self._blocking[cache_fun].discard(component)
logger.debug("%s._blocking=%s", self, self._blocking)
if len(self._blocking[cache_fun]) == 0:
logger.info("Clearing cache of %s.%s", self, cache_fun)
self._caches[cache_fun] = (None, None)
logger.debug("%s._caches=%s", self, self._caches)
clear_cache()
def update_read_length(
self,
left_shift: int = 0,
right_shift: int = 0,
min_len_shift: int = 0,
max_len_shift: int = 0,
new_min_len: int | None = None,
new_max_len: int | None = None,
) -> None:
"""Updates the input shape as part of a flank update.
Args:
left_shift: The change in size on the left side of the sequence.
right_shift: The change in size on the right side of the sequence.
min_len_shift: The change in the number of short input lengths.
max_len_shift: The change in the number of long input lengths.
new_min_len: The new `min_input_length`.
new_max_len: The new `max_input_length`.
"""
self._update_propagation(
0,
left_shift=left_shift,
right_shift=right_shift,
min_len_shift=min_len_shift,
max_len_shift=max_len_shift,
new_min_len=new_min_len,
new_max_len=new_max_len,
)
def _update_propagation(
self,
layer_idx: int,
left_shift: int = 0,
right_shift: int = 0,
min_len_shift: int = 0,
max_len_shift: int = 0,
new_min_len: int | None = None,
new_max_len: int | None = None,
) -> None:
"""Updates input shapes, called by a child LayerSpec after its update.
Args:
layer_idx: The index of the layer.
left_shift: The change in size on the left side of the sequence.
right_shift: The change in size on the right side of the sequence.
min_len_shift: The change in the number of short input lengths.
max_len_shift: The change in the number of long input lengths.
new_min_len: The new `min_input_length`.
new_max_len: The new `max_input_length`.
"""
if layer_idx >= len(self.layers):
for coop in self._cooperativities:
# pylint: disable-next=protected-access
coop._update_propagation(
self,
left_shift=left_shift,
right_shift=right_shift,
min_len_shift=min_len_shift,
max_len_shift=max_len_shift,
)
return
layer = self.layers[layer_idx]
# Establish baseline
old_shape = layer.out_len(layer.input_shape)
old_min_len = layer.out_len(layer.min_input_length, mode="min")
old_max_len = layer.out_len(layer.max_input_length, mode="max")
# Get shifts
left_shape = layer.out_len(layer.input_shape + left_shift)
right_shape = layer.out_len(
layer.input_shape + right_shift + left_shift
)
# Apply update
layer.update_input_length(
left_shift=left_shift,
right_shift=right_shift,
min_len_shift=min_len_shift,
max_len_shift=max_len_shift,
new_min_len=new_min_len,
new_max_len=new_max_len,
)
# Get new lengths
new_min_len = layer.out_len(layer.min_input_length, mode="min")
new_max_len = layer.out_len(layer.max_input_length, mode="max")
# Propagate update
layer.check_length_consistency()
self._update_propagation(
layer_idx + 1,
left_shift=left_shape - old_shape,
right_shift=right_shape - left_shape,
min_len_shift=new_min_len - old_min_len - right_shape + old_shape,
max_len_shift=new_max_len - old_max_len - right_shape + old_shape,
new_min_len=new_min_len,
new_max_len=new_max_len,
)
@override
def expected_sequence(self) -> Tensor:
return torch.full(
size=(1, self.in_channels, self.input_shape),
fill_value=1 / self.in_channels,
device=self.log_hill.device,
)
@override
@Transform.cache
def score_windows(self, seqs: Tensor) -> Tensor:
r"""Calculates the log score of each window before summing over them.
.. math::
\log \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}
Args:
seqs: A sequence tensor of shape
:math:`(\text{minibatch},\text{length})` or
:math:`(\text{minibatch},\text{in_channels},\text{in_length})`.
Returns:
A tensor with the log score of each window of shape
:math:`(\text{minibatch},\text{out_channels},\text{out_length})`.
"""
for module in self.layers:
seqs = module(seqs)
return seqs
@override
@Transform.cache
def forward(self, seqs: Tensor) -> Tensor:
r"""Calculates the log score of each sequence.
.. math::
\log \frac{1}{K^{rel}_{\text{D}, a} (S_i)}
= \log \sum_x \frac{1}{K^{rel}_{\text{D}, a} (S_{i, x})}
Args:
seqs: A sequence tensor of shape
:math:`(\text{minibatch},\text{length})` or
:math:`(\text{minibatch},\text{in_channels},\text{length})`.
Returns:
The log score tensor of shape :math:`(\text{minibatch},)`.
"""
return (torch.exp(self.log_hill) * self.score_windows(seqs)).logsumexp(
(1, 2)
)