Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
import packaging.version as pv
import torch
from transformers import __version__ as transformers_version
from transformers.cache_utils import DynamicCache, EncoderDecoderCache
 
 
def get_dynamic_cache_key_value(
    cache: DynamicCache,
) -> tuple[list[torch.Tensor], list[torch.Tensor]]:
    if hasattr(cache, "key_cache"):
        return cache.key_cache, cache.value_cache
    return [layer.keys for layer in cache.layers], [layer.values for layer in cache.layers]
 
 
def is_cache_dynamic_registered(fast: bool = False) -> bool:
    """
    Tells class :class:`DynamicCache` can be
    serialized and deserialized. Only then, :func:`torch.export.export`
    can export a model.
 
    :param fast: if True, do not check the serialization is ok as well
    :return: result
    """
    if fast:
        return DynamicCache in torch.utils._pytree.SUPPORTED_NODES
    bsize, nheads, slen, dim = 2, 4, 3, 7
    cache = make_dynamic_cache(
        [
            (
                torch.randn(bsize, nheads, slen, dim),
                torch.randn(bsize, nheads, slen, dim),
            )
            for i in range(2)
        ]
    )
    values, spec = torch.utils._pytree.tree_flatten(cache)
    cache2 = torch.utils._pytree.tree_unflatten(values, spec)
    cache2_keys, _ = get_dynamic_cache_key_value(cache2)
    _, cache_values = get_dynamic_cache_key_value(cache)
    return len(cache2_keys) == len(cache_values)
 
 
if pv.Version(transformers_version) > pv.Version("4.49.99999"):
 
    def make_dynamic_cache(
        key_value_pairs: list[tuple[torch.Tensor, torch.Tensor]],
    ) -> DynamicCache:
        """
        Creates an instance of :class:`DynamicCache`.
        This version is valid for ``transformers >= 4.50``.
 
        :param key_value_pairs: list of pairs of (key, values)
        :return: :class:`DynamicCache`
        """
        return DynamicCache(key_value_pairs)
 
else:
 
    def make_dynamic_cache(
        key_value_pairs: list[tuple[torch.Tensor, torch.Tensor]],
    ) -> DynamicCache:
        """
        Creates an instance of :class:`DynamicCache`.
        This version is valid for ``transformers < 4.50``.
 
        :param key_value_pairs: list of pairs of (key, values)
        :return: :class:`DynamicCache`
        """
        cache = DynamicCache(len(key_value_pairs))
        for i, (key, value) in enumerate(key_value_pairs):
            cache.update(key, value, i)
        return cache
 
 
def make_encoder_decoder_cache(
    self_attention_cache: DynamicCache,
    cross_attention_cache: DynamicCache,
) -> EncoderDecoderCache:
    """
    Creates an EncoderDecoderCache.
    """
    return EncoderDecoderCache(self_attention_cache, cross_attention_cache)