x-transformers 1.44.2__tar.gz → 1.44.4__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {x_transformers-1.44.2/x_transformers.egg-info → x_transformers-1.44.4}/PKG-INFO +1 -1
- {x_transformers-1.44.2 → x_transformers-1.44.4}/setup.py +1 -1
- {x_transformers-1.44.2 → x_transformers-1.44.4}/tests/test_x_transformers.py +4 -1
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/x_transformers.py +43 -2
- {x_transformers-1.44.2 → x_transformers-1.44.4/x_transformers.egg-info}/PKG-INFO +1 -1
- {x_transformers-1.44.2 → x_transformers-1.44.4}/LICENSE +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/README.md +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/setup.cfg +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/__init__.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/attend.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/autoregressive_wrapper.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/continuous.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/dpo.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/multi_input.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/neo_mlp.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/nonautoregressive_wrapper.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/xl_autoregressive_wrapper.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers/xval.py +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers.egg-info/SOURCES.txt +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers.egg-info/dependency_links.txt +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers.egg-info/requires.txt +0 -0
- {x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers.egg-info/top_level.txt +0 -0
@@ -614,7 +614,8 @@ def test_hyper_connections(tanh):
|
|
614
614
|
|
615
615
|
model(x)
|
616
616
|
|
617
|
-
|
617
|
+
@pytest.mark.parametrize('hybrid_axial_dim', (1, 4))
|
618
|
+
def test_hybrid(hybrid_axial_dim):
|
618
619
|
from torch.nn import GRU
|
619
620
|
|
620
621
|
dec = TransformerWrapper(
|
@@ -625,6 +626,7 @@ def test_hybrid():
|
|
625
626
|
depth = 6,
|
626
627
|
heads = 8,
|
627
628
|
attn_dim_head = 64,
|
629
|
+
attn_hybrid_fold_axial_dim = hybrid_axial_dim,
|
628
630
|
attn_hybrid_module = GRU(128, 64 * 8, batch_first = True)
|
629
631
|
)
|
630
632
|
)
|
@@ -641,6 +643,7 @@ def test_hybrid():
|
|
641
643
|
depth = 6,
|
642
644
|
heads = 8,
|
643
645
|
attn_dim_head = 64,
|
646
|
+
attn_hybrid_fold_axial_dim = hybrid_axial_dim,
|
644
647
|
attn_hybrid_module = GRU(128, 64 * 4, batch_first = True, bidirectional = True)
|
645
648
|
)
|
646
649
|
)
|
@@ -10,7 +10,7 @@ import torch
|
|
10
10
|
from torch.amp import autocast
|
11
11
|
import torch.nn.functional as F
|
12
12
|
from torch import nn, einsum, Tensor
|
13
|
-
from torch.utils._pytree import tree_flatten
|
13
|
+
from torch.utils._pytree import tree_flatten, tree_unflatten
|
14
14
|
from torch.nn import Module, ModuleList, ModuleDict
|
15
15
|
|
16
16
|
from functools import partial, wraps
|
@@ -966,6 +966,42 @@ class ShiftTokens(Module):
|
|
966
966
|
x = torch.cat((*segments_to_shift, *rest), dim = -1)
|
967
967
|
return self.fn(x, **kwargs)
|
968
968
|
|
969
|
+
class FoldAxially(Module):
|
970
|
+
def __init__(
|
971
|
+
self,
|
972
|
+
axial_dim,
|
973
|
+
fn: Module
|
974
|
+
):
|
975
|
+
super().__init__()
|
976
|
+
self.fn = fn
|
977
|
+
self.axial_dim = axial_dim # will fold the sequence as rearrange("b (n axial_dim) ... -> (b axial_dim) n ...")
|
978
|
+
|
979
|
+
def forward(
|
980
|
+
self,
|
981
|
+
x,
|
982
|
+
**kwargs
|
983
|
+
):
|
984
|
+
if self.axial_dim == 1:
|
985
|
+
return self.fn(x, **kwargs)
|
986
|
+
|
987
|
+
seq_len, axial_dim = x.shape[1], self.axial_dim
|
988
|
+
|
989
|
+
next_multiple = math.ceil(seq_len / axial_dim) * axial_dim
|
990
|
+
x = pad_at_dim(x, (0, next_multiple - seq_len), dim = 1)
|
991
|
+
|
992
|
+
x = rearrange(x, 'b (n axial_dim) ... -> (b axial_dim) n ...', axial_dim = axial_dim)
|
993
|
+
|
994
|
+
out = self.fn(x, **kwargs)
|
995
|
+
|
996
|
+
(out, *rest_out), tree_spec = tree_flatten(out)
|
997
|
+
|
998
|
+
out = rearrange(out, '(b axial_dim) n ... -> b (n axial_dim) ...', axial_dim = axial_dim)
|
999
|
+
|
1000
|
+
out = out[:, :seq_len]
|
1001
|
+
out = tree_unflatten((out, *rest_out), tree_spec)
|
1002
|
+
|
1003
|
+
return out
|
1004
|
+
|
969
1005
|
# post branch operator
|
970
1006
|
|
971
1007
|
class LayerScale(Module):
|
@@ -1140,6 +1176,7 @@ class Attention(Module):
|
|
1140
1176
|
custom_attn_fn: Callable | None = None,
|
1141
1177
|
hybrid_module: Module | None = None,
|
1142
1178
|
hybrid_mask_kwarg: str | None = None,
|
1179
|
+
hybrid_fold_axial_dim: int | None = None,
|
1143
1180
|
one_kv_head = False,
|
1144
1181
|
kv_heads = None,
|
1145
1182
|
shared_kv = False,
|
@@ -1341,8 +1378,12 @@ class Attention(Module):
|
|
1341
1378
|
|
1342
1379
|
# hybrid module, in same vein as hymba https://www.arxiv.org/abs/2411.13676
|
1343
1380
|
|
1344
|
-
|
1381
|
+
hybrid_module = maybe(deepcopy)(hybrid_module)
|
1382
|
+
|
1383
|
+
if exists(hybrid_module) and exists(hybrid_fold_axial_dim):
|
1384
|
+
hybrid_module = FoldAxially(axial_dim = hybrid_fold_axial_dim, fn = hybrid_module)
|
1345
1385
|
|
1386
|
+
self.hybrid_module = hybrid_module
|
1346
1387
|
self.hybrid_mask_kwarg = hybrid_mask_kwarg # for bidirectional, can forward `mask` into the hybrid module and let it handle variable lengths
|
1347
1388
|
|
1348
1389
|
# output dimension by default same as input, but can be overridden
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{x_transformers-1.44.2 → x_transformers-1.44.4}/x_transformers.egg-info/dependency_links.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|