Исходный код СУБД 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
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
from typing import Any
 
import torch
from transformers.cache_utils import DynamicCache
 
try:
    from transformers.cache_utils import MambaCache
except ImportError:
    MambaCache = None
 
from .cache_helper import get_dynamic_cache_key_value, make_dynamic_cache
 
############
# MambaCache
############
 
 
# self.conv_states: torch.Tensor = torch.zeros(
#     config.num_hidden_layers,
#     self.max_batch_size,
#     self.intermediate_size,
#     self.conv_kernel_size,
#     device=device,
#     dtype=dtype,
# )
# self.ssm_states: torch.Tensor = torch.zeros(
#     config.num_hidden_layers,
#     self.max_batch_size,
#     self.intermediate_size,
#     self.ssm_state_size,
#     device=device,
#     dtype=dtype,
# )
def flatten_mamba_cache(
    mamba_cache: MambaCache,
) -> tuple[list[Any], torch.utils._pytree.Context]:
    """Serializes a :class:`MambaCache` with python objects."""
    flat = [
        (k, getattr(mamba_cache, k))
        for k in [
            # "max_batch_size",  # new in transformers==4.47
            # "intermediate_size",
            # "ssm_state_size",
            # "conv_kernel_size",
            "conv_states",
            "ssm_states",
        ]
        if hasattr(mamba_cache, k)
    ]
    return [f[1] for f in flat], [f[0] for f in flat]
 
 
def unflatten_mamba_cache(
    values: list[Any],
    context: torch.utils._pytree.Context,
    output_type=None,
) -> MambaCache:
    """Restores a :class:`MambaCache` from python objects."""
    conv_states, ssm_states = values
 
    class _config:
        def __init__(self):
            if isinstance(conv_states, list):
                self.intermediate_size = conv_states[0].shape[1]
                self.state_size = ssm_states[0].shape[2]
                self.conv_kernel = conv_states[0].shape[2]
                self.num_hidden_layers = len(conv_states)
            else:
                self.intermediate_size = conv_states.shape[2]
                self.state_size = ssm_states.shape[3]
                self.conv_kernel = conv_states.shape[3]
                self.num_hidden_layers = conv_states.shape[0]
 
    cache = MambaCache(
        _config(),
        max_batch_size=1,
        dtype=values[-1][0].dtype,
        device="cpu" if values[-1][0].get_device() < 0 else "cuda",
    )
    values = dict(zip(context, values, strict=False))
    for k, v in values.items():
        setattr(cache, k, v)
    return cache
 
 
def flatten_with_keys_mamba_cache(
    d: dict[Any, Any],
) -> tuple[
    list[tuple[torch.utils._pytree.KeyEntry, Any]],
    torch.utils._pytree.Context,
]:
    """Serializes a :class:`MambaCache` with python objects."""
    values, context = flatten_mamba_cache(d)
    return [(torch.utils._pytree.MappingKey(k), v) for k, v in zip(context, values, strict=False)], context
 
 
##############
# DynamicCache
##############
 
 
def flatten_dynamic_cache(
    dynamic_cache: DynamicCache,
) -> tuple[list[Any], torch.utils._pytree.Context]:
    """Serializes a :class:`DynamicCache` with python objects."""
    key_cache, value_cache = get_dynamic_cache_key_value(dynamic_cache)
    return [key_cache, value_cache], ["key_cache", "value_cache"]
 
 
def flatten_with_keys_dynamic_cache(
    d: dict[Any, Any],
) -> tuple[
    list[tuple[torch.utils._pytree.KeyEntry, Any]],
    torch.utils._pytree.Context,
]:
    """Serializes a :class:`DynamicCache` with python objects."""
    values, context = flatten_dynamic_cache(d)
    return [(torch.utils._pytree.MappingKey(k), v) for k, v in zip(context, values, strict=False)], context
 
 
def unflatten_dynamic_cache(
    values: list[Any],
    context: torch.utils._pytree.Context,
    output_type=None,
) -> DynamicCache:
    """Restores a :class:`DynamicCache` from python objects."""
    values = dict(zip(context, values, strict=False))
    return make_dynamic_cache(list(zip(values["key_cache"], values["value_cache"], strict=False)))