Upload 2 files
Browse files- configuration_kanllama.py +192 -0
- modeling_kanllama.py +347 -0
configuration_kanllama.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
"""LLaMA model configuration"""
|
21 |
+
|
22 |
+
from transformers.configuration_utils import PretrainedConfig
|
23 |
+
from transformers.utils import logging
|
24 |
+
|
25 |
+
|
26 |
+
logger = logging.get_logger(__name__)
|
27 |
+
|
28 |
+
|
29 |
+
class KANLlamaConfig(PretrainedConfig):
|
30 |
+
r"""
|
31 |
+
This is the configuration class to store the configuration of a [`LlamaModel`]. It is used to instantiate an LLaMA
|
32 |
+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
|
33 |
+
defaults will yield a similar configuration to that of the LLaMA-7B.
|
34 |
+
|
35 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
36 |
+
documentation from [`PretrainedConfig`] for more information.
|
37 |
+
|
38 |
+
|
39 |
+
Args:
|
40 |
+
vocab_size (`int`, *optional*, defaults to 32000):
|
41 |
+
Vocabulary size of the LLaMA model. Defines the number of different tokens that can be represented by the
|
42 |
+
`inputs_ids` passed when calling [`LlamaModel`]
|
43 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
44 |
+
Dimension of the hidden representations.
|
45 |
+
intermediate_size (`int`, *optional*, defaults to 11008):
|
46 |
+
Dimension of the MLP representations.
|
47 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
48 |
+
Number of hidden layers in the Transformer decoder.
|
49 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
50 |
+
Number of attention heads for each attention layer in the Transformer decoder.
|
51 |
+
num_key_value_heads (`int`, *optional*):
|
52 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
53 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
54 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
55 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
56 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
57 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
|
58 |
+
`num_attention_heads`.
|
59 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
60 |
+
The non-linear activation function (function or string) in the decoder.
|
61 |
+
max_position_embeddings (`int`, *optional*, defaults to 2048):
|
62 |
+
The maximum sequence length that this model might ever be used with. Llama 1 supports up to 2048 tokens,
|
63 |
+
Llama 2 up to 4096, CodeLlama up to 16384.
|
64 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
65 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
66 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
67 |
+
The epsilon used by the rms normalization layers.
|
68 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
69 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
70 |
+
relevant if `config.is_decoder=True`.
|
71 |
+
pad_token_id (`int`, *optional*):
|
72 |
+
Padding token id.
|
73 |
+
bos_token_id (`int`, *optional*, defaults to 1):
|
74 |
+
Beginning of stream token id.
|
75 |
+
eos_token_id (`int`, *optional*, defaults to 2):
|
76 |
+
End of stream token id.
|
77 |
+
pretraining_tp (`int`, *optional*, defaults to 1):
|
78 |
+
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
|
79 |
+
document](https://huggingface.co/docs/transformers/main/perf_train_gpu_many#tensor-parallelism) to understand more about it. This value is
|
80 |
+
necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
|
81 |
+
issue](https://github.com/pytorch/pytorch/issues/76232).
|
82 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
83 |
+
Whether to tie weight embeddings
|
84 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
85 |
+
The base period of the RoPE embeddings.
|
86 |
+
rope_scaling (`Dict`, *optional*):
|
87 |
+
Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
|
88 |
+
strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is
|
89 |
+
`{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
|
90 |
+
`max_position_embeddings` to the expected new maximum. See the following thread for more information on how
|
91 |
+
these scaling strategies behave:
|
92 |
+
https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
|
93 |
+
experimental feature, subject to breaking API changes in future versions.
|
94 |
+
attention_bias (`bool`, *optional*, defaults to `False`):
|
95 |
+
Whether to use a bias in the query, key, value and output projection layers during self-attention.
|
96 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
97 |
+
The dropout ratio for the attention probabilities.
|
98 |
+
mlp_bias (`bool`, *optional*, defaults to `False`):
|
99 |
+
Whether to use a bias in up_proj, down_proj and gate_proj layers in the MLP layers.
|
100 |
+
|
101 |
+
```python
|
102 |
+
>>> from transformers import LlamaModel, LlamaConfig
|
103 |
+
|
104 |
+
>>> # Initializing a LLaMA llama-7b style configuration
|
105 |
+
>>> configuration = LlamaConfig()
|
106 |
+
|
107 |
+
>>> # Initializing a model from the llama-7b style configuration
|
108 |
+
>>> model = LlamaModel(configuration)
|
109 |
+
|
110 |
+
>>> # Accessing the model configuration
|
111 |
+
>>> configuration = model.config
|
112 |
+
```"""
|
113 |
+
|
114 |
+
model_type = "llama"
|
115 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
116 |
+
|
117 |
+
def __init__(
|
118 |
+
self,
|
119 |
+
vocab_size=32000,
|
120 |
+
hidden_size=4096,
|
121 |
+
intermediate_size=11008,
|
122 |
+
num_hidden_layers=32,
|
123 |
+
num_attention_heads=32,
|
124 |
+
num_key_value_heads=None,
|
125 |
+
hidden_act="silu",
|
126 |
+
max_position_embeddings=2048,
|
127 |
+
initializer_range=0.02,
|
128 |
+
rms_norm_eps=1e-6,
|
129 |
+
use_cache=True,
|
130 |
+
pad_token_id=None,
|
131 |
+
bos_token_id=1,
|
132 |
+
eos_token_id=2,
|
133 |
+
pretraining_tp=1,
|
134 |
+
tie_word_embeddings=False,
|
135 |
+
rope_theta=10000.0,
|
136 |
+
rope_scaling=None,
|
137 |
+
attention_bias=False,
|
138 |
+
attention_dropout=0.0,
|
139 |
+
mlp_bias=False,
|
140 |
+
**kwargs,
|
141 |
+
):
|
142 |
+
self.vocab_size = vocab_size
|
143 |
+
self.max_position_embeddings = max_position_embeddings
|
144 |
+
self.hidden_size = hidden_size
|
145 |
+
self.intermediate_size = intermediate_size
|
146 |
+
self.num_hidden_layers = num_hidden_layers
|
147 |
+
self.num_attention_heads = num_attention_heads
|
148 |
+
|
149 |
+
# for backward compatibility
|
150 |
+
if num_key_value_heads is None:
|
151 |
+
num_key_value_heads = num_attention_heads
|
152 |
+
|
153 |
+
self.num_key_value_heads = num_key_value_heads
|
154 |
+
self.hidden_act = hidden_act
|
155 |
+
self.initializer_range = initializer_range
|
156 |
+
self.rms_norm_eps = rms_norm_eps
|
157 |
+
self.pretraining_tp = pretraining_tp
|
158 |
+
self.use_cache = use_cache
|
159 |
+
self.rope_theta = rope_theta
|
160 |
+
self.rope_scaling = rope_scaling
|
161 |
+
self._rope_scaling_validation()
|
162 |
+
self.attention_bias = attention_bias
|
163 |
+
self.attention_dropout = attention_dropout
|
164 |
+
self.mlp_bias = mlp_bias
|
165 |
+
|
166 |
+
super().__init__(
|
167 |
+
pad_token_id=pad_token_id,
|
168 |
+
bos_token_id=bos_token_id,
|
169 |
+
eos_token_id=eos_token_id,
|
170 |
+
tie_word_embeddings=tie_word_embeddings,
|
171 |
+
**kwargs,
|
172 |
+
)
|
173 |
+
|
174 |
+
def _rope_scaling_validation(self):
|
175 |
+
"""
|
176 |
+
Validate the `rope_scaling` configuration.
|
177 |
+
"""
|
178 |
+
if self.rope_scaling is None:
|
179 |
+
return
|
180 |
+
|
181 |
+
if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
|
182 |
+
raise ValueError(
|
183 |
+
"`rope_scaling` must be a dictionary with two fields, `type` and `factor`, " f"got {self.rope_scaling}"
|
184 |
+
)
|
185 |
+
rope_scaling_type = self.rope_scaling.get("type", None)
|
186 |
+
rope_scaling_factor = self.rope_scaling.get("factor", None)
|
187 |
+
if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
|
188 |
+
raise ValueError(
|
189 |
+
f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
|
190 |
+
)
|
191 |
+
if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
|
192 |
+
raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
|
modeling_kanllama.py
ADDED
@@ -0,0 +1,347 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn.functional as F
|
3 |
+
import math
|
4 |
+
|
5 |
+
from .configuration_kanllama import KANLlamaConfig
|
6 |
+
|
7 |
+
######
|
8 |
+
# KAN and KANLinear are take from efficient KAN
|
9 |
+
# https://github.com/Blealtan/efficient-kan
|
10 |
+
######
|
11 |
+
|
12 |
+
class KANLinear(torch.nn.Module):
|
13 |
+
def __init__(
|
14 |
+
self,
|
15 |
+
in_features,
|
16 |
+
out_features,
|
17 |
+
grid_size=5,
|
18 |
+
spline_order=3,
|
19 |
+
scale_noise=0.1,
|
20 |
+
scale_base=1.0,
|
21 |
+
scale_spline=1.0,
|
22 |
+
enable_standalone_scale_spline=True,
|
23 |
+
base_activation=torch.nn.SiLU,
|
24 |
+
grid_eps=0.02,
|
25 |
+
grid_range=[-1, 1],
|
26 |
+
):
|
27 |
+
super(KANLinear, self).__init__()
|
28 |
+
self.in_features = in_features
|
29 |
+
self.out_features = out_features
|
30 |
+
self.grid_size = grid_size
|
31 |
+
self.spline_order = spline_order
|
32 |
+
|
33 |
+
h = (grid_range[1] - grid_range[0]) / grid_size
|
34 |
+
grid = (
|
35 |
+
(
|
36 |
+
torch.arange(-spline_order, grid_size + spline_order + 1) * h
|
37 |
+
+ grid_range[0]
|
38 |
+
)
|
39 |
+
.expand(in_features, -1)
|
40 |
+
.contiguous()
|
41 |
+
)
|
42 |
+
self.register_buffer("grid", grid)
|
43 |
+
|
44 |
+
self.base_weight = torch.nn.Parameter(torch.Tensor(out_features, in_features))
|
45 |
+
self.spline_weight = torch.nn.Parameter(
|
46 |
+
torch.Tensor(out_features, in_features, grid_size + spline_order)
|
47 |
+
)
|
48 |
+
if enable_standalone_scale_spline:
|
49 |
+
self.spline_scaler = torch.nn.Parameter(
|
50 |
+
torch.Tensor(out_features, in_features)
|
51 |
+
)
|
52 |
+
|
53 |
+
self.scale_noise = scale_noise
|
54 |
+
self.scale_base = scale_base
|
55 |
+
self.scale_spline = scale_spline
|
56 |
+
self.enable_standalone_scale_spline = enable_standalone_scale_spline
|
57 |
+
self.base_activation = base_activation()
|
58 |
+
self.grid_eps = grid_eps
|
59 |
+
|
60 |
+
self.reset_parameters()
|
61 |
+
|
62 |
+
def reset_parameters(self):
|
63 |
+
torch.nn.init.kaiming_uniform_(self.base_weight, a=math.sqrt(5) * self.scale_base)
|
64 |
+
with torch.no_grad():
|
65 |
+
noise = (
|
66 |
+
(
|
67 |
+
torch.rand(self.grid_size + 1, self.in_features, self.out_features)
|
68 |
+
- 1 / 2
|
69 |
+
)
|
70 |
+
* self.scale_noise
|
71 |
+
/ self.grid_size
|
72 |
+
)
|
73 |
+
self.spline_weight.data.copy_(
|
74 |
+
(self.scale_spline if not self.enable_standalone_scale_spline else 1.0)
|
75 |
+
* self.curve2coeff(
|
76 |
+
self.grid.T[self.spline_order : -self.spline_order],
|
77 |
+
noise,
|
78 |
+
)
|
79 |
+
)
|
80 |
+
if self.enable_standalone_scale_spline:
|
81 |
+
# torch.nn.init.constant_(self.spline_scaler, self.scale_spline)
|
82 |
+
torch.nn.init.kaiming_uniform_(self.spline_scaler, a=math.sqrt(5) * self.scale_spline)
|
83 |
+
|
84 |
+
def b_splines(self, x: torch.Tensor):
|
85 |
+
"""
|
86 |
+
Compute the B-spline bases for the given input tensor.
|
87 |
+
|
88 |
+
Args:
|
89 |
+
x (torch.Tensor): Input tensor of shape (batch_size, in_features).
|
90 |
+
|
91 |
+
Returns:
|
92 |
+
torch.Tensor: B-spline bases tensor of shape (batch_size, in_features, grid_size + spline_order).
|
93 |
+
"""
|
94 |
+
assert x.dim() == 2 and x.size(1) == self.in_features
|
95 |
+
|
96 |
+
grid: torch.Tensor = (
|
97 |
+
self.grid
|
98 |
+
) # (in_features, grid_size + 2 * spline_order + 1)
|
99 |
+
x = x.unsqueeze(-1)
|
100 |
+
bases = ((x >= grid[:, :-1]) & (x < grid[:, 1:])).to(x.dtype)
|
101 |
+
for k in range(1, self.spline_order + 1):
|
102 |
+
bases = (
|
103 |
+
(x - grid[:, : -(k + 1)])
|
104 |
+
/ (grid[:, k:-1] - grid[:, : -(k + 1)])
|
105 |
+
* bases[:, :, :-1]
|
106 |
+
) + (
|
107 |
+
(grid[:, k + 1 :] - x)
|
108 |
+
/ (grid[:, k + 1 :] - grid[:, 1:(-k)])
|
109 |
+
* bases[:, :, 1:]
|
110 |
+
)
|
111 |
+
|
112 |
+
assert bases.size() == (
|
113 |
+
x.size(0),
|
114 |
+
self.in_features,
|
115 |
+
self.grid_size + self.spline_order,
|
116 |
+
)
|
117 |
+
return bases.contiguous()
|
118 |
+
|
119 |
+
def curve2coeff(self, x: torch.Tensor, y: torch.Tensor):
|
120 |
+
"""
|
121 |
+
Compute the coefficients of the curve that interpolates the given points.
|
122 |
+
|
123 |
+
Args:
|
124 |
+
x (torch.Tensor): Input tensor of shape (batch_size, in_features).
|
125 |
+
y (torch.Tensor): Output tensor of shape (batch_size, in_features, out_features).
|
126 |
+
|
127 |
+
Returns:
|
128 |
+
torch.Tensor: Coefficients tensor of shape (out_features, in_features, grid_size + spline_order).
|
129 |
+
"""
|
130 |
+
assert x.dim() == 2 and x.size(1) == self.in_features
|
131 |
+
assert y.size() == (x.size(0), self.in_features, self.out_features)
|
132 |
+
|
133 |
+
A = self.b_splines(x).transpose(
|
134 |
+
0, 1
|
135 |
+
) # (in_features, batch_size, grid_size + spline_order)
|
136 |
+
B = y.transpose(0, 1) # (in_features, batch_size, out_features)
|
137 |
+
solution = torch.linalg.lstsq(
|
138 |
+
A, B
|
139 |
+
).solution # (in_features, grid_size + spline_order, out_features)
|
140 |
+
result = solution.permute(
|
141 |
+
2, 0, 1
|
142 |
+
) # (out_features, in_features, grid_size + spline_order)
|
143 |
+
|
144 |
+
assert result.size() == (
|
145 |
+
self.out_features,
|
146 |
+
self.in_features,
|
147 |
+
self.grid_size + self.spline_order,
|
148 |
+
)
|
149 |
+
return result.contiguous()
|
150 |
+
|
151 |
+
@property
|
152 |
+
def scaled_spline_weight(self):
|
153 |
+
return self.spline_weight * (
|
154 |
+
self.spline_scaler.unsqueeze(-1)
|
155 |
+
if self.enable_standalone_scale_spline
|
156 |
+
else 1.0
|
157 |
+
)
|
158 |
+
|
159 |
+
def forward(self, x: torch.Tensor):
|
160 |
+
assert x.size(-1) == self.in_features
|
161 |
+
original_shape = x.shape
|
162 |
+
x = x.view(-1, self.in_features)
|
163 |
+
|
164 |
+
base_output = F.linear(self.base_activation(x), self.base_weight)
|
165 |
+
spline_output = F.linear(
|
166 |
+
self.b_splines(x).view(x.size(0), -1),
|
167 |
+
self.scaled_spline_weight.view(self.out_features, -1),
|
168 |
+
)
|
169 |
+
output = base_output + spline_output
|
170 |
+
|
171 |
+
output = output.view(*original_shape[:-1], self.out_features)
|
172 |
+
return output
|
173 |
+
|
174 |
+
@torch.no_grad()
|
175 |
+
def update_grid(self, x: torch.Tensor, margin=0.01):
|
176 |
+
assert x.dim() == 2 and x.size(1) == self.in_features
|
177 |
+
batch = x.size(0)
|
178 |
+
|
179 |
+
splines = self.b_splines(x) # (batch, in, coeff)
|
180 |
+
splines = splines.permute(1, 0, 2) # (in, batch, coeff)
|
181 |
+
orig_coeff = self.scaled_spline_weight # (out, in, coeff)
|
182 |
+
orig_coeff = orig_coeff.permute(1, 2, 0) # (in, coeff, out)
|
183 |
+
unreduced_spline_output = torch.bmm(splines, orig_coeff) # (in, batch, out)
|
184 |
+
unreduced_spline_output = unreduced_spline_output.permute(
|
185 |
+
1, 0, 2
|
186 |
+
) # (batch, in, out)
|
187 |
+
|
188 |
+
# sort each channel individually to collect data distribution
|
189 |
+
x_sorted = torch.sort(x, dim=0)[0]
|
190 |
+
grid_adaptive = x_sorted[
|
191 |
+
torch.linspace(
|
192 |
+
0, batch - 1, self.grid_size + 1, dtype=torch.int64, device=x.device
|
193 |
+
)
|
194 |
+
]
|
195 |
+
|
196 |
+
uniform_step = (x_sorted[-1] - x_sorted[0] + 2 * margin) / self.grid_size
|
197 |
+
grid_uniform = (
|
198 |
+
torch.arange(
|
199 |
+
self.grid_size + 1, dtype=torch.float32, device=x.device
|
200 |
+
).unsqueeze(1)
|
201 |
+
* uniform_step
|
202 |
+
+ x_sorted[0]
|
203 |
+
- margin
|
204 |
+
)
|
205 |
+
|
206 |
+
grid = self.grid_eps * grid_uniform + (1 - self.grid_eps) * grid_adaptive
|
207 |
+
grid = torch.concatenate(
|
208 |
+
[
|
209 |
+
grid[:1]
|
210 |
+
- uniform_step
|
211 |
+
* torch.arange(self.spline_order, 0, -1, device=x.device).unsqueeze(1),
|
212 |
+
grid,
|
213 |
+
grid[-1:]
|
214 |
+
+ uniform_step
|
215 |
+
* torch.arange(1, self.spline_order + 1, device=x.device).unsqueeze(1),
|
216 |
+
],
|
217 |
+
dim=0,
|
218 |
+
)
|
219 |
+
|
220 |
+
self.grid.copy_(grid.T)
|
221 |
+
self.spline_weight.data.copy_(self.curve2coeff(x, unreduced_spline_output))
|
222 |
+
|
223 |
+
def regularization_loss(self, regularize_activation=1.0, regularize_entropy=1.0):
|
224 |
+
"""
|
225 |
+
Compute the regularization loss.
|
226 |
+
|
227 |
+
This is a dumb simulation of the original L1 regularization as stated in the
|
228 |
+
paper, since the original one requires computing absolutes and entropy from the
|
229 |
+
expanded (batch, in_features, out_features) intermediate tensor, which is hidden
|
230 |
+
behind the F.linear function if we want an memory efficient implementation.
|
231 |
+
|
232 |
+
The L1 regularization is now computed as mean absolute value of the spline
|
233 |
+
weights. The authors implementation also includes this term in addition to the
|
234 |
+
sample-based regularization.
|
235 |
+
"""
|
236 |
+
l1_fake = self.spline_weight.abs().mean(-1)
|
237 |
+
regularization_loss_activation = l1_fake.sum()
|
238 |
+
p = l1_fake / regularization_loss_activation
|
239 |
+
regularization_loss_entropy = -torch.sum(p * p.log())
|
240 |
+
return (
|
241 |
+
regularize_activation * regularization_loss_activation
|
242 |
+
+ regularize_entropy * regularization_loss_entropy
|
243 |
+
)
|
244 |
+
|
245 |
+
|
246 |
+
class KAN(torch.nn.Module):
|
247 |
+
def __init__(
|
248 |
+
self,
|
249 |
+
layers_hidden,
|
250 |
+
grid_size=5,
|
251 |
+
spline_order=3,
|
252 |
+
scale_noise=0.1,
|
253 |
+
scale_base=1.0,
|
254 |
+
scale_spline=1.0,
|
255 |
+
base_activation=torch.nn.SiLU,
|
256 |
+
grid_eps=0.02,
|
257 |
+
grid_range=[-1, 1],
|
258 |
+
):
|
259 |
+
super(KAN, self).__init__()
|
260 |
+
self.grid_size = grid_size
|
261 |
+
self.spline_order = spline_order
|
262 |
+
|
263 |
+
self.layers = torch.nn.ModuleList()
|
264 |
+
for in_features, out_features in zip(layers_hidden, layers_hidden[1:]):
|
265 |
+
self.layers.append(
|
266 |
+
KANLinear(
|
267 |
+
in_features,
|
268 |
+
out_features,
|
269 |
+
grid_size=grid_size,
|
270 |
+
spline_order=spline_order,
|
271 |
+
scale_noise=scale_noise,
|
272 |
+
scale_base=scale_base,
|
273 |
+
scale_spline=scale_spline,
|
274 |
+
base_activation=base_activation,
|
275 |
+
grid_eps=grid_eps,
|
276 |
+
grid_range=grid_range,
|
277 |
+
)
|
278 |
+
)
|
279 |
+
|
280 |
+
def forward(self, x: torch.Tensor, update_grid=False):
|
281 |
+
for layer in self.layers:
|
282 |
+
if update_grid:
|
283 |
+
layer.update_grid(x)
|
284 |
+
x = layer(x)
|
285 |
+
return x
|
286 |
+
|
287 |
+
def regularization_loss(self, regularize_activation=1.0, regularize_entropy=1.0):
|
288 |
+
return sum(
|
289 |
+
layer.regularization_loss(regularize_activation, regularize_entropy)
|
290 |
+
for layer in self.layers
|
291 |
+
)
|
292 |
+
|
293 |
+
"""## Build Kanformer"""
|
294 |
+
|
295 |
+
from transformers import AutoConfig, AutoTokenizer, AutoModel
|
296 |
+
|
297 |
+
from transformers.models.llama.modeling_llama import *
|
298 |
+
|
299 |
+
class KANLlamaAttention(LlamaAttention):
|
300 |
+
def __init__(self, config, **args):
|
301 |
+
super().__init__(config, **args)
|
302 |
+
head_dim = config.hidden_size // config.num_attention_heads
|
303 |
+
self.q_proj = KANLinear(config.hidden_size, config.num_attention_heads * head_dim)
|
304 |
+
self.k_proj = KANLinear(config.hidden_size, config.num_key_value_heads * head_dim)
|
305 |
+
self.v_proj = KANLinear(config.hidden_size, config.num_key_value_heads * head_dim)
|
306 |
+
self.o_proj = KANLinear(config.hidden_size, config.hidden_size)
|
307 |
+
self._init_rope()
|
308 |
+
|
309 |
+
class KANLlamaDecoderLayer(LlamaDecoderLayer):
|
310 |
+
def __init__(self,config, layer_idx):
|
311 |
+
super().__init__(config, layer_idx)
|
312 |
+
self.hidden_size = config.hidden_size
|
313 |
+
|
314 |
+
self.self_attn = KANLlamaAttention(config=config, layer_idx=layer_idx)
|
315 |
+
self.mlp = KAN([config.hidden_size,config.intermediate_size,config.intermediate_size,config.hidden_size])
|
316 |
+
self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
317 |
+
self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
318 |
+
|
319 |
+
class KANLlamaModel(LlamaModel):
|
320 |
+
config_class = KANLlamaConfig
|
321 |
+
def __init__(self, config):
|
322 |
+
super().__init__(config)
|
323 |
+
self.layers = nn.ModuleList(
|
324 |
+
[KANLlamaDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
325 |
+
)
|
326 |
+
|
327 |
+
class KANLlamaForSequenceClassification(LlamaForSequenceClassification):
|
328 |
+
config_class = KANLlamaConfig
|
329 |
+
def __init__(self, config):
|
330 |
+
super().__init__(config)
|
331 |
+
self.num_labels = config.num_labels
|
332 |
+
self.model = KANLlamaModel(config)
|
333 |
+
self.score = KANLinear(config.hidden_size, self.num_labels)
|
334 |
+
|
335 |
+
# Initialize weights and apply final processing
|
336 |
+
self.post_init()
|
337 |
+
|
338 |
+
class KANLlamaForCausalLM(LlamaForCausalLM):
|
339 |
+
config_class = KANLlamaConfig
|
340 |
+
def __init__(self, config):
|
341 |
+
super().__init__(config)
|
342 |
+
self.model = KANLlamaModel(config)
|
343 |
+
self.vocab_size = config.vocab_size
|
344 |
+
self.lm_head = KANLinear(config.hidden_size, config.vocab_size)
|
345 |
+
|
346 |
+
# Initialize weights and apply final processing
|
347 |
+
self.post_init()
|