titans-pytorch 0.3.3__tar.gz → 0.3.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: titans-pytorch
3
- Version: 0.3.3
3
+ Version: 0.3.4
4
4
  Summary: Titans
5
5
  Project-URL: Homepage, https://pypi.org/project/titans-pytorch/
6
6
  Project-URL: Repository, https://github.com/lucidrains/titans-pytorch
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "titans-pytorch"
3
- version = "0.3.3"
3
+ version = "0.3.4"
4
4
  description = "Titans"
5
5
  authors = [
6
6
  { name = "Phil Wang", email = "lucidrains@gmail.com" }
@@ -31,6 +31,7 @@ def torch_default_dtype(dtype):
31
31
  @pytest.mark.parametrize('qk_rmsnorm', (False, True))
32
32
  @pytest.mark.parametrize('max_grad_norm', (None, 2.))
33
33
  @pytest.mark.parametrize('per_parameter_lr_modulation', (False, True))
34
+ @pytest.mark.parametrize('per_head_learned_parameters', (False, True))
34
35
  def test_titans(
35
36
  seq_len,
36
37
  silu,
@@ -39,7 +40,8 @@ def test_titans(
39
40
  momentum,
40
41
  qk_rmsnorm,
41
42
  max_grad_norm,
42
- per_parameter_lr_modulation
43
+ per_parameter_lr_modulation,
44
+ per_head_learned_parameters
43
45
  ):
44
46
  mem = NeuralMemory(
45
47
  dim = 16,
@@ -50,6 +52,7 @@ def test_titans(
50
52
  momentum = momentum,
51
53
  qk_rmsnorm = qk_rmsnorm,
52
54
  per_parameter_lr_modulation = per_parameter_lr_modulation,
55
+ per_head_learned_parameters = per_head_learned_parameters
53
56
  )
54
57
 
55
58
  seq = torch.randn(2, seq_len, 16)
@@ -3,18 +3,39 @@ from typing import Callable
3
3
 
4
4
  import torch
5
5
  from torch import Tensor
6
+ from torch.nn import Module
6
7
  import torch.nn.functional as F
7
8
 
9
+ from einops import rearrange, repeat, reduce, pack, unpack
10
+
8
11
  # taken from S5-pytorch repository
9
12
  # https://github.com/i404788/s5-pytorch/blob/74e2fdae00b915a62c914bf3615c0b8a4279eb84/s5/jax_compat.py#L51-L134
10
13
 
11
14
  # helper functions
12
15
 
16
+ def exists(v):
17
+ return v is not None
18
+
19
+ def default(*args):
20
+ for arg in args:
21
+ if exists(arg):
22
+ return arg
23
+ return None
24
+
13
25
  def pad_at_dim(t, pad, dim = -1, value = 0.):
14
26
  dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1)
15
27
  zeros = ((0, 0) * dims_from_right)
16
28
  return F.pad(t, (*zeros, *pad), value = value)
17
29
 
30
+ def pack_one_with_inverse(t, pattern):
31
+ packed, packed_shape = pack([t], pattern)
32
+
33
+ def inverse(out, inv_pattern = None):
34
+ inv_pattern = default(inv_pattern, pattern)
35
+ return unpack(out, packed_shape, inv_pattern)[0]
36
+
37
+ return packed, inverse
38
+
18
39
  # the operator that is needed
19
40
 
20
41
  @torch.jit.script
@@ -88,3 +109,69 @@ def _interleave(a, b):
88
109
  interleaved = torch.flatten(stacked, start_dim=1, end_dim=2)
89
110
 
90
111
  return interleaved[:, :output_axis_len]
112
+
113
+ # associative scan wrapper around naive and accelerated version
114
+
115
+ class AssocScan(Module):
116
+ def __init__(
117
+ self,
118
+ use_accelerated = False
119
+ ):
120
+ super().__init__()
121
+ self.use_accelerated = use_accelerated
122
+
123
+ def forward(
124
+ self,
125
+ gates,
126
+ inputs,
127
+ prev = None,
128
+ remove_prev = None
129
+ ):
130
+ remove_prev = default(remove_prev, exists(prev))
131
+
132
+ inputs, inverse_pack_weight_shape = pack_one_with_inverse(inputs, 'b n *')
133
+ gates, _ = pack_one_with_inverse(gates, 'b n *')
134
+
135
+ if exists(prev):
136
+ prev, _ = pack_one_with_inverse(prev, 'b *')
137
+
138
+ if exists(prev):
139
+ inputs, _ = pack([prev, inputs], 'b * d')
140
+ gates = pad_at_dim(gates, (1, 0), value = 1., dim = -2)
141
+
142
+ if not self.use_accelerated:
143
+ _, out = associative_scan(binary_operator, (gates, inputs))
144
+
145
+ if remove_prev:
146
+ out = out[:, 1:]
147
+
148
+ return inverse_pack_weight_shape(out)
149
+
150
+ from accelerated_scan.triton import scan as triton_scan
151
+ from accelerated_scan.warp import scan as warp_scan
152
+
153
+ scan = triton_scan if gates.is_cuda else warp_scan
154
+
155
+ def accelerate_scan_fn(gates, inputs):
156
+ gates = gates.expand_as(inputs)
157
+ gates, inputs = tuple(rearrange(t, 'b n d -> b d n') for t in (gates, inputs))
158
+
159
+ seq_len = gates.shape[-1]
160
+ next_power_two_seq_len = 2 ** max(5, int(math.ceil(math.log2(seq_len))))
161
+
162
+ gates = F.pad(gates, (0, next_power_two_seq_len - seq_len))
163
+ inputs = F.pad(inputs, (0, next_power_two_seq_len - seq_len))
164
+
165
+ outputs = scan(gates.contiguous(), inputs.contiguous())
166
+
167
+ outputs = outputs[..., :seq_len]
168
+ outputs = rearrange(outputs, 'b d n -> b n d')
169
+
170
+ return outputs
171
+
172
+ out = accelerate_scan_fn(gates, inputs)
173
+
174
+ if remove_prev:
175
+ out = out[:, 1:]
176
+
177
+ return inverse_pack_weight_shape(out)
@@ -8,16 +8,12 @@ from collections import namedtuple
8
8
  import torch
9
9
  from torch import nn, cat, tensor, Tensor
10
10
  import torch.nn.functional as F
11
- from torch.nn import Linear, Module, Parameter, ParameterList
11
+ from torch.nn import Linear, Module, Parameter, ParameterList, ParameterDict
12
12
  from torch.func import functional_call, vmap, grad
13
13
 
14
14
  from tensordict import TensorDict
15
15
 
16
- from titans_pytorch.associative_scan import (
17
- associative_scan,
18
- binary_operator,
19
- pad_at_dim
20
- )
16
+ from titans_pytorch.associative_scan import AssocScan
21
17
 
22
18
  from titans_pytorch.memory_models import(
23
19
  MemoryMLP
@@ -79,8 +75,8 @@ def safe_cat(inputs, dim = -2):
79
75
  def is_empty_tensor(t):
80
76
  return t.numel() == 0
81
77
 
82
- def dict_get_shape(td):
83
- return {k: v.shape for k, v in td.items()}
78
+ def dict_get_value_shapes(td):
79
+ return [v.shape for k, v in td.items()]
84
80
 
85
81
  def rearrange_dict_values(td, pattern, **kwargs):
86
82
  return td.apply(lambda t: rearrange(t, pattern, **kwargs))
@@ -97,6 +93,11 @@ def round_down_multiple(seq, mult):
97
93
  def round_up_multiple(seq, mult):
98
94
  return math.ceil(seq / mult) * mult
99
95
 
96
+ def pad_at_dim(t, pad, dim = -1, value = 0.):
97
+ dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1)
98
+ zeros = ((0, 0) * dims_from_right)
99
+ return F.pad(t, (*zeros, *pad), value = value)
100
+
100
101
  def pack_one_with_inverse(t, pattern):
101
102
  packed, packed_shape = pack([t], pattern)
102
103
 
@@ -197,72 +198,6 @@ class AttentionPool(Module):
197
198
 
198
199
  return reduce(x * attn, 'b n c d -> b n d', 'sum')
199
200
 
200
- # associative scan wrapper
201
-
202
- class AssocScan(Module):
203
- def __init__(
204
- self,
205
- use_accelerated = False
206
- ):
207
- super().__init__()
208
- self.use_accelerated = use_accelerated
209
-
210
- def forward(
211
- self,
212
- gates,
213
- inputs,
214
- prev = None,
215
- remove_prev = None
216
- ):
217
- remove_prev = default(remove_prev, exists(prev))
218
-
219
- inputs, inverse_pack_weight_shape = pack_one_with_inverse(inputs, 'b n *')
220
- gates, _ = pack_one_with_inverse(gates, 'b n *')
221
-
222
- if exists(prev):
223
- prev, _ = pack_one_with_inverse(prev, 'b *')
224
-
225
- if exists(prev):
226
- inputs, _ = pack([prev, inputs], 'b * d')
227
- gates = pad_at_dim(gates, (1, 0), value = 1., dim = -2)
228
-
229
- if not self.use_accelerated:
230
- _, out = associative_scan(binary_operator, (gates, inputs))
231
-
232
- if remove_prev:
233
- out = out[:, 1:]
234
-
235
- return inverse_pack_weight_shape(out)
236
-
237
- from accelerated_scan.triton import scan as triton_scan
238
- from accelerated_scan.warp import scan as warp_scan
239
-
240
- scan = triton_scan if gates.is_cuda else warp_scan
241
-
242
- def accelerate_scan_fn(gates, inputs):
243
- gates = gates.expand_as(inputs)
244
- gates, inputs = tuple(rearrange(t, 'b n d -> b d n') for t in (gates, inputs))
245
-
246
- seq_len = gates.shape[-1]
247
- next_power_two_seq_len = 2 ** max(5, int(math.ceil(math.log2(seq_len))))
248
-
249
- gates = F.pad(gates, (0, next_power_two_seq_len - seq_len))
250
- inputs = F.pad(inputs, (0, next_power_two_seq_len - seq_len))
251
-
252
- outputs = scan(gates.contiguous(), inputs.contiguous())
253
-
254
- outputs = outputs[..., :seq_len]
255
- outputs = rearrange(outputs, 'b d n -> b n d')
256
-
257
- return outputs
258
-
259
- out = accelerate_scan_fn(gates, inputs)
260
-
261
- if remove_prev:
262
- out = out[:, 1:]
263
-
264
- return inverse_pack_weight_shape(out)
265
-
266
201
  # main neural memory
267
202
 
268
203
  def default_adaptive_step_transform(adaptive_step, max_lr = 1e-2):
@@ -285,6 +220,7 @@ class NeuralMemory(Module):
285
220
  default_step_transform_max_lr = 1.,
286
221
  per_parameter_lr_modulation = False, # allow outer network to control learning rate per weight matrix of memory network
287
222
  max_mem_layer_modulation = 1., # max of 10.
223
+ per_head_learned_parameters = True,
288
224
  attn_pool_chunks = False,
289
225
  momentum = True,
290
226
  pre_rmsnorm = True,
@@ -370,9 +306,21 @@ class NeuralMemory(Module):
370
306
 
371
307
  self.memory_model = model
372
308
 
373
- self.num_memory_parameter_tensors = len(set(model.parameters()))
309
+ mem_model_params = dict(model.named_parameters())
310
+
311
+ self.num_memory_parameter_tensors = len(mem_model_params)
374
312
 
375
- self.init_weight_shape = dict_get_shape(dict(model.named_parameters()))
313
+ self.memory_model_parameter_names = [*mem_model_params.keys()]
314
+
315
+ memory_model_parameters = [*mem_model_params.values()]
316
+
317
+ if per_head_learned_parameters:
318
+ memory_model_parameters = [repeat(p, '... -> h ...', h = heads) for p in memory_model_parameters]
319
+
320
+ self.init_weight_shape = [p.shape for p in memory_model_parameters]
321
+
322
+ self.memory_model_parameters = ParameterList(memory_model_parameters)
323
+ self.per_head_learned_parameters = per_head_learned_parameters
376
324
 
377
325
  # the chunk size within the paper where adaptive step, momentum, weight decay are shared
378
326
 
@@ -488,21 +436,32 @@ class NeuralMemory(Module):
488
436
 
489
437
  self.register_buffer('zero', torch.tensor(0.), persistent = False)
490
438
 
439
+ @property
440
+ def memory_model_parameter_dict(self):
441
+ return TensorDict(dict(zip(self.memory_model_parameter_names, self.memory_model_parameters)))
442
+
491
443
  def init_weights(
492
444
  self,
493
445
  batch,
494
446
  ):
495
- weights = TensorDict(dict(self.memory_model.named_parameters()))
496
- weights = repeat_dict_values(weights, '... -> bh ...', bh = batch * self.heads)
447
+ if self.per_head_learned_parameters:
448
+ weights = repeat_dict_values(self.memory_model_parameter_dict, 'h ... -> (b h) ...', b = batch)
449
+ else:
450
+ weights = repeat_dict_values(self.memory_model_parameter_dict, '... -> bh ...', bh = batch * self.heads)
451
+
497
452
  return weights
498
453
 
499
454
  def init_momentum(
500
455
  self,
501
456
  batch,
502
457
  ):
503
- weights = TensorDict(dict(self.memory_model.named_parameters()))
504
- zeros = weights.clone().zero_()
505
- zeros = repeat_dict_values(zeros, '... -> bh ...', bh = batch * self.heads)
458
+ zeros = self.memory_model_parameter_dict.clone().zero_()
459
+
460
+ if self.per_head_learned_parameters:
461
+ zeros = repeat_dict_values(zeros, 'h ... -> (b h) ...', b = batch)
462
+ else:
463
+ zeros = repeat_dict_values(zeros, '... -> bh ...', bh = batch * self.heads)
464
+
506
465
  return zeros
507
466
 
508
467
  def store_memories(
@@ -694,7 +653,7 @@ class NeuralMemory(Module):
694
653
  ):
695
654
  chunk_size = self.retrieve_chunk_size
696
655
 
697
- weights_have_expanded_shape = dict_get_shape(weights) != self.init_weight_shape
656
+ weights_have_expanded_shape = dict_get_value_shapes(weights) != self.init_weight_shape
698
657
 
699
658
  batch, seq_len = seq.shape[:2]
700
659
 
@@ -10,7 +10,11 @@ from torch.utils.data import DataLoader, Dataset
10
10
 
11
11
  from adam_atan2_pytorch import AdoptAtan2
12
12
 
13
- from titans_pytorch import MemoryAsContextTransformer, MemoryMLP
13
+ from titans_pytorch import (
14
+ MemoryAsContextTransformer,
15
+ MemoryMLP,
16
+ MemoryAttention
17
+ )
14
18
 
15
19
  # constants
16
20
 
@@ -35,6 +39,7 @@ NEURAL_MEM_GATE_ATTN_OUTPUT = False
35
39
  NEURAL_MEM_MOMENTUM = True
36
40
  NEURAL_MEM_QK_NORM = True
37
41
  NEURAL_MEM_MAX_LR = 1e-1
42
+ USE_MEM_ATTENTION_MODEL = False
38
43
  WINDOW_SIZE = 32
39
44
  NEURAL_MEM_SEGMENT_LEN = 4 # set smaller for more granularity for learning rate / momentum etc
40
45
  NEURAL_MEM_BATCH_SIZE = 128 # set smaller to update the neural memory weights more often as it traverses the sequence
@@ -75,6 +80,18 @@ def decode_token(token):
75
80
  def decode_tokens(tokens):
76
81
  return ''.join(list(map(decode_token, tokens)))
77
82
 
83
+ # memory model
84
+
85
+ if USE_MEM_ATTENTION_MODEL:
86
+ neural_memory_model = MemoryAttention(
87
+ dim = 64
88
+ )
89
+ else:
90
+ neural_memory_model = MemoryMLP(
91
+ dim = 64,
92
+ depth = NEURAL_MEMORY_DEPTH
93
+ )
94
+
78
95
  # instantiate memory-as-context transformer
79
96
 
80
97
  model = MemoryAsContextTransformer(
@@ -91,10 +108,7 @@ model = MemoryAsContextTransformer(
91
108
  neural_mem_weight_residual = NEURAL_MEM_WEIGHT_RESIDUAL,
92
109
  use_flex_attn = USE_FLEX_ATTN,
93
110
  sliding_window_attn = SLIDING_WINDOWS,
94
- neural_memory_model = MemoryMLP(
95
- dim = 64,
96
- depth = NEURAL_MEMORY_DEPTH
97
- ),
111
+ neural_memory_model = neural_memory_model,
98
112
  neural_memory_kwargs = dict(
99
113
  dim_head = 64,
100
114
  heads = 4,
File without changes
File without changes
File without changes
File without changes