File size: 15,176 Bytes
d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 a0ce150 e83a997 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 a0ce150 731cb10 a0ce150 e83a997 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 e70ad00 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 a0ce150 731cb10 a0ce150 731cb10 a0ce150 d72b2c3 731cb10 d72b2c3 e83a997 d72b2c3 731cb10 d72b2c3 2e6c69d 731cb10 d72b2c3 a0ce150 d72b2c3 a84b206 d72b2c3 60fbcf9 731cb10 60fbcf9 d72b2c3 731cb10 d72b2c3 6cb7713 60fbcf9 6cb7713 60fbcf9 6cb7713 60fbcf9 731cb10 60fbcf9 731cb10 d72b2c3 a0ce150 731cb10 a84b206 731cb10 e70ad00 2e6c69d 731cb10 2e6c69d 731cb10 2e6c69d 731cb10 d72b2c3 731cb10 2e6c69d d72b2c3 731cb10 d72b2c3 731cb10 a0ce150 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 a0ce150 731cb10 e70ad00 731cb10 e70ad00 e83a997 731cb10 a0ce150 731cb10 e83a997 731cb10 e83a997 731cb10 d72b2c3 a0ce150 731cb10 a0ce150 731cb10 6cb7713 a0ce150 731cb10 a0ce150 6cb7713 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 731cb10 d72b2c3 a0ce150 731cb10 a0ce150 d72b2c3 731cb10 a0ce150 731cb10 d72b2c3 6cb7713 a0ce150 731cb10 d72b2c3 731cb10 a84b206 731cb10 |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
import typing as tp
from einops import rearrange
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.utils.checkpoint import checkpoint as torch_checkpoint
from xformers import ops
_efficient_attention_backend: str = 'torch'
def _get_attention_time_dimension(memory_efficient: bool) -> int:
if _efficient_attention_backend == 'torch' and memory_efficient:
return 2
else:
return 1
def create_sin_embedding(positions: torch.Tensor, dim: int, max_period: float = 10000,
dtype: torch.dtype = torch.float32) -> torch.Tensor:
"""Create sinusoidal positional embedding, with shape `[B, T, C]`.
Args:
positions (torch.Tensor): LongTensor of positions.
dim (int): Dimension of the embedding.
max_period (float): Maximum period of the cosine/sine functions.
dtype (torch.dtype or str): dtype to use to generate the embedding.
Returns:
torch.Tensor: Sinusoidal positional embedding.
"""
# We aim for BTC format
assert dim % 2 == 0
half_dim = dim // 2
positions = positions.to(dtype)
adim = torch.arange(half_dim, device=positions.device, dtype=dtype).view(1, 1, -1)
max_period_tensor = torch.full([], max_period, device=positions.device, dtype=dtype) # avoid sync point
phase = positions / (max_period_tensor ** (adim / (half_dim - 1)))
return torch.cat([torch.cos(phase), torch.sin(phase)], dim=-1)
def expand_repeated_kv(x: torch.Tensor, n_rep: int, memory_efficient: bool) -> torch.Tensor:
"""torch.repeat_interleave(x, dim=2, repeats=n_rep) from xlformers."""
if n_rep == 1:
return x
if _efficient_attention_backend == 'torch' and memory_efficient:
bs, n_kv_heads, slen, head_dim = x.shape
return (
x[:, :, None, :, :]
.expand(bs, n_kv_heads, n_rep, slen, head_dim)
.reshape(bs, n_kv_heads * n_rep, slen, head_dim)
)
else:
bs, slen, n_kv_heads, head_dim = x.shape
return (
x[:, :, :, None, :]
.expand(bs, slen, n_kv_heads, n_rep, head_dim)
.reshape(bs, slen, n_kv_heads * n_rep, head_dim)
)
class StreamingMultiheadAttention(nn.Module):
def __init__(self,
embed_dim,
num_heads, dropout: float = 0.0, bias: bool = True,
causal: bool = False, past_context: tp.Optional[int] = None, custom: bool = False,
memory_efficient: bool = False, attention_as_float32: bool = False,
cross_attention: bool = False,
kv_repeat: int = 1,
device=None, dtype=None):
super().__init__()
factory_kwargs = {'device': device, 'dtype': dtype}
if past_context is not None:
assert causal
self.embed_dim = embed_dim
self.k_history = None # previous k from the previous tokens seen in the current generation - only for selt.attn
self.v_history = None # clean up IN LM after finishing GENERATION - Each 1...47 mha has different kv history
self.memory_efficient = memory_efficient
self.cross_attention = cross_attention
self.num_heads = num_heads
self.dropout = dropout
self.kv_repeat = kv_repeat
self.custom = True #_is_custom(custom, memory_efficient)
if not self.custom:
print(f'{self.custom}')
if self.custom:
out_dim = embed_dim
assert num_heads % kv_repeat == 0
assert not cross_attention or kv_repeat == 1
num_kv = num_heads // kv_repeat
kv_dim = (embed_dim // num_heads) * num_kv
out_dim += 2 * kv_dim
in_proj = nn.Linear(embed_dim, out_dim, bias=bias, **factory_kwargs)
# We try to follow the default PyTorch MHA convention, to easily compare results.
self.in_proj_weight = in_proj.weight
self.in_proj_bias = in_proj.bias
if bias:
self.in_proj_bias.data.zero_() # Following Pytorch convention
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias, **factory_kwargs)
if bias:
self.out_proj.bias.data.zero_()
else:
assert kv_repeat == 1
self.mha = nn.MultiheadAttention(
embed_dim, num_heads, dropout=dropout, bias=bias, batch_first=True,
**factory_kwargs)
def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs):
if not self.custom:
# Support compat with regular MHA
keys = [n for n, _ in self.mha.named_parameters()]
for key in keys:
if prefix + key in state_dict:
state_dict[prefix + "mha." + key] = state_dict.pop(prefix + key)
super()._load_from_state_dict(state_dict, prefix, *args, **kwargs)
def forward(self,
query,
key=None, # ignores those 2 args if not self.cross_attn
value=None):
# time_dim = _get_attention_time_dimension(self.memory_efficient)
# if time_dim == 2:
layout = "b h t d"
# else:
# layout = "b t h d"
# dtype = query.dtype
if self.custom:
if self.cross_attention:
# Different queries, keys, values, we have to spit manually the weights
# before applying the linear.
dim = self.in_proj_weight.shape[0] // 3
if self.in_proj_bias is None:
bias_q, bias_k, bias_v = None, None, None
else:
bias_q = self.in_proj_bias[:dim]
bias_k = self.in_proj_bias[dim: 2 * dim]
bias_v = self.in_proj_bias[2 * dim:]
q = nn.functional.linear(query, self.in_proj_weight[:dim], bias_q)
# todo: when streaming, we could actually save k, v and check the shape actually match.
k = nn.functional.linear(key, self.in_proj_weight[dim: 2 * dim], bias_k)
v = nn.functional.linear(value, self.in_proj_weight[2 * dim:], bias_v)
q, k, v = [rearrange(x, f"b t (h d) -> {layout}", h=self.num_heads) for x in [q, k, v]]
# print(q.shape, k.shape, v.shape, q.sum(), k.sum(), v.sum(),'CROSS A5')
else:
# 1st projected makes k,v (instantaneous)
# 2nd cat
# HISTORY - DIFFERENT FOR EACH TRANSF LAYER
projected = nn.functional.linear(query, self.in_proj_weight, self.in_proj_bias)
if self.kv_repeat == 1:
# if time_dim == 2:
bound_layout = "b h p t d"
# else:
# bound_layout = "b t p h d"
packed = rearrange(projected, f"b t (p h d) -> {bound_layout}", p=3, h=self.num_heads)
q, k, v = ops.unbind(packed, dim=2)
if self.k_history is not None:
#
# pk.shape=torch.Size([2, 24, 3, 64]) k.shape=torch.Size([2, 24, 1, 64]) CONCAT
# has to be 4D with batch 1 due to single condition 3=seqlen
# 24 heads 64 dimofh
self.k_history = torch.cat([self.k_history, k], 2)
self.v_history = torch.cat([self.v_history, v], 2)
else:
# init on 1st token (for all 47 transf layers)
print(f'else skip')
self.k_history = k
self.v_history = v
k = self.k_history
v = self.v_history
# KV COMPLETION ONLY ON SELF ATTENTION
# print('KV5', self.k_history.sum(), self.v_history.sum(), self.k_history.shape, self.v_history.shape)
if self.memory_efficient:
# print('EVER IN MEMORY EFFICIENT A')
p = self.dropout if self.training else 0
if _efficient_attention_backend == 'torch':
# print(q.shape, k.shape, v.shape, q.sum(), k.sum(), v.sum(), 'CROSSopen')
x = torch.nn.functional.scaled_dot_product_attention(
q, k, v, is_causal=False, dropout_p=p
)
x = x.to(q.dtype)
x = rearrange(x, f"{layout} -> b t (h d)", h=self.num_heads)
x = self.out_proj(x)
return x
class StreamingTransformerLayer(nn.Module): #nn.TransformerEncoderLayer):
# INHERITS MHA !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def __init__(self,
d_model: int,
num_heads: int,
dim_feedforward: int = 2048,
dropout: float = 0.1,
bias_ff: bool = True,
bias_attn: bool = True,
custom: bool = False,
memory_efficient: bool = False,
attention_as_float32: bool = False,
cross_attention: bool = False,
attention_dropout: tp.Optional[float] = None,
kv_repeat: int = 1,
norm: str = 'layer_norm',
device=None,
dtype=None,
**kwargs):
super().__init__() #d_model, num_heads, dim_feedforward, dropout,
#device=device, dtype=dtype, batch_first=True, **kwargs)
# print(kwargs['activation'], 'ACTIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII\n\n\n\n')
# -- EN Layer
# DOES NOT INHERIT NO VARIABLE FROM nn.TransformerEncoderLayer only the _sa_block function
# -- EN layer
factory_kwargs = {'device': device, 'dtype': dtype}
# Redefine self_attn to our streaming multi-head attention
attn_kwargs: tp.Dict[str, tp.Any] = {
'embed_dim': d_model,
'num_heads': num_heads,
'dropout': dropout if attention_dropout is None else attention_dropout,
'bias': bias_attn,
'custom': custom,
'memory_efficient': memory_efficient,
'attention_as_float32': attention_as_float32,
}
self.self_attn = StreamingMultiheadAttention(
kv_repeat=kv_repeat,
**attn_kwargs,
**factory_kwargs) # type: ignore
# Redefine feedforward layers to expose bias parameter
self.linear1 = nn.Linear(d_model, dim_feedforward, bias=bias_ff, **factory_kwargs)
self.linear2 = nn.Linear(dim_feedforward, d_model, bias=bias_ff, **factory_kwargs)
# print('LAYER scale', layer_scale, '\n\n\n\n\n\n\n\n\n') # always
self.cross_attention= None
if cross_attention:
self.cross_attention = StreamingMultiheadAttention(
cross_attention=True,
**attn_kwargs,
**factory_kwargs)
self.dropout_cross = nn.Dropout(dropout)
self.norm_cross = nn.LayerNorm(d_model, eps=1e-5, **factory_kwargs)
self.norm1 = nn.LayerNorm(d_model, eps=1e-5)
self.norm2 = nn.LayerNorm(d_model, eps=1e-5)
def forward(self,
src,
cross_attention_src=None): # txtcond
'''T is saved float16 weights - should we cast src to float16'''
x = src
x = x + self.self_attn(self.norm1(x))
if cross_attention_src is not None:
x = x + self.cross_attention(
query = self.norm_cross(x),
key = cross_attention_src,
value = cross_attention_src) # txtcondition
x = x + self.linear2(F.gelu(self.linear1( self.norm2(x) )))
return x
class StreamingTransformer(nn.Module):
def __init__(self, d_model: int,
num_heads: int,
num_layers: int,
dim_feedforward: int = 2048,
dropout: float = 0.1,
bias_ff: bool = True,
bias_attn: bool = True,
custom: bool = False,
memory_efficient: bool = False,
attention_as_float32: bool = False,
cross_attention: bool = False,
positional_embedding: str = 'sin',
max_period: float = 10_000,
layer_class=StreamingTransformerLayer,
checkpointing: str = 'none',
device=None,
dtype=None,
**kwargs):
super().__init__()
assert d_model % num_heads == 0
self.positional_embedding = positional_embedding
self.max_period = max_period
# self._stream_off = 0 # the llm should reinitialize this at ery generate()
self.checkpointing = checkpointing
self.layers = nn.ModuleList()
for idx in range(num_layers):
self.layers.append(
layer_class(
d_model=d_model, num_heads=num_heads, dim_feedforward=dim_feedforward,
dropout=dropout, bias_ff=bias_ff, bias_attn=bias_attn,
custom=custom,
memory_efficient=memory_efficient, attention_as_float32=attention_as_float32,
cross_attention=cross_attention,
device=device, dtype=dtype, **kwargs))
if self.checkpointing != 'none':
for layer in self.layers:
# see audiocraft/optim/fsdp.py, magic signal to indicate this requires fixing the
# backward hook inside of FSDP...
layer._magma_checkpointed = True # type: ignore
def forward(self, x: torch.Tensor, *args, **kwargs):
B, T, C = x.shape
if self.positional_embedding in ['sin', 'sin_rope']:
positions = torch.arange(T, device=x.device).view(1, -1, 1)
positions = positions + kwargs['token_count'] #offsets.view(-1, 1, 1)
pos_emb = create_sin_embedding(positions, C, max_period=self.max_period, dtype=x.dtype)
x = x + pos_emb
for j, lay in enumerate(self.layers):
# print(f'Transf Layer{j} {pos_emb.sum()=} {pos_emb.shape=}{x.shape=}___________________')
x = lay(x, cross_attention_src=kwargs["cross_attention_src"]) # cross_attention_src = txt-cond
# each layer (mha) keeps history of its own k,v for all tokens
return x
|