python_code
stringlengths
0
992k
repo_name
stringlengths
8
46
file_path
stringlengths
5
162
Pegasus-master
pegasus/ImageBind/models/__init__.py
#!/usr/bin/env python3 # Portions Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os from functools import partial from types import SimpleNamespace import torch import torch.nn as nn from .helpers import ( EinOpsRearrange, LearnableLogitScaling, Normalize, SelectElement, SelectEOSAndProject, ) from .multimodal_preprocessors import ( AudioPreprocessor, IMUPreprocessor, PadIm2Video, PatchEmbedGeneric, RGBDTPreprocessor, SpatioTemporalPosEmbeddingHelper, TextPreprocessor, ThermalPreprocessor, ) from .transformer import MultiheadAttention, SimpleTransformer ModalityType = SimpleNamespace( VISION="vision", TEXT="text", AUDIO="audio", THERMAL="thermal", DEPTH="depth", IMU="imu", ) class ImageBindModel(nn.Module): def __init__( self, video_frames=2, kernel_size=(2, 14, 14), audio_kernel_size=16, audio_stride=10, out_embed_dim=768, vision_embed_dim=1024, vision_num_blocks=24, vision_num_heads=16, audio_embed_dim=768, audio_num_blocks=12, audio_num_heads=12, audio_num_mel_bins=128, audio_target_len=204, audio_drop_path=0.1, text_embed_dim=768, text_num_blocks=12, text_num_heads=12, depth_embed_dim=384, depth_kernel_size=16, depth_num_blocks=12, depth_num_heads=8, depth_drop_path=0.0, thermal_embed_dim=768, thermal_kernel_size=16, thermal_num_blocks=12, thermal_num_heads=12, thermal_drop_path=0.0, imu_embed_dim=512, imu_kernel_size=8, imu_num_blocks=6, imu_num_heads=8, imu_drop_path=0.7, ): super().__init__() self.modality_preprocessors = self._create_modality_preprocessors( video_frames, vision_embed_dim, kernel_size, text_embed_dim, audio_embed_dim, audio_kernel_size, audio_stride, audio_num_mel_bins, audio_target_len, depth_embed_dim, depth_kernel_size, thermal_embed_dim, thermal_kernel_size, imu_embed_dim, ) self.modality_trunks = self._create_modality_trunks( vision_embed_dim, vision_num_blocks, vision_num_heads, text_embed_dim, text_num_blocks, text_num_heads, audio_embed_dim, audio_num_blocks, audio_num_heads, audio_drop_path, depth_embed_dim, depth_num_blocks, depth_num_heads, depth_drop_path, thermal_embed_dim, thermal_num_blocks, thermal_num_heads, thermal_drop_path, imu_embed_dim, imu_num_blocks, imu_num_heads, imu_drop_path, ) self.modality_heads = self._create_modality_heads( out_embed_dim, vision_embed_dim, text_embed_dim, audio_embed_dim, depth_embed_dim, thermal_embed_dim, imu_embed_dim, ) self.modality_postprocessors = self._create_modality_postprocessors( out_embed_dim ) def _create_modality_preprocessors( self, video_frames=2, vision_embed_dim=1024, kernel_size=(2, 14, 14), text_embed_dim=768, audio_embed_dim=768, audio_kernel_size=16, audio_stride=10, audio_num_mel_bins=128, audio_target_len=204, depth_embed_dim=768, depth_kernel_size=16, thermal_embed_dim=768, thermal_kernel_size=16, imu_embed_dim=512, ): rgbt_stem = PatchEmbedGeneric( proj_stem=[ PadIm2Video(pad_type="repeat", ntimes=2), nn.Conv3d( in_channels=3, kernel_size=kernel_size, out_channels=vision_embed_dim, stride=kernel_size, bias=False, ), ] ) rgbt_preprocessor = RGBDTPreprocessor( img_size=[3, video_frames, 224, 224], num_cls_tokens=1, pos_embed_fn=partial(SpatioTemporalPosEmbeddingHelper, learnable=True), rgbt_stem=rgbt_stem, depth_stem=None, ) text_preprocessor = TextPreprocessor( context_length=77, vocab_size=49408, embed_dim=text_embed_dim, causal_masking=True, ) audio_stem = PatchEmbedGeneric( proj_stem=[ nn.Conv2d( in_channels=1, kernel_size=audio_kernel_size, stride=audio_stride, out_channels=audio_embed_dim, bias=False, ), ], norm_layer=nn.LayerNorm(normalized_shape=audio_embed_dim), ) audio_preprocessor = AudioPreprocessor( img_size=[1, audio_num_mel_bins, audio_target_len], num_cls_tokens=1, pos_embed_fn=partial(SpatioTemporalPosEmbeddingHelper, learnable=True), audio_stem=audio_stem, ) depth_stem = PatchEmbedGeneric( [ nn.Conv2d( kernel_size=depth_kernel_size, in_channels=1, out_channels=depth_embed_dim, stride=depth_kernel_size, bias=False, ), ], norm_layer=nn.LayerNorm(normalized_shape=depth_embed_dim), ) depth_preprocessor = RGBDTPreprocessor( img_size=[1, 224, 224], num_cls_tokens=1, pos_embed_fn=partial(SpatioTemporalPosEmbeddingHelper, learnable=True), rgbt_stem=None, depth_stem=depth_stem, ) thermal_stem = PatchEmbedGeneric( [ nn.Conv2d( kernel_size=thermal_kernel_size, in_channels=1, out_channels=thermal_embed_dim, stride=thermal_kernel_size, bias=False, ), ], norm_layer=nn.LayerNorm(normalized_shape=thermal_embed_dim), ) thermal_preprocessor = ThermalPreprocessor( img_size=[1, 224, 224], num_cls_tokens=1, pos_embed_fn=partial(SpatioTemporalPosEmbeddingHelper, learnable=True), thermal_stem=thermal_stem, ) imu_stem = PatchEmbedGeneric( [ nn.Linear( in_features=48, out_features=imu_embed_dim, bias=False, ), ], norm_layer=nn.LayerNorm(normalized_shape=imu_embed_dim), ) imu_preprocessor = IMUPreprocessor( img_size=[6, 2000], num_cls_tokens=1, kernel_size=8, embed_dim=imu_embed_dim, pos_embed_fn=partial(SpatioTemporalPosEmbeddingHelper, learnable=True), imu_stem=imu_stem, ) modality_preprocessors = { ModalityType.VISION: rgbt_preprocessor, ModalityType.TEXT: text_preprocessor, ModalityType.AUDIO: audio_preprocessor, ModalityType.DEPTH: depth_preprocessor, ModalityType.THERMAL: thermal_preprocessor, ModalityType.IMU: imu_preprocessor, } return nn.ModuleDict(modality_preprocessors) def _create_modality_trunks( self, vision_embed_dim=1024, vision_num_blocks=24, vision_num_heads=16, text_embed_dim=768, text_num_blocks=12, text_num_heads=12, audio_embed_dim=768, audio_num_blocks=12, audio_num_heads=12, audio_drop_path=0.0, depth_embed_dim=768, depth_num_blocks=12, depth_num_heads=12, depth_drop_path=0.0, thermal_embed_dim=768, thermal_num_blocks=12, thermal_num_heads=12, thermal_drop_path=0.0, imu_embed_dim=512, imu_num_blocks=6, imu_num_heads=8, imu_drop_path=0.7, ): def instantiate_trunk( embed_dim, num_blocks, num_heads, pre_transformer_ln, add_bias_kv, drop_path ): return SimpleTransformer( embed_dim=embed_dim, num_blocks=num_blocks, ffn_dropout_rate=0.0, drop_path_rate=drop_path, attn_target=partial( MultiheadAttention, embed_dim=embed_dim, num_heads=num_heads, bias=True, add_bias_kv=add_bias_kv, ), pre_transformer_layer=nn.Sequential( nn.LayerNorm(embed_dim, eps=1e-6) if pre_transformer_ln else nn.Identity(), EinOpsRearrange("b l d -> l b d"), ), post_transformer_layer=EinOpsRearrange("l b d -> b l d"), ) modality_trunks = {} modality_trunks[ModalityType.VISION] = instantiate_trunk( vision_embed_dim, vision_num_blocks, vision_num_heads, pre_transformer_ln=True, add_bias_kv=False, drop_path=0.0, ) modality_trunks[ModalityType.TEXT] = instantiate_trunk( text_embed_dim, text_num_blocks, text_num_heads, pre_transformer_ln=False, add_bias_kv=False, drop_path=0.0, ) modality_trunks[ModalityType.AUDIO] = instantiate_trunk( audio_embed_dim, audio_num_blocks, audio_num_heads, pre_transformer_ln=False, add_bias_kv=True, drop_path=audio_drop_path, ) modality_trunks[ModalityType.DEPTH] = instantiate_trunk( depth_embed_dim, depth_num_blocks, depth_num_heads, pre_transformer_ln=False, add_bias_kv=True, drop_path=depth_drop_path, ) modality_trunks[ModalityType.THERMAL] = instantiate_trunk( thermal_embed_dim, thermal_num_blocks, thermal_num_heads, pre_transformer_ln=False, add_bias_kv=True, drop_path=thermal_drop_path, ) modality_trunks[ModalityType.IMU] = instantiate_trunk( imu_embed_dim, imu_num_blocks, imu_num_heads, pre_transformer_ln=False, add_bias_kv=True, drop_path=imu_drop_path, ) return nn.ModuleDict(modality_trunks) def _create_modality_heads( self, out_embed_dim, vision_embed_dim, text_embed_dim, audio_embed_dim, depth_embed_dim, thermal_embed_dim, imu_embed_dim, ): modality_heads = {} modality_heads[ModalityType.VISION] = nn.Sequential( nn.LayerNorm(normalized_shape=vision_embed_dim, eps=1e-6), SelectElement(index=0), nn.Linear(vision_embed_dim, out_embed_dim, bias=False), ) modality_heads[ModalityType.TEXT] = SelectEOSAndProject( proj=nn.Sequential( nn.LayerNorm(normalized_shape=text_embed_dim, eps=1e-6), nn.Linear(text_embed_dim, out_embed_dim, bias=False), ) ) modality_heads[ModalityType.AUDIO] = nn.Sequential( nn.LayerNorm(normalized_shape=audio_embed_dim, eps=1e-6), SelectElement(index=0), nn.Linear(audio_embed_dim, out_embed_dim, bias=False), ) modality_heads[ModalityType.DEPTH] = nn.Sequential( nn.LayerNorm(normalized_shape=depth_embed_dim, eps=1e-6), SelectElement(index=0), nn.Linear(depth_embed_dim, out_embed_dim, bias=False), ) modality_heads[ModalityType.THERMAL] = nn.Sequential( nn.LayerNorm(normalized_shape=thermal_embed_dim, eps=1e-6), SelectElement(index=0), nn.Linear(thermal_embed_dim, out_embed_dim, bias=False), ) modality_heads[ModalityType.IMU] = nn.Sequential( nn.LayerNorm(normalized_shape=imu_embed_dim, eps=1e-6), SelectElement(index=0), nn.Dropout(p=0.5), nn.Linear(imu_embed_dim, out_embed_dim, bias=False), ) return nn.ModuleDict(modality_heads) def _create_modality_postprocessors(self, out_embed_dim): modality_postprocessors = {} modality_postprocessors[ModalityType.VISION] = Normalize(dim=-1) modality_postprocessors[ModalityType.TEXT] = nn.Sequential( Normalize(dim=-1), LearnableLogitScaling(learnable=True) ) modality_postprocessors[ModalityType.AUDIO] = nn.Sequential( Normalize(dim=-1), LearnableLogitScaling(logit_scale_init=20.0, learnable=False), ) modality_postprocessors[ModalityType.DEPTH] = nn.Sequential( Normalize(dim=-1), LearnableLogitScaling(logit_scale_init=5.0, learnable=False), ) modality_postprocessors[ModalityType.THERMAL] = nn.Sequential( Normalize(dim=-1), LearnableLogitScaling(logit_scale_init=10.0, learnable=False), ) modality_postprocessors[ModalityType.IMU] = nn.Sequential( Normalize(dim=-1), LearnableLogitScaling(logit_scale_init=5.0, learnable=False), ) return nn.ModuleDict(modality_postprocessors) def forward(self, inputs): outputs = {} for modality_key, modality_value in inputs.items(): reduce_list = ( modality_value.ndim >= 5 ) # Audio and Video inputs consist of multiple clips if reduce_list: B, S = modality_value.shape[:2] modality_value = modality_value.reshape( B * S, *modality_value.shape[2:] ) if modality_value is not None: modality_value = self.modality_preprocessors[modality_key]( **{modality_key: modality_value} ) trunk_inputs = modality_value["trunk"] head_inputs = modality_value["head"] modality_value = self.modality_trunks[modality_key](**trunk_inputs) modality_value = self.modality_heads[modality_key]( modality_value, **head_inputs ) modality_value = self.modality_postprocessors[modality_key]( modality_value ) if reduce_list: modality_value = modality_value.reshape(B, S, -1) modality_value = modality_value.mean(dim=1) outputs[modality_key] = modality_value return outputs def imagebind_huge(pretrained=False): model = ImageBindModel( vision_embed_dim=1280, vision_num_blocks=32, vision_num_heads=16, text_embed_dim=1024, text_num_blocks=24, text_num_heads=16, out_embed_dim=1024, audio_drop_path=0.1, imu_drop_path=0.7, ) if pretrained: if not os.path.exists(".checkpoints/imagebind_huge.pth"): print( "Downloading imagebind weights to .checkpoints/imagebind_huge.pth ..." ) os.makedirs(".checkpoints", exist_ok=True) torch.hub.download_url_to_file( "https://dl.fbaipublicfiles.com/imagebind/imagebind_huge.pth", ".checkpoints/imagebind_huge.pth", progress=True, ) model.load_state_dict(torch.load(".checkpoints/imagebind_huge.pth")) return model
Pegasus-master
pegasus/ImageBind/models/imagebind_model.py
#!/usr/bin/env python3 # Portions Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # Code modified from # https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py ; # https://github.com/facebookresearch/deit/blob/main/models.py # and https://github.com/facebookresearch/vissl/blob/main/vissl/models/trunks/vision_transformer.py from functools import partial from typing import Callable, List import torch import torch.nn as nn import torch.utils.checkpoint as checkpoint from timm.models.layers import DropPath, trunc_normal_ class Attention(nn.Module): def __init__( self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0, ): super().__init__() self.num_heads = num_heads head_dim = dim // num_heads # NOTE scale factor was wrong in my original version, # can set manually to be compat with prev weights self.scale = qk_scale or head_dim**-0.5 self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) def forward(self, x): B, N, C = x.shape qkv = ( self.qkv(x) .reshape(B, N, 3, self.num_heads, C // self.num_heads) .permute(2, 0, 3, 1, 4) ) q, k, v = ( qkv[0], qkv[1], qkv[2], ) # make torchscript happy (cannot use tensor as tuple) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) attn = self.attn_drop(attn) x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.proj(x) x = self.proj_drop(x) return x class Mlp(nn.Module): def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x class MultiheadAttention(nn.MultiheadAttention): def forward(self, x: torch.Tensor, attn_mask: torch.Tensor): return super().forward(x, x, x, need_weights=False, attn_mask=attn_mask)[0] class ViTAttention(Attention): def forward(self, x: torch.Tensor, attn_mask: torch.Tensor): assert attn_mask is None return super().forward(x) class BlockWithMasking(nn.Module): def __init__( self, dim: int, attn_target: Callable, mlp_ratio: int = 4, act_layer: Callable = nn.GELU, norm_layer: Callable = nn.LayerNorm, ffn_dropout_rate: float = 0.0, drop_path: float = 0.0, layer_scale_type: str = None, layer_scale_init_value: float = 1e-4, ): super().__init__() assert not isinstance( attn_target, nn.Module ), "attn_target should be a Callable. Otherwise attn_target is shared across blocks!" self.attn = attn_target() if drop_path > 0.0: self.drop_path = DropPath(drop_path) else: self.drop_path = nn.Identity() self.norm_1 = norm_layer(dim) mlp_hidden_dim = int(mlp_ratio * dim) self.mlp = Mlp( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=ffn_dropout_rate, ) self.norm_2 = norm_layer(dim) self.layer_scale_type = layer_scale_type if self.layer_scale_type is not None: assert self.layer_scale_type in [ "per_channel", "scalar", ], f"Found Layer scale type {self.layer_scale_type}" if self.layer_scale_type == "per_channel": # one gamma value per channel gamma_shape = [1, 1, dim] elif self.layer_scale_type == "scalar": # single gamma value for all channels gamma_shape = [1, 1, 1] # two gammas: for each part of the fwd in the encoder self.layer_scale_gamma1 = nn.Parameter( torch.ones(size=gamma_shape) * layer_scale_init_value, requires_grad=True, ) self.layer_scale_gamma2 = nn.Parameter( torch.ones(size=gamma_shape) * layer_scale_init_value, requires_grad=True, ) def forward(self, x: torch.Tensor, attn_mask: torch.Tensor): if self.layer_scale_type is None: x = x + self.drop_path(self.attn(self.norm_1(x), attn_mask)) x = x + self.drop_path(self.mlp(self.norm_2(x))) else: x = ( x + self.drop_path(self.attn(self.norm_1(x), attn_mask)) * self.layer_scale_gamma1 ) x = x + self.drop_path(self.mlp(self.norm_2(x))) * self.layer_scale_gamma2 return x _LAYER_NORM = partial(nn.LayerNorm, eps=1e-6) class SimpleTransformer(nn.Module): def __init__( self, attn_target: Callable, embed_dim: int, num_blocks: int, block: Callable = BlockWithMasking, pre_transformer_layer: Callable = None, post_transformer_layer: Callable = None, drop_path_rate: float = 0.0, drop_path_type: str = "progressive", norm_layer: Callable = _LAYER_NORM, mlp_ratio: int = 4, ffn_dropout_rate: float = 0.0, layer_scale_type: str = None, # from cait; possible values are None, "per_channel", "scalar" layer_scale_init_value: float = 1e-4, # from cait; float weight_init_style: str = "jax", # possible values jax or pytorch ): """ Simple Transformer with the following features 1. Supports masked attention 2. Supports DropPath 3. Supports LayerScale 4. Supports Dropout in Attention and FFN 5. Makes few assumptions about the input except that it is a Tensor """ super().__init__() self.pre_transformer_layer = pre_transformer_layer if drop_path_type == "progressive": dpr = [x.item() for x in torch.linspace(0, drop_path_rate, num_blocks)] elif drop_path_type == "uniform": dpr = [drop_path_rate for i in range(num_blocks)] else: raise ValueError(f"Unknown drop_path_type: {drop_path_type}") self.blocks = nn.Sequential( *[ block( dim=embed_dim, attn_target=attn_target, mlp_ratio=mlp_ratio, ffn_dropout_rate=ffn_dropout_rate, drop_path=dpr[i], norm_layer=norm_layer, layer_scale_type=layer_scale_type, layer_scale_init_value=layer_scale_init_value, ) for i in range(num_blocks) ] ) self.post_transformer_layer = post_transformer_layer self.weight_init_style = weight_init_style self.apply(self._init_weights) def _init_weights(self, m): if isinstance(m, nn.Linear): if self.weight_init_style == "jax": # Based on MAE and official Jax ViT implementation torch.nn.init.xavier_uniform_(m.weight) elif self.weight_init_style == "pytorch": # PyTorch ViT uses trunc_normal_ trunc_normal_(m.weight, std=0.02) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, (nn.LayerNorm)): nn.init.constant_(m.bias, 0) nn.init.constant_(m.weight, 1.0) def forward( self, tokens: torch.Tensor, attn_mask: torch.Tensor = None, use_checkpoint: bool = False, checkpoint_every_n: int = 1, checkpoint_blk_ids: List[int] = None, ): """ Inputs - tokens: data of shape N x L x D (or L x N x D depending on the attention implementation) - attn: mask of shape L x L Output - x: data of shape N x L x D (or L x N x D depending on the attention implementation) """ if self.pre_transformer_layer: tokens = self.pre_transformer_layer(tokens) if use_checkpoint and checkpoint_blk_ids is None: checkpoint_blk_ids = [ blk_id for blk_id in range(len(self.blocks)) if blk_id % checkpoint_every_n == 0 ] if checkpoint_blk_ids: checkpoint_blk_ids = set(checkpoint_blk_ids) for blk_id, blk in enumerate(self.blocks): if use_checkpoint and blk_id in checkpoint_blk_ids: tokens = checkpoint.checkpoint( blk, tokens, attn_mask, use_reentrant=False ) else: tokens = blk(tokens, attn_mask=attn_mask) if self.post_transformer_layer: tokens = self.post_transformer_layer(tokens) return tokens
Pegasus-master
pegasus/ImageBind/models/transformer.py
#!/usr/bin/env python3 # Portions Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import gzip import html import io import math from functools import lru_cache from typing import Callable, List, Optional import ftfy import numpy as np import regex as re import torch import torch.nn as nn from iopath.common.file_io import g_pathmgr from timm.models.layers import trunc_normal_ from .helpers import cast_if_src_dtype, VerboseNNModule def get_sinusoid_encoding_table(n_position, d_hid): """Sinusoid position encoding table""" # TODO: make it with torch instead of numpy def get_position_angle_vec(position): return [ position / np.power(10000, 2 * (hid_j // 2) / d_hid) for hid_j in range(d_hid) ] sinusoid_table = np.array( [get_position_angle_vec(pos_i) for pos_i in range(n_position)] ) sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1 return torch.FloatTensor(sinusoid_table).unsqueeze(0) def interpolate_pos_encoding_2d(target_spatial_size, pos_embed): N = pos_embed.shape[1] if N == target_spatial_size: return pos_embed dim = pos_embed.shape[-1] # nn.functional.interpolate doesn't work with bfloat16 so we cast to float32 pos_embed, updated = cast_if_src_dtype(pos_embed, torch.bfloat16, torch.float32) pos_embed = nn.functional.interpolate( pos_embed.reshape(1, int(math.sqrt(N)), int(math.sqrt(N)), dim).permute( 0, 3, 1, 2 ), scale_factor=math.sqrt(target_spatial_size / N), mode="bicubic", ) if updated: pos_embed, _ = cast_if_src_dtype(pos_embed, torch.float32, torch.bfloat16) pos_embed = pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) return pos_embed def interpolate_pos_encoding( npatch_per_img, pos_embed, patches_layout, input_shape=None, first_patch_idx=1, ): assert first_patch_idx == 0 or first_patch_idx == 1, "there is 1 CLS token or none" N = pos_embed.shape[1] - first_patch_idx # since it's 1 if cls_token exists if npatch_per_img == N: return pos_embed assert ( patches_layout[-1] == patches_layout[-2] ), "Interpolation of pos embed not supported for non-square layouts" class_emb = pos_embed[:, :first_patch_idx] pos_embed = pos_embed[:, first_patch_idx:] if input_shape is None or patches_layout[0] == 1: # simple 2D pos embedding, no temporal component pos_embed = interpolate_pos_encoding_2d(npatch_per_img, pos_embed) elif patches_layout[0] > 1: # pos embed has a temporal component assert len(input_shape) == 4, "temporal interpolation not supported" # we only support 2D interpolation in this case num_frames = patches_layout[0] num_spatial_tokens = patches_layout[1] * patches_layout[2] pos_embed = pos_embed.view(1, num_frames, num_spatial_tokens, -1) # interpolate embedding for zeroth frame pos_embed = interpolate_pos_encoding_2d( npatch_per_img, pos_embed[0, 0, ...].unsqueeze(0) ) else: raise ValueError("This type of interpolation isn't implemented") return torch.cat((class_emb, pos_embed), dim=1) def _get_pos_embedding( npatch_per_img, pos_embed, patches_layout, input_shape, first_patch_idx=1, ): pos_embed = interpolate_pos_encoding( npatch_per_img, pos_embed, patches_layout, input_shape=input_shape, first_patch_idx=first_patch_idx, ) return pos_embed class PatchEmbedGeneric(nn.Module): """ PatchEmbed from Hydra """ def __init__(self, proj_stem, norm_layer: Optional[nn.Module] = None): super().__init__() if len(proj_stem) > 1: self.proj = nn.Sequential(*proj_stem) else: # Special case to be able to load pre-trained models that were # trained with a standard stem self.proj = proj_stem[0] self.norm_layer = norm_layer def get_patch_layout(self, img_size): with torch.no_grad(): dummy_img = torch.zeros( [ 1, ] + img_size ) dummy_out = self.proj(dummy_img) embed_dim = dummy_out.shape[1] patches_layout = tuple(dummy_out.shape[2:]) num_patches = np.prod(patches_layout) return patches_layout, num_patches, embed_dim def forward(self, x): x = self.proj(x) # B C (T) H W -> B (T)HW C x = x.flatten(2).transpose(1, 2) if self.norm_layer is not None: x = self.norm_layer(x) return x class SpatioTemporalPosEmbeddingHelper(VerboseNNModule): def __init__( self, patches_layout: List, num_patches: int, num_cls_tokens: int, embed_dim: int, learnable: bool, ) -> None: super().__init__() self.num_cls_tokens = num_cls_tokens self.patches_layout = patches_layout self.num_patches = num_patches self.num_tokens = num_cls_tokens + num_patches self.learnable = learnable if self.learnable: self.pos_embed = nn.Parameter(torch.zeros(1, self.num_tokens, embed_dim)) trunc_normal_(self.pos_embed, std=0.02) else: self.register_buffer( "pos_embed", get_sinusoid_encoding_table(self.num_tokens, embed_dim) ) def get_pos_embedding(self, vision_input, all_vision_tokens): input_shape = vision_input.shape pos_embed = _get_pos_embedding( all_vision_tokens.size(1) - self.num_cls_tokens, pos_embed=self.pos_embed, patches_layout=self.patches_layout, input_shape=input_shape, first_patch_idx=self.num_cls_tokens, ) return pos_embed class RGBDTPreprocessor(VerboseNNModule): def __init__( self, rgbt_stem: PatchEmbedGeneric, depth_stem: PatchEmbedGeneric, img_size: List = (3, 224, 224), num_cls_tokens: int = 1, pos_embed_fn: Callable = None, use_type_embed: bool = False, init_param_style: str = "openclip", ) -> None: super().__init__() stem = rgbt_stem if rgbt_stem is not None else depth_stem ( self.patches_layout, self.num_patches, self.embed_dim, ) = stem.get_patch_layout(img_size) self.rgbt_stem = rgbt_stem self.depth_stem = depth_stem self.use_pos_embed = pos_embed_fn is not None self.use_type_embed = use_type_embed self.num_cls_tokens = num_cls_tokens if self.use_pos_embed: self.pos_embedding_helper = pos_embed_fn( patches_layout=self.patches_layout, num_cls_tokens=num_cls_tokens, num_patches=self.num_patches, embed_dim=self.embed_dim, ) if self.num_cls_tokens > 0: self.cls_token = nn.Parameter( torch.zeros(1, self.num_cls_tokens, self.embed_dim) ) if self.use_type_embed: self.type_embed = nn.Parameter(torch.zeros(1, 1, self.embed_dim)) self.init_parameters(init_param_style) @torch.no_grad() def init_parameters(self, init_param_style): if init_param_style == "openclip": # OpenCLIP style initialization scale = self.embed_dim**-0.5 if self.use_pos_embed: nn.init.normal_(self.pos_embedding_helper.pos_embed) self.pos_embedding_helper.pos_embed *= scale if self.num_cls_tokens > 0: nn.init.normal_(self.cls_token) self.cls_token *= scale elif init_param_style == "vit": self.cls_token.data.fill_(0) else: raise ValueError(f"Unknown init {init_param_style}") if self.use_type_embed: nn.init.normal_(self.type_embed) def tokenize_input_and_cls_pos(self, input, stem, mask): # tokens is of shape B x L x D tokens = stem(input) assert tokens.ndim == 3 assert tokens.shape[2] == self.embed_dim B = tokens.shape[0] if self.num_cls_tokens > 0: class_tokens = self.cls_token.expand( B, -1, -1 ) # stole class_tokens impl from Phil Wang, thanks tokens = torch.cat((class_tokens, tokens), dim=1) if self.use_pos_embed: pos_embed = self.pos_embedding_helper.get_pos_embedding(input, tokens) tokens = tokens + pos_embed if self.use_type_embed: tokens = tokens + self.type_embed.expand(B, -1, -1) return tokens def forward(self, vision=None, depth=None, patch_mask=None): if patch_mask is not None: raise NotImplementedError() if vision is not None: vision_tokens = self.tokenize_input_and_cls_pos( vision, self.rgbt_stem, patch_mask ) if depth is not None: depth_tokens = self.tokenize_input_and_cls_pos( depth, self.depth_stem, patch_mask ) # aggregate tokens if vision is not None and depth is not None: final_tokens = vision_tokens + depth_tokens else: final_tokens = vision_tokens if vision is not None else depth_tokens return_dict = { "trunk": { "tokens": final_tokens, }, "head": {}, } return return_dict class AudioPreprocessor(RGBDTPreprocessor): def __init__(self, audio_stem: PatchEmbedGeneric, **kwargs) -> None: super().__init__(rgbt_stem=audio_stem, depth_stem=None, **kwargs) def forward(self, audio=None): return super().forward(vision=audio) class ThermalPreprocessor(RGBDTPreprocessor): def __init__(self, thermal_stem: PatchEmbedGeneric, **kwargs) -> None: super().__init__(rgbt_stem=thermal_stem, depth_stem=None, **kwargs) def forward(self, thermal=None): return super().forward(vision=thermal) def build_causal_attention_mask(context_length): # lazily create causal attention mask, with full attention between the vision tokens # pytorch uses additive attention mask; fill with -inf mask = torch.empty(context_length, context_length, requires_grad=False) mask.fill_(float("-inf")) mask.triu_(1) # zero out the lower diagonal return mask class TextPreprocessor(VerboseNNModule): def __init__( self, vocab_size: int, context_length: int, embed_dim: int, causal_masking: bool, supply_seq_len_to_head: bool = True, num_cls_tokens: int = 0, init_param_style: str = "openclip", ) -> None: super().__init__() self.vocab_size = vocab_size self.context_length = context_length self.token_embedding = nn.Embedding(vocab_size, embed_dim) self.pos_embed = nn.Parameter( torch.empty(1, self.context_length + num_cls_tokens, embed_dim) ) self.causal_masking = causal_masking if self.causal_masking: mask = build_causal_attention_mask(self.context_length) # register the mask as a buffer so it can be moved to the right device self.register_buffer("mask", mask) self.supply_seq_len_to_head = supply_seq_len_to_head self.num_cls_tokens = num_cls_tokens self.embed_dim = embed_dim if num_cls_tokens > 0: assert self.causal_masking is False, "Masking + CLS token isn't implemented" self.cls_token = nn.Parameter( torch.zeros(1, self.num_cls_tokens, embed_dim) ) self.init_parameters(init_param_style) @torch.no_grad() def init_parameters(self, init_param_style="openclip"): # OpenCLIP style initialization nn.init.normal_(self.token_embedding.weight, std=0.02) nn.init.normal_(self.pos_embed, std=0.01) if init_param_style == "openclip": # OpenCLIP style initialization scale = self.embed_dim**-0.5 if self.num_cls_tokens > 0: nn.init.normal_(self.cls_token) self.cls_token *= scale elif init_param_style == "vit": self.cls_token.data.fill_(0) else: raise ValueError(f"Unknown init {init_param_style}") def forward(self, text): # text tokens are of shape B x L x D text_tokens = self.token_embedding(text) # concat CLS tokens if any if self.num_cls_tokens > 0: B = text_tokens.shape[0] class_tokens = self.cls_token.expand( B, -1, -1 ) # stole class_tokens impl from Phil Wang, thanks text_tokens = torch.cat((class_tokens, text_tokens), dim=1) text_tokens = text_tokens + self.pos_embed return_dict = { "trunk": { "tokens": text_tokens, }, "head": {}, } # Compute sequence length after adding CLS tokens if self.supply_seq_len_to_head: text_lengths = text.argmax(dim=-1) return_dict["head"] = { "seq_len": text_lengths, } if self.causal_masking: return_dict["trunk"].update({"attn_mask": self.mask}) return return_dict class Im2Video(nn.Module): """Convert an image into a trivial video.""" def __init__(self, time_dim=2): super().__init__() self.time_dim = time_dim def forward(self, x): if x.ndim == 4: # B, C, H, W -> B, C, T, H, W return x.unsqueeze(self.time_dim) elif x.ndim == 5: return x else: raise ValueError(f"Dimension incorrect {x.shape}") class PadIm2Video(Im2Video): def __init__(self, ntimes, pad_type, time_dim=2): super().__init__(time_dim=time_dim) assert ntimes > 0 assert pad_type in ["zero", "repeat"] self.ntimes = ntimes self.pad_type = pad_type def forward(self, x): x = super().forward(x) if x.shape[self.time_dim] == 1: if self.pad_type == "repeat": new_shape = [1] * len(x.shape) new_shape[self.time_dim] = self.ntimes x = x.repeat(new_shape) elif self.pad_type == "zero": padarg = [0, 0] * len(x.shape) padarg[2 * self.time_dim + 1] = self.ntimes - x.shape[self.time_dim] x = nn.functional.pad(x, padarg) return x # Modified from github.com/openai/CLIP @lru_cache() def bytes_to_unicode(): """ Returns list of utf-8 byte and a corresponding list of unicode strings. The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. This is a signficant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup tables between utf-8 bytes and unicode strings. And avoids mapping to whitespace/control characters the bpe code barfs on. """ bs = ( list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1)) ) cs = bs[:] n = 0 for b in range(2**8): if b not in bs: bs.append(b) cs.append(2**8 + n) n += 1 cs = [chr(n) for n in cs] return dict(zip(bs, cs)) def get_pairs(word): """Return set of symbol pairs in a word. Word is represented as tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip() def whitespace_clean(text): text = re.sub(r"\s+", " ", text) text = text.strip() return text class SimpleTokenizer(object): def __init__(self, bpe_path: str, context_length=77): self.byte_encoder = bytes_to_unicode() self.byte_decoder = {v: k for k, v in self.byte_encoder.items()} with g_pathmgr.open(bpe_path, "rb") as fh: bpe_bytes = io.BytesIO(fh.read()) merges = gzip.open(bpe_bytes).read().decode("utf-8").split("\n") merges = merges[1 : 49152 - 256 - 2 + 1] merges = [tuple(merge.split()) for merge in merges] vocab = list(bytes_to_unicode().values()) vocab = vocab + [v + "</w>" for v in vocab] for merge in merges: vocab.append("".join(merge)) vocab.extend(["<|startoftext|>", "<|endoftext|>"]) self.encoder = dict(zip(vocab, range(len(vocab)))) self.decoder = {v: k for k, v in self.encoder.items()} self.bpe_ranks = dict(zip(merges, range(len(merges)))) self.cache = { "<|startoftext|>": "<|startoftext|>", "<|endoftext|>": "<|endoftext|>", } self.pat = re.compile( r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""", re.IGNORECASE, ) self.context_length = context_length def bpe(self, token): if token in self.cache: return self.cache[token] word = tuple(token[:-1]) + (token[-1] + "</w>",) pairs = get_pairs(word) if not pairs: return token + "</w>" while True: bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf"))) if bigram not in self.bpe_ranks: break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word if len(word) == 1: break else: pairs = get_pairs(word) word = " ".join(word) self.cache[token] = word return word def encode(self, text): bpe_tokens = [] text = whitespace_clean(basic_clean(text)).lower() for token in re.findall(self.pat, text): token = "".join(self.byte_encoder[b] for b in token.encode("utf-8")) bpe_tokens.extend( self.encoder[bpe_token] for bpe_token in self.bpe(token).split(" ") ) return bpe_tokens def decode(self, tokens): text = "".join([self.decoder[token] for token in tokens]) text = ( bytearray([self.byte_decoder[c] for c in text]) .decode("utf-8", errors="replace") .replace("</w>", " ") ) return text def __call__(self, texts, context_length=None): if not context_length: context_length = self.context_length if isinstance(texts, str): texts = [texts] sot_token = self.encoder["<|startoftext|>"] eot_token = self.encoder["<|endoftext|>"] all_tokens = [[sot_token] + self.encode(text) + [eot_token] for text in texts] result = torch.zeros(len(all_tokens), context_length, dtype=torch.long) for i, tokens in enumerate(all_tokens): tokens = tokens[:context_length] result[i, : len(tokens)] = torch.tensor(tokens) if len(result) == 1: return result[0] return result class IMUPreprocessor(VerboseNNModule): def __init__( self, kernel_size: int, imu_stem: PatchEmbedGeneric, embed_dim: int, img_size: List = (6, 2000), num_cls_tokens: int = 1, pos_embed_fn: Callable = None, init_param_style: str = "openclip", ) -> None: super().__init__() self.imu_stem = imu_stem self.embed_dim = embed_dim self.use_pos_embed = pos_embed_fn is not None self.num_cls_tokens = num_cls_tokens self.kernel_size = kernel_size self.pos_embed = nn.Parameter( torch.empty(1, (img_size[1] // kernel_size) + num_cls_tokens, embed_dim) ) if self.num_cls_tokens > 0: self.cls_token = nn.Parameter( torch.zeros(1, self.num_cls_tokens, self.embed_dim) ) self.init_parameters(init_param_style) @torch.no_grad() def init_parameters(self, init_param_style): nn.init.normal_(self.pos_embed, std=0.01) if init_param_style == "openclip": # OpenCLIP style initialization scale = self.embed_dim**-0.5 if self.num_cls_tokens > 0: nn.init.normal_(self.cls_token) self.cls_token *= scale elif init_param_style == "vit": self.cls_token.data.fill_(0) else: raise ValueError(f"Unknown init {init_param_style}") def tokenize_input_and_cls_pos(self, input, stem): # tokens is of shape B x L x D tokens = stem.norm_layer(stem.proj(input)) assert tokens.ndim == 3 assert tokens.shape[2] == self.embed_dim B = tokens.shape[0] if self.num_cls_tokens > 0: class_tokens = self.cls_token.expand( B, -1, -1 ) # stole class_tokens impl from Phil Wang, thanks tokens = torch.cat((class_tokens, tokens), dim=1) if self.use_pos_embed: tokens = tokens + self.pos_embed return tokens def forward(self, imu): # Patchify imu = imu.unfold( -1, self.kernel_size, self.kernel_size, ).permute(0, 2, 1, 3) imu = imu.reshape(imu.size(0), imu.size(1), -1) imu_tokens = self.tokenize_input_and_cls_pos( imu, self.imu_stem, ) return_dict = { "trunk": { "tokens": imu_tokens, }, "head": {}, } return return_dict
Pegasus-master
pegasus/ImageBind/models/multimodal_preprocessors.py
#!/usr/bin/env python3 # Portions Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import einops import numpy as np import torch import torch.nn as nn class Normalize(nn.Module): def __init__(self, dim: int) -> None: super().__init__() self.dim = dim def forward(self, x): return torch.nn.functional.normalize(x, dim=self.dim, p=2) class LearnableLogitScaling(nn.Module): def __init__( self, logit_scale_init: float = 1 / 0.07, learnable: bool = True, max_logit_scale: float = 100, ) -> None: super().__init__() self.max_logit_scale = max_logit_scale self.logit_scale_init = logit_scale_init self.learnable = learnable log_logit_scale = torch.ones([]) * np.log(self.logit_scale_init) if learnable: self.log_logit_scale = nn.Parameter(log_logit_scale) else: self.register_buffer("log_logit_scale", log_logit_scale) def forward(self, x): return torch.clip(self.log_logit_scale.exp(), max=self.max_logit_scale) * x def extra_repr(self): st = f"logit_scale_init={self.logit_scale_init},learnable={self.learnable}, max_logit_scale={self.max_logit_scale}" return st class EinOpsRearrange(nn.Module): def __init__(self, rearrange_expr: str, **kwargs) -> None: super().__init__() self.rearrange_expr = rearrange_expr self.kwargs = kwargs def forward(self, x): assert isinstance(x, torch.Tensor) return einops.rearrange(x, self.rearrange_expr, **self.kwargs) class VerboseNNModule(nn.Module): """ Wrapper around nn.Module that prints registered buffers and parameter names. """ @staticmethod def get_readable_tensor_repr(name: str, tensor: torch.Tensor) -> str: st = ( "(" + name + "): " + "tensor(" + str(tuple(tensor[1].shape)) + ", requires_grad=" + str(tensor[1].requires_grad) + ")\n" ) return st def extra_repr(self) -> str: named_modules = set() for p in self.named_modules(): named_modules.update([p[0]]) named_modules = list(named_modules) string_repr = "" for p in self.named_parameters(): name = p[0].split(".")[0] if name not in named_modules: string_repr += self.get_readable_tensor_repr(name, p) for p in self.named_buffers(): name = p[0].split(".")[0] string_repr += self.get_readable_tensor_repr(name, p) return string_repr def cast_if_src_dtype( tensor: torch.Tensor, src_dtype: torch.dtype, tgt_dtype: torch.dtype ): updated = False if tensor.dtype == src_dtype: tensor = tensor.to(dtype=tgt_dtype) updated = True return tensor, updated class QuickGELU(nn.Module): # From https://github.com/openai/CLIP/blob/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1/clip/model.py#L166 def forward(self, x: torch.Tensor): return x * torch.sigmoid(1.702 * x) class SelectElement(nn.Module): def __init__(self, index) -> None: super().__init__() self.index = index def forward(self, x): assert x.ndim >= 3 return x[:, self.index, ...] class SelectEOSAndProject(nn.Module): """ Text Pooling used in OpenCLIP """ def __init__(self, proj: nn.Module) -> None: super().__init__() self.proj = proj def forward(self, x, seq_len): assert x.ndim == 3 # x is of shape B x L x D # take features from the eot embedding (eot_token is the highest number in each sequence) x = x[torch.arange(x.shape[0]), seq_len] x = self.proj(x) return x
Pegasus-master
pegasus/ImageBind/models/helpers.py
from setuptools import setup, find_packages setup( name = 'the-compiler', packages = find_packages(exclude=[]), version = '0.0.6', license='MIT', description = 'The Compiler ', author = 'Kye Gomez', author_email = '[email protected]', long_description_content_type = 'text/markdown', url = 'https://github.com/kyegomez/the-compiler', keywords = [ 'artificial intelligence', 'deep learning', 'optimizers', "Prompt Engineering" ], install_requires=[ "swarms" ], classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Topic :: Scientific/Engineering :: Artificial Intelligence', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3.6', ], )
the-compiler-main
setup.py
from the_compiler import TheCompiler api_key = "" # Your OpenAI API key create = "a simple calculator program" compiler = TheCompiler(api_key) code = compiler.run(create) print("Generated Code:\n", code)
the-compiler-main
example.py
the-compiler-main
the_compiler/__init__.py
from swarms import Swarms class Architect: def __init__(self, create, api_key): self.create = create self.boss_node = boss_node(openai_api_key=api_key) def generate_architecture(self): objective = f""" Create an architectural analysis specification in markdown in the most optimal programming language for {self.create}, provide the fastest, reliable architecture, and then break down that architecture into classes and algorithms needed to create {self.create} """ task = self.boss_node.create_task(objective=objective) return self.boss_node.execute(task) class CodeGenerator: def __init__(self, boss1, create, api_key, unit_tests): self.boss1 = boss1 self.create = create self.unit_tests = unit_tests self.boss_node = boss_node(openai_api_key=api_key) def generate_code(self): objective = f""" Generate a Python program that meets the following product specification: {self.boss1} to create: {self.create}. Use the following unit tests as an evaluation score: {self.unit_tests}. """ task = self.boss_node.create_task(objective=objective) return self.boss_node.execute(task) class TestCreator: def __init__(self, boss1, api_key): self.boss1 = boss1 self.boss_node = boss_node(openai_api_key=api_key) def generate_tests(self): objective = f""" Generate a suite of unit tests for a Python program that meets the following product specification: {self.boss1} """ task = self.boss_node.create_task(objective=objective) return self.boss_node.execute(task) class TheCompiler: def __init__(self, api_key): if not api_key: raise ValueError("API key is required") self.swarms = Swarms(api_key=api_key) def run(self, create): if not create: raise ValueError("You need to specify what to create") try: architecture = self.swarms.run_swarms( objective=f"Create an architectural analysis specification in markdown in the most optimal programming language for {create}, provide the fastest, reliable architecture, and then break down that architecture into classes and algorithms needed to create {create}" ) unit_tests = self.swarms.run_swarms( objective=f"Generate a suite of unit tests for a Python program that meets the following product specification: {architecture}" ) generate_code = self.swarms.run_swarms( objective=f"Generate a Python program that meets the following product specification: {architecture} to create: {create}. Use the following unit tests as an evaluation score: {unit_tests}." ) except Exception as e: raise RuntimeError("An error occurred while generating the code") from e return generate_code # api_key = "" # Your OpenAI API key # create = "a simple calculator program" # compiler = TheCompiler(api_key) # code = compiler.run(create) # print("Generated Code:\n", code) # class TheCompiler(Swarms): # def __init__(self, api_key): # super().__init__(api_key=api_key) # def _generate_architecture(self, create): # objective = f""" # Create an architectural analysis specification in markdown in the most optimal programming language for {create}, provide the fastest, reliable architecture, and then break down that architecture into classes and algorithms needed to create {create} # """ # return self.run_swarms(objective=objective) # def _generate_tests(self, boss1): # objective = f""" # Generate a suite of unit tests for a Python program that meets the following product specification: {boss1} # """ # return self.run_swarms(objective=objective) # def _generate_code(self, boss1, create, unit_tests): # objective = f""" # Generate a Python program that meets the following product specification: {boss1} to create: {create}. Use the following unit tests as an evaluation score: {unit_tests}. # """ # return self.run_swarms(objective=objective) # def run(self, create): # architecture = self._generate_architecture(create) # unit_tests = self._generate_tests(architecture) # code = self._generate_code(architecture, create, unit_tests) # return code, unit_tests # class TheCompiler: # def __init__(self, create, api_key): # self.create = create # self.api_key = api_key # def generate_code(self): # architect = Architect(self.create, self.api_key) # architecture = architect.generate_architecture() # test_creator = TestCreator(architecture, self.api_key) # unit_tests = test_creator.generate_tests() # code_generator = CodeGenerator(architecture, self.create, self.api_key, unit_tests) # code = code_generator.generate_code() # return code, unit_tests # # Sample usage # openai_api_key = os.environ['OPENAI_API_KEY'] = 'api key here' # Replace with your OpenAI API key # compiler = TheCompiler(create="an e-commerce website", api_key=openai_api_key) # code, unit_tests = compiler.generate_code() # create = "What do you want to create?" # architect_prompt = f""" # Create an architectural analysis specification in markdown in the most optimal programming language for {create}, provide the fastest, reliable architecture, and then break down that architecture into classes and algorithms needed to create {create} # """ # objective = f"{architect_prompt}" # #create a task # task1 = boss_node.create_task(objective=objective) # boss1 = boss_node.execute(task1) # ##### 2nd agent - code generator # generator_prompt = f""" # Generate a Python program that meets the following product specification: {boss1} to create: {create}. Use the following unit tests as an evaluation score: {unit_tests}. # """ # task2 = boss_node.create(objective=f"{generator_prompt}") # boss2 = boss_node.execute(task2) # ############### 3rd agent -- Unit test creator # generator_prompt = f""" # Generate a suite of unit tests for a Python program that meets the following product specification: {boss1} # """ # task3 = boss_node.create(objective=f"{generator_prompt}") # boss3 = boss_node.execute(task2)
the-compiler-main
the_compiler/main.py
import gzip import random import numpy as np import torch import torch.optim as optim import tqdm from torch.utils.data import DataLoader, Dataset from Andromeda.model import Andromeda from Andromeda.core.transformer import Decoder, Transformer from Andromeda.core.autoregressive_wrapper import AutoregressiveWrapper # constants NUM_BATCHES = int(1e5) BATCH_SIZE = 4 GRADIENT_ACCUMULATE_EVERY = 1 LEARNING_RATE = 1e-4 VALIDATE_EVERY = 100 GENERATE_EVERY = 500 GENERATE_LENGTH = 1024 SEQ_LEN = 1024 # helpers def cycle(loader): while True: for data in loader: yield data def decode_token(token): return str(chr(max(32, token))) def decode_tokens(tokens): return ''.join(list(map(decode_token, tokens))) # instantiate GPT-like decoder model model = Andromeda() model.cuda() # prepare enwik8 data with gzip.open('./data/enwik8.gz') as file: data = np.frombuffer(file.read(int(95e6)), dtype=np.uint8).copy() train_x, valid_x = np.split(data, [int(90e6)]) data_train, data_val = torch.from_numpy(train_x), torch.from_numpy(valid_x) class TextSamplerDataset(Dataset): def __init__(self, data, seq_len): super().__init__() self.data = data self.seq_len = seq_len def __getitem__(self, index): rand_start = torch.randint(0, self.data.size(0) - self.seq_len - 1, (1,)) full_seq = self.data[rand_start: rand_start + self.seq_len + 1].long() return full_seq.cuda() def __len__(self): return self.data.size(0) // self.seq_len train_dataset = TextSamplerDataset(data_train, SEQ_LEN) val_dataset = TextSamplerDataset(data_val, SEQ_LEN) train_loader = cycle(DataLoader(train_dataset, batch_size = BATCH_SIZE, drop_last = True)) val_loader = cycle(DataLoader(val_dataset, batch_size = BATCH_SIZE, drop_last = True)) # optimizer optim = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE) # training for i in tqdm.tqdm(range(NUM_BATCHES), mininterval=10., desc='training'): model.train() for __ in range(GRADIENT_ACCUMULATE_EVERY): loss = model(next(train_loader)) (loss / GRADIENT_ACCUMULATE_EVERY).backward() print(f'training loss: {loss.item()}') torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) optim.step() optim.zero_grad() if i % VALIDATE_EVERY == 0: model.eval() with torch.no_grad(): loss = model(next(val_loader)) print(f'validation loss: {loss.item()}') #save the model weights torch.save(model.state_dict(), f"./model_{i}.pth") if i % GENERATE_EVERY == 0: model.eval() inp = random.choice(val_dataset)[:-1] prime = decode_tokens(inp) print('%s \n\n %s', (prime, '*' * 100)) sample = model.generate(inp, GENERATE_LENGTH) output_str = decode_tokens(sample) print(output_str)
Andromeda-master
train_simple.py
import torch from Andromeda.model import Andromeda model = Andromeda().cuda() x = torch.randint(0, 256, (1, 1024)).cuda() model(x) # (1, 1024, 20000)
Andromeda-master
example.py
import math import multiprocessing import os from datetime import timedelta from functools import partial from itertools import chain import torch # import bitsandbytes as bnb from torch.distributed.fsdp import ( FullyShardedDataParallel, MixedPrecision, BackwardPrefetch, ShardingStrategy, ) from accelerate import Accelerator from accelerate.utils import (DummyOptim, InitProcessGroupKwargs) from accelerate.logging import get_logger from datasets import load_dataset from lion_pytorch import Lion from torch.nn import LayerNorm from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import ( CheckpointImpl, apply_activation_checkpointing, checkpoint_wrapper) from torch.distributed.fsdp.wrap import ( transformer_auto_wrap_policy ) from torch.optim import AdamW from torch.utils.data import DataLoader from tqdm import tqdm from transformers import (AutoTokenizer, default_data_collator, get_cosine_schedule_with_warmup, get_linear_schedule_with_warmup, set_seed) from Andromeda.utils.stable_adamw import StableAdamWUnfused from Andromeda.core.transformer import Transformer # from Andromeda.model import Andromeda from Andromeda.configs import Andromeda1Billion ########### SETUP CONFIG import torch.distributed as dist from accelerate.state import AcceleratorState # state = AcceleratorState() logger = get_logger(__name__, log_level="INFO") class CFG: BATCH_SIZE = 1 GRADIENT_ACCUMULATE_EVERY: int = 1 SEED: int = 42 LEARNING_RATE: float = 1e-4 #3e-4 # 1e-4 for lion WEIGHT_DECAY: float = 0.1 SEQ_LEN: int = 8192 NUM_CPU: int = multiprocessing.cpu_count() USE_DEEPSPEED: bool = True USE_FSDP: bool = True USE_PRETOKENIZED: bool = True USE_ACTIVATION_CHECKPOINTING: bool = True RESUME_FROM_CHECKPOINT: str = False CHECKPOINTING_STEPS: int = 1000 OUTPUT_DIR: str = 'checkpoints/' # Folder ENTITY_NAME: str = "Andromeda" LOGGING_STEPS: int = 100 # helpers def print_num_params(model, accelerator: Accelerator): # n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) accelerator.print(f"Number of parameters in model: {n_params}") # activation checkpointing def activation_checkpointing( model: torch.nn.Module, offload_to_cpu: bool = False, accelerator: Accelerator = None, ): """ Apply activation checkpointing to a model. Args: model (Module): The model to which to apply activation checkpointing. offload_to_cpu (bool, optional): Whether to offload the activations to CPU. Defaults to False. accelerator (Accelerator, optional): The Accelerate library accelerator. Defaults to None. """ if accelerator is not None: accelerator.print("Using activation checkpointing") def check_fn(submodule): return isinstance(submodule, Transformer) non_reentrant_wrapper = partial( checkpoint_wrapper, offload_to_cpu=offload_to_cpu, checkpoint_impl=CheckpointImpl.NO_REENTRANT, ) apply_activation_checkpointing( model, checkpoint_wrapper_fn=non_reentrant_wrapper, check_fn=check_fn ) # FSDP def fsdp( model: torch.nn.Module, auto_wrap: bool = False, mp: str = "fp32", shard_strat: str = "NO_SHARD", ): """ This function wraps a given PyTorch model with the FullyShardedDataParallel (FSDP) wrapper to enable efficient data parallelism and model sharding. Args: model (torch.nn.Module): The original PyTorch model to be wrapped with FSDP. auto_wrap (bool, optional): If True, it enables automatic wrapping of the model's layers according to the transformer_auto_wrap_policy. Default is False. mp (str, optional): The mixed precision mode to be used. Can be 'bf16' for BFloat16, 'fp16' for Float16 or 'fp32' for Float32 precision. Default is 'fp32'. shard_strat (str, optional): The sharding strategy to be used. Can be 'SHARD_GRAD' for sharding at gradient computation, 'FULL_SHARD' for full model sharding or 'NO_SHARD' for no sharding. Default is 'NO_SHARD'. Raises: ValueError: If the provided mp (mixed precision mode) is not 'bf16', 'fp16' or 'fp32'. ValueError: If the provided shard_strat (sharding strategy) is not 'SHARD_GRAD', 'FULL_SHARD' or 'NO_SHARD'. Returns: torch.nn.Module: The input model wrapped with FSDP. """ if auto_wrap: Andromeda_auto_wrap_policy = partial( transformer_auto_wrap_policy, transformer_layer_cls={ Transformer, }, ) else: Andromeda_auto_wrap_policy = None if mp == "bf16": mp_fsdp = MixedPrecision( param_dtype=torch.bfloat16, # Gradient communication precision. reduce_dtype=torch.bfloat16, # Buffer precision. buffer_dtype=torch.bfloat16, ) elif mp == "fp16": mp_fsdp = MixedPrecision( param_dtype=torch.float16, # Gradient communication precision. reduce_dtype=torch.float16, # Buffer precision. buffer_dtype=torch.float16, ) elif mp == "fp32": mp_fsdp = MixedPrecision( param_dtype=torch.float32, # Gradient communication precision. reduce_dtype=torch.float32, # Buffer precision. buffer_dtype=torch.float32, ) else: raise ValueError( "Invalid scheduler_type. Expected 'bf16', 'fp16' or 'fp32', got: {}".format( mp ) ) if shard_strat == "SHARD_GRAD": sharding_strat_fsdp = ShardingStrategy.SHARD_GRAD_OP elif shard_strat == "FULL_SHARD": sharding_strat_fsdp = ShardingStrategy.FULL_SHARD elif shard_strat == "NO_SHARD": sharding_strat_fsdp = ShardingStrategy.NO_SHARD else: raise ValueError( "Invalid scheduler_type. Expected 'SHARD_GRAD', 'FULL_SHARD' or 'NO_SHARD', got: {}".format( shard_strat ) ) model = FullyShardedDataParallel( model, auto_wrap_policy=Andromeda_auto_wrap_policy, mixed_precision=mp_fsdp, backward_prefetch=BackwardPrefetch.BACKWARD_PRE, sharding_strategy=sharding_strat_fsdp, forward_prefetch=True, use_orig_params=True, ) return model # learning rate scheduler def get_lr_scheduler_with_warmup( optimizer: torch.optim.Optimizer, scheduler_type: str, num_warmup_steps: int, max_train_steps: int, grad_accumulate_every: int = 1, accelerator: Accelerator = None, ): """ Get a learning rate scheduler with warmup. Args: optimizer (Optimizer): The optimizer for which to create the learning rate scheduler. scheduler_type (str): The type of learning rate scheduler to create, either "linear" or "cosine". num_warmup_steps (int): The number of warmup steps for the learning rate scheduler. max_train_steps (int): The maximum number of training steps. grad_accumulate_every (int, optional): The gradient accumulation factor. Defaults to 1. accelerator (Accelerator, optional): The Accelerate library accelerator. Defaults to None. Returns: The learning rate scheduler with warmup. Raises: ValueError: If scheduler_type is not "linear" or "cosine". """ NUM_WARMUP_STEPS = num_warmup_steps GRADIENT_ACCUMULATE_EVERY = grad_accumulate_every if accelerator is not None: accelerator.print(f"Using {scheduler_type} lr scheduler") if scheduler_type == "linear": return get_linear_schedule_with_warmup( optimizer=optimizer, num_warmup_steps=NUM_WARMUP_STEPS * GRADIENT_ACCUMULATE_EVERY, num_training_steps=max_train_steps * GRADIENT_ACCUMULATE_EVERY, ) elif scheduler_type == "cosine": return get_cosine_schedule_with_warmup( optimizer=optimizer, num_warmup_steps=NUM_WARMUP_STEPS * GRADIENT_ACCUMULATE_EVERY, num_training_steps=max_train_steps * GRADIENT_ACCUMULATE_EVERY, ) else: raise ValueError( "Invalid scheduler_type. Expected 'linear' or 'cosine', got: {}".format( scheduler_type ) ) # optimizers def decoupled_optimizer( model: torch.nn.Module, learning_rate: float, weight_decay: float, beta_1: float, beta_2: float, optimizer_type: str, use_fsdp: bool = True, accelerator: Accelerator = None, ): """ Decouples the optimizer from the training process. This function sets up the optimizer for the model by creating two groups of parameters: one for weight decay and one without weight decay. Then, it initializes the optimizer with these two groups of parameters. Args: model (Module): The model whose parameters are optimized. learning_rate (float): The learning rate for the optimizer. weight_decay (float): The weight decay for the optimizer. beta_1 (float): The exponential decay rate for the 1st moment estimates. beta_2 (float): The exponential decay rate for the 2nd moment estimates. optimizer_type (str): The type of the optimizer. Can be 'lion', 'adamw', or 'stable_adamw'. use_fsdp (bool, optional): If True, the optimizer will work with fully sharded data parallelism. Defaults to True. accelerator (Accelerator, optional): The accelerator from HuggingFace's Accelerate library. Defaults to None. Returns: Optimizer: The initialized optimizer. Raises: ValueError: If the optimizer type is not 'lion', 'adamw' or 'stable_adamw'. """ accelerator.print(f"Using {optimizer_type} optimizer") # Create an empty dictionary called param_dict to store the model's named parameters. param_dict = {} # Iterate over the model's named parameters and populate the param_dict with key-value pairs. for param_name, param in model.named_parameters(): param_dict[param_name] = param # Separate the model's named modules into two groups: decay and no_decay. # Create an empty list to store the names of the LayerNorm and Embedding layer weights with no weight decay. no_decay = [] if use_fsdp: exclude_module = "_fsdp_wrapped_module.token_emb" else: exclude_module = "token_emb" # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of any of the desired types (LayerNorm or torch.nn.Embedding). for ndim in [LayerNorm, torch.nn.Embedding]: if isinstance(module, ndim): # If torch.nn.Embedding, append its name with a ".weight" suffix to the no_decay list. if module_name == exclude_module: no_decay.append(f"{module_name}.weight") else: # If the module is an instance of LayerNorm no_decay.append(f"{module_name}.gamma") # Exit the inner loop since the desired module has been found. break # Create an empty list to store the names of the Linear layer weights with weight decay. decay = [] # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of the desired type (torch.nn.Linear). for ndim in [torch.nn.Linear]: if isinstance(module, ndim): # If the module is an instance of torch.nn.Linear, append its name with a ".weight" suffix to the decay list. decay.append(f"{module_name}.weight") # Exit the inner loop since the desired module has been found. break # Create two separate lists of model parameters: decay_param and no_decay_param. # The decay_param list contains the parameters that should have weight decay applied. # The no_decay_param list contains the parameters that should not have weight decay applied, excluding the 'to_logits.weight' parameter. # Create an empty list called decay_param to store the parameters with weight decay. decay_param = [] if use_fsdp: exclude_param = "_fsdp_wrapped_module.to_logits.weight" else: exclude_param = "to_logits.weight" # Iterate over the decay list, which contains the names of the parameters with weight decay. for param in decay: # Check if the current parameter is not 'to_logits.weight'. # Append the corresponding parameter from param_dict to the decay_param list. if param != exclude_param: decay_param.append(param_dict[param]) # Create an empty list called no_decay_param to store the parameters without weight decay. no_decay_param = [] # Iterate over the no_decay list, which contains the names of the parameters without weight decay. for param in no_decay: try: # Append the corresponding parameter from param_dict to the no_decay_param list. no_decay_param.append(param_dict[param]) except KeyError: # print(f"Parameter {param_name} does not exist in the model") pass # Create a list called grouped_params that contains two dictionaries. # The first dictionary has the decay_param list and the corresponding weight_decay value. # The second dictionary has the no_decay_param list and a weight_decay value of 0.0. grouped_params = [ {"params": decay_param, "weight_decay": weight_decay}, {"params": no_decay_param, "weight_decay": 0.0}, ] # Create a variable called optimizer that stores an instance of the optimizer. if optimizer_type == "lion": optimizer = Lion(grouped_params, lr=learning_rate, betas=(beta_1, beta_2),) elif optimizer_type == "adamw": optimizer = AdamW(grouped_params, lr=learning_rate, betas=(beta_1, beta_2),) elif optimizer_type == "deepspeed": optimizer = DummyOptim(grouped_params, lr=learning_rate, betas=(beta_1, beta_2),) elif optimizer_type == "stable_adamw": optimizer = StableAdamWUnfused( grouped_params, lr=learning_rate, betas=(beta_1, beta_2), ) # elif optimizer_type=="Adam8bit": # optimizer = bnb.optim.Adam8bit(grouped_params, lr=learning_rate, betas=(beta_1, beta_2)) # elif optimizer_type=="Lion8Bit": # optimizer = bnb.optim.Lion8bit(grouped_params, lr=learning_rate, betas=(beta_1, beta_2)) else: raise ValueError( "Invalid optimizer_type. Expected 'lion', 'adamw', 'deepspeed' or 'stable_adamw', got: {}".format( optimizer_type ) ) # Return the optimizer. return optimizer # dataloaders def build_dataloaders(): """ Build data loaders for training. This function performs the following steps: 1. Load the tokenizer from the pretrained "EleutherAI/gpt-neox-20b" model. 2. Load the "openwebtext" dataset. 3. Tokenize the dataset, adding the end-of-sentence token to each text. 4. Process the tokenized dataset into chunks of a specified block size. Returns: Dataset: The processed dataset ready for training. """ tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b") dataset = load_dataset("openwebtext", split="train") tokenized_dataset = dataset.map( lambda example: tokenizer([t + tokenizer.eos_token for t in example["text"]]), batched=True, num_proc=CFG.NUM_CPU, remove_columns=["text"], ) block_size = CFG.SEQ_LEN # Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size. def group_texts(examples): # Concatenate all texts. concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can # customize this part to your needs. if total_length >= block_size: total_length = (total_length // block_size) * block_size # Split by chunks of max_len. result = { k: [t[i : i + block_size] for i in range(0, total_length, block_size)] for k, t in concatenated_examples.items() } return result train_dataset = tokenized_dataset.map( group_texts, batched=True, num_proc=CFG.NUM_CPU, ) return train_dataset #switch to falconwebdataset def build_pre_tokenized(): d0 = load_dataset("conceptofmind/c4_0-to-20_neox_with_eos_8k", split="train[:10]") # d1 = load_dataset("conceptofmind/c4_21-to-40_neox_with_eos_8k", split="train") # d2 = load_dataset("conceptofmind/c4_41-to-60_neox_with_eos_8k", split="train") # d3 = load_dataset("conceptofmind/c4_61-to-80_neox_with_eos_8k", split="train") # d4 = load_dataset("conceptofmind/c4_81-to-100_neox_with_eos_8k", split="train") # train_dataset = concatenate_datasets([d0, d1, d2, d3, d4]) return d0 def Train(): # accelerator timeout = InitProcessGroupKwargs(timeout=timedelta(seconds=1_000_000)) accelerator = Accelerator( gradient_accumulation_steps=CFG.GRADIENT_ACCUMULATE_EVERY, mixed_precision="fp16", log_with="wandb", kwargs_handlers=[timeout], ) state = AcceleratorState() state.deepspeed_plugin.deepspeed_config['train_micro_batch_size_per_gpu'] = CFG.BATCH_SIZE #?????? accelerator.init_trackers( project_name="Andromeda", config={ "batch_size": CFG.BATCH_SIZE, "gradient_accumulate_every": CFG.GRADIENT_ACCUMULATE_EVERY, "learning_rate": CFG.LEARNING_RATE, "seq_len": CFG.SEQ_LEN, }, # init_kwargs={"wandb": {"entity": CFG.ENTITY_NAME}}, ) accelerator.print(f"Total GPUS: {accelerator.num_processes}") # set seed set_seed(CFG.SEED) # model = Andromeda( # num_tokens=50432, # max_seq_len=8192, # dim=3072, # depth=24, # dim_head=128, # heads=12, # use_abs_pos_emb=False, # alibi_pos_bias=True, # alibi_num_heads=6, # rotary_xpos=True, # attn_flash=True, # shift_tokens=1, # attn_one_kv_head=True, # qk_norm=True, # attn_qk_norm=True, # attn_qk_norm_dim_scale=True, # embedding_provider=AndromedaEmbedding() # ) model = Andromeda1Billion() print_num_params(model, accelerator) if CFG.USE_FSDP: model = fsdp( model, mp="fp16", shard_strat="SHARD_GRAD" ) if CFG.USE_ACTIVATION_CHECKPOINTING: activation_checkpointing(model, accelerator) model = accelerator.prepare(model) # dataloaders if CFG.USE_PRETOKENIZED: train_dataset = build_pre_tokenized() else: train_dataset = build_dataloaders() train_loader = DataLoader( train_dataset, batch_size=CFG.BATCH_SIZE, collate_fn=default_data_collator, ) # optimizer optim = decoupled_optimizer( model=model, learning_rate=CFG.LEARNING_RATE, weight_decay=CFG.WEIGHT_DECAY, beta_1=0.90, beta_2=0.95, optimizer_type='lion', use_fsdp=True, accelerator=accelerator ) # Determine number of training steps max_train_steps = math.ceil(len(train_loader) / CFG.GRADIENT_ACCUMULATE_EVERY) accelerator.print(f"Max train steps: {max_train_steps}") # lr scheduler NUM_WARMUP_STEPS = int(max_train_steps * 0.01) accelerator.print(f"Num warmup steps: {NUM_WARMUP_STEPS}") # if False: # if CFG.USE_DEEPSPEED: # lr_scheduler = DummyScheduler( # optim, # total_num_steps=max_train_steps * accelerator.num_processes, # warmup_num_steps=NUM_WARMUP_STEPS # ) # else: lr_scheduler = get_lr_scheduler_with_warmup( optimizer=optim, scheduler_type="cosine", num_warmup_steps=NUM_WARMUP_STEPS, max_train_steps=max_train_steps, grad_accumulate_every=CFG.GRADIENT_ACCUMULATE_EVERY, ) # prepare optim, train_loader, lr_scheduler = accelerator.prepare( optim, train_loader, lr_scheduler ) # checkpoint scheduler accelerator.register_for_checkpointing(lr_scheduler) # I do not know why Huggingface recommends recalculation of max_train_steps max_train_steps = math.ceil(len(train_loader) / CFG.GRADIENT_ACCUMULATE_EVERY) accelerator.print(f"Max train steps recalculated: {max_train_steps}") # Total batch size for logging total_batch_size = ( CFG.BATCH_SIZE * accelerator.num_processes * CFG.GRADIENT_ACCUMULATE_EVERY ) accelerator.print(f"Total batch size: {total_batch_size}") # resume training progress_bar = tqdm( range(max_train_steps), disable=not accelerator.is_local_main_process ) completed_steps = 0 if CFG.RESUME_FROM_CHECKPOINT: if CFG.RESUME_FROM_CHECKPOINT is not None or CFG.RESUME_FROM_CHECKPOINT != "": accelerator.print(f"Resuming from checkpoint {CFG.RESUME_FROM_CHECKPOINT}") accelerator.load_state(CFG.RESUME_FROM_CHECKPOINT) path = os.path.basename(CFG.RESUME_FROM_CHECKPOINT) training_difference = os.path.splitext(path)[0] # need to multiply `gradient_accumulation_steps` to reflect real steps resume_step = ( int(training_difference.replace("step_", "")) * CFG.GRADIENT_ACCUMULATE_EVERY ) if CFG.RESUME_FROM_CHECKPOINT and resume_step is not None: train_loader = accelerator.skip_first_batches(train_loader, resume_step) completed_steps += resume_step progress_bar.update(resume_step) # training model.train() for step, batch in enumerate(train_loader): with accelerator.accumulate(model): inputs = batch["input_ids"].to(accelerator.device) loss = model(inputs, return_loss=True) accelerator.backward(loss) accelerator.log({"loss": loss.item()}, step=step) if accelerator.sync_gradients: accelerator.clip_grad_norm_(model.parameters(), 1.0) optim.step() lr_scheduler.step() optim.zero_grad() if accelerator.sync_gradients: progress_bar.update(1) completed_steps += 1 if isinstance(CFG.CHECKPOINTING_STEPS, int): if completed_steps % CFG.CHECKPOINTING_STEPS == 0: output_dir = f"step_{completed_steps }" if CFG.OUTPUT_DIR is not None: output_dir = os.path.join(CFG.OUTPUT_DIR, output_dir) accelerator.save_state(output_dir) if completed_steps >= max_train_steps: break #logging every CFG.LOGGING STEPS if CFG.LOGGING_STEPS > 0 and step % CFG.LOGGING_STEPS == 0: logger.info( f"Step: {completed_steps}/{max_train_steps}, Loss: {loss.item():.5f}" ) # end training # accelerator.print(f"Training Finished") accelerator.end_training() # save final model # accelerator.print(f"Saving model to {CFG.OUTPUT_DIR}") if CFG.OUTPUT_DIR is not None: accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) with accelerator.main_process_first(): accelerator.save( unwrapped_model.state_dict(), f"{CFG.OUTPUT_DIR}/final/final_model.pt" ) def main(): os.environ['MASTER_ADDR'] #'localhost' os.environ['MASTER_PORT'] #= '9994' # # [CRITICAL] Pay attention to this when scaling to multiple GPUs and clusters # # Pay attention to this, use "accelerate config" os.environ['RANK'] #= str(0) # Number of nodes (servers) os.environ['WORLD_SIZE'] # = str(torch.cuda.device_count()) dist.init_process_group(backend='nccl') #init_method="env://") Train() if __name__ == '__main__': main()
Andromeda-master
train.py
from Andromeda.model import Andromeda Andromeda1Billion = Andromeda( num_tokens=25000, max_seq_len=4192, dim=2048, depth=16, dim_head=128, heads=8, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=4, rotary_xpos=True, attn_flash=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) Andromeda3Billion = Andromeda( num_tokens=50432, max_seq_len=8192, dim=3072, depth=24, dim_head=128, heads=12, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=6, rotary_xpos=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) Andromeda7Billion = Andromeda( num_tokens=50432, max_seq_len=8192, dim=4096, depth=32, dim_head=128, heads=16, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=8, rotary_xpos=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) Andromeda10Billion = Andromeda( num_tokens=50432, max_seq_len=8192, dim=5120, depth=32, dim_head=128, heads=20, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=4, rotary_xpos=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) Andromeda15Billion = Andromeda( num_tokens=50432, max_seq_len=8192, dim=6144, depth=40, dim_head=128, heads=24, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=4, rotary_xpos=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) Andromeda20Billion = Andromeda( num_tokens=50432, max_seq_len=8192, dim=7168, depth=48, dim_head=128, heads=28, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=4, rotary_xpos=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ) #to GPT like 176Billion Parameters 122888 dimension, 96 depth, 96 heads, attn dim head 128
Andromeda-master
Andromeda/configs.py
# from Andromeda.train import Train from Andromeda.model import AndromedaTokenizer, Andromeda from Andromeda.train import Train, train
Andromeda-master
Andromeda/__init__.py
from torch.nn import Module from transformers import AutoTokenizer from Andromeda.core.transformer import ( Decoder, Transformer, ) from Andromeda.core.autoregressive_wrapper import AutoregressiveWrapper class AndromedaTokenizer: def __init__(self): self.tokenizer= AutoTokenizer.from_pretrained( "EleutherAI/gpt-neox-20b", eos_token="<eos>", pad_token="<pad>", extra_ids=0, model_max_length=8192 ) def tokenize_texts(self, texts): return self.tokenizer(texts, return_tensors='pt', padding=True, truncation=True).input_ids def decode(self, texts): return self.tokenizer.decode(texts) def __len__(self): num_tokens = len(self.tokenizer) return num_tokens class Andromeda(Module): """ Andromeda is a transformer-based model architecture. It initializes with a Transformer and AutoregressiveWrapper with default or user-specified parameters. """ def __init__(self, num_tokens=50432, max_seq_len=8192, dim=2560, depth=32, dim_head=128, heads=24, use_abs_pos_emb=False, alibi_pos_bias=True, alibi_num_heads=12, rotary_xpos=True, attn_flash=True, attn_kv_heads = 2, qk_norm=True, attn_qk_norm=True, attn_qk_norm_dim_scale=True, ): """ Initialize the model with specified or default parameters. Args: - num_tokens: Number of tokens in the vocabulary - max_seq_len: Maximum sequence length - dim: Dimension of the model - depth: Depth of the model - dim_head: Dimension of the model head - heads: Number of heads - use_abs_pos_emb: Whether to use absolute position embedding - alibi_pos_bias: Alibi position bias - alibi_num_heads: Number of alibi heads - rotary_xpos: Rotary position - attn_flash: Attention flash - deepnorm: Deep normalization - shift_tokens: Number of tokens to shift - attn_one_kv_head: Attention one key/value head - qk_norm: Query-key normalization - attn_qk_norm: Attention query-key normalization - attn_qk_norm_dim_scale: Attention query-key normalization dimension scale - embedding_provider: Embedding provider module """ super().__init__() try: self.Andromeda = Transformer( num_tokens=num_tokens, max_seq_len=max_seq_len, use_abs_pos_emb=use_abs_pos_emb, attn_layers=Decoder( dim=dim, depth=depth, dim_head=dim_head, heads=heads, alibi_pos_bias=alibi_pos_bias, alibi_num_heads=alibi_num_heads, rotary_xpos=rotary_xpos, attn_flash=attn_flash, attn_kv_heads=attn_kv_heads, qk_norm=qk_norm, attn_qk_norm=attn_qk_norm, attn_qk_norm_dim_scale=attn_qk_norm_dim_scale ) ) self.decoder = AutoregressiveWrapper(self.Andromeda) except Exception as e: print("Failed to initialize Andromeda: ", e) raise def forward(self, text_tokens, **kwargs): """ Forward pass through the model. It expects the input text_tokens. Args: - text_tokens: Input tokens - kwargs: Other arguments Returns: - output from the decoder """ try: model_input = self.decoder.forward(text_tokens)[0] return self.decoder(model_input, padded_x=model_input[0]) except Exception as e: print("Failed in forward method: ", e) raise
Andromeda-master
Andromeda/model.py
import math import multiprocessing import os from datetime import timedelta from functools import partial from itertools import chain import torch ########### SETUP CONFIG import torch.distributed as dist from accelerate import Accelerator from accelerate.logging import get_logger from accelerate.state import AcceleratorState from accelerate.utils import InitProcessGroupKwargs from datasets import load_dataset from lion_pytorch import Lion from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import ( CheckpointImpl, apply_activation_checkpointing, checkpoint_wrapper, ) # import bitsandbytes as bnb from torch.distributed.fsdp import ( BackwardPrefetch, FullyShardedDataParallel, MixedPrecision, ShardingStrategy, ) from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy from torch.nn import LayerNorm from torch.optim import AdamW from torch.utils.data import DataLoader from tqdm import tqdm from transformers import ( AutoTokenizer, default_data_collator, get_cosine_schedule_with_warmup, get_linear_schedule_with_warmup, set_seed, ) # from Andromeda.model import Andromeda from Andromeda.configs import Andromeda1Billion from Andromeda.core.transformer import Transformer from Andromeda.utils.stable_adamw import StableAdamWUnfused # state = AcceleratorState() logger = get_logger(__name__, log_level="INFO") class CFG: BATCH_SIZE = 1 GRADIENT_ACCUMULATE_EVERY: int = 1 SEED: int = 42 LEARNING_RATE: float = 1e-4 #3e-4 # 1e-4 for lion WEIGHT_DECAY: float = 0.1 SEQ_LEN: int = 8192 NUM_CPU: int = multiprocessing.cpu_count() USE_DEEPSPEED: bool = True USE_FSDP: bool = True USE_PRETOKENIZED: bool = True USE_ACTIVATION_CHECKPOINTING: bool = True RESUME_FROM_CHECKPOINT: str = False CHECKPOINTING_STEPS: int = 1000 OUTPUT_DIR: str = 'checkpoints/' # Folder ENTITY_NAME: str = "Andromeda" LOGGING_STEPS: int = 100 # helpers def print_num_params(model, accelerator: Accelerator): # n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) accelerator.print(f"Number of parameters in model: {n_params}") # activation checkpointing def activation_checkpointing( model: torch.nn.Module, offload_to_cpu: bool = False, accelerator: Accelerator = None, ): """ Apply activation checkpointing to a model. Args: model (Module): The model to which to apply activation checkpointing. offload_to_cpu (bool, optional): Whether to offload the activations to CPU. Defaults to False. accelerator (Accelerator, optional): The Accelerate library accelerator. Defaults to None. """ if accelerator is not None: accelerator.print("Using activation checkpointing") def check_fn(submodule): return isinstance(submodule, Transformer) non_reentrant_wrapper = partial( checkpoint_wrapper, offload_to_cpu=offload_to_cpu, checkpoint_impl=CheckpointImpl.NO_REENTRANT, ) apply_activation_checkpointing( model, checkpoint_wrapper_fn=non_reentrant_wrapper, check_fn=check_fn ) # FSDP def fsdp( model: torch.nn.Module, auto_wrap: bool = False, mp: str = "fp32", shard_strat: str = "NO_SHARD", ): """ This function wraps a given PyTorch model with the FullyShardedDataParallel (FSDP) wrapper to enable efficient data parallelism and model sharding. Args: model (torch.nn.Module): The original PyTorch model to be wrapped with FSDP. auto_wrap (bool, optional): If True, it enables automatic wrapping of the model's layers according to the transformer_auto_wrap_policy. Default is False. mp (str, optional): The mixed precision mode to be used. Can be 'bf16' for BFloat16, 'fp16' for Float16 or 'fp32' for Float32 precision. Default is 'fp32'. shard_strat (str, optional): The sharding strategy to be used. Can be 'SHARD_GRAD' for sharding at gradient computation, 'FULL_SHARD' for full model sharding or 'NO_SHARD' for no sharding. Default is 'NO_SHARD'. Raises: ValueError: If the provided mp (mixed precision mode) is not 'bf16', 'fp16' or 'fp32'. ValueError: If the provided shard_strat (sharding strategy) is not 'SHARD_GRAD', 'FULL_SHARD' or 'NO_SHARD'. Returns: torch.nn.Module: The input model wrapped with FSDP. """ if auto_wrap: Andromeda_auto_wrap_policy = partial( transformer_auto_wrap_policy, transformer_layer_cls={ Transformer, }, ) else: Andromeda_auto_wrap_policy = None if mp == "bf16": mp_fsdp = MixedPrecision( param_dtype=torch.bfloat16, # Gradient communication precision. reduce_dtype=torch.bfloat16, # Buffer precision. buffer_dtype=torch.bfloat16, ) elif mp == "fp16": mp_fsdp = MixedPrecision( param_dtype=torch.float16, # Gradient communication precision. reduce_dtype=torch.float16, # Buffer precision. buffer_dtype=torch.float16, ) elif mp == "fp32": mp_fsdp = MixedPrecision( param_dtype=torch.float32, # Gradient communication precision. reduce_dtype=torch.float32, # Buffer precision. buffer_dtype=torch.float32, ) else: raise ValueError( "Invalid scheduler_type. Expected 'bf16', 'fp16' or 'fp32', got: {}".format( mp ) ) if shard_strat == "SHARD_GRAD": sharding_strat_fsdp = ShardingStrategy.SHARD_GRAD_OP elif shard_strat == "FULL_SHARD": sharding_strat_fsdp = ShardingStrategy.FULL_SHARD elif shard_strat == "NO_SHARD": sharding_strat_fsdp = ShardingStrategy.NO_SHARD else: raise ValueError( "Invalid scheduler_type. Expected 'SHARD_GRAD', 'FULL_SHARD' or 'NO_SHARD', got: {}".format( shard_strat ) ) model = FullyShardedDataParallel( model, auto_wrap_policy=Andromeda_auto_wrap_policy, mixed_precision=mp_fsdp, backward_prefetch=BackwardPrefetch.BACKWARD_PRE, sharding_strategy=sharding_strat_fsdp, forward_prefetch=True, use_orig_params=True, ) return model # learning rate scheduler def get_lr_scheduler_with_warmup( optimizer: torch.optim.Optimizer, scheduler_type: str, num_warmup_steps: int, max_train_steps: int, grad_accumulate_every: int = 1, accelerator: Accelerator = None, ): """ Get a learning rate scheduler with warmup. Args: optimizer (Optimizer): The optimizer for which to create the learning rate scheduler. scheduler_type (str): The type of learning rate scheduler to create, either "linear" or "cosine". num_warmup_steps (int): The number of warmup steps for the learning rate scheduler. max_train_steps (int): The maximum number of training steps. grad_accumulate_every (int, optional): The gradient accumulation factor. Defaults to 1. accelerator (Accelerator, optional): The Accelerate library accelerator. Defaults to None. Returns: The learning rate scheduler with warmup. Raises: ValueError: If scheduler_type is not "linear" or "cosine". """ NUM_WARMUP_STEPS = num_warmup_steps GRADIENT_ACCUMULATE_EVERY = grad_accumulate_every if accelerator is not None: accelerator.print(f"Using {scheduler_type} lr scheduler") if scheduler_type == "linear": return get_linear_schedule_with_warmup( optimizer=optimizer, num_warmup_steps=NUM_WARMUP_STEPS * GRADIENT_ACCUMULATE_EVERY, num_training_steps=max_train_steps * GRADIENT_ACCUMULATE_EVERY, ) elif scheduler_type == "cosine": return get_cosine_schedule_with_warmup( optimizer=optimizer, num_warmup_steps=NUM_WARMUP_STEPS * GRADIENT_ACCUMULATE_EVERY, num_training_steps=max_train_steps * GRADIENT_ACCUMULATE_EVERY, ) else: raise ValueError( "Invalid scheduler_type. Expected 'linear' or 'cosine', got: {}".format( scheduler_type ) ) # optimizers def decoupled_optimizer( model: torch.nn.Module, learning_rate: float, weight_decay: float, beta_1: float, beta_2: float, optimizer_type: str, use_fsdp: bool = True, accelerator: Accelerator = None, ): """ Decouples the optimizer from the training process. This function sets up the optimizer for the model by creating two groups of parameters: one for weight decay and one without weight decay. Then, it initializes the optimizer with these two groups of parameters. Args: model (Module): The model whose parameters are optimized. learning_rate (float): The learning rate for the optimizer. weight_decay (float): The weight decay for the optimizer. beta_1 (float): The exponential decay rate for the 1st moment estimates. beta_2 (float): The exponential decay rate for the 2nd moment estimates. optimizer_type (str): The type of the optimizer. Can be 'lion', 'adamw', or 'stable_adamw'. use_fsdp (bool, optional): If True, the optimizer will work with fully sharded data parallelism. Defaults to True. accelerator (Accelerator, optional): The accelerator from HuggingFace's Accelerate library. Defaults to None. Returns: Optimizer: The initialized optimizer. Raises: ValueError: If the optimizer type is not 'lion', 'adamw' or 'stable_adamw'. """ accelerator.print(f"Using {optimizer_type} optimizer") # Create an empty dictionary called param_dict to store the model's named parameters. param_dict = {} # Iterate over the model's named parameters and populate the param_dict with key-value pairs. for param_name, param in model.named_parameters(): param_dict[param_name] = param # Separate the model's named modules into two groups: decay and no_decay. # Create an empty list to store the names of the LayerNorm and Embedding layer weights with no weight decay. no_decay = [] if use_fsdp: exclude_module = "_fsdp_wrapped_module.token_emb" else: exclude_module = "token_emb" # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of any of the desired types (LayerNorm or torch.nn.Embedding). for ndim in [LayerNorm, torch.nn.Embedding]: if isinstance(module, ndim): # If torch.nn.Embedding, append its name with a ".weight" suffix to the no_decay list. if module_name == exclude_module: no_decay.append(f"{module_name}.weight") else: # If the module is an instance of LayerNorm no_decay.append(f"{module_name}.gamma") # Exit the inner loop since the desired module has been found. break # Create an empty list to store the names of the Linear layer weights with weight decay. decay = [] # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of the desired type (torch.nn.Linear). for ndim in [torch.nn.Linear]: if isinstance(module, ndim): # If the module is an instance of torch.nn.Linear, append its name with a ".weight" suffix to the decay list. decay.append(f"{module_name}.weight") # Exit the inner loop since the desired module has been found. break # Create two separate lists of model parameters: decay_param and no_decay_param. # The decay_param list contains the parameters that should have weight decay applied. # The no_decay_param list contains the parameters that should not have weight decay applied, excluding the 'to_logits.weight' parameter. # Create an empty list called decay_param to store the parameters with weight decay. decay_param = [] if use_fsdp: exclude_param = "_fsdp_wrapped_module.to_logits.weight" else: exclude_param = "to_logits.weight" # Iterate over the decay list, which contains the names of the parameters with weight decay. for param in decay: # Check if the current parameter is not 'to_logits.weight'. # Append the corresponding parameter from param_dict to the decay_param list. if param != exclude_param: decay_param.append(param_dict[param]) # Create an empty list called no_decay_param to store the parameters without weight decay. no_decay_param = [] # Iterate over the no_decay list, which contains the names of the parameters without weight decay. for param in no_decay: try: # Append the corresponding parameter from param_dict to the no_decay_param list. no_decay_param.append(param_dict[param]) except KeyError: # print(f"Parameter {param_name} does not exist in the model") pass # Create a list called grouped_params that contains two dictionaries. # The first dictionary has the decay_param list and the corresponding weight_decay value. # The second dictionary has the no_decay_param list and a weight_decay value of 0.0. grouped_params = [ {"params": decay_param, "weight_decay": weight_decay}, {"params": no_decay_param, "weight_decay": 0.0}, ] # Create a variable called optimizer that stores an instance of the optimizer. if optimizer_type == "lion": optimizer = Lion(grouped_params, lr=learning_rate, betas=(beta_1, beta_2),) elif optimizer_type == "adamw": optimizer = AdamW(grouped_params, lr=learning_rate, betas=(beta_1, beta_2),) elif optimizer_type == "stable_adamw": optimizer = StableAdamWUnfused( grouped_params, lr=learning_rate, betas=(beta_1, beta_2), ) # elif optimizer_type=="Adam8bit": # optimizer = bnb.optim.Adam8bit(grouped_params, lr=learning_rate, betas=(beta_1, beta_2)) # elif optimizer_type=="Lion8Bit": # optimizer = bnb.optim.Lion8bit(grouped_params, lr=learning_rate, betas=(beta_1, beta_2)) else: raise ValueError( "Invalid optimizer_type. Expected 'lion', 'adamw', 'deepspeed' or 'stable_adamw', got: {}".format( optimizer_type ) ) # Return the optimizer. return optimizer # dataloaders def build_dataloaders(): """ Build data loaders for training. This function performs the following steps: 1. Load the tokenizer from the pretrained "EleutherAI/gpt-neox-20b" model. 2. Load the "openwebtext" dataset. 3. Tokenize the dataset, adding the end-of-sentence token to each text. 4. Process the tokenized dataset into chunks of a specified block size. Returns: Dataset: The processed dataset ready for training. """ tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b") dataset = load_dataset("openwebtext", split="train") tokenized_dataset = dataset.map( lambda example: tokenizer([t + tokenizer.eos_token for t in example["text"]]), batched=True, num_proc=CFG.NUM_CPU, remove_columns=["text"], ) block_size = CFG.SEQ_LEN # Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size. def group_texts(examples): # Concatenate all texts. concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can # customize this part to your needs. if total_length >= block_size: total_length = (total_length // block_size) * block_size # Split by chunks of max_len. result = { k: [t[i : i + block_size] for i in range(0, total_length, block_size)] for k, t in concatenated_examples.items() } return result train_dataset = tokenized_dataset.map( group_texts, batched=True, num_proc=CFG.NUM_CPU, ) return train_dataset #switch to falconwebdataset def build_pre_tokenized(): d0 = load_dataset("conceptofmind/c4_0-to-20_neox_with_eos_8k", split="train[:10]") # d1 = load_dataset("conceptofmind/c4_21-to-40_neox_with_eos_8k", split="train") # d2 = load_dataset("conceptofmind/c4_41-to-60_neox_with_eos_8k", split="train") # d3 = load_dataset("conceptofmind/c4_61-to-80_neox_with_eos_8k", split="train") # d4 = load_dataset("conceptofmind/c4_81-to-100_neox_with_eos_8k", split="train") # train_dataset = concatenate_datasets([d0, d1, d2, d3, d4]) return d0 def Train(): # accelerator timeout = InitProcessGroupKwargs(timeout=timedelta(seconds=1_000_000)) accelerator = Accelerator( gradient_accumulation_steps=CFG.GRADIENT_ACCUMULATE_EVERY, mixed_precision="fp16", log_with="wandb", kwargs_handlers=[timeout], ) state = AcceleratorState() state.deepspeed_plugin.deepspeed_config['train_micro_batch_size_per_gpu'] = CFG.BATCH_SIZE #?????? accelerator.init_trackers( project_name="Andromeda", config={ "batch_size": CFG.BATCH_SIZE, "gradient_accumulate_every": CFG.GRADIENT_ACCUMULATE_EVERY, "learning_rate": CFG.LEARNING_RATE, "seq_len": CFG.SEQ_LEN, }, # init_kwargs={"wandb": {"entity": CFG.ENTITY_NAME}}, ) accelerator.print(f"Total GPUS: {accelerator.num_processes}") # set seed set_seed(CFG.SEED) model = Andromeda1Billion() print_num_params(model, accelerator) if CFG.USE_FSDP: model = fsdp( model, mp="fp16", shard_strat="SHARD_GRAD" ) if CFG.USE_ACTIVATION_CHECKPOINTING: activation_checkpointing(model, accelerator) model = accelerator.prepare(model) # dataloaders if CFG.USE_PRETOKENIZED: train_dataset = build_pre_tokenized() else: train_dataset = build_dataloaders() train_loader = DataLoader( train_dataset, batch_size=CFG.BATCH_SIZE, collate_fn=default_data_collator, ) # optimizer optim = decoupled_optimizer( model=model, learning_rate=CFG.LEARNING_RATE, weight_decay=CFG.WEIGHT_DECAY, beta_1=0.90, beta_2=0.95, optimizer_type='lion', use_fsdp=True, accelerator=accelerator ) # Determine number of training steps max_train_steps = math.ceil(len(train_loader) / CFG.GRADIENT_ACCUMULATE_EVERY) accelerator.print(f"Max train steps: {max_train_steps}") # lr scheduler NUM_WARMUP_STEPS = int(max_train_steps * 0.01) accelerator.print(f"Num warmup steps: {NUM_WARMUP_STEPS}") # if False: # if CFG.USE_DEEPSPEED: # lr_scheduler = DummyScheduler( # optim, # total_num_steps=max_train_steps * accelerator.num_processes, # warmup_num_steps=NUM_WARMUP_STEPS # ) # else: lr_scheduler = get_lr_scheduler_with_warmup( optimizer=optim, scheduler_type="cosine", num_warmup_steps=NUM_WARMUP_STEPS, max_train_steps=max_train_steps, grad_accumulate_every=CFG.GRADIENT_ACCUMULATE_EVERY, ) # prepare optim, train_loader, lr_scheduler = accelerator.prepare( optim, train_loader, lr_scheduler ) # checkpoint scheduler accelerator.register_for_checkpointing(lr_scheduler) # I do not know why Huggingface recommends recalculation of max_train_steps max_train_steps = math.ceil(len(train_loader) / CFG.GRADIENT_ACCUMULATE_EVERY) accelerator.print(f"Max train steps recalculated: {max_train_steps}") # Total batch size for logging total_batch_size = ( CFG.BATCH_SIZE * accelerator.num_processes * CFG.GRADIENT_ACCUMULATE_EVERY ) accelerator.print(f"Total batch size: {total_batch_size}") # resume training progress_bar = tqdm( range(max_train_steps), disable=not accelerator.is_local_main_process ) completed_steps = 0 if CFG.RESUME_FROM_CHECKPOINT: if CFG.RESUME_FROM_CHECKPOINT is not None or CFG.RESUME_FROM_CHECKPOINT != "": accelerator.print(f"Resuming from checkpoint {CFG.RESUME_FROM_CHECKPOINT}") accelerator.load_state(CFG.RESUME_FROM_CHECKPOINT) path = os.path.basename(CFG.RESUME_FROM_CHECKPOINT) training_difference = os.path.splitext(path)[0] # need to multiply `gradient_accumulation_steps` to reflect real steps resume_step = ( int(training_difference.replace("step_", "")) * CFG.GRADIENT_ACCUMULATE_EVERY ) if CFG.RESUME_FROM_CHECKPOINT and resume_step is not None: train_loader = accelerator.skip_first_batches(train_loader, resume_step) completed_steps += resume_step progress_bar.update(resume_step) # training model.train() for step, batch in enumerate(train_loader): with accelerator.accumulate(model): inputs = batch["input_ids"].to(accelerator.device) loss = model(inputs, return_loss=True) accelerator.backward(loss) accelerator.log({"loss": loss.item()}, step=step) if accelerator.sync_gradients: accelerator.clip_grad_norm_(model.parameters(), 1.0) optim.step() lr_scheduler.step() optim.zero_grad() if accelerator.sync_gradients: progress_bar.update(1) completed_steps += 1 if isinstance(CFG.CHECKPOINTING_STEPS, int): if completed_steps % CFG.CHECKPOINTING_STEPS == 0: output_dir = f"step_{completed_steps }" if CFG.OUTPUT_DIR is not None: output_dir = os.path.join(CFG.OUTPUT_DIR, output_dir) accelerator.save_state(output_dir) if completed_steps >= max_train_steps: break #logging every CFG.LOGGING STEPS if CFG.LOGGING_STEPS > 0 and step % CFG.LOGGING_STEPS == 0: logger.info( f"Step: {completed_steps}/{max_train_steps}, Loss: {loss.item():.5f}" ) # end training # accelerator.print(f"Training Finished") accelerator.end_training() # save final model # accelerator.print(f"Saving model to {CFG.OUTPUT_DIR}") if CFG.OUTPUT_DIR is not None: accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) with accelerator.main_process_first(): accelerator.save( unwrapped_model.state_dict(), f"{CFG.OUTPUT_DIR}/final/final_model.pt" ) def train(): os.environ['MASTER_ADDR'] #'localhost' os.environ['MASTER_PORT'] #= '9994' # # [CRITICAL] Pay attention to this when scaling to multiple GPUs and clusters # # Pay attention to this, use "accelerate config" os.environ['RANK'] #= str(0) # Number of nodes (servers) os.environ['WORLD_SIZE'] # = str(torch.cuda.device_count()) dist.init_process_group(backend='nccl') #init_method="env://") Train() if __name__ == '__main__': train()
Andromeda-master
Andromeda/train.py
import torch from transformers import AutoTokenizer from einops._torch_specific import allow_ops_in_compiled_graph import argparse # class AndromedaEval: # def __init__(self, path, seed=42, device=None): # self.path = path # self.seed = seed # self.device = device # if self.device is None: # self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # set_seed(self.seed) # #tokenizer # self.tokenizer = AndromedaTokenizer # #model # self.model = Andromeda # #checkpoint # self.model.load_state_dict(torch.load(self.path)) # self.model.eval() # #device # self.model = self.model.to(self.device) # #metrics # self.metrics = {} # self.reset_metrics() # def reset_metrics(self): # self.metrics = { # "generation_steps": None, # "time_forward": [], # "time_forward_average": None, # "memory_usages": [], # "memory_usage_average": None, # "time_end_to_end": None, # "throughput": None # } # def get_num_params(self): # num_params = sum(param.numel() for param in self.model.parameters() if param.requires_grad) # return num_params # def generate(self, prompt, generation_steps=32): # #make sure all of the metrics reset at every generation # self.reset_metrics() # self.metrics["generation_steps"] = generation_steps # tokens = self.tokenizer.encode(prompt) # tokens_new = [] # time_end_to_end = time.time() # #generation loop # for _ in range(generation_steps): # tokens_tensor = torch.tensor([tokens], device=self.device) # #forward pass # tracemalloc.start() # time_forward_0 = time.time() # logits = self.model(tokens_tensor, return_loss=False)[:, -1] # no loss takes the output of the last tokens # time_forward_1 = time.time() # _, memory_usage = tracemalloc.get_traced_memory() # tracemalloc.stop() # self.metrics["memory_usages"].append(memory_usage) # time_forward = time_forward_1 - time_forward_0 # self.metrics["times_forward"].append(time_forward) # next_token = torch.armax(logits).item() # #save the newly generated token # tokens.append(next_token) # tokens_new.append(next_token) # time_end_to_end_1 = time.time() # time_end_to_end = time_end_to_end_1 - time_end_to_end_0 # self.metrics["time_end_to_end"] = time_end_to_end # decoded = self.tokenizer.decode(tokens) # self.metrics["time_forward_average"] = np.mean(self.metrics["times_forward"]) # self.metrics["memory_usage_average"] = np.mean(self.metrics["memory_usage"]) # self.metrics['throughput'] = generation_steps / np.sum(self.metrics["times_forward"]) # return tokens_new, decoded # def main(): # prompt = 'My name is' # andromeda = EvalAndromeda(path='checkpoints/step_44927_6656/pytorch_model.bin') # num_params = Andromeda.get_num_params() # print(f'The model has {num_params} parameters') # _, output = Andromeda.generate(prompt) # for metric, value in Andromeda.metrics.items(): # print(f'{metric}: {value}\n') # print('\n') # print(output) def main(): allow_ops_in_compiled_graph() torch.hub._validate_not_a_forked_repo = lambda a, b, c: True parser = argparse.ArgumentParser(description="Generate text using Andromeda model") parser.add_argument("prompt", type=str, help="Text prompt to generate text") parser.add_argument( "--seq_len", type=int, default=256, help="Sequence length for generated text" ) parser.add_argument( "--temperature", type=float, default=0.8, help="Sampling temperature" ) parser.add_argument( "--filter_thres", type=float, default=0.9, help="Filter threshold for sampling" ) parser.add_argument( "--model", type=str, default="andromeda-e-1", help="Model to use for generation", ) parser.add_argument( "--dtype", type=str, default="fp32", help="Data type for the model: 'bf16', or 'fp32'", ) args = parser.parse_args() dtype = torch.float32 if args.dtype == 'bf16': dtype = torch.bfloat16 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") #need to submit to torch hub model = torch.hub.load("apacai/andromeda", args.model).to(device).to(dtype) opt_model = torch.compile(model, backend="hidet") tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b") encoded_text = tokenizer(args.prompt, return_tensors="pt") output_tensor = opt_model.generate( seq_len=args.seq_len, prompt=encoded_text["input_ids"].to(device), temperature=args.temperature, filter_thres=args.filter_thres, pad_value=0.0, eos_token=tokenizer.eos_token_id, return_seq_without_prompt=False, use_tqdm=True, ) decoded_output = tokenizer.batch_decode(output_tensor, skip_special_tokens=True) return decoded_output if __name__ == "__main__": generated_text = main() for text in generated_text: print(f"{text}")
Andromeda-master
Andromeda/inference.py
from math import ceil import torch from torch import nn import torch.nn.functional as F from einops import rearrange, pack, unpack def exists(val): return val is not None def eval_decorator(fn): def inner(self, *args, **kwargs): was_training = self.training self.eval() out = fn(self, *args, **kwargs) self.train(was_training) return out return inner # nucleus def top_p(logits, thres = 0.9): sorted_logits, sorted_indices = torch.sort(logits, descending=True) cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) sorted_indices_to_remove = cum_probs > (1 - thres) sorted_indices_to_remove[:, 1:] = sorted_indices_to_remove[:, :-1].clone() sorted_indices_to_remove[:, 0] = 0 sorted_logits[sorted_indices_to_remove] = float('-inf') return sorted_logits.scatter(1, sorted_indices, sorted_logits) # topk def top_k(logits, thres = 0.9): k = ceil((1 - thres) * logits.shape[-1]) val, ind = torch.topk(logits, k) probs = torch.full_like(logits, float('-inf')) probs.scatter_(1, ind, val) return probs # top_a def top_a(logits, min_p_pow=2.0, min_p_ratio=0.02): probs = F.softmax(logits, dim=-1) limit = torch.pow(torch.max(probs), min_p_pow) * min_p_ratio logits[probs < limit] = float('-inf') logits[probs >= limit] = 1 return logits # autoregressive wrapper class class AutoregressiveWrapper(nn.Module): def __init__( self, net, ignore_index = -100, pad_value = 0, mask_prob = 0. ): super().__init__() self.pad_value = pad_value self.ignore_index = ignore_index self.net = net self.max_seq_len = net.max_seq_len # paper shows masking (MLM) in conjunction with autoregressive decoder-only training leads to big improvements https://arxiv.org/abs/2210.13432 assert mask_prob < 1. self.mask_prob = mask_prob @torch.no_grad() @eval_decorator def generate( self, start_tokens, seq_len, eos_token = None, temperature = 1., filter_logits_fn = top_k, filter_thres = 0.9, min_p_pow = 2.0, min_p_ratio = 0.02, **kwargs ): start_tokens, ps = pack([start_tokens], '* n') b, t = start_tokens.shape out = start_tokens for _ in range(seq_len): x = out[:, -self.max_seq_len:] logits = self.net(x, **kwargs)[:, -1] if filter_logits_fn in {top_k, top_p}: filtered_logits = filter_logits_fn(logits, thres = filter_thres) probs = F.softmax(filtered_logits / temperature, dim=-1) elif filter_logits_fn is top_a: filtered_logits = filter_logits_fn(logits, min_p_pow = min_p_pow, min_p_ratio= min_p_ratio) probs = F.softmax(filtered_logits / temperature, dim=-1) sample = torch.multinomial(probs, 1) out = torch.cat((out, sample), dim=-1) if exists(eos_token): is_eos_tokens = (out == eos_token) if is_eos_tokens.any(dim = -1).all(): # mask out everything after the eos tokens shifted_is_eos_tokens = F.pad(is_eos_tokens, (1, -1)) mask = shifted_is_eos_tokens.float().cumsum(dim = -1) >= 1 out = out.masked_fill(mask, self.pad_value) break out = out[:, t:] out, = unpack(out, ps, '* n') return out def forward(self, x, return_loss=True, **kwargs): seq, ignore_index = x.shape[1], self.ignore_index inp, target = x[:, :-1], x[:, 1:] if self.mask_prob > 0.: rand = torch.randn(inp.shape, device = x.device) rand[:, 0] = -torch.finfo(rand.dtype).max # first token should not be masked out num_mask = min(int(seq * self.mask_prob), seq - 1) indices = rand.topk(num_mask, dim = -1).indices mask = ~torch.zeros_like(inp).scatter(1, indices, 1.).bool() kwargs.update(self_attn_context_mask = mask) logits = self.net(inp, **kwargs) loss = F.cross_entropy( rearrange(logits, 'b n c -> b c n'), target, ignore_index = ignore_index ) if return_loss: return logits, loss return logits
Andromeda-master
Andromeda/core/autoregressive_wrapper.py
import torch from packaging import version if version.parse(torch.__version__) >= version.parse('2.0.0'): from einops._torch_specific import allow_ops_in_compiled_graph allow_ops_in_compiled_graph()
Andromeda-master
Andromeda/core/__init__.py
from functools import partial from typing import Optional import torch from torch import nn, einsum, Tensor import torch.nn.functional as F from collections import namedtuple from functools import wraps from packaging import version from dataclasses import dataclass from einops import rearrange, repeat # constants EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) @dataclass class Intermediates: qk_similarities: Optional[Tensor] = None pre_softmax_attn: Optional[Tensor] = None post_softmax_attn: Optional[Tensor] = None def to_tuple(self): return (self.qk_similarities, self.pre_softmax_attn, self.post_softmax_attn) # helpers def exists(val): return val is not None def default(val, d): return val if exists(val) else d def compact(arr): return [*filter(exists, arr)] def once(fn): called = False @wraps(fn) def inner(x): nonlocal called if called: return called = True return fn(x) return inner print_once = once(print) # functions for creating causal mask # need a special one for onnx cpu (no support for .triu) def create_causal_mask(i, j, device): return torch.ones((i, j), device = device, dtype = torch.bool).triu(j - i + 1) def onnx_create_causal_mask(i, j, device): r = torch.arange(i, device = device) causal_mask = rearrange(r, 'i -> i 1') < rearrange(r, 'j -> 1 j') causal_mask = F.pad(causal_mask, (j - i, 0), value = False) return causal_mask # main class class Attend(nn.Module): def __init__( self, *, dropout = 0., causal = False, heads = None, talking_heads = False, sparse_topk = None, scale = None, qk_norm = False, flash = False, add_zero_kv = False, onnxable = False ): super().__init__() self.scale = scale self.qk_norm = qk_norm self.causal = causal self.create_causal_mask = onnx_create_causal_mask if onnxable else create_causal_mask self.attn_fn = partial(F.softmax, dtype = torch.float32) if not qk_norm else F.softmax self.dropout = dropout self.attn_dropout = nn.Dropout(dropout) # talking heads assert not (flash and talking_heads), 'talking heads not compatible with flash attention' self.talking_heads = talking_heads if talking_heads: self.pre_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) self.post_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) # sparse topk assert not (flash and sparse_topk), 'sparse topk not compatible with flash attention' self.sparse_topk = sparse_topk # add a key / value token composed of zeros # in case this helps controlling outliers, proposed by https://www.evanmiller.org/attention-is-off-by-one.html self.add_zero_kv = add_zero_kv # flash attention self.flash = flash assert not (flash and version.parse(torch.__version__) < version.parse('2.0.0')), 'in order to use flash attention, you must be using pytorch 2.0 or above' # determine efficient attention configs for cuda and cpu self.cpu_config = EfficientAttentionConfig(True, True, True) self.cuda_config = None if not torch.cuda.is_available() or not flash: return device_properties = torch.cuda.get_device_properties(torch.device('cuda')) if device_properties.major == 8 and device_properties.minor == 0: print_once('A100 GPU detected, using flash attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(True, False, False) else: print_once('Non-A100 GPU detected, using math or mem efficient attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(False, True, True) def flash_attn( self, q, k, v, mask = None, attn_bias = None ): batch, heads, q_len, _, k_len, is_cuda, device = *q.shape, k.shape[-2], q.is_cuda, q.device # Recommended for multi-query single-key-value attention by Tri Dao # kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64]) if k.ndim == 3: k = rearrange(k, 'b ... -> b 1 ...').expand_as(q) if v.ndim == 3: v = rearrange(v, 'b ... -> b 1 ...').expand_as(q) # handle scale - by default they scale by dim_head ** -0.5, but need to take care if using cosine sim attention if self.qk_norm: default_scale = q.shape[-1] ** -0.5 q = q * (default_scale / self.scale) # Check if mask exists and expand to compatible shape # The mask is B L, so it would have to be expanded to B H N L causal = self.causal if exists(mask): assert mask.ndim == 4 mask = mask.expand(batch, heads, q_len, k_len) # manually handle causal mask, if another mask was given if causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) mask = mask & ~causal_mask causal = False # handle alibi positional bias # convert from bool to float if exists(attn_bias): attn_bias = rearrange(attn_bias, 'h i j -> 1 h i j').expand(batch, heads, -1, -1) # if mask given, the mask would already contain the causal mask from above logic # otherwise, if no mask given but still causal, mask out alibi positional bias to a large negative number mask_value = -torch.finfo(q.dtype).max if exists(mask): attn_bias = attn_bias.masked_fill(~mask, mask_value // 2) elif causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) attn_bias = attn_bias.masked_fill(causal_mask, mask_value // 2) causal = False # scaled_dot_product_attention handles attn_mask either as bool or additive bias # make it an additive bias here mask = attn_bias # Check if there is a compatible device for flash attention config = self.cuda_config if is_cuda else self.cpu_config # pytorch 2.0 flash attn: q, k, v, mask, dropout, causal, softmax_scale with torch.backends.cuda.sdp_kernel(**config._asdict()): out = F.scaled_dot_product_attention( q, k, v, attn_mask = mask, dropout_p = self.dropout if self.training else 0., is_causal = causal ) return out, Intermediates() def forward( self, q, k, v, mask = None, attn_bias = None, prev_attn = None ): """ einstein notation b - batch h - heads n, i, j - sequence length (base sequence length, source, target) d - feature dimension """ n, heads, kv_heads, device = q.shape[-2], q.shape[1], k.shape[1], q.device scale = default(self.scale, q.shape[-1] ** -0.5) # handle grouped multi-query attention if kv_heads == 1: k, v = map(lambda t: rearrange(t, 'b 1 n d -> b n d'), (k, v)) elif kv_heads < heads: k, v = map(lambda t: repeat(t, 'b kvh n d -> b (r kvh) n d', r = heads // kv_heads), (k, v)) # handle zero kv, as means for allowing network to attend to nothing if self.add_zero_kv: k, v = map(lambda t: F.pad(t, (0, 0, 1, 0), value = 0.), (k, v)) if exists(mask): mask = F.pad(mask, (1, 0), value = True) if exists(attn_bias): attn_bias = F.pad(attn_bias, (1, 0), value = 0.) if self.flash: assert not exists(prev_attn), 'residual attention not compatible with flash attention' return self.flash_attn(q, k, v, mask = mask, attn_bias = attn_bias) kv_einsum_eq = 'b j d' if k.ndim == 3 else 'b h j d' dots = einsum(f'b h i d, {kv_einsum_eq} -> b h i j', q, k) * scale if exists(prev_attn): dots = dots + prev_attn qk_similarities = dots.clone() if self.talking_heads: dots = self.pre_softmax_talking_heads(dots) if exists(attn_bias): dots = dots + attn_bias i, j, dtype = *dots.shape[-2:], dots.dtype mask_value = -torch.finfo(dots.dtype).max if exists(self.sparse_topk) and self.sparse_topk < j: top_values, _ = dots.topk(self.sparse_topk, dim = -1) sparse_topk_mask = dots < top_values[..., -1:] mask = (mask & sparse_topk_mask) if exists(mask) else sparse_topk_mask if exists(mask): dots = dots.masked_fill(~mask, mask_value) if self.causal: causal_mask = self.create_causal_mask(i, j, device = device) dots = dots.masked_fill(causal_mask, mask_value) pre_softmax_attn = dots.clone() attn = self.attn_fn(dots, dim = -1) attn = attn.type(dtype) post_softmax_attn = attn.clone() attn = self.attn_dropout(attn) if self.talking_heads: attn = self.post_softmax_talking_heads(attn) out = einsum(f'b h i j, {kv_einsum_eq} -> b h i d', attn, v) intermediates = Intermediates( qk_similarities = qk_similarities, pre_softmax_attn = pre_softmax_attn, post_softmax_attn = post_softmax_attn ) return out, intermediates # cascading heads logic def to_single_heads(t, dim = 1): heads = t.unbind(dim = dim) return tuple(head.unsqueeze(dim) for head in heads) class CascadingHeads(nn.Module): def __init__(self, attend: Attend): super().__init__() self.attend = attend def forward( self, q, k, v, mask = None, attn_bias = None, prev_attn = None ): assert q.shape[-1] == v.shape[-1], 'cascading heads can only be done if query / key and value head dimensions are the same' # split inputs into per-head inputs heads = q.shape[1] queries = to_single_heads(q) keys = to_single_heads(k) if k.ndim == 4 else ((k,) * heads) values = to_single_heads(v) if v.ndim == 4 else ((v,) * heads) mask = (mask,) * heads attn_bias = to_single_heads(attn_bias, dim = 0) if exists(attn_bias) else ((None,) * heads) prev_attn = to_single_heads(prev_attn) if exists(prev_attn) else ((None,) * heads) # now loop through each head, without output of previous head summed with the next head # thus cascading all_outs = [] all_intermediates = [] prev_head_out = None for h_q, h_k, h_v, h_mask, h_attn_bias, h_prev_attn in zip(queries, keys, values, mask, attn_bias, prev_attn): if exists(prev_head_out): h_q = h_q + prev_head_out out, intermediates = self.attend( h_q, h_k, h_v, mask = h_mask, attn_bias = h_attn_bias, prev_attn = h_prev_attn ) prev_head_out = out all_outs.append(out) all_intermediates.append(intermediates) # cat all output heads all_outs = torch.cat(all_outs, dim = 1) # cat all intermediates, if they exist qk_similarities, pre_softmax_attn, post_softmax_attn = zip(*map(lambda i: i.to_tuple(), all_intermediates)) qk_similarities, pre_softmax_attn, post_softmax_attn = map(compact, (qk_similarities, pre_softmax_attn, post_softmax_attn)) aggregated_intermediates = Intermediates( qk_similarities = torch.cat(qk_similarities, dim = 1) if len(qk_similarities) > 0 else None, pre_softmax_attn = torch.cat(pre_softmax_attn, dim = 1) if len(pre_softmax_attn) > 0 else None, post_softmax_attn = torch.cat(post_softmax_attn, dim = 1) if len(post_softmax_attn) > 0 else None ) return all_outs, aggregated_intermediates
Andromeda-master
Andromeda/core/attend.py
import math from random import random import torch from torch import nn, einsum, Tensor import torch.nn.functional as F from functools import partial, wraps from inspect import isfunction from dataclasses import dataclass from typing import Callable, List, Optional from einops import rearrange, repeat, reduce from Andromeda.core.attend import Attend, Intermediates DEFAULT_DIM_HEAD = 64 @dataclass class LayerIntermediates: hiddens: Optional[List[Tensor]] = None attn_intermediates: Optional[List[Intermediates]] = None layer_hiddens: Optional[List[Tensor]] = None attn_z_loss: Optional[Tensor] = None # helpers def exists(val): return val is not None def default(val, d): if exists(val): return val return d() if isfunction(d) else d def cast_tuple(val, depth): return val if isinstance(val, tuple) else (val,) * depth def divisible_by(num, den): return (num % den) == 0 def maybe(fn): @wraps(fn) def inner(x, *args, **kwargs): if not exists(x): return x return fn(x, *args, **kwargs) return inner class always(): def __init__(self, val): self.val = val def __call__(self, *args, **kwargs): return self.val class not_equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x != self.val class equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x == self.val def Sequential(*modules): return nn.Sequential(*filter(exists, modules)) # tensor helpers def max_neg_value(tensor): return -torch.finfo(tensor.dtype).max def l2norm(t, groups = 1): t = rearrange(t, '... (g d) -> ... g d', g = groups) t = F.normalize(t, p = 2, dim = -1) return rearrange(t, '... g d -> ... (g d)') def pad_at_dim(t, pad, dim = -1, value = 0.): dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) zeros = ((0, 0) * dims_from_right) return F.pad(t, (*zeros, *pad), value = value) def or_reduce(masks): head, *body = masks for rest in body: head = head | rest return head # auxiliary loss helpers def calc_z_loss( pre_softmax_attns: List[Tensor], mask = None, weight = 1. ): # the same loss applied to the mixture of experts router logits in https://arxiv.org/abs/2202.08906 # in the paper, in a tiny footnote, they mention using it on attention logits with stabilizing effects # also used in PaLM as one of the measures lse = 0. for attn in pre_softmax_attns: lse = lse + attn.logsumexp(dim = -1) loss = torch.square(lse) loss = reduce(loss, 'b h n -> b n', 'sum') if not exists(mask): return loss.mean() * weight loss = loss[mask].sum() / mask.sum().clamp(min = 1e-5) return loss * weight # init helpers def init_zero_(layer): nn.init.constant_(layer.weight, 0.) if exists(layer.bias): nn.init.constant_(layer.bias, 0.) # keyword argument helpers def pick_and_pop(keys, d): values = list(map(lambda key: d.pop(key), keys)) return dict(zip(keys, values)) def group_dict_by_key(cond, d): return_val = [dict(),dict()] for key in d.keys(): match = bool(cond(key)) ind = int(not match) return_val[ind][key] = d[key] return (*return_val,) def string_begins_with(prefix, str): return str.startswith(prefix) def group_by_key_prefix(prefix, d): return group_dict_by_key(partial(string_begins_with, prefix), d) def groupby_prefix_and_trim(prefix, d): kwargs_with_prefix, kwargs = group_dict_by_key(partial(string_begins_with, prefix), d) kwargs_without_prefix = dict(map(lambda x: (x[0][len(prefix):], x[1]), tuple(kwargs_with_prefix.items()))) return kwargs_without_prefix, kwargs # initializations def deepnorm_init( transformer, beta, module_name_match_list = ['.ff.', '.to_v', '.to_out'] ): for name, module in transformer.named_modules(): if type(module) != nn.Linear: continue needs_beta_gain = any(map(lambda substr: substr in name, module_name_match_list)) gain = beta if needs_beta_gain else 1 nn.init.xavier_normal_(module.weight.data, gain = gain) if exists(module.bias): nn.init.constant_(module.bias.data, 0) # structured dropout, more effective than traditional attention dropouts def dropout_seq(seq, mask, dropout): b, n, *_, device = *seq.shape, seq.device logits = torch.randn(b, n, device = device) if exists(mask): mask_value = max_neg_value(logits) logits = logits.masked_fill(~mask, mask_value) keep_prob = 1. - dropout num_keep = max(1, int(keep_prob * n)) keep_indices = logits.topk(num_keep, dim = 1).indices batch_indices = torch.arange(b, device = device) batch_indices = rearrange(batch_indices, 'b -> b 1') seq = seq[batch_indices, keep_indices] if exists(mask): seq_counts = mask.sum(dim = -1) seq_keep_counts = torch.ceil(seq_counts * keep_prob).int() keep_mask = torch.arange(num_keep, device = device) < rearrange(seq_keep_counts, 'b -> b 1') mask = mask[batch_indices, keep_indices] & keep_mask return seq, mask # activations class ReluSquared(nn.Module): def forward(self, x): return F.relu(x) ** 2 # embedding class TokenEmbedding(nn.Module): def __init__(self, dim, num_tokens, l2norm_embed = False): super().__init__() self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(num_tokens, dim) def forward(self, x): token_emb = self.emb(x) return l2norm(token_emb) if self.l2norm_embed else token_emb # positional embeddings class AbsolutePositionalEmbedding(nn.Module): def __init__(self, dim, max_seq_len, l2norm_embed = False): super().__init__() self.scale = dim ** -0.5 if not l2norm_embed else 1. self.max_seq_len = max_seq_len self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(max_seq_len, dim) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device assert seq_len <= self.max_seq_len, f'you are passing in a sequence length of {seq_len} but your absolute positional embedding has a max sequence length of {self.max_seq_len}' if not exists(pos): pos = torch.arange(seq_len, device = device) pos_emb = self.emb(pos) pos_emb = pos_emb * self.scale return l2norm(pos_emb) if self.l2norm_embed else pos_emb class ScaledSinusoidalEmbedding(nn.Module): def __init__(self, dim, theta = 10000): super().__init__() assert divisible_by(dim, 2) self.scale = nn.Parameter(torch.ones(1) * dim ** -0.5) half_dim = dim // 2 freq_seq = torch.arange(half_dim).float() / half_dim inv_freq = theta ** -freq_seq self.register_buffer('inv_freq', inv_freq, persistent = False) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device if not exists(pos): pos = torch.arange(seq_len, device = device) emb = einsum('i, j -> i j', pos, self.inv_freq) emb = torch.cat((emb.sin(), emb.cos()), dim = -1) return emb * self.scale class RelativePositionBias(nn.Module): def __init__(self, scale, causal = False, num_buckets = 32, max_distance = 128, heads = 8): super().__init__() self.scale = scale self.causal = causal self.num_buckets = num_buckets self.max_distance = max_distance self.relative_attention_bias = nn.Embedding(num_buckets, heads) @staticmethod def _relative_position_bucket(relative_position, causal = True, num_buckets = 32, max_distance = 128): ret = 0 n = -relative_position if not causal: num_buckets //= 2 ret += (n < 0).long() * num_buckets n = torch.abs(n) else: n = torch.max(n, torch.zeros_like(n)) max_exact = num_buckets // 2 is_small = n < max_exact val_if_large = max_exact + ( torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) ).long() val_if_large = torch.min(val_if_large, torch.full_like(val_if_large, num_buckets - 1)) ret += torch.where(is_small, n, val_if_large) return ret @property def device(self): return next(self.parameters()).device def forward(self, i, j): device = self.device q_pos = torch.arange(j - i, j, dtype = torch.long, device = device) k_pos = torch.arange(j, dtype = torch.long, device = device) rel_pos = k_pos[None, :] - q_pos[:, None] rp_bucket = self._relative_position_bucket(rel_pos, causal = self.causal, num_buckets = self.num_buckets, max_distance = self.max_distance) values = self.relative_attention_bias(rp_bucket) bias = rearrange(values, 'i j h -> h i j') return bias * self.scale class DynamicPositionBias(nn.Module): def __init__(self, dim, *, heads, depth, log_distance = False, norm = False): super().__init__() assert depth >= 1, 'depth for dynamic position bias MLP must be greater or equal to 1' self.log_distance = log_distance self.mlp = nn.ModuleList([]) self.mlp.append(Sequential( nn.Linear(1, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) for _ in range(depth - 1): self.mlp.append(Sequential( nn.Linear(dim, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) self.mlp.append(nn.Linear(dim, heads)) @property def device(self): return next(self.parameters()).device def forward(self, i, j): assert i == j n, device = j, self.device # get the (n x n) matrix of distances seq_arange = torch.arange(n, device = device) context_arange = torch.arange(n, device = device) indices = rearrange(seq_arange, 'i -> i 1') - rearrange(context_arange, 'j -> 1 j') indices += (n - 1) # input to continuous positions MLP pos = torch.arange(-n + 1, n, device = device).float() pos = rearrange(pos, '... -> ... 1') if self.log_distance: pos = torch.sign(pos) * torch.log(pos.abs() + 1) # log of distance is sign(rel_pos) * log(abs(rel_pos) + 1) for layer in self.mlp: pos = layer(pos) # get position biases bias = pos[indices] bias = rearrange(bias, 'i j h -> h i j') return bias class AlibiPositionalBias(nn.Module): def __init__(self, heads, total_heads, **kwargs): super().__init__() self.heads = heads self.total_heads = total_heads slopes = Tensor(self._get_slopes(heads)) slopes = rearrange(slopes, 'h -> h 1 1') self.register_buffer('slopes', slopes, persistent = False) self.register_buffer('bias', None, persistent = False) def get_bias(self, i, j, device): i_arange = torch.arange(j - i, j, device = device) j_arange = torch.arange(j, device = device) bias = -torch.abs(rearrange(j_arange, 'j -> 1 1 j') - rearrange(i_arange, 'i -> 1 i 1')) return bias @staticmethod def _get_slopes(heads): def get_slopes_power_of_2(n): start = (2**(-2**-(math.log2(n)-3))) ratio = start return [start*ratio**i for i in range(n)] if math.log2(heads).is_integer(): return get_slopes_power_of_2(heads) closest_power_of_2 = 2 ** math.floor(math.log2(heads)) return get_slopes_power_of_2(closest_power_of_2) + get_slopes_power_of_2(2 * closest_power_of_2)[0::2][:heads-closest_power_of_2] @property def device(self): return next(self.buffers()).device def forward(self, i, j): h, device = self.total_heads, self.device if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: return self.bias[..., :i, :j] bias = self.get_bias(i, j, device) bias = bias * self.slopes num_heads_unalibied = h - bias.shape[0] bias = pad_at_dim(bias, (0, num_heads_unalibied), dim = 0) self.register_buffer('bias', bias, persistent = False) return self.bias class RotaryEmbedding(nn.Module): def __init__( self, dim, use_xpos = False, scale_base = 512, interpolation_factor = 1., base = 10000, base_rescale_factor = 1. ): super().__init__() # proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning # has some connection to NTK literature # https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ base *= base_rescale_factor ** (dim / (dim - 2)) inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) assert interpolation_factor >= 1. self.interpolation_factor = interpolation_factor if not use_xpos: self.register_buffer('scale', None) return scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.scale_base = scale_base self.register_buffer('scale', scale) def forward(self, seq_len, device): t = torch.arange(seq_len, device = device).type_as(self.inv_freq) t = t / self.interpolation_factor freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) if not exists(self.scale): return freqs, 1. power = (torch.arange(seq_len, device = device) - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) return freqs, scale def rotate_half(x): x = rearrange(x, '... (j d) -> ... j d', j = 2) x1, x2 = x.unbind(dim = -2) return torch.cat((-x2, x1), dim = -1) def apply_rotary_pos_emb(t, freqs, scale = 1): seq_len = t.shape[-2] freqs = freqs[-seq_len:, :] return (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale) # norms class Scale(nn.Module): def __init__(self, value, fn): super().__init__() self.value = value self.fn = fn def forward(self, x, **kwargs): out = self.fn(x, **kwargs) def scale_fn(t): return t * self.value if not isinstance(out, tuple): return scale_fn(out) return (scale_fn(out[0]), *out[1:]) class ScaleNorm(nn.Module): def __init__(self, dim, eps = 1e-5): super().__init__() self.eps = eps self.g = nn.Parameter(torch.ones(1) * (dim ** -0.5)) def forward(self, x): norm = torch.norm(x, dim = -1, keepdim = True) return x / norm.clamp(min = self.eps) * self.g class RMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 self.g = nn.Parameter(torch.ones(dim)) def forward(self, x): return F.normalize(x, dim = -1) * self.scale * self.g class SimpleRMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 def forward(self, x): return F.normalize(x, dim = -1) * self.scale # residual and residual gates class Residual(nn.Module): def __init__(self, dim, scale_residual = False, scale_residual_constant = 1.): super().__init__() self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None self.scale_residual_constant = scale_residual_constant def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale if self.scale_residual_constant != 1: residual = residual * self.scale_residual_constant return x + residual class GRUGating(nn.Module): def __init__(self, dim, scale_residual = False, **kwargs): super().__init__() self.gru = nn.GRUCell(dim, dim) self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale gated_output = self.gru( rearrange(x, 'b n d -> (b n) d'), rearrange(residual, 'b n d -> (b n) d') ) return gated_output.reshape_as(x) # token shifting def shift(t, amount, mask = None): if amount == 0: return t else: amount = min(amount, t.shape[1]) if exists(mask): t = t.masked_fill(~mask[..., None], 0.) return pad_at_dim(t, (amount, -amount), dim = - 2, value = 0.) class ShiftTokens(nn.Module): def __init__(self, shifts, fn): super().__init__() self.fn = fn self.shifts = tuple(shifts) def forward(self, x, **kwargs): mask = kwargs.get('mask', None) shifts = self.shifts segments = len(shifts) feats_per_shift = x.shape[-1] // segments splitted = x.split(feats_per_shift, dim = -1) segments_to_shift, rest = splitted[:segments], splitted[segments:] segments_to_shift = list(map(lambda args: shift(*args, mask = mask), zip(segments_to_shift, shifts))) x = torch.cat((*segments_to_shift, *rest), dim = -1) return self.fn(x, **kwargs) # feedforward class GLU(nn.Module): def __init__( self, dim_in, dim_out, activation: Callable, mult_bias = False ): super().__init__() self.act = activation self.proj = nn.Linear(dim_in, dim_out * 2) self.mult_bias = nn.Parameter(torch.ones(dim_out)) if mult_bias else 1. def forward(self, x): x, gate = self.proj(x).chunk(2, dim = -1) return x * self.act(gate) * self.mult_bias class FeedForward(nn.Module): def __init__( self, dim, dim_out = None, mult = 4, glu = False, glu_mult_bias = False, swish = False, relu_squared = False, post_act_ln = False, dropout = 0., no_bias = False, zero_init_output = False ): super().__init__() inner_dim = int(dim * mult) dim_out = default(dim_out, dim) if relu_squared: activation = ReluSquared() elif swish: activation = nn.SiLU() else: activation = nn.GELU() if glu: project_in = GLU(dim, inner_dim, activation, mult_bias = glu_mult_bias) else: project_in = nn.Sequential( nn.Linear(dim, inner_dim, bias = not no_bias), activation ) self.ff = Sequential( project_in, nn.LayerNorm(inner_dim) if post_act_ln else None, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out, bias = not no_bias) ) # init last linear layer to 0 if zero_init_output: init_zero_(self.ff[-1]) def forward(self, x): return self.ff(x) # attention. it is all we need class Attention(nn.Module): def __init__( self, dim, dim_head = DEFAULT_DIM_HEAD, heads = 8, causal = False, flash = False, talking_heads = False, head_scale = False, sparse_topk = None, num_mem_kv = 0, dropout = 0., on_attn = False, gate_values = False, zero_init_output = False, max_attend_past = None, qk_norm = False, qk_norm_groups = 1, qk_norm_scale = 10, qk_norm_dim_scale = False, one_kv_head = False, kv_heads = None, shared_kv = False, value_dim_head = None, tensor_product = False, # https://arxiv.org/abs/2208.06061 cascading_heads = False, add_zero_kv = False, # same as add_zero_attn in pytorch onnxable = False ): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads self.causal = causal self.max_attend_past = max_attend_past assert not (exists(kv_heads) and one_kv_head), 'either attn_one_kv_head is set to True (in which case kv_heads is set to 1), or attn_kv_heads is set, but not both' value_dim_head = default(value_dim_head, dim_head) kv_heads = default(kv_heads, heads) kv_heads = 1 if one_kv_head else kv_heads assert divisible_by(heads, kv_heads) self.kv_heads = kv_heads q_dim = dim_head * heads k_dim = dim_head * kv_heads v_dim = value_dim_head * kv_heads out_dim = value_dim_head * heads self.to_q = nn.Linear(dim, q_dim, bias = False) self.to_k = nn.Linear(dim, k_dim, bias = False) # shared key / values, for further memory savings during inference assert not (shared_kv and value_dim_head != dim_head), 'key and value head dimensions must be equal for shared key / values' self.to_v = nn.Linear(dim, v_dim, bias = False) if not shared_kv else None # relations projection from tp-attention self.to_r = nn.Linear(dim, v_dim, bias = False) if tensor_product else None # add GLU gating for aggregated values, from alphafold2 self.to_v_gate = None if gate_values: self.to_v_gate = nn.Linear(dim, out_dim) nn.init.constant_(self.to_v_gate.weight, 0) nn.init.constant_(self.to_v_gate.bias, 1) # cosine sim attention self.qk_norm = qk_norm self.qk_norm_groups = qk_norm_groups self.qk_norm_scale = qk_norm_scale # whether to use the rmsnorm (equivalent to cosine sim attention when scale is equal to 1) - https://arxiv.org/abs/2302.05442 self.qk_norm_dim_scale = qk_norm_dim_scale self.qk_norm_q_scale = self.qk_norm_k_scale = 1 if qk_norm and qk_norm_dim_scale: self.qk_norm_q_scale = nn.Parameter(torch.ones(dim_head)) self.qk_norm_k_scale = nn.Parameter(torch.ones(dim_head)) assert (not qk_norm) or divisible_by(dim_head, qk_norm_groups), 'dimension per attention head must be divisible by the qk norm groups' assert not (qk_norm and (dim_head // qk_norm_groups) <= 2), 'the group dimension may be too small (2 was too small in my tests, but 4 still works, surprisingly)' # attend class - includes core attention algorithm + talking heads self.attend = Attend( heads = heads, causal = causal, talking_heads = talking_heads, dropout = dropout, sparse_topk = sparse_topk, qk_norm = qk_norm, scale = qk_norm_scale if qk_norm else self.scale, add_zero_kv = add_zero_kv, flash = flash, onnxable = onnxable ) # head scaling self.head_scale = head_scale if head_scale: self.head_scale_params = nn.Parameter(torch.ones(1, heads, 1, 1)) # explicit topk sparse attention self.sparse_topk = sparse_topk # add memory key / values self.num_mem_kv = num_mem_kv if num_mem_kv > 0: self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) # attention on attention self.attn_on_attn = on_attn self.to_out = nn.Sequential(nn.Linear(out_dim, dim * 2, bias = False), nn.GLU()) if on_attn else nn.Linear(out_dim, dim, bias = False) # init output projection 0 if zero_init_output: init_zero_(self.to_out) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, rel_pos = None, rotary_pos_emb = None, prev_attn = None, mem = None ): b, n, _, h, kv_h, head_scale, device, has_context = *x.shape, self.heads, self.kv_heads, self.head_scale, x.device, exists(context) kv_input = default(context, x) q_input = x k_input = kv_input v_input = kv_input r_input = x if exists(mem): k_input = torch.cat((mem, k_input), dim = -2) v_input = torch.cat((mem, v_input), dim = -2) q = self.to_q(q_input) k = self.to_k(k_input) v = self.to_v(v_input) if exists(self.to_v) else k r = self.to_r(r_input) if exists(self.to_r) else None q = rearrange(q, 'b n (h d) -> b h n d', h = h) k, v, r = map(lambda t: maybe(rearrange)(t, 'b n (h d) -> b h n d', h = kv_h), (k, v, r)) if self.qk_norm: qk_l2norm = partial(l2norm, groups = self.qk_norm_groups) q, k = map(qk_l2norm, (q, k)) q = q * self.qk_norm_q_scale k = k * self.qk_norm_k_scale if exists(rotary_pos_emb) and not has_context: freqs, xpos_scale = rotary_pos_emb l = freqs.shape[-1] q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale ** -1.) if exists(xpos_scale) else (1., 1.) (ql, qr), (kl, kr), (vl, vr) = map(lambda t: (t[..., :l], t[..., l:]), (q, k, v)) ql, kl, vl = map(lambda arg: apply_rotary_pos_emb(arg[0], freqs, arg[1]), ((ql, q_xpos_scale), (kl, k_xpos_scale), (vl, k_xpos_scale))) q, k, v = map(lambda t: torch.cat(t, dim = -1), ((ql, qr), (kl, kr), (vl, vr))) input_mask = context_mask if has_context else mask if self.num_mem_kv > 0: mem_k, mem_v = map(lambda t: repeat(t, 'h n d -> b h n d', b = b), (self.mem_k, self.mem_v)) if self.qk_norm: mem_k = l2norm(mem_k) mem_k = mem_k * self.qk_norm_k_scale k = torch.cat((mem_k, k), dim = -2) v = torch.cat((mem_v, v), dim = -2) if exists(input_mask): input_mask = pad_at_dim(input_mask, (self.num_mem_kv, 0), dim = -1, value = True) i, j = map(lambda t: t.shape[-2], (q, k)) # determine masking max_neg_value(q) masks = [] final_attn_mask = None if exists(input_mask): input_mask = rearrange(input_mask, 'b j -> b 1 1 j') masks.append(~input_mask) if exists(attn_mask): assert 2 <= attn_mask.ndim <= 4, 'attention mask must have greater than 2 dimensions but less than or equal to 4' if attn_mask.ndim == 2: attn_mask = rearrange(attn_mask, 'i j -> 1 1 i j') elif attn_mask.ndim == 3: attn_mask = rearrange(attn_mask, 'h i j -> 1 h i j') masks.append(~attn_mask) if exists(self.max_attend_past): range_q = torch.arange(j - i, j, device = device) range_k = torch.arange(j, device = device) dist = rearrange(range_q, 'i -> 1 1 i 1') - rearrange(range_k, 'j -> 1 1 1 j') max_attend_past_mask = dist > self.max_attend_past masks.append(max_attend_past_mask) if len(masks) > 0: final_attn_mask = ~or_reduce(masks) # prepare relative positional bias, if needed attn_bias = None if exists(rel_pos): attn_bias = rel_pos(i, j) # attention is all we need out, intermediates = self.attend( q, k, v, mask = final_attn_mask, attn_bias = attn_bias, prev_attn = prev_attn ) # https://arxiv.org/abs/2208.06061 proposes to add a residual for better gradients if exists(r): out = out * r + out # normformer scaling of heads if head_scale: out = out * self.head_scale_params # merge heads out = rearrange(out, 'b h n d -> b n (h d)') # alphafold2 styled gating of the values if exists(self.to_v_gate): gates = self.to_v_gate(x) out = out * gates.sigmoid() # combine the heads out = self.to_out(out) if exists(mask): mask = rearrange(mask, 'b n -> b n 1') out = out.masked_fill(~mask, 0.) return out, intermediates class AttentionLayers(nn.Module): def __init__( self, dim, depth, heads = 8, causal = False, cross_attend = False, only_cross = False, use_scalenorm = False, use_rmsnorm = False, use_simple_rmsnorm = False, alibi_pos_bias = False, alibi_num_heads = None, rel_pos_bias = False, rel_pos_num_buckets = 32, rel_pos_max_distance = 128, dynamic_pos_bias = False, dynamic_pos_bias_log_distance = False, dynamic_pos_bias_mlp_depth = 2, dynamic_pos_bias_norm = False, rotary_pos_emb = False, rotary_emb_dim = None, rotary_xpos = False, rotary_interpolation_factor = 1., rotary_xpos_scale_base = 512, rotary_base_rescale_factor = 1., custom_layers = None, sandwich_coef = None, par_ratio = None, residual_attn = False, cross_residual_attn = False, macaron = False, pre_norm = True, pre_norm_has_final_norm = True, gate_residual = False, scale_residual = False, scale_residual_constant = 1., deepnorm = False, shift_tokens = 0, sandwich_norm = False, resi_dual = False, resi_dual_scale = 1., zero_init_branch_output = False, layer_dropout = 0., cross_attn_tokens_dropout = 0., **kwargs ): super().__init__() rotary_pos_emb = rotary_pos_emb or rotary_xpos ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) attn_kwargs, kwargs = groupby_prefix_and_trim('attn_', kwargs) dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) self.dim = dim self.depth = depth self.layers = nn.ModuleList([]) self.has_pos_emb = rel_pos_bias or rotary_pos_emb rotary_emb_dim = max(default(rotary_emb_dim, dim_head // 2), 32) assert not (rotary_xpos and not causal), 'rotary xpos is not compatible with bidirectional attention' self.rotary_pos_emb = RotaryEmbedding(rotary_emb_dim, use_xpos = rotary_xpos, scale_base = rotary_xpos_scale_base, interpolation_factor = rotary_interpolation_factor, base_rescale_factor = rotary_base_rescale_factor) if rotary_pos_emb else None assert not (alibi_pos_bias and rel_pos_bias), 'you can only choose Alibi positional bias or T5 relative positional bias, not both' assert rel_pos_num_buckets <= rel_pos_max_distance, 'number of relative position buckets must be less than the relative position max distance' # relative positional bias flash_attn = attn_kwargs.get('flash', False) assert (int(rel_pos_bias) + int(dynamic_pos_bias) + int(alibi_pos_bias)) <= 1, 'you can only choose up to one of t5, alibi, or dynamic positional bias' self.rel_pos = None if rel_pos_bias: assert not flash_attn, 'flash attention not compatible with t5 relative positional bias' self.rel_pos = RelativePositionBias(scale = dim_head ** 0.5, causal = causal, heads = heads, num_buckets = rel_pos_num_buckets, max_distance = rel_pos_max_distance) elif dynamic_pos_bias: assert not flash_attn, 'flash attention not compatible with dynamic positional bias' self.rel_pos = DynamicPositionBias(dim = dim // 4, heads = heads, log_distance = dynamic_pos_bias_log_distance, depth = dynamic_pos_bias_mlp_depth, norm = dynamic_pos_bias_norm) elif alibi_pos_bias: alibi_num_heads = default(alibi_num_heads, heads) assert alibi_num_heads <= heads, 'number of ALiBi heads must be less than the total number of heads' self.rel_pos = AlibiPositionalBias(heads = alibi_num_heads, total_heads = heads) # determine deepnorm and residual scale if deepnorm: assert scale_residual_constant == 1, 'scale residual constant is being overridden by deep norm settings' pre_norm = sandwich_norm = resi_dual = False scale_residual = True scale_residual_constant = (2 * depth) ** 0.25 assert (int(sandwich_norm) + int(resi_dual)) <= 1, 'either sandwich norm or resiDual is selected, but not both' assert not (not pre_norm and sandwich_norm), 'sandwich norm cannot be used when not using prenorm' if resi_dual: pre_norm = False self.pre_norm = pre_norm self.sandwich_norm = sandwich_norm self.resi_dual = resi_dual assert 0 < resi_dual_scale <= 1., 'resiDual prenorm residual must be scaled by a factor greater than 0 and less than or equal to 1.' self.resi_dual_scale = resi_dual_scale self.residual_attn = residual_attn self.cross_residual_attn = cross_residual_attn assert not (flash_attn and (residual_attn or cross_residual_attn)), 'flash attention is not compatible with residual attention' self.cross_attend = cross_attend assert (int(use_scalenorm) + int(use_rmsnorm) + int(use_simple_rmsnorm)) <= 1, 'you can only use either scalenorm, rmsnorm, or simple rmsnorm' if use_scalenorm: norm_class = ScaleNorm elif use_rmsnorm: norm_class = RMSNorm elif use_simple_rmsnorm: norm_class = SimpleRMSNorm else: norm_class = nn.LayerNorm norm_fn = partial(norm_class, dim) if cross_attend and not only_cross: default_block = ('a', 'c', 'f') elif cross_attend and only_cross: default_block = ('c', 'f') else: default_block = ('a', 'f') if macaron: default_block = ('f',) + default_block # zero init if zero_init_branch_output: attn_kwargs = {**attn_kwargs, 'zero_init_output': True} ff_kwargs = {**ff_kwargs, 'zero_init_output': True} # calculate layer block order if exists(custom_layers): layer_types = custom_layers elif exists(par_ratio): par_depth = depth * len(default_block) assert 1 < par_ratio <= par_depth, 'par ratio out of range' default_block = tuple(filter(not_equals('f'), default_block)) par_attn = par_depth // par_ratio depth_cut = par_depth * 2 // 3 # 2 / 3 attention layer cutoff suggested by PAR paper par_width = (depth_cut + depth_cut // par_attn) // par_attn assert len(default_block) <= par_width, 'default block is too large for par_ratio' par_block = default_block + ('f',) * (par_width - len(default_block)) par_head = par_block * par_attn layer_types = par_head + ('f',) * (par_depth - len(par_head)) elif exists(sandwich_coef): assert sandwich_coef > 0 and sandwich_coef <= depth, 'sandwich coefficient should be less than the depth' layer_types = ('a',) * sandwich_coef + default_block * (depth - sandwich_coef) + ('f',) * sandwich_coef else: layer_types = default_block * depth self.layer_types = layer_types self.num_attn_layers = len(list(filter(equals('a'), layer_types))) # stochastic depth self.layer_dropouts = cast_tuple(layer_dropout, len(layer_types)) # structured dropout for cross attending self.cross_attn_tokens_dropout = cross_attn_tokens_dropout # calculate token shifting shift_tokens = cast_tuple(shift_tokens, len(layer_types)) # whether it has post norm self.final_norm = norm_fn() if pre_norm or resi_dual else nn.Identity() # iterate and construct layers for ind, (layer_type, layer_shift_tokens) in enumerate(zip(self.layer_types, shift_tokens)): ind == (len(self.layer_types) - 1) if layer_type == 'a': layer = Attention(dim, heads = heads, causal = causal, **attn_kwargs) elif layer_type == 'c': layer = Attention(dim, heads = heads, **attn_kwargs) elif layer_type == 'f': layer = FeedForward(dim, **ff_kwargs) layer = layer if not macaron else Scale(0.5, layer) else: raise Exception(f'invalid layer type {layer_type}') if layer_shift_tokens > 0: shift_range_upper = layer_shift_tokens + 1 shift_range_lower = -layer_shift_tokens if not causal else 0 layer = ShiftTokens(range(shift_range_lower, shift_range_upper), layer) residual_fn = GRUGating if gate_residual else Residual residual = residual_fn(dim, scale_residual = scale_residual, scale_residual_constant = scale_residual_constant) pre_branch_norm = norm_fn() if pre_norm else None post_branch_norm = norm_fn() if sandwich_norm else None post_main_norm = norm_fn() if not pre_norm else None norms = nn.ModuleList([ pre_branch_norm, post_branch_norm, post_main_norm ]) self.layers.append(nn.ModuleList([ norms, layer, residual ])) if deepnorm: init_gain = (8 * depth) ** -0.25 deepnorm_init(self, init_gain) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, self_attn_context_mask = None, mems = None, return_hiddens = False ): assert not (self.cross_attend ^ exists(context)), 'context must be passed in if cross_attend is set to True' hiddens = [] layer_hiddens = [] intermediates = [] prev_attn = None prev_cross_attn = None mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers rotary_pos_emb = None if exists(self.rotary_pos_emb): max_rotary_emb_length = max(list(map(lambda m: (m.shape[1] if exists(m) else 0) + x.shape[1], mems))) rotary_pos_emb = self.rotary_pos_emb(max_rotary_emb_length, x.device) outer_residual = x * self.resi_dual_scale for ind, (layer_type, (norm, block, residual_fn), layer_dropout) in enumerate(zip(self.layer_types, self.layers, self.layer_dropouts)): ind == (len(self.layers) - 1) if self.training and layer_dropout > 0. and random() < layer_dropout: continue if layer_type == 'a': if return_hiddens: hiddens.append(x) layer_mem = mems.pop(0) if mems else None if layer_type == 'c': if self.training and self.cross_attn_tokens_dropout > 0.: context, context_mask = dropout_seq(context, context_mask, self.cross_attn_tokens_dropout) inner_residual = x if return_hiddens: layer_hiddens.append(x) pre_norm, post_branch_norm, post_main_norm = norm if exists(pre_norm): x = pre_norm(x) if layer_type == 'a': out, inter = block(x, mask = mask, context_mask = self_attn_context_mask, attn_mask = attn_mask, rel_pos = self.rel_pos, rotary_pos_emb = rotary_pos_emb, prev_attn = prev_attn, mem = layer_mem) elif layer_type == 'c': out, inter = block(x, context = context, mask = mask, context_mask = context_mask, prev_attn = prev_cross_attn) elif layer_type == 'f': out = block(x) if self.resi_dual: outer_residual = outer_residual + out * self.resi_dual_scale if exists(post_branch_norm): out = post_branch_norm(out) x = residual_fn(out, inner_residual) if layer_type in ('a', 'c') and return_hiddens: intermediates.append(inter) if layer_type == 'a' and self.residual_attn: prev_attn = inter.pre_softmax_attn elif layer_type == 'c' and self.cross_residual_attn: prev_cross_attn = inter.pre_softmax_attn if exists(post_main_norm): x = post_main_norm(x) if return_hiddens: layer_hiddens.append(x) if self.resi_dual: x = x + self.final_norm(outer_residual) else: x = self.final_norm(x) if return_hiddens: intermediates = LayerIntermediates( hiddens = hiddens, attn_intermediates = intermediates, layer_hiddens = layer_hiddens ) return x, intermediates return x class Encoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on encoder' super().__init__(causal = False, **kwargs) class Decoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on decoder' super().__init__(causal = True, **kwargs) class CrossAttender(AttentionLayers): def __init__(self, **kwargs): super().__init__(cross_attend = True, only_cross = True, **kwargs) class ViTransformerWrapper(nn.Module): def __init__( self, *, image_size, patch_size, attn_layers, channels = 3, num_classes = None, post_emb_norm = False, emb_dropout = 0. ): super().__init__() assert isinstance(attn_layers, Encoder), 'attention layers must be an Encoder' assert divisible_by(image_size, patch_size), 'image dimensions must be divisible by the patch size' dim = attn_layers.dim num_patches = (image_size // patch_size) ** 2 patch_dim = channels * patch_size ** 2 self.patch_size = patch_size self.pos_embedding = nn.Parameter(torch.randn(1, num_patches, dim)) self.patch_to_embedding = nn.Sequential( nn.LayerNorm(patch_dim), nn.Linear(patch_dim, dim), nn.LayerNorm(dim) ) self.post_emb_norm = nn.LayerNorm(dim) if post_emb_norm else nn.Identity() self.dropout = nn.Dropout(emb_dropout) self.attn_layers = attn_layers self.mlp_head = nn.Linear(dim, num_classes) if exists(num_classes) else nn.Identity() def forward( self, img, return_embeddings = False ): p = self.patch_size x = rearrange(img, 'b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = p, p2 = p) x = self.patch_to_embedding(x) n = x.shape[1] x = x + self.pos_embedding[:, :n] x = self.post_emb_norm(x) x = self.dropout(x) x = self.attn_layers(x) if not exists(self.mlp_head) or return_embeddings: return x x = x.mean(dim = -2) return self.mlp_head(x) class Transformer(nn.Module): def __init__( self, *, num_tokens, max_seq_len, attn_layers, emb_dim = None, max_mem_len = 0, shift_mem_down = 0, emb_dropout = 0., post_emb_norm = False, num_memory_tokens = None, tie_embedding = False, logits_dim = None, use_abs_pos_emb = True, scaled_sinu_pos_emb = False, l2norm_embed = False, emb_frac_gradient = 1., # GLM-130B and Cogview successfully used this, set at 0.1 attn_z_loss_weight = 1e-4 ): super().__init__() assert isinstance(attn_layers, AttentionLayers), 'attention layers must be one of Encoder or Decoder' dim = attn_layers.dim emb_dim = default(emb_dim, dim) self.emb_dim = emb_dim self.num_tokens = num_tokens self.max_seq_len = max_seq_len self.max_mem_len = max_mem_len self.shift_mem_down = shift_mem_down self.l2norm_embed = l2norm_embed self.token_emb = TokenEmbedding(emb_dim, num_tokens, l2norm_embed = l2norm_embed) if not (use_abs_pos_emb and not attn_layers.has_pos_emb): self.pos_emb = always(0) elif scaled_sinu_pos_emb: self.pos_emb = ScaledSinusoidalEmbedding(emb_dim) else: self.pos_emb = AbsolutePositionalEmbedding(emb_dim, max_seq_len, l2norm_embed = l2norm_embed) self.emb_frac_gradient = emb_frac_gradient # fraction of the gradient that should go to the embedding, https://arxiv.org/abs/2105.13290 self.post_emb_norm = nn.LayerNorm(emb_dim) if post_emb_norm else nn.Identity() self.emb_dropout = nn.Dropout(emb_dropout) self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() self.attn_layers = attn_layers self.init_() logits_dim = default(logits_dim, num_tokens) self.to_logits = nn.Linear(dim, logits_dim) if not tie_embedding else lambda t: t @ self.token_emb.emb.weight.t() # memory tokens (like [cls]) from Memory Transformers paper num_memory_tokens = default(num_memory_tokens, 0) self.num_memory_tokens = num_memory_tokens if num_memory_tokens > 0: self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim)) def init_(self): if self.l2norm_embed: nn.init.normal_(self.token_emb.emb.weight, std = 1e-5) if not isinstance(self.pos_emb, always): nn.init.normal_(self.pos_emb.emb.weight, std = 1e-5) return nn.init.kaiming_normal_(self.token_emb.emb.weight) def forward( self, x, return_embeddings = False, return_logits_and_embeddings = False, return_intermediates = False, mask = None, return_mems = False, return_attn = False, mems = None, pos = None, prepend_embeds = None, sum_embeds = None, return_attn_z_loss = False, attn_z_loss_weight = 1e-4, **kwargs ): b, n, device, num_mem, emb_frac_gradient = *x.shape, x.device, self.num_memory_tokens, self.emb_frac_gradient return_hiddens = return_mems | return_attn | return_intermediates | return_attn_z_loss # absolute positional embedding external_pos_emb = exists(pos) and pos.dtype != torch.long pos_emb = self.pos_emb(x, pos = pos) if not external_pos_emb else pos x = self.token_emb(x) + pos_emb # for summing embeddings passed externally - needs this for self-conditioning in non-autoregressive training if exists(sum_embeds): x = x + sum_embeds # post embedding norm, purportedly leads to greater stabilization x = self.post_emb_norm(x) # whether to append embeds, as in PaLI, for image embeddings if exists(prepend_embeds): prepend_seq, prepend_dim = prepend_embeds.shape[1:] assert prepend_dim == x.shape[-1], 'prepended embeddings need to have same dimensions as text model dimensions' x = torch.cat((prepend_embeds, x), dim = -2) # whether to reduce the gradient going to the embedding, from cogview paper, corroborated by GLM-130B model if emb_frac_gradient < 1: assert emb_frac_gradient > 0 x = x * emb_frac_gradient + x.detach() * (1 - emb_frac_gradient) # embedding dropout x = self.emb_dropout(x) x = self.project_emb(x) if num_mem > 0: mem = repeat(self.memory_tokens, 'n d -> b n d', b = b) x = torch.cat((mem, x), dim = 1) # auto-handle masking after appending memory tokens if exists(mask): mask = pad_at_dim(mask, (num_mem, 0), dim = -1, value = True) if self.shift_mem_down and exists(mems): mems_l, mems_r = mems[:self.shift_mem_down], mems[self.shift_mem_down:] mems = [*mems_r, *mems_l] if return_hiddens: x, intermediates = self.attn_layers(x, mask = mask, mems = mems, return_hiddens = True, **kwargs) else: x = self.attn_layers(x, mask = mask, mems = mems, **kwargs) mem, x = x[:, :num_mem], x[:, num_mem:] if return_logits_and_embeddings: out = (self.to_logits(x), x) elif return_embeddings: out = x else: out = self.to_logits(x) if return_attn_z_loss: pre_softmax_attns = list(map(lambda t: t.pre_softmax_attn, intermediates.attn_intermediates)) intermediates.attn_z_loss = calc_z_loss(pre_softmax_attns, weight = attn_z_loss_weight) return_intermediates = True if return_intermediates: return out, intermediates if return_mems: hiddens = intermediates.hiddens new_mems = list(map(lambda pair: torch.cat(pair, dim = -2), zip(mems, hiddens))) if exists(mems) else hiddens new_mems = list(map(lambda t: t[..., -self.max_mem_len:, :].detach(), new_mems)) return out, new_mems if return_attn: attn_maps = list(map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)) return out, attn_maps return out
Andromeda-master
Andromeda/core/transformer.py
import torch import triton import triton.language as tl @triton.jit def max_fn(x, y): return tl.math.max(x, y) @triton.jit def _fwd_kernel( Q, K, V, sm_scale, L, Out, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, stride_oz, stride_oh, stride_om, stride_on, Z, H, N_CTX, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, IS_CAUSAL: tl.constexpr, ): start_m = tl.program_id(0) off_hz = tl.program_id(1) qvk_offset = off_hz * stride_qh Q_block_ptr = tl.make_block_ptr( base=Q + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_qm, stride_qk), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) K_block_ptr = tl.make_block_ptr( base=K + qvk_offset, shape=(BLOCK_DMODEL, N_CTX), strides=(stride_kk, stride_kn), offsets=(0, 0), block_shape=(BLOCK_DMODEL, BLOCK_N), order=(0, 1) ) V_block_ptr = tl.make_block_ptr( base=V + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_vk, stride_vn), offsets=(0, 0), block_shape=(BLOCK_N, BLOCK_DMODEL), order=(1, 0) ) # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) # initialize pointer to m and l m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") l_i = tl.zeros([BLOCK_M], dtype=tl.float32) acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # scale sm_scale by log_2(e) and use # 2^x instead of exp in the loop because CSE and LICM # don't work as expected with `exp` in the loop qk_scale = sm_scale * 1.44269504 # load q: it will stay in SRAM throughout q = tl.load(Q_block_ptr) q = (q * qk_scale).to(tl.float16) # loop over k, v and update accumulator lo = 0 hi = (start_m + 1) * BLOCK_M if IS_CAUSAL else N_CTX for start_n in range(lo, hi, BLOCK_N): # -- load k, v -- k = tl.load(K_block_ptr) v = tl.load(V_block_ptr) # -- compute qk --- qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) if IS_CAUSAL: qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, float("-inf")) qk += tl.dot(q, k) # -- compute scaling constant --- m_i_new = tl.maximum(m_i, tl.max(qk, 1)) alpha = tl.math.exp2(m_i - m_i_new) p = tl.math.exp2(qk - m_i_new[:, None]) # -- scale and update acc -- acc_scale = l_i * 0 + alpha # workaround some compiler bug acc *= acc_scale[:, None] acc += tl.dot(p.to(tl.float16), v) # -- update m_i and l_i -- l_i = l_i * alpha + tl.sum(p, 1) m_i = m_i_new # update pointers K_block_ptr = tl.advance(K_block_ptr, (0, BLOCK_N)) V_block_ptr = tl.advance(V_block_ptr, (BLOCK_N, 0)) # write back l and m acc = acc / l_i[:, None] l_ptrs = L + off_hz * N_CTX + offs_m tl.store(l_ptrs, m_i + tl.math.log2(l_i)) # write back O O_block_ptr = tl.make_block_ptr( base=Out + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_om, stride_on), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) tl.store(O_block_ptr, acc.to(tl.float16)) @triton.jit def _bwd_preprocess( Out, DO, Delta, BLOCK_M: tl.constexpr, D_HEAD: tl.constexpr, ): off_m = tl.program_id(0) * BLOCK_M + tl.arange(0, BLOCK_M) off_n = tl.arange(0, D_HEAD) # load o = tl.load(Out + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) do = tl.load(DO + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) # compute delta = tl.sum(o * do, axis=1) # write-back tl.store(Delta + off_m, delta) @triton.jit def _bwd_kernel( Q, K, V, sm_scale, Out, DO, DQ, DK, DV, L, D, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, num_block, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, CAUSAL: tl.constexpr, ): off_hz = tl.program_id(0) off_z = off_hz // H off_h = off_hz % H qk_scale = sm_scale * 1.44269504 # offset pointers for batch/head Q += off_z * stride_qz + off_h * stride_qh K += off_z * stride_qz + off_h * stride_qh V += off_z * stride_qz + off_h * stride_qh DO += off_z * stride_qz + off_h * stride_qh DQ += off_z * stride_qz + off_h * stride_qh DK += off_z * stride_qz + off_h * stride_qh DV += off_z * stride_qz + off_h * stride_qh for start_n in range(0, num_block): if CAUSAL: lo = start_n * BLOCK_M else: lo = 0 # initialize row/col offsets offs_qm = lo + tl.arange(0, BLOCK_M) offs_n = start_n * BLOCK_M + tl.arange(0, BLOCK_M) offs_m = tl.arange(0, BLOCK_N) offs_k = tl.arange(0, BLOCK_DMODEL) # initialize pointers to value-like data q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) k_ptrs = K + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) v_ptrs = V + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk) do_ptrs = DO + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) dq_ptrs = DQ + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) # pointer to row-wise quantities in value-like data D_ptrs = D + off_hz * N_CTX l_ptrs = L + off_hz * N_CTX # initialize dv amd dk dv = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) dk = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # k and v stay in SRAM throughout k = tl.load(k_ptrs) v = tl.load(v_ptrs) # loop over rows for start_m in range(lo, num_block * BLOCK_M, BLOCK_M): offs_m_curr = start_m + offs_m # load q, k, v, do on-chip q = tl.load(q_ptrs) # recompute p = softmax(qk, dim=-1).T if CAUSAL: qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), float(0.), float("-inf")) else: qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, tl.trans(k)) qk *= qk_scale l_i = tl.load(l_ptrs + offs_m_curr) p = tl.math.exp2(qk - l_i[:, None]) # compute dv do = tl.load(do_ptrs) dv += tl.dot(tl.trans(p.to(Q.dtype.element_ty)), do) # compute dp = dot(v, do) Di = tl.load(D_ptrs + offs_m_curr) dp = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - Di[:, None] dp += tl.dot(do, tl.trans(v)) # compute ds = p * (dp - delta[:, None]) ds = p * dp * sm_scale # compute dk = dot(ds.T, q) dk += tl.dot(tl.trans(ds.to(Q.dtype.element_ty)), q) # compute dq dq = tl.load(dq_ptrs) dq += tl.dot(ds.to(Q.dtype.element_ty), k) tl.store(dq_ptrs, dq) # increment pointers dq_ptrs += BLOCK_M * stride_qm q_ptrs += BLOCK_M * stride_qm do_ptrs += BLOCK_M * stride_qm # write-back dv_ptrs = DV + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk) dk_ptrs = DK + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) tl.store(dv_ptrs, dv) tl.store(dk_ptrs, dk) empty = torch.empty(128, device="cuda") class _attention(torch.autograd.Function): @staticmethod def forward(ctx, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, causal, sm_scale): # shape constraints Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1] assert Lq == Lk and Lk == Lv assert Lk in {16, 32, 64, 128} o = torch.empty_like(q) BLOCK_M = 128 BLOCK_N = 64 grid = (triton.cdiv(q.shape[2], BLOCK_M), q.shape[0] * q.shape[1], 1) L = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) num_warps = 4 if Lk <= 64 else 8 _fwd_kernel[grid]( q, k, v, sm_scale, L, o, q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), o.stride(0), o.stride(1), o.stride(2), o.stride(3), q.shape[0], q.shape[1], q.shape[2], BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, BLOCK_DMODEL=Lk, IS_CAUSAL=causal, num_warps=num_warps, num_stages=4) ctx.save_for_backward(q, k, v, o, L) ctx.grid = grid ctx.sm_scale = sm_scale ctx.BLOCK_DMODEL = Lk ctx.causal = causal return o @staticmethod def backward(ctx, do): BLOCK = 128 q, k, v, o, L = ctx.saved_tensors do = do.contiguous() dq = torch.zeros_like(q, dtype=torch.float32) dk = torch.empty_like(k) dv = torch.empty_like(v) delta = torch.empty_like(L) _bwd_preprocess[(ctx.grid[0] * ctx.grid[1], )]( o, do, delta, BLOCK_M=BLOCK, D_HEAD=ctx.BLOCK_DMODEL, ) _bwd_kernel[(ctx.grid[1],)]( q, k, v, ctx.sm_scale, o, do, dq, dk, dv, L, delta, q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), q.shape[0], q.shape[1], q.shape[2], ctx.grid[0], BLOCK_M=BLOCK, BLOCK_N=BLOCK, BLOCK_DMODEL=ctx.BLOCK_DMODEL, num_warps=8, CAUSAL=ctx.causal, num_stages=1, ) return dq, dk, dv, None, None attention = _attention.apply
Andromeda-master
Andromeda/core/flash.py
import torch # This is the unfused version of StableAdamW. It is slower than the fused version (coming). class StableAdamWUnfused(torch.optim.Optimizer): def __init__( self, params, lr=0.002, weight_decay=0.2, betas=(0.9, 0.99), eps=1e-8, clip_thresh=1.0, precision="amp_bfloat16", custom_scalar=65536, ): beta1, beta2 = betas[0], betas[1] defaults = dict(lr=lr, weight_decay=weight_decay, beta1=beta1, beta2=beta2) super(StableAdamWUnfused, self).__init__(params, defaults) self.eps = eps self.d = clip_thresh # Set precision to "custom_fp16" if you want to use a fixed loss scalar, custom_scalar, which is divided out in the update step. # If you do this, call (custom_scalar * loss).backward() instead of loss.backward(). self.precision = precision self.custom_scaler = custom_scalar for group in self.param_groups: group["step"] = 1.0 print("Using StableAdamWUnfused-v1") def __setstate__(self, state): super(StableAdamWUnfused, self).__setstate__(state) def step(self, closure=None): if closure is not None: closure() for group in self.param_groups: lr = group["lr"] weight_decay = group["weight_decay"] beta1 = group["beta1"] beta2 = group["beta2"] step = group["step"] for p in group["params"]: if p.grad is None: continue theta = p.data param_state = self.state[p] if self.precision == "custom_fp16": g = p.grad.data / self.custom_scaler if torch.any(torch.isnan(g) | torch.isinf(g)): continue else: g = p.grad.data if "exp_avg" not in param_state: v = param_state["exp_avg"] = torch.zeros_like(theta) u = param_state["exp_avg_sq"] = torch.zeros_like(theta) else: v = param_state["exp_avg"] u = param_state["exp_avg_sq"] beta1hat = beta1 * (1 - beta1 ** (step - 1)) / (1 - beta1**step) beta2hat = beta2 * (1 - beta2 ** (step - 1)) / (1 - beta2**step) v = v.mul_(beta1hat).add_(g, alpha=1.0 - beta1hat) u = u.mul_(beta2hat).addcmul_(g, g, value=1.0 - beta2hat) denominator = u.sqrt().add_(self.eps) # StableAdamW = AdamW + update clipping (https://arxiv.org/abs/1804.04235) applied tensor-wise. rms = ( torch.div( g.pow(2), torch.maximum(u, (self.eps**2) * torch.ones_like(u)) ) .mean() .sqrt() .item() ) theta = theta.mul_(1.0 - lr * weight_decay).addcdiv_( v, denominator, value=-lr * (1.0 / max(1.0, rms / self.d)) ) # save current params param_state["exp_avg"] = v param_state["exp_avg_sq"] = u group["step"] = step + 1
Andromeda-master
Andromeda/utils/stable_adamw.py
Andromeda-master
Andromeda/utils/__init__.py
import torch # from palm_rlhf_pytorch.palm import LayerNorm from torch.nn import LayerNorm from torch.optim import AdamW # from palm.utils import print_main from Andromeda.utils.helpers import print_main from Andromeda.utils.stable_adamw import StableAdamWUnfused # optimizers def decoupled_optimizer( model: torch.nn.Module, learning_rate: float, weight_decay: float = 0.1, beta_1: float = 0.90, beta_2: float = 0.95, optimizer_type: str = "adamw", use_fsdp: bool = True, ): """ Decouples the optimizer from the training process. This function sets up the optimizer for the model by creating two groups of parameters: one for weight decay and one without weight decay. Then, it initializes the optimizer with these two groups of parameters. Args: model (Module): The model whose parameters are optimized. learning_rate (float): The learning rate for the optimizer. weight_decay (float): The weight decay for the optimizer. beta_1 (float): The exponential decay rate for the 1st moment estimates. beta_2 (float): The exponential decay rate for the 2nd moment estimates. optimizer_type (str): The type of the optimizer. Can be 'lion', 'adamw', or 'stable_adamw'. use_fsdp (bool, optional): If True, the optimizer will work with fully sharded data parallelism. Defaults to True. accelerator (Accelerator, optional): The accelerator from HuggingFace's Accelerate library. Defaults to None. Returns: Optimizer: The initialized optimizer. Raises: ValueError: If the optimizer type is not 'lion', 'adamw' or 'stable_adamw'. """ print_main(f"Using {optimizer_type} optimizer") # Create an empty dictionary called param_dict to store the model's named parameters. param_dict = {} # Iterate over the model's named parameters and populate the param_dict with key-value pairs. for param_name, param in model.named_parameters(): print_main(param_name) param_dict[param_name] = param # Separate the model's named modules into two groups: decay and no_decay. # Create an empty list to store the names of the LayerNorm and Embedding layer weights with no weight decay. no_decay = [] if use_fsdp: exclude_module = "_fsdp_wrapped_module.token_emb" else: exclude_module = "token_emb" # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of any of the desired types (LayerNorm or torch.nn.Embedding). for ndim in [LayerNorm, torch.nn.Embedding]: if isinstance(module, ndim): # If torch.nn.Embedding, append its name with a ".weight" suffix to the no_decay list. if module_name == exclude_module: no_decay.append(f"{module_name}.weight") else: # If the module is an instance of LayerNorm no_decay.append(f"{module_name}.gamma") # Exit the inner loop since the desired module has been found. break # Create an empty list to store the names of the Linear layer weights with weight decay. decay = [] # Iterate through the named modules of the model. for module_name, module in model.named_modules(): # Check if the current module is an instance of the desired type (torch.nn.Linear). for ndim in [torch.nn.Linear]: if isinstance(module, ndim): # If the module is an instance of torch.nn.Linear, append its name with a ".weight" suffix to the decay list. decay.append(f"{module_name}.weight") # Exit the inner loop since the desired module has been found. break # Create two separate lists of model parameters: decay_param and no_decay_param. # The decay_param list contains the parameters that should have weight decay applied. # The no_decay_param list contains the parameters that should not have weight decay applied, excluding the 'to_logits.weight' parameter. # Create an empty list called decay_param to store the parameters with weight decay. decay_param = [] if use_fsdp: exclude_param = "_fsdp_wrapped_module.to_logits.weight" else: exclude_param = "to_logits.weight" # Iterate over the decay list, which contains the names of the parameters with weight decay. for param in decay: # Check if the current parameter is not 'to_logits.weight'. # Append the corresponding parameter from param_dict to the decay_param list. if param != exclude_param: decay_param.append(param_dict[param]) # Create an empty list called no_decay_param to store the parameters without weight decay. no_decay_param = [] # Iterate over the no_decay list, which contains the names of the parameters without weight decay. for param in no_decay: # Append the corresponding parameter from param_dict to the no_decay_param list. no_decay_param.append(param_dict[param]) # Create a list called grouped_params that contains two dictionaries. # The first dictionary has the decay_param list and the corresponding weight_decay value. # The second dictionary has the no_decay_param list and a weight_decay value of 0.0. grouped_params = [ {"params": decay_param, "weight_decay": weight_decay}, {"params": no_decay_param, "weight_decay": 0.0}, ] # Create a variable called optimizer that stores an instance of the optimizer. if optimizer_type == "adamw": optimizer = AdamW( grouped_params, lr=learning_rate, betas=(beta_1, beta_2), ) elif optimizer_type == "stable_adamw": optimizer = StableAdamWUnfused( grouped_params, lr=learning_rate, betas=(beta_1, beta_2), ) else: raise ValueError( "Invalid optimizer_type. Expected 'lion', 'adamw', 'deepspeed' or 'stable_adamw', got: {}".format( optimizer_type ) ) # Return the optimizer. return optimizer
Andromeda-master
Andromeda/utils/decoupled_optimizer.py
import math import torch from torch import einsum, _nnpack_available import torch.nn.functional as F from torch import nn from einops import rearrange import copy from pathlib import PurePath from tqdm import tqdm_gui from beartype import beartype from beartype.typing import Tuple, Optional from einops import rearrange, repeat, reduce, unpack from einops.layers.torch import Rearrange, Reduce #helpers def exists(val): return val is not None #decorators def eval_decorator(fn): def inner(self, *args, **kwargs): was_training = self.training self.eval() out = fn(self, *args, **kwargs) self.train(was_training) return out return inner def defaults(val, d): return val if exists(val) else d #tensor helpers def log(t, eps=1e-20): return torch.log(t.clamp(min = eps)) def masked_mean(seq, mask=None, dim=1, keepdim=True): if not exists(mask): return seq.mean(dim=dim) if seq.ndim == 3: mask = rearrange(mask, 'b n -> b n 1') masked_seq = seq.masked_fill(~mask, 0.) numer = masked_seq.sum(dim=dim, keepdim=keepdim) denom = mask.sum(dim=dim, keepdim=keepdim) masked_mean = numer / denom.clamp(min = 1e-3) masked_mean = masked_mean.masked_fill(denom == 0, 0.) return masked_mean #sampling helpers def gumbel_noise(t): noise = torch.zeros_like(t).uniform(0, 1) return -log(-log(noise)) def gumbel_sample(t, temperature = 1., dim=-1): return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim=dim) def top_p(logits, thres=0.9): sorted_logits, sorted_indices = torch.sort(logits, descending=True) cum_probs = torch.einsum(F.softmax(sorted_logits, dim=-1), dim=-1) sorted_indices_to_remove = cum_probs > (1 - thres) sorted_indices_to_remove[:, 1:] = sorted_indices_to_remove[:, :-1].clone() sorted_indices_to_remove[:, 0] = 0 sorted_logits[sorted_indices_to_remove] = float("-inf") return sorted_logits.scatter(1, sorted_indices, sorted_logits) def top_k(logits, thres=0.9): k = math.ceil((1 - thres) * logits.shape[-1]) val, ind = torch.topk(logits, k) probs = torch.full_like(logits, float('-inf')) probs.scatter_(1, ind, val) return probs class LoRA(nn.Module): def __init__( self, dim, dim_out, r=8, alpha=None ): super().__init__() alpha = defaults(alpha, r) self.scale = alpha / r self.A = nn.Parameter(torch.randn(dim, r)) self.B = nn.Parameter(torch.zeros(r, dim_out)) #reward model @beartype class RewardModel(nn.Module): def __init__( self, model: Andromeda, dropout=0.1, num_binned_output = 0., use_lora = True, lora_r = 8, reward_lora_scope = 'reward', ): super().__init__() self.model = copy.deepcopy(Andromeda) self.model.set_dropout(dropout) self.reward_lora_scope = reward_lora_scope is use_lora else None if exists(self.reward_lora_scope): self.model.add_finetune_params(reward_lora_scope, lora_r = lora_r) dim = model.dim self.binned_output = num_binned_output > 1 self.prompt_embed = nn.Parameter(torch.zeros(1, 1, dim)) self.response_embed = nn.Parameter(torch.zeros(1, 1, dim)) if self.binned_output: self.to_pred = nn.Linear(dim, num_binned_output) else: self.to_pred = nn.Sequential( nn.Linear(dim, 1, bias=False), Rearrange('... 1 -> ...') ) def load(self, path): path = Path(path) assert path.exists() self.load_state_dict(torch.load(str(path))) def finetune_parameters(self): return ( *self.to_pred.parameters(), *(self.model.finetune_parameters(self.reward_lora_scope) if exists(self.reward_lora_scope) else model.parameters()) ) def forward( self, x, mask=None, prompt_mask=None, prompt_lengths=None, labels=None, sample=False, sample_temperature=1., disable_lora=False ): assert not (exists(prompt_mask) and exists(prompt_lengths)) #derive prompt mask from prompt lengths if exists(prompt_lengths): batch, seq_len = x.shape arange = torch.arange(seq_len, device = x.device) prompt_mask = repeat(arange, 'n -> n n', b = batch) > rearrange(prompt_lengths, 'b -> b 1') #rward model should have an understand of which section is prompt and which section is repsonse extra_embed = None if exists(prompt_mask): extra_embed = torch.where( rearrange(prompt_mask, 'b n -> b n 1'), self.prompt_embed, self.response_embed ) embeds = self.model( x, )
Andromeda-master
Andromeda/utils/rf_utils.py
import torch.distributed as dist # Add this line def print_num_params(model): n_params = sum(p.numel() for p in model.parameters() if p.requires_grad) if dist.is_available(): if dist.get_rank() == 0: print(f"Number of parameters in model: {n_params}") else: print(f"Number of parameters in model: {n_params}") def print_main(msg): if dist.is_available(): if dist.get_rank() == 0: print(msg) else: print(msg)
Andromeda-master
Andromeda/utils/helpers.py
import unittest from Andromeda.dataset_builder import DatasetBuilder class TestDatasetBuilder(unittest.TestCase): def setUp(self): self.builder = DatasetBuilder(dataset_name="tiiuae/falcon-refinedweb") def test_initialization(self): self.assertEqual(self.builder.dataset_name, "tiiuae/falcon-refinedweb", "Dataset name is not correctly set.") self.assertEqual(self.builder.seq_len, 8192, "Sequence length is not correctly set.") self.assertEqual(self.builder.tokenizer, "EleutherAI/gpt-neox-20b", "Tokenizer is not correctly set.") def test_build_dataset(self): dataset = self.builder.build_dataset() self.assertIsNotNone(dataset, "Dataset is not built.") self.assertTrue(hasattr(dataset, "map"), "Dataset does not have a map method.") def test_tokenize_function(self): example = {"text": ["Hello, world!", "Andromeda is great."]} tokenized_example = self.builder.tokenize_function(example) self.assertIsInstance(tokenized_example, dict, "Tokenized example is not a dictionary.") self.assertTrue(all(isinstance(t, list) for t in tokenized_example.values()), "Tokenized example values are not lists.") def test_group_texts(self): examples = {"input_ids": [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] * 10} grouped_examples = self.builder.group_texts(examples) self.assertIsInstance(grouped_examples, dict, "Grouped examples is not a dictionary.") self.assertTrue(all(isinstance(t, list) for t in grouped_examples.values()), "Grouped example values are not lists.") self.assertTrue(all(len(t) == self.builder.seq_len for t in grouped_examples["input_ids"]), "Grouped example sequences are not the correct length.") if __name__ == '__main__': unittest.main()
Andromeda-master
testing/dataset_builder.py
import torch import unittest from Andromeda.model import Andromeda class TestAndromeda(unittest.TestCase): def setUp(self): self.model = Andromeda() def test_initialization(self): self.assertIsNotNone(self.model.andromeda, "Transformer is not initialized.") self.assertIsNotNone(self.model.decoder, "AutoregressiveWrapper is not initialized.") def test_forward_pass(self): input_tokens = torch.randint(0, 50432, (1, 8192)) output = self.model(input_tokens) self.assertIsInstance(output, torch.Tensor, "Output is not a PyTorch tensor.") self.assertEqual(output.shape[0], input_tokens.shape[0], "Output batch size does not match input.") def test_error_handling(self): with self.assertRaises(Exception): self.model.forward(None) def test_model_parameters(self): self.assertEqual(self.model.Andromeda.num_tokens, 50432, "Number of tokens is not correctly set.") self.assertEqual(self.model.Andromeda.max_seq_len, 8192, "Max sequence length is not correctly set.") def test_model_output(self): input_tokens = torch.randint(0, 50432, (1, 8192)) output1 = self.model(input_tokens) output2 = self.model(input_tokens) self.assertTrue(torch.allclose(output1, output2), "Model does not produce consistent output.") class TestAndromedaExtended(unittest.TestCase): def setUp(self): self.model = Andromeda() def test_input_size(self): for seq_len in [512, 1024, 2048, 4096]: input_tokens = torch.randint(0, 50432, (1, seq_len)) output = self.model(input_tokens) self.assertEqual(output.shape[1], seq_len, f"Output sequence length does not match input for seq_len={seq_len}.") def test_batch_size(self): for batch_size in [2, 4, 8, 16]: input_tokens = torch.randint(0, 50432, (batch_size, 8192)) output = self.model(input_tokens) self.assertEqual(output.shape[0], batch_size, f"Output batch size does not match input for batch_size={batch_size}.") def test_token_range(self): for token in [0, 50431]: input_tokens = torch.full((1, 8192), fill_value=token) output = self.model(input_tokens) self.assertIsInstance(output, torch.Tensor, f"Output is not a PyTorch tensor for token={token}.") def test_model_depth(self): for depth in [16, 32, 64]: model = Andromeda(depth=depth) self.assertEqual(model.Andromeda.attn_layers.depth, depth, f"Model depth is not correctly set for depth={depth}.") def test_model_dim(self): for dim in [1280, 2560, 5120]: model = Andromeda(dim=dim) self.assertEqual(model.Andromeda.attn_layers.dim, dim, f"Model dimension is not correctly set for dim={dim}.") def test_model_heads(self): for heads in [12, 24, 48]: model = Andromeda(heads=heads) self.assertEqual(model.Andromeda.attn_layers.heads, heads, f"Number of heads is not correctly set for heads={heads}.") def test_model_dim_head(self): for dim_head in [64, 128, 256]: model = Andromeda(dim_head=dim_head) self.assertEqual(model.Andromeda.attn_layers.dim_head, dim_head, f"Head dimension is not correctly set for dim_head={dim_head}.") def test_model_alibi_num_heads(self): for alibi_num_heads in [6, 12, 24]: model = Andromeda(alibi_num_heads=alibi_num_heads) self.assertEqual(model.Andromeda.attn_layers.alibi_num_heads, alibi_num_heads, f"Number of alibi heads is not correctly set for alibi_num_heads={alibi_num_heads}.") def test_model_shift_tokens(self): for shift_tokens in [0, 1, 2]: model = Andromeda(shift_tokens=shift_tokens) self.assertEqual(model.Andromeda.attn_layers.shift_tokens, shift_tokens, f"Number of shift tokens is not correctly set for shift_tokens={shift_tokens}.") def test_model_use_abs_pos_emb(self): for use_abs_pos_emb in [True, False]: model = Andromeda(use_abs_pos_emb=use_abs_pos_emb) self.assertEqual(model.Andromeda.use_abs_pos_emb, use_abs_pos_emb, f"Use absolute position embedding flag is not correctly set for use_abs_pos_emb={use_abs_pos_emb}.") def test_model_alibi_pos_bias(self): for alibi_pos_bias in [True, False]: model = Andromeda(alibi_pos_bias=alibi_pos_bias) self.assertEqual(model.Andromeda.attn_layers.alibi_pos_bias, alibi_pos_bias, f"Alibi position bias flag is not correctly set for alibi_pos_bias={alibi_pos_bias}.") def test_model_rotary_xpos(self): for rotary_xpos in [True, False]: model = Andromeda(rotary_xpos=rotary_xpos) self.assertEqual(model.Andromeda.attn_layers.rotary_xpos, rotary_xpos, f"Rotary position flag is not correctly set for rotary_xpos={rotary_xpos}.") def test_model_attn_flash(self): for attn_flash in [True, False]: model = Andromeda(attn_flash=attn_flash) self.assertEqual(model.Andromeda.attn_layers.attn_flash, attn_flash, f"Attention flash flag is not correctly set for attn_flash={attn_flash}") if __name__ == '__main__': unittest.main()
Andromeda-master
testing/model.py
import unittest from Andromeda.model import AndromedaTokenizer class TestAndromedaTokenizer(unittest.TestCase): def setUp(self): self.tokenizer = AndromedaTokenizer() def test_initialization(self): self.assertIsNotNone(self.tokenizer.tokenizer, "Tokenizer is not initialized.") self.assertEqual(self.tokenizer.tokenizer.eos_token, "<eos>", "EOS token is not correctly set.") self.assertEqual(self.tokenizer.tokenizer.pad_token, "<pad>", "PAD token is not correctly set.") self.assertEqual(self.tokenizer.tokenizer.model_max_length, 8192, "Model max length is not correctly set.") def test_tokenize_texts(self): texts = ["Hello, world!", "Andromeda is great."] tokenized_texts = self.tokenizer.tokenize_texts(texts) self.assertEqual(tokenized_texts.shape[0], len(texts), "Number of tokenized texts does not match input.") self.assertTrue(all(isinstance(t, torch.Tensor) for t in tokenized_texts), "Not all tokenized texts are PyTorch tensors.") def test_decode(self): texts = ["Hello, world!", "Andromeda is great."] tokenized_texts = self.tokenizer.tokenize_texts(texts) decoded_texts = [self.tokenizer.decode(t) for t in tokenized_texts] self.assertEqual(decoded_texts, texts, "Decoded texts do not match original texts.") def test_len(self): num_tokens = len(self.tokenizer) self.assertIsInstance(num_tokens, int, "Number of tokens is not an integer.") self.assertGreater(num_tokens, 0, "Number of tokens is not greater than 0.") if __name__ == '__main__': unittest.main()
Andromeda-master
testing/tokenizer.py
import matplotlib.pyplot as plt import time import torch from torch.utils.data import DataLoader from torchvision import datasets, transforms import numpy as np import tracemalloc # from Andromeda.model import Andromeda from Andromeda.model import Andromeda from Andromeda.utils.stable_adamw import StableAdamWUnfused torch.manual_seed(0) if torch.cuda.is_available(): torch.cuda.manual_seed(0) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") import torch.nn.functional as F from nltk.translate.bleu_score import corpus_bleu from rouge import Rouge from sklearn.metrics import f1_score class AccuracyMetrics: def __init__(self): self.rouge = Rouge() def calculate_perplexity(self, model, data_loader): model.eval() total_loss = 0 with torch.no_grad(): for batch in data_loader: input_ids, labels = batch output = model(input_ids) loss = F.cross_entropy(output.view(-1, output.size(-1)), labels.view(-1)) total_loss += loss.item() return torch.exp(torch.tensor(total_loss / len(data_loader))) def calculate_bleu(self, references, hypotheses): return corpus_bleu(references, hypotheses) def calculate_rouge(self, references, hypotheses): scores = self.rouge.get_scores(hypotheses, references, avg=True) return scores def calculate_f1(self, true_labels, pred_labels): return f1_score(true_labels, pred_labels, average="weighted") #mock test dataset test_dataset = datasets.FakeData(size=1000, transform=transforms.ToTensor()) #model model = Andromeda( num_tokens=50304, dim=1024, depth=24, dim_head=128, heads=8, alibi_num_heads=4 ) # Usage: accuracy_metrics = AccuracyMetrics() # Calculate Perplexity perplexity = accuracy_metrics.calculate_perplexity(model, data_loader) print('Perplexity:', perplexity) # Calculate BLEU bleu = accuracy_metrics.calculate_bleu(references, hypotheses) print('BLEU Score:', bleu) # Calculate ROUGE rouge_scores = accuracy_metrics.calculate_rouge(references, hypotheses) print('ROUGE Scores:', rouge_scores) # Calculate F1 Score f1 = accuracy_metrics.calculate_f1(true_labels, pred_labels) print('F1 Score:', f1) # Add at the bottom of your file if __name__ == "__main__": AccuracyMetrics()
Andromeda-master
testing/accuracy.py
import matplotlib.pyplot as plt import time import torch from torch.utils.data import DataLoader from torchvision import datasets, transforms import numpy as np import tracemalloc # from Andromeda.model import Andromeda from Andromeda.model import Andromeda from Andromeda.utils.stable_adamw import StableAdamWUnfused torch.manual_seed(0) if torch.cuda.is_available(): torch.cuda.manual_seed(0) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") class AndromedaModelTest: def __init__(self): self.model = Andromeda self.optimizer = StableAdamWUnfused() self.loss_function = torch.nn.CrossEntropyLoss() self.test_input = torch.randint(0, 256, (1, 1024)).cuda() def test_forward_pass(self): output = self.model(self.test_input) assert output.shape == (1, 1024, 64007), "Forward pass output shape mismatch" def test_backward_pass(self): self.optimizer.zero_grad() output = self.model(self.test_input) loss = self.loss_function(output, self.test_input) loss.backward() for name, parameter in self.model.named_parameters(): assert not torch.isnan(parameter.grad().any()), f"Gradient for {name} contains NaNs" assert not torch.isinf(parameter.grad().any()), f"Gradient for {name} contains Infs" def test_optimizer_step(self): initial_params = [param.clone() for param in self.model_parameters()] output = self.model(self.test_input) loss = self.loss_function(output, self.test_input) self.optimizer.zero_grad() loss.backward() self.optimizer.step() for initial_param, param in zip(initial_params, self.model.parameters()): assert not torch.equal(initial_param, param), "Model Parameters did not change after an optimizer step" class SpeedMetrics: def __init__(self, model): self.model = model.to(device) def forward_pass_time(self): start_time = time.time() self.model.decoder.forward(torch.randint(0, 50304, (1, 8192), device=device, dtype=torch.long))[0] end_time = time.time() return end_time - start_time def backward_pass_time(self): model_input = self.model.decoder.forward(torch.randint(0, 50304, (1, 8192), device=device, dtype=torch.long))[0] start_time = time.time() loss = torch.nn.CrossEntropyLoss()(model_input, torch.randint(0, 50304, (1, 8192), device=device, dtype=torch.long)) loss.backward() end_time = time.time() return end_time - start_time def end_to_end_latency(self): start_time = time.time() self.model.forward(torch.randint(0, 50304, (1, 8192), device=device, dtype=torch.long)) end_time = time.time() return end_time - start_time class ScalabilityMetrics: def __init__(self, model, dataset): self.model = model self.dataset = dataset self.dataloader = DataLoader(dataset, batch_size=32) def throughput(self): start_time = time.time() for i, data in enumerate(self.dataloader, 0): self.model.forward(data) end_time = time.time() return len(self.dataset) / (end_time - start_time) class ConsistencyMetrics: def __init__(self, model): self.model = model def consistency_over_time(self): consistency_times = [] outputs_list = [] for _ in range(10): start_time = time.time() outputs = self.model.forward(torch.randint(0, 50304, (1, 8192))) end_time = time.time() consistency_times.append(end_time - start_time) outputs_list.append(outputs.detach().numpy()) initial_output = outputs_list[0] consistency_score = 0 for output in outputs_list[1:]: if np.array_equal(initial_output, output): consistency_score += 1 consistency_score = consistency_score / len(outputs_list) * 100 return consistency_times, consistency_score class MemoryMetrics: def __init__(self, model): self.model = model def memory_footprint(self): tracemalloc.start() self.model.forward(torch.randint(0, 50304, (1, 8192))) current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() return current, peak class SequenceMetrics: def __init__(self, model): self.model = model def sequence_length_impact(self): seq_lengths = [1024, 2048, 4096, 8192] seq_impact_times = [] for length in seq_lengths: start_time = time.time() self.model.forward(torch.randint(0, 50304, (1, length))) end_time = time.time() seq_impact_times.append(end_time - start_time) return seq_lengths, seq_impact_times class FlopsBenchmark: def __init__(self, model, bsz=32, d_model=1024, num_heads=8, sequence_lengths=list(range(500, 32001, 500))): self.bsz = bsz self.d_model = d_model self.num_heads = num_heads self.sequence_lengths = sequence_lengths self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.dtype=torch.float32 self.model = model.to(self.device) def benchmark(self): time_taken = [] tflops_per_s = [] for seq_len in self.sequence_lengths: x = torch.randn(self.bsz, seq_len, self.d_model).to(self.device).type(self.dtype) torch.cuda.synchronize() start = time.time() self.model(x) torch.cuda.synchronize() elapsed = time.time() - start time_taken.append(elapsed) total_flops = 4 * seq_len **2 * (self.d_model // self.num_heads) * self.num_heads tflops_per_s.append(total_flops / elapsed / 1e12) # Convert to TFLOPs for seq_len, elapsed, tflops in zip(self.sequence_lengths, time_taken, tflops_per_s): print(f"Sequence length: {seq_len}, Time elapsed: {elapsed} s, TFLOPs/s: {tflops}") #mock test dataset test_dataset = datasets.FakeData(size=1000, transform=transforms.ToTensor()) #model model = Andromeda( num_tokens=50304, dim=1024, depth=24, dim_head=128, heads=8, alibi_num_heads=4 ) #speed test metrics test # speed test metrics test speed_metrics = SpeedMetrics(model) forward_pass_time = speed_metrics.forward_pass_time() backward_pass_time = speed_metrics.backward_pass_time() end_to_end_latency = speed_metrics.end_to_end_latency() #scalability metrics test scalability_metrics = ScalabilityMetrics(model, test_dataset) throughput = scalability_metrics.throughput() #consistency metrucs test consistency_metrics = ConsistencyMetrics(model) consistency_times, consistency_score = consistency_metrics.consistency_over_time() #memory metrics test memory_metrics = MemoryMetrics(model) current, peak = memory_metrics.memory_footprint() #sequence metrics test sequence_metrics = SequenceMetrics(model) seq_lengths, seq_impact_times = sequence_metrics.sequence_length_impact() #flops flops_benchmark = FlopsBenchmark(model) flops_benchmark.benchmark() # Graphical Interface fig, axs = plt.subplots(3) axs[0].bar(["Forward Pass Time", "Backward Pass Time", "End-to-End Latency"], [forward_pass_time, backward_pass_time, end_to_end_latency]) axs[0].set_title('Speed Metrics') axs[0].set_xlabel('Metrics') axs[0].set_ylabel('Time (seconds)') axs[1].bar(seq_lengths, seq_impact_times) axs[1].set_title('Sequence Length Impact') axs[1].set_xlabel('Sequence Length') axs[1].set_ylabel('Time (seconds)') axs[2].plot(list(range(1, 11)), consistency_times) axs[2].set_title('Consistency Over Time') axs[2].set_xlabel('Run Number') axs[2].set_ylabel('Time (seconds)') plt.tight_layout() plt.show() print(f"Throughput: {throughput} instances/second") print(f"Memory used: {current / 10**6}MB; Peak: {peak / 10**6}MB") # Add at the bottom of your file if __name__ == "__main__": model_test = AndromedaModelTest() model_test.test_forward_pass() model_test.test_backward_pass() model_test.test_optimizer_step()
Andromeda-master
testing/benchmarking.py
# Copyright 2022 MosaicML LLM Foundry authors # SPDX-License-Identifier: Apache-2.0 """Run pytest using MCP.""" import argparse import time from mcli.sdk import (RunConfig, RunStatus, create_run, follow_run_logs, stop_run, wait_for_run_status) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--name', type=str, default='mcp-pytest', help='Base name of run') parser.add_argument('--cluster', type=str, default='r1z4', help='Cluster to use') parser.add_argument('--gpu_type', type=str, default='a100_40gb', help='Type of GPU to use') parser.add_argument('--gpu_num', type=int, default=2, help='Number of the GPU to use') parser.add_argument('--image', type=str, default='mosaicml/pytorch:latest', help='Docker image to use') parser.add_argument('--git_branch', type=str, help='Git branch to check out') parser.add_argument( '--git_commit', type=str, help='Git commit to check out. Overrides git_branch if specified') parser.add_argument( '--pr_number', type=int, help= 'PR number to check out. Overrides git_branch/git_commit if specified') parser.add_argument('--pytest_markers', type=str, help='Markers to pass to pytest') parser.add_argument('--pytest_command', type=str, help='Command to run pytest') parser.add_argument('--timeout', type=int, default=1800, help='Timeout for run (in seconds)') args = parser.parse_args() name = args.name git_integration = { 'integration_type': 'git_repo', 'git_repo': 'mosaicml/llm-foundry', 'ssh_clone': 'False', } if args.git_branch is not None and args.git_commit is None: name += f'-branch-{args.git_branch}' git_integration['git_branch'] = args.git_branch if args.git_commit is not None: name += f'-commit-{args.git_commit}' git_integration['git_commit'] = args.git_commit command = 'cd llm-foundry' # Checkout a specific PR if specified if args.pr_number is not None: name += f'-pr-{args.pr_number}' command += f''' git fetch origin pull/{args.pr_number}/head:pr_branch git checkout pr_branch ''' # Shorten name if too long if len(name) > 56: name = name[:56] command += f''' pip install --upgrade --user .[all] export COMMON_ARGS="-v --durations=20 -m '{args.pytest_markers}'" make test PYTEST='{args.pytest_command}' EXTRA_ARGS="$COMMON_ARGS --codeblocks" make test-dist PYTEST='{args.pytest_command}' EXTRA_ARGS="$COMMON_ARGS" WORLD_SIZE=2 python -m coverage combine python -m coverage report ''' config = RunConfig( name=name, cluster=args.cluster, gpu_type=args.gpu_type, gpu_num=args.gpu_num, image=args.image, integrations=[git_integration], command=command, ) # Create run run = create_run(config) print(f'[GHA] Run created: {run.name}') # Wait until run starts before fetching logs run = wait_for_run_status(run, status='running') start_time = time.time() print('[GHA] Run started. Following logs...') # Print logs for line in follow_run_logs(run): print(line, end='') # Check if args.timeout seconds have elapsed if time.time() - start_time > args.timeout: print( f'[GHA] Run timed out and did not complete in {args.timeout/60} minutes.' ) run = stop_run(run) print('[GHA] Run stopped.') break print('[GHA] Run completed. Waiting for run to finish...') run = wait_for_run_status(run, status='completed') # Fail if command exited with non-zero exit code or timed out assert run.status == RunStatus.COMPLETED
Andromeda-master
.github/mcp/mcp_pytest.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] from io import open from setuptools import find_packages, setup setup( name="zetascale", version="0.0.3", author="Zeta Team", author_email="[email protected]", description="Transformers at zeta scales", long_description=open("README.md", "r", encoding="utf-8").read(), long_description_content_type="text/markdown", keywords="Transformers at zeta scale", license="MIT", url="https://github.com/kyegomez/zeta", packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]), install_requires=["torch>=1.8", "fairscale==0.4.0", "timm==0.6.13", 'optimus-prime-transformers', 'triton', 'pytest'], python_requires=">=3.8.0", classifiers=[ "Programming Language :: Python :: 3", ], )
zeta-main
setup.py
import torch from zeta import FlashAttention q = torch.randn(2, 4, 6, 8) k = torch.randn(2, 4, 10, 8) v = torch.randn(2, 4, 10, 8) attention = FlashAttention(causal=False, dropout=0.1, flash=False) output = attention(q, k, v) print(output.shape)
zeta-main
example.py
#architecture from zeta.models import * from zeta.models.andromeda import Andromeda #models from zeta.models.gpt4 import GPT4, GPT4MultiModal from zeta.models.palme import PalmE ####### from zeta.nn import * from zeta.nn.architecture.transformer import ( AttentionLayers, Decoder, Encoder, Transformer, ) from zeta.nn.attention.dilated_attention import DilatedAttention from zeta.nn.attention.flash_attention import FlashAttention from zeta.nn.attention.flash_attention2 import FlashAttentionTwo # from zeta.nn.attention.cross_attention import CrossAttend from zeta.nn.attention.multihead_attention import MultiheadAttention # from zeta.nn.architecture.attn_layers import AttentionLayers # from zeta.nn.architecture.encoder import Encoder # from zeta.nn.architecture.decoder import Decoder #attentions from zeta.nn.attention.multiquery_attention import MultiQueryAttention from zeta.tokenizers.language_tokenizer import LanguageTokenizerGPTX #tokenizers from zeta.tokenizers.multi_modal_tokenizer import MultiModalTokenizer from zeta.training import * #loss from zeta.training.loss.nebula import Nebula #train from zeta.training.train import Trainer, train
zeta-main
zeta/__init__.py
import os from logging import getLogger from typing import List, Optional from sentencepiece import SentencePieceProcessor logger = getLogger() class SentencePieceTokenizer: """ A SentencePieceTokenizer is a tokenizer that uses a pretrained SentencePiece model to convert text into tokens and vice versa. It includes the ability to add special tokens for infilling tasks and provides functionality to encode and decode text with or without implicit leading spaces. Parameters: - model_path (str): Path to the pretrained SentencePiece model file. Attributes: - n_words (int): Vocabulary size of the SentencePiece model. - bos_id (int): Token ID of the beginning-of-sentence (BOS) token. - eos_id (int): Token ID of the end-of-sentence (EOS) token. - pad_id (int): Token ID of the padding (PAD) token. - prefix_id (int, optional): Token ID of the prefix token. Default: None. - middle_id (int, optional): Token ID of the middle token. Default: None. - suffix_id (int, optional): Token ID of the suffix token. Default: None. - eot_id (int, optional): Token ID of the end-of-turn (EOT) token. Default: None. """ def __init__(self, model_path: str): # reload tokenizer assert os.path.isfile(model_path), model_path self.sp_model = SentencePieceProcessor(model_file=model_path) logger.info(f"Reloaded SentencePiece model from {model_path}") # BOS / EOS token IDs self.n_words: int = self.sp_model.vocab_size() self.bos_id: int = self.sp_model.bos_id() self.eos_id: int = self.sp_model.eos_id() self.pad_id: int = self.sp_model.pad_id() # token IDs for special infilling tokens self.prefix_id: Optional[int] = self.sp_model.piece_to_id("▁<PRE>") or None self.middle_id: Optional[int] = self.sp_model.piece_to_id("▁<MID>") or None self.suffix_id: Optional[int] = self.sp_model.piece_to_id("▁<SUF>") or None self.eot_id: Optional[int] = self.sp_model.piece_to_id("▁<EOT>") or None logger.info( f"#words: {self.n_words} - BOS ID: {self.bos_id} - EOS ID: {self.eos_id} " f"- PRE ID: {self.prefix_id} - MID ID: {self.middle_id} - SUF ID: {self.suffix_id} - EOT ID: {self.eot_id}" ) assert self.sp_model.vocab_size() == self.sp_model.get_piece_size() def encode(self, s: str, bos: bool, eos: bool) -> List[int]: assert type(s) is str t = self.sp_model.encode(s) if bos: t = [self.bos_id] + t if eos: t = t + [self.eos_id] return t def decode(self, t: List[int]) -> str: return self.sp_model.decode(t) def encode_infilling(self, s: str) -> List[int]: """Encode a string without an implicit leading space.""" return self.sp_model.encode("☺" + s)[2:] def decode_infilling(self, t: List[int]) -> str: """Decode a string without an implicit leading space.""" return self.sp_model.decode([self.sp_model.piece_to_id("☺")] + t)[1:]
zeta-main
zeta/tokenizers/sentence_piece.py
from zeta.tokenizers.language_tokenizer import LanguageTokenizerGPTX from zeta.tokenizers.multi_modal_tokenizer import MultiModalTokenizer from zeta.tokenizers.sentence_piece import SentencePieceTokenizer
zeta-main
zeta/tokenizers/__init__.py
from transformers import AutoTokenizer class LanguageTokenizerGPTX: def __init__(self): self.tokenizer= AutoTokenizer.from_pretrained( "EleutherAI/gpt-neox-20b", eos_token="<eos>", pad_token="<pad>", extra_ids=0, model_max_length=8192 ) def tokenize_texts(self, texts): return self.tokenizer(texts, return_tensors='pt', padding=True, truncation=True).input_ids def decode(self, texts): return self.tokenizer.decode(texts) def __len__(self): num_tokens = len(self.tokenizer) return num_tokens
zeta-main
zeta/tokenizers/language_tokenizer.py
import logging import torch from transformers import CLIPProcessor, AutoTokenizer logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') class MultiModalTokenizer: """ A tokenizer class for the kosmos model Attributes: processor(CLIPProcessor): The processor to tokenize images tokenizer: (AutoTokenizer): The tokenizer to tokenize text im_idx: (int): The Index of the "<image>" token. im_end_idx (int): The index of the "</image>" token. """ def __init__(self, max_length: int = 8192): self.max_length = max_length try: self.processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-L-14-laion2B-s32B-b82K") self.tokenizer = AutoTokenizer.from_pretrained( "EleutherAI/gpt-neox-20b", additional_special_tokens=["<image>", "</image>"], eos_token="<eos>", pad_token="<pad>", extra_ids=0, model_max_length=self.max_length ) except Exception as e: logging.error(f"Failed to initialize KosmosTokenizer: {e}") raise self.im_idx, self.im_end_idx = self.tokenizer.convert_tokens_to_ids(["<image>", "</image>"]) def tokenize_texts(self, texts: str): """ Tokenize given texts. Args: Texts (str): The Text to be tokenized Returns: A tuple containing the tokenized texts and only the text tokens. """ try: texts = self.tokenizer(texts, return_tensors="pt", padding=True, truncation=True).input_ids # Add image tokens to text as "<s> <image> </image> text </s>" image_tokens = torch.tensor([[self.im_idx, self.im_end_idx]] * texts.shape[0]) return torch.cat([texts[:, 0:1], image_tokens, texts[:, 1:]], dim=1), texts except Exception as e: logging.error(f"Failed to tokenize texts: {e}") raise def tokenize_images(self, images): """ Tokenizes given images. Args: images: The images to be tokenized Returns: The tokenized images. """ try: return self.processor(images=images, return_tensors="pt").pixel_values except Exception as e: logging.error(f"Failed to tokenize images: {e}") raise def tokenize(self, sample): """ Tokenizes given sample. Args: Sample: The sample to be tokenized Returns: A dictionary containing the tokenized text tokens, images, labels, and attention mask. """ try: text_tokens, only_text_tokens = self.tokenize_texts(sample["target_text"]) attention_mask = text_tokens != self.tokenizer.pad_token_id dummy_image_features = torch.ones((text_tokens.shape[0], 64)) attention_mask = torch.cat([dummy_image_features, attention_mask], dim=1) return { "text_tokens": text_tokens, "images": self.tokenize_images(sample["image"]), "labels": only_text_tokens, "attention_mask": attention_mask, } except Exception as e: logging.error(f"Failed to tokenize sample: {e}") raise
zeta-main
zeta/tokenizers/multi_modal_tokenizer.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] # attention from zeta.nn.architecture.auto_regressive_wrapper import AutoregressiveWrapper from zeta.nn.architecture.local_transformer import LocalTransformer from zeta.nn.architecture.parallel_transformer import ParallelTransformerBlock from zeta.nn.architecture.transformer import ( Decoder, Encoder, Transformer, ViTransformerWrapper, ) from zeta.nn.attention.flash_attention2 import FlashAttentionTwo from zeta.nn.attention.local_attention import LocalAttention from zeta.nn.attention.local_attention_mha import LocalMHA # from zeta.nn.attention.cross_attention import CrossAttend from zeta.nn.attention.multihead_attention import MultiheadAttention # from zeta.nn.architecture.cross_attender import CrossAttender ######### Attention from zeta.nn.attention.multiquery_attention import MultiQueryAttention from zeta.nn.embeddings.base import BaseEmbedding from zeta.nn.embeddings.bnb_embedding import BnBEmbedding from zeta.nn.embeddings.multiway_network import ( MultiwayEmbedding, MultiwayNetwork, MultiwayWrapper, ) from zeta.nn.embeddings.nominal_embeddings import NominalEmbedding # embeddings from zeta.nn.embeddings.rope import RotaryEmbedding from zeta.nn.embeddings.xpos_relative_position import ( XPOS, apply_rotary_pos_emb, rotate_every_two, ) from zeta.nn.modules.droppath import DropPath from zeta.nn.modules.feedforward_network import FeedForwardNetwork # modules from zeta.nn.modules.lora import Lora from zeta.nn.modules.token_learner import TokenLearner from zeta.nn.modules.dynamic_module import DynamicModule from zeta.nn.architecture.hierarchical_transformer import HierarchicalTransformer
zeta-main
zeta/nn/__init__.py
import math import torch import torch.nn.functional as F from torch import nn try: from apex.normalization import FusedLayerNorm as LayerNorm except ModuleNotFoundError: from torch.nn import LayerNorm from zeta.nn.attention.base import BaseAttention from zeta.nn.embeddings.multiway_network import MultiwayWrapper from zeta.nn.embeddings.xpos_relative_position import XPOS class MultiheadAttention(BaseAttention): def __init__( self, args, embed_dim: int = None, num_heads: int = None, dropout: int = 0.0, self_attention: bool =False, encoder_decoder_attention: bool = False, subln: bool =False, ): super().__init__() self.args = args self.embed_dim = embed_dim self.num_heads = num_heads self.head_dim = embed_dim // num_heads self.scaling = self.head_dim**-0.5 self.self_attention = self_attention self.encoder_decoder_attention = encoder_decoder_attention assert self.self_attention ^ self.encoder_decoder_attention self.k_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.v_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.q_proj = MultiwayWrapper(args, nn.Linear(embed_dim, embed_dim, bias=True)) self.out_proj = MultiwayWrapper( args, nn.Linear(embed_dim, embed_dim, bias=True) ) self.inner_attn_ln = ( MultiwayWrapper(args, LayerNorm(self.embed_dim, eps=args.layernorm_eps)) if subln and self.self_attention else None ) self.dropout_module = torch.nn.Dropout(dropout) self.xpos = ( XPOS(self.head_dim, args.xpos_scale_base) if args.xpos_rel_pos and self.self_attention else None ) def reset_parameters(self): nn.init.xavier_uniform_(self.k_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.v_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.q_proj.weight, gain=1 / math.sqrt(2)) nn.init.xavier_uniform_(self.out_proj.weight) nn.init.constant_(self.out_proj.bias, 0.0) def forward( self, query, key, value, incremental_state=None, key_padding_mask=None, attn_mask=None, rel_pos=None, is_first_step=False, ): bsz, tgt_len, embed_dim = query.size() src_len = tgt_len assert embed_dim == self.embed_dim, f"query dim {embed_dim} != {self.embed_dim}" key_bsz, src_len, _ = key.size() assert key_bsz == bsz, f"{query.size(), key.size()}" assert value is not None assert bsz, src_len == value.shape[:2] q = self.q_proj(query) k = self.k_proj(key) v = self.v_proj(value) q *= self.scaling q = q.view(bsz, tgt_len, self.num_heads, self.head_dim).transpose(1, 2) k = k.view(bsz, src_len, self.num_heads, self.head_dim).transpose(1, 2) v = v.view(bsz, src_len, self.num_heads, self.head_dim).transpose(1, 2) q = q.reshape(bsz * self.num_heads, tgt_len, self.head_dim) k = k.reshape(bsz * self.num_heads, src_len, self.head_dim) v = v.reshape(bsz * self.num_heads, src_len, self.head_dim) if incremental_state is not None: if "prev_key" in incremental_state: prev_key = incremental_state["prev_key"].view( bsz * self.num_heads, -1, self.head_dim ) prev_value = incremental_state["prev_value"].view( bsz * self.num_heads, -1, self.head_dim ) k = torch.cat([prev_key, k], dim=1) v = torch.cat([prev_value, v], dim=1) incremental_state["prev_key"] = k.view( bsz, self.num_heads, -1, self.head_dim ) incremental_state["prev_value"] = v.view( bsz, self.num_heads, -1, self.head_dim ) src_len = k.size(1) if self.xpos is not None: if incremental_state is not None and not is_first_step: offset = src_len - 1 else: offset = 0 k = self.xpos(k, offset=0, downscale=True) q = self.xpos(q, offset=offset, downscale=False) attn_weights = torch.bmm(q, k.transpose(1, 2)) if attn_mask is not None: attn_weights = torch.nan_to_num(attn_weights) attn_mask = attn_mask.unsqueeze(0) attn_weights += attn_mask if key_padding_mask is not None: attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len) attn_weights = attn_weights.masked_fill( key_padding_mask.unsqueeze(1).unsqueeze(2).to(torch.bool), float("-inf"), ) attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len) if rel_pos is not None: rel_pos = rel_pos.view(attn_weights.size()) attn_weights = attn_weights + rel_pos attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).type_as( attn_weights ) attn_probs = self.dropout_module(attn_weights) attn = torch.bmm(attn_probs, v) attn = attn.transpose(0, 1).reshape(tgt_len, bsz, embed_dim).transpose(0, 1) if self.inner_attn_ln is not None: attn = self.inner_attn_ln(attn) attn = self.out_proj(attn) attn_weights = attn_weights.view( bsz, self.num_heads, tgt_len, src_len ).transpose(1, 0) return attn, attn_weights
zeta-main
zeta/nn/attention/multihead_attention.py
import torch from torch import nn import torch.nn.functional as F from einops import rearrange from zeta.nn.attention.local_attention import LocalAttention from zeta.utils.main import default, exists, l2norm class LocalMHA(nn.Module): def __init__( self, *, dim, window_size, dim_head=64, heads=8, dropout=0., causal=False, prenorm=False, qk_rmsnorm=False, qk_scale=8, use_xpos=False, xpos_scale_base=None, exact_windowsize=None, **kwargs ): super().__init__() inner_dim = dim_head * heads self.norm = nn.LayerNorm(dim) if prenorm else None self.heads = heads self.to_qkv = nn.Linear(dim, inner_dim * 3, bias=False) self.qk_rmsnorm = qk_rmsnorm if qk_rmsnorm: self.q_scale = nn.Parameter(torch.ones(dim_head)) self.k_scale = nn.Parameter(torch.ones(dim_head)) self.attn_fn = LocalAttention( dim = dim_head, window_size=window_size, causal=causal, autopad=True, scale = (qk_scale if qk_rmsnorm else None), exact_windowsize = default(exact_windowsize, True), use_xpos=use_xpos, xpos_scale_base=xpos_scale_base, **kwargs ) self.to_out = nn.Linear(inner_dim, dim, bias=False) def forward( self, x, mask=None, attn_bias=None ): if exists(self.norm): x = self.norm(x) q, k, v = self.to_qkv(x).chunk(3, dim=-1) q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=self.heads), (q, k, v)) if self.qk_rmsnorm: q, k = map(l2norm, (q, k)) q = q * self.q_scale k = k * self.k_scale out = self.attn_fn( q, k, v, mask=mask, attn_bias=attn_bias ) out = rearrange(out, 'b h n d -> b n (h d)') return self.to_out(out)
zeta-main
zeta/nn/attention/local_attention_mha.py
import math import warnings from typing import Dict, Optional, Type import torch import torch.nn as nn from einops import rearrange from packaging import version from zeta.nn.attention.base import BaseAttention def _cast_if_autocast_enabled(tensor): if torch.is_autocast_enabled(): if tensor.device.type == 'cuda': dtype = torch.get_autocast_gpu_dtype() elif tensor.device.type == 'cpu': dtype = torch.get_autocast_cpu_dtype() else: raise NotImplementedError() return tensor.to(dtype=dtype) return tensor class LPLayerNorm(nn.Module): def __init__( self, normalized_shape, eps=1e-05, elementwise_affine=True, device=None, dtype=None, ): super().__init__( normalized_shape=normalized_shape, eps=eps, elementwise_affine=elementwise_affine, device=device, dtype=dtype ) def forward(self, x): module_device = x.device downcast_x = _cast_if_autocast_enabled(x) downcast_weight = _cast_if_autocast_enabled( self.weight) if self.weight is not None else self.weight downcast_bias = _cast_if_autocast_enabled( self.bias) if self.bias is not None else self.bias with torch.autocast(enabled=False, device_type=module_device.type): return torch.nn.functional.layer_norm( downcast_x, self.normalized_shape, downcast_weight, downcast_bias, self.eps, ) def rms_norm(x, weight=None, eps=1e-5): output = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps) if weight is not None: return output * weight return output class RMSNorm(nn.Module): def __init__( self, normalized_shape, eps=1e-5, weight=True, dtype=None, device=None, ): super().__init__() self.eps = eps if weight: self.weight = torch.nn.Parameter( torch.ones(normalized_shape, dtype=dtype, device=device) ) else: self.register_parameter('weight', None) def forward(self, x): return rms_norm(x.float(), self.weight, self.eps).to(dtype=x.dtype) class LPRMSNorm(RMSNorm): def __init__( self, normalized_shape, eps=1e-5, weight=True, dtype=None, device=None, ): super().__init__( normalized_shape=normalized_shape, eps=eps, weight=weight, dtype=dtype, device=device, ) def forward(self, x): downcast_x = _cast_if_autocast_enabled(x) downcast_weight = _cast_if_autocast_enabled( self.weight) if self.weight is not None else self.weight with torch.autocast(enabled=False, device_type=x.device_type): return rms_norm(downcast_x, downcast_weight, self.eps).to(dtype=x.dtype) #Registers FC_CLASS_REGISTRY = { 'torch': nn.Linear, } NORM_CLASS_REGISTRY = { 'layernornm': nn.LayerNorm, 'low_precision_layernorm': LPLayerNorm, 'rmsnorm': LPLayerNorm, 'low_precision_rmsnorm': LPRMSNorm, } def _reset_causal(num_query_tokens: int, num_key_tokens: int, original_causal: bool): # disable causal when it is not needed # necessary for flash & triton for generation with kv_cache if original_causal and num_query_tokens != num_key_tokens: if num_query_tokens != 1: raise NotImplementedError( 'MPT does not support query and key with different number of tokens, unless number of query tokens is 1.' ) else: return False return original_causal def scaled_multihead_dot_product_attention( query, key, value, heads, past_key_value=None, softmax_scale=None, bias=None, key_padding_mask=None, causal=False, dropout=0.0, training=False, needs_weights=False, multiquery=False, ): q = rearrange(query, 'b s (h d) -> b h s d', h=heads) kv_heads = 1 if multiquery else heads k = rearrange(key, 'b s (h d) -> b h d s', h=kv_heads) v = rearrange(value, 'b s (h d) -> b h s d', h=kv_heads) if past_key_value is not None: # attn_impl: flash & triton use kernels which expect input shape [b, s, h, d_head]. # kv_cache is therefore stored using that shape. # attn_impl: torch stores the kv_cache in the ordering which is most advantageous # for its attn computation ie # keys are stored as tensors with shape [b, h, d_head, s] and # values are stored as tensors with shape [b, h, s, d_head] if len(past_key_value) != 0: k = torch.cat([past_key_value[0], k], dim=3) v = torch.cat([past_key_value[1], v], dim=2) past_key_value = (k, v) b, _, s_q, d = q.shape s_k = k.size(-1) if softmax_scale is None: softmax_scale = 1 / math.sqrt(d) attn_weight = q.matmul(k) * softmax_scale if bias is not None: # clamp to 0 necessary for torch 2.0 compile() _s_q = max(0, bias.size(2) - s_q) _s_k = max(0, bias.size(3) - s_k) bias = bias[:, :, _s_q:, _s_k:] if (bias.size(-1) != 1 and bias.size(-1) != s_k) or (bias.size(-2) != 1 and bias.size(-2) != s_q): raise RuntimeError( f'bias (shape: {bias.shape}) is expected to broadcast to shape: {attn_weight.shape}.' ) attn_weight = attn_weight + bias min_val = torch.finfo(q.dtype).min if key_padding_mask is not None: if bias is not None: warnings.warn( 'Propogating key_padding_mask to the attention module ' +\ 'and applying it within the attention module can cause ' +\ 'unneccessary computation/memory usage. Consider integrating ' +\ 'into bias once and passing that to each attention ' +\ 'module instead.' ) attn_weight = attn_weight.masked_fill( ~key_padding_mask.view((b, 1, 1, s_k)), min_val) if causal and (not q.size(2) == 1): s = max(s_q, s_k) causal_mask = attn_weight.new_ones(s, s, dtype=torch.float32) causal_mask = causal_mask.tril() causal_mask = causal_mask.to(torch.bool) causal_mask = ~causal_mask causal_mask = causal_mask[-s_q:, -s_k:] attn_weight = attn_weight.masked_fill(causal_mask.view(1, 1, s_q, s_k), min_val) attn_weight = torch.softmax(attn_weight, dim=-1) if dropout: attn_weight = torch.nn.functional.dropout(attn_weight, p=dropout, training=training, inplace=True) out = attn_weight.to(v.dtype).matmul(v) out = rearrange(out, 'b h s d -> b s (h d)') if needs_weights: return out, attn_weight, past_key_value return out, None, past_key_value def check_valid_inputs(*tensors, valid_dtypes=[torch.float16, torch.bfloat16]): for tensor in tensors: if tensor.dtype not in valid_dtypes: raise TypeError(f'{tensor.dtype=} must be in {valid_dtypes=}.') if not tensor.is_cuda: raise TypeError(f'Inputs must be cuda tensors ({tensor.is_cuda=}).') def flash_attn_fn( query, key, value, heads, past_key_value=None, softmax_scale=None, bias=None, key_padding_mask=None, causal=False, dropout=0.0, training=False, needs_weights=False, multiquery=False, ): try: from flash_attn import bert_padding, flash_attn_interface # type: ignore # yapf: disable # isort: skip except: raise RuntimeError('Please install flash-attn==1.0.3.post0') check_valid_inputs(query, key, value) if past_key_value is not None: if len(past_key_value) != 0: key = torch.cat([past_key_value[0], key], dim=1) value = torch.cat([past_key_value[1], value], dim=1) past_key_value = (key, value) if bias is not None: # clamp to 0 necessary for torch 2.0 compile() _s_q = max(0, bias.size(2) - query.size(1)) _s_k = max(0, bias.size(3) - key.size(1)) bias = bias[:, :, _s_q:, _s_k:] if bias is not None: raise NotImplementedError('bias not implemented for flash attn.') batch_size, seqlen = query.shape[:2] if key_padding_mask is None: key_padding_mask = torch.ones_like(key[:, :, 0], dtype=torch.bool) query_padding_mask = key_padding_mask[:, -query.size(1):] query_unpad, indices_q, cu_seqlens_q, max_seqlen_q = bert_padding.unpad_input( query, query_padding_mask) query_unpad = rearrange(query_unpad, 'nnz (h d) -> nnz h d', h=heads) key_unpad, _, cu_seqlens_k, max_seqlen_k = bert_padding.unpad_input( key, key_padding_mask) key_unpad = rearrange(key_unpad, 'nnz (h d) -> nnz h d', h=1 if multiquery else heads) value_unpad, _, _, _ = bert_padding.unpad_input(value, key_padding_mask) value_unpad = rearrange(value_unpad, 'nnz (h d) -> nnz h d', h=1 if multiquery else heads) if multiquery: key_unpad = key_unpad.expand(key_unpad.size(0), heads, key_unpad.size(-1)) value_unpad = value_unpad.expand(value_unpad.size(0), heads, value_unpad.size(-1)) dropout = dropout if training else 0.0 reset_causal = _reset_causal(query.size(1), key.size(1), causal) output_unpad = flash_attn_interface.flash_attn_unpadded_func( query_unpad, key_unpad, value_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout, softmax_scale=softmax_scale, causal=reset_causal, return_attn_probs=needs_weights) output = bert_padding.pad_input( rearrange(output_unpad, 'nnz h d -> nnz (h d)'), indices_q, batch_size, seqlen) return output, None, past_key_value def attn_bias_shape(attn_impl, heads, seq_len, alibi, prefix_lm, causal, use_sequence_id): if attn_impl == 'flash': return None elif attn_impl in ['torch', 'triton']: if alibi: if (prefix_lm or not causal) or use_sequence_id: return (1, heads, seq_len, seq_len) return (1, heads, 1, seq_len) elif prefix_lm or use_sequence_id: return (1, 1, seq_len, seq_len) return None else: raise ValueError(f'{attn_impl=} is an invalid setting.') def build_attn_bias( attn_impl, bias, heads, seq_len, causal=False, alibi=False, alibi_bias_max=8, ): if attn_impl == 'flash': return None elif attn_impl in ['torch', 'triton']: if alibi: # in place add alibi to attn bias device, dtype = bias.device, bias.dtype bias = bias.add( build_alibi_bias( heads, seq_len, full=not causal, alibi_bias_max=alibi_bias_max, device=device, dtype=dtype, )) return bias else: raise ValueError(f'{attn_impl=} is an invalid setting.') #helper helpers def gen_slopes(heads, alibi_bias_max=8, device=None): _heads = 2**math.ceil(math.log2(heads)) m = torch.arange(1, _heads + 1, dtype=torch.float32, device=device) m = m.mul(alibi_bias_max / _heads) slopes = (1. / torch.pow(2, m)) if _heads != heads: # if heads is not a power of two, # Huggingface and FasterTransformer calculate slopes normally, # then return this strided concatenation of slopes slopes = torch.concat([slopes[1::2], slopes[::2]])[:heads] return slopes.view(1, heads, 1, 1) def build_alibi_bias( heads, seq_len, full=False, alibi_bias_max=8, device=None, dtype=None, ): alibi_bias = torch.arange(1 - seq_len, 1, dtype=torch.int32, device=device).view(1, 1, 1, seq_len) if full: # generate 1 x Heads x SeqLen x SeqLen alibi bias mask # otherwise the mask is 1 x Heads x 1 x SeqLen (which is broadcast to the appropriate size) alibi_bias = alibi_bias - torch.arange( 1 - seq_len, 1, dtype=torch.int32, device=device).view( 1, 1, seq_len, 1) alibi_bias = alibi_bias.abs().mul(-1) slopes = gen_slopes(heads, alibi_bias_max, device=device) alibi_bias = alibi_bias * slopes return alibi_bias.to(dtype=dtype) def triton_flash_attn_fn( query, key, value, heads, past_key_value=None, softmax_scale=None, bias=None, key_padding_mask=None, causal=False, dropout=0.0, training=False, needs_weights=False, multiquery=False, ): try: from llmfoundry.models.layers.flash_attn_triton import flash_attn_func except: _installed = False if version.parse(torch.__version__) < version.parse('2.0.0'): _installed = True # if torch1.13.1 revert to using triton flash attn from HazyResearch # with flash-attn==1.0.3.post0 and triton==2.0.0.dev20221202 try: from flash_attn.flash_attn_triton import flash_attn_func except: _installed = False if not _installed: # installing triton-pre-mlir works for both torch1.13.1 and torch2.0+ # default recommendation is to install this variant raise RuntimeError( 'Requirements for `attn_impl: triton` not installed. Either (1) have a CUDA-compatible GPU ' 'and `pip install .[gpu]` if installing from source or ' '`pip install triton-pre-mlir@git+https://github.com/vchiley/triton.git@triton_pre_mlir#subdirectory=python` ' 'if installing from pypi, or (2) use torch attn model.attn_config.attn_impl=torch (torch attn_impl will be slow). ' 'Note: (1) requires you have CMake and PyTorch already installed.' ) check_valid_inputs(query, key, value) if past_key_value is not None: if len(past_key_value) != 0: key = torch.cat([past_key_value[0], key], dim=1) value = torch.cat([past_key_value[1], value], dim=1) past_key_value = (key, value) if bias is not None: # clamp to 0 necessary for torch 2.0 compile() _s_q = max(0, bias.size(2) - query.size(1)) _s_k = max(0, bias.size(3) - key.size(1)) bias = bias[:, :, _s_q:, _s_k:] if dropout: raise NotImplementedError( 'Dropout not implemented for attn_impl: triton.') if needs_weights: raise NotImplementedError( 'attn_impl: triton cannot return attn weights.') if key_padding_mask is not None: warnings.warn( 'Propagating key_padding_mask to the attention module ' +\ 'and applying it within the attention module can cause ' +\ 'unnecessary computation/memory usage. Consider integrating ' +\ 'into bias once and passing that to each attention ' +\ 'module instead.' ) b_size, s_k = key_padding_mask.shape[:2] if bias is None: bias = query.new_zeros(b_size, 1, 1, s_k) bias = bias.masked_fill( ~key_padding_mask.view((b_size, 1, 1, s_k)), torch.finfo(query.dtype).min) query = rearrange(query, 'b s (h d) -> b s h d', h=heads) key = rearrange(key, 'b s (h d) -> b s h d', h=1 if multiquery else heads) value = rearrange(value, 'b s (h d) -> b s h d', h=1 if multiquery else heads) if multiquery: # necessary to repeat instead of expand tensor because # output contains NaN in edge cases such as with head dimension = 8 key = key.repeat(1, 1, heads, 1) value = value.repeat(1, 1, heads, 1) reset_causal = _reset_causal(query.size(1), key.size(1), causal) attn_output = flash_attn_func(query, key, value, bias, reset_causal, softmax_scale) output = attn_output.view(*attn_output.shape[:2], -1) return output, None, past_key_value class MultiHeadAttention(nn.Module): """Multi-head self attention. Using torch or triton attention implemetation enables user to also use additive bias. """ def __init__( self, d_model: int, heads: int, attn_impl: str = 'triton', clip_qkv: Optional[float] = None, qk_ln: bool = False, softmax_scale: Optional[float] = None, attn_pdrop: float = 0.0, norm_type: str = 'low_precision_layernorm', fc_type: str = 'torch', verbose: int = 0, device: Optional[str] = None, ): super().__init__() self.attn_impl = attn_impl self.clip_qkv = clip_qkv self.qk_ln = qk_ln self.d_model = d_model self.heads = heads self.softmax_scale = softmax_scale if self.softmax_scale is None: self.softmax_scale = 1 / math.sqrt(self.d_model / self.heads) self.attn_dropout = attn_pdrop fc_kwargs = {} if fc_type != 'te': fc_kwargs['device'] = device self.Wqkv = FC_CLASS_REGISTRY[fc_type]( self.d_model, 3 * self.d_model, **fc_kwargs, ) # for param init fn; enables shape based init of fused layers fuse_splits = (d_model, 2 * d_model) self.Wqkv._fused = (0, fuse_splits) # type: ignore if self.qk_ln: norm_class = NORM_CLASS_REGISTRY[norm_type.lower()] self.q_ln = norm_class(self.d_model, device=device) self.k_ln = norm_class(self.d_model, device=device) if self.attn_impl == 'flash': self.attn_fn = flash_attn_fn elif self.attn_impl == 'triton': self.attn_fn = triton_flash_attn_fn if verbose: warnings.warn( 'While `attn_impl: triton` can be faster than `attn_impl: flash` ' +\ 'it uses more memory. When training larger models this can trigger ' +\ 'alloc retries which hurts performance. If encountered, we recommend ' +\ 'using `attn_impl: flash` if your model does not use `alibi` or `prefix_lm`.' ) elif self.attn_impl == 'torch': self.attn_fn = scaled_multihead_dot_product_attention if torch.cuda.is_available() and verbose: warnings.warn( 'Using `attn_impl: torch`. If your model does not use `alibi` or ' +\ '`prefix_lm` we recommend using `attn_impl: flash` otherwise ' +\ 'we recommend using `attn_impl: triton`.' ) else: raise ValueError(f'{attn_impl=} is an invalid setting.') self.out_proj = FC_CLASS_REGISTRY[fc_type]( self.d_model, self.d_model, **fc_kwargs, ) self.out_proj._is_residual = True # type: ignore def forward( self, x, past_key_value=None, bias=None, mask=None, causal=True, needs_weights=False, ): qkv = self.Wqkv(x) if self.clip_qkv: qkv = qkv.clamp(min=-self.clip_qkv, max=self.clip_qkv) query, key, value = qkv.chunk(3, dim=2) key_padding_mask = mask if self.qk_ln: # Applying layernorm to qk dtype = query.dtype query = self.q_ln(query).to(dtype) key = self.k_ln(key).to(dtype) context, attn_weights, past_key_value = self.attn_fn( query, key, value, self.heads, past_key_value=past_key_value, softmax_scale=self.softmax_scale, bias=bias, key_padding_mask=key_padding_mask, causal=causal, dropout=self.attn_dropout, training=self.training, needs_weights=needs_weights, ) return self.out_proj(context), attn_weights, past_key_value class MultiQueryAttention(BaseAttention): """Multi-Query self attention. Using torch or triton attention implemetation enables user to also use additive bias. Look for documentation """ def __init__( self, d_model: int, heads: int, attn_impl: str = 'torch', clip_qkv: Optional[float] = None, qk_ln: bool = False, softmax_scale: Optional[float] = None, attn_pdrop: float = 0.0, norm_type: str = 'low_precision_layernorm', fc_type: str = 'torch', verbose: int = 0, device: Optional[str] = None, ): super().__init__() self.attn_impl = attn_impl self.clip_qkv = clip_qkv self.qk_ln = qk_ln self.d_model = d_model self.heads = heads self.head_dim = d_model // heads self.softmax_scale = softmax_scale if self.softmax_scale is None: self.softmax_scale = 1 / math.sqrt(self.head_dim) self.attn_dropout = attn_pdrop fc_kwargs = {} if fc_type != 'te': fc_kwargs['device'] = device # - vchiley self.Wqkv = FC_CLASS_REGISTRY[fc_type]( d_model, d_model + 2 * self.head_dim, **fc_kwargs, ) # for param init fn; enables shape based init of fused layers fuse_splits = (d_model, d_model + self.head_dim) self.Wqkv._fused = (0, fuse_splits) # type: ignore if self.qk_ln: norm_class = NORM_CLASS_REGISTRY[norm_type.lower()] self.q_ln = norm_class(d_model, device=device) self.k_ln = norm_class(self.head_dim, device=device) if self.attn_impl == 'flash': self.attn_fn = flash_attn_fn elif self.attn_impl == 'triton': self.attn_fn = triton_flash_attn_fn if verbose: warnings.warn( 'While `attn_impl: triton` can be faster than `attn_impl: flash` ' +\ 'it uses more memory. When training larger models this can trigger ' +\ 'alloc retries which hurts performance. If encountered, we recommend ' +\ 'using `attn_impl: flash` if your model does not use `alibi` or `prefix_lm`.' ) elif self.attn_impl == 'torch': self.attn_fn = scaled_multihead_dot_product_attention if torch.cuda.is_available() and verbose: warnings.warn( 'Using `attn_impl: torch`. If your model does not use `alibi` or ' +\ '`prefix_lm` we recommend using `attn_impl: flash` otherwise ' +\ 'we recommend using `attn_impl: triton`.' ) else: raise ValueError(f'{attn_impl=} is an invalid setting.') self.out_proj = FC_CLASS_REGISTRY[fc_type]( self.d_model, self.d_model, **fc_kwargs, ) self.out_proj._is_residual = True # type: ignore def forward( self, x, past_key_value=None, bias=None, mask=None, causal=True, needs_weights=False, ): qkv = self.Wqkv(x) if self.clip_qkv: qkv = qkv.clamp(min=-self.clip_qkv, max=self.clip_qkv) query, key, value = qkv.split( [self.d_model, self.head_dim, self.head_dim], dim=2) key_padding_mask = mask if self.qk_ln: # Applying layernorm to qk dtype = query.dtype query = self.q_ln(query).to(dtype) key = self.k_ln(key).to(dtype) context, attn_weights, past_key_value = self.attn_fn( query, key, value, self.heads, past_key_value=past_key_value, softmax_scale=self.softmax_scale, bias=bias, key_padding_mask=key_padding_mask, causal=causal, dropout=self.attn_dropout, training=self.training, needs_weights=needs_weights, multiquery=True, ) return self.out_proj(context), attn_weights, past_key_value
zeta-main
zeta/nn/attention/multiquery_attention.py
import torch from torch import nn from typing import Optional, Any from zeta.nn.attention.attend import Attend class MultiGroupQueryAttention(nn.Module): def __init__( self, dim, heads: int = None, softmax_scale: Optional[float] = None, attn_pdrop: float = 0.0, device: Optional[str] = None, kv_heads: int = None ): super(MultiGroupQueryAttention, self).__init__() self.dim = dim self.heads = heads self.softmax_scale = softmax_scale self.attn_pdrop = attn_pdrop self.device = device self.kv_heads = kv_heads def forward(self): pass
zeta-main
zeta/nn/attention/multi_group_attention.py
"""Zeta Halo""" #attentions from zeta.nn.attention.dilated_attention import DilatedAttention from zeta.nn.attention.flash_attention import FlashAttention from zeta.nn.attention.flash_attention2 import FlashAttentionTwo # from zeta.nn.attention.cross_attention import CrossAttend from zeta.nn.attention.multihead_attention import MultiheadAttention from zeta.nn.attention.multiquery_attention import MultiQueryAttention
zeta-main
zeta/nn/attention/__init__.py
import math import torch from einops import rearrange from torch import einsum, nn from torch.autograd.function import Function from torch.cuda.amp import GradScaler, autocast from torch.nn import DataParallel from zeta.nn.attention.base import BaseAttention # constants EPSILON = 1e-10 # helper functions def exists(val): return val is not None def default(val, d): return val if exists(val) else d # flash attention forwards and backwards # flash attention v1 - https://arxiv.org/abs/2205.14135 # flash attention v2 - https://tridao.me/publications/flash2/flash2.pdf class FlashAttentionFunction(Function): @staticmethod @torch.no_grad() def forward(ctx, q, k, v, mask, causal, q_bucket_size, k_bucket_size): """ Algorithm 1 in the v2 paper """ device = q.device max_neg_value = -torch.finfo(q.dtype).max qk_len_diff = max(k.shape[-2] - q.shape[-2], 0) o = torch.zeros_like(q) all_row_sums = torch.zeros((*q.shape[:-1], 1), device = device) all_row_maxes = torch.full((*q.shape[:-1], 1), max_neg_value, device = device) scale = (q.shape[-1] ** -0.5) num_row_tiles = math.ceil(q.shape[-2] / q_bucket_size) num_col_tiles = math.ceil(k.shape[-2] / k_bucket_size) if exists(mask) and mask.ndim == 2: mask = rearrange(mask, 'b n -> b 1 1 n') if not exists(mask): col_masks = (None,) * num_col_tiles mask = (col_masks,) * num_row_tiles else: mask = ((mask,) * num_row_tiles) if mask.shape[-2] == 1 else mask.split(q_bucket_size, dim = -2) mask = tuple(((row_mask,) * num_col_tiles) if row_mask.shape[-1] == 1 else row_mask.split(k_bucket_size, dim = -1) for row_mask in mask) row_splits = zip( q.split(q_bucket_size, dim = -2), o.split(q_bucket_size, dim = -2), mask, all_row_sums.split(q_bucket_size, dim = -2), all_row_maxes.split(q_bucket_size, dim = -2), ) for ind, (qc, oc, row_mask, row_sums, row_maxes) in enumerate(row_splits): q_start_index = ind * q_bucket_size - qk_len_diff col_splits = zip( k.split(k_bucket_size, dim = -2), v.split(k_bucket_size, dim = -2), row_mask ) for k_ind, (kc, vc, col_mask) in enumerate(col_splits): k_start_index = k_ind * k_bucket_size attn_weights = einsum('... i d, ... j d -> ... i j', qc, kc) * scale if exists(col_mask): attn_weights.masked_fill_(~col_mask, max_neg_value) if causal and q_start_index < (k_start_index + k_bucket_size - 1): causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype = torch.bool, device = device).triu(q_start_index - k_start_index + 1) attn_weights.masked_fill_(causal_mask, max_neg_value) block_row_maxes = attn_weights.amax(dim = -1, keepdims = True) new_row_maxes = torch.maximum(block_row_maxes, row_maxes) exp_weights = torch.exp(attn_weights - new_row_maxes) if exists(col_mask): exp_weights.masked_fill_(~col_mask, 0.) block_row_sums = exp_weights.sum(dim = -1, keepdims = True).clamp(min = EPSILON) exp_values = einsum('... i j, ... j d -> ... i d', exp_weights, vc) exp_row_max_diff = torch.exp(row_maxes - new_row_maxes) new_row_sums = exp_row_max_diff * row_sums + block_row_sums oc.mul_(exp_row_max_diff).add_(exp_values) row_maxes.copy_(new_row_maxes) row_sums.copy_(new_row_sums) oc.div_(row_sums) lse = all_row_sums.log() + all_row_maxes ctx.args = (causal, scale, mask, q_bucket_size, k_bucket_size) ctx.save_for_backward(q, k, v, o, lse) return o @staticmethod @torch.no_grad() def backward(ctx, do): """ Algorithm 2 in the v2 paper """ causal, scale, mask, q_bucket_size, k_bucket_size = ctx.args q, k, v, o, lse = ctx.saved_tensors device = q.device max_neg_value = -torch.finfo(q.dtype).max qk_len_diff = max(k.shape[-2] - q.shape[-2], 0) dq = torch.zeros_like(q) dk = torch.zeros_like(k) dv = torch.zeros_like(v) row_splits = zip( q.split(q_bucket_size, dim = -2), o.split(q_bucket_size, dim = -2), do.split(q_bucket_size, dim = -2), mask, lse.split(q_bucket_size, dim = -2), dq.split(q_bucket_size, dim = -2) ) for ind, (qc, oc, doc, row_mask, lsec, dqc) in enumerate(row_splits): q_start_index = ind * q_bucket_size - qk_len_diff col_splits = zip( k.split(k_bucket_size, dim = -2), v.split(k_bucket_size, dim = -2), dk.split(k_bucket_size, dim = -2), dv.split(k_bucket_size, dim = -2), row_mask ) for k_ind, (kc, vc, dkc, dvc, col_mask) in enumerate(col_splits): k_start_index = k_ind * k_bucket_size attn_weights = einsum('... i d, ... j d -> ... i j', qc, kc) * scale if causal and q_start_index < (k_start_index + k_bucket_size - 1): causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype = torch.bool, device = device).triu(q_start_index - k_start_index + 1) attn_weights.masked_fill_(causal_mask, max_neg_value) p = torch.exp(attn_weights - lsec) if exists(col_mask): p.masked_fill_(~col_mask, 0.) dv_chunk = einsum('... i j, ... i d -> ... j d', p, doc) dp = einsum('... i d, ... j d -> ... i j', doc, vc) D = (doc * oc).sum(dim = -1, keepdims = True) ds = p * scale * (dp - D) dq_chunk = einsum('... i j, ... j d -> ... i d', ds, kc) dk_chunk = einsum('... i j, ... i d -> ... j d', ds, qc) dqc.add_(dq_chunk) dkc.add_(dk_chunk) dvc.add_(dv_chunk) return dq, dk, dv, None, None, None, None # main class # just flash attention in plain pytorch # it will be way slower than implementing it in CUDA # for tinkering and educational purposes class FlashAttentionTwo(BaseAttention): def __init__( self, *, dim: int = None, heads: int = 8, dim_head: int = 64, causal: bool = False, q_bucket_size: int = 512, k_bucket_size: int = 1024, parallel: bool = False, mixed_precision: bool = False ): super().__init__() self.heads = heads self.causal = causal self.parallel = parallel self.mixed_precision = mixed_precision inner_dim = heads * dim_head self.to_q = nn.Linear(dim, inner_dim, bias = False) self.to_kv = nn.Linear(dim, inner_dim * 2, bias = False) self.to_out = nn.Linear(inner_dim, dim, bias = False) # memory efficient attention related parameters # can be overriden on forward self.q_bucket_size = q_bucket_size self.k_bucket_size = k_bucket_size if self.parallel: self.model = DataParallel(self) if self.mixed_precision: self.scaler = GradScaler() def forward( self, x, context = None, mask = None, q_bucket_size = None, k_bucket_size = None, ): q_bucket_size = default(q_bucket_size, self.q_bucket_size) k_bucket_size = default(k_bucket_size, self.k_bucket_size) h = self.heads context = default(context, x) q = self.to_q(x) k, v = self.to_kv(context).chunk(2, dim=-1) q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=h), (q, k, v)) if self.parallel: # Split the input data into chunks and move each chunk to the correct GPU num_gpus = torch.cuda.device_count() x_chunks = x.split(x.size(0) // num_gpus) x_chunks = [chunk.to(f'cuda:{i}') for i, chunk in enumerate(x_chunks)] q = x_chunks if self.mixed_precision: # Use autocast to allow operations to run in lower precision with autocast(): out = FlashAttentionFunction.apply(q, k, v, mask, self.causal, q_bucket_size, k_bucket_size) else: out = FlashAttentionFunction.apply(q, k, v, mask, self.causal, q_bucket_size, k_bucket_size) out = rearrange(out, 'b h n d -> b n (h d)') return self.to_out(out)
zeta-main
zeta/nn/attention/flash_attention2.py
from collections import namedtuple from dataclasses import dataclass from functools import partial, wraps from typing import Optional import torch import torch.nn.functional as F from einops import rearrange, repeat from packaging import version from torch import Tensor, einsum, nn # constants EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) @dataclass class Intermediates: qk_similarities: Optional[Tensor] = None pre_softmax_attn: Optional[Tensor] = None post_softmax_attn: Optional[Tensor] = None def to_tuple(self): return (self.qk_similarities, self.pre_softmax_attn, self.post_softmax_attn) # helpers def exists(val): return val is not None def default(val, d): return val if exists(val) else d def compact(arr): return [*filter(exists, arr)] def once(fn): called = False @wraps(fn) def inner(x): nonlocal called if called: return called = True return fn(x) return inner print_once = once(print) # functions for creating causal mask # need a special one for onnx cpu (no support for .triu) def create_causal_mask(i, j, device): return torch.ones((i, j), device = device, dtype = torch.bool).triu(j - i + 1) def onnx_create_causal_mask(i, j, device): r = torch.arange(i, device = device) causal_mask = rearrange(r, 'i -> i 1') < rearrange(r, 'j -> 1 j') causal_mask = F.pad(causal_mask, (j - i, 0), value = False) return causal_mask # main class class Attend(nn.Module): def __init__( self, *, dropout = 0., causal = False, heads = None, talking_heads = False, sparse_topk = None, scale = None, qk_norm = False, flash = False, add_zero_kv = False, onnxable = False ): super().__init__() self.scale = scale self.qk_norm = qk_norm self.causal = causal self.create_causal_mask = onnx_create_causal_mask if onnxable else create_causal_mask self.attn_fn = partial(F.softmax, dtype = torch.float32) if not qk_norm else F.softmax self.dropout = dropout self.attn_dropout = nn.Dropout(dropout) # talking heads assert not (flash and talking_heads), 'talking heads not compatible with flash attention' self.talking_heads = talking_heads if talking_heads: self.pre_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) self.post_softmax_talking_heads = nn.Conv2d(heads, heads, 1, bias = False) # sparse topk assert not (flash and sparse_topk), 'sparse topk not compatible with flash attention' self.sparse_topk = sparse_topk # add a key / value token composed of zeros # in case this helps controlling outliers, proposed by https://www.evanmiller.org/attention-is-off-by-one.html self.add_zero_kv = add_zero_kv # flash attention self.flash = flash assert not (flash and version.parse(torch.__version__) < version.parse('2.0.0')), 'in order to use flash attention, you must be using pytorch 2.0 or above' # determine efficient attention configs for cuda and cpu self.cpu_config = EfficientAttentionConfig(True, True, True) self.cuda_config = None if not torch.cuda.is_available() or not flash: return device_properties = torch.cuda.get_device_properties(torch.device('cuda')) if device_properties.major == 8 and device_properties.minor == 0: print_once('A100 GPU detected, using flash attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(True, False, False) else: print_once('Non-A100 GPU detected, using math or mem efficient attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(False, True, True) def flash_attn( self, q, k, v, mask = None, attn_bias = None ): batch, heads, q_len, _, k_len, is_cuda, device = *q.shape, k.shape[-2], q.is_cuda, q.device # Recommended for multi-query single-key-value attention by Tri Dao # kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64]) if k.ndim == 3: k = rearrange(k, 'b ... -> b 1 ...').expand_as(q) if v.ndim == 3: v = rearrange(v, 'b ... -> b 1 ...').expand_as(q) # handle scale - by default they scale by dim_head ** -0.5, but need to take care if using cosine sim attention if self.qk_norm: default_scale = q.shape[-1] ** -0.5 q = q * (default_scale / self.scale) # Check if mask exists and expand to compatible shape # The mask is B L, so it would have to be expanded to B H N L causal = self.causal if exists(mask): assert mask.ndim == 4 mask = mask.expand(batch, heads, q_len, k_len) # manually handle causal mask, if another mask was given if causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) mask = mask & ~causal_mask causal = False # handle alibi positional bias # convert from bool to float if exists(attn_bias): attn_bias = rearrange(attn_bias, 'h i j -> 1 h i j').expand(batch, heads, -1, -1) # if mask given, the mask would already contain the causal mask from above logic # otherwise, if no mask given but still causal, mask out alibi positional bias to a large negative number mask_value = -torch.finfo(q.dtype).max if exists(mask): attn_bias = attn_bias.masked_fill(~mask, mask_value // 2) elif causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) attn_bias = attn_bias.masked_fill(causal_mask, mask_value // 2) causal = False # scaled_dot_product_attention handles attn_mask either as bool or additive bias # make it an additive bias here mask = attn_bias # Check if there is a compatible device for flash attention config = self.cuda_config if is_cuda else self.cpu_config # pytorch 2.0 flash attn: q, k, v, mask, dropout, causal, softmax_scale with torch.backends.cuda.sdp_kernel(**config._asdict()): out = F.scaled_dot_product_attention( q, k, v, attn_mask = mask, dropout_p = self.dropout if self.training else 0., is_causal = causal ) return out, Intermediates() def forward( self, q, k, v, mask = None, attn_bias = None, prev_attn = None ): """ einstein notation b - batch h - heads n, i, j - sequence length (base sequence length, source, target) d - feature dimension """ n, heads, kv_heads, device = q.shape[-2], q.shape[1], k.shape[1], q.device scale = default(self.scale, q.shape[-1] ** -0.5) # handle grouped multi-query attention if kv_heads == 1: k, v = map(lambda t: rearrange(t, 'b 1 n d -> b n d'), (k, v)) elif kv_heads < heads: k, v = map(lambda t: repeat(t, 'b kvh n d -> b (r kvh) n d', r = heads // kv_heads), (k, v)) # handle zero kv, as means for allowing network to attend to nothing if self.add_zero_kv: k, v = map(lambda t: F.pad(t, (0, 0, 1, 0), value = 0.), (k, v)) if exists(mask): mask = F.pad(mask, (1, 0), value = True) if exists(attn_bias): attn_bias = F.pad(attn_bias, (1, 0), value = 0.) if self.flash: assert not exists(prev_attn), 'residual attention not compatible with flash attention' return self.flash_attn(q, k, v, mask = mask, attn_bias = attn_bias) kv_einsum_eq = 'b j d' if k.ndim == 3 else 'b h j d' dots = einsum(f'b h i d, {kv_einsum_eq} -> b h i j', q, k) * scale if exists(prev_attn): dots = dots + prev_attn qk_similarities = dots.clone() if self.talking_heads: dots = self.pre_softmax_talking_heads(dots) if exists(attn_bias): dots = dots + attn_bias i, j, dtype = *dots.shape[-2:], dots.dtype mask_value = -torch.finfo(dots.dtype).max if exists(self.sparse_topk) and self.sparse_topk < j: top_values, _ = dots.topk(self.sparse_topk, dim = -1) sparse_topk_mask = dots < top_values[..., -1:] mask = (mask & sparse_topk_mask) if exists(mask) else sparse_topk_mask if exists(mask): dots = dots.masked_fill(~mask, mask_value) if self.causal: causal_mask = self.create_causal_mask(i, j, device = device) dots = dots.masked_fill(causal_mask, mask_value) pre_softmax_attn = dots.clone() attn = self.attn_fn(dots, dim = -1) attn = attn.type(dtype) post_softmax_attn = attn.clone() attn = self.attn_dropout(attn) if self.talking_heads: attn = self.post_softmax_talking_heads(attn) out = einsum(f'b h i j, {kv_einsum_eq} -> b h i d', attn, v) intermediates = Intermediates( qk_similarities = qk_similarities, pre_softmax_attn = pre_softmax_attn, post_softmax_attn = post_softmax_attn ) return out, intermediates # cascading heads logic def to_single_heads(t, dim = 1): heads = t.unbind(dim = dim) return tuple(head.unsqueeze(dim) for head in heads) class CascadingHeads(nn.Module): def __init__(self, attend: Attend): super().__init__() self.attend = attend def forward( self, q, k, v, mask = None, attn_bias = None, prev_attn = None ): assert q.shape[-1] == v.shape[-1], 'cascading heads can only be done if query / key and value head dimensions are the same' # split inputs into per-head inputs heads = q.shape[1] queries = to_single_heads(q) keys = to_single_heads(k) if k.ndim == 4 else ((k,) * heads) values = to_single_heads(v) if v.ndim == 4 else ((v,) * heads) mask = (mask,) * heads attn_bias = to_single_heads(attn_bias, dim = 0) if exists(attn_bias) else ((None,) * heads) prev_attn = to_single_heads(prev_attn) if exists(prev_attn) else ((None,) * heads) # now loop through each head, without output of previous head summed with the next head # thus cascading all_outs = [] all_intermediates = [] prev_head_out = None for h_q, h_k, h_v, h_mask, h_attn_bias, h_prev_attn in zip(queries, keys, values, mask, attn_bias, prev_attn): if exists(prev_head_out): h_q = h_q + prev_head_out out, intermediates = self.attend( h_q, h_k, h_v, mask = h_mask, attn_bias = h_attn_bias, prev_attn = h_prev_attn ) prev_head_out = out all_outs.append(out) all_intermediates.append(intermediates) # cat all output heads all_outs = torch.cat(all_outs, dim = 1) # cat all intermediates, if they exist qk_similarities, pre_softmax_attn, post_softmax_attn = zip(*map(lambda i: i.to_tuple(), all_intermediates)) qk_similarities, pre_softmax_attn, post_softmax_attn = map(compact, (qk_similarities, pre_softmax_attn, post_softmax_attn)) aggregated_intermediates = Intermediates( qk_similarities = torch.cat(qk_similarities, dim = 1) if len(qk_similarities) > 0 else None, pre_softmax_attn = torch.cat(pre_softmax_attn, dim = 1) if len(pre_softmax_attn) > 0 else None, post_softmax_attn = torch.cat(post_softmax_attn, dim = 1) if len(post_softmax_attn) > 0 else None ) return all_outs, aggregated_intermediates
zeta-main
zeta/nn/attention/attend.py
from typing import Optional, Sequence, Tuple, Union import torch import torch.nn.functional as F from einops import rearrange from torch import Tensor, nn from zeta.nn.attention.flash_attention import FlashAttention from zeta.nn.biases.relative_position_bias import RelativePositionBias from zeta.nn.embeddings.xpos_relative_position import XPOS from zeta.nn.attention.base import BaseAttention device = "cuda:0" dtype=torch.float16 class ParallelWrapper: """ A simple wrapper to enable easy usage of data parallelism. Arguments: model: The neural network model to be parallelized. device (optional): The device to which the model should be moved. Default: "cuda". use_data_parallel (optional): A boolean flag to indicate whether to use data parallelism or not. Default: True. """ def __init__( self, model, device="cuda", use_data_parallel=True ): self.model = model.to(device) self.use_data_parallel = use_data_parallel self.device = device if self.use_data_parallel and torch.cuda.device_count() < 1: print(f"Using {torch.cuda.device_count()} GPUS") self.model = nn.DataParallel(self.model) def forward(self, *args, **kwargs): return self.model(*args, **kwargs) def to(self, device): self.device = device self.model= self.model.to(device) return self def __getattr__(self, name): #redirect attribute access to the internal model to allow direct access to its methods and props return getattr(self.model, name) #add alibi, qk layer norm, one write head, multihway, class DilatedAttention(BaseAttention): """ Dilated Attention Module. Arguments: d_model: The dimension of the attention layers. num_heads: The number of attention heads. dilation_rate: The dilation rate for dilated attention. segment_size: The segment size for dilated attention. dropout (optional): The dropout probability. Default: 0.0 casual (optional): If set to True, the attention mechanism is casual. Default: False use_xpos (optional): If set to True, xpos is used for positional encoding. Default: False use_rel_pos_bias (optional): If set to True, relative position bias is used in the attention mechanism. Default: False Usage: The `DilatedAttention` class can be used as a module for neural networks and is especially suited for transformer architectures. Example: attention = DilatedAttention(d_model=512, num_heads=8, dilation_rate=2, segment_size=64, use_xpos=True, use_rel_pos_bias=True) output = attention(input_tensor) This will return the output tensor after applying dilated attention. The `use_xpos` and `use_rel_pos_bias` parameters allow for switching on positional encoding and relative positional bias respectively. """ def __init__(self, d_model: int = None, num_heads: int = None, dilation_rate: int = None, segment_size: int = None, dropout: int = 0.0, casual: bool = False, use_xpos: bool = False, use_rel_pos_bias: bool = False): super(DilatedAttention, self).__init__() self.d_model = d_model self.num_heads = num_heads self.dilation_rate = dilation_rate self.segment_size = segment_size self.dropout = nn.Dropout(dropout) self.casual = casual self.use_xpos = use_xpos self.use_rel_pos_bias = use_rel_pos_bias self.attention = FlashAttention(causal=self.casual, dropout=dropout).to(device) if use_xpos: self.xpos = XPOS(head_dim=d_model//num_heads) if use_rel_pos_bias: self.relative_bias = RelativePositionBias(num_buckets=32, max_distance=128, n_heads=num_heads) #head offsets self.head_offsets = nn.Parameter(torch.randn(num_heads, d_model)) def get_mask(self, i, j): return torch.ones((i, j), device=device, dtype=torch.bool).triu(j - i + 2) def forward(self, x): print(f"X original shape: {x.shape} and x dtype: {x.dtype}") batch_size, seq_len, _ = x.shape padding_len = -seq_len % self.segment_size x = F.pad(x, (0,0,0,padding_len)) seq_len = seq_len + padding_len print(f"Paddex x shape: {x.shape}") if self.use_xpos: x = self.xpos(x) # Split and sparsify x = x.view(batch_size, -1, self.segment_size, self.d_model) print(f"z after view shape: {x.shape}") x = x[:, :, :: self.dilation_rate, :] print(f"x after dilation shape: {x.shape} and x.dtype: {x.dtype}") # Perform attention attn_output = self.attention(x, x, x) print(f"Attn output: {attn_output.shape} and dtype: {attn_output.dtype}") #if use rel pos => apply relative positioning bias if self.use_rel_pos_bias: attn_output += self.relative_bias(batch_size, attn_output.size(1), attn_output.size(1)) print(f"attn_output: {attn_output.shape} and attn output: {attn_output.dtype}") # if casual create a mask and apply to the output if self.casual: mask = self.get_mask(attn_output.size(1), attn_output.size(1)) print(f"mask shape: {mask.shape} and mask dtype: {x.dtype}") attn_output = attn_output.masked_fill(mask, float('-inf')) print(f"attn output shape: {attn_output.shape} and attn_output: {attn_output.dtype}") # apply dropout attn_output = self.dropout(attn_output) print(f"attn output after dropout: {attn_output.shape} and dtype: {attn_output.dtype}") # Scatter and concatenate attn_output = attn_output.reshape(batch_size, -1, self.d_model) print(f"attn_output scatter and concatenate: {attn_output.shape} and {attn_output.dtype}") return attn_output class MultiheadDilatedAttention(nn.Module): def __init__( self, embed_dim: int, num_heads: int, dilation_rates: Sequence[int], segment_lengths: Sequence[int], dropout: float = 0.0, bias: bool = True, layer_norm: bool = True, layer_norm_eps: float = 1e-5, gamma_init: float = 1.0, device: Optional[Union[torch.device, str]] = None, dtype: Optional[torch.dtype] = None, ): super().__init__() self.num_heads = num_heads self.layer_norm = layer_norm self.gamma_init = gamma_init if not embed_dim % self.num_heads == 0: raise ValueError( f"embed_dim ({embed_dim}) must be divisible by " f"num_heads ({num_heads})" ) num_dilations = len(dilation_rates) num_segments = len(segment_lengths) if num_dilations != num_segments: raise ValueError( f"len(dilation_rates) ({num_dilations}) must be equal to " f"len(segment_lengths) ({num_segments})" ) head_dim = embed_dim // num_heads if not head_dim % 8 == 0: raise ValueError( f"head_dim (embed_dim / num_heads = {head_dim}) must be divisible by 8" ) if not head_dim <= 128: raise ValueError( f"head_dim (embed_dim / num_heads = {head_dim}) must be <= 128" ) self.q_proj = nn.Linear( embed_dim, embed_dim, bias=bias, device=device, dtype=dtype ) self.k_proj = nn.Linear( embed_dim, embed_dim, bias=bias, device=device, dtype=dtype ) self.v_proj = nn.Linear( embed_dim, embed_dim, bias=bias, device=device, dtype=dtype ) self.attention = DilatedAttention( segment_lengths=segment_lengths, dilation_rates=dilation_rates, dropout=dropout, # op=op, ) self.norm: Optional[nn.LayerNorm] = None if layer_norm: self.norm = nn.LayerNorm( embed_dim, eps=layer_norm_eps, device=device, dtype=dtype ) self.out_proj = nn.Linear( embed_dim, embed_dim, bias=bias, device=device, dtype=dtype ) self._reset_parameters() def _reset_parameters(self): nn.init.xavier_normal_(self.q_proj.weight) if self.q_proj.bias is not None: nn.init.constant_(self.q_proj.bias, 0) nn.init.xavier_normal_(self.k_proj.weight) if self.k_proj.bias is not None: nn.init.constant_(self.k_proj.bias, 0) # NOTE: We follow the initialization strategy from MAGNETO. See: # https://arxiv.org/pdf/2210.06423.pdf, Fig. 2 # Gain (self.gamma_init) should be provided as a keyword argument when # initializing the larger Transformer model, since it requires knowledge # of the number of encoder/decoder layers in the model. nn.init.xavier_normal_(self.v_proj.weight, gain=self.gamma_init) if self.v_proj.bias is not None: nn.init.constant_(self.v_proj.bias, 0) nn.init.xavier_normal_(self.out_proj.weight, gain=self.gamma_init) if self.out_proj.bias is not None: nn.init.constant_(self.out_proj.bias, 0) def forward( self, query: Tensor, key: Tensor, value: Tensor, is_causal: bool = False ) -> Tuple[Tensor, None]: # Notation: # b - batch size # n - sequence length # h - number of heads # d - embedding dimension # # Input shape: (b, n, d) q = self.q_proj(query) k = self.k_proj(key) v = self.v_proj(value) # Unfold 'd' dimension into 'h' separate attention heads. q = rearrange(q, "b n (h d) -> b n h d", h=self.num_heads) k = rearrange(k, "b n (h d) -> b n h d", h=self.num_heads) v = rearrange(v, "b n (h d) -> b n h d", h=self.num_heads) # Apply attention, then fold 'h' attention heads back into 'd'. x = self.attention(q, k, v, is_causal=is_causal) x = rearrange(x, "b n h d -> b n (h d)") if self.layer_norm: assert self.norm is not None x = self.norm(x) # Linear projection on attention outputs. x = self.out_proj(x) return x, None
zeta-main
zeta/nn/attention/dilated_attention.py
# import torch # import torch.nn as nn # from einops import rearrange # from einops_exts import check_shape, rearrange_many # class SpatialLinearAttention(nn.Module): # def __init__(self, # dim: int = None, # heads: int = 4, # dim_head: int = 32): # super().__init__() # self.scale = dim_head ** -0.5 # self.heads = heads # hidden_dim = dim_head * heads # self.to_qkv = nn.Conv2d(dim, # hidden_dim * 3, # 1, # bias=False) # self.to_out = nn.Conv2d(hidden_dim, # dim, # 1) # def forward(self, x): # b, c, f, h, w = x.shape # x = rearrange(x, 'b c f h w -> (b f) c h w') # qkv = self.to_qkv(x).chunk(3, dim=1) # q, k, v = rearrange_many(qkv, 'b (h c) x y -> b h c (x y)', h = self.heads) # q = q.softmax(dim=-2) # k = k.softmax(dim=-1) # q = q * self.scale # context = torch.einsum('b h d n, b h e n -> b h d e', k, v) # out = torch.einsum('b h d e, b h d n -> b h e n', context, q) # out = rearrange(out, 'b h c (x y) -> b (h c) x y', h=self.heads, x=h, y=w) # out = self.to_out(out) # return rearrange(out, '(b f) c h w -> b c f h w', b=b) # class EinopsToAndFrom(nn.Module): # def __init_(self, from_einops, to_einops, fn): # super().__init__() # self.from_einops = from_einops # self.to_einops = to_einops # self.fn = fn # def forward(self, x, **kwargs): # shape = x.shape # reconstruction_kwargs = dict(tuple(zip(self.from_einops.split(' '), shape))) # x = rearrange(x, f'{self.from_einops} -> {self.to_einops}') # x = self.fn(x, **kwargs) # x = rearrange(x, f"{self.to_einops} -> {self.from_einops}", **reconstitue_kwargs)
zeta-main
zeta/nn/attention/spatial_linear_attention.py
from collections import namedtuple from dataclasses import dataclass from functools import wraps import torch import torch.nn.functional as F from einops import rearrange from packaging import version from torch import Tensor, einsum, nn from zeta.nn.attention.base import BaseAttention # constants EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) # helpers def exists(val): return val is not None def once(fn): called = False @wraps(fn) def inner(x): nonlocal called if called: return called = True return fn(x) return inner print_once = once(print) # main class @dataclass class Intermediates: """ Dataclass to store intermediate tensors during attention computation. Args: qk_similarities (torch.Tensor): Tensor storing the similarities between query and key. pre_softmax_attn (torch.Tensor): Tensor storing the attention weights before softmax. post_softmax_attn (torch.Tensor): Tensor storing the attention weights after softmax. Methods: to_tuple(): Convert the Intermediates object to a tuple. """ qk_similarities: Tensor = None pre_softmax_attn: Tensor = None post_softmax_attn: Tensor = None def to_tuple(self): """ Convert the Intermediates object to a tuple. Returns: tuple: Tuple representation of the Intermediates object. """ return (self.qk_similarities, self.pre_softmax_attn, self.post_softmax_attn) class FlashAttention(BaseAttention): def __init__( self, causal: bool = False, dropout: float = 0., flash: bool = True ): """ FlashAttention module that performs attention computation. Args: causal (bool): Whether to apply causal masking (default: False). dropout (float): Dropout probability (default: 0.). flash (bool): Whether to use flash attention (default: True). """ super().__init__() self.dropout = dropout self.attn_dropout = nn.Dropout(dropout) self.causal = causal self.flash = flash assert not (flash and version.parse(torch.__version__) < version.parse('2.0.0')), 'in order to use flash attention, you must be using pytorch 2.0 or above' # determine efficient attention configs for cuda and cpu self.cpu_config = EfficientAttentionConfig(True, True, True) self.cuda_config = None if not torch.cuda.is_available() or not flash: return device_properties = torch.cuda.get_device_properties(torch.device('cuda')) if device_properties.major == 8 and device_properties.minor == 0: print_once('A100 GPU detected, using flash attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(True, False, False) else: print_once('Non-A100 GPU detected, using math or mem efficient attention if input tensor is on cuda') self.cuda_config = EfficientAttentionConfig(False, True, True) def get_mask(self, i, j, device): """ Generate a mask for attention computation. Args: i (int): Length of the query sequence. j (int): Length of the key sequence. device (torch.device): Device to place the mask tensor. Returns: torch.Tensor: Mask tensor of shape (i, j). """ return torch.ones((i, j), device=device, dtype=torch.bool).triu(j - i + 1) def flash_attn( self, q, k, v, mask = None, attn_bias = None ): """ Perform flash attention computation. Args: q (torch.Tensor): Query tensor of shape (batch, heads, q_len, dim). k (torch.Tensor): Key tensor of shape (batch, heads, k_len, dim). v (torch.Tensor): Value tensor of shape (batch, heads, v_len, dim). mask (torch.Tensor): Mask tensor of shape (batch, heads, q_len, k_len) (default: None). attn_bias (torch.Tensor): Attention bias tensor of shape (batch, heads, q_len, k_len) (default: None). Returns: torch.Tensor: Output tensor of shape (batch, heads, q_len, dim). """ batch, heads, q_len, _, k_len, is_cuda, device = *q.shape, k.shape[-2], q.is_cuda, q.device # Recommended for multi-query single-key-value attention by Tri Dao # kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64]) if k.ndim == 3: k = rearrange(k, 'b ... -> b 1 ...').expand_as(q) if v.ndim == 3: v = rearrange(v, 'b ... -> b 1 ...').expand_as(q) # handle scale - by default they scale by dim_head ** -0.5, but need to take care if using cosine sim attention # Check if mask exists and expand to compatible shape # The mask is B L, so it would have to be expanded to B H N L causal = self.causal if exists(mask): assert mask.ndim == 4 mask = mask.expand(batch, heads, q_len, k_len) # manually handle causal mask, if another mask was given if causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) mask = mask & ~causal_mask causal = False # handle alibi positional bias # convert from bool to float if exists(attn_bias): attn_bias = rearrange(attn_bias, 'h i j -> 1 h i j').expand(batch, heads, -1, -1) # if mask given, the mask would already contain the causal mask from above logic # otherwise, if no mask given but still causal, mask out alibi positional bias to a large negative number mask_value = -torch.finfo(q.dtype).max if exists(mask): attn_bias = attn_bias.masked_fill(~mask, mask_value // 2) elif causal: causal_mask = self.create_causal_mask(q_len, k_len, device = device) attn_bias = attn_bias.masked_fill(causal_mask, mask_value // 2) causal = False # scaled_dot_product_attention handles attn_mask either as bool or additive bias # make it an additive bias here mask = attn_bias # Check if there is a compatible device for flash attention config = self.cuda_config if is_cuda else self.cpu_config # pytorch 2.0 flash attn: q, k, v, mask, dropout, causal, softmax_scale with torch.backends.cuda.sdp_kernel(**config._asdict()): out = F.scaled_dot_product_attention( q, k, v, attn_mask = mask, dropout_p = self.dropout if self.training else 0., is_causal = causal ) return out def forward(self, q, k, v, mask = None, attn_bias = None): """ Perform attention computation. einstein notation b - batch h - heads n, i, j - sequence length (base sequence length, source, target) d - feature dimension Args: q (torch.Tensor): Query tensor of shape (batch, heads, q_len, dim). k (torch.Tensor): Key tensor of shape (batch, heads, k_len, dim). v (torch.Tensor): Value tensor of shape (batch, heads, v_len, dim). mask (torch.Tensor): Mask tensor of shape (batch, heads, q_len, k_len) (default: None). attn_bias (torch.Tensor): Attention bias tensor of shape (batch, heads, q_len, k_len) (default: None). Returns: torch.Tensor: Output tensor of shape (batch, heads, q_len, dim). """ q_len, k_len, device = q.shape[-2], k.shape[-2], q.device scale = q.shape[-1] ** -0.5 kv_einsum_eq = 'b j d' if k.ndim == 3 else 'b h j d' if self.flash: return self.flash_attn(q, k, v, mask = mask, attn_bias = attn_bias) # similarity sim = einsum(f"b h i d, {kv_einsum_eq} -> b h i j", q, k) * scale # attention bias if exists(attn_bias): sim = sim + attn_bias # causal mask if self.causal: causal_mask = self.get_mask(q_len, k_len, device) sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max) # attention attn = sim.softmax(dim=-1) attn = self.attn_dropout(attn) # aggregate values out = einsum(f"b h i j, {kv_einsum_eq} -> b h i d", attn, v) return out
zeta-main
zeta/nn/attention/flash_attention.py
from zeta.nn.architecture.transformer import AttentionLayers class CrossAttend(AttentionLayers): def __init__(self, **kwargs): super().__init__(cross_attend=True, only_cross=True, **kwargs)
zeta-main
zeta/nn/attention/cross_attention.py
import torch from torch import nn, einsum from einops import rearrange, repeat, pack, unpack from zeta.nn.embeddings.sinusoidal import SinusoidalEmbeddings, apply_rotary_pos_emb from zeta.utils.main import exists, default, pad_to_multiple, l2norm, look_around, max_neg_values #constant TOKEN_SELF_ATTN_VALUE = -5e4 class LocalAttention(nn.Module): """ The LocalAttention module provides a mechanism to perform local attention operations. Unlike global attention where every token can attend to every other token, in local attention each token can only attend to a subset of tokens within a defined window. This reduces the computational cost and captures the local structure in sequences like text or time-series data. window_size: (int) The size of the attention window. causal: (bool, optional) If set to True, ensures causal attention. Default: False. look_backward: (int, optional) How many positions to look backward from the current position. Default: 1. look_forward: (int, optional) How many positions to look forward from the current position. Default: None which implies 0 if causal is True. dropout: (float, optional) Dropout rate for attention weights. Default: 0.. shared_qk: (bool, optional) If set to True, the query and key are the same. Useful for certain types of attention mechanisms. Default: False. rel_pos_emb_config: (Optional) Deprecated. Configuration for the relative positional embeddings. dim: (int, optional) Dimension of embeddings. Only needed if rel_pos_emb_config is not provided. autopad: (bool, optional) If set to True, sequence will be automatically padded to be divisible by the window size. Default: False. exact_windowsize: (bool, optional) Ensures exact window size for non-causal attention. Default: False. scale: (Optional) Scaling factor for the queries. use_rotary_pos_emb: (bool, optional) If set to True, rotary positional embeddings will be used. Default: True. use_xpos: (bool, optional) If set to True, allows for extrapolation of window sizes. Requires use_rotary_pos_emb to be True. Default: False. xpos_scale_base: (Optional) Base scaling factor for extrapolated window sizes. """ def __init__( self, window_size, causal = False, look_backward = 1, look_forward = None, dropout = 0., shared_qk = False, rel_pos_emb_config = None, dim = None, autopad = False, exact_windowsize = False, scale = None, use_rotary_pos_emb = True, use_xpos = False, xpos_scale_base = None ): super().__init__() look_forward = default(look_forward, 0 if causal else 1) assert not (causal and look_forward > 0), 'you cannot look forward if causal' self.scale = scale self.window_size = window_size self.autopad = autopad self.exact_windowsize = exact_windowsize self.causal = causal self.look_backward = look_backward self.look_forward = look_forward self.dropout = nn.Dropout(dropout) self.shared_qk = shared_qk # relative positions self.rel_pos = None self.use_xpos = use_xpos if use_rotary_pos_emb and (exists(rel_pos_emb_config) or exists(dim)): # backwards compatible with old `rel_pos_emb_config` deprecated argument if exists(rel_pos_emb_config): dim = rel_pos_emb_config[0] self.rel_pos = SinusoidalEmbeddings( dim, use_xpos = use_xpos, scale_base = default(xpos_scale_base, window_size // 2) ) """ Forward Method Parameters q: (Tensor) The query tensor. k: (Tensor) The key tensor. v: (Tensor) The value tensor. mask: (Optional[Tensor]) A mask tensor for the keys. Can also be passed as input_mask. input_mask: (Optional[Tensor]) Another way to pass the mask tensor for keys. attn_bias: (Optional[Tensor]) Additional biases to add to the attention scores. window_size: (Optional[int]) If provided, this window size will override the default window size defined during initialization. Returns out: (Tensor) The output tensor after the attention operation. """ def forward( self, q, k, v, mask = None, input_mask = None, attn_bias = None, window_size = None ): mask = default(mask, input_mask) assert not (exists(window_size) and not self.use_xpos), 'cannot perform window size extrapolation if xpos is not turned on' shape, autopad, pad_value, window_size, causal, look_backward, look_forward, shared_qk = q.shape, self.autopad, -1, default(window_size, self.window_size), self.causal, self.look_backward, self.look_forward, self.shared_qk # https://github.com/arogozhnikov/einops/blob/master/docs/4-pack-and-unpack.ipynb (q, packed_shape), (k, _), (v, _) = map(lambda t: pack([t], '* n d'), (q, k, v)) # auto padding if autopad: orig_seq_len = q.shape[1] (needed_pad, q), (_, k), (_, v) = map(lambda t: pad_to_multiple(t, self.window_size, dim = -2), (q, k, v)) b, n, dim_head, device, dtype = *q.shape, q.device, q.dtype scale = default(self.scale, dim_head ** -0.5) assert (n % window_size) == 0, f'sequence length {n} must be divisible by window size {window_size} for local attention' windows = n // window_size if shared_qk: k = l2norm(k) seq = torch.arange(n, device = device) b_t = rearrange(seq, '(w n) -> 1 w n', w = windows, n = window_size) # bucketing bq, bk, bv = map(lambda t: rearrange(t, 'b (w n) d -> b w n d', w = windows), (q, k, v)) bq = bq * scale look_around_kwargs = dict( backward = look_backward, forward = look_forward, pad_value = pad_value ) bk = look_around(bk, **look_around_kwargs) bv = look_around(bv, **look_around_kwargs) # rotary embeddings if exists(self.rel_pos): pos_emb, xpos_scale = self.rel_pos(bk) bq, bk = apply_rotary_pos_emb(bq, bk, pos_emb, scale = xpos_scale) # calculate positions for masking bq_t = b_t bq_k = look_around(b_t, **look_around_kwargs) bq_t = rearrange(bq_t, '... i -> ... i 1') bq_k = rearrange(bq_k, '... j -> ... 1 j') pad_mask = bq_k == pad_value sim = einsum('b h i e, b h j e -> b h i j', bq, bk) if exists(attn_bias): heads = attn_bias.shape[0] assert (b % heads) == 0 attn_bias = repeat(attn_bias, 'h i j -> (b h) 1 i j', b = b // heads) sim = sim + attn_bias mask_value = max_neg_values(sim) if shared_qk: self_mask = bq_t == bq_k sim = sim.masked_fill(self_mask, TOKEN_SELF_ATTN_VALUE) del self_mask if causal: causal_mask = bq_t < bq_k if self.exact_windowsize: max_causal_window_size = (self.window_size * self.look_backward) causal_mask = causal_mask | (bq_t > (bq_k + max_causal_window_size)) sim = sim.masked_fill(causal_mask, mask_value) del causal_mask # masking out for exact window size for non-causal # as well as masking out for padding value if not causal and self.exact_windowsize: max_backward_window_size = (self.window_size * self.look_backward) max_forward_window_size = (self.window_size * self.look_forward) window_mask = ((bq_k - max_forward_window_size) > bq_t) | (bq_t > (bq_k + max_backward_window_size)) | pad_mask sim = sim.masked_fill(window_mask, mask_value) else: sim = sim.masked_fill(pad_mask, mask_value) # take care of key padding mask passed in if exists(mask): batch = mask.shape[0] assert (b % batch) == 0 h = b // mask.shape[0] if autopad: _, mask = pad_to_multiple(mask, window_size, dim = -1, value = False) mask = rearrange(mask, '... (w n) -> (...) w n', w = windows, n = window_size) mask = look_around(mask, **{**look_around_kwargs, 'pad_value': False}) mask = rearrange(mask, '... j -> ... 1 j') mask = repeat(mask, 'b ... -> (b h) ...', h = h) sim = sim.masked_fill(~mask, mask_value) del mask # attention attn = sim.softmax(dim = -1) attn = self.dropout(attn) # aggregation out = einsum('b h i j, b h j e -> b h i e', attn, bv) out = rearrange(out, 'b w n d -> b (w n) d') if autopad: out = out[:, :orig_seq_len, :] out, *_ = unpack(out, packed_shape, '* n d') return out
zeta-main
zeta/nn/attention/local_attention.py
import math import torch import torch.nn.functional as F from torch import Tensor, nn, einsum from typing import Tuple, Optional from einops import rearrange, repeat, reduce, pack, unpack from zeta.models.vit import exists from zeta.nn.architecture.attn_layers import RMSNorm, apply_rotary_pos_emb from zeta.nn.attention.attend import Attend from zeta.nn.attention.local_attention_mha import LocalMHA from zeta.utils.main import default, pad_to_multiple from colt5_attention import CoordinateDescentRouter class Attention(nn.Module): def __init__( self, dim, *, dim_head=64, dim_context=None, heads=8, causal=False, groups=1, dropout=0., flash=False, prenorm=False ): super().__init__() self.heads = heads self.groups = groups dim_inner = dim_head * heads dim_context = default(dim_context, dim) self.norm = RMSNorm( dim, groups=groups ) if prenorm else nn.Identity() self.context_norm = RMSNorm( dim_context, groups=groups ) if prenorm else nn.Identity() self.attend = Attend( dropout=dropout, causal=causal, flash=flash ) #null key value to proetect againsta row that is masked self.null_kv = nn.Parameter(torch.randn( 2, groups, heads, 1, dim_head )) #utilizing convo groups to process experts in parallel self.to_q = nn.Conv1d( dim * groups, dim_inner * groups, 1, bias=False, groups=groups ) self.to_kv = nn.Conv1d( dim_context * groups, dim_inner * 2 * groups, 1, bias=False, groups=groups ) self.to_out = nn.Conv1d( dim_inner * groups, dim * groups, 1, bias=False, groups=groups ) def forward( self, x, context = None, mask=None, queries_scale=None, keys_scale=None, values_scale=None, output_scale=None, rotary_emb: Optional[Tuple[Tensor, Tensor]] = None ): """ Einops b - batch g - groups n - sequence d - feature dimension """ b, g, h = x.shape[0], self.groups, self.heads one_expert = x.ndim == 3 if one_expert: assert g == 1 x = rearrange(x, 'b n d -> b 1 n d') assert x.ndim == 4 assert x.shape[1] == g #fold the groups into the feature dimension to be processed in one go by grouped convolutional x = rearrange(x, 'b g n d -> b g d n') #handle context for cross attention if exists(context): context_one_expert = context.ndim == 3 if context_one_expert: assert g == 1 context = rearrange(context, 'b n d -> b 1 n d') assert context.ndim == 4 assert context.shape[1] == g context = rearrange(context, 'b g n d -> b g d n') context = default(context, x) #take care of mask if exists(mask): if mask.ndim == 2: mask = repeat(mask, 'b n -> (b g) n', g=g) elif mask.ndim == 3: mask = rearrange(mask, 'b g n -> (b d) n') mask = F.pad(mask, (1, 0), value=True) #prenorm x = self.norm(x) context = self.context_norm(context) #fold groups into dimension for grouped conv x, context = map(lambda t: rearrange(t, 'b g d n -> b (g d) n'), (x, context)) #q, k, v q, k, v = (self.to_q(x), *self.to_kv(context).chunk(2, dim=1)) #split heads and merge groups into batches q, k, v = map(lambda t: rearrange(t, 'b (g h d) n -> b g h n d', h=h, g=g), (q, k, v)) #rotary embedding if exists(rotary_emb): q_rotary_emb, k_rotary_emb = rotary_emb if q_rotary_emb.ndim > 2: q_rotary_emb = rearrange(q_rotary_emb, 'b g n d -> b g 1 n d') if k_rotary_emb.ndim > 2: k_rotary_emb = rearrange(k_rotary_emb, 'b g n d -> b g 1 n d') q = apply_rotary_pos_emb(q_rotary_emb, q) k = apply_rotary_pos_emb(k_rotary_emb, k) #give gradients to routed keys/values via normalized scores from the router, if passed in if exists(queries_scale): q = q * queries_scale if exists(keys_scale): k = k * keys_scale if exists(values_scale): v = v * values_scale #merge group into batch q, k, v = map(lambda t: rearrange(t, 'b g ... -> (b g) ...'), (q, k, v)) #concat null key /values, to protect against a row having all masked out elements and have a save a lot of headache nk, nv = map(lambda t: repeat(t, 'g h 1 d -> (b g) h 1 d', b=b), self.null_kv) k = torch.cat((nk, k), dim=-2) v = torch.cat((nv, v), dim=-2) #attention out = self.attend(q, k, v, mask=mask) #combine heads out out = rearrange(out, '(b g) h n d -> b (g h d) n', g=g) out = self.to_out(out) out = rearrange(out, 'b (g d) n -> b g n d', g=g) if one_expert: out = rearrange(out, 'b 1 n d -> b n d') if exists(output_scale): out = out * output_scale return out class MixtureOfAttention(nn.Module): def __init__( self, dim, *, num_routed_queries, num_routed_key_values, dim_context=None, local_attn=False, local_attn_window_size=None, num_experts, dim_head=64, heads=8, dropout=0., use_triton=True, flash_attn=True, prenorm=True, average_routed=False, **kwargs ): super().__init__() dim_context = default(dim_context, dim) self.num_routed_queries = num_routed_queries self.num_routed_key_values = num_routed_key_values self.null_routed_token = nn.Parameter(torch.randn(1, 1, dim)) if not local_attn else None self.average_routed = average_routed self.local_attn = None if local_attn: assert exists(local_attn_window_size) self.local_attn = LocalMHA( dim=dim, dim_head=dim_head, head=heads, prenorm=prenorm, window_size=local_attn_window_size ) self.query_router = CoordinateDescentRouter( dim, num_routing_tokens=num_experts, use_triton=use_triton, **kwargs ) self.key_value_router = CoordinateDescentRouter( dim_context, num_routing_tokens=num_experts, use_triton=use_triton, **kwargs ) self.attn = Attention( dim=dim, dim_context=dim_context, dim_head=dim_head, heads=heads, groups=num_experts, dropout=dropout, flash=flash_attn, prenorm=prenorm ) @property def device(self): return next(self.parameters()).device def forward( self, x, context=None, mask=None, context_mask=None, num_routed_queries=None, num_routed_key_values=None, rotary_emb=None ): num_routed_queries = default(num_routed_queries, self.num_routed_queries) num_routed_key_values = default(num_routed_key_values, self.num_routed_key_values) is_cross_attn = exists(context) assert not (exists(self.local_attn) and is_cross_attn), 'cannot do cross attention with local attention' if not is_cross_attn: #self attention if context and context mask not passed in context = x context_mask = mask query_indices, query_scores, queries, query_mask = self.query_router( x, mask=mask, num_routed=num_routed_queries, keep_one_route_dim=True ) query_score = rearrange(query_scores, 'b g n -> b g n 1') kv_indices, key_value_scores, key_values, key_value_mask = self.key_value_router( context, mask=context_mask, num_tokens=num_routed_key_values, keep_one_route_dim=True ) key_value_scores = rearrange(key_value_scores, 'b g n -> b g 1 n 1') #rotary embeddings if exists(rotary_emb): assert not is_cross_attn, 'rotary embedding should not be used for cross attending' q_rotary_emb = rotary_emb[query_indices] if exists(query_indices) else rotary_emb k_rotary_emb = rotary_emb[kv_indices] if exists(kv_indices) else rotary_emb rotary_emb = (q_rotary_emb, k_rotary_emb) #attend attn_out = self.attn( queries, rotary_emb=rotary_emb, context=key_values, mask=key_value_mask, values_scale=key_value_scores, output_scale=query_scores ) local_out = None if exists(self.local_attn): local_out = self.local_attn(x, mask=mask) need_route_queries = exists(query_indices) if not need_route_queries: out = attn_out if exists(local_out): local_out = rearrange(local_out, 'b n d -> b 1 n d') out = torch.cat((local_out, out), dim=1) out = reduce(attn_out, 'b e n d -> b n d', 'mean') if exists(mask): out = out.masked_fill(~mask[..., None], 0.) out = torch.zeros_like(x) counts = torch.zeros(x.shape[:-1], device=x.device) query_indices = rearrange(query_indices, 'b g n -> b (g n)') attn_out = rearrange(attn_out, 'b g n d -> b (g n) d') expanded_query_indices = repeat(query_indices, 'b n -> b n d', d=x.shape[-1]) attn_out_summed = out.scatter_add(1, expanded_query_indices, attn_out) ones = torch.ones(attn_out.shape[:-1], device=self.device) if exists(query_mask): ones = ones * rearrange(query_mask, 'b g n -> b (g n)') counts = counts.scatter_add(1, query_indices, ones) counts = rearrange(counts, '... -> ... 1') has_unrouted = not exists(local_out) if not has_unrouted: counts = counts + 1 attn_out_summed = attn_out_summed + local_out else: not_routed_mask = counts == 0 attn_out_summed = attn_out_summed.masked_fill(not_routed_mask, 0.) out = attn_out_summed #average if needed if self.average_routed: out = out / counts.clamp(min=1e-5) #for the position that were not routed, use a learned routing token instead of just 0s if has_unrouted: out = torch.where( not_routed_mask, self.null_routed_token, out ) if exists(mask): out = out.masked_fill(~mask[..., None], 0.) return out class MixtureOfAutoregressiveAttention(nn.Module): def __init__( self, dim, *, num_routed_queries, num_routed_key_values, local_attn_window_size, routed_window_size=None, num_experts=2, dim_head=64, heads=8, dropout=0., use_triton=False, flash_attn=True, prenorm=True, average_routed=False, **kwargs ): super().__init__() self.num_routed_queries = num_routed_queries self.num_routed_key_values = num_routed_key_values self.num_experts = num_experts self.null_tokens = nn.Parameter(torch.randn(num_experts, dim)) routed_window_size = default(routed_window_size, local_attn_window_size) self.routed_window_size = routed_window_size self.average_routed = average_routed self.local_attn = LocalMHA( dim=dim, dim_head=dim_head, heads=heads, prenorm=prenorm, causal=True, window_size=local_attn_window_size, ) self.query_router = CoordinateDescentRouter( dim, num_routing_tokens=num_experts, use_triton=use_triton, **kwargs ) self.key_value_tensor = CoordinateDescentRouter( dim, num_routing_tokens=num_experts, use_triton=use_triton, **kwargs ) self.attn = Attention( dim=dim, dim_head=dim_head, heads=heads, groups=num_experts, dropout=dropout, flash=flash_attn, prenorm=prenorm ) @property def device(self): return next(self.parameters()).device def forward( self, x, rotary_emb=None, num_routed_queries=None, num_routed_key_values=None ): b = x.shape[0] w = self.routed_window_size num_windows = math.ceil(x.shape[-2] / w) - 1 #cal local attn local_out = self.local_attn(x) if num_windows == 0: return local_out #pad sequence to multiple of routing window size mask = torch.ones(x.shape[:-1], device=self.device, dtype=torch.bool) x, seq_len = pad_to_multiple(x, w, dim=-2) mask, _ = pad_to_multiple(mask, w, dim=-1, value=False) context = x[..., :-w, :] context = repeat(context, 'b n d -> (b nw) n d', mw=num_windows) context_mask = torch.ones((num_windows, num_windows), device=self.device, dtype=torch.bool).tril() context_mask = repeat(context_mask, 'n1 n2 -> (b n1) (n2 w)', b=b, w=w) #fold queries and mask into windows x = rearrange(x, 'b (n w) d -> b n w d', w=w) mask = rearrange(mask, 'b (n w) -> b n w', w=w) #omit the first window of queries as they have nothing to attend to x = rearrange(x[:, 1:, ...], 'b n w d -> (b n) w d') mask = rearrange(mask[:, 1:, ...], 'b n w -> (b n) w') #gets number of queries and key values to route num_routed_queries = default(num_routed_queries, self.num_routed_queries) num_routed_key_values = default(num_routed_key_values, self.num_routed_key_values) #coordinate descent routing query_indices, query_scores, queries, query_mask = self.query_router( x, mask=mask, num_tokens=num_routed_queries, keep_one_route_dim=True ) query_scores = rearrange(query_scores, 'b g n -> b g n 1') kv_indices, key_value_scores, key_values, key_value_mask = self.key_value_router( context, mask=context_mask, num_tokens=num_routed_key_values, keep_one_route_dim=True ) key_value_scores = rearrange(key_value_scores, 'b g n -> b g 1 n 1') if exists(rotary_emb): rotary_emb, _ = pad_to_multiple(rotary_emb, w, dim=-2) windowed_rotary_emb = rearrange(rotary_emb, '(n w) d -> n w d', w=w) windowed_rotary_emb = windowed_rotary_emb[1:] windowed_rotary_emb = repeat( windowed_rotary_emb, 'n w d -> (b n) g w d', b=b, g=query_scores.shape[1] ) if exists(query_indices): rotary_query_indices = repeat( query_indices, '... -> ... d', d=windowed_rotary_emb.shape[-1] ) q_rotary_emb = windowed_rotary_emb.gather(2, rotary_query_indices) else: q_rotary_emb = windowed_rotary_emb k_rotary_emb = rotary_emb[kv_indices] if exists(kv_indices) else rotary_emb[:context.shape[-2]] rotary_emb = (q_rotary_emb, k_rotary_emb) attn_out = self.attn( queries, rotary_emb=rotary_emb, context=key_values, mask=key_value_mask, values_scale=key_value_scores, output_scale=query_scores ) need_route_queries = exists(query_indices) if not need_route_queries: out = F.pad( attn_out, (0, 0, w, 0), value=0. ) out = out[:, :, :seq_len] if exists(local_out): local_out = rearrange(local_out, 'b n d -> b 1 n d') out = torch.cat((local_out, out), dim=1) out = reduce( out, 'b e n d -> b n d', 'mean' if self.averaged_routed else "sum" ) out = torch.zeros(( x.shape[0], self.num_experts, *x.shape[1:] ), device=x.device, dtype=x.dtype) counts = torch.zeros( ( x.shape[0], self.num_experts, x.shape[-2] ), device=x.device ) ones = torch.ones( attn_out.shape[:-1], device=self.device ) if exists(query_mask): ones = ones * query_mask counts = counts.scatter_add( 2, query_indices, ones ) expanded_query_indices = repeat( query_indices, 'b g n -> b g n d', d=x.shape[-1] ) attn_out_summed = out.scatter_add( 2, expanded_query_indices, attn_out ) #for the positions that were not routed fill each with individual expert null token fill_null_tokens = counts == 0 & ~rearrange(mask, 'b n -> b 1 n') attn_out_summed = torch.where( rearrange(fill_null_tokens, '... -> ... 1'), rearrange(self.null_tokens, 'g d -> 1 g 1 d'), attn_out_summed ) #un window the attention output as well as the routed counts attn_out_summed = rearrange(attn_out_summed, '(b n) g w d -> b g (n w) d', b=b) attn_out_summed = F.pad( attn_out_summed, (0, 0, w, 0), value=0. ) attn_out_summed = attn_out_summed[..., :seq_len, :] #local attended tokens with routed otkens attn_out_summed = reduce(attn_out_summed, 'b g n d -> b n d', 'sum') attn_out_summed = attn_out_summed + local_out #in expert seems to perform better while averaging if not self.averaged_routed: return attn_out_summed #avg tokens return attn_out_summed / (self.num_experts + 1)
zeta-main
zeta/nn/attention/mixture_attention.py
from abc import abstractmethod import torch.nn as nn class BaseAttention(nn.Module): @abstractmethod def __init__(self): super().__init__() @abstractmethod def forward(self, x, context=None, mask=None): pass
zeta-main
zeta/nn/attention/base.py
import torch import torch.nn as nn def fixed_pos_embedding(x): """ Generates fixed positional embeddings for the input tensor. Args: - x: Input tensor of shape (seq_len, dim) Returns: - sin: Sine positional embeddings of shape (seq_len, dim) - cos: Cosine positional embeddings of shape (seq_len, dim) """ seq_len, dim = x.shape inv_freq = 1.0 / (10000 ** (torch.arange(0, dim) / dim)) sinusoid_inp = ( torch.einsum("i , j -> i j", torch.arange(0, seq_len, dtype=torch.float), inv_freq).to(x) ) return torch.sin(sinusoid_inp), torch.cos(sinusoid_inp) def rotate_every_two(x): """ Rearranges the elements of the input tensor by rotating every two elements. Args: - x: Input tensor of shape (batch_size, seq_len, dim) Returns: - x: Rearranged tensor of shape (batch_size, seq_len, dim) """ x1 = x[:, :, ::2] x2 = x[:, :, 1::2] x = torch.stack((-x2, x1), dim=-1) return x.flatten(-2) def duplicate_interleave(m): """ Duplicates a matrix while interleaving the copy. Args: - m: Input matrix Returns: - m: Duplicated and interleaved matrix """ dim0 = m.shape[0] m = m.view(-1, 1) m = m.repeat(1, 2) m = m.view(dim0, -1) return m def apply_rotary_pos_emb(x, sin, cos, scale=1): """ Applies rotary positional embeddings to the input tensor. Args: - x: Input tensor of shape (batch_size, seq_len, dim) - sin: Sine positional embeddings of shape (seq_len, dim) - cos: Cosine positional embeddings of shape (seq_len, dim) - scale: Scaling factor for the positional embeddings Returns: - x: Tensor with applied rotary positional embeddings """ sin, cos = map(lambda t: duplicate_interleave(t * scale), (sin, cos)) return (x * cos) + (rotate_every_two(x) * sin) class XPOS(nn.Module): def __init__( self, head_dim: int = None, scale_base: int = 512 ): super().__init__() self.head_dim = head_dim self.scale_base = scale_base self.register_buffer( "scale", (torch.arange(0, head_dim, 2) + 0.4 * head_dim) / (1.4 * head_dim) ) def forward(self, x, offset=0, downscale=False): """ Forward pass of the XPOS module. Args: - x: Input tensor of shape (batch_size, seq_len, dim) - offset: Offset value for positional embeddings - downscale: Boolean indicating whether to downscale the positional embeddings Returns: - x: Tensor with applied rotary positional embeddings """ length = x.shape[1] min_pos = -(length + offset) // 2 max_pos = length + offset + min_pos scale = self.scale ** torch.arange(min_pos, max_pos, 1).to(self.scale).div(self.scale_base)[:, None] sin, cos = fixed_pos_embedding(scale) if scale.shape[0] > length: scale = scale[-length:] sin = sin[-length:] cos = cos[-length:] if downscale: scale = 1 / scale x = apply_rotary_pos_emb(x, sin, cos, scale) return x
zeta-main
zeta/nn/embeddings/xpos_relative_position.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] from torch import nn from zeta.nn.embeddings.base import BaseEmbedding #Other embedding class NominalEmbedding(BaseEmbedding): def forward(self, num_tokens: int, dim: int) -> nn.Module: embedding = nn.Embedding(num_tokens, dim) return embedding
zeta-main
zeta/nn/embeddings/nominal_embeddings.py
import torch from torch import nn from zeta.utils.main import exists, l2norm class AbsolutePositionalEmbedding(nn.Module): def __init__(self, dim, max_seq_len, l2norm_embed=False): super().__init__() self.scale = dim ** -0.5 if not l2norm_embed else 1. self.max_seq_len = max_seq_len self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(max_seq_len, dim) def forward(self, x, pos=None): seq_len, device = x.shape[-1], x.device assert seq_len <= self.max_seq_len, f"You are passing in a sequence length of {seq_len} but you absolute positional embedding has a max of length of {self.max_seq_len}" if not exists(pos): pos = torch.arange(seq_len, device=device) pos_emb = self.emb(pos) pos_emb = pos_emb * self.scale return l2norm(pos_emb) if self.l2norm_embed else pos_emb
zeta-main
zeta/nn/embeddings/abc_pos_emb.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import copy import torch import torch.nn as nn def MultiwayWrapper(args, module, dim=1): if args.multiway: return MultiwayNetwork(module, dim=dim) return module def set_split_position(position): def apply_fn(module): if hasattr(module, "split_position"): module.split_position = position return apply_fn class MultiwayNetwork(nn.Module): def __init__(self, module, dim=1): super().__init__() self.dim = dim self.A = module self.B = copy.deepcopy(module) self.B.reset_parameters() self.split_position = -1 def forward(self, x, **kwargs): if self.split_position == -1: return self.A(x, **kwargs) if self.split_position == 0: return self.B(x, **kwargs) x1, x2 = torch.split( x, [self.split_position, x.size(self.dim) - self.split_position], dim=self.dim, ) # x1, x2 = x[:self.split_position], x[self.split_position:] y1, y2 = self.A(x1, **kwargs), self.B(x2, **kwargs) return torch.cat([y1, y2], dim=self.dim) class MultiwayEmbedding(MultiwayNetwork): """ A specialized version of the MultiwayNetwork to perform multi-way embeddings on an input tensor. Parameters: - modules (List[nn.Module]): A list containing exactly two PyTorch modules. Typically these would be embedding layers. - dim (int): The dimension along which to split and concatenate the input tensor. Default is 1. """ def __init__(self, modules, dim=1): super(MultiwayNetwork, self).__init__() self.dim = dim assert len(modules) == 2 self.A = modules[0] self.B = modules[1] self.split_position = -1
zeta-main
zeta/nn/embeddings/multiway_network.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from abc import ABC, abstractmethod import bitsandbytes as bnb class BaseEmbedding(ABC): @abstractmethod def forward(self, num_tokens: int, dim: int) -> nn.Module: #custom embedding function embedding = ... return embedding #Other embedding class Embedding(BaseEmbedding): def forward(self, num_tokens: int, dim: int) -> nn.Module: embedding = nn.Embedding(num_tokens, dim) return embedding class BnBEmbedding(BaseEmbedding): def forward(self, num_tokens: int, dim: int, padding_idx) -> bnb.nn.modules: embedding = bnb.nn.modules.Embedding(num_tokens, dim, padding_idx) return embedding class TextEmbedding(nn.Embedding): def reset_parameters(self): nn.init.normal_(self.weight, mean=0, std=self.embedding_dim**-0.5) self._fill_padding_idx_with_zero()
zeta-main
zeta/nn/embeddings/embedding.py
# embeddings from zeta.nn.embeddings.rope import RotaryEmbedding from zeta.nn.embeddings.xpos_relative_position import XPOS, rotate_every_two, apply_rotary_pos_emb from zeta.nn.embeddings.multiway_network import MultiwayEmbedding, MultiwayNetwork, MultiwayWrapper from zeta.nn.embeddings.bnb_embedding import BnBEmbedding from zeta.nn.embeddings.base import BaseEmbedding from zeta.nn.embeddings.nominal_embeddings import NominalEmbedding
zeta-main
zeta/nn/embeddings/__init__.py
import torch from torch import nn class VisionEmbedding(nn.Module): """Image to Patch Embedding""" def __init__( self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, contain_mask_token=False, prepend_cls_token=False, ): super().__init__() img_size = (img_size, img_size) patch_size = (patch_size, patch_size) num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0]) self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1]) self.img_size = img_size self.patch_size = patch_size self.num_patches = num_patches self.proj = nn.Conv2d( in_chans, embed_dim, kernel_size=patch_size, stride=patch_size ) if contain_mask_token: self.mask_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) else: self.mask_token = None if prepend_cls_token: self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) else: self.cls_token = None def num_position_embeddings(self): if self.cls_token is None: return self.num_patches else: return self.num_patches + 1 def forward(self, x, masked_position=None, **kwargs): B, C, H, W = x.shape assert ( H == self.img_size[0] and W == self.img_size[1] ), f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})." x = self.proj(x).flatten(2).transpose(1, 2) batch_size, seq_len, _ = x.size() if masked_position is not None: assert self.mask_token is not None mask_token = self.mask_token.expand(batch_size, seq_len, -1) w = masked_position.unsqueeze(-1).type_as(mask_token) x = x * (1 - w) + mask_token * w if self.cls_token is not None: cls_tokens = self.cls_token.expand( batch_size, -1, -1 ) # stole cls_tokens impl from Phil Wang, thanks x = torch.cat((cls_tokens, x), dim=1) return x
zeta-main
zeta/nn/embeddings/vision_emb.py
#from paper:: https://arxiv.org/pdf/2308.10882.pdf import torch from torch import nn class TruncatedRotaryEmbedding(nn.Module): def __init__( self, dim, a, b, rho ): super().__init__() self.dim = dim self.a = a self.b = b self.rho = rho self.base = 10000 self.inv_freq = 1. / (self.base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', self.inv_freq) def forward(self, seq_len, device): t = torch.arange(seq_len, device=device).type_as(self.inv_freq) freqs = torch.einsum('i, j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim=-1) theta = self.base ** (-2 * torch.arange(0, self.dim, 2).float() / self.dim) theta_star = torch.where(theta >= self.b, theta, torch.where(theta < self.a, torch.zeros_like(theta), self.rho * torch.ones_like(theta))) theta_star = torch.cat((theta_star, theta_star), dim=-1) result = freqs * theta_star return result
zeta-main
zeta/nn/embeddings/truncated_rope.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import bitsandbytes as bnb from zeta.nn.embeddings.base import BaseEmbedding class BnBEmbedding(BaseEmbedding): def forward(self, num_tokens: int, dim: int, padding_idx) -> bnb.nn.modules: embedding = bnb.nn.modules.Embedding(num_tokens, dim, padding_idx) return embedding
zeta-main
zeta/nn/embeddings/bnb_embedding.py
import torch from torch import nn, einsum from einops import rearrange def exists(val): return val is not None class SinusoidalEmbeddings(nn.Module): def __init__( self, dim, scale_base = None, use_xpos = False ): super().__init__() inv_freq = 1. / (10000 ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) # xpos related self.use_xpos = use_xpos self.scale_base = scale_base assert not (use_xpos and not exists(scale_base)), 'scale base must be defined if using xpos' scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.register_buffer('scale', scale, persistent = False) def forward(self, x): seq_len, device = x.shape[-2], x.device t = torch.arange(seq_len, device = x.device).type_as(self.inv_freq) freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) if not self.use_xpos: return freqs, torch.ones(1, device = device) power = (t - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) return freqs, scale def rotate_half(x): x = rearrange(x, 'b ... (r d) -> b ... r d', r = 2) x1, x2 = x.unbind(dim = -2) return torch.cat((-x2, x1), dim = -1) def apply_rotary_pos_emb(q, k, freqs, scale = 1): q_len = q.shape[-2] q_freqs = freqs[..., -q_len:, :] inv_scale = scale ** -1 if scale.ndim == 2: scale = scale[-q_len:, :] q = (q * q_freqs.cos() * scale) + (rotate_half(q) * q_freqs.sin() * scale) k = (k * freqs.cos() * inv_scale) + (rotate_half(k) * freqs.sin() * inv_scale) return q, k
zeta-main
zeta/nn/embeddings/sinusoidal.py
#prompts to jquesnelle # https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaDynamicYaRNScaledRotaryEmbedding.py import torch from torch import nn import math #helpers #inveerse dim formula to find dim based on number of rotations def find_correction_dim( num_rotations, dim, base=10000, max_position_embeddings=2048 ): return (dim * math.log( max_position_embeddings/ (num_rotations * 2 * math.pi))) / (2 * math.log(base)) #find dim range bounds based on rotations def find_correction_range( low_rot, high_rot, dim, base=10000, max_position_embeddings=2048 ): low = math.floor(find_correction_dim( low_rot, dim, base, max_position_embeddings )) high = math.ceil(find_correction_dim( high_rot, dim, base, max_position_embeddings )) return max(low, 0), min(high, dim-1) #clamp values just in case def linear_ramp_mask( min, max, dim): if min == max: max += 0.001 linear_func = (torch.arange( dim, dtype=torch.float32 ) - min) / (max - min) ramp_func = torch.clamp(linear_func, 0, 1) return ramp_func def get_mscale(scale=1): if scale <= 1: return 1.0 return 0.1 * math.log(scale) + 1.0 class LLamaDynamicYarnScaledRotaryEmbedding(nn.Module): def __init__( self, dim, max_position_embeddings: int = 2048, base: int = 10000, original_max_position_embeddings: int = 2048, extrapolation_factor: int = 1, attn_factor: int =1, beta_fast: int = 32, beta_slow: int = 1, finetuned=False, device=None ): super().__init__() self.dim = dim self.max_position_embeddings = max_position_embeddings self.base = base self.original_max_position_embeddings = original_max_position_embeddings self.extrapolation_factor = extrapolation_factor self.attn_factor = attn_factor self.beta_fast = beta_fast self.beta_slow = beta_slow if finetuned: self.yarn( self.max_position_embedding / self.original_max_position_embeddings, device ) else: inv_freq = 1.0 / \ (base ** (torch.arange(0, dim, 2).float().to(device) / dim)) self.register_buffer("inv_freq", inv_freq) self.mscale = 1 #build self.max_seq_len_cached = max_position_embeddings t = torch.arange( self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype ) freqs = torch.einsum('i,j->ij', t, self.inv_freq) emb = torch.cat((freqs, freqs), dim=-1) dtype = torch.get_default_dtype() self.register_buffer( "cos_cached", (emb.cos() * self.mscale)[None, None, :, :].to(dtype), persistent=False ) self.register_buffer( "sin_cached", (emb.sin() * self.mscale)[None, None, :, :].to(dtype), persistent=False ) def forward( self, x, seq_len=None ): if seq_len > self.max_seq_len_cached: self.max_seq_len_cached = seq_len self.yarn( seq_len / self.original_max_position_embeddings, x.device ) t = torch.arange( self.max_seq_len_cached, device=x.dtype, dtype=self.inv_freq.dtype ) freqs = torch.einsum( 'i,j->ij', t, self.inv_freq ) emb = torch.cat((freqs, freqs), dim=-1).to(x.device) self.register_buffer( "cos_cached", (emb.cos() * self.mscale)[None, None, :, :].to(x.dtype), persistent=False ) self.register_buffer( "sin_cached", (emb.sin() * self.mscale)[None, None, :, :].to(x.dtype), persistent=False ) return ( self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype), self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype), ) def yarn( self, scale, device ): pos_freqs = self.base ** (torch.arange( 0, self.dim, 2 ).float().to(device) / self.dim) inv_freq_extrapolation = 1.0 / pos_freqs inv_freq_interpolation = 1.0 / (scale * pos_freqs) low, high = find_correction_range( self.beta_fast, self.beta_slow, self.dim, self.base, self.original_max_position_embeddings ) inv_freq_mask = (1 - linear_ramp_mask( low, high, self.dim // 2 ).float().to(device)) * self.extrapolation_factor inv_freq = inv_freq_interpolation * (1 - inv_freq_mask) \ + inv_freq_extrapolation * inv_freq_mask self.register_buffer("inv_freq", inv_freq) self.mscale = float( get_mscale(scale) * self.attn_factor )
zeta-main
zeta/nn/embeddings/yarn.py
#from paper:: https://arxiv.org/pdf/2308.10882.pdf import torch from torch import nn from einops import rearrange def exists(val): return val is not None class RotaryEmbedding(nn.Module): def __init__( self, dim, use_xpos=False, scale_base=512, interpolation_factor=1., base=10000, base_rescale_factor=1., ): super().__init__() #rscal rotary embeddings to long sequence length without finetuning base *= base_rescale_factor ** (dim / (dim - 2)) inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) assert interpolation_factor >= 1 self.interpolation_factor = interpolation_factor if not use_xpos: self.register_buffer('scale', None) return scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.scale_base = scale_base self.register_buffer('scale', scale) def forward(self, seq_len, device): t = torch.arange(seq_len, device=device).type_as(self.inv_freq) t = t / self.interpolation_factor freqs = torch.einsum('i, j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim=-1) if not exists(self.scale): return freqs, 1. power = (torch.arange(seq_len, device=device) - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim=-1) return freqs, scale def rotate_half(x): x = rearrange(x, '... (j d) -> ... j d', j=2) x1, x2 = x.unbind(dim=-1) return torch.cat((-x2, x1), dim=-1) def apply_rotary_pos_emb(t, freqs, scale=1): seq_len = t.shape[-2] freqs = freqs[-seq_len:, :] return (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale)
zeta-main
zeta/nn/embeddings/rope.py
from torch import nn from abc import ABC, abstractmethod class BaseEmbedding(ABC): @abstractmethod def forward(self, num_tokens: int, dim: int) -> nn.Module: #custom embedding function embedding = ... return embedding
zeta-main
zeta/nn/embeddings/base.py
import torch from torch import nn class VisionLanguageEmbedding(nn.Module): def __init__(self, text_embed, vision_embed): super().__init__() self.text_embed = text_embed self.vision_embed = vision_embed def forward(self, textual_tokens, visual_tokens, **kwargs): if textual_tokens is None: return self.vision_embed(visual_tokens) if visual_tokens is None: return self.text_embed(textual_tokens) x1 = self.vision_embed(visual_tokens) x2 = self.text_embed(textual_tokens) return torch.cat([x1, x2], dim=1)
zeta-main
zeta/nn/embeddings/vis_lang_emb.py
import torch from torch import nn import torch.nn.functional as F class PositionalEmbedding(nn.Embedding): def forward( self, x, positions=None, **kwargs, ): if positions is None: # being consistent with Fairseq, which starts from 2. positions = ( torch.arange(2, x.size(1) + 2, device=x.device).long().unsqueeze(0) ) return F.embedding( positions, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse, )
zeta-main
zeta/nn/embeddings/positional.py
from zeta.nn.architecture.attn_layers import AttentionLayers class Decoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on decoder' super().__init__(causal=True, **kwargs)
zeta-main
zeta/nn/architecture/decoder.py
import math from collections import namedtuple from dataclasses import dataclass from functools import partial, wraps from inspect import isfunction from random import random from typing import Callable, List, Optional import torch import torch.nn.functional as F from einops import rearrange, reduce, repeat from torch import Tensor, einsum, nn from zeta.nn.attention.attend import Attend, Intermediates EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) DEFAULT_DIM_HEAD = 64 @dataclass class LayerIntermediates: hiddens: Optional[List[Tensor]] = None attn_intermediates: Optional[List[Intermediates]] = None layer_hiddens: Optional[List[Tensor]] = None attn_z_loss: Optional[Tensor] = None # helpers def exists(val): return val is not None def default(val, d): if exists(val): return val return d() if isfunction(d) else d def cast_tuple(val, depth): return val if isinstance(val, tuple) else (val,) * depth def divisible_by(num, den): return (num % den) == 0 def maybe(fn): @wraps(fn) def inner(x, *args, **kwargs): if not exists(x): return x return fn(x, *args, **kwargs) return inner class always(): def __init__(self, val): self.val = val def __call__(self, *args, **kwargs): return self.val class not_equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x != self.val class equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x == self.val def Sequential(*modules): return nn.Sequential(*filter(exists, modules)) # tensor helpers def max_neg_value(tensor): return -torch.finfo(tensor.dtype).max def l2norm(t, groups = 1): t = rearrange(t, '... (g d) -> ... g d', g = groups) t = F.normalize(t, p = 2, dim = -1) return rearrange(t, '... g d -> ... (g d)') def pad_at_dim(t, pad, dim = -1, value = 0.): dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) zeros = ((0, 0) * dims_from_right) return F.pad(t, (*zeros, *pad), value = value) def or_reduce(masks): head, *body = masks for rest in body: head = head | rest return head # auxiliary loss helpers def calc_z_loss( pre_softmax_attns: List[Tensor], mask = None, weight = 1. ): # the same loss applied to the mixture of experts router logits in https://arxiv.org/abs/2202.08906 # in the paper, in a tiny footnote, they mention using it on attention logits with stabilizing effects # also used in PaLM as one of the measures lse = 0. for attn in pre_softmax_attns: lse = lse + attn.logsumexp(dim = -1) loss = torch.square(lse) loss = reduce(loss, 'b h n -> b n', 'sum') if not exists(mask): return loss.mean() * weight loss = loss[mask].sum() / mask.sum().clamp(min = 1e-5) return loss * weight # init helpers def init_zero_(layer): nn.init.constant_(layer.weight, 0.) if exists(layer.bias): nn.init.constant_(layer.bias, 0.) # keyword argument helpers def pick_and_pop(keys, d): values = list(map(lambda key: d.pop(key), keys)) return dict(zip(keys, values)) def group_dict_by_key(cond, d): return_val = [dict(),dict()] for key in d.keys(): match = bool(cond(key)) ind = int(not match) return_val[ind][key] = d[key] return (*return_val,) def string_begins_with(prefix, str): return str.startswith(prefix) def group_by_key_prefix(prefix, d): return group_dict_by_key(partial(string_begins_with, prefix), d) def groupby_prefix_and_trim(prefix, d): kwargs_with_prefix, kwargs = group_dict_by_key(partial(string_begins_with, prefix), d) kwargs_without_prefix = dict(map(lambda x: (x[0][len(prefix):], x[1]), tuple(kwargs_with_prefix.items()))) return kwargs_without_prefix, kwargs # initializations def deepnorm_init( transformer, beta, module_name_match_list = ['.ff.', '.to_v', '.to_out'] ): for name, module in transformer.named_modules(): if type(module) != nn.Linear: continue needs_beta_gain = any(map(lambda substr: substr in name, module_name_match_list)) gain = beta if needs_beta_gain else 1 nn.init.xavier_normal_(module.weight.data, gain = gain) if exists(module.bias): nn.init.constant_(module.bias.data, 0) # structured dropout, more effective than traditional attention dropouts def dropout_seq(seq, mask, dropout): b, n, *_, device = *seq.shape, seq.device logits = torch.randn(b, n, device = device) if exists(mask): mask_value = max_neg_value(logits) logits = logits.masked_fill(~mask, mask_value) keep_prob = 1. - dropout num_keep = max(1, int(keep_prob * n)) keep_indices = logits.topk(num_keep, dim = 1).indices batch_indices = torch.arange(b, device = device) batch_indices = rearrange(batch_indices, 'b -> b 1') seq = seq[batch_indices, keep_indices] if exists(mask): seq_counts = mask.sum(dim = -1) seq_keep_counts = torch.ceil(seq_counts * keep_prob).int() keep_mask = torch.arange(num_keep, device = device) < rearrange(seq_keep_counts, 'b -> b 1') mask = mask[batch_indices, keep_indices] & keep_mask return seq, mask # activations class ReluSquared(nn.Module): def forward(self, x): return F.relu(x) ** 2 # embedding class TokenEmbedding(nn.Module): def __init__(self, dim, num_tokens, l2norm_embed = False): super().__init__() self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(num_tokens, dim) def forward(self, x): token_emb = self.emb(x) return l2norm(token_emb) if self.l2norm_embed else token_emb # positional embeddings class AbsolutePositionalEmbedding(nn.Module): def __init__(self, dim, max_seq_len, l2norm_embed = False): super().__init__() self.scale = dim ** -0.5 if not l2norm_embed else 1. self.max_seq_len = max_seq_len self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(max_seq_len, dim) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device assert seq_len <= self.max_seq_len, f'you are passing in a sequence length of {seq_len} but your absolute positional embedding has a max sequence length of {self.max_seq_len}' if not exists(pos): pos = torch.arange(seq_len, device = device) pos_emb = self.emb(pos) pos_emb = pos_emb * self.scale return l2norm(pos_emb) if self.l2norm_embed else pos_emb class ScaledSinusoidalEmbedding(nn.Module): def __init__(self, dim, theta = 10000): super().__init__() assert divisible_by(dim, 2) self.scale = nn.Parameter(torch.ones(1) * dim ** -0.5) half_dim = dim // 2 freq_seq = torch.arange(half_dim).float() / half_dim inv_freq = theta ** -freq_seq self.register_buffer('inv_freq', inv_freq, persistent = False) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device if not exists(pos): pos = torch.arange(seq_len, device = device) emb = einsum('i, j -> i j', pos, self.inv_freq) emb = torch.cat((emb.sin(), emb.cos()), dim = -1) return emb * self.scale class RelativePositionBias(nn.Module): def __init__(self, scale, causal = False, num_buckets = 32, max_distance = 128, heads = 8): super().__init__() self.scale = scale self.causal = causal self.num_buckets = num_buckets self.max_distance = max_distance self.relative_attention_bias = nn.Embedding(num_buckets, heads) @staticmethod def _relative_position_bucket(relative_position, causal = True, num_buckets = 32, max_distance = 128): ret = 0 n = -relative_position if not causal: num_buckets //= 2 ret += (n < 0).long() * num_buckets n = torch.abs(n) else: n = torch.max(n, torch.zeros_like(n)) max_exact = num_buckets // 2 is_small = n < max_exact val_if_large = max_exact + ( torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) ).long() val_if_large = torch.min(val_if_large, torch.full_like(val_if_large, num_buckets - 1)) ret += torch.where(is_small, n, val_if_large) return ret @property def device(self): return next(self.parameters()).device def forward(self, i, j): device = self.device q_pos = torch.arange(j - i, j, dtype = torch.long, device = device) k_pos = torch.arange(j, dtype = torch.long, device = device) rel_pos = k_pos[None, :] - q_pos[:, None] rp_bucket = self._relative_position_bucket(rel_pos, causal = self.causal, num_buckets = self.num_buckets, max_distance = self.max_distance) values = self.relative_attention_bias(rp_bucket) bias = rearrange(values, 'i j h -> h i j') return bias * self.scale class DynamicPositionBias(nn.Module): def __init__(self, dim, *, heads, depth, log_distance = False, norm = False): super().__init__() assert depth >= 1, 'depth for dynamic position bias MLP must be greater or equal to 1' self.log_distance = log_distance self.mlp = nn.ModuleList([]) self.mlp.append(Sequential( nn.Linear(1, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) for _ in range(depth - 1): self.mlp.append(Sequential( nn.Linear(dim, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) self.mlp.append(nn.Linear(dim, heads)) @property def device(self): return next(self.parameters()).device def forward(self, i, j): assert i == j n, device = j, self.device # get the (n x n) matrix of distances seq_arange = torch.arange(n, device = device) context_arange = torch.arange(n, device = device) indices = rearrange(seq_arange, 'i -> i 1') - rearrange(context_arange, 'j -> 1 j') indices += (n - 1) # input to continuous positions MLP pos = torch.arange(-n + 1, n, device = device).float() pos = rearrange(pos, '... -> ... 1') if self.log_distance: pos = torch.sign(pos) * torch.log(pos.abs() + 1) # log of distance is sign(rel_pos) * log(abs(rel_pos) + 1) for layer in self.mlp: pos = layer(pos) # get position biases bias = pos[indices] bias = rearrange(bias, 'i j h -> h i j') return bias class AlibiPositionalBias(nn.Module): def __init__(self, heads, total_heads, **kwargs): super().__init__() self.heads = heads self.total_heads = total_heads slopes = Tensor(self._get_slopes(heads)) slopes = rearrange(slopes, 'h -> h 1 1') self.register_buffer('slopes', slopes, persistent = False) self.register_buffer('bias', None, persistent = False) def get_bias(self, i, j, device): i_arange = torch.arange(j - i, j, device = device) j_arange = torch.arange(j, device = device) bias = -torch.abs(rearrange(j_arange, 'j -> 1 1 j') - rearrange(i_arange, 'i -> 1 i 1')) return bias @staticmethod def _get_slopes(heads): def get_slopes_power_of_2(n): start = (2**(-2**-(math.log2(n)-3))) ratio = start return [start*ratio**i for i in range(n)] if math.log2(heads).is_integer(): return get_slopes_power_of_2(heads) closest_power_of_2 = 2 ** math.floor(math.log2(heads)) return get_slopes_power_of_2(closest_power_of_2) + get_slopes_power_of_2(2 * closest_power_of_2)[0::2][:heads-closest_power_of_2] @property def device(self): return next(self.buffers()).device def forward(self, i, j): h, device = self.total_heads, self.device if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: return self.bias[..., :i, :j] bias = self.get_bias(i, j, device) bias = bias * self.slopes num_heads_unalibied = h - bias.shape[0] bias = pad_at_dim(bias, (0, num_heads_unalibied), dim = 0) self.register_buffer('bias', bias, persistent = False) return self.bias class RotaryEmbedding(nn.Module): def __init__( self, dim, use_xpos = False, scale_base = 512, interpolation_factor = 1., base = 10000, base_rescale_factor = 1. ): super().__init__() # proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning # has some connection to NTK literature # https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ base *= base_rescale_factor ** (dim / (dim - 2)) inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) assert interpolation_factor >= 1. self.interpolation_factor = interpolation_factor if not use_xpos: self.register_buffer('scale', None) return scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.scale_base = scale_base self.register_buffer('scale', scale) def forward(self, seq_len, device): t = torch.arange(seq_len, device = device).type_as(self.inv_freq) t = t / self.interpolation_factor freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) if not exists(self.scale): return freqs, 1. power = (torch.arange(seq_len, device = device) - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) return freqs, scale def rotate_half(x): x = rearrange(x, '... (j d) -> ... j d', j = 2) x1, x2 = x.unbind(dim = -2) return torch.cat((-x2, x1), dim = -1) def apply_rotary_pos_emb(t, freqs, scale = 1): seq_len = t.shape[-2] freqs = freqs[-seq_len:, :] return (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale) # norms class Scale(nn.Module): def __init__(self, value, fn): super().__init__() self.value = value self.fn = fn def forward(self, x, **kwargs): out = self.fn(x, **kwargs) def scale_fn(t): return t * self.value if not isinstance(out, tuple): return scale_fn(out) return (scale_fn(out[0]), *out[1:]) class ScaleNorm(nn.Module): def __init__(self, dim, eps = 1e-5): super().__init__() self.eps = eps self.g = nn.Parameter(torch.ones(1) * (dim ** -0.5)) def forward(self, x): norm = torch.norm(x, dim = -1, keepdim = True) return x / norm.clamp(min = self.eps) * self.g class RMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 self.g = nn.Parameter(torch.ones(dim)) def forward(self, x): return F.normalize(x, dim = -1) * self.scale * self.g class SimpleRMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 def forward(self, x): return F.normalize(x, dim = -1) * self.scale # residual and residual gates class Residual(nn.Module): def __init__(self, dim, scale_residual = False, scale_residual_constant = 1.): super().__init__() self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None self.scale_residual_constant = scale_residual_constant def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale if self.scale_residual_constant != 1: residual = residual * self.scale_residual_constant return x + residual class GRUGating(nn.Module): def __init__(self, dim, scale_residual = False, **kwargs): super().__init__() self.gru = nn.GRUCell(dim, dim) self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale gated_output = self.gru( rearrange(x, 'b n d -> (b n) d'), rearrange(residual, 'b n d -> (b n) d') ) return gated_output.reshape_as(x) # token shifting def shift(t, amount, mask = None): if amount == 0: return t else: amount = min(amount, t.shape[1]) if exists(mask): t = t.masked_fill(~mask[..., None], 0.) return pad_at_dim(t, (amount, -amount), dim = - 2, value = 0.) class ShiftTokens(nn.Module): def __init__(self, shifts, fn): super().__init__() self.fn = fn self.shifts = tuple(shifts) def forward(self, x, **kwargs): mask = kwargs.get('mask', None) shifts = self.shifts segments = len(shifts) feats_per_shift = x.shape[-1] // segments splitted = x.split(feats_per_shift, dim = -1) segments_to_shift, rest = splitted[:segments], splitted[segments:] segments_to_shift = list(map(lambda args: shift(*args, mask = mask), zip(segments_to_shift, shifts))) x = torch.cat((*segments_to_shift, *rest), dim = -1) return self.fn(x, **kwargs) # feedforward class GLU(nn.Module): def __init__( self, dim_in, dim_out, activation: Callable, mult_bias = False ): super().__init__() self.act = activation self.proj = nn.Linear(dim_in, dim_out * 2) self.mult_bias = nn.Parameter(torch.ones(dim_out)) if mult_bias else 1. def forward(self, x): x, gate = self.proj(x).chunk(2, dim = -1) return x * self.act(gate) * self.mult_bias class FeedForward(nn.Module): def __init__( self, dim, dim_out = None, mult = 4, glu = False, glu_mult_bias = False, swish = False, relu_squared = False, post_act_ln = False, dropout = 0., no_bias = False, zero_init_output = False ): super().__init__() inner_dim = int(dim * mult) dim_out = default(dim_out, dim) if relu_squared: activation = ReluSquared() elif swish: activation = nn.SiLU() else: activation = nn.GELU() if glu: project_in = GLU(dim, inner_dim, activation, mult_bias = glu_mult_bias) else: project_in = nn.Sequential( nn.Linear(dim, inner_dim, bias = not no_bias), activation ) self.ff = Sequential( project_in, nn.LayerNorm(inner_dim) if post_act_ln else None, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out, bias = not no_bias) ) # init last linear layer to 0 if zero_init_output: init_zero_(self.ff[-1]) def forward(self, x): return self.ff(x) # attention. it is all we need class Attention(nn.Module): def __init__( self, dim, dim_head = DEFAULT_DIM_HEAD, heads = 8, causal = False, flash = False, talking_heads = False, head_scale = False, sparse_topk = None, num_mem_kv = 0, dropout = 0., on_attn = False, gate_values = False, zero_init_output = False, max_attend_past = None, qk_norm = False, qk_norm_groups = 1, qk_norm_scale = 10, qk_norm_dim_scale = False, one_kv_head = False, kv_heads = None, shared_kv = False, value_dim_head = None, tensor_product = False, # https://arxiv.org/abs/2208.06061 cascading_heads = False, add_zero_kv = False, # same as add_zero_attn in pytorch onnxable = False ): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads self.causal = causal self.max_attend_past = max_attend_past assert not (exists(kv_heads) and one_kv_head), 'either attn_one_kv_head is set to True (in which case kv_heads is set to 1), or attn_kv_heads is set, but not both' value_dim_head = default(value_dim_head, dim_head) kv_heads = default(kv_heads, heads) kv_heads = 1 if one_kv_head else kv_heads assert divisible_by(heads, kv_heads) self.kv_heads = kv_heads q_dim = dim_head * heads k_dim = dim_head * kv_heads v_dim = value_dim_head * kv_heads out_dim = value_dim_head * heads self.to_q = nn.Linear(dim, q_dim, bias = False) self.to_k = nn.Linear(dim, k_dim, bias = False) # shared key / values, for further memory savings during inference assert not (shared_kv and value_dim_head != dim_head), 'key and value head dimensions must be equal for shared key / values' self.to_v = nn.Linear(dim, v_dim, bias = False) if not shared_kv else None # relations projection from tp-attention self.to_r = nn.Linear(dim, v_dim, bias = False) if tensor_product else None # add GLU gating for aggregated values, from alphafold2 self.to_v_gate = None if gate_values: self.to_v_gate = nn.Linear(dim, out_dim) nn.init.constant_(self.to_v_gate.weight, 0) nn.init.constant_(self.to_v_gate.bias, 1) # cosine sim attention self.qk_norm = qk_norm self.qk_norm_groups = qk_norm_groups self.qk_norm_scale = qk_norm_scale # whether to use the rmsnorm (equivalent to cosine sim attention when scale is equal to 1) - https://arxiv.org/abs/2302.05442 self.qk_norm_dim_scale = qk_norm_dim_scale self.qk_norm_q_scale = self.qk_norm_k_scale = 1 if qk_norm and qk_norm_dim_scale: self.qk_norm_q_scale = nn.Parameter(torch.ones(dim_head)) self.qk_norm_k_scale = nn.Parameter(torch.ones(dim_head)) assert (not qk_norm) or divisible_by(dim_head, qk_norm_groups), 'dimension per attention head must be divisible by the qk norm groups' assert not (qk_norm and (dim_head // qk_norm_groups) <= 2), 'the group dimension may be too small (2 was too small in my tests, but 4 still works, surprisingly)' # attend class - includes core attention algorithm + talking heads self.attend = Attend( heads = heads, causal = causal, talking_heads = talking_heads, dropout = dropout, sparse_topk = sparse_topk, qk_norm = qk_norm, scale = qk_norm_scale if qk_norm else self.scale, add_zero_kv = add_zero_kv, flash = flash, onnxable = onnxable ) # if cascading_heads: # # cascading heads - wrap the Attend logic # self.attend = CascadingHeads(self.attend) # head scaling self.head_scale = head_scale if head_scale: self.head_scale_params = nn.Parameter(torch.ones(1, heads, 1, 1)) # explicit topk sparse attention self.sparse_topk = sparse_topk # add memory key / values self.num_mem_kv = num_mem_kv if num_mem_kv > 0: self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) # attention on attention self.attn_on_attn = on_attn self.to_out = nn.Sequential(nn.Linear(out_dim, dim * 2, bias = False), nn.GLU()) if on_attn else nn.Linear(out_dim, dim, bias = False) # init output projection 0 if zero_init_output: init_zero_(self.to_out) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, rel_pos = None, rotary_pos_emb = None, prev_attn = None, mem = None ): b, n, _, h, kv_h, head_scale, device, has_context = *x.shape, self.heads, self.kv_heads, self.head_scale, x.device, exists(context) kv_input = default(context, x) q_input = x k_input = kv_input v_input = kv_input r_input = x if exists(mem): k_input = torch.cat((mem, k_input), dim = -2) v_input = torch.cat((mem, v_input), dim = -2) q = self.to_q(q_input) k = self.to_k(k_input) v = self.to_v(v_input) if exists(self.to_v) else k r = self.to_r(r_input) if exists(self.to_r) else None q = rearrange(q, 'b n (h d) -> b h n d', h = h) k, v, r = map(lambda t: maybe(rearrange)(t, 'b n (h d) -> b h n d', h = kv_h), (k, v, r)) if self.qk_norm: qk_l2norm = partial(l2norm, groups = self.qk_norm_groups) q, k = map(qk_l2norm, (q, k)) q = q * self.qk_norm_q_scale k = k * self.qk_norm_k_scale if exists(rotary_pos_emb) and not has_context: freqs, xpos_scale = rotary_pos_emb l = freqs.shape[-1] q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale ** -1.) if exists(xpos_scale) else (1., 1.) (ql, qr), (kl, kr), (vl, vr) = map(lambda t: (t[..., :l], t[..., l:]), (q, k, v)) ql, kl, vl = map(lambda arg: apply_rotary_pos_emb(arg[0], freqs, arg[1]), ((ql, q_xpos_scale), (kl, k_xpos_scale), (vl, k_xpos_scale))) q, k, v = map(lambda t: torch.cat(t, dim = -1), ((ql, qr), (kl, kr), (vl, vr))) input_mask = context_mask if has_context else mask if self.num_mem_kv > 0: mem_k, mem_v = map(lambda t: repeat(t, 'h n d -> b h n d', b = b), (self.mem_k, self.mem_v)) if self.qk_norm: mem_k = l2norm(mem_k) mem_k = mem_k * self.qk_norm_k_scale k = torch.cat((mem_k, k), dim = -2) v = torch.cat((mem_v, v), dim = -2) if exists(input_mask): input_mask = pad_at_dim(input_mask, (self.num_mem_kv, 0), dim = -1, value = True) i, j = map(lambda t: t.shape[-2], (q, k)) # determine masking max_neg_value(q) masks = [] final_attn_mask = None if exists(input_mask): input_mask = rearrange(input_mask, 'b j -> b 1 1 j') masks.append(~input_mask) if exists(attn_mask): assert 2 <= attn_mask.ndim <= 4, 'attention mask must have greater than 2 dimensions but less than or equal to 4' if attn_mask.ndim == 2: attn_mask = rearrange(attn_mask, 'i j -> 1 1 i j') elif attn_mask.ndim == 3: attn_mask = rearrange(attn_mask, 'h i j -> 1 h i j') masks.append(~attn_mask) if exists(self.max_attend_past): range_q = torch.arange(j - i, j, device = device) range_k = torch.arange(j, device = device) dist = rearrange(range_q, 'i -> 1 1 i 1') - rearrange(range_k, 'j -> 1 1 1 j') max_attend_past_mask = dist > self.max_attend_past masks.append(max_attend_past_mask) if len(masks) > 0: final_attn_mask = ~or_reduce(masks) # prepare relative positional bias, if needed attn_bias = None if exists(rel_pos): attn_bias = rel_pos(i, j) # attention is all we need out, intermediates = self.attend( q, k, v, mask = final_attn_mask, attn_bias = attn_bias, prev_attn = prev_attn ) # https://arxiv.org/abs/2208.06061 proposes to add a residual for better gradients if exists(r): out = out * r + out # normformer scaling of heads if head_scale: out = out * self.head_scale_params # merge heads out = rearrange(out, 'b h n d -> b n (h d)') # alphafold2 styled gating of the values if exists(self.to_v_gate): gates = self.to_v_gate(x) out = out * gates.sigmoid() # combine the heads out = self.to_out(out) if exists(mask): mask = rearrange(mask, 'b n -> b n 1') out = out.masked_fill(~mask, 0.) return out, intermediates class AttentionLayers(nn.Module): def __init__( self, dim, depth, heads = 8, causal = False, cross_attend = False, only_cross = False, use_scalenorm = False, use_rmsnorm = False, use_simple_rmsnorm = False, alibi_pos_bias = False, alibi_num_heads = None, rel_pos_bias = False, rel_pos_num_buckets = 32, rel_pos_max_distance = 128, dynamic_pos_bias = False, dynamic_pos_bias_log_distance = False, dynamic_pos_bias_mlp_depth = 2, dynamic_pos_bias_norm = False, rotary_pos_emb = False, rotary_emb_dim = None, rotary_xpos = False, rotary_interpolation_factor = 1., rotary_xpos_scale_base = 512, rotary_base_rescale_factor = 1., custom_layers = None, sandwich_coef = None, par_ratio = None, residual_attn = False, cross_residual_attn = False, macaron = False, pre_norm = True, pre_norm_has_final_norm = True, gate_residual = False, scale_residual = False, scale_residual_constant = 1., deepnorm = False, shift_tokens = 0, sandwich_norm = False, resi_dual = False, resi_dual_scale = 1., zero_init_branch_output = False, layer_dropout = 0., cross_attn_tokens_dropout = 0., **kwargs ): super().__init__() rotary_pos_emb = rotary_pos_emb or rotary_xpos ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) attn_kwargs, kwargs = groupby_prefix_and_trim('attn_', kwargs) dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) self.dim = dim self.depth = depth self.layers = nn.ModuleList([]) self.has_pos_emb = rel_pos_bias or rotary_pos_emb rotary_emb_dim = max(default(rotary_emb_dim, dim_head // 2), 32) assert not (rotary_xpos and not causal), 'rotary xpos is not compatible with bidirectional attention' self.rotary_pos_emb = RotaryEmbedding(rotary_emb_dim, use_xpos = rotary_xpos, scale_base = rotary_xpos_scale_base, interpolation_factor = rotary_interpolation_factor, base_rescale_factor = rotary_base_rescale_factor) if rotary_pos_emb else None assert not (alibi_pos_bias and rel_pos_bias), 'you can only choose Alibi positional bias or T5 relative positional bias, not both' assert rel_pos_num_buckets <= rel_pos_max_distance, 'number of relative position buckets must be less than the relative position max distance' # relative positional bias flash_attn = attn_kwargs.get('flash', False) assert (int(rel_pos_bias) + int(dynamic_pos_bias) + int(alibi_pos_bias)) <= 1, 'you can only choose up to one of t5, alibi, or dynamic positional bias' self.rel_pos = None if rel_pos_bias: assert not flash_attn, 'flash attention not compatible with t5 relative positional bias' self.rel_pos = RelativePositionBias(scale = dim_head ** 0.5, causal = causal, heads = heads, num_buckets = rel_pos_num_buckets, max_distance = rel_pos_max_distance) elif dynamic_pos_bias: assert not flash_attn, 'flash attention not compatible with dynamic positional bias' self.rel_pos = DynamicPositionBias(dim = dim // 4, heads = heads, log_distance = dynamic_pos_bias_log_distance, depth = dynamic_pos_bias_mlp_depth, norm = dynamic_pos_bias_norm) elif alibi_pos_bias: alibi_num_heads = default(alibi_num_heads, heads) assert alibi_num_heads <= heads, 'number of ALiBi heads must be less than the total number of heads' self.rel_pos = AlibiPositionalBias(heads = alibi_num_heads, total_heads = heads) # determine deepnorm and residual scale if deepnorm: assert scale_residual_constant == 1, 'scale residual constant is being overridden by deep norm settings' pre_norm = sandwich_norm = resi_dual = False scale_residual = True scale_residual_constant = (2 * depth) ** 0.25 assert (int(sandwich_norm) + int(resi_dual)) <= 1, 'either sandwich norm or resiDual is selected, but not both' assert not (not pre_norm and sandwich_norm), 'sandwich norm cannot be used when not using prenorm' if resi_dual: pre_norm = False self.pre_norm = pre_norm self.sandwich_norm = sandwich_norm self.resi_dual = resi_dual assert 0 < resi_dual_scale <= 1., 'resiDual prenorm residual must be scaled by a factor greater than 0 and less than or equal to 1.' self.resi_dual_scale = resi_dual_scale self.residual_attn = residual_attn self.cross_residual_attn = cross_residual_attn assert not (flash_attn and (residual_attn or cross_residual_attn)), 'flash attention is not compatible with residual attention' self.cross_attend = cross_attend assert (int(use_scalenorm) + int(use_rmsnorm) + int(use_simple_rmsnorm)) <= 1, 'you can only use either scalenorm, rmsnorm, or simple rmsnorm' if use_scalenorm: norm_class = ScaleNorm elif use_rmsnorm: norm_class = RMSNorm elif use_simple_rmsnorm: norm_class = SimpleRMSNorm else: norm_class = nn.LayerNorm norm_fn = partial(norm_class, dim) if cross_attend and not only_cross: default_block = ('a', 'c', 'f') elif cross_attend and only_cross: default_block = ('c', 'f') else: default_block = ('a', 'f') if macaron: default_block = ('f',) + default_block # zero init if zero_init_branch_output: attn_kwargs = {**attn_kwargs, 'zero_init_output': True} ff_kwargs = {**ff_kwargs, 'zero_init_output': True} # calculate layer block order if exists(custom_layers): layer_types = custom_layers elif exists(par_ratio): par_depth = depth * len(default_block) assert 1 < par_ratio <= par_depth, 'par ratio out of range' default_block = tuple(filter(not_equals('f'), default_block)) par_attn = par_depth // par_ratio depth_cut = par_depth * 2 // 3 # 2 / 3 attention layer cutoff suggested by PAR paper par_width = (depth_cut + depth_cut // par_attn) // par_attn assert len(default_block) <= par_width, 'default block is too large for par_ratio' par_block = default_block + ('f',) * (par_width - len(default_block)) par_head = par_block * par_attn layer_types = par_head + ('f',) * (par_depth - len(par_head)) elif exists(sandwich_coef): assert sandwich_coef > 0 and sandwich_coef <= depth, 'sandwich coefficient should be less than the depth' layer_types = ('a',) * sandwich_coef + default_block * (depth - sandwich_coef) + ('f',) * sandwich_coef else: layer_types = default_block * depth self.layer_types = layer_types self.num_attn_layers = len(list(filter(equals('a'), layer_types))) # stochastic depth self.layer_dropouts = cast_tuple(layer_dropout, len(layer_types)) # structured dropout for cross attending self.cross_attn_tokens_dropout = cross_attn_tokens_dropout # calculate token shifting shift_tokens = cast_tuple(shift_tokens, len(layer_types)) # whether it has post norm self.final_norm = norm_fn() if pre_norm or resi_dual else nn.Identity() # iterate and construct layers for ind, (layer_type, layer_shift_tokens) in enumerate(zip(self.layer_types, shift_tokens)): ind == (len(self.layer_types) - 1) if layer_type == 'a': layer = Attention(dim, heads = heads, causal = causal, **attn_kwargs) elif layer_type == 'c': layer = Attention(dim, heads = heads, **attn_kwargs) elif layer_type == 'f': layer = FeedForward(dim, **ff_kwargs) layer = layer if not macaron else Scale(0.5, layer) else: raise Exception(f'invalid layer type {layer_type}') if layer_shift_tokens > 0: shift_range_upper = layer_shift_tokens + 1 shift_range_lower = -layer_shift_tokens if not causal else 0 layer = ShiftTokens(range(shift_range_lower, shift_range_upper), layer) residual_fn = GRUGating if gate_residual else Residual residual = residual_fn(dim, scale_residual = scale_residual, scale_residual_constant = scale_residual_constant) pre_branch_norm = norm_fn() if pre_norm else None post_branch_norm = norm_fn() if sandwich_norm else None post_main_norm = norm_fn() if not pre_norm else None norms = nn.ModuleList([ pre_branch_norm, post_branch_norm, post_main_norm ]) self.layers.append(nn.ModuleList([ norms, layer, residual ])) if deepnorm: init_gain = (8 * depth) ** -0.25 deepnorm_init(self, init_gain) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, self_attn_context_mask = None, mems = None, return_hiddens = False ): assert not (self.cross_attend ^ exists(context)), 'context must be passed in if cross_attend is set to True' hiddens = [] layer_hiddens = [] intermediates = [] prev_attn = None prev_cross_attn = None mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers rotary_pos_emb = None if exists(self.rotary_pos_emb): max_rotary_emb_length = max(list(map(lambda m: (m.shape[1] if exists(m) else 0) + x.shape[1], mems))) rotary_pos_emb = self.rotary_pos_emb(max_rotary_emb_length, x.device) outer_residual = x * self.resi_dual_scale for ind, (layer_type, (norm, block, residual_fn), layer_dropout) in enumerate(zip(self.layer_types, self.layers, self.layer_dropouts)): ind == (len(self.layers) - 1) if self.training and layer_dropout > 0. and random() < layer_dropout: continue if layer_type == 'a': if return_hiddens: hiddens.append(x) layer_mem = mems.pop(0) if mems else None if layer_type == 'c': if self.training and self.cross_attn_tokens_dropout > 0.: context, context_mask = dropout_seq(context, context_mask, self.cross_attn_tokens_dropout) inner_residual = x if return_hiddens: layer_hiddens.append(x) pre_norm, post_branch_norm, post_main_norm = norm if exists(pre_norm): x = pre_norm(x) if layer_type == 'a': out, inter = block(x, mask = mask, context_mask = self_attn_context_mask, attn_mask = attn_mask, rel_pos = self.rel_pos, rotary_pos_emb = rotary_pos_emb, prev_attn = prev_attn, mem = layer_mem) elif layer_type == 'c': out, inter = block(x, context = context, mask = mask, context_mask = context_mask, prev_attn = prev_cross_attn) elif layer_type == 'f': out = block(x) if self.resi_dual: outer_residual = outer_residual + out * self.resi_dual_scale if exists(post_branch_norm): out = post_branch_norm(out) x = residual_fn(out, inner_residual) if layer_type in ('a', 'c') and return_hiddens: intermediates.append(inter) if layer_type == 'a' and self.residual_attn: prev_attn = inter.pre_softmax_attn elif layer_type == 'c' and self.cross_residual_attn: prev_cross_attn = inter.pre_softmax_attn if exists(post_main_norm): x = post_main_norm(x) if return_hiddens: layer_hiddens.append(x) if self.resi_dual: x = x + self.final_norm(outer_residual) else: x = self.final_norm(x) if return_hiddens: intermediates = LayerIntermediates( hiddens = hiddens, attn_intermediates = intermediates, layer_hiddens = layer_hiddens ) return x, intermediates return x
zeta-main
zeta/nn/architecture/attn_layers.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from zeta.nn.architecture.decoder import Decoder from zeta.nn.architecture.encoder import Encoder class EncoderDecoder(nn.Module): def __init__( self, args, encoder_embed_tokens=None, encoder_embed_positions=None, decoder_embed_tokens=None, decoder_embed_positions=None, output_projection=None, **kwargs ): super().__init__() self.args = args if args.share_all_embeddings: args.share_decoder_input_output_embed = True self.encoder = Encoder( args, encoder_embed_tokens, encoder_embed_positions, is_encoder_decoder=True, **kwargs ) if args.share_all_embeddings and decoder_embed_tokens is None: decoder_embed_tokens = self.encoder.embed_tokens self.decoder = Decoder( args, decoder_embed_tokens, decoder_embed_positions, output_projection, is_encoder_decoder=True, **kwargs ) def forward( self, src_tokens, prev_output_tokens, return_all_hiddens=False, features_only=False, **kwargs ): encoder_out = self.encoder(src_tokens, return_all_hiddens=return_all_hiddens) decoder_out = self.decoder( prev_output_tokens, encoder_out=encoder_out, features_only=features_only, return_all_hiddens=return_all_hiddens, ) return decoder_out
zeta-main
zeta/nn/architecture/encoder_decoder.py
import torch from torch import nn import torch.nn.functional as F from einops import rearrange from zeta.nn.attention.attend import Attend as Attention # functions and decorators def exists(val): return val is not None def default(val, d): return val if exists(val) else d def identity(t, *args, **kwargs): return t def l2norm(t): return F.normalize(t, dim = -1) # normalization # they use layernorm without bias, something that pytorch does not offer class LayerNorm(nn.Module): def __init__(self, dim): super().__init__() self.gamma = nn.Parameter(torch.ones(dim)) self.register_buffer("beta", torch.zeros(dim)) def forward(self, x): return F.layer_norm(x, x.shape[-1:], self.gamma, self.beta) # residual class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn def forward(self, x, **kwargs): y = self.fn(x, **kwargs) if not any([t.requires_grad for t in (x, y)]): return x.add_(y) return y + x # rotary positional embedding w/ xpos # https://arxiv.org/abs/2104.09864 # https://arxiv.org/abs/2212.10554v1 class RotaryEmbedding(nn.Module): def __init__( self, dim, scale_base = 512, use_xpos = True ): super().__init__() inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer("inv_freq", inv_freq) self.use_xpos = use_xpos self.scale_base = scale_base scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.register_buffer('scale', scale) def forward( self, seq_len, device ): t = torch.arange(seq_len, device = device).type_as(self.inv_freq) freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) if not self.use_xpos: return freqs, torch.ones(1, device = device) power = (t - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) return freqs, scale def rotate_half(x): x1, x2 = x.chunk(2, dim=-1) return torch.cat((-x2, x1), dim=-1) def apply_rotary_pos_emb(pos, t, scale = 1.): return (t * pos.cos() * scale) + (rotate_half(t) * pos.sin() * scale) # classic Noam Shazeer paper, except here they use SwiGLU instead of the more popular GEGLU for gating the feedforward # https://arxiv.org/abs/2002.05202 class SwiGLU(nn.Module): def forward(self, x): x, gate = x.chunk(2, dim=-1) return F.silu(gate) * x # parallel attention and feedforward with residual # discovered by Wang et al + EleutherAI from GPT-J fame class ParallelTransformerBlock(nn.Module): def __init__( self, dim, dim_head = 64, causal = True, heads = 8, qk_rmsnorm = False, qk_scale = 8, ff_mult = 4, attn_dropout = 0., ff_dropout = 0., use_xpos = True, xpos_scale_base = 512, flash_attn = False, ): super().__init__() self.norm = LayerNorm(dim) attn_inner_dim = dim_head * heads ff_inner_dim = dim * ff_mult self.fused_dims = (attn_inner_dim, dim_head, dim_head, (ff_inner_dim * 2)) self.qk_rmsnorm = qk_rmsnorm if qk_rmsnorm: self.q_scale = nn.Parameter(torch.ones(dim_head)) self.k_scale = nn.Parameter(torch.ones(dim_head)) self.attend = Attention( causal = causal, dropout = attn_dropout, use_flash_attn = flash_attn ) self.heads = heads self.scale = (dim_head ** -0.5) if not qk_rmsnorm else qk_scale self.causal = causal self.rotary_emb = RotaryEmbedding(dim_head, scale_base = xpos_scale_base, use_xpos = use_xpos and causal) self.fused_attn_ff_proj = nn.Linear(dim, sum(self.fused_dims), bias=False) self.flash_attn = flash_attn self.attn_out = nn.Linear(attn_inner_dim, dim, bias=False) self.attn_dropout = nn.Dropout(attn_dropout) self.flash_attn_dropout = attn_dropout # parallel feedforward tail self.ff_out = nn.Sequential( SwiGLU(), nn.Dropout(ff_dropout), nn.Linear(ff_inner_dim, dim, bias=False) ) # for caching causal mask and rotary embeddings self.register_buffer("pos_emb", None, persistent=False) self.register_buffer("pos_emb_scale", None, persistent=False) def get_rotary_embedding(self, n, device): if exists(self.pos_emb) and self.pos_emb.shape[-2] >= n: return self.pos_emb[:n], self.pos_emb_scale[:n] pos_emb, scale = self.rotary_emb(n, device=device) self.register_buffer("pos_emb", pos_emb, persistent=False) self.register_buffer("pos_emb_scale", scale, persistent=False) return pos_emb, scale def forward( self, x, mask = None, finetune_modules = None ): """ einstein notation b - batch h - heads n, i, j - sequence length (base sequence length, source, target) d - feature dimension """ n, device, h = x.shape[1], x.device, self.heads # pre layernorm x = self.norm(x) # attention queries, keys, values, and feedforward inner q, k, v, ff = self.fused_attn_ff_proj(x).split(self.fused_dims, dim=-1) # finetune loras lora_q = lora_k = lora_v = lora_o = None if exists(finetune_modules): lora_q, lora_k, lora_v, lora_o = finetune_modules q = q + lora_q(x) k = k + lora_k(x) v = v + lora_v(x) # split heads # they use multi-query single-key-value attention, yet another Noam Shazeer paper # they found no performance loss past a certain scale, and more efficient decoding obviously # https://arxiv.org/abs/1911.02150 q = rearrange(q, "b n (h d) -> b h n d", h=h) # qk rmsnorm if self.qk_rmsnorm: q, k = map(l2norm, (q, k)) q = q * self.q_scale k = k * self.k_scale # rotary embeddings with xpos decay for better length extrapolation positions, scale = self.get_rotary_embedding(n, device) q = apply_rotary_pos_emb(positions, q, scale) k = apply_rotary_pos_emb(positions, k, scale ** -1) # attention function, either regular or flash out = self.attend(q, k, v, mask = mask) # merge heads out = rearrange(out, "b h n d -> b n (h d)") attn_out = self.attn_out(out) ff_out = self.ff_out(ff) if exists(lora_o): attn_out = attn_out + lora_o(out) return attn_out + ff_out # transformer
zeta-main
zeta/nn/architecture/parallel_transformer.py
zeta-main
zeta/nn/architecture/__init__.py
from zeta.nn.architecture.attn_layers import AttentionLayers class Encoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on encoder' super().__init__(causal=False, **kwargs)
zeta-main
zeta/nn/architecture/encoder.py
from einops import rearrange from torch import nn import torch import torch.nn.functional as F from zeta.nn.attention.local_attention_mha import LocalMHA from zeta.nn.biases.dynamic_position_bias import DynamicPositionBias from zeta.nn.modules import feedforward_network from zeta.utils.main import eval_decorator, exists, top_k class LocalTransformer(nn.Module): def __init__( self, *, num_tokens, max_seq_len, dim, depth, causal = True, local_attn_window_size = 512, dim_head = 64, heads = 8, ff_mult = 4, attn_dropout = 0., ff_dropout = 0., ignore_index = -1, use_xpos = False, xpos_scale_base = None, use_dynamic_pos_bias = False, **kwargs ): super().__init__() self.token_emb = nn.Embedding(num_tokens, dim) self.pos_emb = nn.Embedding(max_seq_len, dim) self.max_seq_len = max_seq_len self.layers = nn.ModuleList([]) self.local_attn_window_size = local_attn_window_size self.dynamic_pos_bias = None if use_dynamic_pos_bias: self.dynamic_pos_bias = DynamicPositionBias(dim = dim // 2, heads = heads) for _ in range(depth): self.layers.append(nn.ModuleList([ LocalMHA(dim = dim, dim_head = dim_head, heads = heads, dropout = attn_dropout, causal = causal, window_size = local_attn_window_size, use_xpos = use_xpos, xpos_scale_base = xpos_scale_base, use_rotary_pos_emb = not use_dynamic_pos_bias, prenorm = True, **kwargs), feedforward_network(dim = dim, mult = ff_mult, dropout = ff_dropout) ])) self.ignore_index = ignore_index self.to_logits = nn.Sequential( nn.LayerNorm(dim), nn.Linear(dim, num_tokens, bias = False) ) @torch.no_grad() @eval_decorator def generate( self, prime, seq_len, temperature = 1., filter_thres = 0.9, **kwargs ): n, device = prime.shape[1], prime.device out = prime for _ in range(seq_len): logits = self.forward(out[:, -self.max_seq_len:], **kwargs) filtered_logits = top_k(logits[:, -1], thres = filter_thres) probs = F.softmax(filtered_logits / temperature, dim = -1) sampled = torch.multinomial(probs, 1) out = torch.cat((out, sampled), dim = -1) return out[:, n:] def forward(self, x, mask = None, return_loss = False): if return_loss: x, labels = x[:, :-1], x[:, 1:] n, device = x.shape[1], x.device x = self.token_emb(x) assert n <= self.max_seq_len x = x + self.pos_emb(torch.arange(n, device = device)) # dynamic pos bias attn_bias = None if exists(self.dynamic_pos_bias): w = self.local_attn_window_size attn_bias = self.dynamic_pos_bias(w, w * 2) # go through layers for attn, ff in self.layers: x = attn(x, mask = mask, attn_bias = attn_bias) + x x = ff(x) + x logits = self.to_logits(x) if not return_loss: return logits logits = rearrange(logits, 'b n c -> b c n') loss = F.cross_entropy(logits, labels, ignore_index = self.ignore_index) return loss
zeta-main
zeta/nn/architecture/local_transformer.py
from inspect import isfunction import math from abc import ABC, abstractmethod from collections import namedtuple from dataclasses import dataclass from functools import partial, wraps from random import random from typing import Callable, List, Optional import torch import torch.nn.functional as F from einops import rearrange, reduce, repeat from torch import Tensor, einsum, nn from zeta.nn.attention.attend import Attend, Intermediates EfficientAttentionConfig = namedtuple('EfficientAttentionConfig', ['enable_flash', 'enable_math', 'enable_mem_efficient']) DEFAULT_DIM_HEAD = 64 @dataclass class LayerIntermediates: hiddens: Optional[List[Tensor]] = None attn_intermediates: Optional[List[Intermediates]] = None layer_hiddens: Optional[List[Tensor]] = None attn_z_loss: Optional[Tensor] = None # helpers def exists(val): return val is not None def default(val, d): if exists(val): return val return d() if isfunction(d) else d def cast_tuple(val, depth): return val if isinstance(val, tuple) else (val,) * depth def divisible_by(num, den): return (num % den) == 0 def maybe(fn): @wraps(fn) def inner(x, *args, **kwargs): if not exists(x): return x return fn(x, *args, **kwargs) return inner class always(): def __init__(self, val): self.val = val def __call__(self, *args, **kwargs): return self.val class not_equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x != self.val class equals(): def __init__(self, val): self.val = val def __call__(self, x, *args, **kwargs): return x == self.val def Sequential(*modules): return nn.Sequential(*filter(exists, modules)) # tensor helpers def max_neg_value(tensor): return -torch.finfo(tensor.dtype).max def l2norm(t, groups = 1): t = rearrange(t, '... (g d) -> ... g d', g = groups) t = F.normalize(t, p = 2, dim = -1) return rearrange(t, '... g d -> ... (g d)') def pad_at_dim(t, pad, dim = -1, value = 0.): dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) zeros = ((0, 0) * dims_from_right) return F.pad(t, (*zeros, *pad), value = value) def or_reduce(masks): head, *body = masks for rest in body: head = head | rest return head # auxiliary loss helpers def calc_z_loss( pre_softmax_attns: List[Tensor], mask = None, weight = 1. ): # the same loss applied to the mixture of experts router logits in https://arxiv.org/abs/2202.08906 # in the paper, in a tiny footnote, they mention using it on attention logits with stabilizing effects # also used in PaLM as one of the measures lse = 0. for attn in pre_softmax_attns: lse = lse + attn.logsumexp(dim = -1) loss = torch.square(lse) loss = reduce(loss, 'b h n -> b n', 'sum') if not exists(mask): return loss.mean() * weight loss = loss[mask].sum() / mask.sum().clamp(min = 1e-5) return loss * weight # init helpers def init_zero_(layer): nn.init.constant_(layer.weight, 0.) if exists(layer.bias): nn.init.constant_(layer.bias, 0.) # keyword argument helpers def pick_and_pop(keys, d): values = list(map(lambda key: d.pop(key), keys)) return dict(zip(keys, values)) def group_dict_by_key(cond, d): return_val = [dict(),dict()] for key in d.keys(): match = bool(cond(key)) ind = int(not match) return_val[ind][key] = d[key] return (*return_val,) def string_begins_with(prefix, str): return str.startswith(prefix) def group_by_key_prefix(prefix, d): return group_dict_by_key(partial(string_begins_with, prefix), d) def groupby_prefix_and_trim(prefix, d): kwargs_with_prefix, kwargs = group_dict_by_key(partial(string_begins_with, prefix), d) kwargs_without_prefix = dict(map(lambda x: (x[0][len(prefix):], x[1]), tuple(kwargs_with_prefix.items()))) return kwargs_without_prefix, kwargs # initializations def deepnorm_init( transformer, beta, module_name_match_list = ['.ff.', '.to_v', '.to_out'] ): for name, module in transformer.named_modules(): if type(module) != nn.Linear: continue needs_beta_gain = any(map(lambda substr: substr in name, module_name_match_list)) gain = beta if needs_beta_gain else 1 nn.init.xavier_normal_(module.weight.data, gain = gain) if exists(module.bias): nn.init.constant_(module.bias.data, 0) # structured dropout, more effective than traditional attention dropouts def dropout_seq(seq, mask, dropout): b, n, *_, device = *seq.shape, seq.device logits = torch.randn(b, n, device = device) if exists(mask): mask_value = max_neg_value(logits) logits = logits.masked_fill(~mask, mask_value) keep_prob = 1. - dropout num_keep = max(1, int(keep_prob * n)) keep_indices = logits.topk(num_keep, dim = 1).indices batch_indices = torch.arange(b, device = device) batch_indices = rearrange(batch_indices, 'b -> b 1') seq = seq[batch_indices, keep_indices] if exists(mask): seq_counts = mask.sum(dim = -1) seq_keep_counts = torch.ceil(seq_counts * keep_prob).int() keep_mask = torch.arange(num_keep, device = device) < rearrange(seq_keep_counts, 'b -> b 1') mask = mask[batch_indices, keep_indices] & keep_mask return seq, mask # activations class ReluSquared(nn.Module): def forward(self, x): return F.relu(x) ** 2 # embedding class TokenEmbedding(nn.Module): def __init__(self, dim, num_tokens, l2norm_embed = False): super().__init__() self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(num_tokens, dim) def forward(self, x): token_emb = self.emb(x) return l2norm(token_emb) if self.l2norm_embed else token_emb # positional embeddings class AbsolutePositionalEmbedding(nn.Module): def __init__(self, dim, max_seq_len, l2norm_embed = False): super().__init__() self.scale = dim ** -0.5 if not l2norm_embed else 1. self.max_seq_len = max_seq_len self.l2norm_embed = l2norm_embed self.emb = nn.Embedding(max_seq_len, dim) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device assert seq_len <= self.max_seq_len, f'you are passing in a sequence length of {seq_len} but your absolute positional embedding has a max sequence length of {self.max_seq_len}' if not exists(pos): pos = torch.arange(seq_len, device = device) pos_emb = self.emb(pos) pos_emb = pos_emb * self.scale return l2norm(pos_emb) if self.l2norm_embed else pos_emb class ScaledSinusoidalEmbedding(nn.Module): def __init__(self, dim, theta = 10000): super().__init__() assert divisible_by(dim, 2) self.scale = nn.Parameter(torch.ones(1) * dim ** -0.5) half_dim = dim // 2 freq_seq = torch.arange(half_dim).float() / half_dim inv_freq = theta ** -freq_seq self.register_buffer('inv_freq', inv_freq, persistent = False) def forward(self, x, pos = None): seq_len, device = x.shape[1], x.device if not exists(pos): pos = torch.arange(seq_len, device = device) emb = einsum('i, j -> i j', pos, self.inv_freq) emb = torch.cat((emb.sin(), emb.cos()), dim = -1) return emb * self.scale class RelativePositionBias(nn.Module): def __init__(self, scale, causal = False, num_buckets = 32, max_distance = 128, heads = 8): super().__init__() self.scale = scale self.causal = causal self.num_buckets = num_buckets self.max_distance = max_distance self.relative_attention_bias = nn.Embedding(num_buckets, heads) @staticmethod def _relative_position_bucket(relative_position, causal = True, num_buckets = 32, max_distance = 128): ret = 0 n = -relative_position if not causal: num_buckets //= 2 ret += (n < 0).long() * num_buckets n = torch.abs(n) else: n = torch.max(n, torch.zeros_like(n)) max_exact = num_buckets // 2 is_small = n < max_exact val_if_large = max_exact + ( torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) ).long() val_if_large = torch.min(val_if_large, torch.full_like(val_if_large, num_buckets - 1)) ret += torch.where(is_small, n, val_if_large) return ret @property def device(self): return next(self.parameters()).device def forward(self, i, j): device = self.device q_pos = torch.arange(j - i, j, dtype = torch.long, device = device) k_pos = torch.arange(j, dtype = torch.long, device = device) rel_pos = k_pos[None, :] - q_pos[:, None] rp_bucket = self._relative_position_bucket(rel_pos, causal = self.causal, num_buckets = self.num_buckets, max_distance = self.max_distance) values = self.relative_attention_bias(rp_bucket) bias = rearrange(values, 'i j h -> h i j') return bias * self.scale class DynamicPositionBias(nn.Module): def __init__(self, dim, *, heads, depth, log_distance = False, norm = False): super().__init__() assert depth >= 1, 'depth for dynamic position bias MLP must be greater or equal to 1' self.log_distance = log_distance self.mlp = nn.ModuleList([]) self.mlp.append(Sequential( nn.Linear(1, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) for _ in range(depth - 1): self.mlp.append(Sequential( nn.Linear(dim, dim), nn.LayerNorm(dim) if norm else None, nn.SiLU() )) self.mlp.append(nn.Linear(dim, heads)) @property def device(self): return next(self.parameters()).device def forward(self, i, j): assert i == j n, device = j, self.device # get the (n x n) matrix of distances seq_arange = torch.arange(n, device = device) context_arange = torch.arange(n, device = device) indices = rearrange(seq_arange, 'i -> i 1') - rearrange(context_arange, 'j -> 1 j') indices += (n - 1) # input to continuous positions MLP pos = torch.arange(-n + 1, n, device = device).float() pos = rearrange(pos, '... -> ... 1') if self.log_distance: pos = torch.sign(pos) * torch.log(pos.abs() + 1) # log of distance is sign(rel_pos) * log(abs(rel_pos) + 1) for layer in self.mlp: pos = layer(pos) # get position biases bias = pos[indices] bias = rearrange(bias, 'i j h -> h i j') return bias class AlibiPositionalBias(nn.Module): def __init__(self, heads, total_heads, **kwargs): super().__init__() self.heads = heads self.total_heads = total_heads slopes = Tensor(self._get_slopes(heads)) slopes = rearrange(slopes, 'h -> h 1 1') self.register_buffer('slopes', slopes, persistent = False) self.register_buffer('bias', None, persistent = False) def get_bias(self, i, j, device): i_arange = torch.arange(j - i, j, device = device) j_arange = torch.arange(j, device = device) bias = -torch.abs(rearrange(j_arange, 'j -> 1 1 j') - rearrange(i_arange, 'i -> 1 i 1')) return bias @staticmethod def _get_slopes(heads): def get_slopes_power_of_2(n): start = (2**(-2**-(math.log2(n)-3))) ratio = start return [start*ratio**i for i in range(n)] if math.log2(heads).is_integer(): return get_slopes_power_of_2(heads) closest_power_of_2 = 2 ** math.floor(math.log2(heads)) return get_slopes_power_of_2(closest_power_of_2) + get_slopes_power_of_2(2 * closest_power_of_2)[0::2][:heads-closest_power_of_2] @property def device(self): return next(self.buffers()).device def forward(self, i, j): h, device = self.total_heads, self.device if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: return self.bias[..., :i, :j] bias = self.get_bias(i, j, device) bias = bias * self.slopes num_heads_unalibied = h - bias.shape[0] bias = pad_at_dim(bias, (0, num_heads_unalibied), dim = 0) self.register_buffer('bias', bias, persistent = False) return self.bias class RotaryEmbedding(nn.Module): def __init__( self, dim, use_xpos = False, scale_base = 512, interpolation_factor = 1., base = 10000, base_rescale_factor = 1. ): super().__init__() # proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning # has some connection to NTK literature # https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ base *= base_rescale_factor ** (dim / (dim - 2)) inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim)) self.register_buffer('inv_freq', inv_freq) assert interpolation_factor >= 1. self.interpolation_factor = interpolation_factor if not use_xpos: self.register_buffer('scale', None) return scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) self.scale_base = scale_base self.register_buffer('scale', scale) def forward(self, seq_len, device): t = torch.arange(seq_len, device = device).type_as(self.inv_freq) t = t / self.interpolation_factor freqs = torch.einsum('i , j -> i j', t, self.inv_freq) freqs = torch.cat((freqs, freqs), dim = -1) if not exists(self.scale): return freqs, 1. power = (torch.arange(seq_len, device = device) - (seq_len // 2)) / self.scale_base scale = self.scale ** rearrange(power, 'n -> n 1') scale = torch.cat((scale, scale), dim = -1) return freqs, scale def rotate_half(x): x = rearrange(x, '... (j d) -> ... j d', j = 2) x1, x2 = x.unbind(dim = -2) return torch.cat((-x2, x1), dim = -1) def apply_rotary_pos_emb(t, freqs, scale = 1): seq_len = t.shape[-2] freqs = freqs[-seq_len:, :] return (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale) # norms class Scale(nn.Module): def __init__(self, value, fn): super().__init__() self.value = value self.fn = fn def forward(self, x, **kwargs): out = self.fn(x, **kwargs) scale_fn = lambda t: t * self.value if not isinstance(out, tuple): return scale_fn(out) return (scale_fn(out[0]), *out[1:]) class ScaleNorm(nn.Module): def __init__(self, dim, eps = 1e-5): super().__init__() self.eps = eps self.g = nn.Parameter(torch.ones(1) * (dim ** -0.5)) def forward(self, x): norm = torch.norm(x, dim = -1, keepdim = True) return x / norm.clamp(min = self.eps) * self.g class RMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 self.g = nn.Parameter(torch.ones(dim)) def forward(self, x): return F.normalize(x, dim = -1) * self.scale * self.g class SimpleRMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 def forward(self, x): return F.normalize(x, dim = -1) * self.scale # residual and residual gates class Residual(nn.Module): def __init__(self, dim, scale_residual = False, scale_residual_constant = 1.): super().__init__() self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None self.scale_residual_constant = scale_residual_constant def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale if self.scale_residual_constant != 1: residual = residual * self.scale_residual_constant return x + residual class GRUGating(nn.Module): def __init__(self, dim, scale_residual = False, **kwargs): super().__init__() self.gru = nn.GRUCell(dim, dim) self.residual_scale = nn.Parameter(torch.ones(dim)) if scale_residual else None def forward(self, x, residual): if exists(self.residual_scale): residual = residual * self.residual_scale gated_output = self.gru( rearrange(x, 'b n d -> (b n) d'), rearrange(residual, 'b n d -> (b n) d') ) return gated_output.reshape_as(x) # token shifting def shift(t, amount, mask = None): if amount == 0: return t else: amount = min(amount, t.shape[1]) if exists(mask): t = t.masked_fill(~mask[..., None], 0.) return pad_at_dim(t, (amount, -amount), dim = - 2, value = 0.) class ShiftTokens(nn.Module): def __init__(self, shifts, fn): super().__init__() self.fn = fn self.shifts = tuple(shifts) def forward(self, x, **kwargs): mask = kwargs.get('mask', None) shifts = self.shifts segments = len(shifts) feats_per_shift = x.shape[-1] // segments splitted = x.split(feats_per_shift, dim = -1) segments_to_shift, rest = splitted[:segments], splitted[segments:] segments_to_shift = list(map(lambda args: shift(*args, mask = mask), zip(segments_to_shift, shifts))) x = torch.cat((*segments_to_shift, *rest), dim = -1) return self.fn(x, **kwargs) # feedforward class GLU(nn.Module): def __init__( self, dim_in, dim_out, activation: Callable, mult_bias = False ): super().__init__() self.act = activation self.proj = nn.Linear(dim_in, dim_out * 2) self.mult_bias = nn.Parameter(torch.ones(dim_out)) if mult_bias else 1. def forward(self, x): x, gate = self.proj(x).chunk(2, dim = -1) return x * self.act(gate) * self.mult_bias class FeedForward(nn.Module): def __init__( self, dim, dim_out = None, mult = 4, glu = False, glu_mult_bias = False, swish = False, relu_squared = False, post_act_ln = False, dropout = 0., no_bias = False, zero_init_output = False ): super().__init__() inner_dim = int(dim * mult) dim_out = default(dim_out, dim) if relu_squared: activation = ReluSquared() elif swish: activation = nn.SiLU() else: activation = nn.GELU() if glu: project_in = GLU(dim, inner_dim, activation, mult_bias = glu_mult_bias) else: project_in = nn.Sequential( nn.Linear(dim, inner_dim, bias = not no_bias), activation ) self.ff = Sequential( project_in, nn.LayerNorm(inner_dim) if post_act_ln else None, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out, bias = not no_bias) ) # init last linear layer to 0 if zero_init_output: init_zero_(self.ff[-1]) def forward(self, x): return self.ff(x) # attention. it is all we need class Attention(nn.Module): def __init__( self, dim, dim_head = DEFAULT_DIM_HEAD, heads = 8, causal = False, flash = False, talking_heads = False, head_scale = False, sparse_topk = None, num_mem_kv = 0, dropout = 0., on_attn = False, gate_values = False, zero_init_output = False, max_attend_past = None, qk_norm = False, qk_norm_groups = 1, qk_norm_scale = 10, qk_norm_dim_scale = False, one_kv_head = False, kv_heads = None, shared_kv = False, value_dim_head = None, tensor_product = False, # https://arxiv.org/abs/2208.06061 cascading_heads = False, add_zero_kv = False, # same as add_zero_attn in pytorch onnxable = False ): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads self.causal = causal self.max_attend_past = max_attend_past assert not (exists(kv_heads) and one_kv_head), 'either attn_one_kv_head is set to True (in which case kv_heads is set to 1), or attn_kv_heads is set, but not both' value_dim_head = default(value_dim_head, dim_head) kv_heads = default(kv_heads, heads) kv_heads = 1 if one_kv_head else kv_heads assert divisible_by(heads, kv_heads) self.kv_heads = kv_heads q_dim = dim_head * heads k_dim = dim_head * kv_heads v_dim = value_dim_head * kv_heads out_dim = value_dim_head * heads self.to_q = nn.Linear(dim, q_dim, bias = False) self.to_k = nn.Linear(dim, k_dim, bias = False) # shared key / values, for further memory savings during inference assert not (shared_kv and value_dim_head != dim_head), 'key and value head dimensions must be equal for shared key / values' self.to_v = nn.Linear(dim, v_dim, bias = False) if not shared_kv else None # relations projection from tp-attention self.to_r = nn.Linear(dim, v_dim, bias = False) if tensor_product else None # add GLU gating for aggregated values, from alphafold2 self.to_v_gate = None if gate_values: self.to_v_gate = nn.Linear(dim, out_dim) nn.init.constant_(self.to_v_gate.weight, 0) nn.init.constant_(self.to_v_gate.bias, 1) # cosine sim attention self.qk_norm = qk_norm self.qk_norm_groups = qk_norm_groups self.qk_norm_scale = qk_norm_scale # whether to use the rmsnorm (equivalent to cosine sim attention when scale is equal to 1) - https://arxiv.org/abs/2302.05442 self.qk_norm_dim_scale = qk_norm_dim_scale self.qk_norm_q_scale = self.qk_norm_k_scale = 1 if qk_norm and qk_norm_dim_scale: self.qk_norm_q_scale = nn.Parameter(torch.ones(dim_head)) self.qk_norm_k_scale = nn.Parameter(torch.ones(dim_head)) assert (not qk_norm) or divisible_by(dim_head, qk_norm_groups), 'dimension per attention head must be divisible by the qk norm groups' assert not (qk_norm and (dim_head // qk_norm_groups) <= 2), 'the group dimension may be too small (2 was too small in my tests, but 4 still works, surprisingly)' # attend class - includes core attention algorithm + talking heads self.attend = Attend( heads = heads, causal = causal, talking_heads = talking_heads, dropout = dropout, sparse_topk = sparse_topk, qk_norm = qk_norm, scale = qk_norm_scale if qk_norm else self.scale, add_zero_kv = add_zero_kv, flash = flash, onnxable = onnxable ) # if cascading_heads: # # cascading heads - wrap the Attend logic # self.attend = CascadingHeads(self.attend) # head scaling self.head_scale = head_scale if head_scale: self.head_scale_params = nn.Parameter(torch.ones(1, heads, 1, 1)) # explicit topk sparse attention self.sparse_topk = sparse_topk # add memory key / values self.num_mem_kv = num_mem_kv if num_mem_kv > 0: self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head)) # attention on attention self.attn_on_attn = on_attn self.to_out = nn.Sequential(nn.Linear(out_dim, dim * 2, bias = False), nn.GLU()) if on_attn else nn.Linear(out_dim, dim, bias = False) # init output projection 0 if zero_init_output: init_zero_(self.to_out) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, rel_pos = None, rotary_pos_emb = None, prev_attn = None, mem = None ): b, n, _, h, kv_h, head_scale, device, has_context = *x.shape, self.heads, self.kv_heads, self.head_scale, x.device, exists(context) kv_input = default(context, x) q_input = x k_input = kv_input v_input = kv_input r_input = x if exists(mem): k_input = torch.cat((mem, k_input), dim = -2) v_input = torch.cat((mem, v_input), dim = -2) q = self.to_q(q_input) k = self.to_k(k_input) v = self.to_v(v_input) if exists(self.to_v) else k r = self.to_r(r_input) if exists(self.to_r) else None q = rearrange(q, 'b n (h d) -> b h n d', h = h) k, v, r = map(lambda t: maybe(rearrange)(t, 'b n (h d) -> b h n d', h = kv_h), (k, v, r)) if self.qk_norm: qk_l2norm = partial(l2norm, groups = self.qk_norm_groups) q, k = map(qk_l2norm, (q, k)) scale = self.qk_norm_scale q = q * self.qk_norm_q_scale k = k * self.qk_norm_k_scale if exists(rotary_pos_emb) and not has_context: freqs, xpos_scale = rotary_pos_emb l = freqs.shape[-1] q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale ** -1.) if exists(xpos_scale) else (1., 1.) (ql, qr), (kl, kr), (vl, vr) = map(lambda t: (t[..., :l], t[..., l:]), (q, k, v)) ql, kl, vl = map(lambda arg: apply_rotary_pos_emb(arg[0], freqs, arg[1]), ((ql, q_xpos_scale), (kl, k_xpos_scale), (vl, k_xpos_scale))) q, k, v = map(lambda t: torch.cat(t, dim = -1), ((ql, qr), (kl, kr), (vl, vr))) input_mask = context_mask if has_context else mask if self.num_mem_kv > 0: mem_k, mem_v = map(lambda t: repeat(t, 'h n d -> b h n d', b = b), (self.mem_k, self.mem_v)) if self.qk_norm: mem_k = l2norm(mem_k) mem_k = mem_k * self.qk_norm_k_scale k = torch.cat((mem_k, k), dim = -2) v = torch.cat((mem_v, v), dim = -2) if exists(input_mask): input_mask = pad_at_dim(input_mask, (self.num_mem_kv, 0), dim = -1, value = True) i, j = map(lambda t: t.shape[-2], (q, k)) # determine masking mask_value = max_neg_value(q) masks = [] final_attn_mask = None if exists(input_mask): input_mask = rearrange(input_mask, 'b j -> b 1 1 j') masks.append(~input_mask) if exists(attn_mask): assert 2 <= attn_mask.ndim <= 4, 'attention mask must have greater than 2 dimensions but less than or equal to 4' if attn_mask.ndim == 2: attn_mask = rearrange(attn_mask, 'i j -> 1 1 i j') elif attn_mask.ndim == 3: attn_mask = rearrange(attn_mask, 'h i j -> 1 h i j') masks.append(~attn_mask) if exists(self.max_attend_past): range_q = torch.arange(j - i, j, device = device) range_k = torch.arange(j, device = device) dist = rearrange(range_q, 'i -> 1 1 i 1') - rearrange(range_k, 'j -> 1 1 1 j') max_attend_past_mask = dist > self.max_attend_past masks.append(max_attend_past_mask) if len(masks) > 0: final_attn_mask = ~or_reduce(masks) # prepare relative positional bias, if needed attn_bias = None if exists(rel_pos): attn_bias = rel_pos(i, j) # attention is all we need out, intermediates = self.attend( q, k, v, mask = final_attn_mask, attn_bias = attn_bias, prev_attn = prev_attn ) # https://arxiv.org/abs/2208.06061 proposes to add a residual for better gradients if exists(r): out = out * r + out # normformer scaling of heads if head_scale: out = out * self.head_scale_params # merge heads out = rearrange(out, 'b h n d -> b n (h d)') # alphafold2 styled gating of the values if exists(self.to_v_gate): gates = self.to_v_gate(x) out = out * gates.sigmoid() # combine the heads out = self.to_out(out) if exists(mask): mask = rearrange(mask, 'b n -> b n 1') out = out.masked_fill(~mask, 0.) return out, intermediates class AttentionLayers(nn.Module): def __init__( self, dim, depth, heads = 8, causal = False, cross_attend = False, only_cross = False, use_scalenorm = False, use_rmsnorm = False, use_simple_rmsnorm = False, alibi_pos_bias = False, alibi_num_heads = None, rel_pos_bias = False, rel_pos_num_buckets = 32, rel_pos_max_distance = 128, dynamic_pos_bias = False, dynamic_pos_bias_log_distance = False, dynamic_pos_bias_mlp_depth = 2, dynamic_pos_bias_norm = False, rotary_pos_emb = False, rotary_emb_dim = None, rotary_xpos = False, rotary_interpolation_factor = 1., rotary_xpos_scale_base = 512, rotary_base_rescale_factor = 1., custom_layers = None, sandwich_coef = None, par_ratio = None, residual_attn = False, cross_residual_attn = False, macaron = False, pre_norm = True, pre_norm_has_final_norm = True, gate_residual = False, scale_residual = False, scale_residual_constant = 1., deepnorm = False, shift_tokens = 0, sandwich_norm = False, resi_dual = False, resi_dual_scale = 1., zero_init_branch_output = False, layer_dropout = 0., cross_attn_tokens_dropout = 0., **kwargs ): super().__init__() rotary_pos_emb = rotary_pos_emb or rotary_xpos ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) attn_kwargs, kwargs = groupby_prefix_and_trim('attn_', kwargs) dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) self.dim = dim self.depth = depth self.layers = nn.ModuleList([]) self.has_pos_emb = rel_pos_bias or rotary_pos_emb rotary_emb_dim = max(default(rotary_emb_dim, dim_head // 2), 32) assert not (rotary_xpos and not causal), 'rotary xpos is not compatible with bidirectional attention' self.rotary_pos_emb = RotaryEmbedding(rotary_emb_dim, use_xpos = rotary_xpos, scale_base = rotary_xpos_scale_base, interpolation_factor = rotary_interpolation_factor, base_rescale_factor = rotary_base_rescale_factor) if rotary_pos_emb else None assert not (alibi_pos_bias and rel_pos_bias), 'you can only choose Alibi positional bias or T5 relative positional bias, not both' assert rel_pos_num_buckets <= rel_pos_max_distance, 'number of relative position buckets must be less than the relative position max distance' # relative positional bias flash_attn = attn_kwargs.get('flash', False) assert (int(rel_pos_bias) + int(dynamic_pos_bias) + int(alibi_pos_bias)) <= 1, 'you can only choose up to one of t5, alibi, or dynamic positional bias' self.rel_pos = None if rel_pos_bias: assert not flash_attn, 'flash attention not compatible with t5 relative positional bias' self.rel_pos = RelativePositionBias(scale = dim_head ** 0.5, causal = causal, heads = heads, num_buckets = rel_pos_num_buckets, max_distance = rel_pos_max_distance) elif dynamic_pos_bias: assert not flash_attn, 'flash attention not compatible with dynamic positional bias' self.rel_pos = DynamicPositionBias(dim = dim // 4, heads = heads, log_distance = dynamic_pos_bias_log_distance, depth = dynamic_pos_bias_mlp_depth, norm = dynamic_pos_bias_norm) elif alibi_pos_bias: alibi_num_heads = default(alibi_num_heads, heads) assert alibi_num_heads <= heads, 'number of ALiBi heads must be less than the total number of heads' self.rel_pos = AlibiPositionalBias(heads = alibi_num_heads, total_heads = heads) # determine deepnorm and residual scale if deepnorm: assert scale_residual_constant == 1, 'scale residual constant is being overridden by deep norm settings' pre_norm = sandwich_norm = resi_dual = False scale_residual = True scale_residual_constant = (2 * depth) ** 0.25 assert (int(sandwich_norm) + int(resi_dual)) <= 1, 'either sandwich norm or resiDual is selected, but not both' assert not (not pre_norm and sandwich_norm), 'sandwich norm cannot be used when not using prenorm' if resi_dual: pre_norm = False self.pre_norm = pre_norm self.sandwich_norm = sandwich_norm self.resi_dual = resi_dual assert 0 < resi_dual_scale <= 1., 'resiDual prenorm residual must be scaled by a factor greater than 0 and less than or equal to 1.' self.resi_dual_scale = resi_dual_scale self.residual_attn = residual_attn self.cross_residual_attn = cross_residual_attn assert not (flash_attn and (residual_attn or cross_residual_attn)), 'flash attention is not compatible with residual attention' self.cross_attend = cross_attend assert (int(use_scalenorm) + int(use_rmsnorm) + int(use_simple_rmsnorm)) <= 1, 'you can only use either scalenorm, rmsnorm, or simple rmsnorm' if use_scalenorm: norm_class = ScaleNorm elif use_rmsnorm: norm_class = RMSNorm elif use_simple_rmsnorm: norm_class = SimpleRMSNorm else: norm_class = nn.LayerNorm norm_fn = partial(norm_class, dim) if cross_attend and not only_cross: default_block = ('a', 'c', 'f') elif cross_attend and only_cross: default_block = ('c', 'f') else: default_block = ('a', 'f') if macaron: default_block = ('f',) + default_block # zero init if zero_init_branch_output: attn_kwargs = {**attn_kwargs, 'zero_init_output': True} ff_kwargs = {**ff_kwargs, 'zero_init_output': True} # calculate layer block order if exists(custom_layers): layer_types = custom_layers elif exists(par_ratio): par_depth = depth * len(default_block) assert 1 < par_ratio <= par_depth, 'par ratio out of range' default_block = tuple(filter(not_equals('f'), default_block)) par_attn = par_depth // par_ratio depth_cut = par_depth * 2 // 3 # 2 / 3 attention layer cutoff suggested by PAR paper par_width = (depth_cut + depth_cut // par_attn) // par_attn assert len(default_block) <= par_width, 'default block is too large for par_ratio' par_block = default_block + ('f',) * (par_width - len(default_block)) par_head = par_block * par_attn layer_types = par_head + ('f',) * (par_depth - len(par_head)) elif exists(sandwich_coef): assert sandwich_coef > 0 and sandwich_coef <= depth, 'sandwich coefficient should be less than the depth' layer_types = ('a',) * sandwich_coef + default_block * (depth - sandwich_coef) + ('f',) * sandwich_coef else: layer_types = default_block * depth self.layer_types = layer_types self.num_attn_layers = len(list(filter(equals('a'), layer_types))) # stochastic depth self.layer_dropouts = cast_tuple(layer_dropout, len(layer_types)) # structured dropout for cross attending self.cross_attn_tokens_dropout = cross_attn_tokens_dropout # calculate token shifting shift_tokens = cast_tuple(shift_tokens, len(layer_types)) # whether it has post norm self.final_norm = norm_fn() if pre_norm or resi_dual else nn.Identity() # iterate and construct layers for ind, (layer_type, layer_shift_tokens) in enumerate(zip(self.layer_types, shift_tokens)): is_last_layer = ind == (len(self.layer_types) - 1) if layer_type == 'a': layer = Attention(dim, heads = heads, causal = causal, **attn_kwargs) elif layer_type == 'c': layer = Attention(dim, heads = heads, **attn_kwargs) elif layer_type == 'f': layer = FeedForward(dim, **ff_kwargs) layer = layer if not macaron else Scale(0.5, layer) else: raise Exception(f'invalid layer type {layer_type}') if layer_shift_tokens > 0: shift_range_upper = layer_shift_tokens + 1 shift_range_lower = -layer_shift_tokens if not causal else 0 layer = ShiftTokens(range(shift_range_lower, shift_range_upper), layer) residual_fn = GRUGating if gate_residual else Residual residual = residual_fn(dim, scale_residual = scale_residual, scale_residual_constant = scale_residual_constant) pre_branch_norm = norm_fn() if pre_norm else None post_branch_norm = norm_fn() if sandwich_norm else None post_main_norm = norm_fn() if not pre_norm else None norms = nn.ModuleList([ pre_branch_norm, post_branch_norm, post_main_norm ]) self.layers.append(nn.ModuleList([ norms, layer, residual ])) if deepnorm: init_gain = (8 * depth) ** -0.25 deepnorm_init(self, init_gain) def forward( self, x, context = None, mask = None, context_mask = None, attn_mask = None, self_attn_context_mask = None, mems = None, return_hiddens = False ): assert not (self.cross_attend ^ exists(context)), 'context must be passed in if cross_attend is set to True' hiddens = [] layer_hiddens = [] intermediates = [] prev_attn = None prev_cross_attn = None mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers rotary_pos_emb = None if exists(self.rotary_pos_emb): max_rotary_emb_length = max(list(map(lambda m: (m.shape[1] if exists(m) else 0) + x.shape[1], mems))) rotary_pos_emb = self.rotary_pos_emb(max_rotary_emb_length, x.device) outer_residual = x * self.resi_dual_scale for ind, (layer_type, (norm, block, residual_fn), layer_dropout) in enumerate(zip(self.layer_types, self.layers, self.layer_dropouts)): is_last = ind == (len(self.layers) - 1) if self.training and layer_dropout > 0. and random() < layer_dropout: continue if layer_type == 'a': if return_hiddens: hiddens.append(x) layer_mem = mems.pop(0) if mems else None if layer_type == 'c': if self.training and self.cross_attn_tokens_dropout > 0.: context, context_mask = dropout_seq(context, context_mask, self.cross_attn_tokens_dropout) inner_residual = x if return_hiddens: layer_hiddens.append(x) pre_norm, post_branch_norm, post_main_norm = norm if exists(pre_norm): x = pre_norm(x) if layer_type == 'a': out, inter = block(x, mask = mask, context_mask = self_attn_context_mask, attn_mask = attn_mask, rel_pos = self.rel_pos, rotary_pos_emb = rotary_pos_emb, prev_attn = prev_attn, mem = layer_mem) elif layer_type == 'c': out, inter = block(x, context = context, mask = mask, context_mask = context_mask, prev_attn = prev_cross_attn) elif layer_type == 'f': out = block(x) if self.resi_dual: outer_residual = outer_residual + out * self.resi_dual_scale if exists(post_branch_norm): out = post_branch_norm(out) x = residual_fn(out, inner_residual) if layer_type in ('a', 'c') and return_hiddens: intermediates.append(inter) if layer_type == 'a' and self.residual_attn: prev_attn = inter.pre_softmax_attn elif layer_type == 'c' and self.cross_residual_attn: prev_cross_attn = inter.pre_softmax_attn if exists(post_main_norm): x = post_main_norm(x) if return_hiddens: layer_hiddens.append(x) if self.resi_dual: x = x + self.final_norm(outer_residual) else: x = self.final_norm(x) if return_hiddens: intermediates = LayerIntermediates( hiddens = hiddens, attn_intermediates = intermediates, layer_hiddens = layer_hiddens ) return x, intermediates return x class Encoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on encoder' super().__init__(causal = False, **kwargs) class Decoder(AttentionLayers): def __init__(self, **kwargs): assert 'causal' not in kwargs, 'cannot set causality on decoder' super().__init__(causal = True, **kwargs) class CrossAttender(AttentionLayers): def __init__(self, **kwargs): super().__init__(cross_attend = True, only_cross = True, **kwargs) class ViTransformerWrapper(nn.Module): def __init__( self, *, image_size, patch_size, attn_layers, channels = 3, num_classes = None, post_emb_norm = False, emb_dropout = 0. ): super().__init__() assert isinstance(attn_layers, Encoder), 'attention layers must be an Encoder' assert divisible_by(image_size, patch_size), 'image dimensions must be divisible by the patch size' dim = attn_layers.dim num_patches = (image_size // patch_size) ** 2 patch_dim = channels * patch_size ** 2 self.patch_size = patch_size self.pos_embedding = nn.Parameter(torch.randn(1, num_patches, dim)) self.patch_to_embedding = nn.Sequential( nn.LayerNorm(patch_dim), nn.Linear(patch_dim, dim), nn.LayerNorm(dim) ) self.post_emb_norm = nn.LayerNorm(dim) if post_emb_norm else nn.Identity() self.dropout = nn.Dropout(emb_dropout) self.attn_layers = attn_layers self.mlp_head = nn.Linear(dim, num_classes) if exists(num_classes) else nn.Identity() def forward( self, img, return_embeddings = False ): p = self.patch_size x = rearrange(img, 'b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = p, p2 = p) x = self.patch_to_embedding(x) n = x.shape[1] x = x + self.pos_embedding[:, :n] x = self.post_emb_norm(x) x = self.dropout(x) x = self.attn_layers(x) if not exists(self.mlp_head) or return_embeddings: return x x = x.mean(dim = -2) return self.mlp_head(x) class Transformer(nn.Module): def __init__( self, *, num_tokens, max_seq_len, attn_layers, emb_dim = None, max_mem_len = 0, shift_mem_down = 0, emb_dropout = 0., post_emb_norm = False, num_memory_tokens = None, tie_embedding = False, logits_dim = None, use_abs_pos_emb = True, scaled_sinu_pos_emb = False, l2norm_embed = False, emb_frac_gradient = 1., # GLM-130B and Cogview successfully used this, set at 0.1 attn_z_loss_weight = 1e-4 ): super().__init__() assert isinstance(attn_layers, AttentionLayers), 'attention layers must be one of Encoder or Decoder' dim = attn_layers.dim emb_dim = default(emb_dim, dim) self.emb_dim = emb_dim self.num_tokens = num_tokens self.max_seq_len = max_seq_len self.max_mem_len = max_mem_len self.shift_mem_down = shift_mem_down self.l2norm_embed = l2norm_embed self.token_emb = TokenEmbedding(emb_dim, num_tokens, l2norm_embed = l2norm_embed) if not (use_abs_pos_emb and not attn_layers.has_pos_emb): self.pos_emb = always(0) elif scaled_sinu_pos_emb: self.pos_emb = ScaledSinusoidalEmbedding(emb_dim) else: self.pos_emb = AbsolutePositionalEmbedding(emb_dim, max_seq_len, l2norm_embed = l2norm_embed) self.emb_frac_gradient = emb_frac_gradient # fraction of the gradient that should go to the embedding, https://arxiv.org/abs/2105.13290 self.post_emb_norm = nn.LayerNorm(emb_dim) if post_emb_norm else nn.Identity() self.emb_dropout = nn.Dropout(emb_dropout) self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() self.attn_layers = attn_layers self.init_() logits_dim = default(logits_dim, num_tokens) self.to_logits = nn.Linear(dim, logits_dim) if not tie_embedding else lambda t: t @ self.token_emb.emb.weight.t() # memory tokens (like [cls]) from Memory Transformers paper num_memory_tokens = default(num_memory_tokens, 0) self.num_memory_tokens = num_memory_tokens if num_memory_tokens > 0: self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim)) def init_(self): if self.l2norm_embed: nn.init.normal_(self.token_emb.emb.weight, std = 1e-5) if not isinstance(self.pos_emb, always): nn.init.normal_(self.pos_emb.emb.weight, std = 1e-5) return nn.init.kaiming_normal_(self.token_emb.emb.weight) def forward( self, x, return_embeddings = False, return_logits_and_embeddings = False, return_intermediates = False, mask = None, return_mems = False, return_attn = False, mems = None, pos = None, prepend_embeds = None, sum_embeds = None, return_attn_z_loss = False, attn_z_loss_weight = 1e-4, **kwargs ): b, n, device, num_mem, emb_frac_gradient = *x.shape, x.device, self.num_memory_tokens, self.emb_frac_gradient return_hiddens = return_mems | return_attn | return_intermediates | return_attn_z_loss # absolute positional embedding external_pos_emb = exists(pos) and pos.dtype != torch.long pos_emb = self.pos_emb(x, pos = pos) if not external_pos_emb else pos x = self.token_emb(x) + pos_emb # for summing embeddings passed externally - needs this for self-conditioning in non-autoregressive training if exists(sum_embeds): x = x + sum_embeds # post embedding norm, purportedly leads to greater stabilization x = self.post_emb_norm(x) # whether to append embeds, as in PaLI, for image embeddings if exists(prepend_embeds): prepend_seq, prepend_dim = prepend_embeds.shape[1:] assert prepend_dim == x.shape[-1], 'prepended embeddings need to have same dimensions as text model dimensions' x = torch.cat((prepend_embeds, x), dim = -2) # whether to reduce the gradient going to the embedding, from cogview paper, corroborated by GLM-130B model if emb_frac_gradient < 1: assert emb_frac_gradient > 0 x = x * emb_frac_gradient + x.detach() * (1 - emb_frac_gradient) # embedding dropout x = self.emb_dropout(x) x = self.project_emb(x) if num_mem > 0: mem = repeat(self.memory_tokens, 'n d -> b n d', b = b) x = torch.cat((mem, x), dim = 1) # auto-handle masking after appending memory tokens if exists(mask): mask = pad_at_dim(mask, (num_mem, 0), dim = -1, value = True) if self.shift_mem_down and exists(mems): mems_l, mems_r = mems[:self.shift_mem_down], mems[self.shift_mem_down:] mems = [*mems_r, *mems_l] if return_hiddens: x, intermediates = self.attn_layers(x, mask = mask, mems = mems, return_hiddens = True, **kwargs) else: x = self.attn_layers(x, mask = mask, mems = mems, **kwargs) mem, x = x[:, :num_mem], x[:, num_mem:] if return_logits_and_embeddings: out = (self.to_logits(x), x) elif return_embeddings: out = x else: out = self.to_logits(x) if return_attn_z_loss: pre_softmax_attns = list(map(lambda t: t.pre_softmax_attn, intermediates.attn_intermediates)) intermediates.attn_z_loss = calc_z_loss(pre_softmax_attns, weight = attn_z_loss_weight) return_intermediates = True if return_intermediates: return out, intermediates if return_mems: hiddens = intermediates.hiddens new_mems = list(map(lambda pair: torch.cat(pair, dim = -2), zip(mems, hiddens))) if exists(mems) else hiddens new_mems = list(map(lambda t: t[..., -self.max_mem_len:, :].detach(), new_mems)) return out, new_mems if return_attn: attn_maps = list(map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)) return out, attn_maps return out
zeta-main
zeta/nn/architecture/transformer.py
import math from functools import partial from itertools import zip_longest from typing import Tuple import torch import torch.nn.functional as F from einops import rearrange, repeat from einops.layers.torch import Rearrange from torch import einsum, nn from vector_quantize_pytorch import RandomProjectionQuantizer from zeta.nn.architecture.attn_layers import rotate_half from zeta.nn.attention.attend import Attend from zeta.nn.attention.local_attention_mha import LocalMHA from zeta.nn.embeddings.rope import RotaryEmbedding # constants mlist = nn.ModuleList Linear = partial(nn.Linear, bias = False) LocalMHA = partial(LocalMHA, causal = True, prenorm = True) # helper functions def exists(val): return val is not None def is_power_of_two(n): return math.log2(n).is_integer() def all_unique(arr): return len(set(arr)) == len(arr) def apply_fns(fns, tensors): return [fn(tensor) for fn, tensor in zip(fns, tensors)] def cast_tuple(t, length = 1): return t if isinstance(t, tuple) else ((t,) * length) def default(*vals): for val in vals: if exists(val): return val return None def eval_decorator(fn): def inner(model, *args, **kwargs): was_training = model.training model.eval() out = fn(model, *args, **kwargs) model.train(was_training) return out return inner # sampling helpers def log(t, eps = 1e-20): return t.clamp(min = eps).log() def gumbel_noise(t): noise = torch.zeros_like(t).uniform_(0, 1) return -log(-log(noise)) def gumbel_sample(t, temperature = 1., dim = -1): return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim = dim) def top_k(logits, thres = 0.9): k = int((1 - thres) * logits.shape[-1]) val, ind = torch.topk(logits, k) probs = torch.full_like(logits, -torch.finfo(logits.dtype).max) probs.scatter_(1, ind, val) return probs # rotary positional embedding w/ xpos # https://arxiv.org/abs/2104.09864 # https://arxiv.org/abs/2212.10554v1 def apply_rotary_pos_emb(pos, t, scale = 1.): seq_len = t.shape[-2] pos = pos[..., -seq_len:, :] if not isinstance(scale, (int, float)): scale = scale[..., -seq_len:, :] return (t * pos.cos() * scale) + (rotate_half(t) * pos.sin() * scale) def apply_rotary_pos_emb_qk(rotary_emb, q, k): freqs, scale = rotary_emb q = apply_rotary_pos_emb(freqs, q, scale) k = apply_rotary_pos_emb(freqs, k, scale ** -1) return q, k # token shift, from Peng et al of RWKV def token_shift(t): t, t_shift = t.chunk(2, dim = -1) t_shift = F.pad(t_shift, (0, 0, 1, -1)) return torch.cat((t, t_shift), dim = -1) # hierarchy related classes def pad_seq_to_multiple(t, mult): seq_len = t.shape[-2] next_seq_len_mult = math.ceil(seq_len / mult) * mult remainder = next_seq_len_mult - seq_len if remainder == 0: return t, seq_len t = F.pad(t, (0, 0, 0, remainder), value = 0.) return t, seq_len def curtail_seq_to_multiple(t, mult): seq_len = t.shape[-2] prev_seq_len_mult = (seq_len // mult) * mult remainder = seq_len - prev_seq_len_mult if remainder == 0: return t t = t[..., :prev_seq_len_mult, :] return t def hierarchical_cat(tokens, strides: Tuple[int, ...]): assert len(tokens) == len(strides) if all([s == 1 for s in strides]): return torch.cat(tokens, dim = -1) tokens = [repeat(t, 'b n d -> b (n s) d', s = s) for t, s in zip(tokens, strides)] min_seq_len = min([t.shape[-2] for t in tokens]) tokens = [t[..., :min_seq_len, :] for t in tokens] return torch.cat(tokens, dim = -1) class CausalConv(nn.Module): def __init__( self, dim_in, dim_out, kernel_size, stride = 1 ): super().__init__() self.causal_padding = kernel_size - 1 self.conv = nn.Conv1d(dim_in, dim_out, kernel_size, stride = stride) def forward(self, x): x = F.pad(x, (self.causal_padding, 0)) return self.conv(x) class Compress(nn.Module): def __init__( self, *, dim, dim_out, num_tokens = None, stride = 1, compress_factor = 1, expansion_factor = 4, dim_head = 64, heads = 8, ignore_index = 0, should_recon = False, should_prophet = False, prophet_num_predictions = None ): super().__init__() assert compress_factor > 0 and is_power_of_two(compress_factor) self.stride = stride self.no_compress = compress_factor == 1 self.compress_factor = compress_factor self.should_recon = should_recon self.should_prophet = should_prophet if self.no_compress: self.compress_fn = Linear(dim, dim_out) if dim != dim_out else nn.Identity() return dim_inner = int(dim * expansion_factor) self.compress_fn = nn.Sequential( Rearrange('b n d -> b d n'), CausalConv(dim, dim_inner, compress_factor, stride = stride), nn.SiLU(), nn.Conv1d(dim_inner, dim_out, 1), Rearrange('b d n -> b n d') ) if should_recon: assert exists(num_tokens) self.to_recon = Linear(dim_out, compress_factor * num_tokens) if should_prophet: assert exists(prophet_num_predictions) self.to_prophet = Linear(dim_out, prophet_num_predictions) self.ignore_index = ignore_index def prophet(self, h, ids): if not self.should_prophet: return torch.zeros((), device = h.device).requires_grad_() c = self.compress_factor seq_len = ids.shape[-1] prophet_logits = self.to_prophet(h) prophet_logits = rearrange(prophet_logits, 'b n (c d) -> (b c) d n', c = c) prophet_ids = F.pad(ids, (-1, c), value = self.ignore_index) prophet_ids = tuple(prophet_ids[:, i:(seq_len + i)] for i in range(c)) prophet_ids = torch.stack(prophet_ids, dim = 1) prophet_ids = rearrange(prophet_ids, 'b c n -> (b c) n') if self.stride > 1: prophet_ids = prophet_ids[..., ::self.stride] prophet_loss = F.cross_entropy(prophet_logits, prophet_ids, ignore_index = self.ignore_index) return prophet_loss def recon(self, h, ids): assert self.should_recon if self.no_compress: return torch.zeros((), device = h.device).requires_grad_() c = self.compress_factor seq_len = ids.shape[-1] recon_logits = self.to_recon(h) recon_logits = rearrange(recon_logits, 'b n (c d) -> (b c) d n', c = c) recon_ids = F.pad(ids, (c - 1, 0), value = self.ignore_index) recon_ids = tuple(recon_ids[:, i:(seq_len + i)] for i in range(c)) recon_ids = torch.stack(recon_ids, dim = 1) recon_ids = rearrange(recon_ids, 'b c n -> (b c) n') if self.stride > 1: recon_ids = recon_ids[..., ::self.stride] recon_loss = F.cross_entropy(recon_logits, recon_ids, ignore_index = self.ignore_index) return recon_loss def forward(self, x): return self.compress_fn(x) class HierarchicalMerge(nn.Module): def __init__( self, dims: Tuple[int, ...], dim_out, h_strides = 1 ): super().__init__() dim = sum(dims) strides = cast_tuple(h_strides, len(dims)) assert len(strides) == len(dims) self.strides = strides self.net = nn.Sequential( RMSNorm(dim), nn.Linear(dim, dim_out * 2), nn.SiLU(), nn.Linear(dim_out * 2, dim_out) ) def forward(self, tokens): x = hierarchical_cat(tokens, self.strides) return self.net(x) # classes class RMSNorm(nn.Module): def __init__(self, dim): super().__init__() self.scale = dim ** 0.5 self.gamma = nn.Parameter(torch.ones(dim)) def forward(self, x): return F.normalize(x, dim = -1) * self.scale * self.gamma class FeedForward(nn.Module): def __init__(self, dim, mult = 4): super().__init__() dim_inner = int(dim * mult) self.net = nn.Sequential( RMSNorm(dim), Linear(dim, dim_inner), nn.GELU(), Linear(dim_inner, dim) ) def forward(self, x): return self.net(x) class Attention(nn.Module): def __init__( self, dim, dim_head = 64, heads = 8, use_flash_attn = False ): super().__init__() self.scale = dim_head ** -0.5 self.heads = heads dim_inner = dim_head * heads self.norm = RMSNorm(dim) self.rotary_emb = RotaryEmbedding(dim_head) self.attend = Attend(causal = True, use_flash_attn = use_flash_attn) self.to_qkv = Linear(dim, dim_inner * 3) self.to_out = Linear(dim_inner, dim) def forward(self, x): n = x.shape[-2] x = self.norm(x) q, k, v = self.to_qkv(x).chunk(3, dim = -1) q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), (q, k, v)) rotary_emb = self.rotary_emb(n) q, k = apply_rotary_pos_emb_qk(rotary_emb, q, k) out = self.attend(q, k, v) out = rearrange(out, 'b h n d -> b n (h d)') return self.to_out(out) class HierarchicalBlock(nn.Module): def __init__( self, dim, dim_head = 64, heads = 8, window_size = None, compress_factor = 1, stride = 1, ff_mult = 4 ): super().__init__() self.stride = stride assert is_power_of_two(compress_factor) self.compress_factor = compress_factor self.no_compress = compress_factor == 1 assert not exists(window_size) or window_size >= 0 self.has_attn = window_size != 0 self.attn = None if self.has_attn: attn_klass = Attention if exists(window_size): attn_klass = partial(LocalMHA, window_size = window_size) self.attn = attn_klass(dim = dim, dim_head = dim_head, heads = heads) self.ff = FeedForward(dim = dim, mult = ff_mult) def forward(self, x): c = self.compress_factor axial_dim = c // self.stride x, orig_seq_len = pad_seq_to_multiple(x, axial_dim) # hierarchical attention is performed with a simple axial attention # this, and using a convolution for compressing at the beginning # is one of the improvements on top of hourglass transformer # the downside is that the savings are only O(c) instead of O(c ** 2) as in hourglass transformer # you can get the O(c ** 2) saving by setting the hierarchical stride == c, but you'll see that performance is much worse, as some tokens will have a c - 1 token gap to the last hierarchical token if not self.no_compress: x = rearrange(x, 'b (n c) d -> (b c) n d', c = axial_dim) if exists(self.attn): x = self.attn(token_shift(x)) + x x = self.ff(token_shift(x)) + x if not self.no_compress: x = rearrange(x, '(b c) n d -> b (n c) d', c = axial_dim) return x[:, :orig_seq_len] class HierarchicalTransformer(nn.Module): def __init__( self, *, num_tokens, dim, depth, seq_len = 2048, dim_head = 64, heads = 8, ff_mult = 4, hierarchies = 1, window_sizes = None, hierarchical_stride = 1, hierarchy_merge_all = False, # whether to pass the pooled hierarchical information back to all hierarchies or just one doing the prediction ignore_index = 0, use_flash_attn = False, recon_loss_weight = 0.1, prophet_loss_weight = 0., prophet_loss_use_quantized = False, # for prophet, whether to use the next 1x token ids, or use the ids from random projection quantization prophet_quantized_use_embed = False, predict_hierarchy = None, predict_use_all_hierarchy = False, rq_num_codebooks = 4, rq_codebook_dim = 256, rq_codebook_size = 1024, ): """ By not specifying hierarchies and window_sizes, you basically default to a regular autoregressive transformer with attention across full sequence length, Three hierarchies, all servicing predicting the next token from zeta.nn import HierarchicalTransformer model = HierarchicalTransformer( num_tokens = 256, dim = (128, 256, 512, 1024), depth = 8, seq_len = 1024, use_flash_attn = True, ff_mult = (2, 2, 4, 4), dim_head = (16, 32, 64, 64), heads = (2, 4, 8, 8), hierarchies = (1, 2, 4, 16), hierarchical_stride = (1, 1, 1, 8), # this would determine the stride when compressing, and when concatting the hierarchical tokens to the fine tokens, the past tokens will be repeated this amount of time. causality is not violated as using the trick from hourglass transformers where sequence is shifted by compression factor - 1. recommend sticking with 1 except for highly compressed hierarchies, as it becomes very uncompetitive with baseline and generations look off window_sizes = (16, 32, 64, None) ).cuda() # hierarchies # 1x - dim 128 - attention (2 heads, 16 dim, receptive field 16) # 2x - dim 256 - attention (4 heads, 32 dim, receptive field 32) # 4x - dim 512 - attention (8 heads, 64 dim, receptive field 64) # 8x - dim 1024 - attention (8 heads, 64 dim, receptive field of all) """ super().__init__() self.seq_len = seq_len hierarchies = cast_tuple(hierarchies) assert all_unique(hierarchies), 'hierarchies compression factors must be all unique integers' assert all([*map(is_power_of_two, hierarchies)]), 'only powers of two allowed for hierarchies' self.hierarchies = hierarchies # just use a simple tuple list per hyperparameter to customize each hierarchy num_hierarchies = len(hierarchies) dims = cast_tuple(dim, num_hierarchies) assert len(dims) == num_hierarchies window_sizes = cast_tuple(window_sizes, num_hierarchies) assert len(window_sizes) == num_hierarchies dim_head = cast_tuple(dim_head, num_hierarchies) assert len(dim_head) == num_hierarchies heads = cast_tuple(heads, num_hierarchies) assert len(heads) == num_hierarchies ff_mult = cast_tuple(ff_mult, num_hierarchies) assert len(ff_mult) == num_hierarchies hierarchical_stride = cast_tuple(hierarchical_stride, num_hierarchies) assert all([*map(is_power_of_two, hierarchical_stride)]), 'all hierarchical strides must be power of two' assert all([s <= h for s, h in zip(hierarchical_stride, hierarchies)]), 'all strides must be less than the compression factor of the hierarchy' self.h_strides = hierarchical_stride assert len(hierarchical_stride) == num_hierarchies # this determines to which hierarchy is everything pooled into for final prediction # however, final next token prediction can also use all hierarchies with `predict_use_all_hierarchy` predict_hierarchy = default(predict_hierarchy, min(hierarchies)) self.predict_hierarchy_index = hierarchies.index(predict_hierarchy) hierarchy_predict_dim = dims[self.predict_hierarchy_index] self.hierarchy_merge_all = hierarchy_merge_all assert hierarchy_merge_all or self.h_strides[self.predict_hierarchy_index] == 1, 'the hierarchy level being used for final next token prediction must have compression stride of 1' # training related loss weights self.recon_loss_weight = recon_loss_weight self.prophet_loss_weight = prophet_loss_weight should_recon = recon_loss_weight > 0 should_prophet = prophet_loss_weight > 0 self.should_recon = should_recon self.should_prophet = should_prophet self.prophet_loss_use_quantized = prophet_loss_use_quantized self.prophet_quantized_use_embed = prophet_quantized_use_embed # token embedding dim_token_emb = max(dims) self.token_emb = nn.Embedding(num_tokens, dim_token_emb) # hierarchy compressions - 1x just uses the base token_emb weights self.compressors = mlist([]) for dim, hierarchy, stride in zip(dims, hierarchies, hierarchical_stride): self.compressors.append(Compress( dim = dim_token_emb, dim_out = dim, num_tokens = num_tokens, compress_factor = hierarchy, stride = stride, should_recon = should_recon, should_prophet = should_prophet, prophet_num_predictions = ((hierarchy * num_tokens) if not prophet_loss_use_quantized else (rq_num_codebooks * rq_codebook_size)) )) # post token embedding norms self.post_token_emb_norms = mlist([nn.LayerNorm(dim) for dim in dims]) # layers self.layers = mlist([]) self.dims = dims self.hierarchical_merges = mlist([]) self.need_hierarchical_merge = num_hierarchies > 1 for _ in range(depth): hierarchical_layer = mlist([]) # add a transformer block for each layer in the hierarchy for hierarchy, h_stride, h_dim, h_window_size, h_dim_head, h_heads, h_ff_mult in zip(hierarchies, hierarchical_stride, dims, window_sizes, dim_head, heads, ff_mult): # make sure the window size never exceeds the effective sequence length effective_seq_len = seq_len // hierarchy if exists(h_window_size) and h_window_size > effective_seq_len: print(f'window size for hierarchy {hierarchy}x is greater than effective sequence length - setting window size to None (which would use normal full attention)') h_window_size = None # add attention and feedforward hierarchical_layer.append( HierarchicalBlock( dim = h_dim, dim_head = h_dim_head, heads = h_heads, window_size = h_window_size, compress_factor = hierarchy, stride = h_stride, ff_mult = h_ff_mult ) ) self.layers.append(hierarchical_layer) # for merging the information across hierarchies # for now, only one direction, from all hierarchies to the hierarchy that is being used to make predictions on, set by predict_hierarchy_index above if not self.need_hierarchical_merge: continue merge = HierarchicalMerge( dims = dims, dim_out = hierarchy_predict_dim if not self.hierarchy_merge_all else sum(dims), h_strides = hierarchical_stride ) self.hierarchical_merges.append(merge) # final post-transformer norms, for all hierarchies self.norms = mlist([nn.LayerNorm(dim) for dim in dims]) # random projection quantizer, for another approach to hierarchical predictive coding if self.prophet_loss_use_quantized: rpq_klass = partial( RandomProjectionQuantizer, num_codebooks = rq_num_codebooks, codebook_dim = rq_codebook_dim, codebook_size = rq_codebook_size ) self.rand_proj_quantizers = mlist([rpq_klass(dim = dim) for dim in dims]) self.rq_num_codebooks = rq_num_codebooks # to logit, for hierarchy set at predict_hierarchy_index, or all hierarchies self.predict_use_all_hierarchy = predict_use_all_hierarchy logit_dim_in = sum(dims) if predict_use_all_hierarchy else hierarchy_predict_dim self.to_logits = Linear(logit_dim_in, num_tokens) # training related loss parameters self.ignore_index = ignore_index @torch.no_grad() @eval_decorator def generate( self, prompt, seq_len, temperature = 1.0, filter_thres = 0.9, **kwargs ): b, t, device = *prompt.shape, prompt.device out = prompt for _ in range(seq_len): logits = self.forward(out[:, -self.seq_len:], **kwargs)[:, -1] filtered_logits = top_k(logits, thres = filter_thres) sample = gumbel_sample(filtered_logits, temperature = temperature) sample = rearrange(sample, 'b -> b 1') out = torch.cat((out, sample), dim = -1) return out[:, t:] @property def device(self): return next(self.parameters()).device def forward( self, ids, return_loss = False, return_hierarchical_token_embeds = False, return_hierarchical_embeds = False, ablate_hierarchical_merge = False, return_random_proj_quantize_ids = False ): """ einops notation: b - batch n - sequence length c - compression factor d - dimension """ # if training, predict next token in sequence if return_loss: ids, labels = ids[:, :-1], ids[:, 1:] # assert seq len assert ids.shape[-1] <= self.seq_len # get token embeddings, and pad to multiple of compression factor x = self.token_emb(ids) # for every hierarchy, compress token embeddings appropriately to the hierarchical embeddings tokens = [] for compress in self.compressors: tokens.append(compress(x)) # save hierarchical tokens right before norm for random projection quantization, if needed post_compressed_tokens = tokens # post embedding norms tokens = apply_fns(self.post_token_emb_norms, tokens) # if one wants all the compressed token embeds # just to investigate the space if return_hierarchical_token_embeds: return tokens # layers for layer, merge in zip_longest(self.layers, self.hierarchical_merges): tokens = apply_fns(layer, tokens) # pool the information all hierarchies # and then update the tokens that will be used to make the final autoregressive prediction if not self.need_hierarchical_merge or ablate_hierarchical_merge: continue pooled = merge(tokens) if self.hierarchy_merge_all: tokens = [(t + p[..., ::s, :]) for t, p, s in zip(tokens, pooled.split(self.dims, dim = -1), self.h_strides)] else: predict_tokens = tokens[self.predict_hierarchy_index] predict_tokens = predict_tokens + pooled tokens[self.predict_hierarchy_index] = predict_tokens # final normalized embeddings embeds = apply_fns(self.norms, tokens) # if the researcher wants the randomly projected ids of either compressed tokens or embeddings of the hierarchies if return_random_proj_quantize_ids: assert self.prophet_loss_use_quantized quantize_input = embeds if self.prophet_quantized_use_embed else post_compressed_tokens hierarchical_ids = apply_fns(self.rand_proj_quantizers, quantize_input) return hierarchical_ids # if one wants all the normalized hierarchical embeds if return_hierarchical_embeds: return embeds # select the hierarchical embeddings that will be doing the predicting if self.predict_use_all_hierarchy: predict_embed = hierarchical_cat(embeds, self.h_strides) else: predict_embed = embeds[self.predict_hierarchy_index] # logits for predicting next token logits = self.to_logits(predict_embed) if not return_loss: return logits ce_loss_fn = partial(F.cross_entropy, ignore_index = self.ignore_index) # autoregressive loss (predictive coding) logits = rearrange(logits, 'b n c -> b c n') ce_loss = ce_loss_fn(logits, labels) # reconstruction losses for hierarchy tokens recon_losses = prophet_losses = torch.zeros((), device = self.device).requires_grad_() if self.should_recon: for compress, t in zip(self.compressors, embeds): recon_loss = compress.recon(t, ids) recon_losses = recon_losses + recon_loss # prophet losses for hierarchy tokens if self.should_prophet: if self.prophet_loss_use_quantized: # using random projected quantizer of the next hierarchical token quantize_input = embeds if self.prophet_quantized_use_embed else post_compressed_tokens hierarchical_ids = apply_fns(self.rand_proj_quantizers, quantize_input) for hierarchy, stride, compress, embed, pred_ids in zip(self.hierarchies, self.h_strides, self.compressors, embeds, hierarchical_ids): if hierarchy == 1: continue prophet_logits = compress.to_prophet(embed) axial_dim = hierarchy // stride prophet_logits = curtail_seq_to_multiple(prophet_logits, axial_dim) pred_ids = curtail_seq_to_multiple(pred_ids, axial_dim) prophet_logits, pred_ids = map(lambda t: rearrange(t, 'b (n c) ... -> (b c) n ...', c = axial_dim), (prophet_logits, pred_ids)) prophet_logits = rearrange(prophet_logits[:, :-1], 'b n (q c) -> (b q) c n', q = self.rq_num_codebooks) pred_ids = rearrange(pred_ids[:, 1:], 'b n q -> (b q) n') prophet_loss = ce_loss_fn(prophet_logits, pred_ids) prophet_losses = prophet_losses + prophet_loss else: # or predicting the next N 1x base token ids # like prophetnet paper for compress, t in zip(self.compressors, embeds): prophet_loss = compress.prophet(t, ids) prophet_losses = prophet_losses + prophet_loss # total loss total_loss = ce_loss + recon_losses * self.recon_loss_weight + prophet_losses * self.prophet_loss_weight return total_loss, (ce_loss, recon_losses, prophet_losses)
zeta-main
zeta/nn/architecture/hierarchical_transformer.py
from zeta.nn.architecture.attn_layers import AttentionLayers class CrossAttender(AttentionLayers): def __init__(self, **kwargs): super().__init__(cross_attend=True, only_cross=True, **kwargs)
zeta-main
zeta/nn/architecture/cross_attender.py
import torch import torch.nn.functional as F from einops import pack, rearrange, unpack from torch import nn from zeta.utils.main import ( # noqa: E402 eval_decorator, exists, once, # noqa: F401 ) from zeta.utils.main import top_a, top_k, top_p class AutoregressiveWrapper(nn.Module): def __init__( self, net, ignore_index = -100, pad_value = 0, mask_prob = 0., speculative = False ): super().__init__() self.pad_value = pad_value self.ignore_index = ignore_index self.net = net self.max_seq_len = net.max_seq_len # paper shows masking (MLM) in conjunction with autoregressive decoder-only training leads to big improvements https://arxiv.org/abs/2210.13432 assert mask_prob < 1. self.mask_prob = mask_prob @torch.no_grad() @eval_decorator def generate( self, start_tokens, seq_len, eos_token = None, temperature = 1., filter_logits_fn = top_k, filter_thres = 0.9, min_p_pow = 2.0, min_p_ratio = 0.02, gamma=5, #number of guesses for speculative decoding **kwargs ): start_tokens, ps = pack([start_tokens], '* n') b, t = start_tokens.shape out = start_tokens if self.speculative: for _ in range(seq_len): x = out[:, -self.max_seq_len] logits = self.net(x, **kwargs)[:, -1] if filter_logits_fn in {top_k, top_p}: filtered_logits = filter_logits_fn(logits, thres=filter_thres) probs = F.softmax(filtered_logits / temperature, dim=-1) elif filter_logits_fn is top_a: filtered_logits = filter_logits_fn(logits, min_p_pow=min_p_pow, min_p_ratio=min_p_ratio) probs = F.softmax(filtered_logits / temperature, dim=-1) #speculative decoding guesses = torch.multinomial(probs, gamma, replacement=True) p_values = [] for guess in guesses: x_prime = torch.cat((x, guess.unsqueeze(0)), dim=1) logits_prime = self.net(x_prime, **kwargs)[:, -1] p_values.append(F.softmax(logits_prime / temperature, dim=-1)) n = gamma for i in range(gamma): ri = torch.rand(1).item() if ri > p_values[i][guesses[i].item()] / probs[guesses[i].item()]: n = i - 1 break p_0 = p_values[n] if n < gamma: q_n = probs[guesses[n].item()] p_0 = F.normalize(torch.clamp(p_0 - q_n, min=0), p=1, dim=0) sample = torch.multinomial(p_0, 1) out = torch.cat((out, sample), dim=-1) if exists(eos_token): is_eos_tokens = (out == eos_token) if is_eos_tokens.any(dim = -1).all(): # mask out everything after the eos tokens shifted_is_eos_tokens = F.pad(is_eos_tokens, (1, -1)) mask = shifted_is_eos_tokens.float().cumsum(dim = -1) >= 1 out = out.masked_fill(mask, self.pad_value) break out = out[:, t:] out, = unpack(out, ps, '* n') return out else: for _ in range(seq_len): x = out[:, -self.max_seq_len:] logits = self.net(x, **kwargs)[:, -1] if filter_logits_fn in {top_k, top_p}: filtered_logits = filter_logits_fn(logits, thres = filter_thres) probs = F.softmax(filtered_logits / temperature, dim=-1) elif filter_logits_fn is top_a: filtered_logits = filter_logits_fn(logits, min_p_pow = min_p_pow, min_p_ratio= min_p_ratio) probs = F.softmax(filtered_logits / temperature, dim=-1) sample = torch.multinomial(probs, 1) out = torch.cat((out, sample), dim=-1) if exists(eos_token): is_eos_tokens = (out == eos_token) if is_eos_tokens.any(dim = -1).all(): # mask out everything after the eos tokens shifted_is_eos_tokens = F.pad(is_eos_tokens, (1, -1)) mask = shifted_is_eos_tokens.float().cumsum(dim = -1) >= 1 out = out.masked_fill(mask, self.pad_value) break out = out[:, t:] out, = unpack(out, ps, '* n') return out def forward(self, x, return_loss=True, **kwargs): seq, ignore_index = x.shape[1], self.ignore_index inp, target = x[:, :-1], x[:, 1:] if self.mask_prob > 0.: rand = torch.randn(inp.shape, device = x.device) rand[:, 0] = -torch.finfo(rand.dtype).max # first token should not be masked out num_mask = min(int(seq * self.mask_prob), seq - 1) indices = rand.topk(num_mask, dim = -1).indices mask = ~torch.zeros_like(inp).scatter(1, indices, 1.).bool() kwargs.update(self_attn_context_mask = mask) logits = self.net(inp, **kwargs) loss = F.cross_entropy( rearrange(logits, 'b n c -> b c n'), target, ignore_index = ignore_index ) if return_loss: return logits, loss return logits
zeta-main
zeta/nn/architecture/auto_regressive_wrapper.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import math import torch import torch.nn as nn from zeta.nn.biases.base import BaseBias class RelativePositionBias(BaseBias): def __init__( self, bidirectional: int = True, num_buckets: int =32, max_distance: int = 128, num_heads: int = 1 ): super().__init__() self.bidirectional = bidirectional self.num_buckets = num_buckets self.max_distance = max_distance self.num_heads = num_heads self.relative_attention_bias = nn.Embedding(self.num_buckets, self.num_heads) @staticmethod def _relative_position_bucket( relative_position, bidirectional=True, num_buckets=32, max_distance=128 ): ret = 0 n = -relative_position if bidirectional: num_buckets //= 2 ret += (n < 0).to(torch.long) * num_buckets n = torch.abs(n) else: n = torch.max(n, torch.zeros_like(n)) max_exact = num_buckets // 2 is_small = n < max_exact val_if_large = max_exact + ( torch.log(n.float() / max_exact) / math.log(max_distance / max_exact) * (num_buckets - max_exact) ).to(torch.long) val_if_large = torch.min( val_if_large, torch.full_like(val_if_large, num_buckets - 1) ) ret += torch.where(is_small, n, val_if_large) return ret def compute_bias(self, qlen, klen, step=None): step = 0 if step is None else step context_position = torch.arange( step, step + qlen, dtype=torch.long, device=self.relative_attention_bias.weight.device, )[:, None] memory_position = torch.arange( klen, dtype=torch.long, device=self.relative_attention_bias.weight.device )[None, :] relative_position = memory_position - context_position # shape (qlen, klen) rp_bucket = self._relative_position_bucket( relative_position, # shape (qlen, klen) bidirectional=self.bidirectional, num_buckets=self.num_buckets, max_distance=self.max_distance, ) rp_bucket = rp_bucket.to(self.relative_attention_bias.weight.device) values = self.relative_attention_bias( rp_bucket ) # shape (qlen, klen, num_heads) values = values.permute([2, 0, 1]).unsqueeze( 0 ) # shape (1, num_heads, qlen, klen) return values def forward(self, batch_size, qlen, klen, step=None): # shape (batch * num_heads, qlen, klen) return ( self.compute_bias(qlen, klen, step) .repeat(batch_size, 1, 1, 1) .view(-1, qlen, klen) )
zeta-main
zeta/nn/biases/relative_position_bias.py
zeta-main
zeta/nn/biases/__init__.py
import torch from torch import nn from einops import rearrange class DynamicPositionBias(nn.Module): def __init__( self, dim, heads ): super().__init__() self.mlp = nn.Sequential( nn.Linear(1, dim), nn.SiLU(), nn.Linear(dim, dim), nn.SiLU(), nn.Linear(dim, heads) ) @property def device(self): return next(self.parameters()).device def forward(self, i, j): device = self.device assert j >= i rel_dist = torch.arange(j, dtype = torch.float, device = device) bias = self.mlp(rearrange(rel_dist, '... -> ... 1')) i_seq = torch.arange(j - i, j, device = device) j_seq = torch.arange(j, device = device) rel_dist_indices = (rearrange(i_seq, 'i -> i 1') - rearrange(j_seq, 'j -> 1 j')).abs() bias = rearrange(bias[rel_dist_indices], 'i j h -> h i j') return bias
zeta-main
zeta/nn/biases/dynamic_position_bias.py
from abc import abstractmethod import torch.nn as nn class BaseBias(nn.Module): @abstractmethod def __init__(self, num_heads): super().__init__() self.num_heads @abstractmethod def forward(self): pass
zeta-main
zeta/nn/biases/base.py
import math import torch from torch import nn, Tensor import torch.nn.functional as F from zeta.nn.biases.base import BaseBias from einops import rearrange ######## Helpers def exists(val): return val is not None def pad_at_dim(t, pad, dim=-1, value=0.): dims_from_right = (- dim - 1) if dim < 0 else (t.ndim - dim - 1) zeros = ((0, 0) * dims_from_right) return F.pad(t, (*zeros, *pad), value=value) class AlibiPositionalBias(BaseBias): def __init__(self, heads, num_heads, **kwargs): super().__init__() self.heads = heads self.num_heads = num_heads slopes = Tensor(self.__get_slopes(heads)) slopes = rearrange(slopes, 'h -> h 1 1') self.register_buffer('slopes', slopes, persistent=False) self.register_buffer('bias', None, persistent=False) def get_bias(self, i, j, device): torch.arange(j - i, j, device=device) j_arange = torch.arange(j, device = device) bias = -torch.abs(rearrange(j_arange, 'j -> 1 1 j')) return bias @staticmethod def _get_slopes(heads): def get_slopes_power_of_2(n): start = (2**(-2**-(math.log2(n)-3))) ratio = start return [start*ratio**i for i in range(n)] if math.log2(heads).is_integer(): return get_slopes_power_of_2(heads) closest_power_of_2 = 2 ** math.floor(math.log2(heads)) return get_slopes_power_of_2(closest_power_of_2) + get_slopes_power_of_2(2 * closest_power_of_2)[0::2][:heads-closest_power_of_2] @property def device(self): return next(self.buffers()).device def forward(self, i, j): h, device = self.num_heads, self.device if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: return self.bias[..., :i, :j] bias = self.get_bias(i, j, device) bias = bias * self._get_slopes num_heads_unalibied = h - bias.shape[0] bias = pad_at_dim(bias, (0, num_heads_unalibied), dim=0) self.register_buffer('bias', bias, persistent=False) return self.bias class LearnedAlibiPositionalBias(AlibiPositionalBias): def __init__(self, heads, num_heads): super().__init__(heads, num_heads) log_slopes = torch.log(self.slopes) self.learned_logslopes = nn.Parameter(log_slopes) def forward(self, i, j): h, device = self.heads, self.device def get_slopes(param): return pad_at_dim(param.exp(), (0, h - param.shape[0]), dim=-2) if exists(self.bias) and self.bias.shape[-1] >= j and self.bias.shape[-2] >= i: bias = self.bias[..., :i, :j] else: bias = self.get_bias(i, j, device) self.register_buffer('bias', bias, persistent=False) slopes = get_slopes(self.learned_logslopes) bias = bias * slopes return bias
zeta-main
zeta/nn/biases/alibi.py
#from lucirains rt-1 from torch import nn from einops import pack, unpack, repeat, reduce, rearrange #helpers def pack_one(x, pattern): return pack([x], pattern) def unpack_one(x, ps, pattern): return unpack(x, ps, pattern)[0] #main class TokenLearner(nn.Module): def __init__( self, *, dim: int = None, ff_mult: int = 2, num_output_tokens: int = 8, num_layers: int = 2 ): super().__init__() inner_dim = dim * ff_mult * num_output_tokens self.num_output_tokens = num_output_tokens self.net = nn.Sequential( nn.Comv2d(dim * num_output_tokens, inner_dim, 1, groups=num_output_tokens), nn.GELU(), nn.Conv2d(inner_dim, num_output_tokens, 1, groups=num_output_tokens), ) def forward(self, x): x, ps = pack_one(x, '* c h w') x = repeat(x, 'b c h w -> b (g c) h w', g=self.num_output_tokens) attn = self.net(x) attn = rearrange(attn, 'b g h w -> b 1 g h w') x = rearrange(x, 'b (g c) h w -> b c g h w', g=self.num_output_tokens) x = reduce(x * attn, 'b c g h w -> b c g', 'mean') x = unpack_one(x, ps, '* c n') return x
zeta-main
zeta/nn/modules/token_learner.py
from torch import nn class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn def forward(self, x): return self.fn(x) + x
zeta-main
zeta/nn/modules/residual.py
import torch.nn as nn class AdaptiveParameterList(nn.ParameterList): """ A container that allows for parameters to adapt their values based on the learning process Example: ``` def adaptation_function(param): return param * 0.9 adaptive_params = AdaptiveParameterList( [nn.Parameter(torch.radnn(10, 10))] ) adaptive_params.adapt(adaptation_func) ```` """ def __init__(self, parameters=None): super(AdaptiveParameterList, self).__init__(parameters) def adapt(self, adaptation_functions): """ adapt the parameters using the provided func Args: adaptatio_function (callable) the function to adapt the parameters """ if not isinstance(adaptation_functions, dict): raise ValueError("adaptation_functions must be a dictionary") for i, param in enumerate(self): if i in adaptation_functions: adaptation_function = adaptation_functions[i] if not callable(adaptation_function): raise ValueError("adaptation_function must be callable") new_param = adaptation_function(param) if not new_param.shape == param.shape: raise ValueError("adaptation_function must return a tensor of the same shape as the input parameter") self[i] = nn.Parameter(new_param)
zeta-main
zeta/nn/modules/adaptive_parameter_list.py
import torch from torch import nn class LN(nn.Module): def __init__(self, dim=None, eps=None ): self.dim = dim self.eps = eps def forward(self): nn.LayerNorm(self.dim, self.eps) def subln(x): return x + LN(x)
zeta-main
zeta/nn/modules/sublayer.py
import torch from torch import nn class DynamicModule(nn.Module): """ A container that allows for dynamic addition, removal, and modification of modules examples ```` dynamic_module = DynamicModule() dynamic_module.add('linear', nn.Linear(10, 10)) dynamic_module.add('relu', nn.ReLU()) output = dynamic_module(torch.randn(1, 10)) dynamic_module.remove('relu') """ def __init__( self, forward_method=None, ): super(DynamicModule, self).__init__() self.module_dict = nn.ModuleDict() self.forward_method = forward_method def add(self, name, module): """ Add a module to the container Args: name (str) the name of the module module(nn.Module) the module to add """ if isinstance(name, list): name = '.'.join(name) if not isinstance(module, nn.Module): raise ValueError("Module must be a nn.Module") if name in self.module_dict: raise ValueError("Module name must be unique") self.module_dict[name] = module def remove(self, name): """ Remove a module from the container Args: name (str) the name of the module to remove """ if isinstance(name, list): name = '.'.join(name, list) if name not in self.module_dict: raise ValueError("module name does not exist") del self.module_dict[name] def forward(self, x): """ Forward pass through the modules Args: x (Tensor) the input tensor Returns: Tensor: the output tensor """ if self.forward_method is not None: return self.forward_method(self.module_dict, x) for module in self.module_dict.values(): x = module(x) return x def save_state(self, path): torch.save(self.state_dict(), path) def load_state(self, path): self.load_state_dict(torch.load(path))
zeta-main
zeta/nn/modules/dynamic_module.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import torch.nn as nn from timm.models.layers import drop_path class DropPath(nn.Module): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).""" def __init__(self, drop_prob=None): super(DropPath, self).__init__() self.drop_prob = drop_prob def forward(self, x): return drop_path(x, self.drop_prob, self.training) def extra_repr(self): return "p={}".format(self.drop_prob)
zeta-main
zeta/nn/modules/droppath.py
# modules from zeta.nn.modules.lora import Lora from zeta.nn.modules.feedforward_network import FeedForwardNetwork from zeta.nn.modules.droppath import DropPath from zeta.nn.modules.token_learner import TokenLearner
zeta-main
zeta/nn/modules/__init__.py
import torch from torch import nn from einops import Reduce, Rearrange class DropSample(nn.Module): def __init__(self, prob=0): super().__init__() self.prob = prob def forward(self, x): device = x.device if self.prob == 0. or (not self.training): return x keep_mask = torch.FloatTensor((x.shape[0], 1, 1, 1), device=device).uniform_() > self.prob return x + keep_mask / (1 - self.prob) class SqueezeExcitation(nn.Module): def __init__( self, dim, shrinkage_rate=0.25 ): super().__init__() hidden_dim = int(dim * shrinkage_rate) self.gate = nn.Sequential( Reduce('b c h w -> b c', 'mean'), nn.Linear( dim, hidden_dim, bias=False ), nn.SiLU(), nn.Linear( hidden_dim, dim, bias=False ), nn.Sigmoid(), Rearrange('b c -> b c 11') ) def forward(self, x): return x + self.gate(x) class MBConvResidual(nn.Module): def __init__( self, fn, dropout=0. ): super().__init__() self.fn = fn self.downsample = DropSample(dropout) def forward(self, x): out = self.fn(x) out = self.dropsample(out) return out + x def MBConv( dim_in, dim_out, *, downsample, expansion_rate=4, shrinkage_rate=0.25, dropout=0. ): hidden_dim = int(expansion_rate * dim_out) stride = 2 if downsample else 1 net = nn.Sequential( nn.Conv2d(dim_in, hidden_dim, 1), nn.BatchNorm2d(hidden_dim), nn.GELU(), nn.Conv2d( hidden_dim, hidden_dim, 3, stride=stride, padding=1, groups=hidden_dim ), nn.BatchNorm2d(hidden_dim), nn.GELU(), SqueezeExcitation( hidden_dim, shrinkage_rate=shrinkage_rate ), nn.Conv2d( hidden_dim, dim_out, 1 ), nn.BatchNorm2d(dim_out) ) if dim_in == dim_out and not downsample: net = MBConvResidual( net, dropout=dropout ) return net
zeta-main
zeta/nn/modules/mbconv.py
import torch from torch import nn class Lora(nn.Module): def __init__( self, dim, dim_out, r=8, alpha=None ): super().__init__() self.scale = alpha / r self.A = nn.Parameter(torch.randn(dim, r)) self.B = nn.Parameter(torch.randn(r, dim_out)) @property def weight(self): return (self.A @ self.B) * self.scale def forward(self, x): return x @ self.weight
zeta-main
zeta/nn/modules/lora.py
import torch import torch.nn.functional as F def token_shift(t): t, t_shift = t.chunk(2, dim=1) t_shift = F.pad(t_shift, (0, 0, 1, -1)) return torch.cat((t, t_shift), dim=-1)
zeta-main
zeta/nn/modules/token_shift.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details] import torch import torch.nn as nn import torch.nn.functional as F try: from apex.normalization import FusedLayerNorm as LayerNorm except ModuleNotFoundError: from torch.nn import LayerNorm from .xmoe.global_groups import get_moe_group class set_torch_seed(object): def __init__(self, seed): assert isinstance(seed, int) self.rng_state = self.get_rng_state() torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) def get_rng_state(self): state = {"torch_rng_state": torch.get_rng_state()} if torch.cuda.is_available(): state["cuda_rng_state"] = torch.cuda.get_rng_state() return state def set_rng_state(self, state): torch.set_rng_state(state["torch_rng_state"]) if torch.cuda.is_available(): torch.cuda.set_rng_state(state["cuda_rng_state"]) def __enter__(self): return self def __exit__(self, *exc): self.set_rng_state(self.rng_state) def make_experts(args, embed_dim, expert_ffn_dim): world_size = ( 1 if not torch.distributed.is_initialized() else torch.distributed.get_world_size() ) expert_list = [] ddp_rank = args.ddp_rank start_seed = torch.randint(1000000, (1,)).item() # at least as many experts than gpus if args.moe_expert_count >= world_size: assert ( args.moe_expert_count % world_size == 0 ), f"{args.moe_expert_count}, {world_size}" local_moe_expert_count = args.moe_expert_count // world_size for i in range(local_moe_expert_count): with set_torch_seed(start_seed + ddp_rank * local_moe_expert_count + i): expert_list.append( FeedForwardNetwork( embed_dim, expert_ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.layernorm_eps, args.subln, ) ) else: assert ( world_size % args.moe_expert_count == 0 ), f"{world_size}, {args.moe_expert_count}" moe_idx, _ = get_moe_group(args.moe_expert_count) with set_torch_seed(start_seed + moe_idx): expert_list.append( FeedForwardNetwork( embed_dim, expert_ffn_dim, args.activation_fn, args.dropout, args.activation_dropout, args.layernorm_eps, args.subln, ) ) experts = nn.ModuleList(expert_list) return experts def get_activation_fn(activation): if activation == "relu": return F.relu elif activation == "gelu": return F.gelu else: raise NotImplementedError class FeedForwardNetwork(nn.Module): def __init__( self, embed_dim, ffn_dim, activation_fn, dropout, activation_dropout, layernorm_eps, subln=False, ): super().__init__() self.embed_dim = embed_dim self.activation_fn = get_activation_fn(activation=str(activation_fn)) self.activation_dropout_module = torch.nn.Dropout(activation_dropout) self.dropout_module = torch.nn.Dropout(dropout) self.fc1 = nn.Linear(self.embed_dim, ffn_dim) self.fc2 = nn.Linear(ffn_dim, self.embed_dim) self.ffn_layernorm = LayerNorm(ffn_dim, eps=layernorm_eps) if subln else None def reset_parameters(self): self.fc1.reset_parameters() self.fc2.reset_parameters() if self.ffn_layernorm is not None: self.ffn_layernorm.reset_parameters() def forward(self, x): x_shape = x.shape x = x.reshape(-1, x.size(-1)) x = self.fc1(x) x = self.activation_fn(x.float()).type_as(x) x = self.activation_dropout_module(x) if self.ffn_layernorm is not None: x = self.ffn_layernorm(x) x = self.fc2(x) x = x.view(x_shape) x = self.dropout_module(x) return x
zeta-main
zeta/nn/modules/feedforward_network.py
import torch from torch import nn import torch.nn.functional as F class RMSNorm(nn.Module): def __init__( self, dim, groups=1 ): super().__init__() self.scale = dim ** -0.5 self.gamma = nn.Parameter(torch.ones(groups, dim, 1)) def forward(self, x): normed = F.normalize(x, dim=-2) return normed * self.scale * self.gamma
zeta-main
zeta/nn/modules/rms_norm.py
import torch.distributed as dist def _find_my_group_index(grouped_ranks): my_rank = dist.get_rank() for i, group in enumerate(grouped_ranks): if my_rank in group: return i raise RuntimeError def get_moe_group(moe_expert_count=None): if dist.is_initialized(): if not hasattr(get_moe_group, "_moe_groups"): world_size = dist.get_world_size() if world_size <= moe_expert_count: assert moe_expert_count % world_size == 0 moe_groups = [[i] for i in range(world_size)] else: assert world_size % moe_expert_count == 0 ranks_per_group = world_size // moe_expert_count moe_groups = [ [i + j * moe_expert_count for j in range(ranks_per_group)] for i in range(moe_expert_count) ] get_moe_group._moe_expert_count = moe_expert_count get_moe_group._moe_group_idx = moe_groups get_moe_group._moe_groups = [dist.new_group(g) for g in moe_groups] my_group_idx = _find_my_group_index(get_moe_group._moe_group_idx) return my_group_idx, get_moe_group._moe_groups[my_group_idx] def get_all2all_group(moe_expert_count): if dist.is_initialized(): if not hasattr(get_all2all_group, "_all2all_groups"): world_size = dist.get_world_size() # more experts than world size if world_size <= moe_expert_count: assert moe_expert_count % world_size == 0 all2all_groups = [[i for i in range(world_size)]] # larger world than num experts else: assert world_size % moe_expert_count == 0 ranks_per_group = world_size // moe_expert_count all2all_groups = [ [i * moe_expert_count + j for j in range(moe_expert_count)] for i in range(ranks_per_group) ] get_all2all_group._all2all_group_idx = all2all_groups get_all2all_group._all2all_groups = [ dist.new_group(g) for g in all2all_groups ] my_group_idx = _find_my_group_index(get_all2all_group._all2all_group_idx) return get_all2all_group._all2all_groups[my_group_idx]
zeta-main
zeta/nn/modules/xmoe/global_groups.py
# Copyright (c) 2022 Agora # Licensed under The MIT License [see LICENSE for details]
zeta-main
zeta/nn/modules/xmoe/__init__.py