x-transformers 2.2.3__py3-none-any.whl → 2.2.5__py3-none-any.whl
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.
- x_transformers/entropy_based_tokenizer.py +19 -9
- {x_transformers-2.2.3.dist-info → x_transformers-2.2.5.dist-info}/METADATA +1 -1
- {x_transformers-2.2.3.dist-info → x_transformers-2.2.5.dist-info}/RECORD +5 -5
- {x_transformers-2.2.3.dist-info → x_transformers-2.2.5.dist-info}/WHEEL +0 -0
- {x_transformers-2.2.3.dist-info → x_transformers-2.2.5.dist-info}/licenses/LICENSE +0 -0
@@ -6,10 +6,14 @@ import torch.nn.functional as F
|
|
6
6
|
from torch.nn import Module
|
7
7
|
from torch.nn.utils.rnn import pad_sequence
|
8
8
|
|
9
|
-
from x_transformers.x_transformers import
|
9
|
+
from x_transformers.x_transformers import (
|
10
|
+
Decoder,
|
11
|
+
TransformerWrapper,
|
12
|
+
calc_entropy
|
13
|
+
)
|
10
14
|
|
11
15
|
import einx
|
12
|
-
from einops import repeat, rearrange
|
16
|
+
from einops import repeat, rearrange, pack, unpack
|
13
17
|
|
14
18
|
# helper functions
|
15
19
|
|
@@ -27,22 +31,23 @@ def default(v, d):
|
|
27
31
|
class EntropyBasedTokenizer(Module):
|
28
32
|
def __init__(
|
29
33
|
self,
|
30
|
-
decoder:
|
34
|
+
decoder: Module,
|
31
35
|
entropy_threshold: float
|
32
36
|
):
|
33
37
|
super().__init__()
|
34
|
-
assert isinstance(decoder.attn_layers, Decoder)
|
35
|
-
|
36
38
|
self.decoder = decoder
|
37
39
|
self.entropy_threshold = entropy_threshold
|
38
40
|
|
39
41
|
@torch.no_grad()
|
40
42
|
def forward(
|
41
43
|
self,
|
42
|
-
seq,
|
43
|
-
lens = None,
|
44
|
+
seq, # Float['b n'] | Float['n']
|
45
|
+
lens = None, # Int['b']
|
44
46
|
return_segmented_seq = False
|
45
47
|
):
|
48
|
+
no_batch_dim = seq.ndim == 1
|
49
|
+
seq, maybe_batch_ps = pack((seq,), '* n')
|
50
|
+
|
46
51
|
self.decoder.eval()
|
47
52
|
|
48
53
|
is_var_length = exists(lens)
|
@@ -52,9 +57,9 @@ class EntropyBasedTokenizer(Module):
|
|
52
57
|
|
53
58
|
# forward through a small trained decoder and get the entropies of the logits
|
54
59
|
|
55
|
-
|
60
|
+
logits = self.decoder(seq)
|
56
61
|
|
57
|
-
entropies =
|
62
|
+
entropies = calc_entropy(logits)
|
58
63
|
|
59
64
|
# get length mask for boundaries
|
60
65
|
|
@@ -106,6 +111,8 @@ class EntropyBasedTokenizer(Module):
|
|
106
111
|
# early return
|
107
112
|
|
108
113
|
if not return_segmented_seq:
|
114
|
+
token_lengths, = unpack(token_lengths, maybe_batch_ps, '* num_tokens')
|
115
|
+
|
109
116
|
return token_lengths
|
110
117
|
|
111
118
|
# segment the sequence based on the token lengths
|
@@ -123,4 +130,7 @@ class EntropyBasedTokenizer(Module):
|
|
123
130
|
splitted_seq = one_seq.split(one_token_length.tolist())
|
124
131
|
segmented_seq.append(splitted_seq)
|
125
132
|
|
133
|
+
if no_batch_dim:
|
134
|
+
segmented_seq = segmented_seq[0]
|
135
|
+
|
126
136
|
return segmented_seq
|
@@ -4,14 +4,14 @@ x_transformers/autoregressive_wrapper.py,sha256=reLCno9Z9pchVU79tBF8OMo21LwSZ67K
|
|
4
4
|
x_transformers/belief_state_wrapper.py,sha256=YLUMk6t2MhFBEw5lHDDHJHcoCxTIkHvxTNY__GGZEKU,13374
|
5
5
|
x_transformers/continuous.py,sha256=p0sCAiH1na236ygwgL1Yyhu36eZBf9cZvoW1JyP_fFE,7073
|
6
6
|
x_transformers/dpo.py,sha256=xt4OuOWhU8pN3OKN2LZAaC2NC8iiEnchqqcrPWVqf0o,3521
|
7
|
-
x_transformers/entropy_based_tokenizer.py,sha256=
|
7
|
+
x_transformers/entropy_based_tokenizer.py,sha256=Dr7Hvb0WbdhCeD3_61jAQeONr6IWFFSCRrqBfn0d46Y,3772
|
8
8
|
x_transformers/multi_input.py,sha256=tCh-fTJDj2ib4SMGtsa-AM8MxKzJAQSwqAXOu3HU2mg,9252
|
9
9
|
x_transformers/neo_mlp.py,sha256=XCNnnop9WLarcxap1kGuYc1x8GHvwkZiDRnXOxSl3Po,3452
|
10
10
|
x_transformers/nonautoregressive_wrapper.py,sha256=2NU58hYMgn-4Jzg3mie-mXb0XH_dCN7fjlzd3K1rLUY,10510
|
11
11
|
x_transformers/x_transformers.py,sha256=Fl2CuAKTxJDOQvqwQo2FK8eO2s1iNLO-P1PP2Yw64rQ,111826
|
12
12
|
x_transformers/xl_autoregressive_wrapper.py,sha256=CvZMJ6A6PA-Y_bQAhnORwjJBSl6Vjq2IdW5KTdk8NI8,4195
|
13
13
|
x_transformers/xval.py,sha256=7S00kCuab4tWQa-vf-z-XfzADjVj48MoFIr7VSIvttg,8575
|
14
|
-
x_transformers-2.2.
|
15
|
-
x_transformers-2.2.
|
16
|
-
x_transformers-2.2.
|
17
|
-
x_transformers-2.2.
|
14
|
+
x_transformers-2.2.5.dist-info/METADATA,sha256=5hJR7TPpRznsPZrYAHsbk3Eq9E9XKrAuwybmc3r6B1s,88686
|
15
|
+
x_transformers-2.2.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
16
|
+
x_transformers-2.2.5.dist-info/licenses/LICENSE,sha256=As9u198X-U-vph5noInuUfqsAG2zX_oXPHDmdjwlPPY,1066
|
17
|
+
x_transformers-2.2.5.dist-info/RECORD,,
|
File without changes
|
File without changes
|