python_code
stringlengths
0
992k
repo_name
stringlengths
8
46
file_path
stringlengths
5
162
import os from pathlib import Path current_dir = Path(__file__).parent.absolute() import pytest import torch import dotenv from src.datamodules.language_modeling_hf import LMDataModule # load environment variables from `.env` file if it exists # recursively searches for `.env` in all folders starting from work dir dotenv.load_dotenv(override=True) def div_up(x: int, y: int) -> int: return (x + y - 1) // y # https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python/55423170#55423170 def num_cpu_cores(): try: import psutil return psutil.cpu_count(logical=False) except ImportError: return len(os.sched_getaffinity(0)) class TestLMDataModule: def test_wikitext2(self): batch_size = 7 dataset_name = 'wikitext' dataset_config_name = 'wikitext-2-raw-v1' data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'wikitext-2' / 'cache' max_length = 1024 datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=False, batch_size=batch_size, num_workers=4) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 2391884 val_len = 247289 test_len = 283287 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1]) def test_wikitext103(self): batch_size = 7 dataset_name = 'wikitext' dataset_config_name = 'wikitext-103-raw-v1' data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'wikitext-103' / 'cache' max_length = 1024 datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=False, batch_size=batch_size, num_workers=4) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 117920140 val_len = 247289 test_len = 283287 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1]) def test_openwebtext(self): batch_size = 8 dataset_name = 'openwebtext' dataset_config_name = None data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'openwebtext' / 'cache' max_length = 1024 datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=True, batch_size=batch_size, num_workers=num_cpu_cores() // 2) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 9035582198 val_len = 4434897 test_len = 4434897 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1]) def test_lambada(self): batch_size = 8 dataset_name = 'lambada' dataset_config_name = None data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'lambada' / 'cache' max_length = 1024 datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=True, batch_size=batch_size, num_workers=64) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 9035582198 val_len = 4434897 test_len = 4434897 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1]) def test_the_pile(self): batch_size = 8 dataset_name = 'the_pile' dataset_config_name = None data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'the_pile' / 'cache' max_length = 2048 # Dataset is too large to fit into memory, need to use disk for concatenation datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=True, batch_size=batch_size, num_workers=num_cpu_cores() // 2, use_shmem=False) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 374337375694 val_len = 383326395 test_len = 373297018 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1]) def test_pg19(self): batch_size = 8 dataset_name = 'pg19' dataset_config_name = None data_dir = Path(os.getenv('DATA_DIR', current_dir.parent.parent / 'data')) cache_dir = data_dir / 'pg19' / 'cache' max_length = 2048 # Dataset is too large to fit into memory, need to use disk for concatenation datamodule = LMDataModule(dataset_name, tokenizer_name='gpt2', dataset_config_name=dataset_config_name, max_length=max_length, cache_dir=cache_dir, add_eos=True, batch_size=batch_size, num_workers=num_cpu_cores() // 2) datamodule.prepare_data() datamodule.setup(stage='fit') train_loader = datamodule.train_dataloader() val_loader = datamodule.val_dataloader() datamodule.setup(stage='test') test_loader = datamodule.test_dataloader() train_len = 3066544128 val_len = 4653056 test_len = 10584064 assert len(train_loader) == div_up((train_len - 1) // max_length, batch_size) assert len(val_loader) == div_up((val_len - 1) // max_length, batch_size) assert len(test_loader) == div_up((test_len - 1) // max_length, batch_size) for loader in [train_loader, val_loader, test_loader]: x, y = next(iter(loader)) assert x.dim() == 2 assert x.shape == (batch_size, max_length) assert x.dtype == torch.long assert torch.allclose(x[:, 1:], y[:, :-1])
EXA-1-master
exa/modular_components/attentions/flash-attention/training/tests/datamodules/test_language_modeling_hf.py
from typing import List, Optional, Sequence from pathlib import Path import hydra from omegaconf import OmegaConf, DictConfig from pytorch_lightning import ( Callback, LightningDataModule, LightningModule, Trainer, seed_everything, ) from pytorch_lightning.loggers import LightningLoggerBase from src.utils import utils log = utils.get_logger(__name__) def last_modification_time(path): """Including files / directory 1-level below the path """ path = Path(path) if path.is_file(): return path.stat().st_mtime elif path.is_dir(): return max(child.stat().st_mtime for child in path.iterdir()) else: return None def train(config: DictConfig) -> Optional[float]: """Contains training pipeline. Instantiates all PyTorch Lightning objects from config. Args: config (DictConfig): Configuration composed by Hydra. Returns: Optional[float]: Metric score for hyperparameter optimization. """ # Set seed for random number generators in pytorch, numpy and python.random if config.get("seed"): seed_everything(config.seed, workers=True) # We want to add fields to config so need to call OmegaConf.set_struct OmegaConf.set_struct(config, False) # Init lightning model model: LightningModule = hydra.utils.instantiate(config.task, cfg=config, _recursive_=False) datamodule: LightningDataModule = model._datamodule # Init lightning callbacks callbacks: List[Callback] = [] if "callbacks" in config: for _, cb_conf in config.callbacks.items(): if cb_conf is not None and "_target_" in cb_conf: log.info(f"Instantiating callback <{cb_conf._target_}>") callbacks.append(hydra.utils.instantiate(cb_conf)) # Init lightning loggers logger: List[LightningLoggerBase] = [] if "logger" in config: for _, lg_conf in config.logger.items(): if lg_conf is not None and "_target_" in lg_conf: log.info(f"Instantiating logger <{lg_conf._target_}>") logger.append(hydra.utils.instantiate(lg_conf)) ckpt_cfg = {} if config.get('resume'): try: checkpoint_path = Path(config.callbacks.model_checkpoint.dirpath) if checkpoint_path.is_dir(): last_ckpt = checkpoint_path / 'last.ckpt' autosave_ckpt = checkpoint_path / '.pl_auto_save.ckpt' if not (last_ckpt.exists() or autosave_ckpt.exists()): raise FileNotFoundError("Resume requires either last.ckpt or .pl_autosave.ckpt") if ((not last_ckpt.exists()) or (autosave_ckpt.exists() and last_modification_time(autosave_ckpt) > last_modification_time(last_ckpt))): # autosave_ckpt = autosave_ckpt.replace(autosave_ckpt.with_name('.pl_auto_save_loaded.ckpt')) checkpoint_path = autosave_ckpt else: checkpoint_path = last_ckpt # DeepSpeed's checkpoint is a directory, not a file if checkpoint_path.is_file() or checkpoint_path.is_dir(): ckpt_cfg = {'ckpt_path': str(checkpoint_path)} else: log.info(f'Checkpoint file {str(checkpoint_path)} not found. Will start training from scratch') except (KeyError, FileNotFoundError): pass # Configure ddp automatically n_devices = config.trainer.get('devices', 1) if isinstance(n_devices, Sequence): # trainer.devices could be [1, 3] for example n_devices = len(n_devices) if n_devices > 1 and config.trainer.get('strategy', None) is None: config.trainer.strategy = dict( _target_='pytorch_lightning.strategies.DDPStrategy', find_unused_parameters=False, gradient_as_bucket_view=True, # https://pytorch-lightning.readthedocs.io/en/stable/advanced/advanced_gpu.html#ddp-optimizations ) # Init lightning trainer log.info(f"Instantiating trainer <{config.trainer._target_}>") trainer: Trainer = hydra.utils.instantiate( config.trainer, callbacks=callbacks, logger=logger) # Train the model log.info("Starting training!") trainer.fit(model=model, datamodule=datamodule, **ckpt_cfg) # Evaluate model on test set, using the best model achieved during training if config.get("test_after_training") and not config.trainer.get("fast_dev_run"): log.info("Starting testing!") trainer.test(model=model, datamodule=datamodule) # Make sure everything closed properly log.info("Finalizing!") utils.finish( config=config, model=model, datamodule=datamodule, trainer=trainer, callbacks=callbacks, logger=logger, ) # Print path to best checkpoint if not config.trainer.get("fast_dev_run"): log.info(f"Best model ckpt: {trainer.checkpoint_callback.best_model_path}") # Return metric score for hyperparameter optimization optimized_metric = config.get("optimized_metric") if optimized_metric: return trainer.callback_metrics[optimized_metric]
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/train.py
from typing import List, Optional from pathlib import Path import torch import hydra from omegaconf import OmegaConf, DictConfig from pytorch_lightning import ( Callback, LightningDataModule, LightningModule, Trainer, seed_everything, ) from pytorch_lightning.loggers import LightningLoggerBase from src.utils import utils log = utils.get_logger(__name__) def remove_prefix(text: str, prefix: str): if text.startswith(prefix): return text[len(prefix) :] return text # or whatever def load_checkpoint(path, device='cpu'): path = Path(path).expanduser() if path.is_dir(): path /= 'last.ckpt' # dst = f'cuda:{torch.cuda.current_device()}' log.info(f'Loading checkpoint from {str(path)}') state_dict = torch.load(path, map_location=device) # T2T-ViT checkpoint is nested in the key 'state_dict_ema' if state_dict.keys() == {'state_dict_ema'}: state_dict = state_dict['state_dict_ema'] # Swin checkpoint is nested in the key 'model' if state_dict.keys() == {'model'}: state_dict = state_dict['model'] # Lightning checkpoint contains extra stuff, we only want the model state dict if 'pytorch-lightning_version' in state_dict: state_dict = {remove_prefix(k, 'model.'): v for k, v in state_dict['state_dict'].items()} return state_dict def evaluate(config: DictConfig) -> None: """Example of inference with trained model. It loads trained image classification model from checkpoint. Then it loads example image and predicts its label. """ # load model from checkpoint # model __init__ parameters will be loaded from ckpt automatically # you can also pass some parameter explicitly to override it # We want to add fields to config so need to call OmegaConf.set_struct OmegaConf.set_struct(config, False) # load model checkpoint_type = config.eval.get('checkpoint_type', 'pytorch') if checkpoint_type not in ['lightning', 'pytorch']: raise NotImplementedError(f'checkpoint_type ${checkpoint_type} not supported') if checkpoint_type == 'lightning': cls = hydra.utils.get_class(config.task._target_) model = cls.load_from_checkpoint(checkpoint_path=config.eval.ckpt) elif checkpoint_type == 'pytorch': model_cfg = config.model_pretrained if 'model_pretrained' in config else None trained_model: LightningModule = hydra.utils.instantiate(config.task, cfg=config, model_cfg=model_cfg, _recursive_=False) if 'ckpt' in config.eval: load_return = trained_model.model.load_state_dict( load_checkpoint(config.eval.ckpt, device=trained_model.device), strict=False ) log.info(load_return) if 'model_pretrained' in config: ... else: model = trained_model datamodule: LightningDataModule = hydra.utils.instantiate(config.datamodule) # datamodule: LightningDataModule = model._datamodule datamodule.prepare_data() datamodule.setup() # print model hyperparameters log.info(f'Model hyperparameters: {model.hparams}') # Init Lightning callbacks callbacks: List[Callback] = [] if "callbacks" in config: for _, cb_conf in config["callbacks"].items(): if cb_conf is not None and "_target_" in cb_conf: log.info(f"Instantiating callback <{cb_conf._target_}>") callbacks.append(hydra.utils.instantiate(cb_conf)) # Init Lightning loggers logger: List[LightningLoggerBase] = [] if "logger" in config: for _, lg_conf in config["logger"].items(): if lg_conf is not None and "_target_" in lg_conf: log.info(f"Instantiating logger <{lg_conf._target_}>") logger.append(hydra.utils.instantiate(lg_conf)) # Init Lightning trainer log.info(f"Instantiating trainer <{config.trainer._target_}>") trainer: Trainer = hydra.utils.instantiate( config.trainer, callbacks=callbacks, logger=logger, _convert_="partial" ) # Evaluate the model log.info("Starting evaluation!") if config.eval.get('run_val', True): trainer.validate(model=model, datamodule=datamodule) if config.eval.get('run_test', True): trainer.test(model=model, datamodule=datamodule) # Make sure everything closed properly log.info("Finalizing!") utils.finish( config=config, model=model, datamodule=datamodule, trainer=trainer, callbacks=callbacks, logger=logger, )
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/eval.py
from typing import Any, Dict, Optional import torch from torch import Tensor from torchmetrics import Metric class NumTokens(Metric): """Keep track of how many tokens we've seen. """ # TODO: how do we prevent the reset between the epochs? The reset happens on the 1st batch # of the next epoch. # Right now the hack is that we override reset(), which would mess up the forward method. # We then override forward to do the right thing. is_differentiable = False higher_is_better = False full_state_update = False count: Tensor def __init__(self, **kwargs: Dict[str, Any]): super().__init__(**kwargs) self.add_state("count", default=torch.tensor(0, dtype=torch.int64), dist_reduce_fx="sum", persistent=True) # We want the count to be saved to state-dict def update(self, preds: Tensor, target: Tensor, loss: Optional[Tensor] = None) -> None: # type: ignore self.count += target.numel() def compute(self) -> Tensor: return self.count def reset(self): count = self.count super().reset() self.count = count # Adapted from https://github.com/Lightning-AI/metrics/blob/master/src/torchmetrics/metric.py def _forward_reduce_state_update(self, *args: Any, **kwargs: Any) -> Any: """forward computation using single call to `update` to calculate the metric value on the current batch and accumulate global state. This can be done when the global metric state is a sinple reduction of batch states. """ self.update(*args, **kwargs) return self.compute()
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/metrics/num_tokens.py
# Inspired by https://github.com/NVIDIA/NeMo/blob/main/nemo/collections/common/metrics/perplexity.py # But we compute the perplexity correctly: exp(average(nll)), not average(exp(nll)) # Also adapted from https://github.com/Lightning-AI/metrics/blob/master/src/torchmetrics/text/perplexity.py # But we pass in the loss to avoid recomputation from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import Tensor from torchmetrics import Metric try: from flash_attn.losses.cross_entropy import CrossEntropyLoss except ImportError: CrossEntropyLoss = torch.nn.CrossEntropyLoss __all__ = ['Perplexity'] class Perplexity(Metric): r""" Perplexity measures how well a language model predicts a text sample. It's calculated as the average number of bits per word a model needs to represent the sample. Args: kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info. Examples: >>> import torch >>> preds = torch.rand(2, 8, 5, generator=torch.manual_seed(22)) >>> target = torch.randint(5, (2, 8), generator=torch.manual_seed(22)) >>> target[0, 6:] = -100 >>> metric = Perplexity(ignore_index=-100) >>> metric(preds, target) tensor(5.2545) """ is_differentiable = True higher_is_better = False full_state_update = False total_log_probs: Tensor count: Tensor def __init__(self, **kwargs: Dict[str, Any]): super().__init__(**kwargs) self.add_state("total_log_probs", default=torch.tensor(0.0, dtype=torch.float64), dist_reduce_fx="sum") self.add_state("count", default=torch.tensor(0, dtype=torch.int64), dist_reduce_fx="sum") self.loss_fn = CrossEntropyLoss() def update(self, preds: Tensor, target: Tensor, loss: Optional[Tensor] = None) -> None: # type: ignore """Compute and store intermediate statistics for Perplexity. Args: preds: Probabilities assigned to each token in a sequence with shape [batch_size, seq_len, vocab_size]. target: Ground truth values with a shape [batch_size, seq_len]. """ count = target.numel() if loss is None: loss = self.loss_fn(preds, target) self.total_log_probs += loss.double() * count self.count += count def compute(self) -> Tensor: """Compute the Perplexity. Returns: Perplexity """ return torch.exp(self.total_log_probs / self.count)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/metrics/perplexity.py
import torch from torch import Tensor from torchmetrics import Metric, Accuracy class AccuracyMine(Accuracy): """Wrap torchmetrics.Accuracy to take argmax of y in case of Mixup. """ def update(self, preds: Tensor, target: Tensor) -> None: # type: ignore super().update(preds, target.argmax(dim=-1) if target.is_floating_point() else target)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/metrics/accuracy.py
from typing import Any, List import inspect import torch import hydra from pytorch_lightning import LightningModule, LightningDataModule from torchmetrics import MetricCollection from einops import rearrange from omegaconf import OmegaConf from src.utils.utils import get_logger from src.optim.param_grouping import group_parameters_for_optimizer from src.utils.checkpoint import load_checkpoint logger = get_logger(__name__) class SequenceModel(LightningModule): def __init__(self, cfg, model_cfg=None): """If model_cfg is passed, it will take precedence over cfg.model """ super().__init__() # this line ensures params passed to LightningModule will be saved to ckpt # it also allows to access params with 'self.hparams' attribute self.save_hyperparameters(cfg) self.cfg = cfg self.model_cfg = model_cfg or self.cfg.model self.instantiate_datamodule() self.instantiate_model() self.warmstart() self.instantiate_loss() self.instantiate_metrics() def instantiate_datamodule(self): logger.info(f"Instantiating datamodule <{self.cfg.datamodule._target_}>") # Calling this self.datamodule will mess with PL since it also assigns self.datamodule self._datamodule: LightningDataModule = hydra.utils.instantiate(self.cfg.datamodule) self._datamodule.prepare_data() self._datamodule.setup() OmegaConf.clear_resolver('datamodule') OmegaConf.register_new_resolver('datamodule', lambda attr: getattr(self._datamodule, attr)) def instantiate_model(self): # if hasattr(self._datamodule, 'num_classes'): # self.model_cfg.num_classes = self._datamodule.num_classes # if (hasattr(self._datamodule, 'vocab_size') # and self.model_cfg.get('embedding_cfg', None) is not None # and self.model_cfg.embedding_cfg._target_ == "torch.nn.Embedding"): # self.model_cfg.embedding_cfg.num_embeddings = self._datamodule.vocab_size logger.info(f"Instantiating model <{self.model_cfg._target_}>") recursive = getattr(self.model_cfg, '_recursive_', False) self.model = hydra.utils.instantiate(self.model_cfg, _recursive_=recursive) def instantiate_loss(self): loss_fn_cfg = self.cfg.train.get('loss_fn') if loss_fn_cfg is None: loss_fn_cfg = {'_target_': 'torch.nn.CrossEntropyLoss'} self.loss_fn = hydra.utils.instantiate(loss_fn_cfg) loss_fn_val_cfg = self.cfg.train.get('loss_fn_val', loss_fn_cfg) self.loss_fn_val = hydra.utils.instantiate(loss_fn_val_cfg) def instantiate_metrics(self): # use separate metric instance for train, val and test step # to ensure a proper reduction over the epoch if 'eval' in self.cfg and 'metrics' in self.cfg.eval: metrics_cfg = self.cfg.eval.metrics else: metrics_cfg = {'acc': {'_target_': 'torchmetrics.Accuracy'}} metrics = MetricCollection({name: hydra.utils.instantiate(cfg) for name, cfg in metrics_cfg.items()}) self.train_metrics = metrics.clone(prefix='train/') self.val_metrics = metrics.clone(prefix='val/') self.test_metrics = metrics.clone(prefix='test/') def warmstart(self): if self.cfg.train.get('warmstart', None) is not None: logger.info(f"Warm-starting with weights from {self.cfg.train.warmstart.path}") strict = self.cfg.train.warmstart.get('strict', True) state_dict = load_checkpoint(self.cfg.train.warmstart.path) if self.cfg.train.warmstart.get('post_process', None) is not None: state_dict = hydra.utils.instantiate(self.cfg.train.warmstart.post_process, state_dict) load_return = self.model.load_state_dict(state_dict, strict=False) logger.info(load_return) def forward(self, *args, **kwargs): return self.model(*args, **kwargs) def step(self, batch: Any, is_train=True): try: x, y, lengths = batch except ValueError: x, y = batch lengths = None output = self.forward(x) if lengths is None else self.forward(x, lengths=lengths) loss = self.loss_fn(output, y) if is_train else self.loss_fn_val(output, y) return loss, output, y def shared_step(self, batch: Any, batch_idx: int, phase='train'): loss, output, targets = self.step(batch, is_train=(phase == 'train')) metrics = getattr(self, f'{phase}_metrics') metrics(output, targets) log_on_step = 'eval' in self.cfg and self.cfg.eval.get('log_on_step', False) and phase == 'train' self.log(f"{phase}/loss", loss, on_step=log_on_step, on_epoch=True, prog_bar=False, sync_dist=True) # https://pytorch-lightning.readthedocs.io/en/stable/visualize/logging_advanced.html#enable-metrics-for-distributed-training # We need to log the Metrics object, not the metric result, since otherwise # pytorch-lightning will use torch.mean to reduce it. # This would be wrong for perplexity, for example. self.log_dict(metrics, on_step=log_on_step, on_epoch=True, prog_bar=True, sync_dist=True) return {"loss": loss, "output": output, "targets": targets} def training_step(self, batch: Any, batch_idx: int): return self.shared_step(batch, batch_idx, phase='train') def validation_step(self, batch: Any, batch_idx: int): return self.shared_step(batch, batch_idx, phase='val') def test_step(self, batch: Any, batch_idx: int): return self.shared_step(batch, batch_idx, phase='test') def configure_optimizers(self): if 'optimizer_param_grouping' in self.cfg.train: # Set zero weight decay for some params parameters = group_parameters_for_optimizer(self.model, self.cfg.train.optimizer, **self.cfg.train.optimizer_param_grouping) else: # parameters = self.model.parameters() parameters = self.parameters() # [21-09-08] AG: this will train task specific parameters such as Retrieval head for AAN optimizer = hydra.utils.instantiate(self.cfg.train.optimizer, parameters) # Log optimizer info for i, g in enumerate(optimizer.param_groups): ntensors = len(g['params']) nparams = sum(p.numel() for p in g['params']) hparams = {k: v for k, v in g.items() if k != 'params'} logger.info(f'Optimizer group {i}: {ntensors} tensors, {nparams} parameters, {hparams}') if 'scheduler' not in self.cfg.train: return optimizer else: # lr_scheduler should be called either every step (default) or every epoch lr_scheduler = hydra.utils.instantiate(self.cfg.train.scheduler, optimizer) return [optimizer], {'scheduler': lr_scheduler, 'interval': self.cfg.train.get('scheduler_interval', 'step'), 'monitor': self.cfg.train.get('scheduler_monitor', 'val/loss')} def optimizer_zero_grad(self, epoch, batch_idx, optimizer, optimizer_idx): # https://pytorch-lightning.readthedocs.io/en/latest/guides/speed.html#set-grads-to-none # TD [2022-04-30]: DeepSpeed optimizer uses the kwarg set_grad_to_none instead of set_to_none if 'set_to_none' in inspect.signature(optimizer.zero_grad).parameters: optimizer.zero_grad(set_to_none=True) else: optimizer.zero_grad() def on_save_checkpoint(self, checkpoint): # TD [2022-08-07] ['epoch_loop.batch_progress']['total']['completed'] is 1 iteration # behind, so we're using the optimizer's progress. checkpoint['loops']['fit_loop']['epoch_loop.batch_progress']['total']['completed'] = checkpoint['loops']['fit_loop']['epoch_loop.batch_loop.optimizer_loop.optim_progress']['optimizer']['step']['total']['completed'] * self.trainer.accumulate_grad_batches checkpoint['loops']['fit_loop']['epoch_loop.batch_progress']['current']['completed'] = checkpoint['loops']['fit_loop']['epoch_loop.batch_loop.optimizer_loop.optim_progress']['optimizer']['step']['current']['completed'] * self.trainer.accumulate_grad_batches # _batches_that_stepped tracks the number of global steps, not the number # of local steps, so we don't multiply with self.trainer.accumulate_grad_batches here. checkpoint['loops']['fit_loop']['epoch_loop.state_dict']['_batches_that_stepped'] = checkpoint['loops']['fit_loop']['epoch_loop.batch_loop.optimizer_loop.optim_progress']['optimizer']['step']['total']['completed'] class SequenceLMModel(SequenceModel): def step(self, batch: Any, is_train=True): x, y = batch output = self.forward(x).logits output = rearrange(output, '... C -> (...) C') y = rearrange(y, '... -> (...)') loss = self.loss_fn(output, y) if is_train else self.loss_fn_val(output, y) return loss, output, y def shared_step(self, batch: Any, batch_idx: int, phase='train'): loss, output, targets = self.step(batch, is_train=(phase == 'train')) # Passing the loss to the perplexity metrics to avoid recomputation metrics = getattr(self, f'{phase}_metrics') metrics(output, targets, loss=loss) log_on_step = 'eval' in self.cfg and self.cfg.eval.get('log_on_step', False) and phase == 'train' self.log(f"{phase}/loss", loss, on_step=log_on_step, on_epoch=True, prog_bar=False, sync_dist=True) # https://pytorch-lightning.readthedocs.io/en/stable/visualize/logging_advanced.html#enable-metrics-for-distributed-training # We need to log the Metrics object, not the metric result, since otherwise # pytorch-lightning will use torch.mean to reduce it. # This would be wrong for perplexity, for example. self.log_dict(metrics, on_step=log_on_step, on_epoch=True, prog_bar=True, sync_dist=True) return {"loss": loss, "output": output, "targets": targets}
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/tasks/seq.py
# Adapted from https://pytorch.org/docs/stable/_modules/torch/distributed/algorithms/ddp_comm_hooks/default_hooks.html # We divide by world_size first before converting to fp16, so it's safer. from typing import Any, Callable import torch import torch.distributed as dist def fp16_compress_hook( process_group: dist.ProcessGroup, bucket: dist.GradBucket ) -> torch.futures.Future[torch.Tensor]: """ This DDP communication hook implements a simple gradient compression approach that casts ``GradBucket`` tensor to half-precision floating-point format (``torch.float16``) and then divides it by the process group size. It allreduces those ``float16`` gradient tensors. Once compressed gradient tensors are allreduced, the chained callback ``decompress`` casts it back to the input data type (such as ``float32``). Example:: >>> ddp_model.register_comm_hook(process_group, fp16_compress_hook) """ group_to_use = process_group if process_group is not None else dist.group.WORLD world_size = group_to_use.size() # Divide first before converting to fp16 # Use out argument to fuse the division and the conversion. compressed_tensor = torch.div(bucket.buffer(), world_size, out=torch.empty_like(bucket.buffer(), dtype=torch.float16)) fut = dist.all_reduce( compressed_tensor, group=group_to_use, async_op=True ).get_future() def decompress(fut): decompressed_tensor = bucket.buffer() # Decompress in place to reduce the peak memory. # See: https://github.com/pytorch/pytorch/issues/45968 decompressed_tensor.copy_(fut.value()[0]) return decompressed_tensor # TODO: maybe have a backoff strategy: check if the buffer has inf / NaN, in that case # resend with fp32? return fut.then(decompress)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/distributed/ddp_comm_hooks.py
# Adapted from https://github.com/rwightman/pytorch-image-models/blob/master/benchmark.py from typing import Any, List, Sequence import torch from pytorch_lightning import Callback, Trainer, LightningModule from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.utilities.parsing import AttributeDict from src.utils.flops import has_deepspeed_profiling, has_fvcore_profiling from src.utils.flops import profile_deepspeed, profile_fvcore class FlopCount(Callback): """Counter the number of FLOPs used by the model """ def __init__(self, profilers: List[str] = ['fvcore', 'deepspeed'], input_size: tuple = (3, 224, 224), input_dtype=torch.float32, device=None): if not isinstance(profilers, Sequence): profilers = [profilers] if any(p not in ['fvcore', 'deepspeed'] for p in profilers): raise NotImplementedError('Only support fvcore and deepspeed profilers') if 'fvcore' in profilers and not has_fvcore_profiling: raise ImportError('fvcore is not installed. Install it by running `pip install fvcore`') elif 'deepspeed' in profilers and not has_deepspeed_profiling: raise ImportError('deepspeed is not installed') super().__init__() self.profilers = profilers self.input_size = tuple(input_size) self.input_dtype = input_dtype self.device = device @rank_zero_only def on_fit_start(self, trainer: Trainer, pl_module: LightningModule) -> None: if 'fvcore' in self.profilers: _, macs, _, acts = profile_fvcore(pl_module.to(self.device), input_size=self.input_size, input_dtype=self.input_dtype, detailed=True) trainer.logger.log_hyperparams({'GMACs': macs * 1e-9, 'MActs': acts * 1e-6}) if 'deepspeed' in self.profilers: macs, _= profile_deepspeed(pl_module.to(self.device), input_size=self.input_size, input_dtype=self.input_dtype, detailed=True) if 'fvcore' not in self.profilers: # fvcore's MACs seem more accurate trainer.logger.log_hyperparams({'GMACs': macs * 1e-9})
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/flop_count.py
import subprocess from pathlib import Path from typing import List import matplotlib.pyplot as plt import seaborn as sn import torch import wandb from pytorch_lightning import Callback, Trainer from pytorch_lightning.loggers import LoggerCollection, WandbLogger from pytorch_lightning.utilities import rank_zero_only from sklearn import metrics from sklearn.metrics import f1_score, precision_score, recall_score def get_wandb_logger(trainer: Trainer) -> WandbLogger: """Safely get Weights&Biases logger from Trainer.""" if trainer.fast_dev_run: raise Exception( "Cannot use wandb callbacks since pytorch lightning disables loggers in `fast_dev_run=true` mode." ) if isinstance(trainer.logger, WandbLogger): return trainer.logger if isinstance(trainer.logger, LoggerCollection): for logger in trainer.logger: if isinstance(logger, WandbLogger): return logger raise Exception( "You are using wandb related callback, but WandbLogger was not found for some reason..." ) class WatchModel(Callback): """Make wandb watch model at the beginning of the run.""" def __init__(self, log: str = "gradients", log_freq: int = 100): self.log = log self.log_freq = log_freq @rank_zero_only def on_train_start(self, trainer, pl_module): logger = get_wandb_logger(trainer=trainer) logger.watch(model=trainer.model, log=self.log, log_freq=self.log_freq) class UploadCodeAsArtifact(Callback): """Upload all code files to wandb as an artifact, at the beginning of the run.""" def __init__(self, code_dir: str, use_git: bool = True): """ Args: code_dir: the code directory use_git: if using git, then upload all files that are not ignored by git. if not using git, then upload all '*.py' file """ self.code_dir = code_dir self.use_git = use_git @rank_zero_only def on_train_start(self, trainer, pl_module): logger = get_wandb_logger(trainer=trainer) experiment = logger.experiment code = wandb.Artifact("project-source", type="code") if self.use_git: # get .git folder # https://alexwlchan.net/2020/11/a-python-function-to-ignore-a-path-with-git-info-exclude/ git_dir_path = Path( subprocess.check_output(["git", "rev-parse", "--git-dir"]).strip().decode("utf8") ).resolve() for path in Path(self.code_dir).resolve().rglob("*"): if ( path.is_file() # ignore files in .git and not str(path).startswith(str(git_dir_path)) # noqa: W503 # ignore files ignored by git and ( # noqa: W503 subprocess.run(["git", "check-ignore", "-q", str(path)]).returncode == 1 ) ): code.add_file(str(path), name=str(path.relative_to(self.code_dir))) else: for path in Path(self.code_dir).resolve().rglob("*.py"): code.add_file(str(path), name=str(path.relative_to(self.code_dir))) experiment.log_artifact(code) class UploadCheckpointsAsArtifact(Callback): """Upload checkpoints to wandb as an artifact, at the end of run.""" def __init__(self, ckpt_dir: str = "checkpoints/", upload_best_only: bool = False): self.ckpt_dir = ckpt_dir self.upload_best_only = upload_best_only @rank_zero_only def on_keyboard_interrupt(self, trainer, pl_module): self.on_train_end(trainer, pl_module) @rank_zero_only def on_train_end(self, trainer, pl_module): logger = get_wandb_logger(trainer=trainer) experiment = logger.experiment ckpts = wandb.Artifact("experiment-ckpts", type="checkpoints") if self.upload_best_only: ckpts.add_file(trainer.checkpoint_callback.best_model_path) else: for path in Path(self.ckpt_dir).rglob("*.ckpt"): ckpts.add_file(str(path)) experiment.log_artifact(ckpts) class LogConfusionMatrix(Callback): """Generate confusion matrix every epoch and send it to wandb. Expects validation step to return predictions and targets. """ def __init__(self): self.preds = [] self.targets = [] self.ready = True def on_sanity_check_start(self, trainer, pl_module) -> None: self.ready = False def on_sanity_check_end(self, trainer, pl_module): """Start executing this callback only after all validation sanity checks end.""" self.ready = True def on_validation_batch_end( self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx ): """Gather data from single batch.""" if self.ready: self.preds.append(outputs["preds"]) self.targets.append(outputs["targets"]) def on_validation_epoch_end(self, trainer, pl_module): """Generate confusion matrix.""" if self.ready: logger = get_wandb_logger(trainer) experiment = logger.experiment preds = torch.cat(self.preds).cpu().numpy() targets = torch.cat(self.targets).cpu().numpy() confusion_matrix = metrics.confusion_matrix(y_true=targets, y_pred=preds) # set figure size plt.figure(figsize=(14, 8)) # set labels size sn.set(font_scale=1.4) # set font size sn.heatmap(confusion_matrix, annot=True, annot_kws={"size": 8}, fmt="g") # names should be uniqe or else charts from different experiments in wandb will overlap experiment.log({f"confusion_matrix/{experiment.name}": wandb.Image(plt)}, commit=False) # according to wandb docs this should also work but it crashes # experiment.log(f{"confusion_matrix/{experiment.name}": plt}) # reset plot plt.clf() self.preds.clear() self.targets.clear() class LogF1PrecRecHeatmap(Callback): """Generate f1, precision, recall heatmap every epoch and send it to wandb. Expects validation step to return predictions and targets. """ def __init__(self, class_names: List[str] = None): self.preds = [] self.targets = [] self.ready = True def on_sanity_check_start(self, trainer, pl_module): self.ready = False def on_sanity_check_end(self, trainer, pl_module): """Start executing this callback only after all validation sanity checks end.""" self.ready = True def on_validation_batch_end( self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx ): """Gather data from single batch.""" if self.ready: self.preds.append(outputs["preds"]) self.targets.append(outputs["targets"]) def on_validation_epoch_end(self, trainer, pl_module): """Generate f1, precision and recall heatmap.""" if self.ready: logger = get_wandb_logger(trainer=trainer) experiment = logger.experiment preds = torch.cat(self.preds).cpu().numpy() targets = torch.cat(self.targets).cpu().numpy() f1 = f1_score(targets, preds, average=None) r = recall_score(targets, preds, average=None) p = precision_score(targets, preds, average=None) data = [f1, p, r] # set figure size plt.figure(figsize=(14, 3)) # set labels size sn.set(font_scale=1.2) # set font size sn.heatmap( data, annot=True, annot_kws={"size": 10}, fmt=".3f", yticklabels=["F1", "Precision", "Recall"], ) # names should be uniqe or else charts from different experiments in wandb will overlap experiment.log({f"f1_p_r_heatmap/{experiment.name}": wandb.Image(plt)}, commit=False) # reset plot plt.clf() self.preds.clear() self.targets.clear() class LogImagePredictions(Callback): """Logs a validation batch and their predictions to wandb. Example adapted from: https://wandb.ai/wandb/wandb-lightning/reports/Image-Classification-using-PyTorch-Lightning--VmlldzoyODk1NzY """ def __init__(self, num_samples: int = 8): super().__init__() self.num_samples = num_samples self.ready = True def on_sanity_check_start(self, trainer, pl_module): self.ready = False def on_sanity_check_end(self, trainer, pl_module): """Start executing this callback only after all validation sanity checks end.""" self.ready = True def on_validation_epoch_end(self, trainer, pl_module): if self.ready: logger = get_wandb_logger(trainer=trainer) experiment = logger.experiment # get a validation batch from the validation dat loader val_samples = next(iter(trainer.datamodule.val_dataloader())) val_imgs, val_labels = val_samples # run the batch through the network val_imgs = val_imgs.to(device=pl_module.device) logits = pl_module(val_imgs) preds = torch.argmax(logits, dim=-1) # log the images as wandb Image experiment.log( { f"Images/{experiment.name}": [ wandb.Image(x, caption=f"Pred:{pred}, Label:{y}") for x, pred, y in zip( val_imgs[: self.num_samples], preds[: self.num_samples], val_labels[: self.num_samples], ) ] } )
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/wandb_callbacks.py
import torch from pytorch_lightning import Callback, Trainer, LightningModule import logging log = logging.getLogger(__name__) # We want a logger for each process, not just the rank 0 def l2_promote(): import ctypes _libcudart = ctypes.CDLL('libcudart.so') # Set device limit on the current device # cudaLimitMaxL2FetchGranularity = 0x05 pValue = ctypes.cast((ctypes.c_int*1)(), ctypes.POINTER(ctypes.c_int)) _libcudart.cudaDeviceSetLimit(ctypes.c_int(0x05), ctypes.c_int(128)) _libcudart.cudaDeviceGetLimit(pValue, ctypes.c_int(0x05)) assert pValue.contents.value == 128 def set_affinity(trainer): try: from src.utils.gpu_affinity import set_affinity nproc_per_node = torch.cuda.device_count() affinity = set_affinity(trainer.local_rank, nproc_per_node, 'socket_unique_continuous') log.info(f'{trainer.local_rank}: thread affinity: {affinity}') # TD [2022-05-07] Somehow calling this causes GPU 0 to allocate extra ~800MB of memory per # number of GPUs (e.g., 6.4GB of extra memory in a 8-GPU setup). H/t Dan. # l2_promote() except: pass class GpuAffinity(Callback): """Set GPU affinity and increase the L2 fetch granularity. Adapted from https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/LanguageModeling/Transformer-XL """ def setup(self, trainer: Trainer, pl_module: LightningModule, stage=None) -> None: set_affinity(trainer)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/gpu_affinity.py
# Inspired by https://github.com/Lightning-AI/lightning/blob/master/src/pytorch_lightning/utilities/grads.py # However, they compute grad at every iteration (I think), and the .item() calls incur a lot of overhead # (6-7% slow down on GPT-2 small). Instead we only compute for iterations where we need to log, and don't # call .item() explicitly. from typing import Any from collections import OrderedDict from pytorch_lightning import Callback, Trainer from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.strategies import DeepSpeedStrategy import torch import torch.nn as nn try: from apex.contrib.layer_norm import FastLayerNorm except ImportError: FastLayerNorm = None class NormMonitor(Callback): """Monitor the scales of weights and gradients. """ def __init__(self, layer_norm_only: bool = False): super().__init__() self.layer_norm_only = layer_norm_only # Use on_before_optimizer_step instead of on_train_batch_start since there might be # gradient accumulation and we only care about scale when it could change (i.e., optimizer.step). @rank_zero_only def on_before_optimizer_step(self, trainer: Trainer, pl_module, *args: Any, **kwargs: Any) -> None: if not trainer._logger_connector.should_update_logs: return model = pl_module.model named_parameters = {} if self.layer_norm_only: ln_modules = (nn.LayerNorm, nn.Embedding) if FastLayerNorm is not None: ln_modules += (FastLayerNorm,) for mn, m in model.named_modules(): if isinstance(m, ln_modules): for pn, p in m.named_parameters(): fpn = '%s.%s' % (mn, pn) if mn else pn # full param name named_parameters[fpn] = p else: named_parameters = dict(model.named_parameters()) if isinstance(trainer.strategy, DeepSpeedStrategy): loss_scale = trainer.model.optimizer.loss_scale else: loss_scale = 1.0 stats = {} param_l1_norm, grad_l1_norm = [], [] for param_name, param in named_parameters.items(): param_abs = param.abs() param_abs_mean = param_abs.mean(dtype=torch.float32) stats[f'stats/{param_name}_max'] = param_abs.max() stats[f'stats/{param_name}_mean'] = param_abs_mean param_l1_norm.append(param_abs_mean * param.numel()) if param.grad is not None: # If using AMP, gradient is already unscaled by the AMP loss scaler at this point # https://github.com/Lightning-AI/lightning/pull/9606 # However, if using DeepSpeed, we need to scale it ourselves param_grad_abs = param.grad.abs() param_grad_abs_mean = param_grad_abs.mean(dtype=torch.float32) / loss_scale stats[f'stats/{param_name}_grad_max'] = param_grad_abs.max() / loss_scale stats[f'stats/{param_name}_grad_mean'] = param_grad_abs_mean grad_l1_norm.append(param_grad_abs_mean * param.grad.numel()) stats['total_param_l1_norm'] = torch.stack(param_l1_norm).sum() if grad_l1_norm: stats['total_grad_l1_norm'] = torch.stack(grad_l1_norm).sum() # Sort by params name stats = OrderedDict(sorted(stats.items())) if trainer.loggers is not None: for logger in trainer.loggers: logger.log_metrics(stats, step=trainer.fit_loop.epoch_loop._batches_that_stepped)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/norm_monitor.py
import pytorch_lightning as pl from pytorch_lightning import Callback from pytorch_lightning.utilities import rank_zero_only import torch from torch.autograd import grad class CausalityMonitor(Callback): r"""Monitor causality of a model by tracking gradient leakage forward in time. In a fully causal model, dy[k]du[s] ~= 0 for all k < s. Args: seq_len (int): Length of the sequence to monitor. input_dim (int): Dimension of the input to monitor. If 0, the callback assumes the task to be language modeling, and skips the embedding layer. If > 0, input_dim is interpreted as the input channel dimension, i.e. D with dummy input of dimension [B, L, D]. Notes: This callback assumes that `pl_module.model` has a `net` or `s4seq` attribute, indicating the primary model to monitor. For LMs, `net` or `s4seq` should be after the embedding layer. """ def __init__(self, seq_len: int = 10, input_dim: int = 0): super().__init__() self.seq_len = seq_len self.input_dim = input_dim @rank_zero_only def on_train_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: model = pl_module.model with torch.enable_grad(): if self.input_dim == 0: # [MP] LongTensors cannot have gradients - we start from post # embedding in the LM case input_dim = model.d_model x = torch.randn((2, self.seq_len, input_dim), \ requires_grad=True).to(pl_module.device) # [DF] HACK: we need to get the layer that comes after the embedding if hasattr(model, 'net'): y = model.net(x) else: y = model.s4seq(x) else: x = torch.randn(1, self.seq_len, self.input_dim, \ requires_grad=True).to(pl_module.device) y = model(x) stats = {} for i in range(self.seq_len): # total gradients flowing from y_i to x g = grad(y[0,0,i].mean(), x, retain_graph=True, allow_unused=True)[0] g = g[0,i+1:,:].abs().mean() stats[f'stats/causality_{i}'] = g.item() if trainer.loggers is not None: for logger in trainer.loggers: logger.log_metrics(stats, step=trainer.global_step)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/causality_monitor.py
# Inspired by https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/callbacks/stochastic_weight_avg.py # https://github.com/PyTorchLightning/Lightning-Bolts/blob/master/pl_bolts/callbacks/byol_updates.py # https://forums.pytorchlightning.ai/t/adopting-exponential-moving-average-ema-for-pl-pipeline/488/2 # https://github.com/PyTorchLightning/pytorch-lightning/issues/8100 from typing import Dict, Any from pytorch_lightning import Callback, Trainer from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.utilities.parsing import AttributeDict from pytorch_lightning.utilities.types import STEP_OUTPUT from src.utils.ema import ExponentialMovingAverage class EMACallback(Callback): """TD [2021-08-31]: saving and loading from checkpoint should work. """ def __init__(self, decay: float, use_num_updates: bool = True): """ decay: The exponential decay. use_num_updates: Whether to use number of updates when computing averages. """ super().__init__() self.decay = decay self.use_num_updates = use_num_updates self.ema = None def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"): # It's possible that we already loaded EMA from the checkpoint if self.ema is None: self.ema = ExponentialMovingAverage([p for p in pl_module.parameters() if p.requires_grad], decay=self.decay, use_num_updates=self.use_num_updates) # Ideally we want on_after_optimizer_step but pytorch-lightning doesn't have it # We only want to update when parameters are changing. # Because of gradient accumulation, this doesn't happen every training step. # https://github.com/PyTorchLightning/pytorch-lightning/issues/11688 def on_train_batch_end( self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", outputs: STEP_OUTPUT, batch: Any, batch_idx: int, ) -> None: if (batch_idx + 1) % trainer.accumulate_grad_batches == 0: self.ema.update() def on_validation_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: # During the initial validation we don't have self.ema yet if self.ema is not None: self.ema.store() self.ema.copy_to() def on_validation_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: if self.ema is not None: self.ema.restore() def on_test_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: if self.ema is not None: self.ema.store() self.ema.copy_to() def on_test_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: if self.ema is not None: self.ema.restore() def on_save_checkpoint( self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", checkpoint: Dict[str, Any] ) -> Dict[str, Any]: return self.ema.state_dict() def on_load_checkpoint( self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", checkpoint: Dict[str, Any] ) -> None: if self.ema is None: self.ema = ExponentialMovingAverage([p for p in pl_module.parameters() if p.requires_grad], decay=self.decay, use_num_updates=self.use_num_updates) self.ema.load_state_dict(checkpoint)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/ema.py
# Adapted from https://pytorch-lightning.readthedocs.io/en/latest/_modules/pytorch_lightning/callbacks/gpu_stats_monitor.html#GPUStatsMonitor # We only need the speed monitoring, not the GPU monitoring import time from typing import Any from pytorch_lightning import Callback, Trainer from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.utilities.parsing import AttributeDict from pytorch_lightning.utilities.types import STEP_OUTPUT class SpeedMonitor(Callback): """Monitor the speed of each step and each epoch. """ def __init__(self, intra_step_time: bool = True, inter_step_time: bool = True, epoch_time: bool = True, verbose=False): super().__init__() self._log_stats = AttributeDict( { 'intra_step_time': intra_step_time, 'inter_step_time': inter_step_time, 'epoch_time': epoch_time, } ) self.verbose = verbose def on_train_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: self._snap_epoch_time = None def on_train_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: self._snap_intra_step_time = None self._snap_inter_step_time = None self._snap_epoch_time = time.time() def on_validation_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: self._snap_inter_step_time = None def on_test_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: self._snap_inter_step_time = None @rank_zero_only def on_train_batch_start( self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", batch: Any, batch_idx: int, ) -> None: if self._log_stats.intra_step_time: self._snap_intra_step_time = time.time() if not trainer._logger_connector.should_update_logs: return logs = {} if self._log_stats.inter_step_time and self._snap_inter_step_time: # First log at beginning of second step logs["time/inter_step (ms)"] = (time.time() - self._snap_inter_step_time) * 1000 if trainer.logger is not None: trainer.logger.log_metrics(logs, step=trainer.global_step) @rank_zero_only def on_train_batch_end( self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", outputs: STEP_OUTPUT, batch: Any, batch_idx: int, ) -> None: if self._log_stats.inter_step_time: self._snap_inter_step_time = time.time() if self.verbose and self._log_stats.intra_step_time and self._snap_intra_step_time: pl_module.print(f"time/intra_step (ms): {(time.time() - self._snap_intra_step_time) * 1000}") if not trainer._logger_connector.should_update_logs: return logs = {} if self._log_stats.intra_step_time and self._snap_intra_step_time: logs["time/intra_step (ms)"] = (time.time() - self._snap_intra_step_time) * 1000 if trainer.logger is not None: trainer.logger.log_metrics(logs, step=trainer.global_step) @rank_zero_only def on_train_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule",) -> None: logs = {} if self._log_stats.epoch_time and self._snap_epoch_time: logs["time/epoch (s)"] = time.time() - self._snap_epoch_time if trainer.logger is not None: trainer.logger.log_metrics(logs, step=trainer.global_step)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/speed_monitor.py
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/__init__.py
# Adapted from https://github.com/Lightning-AI/lightning/blob/master/src/pytorch_lightning/callbacks/fault_tolerance.py from typing import Any from pathlib import Path import pytorch_lightning as pl class ModelCheckpointMine(pl.callbacks.model_checkpoint.ModelCheckpoint): def __init__(self, *args, fault_tolerant=False, **kwargs): super().__init__(*args, **kwargs) self.fault_tolerant = fault_tolerant def on_exception(self, trainer: "pl.Trainer", *_: Any, **__: Any) -> None: if self.fault_tolerant: # overwrite if necessary trainer.save_checkpoint(str(Path(self.dirpath) / '.pl_auto_save.ckpt')) # def teardown(self, trainer: "pl.Trainer", *_: Any, **__: Any) -> None: # if self.fault_tolerant: # trainer.strategy.remove_checkpoint(str(Path(self.dirpath) / '.pl_auto_save.ckpt')) # TD [2022-07-17] I was trying to make resuming from standard checkpoint fault-tolerant. # However, when it resumes it's off by 1 iteration. My attempt to fix it in seq.py (below) didn't work. # So I decided to just copy _FaultToleranceCheckpoint and just save on_exception. # def on_save_checkpoint(self, checkpoint): # # TD [2022-07-12] The "completed" counter is off by 1 so when it resumes # # it's off by 1 iteration. However, the data is still off by 1 iteration, probably # # because the dataloader_state_dict['counter'] is off by @batch_size, and idk how # # to fix it cleanly. # checkpoint['loops']['fit_loop']['epoch_loop.batch_progress']['total']['completed'] += 1 # checkpoint['loops']['fit_loop']['epoch_loop.batch_progress']['current']['completed'] += 1 # checkpoint['loops']['fit_loop']['epoch_loop.state_dict']['_batches_that_stepped'] += 1 # checkpoint['loops']['fit_loop']['epoch_loop.state_dict']['dataloader_state_dict'][0]['state'][0]['num_batches_fetched'] += 1
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/model_checkpoint.py
from typing import Any from pytorch_lightning import Callback, Trainer, LightningModule from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.utilities.parsing import AttributeDict class ParamsLog(Callback): """Log the number of parameters of the model """ def __init__(self, total_params_log: bool = True, trainable_params_log: bool = True, non_trainable_params_log: bool = True): super().__init__() self._log_stats = AttributeDict( { 'total_params_log': total_params_log, 'trainable_params_log': trainable_params_log, 'non_trainable_params_log': non_trainable_params_log, } ) @rank_zero_only def on_fit_start(self, trainer: Trainer, pl_module: LightningModule) -> None: logs = {} if self._log_stats.total_params_log: logs["model/params_total"] = sum(p.numel() for p in pl_module.parameters()) if self._log_stats.trainable_params_log: logs["model/params_trainable"] = sum(p.numel() for p in pl_module.parameters() if p.requires_grad) if self._log_stats.non_trainable_params_log: logs["model/params_not_trainable"] = sum(p.numel() for p in pl_module.parameters() if not p.requires_grad) if trainer.logger is not None: trainer.logger.log_hyperparams(logs)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/params_log.py
# Adapted from https://github.com/Lightning-AI/lightning/blob/master/src/pytorch_lightning/callbacks/lr_monitor.py. from typing import Any from pytorch_lightning import Callback, Trainer from pytorch_lightning.utilities import rank_zero_only from pytorch_lightning.strategies import DeepSpeedStrategy class LossScaleMonitor(Callback): """Monitor the loss scale for AMP (fp16). """ # Use on_before_optimizer_step instead of on_train_batch_start since there might be # gradient accumulation and we only care about the loss scale when it could change (i.e., # optimizer.step). @rank_zero_only def on_before_optimizer_step(self, trainer: Trainer, *args: Any, **kwargs: Any) -> None: if not trainer._logger_connector.should_update_logs: return stats = {} if isinstance(trainer.strategy, DeepSpeedStrategy): stats = {'scalar/scale': trainer.model.optimizer.loss_scale} if hasattr(trainer, 'precision_plugin') and hasattr(trainer.precision_plugin, 'scaler'): scaler = trainer.precision_plugin.scaler if scaler is not None: stats = { 'scaler/scale': scaler.get_scale(), 'scaler/growth_tracker': scaler._get_growth_tracker(), } if stats and trainer.loggers is not None: for logger in trainer.loggers: logger.log_metrics(stats, step=trainer.fit_loop.epoch_loop._batches_that_stepped)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/callbacks/loss_scale_monitor.py
# Adapted from https://github.com/huggingface/transformers/blob/master/examples/pytorch/language-modeling/run_clm.py from itertools import chain from pathlib import Path import pickle from typing import Any, List, Union import subprocess import mmap from multiprocessing.shared_memory import SharedMemory import numpy as np import torch from torch.utils.data.dataloader import DataLoader, Dataset from transformers import AutoTokenizer from datasets import load_dataset from pytorch_lightning import LightningDataModule from src.datamodules.datasets.lm_dataset import LMDataset from src.datamodules.fault_tolerant_sampler import RandomFaultTolerantSampler from src.datamodules.fault_tolerant_sampler import FaultTolerantDistributedSampler from src.datamodules.datasets.detokenizer import DATASET_TOKENIZATION_REGISTRY from src.utils.utils import get_logger logger = get_logger() # https://github.com/numpy/numpy/issues/18294 class SHMArray(np.ndarray): #copied from https://numpy.org/doc/stable/user/basics.subclassing.html#slightly-more-realistic-example-attribute-added-to-existing-array def __new__(cls, input_array, shm=None): obj = np.asarray(input_array).view(cls) obj.shm = shm return obj def __array_finalize__(self, obj): if obj is None: return self.shm = getattr(obj, 'shm', None) class LMDataModule(LightningDataModule): def __init__(self, dataset_name, tokenizer_name, dataset_config_name=None, max_length=1024, cache_dir=None, val_ratio=0.0005, val_split_seed=2357, add_eos=True, detokenize=False, val_only=False, batch_size=32, batch_size_eval=None, num_workers=1, shuffle=False, pin_memory=False, drop_last=False, fault_tolerant=False, ddp=False, fast_forward_epochs=None, fast_forward_batches=None, use_shmem=True): super().__init__() self.dataset_name = dataset_name self.dataset_config_name = dataset_config_name self.tokenizer_name = tokenizer_name self.cache_dir = None if cache_dir is None else Path(cache_dir).expanduser() self.max_length = max_length self.val_ratio = val_ratio self.val_split_seed = val_split_seed self.val_only = val_only self.add_eos = add_eos self.detokenize = detokenize self.batch_size = batch_size self.batch_size_eval = batch_size_eval if batch_size_eval is not None else self.batch_size self.num_workers = num_workers self.shuffle = shuffle self.pin_memory = pin_memory self.drop_last = drop_last if fault_tolerant: assert self.shuffle self.fault_tolerant = fault_tolerant if ddp: assert fault_tolerant self.ddp = ddp self.fast_forward_epochs = fast_forward_epochs self.fast_forward_batches = fast_forward_batches if self.fast_forward_epochs is not None or self.fast_forward_batches is not None: assert ddp and fault_tolerant self.use_shmem = use_shmem if self.use_shmem: assert cache_dir is not None def prepare_data(self): if self.cache_dir is None: # Just download the dataset load_dataset(self.dataset_name, self.dataset_config_name) else: # Process the dataset and save it self.process_dataset() def setup(self, stage=None): if stage == 'test' and hasattr(self, 'dataset_test'): return concat_ids, self.tokenizer = self.process_dataset() self.vocab_size = len(self.tokenizer) # Create all splits self.dataset_train, self.dataset_val, self.dataset_test = [ LMDataset(concat_ids[split], seq_len=self.max_length) for split in ['train', 'validation', 'test'] ] def process_dataset(self): cache_dir = None if self.cache_dir is None else self.cache_dir / self._cache_dir_name if cache_dir is not None: if cache_dir.is_dir(): return self._load_from_cache(cache_dir) raw_datasets = load_dataset(self.dataset_name, self.dataset_config_name) # https://github.com/stanford-crfm/mistral/blob/main/src/corpora/auto.py if 'validation' not in raw_datasets: assert "train" in raw_datasets, "You must have train in raw_datasets to make a validation raw_datasets" raw_datasets = raw_datasets["train"].train_test_split( test_size=self.val_ratio, seed=self.val_split_seed, shuffle=True # Otherwise test will be at the end of the dataset ) raw_datasets['validation'] = raw_datasets['test'] if self.val_only: # Should only be used for evaluation, not for training raw_datasets['train'] = raw_datasets['validation'] # [2021-12-25] TD: Running the detokenizer on wikitext-103 makes ppl worse # (GPT2-small val ppl after 10 epochs ~22 -> ~25) # However, it's useful for zero-shot transfer from Openwebtext, # as after detokenization it's closer to Openwebtext's format. # https://github.com/stanford-crfm/mistral/issues/12 if self.detokenize: if self.dataset_name in DATASET_TOKENIZATION_REGISTRY: detokenizer = DATASET_TOKENIZATION_REGISTRY[self.dataset_name] raw_datasets = raw_datasets.map( lambda example: {'text': detokenizer(example['text'])}, num_proc=max(self.num_workers, 1), desc='Running detokenizer on dataset' ) tokenizer = AutoTokenizer.from_pretrained(self.tokenizer_name, use_fast=True) # Preprocessing the datasets. # First we tokenize all the texts. column_names = raw_datasets["train"].column_names text_column_name = "text" if "text" in column_names else column_names[0] # [2021-12-25] TD: For wikitext, don't need to add the EOS since each example already ends # with '\n', and there are no other '\n' in the examples. # assert all([t.count('\n') == 1 for t in raw_datasets['train']['text'] if t]) # Add EOS token to the end of the text if the text is not empty # https://github.com/stanford-crfm/mistral/issues/91 # https://github.com/stanford-crfm/mistral/pull/98 if self.add_eos: add_eos = lambda seq: (seq + tokenizer.eos_token) if seq else seq add_eos_batched = lambda seqs: [add_eos(seq) for seq in seqs] tokenize = lambda example: tokenizer(add_eos_batched(example[text_column_name])) else: tokenize = lambda example: tokenizer(example[text_column_name]) # tokenized_datasets = raw_datasets.map( # tokenize, # batched=True, # num_proc=max(self.num_workers, 1), # remove_columns=column_names, # desc="Running tokenizer on dataset", # ) dtype = np.uint16 if tokenizer.vocab_size < 64 * 1024 else np.int32 def tokenize_concat(examples): # We just need 'input_ids', not 'attention_mask' (since it's all 1) input_ids = np.fromiter(chain(*tokenize(examples)['input_ids']), dtype=dtype) # Need to return a list since we're doing batched processing return {'input_ids': [input_ids], 'len': [len(input_ids)]} tokenized_datasets = raw_datasets.map( tokenize_concat, batched=True, num_proc=max(self.num_workers, 1), remove_columns=column_names, desc="Running tokenizer on dataset", ) if self.use_shmem: # Concatenate all input_ids into an array in shared memory def write_ids_to_shm(example, shm_name, array_len): shm = SharedMemory(name=shm_name) shm_arr = np.ndarray((array_len,), dtype=dtype, buffer=shm.buf) start_idx = example['len_offset'] - len(example['input_ids']) shm_arr[start_idx:example['len_offset']] = example['input_ids'] shm.close() concat_ids = {} for name, ds in tokenized_datasets.items(): tokenized_datasets[name] = ds.add_column('len_offset', np.cumsum(ds['len'])) array_len = tokenized_datasets[name][-1]['len_offset'] shm = SharedMemory(create=True, size=array_len * np.dtype(dtype).itemsize) shm_name = shm.name tokenized_datasets[name].map( write_ids_to_shm, fn_kwargs={'shm_name': shm_name, 'array_len': array_len}, batched=False, num_proc=max(self.num_workers, 1), desc="Concatenating examples", ) shm_arr = np.ndarray((array_len,), dtype=dtype, buffer=shm.buf) # We need to keep a reference to the shared memory, otherwise it gets garbage-collected # when it goes out of scope, and that memory is gone. # https://github.com/numpy/numpy/issues/18294 concat_ids[name] = SHMArray(shm_arr, shm=shm) else: # Use disk concat_ids = {} assert cache_dir is not None cache_dir.mkdir(parents=True, exist_ok=True) def write_ids_to_disk(example, filename): with open(filename, 'r+b') as f: mm = mmap.mmap(f.fileno(), 0) start_idx = example['len_offset'] - len(example['input_ids']) array_len = len(example['input_ids']) arr = np.ndarray((array_len,), dtype=dtype, buffer=mm, offset=np.dtype(dtype).itemsize * start_idx) arr[:] = example['input_ids'] mm.flush() for name, ds in tokenized_datasets.items(): tokenized_datasets[name] = ds.add_column('len_offset', np.cumsum(ds['len'])) array_len = tokenized_datasets[name][-1]['len_offset'] filename = cache_dir / f'{name}.bin' # Need to create the file with this specific size first # https://ostechnix.com/create-files-certain-size-linux/ subprocess.run(['truncate', '-s', str(array_len * np.dtype(dtype).itemsize), str(filename)], check=True) tokenized_datasets[name].map( write_ids_to_disk, fn_kwargs={'filename': filename}, batched=False, num_proc=max(self.num_workers, 1), desc="Concatenating examples", ) concat_ids[name] = np.memmap(filename, dtype=dtype, mode='r', shape=(array_len,)) if cache_dir is not None: self._save_to_cache(concat_ids, tokenizer, cache_dir) if not self.use_shmem: for name in concat_ids: Path(cache_dir / f'{name}.bin').unlink() return concat_ids, tokenizer def _save_to_cache(self, concat_ids, tokenizer, cache_dir): cache_dir.mkdir(parents=True, exist_ok=True) logger.info(f'Saving to cache at {str(cache_dir)}') for k, v in concat_ids.items(): np.save(cache_dir / f'{k}.npy', v) with open(cache_dir / 'tokenizer.pkl', 'wb') as f: pickle.dump(tokenizer, f) def _load_from_cache(self, cache_dir): assert cache_dir.is_dir() logger.info(f'Load from cache at {str(cache_dir)}') concat_ids = {split: np.load(cache_dir / f'{split}.npy', mmap_mode='r') for split in ['train', 'validation', 'test']} with open(cache_dir / 'tokenizer.pkl', 'rb') as f: tokenizer = pickle.load(f) return concat_ids, tokenizer @property def _cache_dir_name(self): return f'tokenizer_name-{self.tokenizer_name}-val_ratio-{self.val_ratio}-val_split_seed-{self.val_split_seed}-add_eos-{self.add_eos}-detokenize-{self.detokenize}' def train_dataloader(self, *args: Any, **kwargs: Any) -> DataLoader: """ The train dataloader """ if self.shuffle and self.fault_tolerant: shuffle = False sampler = (FaultTolerantDistributedSampler(self.dataset_train) if self.ddp else RandomFaultTolerantSampler(self.dataset_train)) # TD [2022-08-06]: Only the DDP sampler supports fast-forwarding for now # We assume that it's being resumed with the same number of GPUs if self.ddp and self.fast_forward_epochs is not None and self.fast_forward_batches is not None: sampler.load_state_dict({ 'epoch': self.fast_forward_epochs, 'counter': self.fast_forward_batches * self.batch_size }) else: shuffle = self.shuffle sampler = None return self._data_loader(self.dataset_train, batch_size=self.batch_size, shuffle=shuffle, sampler=sampler) def val_dataloader(self, *args: Any, **kwargs: Any) -> Union[DataLoader, List[DataLoader]]: """ The val dataloader """ return self._data_loader(self.dataset_val, batch_size=self.batch_size_eval) def test_dataloader(self, *args: Any, **kwargs: Any) -> Union[DataLoader, List[DataLoader]]: """ The test dataloader """ return self._data_loader(self.dataset_test, batch_size=self.batch_size_eval) def _data_loader(self, dataset: Dataset, batch_size: int, shuffle: bool = False, sampler=None) -> DataLoader: return DataLoader( dataset, batch_size=batch_size, num_workers=1, # Data is already in memory, we don't need many workers shuffle=shuffle, sampler=sampler, drop_last=self.drop_last, pin_memory=self.pin_memory, # persistent_workers=True ) def load_state_dict(self, checkpoint): if self.fault_tolerant: self.fast_forward_epochs = checkpoint['loops']['fit_loop']['epoch_progress']['current']['completed'] # TD [2022-08-07] ['epoch_loop.batch_progress']['total']['completed'] is 1 iteration # behind, so we're using the optimizer's progress. This is set correctly in seq.py. self.fast_forward_batches = checkpoint['loops']['fit_loop']['epoch_loop.batch_progress']['current']['completed'] # At this point the train loader hasn't been constructed yet
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/language_modeling_hf.py
# Adapted from https://github.com/PyTorchLightning/lightning-bolts/blob/master/pl_bolts/datamodules/imagenet_datamodule.py import os from pathlib import Path from typing import Any, List, Union, Callable, Optional import torch from torch.utils.data import Dataset, DataLoader, SequentialSampler from torch.utils.data.dataloader import default_collate from torch.utils.data.distributed import DistributedSampler from pytorch_lightning import LightningDataModule from torchvision import transforms from torchvision.datasets import ImageFolder class DictDataset(Dataset): def __init__(self, dataset_dict, length=None): """dataset_dict: dictionary mapping from index to batch length is used in the case of DistributedSampler: e.g. the dataset could have size 1k, but with 8 GPUs the dataset_dict would only have 125 items. """ super().__init__() self.dataset_dict = dataset_dict self.length = length or len(self.dataset_dict) def __getitem__(self, index): return self.dataset_dict[index] def __len__(self): return self.length # From https://github.com/PyTorchLightning/lightning-bolts/blob/2415b49a2b405693cd499e09162c89f807abbdc4/pl_bolts/transforms/dataset_normalizations.py#L10 def imagenet_normalization(): return transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) class ImagenetDataModule(LightningDataModule): """ .. figure:: https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2017/08/ Sample-of-Images-from-the-ImageNet-Dataset-used-in-the-ILSVRC-Challenge.png :width: 400 :alt: Imagenet Specs: - 1000 classes - Each image is (3 x varies x varies) (here we default to 3 x 224 x 224) Imagenet train, val and test dataloaders. The train set is the imagenet train. The val set is taken from the train set with `num_imgs_per_val_class` images per class. For example if `num_imgs_per_val_class=2` then there will be 2,000 images in the validation set. The test set is the official imagenet validation set. Example:: from pl_bolts.datamodules import ImagenetDataModule dm = ImagenetDataModule(IMAGENET_PATH) model = LitModel() Trainer().fit(model, datamodule=dm) """ name = "imagenet" def __init__( self, data_dir: str, image_size: int = 224, train_transforms=None, val_transforms=None, test_transforms=None, img_dtype='float32', # Using str since OmegaConf doesn't support non-primitive type cache_val_dataset=False, mixup: Optional[Callable] = None, num_aug_repeats: int = 0, num_workers: int = 0, batch_size: int = 32, batch_size_eval: Optional[int] = None, shuffle: bool = True, pin_memory: bool = True, drop_last: bool = False, *args: Any, **kwargs: Any, ) -> None: """ Args: data_dir: path to the imagenet dataset file num_imgs_per_val_class: how many images per class for the validation set image_size: final image size num_workers: how many data workers batch_size: batch_size shuffle: If true shuffles the data every epoch pin_memory: If true, the data loader will copy Tensors into CUDA pinned memory before returning them drop_last: If true drops the last incomplete batch """ super().__init__(*args, **kwargs) self.image_size = image_size self.train_transforms = train_transforms self.val_transforms = val_transforms self.test_transforms = test_transforms assert img_dtype in ['float32', 'float16', 'bfloat16'] self.img_dtype = torch.__getattribute__(img_dtype) self.cache_val_dataset = cache_val_dataset self.mixup = mixup self.num_aug_repeats = num_aug_repeats self.dims = (3, self.image_size, self.image_size) self.data_dir = Path(data_dir).expanduser() self.num_workers = num_workers self.batch_size = batch_size self.batch_size_eval = batch_size_eval if batch_size_eval is not None else self.batch_size self.shuffle = shuffle self.pin_memory = pin_memory self.drop_last = drop_last @property def num_classes(self) -> int: """ Return: 1000 """ return 1000 def _verify_splits(self, data_dir: str, split: str) -> None: dirs = os.listdir(data_dir) if split not in dirs: raise FileNotFoundError( f"a {split} Imagenet split was not found in {data_dir}," f" make sure the folder contains a subfolder named {split}" ) def prepare_data(self) -> None: """This method already assumes you have imagenet2012 downloaded. It validates the data using the meta.bin. .. warning:: Please download imagenet on your own first. """ self._verify_splits(self.data_dir, "train") self._verify_splits(self.data_dir, "val") def setup(self, stage: Optional[str] = None) -> None: """Creates train, val, and test dataset.""" if stage == "fit" or stage is None: train_transforms = (self.train_transform() if self.train_transforms is None else self.train_transforms) val_transforms = (self.val_transform() if self.val_transforms is None else self.val_transforms) if self.img_dtype is not torch.float32: assert isinstance(train_transforms, transforms.Compose) assert isinstance(val_transforms, transforms.Compose) convert_dtype = transforms.Lambda(lambda x: x.to(dtype=self.img_dtype)) train_transforms.transforms.append(convert_dtype) val_transforms.transforms.append(convert_dtype) self.dataset_train = ImageFolder(self.data_dir / 'train', transform=train_transforms) self.dataset_val = ImageFolder(self.data_dir / 'val', transform=val_transforms) if stage == "test" or stage is None: test_transforms = (self.val_transform() if self.test_transforms is None else self.test_transforms) if self.img_dtype is not torch.float32: assert isinstance(test_transforms, transforms.Compose) convert_dtype = transforms.Lambda(lambda x: x.to(dtype=self.img_dtype)) test_transforms.transforms.append(convert_dtype) self.dataset_test = ImageFolder(self.data_dir / 'val', transform=test_transforms) def train_transform(self) -> Callable: """The standard imagenet transforms. .. code-block:: python transforms.Compose([ transforms.RandomResizedCrop(self.image_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ), ]) """ preprocessing = transforms.Compose( [ transforms.RandomResizedCrop(self.image_size), transforms.RandomHorizontalFlip(), transforms.ToTensor(), imagenet_normalization(), ] ) return preprocessing def val_transform(self) -> Callable: """The standard imagenet transforms for validation. .. code-block:: python transforms.Compose([ transforms.Resize(self.image_size + 32), transforms.CenterCrop(self.image_size), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ), ]) """ preprocessing = transforms.Compose( [ transforms.Resize(self.image_size + 32), transforms.CenterCrop(self.image_size), transforms.ToTensor(), imagenet_normalization(), ] ) return preprocessing def train_dataloader(self, *args: Any, **kwargs: Any) -> DataLoader: """ The train dataloader """ if self.num_aug_repeats == 0: shuffle = self.shuffle sampler = None else: shuffle = False from timm.data.distributed_sampler import RepeatAugSampler sampler = RepeatAugSampler(self.dataset_train, num_repeats=self.num_aug_repeats) return self._data_loader(self.dataset_train, batch_size=self.batch_size, shuffle=shuffle, mixup=self.mixup, sampler=sampler) def val_dataloader(self, *args: Any, **kwargs: Any) -> Union[DataLoader, List[DataLoader]]: """ The val dataloader """ # If using RepeatAugment, we set trainer.replace_sampler_ddp=False, so we have to # construct the DistributedSampler ourselves. if not self.cache_val_dataset: sampler = (DistributedSampler(self.dataset_val, shuffle=False, drop_last=self.drop_last) if self.num_aug_repeats != 0 else None) return self._data_loader(self.dataset_val, batch_size=self.batch_size_eval, sampler=sampler) else: print('Caching val dataset') sampler = (SequentialSampler(self.dataset_val) if self.trainer.world_size <= 1 else DistributedSampler(self.dataset_val, shuffle=False, drop_last=self.drop_last)) indices = list(iter(sampler)) loader = DataLoader(self.dataset_val, batch_size=None, shuffle=False, sampler=sampler, num_workers=self.num_workers, drop_last=self.drop_last) batches = list(loader) assert len(batches) == len(indices) self.dataset_val = DictDataset(dict(zip(indices, batches)), length=len(self.dataset_val)) sampler = (DistributedSampler(self.dataset_val, shuffle=False, drop_last=self.drop_last) if self.num_aug_repeats != 0 else None) return self._data_loader(self.dataset_val, batch_size=self.batch_size_eval, sampler=sampler) def test_dataloader(self, *args: Any, **kwargs: Any) -> Union[DataLoader, List[DataLoader]]: """ The test dataloader """ sampler = (DistributedSampler(self.dataset_test, shuffle=False, drop_last=self.drop_last) if self.num_aug_repeats != 0 else None) return self._data_loader(self.dataset_test, batch_size=self.batch_size_eval, sampler=sampler) def _data_loader(self, dataset: Dataset, batch_size: int, shuffle: bool = False, mixup: Optional[Callable] = None, sampler=None) -> DataLoader: collate_fn = ((lambda batch: mixup(*default_collate(batch))) if mixup is not None else default_collate) return DataLoader( dataset, collate_fn=collate_fn, batch_size=batch_size, shuffle=shuffle, sampler=sampler, num_workers=self.num_workers, drop_last=self.drop_last, pin_memory=self.pin_memory, persistent_workers=True ) class Imagenet21kPDataModule(ImagenetDataModule): """ImageNet-21k (winter 21) processed with https://github.com/Alibaba-MIIL/ImageNet21K """ @property def num_classes(self) -> int: """ Return: 10450 """ return 10450
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/imagenet.py
import torch from timm.data import Mixup from timm.data.mixup import mixup_target class TimmMixup(Mixup): """ Wrap timm.data.Mixup that avoids the assert that batch size must be even. """ def __call__(self, x, target): if self.mode == 'elem': lam = self._mix_elem(x) elif self.mode == 'pair': # We move the assert from the beginning of the function to here assert len(x) % 2 == 0, 'Batch size should be even when using this' lam = self._mix_pair(x) else: lam = self._mix_batch(x) target = mixup_target(target, self.num_classes, lam, self.label_smoothing, x.device) return x, target
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/timm_mixup.py
# Adapted from https://github.com/Lightning-AI/lightning/blob/2845e7565dbe6b765ae32870e7d2bc456529c30a/tests/tests_pytorch/utilities/test_auto_restart.py#L1397 from typing import Iterator import math import torch from torch.utils.data import RandomSampler, DistributedSampler class RandomFaultTolerantSampler(RandomSampler): def __init__(self, *args, generator=None, **kwargs): # generator = torch.Generator().manual_seed(seed) # super().__init__(*args, generator=generator, **kwargs) # TD [2022-07-17]: We don't force the seed to be zero. We generate random seed, # which should be reproducible if pl.seed_everything was called before hand. # This means that changing the seed of the experiment will also change the # sampling order. if generator is None: seed = int(torch.empty((), dtype=torch.int64).random_().item()) generator = torch.Generator().manual_seed(seed) super().__init__(*args, generator=generator, **kwargs) self.counter = 0 # self.start_counter = 0 self.restarting = False def state_dict(self): return {"random_state": self.state, "counter": self.counter} def load_state_dict(self, state_dict): self.generator.set_state(state_dict.get("random_state")) self.counter = state_dict["counter"] # self.start_counter = self.counter self.restarting = True # TD [2022-08-28] Setting the len will cause PL to think there are only a few batches left per # epoch, and subsequent epoch will have very few batches. # def __len__(self): # # We need a separate self.start_counter because PL seems to call len repeatedly. # # If we use len(self.data_source) - self.counter then PL will think the epoch ends # # when we're only half way through. # return len(self.data_source) - self.start_counter def __iter__(self) -> Iterator[int]: n = len(self.data_source) self.state = self.generator.get_state() indices = torch.randperm(n, generator=self.generator).tolist() if not self.restarting: self.counter = 0 else: indices = indices[self.counter:] self.restarting = False # self.start_counter = self.counter for index in indices: self.counter += 1 yield index self.counter = 0 # self.start_counter = self.counter class FaultTolerantDistributedSampler(DistributedSampler): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.counter = 0 # self.start_counter = 0 self.restarting = False def state_dict(self): return {"epoch": self.epoch, "counter": self.counter} def load_state_dict(self, state_dict): self.epoch = state_dict["epoch"] self.counter = state_dict["counter"] # self.start_counter = self.counter self.restarting = True # TD [2022-08-28] Setting the len will cause PL to think there are only a few batches left per # epoch, and subsequent epoch will have very few batches. # def __len__(self) -> int: # return self.num_samples - self.start_counter def __iter__(self): if self.shuffle: # deterministically shuffle based on epoch and seed g = torch.Generator() g.manual_seed(self.seed + self.epoch) indices = torch.randperm(len(self.dataset), generator=g).tolist() # type: ignore[arg-type] else: indices = list(range(len(self.dataset))) # type: ignore[arg-type] if not self.drop_last: # add extra samples to make it evenly divisible padding_size = self.total_size - len(indices) if padding_size <= len(indices): indices += indices[:padding_size] else: indices += (indices * math.ceil(padding_size / len(indices)))[:padding_size] else: # remove tail of data to make it evenly divisible. indices = indices[:self.total_size] assert len(indices) == self.total_size # subsample indices = indices[self.rank:self.total_size:self.num_replicas] assert len(indices) == self.num_samples if not self.restarting: self.counter = 0 else: indices = indices[self.counter:] self.restarting = False # self.start_counter = self.counter for index in indices: self.counter += 1 yield index self.counter = 0 # self.start_counter = self.counter
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/fault_tolerant_sampler.py
# Copied from https://github.com/stanford-crfm/mistral/blob/main/src/corpora/detokenization.py # Which was originally from https://github.com/NVIDIA/Megatron-LM/blob/aed2f75e209e525c842aec7c044af7acae2a4614/tasks/zeroshot_gpt/detokenizer.py """ Handle detokenization for different dataset for zero-shot LM evaluation. """ import re def wikitext_detokenize(string: str) -> str: """ Wikitext is whitespace tokenized and we remove these whitespaces. Taken from https://github.com/NVIDIA/Megatron-LM/blob/main/tasks/zeroshot_gpt2/detokenizer.py """ # Contractions string = string.replace("s '", "s'") string = re.sub(r"/' [0-9]/", r"/'[0-9]/", string) # Number Separators string = string.replace(" @-@ ", "-") string = string.replace(" @,@ ", ",") string = string.replace(" @.@ ", ".") # Punctuation string = string.replace(" : ", ": ") string = string.replace(" ; ", "; ") string = string.replace(" . ", ". ") string = string.replace(" ! ", "! ") string = string.replace(" ? ", "? ") string = string.replace(" , ", ", ") # Double Brackets string = re.sub(r"\(\s*([^\)]*?)\s*\)", r"(\1)", string) string = re.sub(r"\[\s*([^\]]*?)\s*\]", r"[\1]", string) string = re.sub(r"{\s*([^}]*?)\s*}", r"{\1}", string) string = re.sub(r"\"\s*([^\"]*?)\s*\"", r'"\1"', string) string = re.sub(r"'\s*([^']*?)\s*'", r"'\1'", string) # Miscellaneous string = string.replace("= = = =", "====") string = string.replace("= = =", "===") string = string.replace("= =", "==") string = string.replace(" " + chr(176) + " ", chr(176)) string = string.replace(" \n", "\n") string = string.replace("\n ", "\n") string = string.replace(" N ", " 1 ") string = string.replace(" 's", "'s") return string # Set Registry for Various Datasets DATASET_TOKENIZATION_REGISTRY = {"wikitext": wikitext_detokenize}
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/datasets/detokenizer.py
# Inspired by https://github.com/NVIDIA/Megatron-LM/blob/main/tasks/zeroshot_gpt/datasets.py # Except we don't pad the last block and don't use overlapping eval # And we return both the input and the target import math import numpy as np import torch class LMDataset(torch.utils.data.Dataset): def __init__(self, tokens, seq_len, drop_last=True): """tokens should be a numpy array """ self.seq_len = seq_len ntokens = len(tokens) if drop_last: ntokens = ((ntokens - 1) // seq_len) * seq_len + 1 self.ntokens = ntokens # We're careful not to slice tokens, since it could be a memmap'ed array or H5 dataset, # and slicing would load it to memory. self.tokens = tokens self.total_sequences = math.ceil((self.ntokens - 1) / self.seq_len) def __len__(self): return self.total_sequences def __getitem__(self, idx): start_idx = idx * self.seq_len seq_len = min(self.seq_len, self.ntokens - 1 - start_idx) data = torch.as_tensor(self.tokens[start_idx:(start_idx + seq_len + 1)].astype(np.int64)) return data[:-1], data[1:].clone()
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/datamodules/datasets/lm_dataset.py
import inspect import torch.nn as nn import hydra try: from apex.contrib.layer_norm import FastLayerNorm except ImportError: FastLayerNorm = None from src.models.modules.seq_common import PositionalEncoding def group_parameters_for_optimizer(model, optimizer_cfg, bias_weight_decay=False, normalization_weight_decay=False): """Set weight_decay=0.0 for parameters in model.no_weight_decay, for parameters with attribute _no_weight_decay==True, for bias parameters if bias_weight_decay==False, for normalization parameters if normalization_weight_decay==False """ # Get the weight decay from the config, or from the default value of the optimizer constructor # if it's not specified in the config. if 'weight_decay' in optimizer_cfg: weight_decay = optimizer_cfg.weight_decay else: # https://stackoverflow.com/questions/12627118/get-a-function-arguments-default-value signature = inspect.signature(hydra.utils.get_class(optimizer_cfg._target_)) if 'weight_decay' in signature.parameters: weight_decay = signature.parameters['weight_decay'].default if weight_decay is inspect.Parameter.empty: weight_decay = 0.0 else: weight_decay = 0.0 # If none of the parameters have weight decay anyway, and there are no parameters with special # optimization params if weight_decay == 0.0 and not any(hasattr(p, '_optim') for p in model.parameters()): return model.parameters() skip = model.no_weight_decay() if hasattr(model, 'no_weight_decay') else set() skip_keywords = (model.no_weight_decay_keywords() if hasattr(model, 'no_weight_decay_keywords') else set()) # Adapted from https://github.com/karpathy/minGPT/blob/master/mingpt/model.py#L134 """ This long function is unfortunately doing something very simple and is being very defensive: We are separating out all parameters of the model into two buckets: those that will experience weight decay for regularization and those that won't (biases, and layernorm/embedding weights). We are then returning the PyTorch optimizer object. """ # separate out all parameters to those that will and won't experience regularizing weight decay decay = set() no_decay = set() special = set() whitelist_weight_modules = (nn.Linear, ) blacklist_weight_modules = (nn.Embedding, PositionalEncoding) if not normalization_weight_decay: blacklist_weight_modules += (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.LazyBatchNorm1d, nn.LazyBatchNorm2d, nn.LazyBatchNorm3d, nn.GroupNorm, nn.SyncBatchNorm, nn.InstanceNorm1d, nn.InstanceNorm2d, nn.InstanceNorm3d, nn.LayerNorm, nn.LocalResponseNorm) if FastLayerNorm is not None: blacklist_weight_modules += (FastLayerNorm,) param_dict = {pn: p for pn, p in model.named_parameters() if p.requires_grad} for mn, m in model.named_modules(): for pn, p in m.named_parameters(): fpn = '%s.%s' % (mn, pn) if mn else pn # full param name # In case of parameter sharing, some parameters show up here but are not in # param_dict.keys() if not p.requires_grad or fpn not in param_dict: continue # frozen weights if hasattr(p, '_optim'): special.add(fpn) elif fpn in skip or any(skip_keyword in fpn for skip_keyword in skip_keywords): no_decay.add(fpn) elif getattr(p, '_no_weight_decay', False): no_decay.add(fpn) elif not bias_weight_decay and pn.endswith('bias'): no_decay.add(fpn) elif pn.endswith('weight') and isinstance(m, whitelist_weight_modules): # weights of whitelist modules will be weight decayed decay.add(fpn) elif isinstance(m, blacklist_weight_modules): # weights of blacklist modules will NOT be weight decayed no_decay.add(fpn) decay |= (param_dict.keys() - no_decay - special) # validate that we considered every parameter inter_params = decay & no_decay union_params = decay | no_decay assert len(inter_params) == 0, f"Parameters {str(inter_params)} made it into both decay/no_decay sets!" assert len(param_dict.keys() - special - union_params) == 0, f"parameters {str(param_dict.keys() - union_params)} were not separated into either decay/no_decay set!" if weight_decay == 0.0 or not no_decay: param_groups = [{"params": [param_dict[pn] for pn in sorted(list(no_decay | decay))], "weight_decay": weight_decay}] else: # We need sorted(list()) so that the order is deterministic. Otherwise when we resume # the order could change and resume will fail. [H/t Albert] param_groups = [ {"params": [param_dict[pn] for pn in sorted(list(decay))], "weight_decay": weight_decay}, {"params": [param_dict[pn] for pn in sorted(list(no_decay))], "weight_decay": 0.0}, ] # Add parameters with special hyperparameters # Unique dicts hps = [dict(s) for s in set(frozenset(param_dict[pn]._optim.items()) for pn in special)] for hp in hps: params = [param_dict[pn] for pn in sorted(list(special)) if param_dict[pn]._optim == hp] param_groups.append({"params": params, **hp}) return param_groups
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/optim/param_grouping.py
import torch from torch.optim import Optimizer from timm.scheduler import CosineLRScheduler # We need to subclass torch.optim.lr_scheduler._LRScheduler, or Pytorch-lightning will complain class TimmCosineLRScheduler(CosineLRScheduler, torch.optim.lr_scheduler._LRScheduler): """ Wrap timm.scheduler.CosineLRScheduler so we can call scheduler.step() without passing in epoch. It supports resuming as well. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._last_epoch = -1 self.step(epoch=0) def step(self, epoch=None): if epoch is None: self._last_epoch += 1 else: self._last_epoch = epoch # We call either step or step_update, depending on whether we're using the scheduler every # epoch or every step. # Otherwise, lightning will always call step (i.e., meant for each epoch), and if we set # scheduler interval to "step", then the learning rate update will be wrong. if self.t_in_epochs: super().step(epoch=self._last_epoch) else: super().step_update(num_updates=self._last_epoch)
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/optim/timm_lr_scheduler.py
# Meant to work with Apex's DistributeFusedAdam from typing import Any, Callable, Dict, List, Optional, Union from pathlib import Path import types import torch from torch.optim.optimizer import Optimizer from torch.optim import LBFGS from apex.contrib.optimizers.distributed_fused_adam import DistributedFusedAdam from pytorch_lightning.strategies.ddp import DDPStrategy from pytorch_lightning.plugins.precision import PrecisionPlugin, NativeMixedPrecisionPlugin from pytorch_lightning.core.optimizer import LightningOptimizer from pytorch_lightning.utilities.exceptions import MisconfigurationException try: # pytorch_lightning <= 1.7 from pytorch_lightning.utilities.types import _PATH except ImportError: # pytorch_lightning >= 1.8 try: from lightning_lite.utilities.types import _PATH except ImportError: # pytorch_lightning >= 1.9 from lightning_fabric.utilities.types import _PATH class DistAdamNativeMixedPrecisionPlugin(NativeMixedPrecisionPlugin): def optimizer_step( # type: ignore[override] self, model: "pl.LightningModule", optimizer, optimizer_idx: int, closure: Callable[[], Any], **kwargs: Any, ) -> Any: if self.scaler is None: # skip scaler logic, as bfloat16 does not require scaler return NativeMixedPrecisionPlugin.optimizer_step( self, optimizer, model=model, optimizer_idx=optimizer_idx, closure=closure, **kwargs ) if isinstance(optimizer, LBFGS): raise MisconfigurationException( f"Native AMP and the LBFGS optimizer are not compatible (optimizer {optimizer_idx})." ) closure_result = closure() # HACK: we don't call self.scaler.unscale_ here. This is because DistributedFusedAdam # optimizer internally takes the scale into account. # If we call unscale_ here, it would be equivalent to unscaling the gradients twice. # Not unscaling has the side-effect that the NormMonitor callback will report the # gradient norm to be much larger than reality. # # `unscale` after the closure is executed but before the `on_before_optimizer_step` hook. # self.scaler.unscale_(optimizer) # This will call gradient clipping self._after_closure(model, optimizer, optimizer_idx) skipped_backward = closure_result is None # in manual optimization, the closure does not return a value if not model.automatic_optimization or not skipped_backward: # note: the scaler will skip the `optimizer.step` if nonfinite gradients are found step_output = self.scaler.step(optimizer, **kwargs) self.scaler.update() return step_output return closure_result def clip_grad_by_norm(self, optimizer: DistributedFusedAdam, clip_val: Union[int, float]) -> None: """Clip gradients by norm.""" # DistributedFusedAdam wants list, not generator # Gradients have not be scaled, so we need to scale up the clip_val if self.scaler is not None: clip_val *= self.scaler.get_scale() return optimizer.clip_grad_norm(clip_val) class DDPStrategyZero2(DDPStrategy): """To use Apex's DistributedFusedAdam, we need to shard the optimizer states when saving/loading checkpoints. """ strategy_name = "ddp_zero2" def __init__( self, *args, precision_plugin: Optional[PrecisionPlugin] = DistAdamNativeMixedPrecisionPlugin, # precision_plugin: Optional[PrecisionPlugin] = None, **kwargs: Union[Any, Dict[str, Any]], ) -> None: super().__init__( *args, precision_plugin=precision_plugin, **kwargs ) @property def precision_plugin(self) -> PrecisionPlugin: return self._precision_plugin if self._precision_plugin is not None else PrecisionPlugin() @precision_plugin.setter def precision_plugin(self, precision_plugin: Optional[PrecisionPlugin]) -> None: self._precision_plugin = precision_plugin # https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance self._precision_plugin.optimizer_step = types.MethodType( DistAdamNativeMixedPrecisionPlugin.optimizer_step, self._precision_plugin ) self._precision_plugin.clip_grad_by_norm = types.MethodType( DistAdamNativeMixedPrecisionPlugin.clip_grad_by_norm, self._precision_plugin ) def optimizer_state(self, optimizer: Optimizer) -> Optional[dict]: if isinstance(optimizer, LightningOptimizer): optimizer = optimizer._optimizer if isinstance(optimizer, DistributedFusedAdam): return optimizer.state_dict(gather_on_root=False) else: return optimizer.state_dict() def save_checkpoint( self, checkpoint: Dict[str, Any], filepath: _PATH, storage_options: Optional[Any] = None ) -> None: """Save model/training states as a checkpoint file through state-dump and file-write. Args: checkpoint: dict containing model and trainer state filepath: write-target file's path storage_options: parameter for how to save to storage, passed to ``CheckpointIO`` plugin """ filepath = Path(filepath) filepath.mkdir(parents=True, exist_ok=True) local_optimizer_states = checkpoint.pop('optimizer_states') if self.is_global_zero: self.checkpoint_io.save_checkpoint(checkpoint, filepath / 'model_states.pt', storage_options=storage_options) self.checkpoint_io.save_checkpoint(local_optimizer_states, filepath / f'{self.global_rank:03d}_optim_states.pt', storage_options=storage_options) def load_checkpoint(self, checkpoint_path: _PATH) -> Dict[str, Any]: torch.cuda.empty_cache() checkpoint_path = Path(checkpoint_path) if checkpoint_path.is_file(): return super().load_checkpoint(self, str(checkpoint_path)) else: assert checkpoint_path.is_dir() global_states = self.checkpoint_io.load_checkpoint(checkpoint_path / 'model_states.pt') local_optimizer_states = self.checkpoint_io.load_checkpoint( checkpoint_path / f'{self.global_rank:03d}_optim_states.pt', map_location='cuda' ) global_states['optimizer_states'] = local_optimizer_states return global_states
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/ddp_zero2.py
import collections import math import os import pathlib import re import pynvml pynvml.nvmlInit() def systemGetDriverVersion(): return pynvml.nvmlSystemGetDriverVersion() def deviceGetCount(): return pynvml.nvmlDeviceGetCount() class device: # assume nvml returns list of 64 bit ints _nvml_affinity_elements = math.ceil(os.cpu_count() / 64) def __init__(self, device_idx): super().__init__() self.handle = pynvml.nvmlDeviceGetHandleByIndex(device_idx) def getName(self): return pynvml.nvmlDeviceGetName(self.handle) def getCpuAffinity(self): affinity_string = '' for j in pynvml.nvmlDeviceGetCpuAffinity( self.handle, device._nvml_affinity_elements ): # assume nvml returns list of 64 bit ints affinity_string = '{:064b}'.format(j) + affinity_string affinity_list = [int(x) for x in affinity_string] affinity_list.reverse() # so core 0 is in 0th element of list ret = [i for i, e in enumerate(affinity_list) if e != 0] return ret def set_socket_affinity(gpu_id): dev = device(gpu_id) affinity = dev.getCpuAffinity() os.sched_setaffinity(0, affinity) def set_single_affinity(gpu_id): dev = device(gpu_id) affinity = dev.getCpuAffinity() os.sched_setaffinity(0, affinity[:1]) def set_single_unique_affinity(gpu_id, nproc_per_node): devices = [device(i) for i in range(nproc_per_node)] socket_affinities = [dev.getCpuAffinity() for dev in devices] siblings_list = get_thread_siblings_list() siblings_dict = dict(siblings_list) # remove siblings for idx, socket_affinity in enumerate(socket_affinities): socket_affinities[idx] = list(set(socket_affinity) - set(siblings_dict.values())) affinities = [] assigned = [] for socket_affinity in socket_affinities: for core in socket_affinity: if core not in assigned: affinities.append([core]) assigned.append(core) break os.sched_setaffinity(0, affinities[gpu_id]) def set_socket_unique_affinity(gpu_id, nproc_per_node, mode): device_ids = [device(i) for i in range(nproc_per_node)] socket_affinities = [dev.getCpuAffinity() for dev in device_ids] siblings_list = get_thread_siblings_list() siblings_dict = dict(siblings_list) # remove siblings for idx, socket_affinity in enumerate(socket_affinities): socket_affinities[idx] = list(set(socket_affinity) - set(siblings_dict.values())) socket_affinities_to_device_ids = collections.defaultdict(list) for idx, socket_affinity in enumerate(socket_affinities): socket_affinities_to_device_ids[tuple(socket_affinity)].append(idx) for socket_affinity, device_ids in socket_affinities_to_device_ids.items(): devices_per_group = len(device_ids) cores_per_device = len(socket_affinity) // devices_per_group for group_id, device_id in enumerate(device_ids): if device_id == gpu_id: if mode == 'interleaved': affinity = list(socket_affinity[group_id::devices_per_group]) elif mode == 'continuous': affinity = list(socket_affinity[group_id*cores_per_device:(group_id+1)*cores_per_device]) else: raise RuntimeError('Unknown set_socket_unique_affinity mode') # reintroduce siblings affinity += [siblings_dict[aff] for aff in affinity if aff in siblings_dict] os.sched_setaffinity(0, affinity) def get_thread_siblings_list(): path = '/sys/devices/system/cpu/cpu*/topology/thread_siblings_list' thread_siblings_list = [] pattern = re.compile(r'(\d+)\D(\d+)') for fname in pathlib.Path(path[0]).glob(path[1:]): with open(fname) as f: content = f.read().strip() res = pattern.findall(content) if res: pair = tuple(map(int, res[0])) thread_siblings_list.append(pair) return thread_siblings_list def set_affinity(gpu_id, nproc_per_node, mode='socket'): if mode == 'socket': set_socket_affinity(gpu_id) elif mode == 'single': set_single_affinity(gpu_id) elif mode == 'single_unique': set_single_unique_affinity(gpu_id, nproc_per_node) elif mode == 'socket_unique_interleaved': set_socket_unique_affinity(gpu_id, nproc_per_node, 'interleaved') elif mode == 'socket_unique_continuous': set_socket_unique_affinity(gpu_id, nproc_per_node, 'continuous') else: raise RuntimeError('Unknown affinity mode') affinity = os.sched_getaffinity(0) return affinity
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/gpu_affinity.py
import re from pathlib import Path import torch import math from einops import rearrange def load_checkpoint(path, device='cpu'): path = Path(path).expanduser() is_deepspeed = False if path.is_dir(): # DeepSpeed checkpoint is_deepspeed = True latest_path = path / 'latest' if latest_path.is_file(): with open(latest_path, 'r') as fd: tag = fd.read().strip() else: raise ValueError(f"Unable to find 'latest' file at {latest_path}") path /= f'{tag}/mp_rank_00_model_states.pt' state_dict = torch.load(path, map_location=device) if is_deepspeed: state_dict = state_dict['module'] # Replace the names of some of the submodules def key_mapping(key): return re.sub(r'^module.model.', '', key) state_dict = {key_mapping(k): v for k, v in state_dict.items()} return state_dict def blockdiag_to_dense_mlp_bert(state_dict): from src.ops.blockdiag_multiply import blockdiag_weight_to_dense_weight names = {name for name in state_dict if re.match('bert.encoder.layer.(\d+).(mlp.fc(1|2)|(intermediate|output).dense).weight', name)} for name in names: state_dict[name] = blockdiag_weight_to_dense_weight(state_dict[name]) return state_dict def interpolate_pos_embedding(state_dict, out_seqlen, pos_embedding_name='model.pos_encoder.pe', interleave=False): orig_emb = state_dict['state_dict'][pos_embedding_name] assert (out_seqlen % orig_emb.shape[1]) == 0, 'out_seqlen must be a multiple of the original sequence length' reps = [1 for i in orig_emb.shape] reps[1] = out_seqlen // orig_emb.shape[1] if interleave: assert math.isqrt(orig_emb.shape[1]) ** 2 == orig_emb.shape[1], 'interleave only works for square lengths' assert math.isqrt(out_seqlen) ** 2 == out_seqlen, 'interleave only works for square lengths' assert math.isqrt(reps[1]) ** 2 == reps[1], 'out_seqlen / seqlen must be a perfect square' emb_square = rearrange(orig_emb, 'b (h w) d -> b h w d', h = math.isqrt(orig_emb.shape[1])) emb_square_expanded = emb_square.repeat_interleave(math.isqrt(reps[1]), axis=1).repeat_interleave(math.isqrt(reps[1]), axis=2) new_emb = rearrange(emb_square_expanded, 'b h w d -> b (h w) d') state_dict['state_dict'][pos_embedding_name] = new_emb else: state_dict['state_dict'][pos_embedding_name] = orig_emb.repeat(*reps) ret = remove_model_prefix(state_dict) # # HACK: this is a hack for block-sparse flash attention ret = { k: v for k, v in ret.items() if not k.endswith('inner_attn.layout') } return ret def remove_model_prefix(state_dict): # HACK: this is a hack to get the model to load properly, get rid of 'model.' prefix for key in list(state_dict['state_dict'].keys()): if key.startswith('model.'): new_key = key[len('model.'):] state_dict['state_dict'][new_key] = state_dict['state_dict'].pop(key) # HACK: something is wrong with the state dict being loaded... return state_dict['state_dict']
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/checkpoint.py
# Copied from https://github.com/fadel/pytorch_ema/blob/master/torch_ema/ema.py from __future__ import division from __future__ import unicode_literals from typing import Iterable, Optional import weakref import copy import contextlib import torch def to_float_maybe(x): return x.float() if x.dtype in [torch.float16, torch.bfloat16] else x # Partially based on: # https://github.com/tensorflow/tensorflow/blob/r1.13/tensorflow/python/training/moving_averages.py class ExponentialMovingAverage: """ Maintains (exponential) moving average of a set of parameters. Args: parameters: Iterable of `torch.nn.Parameter` (typically from `model.parameters()`). decay: The exponential decay. use_num_updates: Whether to use number of updates when computing averages. """ def __init__( self, parameters: Iterable[torch.nn.Parameter], decay: float, use_num_updates: bool = True ): if decay < 0.0 or decay > 1.0: raise ValueError('Decay must be between 0 and 1') self.decay = decay self.num_updates = 0 if use_num_updates else None parameters = list(parameters) self.shadow_params = [to_float_maybe(p.clone().detach()) for p in parameters if p.requires_grad] self.collected_params = None # By maintaining only a weakref to each parameter, # we maintain the old GC behaviour of ExponentialMovingAverage: # if the model goes out of scope but the ExponentialMovingAverage # is kept, no references to the model or its parameters will be # maintained, and the model will be cleaned up. self._params_refs = [weakref.ref(p) for p in parameters] def _get_parameters( self, parameters: Optional[Iterable[torch.nn.Parameter]] ) -> Iterable[torch.nn.Parameter]: if parameters is None: parameters = [p() for p in self._params_refs] if any(p is None for p in parameters): raise ValueError( "(One of) the parameters with which this " "ExponentialMovingAverage " "was initialized no longer exists (was garbage collected);" " please either provide `parameters` explicitly or keep " "the model to which they belong from being garbage " "collected." ) return parameters else: parameters = list(parameters) if len(parameters) != len(self.shadow_params): raise ValueError( "Number of parameters passed as argument is different " "from number of shadow parameters maintained by this " "ExponentialMovingAverage" ) return parameters def update( self, parameters: Optional[Iterable[torch.nn.Parameter]] = None ) -> None: """ Update currently maintained parameters. Call this every time the parameters are updated, such as the result of the `optimizer.step()` call. Args: parameters: Iterable of `torch.nn.Parameter`; usually the same set of parameters used to initialize this object. If `None`, the parameters with which this `ExponentialMovingAverage` was initialized will be used. """ parameters = self._get_parameters(parameters) decay = self.decay if self.num_updates is not None: self.num_updates += 1 decay = min( decay, (1 + self.num_updates) / (10 + self.num_updates) ) one_minus_decay = 1.0 - decay if parameters[0].device != self.shadow_params[0].device: self.to(device=parameters[0].device) with torch.no_grad(): parameters = [p for p in parameters if p.requires_grad] for s_param, param in zip(self.shadow_params, parameters): torch.lerp(s_param, param.to(dtype=s_param.dtype), one_minus_decay, out=s_param) def copy_to( self, parameters: Optional[Iterable[torch.nn.Parameter]] = None ) -> None: """ Copy current averaged parameters into given collection of parameters. Args: parameters: Iterable of `torch.nn.Parameter`; the parameters to be updated with the stored moving averages. If `None`, the parameters with which this `ExponentialMovingAverage` was initialized will be used. """ parameters = self._get_parameters(parameters) for s_param, param in zip(self.shadow_params, parameters): if param.requires_grad: param.data.copy_(s_param.data) def store( self, parameters: Optional[Iterable[torch.nn.Parameter]] = None ) -> None: """ Save the current parameters for restoring later. Args: parameters: Iterable of `torch.nn.Parameter`; the parameters to be temporarily stored. If `None`, the parameters of with which this `ExponentialMovingAverage` was initialized will be used. """ parameters = self._get_parameters(parameters) self.collected_params = [ param.clone() for param in parameters if param.requires_grad ] def restore( self, parameters: Optional[Iterable[torch.nn.Parameter]] = None ) -> None: """ Restore the parameters stored with the `store` method. Useful to validate the model with EMA parameters without affecting the original optimization process. Store the parameters before the `copy_to` method. After validation (or model saving), use this to restore the former parameters. Args: parameters: Iterable of `torch.nn.Parameter`; the parameters to be updated with the stored parameters. If `None`, the parameters with which this `ExponentialMovingAverage` was initialized will be used. """ if self.collected_params is None: raise RuntimeError( "This ExponentialMovingAverage has no `store()`ed weights " "to `restore()`" ) parameters = self._get_parameters(parameters) for c_param, param in zip(self.collected_params, parameters): if param.requires_grad: param.data.copy_(c_param.data) @contextlib.contextmanager def average_parameters( self, parameters: Optional[Iterable[torch.nn.Parameter]] = None ): r""" Context manager for validation/inference with averaged parameters. Equivalent to: ema.store() ema.copy_to() try: ... finally: ema.restore() Args: parameters: Iterable of `torch.nn.Parameter`; the parameters to be updated with the stored parameters. If `None`, the parameters with which this `ExponentialMovingAverage` was initialized will be used. """ parameters = self._get_parameters(parameters) self.store(parameters) self.copy_to(parameters) try: yield finally: self.restore(parameters) def to(self, device=None, dtype=None) -> None: r"""Move internal buffers of the ExponentialMovingAverage to `device`. Args: device: like `device` argument to `torch.Tensor.to` """ # .to() on the tensors handles None correctly self.shadow_params = [ p.to(device=device, dtype=dtype) if p.is_floating_point() else p.to(device=device) for p in self.shadow_params ] if self.collected_params is not None: self.collected_params = [ p.to(device=device, dtype=dtype) if p.is_floating_point() else p.to(device=device) for p in self.collected_params ] return def state_dict(self) -> dict: r"""Returns the state of the ExponentialMovingAverage as a dict.""" # Following PyTorch conventions, references to tensors are returned: # "returns a reference to the state and not its copy!" - # https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict return { "decay": self.decay, "num_updates": self.num_updates, "shadow_params": self.shadow_params, "collected_params": self.collected_params } def load_state_dict(self, state_dict: dict) -> None: r"""Loads the ExponentialMovingAverage state. Args: state_dict (dict): EMA state. Should be an object returned from a call to :meth:`state_dict`. """ # deepcopy, to be consistent with module API state_dict = copy.deepcopy(state_dict) self.decay = state_dict["decay"] if self.decay < 0.0 or self.decay > 1.0: raise ValueError('Decay must be between 0 and 1') self.num_updates = state_dict["num_updates"] assert self.num_updates is None or isinstance(self.num_updates, int), \ "Invalid num_updates" self.shadow_params = state_dict["shadow_params"] assert isinstance(self.shadow_params, list), \ "shadow_params must be a list" assert all( isinstance(p, torch.Tensor) for p in self.shadow_params ), "shadow_params must all be Tensors" self.collected_params = state_dict["collected_params"] if self.collected_params is not None: assert isinstance(self.collected_params, list), \ "collected_params must be a list" assert all( isinstance(p, torch.Tensor) for p in self.collected_params ), "collected_params must all be Tensors" assert len(self.collected_params) == len(self.shadow_params), \ "collected_params and shadow_params had different lengths" if len(self.shadow_params) == len(self._params_refs): # Consistent with torch.optim.Optimizer, cast things to consistent # device and dtype with the parameters params = [p() for p in self._params_refs] # If parameters have been garbage collected, just load the state # we were given without change. if not any(p is None for p in params): # ^ parameter references are still good for i, p in enumerate(params): self.shadow_params[i] = to_float_maybe(self.shadow_params[i].to( device=p.device, dtype=p.dtype )) if self.collected_params is not None: self.collected_params[i] = self.collected_params[i].to( device=p.device, dtype=p.dtype ) else: raise ValueError( "Tried to `load_state_dict()` with the wrong number of " "parameters in the saved state." )
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/ema.py
# Adapted from https://github.com/rwightman/pytorch-image-models/blob/master/benchmark.py import torch try: from deepspeed.profiling.flops_profiler import get_model_profile has_deepspeed_profiling = True except ImportError as e: has_deepspeed_profiling = False try: from fvcore.nn import FlopCountAnalysis, flop_count_str, flop_count_table from fvcore.nn import ActivationCountAnalysis has_fvcore_profiling = True except ImportError as e: FlopCountAnalysis = None ActivationCountAnalysis = None has_fvcore_profiling = False def profile_deepspeed(model, input_size=(3, 224, 224), input_dtype=torch.float32, batch_size=1, detailed=False): device, dtype = next(model.parameters()).device, next(model.parameters()).dtype flops, macs, params = get_model_profile( model=model, args=torch.zeros((batch_size,) + input_size, device=device, dtype=input_dtype), print_profile=detailed, # prints the model graph with the measured profile attached to each module detailed=detailed, # print the detailed profile warm_up=10, # the number of warm-ups before measuring the time of each module as_string=False, # print raw numbers (e.g. 1000) or as human-readable strings (e.g. 1k) output_file=None, # path to the output file. If None, the profiler prints to stdout. ignore_modules=None) # the list of modules to ignore in the profiling return macs, 0 # no activation count in DS def profile_fvcore(model, input_size=(3, 224, 224), input_dtype=torch.float32, max_depth=4, batch_size=1, detailed=False, force_cpu=False): if force_cpu: model = model.to('cpu') device, dtype = next(model.parameters()).device, next(model.parameters()).dtype example_input = torch.zeros((batch_size,) + input_size, device=device, dtype=input_dtype) fca = FlopCountAnalysis(model, example_input) aca = ActivationCountAnalysis(model, example_input) if detailed: print(flop_count_table(fca, max_depth=max_depth)) return fca, fca.total(), aca, aca.total()
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/flops.py
# Copied from https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/Transformer-XL/pytorch/utils/distributed.py # Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from contextlib import contextmanager import torch def init_distributed(cuda): """ Initializes distributed backend. :param cuda: (bool) if True initializes nccl backend, if False initializes gloo backend """ world_size = int(os.environ.get('WORLD_SIZE', 1)) distributed = (world_size > 1) if distributed: backend = 'nccl' if cuda else 'gloo' torch.distributed.init_process_group(backend=backend, init_method='env://') assert torch.distributed.is_initialized() return distributed def barrier(): """ Call torch.distributed.barrier() if distritubed is in use """ if torch.distributed.is_available() and torch.distributed.is_initialized(): torch.distributed.barrier() def get_rank(): """ Gets distributed rank or returns zero if distributed is not initialized. """ if torch.distributed.is_available() and torch.distributed.is_initialized(): rank = torch.distributed.get_rank() else: rank = 0 return rank def get_world_size(): """ Gets total number of distributed workers or returns one if distributed is not initialized. """ if torch.distributed.is_available() and torch.distributed.is_initialized(): world_size = torch.distributed.get_world_size() else: world_size = 1 return world_size def all_reduce_item(value, op='sum'): """ All-reduces single scalar value if distributed is in use """ if torch.distributed.is_available() and torch.distributed.is_initialized(): if op == 'sum' or op == 'mean': dop = torch.distributed.ReduceOp.SUM elif op == 'min': dop = torch.distributed.ReduceOp.MIN elif op == 'max': dop = torch.distributed.ReduceOp.MAX elif op == 'product': dop = torch.distributed.ReduceOp.PRODUCT else: raise RuntimeError('Unsupported reduce op') backend = torch.distributed.get_backend() if backend == torch.distributed.Backend.NCCL: device = torch.device('cuda') elif backend == torch.distributed.Backend.GLOO: device = torch.device('cpu') else: raise RuntimeError('Unsupported distributed backend') tensor = torch.tensor(value, device=device) torch.distributed.all_reduce(tensor, dop) if op == 'mean': tensor /= get_world_size() ret = tensor.item() else: ret = value return ret @contextmanager def sync_workers(): """ Yields distributed rank and synchronizes all workers on exit. """ rank = get_rank() yield rank barrier()
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/distributed.py
import logging import warnings from typing import List, Sequence import pytorch_lightning as pl import rich.syntax import rich.tree from omegaconf import DictConfig, OmegaConf from pytorch_lightning.utilities import rank_zero_only # Copied from https://docs.python.org/3/howto/logging-cookbook.html#using-a-context-manager-for-selective-logging class LoggingContext: def __init__(self, logger, level=None, handler=None, close=True): self.logger = logger self.level = level self.handler = handler self.close = close def __enter__(self): if self.level is not None: self.old_level = self.logger.level self.logger.setLevel(self.level) if self.handler: self.logger.addHandler(self.handler) def __exit__(self, et, ev, tb): if self.level is not None: self.logger.setLevel(self.old_level) if self.handler: self.logger.removeHandler(self.handler) if self.handler and self.close: self.handler.close() # implicit return of None => don't swallow exceptions def get_logger(name=__name__) -> logging.Logger: """Initializes multi-GPU-friendly python logger.""" logger = logging.getLogger(name) # this ensures all logging levels get marked with the rank zero decorator # otherwise logs would get multiplied for each GPU process in multi-GPU setup for level in ("debug", "info", "warning", "error", "exception", "fatal", "critical"): setattr(logger, level, rank_zero_only(getattr(logger, level))) return logger def extras(config: DictConfig) -> None: """A couple of optional utilities, controlled by main config file: - disabling warnings - forcing debug friendly configuration - verifying experiment name is set when running in experiment mode Modifies DictConfig in place. Args: config (DictConfig): Configuration composed by Hydra. """ log = get_logger(__name__) # disable python warnings if <config.ignore_warnings=True> if config.get("ignore_warnings"): log.info("Disabling python warnings! <config.ignore_warnings=True>") warnings.filterwarnings("ignore") # verify experiment name is set when running in experiment mode if config.get("experiment_mode") and not config.get("name"): log.info( "Running in experiment mode without the experiment name specified! " "Use `python run.py mode=exp name=experiment_name`" ) log.info("Exiting...") exit() # force debugger friendly configuration if <config.trainer.fast_dev_run=True> # debuggers don't like GPUs and multiprocessing if config.trainer.get("fast_dev_run"): log.info("Forcing debugger friendly configuration! <config.trainer.fast_dev_run=True>") if config.trainer.get("gpus"): config.trainer.gpus = 0 if config.datamodule.get("pin_memory"): config.datamodule.pin_memory = False if config.datamodule.get("num_workers"): config.datamodule.num_workers = 0 @rank_zero_only def print_config( config: DictConfig, fields: Sequence[str] = ( "trainer", "model", "datamodule", "train", "eval", "callbacks", "logger", "seed", "name", ), resolve: bool = True, ) -> None: """Prints content of DictConfig using Rich library and its tree structure. Args: config (DictConfig): Configuration composed by Hydra. fields (Sequence[str], optional): Determines which main fields from config will be printed and in what order. resolve (bool, optional): Whether to resolve reference fields of DictConfig. """ style = "dim" tree = rich.tree.Tree("CONFIG", style=style, guide_style=style) for field in fields: branch = tree.add(field, style=style, guide_style=style) config_section = config.get(field) branch_content = str(config_section) if isinstance(config_section, DictConfig): branch_content = OmegaConf.to_yaml(config_section, resolve=resolve) branch.add(rich.syntax.Syntax(branch_content, "yaml")) rich.print(tree) with open("config_tree.txt", "w") as fp: rich.print(tree, file=fp) def finish( config: DictConfig, model: pl.LightningModule, datamodule: pl.LightningDataModule, trainer: pl.Trainer, callbacks: List[pl.Callback], logger: List[pl.loggers.LightningLoggerBase], ) -> None: """Makes sure everything closed properly.""" # without this sweeps with wandb logger might crash! for lg in logger: if isinstance(lg, pl.loggers.wandb.WandbLogger): import wandb wandb.finish()
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/utils.py
# Meant to work with Pytorch's ZeroRedundancyOptimizer from typing import Any, Callable, Dict, List, Optional, Union from pathlib import Path import torch from torch.optim.optimizer import Optimizer from torch.distributed.optim import ZeroRedundancyOptimizer from pytorch_lightning.strategies.ddp import DDPStrategy from pytorch_lightning.core.optimizer import LightningOptimizer try: # pytorch_lightning <= 1.7 from pytorch_lightning.utilities.types import _PATH except ImportError: # pytorch_lightning >= 1.8 try: from lightning_lite.utilities.types import _PATH except ImportError: # pytorch_lightning >= 1.9 from lightning_fabric.utilities.types import _PATH # Copied from Pytorch's ZeroRedundancyOptimizer's state_dict method, but we only get # the local state dict to avoid synchronization across GPUs. # https://github.com/pytorch/pytorch/blob/0c7ca2d97ba5980a2af7dcd6b8106dc915e591cd/torch/distributed/optim/zero_redundancy_optimizer.py#L1131 def get_zero_optimizer_state_dict_local(optimizer, global_rank): optimizer._check_overlap_initialized() # Sync the exposed `param_groups` attributes to the local optimizer in # case they have been updated optimizer._sync_param_groups(optimizer.param_groups, optimizer.optim.param_groups) local_state_dict = optimizer.optim.state_dict() state_dict = super(ZeroRedundancyOptimizer, optimizer).state_dict() # Update the global optimizer state with local state information, # factoring in the translation from local to global indexing rank = global_rank # TODO: recursive copy to device local_param_groups = local_state_dict["param_groups"] global_param_groups = optimizer._partition_parameters()[rank] assert len(local_param_groups) == len(global_param_groups), \ "Mismatch between number of local and global parameter groups" for local_param_group, global_param_group in zip(local_param_groups, global_param_groups): # `local_param_group` stores local indices, while # `global_param_group` stores the tensors directly local_param_indices = local_param_group["params"] global_params = global_param_group["params"] assert len(local_param_indices) == len(global_params), \ "Mismatch between number of local and global parameters in parameter group" for local_param_index, global_param in zip(local_param_indices, global_params): # Update the global parameter state, if any if local_param_index in local_state_dict["state"]: global_param_index = optimizer._param_to_index[global_param] state_dict["state"][global_param_index] = local_state_dict["state"][local_param_index] # Sort the parameters in the state state_dict["state"] = dict(sorted(state_dict["state"].items())) return state_dict class DDPStrategyZero1(DDPStrategy): """To use ZeroRedundancyOptimizer, we need to shard the optimizer states when saving/loading checkpoints. """ strategy_name = "ddp_zero1" def optimizer_state(self, optimizer: Optimizer) -> Optional[dict]: if isinstance(optimizer, LightningOptimizer): optimizer = optimizer._optimizer if isinstance(optimizer, ZeroRedundancyOptimizer): return get_zero_optimizer_state_dict_local(optimizer, self.global_rank) else: return optimizer.state_dict() def save_checkpoint( self, checkpoint: Dict[str, Any], filepath: _PATH, storage_options: Optional[Any] = None ) -> None: """Save model/training states as a checkpoint file through state-dump and file-write. Args: checkpoint: dict containing model and trainer state filepath: write-target file's path storage_options: parameter for how to save to storage, passed to ``CheckpointIO`` plugin """ filepath = Path(filepath) filepath.mkdir(parents=True, exist_ok=True) local_optimizer_states = checkpoint.pop('optimizer_states') if self.is_global_zero: self.checkpoint_io.save_checkpoint(checkpoint, filepath / 'model_states.pt', storage_options=storage_options) self.checkpoint_io.save_checkpoint(local_optimizer_states, filepath / f'{self.global_rank:03d}_optim_states.pt', storage_options=storage_options) def load_checkpoint(self, checkpoint_path: _PATH) -> Dict[str, Any]: torch.cuda.empty_cache() checkpoint_path = Path(checkpoint_path) if checkpoint_path.is_file(): return super().load_checkpoint(self, str(checkpoint_path)) else: assert checkpoint_path.is_dir() global_states = self.checkpoint_io.load_checkpoint(checkpoint_path / 'model_states.pt') local_optimizer_states = self.checkpoint_io.load_checkpoint(checkpoint_path / f'{self.global_rank:03d}_optim_states.pt') global_states['optimizer_states'] = local_optimizer_states return global_states
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/utils/ddp_zero1.py
import math from functools import partial from collections import namedtuple import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.modules.utils import _pair import hydra from einops import reduce, rearrange def pooling(x, pooling_mode='CLS', key_padding_mask=None, batch_first=True): if pooling_mode not in ['MEAN', 'SUM', 'CLS', 'LAST', 'FLATTEN']: raise NotImplementedError(f'pooling_mode must be MEAN, SUM, CLS, LAST, FLATTEN') if pooling_mode in ['MEAN', 'SUM']: if key_padding_mask is not None: mask = rearrange(~key_padding_mask.bool_matrix, 'b s -> b s 1' if batch_first else 'b s -> s b 1') x = x.masked_fill(mask, 0) s = reduce(x, 'b s ... -> b ...' if batch_first else 's b ... -> b ...', 'sum') if pooling_mode == 'SUM': return s else: if key_padding_mask is None: return s / x.shape[1 if batch_first else 0] else: lengths = rearrange(key_padding_mask._lengths, 'b -> b 1') return s / lengths elif pooling_mode == 'CLS': return x[:, 0] if batch_first else x[0] elif pooling_mode == 'LAST': if key_padding_mask is None: return x[:, -1] if batch_first else x[-1] else: lengths = key_padding_mask._lengths if batch_first: batch_size = x.shape[0] return x[torch.arange(batch_size, device=x.device), lengths - 1] else: batch_size = x.shape[1] return x[lengths - 1, torch.arange(batch_size, device=x.device)] elif pooling_mode == 'FLATTEN': return rearrange(x, 'b ... -> b (...)' if batch_first else 's b ... -> b (s ...)') class ClassificationHeadLinear(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, d_model, num_classes, pooling_mode='MEAN', batch_first=False, **kwargs): super().__init__() assert pooling_mode in ['MEAN', 'SUM', 'CLS', 'LAST', 'FLATTEN'], 'pooling_mode not supported' self.pooling_mode = pooling_mode self.batch_first = batch_first self.out_proj = nn.Linear(d_model, num_classes) def forward(self, hidden_states, key_padding_mask=None, **kwargs): """ hidden_states: (B, S, D) if batch_first else (S, B, D) """ hidden_states = pooling(hidden_states, pooling_mode=self.pooling_mode, key_padding_mask=key_padding_mask, batch_first=self.batch_first) hidden_states = self.out_proj(hidden_states) return hidden_states # Adapted from https://github.com/huggingface/transformers/blob/master/src/transformers/models/reformer/modeling_reformer.py class ClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, d_model, d_inner, num_classes, dropout=0.0, pooling_mode='MEAN', batch_first=False): super().__init__() assert pooling_mode in ['MEAN', 'SUM', 'CLS', 'LAST', 'FLATTEN'], 'pooling_mode not supported' self.pooling_mode = pooling_mode self.batch_first = batch_first self.dense = nn.Linear(d_model, d_inner) self.dropout = nn.Dropout(dropout) self.out_proj = nn.Linear(d_inner, num_classes) def forward(self, hidden_states, key_padding_mask=None, **kwargs): """ hidden_states: (B, S, D) if batch_first else (S, B, D) """ hidden_states = pooling(hidden_states, pooling_mode=self.pooling_mode, key_padding_mask=key_padding_mask, batch_first=self.batch_first) hidden_states = self.dropout(hidden_states) hidden_states = self.dense(hidden_states) # Huggingface uses tanh instead of relu hidden_states = torch.relu(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.out_proj(hidden_states) return hidden_states class ClassificationHeadDual(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, d_model, d_inner, num_classes, dropout=0.0, pooling_mode='MEAN', batch_first=False, interaction='NLI'): super().__init__() assert pooling_mode in ['MEAN', 'SUM', 'CLS'], 'pooling_mode not supported' assert interaction in [None, 'NLI'], 'interaction not supported' self.pooling_mode = pooling_mode self.batch_first = batch_first self.interaction = interaction self.dense = nn.Linear(d_model * (4 if self.interaction == 'NLI' else 2), d_inner) self.dropout = nn.Dropout(dropout) self.out_proj = nn.Linear(d_inner, num_classes) def forward(self, hidden_states1, hidden_states2, key_padding_mask1=None, key_padding_mask2=None, **kwargs): """ hidden_states: (B, S, D) if batch_first else (S, B, D) """ x1 = pooling(hidden_states1, pooling_mode=self.pooling_mode, key_padding_mask=key_padding_mask1, batch_first=self.batch_first) x2 = pooling(hidden_states2, pooling_mode=self.pooling_mode, key_padding_mask=key_padding_mask2, batch_first=self.batch_first) hidden_states = (torch.cat([x1, x2, x1 * x2, x1 - x2], dim=-1) if self.interaction == 'NLI' else torch.cat([x1, x2], dim=-1)) hidden_states = self.dropout(hidden_states) hidden_states = self.dense(hidden_states) # Huggingface uses tanh instead of relu hidden_states = torch.relu(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.out_proj(hidden_states) return hidden_states class LMHead(nn.Module): def __init__(self, d_model, num_classes, batch_first=True, bias=True): super().__init__() self.lm_head = nn.Linear(d_model, num_classes, bias=bias) def forward(self, hidden_states, **kwargs): """ hidden_states: (B, S, D) if batch_first else (S, B, D) """ CausalLMOutput = namedtuple('CausalLMOutput', ['logits']) return CausalLMOutput(self.lm_head(hidden_states)) def sinusoidal_init_(tensor): """ tensor: (max_len, d_model) """ max_len, d_model = tensor.shape position = rearrange(torch.arange(0.0, max_len), 's -> s 1') div_term = torch.exp(-math.log(10000.0) * torch.arange(0.0, d_model, 2.0) / d_model) tensor[:, 0::2] = torch.sin(position * div_term) tensor[:, 1::2] = torch.cos(position * div_term) return tensor # Adapted from https://github.com/pytorch/examples/blob/master/word_language_model/model.py class PositionalEncoding(nn.Module): r"""Inject some information about the relative or absolute position of the tokens in the sequence. The positional encodings have the same dimension as the embeddings, so that the two can be summed. Here, we use sine and cosine functions of different frequencies. .. math:: \text{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model)) \text{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model)) \text{where pos is the word position and i is the embed idx) Args: d_model: the embed dim (required). dropout: the dropout value (default=0.1). max_len: the max. length of the incoming sequence (default=5000). Examples: >>> pos_encoder = PositionalEncoding(d_model) """ def __init__(self, d_model, dropout=0.1, max_len=5000, batch_first=False, initializer=None): super().__init__() self.batch_first = batch_first self.dropout = nn.Dropout(p=dropout) pe = torch.empty(max_len, d_model) if initializer is None: sinusoidal_init_(pe) pe = rearrange(pe, 's d -> 1 s d' if self.batch_first else 's d -> s 1 d') self.register_buffer('pe', pe) else: hydra.utils.call(initializer, pe) pe = rearrange(pe, 's d -> 1 s d' if self.batch_first else 's d -> s 1 d') self.pe = nn.Parameter(pe) def forward(self, x): r"""Inputs of forward function Args: x: the sequence fed to the positional encoder model (required). Shape: x: [sequence length, batch size, embed dim] if not batch_first else [B, S, D] output: [sequence length, batch size, embed dim] if not batch_first else [B, S, D] Examples: >>> output = pos_encoder(x) """ x = x + (self.pe[:, :x.size(1)] if self.batch_first else self.pe[:x.size(0)]) return self.dropout(x) # Adapted from https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/mlp.py class Mlp(nn.Module): """ MLP as used in Vision Transformer, MLP-Mixer and related networks """ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, act_fn=None, drop=0., device=None, dtype=None): """TD [2021-10-27] act_fn takes precedence over act_layer if set. This is to support Pytorch 1.10 Transformer interface that construct the activation *function*, not the activation *layer*. """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features drop_probs = _pair(drop) self.fc1 = nn.Linear(in_features, hidden_features, **factory_kwargs) self.act = act_layer() if act_fn is None else act_fn self.drop1 = nn.Dropout(drop_probs[0]) self.fc2 = nn.Linear(hidden_features, out_features, **factory_kwargs) self.drop2 = nn.Dropout(drop_probs[1]) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop1(x) x = self.fc2(x) x = self.drop2(x) return x class MlpBig(nn.Module): """ MLP as used in Vision Transformer, MLP-Mixer and related networks """ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, act_fn=None, drop=0., device=None, dtype=None): """Copied from Mlp above. If num_layers > 2, add more Mlp layers, doubling each time. """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features cur_hidden_features = hidden_features layers = [] for _ in range(4): layers.append(nn.Linear(in_features, cur_hidden_features, **factory_kwargs)) layers.append(act_layer()) layers.append(nn.Dropout(drop)) in_features = cur_hidden_features cur_hidden_features *= 2 layers.append(nn.Linear(in_features, out_features, **factory_kwargs)) layers.append(nn.Dropout(drop)) self.fwd = nn.Sequential(*layers) def forward(self, x): return self.fwd(x) class GluMlp(nn.Module): """ MLP w/ GLU style gating See: https://arxiv.org/abs/1612.08083, https://arxiv.org/abs/2002.05202 """ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.Sigmoid, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features assert hidden_features % 2 == 0 self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features // 2, out_features) self.drop = nn.Dropout(drop) def init_weights(self): # override init of fc1 w/ gate portion set to weight near zero, bias=1 fc1_mid = self.fc1.bias.shape[0] // 2 nn.init.ones_(self.fc1.bias[fc1_mid:]) nn.init.normal_(self.fc1.weight[fc1_mid:], std=1e-6) def forward(self, x): x = self.fc1(x) x, gates = x.chunk(2, dim=-1) x = x * self.act(gates) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x class GatedMlp(nn.Module): """ MLP as used in gMLP """ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, gate_layer=None, drop=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() if gate_layer is not None: assert hidden_features % 2 == 0 self.gate = gate_layer(hidden_features) hidden_features = hidden_features // 2 # FIXME base reduction on gate property? else: self.gate = nn.Identity() 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.gate(x) x = self.fc2(x) x = self.drop(x) return x class ConvMlp(nn.Module): """ MLP using 1x1 convs that keeps spatial dims """ def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU, norm_layer=None, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Conv2d(in_features, hidden_features, kernel_size=1, bias=True) self.norm = norm_layer(hidden_features) if norm_layer else nn.Identity() self.act = act_layer() self.fc2 = nn.Conv2d(hidden_features, out_features, kernel_size=1, bias=True) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.norm(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) return x
EXA-1-master
exa/modular_components/attentions/flash-attention/training/src/models/modules/seq_common.py
import math import torch import torch.nn.functional as F import pytest from einops import rearrange from flash_attn.layers.rotary import apply_rotary_emb_func, apply_rotary_emb_torch is_sm8x = torch.cuda.get_device_capability('cuda') >= (8, 0) @pytest.mark.parametrize('dtype', ([torch.float16] if not is_sm8x else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', ([torch.float16])) @pytest.mark.parametrize('rotary_fraction', [1.0, 0.5]) # @pytest.mark.parametrize('rotary_fraction', [0.5]) @pytest.mark.parametrize('inplace', [False, True]) # @pytest.mark.parametrize('inplace', [False]) def test_rotary_single_tensor(inplace, rotary_fraction, dtype): rtol = 1e-3 batch_size = 32 nheads = 4 seqlen = 217 headdim = 128 x = torch.randn(batch_size, seqlen, nheads, headdim, dtype=dtype, device='cuda', requires_grad=True) x_pt = x.detach().clone().requires_grad_() rotary_dim = int(rotary_fraction * headdim) assert rotary_dim % 2 == 0 angle = torch.randn(seqlen, rotary_dim // 2, device='cuda') cos = torch.cos(angle).to(dtype=dtype) sin = torch.sin(angle).to(dtype=dtype) out = apply_rotary_emb_func(x, cos, sin, inplace) out_pt = apply_rotary_emb_torch(x_pt, cos, sin) # Numerical error if we just do any arithmetic atol = ((out + 0.3 - 0.3) - out).abs().max().item() assert torch.allclose(out, out_pt, rtol=rtol, atol=2 * atol) g = torch.randn_like(out) g_pt = g.clone() # If inplace=True, we might modify the gradient inplace out.backward(g) out_pt.backward(g_pt) atol = ((x_pt.grad + 0.3 - 0.3) - x_pt.grad).abs().max().item() assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=2 * atol)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/test_rotary.py
import math from functools import partial import torch import torch.nn.functional as F import pytest from einops import rearrange, repeat from flash_attn.flash_attn_interface import flash_attn_func, flash_attn_unpadded_qkvpacked_func, _get_block_size, flash_attn_unpadded_kvpacked_func, flash_attn_unpadded_func from flash_attn.flash_attn_interface import flash_attn_unpadded_qkvpacked_split_func from flash_attn.bert_padding import unpad_input, pad_input, index_first_axis try: from flash_attn.flash_attn_triton import flash_attn_func except (ImportError, AttributeError): # Older version of Triton doesn't have tl.constexpr flash_attn_func = None is_sm75 = torch.cuda.get_device_capability('cuda') == (7, 5) is_sm80 = torch.cuda.get_device_capability('cuda') == (8, 0) def generate_random_padding_mask(max_seqlen, batch_size, device, mode='random'): assert mode in ['full', 'random', 'third', 'split'] if mode == 'full': lengths = torch.full((batch_size, 1), max_seqlen, device=device, dtype=torch.int32) elif mode == 'random': lengths = torch.randint(max(1, max_seqlen - 20), max_seqlen + 1, (batch_size, 1), device=device) elif mode == 'third': lengths = torch.randint(max_seqlen // 3, max_seqlen + 1, (batch_size, 1), device=device) elif mode == 'split': lengths0 = torch.randint(min(128, max_seqlen), max_seqlen + 1, (batch_size // 4 * 3, 1), device=device) lengths1 = torch.randint(min(max(1, max_seqlen - 20), 128), min(max_seqlen, 128) + 1, (batch_size - batch_size // 4 * 3, 1), device=device) lengths = torch.cat([lengths0, lengths1], dim=0) padding_mask = repeat(torch.arange(max_seqlen, device=device), 's -> b s', b=batch_size) < lengths return padding_mask def generate_qkv(x, Wqkv, nheads, query_padding_mask=None, key_padding_mask=None, kvpacked=False, qkvpacked=False): """ Arguments: x: (batch_size, seqlen, nheads * d) Wqkv: nn.Linear(nheads * d, 3 * nheads * d) query_padding_mask: (batch_size, seqlen), bool key_padding_mask: (batch_size, seqlen), bool """ assert not (kvpacked and qkvpacked) batch_size, seqlen, dim = x.shape q, k, v = Wqkv(x).chunk(3, dim=-1) if query_padding_mask is not None: q_unpad, indices_q, cu_seqlens_q, max_seqlen_q = unpad_input(q, query_padding_mask) q_unpad = rearrange(q_unpad, 'nnz (h d) -> nnz h d', h=nheads) output_pad_fn = lambda output_unpad: rearrange( pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'), indices_q, batch_size, seqlen), 'b s (h d) -> b s h d', h=nheads ) else: q_unpad = rearrange(q, 'b s (h d) -> (b s) h d', h=nheads) cu_seqlens_q = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=q_unpad.device) max_seqlen_q = seqlen output_pad_fn = lambda output_unpad: rearrange(output_unpad, '(b s) h d -> b s h d', b=batch_size) if key_padding_mask is not None: k_unpad, indices_k, cu_seqlens_k, max_seqlen_k = unpad_input(k, key_padding_mask) k_unpad = rearrange(k_unpad, 'nnz (h d) -> nnz h d', h=nheads) v_unpad, _, _, _ = unpad_input(v, key_padding_mask) v_unpad = rearrange(v_unpad, 'nnz (h d) -> nnz h d', h=nheads) else: k_unpad = rearrange(k, 'b s (h d) -> (b s) h d', h=nheads) v_unpad = rearrange(v, 'b s (h d) -> (b s) h d', h=nheads) cu_seqlens_k = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=q_unpad.device) max_seqlen_k = seqlen if qkvpacked: assert (query_padding_mask == key_padding_mask).all() qkv_unpad = torch.stack([q_unpad, k_unpad, v_unpad], dim=1) qkv = rearrange(torch.stack([q, k, v], dim=2), 'b s t (h d) -> b s t h d', h=nheads) if query_padding_mask is not None: dqkv_pad_fn = lambda dqkv_unpad: rearrange( pad_input(rearrange(dqkv_unpad, 'nnz t h d -> nnz (t h d)'), indices_q, batch_size, seqlen), 'b s (t h d) -> b s t h d', t=3, h=nheads ) else: dqkv_pad_fn = lambda dqkv_unpad: rearrange(dqkv_unpad, '(b s) t h d -> b s t h d', b=batch_size) return (qkv_unpad.detach().requires_grad_(), cu_seqlens_q, max_seqlen_q, qkv.detach().requires_grad_(), output_pad_fn, dqkv_pad_fn) elif kvpacked: kv_unpad = torch.stack([k_unpad, v_unpad], dim=1) q = rearrange(q, 'b s (h d) -> b s h d', h=nheads) kv = rearrange(torch.stack([k, v], dim=2), 'b s t (h d) -> b s t h d', h=nheads) dq_pad_fn = output_pad_fn if key_padding_mask is not None: dkv_pad_fn = lambda dkv_unpad: rearrange( pad_input(rearrange(dkv_unpad, 'nnz t h d -> nnz (t h d)'), indices_k, batch_size, seqlen), 'b s (t h d) -> b s t h d', t=2, h=nheads ) else: dkv_pad_fn = lambda dkv_unpad: rearrange(dkv_unpad, '(b s) t h d -> b s t h d', b=batch_size) return (q_unpad.detach().requires_grad_(), kv_unpad.detach().requires_grad_(), cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, q.detach().requires_grad_(), kv.detach().requires_grad_(), output_pad_fn, dq_pad_fn, dkv_pad_fn) else: q, k, v = [rearrange(z, 'b s (h d) -> b s h d', h=nheads).detach().requires_grad_() for z in [q, k, v]] dq_pad_fn = output_pad_fn if key_padding_mask is not None: dk_pad_fn = lambda dk_unpad: rearrange( pad_input(rearrange(dk_unpad, 'nnz h d -> nnz (h d)'), indices_k, batch_size, seqlen), 'b s (h d) -> b s h d', h=nheads ) else: dk_pad_fn = lambda dk_unpad: rearrange(dk_unpad, '(b s) h d -> b s h d', b=batch_size) return (q_unpad.detach().requires_grad_(), k_unpad.detach().requires_grad_(), v_unpad.detach().requires_grad_(), cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, q, k, v, output_pad_fn, dq_pad_fn, dk_pad_fn) def attention_ref(q, k, v, query_padding_mask=None, key_padding_mask=None, dropout_p=0.0, dropout_mask=None, causal=False, bias=None, upcast=True, reorder_ops=False): """ Arguments: q: (batch_size, seqlen_q, nheads, head_dim) k: (batch_size, seqlen_k, nheads, head_dim) v: (batch_size, seqlen_k, nheads, head_dim) query_padding_mask: (batch_size, seqlen_q) key_padding_mask: (batch_size, seqlen_k) dropout_p: float dropout_mask: (batch_size, nheads, seqlen_q, seqlen_k) bias: (batch_size, nheads, seqlen_q, seqlen_k) upcast: whether to cast all inputs to fp32, do all computation in fp32, then cast output back to fp16/bf16. reorder_ops: whether to change the order of operations (scaling k instead of scaling k, etc.) without changing the math. This is to estimate the numerical error from operation reordering. Output: output: (batch_size, seqlen_q, nheads, head_dim) attention: (batch_size, nheads, seqlen_q, seqlen_k), softmax after dropout """ dtype_og = q.dtype if upcast: q, k, v = q.float(), k.float(), v.float() seqlen_q, seqlen_k = q.shape[1], k.shape[1] d = q.shape[-1] if not reorder_ops: scores = torch.einsum('bthd,bshd->bhts', q / math.sqrt(d), k) else: scores = torch.einsum('bthd,bshd->bhts', q, k / math.sqrt(d)) if bias is not None: scores = (scores + bias).to(dtype=scores.dtype) if key_padding_mask is not None: scores.masked_fill_(rearrange(~key_padding_mask, 'b s -> b 1 1 s'), float('-inf')) if causal: causal_mask = torch.triu(torch.ones(seqlen_q, seqlen_k, dtype=torch.bool, device=q.device), 1) scores.masked_fill_(causal_mask, float('-inf')) attention = torch.softmax(scores, dim=-1) dropout_scaling = 1.0 / (1 - dropout_p) # attention_drop = attention.masked_fill(~dropout_mask, 0.0) * dropout_scaling # output = torch.einsum('bhts,bshd->bthd', attention_drop , v) if dropout_mask is not None: attention_drop = attention.masked_fill(~dropout_mask, 0.0) else: attention_drop = attention output = torch.einsum('bhts,bshd->bthd', attention_drop, v * dropout_scaling) if query_padding_mask is not None: output.masked_fill_(rearrange(~query_padding_mask, 'b s -> b s 1 1'), 0.0) attention = attention.masked_fill(rearrange(~query_padding_mask, 'b s -> b 1 s 1'), 0.0) return output.to(dtype=dtype_og), attention.to(dtype=dtype_og) def attention_kvpacked_ref(q, kv, query_padding_mask=None, key_padding_mask=None, dropout_p=0.0, dropout_mask=None, causal=False, upcast=True, reorder_ops=False): return attention_ref(q, kv[:, :, 0], kv[:, :, 1], query_padding_mask, key_padding_mask, dropout_p, dropout_mask, upcast=upcast, causal=causal, reorder_ops=reorder_ops) def attention_qkvpacked_ref(qkv, key_padding_mask=None, dropout_p=0.0, dropout_mask=None, causal=False, upcast=True, reorder_ops=False): return attention_ref(qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], key_padding_mask, key_padding_mask, dropout_p, dropout_mask, upcast=upcast, causal=causal, reorder_ops=reorder_ops) def generate_sparsity_mask(seqlen, sparsity=0.3): repeats = seqlen // 16 // 2 # mask = torch.stack([torch.tensor([1, 0] * repeats, dtype=torch.bool, device='cuda'), # torch.tensor([0, 1] * repeats, dtype=torch.bool, device='cuda')], dim=-1) # mask = torch.stack([torch.tensor([1, 1] * repeats, dtype=torch.bool, device='cuda'), # torch.tensor([1, 1] * repeats, dtype=torch.bool, device='cuda')], dim=-1) # mask = torch.stack([torch.tensor([1, 1] * repeats, dtype=torch.bool, device='cuda')], dim=-1) # mask = torch.stack([torch.tensor([1, 0] * repeats, dtype=torch.bool, device='cuda')], dim=-1) nrow, ncol = seqlen // 16, seqlen // 256 mask = torch.rand(nrow, ncol, device='cuda') < sparsity return mask def attention_blocksparse_ref(qkv, blockmask, attn_mask, dropout_p, dropout_mask): """ Arguments: qkv: (batch_size, seqlen, 3, nheads, head_dim) blockmask: (seqlen / 16, seqlen / 256) attn_mask: (batch_size, seqlen) dropout_p: float dropout_mask: (batch_size, nheads, seqlen, seqlen) Output: output: (batch_size, seqlen, nheads, head_dim) attention: softmax after dropout """ q, k, v = qkv.float().unbind(dim=2) d = qkv.shape[-1] seqlen = qkv.shape[1] scores = torch.einsum('bthd,bshd->bhts', q / math.sqrt(d), k) scores.masked_fill_(rearrange(~attn_mask, 'b s -> b 1 1 s'), float('-inf')) blockmask = repeat(blockmask, 's_16 s_256 -> (s_16 16) (s_256 256)') blockmask = blockmask[:seqlen, :seqlen] scores.masked_fill_(rearrange(~blockmask, 't s -> 1 1 t s'), float('-inf')) attention = torch.softmax(scores, dim=-1) attention = attention.masked_fill(rearrange(~attn_mask, 'b s -> b 1 s 1'), 0.0) attention = attention.masked_fill_(rearrange(~blockmask, 't s -> 1 1 t s'), 0.0) attention_drop = attention.masked_fill(~dropout_mask, 0.0) / (1 - dropout_p) output = torch.einsum('bhts,bshd->bthd', attention_drop , v) output.masked_fill_(rearrange(~attn_mask, 'b s -> b s 1 1'), 0) return output.to(dtype=qkv.dtype), attention.to(dtype=qkv.dtype) def convert_flash_attn_S_to_softmax(S, query_padding_mask, key_padding_mask, head_dim, is_dropout, causal=False): """FlashAttention stores the S matrix in a different way. Arguments: S: (batch_size, nheads, seqlen_q, seqlen_k) query_padding_mask: (batch_size, seqlen_q) key_padding_mask: (batch_size, seqlen_k) """ S_flat = rearrange(S, 'b h t s -> b h (t s)') seqlen_q, seqlen_k = S.shape[-2:] block_size = _get_block_size(S.device, head_dim, is_dropout) loop_steps = (seqlen_k + block_size - 1) // block_size warps_n = 4 mmas_n = (seqlen_k // warps_n // 16) if seqlen_k <= block_size else (block_size // warps_n // 16) S_converted = rearrange(S_flat, 'b h (loop nsteps mmas_n warps_n eight t r c0 c1) -> b h (nsteps r eight) (loop mmas_n warps_n c0 t c1)', loop=loop_steps, nsteps=seqlen_q // 16, mmas_n=mmas_n, warps_n=warps_n, eight=8, t=4, r=2, c0=2, c1=2) # Need to zero out things not in attention_mask in case S was initialized with random values # and some of those values aren't overwritten. seqlen_q_og = query_padding_mask.shape[-1] if seqlen_q_og < seqlen_q: query_padding_mask = F.pad(query_padding_mask, (0, seqlen_q - seqlen_q_og)) else: query_padding_mask = query_padding_mask[:, :seqlen_q] S_converted = S_converted.masked_fill(rearrange(~query_padding_mask, 'b s -> b 1 s 1'), 0.0) seqlen_k_og = key_padding_mask.shape[-1] if seqlen_k_og < seqlen_k: key_padding_mask = F.pad(key_padding_mask, (0, seqlen_k - seqlen_k_og)) else: key_padding_mask = key_padding_mask[:, :seqlen_k] S_converted = S_converted.masked_fill(rearrange(~key_padding_mask, 'b s -> b 1 1 s'), 0.0) if causal: causal_mask = torch.triu(torch.ones(seqlen_q, seqlen_k, dtype=torch.bool, device=S.device), 1) S_converted.masked_fill_(causal_mask, 0.0) if seqlen_q_og < seqlen_q: S_converted = S_converted[:, :, :seqlen_q_og, :] else: S_converted = F.pad(S_converted, (0, 0, 0, seqlen_q_og - seqlen_q)) if seqlen_k_og < seqlen_k: S_converted = S_converted[:, :, :, :seqlen_k_og] else: S_converted = F.pad(S_converted, (0, seqlen_k_og - seqlen_k)) return S_converted def normalize_flash_attn_S(attn_unnorm, q, k, v, query_padding_mask=None, key_padding_mask=None, is_dropout=False, causal=False): """ Arguments: q: (batch_size, seqlen_q, nheads, head_dim) k, v: (batch_size, seqlen_k, nheads, head_dim) key_padding_mask: (batch_size, seqlen_q) Output: softmax_lse: (batch_size, nheads, seqlen_q) softmax_max: (batch_size, nheads, seqlen_q) """ q, k, v = q.float(), k.float(), v.float() _, seqlen_q, _, head_dim = q.shape seqlen_k = k.shape[1] scores = torch.einsum('bthd,bshd->bhts', q / math.sqrt(head_dim), k) if key_padding_mask is not None: scores.masked_fill_(rearrange(~key_padding_mask, 'b s -> b 1 1 s'), float('-inf')) if causal: causal_mask = torch.triu(torch.ones(seqlen_q, seqlen_k, dtype=torch.bool, device=q.device), 1) scores.masked_fill_(causal_mask, float('-inf')) block_size = _get_block_size(scores.device, head_dim, is_dropout) scores_block = scores.split(block_size, dim=-1) lse_block = torch.stack([torch.logsumexp(s, dim=-1) for s in scores_block], dim=-1) lcse_block = torch.logcumsumexp(lse_block, dim=-1).unbind(dim=-1) scores_max_block = ([torch.amax(scores_block[0], dim=-1)] + [torch.maximum(torch.amax(s, dim=-1), lcse) for s, lcse in zip(scores_block[1:], lcse_block[:-1])]) attn_unnorm_block = attn_unnorm.split(block_size, dim=-1) attn_norm = torch.cat([a / rearrange(torch.exp(lcse_block[-1] - m), 'b h s -> b h s 1') for a, m in zip(attn_unnorm_block, scores_max_block)], dim=-1) if query_padding_mask is not None: attn_norm.masked_fill_(rearrange(~query_padding_mask, 'b s -> b 1 s 1'), 0.0) return attn_norm.to(dtype=attn_unnorm.dtype) def get_dropout_fraction(dropout_mask, query_padding_mask=None, key_padding_mask=None, causal=False): """ dropout_mask: (batch_size, nheads, seqlen_q, seqlen_k), bool. True means keep, False means drop. query_padding_mask: (batch_size, seqlen_q) key_padding_mask: (batch_size, seqlen_k) """ batch_size, nheads, seqlen_q, seqlen_k = dropout_mask.shape dropped = ~dropout_mask if query_padding_mask is not None: dropped.masked_fill_(rearrange(~query_padding_mask, 'b s -> b 1 s 1'), False) if key_padding_mask is not None: dropped.masked_fill_(rearrange(~key_padding_mask, 'b s -> b 1 1 s'), False) if causal: causal_mask = torch.triu(torch.ones(seqlen_q, seqlen_k, dtype=torch.bool, device=dropout_mask.device), 1) dropped.masked_fill_(causal_mask, False) dropped_total = dropped.sum() query_lengths = (query_padding_mask.sum(dim=-1) if query_padding_mask is not None else torch.full((batch_size,), seqlen_q, device=dropout_mask.device)) key_lengths = (key_padding_mask.sum(dim=-1) if key_padding_mask is not None else torch.full((batch_size,), seqlen_k, device=dropout_mask.device)) if not causal: numel_per_batch = query_lengths * key_lengths else: numel_per_batch = torch.where( query_lengths <= key_lengths, query_lengths * (query_lengths + 1) / 2, query_lengths * key_lengths - (key_lengths * (key_lengths - 1) / 2) ) return dropped_total / (numel_per_batch.sum() * nheads) @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('causal', [False, True]) # @pytest.mark.parametrize('causal', [False]) @pytest.mark.parametrize('d', [128, 64, 80, 40, 32, 16]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen', [97, 128, 200, 256, 257, 384, 512, 768, 1024, 1025, 2048]) # @pytest.mark.parametrize('seqlen', [128]) @pytest.mark.parametrize('dropout_p', [0.0, 0.17]) # @pytest.mark.parametrize('dropout_p', [0.0]) def test_flash_attn_unpadded_qkvpacked(seqlen, d, dropout_p, causal, dtype): if seqlen >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # if dtype == torch.float16: # rtol, atol = (1e-3, 3e-4) if not causal else (1e-3, 1e-3) # else: # torch.bfloat16 # rtol, atol = (3e-3, 3e-3) if not causal else (1e-3, 1e-3) # set seed torch.random.manual_seed(0) # Set smaller batch size so it would trigger num_splits > 1 batch_size = 8 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') # key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='full') qkv_unpad, cu_seqlens, max_seqlen, qkv, output_pad_fn, dqkv_pad_fn = generate_qkv( x, Wqkv, nheads, key_padding_mask, key_padding_mask, qkvpacked=True ) output_unpad, sm_lse, S_dmask = flash_attn_unpadded_qkvpacked_func( qkv_unpad, cu_seqlens, max_seqlen, dropout_p, return_attn_probs=True, causal=causal ) output = output_pad_fn(output_unpad) S_dmask_converted = convert_flash_attn_S_to_softmax( S_dmask, key_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) dropout_mask = S_dmask_converted >= 0 attn_unnorm = S_dmask_converted.abs() attn = normalize_flash_attn_S(attn_unnorm, qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], key_padding_mask, key_padding_mask, dropout_p > 0.0, causal=causal) dropout_fraction = get_dropout_fraction(dropout_mask, key_padding_mask, key_padding_mask, causal=causal).item() output_ref, attn_ref = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal) output_pt, attn_pt = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal, upcast=False, reorder_ops=True) print(f'Actual dropout fraction: {dropout_fraction}') print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') print(f'Attention max diff: {(attn - attn_ref).abs().max().item()}') print(f'Attention Pytorch max diff: {(attn_pt - attn_ref).abs().max().item()}') if is_sm80 or d <= 64: # Only run backward for d=128 on A100 g = torch.randn_like(output) dqkv_unpad, = torch.autograd.grad(output, qkv_unpad, g) dqkv = dqkv_pad_fn(dqkv_unpad) dqkv_ref, = torch.autograd.grad(output_ref, qkv, g) dqkv_pt, = torch.autograd.grad(output_pt, qkv, g) print(f'dQ max diff: {(dqkv[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK max diff: {(dqkv[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV max diff: {(dqkv[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV mean diff: {(dqkv - dqkv_ref).abs().mean().item()}') print(f'dQ Pytorch max diff: {(dqkv_pt[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK Pytorch max diff: {(dqkv_pt[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV Pytorch max diff: {(dqkv_pt[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV Pytorch mean diff: {(dqkv_pt - dqkv_ref).abs().mean().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (attn - attn_ref).abs().max().item() <= 2 * (attn_pt - attn_ref).abs().max().item() # assert torch.allclose(attn, attn_ref, rtol=rtol, atol=atol) if dropout_p == 0.0: assert dropout_mask.all() else: assert 0.98 <= dropout_fraction / dropout_p <= 1.02 if is_sm80 or d <= 64: # Only run backward for d=128 on A100 # Error for dK and dV could be a bit higher if we're splitting along seqlen_q dimension assert (dqkv - dqkv_ref).abs().max().item() <= 4 * (dqkv_pt - dqkv_ref).abs().max().item() # assert torch.allclose(dqkv, dqkv_ref, rtol=rtol, atol=atol) @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('causal', [False, True]) @pytest.mark.parametrize('d', [128, 64, 80, 40, 32, 16]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen', [97, 128, 200, 256, 257, 384, 512, 768, 1024, 1025, 2048]) # @pytest.mark.parametrize('seqlen', [128]) @pytest.mark.parametrize('dropout_p', [0.0, 0.17]) # @pytest.mark.parametrize('dropout_p', [0.0]) def test_flash_attn_unpadded_kvpacked(seqlen, d, dropout_p, causal, dtype): if seqlen >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # if dtype == torch.float16: # rtol, atol = (1e-3, 3e-4) if not causal else (1e-3, 1e-3) # else: # torch.bfloat16 # rtol, atol = (3e-3, 3e-3) if not causal else (1e-3, 1e-3) # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) query_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') (q_unpad, kv_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, q, kv, output_pad_fn, dq_pad_fn, dkv_pad_fn) = generate_qkv( x, Wqkv, nheads, query_padding_mask, key_padding_mask, kvpacked=True ) output_unpad, sm_lse, S_dmask = flash_attn_unpadded_kvpacked_func( q_unpad, kv_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, return_attn_probs=True, causal=causal ) output = output_pad_fn(output_unpad) S_dmask_converted = convert_flash_attn_S_to_softmax( S_dmask, query_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) dropout_mask = S_dmask_converted >= 0 attn_unnorm = S_dmask_converted.abs() attn = normalize_flash_attn_S(attn_unnorm, q, kv[:, :, 0], kv[:, :, 1], query_padding_mask, key_padding_mask, dropout_p > 0.0, causal=causal) dropout_fraction = get_dropout_fraction(dropout_mask, query_padding_mask, key_padding_mask, causal=causal) output_ref, attn_ref = attention_kvpacked_ref(q, kv, query_padding_mask, key_padding_mask, dropout_p, dropout_mask, causal=causal) output_pt, attn_pt = attention_kvpacked_ref(q, kv, query_padding_mask, key_padding_mask, dropout_p, dropout_mask, causal=causal, upcast=False, reorder_ops=True) print(f'Actual dropout fraction: {dropout_fraction}') print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') print(f'Attention max diff: {(attn - attn_ref).abs().max().item()}') print(f'Attention Pytorch max diff: {(attn_pt - attn_ref).abs().max().item()}') if is_sm80 or d <= 64: # Only run backward for d=128 on A100 g = torch.randn_like(output) dq_unpad, dkv_unpad, = torch.autograd.grad(output, (q_unpad, kv_unpad), g) dq = dq_pad_fn(dq_unpad) dkv = dkv_pad_fn(dkv_unpad) dq_ref, dkv_ref, = torch.autograd.grad(output_ref, (q, kv), g) dq_pt, dkv_pt = torch.autograd.grad(output_pt, (q, kv), g) print(f'dQ max diff: {(dq - dq_ref).abs().max().item()}') print(f'dK max diff: {(dkv[:, :, 0] - dkv_ref[:, :, 0]).abs().max().item()}') print(f'dV max diff: {(dkv[:, :, 1] - dkv_ref[:, :, 1]).abs().max().item()}') print(f'dQ Pytorch max diff: {(dq_pt - dq_ref).abs().max().item()}') print(f'dK Pytorch max diff: {(dkv_pt[:, :, 0] - dkv_ref[:, :, 0]).abs().max().item()}') print(f'dV Pytorch max diff: {(dkv_pt[:, :, 1] - dkv_ref[:, :, 1]).abs().max().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (attn - attn_ref).abs().max().item() <= 2 * (attn_pt - attn_ref).abs().max().item() # assert torch.allclose(attn, attn_ref, rtol=rtol, atol=atol) if dropout_p == 0.0: assert dropout_mask.all() else: assert 0.99 <= dropout_fraction / dropout_p <= 1.01 if is_sm80 or d <= 64: # Only run backward for d=128 on A100 assert (dq - dq_ref).abs().max().item() <= 2 * (dq_pt - dq_ref).abs().max().item() assert (dkv - dkv_ref).abs().max().item() <= 2 * (dkv_pt - dkv_ref).abs().max().item() # assert torch.allclose(dq, dq_ref, rtol=rtol, atol=atol) # assert torch.allclose(dkv, dkv_ref, rtol=rtol, atol=atol) @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('causal', [False, True]) @pytest.mark.parametrize('d', [128, 64, 80, 40, 32, 16]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen', [97, 128, 200, 256, 257, 384, 512, 768, 1024, 1025, 2048]) # @pytest.mark.parametrize('seqlen', [128]) @pytest.mark.parametrize('dropout_p', [0.0, 0.17]) # @pytest.mark.parametrize('dropout_p', [0.0]) def test_flash_attn_unpadded(seqlen, d, dropout_p, causal, dtype): if seqlen >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # if dtype == torch.float16: # rtol, atol = (1e-3, 3e-4) if not causal else (1e-3, 1e-3) # else: # torch.bfloat16 # rtol, atol = (3e-3, 3e-3) if not causal else (1e-3, 1e-3) # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) query_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') (q_unpad, k_unpad, v_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, q, k, v, output_pad_fn, dq_pad_fn, dk_pad_fn) = generate_qkv( x, Wqkv, nheads, query_padding_mask, key_padding_mask ) output_unpad, sm_lse, S_dmask = flash_attn_unpadded_func( q_unpad, k_unpad, v_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, return_attn_probs=True, causal=causal ) output = output_pad_fn(output_unpad) S_dmask_converted = convert_flash_attn_S_to_softmax( S_dmask, query_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) dropout_mask = S_dmask_converted >= 0 attn_unnorm = S_dmask_converted.abs() attn = normalize_flash_attn_S(attn_unnorm, q, k, v, query_padding_mask, key_padding_mask, dropout_p > 0.0, causal=causal) dropout_fraction = get_dropout_fraction(dropout_mask, query_padding_mask, key_padding_mask, causal=causal) output_ref, attn_ref = attention_ref(q, k, v, query_padding_mask, key_padding_mask, dropout_p, dropout_mask, causal=causal) output_pt, attn_pt = attention_ref(q, k, v, query_padding_mask, key_padding_mask, dropout_p, dropout_mask, causal=causal, upcast=False, reorder_ops=True) print(f'Actual dropout fraction: {dropout_fraction}') print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') print(f'Attention max diff: {(attn - attn_ref).abs().max().item()}') print(f'Attention Pytorch max diff: {(attn_pt - attn_ref).abs().max().item()}') if is_sm80 or d <= 64: # Only run backward for d=128 on A100 g = torch.randn_like(output) dq_unpad, dk_unpad, dv_unpad, = torch.autograd.grad(output, (q_unpad, k_unpad, v_unpad), g) dq = dq_pad_fn(dq_unpad) dk = dk_pad_fn(dk_unpad) dv = dk_pad_fn(dv_unpad) dq_ref, dk_ref, dv_ref, = torch.autograd.grad(output_ref, (q, k, v), g) dq_pt, dk_pt, dv_pt, = torch.autograd.grad(output_pt, (q, k, v), g) print(f'dQ max diff: {(dq - dq_ref).abs().max().item()}') print(f'dK max diff: {(dk - dk_ref).abs().max().item()}') print(f'dV max diff: {(dv - dv_ref).abs().max().item()}') print(f'dQ Pytorch max diff: {(dq_pt - dq_ref).abs().max().item()}') print(f'dK Pytorch max diff: {(dk_pt - dk_ref).abs().max().item()}') print(f'dV Pytorch max diff: {(dv_pt - dv_ref).abs().max().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (attn - attn_ref).abs().max().item() <= 2 * (attn_pt - attn_ref).abs().max().item() # assert torch.allclose(attn, attn_ref, rtol=rtol, atol=atol) if dropout_p == 0.0: assert dropout_mask.all() else: assert 0.99 <= dropout_fraction / dropout_p <= 1.01 if is_sm80 or d <= 64: # Only run backward for d=128 on A100 assert (dq - dq_ref).abs().max().item() <= 2 * (dq_pt - dq_ref).abs().max().item() assert (dk - dk_ref).abs().max().item() <= 2 * (dk_pt - dk_ref).abs().max().item() assert (dv - dv_ref).abs().max().item() <= 2 * (dv_pt - dv_ref).abs().max().item() # assert torch.allclose(dq, dq_ref, rtol=rtol, atol=atol) # assert torch.allclose(dk, dk_ref, rtol=rtol, atol=atol) # assert torch.allclose(dv, dv_ref, rtol=rtol, atol=atol) @pytest.mark.skipif(True, reason='Experimental, not being used') @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('causal', [False, True]) # @pytest.mark.parametrize('causal', [False]) @pytest.mark.parametrize('d', [128, 64, 80, 40, 32, 16]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen', [512]) @pytest.mark.parametrize('dropout_p', [0.0, 0.17]) # @pytest.mark.parametrize('dropout_p', [0.0]) def test_flash_attn_split(seqlen, d, dropout_p, causal, dtype): if seqlen >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # if dtype == torch.float16: # rtol, atol = (1e-3, 3e-4) if not causal else (1e-3, 1e-3) # else: # torch.bfloat16 # rtol, atol = (3e-3, 3e-3) if not causal else (1e-3, 1e-3) # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='split') batch_size0 = batch_size // 4 * 3 # this must match what's in generate_random_padding_mask # key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='full') qkv_unpad, cu_seqlens, max_seqlen0, qkv, output_pad_fn, dqkv_pad_fn = generate_qkv( x, Wqkv, nheads, key_padding_mask, key_padding_mask, qkvpacked=True ) max_seqlen1 = 128 output_unpad, sm_lse, S_dmask0, S_dmask1 = flash_attn_unpadded_qkvpacked_split_func( qkv_unpad, cu_seqlens, max_seqlen0, max_seqlen1, batch_size0, dropout_p, return_attn_probs=True, causal=causal ) output = output_pad_fn(output_unpad) S_dmask0_converted = convert_flash_attn_S_to_softmax( S_dmask0, key_padding_mask[:batch_size0], key_padding_mask[:batch_size0], d, dropout_p > 0.0, causal=causal ) S_dmask1_converted = convert_flash_attn_S_to_softmax( S_dmask1, key_padding_mask[batch_size0:, :max_seqlen1], key_padding_mask[batch_size0:, :max_seqlen1], d, dropout_p > 0.0, causal=causal ) padding = (S_dmask0_converted.shape[-1] - S_dmask1_converted.shape[-1], S_dmask0_converted.shape[-2] - S_dmask1_converted.shape[-2]) S_dmask_converted = torch.cat([S_dmask0_converted, F.pad(S_dmask1_converted, (0, padding[0], 0, padding[1]))], dim=0) dropout_mask = S_dmask_converted >= 0 attn_unnorm = S_dmask_converted.abs() attn = normalize_flash_attn_S(attn_unnorm, qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], key_padding_mask, key_padding_mask, dropout_p > 0.0, causal=causal) dropout_fraction = get_dropout_fraction(dropout_mask, key_padding_mask, key_padding_mask, causal=causal).item() output_ref, attn_ref = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal) output_pt, attn_pt = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal, upcast=False, reorder_ops=True) print(f'Actual dropout fraction: {dropout_fraction}') print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') print(f'Attention max diff: {(attn - attn_ref).abs().max().item()}') print(f'Attention Pytorch max diff: {(attn_pt - attn_ref).abs().max().item()}') if is_sm80 or d <= 64: # Only run backward for d=128 on A100 g = torch.randn_like(output) dqkv_unpad, = torch.autograd.grad(output, qkv_unpad, g) dqkv = dqkv_pad_fn(dqkv_unpad) dqkv_ref, = torch.autograd.grad(output_ref, qkv, g) dqkv_pt, = torch.autograd.grad(output_pt, qkv, g) print(f'dQ max diff: {(dqkv[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK max diff: {(dqkv[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV max diff: {(dqkv[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV mean diff: {(dqkv - dqkv_ref).abs().mean().item()}') print(f'dQ Pytorch max diff: {(dqkv_pt[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK Pytorch max diff: {(dqkv_pt[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV Pytorch max diff: {(dqkv_pt[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV Pytorch mean diff: {(dqkv_pt - dqkv_ref).abs().mean().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (attn - attn_ref).abs().max().item() <= 2 * (attn_pt - attn_ref).abs().max().item() # assert torch.allclose(attn, attn_ref, rtol=rtol, atol=atol) if dropout_p == 0.0: assert dropout_mask.all() else: assert 0.99 <= dropout_fraction / dropout_p <= 1.01 if is_sm80 or d <= 64: # Only run backward for d=128 on A100 assert (dqkv - dqkv_ref).abs().max().item() <= 2 * (dqkv_pt - dqkv_ref).abs().max().item() # assert torch.allclose(dqkv, dqkv_ref, rtol=rtol, atol=atol) @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('causal', [False, True]) @pytest.mark.parametrize('d', [128, 64, 80, 40, 32, 16]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen', [97, 128, 200, 256, 257, 384, 512, 768, 1024, 1025, 2048]) # @pytest.mark.parametrize('seqlen', [128]) @pytest.mark.parametrize('dropout_p', [0.0, 0.17]) # @pytest.mark.parametrize('dropout_p', [0.0]) def test_flash_attn_race_condition(seqlen, d, dropout_p, causal, dtype): if seqlen >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) query_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') (q_unpad, k_unpad, v_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, q, k, v, output_pad_fn, dq_pad_fn, dk_pad_fn) = generate_qkv( x, Wqkv, nheads, query_padding_mask, key_padding_mask ) torch.random.manual_seed(0) output_unpad_0, sm_lse_0, S_dmask_0 = flash_attn_unpadded_func( q_unpad, k_unpad, v_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, return_attn_probs=True, causal=causal ) S_dmask_converted_0 = convert_flash_attn_S_to_softmax( S_dmask_0, query_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) if is_sm80 or d <= 64: # Only run backward for d=128 on A100 g = torch.randn_like(output_unpad_0) dq_unpad_0, dk_unpad_0, dv_unpad_0, = torch.autograd.grad(output_unpad_0, (q_unpad, k_unpad, v_unpad), g) # Parallelizing over seqlen_k makes dq non-deterministic deterministic_dq = False # Numerical error if we just do any arithmetic on dq dq_atol = ((dq_unpad_0 + 0.3 - 0.3) - dq_unpad_0).abs().max().item() equal_fn = torch.equal if deterministic_dq else partial(torch.allclose, atol=dq_atol) for _ in range(10): torch.random.manual_seed(0) output_unpad, sm_lse, S_dmask = flash_attn_unpadded_func( q_unpad, k_unpad, v_unpad, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, return_attn_probs=True, causal=causal ) S_dmask_converted = convert_flash_attn_S_to_softmax( S_dmask, query_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) assert torch.equal(output_unpad, output_unpad_0) # sm_lse has some parts that are uninitialized from torch.empty # assert torch.equal(sm_lse, sm_lse_0) assert torch.equal(S_dmask_converted, S_dmask_converted_0) if is_sm80 or d <= 64: # Only run backward for d=128 on A100 dq_unpad, dk_unpad, dv_unpad, = torch.autograd.grad(output_unpad, (q_unpad, k_unpad, v_unpad), g) assert equal_fn(dq_unpad, dq_unpad_0) assert torch.equal(dk_unpad, dk_unpad_0) assert torch.equal(dv_unpad, dv_unpad_0) @pytest.mark.skipif(torch.cuda.device_count() < 2, reason='requires multiple GPUs') def test_flash_attn_multigpu(): seqlen = 256 d = 64 dropout_p = 0.0 causal = False dtype = torch.float16 device = 'cuda:1' torch.random.manual_seed(0) batch_size = 32 nheads = 4 x = torch.randn(batch_size, seqlen, nheads * d, device=device, dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='random') # key_padding_mask = generate_random_padding_mask(seqlen, batch_size, device, mode='full') qkv_unpad, cu_seqlens, max_seqlen, qkv, output_pad_fn, dqkv_pad_fn = generate_qkv( x, Wqkv, nheads, key_padding_mask, key_padding_mask, qkvpacked=True ) output_unpad, sm_lse, S_dmask = flash_attn_unpadded_qkvpacked_func( qkv_unpad, cu_seqlens, max_seqlen, dropout_p, return_attn_probs=True, causal=causal ) output = output_pad_fn(output_unpad) S_dmask_converted = convert_flash_attn_S_to_softmax( S_dmask, key_padding_mask, key_padding_mask, d, dropout_p > 0.0, causal=causal ) dropout_mask = S_dmask_converted >= 0 attn_unnorm = S_dmask_converted.abs() attn = normalize_flash_attn_S(attn_unnorm, qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], key_padding_mask, key_padding_mask, dropout_p > 0.0, causal=causal) dropout_fraction = get_dropout_fraction(dropout_mask, key_padding_mask, key_padding_mask, causal=causal).item() output_ref, attn_ref = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal) output_pt, attn_pt = attention_qkvpacked_ref(qkv, key_padding_mask, dropout_p, dropout_mask, causal=causal, upcast=False, reorder_ops=True) print(f'Actual dropout fraction: {dropout_fraction}') print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') print(f'Attention max diff: {(attn - attn_ref).abs().max().item()}') print(f'Attention Pytorch max diff: {(attn_pt - attn_ref).abs().max().item()}') g = torch.randn_like(output) dqkv_unpad, = torch.autograd.grad(output, qkv_unpad, g) dqkv = dqkv_pad_fn(dqkv_unpad) dqkv_ref, = torch.autograd.grad(output_ref, qkv, g) dqkv_pt, = torch.autograd.grad(output_pt, qkv, g) print(f'dQ max diff: {(dqkv[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK max diff: {(dqkv[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV max diff: {(dqkv[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV mean diff: {(dqkv - dqkv_ref).abs().mean().item()}') print(f'dQ Pytorch max diff: {(dqkv_pt[:, :, 0] - dqkv_ref[:, :, 0]).abs().max().item()}') print(f'dK Pytorch max diff: {(dqkv_pt[:, :, 1] - dqkv_ref[:, :, 1]).abs().max().item()}') print(f'dV Pytorch max diff: {(dqkv_pt[:, :, 2] - dqkv_ref[:, :, 2]).abs().max().item()}') print(f'dQKV Pytorch mean diff: {(dqkv_pt - dqkv_ref).abs().mean().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (attn - attn_ref).abs().max().item() <= 2 * (attn_pt - attn_ref).abs().max().item() # assert torch.allclose(attn, attn_ref, rtol=rtol, atol=atol) if dropout_p == 0.0: assert dropout_mask.all() else: assert 0.99 <= dropout_fraction / dropout_p <= 1.01 assert (dqkv - dqkv_ref).abs().max().item() <= 2 * (dqkv_pt - dqkv_ref).abs().max().item() @pytest.mark.skipif(flash_attn_func is None, reason='Triton is not installed or is too old') @pytest.mark.skipif(not is_sm80, reason='Triton version is only tested on A100') @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('causal', [False, True]) # @pytest.mark.parametrize('causal', [True]) @pytest.mark.parametrize('d', [40, 48, 64, 128, 80, 88, 96]) # @pytest.mark.parametrize('d', [48]) @pytest.mark.parametrize('seqlen_q,seqlen_k', [(113, 203), (128, 217), (113, 211), (108, 256), (256, 512), (512, 256), (1024, 1024), (1023, 1024), (1024, 1023), (2048, 2048)]) # @pytest.mark.parametrize('seqlen_q,seqlen_k', [(1024, 1023)]) @pytest.mark.parametrize('bias_shape', ([None, '1h1k', '1hqk', 'b11k', 'b1qk'])) # @pytest.mark.parametrize('bias_shape', (['1hqk'])) def test_flash_attn_triton_output(seqlen_q, seqlen_k, d, causal, dtype, bias_shape): if seqlen_q >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 q = torch.randn(batch_size, seqlen_q, nheads, d, device=device, dtype=dtype) k, v = torch.randn(batch_size, seqlen_k, 2, nheads, d, device=device, dtype=dtype).unbind(dim=2) if bias_shape == '1h1k': bias = torch.randn(1, nheads, 1, seqlen_k, dtype=torch.float, device=device) elif bias_shape == '1hqk': bias = torch.randn(1, nheads, seqlen_q, seqlen_k, dtype=torch.float, device=device) elif bias_shape == 'b11k': bias = torch.randn(batch_size, 1, 1, seqlen_k, dtype=torch.float, device=device) elif bias_shape == 'b1qk': bias = torch.randn(batch_size, 1, seqlen_q, seqlen_k, dtype=torch.float, device=device) else: bias = None q, k, v = [x.detach().requires_grad_() for x in [q, k, v]] output = flash_attn_func(q, k, v, bias, causal) output_ref, attn_ref = attention_ref(q, k, v, bias=bias, causal=causal) output_pt, attn_pt = attention_ref(q, k, v, bias=bias, causal=causal, upcast=False, reorder_ops=True) print(f'Output max diff: {(output - output_ref).abs().max().item()}') print(f'Output mean diff: {(output - output_ref).abs().mean().item()}') print(f'Pytorch max diff: {(output_pt - output_ref).abs().max().item()}') print(f'Pytorch mean diff: {(output_pt - output_ref).abs().mean().item()}') g = torch.randn_like(output) dq, dk, dv = torch.autograd.grad(output, (q, k, v), g) dq_ref, dk_ref, dv_ref, = torch.autograd.grad(output_ref, (q, k, v), g) dq_pt, dk_pt, dv_pt, = torch.autograd.grad(output_pt, (q, k, v), g) print(f'dQ max diff: {(dq - dq_ref).abs().max().item()}') print(f'dK max diff: {(dk - dk_ref).abs().max().item()}') print(f'dV max diff: {(dv - dv_ref).abs().max().item()}') print(f'dQ mean diff: {(dq - dq_ref).abs().mean().item()}') print(f'dK mean diff: {(dk - dk_ref).abs().mean().item()}') print(f'dV mean diff: {(dv - dv_ref).abs().mean().item()}') print(f'dQ Pytorch max diff: {(dq_pt - dq_ref).abs().max().item()}') print(f'dK Pytorch max diff: {(dk_pt - dk_ref).abs().max().item()}') print(f'dV Pytorch max diff: {(dv_pt - dv_ref).abs().max().item()}') print(f'dQ Pytorch mean diff: {(dq_pt - dq_ref).abs().mean().item()}') print(f'dK Pytorch mean diff: {(dk_pt - dk_ref).abs().mean().item()}') print(f'dV Pytorch mean diff: {(dv_pt - dv_ref).abs().mean().item()}') # Check that FlashAttention's numerical error is at most twice the numerical error # of a Pytorch implementation. assert (output - output_ref).abs().max().item() <= 2 * (output_pt - output_ref).abs().max().item() # assert torch.allclose(output, output_ref, rtol=rtol, atol=atol) assert (dq - dq_ref).abs().max().item() <= 2 * (dq_pt - dq_ref).abs().max().item() assert (dk - dk_ref).abs().max().item() <= 2 * (dk_pt - dk_ref).abs().max().item() assert (dv - dv_ref).abs().max().item() <= 2 * (dv_pt - dv_ref).abs().max().item() @pytest.mark.skipif(flash_attn_func is None, reason='Triton is not installed or is too old') @pytest.mark.skipif(not is_sm80, reason='Triton version is only tested on A100') @pytest.mark.parametrize('dtype', ([torch.float16] if is_sm75 else [torch.float16, torch.bfloat16])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('causal', [False, True]) # @pytest.mark.parametrize('causal', [True]) @pytest.mark.parametrize('d', [40, 48, 64, 128, 80, 88, 96]) # @pytest.mark.parametrize('d', [64]) @pytest.mark.parametrize('seqlen_q,seqlen_k', [(113, 203), (128, 217), (91, 211), (108, 256), (256, 512), (512, 256), (1024, 1024), (1023, 1024), (1024, 1023), (2048, 2048)]) # @pytest.mark.parametrize('seqlen_q,seqlen_k', [(113, 203)]) @pytest.mark.parametrize('bias_shape', ([None, '1h1k', '1hqk', 'b11k', 'b1qk'])) # @pytest.mark.parametrize('bias_shape', (['b1qk'])) def test_flash_attn_triton_race_condition(seqlen_q, seqlen_k, d, causal, dtype, bias_shape): if seqlen_q >= 2048 and torch.cuda.get_device_properties('cuda').total_memory <= 16 * 2**30: pytest.skip() # Reference implementation OOM device = 'cuda' # set seed torch.random.manual_seed(0) batch_size = 32 nheads = 4 q = torch.randn(batch_size, seqlen_q, nheads, d, device=device, dtype=dtype) k, v = torch.randn(batch_size, seqlen_k, 2, nheads, d, device=device, dtype=dtype).unbind(dim=2) if bias_shape == '1h1k': bias = torch.randn(1, nheads, 1, seqlen_k, dtype=torch.float, device=device) elif bias_shape == '1hqk': bias = torch.randn(1, nheads, seqlen_q, seqlen_k, dtype=torch.float, device=device) elif bias_shape == 'b11k': bias = torch.randn(batch_size, 1, 1, seqlen_k, dtype=torch.float, device=device) elif bias_shape == 'b1qk': bias = torch.randn(batch_size, 1, seqlen_q, seqlen_k, dtype=torch.float, device=device) else: bias = None q, k, v = [x.detach().requires_grad_() for x in [q, k, v]] output_0 = flash_attn_func(q, k, v, bias, causal) g = torch.randn_like(output_0) dq_0, dk_0, dv_0 = torch.autograd.grad(output_0, (q, k, v), g) # The SEQUENCE_PARALLEL option for the bwd to makes dq non-deterministic deterministic_dq = False # Numerical error if we just do any arithmetic on dq dq_atol = ((dq_0 + 0.3 - 0.3) - dq_0).abs().max().item() equal_fn = torch.equal if deterministic_dq else partial(torch.allclose, atol=dq_atol) # Run 10000 times and check that the results don't change for i in range(10000): output = flash_attn_func(q, k, v, bias, causal) output_equal = torch.equal(output, output_0) if not output_equal: # Printing / computing diff sometimes makes the race condition disappear print(f'{dtype = }, {causal = }, {d = }, {seqlen_q = }, {seqlen_k = }, {bias_shape = }, {i = }') print(f'Output max diff: {(output - output_0).abs().max().item()}') assert torch.equal(output, output_0) dq, dk, dv = torch.autograd.grad(output, (q, k, v), g) dq_equal = equal_fn(dq, dq_0) dk_equal = torch.equal(dk, dk_0) dv_equal = torch.equal(dv, dv_0) if not (dq_equal and dk_equal and dv_equal): print(f'{dtype = }, {causal = }, {d = }, {seqlen_q = }, {seqlen_k = }, {bias_shape = }, {i = }') print(f'dQ max diff: {(dq - dq_0).abs().max().item()}') print(f'dK max diff: {(dk - dk_0).abs().max().item()}') print(f'dV max diff: {(dv - dv_0).abs().max().item()}') assert equal_fn(dq, dq_0) assert torch.equal(dk, dk_0) assert torch.equal(dv, dv_0)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/test_flash_attn.py
import math import torch import torch.nn.functional as F import pytest from einops import rearrange from flash_attn.losses.cross_entropy import CrossEntropyLossApex is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16, torch.float32] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('inplace_backward', [False, True]) # @pytest.mark.parametrize('inplace_backward', [False]) @pytest.mark.parametrize('smoothing', [0.0, 0.9]) @pytest.mark.parametrize('vocab_size', [50257]) def test_cross_entropy_loss_apex(vocab_size, smoothing, inplace_backward, dtype): device = 'cuda' rtol, atol = (1e-5, 1e-6) if dtype == torch.float32 else (1e-3, 1e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 128 x_pt = torch.randn(batch_size * seqlen, vocab_size, device=device, dtype=dtype, requires_grad=True) x = x_pt.detach().clone().requires_grad_() y = torch.randint(0, vocab_size, (batch_size * seqlen,), dtype=torch.long, device=device) y[torch.randperm(batch_size * seqlen)[:10]] = -100 model_pt = torch.nn.CrossEntropyLoss(label_smoothing=smoothing) model = CrossEntropyLossApex(label_smoothing=smoothing, inplace_backward=inplace_backward) out = model(x, y) out_pt = model_pt(x_pt.float(), y) assert torch.allclose(out, out_pt, rtol=rtol, atol=atol) g = torch.randn_like(out) out_pt.backward(g) out.backward(g) assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/losses/test_cross_entropy.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/losses/test_cross_entropy_parallel.py import math import torch import torch.nn.functional as F import pytest from apex.transformer import parallel_state from apex.transformer import tensor_parallel from flash_attn.losses.cross_entropy import CrossEntropyLoss is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16, torch.float32] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('inplace_backward', [False, True]) # @pytest.mark.parametrize('inplace_backward', [False]) @pytest.mark.parametrize('smoothing', [0.0, 0.9]) # @pytest.mark.parametrize('smoothing', [0.9]) @pytest.mark.parametrize('vocab_size', [50264]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) def test_cross_entropy_loss_parallel(vocab_size, world_size, smoothing, inplace_backward, dtype): assert vocab_size % world_size == 0 rtol, atol = ((1e-5, 1e-6) if dtype == torch.float32 else ((1e-3, 1e-4) if dtype == torch.float16 else (1e-2, 3e-3))) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') partition_vocab_size = vocab_size // world_size device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 128 x_pt = (torch.randn(batch_size * seqlen, vocab_size, device=device, dtype=dtype) * 10).requires_grad_() x = tensor_parallel.scatter_to_tensor_model_parallel_region(x_pt).detach().clone().requires_grad_() y = torch.randint(0, vocab_size, (batch_size * seqlen,), dtype=torch.long, device=device) y[torch.randperm(batch_size * seqlen)[:10]] = -100 model_pt = torch.nn.CrossEntropyLoss(label_smoothing=smoothing, reduction='none') model = CrossEntropyLoss(label_smoothing=smoothing, reduction='none', inplace_backward=inplace_backward, process_group=parallel_state.get_tensor_model_parallel_group()) out = model(x, y) out_pt = model_pt(x_pt.float(), y) assert torch.allclose(out, out_pt, rtol=1e-5, atol=1e-6) g = torch.randn_like(out) out_pt.backward(g) out.backward(g) assert torch.allclose(x.grad, x_pt.grad[:, (rank * partition_vocab_size):(rank + 1) * partition_vocab_size], rtol=rtol, atol=atol) parallel_state.destroy_model_parallel()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/losses/test_cross_entropy_parallel.py
# Copyright (c) 2023, Tri Dao. import math import torch import torch.nn.functional as F import pytest from einops import rearrange from transformers.models.gpt_neox.modeling_gpt_neox import RotaryEmbedding as RotaryEmbeddingNeoX from transformers.models.gpt_neox.modeling_gpt_neox import apply_rotary_pos_emb as apply_rotary_pos_emb_neox from transformers.models.gptj.modeling_gptj import fixed_pos_embedding from transformers.models.gptj.modeling_gptj import apply_rotary_pos_emb as apply_rotary_pos_emb_gptj from flash_attn.layers.rotary import apply_rotary_emb_func, apply_rotary_emb_qkv_ from flash_attn.layers.rotary import RotaryEmbedding # NeoX-style rotary embedding @pytest.mark.parametrize('seqlen_offset', [0, 711]) @pytest.mark.parametrize('rotary_emb_fraction', [0.5, 1.0]) def test_rotary(rotary_emb_fraction, seqlen_offset): device = 'cuda' dtype = torch.float16 rtol, atol = (1e-3, 5e-3) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen_total = 2048 seqlen = seqlen_total - seqlen_offset nheads = 16 headdim = 128 rotary_dim = int(headdim * rotary_emb_fraction) qkv = torch.randn(batch_size, seqlen, 3, nheads, headdim, device=device, dtype=dtype, requires_grad=True) qkv_og = qkv.clone().detach() # Our implementation modifies qkv inplace rotary = RotaryEmbedding(rotary_dim, device=device) rotary_neox = RotaryEmbeddingNeoX(rotary_dim, seqlen_total, device=device) # Doesn't matter what tensor we pass in, rotary_neox only uses the device of the tensor cos_neox, sin_neox = rotary_neox(qkv, seq_len=seqlen_total) cos_neox, sin_neox = cos_neox.to(dtype=dtype), sin_neox.to(dtype=dtype) q_pt = rearrange(qkv[:, :, 0, :, :rotary_dim], 'b s h d -> b h s d').detach().clone().requires_grad_(True) k_pt = rearrange(qkv[:, :, 1, :, :rotary_dim], 'b s h d -> b h s d').detach().clone().requires_grad_(True) q_neox, k_neox = apply_rotary_pos_emb_neox(q_pt, k_pt, cos_neox, sin_neox, offset=seqlen_offset) out = rotary(qkv, seqlen_offset=seqlen_offset) assert torch.allclose(rotary._cos_cached, cos_neox[..., :rotary_dim // 2].to(dtype=dtype), rtol=rtol, atol=atol) assert torch.allclose(rotary._sin_cached, sin_neox[..., :rotary_dim // 2].to(dtype=dtype), rtol=rtol, atol=atol) assert torch.allclose(rearrange(q_neox, 'b h s d -> b s h d'), out[:, :, 0, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.allclose(rearrange(k_neox, 'b h s d -> b s h d'), out[:, :, 1, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.equal(out[:, :, 0:2, :, rotary_dim:], qkv_og[:, :, 0:2, :, rotary_dim:]) assert torch.equal(out[:, :, 2], qkv_og[:, :, 2]) g = torch.randn_like(out) g_og = g.clone().detach() # Our implementation modifies g inplace out.backward(g) q_neox.backward(rearrange(g_og[:, :, 0, :, :rotary_dim], 'b s h d -> b h s d')) k_neox.backward(rearrange(g_og[:, :, 1, :, :rotary_dim], 'b s h d -> b h s d')) assert torch.allclose(rearrange(q_pt.grad, 'b h s d -> b s h d'), qkv.grad[:, :, 0, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.allclose(rearrange(k_pt.grad, 'b h s d -> b s h d'), qkv.grad[:, :, 1, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.equal(qkv.grad[:, :, 0:2, :, rotary_dim:], g_og[:, :, 0:2, :, rotary_dim:]) assert torch.equal(qkv.grad[:, :, 2], g_og[:, :, 2]) # GPT-J-style rotary embedding @pytest.mark.parametrize('seqlen_offset', [0, 711]) @pytest.mark.parametrize('rotary_emb_fraction', [0.5, 1.0]) def test_rotary_interleaved(rotary_emb_fraction, seqlen_offset): device = 'cuda' dtype = torch.float16 rtol, atol = (1e-3, 5e-3) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen_total = 2048 seqlen = seqlen_total - seqlen_offset nheads = 16 headdim = 128 rotary_dim = int(headdim * rotary_emb_fraction) qkv = torch.randn(batch_size, seqlen, 3, nheads, headdim, device=device, dtype=dtype, requires_grad=True) qkv_og = qkv.clone().detach() # Our implementation modifies qkv inplace rotary = RotaryEmbedding(rotary_dim, interleaved=True, device=device) sincos_gptj = fixed_pos_embedding(qkv[..., :rotary_dim], seq_dim=1, seq_len=seqlen_total) sincos_gptj = tuple(x.to(dtype=dtype) for x in sincos_gptj) q_pt = qkv[:, :, 0, :, :rotary_dim].detach().clone().requires_grad_(True) k_pt = qkv[:, :, 1, :, :rotary_dim].detach().clone().requires_grad_(True) q_gptj = apply_rotary_pos_emb_gptj(q_pt, sincos_gptj, offset=seqlen_offset) k_gptj = apply_rotary_pos_emb_gptj(k_pt, sincos_gptj, offset=seqlen_offset) out = rotary(qkv, seqlen_offset=seqlen_offset) assert torch.allclose(rotary._cos_cached, sincos_gptj[1], rtol=rtol, atol=atol) assert torch.allclose(rotary._sin_cached, sincos_gptj[0], rtol=rtol, atol=atol) assert torch.allclose(q_gptj, out[:, :, 0, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.allclose(k_gptj, out[:, :, 1, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.equal(out[:, :, 0:2, :, rotary_dim:], qkv_og[:, :, 0:2, :, rotary_dim:]) assert torch.equal(out[:, :, 2], qkv_og[:, :, 2]) g = torch.randn_like(out) g_og = g.clone().detach() # Our implementation modifies g inplace out.backward(g) q_gptj.backward(g_og[:, :, 0, :, :rotary_dim]) k_gptj.backward(g_og[:, :, 1, :, :rotary_dim]) assert torch.allclose(q_pt.grad, qkv.grad[:, :, 0, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.allclose(k_pt.grad, qkv.grad[:, :, 1, :, :rotary_dim], rtol=rtol, atol=atol) assert torch.equal(qkv.grad[:, :, 0:2, :, rotary_dim:], g_og[:, :, 0:2, :, rotary_dim:]) assert torch.equal(qkv.grad[:, :, 2], g_og[:, :, 2])
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/layers/test_rotary.py
import re import torch import pytest from transformers import OPTConfig from transformers.models.opt.modeling_opt import OPTForCausalLM from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.opt import remap_state_dict_hf_opt, opt_config_to_gpt2_config from flash_attn.utils.pretrained import state_dict_from_pretrained @pytest.mark.parametrize('model_name', ["facebook/opt-125m", "facebook/opt-350m", "facebook/opt-1.3b"]) # @pytest.mark.parametrize('model_name', ["facebook/opt-350m"]) def test_opt_state_dict(model_name): config = opt_config_to_gpt2_config(OPTConfig.from_pretrained(model_name)) pretrained_state_dict = remap_state_dict_hf_opt(state_dict_from_pretrained(model_name), config) model = GPTLMHeadModel(config) state_dict = model.state_dict() assert state_dict.keys() == pretrained_state_dict.keys() for k in state_dict.keys(): assert state_dict[k].shape == pretrained_state_dict[k].shape @pytest.mark.parametrize('model_name', ["facebook/opt-125m", "facebook/opt-350m", "facebook/opt-1.3b"]) # @pytest.mark.parametrize('model_name', ["facebook/opt-350m"]) def test_opt_optimized(model_name): """Check that our implementation of OPT (without all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 device = 'cuda' config = opt_config_to_gpt2_config(OPTConfig.from_pretrained(model_name)) config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True # Only prenorm supports residual_in_fp32 config.residual_in_fp32 = getattr(config, 'prenorm', True) config.pad_vocab_size_multiple = 8 model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype) model_ref = OPTForCausalLM.from_pretrained(model_name).to(device=device) model_hf = OPTForCausalLM.from_pretrained(model_name, torch_dtype=dtype).to(device=device) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 2 max_seqlen = 256 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') if model_name != 'facebook/opt-350m': # The OPT-350m projects the embeddings to dimension 512 out = model.transformer(input_ids) out_hf = model_hf.model(input_ids).last_hidden_state out_ref = model_ref.model(input_ids).last_hidden_state print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}') assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item() logits = model(input_ids).logits logits_hf = model_hf(input_ids).logits logits_ref = model_ref(input_ids).logits print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}') print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}') assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_opt.py
import os import re import time import torch import pytest from einops import rearrange from transformers import GPT2Config, GPT2Tokenizer, OPTConfig, AutoTokenizer from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel as GPT2LMHeadModelHF from transformers.models.opt.modeling_opt import OPTForCausalLM from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.gpt import remap_state_dict_hf_gpt2 from flash_attn.models.opt import remap_state_dict_hf_opt, opt_config_to_gpt2_config from flash_attn.utils.pretrained import state_dict_from_pretrained from flash_attn.utils.generation import update_graph_cache @pytest.mark.parametrize('fused_ft_kernel', [False, True]) # @pytest.mark.parametrize('fused_ft_kernel', [True]) @pytest.mark.parametrize('optimized', [False, True]) # @pytest.mark.parametrize('optimized', [False]) @pytest.mark.parametrize('rotary', [False, True]) # @pytest.mark.parametrize('rotary', [False]) @pytest.mark.parametrize('model_name', ["gpt2"]) def test_greedy_decode_gpt2(model_name, rotary, optimized, fused_ft_kernel): """Check that our implementation of GPT2 generation matches the HF implementation: the scores in fp16 should be around the same as the HF scores in fp16, when compared to the HF scores in fp32. """ dtype = torch.float16 device = 'cuda' rtol, atol = 3e-3, 3e-1 config = GPT2Config.from_pretrained(model_name) if rotary: config.n_positions = 0 config.rotary_emb_dim = 64 config.residual_in_fp32 = True if optimized: config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True # if not rotary, we load the weight from HF but ignore the position embeddings. # The model would be nonsense but it doesn't matter for the test. model = GPTLMHeadModel.from_pretrained(model_name, config, strict=not rotary, device=device, dtype=dtype) model.eval() if not rotary: model_ref = GPT2LMHeadModelHF.from_pretrained(model_name).to(device=device) model_hf = GPT2LMHeadModelHF.from_pretrained(model_name, torch_dtype=dtype).to(device=device) model_ref.eval() model_hf.eval() torch.manual_seed(0) tokenizer = GPT2Tokenizer.from_pretrained("gpt2") input_ids = tokenizer("Hello, my dog is cute and", return_tensors="pt").input_ids.to(device=device) max_length = 30 # input_ids = torch.randint(0, 100, (2, 10), dtype=torch.long, device='cuda') # max_length = input_ids.shape[1] + 40 # Slow generation for reference sequences = [] scores = [] cur_input_ids = input_ids with torch.inference_mode(): scores.append(model(cur_input_ids).logits[:, -1]) sequences.append(scores[-1].argmax(dim=-1)) for _ in range(input_ids.shape[1] + 1, max_length): cur_input_ids = torch.cat([cur_input_ids, rearrange(sequences[-1], 'b -> b 1')], dim=-1) scores.append(model(cur_input_ids).logits[:, -1]) sequences.append(scores[-1].argmax(dim=-1)) sequences = torch.cat([input_ids, torch.stack(sequences, dim=1)], dim=1) scores = tuple(scores) out = model.generate(input_ids=input_ids, max_length=max_length, fused_ft_kernel=fused_ft_kernel, return_dict_in_generate=True, output_scores=True, timing=True) print(out.sequences) print(tokenizer.batch_decode(out.sequences.tolist())) if fused_ft_kernel: out_cg = model.generate(input_ids=input_ids, max_length=max_length, fused_ft_kernel=fused_ft_kernel, cg=True, return_dict_in_generate=True, output_scores=True, timing=True) print(out_cg.sequences) if not rotary: out_hf = model_hf.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) out_ref = model_ref.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) print(f'Scores max diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'Scores mean diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') print(f'HF fp16 max diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'HF fp16 mean diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') print(tokenizer.batch_decode(out_ref.sequences.tolist())) assert torch.all(out.sequences == sequences) assert torch.allclose(torch.stack(out.scores, dim=1), torch.stack(scores, dim=1), rtol=rtol, atol=atol) if not rotary: assert torch.all(out.sequences == out_ref.sequences) assert torch.all(out.sequences == out_hf.sequences) assert (torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item() < 3 * (torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item() @pytest.mark.parametrize('model_name', ["facebook/opt-125m", "facebook/opt-350m", "facebook/opt-1.3b", "facebook/opt-2.7b", "facebook/opt-6.7b"]) # @pytest.mark.parametrize('model_name', ["facebook/opt-125m"]) def test_greedy_decode_opt(model_name): """Check that our implementation of OPT generation matches the HF implementation: the scores in fp16 should be around the same as the HF scores in fp16, when compared to the HF scores in fp32. """ print(f'\nMODEL: {model_name}') verbose = False dtype = torch.float16 device = 'cuda' rtol, atol = 3e-3, 3e-1 fused_ft_kernel = True config = opt_config_to_gpt2_config(OPTConfig.from_pretrained(model_name)) # Only prenorm supports residual_in_fp32 config.residual_in_fp32 = getattr(config, 'prenorm', True) config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype) model.eval() torch.manual_seed(0) # OPT tokenizer requires use_fast=False # https://huggingface.co/docs/transformers/model_doc/opt tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) eos_token_id = tokenizer.eos_token_id input_ids = tokenizer("Hello, my dog is cute and", return_tensors="pt").input_ids.to(device=device) max_length = 60 # input_ids = torch.randint(0, 100, (2, 10), dtype=torch.long, device='cuda') # max_length = input_ids.shape[1] + 40 # Slow generation for reference sequences = [] scores = [] cur_input_ids = input_ids with torch.inference_mode(): scores.append(model(cur_input_ids).logits[:, -1]) sequences.append(scores[-1].argmax(dim=-1)) for _ in range(input_ids.shape[1] + 1, max_length): cur_input_ids = torch.cat([cur_input_ids, rearrange(sequences[-1], 'b -> b 1')], dim=-1) scores.append(model(cur_input_ids).logits[:, -1]) sequences.append(scores[-1].argmax(dim=-1)) if eos_token_id is not None and (sequences[-1] == eos_token_id).all(): break sequences = torch.cat([input_ids, torch.stack(sequences, dim=1)], dim=1) scores = tuple(scores) print('Without CUDA graph') torch.cuda.synchronize() start = time.time() out = model.generate(input_ids=input_ids, max_length=max_length, eos_token_id=eos_token_id, fused_ft_kernel=fused_ft_kernel, return_dict_in_generate=True, output_scores=True, timing=True) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') if verbose: print(out.sequences) print(tokenizer.batch_decode(out.sequences.tolist())) if fused_ft_kernel: # Capture graph outside the timing loop batch_size, seqlen_og = input_ids.shape model._decoding_cache = update_graph_cache( model, None, batch_size, seqlen_og, max_length ) print('With CUDA graph') torch.cuda.synchronize() start = time.time() out_cg = model.generate(input_ids=input_ids, max_length=max_length, fused_ft_kernel=fused_ft_kernel, cg=True, return_dict_in_generate=True, output_scores=True, timing=True) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') if verbose: print(out_cg.sequences) print(tokenizer.batch_decode(out_cg.sequences.tolist())) del model model_hf = OPTForCausalLM.from_pretrained(model_name, torch_dtype=dtype).to(device=device) model_hf.eval() print("HF fp16") torch.cuda.synchronize() start = time.time() out_hf = model_hf.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') del model_hf model_ref = OPTForCausalLM.from_pretrained(model_name).to(device=device) model_ref.eval() print("HF fp32") torch.cuda.synchronize() start = time.time() out_ref = model_ref.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') del model_ref print(tokenizer.batch_decode(out_ref.sequences.tolist())) if verbose: print(f'Scores max diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'Scores mean diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') print(f'HF fp16 max diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'HF fp16 mean diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') assert torch.all(out.sequences == sequences) assert torch.allclose(torch.stack(out.scores, dim=1), torch.stack(scores, dim=1), rtol=rtol, atol=atol) assert torch.all(out.sequences == out_ref.sequences) assert torch.all(out.sequences == out_hf.sequences) assert (torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item() < 3 * (torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gpt_generation.py
import time import torch import pytest from transformers import GPTJConfig, AutoTokenizer from transformers.models.gptj.modeling_gptj import GPTJForCausalLM from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.gptj import remap_state_dict_hf_gptj, gptj_config_to_gpt2_config from flash_attn.utils.pretrained import state_dict_from_pretrained from flash_attn.utils.generation import update_graph_cache @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-j-6B"]) def test_gptj_state_dict(model_name): config = gptj_config_to_gpt2_config(GPTJConfig.from_pretrained(model_name)) pretrained_state_dict = remap_state_dict_hf_gptj(state_dict_from_pretrained(model_name), config) model = GPTLMHeadModel(config, device='meta') # Without device='meta' init is very slow state_dict = model.state_dict() rotary_inv_freq_keys = {f'transformer.layers.{l}.mixer.rotary_emb.inv_freq' for l in range(config.n_layer)} assert state_dict.keys() == pretrained_state_dict.keys() | rotary_inv_freq_keys for k in state_dict.keys() - rotary_inv_freq_keys: assert state_dict[k].shape == pretrained_state_dict[k].shape @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-j-6B"]) def test_gptj_optimized(model_name): """Check that our implementation of GPT-J (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 device = 'cuda' config = gptj_config_to_gpt2_config(GPTJConfig.from_pretrained(model_name)) config.use_flash_attn = False # FlashAttention doesn't support hdim 256 yet config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True config.residual_in_fp32 = True model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype) model.eval() torch.manual_seed(0) batch_size = 2 max_seqlen = 256 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device=device) input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device=device) with torch.no_grad(): out = model.transformer(input_ids) logits = model(input_ids).logits del model # Without device_map, the model is loaded on the CPU, which is very slow model_ref = GPTJForCausalLM.from_pretrained(model_name, device_map={"": device}) model_ref.eval() with torch.no_grad(): out_ref = model_ref.transformer(input_ids).last_hidden_state logits_ref = model_ref(input_ids).logits del model_ref model_hf = GPTJForCausalLM.from_pretrained(model_name, torch_dtype=dtype, device_map={"": device}) model_hf.eval() out_hf = model_hf.transformer(input_ids).last_hidden_state logits_hf = model_hf(input_ids).logits del model_hf print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}') assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item() print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}') print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}') assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item() @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-j-6B"]) def test_gptj_generation(model_name): """Check that our implementation of GPT-J (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 device = 'cuda' config = gptj_config_to_gpt2_config(GPTJConfig.from_pretrained(model_name)) config.use_flash_attn = False # FlashAttention doesn't support hdim 256 yet config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True # Only prenorm supports residual_in_fp32 config.residual_in_fp32 = True tokenizer = AutoTokenizer.from_pretrained(model_name) eos_token_id = tokenizer.eos_token_id torch.manual_seed(0) batch_size = 1 seqlen = 100 max_length = 150 input_ids = torch.randint(0, config.vocab_size, (batch_size, seqlen), dtype=torch.long, device=device) model_hf = GPTJForCausalLM.from_pretrained(model_name, torch_dtype=dtype, device_map={"": device}) model_hf.eval() print("HF fp16") torch.cuda.synchronize() start = time.time() out_hf = model_hf.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') del model_hf model_ref = GPTJForCausalLM.from_pretrained(model_name, device_map={"": device}) model_ref.eval() with torch.no_grad(): logits_ref = model_ref(out_hf.sequences).logits[:, (seqlen - 1):-1] del model_ref model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype) model.eval() print('Without CUDA graph') torch.cuda.synchronize() start = time.time() out = model.generate(input_ids=input_ids, max_length=max_length, eos_token_id=eos_token_id, fused_ft_kernel=True, # eos_token_id=eos_token_id, fused_ft_kernel=False, return_dict_in_generate=True, output_scores=True, timing=True, teacher_outputs=out_hf.sequences) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') # Capture graph outside the timing loop batch_size, seqlen_og = input_ids.shape model._decoding_cache = update_graph_cache(model, None, batch_size, seqlen_og, max_length) print('With CUDA graph') torch.cuda.synchronize() start = time.time() out_cg = model.generate(input_ids=input_ids, max_length=max_length, fused_ft_kernel=True, cg=True, return_dict_in_generate=True, output_scores=True, timing=True, teacher_outputs=out_hf.sequences) torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') with torch.no_grad(): logits_parallel = model(out_hf.sequences).logits[:, (seqlen - 1):-1] logits_hf = torch.stack(out_hf.scores, dim=1) logits = torch.stack(out.scores, dim=1) logits_cg = torch.stack(out_cg.scores, dim=1) del model hf_error = (logits_hf - logits_ref).abs().max().item() assert (logits_parallel - logits_ref).abs().max().item() < 2 * hf_error print(f'HF fp16 logits max diff: {hf_error}') print(f'Logits max diff: {(logits - logits_ref).abs().max().item() }') assert (logits - logits_ref).abs().max().item() < 2 * hf_error print(f'Logits CG max diff: {(logits_cg - logits_ref).abs().max().item() }') assert torch.equal(logits_cg, logits)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gptj.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/models/test_gpt_generation_parallel.py -k "parallel" import os import re import torch import pytest from einops import rearrange from transformers import GPT2Config, GPT2Tokenizer from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel as GPT2LMHeadModelHF from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.gpt import remap_state_dict_hf_gpt2 from flash_attn.utils.pretrained import state_dict_from_pretrained from flash_attn.utils.distributed import all_gather_raw # @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) @pytest.mark.parametrize('world_size', [2]) # @pytest.mark.parametrize('fused_ft_kernel', [False, True]) @pytest.mark.parametrize('fused_ft_kernel', [True]) # @pytest.mark.parametrize('rotary', [False, True]) @pytest.mark.parametrize('rotary', [False]) @pytest.mark.parametrize('model_name', ["gpt2"]) def test_tensor_parallel(model_name, rotary, fused_ft_kernel, world_size): """Check that our implementation of GPT2 generation matches the HF implementation: the scores in fp16 should be around the same as the HF scores in fp16, when compared to the HF scores in fp32. """ dtype = torch.float16 rtol, atol = 3e-3, 3e-1 config = GPT2Config.from_pretrained(model_name) if rotary: config.n_positions = 0 config.rotary_emb_dim = 64 config.residual_in_fp32 = True config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True config.pad_vocab_size_multiple = 8 * world_size config.sequence_parallel = False # Need to set this to False for generation os.environ["NCCL_ASYNC_ERROR_HANDLING"] = "0" if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() # Need this, otherwise when we capture the graph the process for GPU 1 would run on both # GPU0 and GPU1 and things would hang torch.cuda.set_device(device) from apex.transformer import parallel_state parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() process_group = parallel_state.get_tensor_model_parallel_group() # if not rotary, we load the weight from HF but ignore the position embeddings. # The model would be nonsense but it doesn't matter for the test. model = GPTLMHeadModel.from_pretrained(model_name, config, strict=not rotary, device=device, dtype=dtype, process_group=process_group, world_size=world_size, rank=rank) model.eval() if not rotary: model_ref = GPT2LMHeadModelHF.from_pretrained(model_name).to(device=device) model_hf = GPT2LMHeadModelHF.from_pretrained(model_name).to(device=device, dtype=dtype) model_ref.eval() model_hf.eval() torch.manual_seed(0) tokenizer = GPT2Tokenizer.from_pretrained("gpt2") input_ids = tokenizer("Hello, my dog is cute and ", return_tensors="pt").input_ids.to(device=device) max_length = 30 # input_ids = torch.randint(0, 100, (1, 10), dtype=torch.long, device='cuda') # max_length = input_ids.shape[1] + 40 # Slow generation for reference sequences = [] scores = [] cur_input_ids = input_ids with torch.inference_mode(): logits, _ = all_gather_raw(model(cur_input_ids).logits[:, -1], process_group) logits = rearrange(logits, '(n b) d -> b (n d)', b=input_ids.shape[0])[..., :config.vocab_size] scores.append(logits) sequences.append(scores[-1].argmax(dim=-1)) for _ in range(input_ids.shape[1] + 1, max_length): cur_input_ids = torch.cat([cur_input_ids, rearrange(sequences[-1], 'b -> b 1')], dim=-1) logits, _ = all_gather_raw(model(cur_input_ids).logits[:, -1], process_group) logits = rearrange(logits, '(n b) d -> b (n d)', b=input_ids.shape[0])[..., :config.vocab_size] scores.append(logits) sequences.append(scores[-1].argmax(dim=-1)) sequences = torch.cat([input_ids, torch.stack(sequences, dim=1)], dim=1) scores = tuple(scores) print(sequences) out = model.generate(input_ids=input_ids, max_length=max_length, tensor_parallel=world_size, vocab_size=config.vocab_size, fused_ft_kernel=fused_ft_kernel, return_dict_in_generate=True, output_scores=True, timing=True) print(out.sequences) if fused_ft_kernel: out_cg = model.generate( input_ids=input_ids, max_length=max_length, tensor_parallel=world_size, vocab_size=config.vocab_size, fused_ft_kernel=fused_ft_kernel, cg=True, return_dict_in_generate=True, output_scores=True, timing=True) print(out_cg.sequences) if not rotary: out_hf = model_hf.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) out_ref = model_ref.generate(input_ids=input_ids, max_length=max_length, return_dict_in_generate=True, output_scores=True) print(f'Scores max diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'Scores mean diff: {(torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') print(f'HF fp16 max diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item()}') print(f'HF fp16 mean diff: {(torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().mean().item()}') assert torch.all(out.sequences == sequences) assert torch.allclose(torch.stack(out.scores, dim=1), torch.stack(scores, dim=1), rtol=rtol, atol=atol) if not rotary: assert torch.all(out.sequences == out_ref.sequences) assert torch.all(out.sequences == out_hf.sequences) assert (torch.stack(out.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item() < 3 * (torch.stack(out_hf.scores, 1) - torch.stack(out_ref.scores, 1)).abs().max().item() parallel_state.destroy_model_parallel()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gpt_generation_parallel.py
import time import torch import pytest from transformers import GPTNeoXConfig, AutoTokenizer from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXForCausalLM from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.gpt_neox import remap_state_dict_hf_gpt_neox, gpt_neox_config_to_gpt2_config from flash_attn.utils.pretrained import state_dict_from_pretrained from flash_attn.utils.generation import update_graph_cache @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-neox-20b"]) def test_gptj_state_dict(model_name): config = gpt_neox_config_to_gpt2_config(GPTNeoXConfig.from_pretrained(model_name)) pretrained_state_dict = remap_state_dict_hf_gpt_neox(state_dict_from_pretrained(model_name), config) model = GPTLMHeadModel(config, device='meta') # Without device='meta' init is very slow state_dict = model.state_dict() assert state_dict.keys() == pretrained_state_dict.keys() for k in state_dict.keys(): assert state_dict[k].shape == pretrained_state_dict[k].shape @pytest.mark.parametrize('model_name', ["EleutherAI/gpt-neox-20b"]) def test_gpt_neox_optimized(model_name): """Check that our implementation of GPT-NeoX (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 device = 'cuda' config = gpt_neox_config_to_gpt2_config(GPTNeoXConfig.from_pretrained(model_name)) config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True # GPT-NeoX-20B uses "gelu_fast" config.fused_dropout_add_ln = True config.residual_in_fp32 = True model = GPTLMHeadModel.from_pretrained(model_name, config, device=device, dtype=dtype) model.eval() torch.manual_seed(0) batch_size = 2 max_seqlen = 256 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device=device) input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device=device) with torch.no_grad(): out = model.transformer(input_ids) logits = model(input_ids).logits del model # Need at least 2 GPUs, otherwise we'll OOM # Without device_map, the model is loaded on the CPU, which is very slow model_ref = GPTNeoXForCausalLM.from_pretrained(model_name, device_map='auto') model_ref.eval() with torch.no_grad(): out_ref = model_ref.gpt_neox(input_ids).last_hidden_state.to(device=device) logits_ref = model_ref(input_ids).logits.to(device=device) del model_ref model_hf = GPTNeoXForCausalLM.from_pretrained(model_name, torch_dtype=dtype, device_map={"": device}) model_hf.eval() with torch.no_grad(): out_hf = model_hf.gpt_neox(input_ids).last_hidden_state logits_hf = model_hf(input_ids).logits del model_hf print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}') assert (out - out_ref).abs().max().item() < 2 * (out_hf - out_ref).abs().max().item() assert (out - out_ref).abs().mean().item() < 2 * (out_hf - out_ref).abs().mean().item() print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}') print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}') assert (logits - logits_ref).abs().max().item() < 2 * (logits_hf - logits_ref).abs().max().item() assert (logits - logits_ref).abs().mean().item() < 2 * (logits_hf - logits_ref).abs().mean().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gpt_neox.py
import re import torch import pytest from transformers import GPT2Config from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel as GPT2LMHeadModelHF from flash_attn.models.gpt import GPTLMHeadModel from flash_attn.models.gpt import remap_state_dict_hf_gpt2 from flash_attn.utils.pretrained import state_dict_from_pretrained @pytest.mark.parametrize('model_name', ["gpt2", "gpt2-medium"]) # @pytest.mark.parametrize('model_name', ["gpt2"]) def test_gpt2_state_dict(model_name): config = GPT2Config.from_pretrained(model_name) pretrained_state_dict = remap_state_dict_hf_gpt2(state_dict_from_pretrained(model_name), config) model = GPTLMHeadModel(config) state_dict = model.state_dict() assert state_dict.keys() == pretrained_state_dict.keys() for k in state_dict.keys(): assert state_dict[k].shape == pretrained_state_dict[k].shape @pytest.mark.parametrize('model_name', ["gpt2", "gpt2-medium"]) # @pytest.mark.parametrize('model_name', ["gpt2"]) def test_gpt2_non_optimized(model_name): """Check that our implementation of GPT2 (without any optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 config = GPT2Config.from_pretrained(model_name) model = GPTLMHeadModel.from_pretrained(model_name, config) model = model.cuda().to(dtype=dtype) model_ref = GPT2LMHeadModelHF.from_pretrained(model_name).cuda() model_hf = GPT2LMHeadModelHF.from_pretrained(model_name).cuda().to(dtype=dtype) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 4 max_seqlen = 512 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') out = model.transformer(input_ids) out_hf = model_hf.transformer(input_ids).last_hidden_state out_ref = model_ref.transformer(input_ids).last_hidden_state print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}') assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item() logits = model(input_ids).logits logits_hf = model_hf(input_ids).logits logits_ref = model_ref(input_ids).logits print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}') print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}') assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item() @pytest.mark.parametrize('model_name', ["gpt2", "gpt2-medium"]) # @pytest.mark.parametrize('model_name', ["gpt2"]) def test_gpt2_optimized(model_name): """Check that our implementation of GPT2 (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 config = GPT2Config.from_pretrained(model_name) vocab_size_og = config.vocab_size config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True config.residual_in_fp32 = True config.pad_vocab_size_multiple = 8 model = GPTLMHeadModel.from_pretrained(model_name, config) model = model.cuda().to(dtype=dtype) model_ref = GPT2LMHeadModelHF.from_pretrained(model_name).cuda() model_hf = GPT2LMHeadModelHF.from_pretrained(model_name).cuda().to(dtype=dtype) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 4 max_seqlen = 512 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') input_ids = torch.randint(0, vocab_size_og, (batch_size, max_seqlen), dtype=torch.long, device='cuda') out = model.transformer(input_ids) out_hf = model_hf.transformer(input_ids).last_hidden_state out_ref = model_ref.transformer(input_ids).last_hidden_state print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(out_hf - out_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(out_hf - out_ref).abs().mean().item()}') assert (out - out_ref).abs().max().item() < 3 * (out_hf - out_ref).abs().max().item() logits = model(input_ids).logits[..., :vocab_size_og] logits_hf = model_hf(input_ids).logits logits_ref = model_ref(input_ids).logits print(f'Logits max diff: {(logits - logits_ref).abs().max().item()}') print(f'Logits mean diff: {(logits - logits_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(logits_hf - logits_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(logits_hf - logits_ref).abs().mean().item()}') assert (logits - logits_ref).abs().max().item() < 3 * (logits_hf - logits_ref).abs().max().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gpt.py
import re import torch import pytest from timm.models.vision_transformer import vit_base_patch16_224 from flash_attn.models.vit import vit_base_patch16_224 as flash_vit_base_patch16_224 @pytest.mark.parametrize('fused_mlp', [False, True]) # @pytest.mark.parametrize('fused_mlp', [False]) @pytest.mark.parametrize('optimized', [False, True]) # @pytest.mark.parametrize('optimized', [True]) def test_vit(optimized, fused_mlp): """Check that our implementation of ViT matches the timm's implementation: the output of our forward pass in fp16 should be around the same as timm' forward pass in fp16, when compared to timm's forward pass in fp32. """ dtype = torch.float16 device = 'cuda' kwargs = {} if optimized: kwargs = dict(use_flash_attn=True, fused_bias_fc=True, fused_dropout_add_ln=True) kwargs['fused_mlp'] = fused_mlp model = flash_vit_base_patch16_224(**kwargs).to(device=device, dtype=dtype) model_ref = vit_base_patch16_224(pretrained=True).to(device=device) model_timm = vit_base_patch16_224(pretrained=True).to(device=device, dtype=dtype) model.load_state_dict(model_ref.state_dict()) model.eval() model_ref.eval() model_timm.eval() torch.manual_seed(0) batch_size = 2 x = torch.randn(batch_size, 3, 224, 224, device=device, dtype=dtype) out = model(x) out_timm = model_timm(x) out_ref = model_ref(x.float()) print(f'Output max diff: {(out - out_ref).abs().max().item()}') print(f'Output mean diff: {(out - out_ref).abs().mean().item()}') print(f'timm fp16 max diff: {(out_timm - out_ref).abs().max().item()}') print(f'timm fp16 mean diff: {(out_timm - out_ref).abs().mean().item()}') rtol = 2 if not fused_mlp else 4 assert (out - out_ref).abs().max().item() < rtol * (out_timm - out_ref).abs().max().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_vit.py
import re from collections import OrderedDict import torch import torch.nn.functional as F import pytest from einops import rearrange from transformers import BertConfig from transformers.models.bert.modeling_bert import BertModel as BertModelHF from transformers.models.bert.modeling_bert import BertForPreTraining as BertForPreTrainingHF from flash_attn.models.bert import BertModel, BertForPreTraining from flash_attn.models.bert import remap_state_dict from flash_attn.utils.pretrained import state_dict_from_pretrained @pytest.mark.parametrize('model_name', ["bert-base-uncased", "bert-large-uncased"]) # @pytest.mark.parametrize('model_name', ["bert-base-uncased"]) def test_bert_state_dict(model_name): config = BertConfig.from_pretrained(model_name) pretrained_state_dict = remap_state_dict(state_dict_from_pretrained(model_name), config) model = BertForPreTraining(config) state_dict = model.state_dict() assert state_dict.keys() == pretrained_state_dict.keys() for k in state_dict.keys(): assert state_dict[k].shape == pretrained_state_dict[k].shape def get_hf_models(model_name, config, dtype): pretrained_state_dict = state_dict_from_pretrained(model_name) def key_mapping_ln_gamma_beta(key): key = re.sub(r'LayerNorm.gamma$', 'LayerNorm.weight', key) key = re.sub(r'LayerNorm.beta$', 'LayerNorm.bias', key) return key pretrained_state_dict = OrderedDict((key_mapping_ln_gamma_beta(k), v) for k, v in pretrained_state_dict.items()) model_hf = BertForPreTrainingHF(config) # Missing key(s) in state_dict: "bert.embeddings.position_ids", "cls.predictions.decoder.bias" # position_ids is a buffer, and predictions.decoder.bias is tied to predictions.bias. model_hf.load_state_dict(pretrained_state_dict, strict=False) model_hf.cuda().to(dtype=dtype) return model_hf @pytest.mark.parametrize('model_name', ["bert-base-uncased", "bert-large-uncased"]) # @pytest.mark.parametrize('model_name', ["bert-base-uncased"]) def test_bert_non_optimized(model_name): """Check that our implementation of BERT (without any optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 config = BertConfig.from_pretrained(model_name) model = BertForPreTraining.from_pretrained(model_name, config) model = model.cuda().to(dtype=dtype) model_ref = get_hf_models(model_name, config, torch.float32) model_hf = get_hf_models(model_name, config, dtype) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 4 max_seqlen = 512 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') attention_mask = torch.arange(max_seqlen, device='cuda')[None, :] < seqlens[:, None] input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') out = model.bert(input_ids, attention_mask=attention_mask) sequence_output, pooled_output = out.last_hidden_state, out.pooler_output out_hf = model_hf.bert(input_ids, attention_mask=attention_mask) sequence_output_hf, pooled_output_hf = out_hf.last_hidden_state, out_hf.pooler_output out_ref = model_ref.bert(input_ids, attention_mask=attention_mask) sequence_output_ref, pooled_output_ref = out_ref.last_hidden_state, out_ref.pooler_output print(f'Output max diff: {(sequence_output - sequence_output_ref).abs().max().item()}') print(f'Output mean diff: {(sequence_output - sequence_output_ref).abs().mean().item()}') print(f'HF fp16 max diff: {(sequence_output_hf - sequence_output_ref).abs().max().item()}') print(f'HF fp16 mean diff: {(sequence_output_hf - sequence_output_ref).abs().mean().item()}') assert (sequence_output - sequence_output_ref).abs().max().item() < 3 * (sequence_output_hf - sequence_output_ref).abs().max().item() assert (pooled_output - pooled_output_ref).abs().max().item() < 3 * (pooled_output_hf - pooled_output_ref).abs().max().item() @pytest.mark.parametrize('model_name', ["bert-base-uncased", "bert-large-uncased"]) # @pytest.mark.parametrize('model_name', ["bert-base-uncased"]) def test_bert_optimized(model_name): """Check that our implementation of BERT (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 config = BertConfig.from_pretrained(model_name) # Our implementation of fused_mlp assumes the activation is # nn.GELU(approximate='tanh'). Huggingface calls it "gelu_new" or "gelu_fast". # If you just want "gelu", disable fused_mlp. config.hidden_act = "gelu_new" config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True model = BertForPreTraining.from_pretrained(model_name, config) model = model.cuda().to(dtype=dtype) model_ref = get_hf_models(model_name, config, torch.float32) model_hf = get_hf_models(model_name, config, dtype) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 4 max_seqlen = 512 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') attention_mask = torch.arange(max_seqlen, device='cuda')[None, :] < seqlens[:, None] input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') out = model.bert(input_ids, attention_mask=attention_mask) sequence_output, pooled_output = out.last_hidden_state, out.pooler_output out_hf = model_hf.bert(input_ids, attention_mask=attention_mask) sequence_output_hf, pooled_output_hf = out_hf.last_hidden_state, out_hf.pooler_output # Need to zero out the padded tokens in the sequence before comparison. sequence_output_hf[~attention_mask, :] = 0.0 out_ref = model_ref.bert(input_ids, attention_mask=attention_mask) sequence_output_ref, pooled_output_ref = out_ref.last_hidden_state, out_ref.pooler_output sequence_output_ref[~attention_mask, :] = 0.0 print(f'BertModel output max diff: {(sequence_output - sequence_output_ref).abs().max().item()}') print(f'BertModel output mean diff: {(sequence_output - sequence_output_ref).abs().mean().item()}') print(f'HF fp16 BertModel max diff: {(sequence_output_hf - sequence_output_ref).abs().max().item()}') print(f'HF fp16 BertModel mean diff: {(sequence_output_hf - sequence_output_ref).abs().mean().item()}') assert (sequence_output - sequence_output_ref).abs().max().item() < 4 * (sequence_output_hf - sequence_output_ref).abs().max().item() assert (pooled_output - pooled_output_ref).abs().max().item() < 4 * (pooled_output_hf - pooled_output_ref).abs().max().item() out = model(input_ids, attention_mask=attention_mask) prediction_scores, seq_relationship_scores = out.prediction_logits, out.seq_relationship_logits # Need to zero out the padded tokens in the sequence before comparison. prediction_scores = prediction_scores.clone() prediction_scores[~attention_mask, :] = 0.0 out_hf = model_hf(input_ids, attention_mask=attention_mask) prediction_scores_hf, seq_relationship_scores_hf = out_hf.prediction_logits, out_hf.seq_relationship_logits prediction_scores_hf[~attention_mask, :] = 0.0 out_ref = model_ref(input_ids, attention_mask=attention_mask) prediction_scores_ref, seq_relationship_scores_ref = out_ref.prediction_logits, out_ref.seq_relationship_logits prediction_scores_ref[~attention_mask, :] = 0.0 print(f'prediction_scores max diff: {(prediction_scores - prediction_scores_ref).abs().max().item()}') print(f'prediction_scores mean diff: {(prediction_scores - prediction_scores_ref).abs().mean().item()}') print(f'HF fp16 prediction_scoresff: {(prediction_scores_hf - prediction_scores_ref).abs().max().item()}') print(f'HF fp16 prediction_scoresiff: {(prediction_scores_hf - prediction_scores_ref).abs().mean().item()}') assert (prediction_scores - prediction_scores_ref).abs().max().item() < 2 * (prediction_scores_hf - prediction_scores_ref).abs().max().item() assert (seq_relationship_scores - seq_relationship_scores_ref).abs().max().item() < 2 * (seq_relationship_scores_hf - seq_relationship_scores_ref).abs().max().item() @pytest.mark.parametrize('last_layer_subset', [False, True]) # @pytest.mark.parametrize('last_layer_subset', [True]) @pytest.mark.parametrize('has_key_padding_mask', [True, False]) # @pytest.mark.parametrize('has_key_padding_mask', [True]) @pytest.mark.parametrize('model_name', ["bert-base-uncased", "bert-large-uncased"]) # @pytest.mark.parametrize('model_name', ["bert-base-uncased"]) def test_bert_dense_seq_output(model_name, has_key_padding_mask, last_layer_subset): """Check that our implementation of BERT (with all optimizations enabled) matches the HF implementation: the output of our forward pass in fp16 should be around the same as the HF forward pass in fp16, when compared to the HF forward pass in fp32. """ dtype = torch.float16 config = BertConfig.from_pretrained(model_name) # Our implementation of fused_mlp assumes the activation is # nn.GELU(approximate='tanh'). Huggingface calls it "gelu_new" or "gelu_fast". # If you just want "gelu", disable fused_mlp. config.hidden_act = "gelu_new" config.use_flash_attn = True config.fused_bias_fc = True config.fused_mlp = True config.fused_dropout_add_ln = True config.dense_seq_output = True config.last_layer_subset = last_layer_subset config.use_xentropy = True model = BertForPreTraining.from_pretrained(model_name, config) model = model.cuda().to(dtype=dtype) model_ref = get_hf_models(model_name, config, torch.float32) model_hf = get_hf_models(model_name, config, dtype) model.eval() model_ref.eval() model_hf.eval() torch.manual_seed(0) batch_size = 4 max_seqlen = 512 seqlens = torch.randint(max_seqlen // 2, max_seqlen + 1, (batch_size,), device='cuda') if has_key_padding_mask: attention_mask = torch.arange(max_seqlen, device='cuda')[None, :] < seqlens[:, None] else: attention_mask = None input_ids = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') labels = torch.randint(0, config.vocab_size, (batch_size, max_seqlen), dtype=torch.long, device='cuda') if attention_mask is not None: labels[~attention_mask] = 0 labels[(torch.rand(batch_size, max_seqlen, device='cuda') > 0.15)] = 0 masked_tokens_mask = labels.flatten() > 0 next_sequence_label = torch.randint(0, 2, (batch_size,), device='cuda') out = model( input_ids, attention_mask=attention_mask, labels=labels, next_sentence_label=next_sequence_label ) prediction_scores, seq_relationship_scores = out.prediction_logits, out.seq_relationship_logits out_hf = model_hf(input_ids, attention_mask=attention_mask, labels=labels, next_sentence_label=next_sequence_label) prediction_scores_hf, seq_relationship_scores_hf = out_hf.prediction_logits, out_hf.seq_relationship_logits prediction_scores_hf = rearrange(prediction_scores_hf, 'b s d -> (b s) d')[masked_tokens_mask] out_ref = model_ref(input_ids, attention_mask=attention_mask, labels=labels, next_sentence_label=next_sequence_label) prediction_scores_ref, seq_relationship_scores_ref = out_ref.prediction_logits, out_ref.seq_relationship_logits prediction_scores_ref = rearrange(prediction_scores_ref, 'b s d -> (b s) d')[masked_tokens_mask] print(f'prediction_scores max diff: {(prediction_scores - prediction_scores_ref).abs().max().item()}') print(f'prediction_scores mean diff: {(prediction_scores - prediction_scores_ref).abs().mean().item()}') print(f'HF fp16 prediction_scoresff: {(prediction_scores_hf - prediction_scores_ref).abs().max().item()}') print(f'HF fp16 prediction_scoresiff: {(prediction_scores_hf - prediction_scores_ref).abs().mean().item()}') assert (prediction_scores - prediction_scores_ref).abs().max().item() < 2 * (prediction_scores_hf - prediction_scores_ref).abs().max().item() assert (seq_relationship_scores - seq_relationship_scores_ref).abs().max().item() < 2 * (seq_relationship_scores_hf - seq_relationship_scores_ref).abs().max().item() # The loss calculation from HF is wrong: it doesn't ignore the labels that are 0. # assert (out.loss - out_ref.loss).abs().max().item() < 2 * (out_hf.loss - out_ref.loss).abs().max().item()
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_bert.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/models/test_gpt_parallel.py import math import torch import torch.nn as nn import torch.nn.functional as F import pytest from einops import rearrange from transformers import GPT2Config from apex.transformer import parallel_state from flash_attn.models.gpt import GPTLMHeadModel, shard_state_dict_tp from flash_attn.losses.cross_entropy import CrossEntropyLoss from flash_attn.utils.distributed import allreduce_sequence_parallel_grad is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [False]) @pytest.mark.parametrize('has_pos_emb', [True, False]) # @pytest.mark.parametrize('has_pos_emb', [True]) @pytest.mark.parametrize('dim', [1024]) def test_gpt_parallel(dim, has_pos_emb, sequence_parallel, world_size, dtype): head_dim = 64 assert dim % head_dim == 0 num_heads = dim // head_dim assert num_heads % world_size == 0 vocab_size = 50264 assert vocab_size % world_size == 0 num_layers = 2 rtol, atol = (3e-3, 1e-1) if dtype == torch.bfloat16 else (3e-3, 1e-2) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() process_group = parallel_state.get_tensor_model_parallel_group() # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 1024 assert (batch_size * seqlen) % world_size == 0 input_ids = torch.randint(0, vocab_size, (batch_size, seqlen + 1), device=device) # We need to generate g here so that all processes get the same gradient, # as rank 0 will have an extra bias that changes the RNG. g = torch.randn(batch_size * seqlen, device=device) config = GPT2Config(n_embd=dim, n_head=num_heads, n_layer=num_layers, n_positions=seqlen if has_pos_emb else 0, vocab_size=50257, resid_pdrop=0.0, embd_pdrop=0.0, attn_pdrop=0.0, scale_attn_by_inverse_layer_idx=True, use_flash_attn=True, fused_mlp=True, fused_bias_fc=True, fused_dropout_add_ln=True, residual_in_fp32=True, rotary_emb_fraction=0.0 if has_pos_emb else 0.5, pad_vocab_size_multiple=8 * world_size, sequence_parallel=sequence_parallel) config.vocab_size = math.ceil(config.vocab_size / (8 * world_size)) * (8 * world_size) model_pt = GPTLMHeadModel(config, device=device) def init_layer_norm(module): if isinstance(module, nn.LayerNorm): nn.init.normal_(module.weight) nn.init.normal_(module.bias) model_pt.apply(init_layer_norm) model = GPTLMHeadModel(config, process_group=process_group, device=device) total_nparams = sum(p.numel() for p in model_pt.parameters()) sharded_nparams = sum(p.numel() for p in model.parameters()) sharded_nparams_all = torch.empty(world_size, dtype=torch.long, device=device) torch.distributed.all_gather_into_tensor( sharded_nparams_all, torch.tensor([sharded_nparams], device=device), group=process_group ) shared_nparams = sum(p.numel() for p in model.parameters() if getattr(p, '_shared_params', False)) shared_nparams_all = torch.empty(world_size, dtype=torch.long, device=device) torch.distributed.all_gather_into_tensor( shared_nparams_all, torch.tensor([shared_nparams], device=device), group=process_group ) assert torch.all(shared_nparams_all == shared_nparams) assert total_nparams == ((sharded_nparams_all - shared_nparams_all).sum().item() + shared_nparams) # vocab_size has been rounded up here partition_vocab_size = config.vocab_size // world_size partition_dim = dim // world_size partition_hidden_dim = 4 * dim // world_size with torch.no_grad(): model.load_state_dict(shard_state_dict_tp(model_pt.state_dict(), config, world_size, rank)) model.tie_weights() with torch.autocast(device_type='cuda', dtype=dtype): out = model(input_ids[:, :-1]).logits if not sequence_parallel: out = rearrange(out, 'b s d -> (b s) d') out_pt = rearrange(model_pt(input_ids[:, :-1]).logits, 'b s d -> (b s) d') partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( out, out_pt[:, rank * partition_vocab_size:(rank + 1) * partition_vocab_size], rtol=rtol, atol=atol ) loss_fn = CrossEntropyLoss(inplace_backward=True, reduction='none', process_group=process_group) loss_fn_pt = CrossEntropyLoss(inplace_backward=True, reduction='none') loss = loss_fn(out, input_ids[:, 1:].flatten()) loss_pt = loss_fn_pt(out_pt, input_ids[:, 1:].flatten()) assert torch.allclose(loss, loss_pt, rtol=rtol, atol=atol) loss_pt.backward(g) loss.backward(g) allreduce_sequence_parallel_grad(model, process_group) parallel_state.destroy_model_parallel() grad_dict = shard_state_dict_tp({k: v.grad for k, v in model_pt.named_parameters()}, config, world_size, rank) assert torch.allclose( model.transformer.embeddings.word_embeddings.weight.grad, grad_dict['transformer.embeddings.word_embeddings.weight'], rtol=rtol, atol=atol * 5 ) if has_pos_emb: assert torch.allclose( model.transformer.embeddings.position_embeddings.weight.grad, grad_dict['transformer.embeddings.position_embeddings.weight'], rtol=rtol, atol=atol ) assert torch.allclose(model.transformer.ln_f.weight.grad, grad_dict['transformer.ln_f.weight'], rtol=rtol, atol=atol) assert torch.allclose(model.transformer.ln_f.bias.grad, grad_dict['transformer.ln_f.bias'], rtol=rtol, atol=atol) for i in range(num_layers): assert torch.allclose( model.transformer.layers[i].mixer.Wqkv.weight.grad, grad_dict[f'transformer.layers.{i}.mixer.Wqkv.weight'], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.transformer.layers[i].mixer.Wqkv.bias.grad, grad_dict[f'transformer.layers.{i}.mixer.Wqkv.bias'], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.transformer.layers[i].mixer.out_proj.weight.grad, grad_dict[f'transformer.layers.{i}.mixer.out_proj.weight'], rtol=rtol, atol=atol * 10 ) if rank == 0: assert torch.allclose(model.transformer.layers[i].mixer.out_proj.bias.grad, grad_dict[f'transformer.layers.{i}.mixer.out_proj.bias'], rtol=rtol, atol=atol * 5) assert torch.allclose( model.transformer.layers[i].mlp.fc1.weight.grad, grad_dict[f'transformer.layers.{i}.mlp.fc1.weight'], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.transformer.layers[i].mlp.fc1.bias.grad, grad_dict[f'transformer.layers.{i}.mlp.fc1.bias'], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.transformer.layers[i].mlp.fc2.weight.grad, grad_dict[f'transformer.layers.{i}.mlp.fc2.weight'], rtol=rtol, atol=atol * 10 ) if rank == 0: assert torch.allclose(model.transformer.layers[i].mlp.fc2.bias.grad, grad_dict[f'transformer.layers.{i}.mlp.fc2.bias'], rtol=rtol, atol=atol * 5) assert torch.allclose(model.transformer.layers[i].norm1.weight.grad, grad_dict[f'transformer.layers.{i}.norm1.weight'], rtol=rtol, atol=atol) assert torch.allclose(model.transformer.layers[i].norm1.bias.grad, grad_dict[f'transformer.layers.{i}.norm1.bias'], rtol=rtol, atol=atol) assert torch.allclose(model.transformer.layers[i].norm2.weight.grad, grad_dict[f'transformer.layers.{i}.norm2.weight'], rtol=rtol, atol=atol) assert torch.allclose(model.transformer.layers[i].norm2.bias.grad, grad_dict[f'transformer.layers.{i}.norm2.bias'], rtol=rtol, atol=atol)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/models/test_gpt_parallel.py
import math import torch import torch.nn.functional as F import pytest from einops import rearrange, repeat from flash_attn.ops.layer_norm import DropoutAddLayerNorm, dropout_add_layer_norm from flash_attn.ops.layer_norm import dropout_add_layer_norm_subset from flash_attn.ops.rms_norm import DropoutAddRMSNorm, dropout_add_rms_norm from flash_attn.ops.rms_norm import dropout_add_rms_norm_subset from flash_attn.ops.layer_norm import dropout_add_layer_norm_parallel_residual from flash_attn.ops.rms_norm import dropout_add_rms_norm_parallel_residual try: from apex.normalization import FusedRMSNorm from apex.normalization.fused_layer_norm import fused_rms_norm_affine except: FusedRMSNorm, fused_rms_norm_affine = None, None is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('is_rms_norm', [False, True]) @pytest.mark.parametrize('has_colscale', [True, False]) # @pytest.mark.parametrize('has_colscale', [False]) @pytest.mark.parametrize('has_rowscale', [True, False]) # @pytest.mark.parametrize('has_rowscale', [True]) @pytest.mark.parametrize('has_residual', [True, False]) # @pytest.mark.parametrize('has_residual', [False]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) # @pytest.mark.parametrize('dropout_p', [0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) # @pytest.mark.parametrize('weight_dtype', [torch.float32]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_training(hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_residual, has_rowscale, has_colscale, is_rms_norm): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported if is_rms_norm and FusedRMSNorm is None: pytest.skip() # We need Apex's FusedRMSNorm to test layer_norm_cls = torch.nn.LayerNorm if not is_rms_norm else FusedRMSNorm our_layer_norm_cls = DropoutAddLayerNorm if not is_rms_norm else DropoutAddRMSNorm our_layer_norm_func = dropout_add_layer_norm if not is_rms_norm else dropout_add_rms_norm device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 1e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_colscale: colscale = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) colscale_pt = colscale.detach().clone().requires_grad_() colscale_ref = colscale.detach().clone().float().requires_grad_() else: colscale = None if has_residual: res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None if has_rowscale: rowscale = torch.empty(batch_size, seqlen, device=device, dtype=input_dtype) survival_rate = 0.87 rowscale = rowscale.bernoulli_(survival_rate) / survival_rate x0_scaled_pt = x0_pt * rearrange(rowscale, '... -> ... 1') x0_scaled_ref = x0_ref * rearrange(rowscale, '... -> ... 1') else: rowscale = None x0_scaled_pt = x0_pt x0_scaled_ref = x0_ref if has_colscale: x0_scaled_pt = x0_scaled_pt * colscale_pt x0_scaled_ref = x0_scaled_ref * colscale_ref model_pt = layer_norm_cls(hidden_size).to(device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) if not is_rms_norm: torch.nn.init.normal_(model_pt.bias) model_ref = layer_norm_cls(hidden_size).to(device=device, dtype=torch.float32) model = our_layer_norm_cls(hidden_size, p=dropout_p, device=device, dtype=weight_dtype) with torch.no_grad(): model.weight.copy_(model_pt.weight) model_ref.weight.copy_(model_pt.weight) if not is_rms_norm: model.bias.copy_(model_pt.bias) model_ref.bias.copy_(model_pt.bias) residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out, dmask = our_layer_norm_func(x0, res, model.weight, model.bias, model.p, model.epsilon, rowscale=rowscale, layerscale=colscale, residual_in_fp32=residual_in_fp32, return_dropout_mask=True) assert out.dtype == input_dtype print(f'Actual dropout fraction: {1 - dmask.float().mean().item()}') if has_residual: residual_pt = ((x0_scaled_pt.float() * dmask.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask.float()) / (1 - dropout_p) + res_ref else: residual_pt = ((x0_scaled_pt.float() * dmask.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask.float()) / (1 - dropout_p) out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(dtype=input_dtype) out_ref = model_ref(residual_ref) assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 g = torch.randn_like(out) / batch_size out_pt.backward(g) out.backward(g) out_ref.backward(g) assert (x0.grad - x0_ref.grad).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad).abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (model.weight.grad - model_ref.weight.grad).abs().max() <= 3 * (model_pt.weight.grad - model_ref.weight.grad).abs().max() + 3e-5 if not is_rms_norm: assert (model.bias.grad - model_ref.bias.grad).abs().max() <= 2 * (model_pt.bias.grad - model_ref.bias.grad).abs().max() + 3e-5 if has_colscale: assert (colscale.grad - colscale_ref.grad).abs().max() <= 2 * (colscale_pt.grad - colscale_ref.grad).abs().max() + 2e-4 @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) @pytest.mark.parametrize('hidden_size', [768, 1024, 1280, 1536, 1600, 2048, 2560, 3072, 4096, 5120]) def test_dropout_layer_norm_eval(hidden_size, input_dtype, residual_dtype, weight_dtype): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported device = 'cuda' # rtol, atol = (1e-5, 1e-6) if dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 1e-4) dropout_p = 0.37 # set seed torch.random.manual_seed(0) batch_size = 32 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() model_pt = torch.nn.LayerNorm(hidden_size, device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) torch.nn.init.normal_(model_pt.bias) model = DropoutAddLayerNorm(hidden_size, p=dropout_p, device=device, dtype=weight_dtype) model_ref = torch.nn.LayerNorm(hidden_size, device=device, dtype=torch.float32) with torch.no_grad(): model.weight.copy_(model_pt.weight) model.bias.copy_(model_pt.bias) model_ref.weight.copy_(model_pt.weight) model_ref.bias.copy_(model_pt.bias) model_pt.eval() model.eval() model_ref.eval() out = model(x0, res) residual_pt = (x0_pt.float() + res_pt.float()).to(dtype=residual_dtype) residual_ref = x0_ref + res_ref out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(input_dtype) out_ref = model_ref(residual_ref) assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 @pytest.mark.parametrize('is_rms_norm', [False, True]) @pytest.mark.parametrize('has_colscale', [True, False]) @pytest.mark.parametrize('has_rowscale', [True, False]) @pytest.mark.parametrize('has_residual', [True, False]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('has_colscale', [True]) # @pytest.mark.parametrize('has_rowscale', [False]) # @pytest.mark.parametrize('has_residual', [True]) # @pytest.mark.parametrize('dropout_p', [0.0]) # @pytest.mark.parametrize('weight_dtype', [torch.float32]) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float32, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_prenorm_training(hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_residual, has_rowscale, has_colscale, is_rms_norm): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported if is_rms_norm and FusedRMSNorm is None: pytest.skip() # We need Apex's FusedRMSNorm to test layer_norm_cls = torch.nn.LayerNorm if not is_rms_norm else FusedRMSNorm our_layer_norm_cls = DropoutAddLayerNorm if not is_rms_norm else DropoutAddRMSNorm our_layer_norm_func = dropout_add_layer_norm if not is_rms_norm else dropout_add_rms_norm device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 2e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_colscale: colscale = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) colscale_pt = colscale.detach().clone().requires_grad_() colscale_ref = colscale.detach().clone().float().requires_grad_() else: colscale = None if has_residual: res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None if has_rowscale: rowscale = torch.empty(batch_size, seqlen, device=device, dtype=input_dtype) survival_rate = 0.87 rowscale = rowscale.bernoulli_(survival_rate) / survival_rate x0_scaled_pt = x0_pt * rearrange(rowscale, '... -> ... 1') x0_scaled_ref = x0_ref * rearrange(rowscale, '... -> ... 1') else: rowscale = None x0_scaled_pt = x0_pt x0_scaled_ref = x0_ref if has_colscale: x0_scaled_pt = x0_scaled_pt * colscale_pt x0_scaled_ref = x0_scaled_ref * colscale_ref model_pt = layer_norm_cls(hidden_size).to(device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) if not is_rms_norm: torch.nn.init.normal_(model_pt.bias) model_ref = layer_norm_cls(hidden_size).to(device=device, dtype=torch.float32) model = our_layer_norm_cls(hidden_size, prenorm=True, p=dropout_p, device=device, dtype=weight_dtype) with torch.no_grad(): model.weight.copy_(model_pt.weight) model_ref.weight.copy_(model_pt.weight) if not is_rms_norm: model.bias.copy_(model_pt.bias) model_ref.bias.copy_(model_pt.bias) residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out, residual, dmask = our_layer_norm_func(x0, res, model.weight, model.bias, model.p, model.epsilon, rowscale=rowscale, layerscale=colscale, prenorm=True, residual_in_fp32=residual_in_fp32, return_dropout_mask=True) print(f'Actual dropout fraction: {1 - dmask.float().mean().item()}') if has_residual: residual_pt = ((x0_scaled_pt.float() * dmask.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask.float()) / (1 - dropout_p) + res_ref else: residual_pt = ((x0_scaled_pt.float() * dmask.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask.float()) / (1 - dropout_p) out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(dtype=input_dtype) out_ref = model_ref(residual_ref) assert out.dtype == input_dtype assert residual.dtype == residual_dtype assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 assert (residual - residual_ref).abs().max() <= 4 * (residual_pt - residual_ref).abs().max() + 1e-4 g = torch.randn_like(out) / batch_size (out_pt * F.sigmoid(residual_pt)).backward(g) (out * F.sigmoid(residual)).backward(g) (out_ref * F.sigmoid(residual_ref.to(dtype=residual_dtype))).backward(g) assert (x0.grad - x0_ref.grad).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad).abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (model.weight.grad - model_ref.weight.grad).abs().max() <= 2 * (model_pt.weight.grad - model_ref.weight.grad).abs().max() + 2e-4 if not is_rms_norm: assert (model.bias.grad - model_ref.bias.grad).abs().max() <= 2 * (model_pt.bias.grad - model_ref.bias.grad).abs().max() + 2e-4 if has_colscale: assert (colscale.grad - colscale_ref.grad).abs().max() <= 2 * (colscale_pt.grad - colscale_ref.grad).abs().max() + 2e-4 @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) @pytest.mark.parametrize('hidden_size', [768, 1024, 1280, 1536, 1600, 2048, 2560, 3072, 4096, 5120]) def test_dropout_layer_norm_prenorm_eval(hidden_size, input_dtype, residual_dtype, weight_dtype): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported device = 'cuda' # rtol, atol = (1e-5, 1e-6) if dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 1e-4) dropout_p = 0.37 # set seed torch.random.manual_seed(0) batch_size = 32 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() model_pt = torch.nn.LayerNorm(hidden_size, device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) torch.nn.init.normal_(model_pt.bias) model = DropoutAddLayerNorm(hidden_size, prenorm=True, p=dropout_p, device=device, dtype=weight_dtype) model_ref = torch.nn.LayerNorm(hidden_size, device=device, dtype=torch.float32) with torch.no_grad(): model.weight.copy_(model_pt.weight) model.bias.copy_(model_pt.bias) model_ref.weight.copy_(model_pt.weight) model_ref.bias.copy_(model_pt.bias) model_pt.eval() model.eval() model_ref.eval() out, residual = model(x0, res) residual_pt = (x0_pt.float() + res_pt.float()).to(dtype=residual_dtype) residual_ref = x0_ref + res_ref out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(input_dtype) out_ref = model_ref(residual_ref) assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 assert (residual - residual_ref).abs().max() <= 4 * (residual_pt - residual_ref).abs().max() + 1e-4 @pytest.mark.parametrize('has_colscale', [True, False]) @pytest.mark.parametrize('has_residual', [True, False]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('has_colscale', [True]) # @pytest.mark.parametrize('has_residual', [True]) # @pytest.mark.parametrize('dropout_p', [0.0]) # @pytest.mark.parametrize('weight_dtype', [torch.float32]) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float32, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_subset_training( hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_residual, has_colscale): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 2e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 drop_path_rate = 0.4 drop_path_scale = 1 / (1 - drop_path_rate) def generate_droppath_masks(batch_size, seqlen, drop_path_rate, device): # Do it on CPU so we can get the numrows (with .item()) without GPU-CPU sync mask_batch = torch.rand(batch_size) < 1 - drop_path_rate numrows = (mask_batch).sum().item() * seqlen mask_batch = mask_batch.to(device=device, non_blocking=True) mask_batch_seqlen = repeat(mask_batch, 'b -> (b s)', s=seqlen) subset = torch.cumsum(mask_batch_seqlen, dim=0, dtype=torch.int32).masked_fill_(~mask_batch_seqlen, 0) return mask_batch, numrows, rearrange(subset, '(b s) -> b s', b=batch_size) x0_mask_batch, x0_numrows, x0_subset = generate_droppath_masks(batch_size, seqlen, drop_path_rate, device) out_mask_batch, out_numrows, out_subset = generate_droppath_masks(batch_size, seqlen, drop_path_rate, device) x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone()[x0_mask_batch].requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_colscale: colscale = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) colscale_pt = colscale.detach().clone().requires_grad_() colscale_ref = colscale.detach().clone().float().requires_grad_() else: colscale = None if has_residual: res_pt = torch.randn_like(x0_pt, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None if has_colscale: x0_scaled_pt = x0_pt * colscale_pt x0_scaled_ref = x0_ref * colscale_ref else: x0_scaled_pt = x0_pt x0_scaled_ref = x0_ref model_pt = torch.nn.LayerNorm(hidden_size, device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) torch.nn.init.normal_(model_pt.bias) model_ref = torch.nn.LayerNorm(hidden_size, device=device, dtype=torch.float32) model = DropoutAddLayerNorm(hidden_size, prenorm=False, p=dropout_p, device=device, dtype=weight_dtype) with torch.no_grad(): model.weight.copy_(model_pt.weight) model.bias.copy_(model_pt.bias) model_ref.weight.copy_(model_pt.weight) model_ref.bias.copy_(model_pt.bias) residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out, dmask = dropout_add_layer_norm_subset( x0, res, model.weight, model.bias, model.p, model.epsilon, layerscale=colscale, x0_subset=x0_subset, out_subset=out_subset, rowscale_const=drop_path_scale, out_numrows = out_numrows, prenorm=False, residual_in_fp32=residual_in_fp32, return_dropout_mask=True) print(f'Actual dropout fraction: {1 - dmask.float().mean().item()}') x0_scaled_pt = x0_scaled_pt.masked_fill( repeat(~x0_mask_batch, 'b -> b s d', s=seqlen, d=hidden_size), 0 ) * drop_path_scale x0_scaled_ref = x0_scaled_ref.masked_fill( repeat(~x0_mask_batch, 'b -> b s d', s=seqlen, d=hidden_size), 0 ) * drop_path_scale dmask_expanded = torch.zeros_like(x0_pt, dtype=torch.uint8) dmask_expanded[x0_mask_batch] = dmask if has_residual: residual_pt = ((x0_scaled_pt.float() * dmask_expanded.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask_expanded.float()) / (1 - dropout_p) + res_ref else: residual_pt = ((x0_scaled_pt.float() * dmask_expanded.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask_expanded.float()) / (1 - dropout_p) out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(dtype=input_dtype)[out_mask_batch] out_ref = model_ref(residual_ref)[out_mask_batch] assert out.dtype == input_dtype assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 g = torch.randn_like(out) / batch_size out_pt.backward(g) out.backward(g) out_ref.backward(g) assert (x0.grad - x0_ref.grad[x0_mask_batch]).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad)[x0_mask_batch].abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (model.weight.grad - model_ref.weight.grad).abs().max() <= 2 * (model_pt.weight.grad - model_ref.weight.grad).abs().max() + 2e-4 assert (model.bias.grad - model_ref.bias.grad).abs().max() <= 2 * (model_pt.bias.grad - model_ref.bias.grad).abs().max() + 2e-4 if has_colscale: assert (colscale.grad - colscale_ref.grad).abs().max() <= 2 * (colscale_pt.grad - colscale_ref.grad).abs().max() + 2e-4 @pytest.mark.parametrize('has_colscale', [True, False]) @pytest.mark.parametrize('has_residual', [True, False]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('has_colscale', [True]) # @pytest.mark.parametrize('has_residual', [True]) # @pytest.mark.parametrize('dropout_p', [0.0]) # @pytest.mark.parametrize('weight_dtype', [torch.float32]) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float32, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_subset_prenorm_training( hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_residual, has_colscale): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 2e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 drop_path_rate = 0.4 drop_path_scale = 1 / (1 - drop_path_rate) def generate_droppath_masks(batch_size, seqlen, drop_path_rate, device): # Do it on CPU so we can get the numrows (with .item()) without GPU-CPU sync mask_batch = torch.rand(batch_size) < 1 - drop_path_rate numrows = (mask_batch).sum().item() * seqlen mask_batch = mask_batch.to(device=device, non_blocking=True) mask_batch_seqlen = repeat(mask_batch, 'b -> (b s)', s=seqlen) subset = torch.cumsum(mask_batch_seqlen, dim=0, dtype=torch.int32).masked_fill_(~mask_batch_seqlen, 0) return mask_batch, numrows, rearrange(subset, '(b s) -> b s', b=batch_size) x0_mask_batch, x0_numrows, x0_subset = generate_droppath_masks(batch_size, seqlen, drop_path_rate, device) out_mask_batch, out_numrows, out_subset = generate_droppath_masks(batch_size, seqlen, drop_path_rate, device) x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone()[x0_mask_batch].requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_colscale: colscale = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) colscale_pt = colscale.detach().clone().requires_grad_() colscale_ref = colscale.detach().clone().float().requires_grad_() else: colscale = None if has_residual: res_pt = torch.randn_like(x0_pt, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None if has_colscale: x0_scaled_pt = x0_pt * colscale_pt x0_scaled_ref = x0_ref * colscale_ref else: x0_scaled_pt = x0_pt x0_scaled_ref = x0_ref model_pt = torch.nn.LayerNorm(hidden_size, device=device, dtype=weight_dtype) torch.nn.init.normal_(model_pt.weight) torch.nn.init.normal_(model_pt.bias) model_ref = torch.nn.LayerNorm(hidden_size, device=device, dtype=torch.float32) model = DropoutAddLayerNorm(hidden_size, prenorm=True, p=dropout_p, device=device, dtype=weight_dtype) with torch.no_grad(): model.weight.copy_(model_pt.weight) model.bias.copy_(model_pt.bias) model_ref.weight.copy_(model_pt.weight) model_ref.bias.copy_(model_pt.bias) residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out, residual, dmask = dropout_add_layer_norm_subset( x0, res, model.weight, model.bias, model.p, model.epsilon, layerscale=colscale, x0_subset=x0_subset, out_subset=out_subset, rowscale_const=drop_path_scale, out_numrows = out_numrows, prenorm=True, residual_in_fp32=residual_in_fp32, return_dropout_mask=True) print(f'Actual dropout fraction: {1 - dmask.float().mean().item()}') x0_scaled_pt = x0_scaled_pt.masked_fill( repeat(~x0_mask_batch, 'b -> b s d', s=seqlen, d=hidden_size), 0 ) * drop_path_scale x0_scaled_ref = x0_scaled_ref.masked_fill( repeat(~x0_mask_batch, 'b -> b s d', s=seqlen, d=hidden_size), 0 ) * drop_path_scale dmask_expanded = torch.zeros_like(x0_pt, dtype=torch.uint8) dmask_expanded[x0_mask_batch] = dmask if has_residual: residual_pt = ((x0_scaled_pt.float() * dmask_expanded.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask_expanded.float()) / (1 - dropout_p) + res_ref else: residual_pt = ((x0_scaled_pt.float() * dmask_expanded.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_scaled_ref * dmask_expanded.float()) / (1 - dropout_p) out_pt = model_pt(residual_pt.to(dtype=weight_dtype)).to(dtype=input_dtype)[out_mask_batch] out_ref = model_ref(residual_ref)[out_mask_batch] assert out.dtype == input_dtype assert residual.dtype == residual_dtype assert (out - out_ref).abs().max() <= 4 * (out_pt - out_ref).abs().max() + 1e-4 assert (residual - residual_ref).abs().max() <= 4 * (residual_pt - residual_ref).abs().max() + 1e-4 g = torch.randn_like(out) / batch_size (out_pt * F.sigmoid(residual_pt[out_mask_batch]) + residual_pt.mean(0, keepdim=True)).backward(g) (out * F.sigmoid(residual[out_mask_batch]) + residual.mean(0, keepdim=True)).backward(g) (out_ref * F.sigmoid(residual_ref[out_mask_batch].to(dtype=residual_dtype)) + residual_ref.mean(0, keepdim=True)).backward(g) assert (x0.grad - x0_ref.grad[x0_mask_batch]).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad)[x0_mask_batch].abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (model.weight.grad - model_ref.weight.grad).abs().max() <= 2 * (model_pt.weight.grad - model_ref.weight.grad).abs().max() + 2e-4 assert (model.bias.grad - model_ref.bias.grad).abs().max() <= 2 * (model_pt.bias.grad - model_ref.bias.grad).abs().max() + 2e-4 if has_colscale: assert (colscale.grad - colscale_ref.grad).abs().max() <= 2 * (colscale_pt.grad - colscale_ref.grad).abs().max() + 2e-4 @pytest.mark.parametrize('is_rms_norm', [False, True]) # @pytest.mark.parametrize('is_rms_norm', [False]) @pytest.mark.parametrize('tied_norm', [False, True]) # @pytest.mark.parametrize('tied_norm', [False]) @pytest.mark.parametrize('has_residual', [True, False]) # @pytest.mark.parametrize('has_residual', [False]) @pytest.mark.parametrize('has_x1', [True, False]) # @pytest.mark.parametrize('has_x1', [True]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) # @pytest.mark.parametrize('dropout_p', [0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) # @pytest.mark.parametrize('weight_dtype', [torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_parallel_residual_training( hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_x1, has_residual, tied_norm, is_rms_norm ): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported if is_rms_norm and fused_rms_norm_affine is None: pytest.skip() # We need Apex's FusedRMSNorm to test our_layer_norm_func = (dropout_add_layer_norm_parallel_residual if not is_rms_norm else dropout_add_rms_norm_parallel_residual) device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 1e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_x1: x1_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x1 = x1_pt.detach().clone().requires_grad_() x1_ref = x1_pt.detach().clone().float().requires_grad_() else: x1 = None if has_residual: res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None weight0 = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) bias0 = (torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) if not is_rms_norm else None) weight0_pt = weight0.detach().clone().requires_grad_() weight0_ref = weight0.detach().clone().float().requires_grad_() bias0_pt = bias0.detach().clone().requires_grad_() if bias0 is not None else None bias0_ref = bias0.detach().clone().float().requires_grad_() if bias0 is not None else None if not tied_norm: weight1 = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) bias1 = (torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) if not is_rms_norm else None) weight1_pt = weight1.detach().clone().requires_grad_() weight1_ref = weight1.detach().clone().float().requires_grad_() bias1_pt = bias1.detach().clone().requires_grad_() if bias1 is not None else None bias1_ref = bias1.detach().clone().float().requires_grad_() if bias1 is not None else None else: weight1, bias1 = None, None epsilon = 1e-5 residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out0, out1, dmask0, dmask1 = our_layer_norm_func( x0, x1, res, weight0, bias0, weight1, bias1, dropout_p, epsilon, residual_in_fp32=residual_in_fp32, return_dropout_mask=True ) assert out0.dtype == input_dtype if not tied_norm: assert out1.dtype == input_dtype print(f'Actual dropout fraction: {1 - dmask0.float().mean().item()}') if has_residual: if has_x1: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + (x1_pt.float() * dmask1.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = ((x0_ref * dmask0.float()) / (1 - dropout_p) + (x1_ref * dmask1.float()) / (1 - dropout_p)) + res_ref else: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_ref * dmask0.float()) / (1 - dropout_p) + res_ref else: if has_x1: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + (x1_pt.float() * dmask1.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = ((x0_ref * dmask0.float()) / (1 - dropout_p) + (x1_ref * dmask1.float()) / (1 - dropout_p)) else: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_ref * dmask0.float()) / (1 - dropout_p) if not is_rms_norm: out0_pt = F.layer_norm(residual_pt.to(dtype=weight_dtype), (hidden_size,), weight0_pt, bias0_pt, eps=epsilon).to(dtype=input_dtype) out0_ref = F.layer_norm(residual_ref, (hidden_size,), weight0_ref, bias0_ref, eps=epsilon) if not tied_norm: out1_pt = F.layer_norm(residual_pt.to(dtype=weight_dtype), (hidden_size,), weight1_pt, bias1_pt, eps=epsilon).to(dtype=input_dtype) out1_ref = F.layer_norm(residual_ref, (hidden_size,), weight1_ref, bias1_ref, eps=epsilon) else: out0_pt = fused_rms_norm_affine(residual_pt.to(dtype=weight_dtype), weight0_pt, (hidden_size,), eps=epsilon).to(dtype=input_dtype) out0_ref = fused_rms_norm_affine(residual_ref, weight0_ref, (hidden_size,), eps=epsilon) if not tied_norm: out1_pt = fused_rms_norm_affine(residual_pt.to(dtype=weight_dtype), weight1_pt, (hidden_size,), eps=epsilon).to(dtype=input_dtype) out1_ref = fused_rms_norm_affine(residual_ref, weight1_ref, (hidden_size,), eps=epsilon) assert (out0 - out0_ref).abs().max() <= 4 * (out0_pt - out0_ref).abs().max() + 1e-4 if not tied_norm: assert (out1 - out1_ref).abs().max() <= 4 * (out1_pt - out1_ref).abs().max() + 1e-4 g0 = torch.randn_like(out0) / batch_size if tied_norm: out0.backward(g0) out0_pt.backward(g0) out0_ref.backward(g0) else: g1 = torch.randn_like(out1) / batch_size (out0 * g0 + out1 * g1).sum().backward() (out0_pt * g0 + out1_pt * g1).sum().backward() (out0_ref * g0 + out1_ref * g1).sum().backward() assert (x0.grad - x0_ref.grad).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad).abs().max() + 1e-4 if has_x1: assert (x1.grad - x1_ref.grad).abs().max() <= 4 * (x1_pt.grad - x1_ref.grad).abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (weight0.grad - weight0_ref.grad).abs().max() <= 3 * (weight0_pt.grad - weight0_ref.grad).abs().max() + 3e-5 if not is_rms_norm: assert (bias0.grad - bias0_ref.grad).abs().max() <= 2 * (bias0_pt.grad - bias0_ref.grad).abs().max() + 3e-5 if not tied_norm: assert (weight1.grad - weight1_ref.grad).abs().max() <= 3 * (weight1_pt.grad - weight1_ref.grad).abs().max() + 3e-5 if not is_rms_norm: assert (bias1.grad - bias1_ref.grad).abs().max() <= 2 * (bias1_pt.grad - bias1_ref.grad).abs().max() + 3e-5 @pytest.mark.parametrize('is_rms_norm', [False, True]) # @pytest.mark.parametrize('is_rms_norm', [False]) @pytest.mark.parametrize('tied_norm', [False, True]) # @pytest.mark.parametrize('tied_norm', [False]) @pytest.mark.parametrize('has_residual', [True, False]) # @pytest.mark.parametrize('has_residual', [False]) @pytest.mark.parametrize('has_x1', [True, False]) # @pytest.mark.parametrize('has_x1', [True]) @pytest.mark.parametrize('dropout_p', [0.37, 0.0]) # @pytest.mark.parametrize('dropout_p', [0.0]) @pytest.mark.parametrize('weight_dtype', [torch.float32, torch.float16]) # @pytest.mark.parametrize('weight_dtype', [torch.float16]) @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float16), (torch.float16, torch.float32), (torch.float32, torch.float32)] + ([(torch.bfloat16, torch.bfloat16), (torch.bfloat16, torch.float32)] if is_sm8x else [])) # @pytest.mark.parametrize('input_dtype,residual_dtype', [(torch.float16, torch.float32)]) @pytest.mark.parametrize('hidden_size', [192, 256, 384, 768, 1024, 1280, 1536, 1600, 2048, 2560, 3000, 3072, 4096, 5120, 6144]) # @pytest.mark.parametrize('hidden_size', [256]) def test_dropout_layer_norm_parallel_residual_prenorm_training( hidden_size, input_dtype, residual_dtype, weight_dtype, dropout_p, has_x1, has_residual, tied_norm, is_rms_norm ): if weight_dtype == torch.float16 and input_dtype == torch.bfloat16: pytest.skip() # Not supported if is_rms_norm and fused_rms_norm_affine is None: pytest.skip() # We need Apex's FusedRMSNorm to test our_layer_norm_func = (dropout_add_layer_norm_parallel_residual if not is_rms_norm else dropout_add_rms_norm_parallel_residual) device = 'cuda' # rtol, atol = (1e-5, 1e-6) if input_dtype == torch.float32 else (1e-3, 1e-4) rtol, atol = (1e-3, 1e-4) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x0_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x0 = x0_pt.detach().clone().requires_grad_() x0_ref = x0_pt.detach().clone().float().requires_grad_() if has_x1: x1_pt = torch.randn(batch_size, seqlen, hidden_size, device=device, dtype=input_dtype, requires_grad=True) x1 = x1_pt.detach().clone().requires_grad_() x1_ref = x1_pt.detach().clone().float().requires_grad_() else: x1 = None if has_residual: res_pt = torch.randn_like(x0, dtype=residual_dtype, requires_grad=True) res = res_pt.detach().clone().requires_grad_() res_ref = res_pt.detach().clone().float().requires_grad_() else: res = None weight0 = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) bias0 = (torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) if not is_rms_norm else None) weight0_pt = weight0.detach().clone().requires_grad_() weight0_ref = weight0.detach().clone().float().requires_grad_() bias0_pt = bias0.detach().clone().requires_grad_() if bias0 is not None else None bias0_ref = bias0.detach().clone().float().requires_grad_() if bias0 is not None else None if not tied_norm: weight1 = torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) bias1 = (torch.randn(hidden_size, device=device, dtype=weight_dtype, requires_grad=True) if not is_rms_norm else None) weight1_pt = weight1.detach().clone().requires_grad_() weight1_ref = weight1.detach().clone().float().requires_grad_() bias1_pt = bias1.detach().clone().requires_grad_() if bias1 is not None else None bias1_ref = bias1.detach().clone().float().requires_grad_() if bias1 is not None else None else: weight1, bias1 = None, None epsilon = 1e-5 residual_in_fp32 = (not has_residual) and residual_dtype == torch.float32 out0, out1, residual, dmask0, dmask1 = our_layer_norm_func( x0, x1, res, weight0, bias0, weight1, bias1, dropout_p, epsilon, prenorm=True, residual_in_fp32=residual_in_fp32, return_dropout_mask=True ) assert out0.dtype == input_dtype if not tied_norm: assert out1.dtype == input_dtype print(f'Actual dropout fraction: {1 - dmask0.float().mean().item()}') if has_residual: if has_x1: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + (x1_pt.float() * dmask1.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = ((x0_ref * dmask0.float()) / (1 - dropout_p) + (x1_ref * dmask1.float()) / (1 - dropout_p)) + res_ref else: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + res_pt.float()).to(dtype=residual_dtype) residual_ref = (x0_ref * dmask0.float()) / (1 - dropout_p) + res_ref else: if has_x1: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p) + (x1_pt.float() * dmask1.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = ((x0_ref * dmask0.float()) / (1 - dropout_p) + (x1_ref * dmask1.float()) / (1 - dropout_p)) else: residual_pt = ((x0_pt.float() * dmask0.float()) / (1 - dropout_p)).to(dtype=residual_dtype) residual_ref = (x0_ref * dmask0.float()) / (1 - dropout_p) if not is_rms_norm: out0_pt = F.layer_norm(residual_pt.to(dtype=weight_dtype), (hidden_size,), weight0_pt, bias0_pt, eps=epsilon).to(dtype=input_dtype) out0_ref = F.layer_norm(residual_ref, (hidden_size,), weight0_ref, bias0_ref, eps=epsilon) if not tied_norm: out1_pt = F.layer_norm(residual_pt.to(dtype=weight_dtype), (hidden_size,), weight1_pt, bias1_pt, eps=epsilon).to(dtype=input_dtype) out1_ref = F.layer_norm(residual_ref, (hidden_size,), weight1_ref, bias1_ref, eps=epsilon) else: out0_pt = fused_rms_norm_affine(residual_pt.to(dtype=weight_dtype), weight0_pt, (hidden_size,), eps=epsilon).to(dtype=input_dtype) out0_ref = fused_rms_norm_affine(residual_ref, weight0_ref, (hidden_size,), eps=epsilon) if not tied_norm: out1_pt = fused_rms_norm_affine(residual_pt.to(dtype=weight_dtype), weight1_pt, (hidden_size,), eps=epsilon).to(dtype=input_dtype) out1_ref = fused_rms_norm_affine(residual_ref, weight1_ref, (hidden_size,), eps=epsilon) assert (out0 - out0_ref).abs().max() <= 4 * (out0_pt - out0_ref).abs().max() + 1e-4 if not tied_norm: assert (out1 - out1_ref).abs().max() <= 4 * (out1_pt - out1_ref).abs().max() + 1e-4 assert (residual - residual_ref).abs().max() <= 4 * (residual_pt - residual_ref).abs().max() + 1e-4 g0 = torch.randn_like(out0) / batch_size if tied_norm: (out0 * F.sigmoid(residual)).backward(g0) (out0_pt * F.sigmoid(residual_pt)).backward(g0) (out0_ref * F.sigmoid(residual_ref)).backward(g0) else: g1 = torch.randn_like(out1) / batch_size (out0 * F.sigmoid(residual) * g0 + out1 * g1).sum().backward() (out0_pt * F.sigmoid(residual_pt) * g0 + out1_pt * g1).sum().backward() (out0_ref * F.sigmoid(residual_ref) * g0 + out1_ref * g1).sum().backward() assert (x0.grad - x0_ref.grad).abs().max() <= 4 * (x0_pt.grad - x0_ref.grad).abs().max() + 1e-4 if has_x1: assert (x1.grad - x1_ref.grad).abs().max() <= 4 * (x1_pt.grad - x1_ref.grad).abs().max() + 1e-4 if has_residual: assert (res.grad - res_ref.grad).abs().max() <= 4 * (res_pt.grad - res_ref.grad).abs().max() + 1e-4 assert (weight0.grad - weight0_ref.grad).abs().max() <= 3 * (weight0_pt.grad - weight0_ref.grad).abs().max() + 3e-5 if not is_rms_norm: assert (bias0.grad - bias0_ref.grad).abs().max() <= 2 * (bias0_pt.grad - bias0_ref.grad).abs().max() + 3e-5 if not tied_norm: assert (weight1.grad - weight1_ref.grad).abs().max() <= 3 * (weight1_pt.grad - weight1_ref.grad).abs().max() + 3e-5 if not is_rms_norm: assert (bias1.grad - bias1_ref.grad).abs().max() <= 2 * (bias1_pt.grad - bias1_ref.grad).abs().max() + 3e-5
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/ops/test_dropout_layer_norm.py
import math from functools import partial import torch import torch.nn.functional as F import pytest from einops import rearrange from flash_attn.ops.fused_dense import FusedDense, FusedMLP @pytest.mark.parametrize('dtype', [torch.float16, torch.bfloat16]) @pytest.mark.parametrize('return_residual', [False, True]) @pytest.mark.parametrize('has_bias', [True, False]) @pytest.mark.parametrize('out_features', [1024, 4096]) @pytest.mark.parametrize('in_features', [1024, 4096]) def test_fused_linear_bias(in_features, out_features, has_bias, return_residual, dtype): device = 'cuda' rtol, atol = (3e-3, 1e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x_pt = torch.randn(batch_size, seqlen, in_features, device=device, dtype=dtype, requires_grad=True) x = x_pt.detach().clone().requires_grad_() model_pt = torch.nn.Linear(in_features, out_features, bias=has_bias, device=device, dtype=dtype) model = FusedDense(in_features, out_features, bias=has_bias, return_residual=return_residual, device=device, dtype=dtype) with torch.no_grad(): model.weight.copy_(model_pt.weight) if has_bias: model.bias.copy_(model_pt.bias) out_pt = model_pt(x_pt) if not return_residual: out = model(x) else: out, x_copy = model(x) x_copy = (x_copy[..., :out_features] if out_features < in_features else F.pad(x_copy, (0, out_features - in_features))) x_pt_copy = (x_pt[..., :out_features] if out_features < in_features else F.pad(x_pt, (0, out_features - in_features))) # Just add some random function of the residual out_pt = out_pt + F.gelu(x_pt_copy) out = out + F.gelu(x_copy) # with torch.no_grad(): # out_fl = F.linear(x_pt.float(), model.weight.float(), model.bias.float()).half() assert torch.allclose(out, out_pt, rtol=rtol, atol=atol) # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(out) / 32 out_pt.backward(g) out.backward(g) assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose(model.weight.grad, model_pt.weight.grad, rtol=rtol, atol=atol * 10) if has_bias: assert torch.allclose(model.bias.grad, model_pt.bias.grad, rtol=rtol, atol=atol * 5) @pytest.mark.parametrize('dtype', [torch.float16, torch.bfloat16]) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('heuristic', ['auto', -1]) # @pytest.mark.parametrize('heuristic', ['auto']) @pytest.mark.parametrize('checkpoint_lvl', [0, 1, 2]) # @pytest.mark.parametrize('checkpoint_lvl', [1]) @pytest.mark.parametrize('return_residual', [False, True]) # @pytest.mark.parametrize('return_residual', [False]) @pytest.mark.parametrize('has_bias2', [True, False]) @pytest.mark.parametrize('has_bias1', [True, False]) # @pytest.mark.parametrize('has_bias2', [True]) # @pytest.mark.parametrize('has_bias1', [True]) @pytest.mark.parametrize('activation', ['gelu_approx', 'relu']) # @pytest.mark.parametrize('activation', ['relu']) @pytest.mark.parametrize('out_features', [1024, 4096]) @pytest.mark.parametrize('in_features', [1024, 4096]) # @pytest.mark.parametrize('out_features', [4096]) # @pytest.mark.parametrize('in_features', [1024]) def test_fused_mlp(in_features, out_features, activation, has_bias1, has_bias2, return_residual, checkpoint_lvl, heuristic, dtype): device = 'cuda' rtol, atol = (3e-3, 3e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3) # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 512 x_pt = torch.randn(batch_size, seqlen, in_features, device=device, dtype=dtype, requires_grad=True) x = x_pt.detach().clone().requires_grad_() model_pt_fc1 = torch.nn.Linear(in_features, out_features, bias=has_bias1, device=device, dtype=dtype) model_pt_fc2 = torch.nn.Linear(out_features, in_features, bias=has_bias2, device=device, dtype=dtype) model = FusedMLP(in_features, out_features, in_features, activation=activation, bias1=has_bias1, bias2=has_bias2, return_residual=return_residual, checkpoint_lvl=checkpoint_lvl, heuristic=heuristic, device=device, dtype=dtype) with torch.no_grad(): model.fc1.weight.copy_(model_pt_fc1.weight) if has_bias1: model.fc1.bias.copy_(model_pt_fc1.bias) model.fc2.weight.copy_(model_pt_fc2.weight) if has_bias2: model.fc2.bias.copy_(model_pt_fc2.bias) activation_fn = (partial(F.gelu, approximate='tanh') if activation == 'gelu_approx' else partial(F.relu, inplace=True)) out_pt = model_pt_fc2(activation_fn(model_pt_fc1(x_pt))) if not return_residual: out = model(x) else: out, x_copy = model(x) # Just add some random function of the residual out_pt = out_pt + F.gelu(x_pt) out = out + F.gelu(x_copy) assert torch.allclose(out, out_pt, rtol=rtol, atol=atol) # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(out) / 32 out_pt.backward(g) out.backward(g) # The error for relu is higher still if activation == 'relu': atol = 1e-1 if dtype == torch.bfloat16 else 5e-2 assert torch.allclose(x.grad, x_pt.grad, rtol=rtol, atol=atol) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose(model.fc1.weight.grad, model_pt_fc1.weight.grad, rtol=rtol, atol=atol * 10) if has_bias1: assert torch.allclose(model.fc1.bias.grad, model_pt_fc1.bias.grad, rtol=rtol, atol=atol * 5) assert torch.allclose(model.fc2.weight.grad, model_pt_fc2.weight.grad, rtol=rtol, atol=atol * 10) if has_bias2: assert torch.allclose(model.fc2.bias.grad, model_pt_fc2.bias.grad, rtol=rtol, atol=atol * 5)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/ops/test_fused_dense.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/ops/test_fused_dense_parallel.py import math import torch import torch.nn.functional as F import pytest from apex.transformer import parallel_state from apex.transformer import tensor_parallel from flash_attn.ops.fused_dense import FusedDense, FusedMLP from flash_attn.ops.fused_dense import ColumnParallelLinear, ParallelFusedMLP is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [False]) @pytest.mark.parametrize('has_bias', [True, False]) # @pytest.mark.parametrize('has_bias', [False]) @pytest.mark.parametrize('out_features', [1024]) @pytest.mark.parametrize('in_features', [4096]) def test_fused_linear_bias(in_features, out_features, has_bias, sequence_parallel, world_size, dtype): assert out_features % world_size == 0 rtol, atol = (3e-3, 3e-2) if dtype == torch.bfloat16 else (3e-3, 3e-3) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 2 seqlen = 512 assert batch_size * seqlen % world_size == 0 x_pt = torch.randn(batch_size * seqlen, in_features, device=device, dtype=dtype, requires_grad=True) if sequence_parallel: x = tensor_parallel.scatter_to_sequence_parallel_region(x_pt).detach().clone().requires_grad_() else: x = x_pt.detach().clone().requires_grad_() model_pt = torch.nn.Linear(in_features, out_features, bias=has_bias, device=device, dtype=dtype) partition_out_features = out_features // world_size model = ColumnParallelLinear(in_features, out_features, parallel_state.get_tensor_model_parallel_group(), bias=has_bias, sequence_parallel=sequence_parallel, device=device, dtype=dtype) with torch.no_grad(): model.weight.copy_( model_pt.weight[rank * partition_out_features:(rank + 1) * partition_out_features] ) if has_bias: model.bias.copy_( model_pt.bias[rank * partition_out_features:(rank + 1) * partition_out_features] ) out = model(x) out_pt = model_pt(x_pt) assert torch.allclose( out, out_pt[:, rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol ) # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(out_pt) / 32 out_pt.backward(g) out.backward(g[:, rank * partition_out_features:(rank + 1) * partition_out_features]) parallel_state.destroy_model_parallel() partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( x.grad, x_pt.grad[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else x_pt.grad, rtol=rtol, atol=atol ) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose( model.weight.grad, model_pt.weight.grad[rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol * 10 ) if has_bias: assert torch.allclose( model.bias.grad, model_pt.bias.grad[rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol * 5 ) @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [False]) @pytest.mark.parametrize('has_bias2', [True, False]) # @pytest.mark.parametrize('has_bias2', [True]) @pytest.mark.parametrize('out_features', [4096]) @pytest.mark.parametrize('in_features', [1024]) def test_fused_mlp(in_features, out_features, has_bias2, sequence_parallel, world_size, dtype): assert out_features % world_size == 0 rtol, atol = (3e-3, 3e-2) if dtype == torch.bfloat16 else (3e-3, 3e-3) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 2 seqlen = 512 assert batch_size * seqlen % world_size == 0 x_pt = torch.randn(batch_size * seqlen, in_features, device=device, dtype=dtype, requires_grad=True) # We need to generate g here so that all processes get the same gradient, # as rank 0 will have an extra bias that changes the RNG. # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(x_pt) / 32 if sequence_parallel: x = tensor_parallel.scatter_to_sequence_parallel_region(x_pt).detach().clone().requires_grad_() else: x = x_pt.detach().clone().requires_grad_() model_pt_fc1 = torch.nn.Linear(in_features, out_features, device=device, dtype=dtype) model_pt_fc2 = torch.nn.Linear(out_features, in_features, bias=has_bias2, device=device, dtype=dtype) partition_out_features = out_features // world_size partition_in_features = in_features // world_size model = ParallelFusedMLP(in_features, out_features, in_features, process_group=parallel_state.get_tensor_model_parallel_group(), bias2=has_bias2 and rank == 0, sequence_parallel=sequence_parallel, device=device, dtype=dtype) with torch.no_grad(): model.fc1.weight.copy_( model_pt_fc1.weight[rank * partition_out_features:(rank + 1) * partition_out_features] ) model.fc1.bias.copy_( model_pt_fc1.bias[rank * partition_out_features:(rank + 1) * partition_out_features] ) model.fc2.weight.copy_( model_pt_fc2.weight[:, rank * partition_out_features:(rank + 1) * partition_out_features] ) if has_bias2 and rank == 0: model.fc2.bias.copy_(model_pt_fc2.bias) out = model(x) out_pt = model_pt_fc2(F.gelu(model_pt_fc1(x_pt), approximate='tanh')) partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( out, out_pt[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else out_pt, rtol=rtol, atol=atol ) out_pt.backward(g) out.backward(g[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else g) parallel_state.destroy_model_parallel() assert torch.allclose( x.grad, x_pt.grad[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else x_pt.grad, rtol=rtol, atol=atol ) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose( model.fc1.weight.grad, model_pt_fc1.weight.grad[rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.fc1.bias.grad, model_pt_fc1.bias.grad[rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol * 5 ) assert torch.allclose( model.fc2.weight.grad, model_pt_fc2.weight.grad[:, rank * partition_out_features:(rank + 1) * partition_out_features], rtol=rtol, atol=atol * 10 ) if has_bias2 and rank == 0: assert torch.allclose(model.fc2.bias.grad, model_pt_fc2.bias.grad, rtol=rtol, atol=atol * 5)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/ops/test_fused_dense_parallel.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/modules/test_embedding_parallel.py import torch import torch.nn as nn import torch.nn.functional as F import pytest from einops import rearrange from apex.transformer import parallel_state from flash_attn.modules.embedding import GPT2Embeddings, ParallelGPT2Embeddings is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.bfloat16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [False]) @pytest.mark.parametrize('has_pos_emb', [True, False]) # @pytest.mark.parametrize('has_pos_emb', [True]) @pytest.mark.parametrize('dim', [1024]) def test_embedding_parallel(dim, has_pos_emb, sequence_parallel, world_size, dtype): vocab_size = 50264 seqlen = 2048 assert vocab_size % world_size == 0 assert dim % world_size == 0 rtol, atol = (3e-3, 5e-2) if dtype == torch.bfloat16 else (3e-3, 3e-3) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 8 seqlen = 1024 assert (batch_size * seqlen) % world_size == 0 input_ids_pt = torch.randint(0, vocab_size, (batch_size, seqlen), device=device) input_ids = input_ids_pt.detach().clone() model_pt = GPT2Embeddings(dim, vocab_size, seqlen if has_pos_emb else 0, device=device, dtype=dtype) model = ParallelGPT2Embeddings(dim, vocab_size, seqlen if has_pos_emb else 0, parallel_state.get_tensor_model_parallel_group(), sequence_parallel=sequence_parallel, device=device, dtype=dtype) partition_vocab_size = vocab_size // world_size partition_dim = dim // world_size with torch.no_grad(): model.word_embeddings.weight.copy_( model_pt.word_embeddings.weight[rank * partition_vocab_size:(rank + 1) * partition_vocab_size] ) if has_pos_emb: model.position_embeddings.weight.copy_( model_pt.position_embeddings.weight[:, rank * partition_dim:(rank + 1) * partition_dim] ) out = model(input_ids, combine_batch_seqlen_dim=True) out_pt = rearrange(model_pt(input_ids), 'b s d -> (b s) d') partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( out, out_pt[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else out_pt, rtol=rtol, atol=atol ) g = torch.randn_like(out_pt) out_pt.backward(g) out.backward(g[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else g) parallel_state.destroy_model_parallel() assert torch.allclose( model.word_embeddings.weight.grad, model_pt.word_embeddings.weight.grad[rank * partition_vocab_size:(rank + 1) * partition_vocab_size], rtol=rtol, atol=atol ) if has_pos_emb: assert torch.allclose( model.position_embeddings.weight.grad, model_pt.position_embeddings.weight.grad[:, rank * partition_dim:(rank + 1) * partition_dim], rtol=rtol, atol=atol )
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/modules/test_embedding_parallel.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/modules/test_block_parallel.py import math from functools import partial import torch import torch.nn as nn import torch.nn.functional as F import pytest from einops import rearrange from apex.transformer import parallel_state from apex.transformer import tensor_parallel from flash_attn.modules.mha import MHA, ParallelMHA from flash_attn.modules.mlp import FusedMLP, ParallelFusedMLP from flash_attn.modules.block import Block from flash_attn.utils.distributed import allreduce_sequence_parallel_grad is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [True]) @pytest.mark.parametrize('dim', [1024]) def test_block_parallel(dim, sequence_parallel, world_size, dtype): head_dim = 64 assert dim % head_dim == 0 num_heads = dim // head_dim assert num_heads % world_size == 0 rtol, atol = (3e-3, 5e-2) if dtype == torch.bfloat16 else (3e-3, 3e-3) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 2 seqlen = 1024 assert (batch_size * seqlen) % world_size == 0 x_pt = torch.randn(batch_size * seqlen, dim, device=device, dtype=dtype, requires_grad=True) residual_pt = torch.randn(batch_size * seqlen, dim, device=device, requires_grad=True) # We need to generate g here so that all processes get the same gradient, # as rank 0 will have an extra bias that changes the RNG. # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(x_pt) / 32 if sequence_parallel: x = tensor_parallel.scatter_to_sequence_parallel_region(x_pt).detach().clone().requires_grad_() residual = tensor_parallel.scatter_to_sequence_parallel_region(residual_pt).detach().clone().requires_grad_() else: x = x_pt.detach().clone().requires_grad_() residual = residual_pt.detach().clone().requires_grad_() mixer_cls_pt = partial(MHA, num_heads=num_heads, rotary_emb_dim=int(head_dim // 2), use_flash_attn=True, device=device, dtype=dtype) mlp_cls_pt = partial(FusedMLP, hidden_features=4 * dim, device=device, dtype=dtype) norm_cls = partial(nn.LayerNorm, device=device, dtype=dtype) model_pt = Block(dim, mixer_cls_pt, mlp_cls_pt, norm_cls, fused_dropout_add_ln=True) with torch.no_grad(): nn.init.normal_(model_pt.norm1.weight) nn.init.normal_(model_pt.norm1.bias) nn.init.normal_(model_pt.norm2.weight) nn.init.normal_(model_pt.norm2.bias) mixer_cls = partial(ParallelMHA, num_heads=num_heads, process_group=parallel_state.get_tensor_model_parallel_group(), rotary_emb_dim=int(head_dim // 2), use_flash_attn=True, sequence_parallel=sequence_parallel, device=device, dtype=dtype) mlp_cls = partial(ParallelFusedMLP, hidden_features=4 * dim, process_group=parallel_state.get_tensor_model_parallel_group(), sequence_parallel=sequence_parallel, device=device, dtype=dtype) model = Block(dim, mixer_cls, mlp_cls, norm_cls, fused_dropout_add_ln=True, sequence_parallel=sequence_parallel, mark_shared_params=True) partition_dim = dim // world_size partition_hidden_dim = 4 * dim // world_size with torch.no_grad(): model.mixer.Wqkv.weight.copy_( rearrange(rearrange(model_pt.mixer.Wqkv.weight, '(three o) i -> three o i', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o i -> (three o) i') ) model.mixer.Wqkv.bias.copy_( rearrange(rearrange(model_pt.mixer.Wqkv.bias, '(three o) -> three o', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o -> (three o)') ) model.mixer.out_proj.weight.copy_( model_pt.mixer.out_proj.weight[:, rank * partition_dim:(rank + 1) * partition_dim] ) if rank == 0: model.mixer.out_proj.bias.copy_(model_pt.mixer.out_proj.bias) model.mlp.fc1.weight.copy_( model_pt.mlp.fc1.weight[rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim] ) model.mlp.fc1.bias.copy_( model_pt.mlp.fc1.bias[rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim] ) model.mlp.fc2.weight.copy_( model_pt.mlp.fc2.weight[:, rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim] ) if rank == 0: model.mlp.fc2.bias.copy_(model_pt.mlp.fc2.bias) model.norm1.weight.copy_(model_pt.norm1.weight) model.norm1.bias.copy_(model_pt.norm1.bias) model.norm2.weight.copy_(model_pt.norm2.weight) model.norm2.bias.copy_(model_pt.norm2.bias) mixer_kwargs = {'seqlen': seqlen} out, out_residual = model(x, residual, mixer_kwargs=mixer_kwargs) out_pt, out_residual_pt = model_pt(rearrange(x_pt, '(b s) d -> b s d', s=seqlen), rearrange(residual_pt, '(b s) d -> b s d', s=seqlen)) out_pt, out_residual_pt = [rearrange(x, 'b s d -> (b s) d') for x in [out_pt, out_residual_pt]] partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( out, out_pt[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else out_pt, rtol=rtol, atol=atol ) assert torch.allclose( out_residual, out_residual_pt[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else out_residual_pt, rtol=rtol, atol=atol ) (out_pt + 2 * out_residual_pt).backward(g) (out + 2 * out_residual).backward(g[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else g) allreduce_sequence_parallel_grad(model, parallel_state.get_tensor_model_parallel_group()) parallel_state.destroy_model_parallel() assert torch.allclose( x.grad, x_pt.grad[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else x_pt.grad, rtol=rtol, atol=atol / 10 # magnitude of x.grad is quite small ) assert torch.allclose( residual.grad, residual_pt.grad[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else residual_pt.grad, rtol=rtol, atol=atol ) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose( model.mixer.Wqkv.weight.grad, rearrange(rearrange(model_pt.mixer.Wqkv.weight.grad, '(three o) i -> three o i', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o i -> (three o) i'), rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.mixer.Wqkv.bias.grad, rearrange(rearrange(model_pt.mixer.Wqkv.bias.grad, '(three o) -> three o', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o -> (three o)'), rtol=rtol, atol=atol * 5 ) assert torch.allclose( model.mixer.out_proj.weight.grad, model_pt.mixer.out_proj.weight.grad[:, rank * partition_dim:(rank + 1) * partition_dim], rtol=rtol, atol=atol * 10 ) if rank == 0: assert torch.allclose(model.mixer.out_proj.bias.grad, model_pt.mixer.out_proj.bias.grad, rtol=rtol, atol=atol * 5) assert torch.allclose( model.mlp.fc1.weight.grad, model_pt.mlp.fc1.weight.grad[rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim], rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.mlp.fc1.bias.grad, model_pt.mlp.fc1.bias.grad[rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim], rtol=rtol, atol=atol * 5 ) assert torch.allclose( model.mlp.fc2.weight.grad, model_pt.mlp.fc2.weight.grad[:, rank * partition_hidden_dim:(rank + 1) * partition_hidden_dim], rtol=rtol, atol=atol * 10 ) if rank == 0: assert torch.allclose(model.mlp.fc2.bias.grad, model_pt.mlp.fc2.bias.grad, rtol=rtol, atol=atol * 5) assert torch.allclose(model.norm1.weight.grad, model_pt.norm1.weight.grad, rtol=rtol, atol=atol * 5) assert torch.allclose(model.norm1.bias.grad, model_pt.norm1.bias.grad, rtol=rtol, atol=atol * 5) assert torch.allclose(model.norm2.weight.grad, model_pt.norm2.weight.grad, rtol=rtol, atol=atol * 5) assert torch.allclose(model.norm2.bias.grad, model_pt.norm2.bias.grad, rtol=rtol, atol=atol * 5)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/modules/test_block_parallel.py
# Run test with: # torchrun --no_python --nproc_per_node=8 pytest -q -s tests/modules/test_mha_parallel.py import math import torch import torch.nn.functional as F import pytest from einops import rearrange from apex.transformer import parallel_state from apex.transformer import tensor_parallel from flash_attn.modules.mha import MHA, ParallelMHA is_sm8x = torch.cuda.get_device_capability('cuda')[0] >= 8 @pytest.mark.parametrize('dtype', [torch.float16] + ([torch.bfloat16] if is_sm8x else [])) # @pytest.mark.parametrize('dtype', [torch.float16]) @pytest.mark.parametrize('world_size', [1, 2, 4, 8]) # @pytest.mark.parametrize('world_size', [2]) @pytest.mark.parametrize('sequence_parallel', [True, False]) # @pytest.mark.parametrize('sequence_parallel', [False]) @pytest.mark.parametrize('head_dim', [64, 128]) # @pytest.mark.parametrize('head_dim', [64]) @pytest.mark.parametrize('embed_dim', [1024, 4096]) # @pytest.mark.parametrize('embed_dim', [1024]) def test_mha_parallel(embed_dim, head_dim, sequence_parallel, world_size, dtype): assert embed_dim % head_dim == 0 num_heads = embed_dim // head_dim assert num_heads % world_size == 0 rtol, atol = (3e-3, 1e-2) if dtype == torch.bfloat16 else (3e-3, 1e-3) if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend='nccl', init_method='env://') device = f'cuda:{torch.distributed.get_rank()}' assert world_size <= torch.distributed.get_world_size() parallel_state.initialize_model_parallel(tensor_model_parallel_size_=world_size) rank = parallel_state.get_tensor_model_parallel_rank() # set seed torch.random.manual_seed(0) batch_size = 2 seqlen = 1024 assert (batch_size * seqlen) % world_size == 0 x_pt = torch.randn(batch_size * seqlen, embed_dim, device=device, dtype=dtype, requires_grad=True) # We need to generate g here so that all processes get the same gradient, # as rank 0 will have an extra bias that changes the RNG. # If we don't divide by batch_size, the gradient gets a bit too large. g = torch.randn_like(x_pt) / 32 if sequence_parallel: x = tensor_parallel.scatter_to_sequence_parallel_region(x_pt).detach().clone().requires_grad_() else: x = x_pt.detach().clone().requires_grad_() model_pt = MHA(embed_dim, num_heads, rotary_emb_dim=int(head_dim // 2), use_flash_attn=True, device=device, dtype=dtype) partition_dim = embed_dim // world_size model = ParallelMHA(embed_dim, num_heads, parallel_state.get_tensor_model_parallel_group(), rotary_emb_dim=int(head_dim // 2), use_flash_attn=True, sequence_parallel=sequence_parallel, device=device, dtype=dtype) with torch.no_grad(): model.Wqkv.weight.copy_( rearrange(rearrange(model_pt.Wqkv.weight, '(three o) i -> three o i', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o i -> (three o) i') ) model.Wqkv.bias.copy_( rearrange(rearrange(model_pt.Wqkv.bias, '(three o) -> three o', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o -> (three o)') ) model.out_proj.weight.copy_( model_pt.out_proj.weight[:, rank * partition_dim:(rank + 1) * partition_dim] ) if rank == 0: model.out_proj.bias.copy_(model_pt.out_proj.bias) out = model(x, seqlen=seqlen) out_pt = rearrange(model_pt(rearrange(x_pt, '(b s) d -> b s d', s=seqlen)), 'b s d -> (b s) d') partition_batch_dim = batch_size * seqlen // world_size assert torch.allclose( out, out_pt[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else out_pt, rtol=rtol, atol=atol ) out_pt.backward(g) out.backward(g[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else g) parallel_state.destroy_model_parallel() assert torch.allclose( x.grad, x_pt.grad[rank * partition_batch_dim:(rank + 1) * partition_batch_dim] if sequence_parallel else x_pt.grad, rtol=rtol, atol=atol / 100 # magnitude of x.grad is quite small ) # The error for d_weight and d_bias is quite a bit higher assert torch.allclose( model.Wqkv.weight.grad, rearrange(rearrange(model_pt.Wqkv.weight.grad, '(three o) i -> three o i', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o i -> (three o) i'), rtol=rtol, atol=atol * 10 ) assert torch.allclose( model.Wqkv.bias.grad, rearrange(rearrange(model_pt.Wqkv.bias.grad, '(three o) -> three o', three=3)[:, rank * partition_dim:(rank + 1) * partition_dim], 'three o -> (three o)'), rtol=rtol, atol=atol * 5 ) assert torch.allclose( model.out_proj.weight.grad, model_pt.out_proj.weight.grad[:, rank * partition_dim:(rank + 1) * partition_dim], rtol=rtol, atol=atol * 10 ) if rank == 0: assert torch.allclose(model.out_proj.bias.grad, model_pt.out_proj.bias.grad, rtol=rtol, atol=atol * 5)
EXA-1-master
exa/modular_components/attentions/flash-attention/tests/modules/test_mha_parallel.py
from functools import partial import math import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange, repeat from flash_attn.utils.benchmark import benchmark_forward, benchmark_all, pytorch_profiler from flash_attn.flash_attn_interface import flash_attn_unpadded_qkvpacked_func # from flash_attn.triton.fused_attention import attention as attention from flash_attn.flash_attn_triton import flash_attn_qkvpacked_func from flash_attn.flash_attn_triton_og import attention as attention_og try: from flash_attn.fused_softmax import scaled_upper_triang_masked_softmax except ImportError: scaled_upper_triang_masked_softmax = None def attention_pytorch(qkv, dropout_p=0.0, causal=True): """ Arguments: qkv: (batch_size, seqlen, 3, nheads, head_dim) dropout_p: float Output: output: (batch_size, seqlen, nheads, head_dim) """ batch_size, seqlen, _, nheads, d = qkv.shape q, k, v = qkv.unbind(dim=2) q = rearrange(q, 'b t h d -> (b h) t d') k = rearrange(k, 'b s h d -> (b h) d s') softmax_scale = 1.0 / math.sqrt(d) # Preallocate attn_weights for `baddbmm` scores = torch.empty(batch_size * nheads, seqlen, seqlen, dtype=qkv.dtype, device=qkv.device) scores = rearrange(torch.baddbmm(scores, q, k, beta=0, alpha=softmax_scale), '(b h) t s -> b h t s', h=nheads) if causal: # "triu_tril_cuda_template" not implemented for 'BFloat16' # So we have to construct the mask in float causal_mask = torch.triu(torch.full((seqlen, seqlen), -10000.0, device=scores.device), 1) # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess) scores = scores + causal_mask.to(dtype=scores.dtype) attention = torch.softmax(scores, dim=-1) attention_drop = F.dropout(attention, dropout_p) output = torch.einsum('bhts,bshd->bthd', attention_drop , v) return output.to(dtype=qkv.dtype) def attention_megatron(qkv): """ Arguments: qkv: (batch_size, seqlen, 3, nheads, head_dim) Output: output: (batch_size, seqlen, nheads, head_dim) """ batch_size, seqlen, _, nheads, d = qkv.shape q, k, v = qkv.unbind(dim=2) q = rearrange(q, 'b t h d -> (b h) t d') k = rearrange(k, 'b s h d -> (b h) d s') softmax_scale = 1.0 / math.sqrt(d) # Preallocate attn_weights for `baddbmm` scores = torch.empty(batch_size * nheads, seqlen, seqlen, dtype=qkv.dtype, device=qkv.device) scores = rearrange(torch.baddbmm(scores, q, k, beta=0, alpha=softmax_scale), '(b h) t s -> b h t s', h=nheads) attention = scaled_upper_triang_masked_softmax(scores, None, scale=1.0) output = torch.einsum('bhts,bshd->bthd', attention, v) return output.to(dtype=qkv.dtype) torch.manual_seed(0) repeats = 30 batch_size = 2 seqlen = 4096 nheads = 12 headdim = 128 # batch_size = 64 # seqlen = 512 # nheads = 8 # headdim = 128 dropout_p = 0.0 causal = True dtype = torch.bfloat16 device = 'cuda' qkv = torch.randn(batch_size, seqlen, 3, nheads, headdim, device=device, dtype=dtype, requires_grad=True) cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=qkv.device) benchmark_all(flash_attn_unpadded_qkvpacked_func, rearrange(qkv, 'b s ... -> (b s) ...'), cu_seqlens, seqlen, dropout_p, causal=causal, repeats=repeats, desc='FlashAttention') benchmark_all(attention_pytorch, qkv, dropout_p, causal=causal, repeats=repeats, desc='PyTorch Attention') benchmark_all(flash_attn_qkvpacked_func, qkv, None, causal, repeats=repeats, desc='FlashAttention Triton') pytorch_profiler(flash_attn_qkvpacked_func, qkv, None, causal, backward=True) q, k, v = [torch.randn(batch_size, nheads, seqlen, headdim, device=device, dtype=dtype, requires_grad=True) for _ in range(3)] benchmark_all(attention_og, q, k, v, 1.0, repeats=repeats, desc='FlashAttention Triton OG') # pytorch_profiler(attention, q, k, v, 1.0, backward=True) if scaled_upper_triang_masked_softmax is not None: benchmark_all(attention_megatron, qkv, repeats=repeats, desc='Megatron Attention')
EXA-1-master
exa/modular_components/attentions/flash-attention/benchmarks/benchmark_causal.py
from functools import partial import math import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange, repeat from flash_attn.utils.benchmark import benchmark_all, benchmark_forward, benchmark_backward, benchmark_combined from flash_attn.bert_padding import unpad_input, pad_input from flash_attn.flash_attn_interface import flash_attn_unpadded_qkvpacked_func def attention_ref(qkv, attn_mask, dropout_p, upcast=False, causal=False): """ Arguments: qkv: (batch_size, seqlen, 3, nheads, head_dim) attn_mask: (batch_size, seqlen) dropout_p: float Output: output: (batch_size, seqlen, nheads, head_dim) attention: softmax after dropout """ q, k, v = (qkv.float() if upcast else qkv).unbind(dim=2) seqlen = qkv.shape[1] d = qkv.shape[-1] scores = torch.einsum('bthd,bshd->bhts', q, k / math.sqrt(d)) scores.masked_fill_(rearrange(~attn_mask, 'b s -> b 1 1 s'), float('-inf')) if causal: causal_mask = torch.triu(torch.ones(seqlen, seqlen, dtype=torch.bool, device=qkv.device), 1) scores.masked_fill_(causal_mask, float('-inf')) attention = torch.softmax(scores, dim=-1) attention_drop = F.dropout(attention, dropout_p) output = torch.einsum('bhts,bshd->bthd', attention_drop , v) # return output.to(dtype=qkv.dtype), attention.to(dtype=qkv.dtype) return output.to(dtype=qkv.dtype) torch.manual_seed(0) repeats = 30 batch_size = 64 nheads = 16 seqlen = 1024 n = 1024 d = n // nheads dropout_p = 0.1 causal = False dtype = torch.float16 device = 'cuda' x = torch.randn(batch_size, seqlen, n, device='cuda', dtype=dtype, requires_grad=True) Wqkv = torch.nn.Linear(nheads * d, 3 * nheads * d, device=device, dtype=dtype) lengths = torch.randint(seqlen - 20, seqlen, (batch_size, 1), device='cuda') attention_mask_bool = repeat(torch.arange(seqlen, device='cuda'), 's -> b s', b=batch_size) < lengths attention_mask = torch.zeros(batch_size, seqlen, device='cuda', dtype=dtype) attention_mask[~attention_mask_bool] = -10000.0 attention_mask = rearrange(attention_mask, 'b s -> b 1 1 s') x_unpad, indices, cu_seqlens, max_seqlen_in_batch = unpad_input(x, attention_mask_bool) qkv_unpad = rearrange(Wqkv(x_unpad), 'nnz (t h d) -> nnz t h d', t=3, h=nheads).detach().requires_grad_() qkv = rearrange(Wqkv(x), 'b s (t h d) -> b s t h d', t=3, h=nheads).detach().requires_grad_() fn = lambda qkv_unpad: flash_attn_unpadded_qkvpacked_func( qkv_unpad, cu_seqlens, max_seqlen_in_batch, dropout_p, causal=causal ) benchmark_all(fn, qkv_unpad, repeats=repeats, desc='FlashAttention') fn = lambda qkv: attention_ref(qkv, attention_mask_bool, dropout_p, causal=causal) benchmark_all(fn, qkv, repeats=repeats, desc='PyTorch Standard Attention')
EXA-1-master
exa/modular_components/attentions/flash-attention/benchmarks/benchmark_flash_attention.py
# [2022-10-23] Copied from https://github.com/NVIDIA/apex/blob/master/apex/transformer/functional/fused_softmax.py # for benchmarking. # We added support for seqlen=2k and seqlen=4k # coding=utf-8 # Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from apex._autocast_utils import _cast_if_autocast_enabled from apex.transformer.enums import AttnMaskType from fused_softmax_lib import scaled_masked_softmax_forward, scaled_masked_softmax_backward from fused_softmax_lib import scaled_masked_softmax_get_batch_per_block from fused_softmax_lib import scaled_upper_triang_masked_softmax_forward, scaled_upper_triang_masked_softmax_backward class ScaledUpperTriangMaskedSoftmax(torch.autograd.Function): """ Fused operation which performs following three operations in sequence 1. Scale the tensor. 2. Apply upper triangular mask (typically used in gpt models). 3. Perform softmax. """ @staticmethod def forward(ctx, inputs, scale): scale_t = torch.tensor([scale]) softmax_results = scaled_upper_triang_masked_softmax_forward( inputs, scale_t[0] ) ctx.save_for_backward(softmax_results, scale_t) return softmax_results @staticmethod def backward(ctx, output_grads): softmax_results, scale_t = ctx.saved_tensors input_grads = scaled_upper_triang_masked_softmax_backward( output_grads, softmax_results, scale_t[0] ) return input_grads, None def scaled_upper_triang_masked_softmax(inputs, _, scale): b, np, sq, sk = inputs.size() assert sq == sk, "causal mask is only for self attention" # Reshaping input to 3D tensor (attn_batches, sq, sk) inputs = inputs.view(-1, sq, sk) args = _cast_if_autocast_enabled(inputs, scale) with torch.cuda.amp.autocast(enabled=False): probs = ScaledUpperTriangMaskedSoftmax.apply(*args) return probs.view(b, np, sq, sk) # NOTE (mkozuki): `ScaledMaskedSoftmax` somehow doesn't work well with `torch.cuda.amp.custom_fwd`. # Without `cast_inputs` kwarg, somehow inputs are not cast to dtype used in the autocast context. # So I needed to manually write two `torch.autograd.Function` inheritances. # Fused operation which performs following three operations in sequence # 1. Scale the tensor. # 2. Apply the mask. # 3. Perform softmax. class ScaledMaskedSoftmax(torch.autograd.Function): @staticmethod def forward(ctx, inputs, mask, scale): scale_t = torch.tensor([scale]) softmax_results = scaled_masked_softmax_forward(inputs, mask, scale_t[0]) ctx.save_for_backward(softmax_results, scale_t) return softmax_results @staticmethod def backward(ctx, output_grads): softmax_results, scale_t = ctx.saved_tensors input_grads = scaled_masked_softmax_backward( output_grads, softmax_results, scale_t[0] ) return input_grads, None, None def scaled_masked_softmax(inputs, mask, scale): # input is 4D tensor (b, np, sq, sk) args = _cast_if_autocast_enabled(inputs, mask, scale) with torch.cuda.amp.autocast(enabled=False): return ScaledMaskedSoftmax.apply(*args) class FusedScaleMaskSoftmax(torch.nn.Module): """ fused operation: scaling + mask + softmax Arguments: input_in_fp16: flag to indicate if input in fp16 data format. input_in_bf16: flag to indicate if input in bf16 data format. attn_mask_type: attention mask type (pad or causal) scaled_masked_softmax_fusion: flag to indicate user want to use softmax fusion mask_func: mask function to be applied. softmax_in_fp32: if true, softmax in performed at fp32 precision. scale: scaling factor used in input tensor scaling. """ def __init__( self, input_in_fp16, input_in_bf16, attn_mask_type, scaled_masked_softmax_fusion, mask_func, softmax_in_fp32, scale, ): super().__init__() self.input_in_fp16 = input_in_fp16 self.input_in_bf16 = input_in_bf16 if self.input_in_fp16 and self.input_in_bf16: raise RuntimeError( "both fp16 and bf16 flags cannot be active at the same time." ) self.input_in_float16 = self.input_in_fp16 or self.input_in_bf16 self.attn_mask_type = attn_mask_type self.scaled_masked_softmax_fusion = scaled_masked_softmax_fusion self.mask_func = mask_func self.softmax_in_fp32 = softmax_in_fp32 self.scale = scale if not (self.scale is None or softmax_in_fp32): raise RuntimeError("softmax should be in fp32 when scaled") if self.scaled_masked_softmax_fusion: if self.attn_mask_type == AttnMaskType.causal: self.fused_softmax_func = scaled_upper_triang_masked_softmax elif self.attn_mask_type == AttnMaskType.padding: self.fused_softmax_func = scaled_masked_softmax else: raise ValueError("Invalid attn_mask_type.") def forward(self, input, mask): # [b, np, sq, sk] assert input.dim() == 4 if self.is_kernel_available(mask, *input.size()): return self.forward_fused_softmax(input, mask) else: return self.forward_torch_softmax(input, mask) def is_kernel_available(self, mask, b, np, sq, sk): attn_batches = b * np if ( self.scaled_masked_softmax_fusion # user want to fuse and self.input_in_float16 # input must be fp16 and ( self.attn_mask_type == AttnMaskType.causal or (self.attn_mask_type == AttnMaskType.padding and mask is not None) ) and 16 < sk <= 8192 # sk must be 16 ~ 8192 and sq % 4 == 0 # sq must be divisor of 4 and sk % 4 == 0 # sk must be divisor of 4 and attn_batches % 4 == 0 # np * b must be divisor of 4 ): if 0 <= sk <= 8192: batch_per_block = self.get_batch_per_block(sq, sk, b, np) if self.attn_mask_type == AttnMaskType.causal: if attn_batches % batch_per_block == 0: return True else: if sq % batch_per_block == 0: return True return False def forward_fused_softmax(self, input, mask): # input.shape = [b, np, sq, sk] scale = self.scale if self.scale is not None else 1.0 return self.fused_softmax_func(input, mask, scale) def forward_torch_softmax(self, input, mask): if self.input_in_float16 and self.softmax_in_fp32: input = input.float() if self.scale is not None: input = input * self.scale mask_output = self.mask_func(input, mask) if mask is not None else input probs = torch.nn.Softmax(dim=-1)(mask_output) if self.input_in_float16 and self.softmax_in_fp32: if self.input_in_fp16: probs = probs.half() else: probs = probs.bfloat16() return probs @staticmethod def get_batch_per_block(sq, sk, b, np): return scaled_masked_softmax_get_batch_per_block(sq, sk, b, np)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/fused_softmax.py
# Adapted from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/fmha.py import torch import torch.nn as nn import flash_attn_cuda def convert_blockmask(blockmask, causal): """Convert from the 0-1 format to the format used by the CUDA code. 0 means the block is skipped. nonzero means the block is not skipped. Argument: blockmask: (row, col): a 0-1 tensor Return: blockmask_converted: (col, row), dtype torch.int32: for each column, it contains the row indices of the nonzero blocks, padded with -1 to reach length @row. The indices are multiplied by 4, with the smallest bit used to encode whether it is the first nonzero in its row, and the 2nd smallest bit to encode whether it is the last nonzero in its row.. """ assert not causal # TD [2022-05-13]: The indexing and sorting is very tricky nrow, ncol = blockmask.shape # Sort does not support bool on CUDA blockmask = blockmask.to(dtype=torch.uint8) nonzero_val, nonzero_sorted_rowidx = blockmask.sort(dim=0, stable=True, descending=True) nonzero_unsorted_rowidx = nonzero_sorted_rowidx.argsort(dim=0) last_nonzero_col_per_row = blockmask.sort(dim=-1, stable=True).indices[:, -1] last_nonzero_col_per_row_after_sort = nonzero_unsorted_rowidx[ torch.arange(nrow, device=blockmask.device), last_nonzero_col_per_row ] first_nonzero_col_per_row = blockmask.sort(dim=-1, stable=True, descending=True).indices[:, 0] first_nonzero_col_per_row_after_sort = nonzero_unsorted_rowidx[ torch.arange(nrow, device=blockmask.device), first_nonzero_col_per_row ] nonzero_idx = nonzero_sorted_rowidx * 4 nonzero_idx[last_nonzero_col_per_row_after_sort, last_nonzero_col_per_row] += 2 nonzero_idx[first_nonzero_col_per_row_after_sort, first_nonzero_col_per_row] += 1 nonzero_idx[nonzero_val == 0] = -1 return nonzero_idx.T.contiguous().to(dtype=torch.int32) def _flash_blocksparse_attn_forward(qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal, return_softmax): context, softmax_lse, *rest = flash_attn_cuda.fwd_block(qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal, return_softmax, None) # if context.isnan().any() or softmax_lse.isnan().any(): # breakpoint() S_dmask = rest[0] if return_softmax else None return context, softmax_lse, S_dmask def _flash_blocksparse_attn_backward(dout, qkv, out, S_dmask, softmax_lse, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal): dqkv, dp, softmax_d = flash_attn_cuda.bwd_block(dout, qkv, out, S_dmask, softmax_lse, cu_seqlens, blockmask, dropout_p, softmax_scale, max_s, causal, None) # if dqkv.isnan().any() or softmax_d.isnan().any(): # breakpoint() return dqkv class FlashBlocksparseAttnFun(torch.autograd.Function): @staticmethod def forward(ctx, qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal): # Save rng_state because the backward pass will regenerate the dropout mask rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None if softmax_scale is None: softmax_scale = qkv.shape[-1] ** (-0.5) context, softmax_lse, S_dmask = _flash_blocksparse_attn_forward( qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal=causal, return_softmax=False ) ctx.save_for_backward(qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state) ctx.dropout_p = dropout_p ctx.max_s = max_s ctx.softmax_scale = softmax_scale ctx.causal = causal return context @staticmethod def backward(ctx, dout): qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state = ctx.saved_tensors if rng_state is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state) # S_dmask is None, temporarily use another tensor just to get it running dqkv = _flash_blocksparse_attn_backward( dout, qkv, context, context, softmax_lse, cu_seqlens, blockmask, ctx.dropout_p, ctx.max_s, ctx.softmax_scale, ctx.causal ) if rng_state is not None: torch.cuda.set_rng_state(cur_rng_state) return dqkv, None, None, None, None, None, None, None # We duplicate code to return both the output and the softmax for testing # Returning both makes backward a bit slower, so we want to keep using the other version for speed. class FlashBlocksparseAttnFunWithS(torch.autograd.Function): @staticmethod def forward(ctx, qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal): # Save rng_state because the backward pass is gonna regenerate the dropout mask rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None if softmax_scale is None: softmax_scale = qkv.shape[-1] ** (-0.5) context, softmax_lse, S_dmask = _flash_blocksparse_attn_forward( qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal=causal, return_softmax=True ) ctx.save_for_backward(qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state) ctx.dropout_p = dropout_p ctx.max_s = max_s ctx.softmax_scale = softmax_scale ctx.causal = causal return context, S_dmask, softmax_lse @staticmethod def backward(ctx, dout, _dS_dmask_ignored, _dsoftmax_sum_ignored): qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, rng_state = ctx.saved_tensors if rng_state is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state) dqkv = _flash_blocksparse_attn_backward( dout, qkv, context, S_dmask, softmax_lse, cu_seqlens, blockmask, ctx.dropout_p, ctx.max_s, ctx.softmax_scale, ctx.causal ) if rng_state is not None: torch.cuda.set_rng_state(cur_rng_state) return dqkv, None, None, None, None, None, None def flash_blocksparse_attn_func(qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale=None, causal=False, return_attn_probs=False, convert_mask=True): """dropout_p should be set to 0.0 during evaluation """ func = FlashBlocksparseAttnFun if not return_attn_probs else FlashBlocksparseAttnFunWithS if convert_mask: blockmask = convert_blockmask(blockmask, causal=causal) return func.apply(qkv, cu_seqlens, blockmask, dropout_p, max_s, softmax_scale, causal)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_blocksparse_attn_interface.py
import math import torch import torch.nn as nn from einops import rearrange import hydra from flash_attn.flash_blocksparse_attn_interface import flash_blocksparse_attn_func from flash_attn.flash_blocksparse_attn_interface import convert_blockmask from flash_attn.bert_padding import unpad_input, pad_input, index_first_axis class FlashBlocksparseAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_temp: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.1) """ def __init__(self, sparsity_config, softmax_temp=None, attention_dropout=0.0, max_seq_length=2048, device=None, dtype=None): super().__init__() self.sparsity_config = hydra.utils.instantiate(sparsity_config) self.softmax_temp = softmax_temp self.dropout_p = attention_dropout # initialize sparse layout and register as buffer max_seq_length = ((max_seq_length + 256 - 1) // 256) * 256 layout = self.sparsity_config.make_layout(max_seq_length) self.register_buffer("layout", layout) blockmask_converted = convert_blockmask(self.layout, causal=False) self.register_buffer("blockmask_converted", blockmask_converted) # logger.info(f'Attention class {self.__class__}: saving={self.layout.float().mean()}') def forward(self, qkv, attn_mask=None, key_padding_mask=None, causal=False, cu_seqlens=None, max_s=None, need_weights=False, convert_mask=True): """Implements the multihead softmax attention. Arguments --------- qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) if key_padding_mask is None attn_mask: An implementation of BaseMask that encodes where each query can attend to key_padding_mask: An implementation of BaseMask that encodes how many query each sequence in the batch consists of """ assert not need_weights assert attn_mask is None assert qkv.dtype == torch.float16 assert qkv.is_cuda if cu_seqlens is None: batch_size = qkv.shape[0] seqlen = qkv.shape[1] # Convert mask to take a subset seqlen_rounded = ((seqlen + 256 - 1) // 256) * 256 assert seqlen_rounded // 16 <= self.layout.shape[0], seqlen_rounded // 256 <= self.layout.shape[1] blockmask = self.layout[:seqlen_rounded // 16, :seqlen_rounded // 256] if key_padding_mask is None: qkv = rearrange(qkv, 'b s ... -> (b s) ...') max_s = seqlen cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=qkv.device) output = flash_blocksparse_attn_func( qkv, cu_seqlens, blockmask, self.dropout_p if self.training else 0.0, max_s, softmax_scale=self.softmax_temp, causal=causal ) output = rearrange(output, '(b s) ... -> b s ...', b=batch_size) else: key_padding_mask_bool = key_padding_mask.bool_matrix nheads = qkv.shape[-2] x = rearrange(qkv, 'b s three h d -> b s (three h d)') x_unpad, indices, cu_seqlens, max_s = unpad_input(x, key_padding_mask_bool) x_unpad = rearrange(x_unpad, 'nnz (three h d) -> nnz three h d', three=3, h=nheads) output_unpad = flash_blocksparse_attn_func( x_unpad, cu_seqlens, blockmask, self.dropout_p if self.training else 0.0, max_s, softmax_scale=self.softmax_temp, causal=causal ) output = rearrange(pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'), indices, batch_size, seqlen), 'b s (h d) -> b s h d', h=nheads) else: assert max_s is not None seqlen = max_s # Convert mask to take a subset seqlen_rounded = ((seqlen + 256 - 1) // 256) * 256 assert seqlen_rounded // 16 <= self.layout.shape[0], seqlen_rounded // 256 <= self.layout.shape[1] blockmask = self.layout[:seqlen_rounded // 16, :seqlen_rounded // 256] if convert_mask: output = flash_blocksparse_attn_func( qkv, cu_seqlens, blockmask, self.dropout_p if self.training else 0.0, max_s, softmax_scale=self.softmax_temp, causal=causal ) else: output = flash_blocksparse_attn_func( qkv, cu_seqlens, self.blockmask_converted, self.dropout_p if self.training else 0.0, max_s, softmax_scale=self.softmax_temp, causal=causal, convert_mask=False, ) return output, None class FlashBlocksparseMHA(nn.Module): def __init__(self, embed_dim, num_heads, sparsity_config, bias=True, batch_first=True, attention_dropout=0.0, causal=False, max_seq_length=2048, device=None, dtype=None, **kwargs) -> None: assert batch_first factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.embed_dim = embed_dim self.causal = causal self.num_heads = num_heads assert self.embed_dim % num_heads == 0, "self.kdim must be divisible by num_heads" self.head_dim = self.embed_dim // num_heads assert self.head_dim in [16, 32, 64], "Only support head_dim == 16, 32, or 64" self.Wqkv = nn.Linear(embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs) self.inner_attn = FlashBlocksparseAttention( sparsity_config, attention_dropout=attention_dropout, max_seq_length=max_seq_length, **factory_kwargs ) self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias, **factory_kwargs) def forward(self, x, x_ignored_, x_ignored_1_, attn_mask=None, key_padding_mask=None, need_weights=False): qkv = self.Wqkv(x) qkv = rearrange(qkv, 'b s (three h d) -> b s three h d', three=3, h=self.num_heads) context, attn_weights = self.inner_attn(qkv, key_padding_mask=key_padding_mask, need_weights=need_weights, causal=self.causal) return self.out_proj(rearrange(context, 'b s h d -> b s (h d)')), attn_weights
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_blocksparse_attention.py
# Adapted from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/padding.py import torch import torch.nn.functional as F from einops import rearrange, repeat class IndexFirstAxis(torch.autograd.Function): @staticmethod def forward(ctx, input, indices): ctx.save_for_backward(indices) assert input.ndim >= 2 ctx.first_axis_dim, other_shape = input.shape[0], input.shape[1:] second_dim = other_shape.numel() # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing. # return input[indices] return torch.gather(rearrange(input, 'b ... -> b (...)'), 0, repeat(indices, 'z -> z d', d=second_dim)).reshape(-1, *other_shape) @staticmethod def backward(ctx, grad_output): indices, = ctx.saved_tensors assert grad_output.ndim >= 2 other_shape = grad_output.shape[1:] grad_output = rearrange(grad_output, 'b ... -> b (...)') grad_input = torch.zeros([ctx.first_axis_dim, grad_output.shape[1]], device=grad_output.device, dtype=grad_output.dtype) # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing. # grad_input[indices] = grad_output grad_input.scatter_(0, repeat(indices, 'z -> z d', d=grad_output.shape[1]), grad_output) return grad_input.reshape(ctx.first_axis_dim, *other_shape), None index_first_axis = IndexFirstAxis.apply class IndexPutFirstAxis(torch.autograd.Function): @staticmethod def forward(ctx, values, indices, first_axis_dim): ctx.save_for_backward(indices) assert indices.ndim == 1 assert values.ndim >= 2 output = torch.zeros(first_axis_dim, *values.shape[1:], device=values.device, dtype=values.dtype) # TD [2022-03-04] For some reason torch.scatter is a bit faster than indexing. output[indices] = values # output.scatter_(0, repeat(indices, 'z -> z d', d=values.shape[1]), values) return output @staticmethod def backward(ctx, grad_output): indices, = ctx.saved_tensors # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing. grad_values = grad_output[indices] # grad_values = torch.gather(grad_output, 0, repeat(indices, 'z -> z d', d=grad_output.shape[1])) return grad_values, None, None index_put_first_axis = IndexPutFirstAxis.apply class IndexFirstAxisResidual(torch.autograd.Function): @staticmethod def forward(ctx, input, indices): ctx.save_for_backward(indices) assert input.ndim >= 2 ctx.first_axis_dim, other_shape = input.shape[0], input.shape[1:] second_dim = other_shape.numel() # TD [2022-03-04] For some reason torch.gather is a bit faster than indexing. output = input[indices] # We don't want to reshape input (b ... -> b (...)) since it could change the channel_last # memory format to channel_first. In other words, input might not be contiguous. # If we don't detach, Pytorch complains about output being a view and is being modified inplace return output, input.detach() @staticmethod def backward(ctx, grad_output, grad_residual): indices, = ctx.saved_tensors assert grad_output.ndim >= 2 other_shape = grad_output.shape[1:] assert grad_residual.shape[1:] == other_shape grad_input = grad_residual # grad_input[indices] += grad_output indices = indices.reshape(indices.shape[0], *((1,) * (grad_output.ndim - 1))) indices = indices.expand_as(grad_output) grad_input.scatter_add_(0, indices, grad_output) return grad_input.reshape(ctx.first_axis_dim, *other_shape), None index_first_axis_residual = IndexFirstAxisResidual.apply def unpad_input(hidden_states, attention_mask): """ Arguments: hidden_states: (batch, seqlen, ...) attention_mask: (batch, seqlen), bool / int, 1 means valid and 0 means not valid. Return: hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask. cu_seqlens: (batch + 1), the cumulative sequence lengths, used to index into hidden_states. max_seqlen_in_batch: int """ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32) indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten() max_seqlen_in_batch = seqlens_in_batch.max().item() cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0)) # TD [2022-03-04] We don't want to index with a bool mask, because Pytorch will expand the # bool mask, then call nonzero to get the indices, then index with those. The indices is @dim # times larger than it needs to be, wasting memory. It's faster and more memory-efficient to # index with integer indices. Moreover, torch's index is a bit slower than it needs to be, # so we write custom forward and backward to make it a bit faster. return (index_first_axis(rearrange(hidden_states, 'b s ... -> (b s) ...'), indices), indices, cu_seqlens, max_seqlen_in_batch) def pad_input(hidden_states, indices, batch, seqlen): """ Arguments: hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask. indices: (total_nnz) Return: hidden_states: (batch, seqlen, ...) """ dim = hidden_states.shape[-1] # output = torch.zeros((batch * seqlen), dim, device=hidden_states.device, dtype=hidden_states.dtype) # output[indices] = hidden_states output = index_put_first_axis(hidden_states, indices, batch * seqlen) return rearrange(output, '(b s) ... -> b s ...', b=batch)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/bert_padding.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/__init__.py
# [2022-10-23] Downloaded from https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py # for benchmarking. # We fixed a few dtype cast to make it work for bf16 """ Fused Attention =============== This is a Triton implementation of the Flash Attention algorithm (see: Dao et al., https://arxiv.org/pdf/2205.14135v2.pdf; Rabe and Staats https://arxiv.org/pdf/2112.05682v2.pdf) """ import pytest import torch import triton import triton.language as tl @triton.jit def _fwd_kernel( Q, K, V, sm_scale, TMP, L, M, # NOTE: TMP is a scratchpad buffer to workaround a compiler bug 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, ): start_m = tl.program_id(0) off_hz = tl.program_id(1) # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) offs_d = tl.arange(0, BLOCK_DMODEL) off_q = off_hz * stride_qh + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk off_k = off_hz * stride_qh + offs_n[:, None] * stride_kn + offs_d[None, :] * stride_kk off_v = off_hz * stride_qh + offs_n[:, None] * stride_qm + offs_d[None, :] * stride_qk # Initialize pointers to Q, K, V q_ptrs = Q + off_q k_ptrs = K + off_k v_ptrs = V + off_v # initialize pointer to m and l t_ptrs = TMP + off_hz * N_CTX + offs_m 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) # load q: it will stay in SRAM throughout q = tl.load(q_ptrs) # loop over k, v and update accumulator for start_n in range(0, (start_m + 1) * BLOCK_M, BLOCK_N): start_n = tl.multiple_of(start_n, BLOCK_N) # -- compute qk ---- k = tl.load(k_ptrs + start_n * stride_kn) qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, k, trans_b=True) qk *= sm_scale qk += tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), 0, float("-inf")) # -- compute m_ij, p, l_ij m_ij = tl.max(qk, 1) p = tl.exp(qk - m_ij[:, None]) l_ij = tl.sum(p, 1) # -- update m_i and l_i m_i_new = tl.maximum(m_i, m_ij) alpha = tl.exp(m_i - m_i_new) beta = tl.exp(m_ij - m_i_new) l_i_new = alpha * l_i + beta * l_ij # -- update output accumulator -- # scale p p_scale = beta / l_i_new p = p * p_scale[:, None] # scale acc acc_scale = l_i / l_i_new * alpha tl.store(t_ptrs, acc_scale) acc_scale = tl.load(t_ptrs) # BUG: have to store and immediately load acc = acc * acc_scale[:, None] # update acc v = tl.load(v_ptrs + start_n * stride_vk) p = p.to(v.dtype) acc += tl.dot(p, v) # update m_i and l_i l_i = l_i_new m_i = m_i_new # rematerialize offsets to save registers start_m = tl.program_id(0) offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) # write back l and m l_ptrs = L + off_hz * N_CTX + offs_m m_ptrs = M + off_hz * N_CTX + offs_m tl.store(l_ptrs, l_i) tl.store(m_ptrs, m_i) # initialize pointers to output offs_n = tl.arange(0, BLOCK_DMODEL) off_o = off_hz * stride_oh + offs_m[:, None] * stride_om + offs_n[None, :] * stride_on out_ptrs = Out + off_o tl.store(out_ptrs, acc) @triton.jit def _bwd_preprocess( Out, DO, L, NewDO, 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) denom = tl.load(L + off_m).to(tl.float32) # compute do = do / denom[:, None] delta = tl.sum(o * do, axis=1) # write-back tl.store(NewDO + off_m[:, None] * D_HEAD + off_n[None, :], do) tl.store(Delta + off_m, delta) @triton.jit def _bwd_kernel( Q, K, V, sm_scale, Out, DO, DQ, DK, DV, L, M, 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, ): off_hz = tl.program_id(0) off_z = off_hz // H off_h = off_hz % H # 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): lo = start_n * BLOCK_M # 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 m_ptrs = M + 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 # NOTE: `do` is pre-divided by `l`; no normalization here qk = tl.dot(q, k, trans_b=True) qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), qk, float("-inf")) m = tl.load(m_ptrs + offs_m_curr) p = tl.exp(qk * sm_scale - m[:, None]) # compute dv do = tl.load(do_ptrs) dv += tl.dot(p.to(do.dtype), do, trans_a=True) # 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, v, trans_b=True) # compute ds = p * (dp - delta[:, None]) ds = p * dp * sm_scale # compute dk = dot(ds.T, q) dk += tl.dot(ds.to(q.dtype), q, trans_a=True) # # compute dq dq = tl.load(dq_ptrs, eviction_policy="evict_last") dq += tl.dot(ds.to(k.dtype), k) tl.store(dq_ptrs, dq, eviction_policy="evict_last") # # 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) class _attention(torch.autograd.Function): @staticmethod def forward(ctx, q, k, v, sm_scale): BLOCK = 128 # 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) grid = (triton.cdiv(q.shape[2], BLOCK), q.shape[0] * q.shape[1]) tmp = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) L = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) m = 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, tmp, L, m, 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, BLOCK_N=BLOCK, BLOCK_DMODEL=Lk, num_warps=num_warps, num_stages=1, ) ctx.save_for_backward(q, k, v, o, L, m) ctx.BLOCK = BLOCK ctx.grid = grid ctx.sm_scale = sm_scale ctx.BLOCK_DMODEL = Lk return o @staticmethod def backward(ctx, do): q, k, v, o, l, m = ctx.saved_tensors do = do.contiguous() dq = torch.zeros_like(q, dtype=torch.float32) dk = torch.empty_like(k) dv = torch.empty_like(v) do_scaled = torch.empty_like(do) delta = torch.empty_like(l) _bwd_preprocess[(ctx.grid[0] * ctx.grid[1], )]( o, do, l, do_scaled, delta, BLOCK_M=ctx.BLOCK, D_HEAD=ctx.BLOCK_DMODEL, ) # NOTE: kernel currently buggy for other values of `num_warps` num_warps = 8 _bwd_kernel[(ctx.grid[1],)]( q, k, v, ctx.sm_scale, o, do_scaled, dq, dk, dv, l, m, 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=ctx.BLOCK, BLOCK_N=ctx.BLOCK, BLOCK_DMODEL=ctx.BLOCK_DMODEL, num_warps=num_warps, num_stages=1, ) return dq.to(q.dtype), dk, dv, None attention = _attention.apply
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_attn_triton_og.py
import math import torch import torch.nn as nn from einops import rearrange from flash_attn.flash_attn_interface import flash_attn_unpadded_qkvpacked_func from flash_attn.bert_padding import unpad_input, pad_input class FlashAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0) """ def __init__(self, softmax_scale=None, attention_dropout=0.0): super().__init__() self.softmax_scale = softmax_scale self.dropout_p = attention_dropout def forward(self, qkv, key_padding_mask=None, causal=False, cu_seqlens=None, max_s=None, need_weights=False): """Implements the multihead softmax attention. Arguments --------- qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) if key_padding_mask is None if unpadded: (nnz, 3, h, d) key_padding_mask: a bool tensor of shape (B, S) """ assert not need_weights assert qkv.dtype in [torch.float16, torch.bfloat16] assert qkv.is_cuda if cu_seqlens is None: batch_size = qkv.shape[0] seqlen = qkv.shape[1] if key_padding_mask is None: qkv = rearrange(qkv, 'b s ... -> (b s) ...') max_s = seqlen cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=qkv.device) output = flash_attn_unpadded_qkvpacked_func( qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) output = rearrange(output, '(b s) ... -> b s ...', b=batch_size) else: nheads = qkv.shape[-2] x = rearrange(qkv, 'b s three h d -> b s (three h d)') x_unpad, indices, cu_seqlens, max_s = unpad_input(x, key_padding_mask) x_unpad = rearrange(x_unpad, 'nnz (three h d) -> nnz three h d', three=3, h=nheads) output_unpad = flash_attn_unpadded_qkvpacked_func( x_unpad, cu_seqlens, max_s, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) output = rearrange(pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'), indices, batch_size, seqlen), 'b s (h d) -> b s h d', h=nheads) else: assert max_s is not None output = flash_attn_unpadded_qkvpacked_func( qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) return output, None class FlashMHA(nn.Module): def __init__(self, embed_dim, num_heads, bias=True, batch_first=True, attention_dropout=0.0, causal=False, device=None, dtype=None) -> None: assert batch_first factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.embed_dim = embed_dim self.causal = causal self.num_heads = num_heads assert self.embed_dim % num_heads == 0, "self.kdim must be divisible by num_heads" self.head_dim = self.embed_dim // num_heads assert self.head_dim % 8 == 0 and self.head_dim <= 128, "Only support head_dim <= 128 and divisible by 8" self.Wqkv = nn.Linear(embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs) self.inner_attn = FlashAttention(attention_dropout=attention_dropout) self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias, **factory_kwargs) def forward(self, x, key_padding_mask=None, need_weights=False): """x: (batch, seqlen, hidden_dim) (where hidden_dim = num heads * head dim) key_padding_mask: bool tensor of shape (batch, seqlen) """ qkv = self.Wqkv(x) qkv = rearrange(qkv, 'b s (three h d) -> b s three h d', three=3, h=self.num_heads) context, attn_weights = self.inner_attn(qkv, key_padding_mask=key_padding_mask, need_weights=need_weights, causal=self.causal) return self.out_proj(rearrange(context, 'b s h d -> b s (h d)')), attn_weights """ The FlashAttention class provided in the code implements the Flash Attention mechanism, which is a faster and more memory-efficient alternative to the standard Scaled Dot-Product Attention. Here's a step-by-step breakdown of the forward function: The input tensor qkv has shape (B, S, 3, H, D), where B is the batch size, S is the sequence length, H is the number of attention heads, and D is the head dimension. qkv contains the query, key, and value tensors. If cu_seqlens is None, the implementation computes the cumulative sequence lengths cu_seqlens and reshapes the input tensor as required for the Flash Attention function. If key_padding_mask is not None, the input tensor is first unpadded using the unpad_input function, then reshaped accordingly. After applying the Flash Attention function, the output tensor is padded back using the pad_input function. The Flash Attention function (flash_attn_unpadded_qkvpacked_func) is called with the processed input tensor, cumulative sequence lengths, dropout rate (if in training mode), softmax scaling factor, and a flag indicating whether causal attention is required. The output tensor of the Flash Attention function is reshaped as needed, and the attention weights are returned as None since the need_weights flag is set to False. The FlashMHA class integrates the FlashAttention mechanism into a multi-head attention (MHA) module. The forward function in FlashMHA consists of the following steps: The input tensor x is passed through the Wqkv linear layer, which computes the query, key, and value tensors. The output tensor is reshaped using the rearrange function to match the expected shape for the Flash Attention module. The reshaped tensor is passed through the inner_attn layer, which is an instance of the FlashAttention class. The need_weights flag is set to False, indicating that the attention weights are not needed in the output. The output tensor from the inner_attn layer is reshaped again using the rearrange function to match the expected shape for the out_proj linear layer. The reshaped tensor is passed through the out_proj linear layer, which returns the final output tensor. The attention weights are returned as None since the need_weights flag is set to False. """
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_attention.py
""" *Experimental* implementation of FlashAttention in Triton. Tested with triton==2.0.0.dev20221202. Triton 2.0 has a new backend (MLIR) but seems like it doesn't yet work for head dimensions other than 64: https://github.com/openai/triton/blob/d376020f90002757eea3ea9475d4f7cfc2ec5ead/python/triton/ops/flash_attention.py#L207 We'll update this implementation with the new Triton backend once this is fixed. We use the FlashAttention implementation from Phil Tillet a starting point. https://github.com/openai/triton/blob/master/python/tutorials/06-fused-attention.py Changes: - Implement both causal and non-causal attention. - Implement both self-attention and cross-attention. - Support arbitrary seqlens (not just multiples of 128), for both forward and backward. - Support all head dimensions up to 128 (not just 16, 32, 64, 128), for both forward and backward. - Support attention bias. - Speed up the forward pass a bit, and only store the LSE instead of m and l. - Make the backward for d=128 much faster by reducing register spilling. - Optionally parallelize the backward pass across seqlen_k, to deal with the case of small batch size * nheads. Caution: - This is an *experimental* implementation. The forward pass should be quite robust but I'm not 100% sure that the backward pass doesn't have race conditions (due to the Triton compiler). - This implementation has only been tested on A100. - If you plan to use headdim other than 64 and 128, you should test for race conditions (due to the Triton compiler), as done in tests/test_flash_attn.py "test_flash_attn_triton_race_condition". I've tested and fixed many race conditions for different head dimensions (40, 48, 64, 128, 80, 88, 96), but I'm still not 100% confident that there are none left for other head dimensions. Differences between this Triton version and the CUDA version: - Triton version doesn't support dropout. - Triton forward is generally faster than CUDA forward, while Triton backward is generally slower than CUDA backward. Overall Triton forward + backward is slightly slower than CUDA forward + backward. - Triton version doesn't support different sequence lengths in a batch (i.e., RaggedTensor/NestedTensor). - Triton version supports attention bias, while CUDA version doesn't. """ import math import torch import triton import triton.language as tl # Disabling autotune for now, set num_warps=4 if headdim=64 and num_warps=8 if headdim=128 # @triton.autotune( # configs=[ # triton.Config({"BLOCK_M": 128, "BLOCK_N": 128}, num_warps=4, num_stages=1), # # This config has a race condition when EVEN_M == False, disabling it for now. # # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64}, num_warps=4, num_stages=1), # ], # key=['CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL', 'BLOCK_HEADDIM'] # ) @triton.heuristics( { "EVEN_M": lambda args: args["seqlen_q"] % args["BLOCK_M"] == 0, "EVEN_N": lambda args: args["seqlen_k"] % args["BLOCK_N"] == 0, "EVEN_HEADDIM": lambda args: args["headdim"] == args["BLOCK_HEADDIM"], } ) @triton.jit def _fwd_kernel( Q, K, V, Bias, Out, Lse, TMP, # NOTE: TMP is a scratchpad buffer to workaround a compiler bug softmax_scale, stride_qb, stride_qh, stride_qm, stride_kb, stride_kh, stride_kn, stride_vb, stride_vh, stride_vn, stride_bb, stride_bh, stride_bm, stride_ob, stride_oh, stride_om, nheads, seqlen_q, seqlen_k, seqlen_q_rounded, headdim, CACHE_KEY_SEQLEN_Q, CACHE_KEY_SEQLEN_K, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, ): start_m = tl.program_id(0) off_hb = tl.program_id(1) off_b = off_hb // nheads off_h = off_hb % nheads # off_b = tl.program_id(1) # off_h = tl.program_id(2) # off_hb = off_b * nheads + off_h # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) offs_d = tl.arange(0, BLOCK_HEADDIM) # Initialize pointers to Q, K, V # Adding parenthesis around indexing might use int32 math instead of int64 math? # https://github.com/openai/triton/issues/741 # I'm seeing a tiny bit of difference (5-7us) q_ptrs = Q + off_b * stride_qb + off_h * stride_qh + (offs_m[:, None] * stride_qm + offs_d[None, :]) k_ptrs = K + off_b * stride_kb + off_h * stride_kh + (offs_n[:, None] * stride_kn + offs_d[None, :]) v_ptrs = V + off_b * stride_vb + off_h * stride_vh + (offs_n[:, None] * stride_vn + offs_d[None, :]) if BIAS_TYPE == 'vector': b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + offs_n elif BIAS_TYPE == 'matrix': b_ptrs = Bias + off_b * stride_bb + off_h * stride_bh + (offs_m[:, None] * stride_bm + offs_n[None, :]) # initialize pointer to m and l t_ptrs = TMP + off_hb * seqlen_q_rounded + offs_m lse_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") acc_o = tl.zeros([BLOCK_M, BLOCK_HEADDIM], dtype=tl.float32) # load q: it will stay in SRAM throughout # [2022-10-30] TD: Triton bug - in the case of EVEN_M=True and EVEN_N=False, if we just call # tl.load(q_ptrs), we get the wrong output! if EVEN_M & EVEN_N: if EVEN_HEADDIM: q = tl.load(q_ptrs) else: q = tl.load(q_ptrs, mask=offs_d[None, :] < headdim, other=0.0) else: if EVEN_HEADDIM: q = tl.load(q_ptrs, mask=offs_m[:, None] < seqlen_q, other=0.0) else: q = tl.load(q_ptrs, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0) # loop over k, v and update accumulator end_n = seqlen_k if not IS_CAUSAL else tl.minimum((start_m + 1) * BLOCK_M, seqlen_k) for start_n in range(0, end_n, BLOCK_N): start_n = tl.multiple_of(start_n, BLOCK_N) # -- compute qk ---- if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition if EVEN_HEADDIM: k = tl.load(k_ptrs + start_n * stride_kn) else: k = tl.load(k_ptrs + start_n * stride_kn, mask=offs_d[None, :] < headdim, other=0.0) else: if EVEN_HEADDIM: k = tl.load(k_ptrs + start_n * stride_kn, mask=(start_n + offs_n)[:, None] < seqlen_k, other=0.0) else: k = tl.load(k_ptrs + start_n * stride_kn, mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0) qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, k, trans_b=True) # Trying to combine the two masks seem to make the result wrong if not EVEN_N: # Need to mask out otherwise the softmax is wrong qk += tl.where((start_n + offs_n)[None, :] < seqlen_k, 0, float("-inf")) if IS_CAUSAL: qk += tl.where(offs_m[:, None] >= (start_n + offs_n)[None, :], 0, float("-inf")) if BIAS_TYPE != 'none': if BIAS_TYPE == 'vector': if EVEN_N: bias = tl.load(b_ptrs + start_n).to(tl.float32) else: bias = tl.load(b_ptrs + start_n, mask=(start_n + offs_n) < seqlen_k, other=0.0).to(tl.float32) bias = bias[None, :] elif BIAS_TYPE == 'matrix': if EVEN_M & EVEN_N: bias = tl.load(b_ptrs + start_n).to(tl.float32) else: bias = tl.load(b_ptrs + start_n, mask=(offs_m[:, None] < seqlen_q) & ((start_n + offs_n)[None, :] < seqlen_k), other=0.0).to(tl.float32) # Slightly faster to multiply the softmax_scale in the tl.exp below since the compiler # can then fuse the mult and add into an fma instruction. But if we have bias we need to # to multiply with softmax_scale here. qk = qk * softmax_scale + bias m_ij = tl.maximum(tl.max(qk, 1), lse_i) p = tl.exp(qk - m_ij[:, None]) else: m_ij = tl.maximum(tl.max(qk, 1) * softmax_scale, lse_i) p = tl.exp(qk * softmax_scale - m_ij[:, None]) l_ij = tl.sum(p, 1) # scale acc_o acc_o_scale = tl.exp(m_i - m_ij) # # -- update output accumulator -- # BUG: have to store and immediately load tl.store(t_ptrs, acc_o_scale) acc_o_scale = tl.load(t_ptrs) acc_o = acc_o * acc_o_scale[:, None] # update acc_o if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition if EVEN_HEADDIM: v = tl.load(v_ptrs + start_n * stride_vn) else: v = tl.load(v_ptrs + start_n * stride_vn, mask=offs_d[None, :] < headdim, other=0.0) else: if EVEN_HEADDIM: v = tl.load(v_ptrs + start_n * stride_vn, mask=(start_n + offs_n)[:, None] < seqlen_k, other=0.0) else: v = tl.load(v_ptrs + start_n * stride_vn, mask=((start_n + offs_n)[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0) p = p.to(v.dtype) acc_o += tl.dot(p, v) # -- update statistics m_i = m_ij l_i_new = tl.exp(lse_i - m_ij) + l_ij lse_i = m_ij + tl.log(l_i_new) o_scale = tl.exp(m_i - lse_i) # BUG: have to store and immediately load tl.store(t_ptrs, o_scale) o_scale = tl.load(t_ptrs) acc_o = acc_o * o_scale[:, None] # rematerialize offsets to save registers start_m = tl.program_id(0) offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) # write back l and m lse_ptrs = Lse + off_hb * seqlen_q_rounded + offs_m tl.store(lse_ptrs, lse_i) # initialize pointers to output offs_d = tl.arange(0, BLOCK_HEADDIM) out_ptrs = Out + off_b * stride_ob + off_h * stride_oh + (offs_m[:, None] * stride_om + offs_d[None, :]) if EVEN_M: if EVEN_HEADDIM: tl.store(out_ptrs, acc_o) else: tl.store(out_ptrs, acc_o, mask=offs_d[None, :] < headdim) else: if EVEN_HEADDIM: tl.store(out_ptrs, acc_o, mask=offs_m[:, None] < seqlen_q) else: tl.store(out_ptrs, acc_o, mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim)) @triton.jit def _bwd_preprocess_do_o_dot( Out, DO, Delta, stride_ob, stride_oh, stride_om, stride_dob, stride_doh, stride_dom, nheads, seqlen_q, seqlen_q_rounded, headdim, BLOCK_M: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, ): start_m = tl.program_id(0) off_hb = tl.program_id(1) off_b = off_hb // nheads off_h = off_hb % nheads # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_d = tl.arange(0, BLOCK_HEADDIM) # load o = tl.load(Out + off_b * stride_ob + off_h * stride_oh + offs_m[:, None] * stride_om + offs_d[None, :], mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0).to(tl.float32) do = tl.load(DO + off_b * stride_dob + off_h * stride_doh + offs_m[:, None] * stride_dom + offs_d[None, :], mask=(offs_m[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0).to(tl.float32) delta = tl.sum(o * do, axis=1) # write-back tl.store(Delta + off_hb * seqlen_q_rounded + offs_m, delta) @triton.jit def _bwd_store_dk_dv( dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, ): # [2022-11-01] TD: Same bug. In the case of EVEN_N=True and EVEN_M=False, # if we just call tl.store(dv_ptrs), there's a race condition if EVEN_N & EVEN_M: if EVEN_HEADDIM: tl.store(dv_ptrs, dv) tl.store(dk_ptrs, dk) else: tl.store(dv_ptrs, dv, mask=offs_d[None, :] < headdim) tl.store(dk_ptrs, dk, mask=offs_d[None, :] < headdim) else: if EVEN_HEADDIM: tl.store(dv_ptrs, dv, mask=offs_n[:, None] < seqlen_k) tl.store(dk_ptrs, dk, mask=offs_n[:, None] < seqlen_k) else: tl.store(dv_ptrs, dv, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim)) tl.store(dk_ptrs, dk, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim)) @triton.jit def _bwd_kernel_one_col_block( start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD: tl.constexpr, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, ): # We need to make sure begin_m is a multiple of BLOCK_M (not BLOCK_N) begin_m = 0 if not IS_CAUSAL else ((start_n * BLOCK_N) // BLOCK_M) * BLOCK_M # initialize row/col offsets offs_qm = begin_m + tl.arange(0, BLOCK_M) offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N) offs_m = tl.arange(0, BLOCK_M) offs_d = tl.arange(0, BLOCK_HEADDIM) # initialize pointers to value-like data q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_d[None, :]) k_ptrs = K + (offs_n[:, None] * stride_kn + offs_d[None, :]) v_ptrs = V + (offs_n[:, None] * stride_vn + offs_d[None, :]) do_ptrs = DO + (offs_qm[:, None] * stride_dom + offs_d[None, :]) dq_ptrs = DQ + (offs_qm[:, None] * stride_dqm + offs_d[None, :]) if BIAS_TYPE == 'vector': b_ptrs = Bias + offs_n elif BIAS_TYPE == 'matrix': b_ptrs = Bias + (offs_qm[:, None] * stride_bm + offs_n[None, :]) # initialize dv and dk dv = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32) dk = tl.zeros([BLOCK_N, BLOCK_HEADDIM], dtype=tl.float32) # There seems to be some problem with Triton pipelining that makes results wrong for # headdim=64, seqlen=(113, 255), bias_type='matrix'. In this case the for loop # may have zero step, and pipelining with the bias matrix could screw it up. # So we just exit early. if begin_m >= seqlen_q: dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :]) dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :]) _bwd_store_dk_dv(dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM) return # k and v stay in SRAM throughout # [2022-10-30] TD: Same bug as the fwd. In the case of EVEN_N=True and EVEN_M=False, # if we just call tl.load(k_ptrs), we get the wrong output! if EVEN_N & EVEN_M: if EVEN_HEADDIM: k = tl.load(k_ptrs) v = tl.load(v_ptrs) else: k = tl.load(k_ptrs, mask=offs_d[None, :] < headdim, other=0.0) v = tl.load(v_ptrs, mask=offs_d[None, :] < headdim, other=0.0) else: if EVEN_HEADDIM: k = tl.load(k_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0) v = tl.load(v_ptrs, mask=offs_n[:, None] < seqlen_k, other=0.0) else: k = tl.load(k_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0) v = tl.load(v_ptrs, mask=(offs_n[:, None] < seqlen_k) & (offs_d[None, :] < headdim), other=0.0) # loop over rows num_block_m = tl.cdiv(seqlen_q, BLOCK_M) for start_m in range(begin_m, num_block_m * BLOCK_M, BLOCK_M): start_m = tl.multiple_of(start_m, BLOCK_M) offs_m_curr = start_m + offs_m # load q, k, v, do on-chip # Same bug as below. Otherwise gives wrong result for headdim=40, seqlen=(128, 117) if EVEN_M & EVEN_HEADDIM: q = tl.load(q_ptrs) else: if EVEN_HEADDIM: q = tl.load(q_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0) else: q = tl.load(q_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0) # recompute p = softmax(qk, dim=-1).T qk = tl.dot(q, k, trans_b=True) # Trying to combine the two masks seem to make the result wrong if not EVEN_N: # Need to mask out otherwise the softmax is wrong qk = tl.where(offs_n[None, :] < seqlen_k, qk, float("-inf")) if IS_CAUSAL: qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), qk, float("-inf")) if BIAS_TYPE != 'none': tl.debug_barrier() # Race condition otherwise if BIAS_TYPE == 'vector': if EVEN_N: bias = tl.load(b_ptrs).to(tl.float32) else: bias = tl.load(b_ptrs, mask=offs_n < seqlen_k, other=0.0).to(tl.float32) bias = bias[None, :] elif BIAS_TYPE == 'matrix': if EVEN_M & EVEN_N: bias = tl.load(b_ptrs).to(tl.float32) else: bias = tl.load(b_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_n[None, :] < seqlen_k), other=0.0).to(tl.float32) qk = qk * softmax_scale + bias # There seems to be a race condition when headdim=48/96, and dq, dk, dv are wrong. # Also wrong for headdim=64. if not (EVEN_M & EVEN_HEADDIM): tl.debug_barrier() lse_i = tl.load(LSE + offs_m_curr) if BIAS_TYPE == 'none': p = tl.exp(qk * softmax_scale - lse_i[:, None]) else: p = tl.exp(qk - lse_i[:, None]) # compute dv # [2022-10-30] TD: A Triton bug: if EVEN_M=True and EVEN_HEADDIM=False, if we call # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0), we get wrong outputs # in the case of headdim=48/96, seqlen_q & seqlen_k >= 512. If headdim=40 or seqlen < 512, # the output is correct. if EVEN_M & EVEN_HEADDIM: do = tl.load(do_ptrs) else: # [2022-11-01] TD: Triton bug, there's a race condition if we just use m_mask and not d_mask. do = tl.load(do_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0) # if EVEN_M: # if EVEN_HEADDIM: # do = tl.load(do_ptrs) # else: # do = tl.load(do_ptrs, mask=offs_d[None, :] < headdim, other=0.0) # else: # if EVEN_HEADDIM: # do = tl.load(do_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0) # else: # do = tl.load(do_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) # & (offs_d[None, :] < headdim), other=0.0) dv += tl.dot(p.to(do.dtype), do, trans_a=True) # compute dp = dot(v, do) # There seems to be a race condition when headdim=48/96, and dq, dk are wrong. # Also wrong for headdim=128, seqlen=(108, 256), and ATOMIC_ADD=True # Also wrong for headdim=64, seqlen=(1023, 1024), and ATOMIC_ADD=False if not (EVEN_M & EVEN_HEADDIM): tl.debug_barrier() dp = tl.dot(do, v, trans_b=True) # There's a race condition for headdim=48 if not EVEN_HEADDIM: tl.debug_barrier() # compute ds = p * (dp - delta[:, None]) # Putting the subtraction after the dp matmul (instead of before) is slightly faster Di = tl.load(D + offs_m_curr) # Converting ds to q.dtype here reduces register pressure and makes it much faster # for BLOCK_HEADDIM=128 ds = (p * (dp - Di[:, None]) * softmax_scale).to(q.dtype) # compute dk = dot(ds.T, q) dk += tl.dot(ds, q, trans_a=True) # compute dq if not (EVEN_M & EVEN_HEADDIM): # Otherewise there's a race condition when BIAS_TYPE='matrix' tl.debug_barrier() if not ATOMIC_ADD: if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M dq = tl.load(dq_ptrs, eviction_policy="evict_last") dq += tl.dot(ds, k) tl.store(dq_ptrs, dq, eviction_policy="evict_last") else: if EVEN_HEADDIM: dq = tl.load(dq_ptrs, mask=offs_m_curr[:, None] < seqlen_q, other=0.0, eviction_policy="evict_last") dq += tl.dot(ds, k) tl.store(dq_ptrs, dq, mask=offs_m_curr[:, None] < seqlen_q, eviction_policy="evict_last") else: dq = tl.load(dq_ptrs, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), other=0.0, eviction_policy="evict_last") dq += tl.dot(ds, k) tl.store(dq_ptrs, dq, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim), eviction_policy="evict_last") else: # If we're parallelizing across the seqlen_k dimension dq = tl.dot(ds, k) if EVEN_M & EVEN_HEADDIM: # Race condition if we just do EVEN_M tl.atomic_add(dq_ptrs, dq) else: if EVEN_HEADDIM: tl.atomic_add(dq_ptrs, dq, mask=offs_m_curr[:, None] < seqlen_q) else: tl.atomic_add(dq_ptrs, dq, mask=(offs_m_curr[:, None] < seqlen_q) & (offs_d[None, :] < headdim)) # increment pointers dq_ptrs += BLOCK_M * stride_dqm q_ptrs += BLOCK_M * stride_qm do_ptrs += BLOCK_M * stride_dom if BIAS_TYPE == 'matrix': b_ptrs += BLOCK_M * stride_bm # write-back dv_ptrs = DV + (offs_n[:, None] * stride_dvn + offs_d[None, :]) dk_ptrs = DK + (offs_n[:, None] * stride_dkn + offs_d[None, :]) _bwd_store_dk_dv(dk_ptrs, dv_ptrs, dk, dv, offs_n, offs_d, seqlen_k, headdim, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM) def init_to_zero(name): return lambda nargs: nargs[name].zero_() @triton.autotune( configs=[ triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "SEQUENCE_PARALLEL": False}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')), triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "SEQUENCE_PARALLEL": True}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')), # Other configs seem to give wrong results when seqlen_q % 128 != 0, disabling them for now # # Kernel is buggy (give wrong result) if we set BLOCK_m=128, BLOCK_n=64, num_warps=*4* # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')), # triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=8, num_stages=1, pre_hook=init_to_zero('DQ')), # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": False}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')), # triton.Config({"BLOCK_M": 64, "BLOCK_N": 64, "SEQUENCE_PARALLEL": True}, num_warps=4, num_stages=1, pre_hook=init_to_zero('DQ')), ], key=['CACHE_KEY_SEQLEN_Q', 'CACHE_KEY_SEQLEN_K', 'BIAS_TYPE', 'IS_CAUSAL', 'BLOCK_HEADDIM'], ) @triton.heuristics( { "EVEN_M": lambda args: args["seqlen_q"] % args["BLOCK_M"] == 0, "EVEN_N": lambda args: args["seqlen_k"] % args["BLOCK_N"] == 0, "EVEN_HEADDIM": lambda args: args["headdim"] == args["BLOCK_HEADDIM"], } ) @triton.jit def _bwd_kernel( Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qb, stride_qh, stride_qm, stride_kb, stride_kh, stride_kn, stride_vb, stride_vh, stride_vn, stride_bb, stride_bh, stride_bm, stride_dob, stride_doh, stride_dom, stride_dqb, stride_dqh, stride_dqm, stride_dkb, stride_dkh, stride_dkn, stride_dvb, stride_dvh, stride_dvn, nheads, seqlen_q, seqlen_k, seqlen_q_rounded, headdim, CACHE_KEY_SEQLEN_Q, CACHE_KEY_SEQLEN_K, BIAS_TYPE: tl.constexpr, IS_CAUSAL: tl.constexpr, BLOCK_HEADDIM: tl.constexpr, SEQUENCE_PARALLEL: tl.constexpr, EVEN_M: tl.constexpr, EVEN_N: tl.constexpr, EVEN_HEADDIM: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, ): off_hb = tl.program_id(1) off_b = off_hb // nheads off_h = off_hb % nheads # offset pointers for batch/head Q += off_b * stride_qb + off_h * stride_qh K += off_b * stride_kb + off_h * stride_kh V += off_b * stride_vb + off_h * stride_vh DO += off_b * stride_dob + off_h * stride_doh DQ += off_b * stride_dqb + off_h * stride_dqh DK += off_b * stride_dkb + off_h * stride_dkh DV += off_b * stride_dvb + off_h * stride_dvh if BIAS_TYPE != 'none': Bias += off_b * stride_bb + off_h * stride_bh # pointer to row-wise quantities in value-like data D += off_hb * seqlen_q_rounded LSE += off_hb * seqlen_q_rounded if not SEQUENCE_PARALLEL: num_block_n = tl.cdiv(seqlen_k, BLOCK_N) for start_n in range(0, num_block_n): _bwd_kernel_one_col_block( start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD=False, BIAS_TYPE=BIAS_TYPE, IS_CAUSAL=IS_CAUSAL, BLOCK_HEADDIM=BLOCK_HEADDIM, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM, BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N ) else: start_n = tl.program_id(0) _bwd_kernel_one_col_block( start_n, Q, K, V, Bias, DO, DQ, DK, DV, LSE, D, softmax_scale, stride_qm, stride_kn, stride_vn, stride_bm, stride_dom, stride_dqm, stride_dkn, stride_dvn, seqlen_q, seqlen_k, headdim, ATOMIC_ADD=True, BIAS_TYPE=BIAS_TYPE, IS_CAUSAL=IS_CAUSAL, BLOCK_HEADDIM=BLOCK_HEADDIM, EVEN_M=EVEN_M, EVEN_N=EVEN_N, EVEN_HEADDIM=EVEN_HEADDIM, BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N ) def _flash_attn_forward(q, k, v, bias=None, causal=False, softmax_scale=None): # shape constraints batch, seqlen_q, nheads, d = q.shape _, seqlen_k, _, _ = k.shape assert k.shape == (batch, seqlen_k, nheads, d) assert v.shape == (batch, seqlen_k, nheads, d) assert d <= 128, 'FlashAttention only support head dimensions up to 128' assert q.dtype == k.dtype == v.dtype, 'All tensors must have the same type' assert q.dtype in [torch.float16, torch.bfloat16], 'Only support fp16 and bf16' assert q.is_cuda and k.is_cuda and v.is_cuda softmax_scale = softmax_scale or 1.0 / math.sqrt(d) has_bias = bias is not None bias_type = 'none' if has_bias: assert bias.dtype in [q.dtype, torch.float] assert bias.is_cuda assert bias.dim() == 4 if bias.stride(-1) != 1: bias = bias.contiguous() if bias.shape[2:] == (1, seqlen_k): bias_type = 'vector' elif bias.shape[2:] == (seqlen_q, seqlen_k): bias_type = 'matrix' else: raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k)' ' or (seqlen_q, seqlen_k)') bias = bias.expand(batch, nheads, seqlen_q, seqlen_k) bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0) seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128 lse = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32) tmp = torch.empty((batch, nheads, seqlen_q_rounded), device=q.device, dtype=torch.float32) o = torch.empty_like(q) BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16) BLOCK = 128 num_warps = 4 if d <= 64 else 8 grid = lambda META: (triton.cdiv(seqlen_q, META["BLOCK_M"]), batch * nheads) _fwd_kernel[grid]( q, k, v, bias, o, lse, tmp, softmax_scale, q.stride(0), q.stride(2), q.stride(1), k.stride(0), k.stride(2), k.stride(1), v.stride(0), v.stride(2), v.stride(1), *bias_strides, o.stride(0), o.stride(2), o.stride(1), nheads, seqlen_q, seqlen_k, seqlen_q_rounded, d, seqlen_q // 32, seqlen_k // 32, # key for triton cache (limit number of compilations) # Can't use kwargs here because triton autotune expects key to be args, not kwargs # IS_CAUSAL=causal, BLOCK_HEADDIM=d, bias_type, causal, BLOCK_HEADDIM, BLOCK_M=BLOCK, BLOCK_N=BLOCK, num_warps=num_warps, num_stages=1, ) return o, lse, softmax_scale # softmax_scale could have been updated def _flash_attn_backward(do, q, k, v, o, lse, dq, dk, dv, bias=None, causal=False, softmax_scale=None): # Make sure that the last dimension is contiguous if do.stride(-1) != 1: do = do.contiguous() batch, seqlen_q, nheads, d = q.shape _, seqlen_k, _, _ = k.shape # assert d in {16, 32, 64, 128} assert d <= 128 seqlen_q_rounded = math.ceil(seqlen_q / 128) * 128 assert lse.shape == (batch, nheads, seqlen_q_rounded) assert q.stride(-1) == k.stride(-1) == v.stride(-1) == o.stride(-1) == 1 assert dq.stride(-1) == dk.stride(-1) == dv.stride(-1) == 1 softmax_scale = softmax_scale or 1.0 / math.sqrt(d) # dq_accum = torch.zeros_like(q, dtype=torch.float32) dq_accum = torch.empty_like(q, dtype=torch.float32) delta = torch.empty_like(lse) # delta = torch.zeros_like(lse) BLOCK_HEADDIM = max(triton.next_power_of_2(d), 16) grid = lambda META: (triton.cdiv(seqlen_q, META["BLOCK_M"]), batch * nheads) _bwd_preprocess_do_o_dot[grid]( o, do, delta, o.stride(0), o.stride(2), o.stride(1), do.stride(0), do.stride(2), do.stride(1), nheads, seqlen_q, seqlen_q_rounded, d, BLOCK_M=128, BLOCK_HEADDIM=BLOCK_HEADDIM, ) has_bias = bias is not None bias_type = 'none' if has_bias: assert bias.dtype in [q.dtype, torch.float] assert bias.is_cuda assert bias.dim() == 4 assert bias.stride(-1) == 1 if bias.shape[2:] == (1, seqlen_k): bias_type = 'vector' elif bias.shape[2:] == (seqlen_q, seqlen_k): bias_type = 'matrix' else: raise RuntimeError('Last 2 dimensions of bias must be (1, seqlen_k)' ' or (seqlen_q, seqlen_k)') bias = bias.expand(batch, nheads, seqlen_q, seqlen_k) bias_strides = (bias.stride(0), bias.stride(1), bias.stride(2)) if has_bias else (0, 0, 0) # BLOCK_M = 128 # BLOCK_N = 64 # num_warps = 4 grid = lambda META: (triton.cdiv(seqlen_k, META["BLOCK_N"]) if META["SEQUENCE_PARALLEL"] else 1, batch * nheads) _bwd_kernel[grid]( q, k, v, bias, do, dq_accum, dk, dv, lse, delta, softmax_scale, q.stride(0), q.stride(2), q.stride(1), k.stride(0), k.stride(2), k.stride(1), v.stride(0), v.stride(2), v.stride(1), *bias_strides, do.stride(0), do.stride(2), do.stride(1), dq_accum.stride(0), dq_accum.stride(2), dq_accum.stride(1), dk.stride(0), dk.stride(2), dk.stride(1), dv.stride(0), dv.stride(2), dv.stride(1), nheads, seqlen_q, seqlen_k, seqlen_q_rounded, d, seqlen_q // 32, seqlen_k // 32, # key for triton cache (limit number of compilations) # Can't use kwargs here because triton autotune expects key to be args, not kwargs # IS_CAUSAL=causal, BLOCK_HEADDIM=d, bias_type, causal, BLOCK_HEADDIM, # SEQUENCE_PARALLEL=False, # BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, # num_warps=num_warps, # num_stages=1, ) dq.copy_(dq_accum) class FlashAttnQKVPackedFunc(torch.autograd.Function): @staticmethod def forward(ctx, qkv, bias=None, causal=False, softmax_scale=None): """ qkv: (batch, seqlen, 3, nheads, headdim) bias: optional, shape broadcastible to (batch, nheads, seqlen, seqlen). For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen). ALiBi mask for non-causal would have shape (1, nheads, seqlen, seqlen) """ # Make sure that the last dimension is contiguous if qkv.stride(-1) != 1: qkv = qkv.contiguous() o, lse, ctx.softmax_scale = _flash_attn_forward( qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], bias=bias, causal=causal, softmax_scale=softmax_scale ) ctx.save_for_backward(qkv, o, lse, bias) ctx.causal = causal return o @staticmethod def backward(ctx, do): qkv, o, lse, bias = ctx.saved_tensors assert not ctx.needs_input_grad[1], 'FlashAttention does not support bias gradient yet' # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version. with torch.inference_mode(): dqkv = torch.empty_like(qkv) _flash_attn_backward(do, qkv[:, :, 0], qkv[:, :, 1], qkv[:, :, 2], o, lse, dqkv[:, :, 0], dqkv[:, :, 1], dqkv[:, :, 2], bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale) return dqkv, None, None, None flash_attn_qkvpacked_func = FlashAttnQKVPackedFunc.apply class FlashAttnKVPackedFunc(torch.autograd.Function): @staticmethod def forward(ctx, q, kv, bias=None, causal=False, softmax_scale=None): """ q: (batch, seqlen_q, nheads, headdim) kv: (batch, seqlen_k, 2, nheads, headdim) bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k). For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k). ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k) """ # Make sure that the last dimension is contiguous q, kv = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, kv]] o, lse, ctx.softmax_scale = _flash_attn_forward( q, kv[:, :, 0], kv[:, :, 1], bias=bias, causal=causal, softmax_scale=softmax_scale ) ctx.save_for_backward(q, kv, o, lse, bias) ctx.causal = causal return o @staticmethod def backward(ctx, do): q, kv, o, lse, bias = ctx.saved_tensors if len(ctx.needs_input_grad) >= 3: assert not ctx.needs_input_grad[2], 'FlashAttention does not support bias gradient yet' # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version. with torch.inference_mode(): dq = torch.empty_like(q) dkv = torch.empty_like(kv) _flash_attn_backward(do, q, kv[:, :, 0], kv[:, :, 1], o, lse, dq, dkv[:, :, 0], dkv[:, :, 1], bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale) return dq, dkv, None, None, None flash_attn_kvpacked_func = FlashAttnKVPackedFunc.apply class FlashAttnFunc(torch.autograd.Function): @staticmethod def forward(ctx, q, k, v, bias=None, causal=False, softmax_scale=None): """ q: (batch_size, seqlen_q, nheads, headdim) k, v: (batch_size, seqlen_k, nheads, headdim) bias: optional, shape broadcastible to (batch, nheads, seqlen_q, seqlen_k). For example, ALiBi mask for causal would have shape (1, nheads, 1, seqlen_k). ALiBi mask for non-causal would have shape (1, nheads, seqlen_q, seqlen_k) """ # Make sure that the last dimension is contiguous q, k, v = [x if x.stride(-1) == 1 else x.contiguous() for x in [q, k, v]] o, lse, ctx.softmax_scale = _flash_attn_forward( q, k, v, bias=bias, causal=causal, softmax_scale=softmax_scale ) ctx.save_for_backward(q, k, v, o, lse, bias) ctx.causal = causal return o @staticmethod def backward(ctx, do): q, k, v, o, lse, bias = ctx.saved_tensors assert not ctx.needs_input_grad[3], 'FlashAttention does not support bias gradient yet' # Triton's autotune causes the Tensor._version to change, and so Pytorch autograd # does a memcpy. To avoid this we run in inference_mode, which doesn't track the version. with torch.inference_mode(): dq = torch.empty_like(q) dk = torch.empty_like(k) dv = torch.empty_like(v) _flash_attn_backward(do, q, k, v, o, lse, dq, dk, dv, bias=bias, causal=ctx.causal, softmax_scale=ctx.softmax_scale) return dq, dk, dv, None, None, None flash_attn_func = FlashAttnFunc.apply
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_attn_triton.py
import torch import torch.nn as nn import torch.nn.functional as F import flash_attn_cuda def _get_block_size(device, head_dim, is_dropout): assert head_dim % 8 == 0 and head_dim <= 128 return 256 if head_dim <= 64 else 128 def _flash_attn_forward(q, k, v, out, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, return_softmax, num_splits=0, generator=None): """ num_splits: how much to parallelize over the seqlen_q dimension. num_splits=0 means it will be set by an internal heuristic. We're exposing num_splits mostly for benchmarking. Don't change it unless you know what you're doing. """ softmax_lse, *rest = flash_attn_cuda.fwd( q, k, v, out, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, False, causal, return_softmax, num_splits, generator ) # if out.isnan().any() or softmax_lse.isnan().any(): # breakpoint() S_dmask = rest[0] if return_softmax else None return out, softmax_lse, S_dmask def _flash_attn_backward(dout, q, k, v, out, softmax_lse, dq, dk, dv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, num_splits=0, generator=None): """ num_splits: whether to parallelize over the seqlen_k dimension (num_splits > 1) or not (num_splits = 1). num_splits=0 means it will be set by an internal heuristic. Any value above 1 will call the same kernel (i.e. num_splits=2 would call the same kernel as num_splits=3), so effectively the choices are 0, 1, and 2. This hyperparameter can be tuned for performance, but default value (heuristic) should work fine. """ dout = dout.contiguous() # CUDA code assumes that dout is contiguous _, _, _, softmax_d = flash_attn_cuda.bwd( dout, q, k, v, out, softmax_lse, dq, dk, dv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, False, causal, num_splits, generator) # if dk.isnan().any() or dk.isnan().any() or dv.isnan().any() or softmax_d.isnan().any(): # breakpoint() return dq, dk, dv, softmax_d class FlashAttnQKVPackedFunc(torch.autograd.Function): @staticmethod def forward(ctx, qkv, cu_seqlens, max_seqlen, dropout_p, softmax_scale, causal, return_softmax, deterministic): # Save rng_state because the backward pass will regenerate the dropout mask rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None if softmax_scale is None: softmax_scale = qkv.shape[-1] ** (-0.5) out, softmax_lse, S_dmask = _flash_attn_forward( qkv[:, 0], qkv[:, 1], qkv[:, 2], torch.empty_like(qkv[:, 0]), cu_seqlens, cu_seqlens, max_seqlen, max_seqlen, dropout_p, softmax_scale, causal=causal, return_softmax=return_softmax ) ctx.save_for_backward(qkv, out, softmax_lse, cu_seqlens, rng_state) ctx.dropout_p = dropout_p ctx.max_seqlen = max_seqlen ctx.softmax_scale = softmax_scale ctx.causal = causal ctx.deterministic = deterministic return out if not return_softmax else (out, softmax_lse, S_dmask) @staticmethod def backward(ctx, dout, *args): qkv, out, softmax_lse, cu_seqlens, rng_state = ctx.saved_tensors if rng_state is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state) dqkv = torch.empty_like(qkv) _flash_attn_backward( dout, qkv[:, 0], qkv[:, 1], qkv[:, 2], out, softmax_lse, dqkv[:, 0], dqkv[:, 1], dqkv[:, 2], cu_seqlens, cu_seqlens, ctx.max_seqlen, ctx.max_seqlen, ctx.dropout_p, ctx.softmax_scale, ctx.causal, num_splits=1 if ctx.deterministic else 0, ) if rng_state is not None: torch.cuda.set_rng_state(cur_rng_state) return dqkv, None, None, None, None, None, None, None class FlashAttnKVPackedFunc(torch.autograd.Function): @staticmethod def forward(ctx, q, kv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, return_softmax, deterministic): # Save rng_state because the backward pass will regenerate the dropout mask rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None if softmax_scale is None: softmax_scale = q.shape[-1] ** (-0.5) out, softmax_lse, S_dmask = _flash_attn_forward( q, kv[:, 0], kv[:, 1], torch.empty_like(q), cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal=causal, return_softmax=return_softmax ) ctx.save_for_backward(q, kv, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state) ctx.dropout_p = dropout_p ctx.max_seqlen_q = max_seqlen_q ctx.max_seqlen_k = max_seqlen_k ctx.softmax_scale = softmax_scale ctx.causal = causal ctx.deterministic = deterministic return out if not return_softmax else (out, softmax_lse, S_dmask) @staticmethod def backward(ctx, dout, *args): q, kv, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state = ctx.saved_tensors if rng_state is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state) dq = torch.empty_like(q) dkv = torch.empty_like(kv) _flash_attn_backward( dout, q, kv[:, 0], kv[:, 1], out, softmax_lse, dq, dkv[:, 0], dkv[:, 1], cu_seqlens_q, cu_seqlens_k, ctx.max_seqlen_q, ctx.max_seqlen_k, ctx.dropout_p, ctx.softmax_scale, ctx.causal, num_splits=1 if ctx.deterministic else 0, ) if rng_state is not None: torch.cuda.set_rng_state(cur_rng_state) return dq, dkv, None, None, None, None, None, None, None, None, None class FlashAttnFunc(torch.autograd.Function): @staticmethod def forward(ctx, q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, return_softmax, deterministic): # Save rng_state because the backward pass will regenerate the dropout mask rng_state = torch.cuda.get_rng_state() if dropout_p > 0 else None if softmax_scale is None: softmax_scale = q.shape[-1] ** (-0.5) out, softmax_lse, S_dmask = _flash_attn_forward( q, k, v, torch.empty_like(q), cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal=causal, return_softmax=return_softmax ) ctx.save_for_backward(q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state) ctx.dropout_p = dropout_p ctx.max_seqlen_q = max_seqlen_q ctx.max_seqlen_k = max_seqlen_k ctx.softmax_scale = softmax_scale ctx.causal = causal ctx.deterministic = deterministic return out if not return_softmax else (out, softmax_lse, S_dmask) @staticmethod def backward(ctx, dout, *args): q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, rng_state = ctx.saved_tensors if rng_state is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state) dq, dk, dv = torch.empty_like(q), torch.empty_like(k), torch.empty_like(v) _flash_attn_backward( dout, q, k, v, out, softmax_lse, dq, dk, dv, cu_seqlens_q, cu_seqlens_k, ctx.max_seqlen_q, ctx.max_seqlen_k, ctx.dropout_p, ctx.softmax_scale, ctx.causal, num_splits=1 if ctx.deterministic else 0, ) if rng_state is not None: torch.cuda.set_rng_state(cur_rng_state) return dq, dk, dv, None, None, None, None, None, None, None, None, None class FlashAttnQKVPackedSplitFunc(torch.autograd.Function): @staticmethod def forward(ctx, qkv, cu_seqlens, max_seqlen0, max_seqlen1, batch_size0, dropout_p, softmax_scale, causal, return_softmax, deterministic): # Save rng_state because the backward pass will regenerate the dropout mask if dropout_p > 0: rng_state0 = torch.cuda.get_rng_state() generator1 = torch.Generator(device='cuda') rng_state1 = generator1.get_state() else: rng_state0, generator1, rng_state1 = None, None, None if softmax_scale is None: softmax_scale = qkv.shape[-1] ** (-0.5) out = torch.empty_like(qkv[:, 0]) _, softmax_lse0, S_dmask0 = _flash_attn_forward( qkv[:, 0], qkv[:, 1], qkv[:, 2], out, cu_seqlens[:batch_size0 + 1], cu_seqlens[:batch_size0 + 1], max_seqlen0, max_seqlen0, dropout_p, softmax_scale, causal=causal, return_softmax=return_softmax ) s = torch.cuda.Stream() with torch.cuda.stream(s): _, softmax_lse1, S_dmask1 = _flash_attn_forward( qkv[:, 0], qkv[:, 1], qkv[:, 2], out, cu_seqlens[batch_size0:], cu_seqlens[batch_size0:], max_seqlen1, max_seqlen1, dropout_p, softmax_scale, causal=causal, return_softmax=return_softmax, generator=generator1 ) torch.cuda.current_stream().wait_stream(s) ctx.save_for_backward(qkv, out, softmax_lse0, softmax_lse1, cu_seqlens, rng_state0, rng_state1) ctx.dropout_p = dropout_p ctx.max_seqlen0 = max_seqlen0 ctx.max_seqlen1 = max_seqlen1 ctx.batch_size0 = batch_size0 ctx.softmax_scale = softmax_scale ctx.causal = causal ctx.deterministic = deterministic if not return_softmax: return out else: max_seqlen_q = max(softmax_lse0.shape[2], softmax_lse1.shape[2]) max_seqlen_k = max(S_dmask0.shape[3], S_dmask1.shape[3]) softmax_lse = torch.cat([F.pad(softmax_lse0, (0, max_seqlen_q - softmax_lse0.shape[2])), F.pad(softmax_lse1, (0, max_seqlen_q - softmax_lse1.shape[2]))], dim=0) return out, softmax_lse, S_dmask0, S_dmask1 @staticmethod def backward(ctx, dout, *args): qkv, out, softmax_lse0, softmax_lse1, cu_seqlens, rng_state0, rng_state1 = ctx.saved_tensors batch_size0 = ctx.batch_size0 if rng_state0 is not None: cur_rng_state = torch.cuda.get_rng_state() torch.cuda.set_rng_state(rng_state0) if rng_state1 is not None: generator1 = torch.Generator(device='cuda') generator1.set_state(rng_state1) else: generator1 = None dqkv = torch.empty_like(qkv) _flash_attn_backward( dout, qkv[:, 0], qkv[:, 1], qkv[:, 2], out, softmax_lse0, dqkv[:, 0], dqkv[:, 1], dqkv[:, 2], cu_seqlens[:batch_size0 + 1], cu_seqlens[:batch_size0 + 1], ctx.max_seqlen0, ctx.max_seqlen0, ctx.dropout_p, ctx.softmax_scale, ctx.causal, num_splits=1 if ctx.deterministic else 0, ) s = torch.cuda.Stream() with torch.cuda.stream(s): _flash_attn_backward( dout, qkv[:, 0], qkv[:, 1], qkv[:, 2], out, softmax_lse1, dqkv[:, 0], dqkv[:, 1], dqkv[:, 2], cu_seqlens[batch_size0:], cu_seqlens[batch_size0:], ctx.max_seqlen1, ctx.max_seqlen1, ctx.dropout_p, ctx.softmax_scale, ctx.causal, generator=generator1, num_splits=1 if ctx.deterministic else 0, ) torch.cuda.current_stream().wait_stream(s) if rng_state0 is not None: torch.cuda.set_rng_state(cur_rng_state) return dqkv, None, None, None, None, None, None, None, None, None def flash_attn_unpadded_qkvpacked_func(qkv, cu_seqlens, max_seqlen, dropout_p, softmax_scale=None, causal=False, return_attn_probs=False, deterministic=False): """dropout_p should be set to 0.0 during evaluation Arguments: qkv: (total, 3, nheads, headdim), where total = total number of tokens in the batch. cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into qkv. max_seqlen: int. Maximum sequence length in the batch. dropout_p: float. Dropout probability. softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim). causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling). return_attn_probs: bool. Whether to return the attention probabilities. This option is for testing only. The returned probabilities are not guaranteed to be correct (they might not have the right scaling). deterministic: bool. Whether or not to ensure deterministic execution. Return: out: (total, nheads, headdim). softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen). The output of softmax (possibly with different scaling). It also encodes the dropout pattern (negative means that location was dropped, nonnegative means it was kept). """ return FlashAttnQKVPackedFunc.apply(qkv, cu_seqlens, max_seqlen, dropout_p, softmax_scale, causal, return_attn_probs, deterministic) def flash_attn_unpadded_kvpacked_func(q, kv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale=None, causal=False, return_attn_probs=False, deterministic=False): """dropout_p should be set to 0.0 during evaluation Arguments: q: (total_q, nheads, headdim), where total_q = total number of query tokens in the batch. kv: (total_k, 2, nheads, headdim), where total_k = total number of key tokens in the batch. cu_seqlens_q: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into q. cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into kv. max_seqlen_q: int. Maximum query sequence length in the batch. max_seqlen_k: int. Maximum key sequence length in the batch. dropout_p: float. Dropout probability. softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim). causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling). return_attn_probs: bool. Whether to return the attention probabilities. This option is for testing only. The returned probabilities are not guaranteed to be correct (they might not have the right scaling). deterministic: bool. Whether or not to ensure deterministic execution. Return: out: (total, nheads, headdim). softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen). The output of softmax (possibly with different scaling). It also encodes the dropout pattern (negative means that location was dropped, nonnegative means it was kept). """ return FlashAttnKVPackedFunc.apply(q, kv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, return_attn_probs, deterministic) def flash_attn_unpadded_func(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale=None, causal=False, return_attn_probs=False, deterministic=False): """dropout_p should be set to 0.0 during evaluation Arguments: q: (total_q, nheads, headdim), where total_q = total number of query tokens in the batch. k: (total_k, nheads, headdim), where total_k = total number of key tokens in the batch. v: (total_k, nheads, headdim), where total_k = total number of key tokens in the batch. cu_seqlens_q: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into q. cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into kv. max_seqlen_q: int. Maximum query sequence length in the batch. max_seqlen_k: int. Maximum key sequence length in the batch. dropout_p: float. Dropout probability. softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim). causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling). return_attn_probs: bool. Whether to return the attention probabilities. This option is for testing only. The returned probabilities are not guaranteed to be correct (they might not have the right scaling). deterministic: bool. Whether or not to ensure deterministic execution. Return: out: (total, nheads, headdim). softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen). The output of softmax (possibly with different scaling). It also encodes the dropout pattern (negative means that location was dropped, nonnegative means it was kept). """ return FlashAttnFunc.apply(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, dropout_p, softmax_scale, causal, return_attn_probs, deterministic) def flash_attn_unpadded_qkvpacked_split_func( qkv, cu_seqlens, max_seqlen0, max_seqlen1, batch_size0, dropout_p, softmax_scale=None, causal=False, return_attn_probs=False, deterministic=False): """ Split attention into 2 kernels running on 2 separate streams for performance reason: e.g., if the batch has some sequences of length <= 128 and some > 128, it might be faster to have one kernel dealing with seqlen <= 128 and one kernel for seqlen > 128. dropout_p should be set to 0.0 during evaluation. Arguments: qkv: (total, 3, nheads, headdim), where total = total number of tokens in the batch. cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into qkv. max_seqlen0: int. Maximum sequence length in 1st part of the batch. max_seqlen1: int. Maximum sequence length in 2nd part of the batch. batch_size0: int. Number of sequences in the 1st part of the batch. dropout_p: float. Dropout probability. softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim). causal: bool. Whether to apply causal attention mask (e.g., for auto-regressive modeling). return_attn_probs: bool. Whether to return the attention probabilities. This option is for testing only. The returned probabilities are not guaranteed to be correct (they might not have the right scaling). deterministic: bool. Whether or not to ensure deterministic execution. Return: out: (total, nheads, headdim). softmax_lse [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen). The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). S_dmask [optional, if return_attn_probs=True]: (batch_size, nheads, seqlen, seqlen). The output of softmax (possibly with different scaling). It also encodes the dropout pattern (negative means that location was dropped, nonnegative means it was kept). """ return FlashAttnQKVPackedSplitFunc.apply(qkv, cu_seqlens, max_seqlen0, max_seqlen1, batch_size0, dropout_p, softmax_scale, causal, return_attn_probs, deterministic) def flash_attn_func(qkv, cu_seqlens, dropout_p, max_s, softmax_scale=None, causal=False, return_attn_probs=False): """For backward-compatibility only, will remove soon. dropout_p should be set to 0.0 during evaluation """ return flash_attn_unpadded_qkvpacked_func(qkv, cu_seqlens, max_s, dropout_p, softmax_scale, causal, return_attn_probs)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/flash_attn_interface.py
# Inspired by https://github.com/NVIDIA/apex/blob/master/apex/transformer/tensor_parallel/cross_entropy.py # But we make it much faster: we compute the local loss and the LSE, and by exchanging the LSE and # the losses we can get the global loss. There's no need to do it step by step # (compute local max, exchange, compute exp, compute local sum, exchange, etc.) # The original xentropy interface is here: https://github.com/NVIDIA/apex/blob/master/apex/contrib/xentropy/softmax_xentropy.py import torch import torch.nn as nn import xentropy_cuda_lib # `all_gather_into_tensor` and `reduce_scatter_tensor` are new placeholders for # `_all_gather_base` and `_reduce_scatter_base`. They require the most recent # version of PyTorch. The following 2 lines are for backward compatibility with # older PyTorch. if "all_gather_into_tensor" not in dir(torch.distributed): torch.distributed.all_gather_into_tensor = torch.distributed._all_gather_base class SoftmaxCrossEntropyLossFn(torch.autograd.Function): @staticmethod def forward(ctx, logits, labels, smoothing=0.0, ignored_index=-100, inplace_backward=False, process_group=None): """ logits: (batch, vocab_size) labels: (batch,) If process_group is not None, we're doing Tensor Parallel: each process is responsible for one part of the vocab. The loss needs to be aggregated across processes. """ batch, vocab_size = logits.shape assert labels.shape == (batch,) world_size = 1 if process_group is None else torch.distributed.get_world_size(process_group) ctx.total_classes = world_size * vocab_size if world_size == 1: losses, lse = xentropy_cuda_lib.forward(logits, labels, smoothing) losses.masked_fill_(labels==ignored_index, 0) labels_local = labels else: rank = torch.distributed.get_rank(process_group) vocab_start_index, vocab_end_index = rank * vocab_size, (rank + 1) * vocab_size # Create a mask of valid vocab ids (1 means it needs to be masked). labels_mask = (labels < vocab_start_index) | (labels >= vocab_end_index) ignored_mask = labels == ignored_index labels_local = torch.where(ignored_mask, labels, labels - vocab_start_index) # For tensor parallel cross entropy with smoothing, we want to pass in the total number # of classes so that smoothing can be applied correctly. If total_classes=-1, use the # last dimension of the input tensor. losses, lse_local = xentropy_cuda_lib.forward(logits, labels_local, smoothing, world_size * vocab_size) assert lse_local.shape == (batch,) assert losses.shape == (batch,) losses.masked_fill_(ignored_mask, 0) # For labels == ignored_index, the loss is always 0. # If there's no smoothing, if labels are in the vocab of this partition, losses contains # lse_local - predicted logit, and 0 otherwise. # If there's smoothing=0.1, for labels in the vocab of this partition, losses contains # 0.9 * (lse_local - predicted logit) + 0.1 * (lse_local - sum logit / total_classes) # For labels not in the vocab of this partition, losses contains # 0.1 * (lse_local - sum logit / total_classes). lse_allgather = torch.empty(world_size, batch, dtype=lse_local.dtype, device=lse_local.device) torch.distributed.all_gather_into_tensor(lse_allgather, lse_local.contiguous(), group=process_group) handle_losses = torch.distributed.all_reduce( losses, op=torch.distributed.ReduceOp.SUM, group=process_group, async_op=True ) lse = torch.logsumexp(lse_allgather, dim=0) # If there's no smoothing, the total losses are lse_local - predicted_logit, # we just have to subtract the lse_local and add the lse (global). # If there's smoothing=0.1, the total losses are # 0.9 * (lse_local - predicted_logit) + 0.1 * (sum of all lse_local - sum logit / total_classes) # We want 0.9 * (lse - predicted_logit) + 0.1 * (lse - sum logit / total_classes). rank_per_sample = torch.div(labels, vocab_size, rounding_mode='floor') lse_local = lse_allgather[rank_per_sample, torch.arange(batch, device=lse_allgather.device)] handle_losses.wait() if smoothing == 0.0: losses += lse - lse_local else: losses += ((1 - smoothing) * (lse - lse_local) + smoothing * (lse - lse_allgather.sum(dim=0))) losses.masked_fill_(ignored_mask, 0) ctx.save_for_backward(logits, lse, labels_local) ctx.smoothing = smoothing ctx.ignored_index = ignored_index ctx.inplace_backward = inplace_backward return losses @staticmethod def backward(ctx, grad_loss): logits, lse, labels = ctx.saved_tensors grad_loss = grad_loss.contiguous() grad_loss.masked_fill_(labels==ctx.ignored_index, 0) grad_logits = xentropy_cuda_lib.backward(grad_loss, logits, lse, labels, ctx.smoothing, ctx.inplace_backward, ctx.total_classes) return grad_logits, None, None, None, None, None, None class CrossEntropyLoss(nn.Module): def __init__(self, ignore_index=-100, reduction='mean', label_smoothing=0.0, inplace_backward=False, process_group=None): super().__init__() if reduction not in ['mean', 'none']: raise NotImplementedError("Only support reduction = 'mean' or 'none'") self.ignore_index = ignore_index self.reduction = reduction self.label_smoothing = label_smoothing self.inplace_backward = inplace_backward self.process_group = process_group def forward(self, input, target): assert input.is_cuda and target.is_cuda # SoftmaxCrossEntropyLoss implicitly casts to float loss = SoftmaxCrossEntropyLossFn.apply( input, target, self.label_smoothing, self.ignore_index, self.inplace_backward, self.process_group ) if self.reduction == 'mean': return loss.sum() / (target != self.ignore_index).sum() else: return loss
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/losses/cross_entropy.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/losses/__init__.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/layers/__init__.py
# We use the same API as https://github.com/rwightman/pytorch-image-models/blob/v0.6.11/timm/models/layers/patch_embed.py # But we use nn.Linear instead of Conv2d and it's about 8x faster. from functools import partial import torch.nn as nn from torch import _assert from torch.nn.modules.utils import _pair from einops import rearrange try: from flash_attn.ops.fused_dense import FusedDense except ImportError: FusedDense = None class PatchEmbed(nn.Module): """ 2D Image to Patch Embedding """ def __init__( self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, norm_layer=None, flatten=True, bias=True, fused_bias_fc=False, ): super().__init__() img_size = _pair(img_size) patch_size = _pair(patch_size) self.img_size = img_size self.patch_size = patch_size self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1]) self.num_patches = self.grid_size[0] * self.grid_size[1] self.flatten = flatten if fused_bias_fc and FusedDense is None: raise ImportError('fused_dense is not installed') linear_cls = nn.Linear if not fused_bias_fc or not bias else FusedDense self.proj = linear_cls(in_chans * patch_size[0] * patch_size[1], embed_dim, bias=bias) self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity() def forward(self, x): _, _, H, W = x.shape _assert(H == self.img_size[0], f"Input image height ({H}) doesn't match model ({self.img_size[0]}).") _assert(W == self.img_size[1], f"Input image width ({W}) doesn't match model ({self.img_size[1]}).") x = self.proj(rearrange(x, 'b c (h p1) (w p2) -> b h w (c p1 p2)', p1=self.patch_size[0], p2=self.patch_size[1])) if self.flatten: x = rearrange(x, 'b h w c -> b (h w) c') x = self.norm(x) return x
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/layers/patch_embed.py
# Copyright (c) 2023, Tri Dao. from typing import Tuple import math import torch from einops import rearrange, repeat import rotary_emb def rotate_half(x, interleaved=False): if not interleaved: x1, x2 = x.chunk(2, dim=-1) return torch.cat((-x2, x1), dim=-1) else: x1, x2 = x[..., ::2], x[..., 1::2] return rearrange(torch.stack((-x2, x1), dim=-1), '... d two -> ... (d two)', two=2) def apply_rotary_emb_torch(x, cos, sin, interleaved=False): """ x: (batch_size, seqlen, nheads, headdim) cos, sin: (seqlen, rotary_dim / 2) """ ro_dim = cos.shape[-1] * 2 assert ro_dim <= x.shape[-1] cos = repeat(cos, 's d -> s 1 (2 d)') sin = repeat(sin, 's d -> s 1 (2 d)') return torch.cat([x[..., :ro_dim] * cos + rotate_half(x[..., :ro_dim], interleaved) * sin, x[..., ro_dim:]], dim=-1) class ApplyRotaryEmb(torch.autograd.Function): @staticmethod def forward(ctx, x, cos, sin, interleaved=False, inplace=False): """ x: (batch_size, seqlen, nheads, headdim) cos, sin: (seqlen, rotary_dim / 2) interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead of 1st half and 2nd half (GPT-NeoX style). rotary_dim must be <= headdim Apply rotary embedding to the first rotary_dim of x. """ batch, seqlen, nheads, headdim = x.shape rotary_seqlen, rotary_dim = cos.shape rotary_dim *= 2 assert rotary_dim <= headdim assert seqlen <= rotary_seqlen assert sin.shape == (rotary_seqlen, rotary_dim // 2) x_ro = x[..., :rotary_dim] x1, x2 = x_ro.chunk(2, dim=-1) if not interleaved else (x_ro[..., ::2], x_ro[..., 1::2]) out = torch.empty_like(x) if not inplace else x out_ro = out[..., :rotary_dim] if inplace: o1, o2 = x1, x2 else: o1, o2 = (out_ro.chunk(2, dim=-1) if not interleaved else (out_ro[..., ::2], out_ro[..., 1::2])) rotary_emb.apply_rotary(x1, x2, rearrange(cos[:seqlen], 's d -> s 1 d'), rearrange(sin[:seqlen], 's d -> s 1 d'), o1, o2, False) if not inplace and rotary_dim < headdim: out[..., rotary_dim:].copy_(x[..., rotary_dim:]) ctx.save_for_backward(cos, sin) ctx.interleaved = interleaved ctx.inplace = inplace return out if not inplace else x @staticmethod def backward(ctx, do): cos, sin = ctx.saved_tensors _, seqlen, _, headdim = do.shape rotary_dim = cos.shape[-1] rotary_dim *= 2 inplace = ctx.inplace do_ro = do[..., :rotary_dim] do1, do2 = (do_ro.chunk(2, dim=-1) if not ctx.interleaved else (do_ro[..., ::2], do_ro[..., 1::2])) dx = torch.empty_like(do) if not inplace else do if inplace: dx1, dx2 = do1, do2 else: dx_ro = dx[..., :rotary_dim] dx1, dx2 = (dx_ro.chunk(2, dim=-1) if not ctx.interleaved else (dx_ro[..., ::2], dx_ro[..., 1::2])) rotary_emb.apply_rotary(do1, do2, rearrange(cos[:seqlen], 's d -> s 1 d'), rearrange(sin[:seqlen], 's d -> s 1 d'), dx1, dx2, True) if not inplace and rotary_dim < headdim: dx[..., rotary_dim:].copy_(do[..., rotary_dim:]) return dx, None, None, None, None apply_rotary_emb_func = ApplyRotaryEmb.apply class ApplyRotaryEmbQKV_(torch.autograd.Function): @staticmethod def forward(ctx, qkv, cos, sin, cos_k=None, sin_k=None, interleaved=False): """ qkv: (batch_size, seqlen, 3, nheads, headdim) cos, sin: (seqlen, rotary_dim / 2) cos_k, sin_k: (seqlen, rotary_dim / 2), optional interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead of 1st half and 2nd half (GPT-NeoX style). rotary_dim must be <= headdim Apply rotary embedding *inplace* to the first rotary_dim of q and k. """ batch, seqlen, three, nheads, headdim = qkv.shape assert three == 3 rotary_seqlen, rotary_dim = cos.shape rotary_dim *= 2 assert rotary_dim <= headdim assert seqlen <= rotary_seqlen cos_k = cos if cos_k is None else cos_k sin_k = sin if sin_k is None else sin_k assert sin.shape == cos_k.shape == sin_k.shape == (rotary_seqlen, rotary_dim // 2) q_ro = qkv[:, :, 0, :, :rotary_dim] q1, q2 = q_ro.chunk(2, dim=-1) if not interleaved else (q_ro[..., ::2], q_ro[..., 1::2]) rotary_emb.apply_rotary(q1, q2, rearrange(cos[:seqlen], 's d -> s 1 d'), rearrange(sin[:seqlen], 's d -> s 1 d'), q1, q2, False) k_ro = qkv[:, :, 1, :, :rotary_dim] k1, k2 = k_ro.chunk(2, dim=-1) if not interleaved else (k_ro[..., ::2], k_ro[..., 1::2]) rotary_emb.apply_rotary(k1, k2, rearrange(cos_k[:seqlen], 's d -> s 1 d'), rearrange(sin_k[:seqlen], 's d -> s 1 d'), k1, k2, False) ctx.save_for_backward(cos, sin, cos_k, sin_k) ctx.interleaved = interleaved return qkv @staticmethod def backward(ctx, dqkv): cos, sin, cos_k, sin_k = ctx.saved_tensors _, seqlen, _, _, headdim = dqkv.shape rotary_dim = cos.shape[-1] rotary_dim *= 2 dq_ro = dqkv[:, :, 0, :, :rotary_dim] dq1, dq2 = (dq_ro.chunk(2, dim=-1) if not ctx.interleaved else (dq_ro[..., ::2], dq_ro[..., 1::2])) rotary_emb.apply_rotary(dq1, dq2, rearrange(cos[:seqlen], 's d -> s 1 d'), rearrange(sin[:seqlen], 's d -> s 1 d'), dq1, dq2, True) dk_ro = dqkv[:, :, 1, :, :rotary_dim] dk1, dk2 = (dk_ro.chunk(2, dim=-1) if not ctx.interleaved else (dk_ro[..., ::2], dk_ro[..., 1::2])) rotary_emb.apply_rotary(dk1, dk2, rearrange(cos_k[:seqlen], 's d -> s 1 d'), rearrange(sin_k[:seqlen], 's d -> s 1 d'), dk1, dk2, True) return dqkv, None, None, None, None, None apply_rotary_emb_qkv_ = ApplyRotaryEmbQKV_.apply class RotaryEmbedding(torch.nn.Module): """ The rotary position embeddings from RoFormer_ (Su et. al). A crucial insight from the method is that the query and keys are transformed by rotation matrices which depend on the relative positions. Other implementations are available in the Rotary Transformer repo_ and in GPT-NeoX_, GPT-NeoX was an inspiration .. _RoFormer: https://arxiv.org/abs/2104.09864 .. _repo: https://github.com/ZhuiyiTechnology/roformer .. _GPT-NeoX: https://github.com/EleutherAI/gpt-neox If scale_base is not None, this implements XPos (Sun et al., https://arxiv.org/abs/2212.10554). A recommended value for scale_base is 512: https://github.com/HazyResearch/flash-attention/issues/96 Reference: https://github.com/sunyt32/torchscale/blob/main/torchscale/component/xpos_relative_position.py """ def __init__(self, dim: int, base=10000, interleaved=False, scale_base=None, device=None): """ interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead of 1st half and 2nd half (GPT-NeoX style). """ super().__init__() # Generate and save the inverse frequency buffer (non trainable) inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, device=device, dtype=torch.float32) / dim)) self.register_buffer("inv_freq", inv_freq) self.interleaved = interleaved self.scale_base = scale_base scale = ((torch.arange(0, dim, 2, device=device, dtype=torch.float32) + 0.4 * dim) / (1.4 * dim) if scale_base is not None else None) self.register_buffer("scale", scale) self._seq_len_cached = 0 self._cos_cached = None self._sin_cached = None self._cos_k_cached = None self._sin_k_cached = None def _update_cos_sin_cache(self, x, seqlen_offset=0): """x: (batch, seqlen, nheads, headdim) or (batch, seqlen, 3, nheads, headdim) """ seqlen = x.shape[1] + seqlen_offset # Reset the tables if the sequence length has changed, # or if we're on a new device (possibly due to tracing for instance) if (seqlen > self._seq_len_cached or self._cos_cached.device != x.device or self._cos_cached.dtype != x.dtype): self._seq_len_cached = seqlen t = torch.arange(seqlen, device=x.device, dtype=self.inv_freq.dtype) # Don't do einsum, it converts fp32 to fp16 # freqs = torch.einsum("i,j->ij", t, self.inv_freq) freqs = torch.outer(t, self.inv_freq.to(device=t.device)) if self.scale is None: self._cos_cached = torch.cos(freqs).to(x.dtype) self._sin_cached = torch.sin(freqs).to(x.dtype) else: power = ((torch.arange(seqlen, dtype=self.scale.dtype, device=self.scale.device) - seqlen // 2) / self.scale_base) scale = self.scale.to(device=power.device) ** rearrange(power, 's -> s 1') # We want the multiplication by scale to happen in fp32 self._cos_cached = (torch.cos(freqs) * scale).to(x.dtype) self._sin_cached = (torch.sin(freqs) * scale).to(x.dtype) self._cos_k_cached = (torch.cos(freqs) / scale).to(x.dtype) self._sin_k_cached = (torch.sin(freqs) / scale).to(x.dtype) def forward(self, qkv: torch.Tensor, seqlen_offset: int = 0) -> Tuple[torch.Tensor, torch.Tensor]: """ qkv: (batch, seqlen, 3, nheads, headdim) seqlen_offset: can be used in generation where the qkv being passed in is only the last token in the batch. """ self._update_cos_sin_cache(qkv, seqlen_offset) if self.scale is None: return apply_rotary_emb_qkv_( qkv, self._cos_cached[seqlen_offset:], self._sin_cached[seqlen_offset:], None, None, self.interleaved ) else: return apply_rotary_emb_qkv_( qkv, self._cos_cached[seqlen_offset:], self._sin_cached[seqlen_offset:], self._cos_k_cached[seqlen_offset:], self._sin_k_cached[seqlen_offset:], self.interleaved )
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/layers/rotary.py
import torch from transformers.utils import WEIGHTS_NAME, WEIGHTS_INDEX_NAME from transformers.utils import is_remote_url from transformers.modeling_utils import load_state_dict from transformers.utils.hub import cached_file, get_checkpoint_shard_files def state_dict_from_pretrained(model_name, device=None, dtype=None): # If not fp32, then we don't want to load directly to the GPU mapped_device = 'cpu' if dtype not in [torch.float32, None] else device is_sharded = False resolved_archive_file = cached_file(model_name, WEIGHTS_NAME, _raise_exceptions_for_missing_entries=False) if resolved_archive_file is None: resolved_archive_file = cached_file(model_name, WEIGHTS_INDEX_NAME, _raise_exceptions_for_missing_entries=False) if resolved_archive_file is not None: is_sharded = True if resolved_archive_file is None: raise EnvironmentError(f"Model name {model_name} was not found.") if is_sharded: # resolved_archive_file becomes a list of files that point to the different # checkpoint shards in this case. resolved_archive_file, sharded_metadata = get_checkpoint_shard_files( model_name, resolved_archive_file ) state_dict = {} for sharded_file in resolved_archive_file: state_dict.update(torch.load(sharded_file, map_location=mapped_device)) else: state_dict = torch.load(cached_file(model_name, WEIGHTS_NAME), map_location=device) # Convert dtype before moving to GPU to save memory if dtype is not None: state_dict = {k: v.to(dtype=dtype) for k, v in state_dict.items()} state_dict = {k: v.to(device=device) for k, v in state_dict.items()} return state_dict
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/utils/pretrained.py
# Copyright (c) 2023, Tri Dao. # Adapted from https://github.com/NVIDIA/Megatron-LM/blob/0bb597b42c53355a567aba2a1357cc34b9d99ddd/megatron/text_generation/forward_step.py#L31 from typing import Optional, Union, Sequence, Callable import gc import time from dataclasses import dataclass, field from collections import namedtuple import torch from torch import Tensor from torch.profiler import profile, record_function, ProfilerActivity from einops import rearrange from transformers.generation import GreedySearchDecoderOnlyOutput, SampleDecoderOnlyOutput @dataclass class InferenceParams: """Inference parameters that are passed to the main model in order to efficienly calculate and store the context during inference.""" max_sequence_len: int max_batch_size: int sequence_len_offset: int = 0 batch_size_offset: int = 0 key_value_memory_dict: dict = field(default_factory=dict) fused_ft_kernel: bool = False lengths_per_sample: Optional[Tensor] = None # https://github.com/NVIDIA/Megatron-LM/blob/0bb597b42c53355a567aba2a1357cc34b9d99ddd/megatron/text_generation/sampling.py # https://github.com/huggingface/transformers/blob/a44985b41cfa2de48a5e1de7f1f93b7483da25d1/src/transformers/generation/logits_process.py#L170 def modify_logits_for_top_p_filtering(logits, top_p): """Set the logits for none top-p values to -inf.""" if top_p <= 0.0: return # First sort and calculate cumulative sum of probabilities. sorted_logits, sorted_indices = torch.sort(logits, descending=False) cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1) # Remove tokens with cumulative top_p above the threshold (token with 0 are kept) sorted_indices_to_remove = cumulative_probs <= (1 - top_p) # scatter sorted tensors to original indexing indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove) logits = logits.masked_fill(indices_to_remove, float('-inf')) def sample(logits, top_k=1, top_p=0.0, temperature=1.0): """Sample from top-k logits. Arguments: logits: Tensor of shape (batch_size, vocab_size) """ if top_k == 1: # Short-circuit for greedy decoding return logits.argmax(dim=-1) else: if top_p > 0.0: assert top_p <= 1.0, 'top-p should be in (0, 1].' if top_k > 0: top_k = min(top_k, logits.size(-1)) # Safety check logits_top, indices = torch.topk(logits, top_k, dim=-1) logits_top /= temperature modify_logits_for_top_p_filtering(logits_top, top_p) return indices[ torch.arange(indices.shape[0], device=indices.device), torch.multinomial(torch.softmax(logits_top, dim=-1), num_samples=1).squeeze(dim=-1) ] else: logits_top = logits / temperature modify_logits_for_top_p_filtering(logits_top, top_p) return torch.multinomial(torch.softmax(logits_top, dim=-1), num_samples=1).squeeze(dim=-1) def decode(input_ids, model, max_length, top_k=1, top_p=0.0, temperature=1.0, eos_token_id=None, teacher_outputs=None, vocab_size=None, tensor_parallel=1, fused_ft_kernel=False, cg=False, timing=False): """Decoding, either greedy or with top-k or top-p sampling. If top-k = 0, don't limit the number of candidates (pure sampling). Top-k and top-p can be used together. If top_k > 0 and top_p > 0, then top-k is applied first, then top-p. We assume that all sequences in the same batch have the same length. Arguments: input_ids: (batch, seq_len) max_length: int teacher_outputs (optional): (batch, seq_len). If provided, instead of sampling from the logits, the next token is taken from the teacher_outputs. Useful for testing. Returns: GreedySearchDecoderOnlyOutput or SampleDecoderOnlyOutput, with the following fields: sequences: (batch, max_length) scores: tuples of (batch, vocab_size) """ batch_size, seqlen_og = input_ids.shape teacher_output_len = teacher_outputs.shape[1] if teacher_outputs is not None else 0 if cg: assert fused_ft_kernel if not hasattr(model, '_decoding_cache'): model._decoding_cache = None model._decoding_cache = update_graph_cache( model, model._decoding_cache, batch_size, seqlen_og, max_length, tensor_parallel=tensor_parallel ) inference_params = model._decoding_cache.inference_params inference_params.max_sequence_len = max_length inference_params.max_batch_size = batch_size inference_params.sequence_len_offset = 0 else: inference_params = InferenceParams(max_sequence_len=max_length, max_batch_size=batch_size, fused_ft_kernel=fused_ft_kernel) scores = [] with torch.inference_mode(): if timing: if tensor_parallel > 1: torch.distributed.barrier() torch.cuda.synchronize() start = time.time() logits = model(input_ids, inference_params=inference_params).logits[:, -1] if vocab_size is not None: logits = logits[..., :vocab_size] scores.append(logits if not cg else logits.clone()) if teacher_outputs is None or teacher_output_len <= seqlen_og: next_token = sample(logits, top_k=top_k, top_p=top_p, temperature=temperature) else: next_token = teacher_outputs[:, seqlen_og] sequences = [next_token] inference_params.sequence_len_offset = seqlen_og while True: position_ids = torch.full((batch_size, 1), inference_params.sequence_len_offset, dtype=torch.long, device=input_ids.device) if not cg: logits = model(rearrange(next_token, 'b -> b 1'), position_ids=position_ids, inference_params=inference_params).logits[:, -1] else: logits = model._decoding_cache.run(rearrange(next_token, 'b -> b 1'), position_ids, inference_params.sequence_len_offset) if vocab_size is not None: logits = logits[..., :vocab_size] scores.append(logits if not cg else logits.clone()) if teacher_outputs is None or teacher_output_len <= inference_params.sequence_len_offset + 1: next_token = sample(logits, top_k=top_k, temperature=temperature) else: next_token = teacher_outputs[:, inference_params.sequence_len_offset + 1] sequences.append(next_token) inference_params.sequence_len_offset += 1 if eos_token_id is not None and (next_token == eos_token_id).all(): break if inference_params.sequence_len_offset >= max_length - 1: break if timing: if tensor_parallel > 1: torch.distributed.barrier() torch.cuda.synchronize() print(f'Prompt processing + decoding time: {(time.time() - start) * 1000:.0f}ms') output_cls = GreedySearchDecoderOnlyOutput if top_k == 1 else SampleDecoderOnlyOutput return output_cls( sequences=torch.cat([input_ids, torch.stack(sequences, dim=1)], dim=1), scores=tuple(scores) ) class GenerationMixin: def generate(self, input_ids, max_length, top_k=1, top_p=0.0, temperature=1.0, return_dict_in_generate=False, output_scores=False, **kwargs): output = decode(input_ids, self, max_length, top_k=top_k, top_p=top_p, temperature=temperature, **kwargs) if not output_scores: output.scores = None return output if return_dict_in_generate else output.sequences def allocate_kv_cache(max_batch_size, max_seqlen, nheads, headdim, layers: Union[int, Sequence], device, dtype=torch.float16): assert dtype in [torch.float16, torch.bfloat16, torch.float32] packsize = 4 if dtype == torch.float32 else 8 assert headdim % packsize == 0 k_cache_shape = (max_batch_size, nheads, headdim // packsize, max_seqlen, packsize) v_cache_shape = (max_batch_size, nheads, max_seqlen, headdim) if isinstance(layers, int): layers = range(layers) return {i: (torch.empty(k_cache_shape, device=device, dtype=dtype), torch.empty(v_cache_shape, device=device, dtype=dtype)) for i in layers} def seqlen_to_seqlen_type(seqlen: int) -> int: """Convert sequence length to a seqlen_type. This is used to determine which cuda graph to use. Arguments: seqlen: int """ return 0 if seqlen < 32 else (1 if seqlen < 2048 else 2) def seqlen_type_to_max_seqlen(seqlen_type: int) -> int: assert seqlen_type in [0, 1, 2] return 32 if seqlen_type == 0 else (2048 if seqlen_type == 1 else 2**32) @dataclass class DecodingCGCache: max_batch_size: int = 0 max_seqlen: int = 0 device = None dtype = None callables: dict = field(default_factory=dict) mempool = None inference_params: Optional[InferenceParams] = None run: Optional[Callable] = None @torch.inference_mode() def update_graph_cache(model, cache, batch_size, seqlen_og, max_seqlen, tensor_parallel=1, dtype=None, n_warmups=2): if cache is None: cache = DecodingCGCache() param_example = next(iter(model.parameters())) device = param_example.device if dtype is None: dtype = param_example.dtype if ((device, dtype) != (cache.device, cache.dtype) or batch_size > cache.max_batch_size or max_seqlen > cache.max_seqlen): # Invalidate the cache cache.callables = {} cache.mempool = None cache.inference_params = None gc.collect() cache.device, cache.dtype = device, dtype cache.max_batch_size, cache.max_seqlen = batch_size, max_seqlen headdim = getattr(model.config, 'head_dim', model.config.hidden_size // model.config.num_attention_heads) kv_cache = allocate_kv_cache( batch_size, max_seqlen, model.config.num_attention_heads // tensor_parallel, headdim, model.config.num_hidden_layers, device, dtype ) lengths_per_sample = torch.full((batch_size,), seqlen_og, dtype=torch.int32, device=device) cache.inference_params = InferenceParams( max_sequence_len=max_seqlen, max_batch_size=batch_size, sequence_len_offset=seqlen_og, key_value_memory_dict=kv_cache, fused_ft_kernel=True, lengths_per_sample=lengths_per_sample ) cache.mempool = torch.cuda.graphs.graph_pool_handle() for s_type in range(seqlen_to_seqlen_type(seqlen_og), seqlen_to_seqlen_type(max_seqlen) + 1): if s_type not in cache.callables: max_seqlen_ = min(max(seqlen_og, seqlen_type_to_max_seqlen(s_type)), max_seqlen) cache.callables[s_type] = capture_graph( model, cache.inference_params, batch_size, max_seqlen_, mempool=cache.mempool, n_warmups=n_warmups ) def dispatch(input_ids, position_ids, seqlen): return cache.callables[seqlen_to_seqlen_type(seqlen)](input_ids, position_ids, seqlen) cache.run = dispatch cache.inference_params.sequence_len_offset = 0 # Reset so it's not confusing return cache def capture_graph(model, inference_params, batch_size, max_seqlen, mempool=None, n_warmups=2): device = next(iter(model.parameters())).device input_ids = torch.full((batch_size, 1), 0, dtype=torch.long, device=device) position_ids = torch.full((batch_size, 1), 0, dtype=torch.long, device=device) sequence_len_offset_og = inference_params.sequence_len_offset # TD [2023-04-14]: important for correctness of the FT's attention kernel, as seqlen_cpu is # used to determine the size of smem. Hence seqlen_cpu must be >= lengths_per_sample. inference_params.sequence_len_offset = max_seqlen - 1 inference_params.lengths_per_sample[:] = max_seqlen - 1 # Warmup before capture s = torch.cuda.Stream() s.wait_stream(torch.cuda.current_stream()) with torch.cuda.stream(s): for _ in range(n_warmups): logits = model(input_ids, position_ids=position_ids, inference_params=inference_params).logits[:, -1] s.synchronize() # This might be needed for correctness if we run with NCCL_GRAPH_MIXING_SUPPORT=0, # which requires that graph launch and non-captured launch to not overlap (I think, # that's how I interpret the documentation). I'm not sure if this is required. if torch.distributed.is_initialized(): torch.distributed.barrier() torch.cuda.current_stream().wait_stream(s) # Captures the graph # To allow capture, automatically sets a side stream as the current stream in the context graph = torch.cuda.CUDAGraph() with torch.cuda.graph(graph, pool=mempool): logits = model(input_ids, position_ids=position_ids, inference_params=inference_params).logits[:, -1] def run(new_input_ids, new_position_ids, seqlen): inference_params.lengths_per_sample[:] = seqlen input_ids.copy_(new_input_ids) position_ids.copy_(new_position_ids) graph.replay() return logits inference_params.sequence_len_offset = sequence_len_offset_og return run
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/utils/generation.py
# Copyright (c) 2022, Tri Dao. """ Useful functions for writing test code. """ import torch import torch.utils.benchmark as benchmark def benchmark_forward(fn, *inputs, repeats=10, desc='', verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs): """ Use Pytorch Benchmark on the forward pass of an arbitrary function. """ if verbose: print(desc, '- Forward pass') def fn_amp(*inputs, **kwinputs): with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): fn(*inputs, **kwinputs) for _ in range(repeats): # warmup fn_amp(*inputs, **kwinputs) t = benchmark.Timer( stmt='fn_amp(*inputs, **kwinputs)', globals={'fn_amp': fn_amp, 'inputs': inputs, 'kwinputs': kwinputs}, num_threads=torch.get_num_threads(), ) m = t.timeit(repeats) if verbose: print(m) return t, m def benchmark_backward(fn, *inputs, grad=None, repeats=10, desc='', verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs): """ Use Pytorch Benchmark on the backward pass of an arbitrary function. """ if verbose: print(desc, '- Backward pass') with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): y = fn(*inputs, **kwinputs) if type(y) is tuple: y = y[0] if grad is None: grad = torch.randn_like(y) else: if grad.shape != y.shape: raise RuntimeError('Grad shape does not match output shape') for _ in range(repeats): # warmup y.backward(grad, retain_graph=True) t = benchmark.Timer( stmt='y.backward(grad, retain_graph=True)', globals={'y': y, 'grad': grad}, num_threads=torch.get_num_threads(), ) m = t.timeit(repeats) if verbose: print(m) return t, m def benchmark_combined(fn, *inputs, grad=None, repeats=10, desc='', verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs): """ Use Pytorch Benchmark on the forward+backward pass of an arbitrary function. """ if verbose: print(desc, '- Forward + Backward pass') def f(grad, *inputs, **kwinputs): with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): y = fn(*inputs, **kwinputs) if type(y) is tuple: y = y[0] if grad is None: grad = torch.randn_like(y) else: if grad.shape != y.shape: raise RuntimeError('Grad shape does not match output shape') y.backward(grad, retain_graph=True) for _ in range(repeats): # warmup f(grad, *inputs, **kwinputs) t = benchmark.Timer( stmt='f(grad, *inputs, **kwinputs)', globals={'f': f, 'fn': fn, 'inputs': inputs, 'grad': grad, 'kwinputs': kwinputs}, num_threads=torch.get_num_threads(), ) m = t.timeit(repeats) if verbose: print(m) return t, m def benchmark_all(fn, *inputs, grad=None, repeats=10, desc='', verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs): """ Use Pytorch Benchmark on the forward+backward pass of an arbitrary function. """ return ( benchmark_forward(fn, *inputs, repeats=repeats, desc=desc, verbose=verbose, amp=amp, amp_dtype=amp_dtype, **kwinputs), benchmark_backward(fn, *inputs, grad=grad, repeats=repeats, desc=desc, verbose=verbose, amp=amp, amp_dtype=amp_dtype, **kwinputs), benchmark_combined(fn, *inputs, grad=grad, repeats=repeats, desc=desc, verbose=verbose, amp=amp, amp_dtype=amp_dtype, **kwinputs), ) def pytorch_profiler(fn, *inputs, trace_filename=None, backward=False, amp=False, amp_dtype=torch.float16, cpu=False, verbose=True, **kwinputs): """ Wrap benchmark functions in Pytorch profiler to see CUDA information. """ if backward: with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): g = torch.randn_like(fn(*inputs, **kwinputs)) for _ in range(30): # Warm up with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): if backward: for x in inputs: if isinstance(x, torch.Tensor): x.grad = None # fn(*inputs, **kwinputs) if not backward else fn(*inputs, **kwinputs).backward(g) out = fn(*inputs, **kwinputs) # Backward should be done outside autocast if backward: out.backward(g) activities = ([torch.profiler.ProfilerActivity.CPU] if cpu else []) + [torch.profiler.ProfilerActivity.CUDA] with torch.profiler.profile( activities=activities, record_shapes=True, # profile_memory=True, with_stack=True, ) as prof: with torch.autocast(device_type='cuda', dtype=amp_dtype, enabled=amp): if backward: for x in inputs: if isinstance(x, torch.Tensor): x.grad = None out = fn(*inputs, **kwinputs) if backward: out.backward(g) if verbose: # print(prof.key_averages().table(sort_by="self_cuda_time_total", row_limit=50)) print(prof.key_averages().table(row_limit=50)) if trace_filename is not None: prof.export_chrome_trace(trace_filename) def benchmark_memory(fn, *inputs, desc='', verbose=True, **kwinputs): torch.cuda.empty_cache() torch.cuda.reset_peak_memory_stats() torch.cuda.synchronize() fn(*inputs, **kwinputs) torch.cuda.synchronize() mem = torch.cuda.max_memory_allocated() / ((2 ** 20) * 1000) if verbose: print(f'{desc} max memory: {mem}GB') torch.cuda.empty_cache() return mem
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/utils/benchmark.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/utils/__init__.py
from typing import Optional import torch from torch import Tensor from torch.distributed import ProcessGroup # `all_gather_into_tensor` and `reduce_scatter_tensor` are new placeholders for # `_all_gather_base` and `_reduce_scatter_base`. They require the most recent # version of PyTorch. The following 4 lines are for backward compatibility with # older PyTorch. if "all_gather_into_tensor" not in dir(torch.distributed): torch.distributed.all_gather_into_tensor = torch.distributed._all_gather_base if "reduce_scatter_tensor" not in dir(torch.distributed): torch.distributed.reduce_scatter_tensor = torch.distributed._reduce_scatter_base # Raw operation, does not support autograd, but does support async def all_gather_raw(input_: Tensor, process_group: ProcessGroup, async_op: bool = False): world_size = torch.distributed.get_world_size(process_group) output = torch.empty(world_size * input_.shape[0], *input_.shape[1:], dtype=input_.dtype, device=input_.device) handle = torch.distributed.all_gather_into_tensor(output, input_.contiguous(), group=process_group, async_op=async_op) return output, handle # Raw operation, does not support autograd, but does support async def reduce_scatter_raw(input_: Tensor, process_group: ProcessGroup, async_op: bool = False): world_size = torch.distributed.get_world_size(process_group) assert input_.shape[0] % world_size == 0 output = torch.empty(input_.shape[0] // world_size, *input_.shape[1:], dtype=input_.dtype, device=input_.device) handle = torch.distributed.reduce_scatter_tensor(output, input_.contiguous(), group=process_group, async_op=async_op) return output, handle # Raw operation, does not support autograd, but does support async def all_reduce_raw(input_: Tensor, process_group: ProcessGroup, async_op: bool = False): input_ = input_.contiguous() handle = torch.distributed.all_reduce(input_, group=process_group, async_op=async_op) return input_, handle class AllGatherFunc(torch.autograd.Function): """Gather the input from sequence parallel region and concatenate.""" @staticmethod def forward(ctx, input_: Tensor, process_group: ProcessGroup) -> Tensor: ctx.process_group = process_group output, _ = all_gather_raw(input_, process_group) return output @staticmethod def backward(ctx, grad_output: Tensor): grad_input, _ = reduce_scatter_raw(grad_output, ctx.process_group) return grad_input, None # Supports autograd, but does not support async all_gather = AllGatherFunc.apply class ReduceScatterFunc(torch.autograd.Function): """Reduce scatter the input from the sequence parallel region and concatenate.""" @staticmethod def forward(ctx, input_: Tensor, process_group: ProcessGroup) -> Tensor: ctx.process_group = process_group output, _ = reduce_scatter_raw(input_, process_group) return output @staticmethod def backward(ctx, grad_output: Tensor): grad_input, _ = all_gather_raw(grad_output, ctx.process_group) return grad_input, None # Supports autograd, but does not support async reduce_scatter = ReduceScatterFunc.apply class AllReduceFunc(torch.autograd.Function): """Gather the input from sequence parallel region and concatenate.""" @staticmethod def forward(ctx, input_: Tensor, process_group: ProcessGroup) -> Tensor: ctx.process_group = process_group output, _ = all_reduce_raw(input_, process_group) return output @staticmethod def backward(ctx, grad_output: Tensor): return grad_output, None # Supports autograd, but does not support async all_reduce = AllReduceFunc.apply def sync_shared_params(model: torch.nn.Module, process_group: ProcessGroup): # We want to iterate over parameters with _shared_params=True in the same order, # as different ranks might have different number of parameters (e.g., only rank 0 has bias). pamams_shared = {name: p for name, p in model.named_parameters() if getattr(p, '_shared_params', False)} for _, p in sorted(pamams_shared.items()): with torch.no_grad(): # Broadcast needs src to be global rank, not group rank torch.distributed.broadcast( p, src=torch.distributed.get_global_rank(process_group, 0), group=process_group ) # Ref: https://github.com/NVIDIA/Megatron-LM/blob/52e636888cccc41e931251c417a7181fc36de926/megatron/optimizer/optimizer.py#L256 def allreduce_sequence_parallel_grad(model: torch.nn.Module, process_group: ProcessGroup): # We want to iterate over parameters with _sequence_parallel=True in the same order, # as different ranks might have different number of parameters (e.g., only rank 0 has bias). params_seqparallel = {name: p for name, p in model.named_parameters() if getattr(p, '_sequence_parallel', False)} grads = [p.grad for _, p in sorted(params_seqparallel.items())] if grads: with torch.no_grad(): coalesced = torch._utils._flatten_dense_tensors(grads) torch.distributed.all_reduce(coalesced, group=process_group) for buf, synced in zip(grads, torch._utils._unflatten_dense_tensors(coalesced, grads)): buf.copy_(synced)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/utils/distributed.py
# Copyright (c) 2023, Tri Dao. import math import re from collections import OrderedDict import torch import torch.nn.functional as F from transformers import GPT2Config, GPTJConfig def remap_state_dict_hf_gptj(state_dict, config): def key_mapping_layers(key): return re.sub(r'^transformer.h.', 'transformer.layers.', key) state_dict = OrderedDict((key_mapping_layers(k), v) for k, v in state_dict.items()) # Word embedding def key_mapping_emb(key): return re.sub(r'^transformer.wte.', 'transformer.embeddings.word_embeddings.', key) state_dict = OrderedDict((key_mapping_emb(k), v) for k, v in state_dict.items()) word_embeddings = state_dict.pop('transformer.embeddings.word_embeddings.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) state_dict['transformer.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, vocab_size - word_embeddings.shape[0]) ) if getattr(config, 'tie_word_embeddings'): state_dict['lm_head.weight'] = state_dict['transformer.embeddings.word_embeddings.weight'] else: output_embeddings = state_dict.pop('lm_head.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. state_dict['lm_head.weight'] = F.pad( output_embeddings, (0, 0, 0, vocab_size - output_embeddings.shape[0]) ) output_embeddings_bias = state_dict.pop('lm_head.bias') state_dict['lm_head.bias'] = F.pad( output_embeddings_bias, (0, vocab_size - output_embeddings_bias.shape[0]) ) # LayerNorm def key_mapping_ln(key): return re.sub(r'^transformer.layers.(\d+).ln_1.', r'transformer.layers.\1.norm1.', key) state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP def key_mapping_mlp(key): key = re.sub(r'^transformer.layers.(\d+).mlp.fc_in.', r'transformer.layers.\1.mlp.fc1.', key) key = re.sub(r'^transformer.layers.(\d+).mlp.fc_out.', r'transformer.layers.\1.mlp.fc2.', key) return key state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention for l in range(config.n_layer): Wq = state_dict.pop(f'transformer.layers.{l}.attn.q_proj.weight') Wk = state_dict.pop(f'transformer.layers.{l}.attn.k_proj.weight') Wv = state_dict.pop(f'transformer.layers.{l}.attn.v_proj.weight') state_dict[f'transformer.layers.{l}.mixer.Wqkv.weight'] = torch.cat( [Wq, Wk, Wv], dim=0 ) # We don't store these biases state_dict.pop(f'transformer.layers.{l}.attn.bias') state_dict.pop(f'transformer.layers.{l}.attn.masked_bias') def key_mapping_attn(key): return re.sub(r'^transformer.layers.(\d+).attn.out_proj.', r'transformer.layers.\1.mixer.out_proj.', key) state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) return state_dict def gptj_config_to_gpt2_config(gptj_config: GPTJConfig) -> GPT2Config: headdim = gptj_config.n_embd // gptj_config.n_head return GPT2Config( vocab_size=gptj_config.vocab_size, n_positions=0, # No absolute position embedding n_embd=gptj_config.n_embd, n_layer=gptj_config.n_layer, n_head=gptj_config.n_head, n_inner=gptj_config.n_inner, activation_function=gptj_config.activation_function, resid_pdrop=gptj_config.resid_pdrop, embd_pdrop=gptj_config.embd_pdrop, attn_pdrop=gptj_config.attn_pdrop, layer_norm_epsilon=gptj_config.layer_norm_epsilon, initializer_range=gptj_config.initializer_range, bos_token_id=gptj_config.bos_token_id, eos_token_id=gptj_config.eos_token_id, # These are new arguments not in the original GPT2Config prenorm=True, parallel_block=True, parallel_block_tied_norm=True, rotary_emb_fraction=gptj_config.rotary_dim / headdim, rotary_emb_interleaved=True, tie_word_embeddings=False, qkv_proj_bias=False, out_proj_bias=False, lm_head_bias=True, )
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/gptj.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/__init__.py
# Copyright (c) 2023, Tri Dao. import math import re from collections import OrderedDict import torch import torch.nn.functional as F from transformers import GPT2Config, OPTConfig def remap_state_dict_hf_opt(state_dict, config): def key_mapping_model(key): key = re.sub(r'^model.decoder.', 'transformer.', key) # The OPT-350m model uses '^decoder' instead of '^model.decoder' key = re.sub(r'^decoder.', 'transformer.', key) return key state_dict = OrderedDict((key_mapping_model(k), v) for k, v in state_dict.items()) # Word embedding and position embedding def key_mapping_emb(key): key = re.sub(r'^transformer.embed_tokens.', 'transformer.embeddings.word_embeddings.', key) # The OPT-350m model uses has project_in and project_out key = re.sub(r'^transformer.project_in.', 'transformer.embeddings.project_in.', key) key = re.sub(r'^transformer.project_out.', 'project_out.', key) key = re.sub(r'^transformer.embed_positions.', 'transformer.embeddings.position_embeddings.', key) return key state_dict = OrderedDict((key_mapping_emb(k), v) for k, v in state_dict.items()) # OPT uses the first 2 indices of pos_emb for padding tokens pos_embeddings = state_dict.pop('transformer.embeddings.position_embeddings.weight') state_dict['transformer.embeddings.position_embeddings.weight'] = pos_embeddings[2:] word_embeddings = state_dict.pop('transformer.embeddings.word_embeddings.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) state_dict['transformer.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, vocab_size - word_embeddings.shape[0]) ) state_dict['lm_head.weight'] = state_dict['transformer.embeddings.word_embeddings.weight'] # LayerNorm def key_mapping_ln(key): key = re.sub(r'^transformer.final_layer_norm.', r'transformer.ln_f.', key) # The OPT-175B checkpoint calls this 'decoder.layer_norm' instead of 'decoder.final_layer_norm' key = re.sub(r'^transformer.layer_norm.', r'transformer.ln_f.', key) key = re.sub(r'^transformer.layers.(\d+).self_attn_layer_norm.', r'transformer.layers.\1.norm1.', key) key = re.sub(r'^transformer.layers.(\d+).final_layer_norm.', r'transformer.layers.\1.norm2.', key) return key state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP def key_mapping_mlp(key): return re.sub(r'^transformer.layers.(\d+).fc(1|2).', r'transformer.layers.\1.mlp.fc\2.', key) state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention for l in range(config.n_layer): Wq = state_dict.pop(f'transformer.layers.{l}.self_attn.q_proj.weight') Wk = state_dict.pop(f'transformer.layers.{l}.self_attn.k_proj.weight') Wv = state_dict.pop(f'transformer.layers.{l}.self_attn.v_proj.weight') bq = state_dict.pop(f'transformer.layers.{l}.self_attn.q_proj.bias') bk = state_dict.pop(f'transformer.layers.{l}.self_attn.k_proj.bias') bv = state_dict.pop(f'transformer.layers.{l}.self_attn.v_proj.bias') state_dict[f'transformer.layers.{l}.mixer.Wqkv.weight'] = torch.cat( [Wq, Wk, Wv], dim=0 ) state_dict[f'transformer.layers.{l}.mixer.Wqkv.bias'] = torch.cat( [bq, bk, bv], dim=0 ) def key_mapping_attn(key): return re.sub(r'^transformer.layers.(\d+).self_attn.out_proj.', r'transformer.layers.\1.mixer.out_proj.', key) state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) return state_dict def opt_config_to_gpt2_config(opt_config: OPTConfig) -> GPT2Config: assert opt_config.layerdrop == 0.0 assert opt_config.layer_norm_elementwise_affine word_embed_proj_dim = (None if opt_config.word_embed_proj_dim == opt_config.hidden_size else opt_config.word_embed_proj_dim) return GPT2Config( vocab_size=opt_config.vocab_size, n_positions=opt_config.max_position_embeddings, n_embd=opt_config.hidden_size, n_layer=opt_config.num_hidden_layers, n_head=opt_config.num_attention_heads, n_inner=opt_config.ffn_dim, activation_function=opt_config.activation_function, resid_pdrop=opt_config.dropout, # HF's implementation of OPT doesn't seem to have embedding dropout embd_pdrop=opt_config.dropout, attn_pdrop=opt_config.attention_dropout, initializer_range=opt_config.init_std, bos_token_id=opt_config.bos_token_id, eos_token_id=opt_config.eos_token_id, # These are new arguments not in the original GPT2Config prenorm=opt_config.do_layer_norm_before, word_embed_proj_dim=word_embed_proj_dim )
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/opt.py
# Copyright (c) 2022, Tri Dao. # Inspired by / adapted from https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py import math import re from functools import partial from copy import deepcopy from collections import OrderedDict import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.init import trunc_normal_ from torchvision.ops import StochasticDepth from einops import rearrange from timm.models.helpers import named_apply from flash_attn.layers.patch_embed import PatchEmbed from flash_attn.modules.mha import MHA from flash_attn.modules.mlp import Mlp, FusedMLP from flash_attn.modules.block import Block try: from flash_attn.ops.layer_norm import dropout_add_layer_norm except ImportError: dropout_add_layer_norm = None def create_mixer_cls(num_heads, qkv_bias, attn_drop, use_flash_attn, fused_bias_fc, cross_attn=False): mixer_cls = partial(MHA, num_heads=num_heads, cross_attn=cross_attn, bias=qkv_bias, dropout=attn_drop, fused_bias_fc=fused_bias_fc, use_flash_attn=use_flash_attn) return mixer_cls def create_mlp_cls(embed_dim, mlp_ratio, act_layer, fused_mlp): inner_dim = int(embed_dim * mlp_ratio) if not fused_mlp: mlp_cls = partial(Mlp, hidden_features=inner_dim, activation=act_layer()) else: mlp_cls = partial(FusedMLP, hidden_features=inner_dim) return mlp_cls def create_block(embed_dim, num_heads, mlp_ratio, qkv_bias, drop_rate, attn_drop_rate, drop_path1, drop_path2, norm_layer, act_layer, use_flash_attn, fused_bias_fc, fused_mlp, fused_dropout_add_ln, layer_idx=None, n_layer=None, last_layer_subset=False): mixer_cls = create_mixer_cls(num_heads, qkv_bias, attn_drop_rate, use_flash_attn, fused_bias_fc, cross_attn=(last_layer_subset and layer_idx == n_layer - 1)) mlp_cls = create_mlp_cls(embed_dim, mlp_ratio, act_layer, fused_mlp) # TD [2022-10-15]: Force residual in fp32 in case of DeepSpeed block = Block(embed_dim, mixer_cls, mlp_cls, norm_cls=norm_layer, prenorm=True, resid_dropout1=drop_rate, resid_dropout2=drop_rate, drop_path1=drop_path1, drop_path2=drop_path2, fused_dropout_add_ln=fused_dropout_add_ln, residual_in_fp32=True) return block class VisionTransformer(nn.Module): """ Vision Transformer A PyTorch impl of : `An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale` - https://arxiv.org/abs/2010.11929 """ def __init__( self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, global_pool='token', embed_dim=768, depth=12, num_heads=12, mlp_ratio=4., qkv_bias=True, init_values=None, class_token=True, no_embed_class=False, pre_norm=False, fc_norm=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0., weight_init='', embed_layer=PatchEmbed, norm_layer=None, act_layer=None, use_flash_attn=False, fused_bias_fc=False, fused_mlp=False, fused_dropout_add_ln=False, ): """ Args: img_size (int, tuple): input image size patch_size (int, tuple): patch size in_chans (int): number of input channels num_classes (int): number of classes for classification head global_pool (str): type of global pooling for final sequence (default: 'token') embed_dim (int): embedding dimension depth (int): depth of transformer num_heads (int): number of attention heads mlp_ratio (int): ratio of mlp hidden dim to embedding dim qkv_bias (bool): enable bias for qkv if True init_values: (float): layer-scale init values class_token (bool): use class token fc_norm (Optional[bool]): pre-fc norm after pool, set if global_pool == 'avg' if None (default: None) drop_rate (float): dropout rate attn_drop_rate (float): attention dropout rate drop_path_rate (float): stochastic depth rate weight_init (str): weight init scheme embed_layer (nn.Module): patch embedding layer norm_layer: (nn.Module): normalization layer act_layer: (nn.Module): MLP activation layer """ super().__init__() assert global_pool == 'token', 'Only support pooling with CLS token' assert class_token assert init_values is None, 'LayerScale is not supported yet' assert weight_init == '' assert fc_norm is None # pre_norm seems redundant, as there's a LayerNorm right at the start of each block, idk assert not pre_norm use_fc_norm = global_pool == 'avg' if fc_norm is None else fc_norm norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6) act_layer = act_layer or nn.GELU self.num_classes = num_classes self.global_pool = global_pool self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models self.num_prefix_tokens = 1 if class_token else 0 self.no_embed_class = no_embed_class patch_embed_extra_kwargs = ({'fused_bias_fc': fused_bias_fc} if embed_layer is PatchEmbed else {}) self.patch_embed = embed_layer( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, bias=not pre_norm, # disable bias if pre-norm is used (e.g. CLIP) **patch_embed_extra_kwargs ) num_patches = self.patch_embed.num_patches self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) if class_token else None embed_len = num_patches if no_embed_class else num_patches + self.num_prefix_tokens self.pos_embed = nn.Parameter(torch.randn(1, embed_len, embed_dim) * .02) dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule # We change the order of dropout, residual and layer norm: # Instead of LN -> Attn / MLP -> Dropout -> Add, we do: # Dropout -> Add -> LN -> Attn / MLP, returning both the residual branch (output of Add) and # the main branch (output of MLP). The model definition is unchanged, but the mapping of the # nn.Dropout probabilities are changed. # This is for performance reason: we can fuse dropout + add + layer_norm. self.blocks = nn.ModuleList([create_block( embed_dim, num_heads, mlp_ratio, qkv_bias, drop_rate, attn_drop_rate, drop_path1=dpr[i-1] if i > 0 else 0., drop_path2=dpr[i], norm_layer=norm_layer, act_layer=act_layer, use_flash_attn=use_flash_attn, fused_bias_fc=fused_bias_fc, fused_mlp=fused_mlp, fused_dropout_add_ln=fused_dropout_add_ln, layer_idx=i, n_layer=depth, last_layer_subset=(global_pool == 'token') ) for i in range(depth)]) self.dropout = nn.Dropout(p=drop_rate) self.drop_path = StochasticDepth(p=dpr[-1], mode='row') self.norm = norm_layer(embed_dim) self.fused_dropout_add_ln = fused_dropout_add_ln if self.fused_dropout_add_ln and dropout_add_layer_norm is None: raise ImportError('dropout_add_layer_norm is not installed') # Classifier Head self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity() self.init_weights(weight_init) def init_weights(self, mode=''): assert mode == '' trunc_normal_(self.pos_embed, std=.02) if self.cls_token is not None: nn.init.normal_(self.cls_token, std=1e-6) named_apply(init_weights_vit_timm, self) def _init_weights(self, m): # this fn left here for compat with downstream users init_weights_vit_timm(m) @torch.jit.ignore def no_weight_decay(self): return {'pos_embed', 'cls_token'} def _pos_embed(self, x): if self.no_embed_class: # deit-3, updated JAX (big vision) # position embedding does not overlap with class token, add then concat x = x + self.pos_embed if self.cls_token is not None: x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) else: # original timm, JAX, and deit vit impl # pos_embed has entry for class token, concat then add if self.cls_token is not None: x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) x = x + self.pos_embed return x def forward_features(self, x, all_tokens=True): """ If all_tokens==False and self.global_pool == 'token', we only return the features for the cls token. """ x = self.patch_embed(x) hidden_states = self._pos_embed(x) residual = None if self.global_pool != 'token' or all_tokens: # if True: for block in self.blocks: hidden_states, residual = block(hidden_states, residual) else: for block in self.blocks[:-1]: hidden_states, residual = block(hidden_states, residual) # For the last layer, we only want the 1st token of the output. So we do cross-attention # where the query is the 1st token and the key/value is the whole sequence. hidden_states, residual = self.blocks[-1](hidden_states, residual, mixer_subset=slice(0, 1)) if not self.fused_dropout_add_ln: residual = self.drop_path(self.dropout(hidden_states)) + residual hidden_states = self.norm(residual.to(dtype=self.norm.weight.dtype)) else: if self.drop_path.p == 0 or not self.training: rowscale = None else: rowscale = self.drop_path(torch.ones( hidden_states.shape[:-1], device=hidden_states.device, dtype=hidden_states.dtype) ) # Set prenorm=False here since we don't need to the residual hidden_states = dropout_add_layer_norm( hidden_states, residual, self.norm.weight, self.norm.bias, self.dropout.p if self.training else 0.0, self.norm.eps, rowscale=rowscale, prenorm=False, residual_in_fp32=True ) return hidden_states def forward_head(self, x, pre_logits: bool = False): if self.global_pool: x = x[:, self.num_prefix_tokens:].mean(dim=1) if self.global_pool == 'avg' else x[:, 0] return x if pre_logits else self.head(x) def forward(self, x): x = self.forward_features(x, all_tokens=False) x = self.forward_head(x) return x def load_state_dict(self, state_dict, strict=True): patch_embed_weight = state_dict['patch_embed.proj.weight'] if patch_embed_weight.dim() == 4: # convert from Conv2d to Linear state_dict['patch_embed.proj.weight'] = rearrange(patch_embed_weight, 'o c h w -> o (c h w)') def key_mapping_attn(key): key = re.sub(r'^blocks.(\d+).attn.qkv.', r'blocks.\1.mixer.Wqkv.', key) key = re.sub(r'^blocks.(\d+).attn.proj.', r'blocks.\1.mixer.out_proj.', key) return key state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) n_layer = len(self.blocks) # Convert from Wqkv to Wq and Wkv for cross attention (last layer) if (self.blocks[-1].mixer.cross_attn and f'blocks.{n_layer - 1}.mixer.Wqkv.weight' in state_dict): Wqkv = state_dict.pop(f'blocks.{n_layer - 1}.mixer.Wqkv.weight') bqkv = state_dict.pop(f'blocks.{n_layer - 1}.mixer.Wqkv.bias') state_dict[f'blocks.{n_layer - 1}.mixer.Wq.weight'] = Wqkv[:self.embed_dim] state_dict[f'blocks.{n_layer - 1}.mixer.Wkv.weight'] = Wqkv[self.embed_dim:] state_dict[f'blocks.{n_layer - 1}.mixer.Wq.bias'] = bqkv[:self.embed_dim] state_dict[f'blocks.{n_layer - 1}.mixer.Wkv.bias'] = bqkv[self.embed_dim:] return super().load_state_dict(state_dict, strict=strict) def init_weights_vit_timm(module: nn.Module, name: str = ''): """ ViT weight initialization, original timm impl (for reproducibility) """ if isinstance(module, nn.Linear): trunc_normal_(module.weight, std=.02) if module.bias is not None: nn.init.zeros_(module.bias) elif hasattr(module, 'init_weights'): module.init_weights() def vit_base_patch16_224(pretrained=False, **kwargs): """ ViT-Base (ViT-B/16) from original paper (https://arxiv.org/abs/2010.11929). ImageNet-1k weights fine-tuned from in21k @ 224x224, source https://github.com/google-research/vision_transformer. """ assert not pretrained model_kwargs = dict(patch_size=16, embed_dim=768, depth=12, num_heads=12, **kwargs) model = VisionTransformer(**model_kwargs) return model
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/vit.py
# Copyright (c) 2022, Tri Dao. # This BERT implementation is based on our MLPerf 2.0 and MLPerf 2.1 BERT implementation. # https://github.com/mlcommons/training_results_v2.0/blob/main/HazyResearch/benchmarks/bert/implementations/pytorch/modeling.py # https://github.com/mlcommons/training_results_v2.1/blob/main/Azure-HazyResearch/benchmarks/bert/implementations/ND96amsr_A100_v4/modeling.py # Inspired by https://github.com/huggingface/transformers/blob/main/src/transformers/models/bert/modeling_bert.py import re import logging from functools import partial from collections.abc import Sequence from collections import OrderedDict import torch import torch.nn as nn import torch.nn.functional as F from transformers import BertConfig from transformers.models.bert.modeling_bert import BaseModelOutputWithPoolingAndCrossAttentions from transformers.models.bert.modeling_bert import BertForPreTrainingOutput from einops import rearrange from flash_attn.modules.mha import MHA from flash_attn.modules.mlp import Mlp, FusedMLP from flash_attn.modules.block import Block from flash_attn.modules.embedding import BertEmbeddings from flash_attn.bert_padding import unpad_input, pad_input from flash_attn.bert_padding import index_first_axis, index_first_axis_residual from flash_attn.utils.pretrained import state_dict_from_pretrained try: from flash_attn.ops.fused_dense import FusedDense except ImportError: FusedDense = None try: from flash_attn.ops.layer_norm import dropout_add_layer_norm, layer_norm except ImportError: dropout_add_layer_norm, layer_norm = None, None try: from flash_attn.losses.cross_entropy import CrossEntropyLoss except ImportError: CrossEntropyLoss = None logger = logging.getLogger(__name__) def create_mixer_cls(config, cross_attn=False, return_residual=False): use_flash_attn = getattr(config, 'use_flash_attn', False) fused_bias_fc = getattr(config, 'fused_bias_fc', False) mixer_cls = partial(MHA, num_heads=config.num_attention_heads, cross_attn=cross_attn, dropout=config.attention_probs_dropout_prob, causal=False, fused_bias_fc=fused_bias_fc, use_flash_attn=use_flash_attn, return_residual=return_residual) return mixer_cls def create_mlp_cls(config, layer_idx=None, return_residual=False): inner_dim = config.intermediate_size fused_mlp = getattr(config, 'fused_mlp', False) if fused_mlp: assert config.hidden_act in ['gelu_new', 'gelu_fast'], ('fused_mlp only ' 'supports approximate gelu') if not fused_mlp: approximate = 'tanh' if config.hidden_act in ['gelu_new', 'gelu_fast'] else 'none' mlp_cls = partial(Mlp, hidden_features=inner_dim, activation=partial(F.gelu, approximate=approximate), return_residual=return_residual) else: if FusedMLP is None: raise ImportError('fused_dense is not installed') mlp_checkpoint_lvl = getattr(config, 'mlp_checkpoint_lvl', 0) # mlp_checkpoint_lvl could be a list, which contains the checkpoint_lvl for each layer if isinstance(mlp_checkpoint_lvl, Sequence): assert layer_idx is not None mlp_checkpoint_lvl = mlp_checkpoint_lvl[layer_idx] mlp_cls = partial(FusedMLP, hidden_features=inner_dim, checkpoint_lvl=mlp_checkpoint_lvl, return_residual=return_residual) return mlp_cls def create_block(config, layer_idx=None): last_layer_subset = getattr(config, 'last_layer_subset', False) cross_attn=last_layer_subset and layer_idx == config.num_hidden_layers - 1 # TD [2022-12-19]: For cross attention (last layer), we actually want to return the # residual x_kv, not residual x. But it's annoying to change the API (and it only affects # one layer) so we just choose not to return residual in this case. return_residual = not cross_attn mixer_cls = create_mixer_cls(config, cross_attn, return_residual=return_residual) mlp_cls = create_mlp_cls(config, layer_idx, return_residual=return_residual) norm_cls = partial(nn.LayerNorm, eps=config.layer_norm_eps) block = Block(config.hidden_size, mixer_cls, mlp_cls, norm_cls=norm_cls, prenorm=False, resid_dropout1=config.hidden_dropout_prob, resid_dropout2=config.hidden_dropout_prob, fused_dropout_add_ln=getattr(config, 'fused_dropout_add_ln', False), return_residual=return_residual) return block # https://github.com/huggingface/transformers/blob/7032e0203262ebb2ebf55da8d2e01f873973e835/src/transformers/models/bert/modeling_bert.py#L748 def _init_weights(module, initializer_range=0.02): if isinstance(module, nn.Linear): nn.init.normal_(module.weight, std=initializer_range) if module.bias is not None: nn.init.zeros_(module.bias) elif isinstance(module, nn.Embedding): nn.init.normal_(module.weight, std=initializer_range) if module.padding_idx is not None: nn.init.zeros_(module.weight[module.padding_idx]) class BertEncoder(nn.Module): def __init__(self, config: BertConfig): super().__init__() self.use_flash_attn = getattr(config, 'use_flash_attn', False) self.layers = nn.ModuleList([create_block(config, layer_idx=i) for i in range(config.num_hidden_layers)]) def forward(self, hidden_states, key_padding_mask=None, subset_mask=None): """If subset_mask is not None, we only want output for the subset of the sequence. This means that we only compute the last layer output for these tokens. subset_mask: (batch, seqlen), dtype=torch.bool """ if key_padding_mask is None or not self.use_flash_attn: mixer_kwargs = ({'key_padding_mask': key_padding_mask} if key_padding_mask is not None else None) for layer in self.layers: hidden_states = layer(hidden_states, mixer_kwargs=mixer_kwargs) if subset_mask is not None: hidden_states = hidden_states[subset_mask] else: batch, seqlen = hidden_states.shape[:2] hidden_states, indices, cu_seqlens, max_seqlen_in_batch = unpad_input( hidden_states, key_padding_mask ) mixer_kwargs = {'cu_seqlens': cu_seqlens, 'max_seqlen': max_seqlen_in_batch} if subset_mask is None: for layer in self.layers: hidden_states = layer(hidden_states, mixer_kwargs=mixer_kwargs) hidden_states = pad_input(hidden_states, indices, batch, seqlen) else: for layer in self.layers[:-1]: hidden_states = layer(hidden_states, mixer_kwargs=mixer_kwargs) if key_padding_mask is not None: subset_idx = torch.nonzero(subset_mask[key_padding_mask], as_tuple=False).flatten() subset_seqlens = (subset_mask & key_padding_mask).sum(dim=-1, dtype=torch.int32) subset_cu_seqlens = F.pad(torch.cumsum(subset_seqlens, dim=0, dtype=torch.torch.int32), (1, 0)) else: subset_idx = torch.nonzero(subset_mask, as_tuple=False).flatten() subset_seqlens = subset_mask.sum(dim=-1, dtype=torch.int32) subset_cu_seqlens = F.pad(torch.cumsum(subset_seqlens, dim=0, dtype=torch.torch.int32), (1, 0)) hidden_states_subset, hidden_states = index_first_axis_residual( hidden_states, subset_idx ) # It's ok to set max_seqlen_q to be much larger mixer_kwargs = {'x_kv': hidden_states, 'cu_seqlens': subset_cu_seqlens, 'max_seqlen': max_seqlen_in_batch, 'cu_seqlens_k': cu_seqlens, 'max_seqlen_k': max_seqlen_in_batch} hidden_states = self.layers[-1](hidden_states_subset, mixer_kwargs=mixer_kwargs) return hidden_states class BertPooler(nn.Module): def __init__(self, config): super().__init__() fused_bias_fc = getattr(config, 'fused_bias_fc', False) if fused_bias_fc and FusedDense is None: raise ImportError('fused_dense is not installed') linear_cls = nn.Linear if not fused_bias_fc else FusedDense self.dense = linear_cls(config.hidden_size, config.hidden_size) self.activation = nn.Tanh() def forward(self, hidden_states, pool=True): # We "pool" the model by simply taking the hidden state corresponding # to the first token. first_token_tensor = hidden_states[:, 0] if pool else hidden_states pooled_output = self.dense(first_token_tensor) pooled_output = self.activation(pooled_output) return pooled_output class BertPredictionHeadTransform(nn.Module): def __init__(self, config): super().__init__() fused_bias_fc = getattr(config, 'fused_bias_fc', False) if fused_bias_fc and FusedDense is None: raise ImportError('fused_dense is not installed') self.fused_dropout_add_ln = getattr(config, 'fused_dropout_add_ln', False) if self.fused_dropout_add_ln and layer_norm is None: raise ImportError('dropout_add_layer_norm is not installed') linear_cls = nn.Linear if not fused_bias_fc else FusedDense self.dense = linear_cls(config.hidden_size, config.hidden_size) approximate = 'tanh' if config.hidden_act in ['gelu_new', 'gelu_fast'] else 'none' self.transform_act_fn = nn.GELU(approximate=approximate) self.layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.dense(hidden_states) hidden_states = self.transform_act_fn(hidden_states) if not self.fused_dropout_add_ln: hidden_states = self.layer_norm(hidden_states) else: hidden_states = layer_norm(hidden_states, self.layer_norm.weight, self.layer_norm.bias, self.layer_norm.eps) return hidden_states class BertLMPredictionHead(nn.Module): def __init__(self, config): super().__init__() fused_bias_fc = getattr(config, 'fused_bias_fc', False) if fused_bias_fc and FusedDense is None: raise ImportError('fused_dense is not installed') linear_cls = nn.Linear if not fused_bias_fc else FusedDense self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = linear_cls(config.hidden_size, config.vocab_size, bias=True) def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states class BertPreTrainingHeads(nn.Module): def __init__(self, config): super().__init__() self.predictions = BertLMPredictionHead(config) self.seq_relationship = nn.Linear(config.hidden_size, 2) def forward(self, sequence_output, pooled_output): prediction_scores = self.predictions(sequence_output) seq_relationship_score = self.seq_relationship(pooled_output) return prediction_scores, seq_relationship_score class BertPreTrainedModel(nn.Module): """ An abstract class to handle weights initialization and a simple interface for dowloading and loading pretrained models. """ def __init__(self, config, *inputs, **kwargs): super().__init__() if not isinstance(config, BertConfig): raise ValueError( "Parameter config in `{}(config)` should be an instance of class `BertConfig`. " "To create a model from a Google pretrained model use " "`model = {}.from_pretrained(PRETRAINED_MODEL_NAME)`".format( self.__class__.__name__, self.__class__.__name__ )) self.config = config @classmethod def from_pretrained(cls, model_name, config, *inputs, **kwargs): """ Instantiate a BertPreTrainedModel from a pre-trained model file or a pytorch state dict. Download and cache the pre-trained model file if needed. Params: pretrained_model_name_or_path: either: - a path or url to a pretrained model archive containing: . `bert_config.json` a configuration file for the model . `pytorch_model.bin` a PyTorch dump of a BertForPretraining instance - a path or url to a pretrained model archive containing: . `bert_config.json` a configuration file for the model . `model.chkpt` a TensorFlow checkpoint *inputs, **kwargs: additional input for the specific Bert class (ex: num_labels for BertForSequenceClassification) """ # Instantiate model. model = cls(config, *inputs, **kwargs) load_return = model.load_state_dict(remap_state_dict(state_dict_from_pretrained(model_name), config), strict=False) logger.info(load_return) return model class BertModel(BertPreTrainedModel): def __init__(self, config: BertConfig, add_pooling_layer=True): super().__init__(config) self.pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) if config.vocab_size % self.pad_vocab_size_multiple != 0: config.vocab_size += (self.pad_vocab_size_multiple - (config.vocab_size % self.pad_vocab_size_multiple)) self.fused_dropout_add_ln = getattr(config, 'fused_dropout_add_ln', False) if self.fused_dropout_add_ln and layer_norm is None: raise ImportError('dropout_add_layer_norm is not installed') assert config.position_embedding_type == 'absolute' assert config.hidden_act in ['gelu', 'gelu_new', 'gelu_fast'] self.embeddings = BertEmbeddings(config.hidden_size, config.vocab_size, config.max_position_embeddings, config.type_vocab_size, padding_idx=config.pad_token_id) self.emb_drop = nn.Dropout(config.hidden_dropout_prob) self.emb_ln = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) self.encoder = BertEncoder(config) self.pooler = BertPooler(config) if add_pooling_layer else None self.apply(partial(_init_weights, initializer_range=config.initializer_range)) def forward(self, input_ids, position_ids=None, token_type_ids=None, attention_mask=None, masked_tokens_mask=None): """If masked_tokens_mask is not None (i.e. last_layer_subset == True in BertForPreTraining), we only want the output for the masked tokens. This means that we only compute the last layer output for these tokens. masked_tokens_mask: (batch, seqlen), dtype=torch.bool """ hidden_states = self.embeddings(input_ids, position_ids=position_ids, token_type_ids=token_type_ids) # TD [2022-12:18]: Don't need to force residual in fp32 # BERT puts embedding LayerNorm before embedding dropout. if not self.fused_dropout_add_ln: hidden_states = self.emb_ln(hidden_states) else: hidden_states = layer_norm(hidden_states, self.emb_ln.weight, self.emb_ln.bias, self.emb_ln.eps) hidden_states = self.emb_drop(hidden_states) if masked_tokens_mask is not None: batch_size, seqlen = input_ids.shape[:2] # We also need the first column for the CLS token first_col_mask = torch.zeros(batch_size, seqlen, dtype=torch.bool, device=input_ids.device) first_col_mask[:, 0] = True subset_mask = masked_tokens_mask | first_col_mask else: subset_mask = None sequence_output = self.encoder(hidden_states, key_padding_mask=attention_mask, subset_mask=subset_mask) if masked_tokens_mask is None: pooled_output = self.pooler(sequence_output) if self.pooler is not None else None else: # TD [2022-03-01]: the indexing here is very tricky. if attention_mask is not None: subset_idx = subset_mask[attention_mask] pool_input = sequence_output[first_col_mask[attention_mask][subset_idx]] sequence_output = sequence_output[masked_tokens_mask[attention_mask][subset_idx]] else: pool_input = sequence_output[first_col_mask[subset_mask]] sequence_output = sequence_output[masked_tokens_mask[subset_mask]] pooled_output = (self.pooler(pool_input, pool=False) if self.pooler is not None else None) return BaseModelOutputWithPoolingAndCrossAttentions( last_hidden_state=sequence_output, pooler_output=pooled_output, ) class BertForPreTraining(BertPreTrainedModel): def __init__(self, config: BertConfig): super().__init__(config) # If dense_seq_output, we only need to pass the hidden states for the masked out tokens # (around 15%) to the classifier heads. self.dense_seq_output = getattr(config, 'dense_seq_output', False) # If last_layer_subset, we only need the compute the last layer for a subset of tokens # (e.g., the tokens we need to compute the masked LM loss and the next-sentence prediction). self.last_layer_subset = getattr(config, 'last_layer_subset', False) if self.last_layer_subset: assert self.dense_seq_output, 'last_layer_subset requires dense_seq_output' use_xentropy = getattr(config, 'use_xentropy', False) if use_xentropy and CrossEntropyLoss is None: raise ImportError('xentropy_cuda is not installed') loss_cls = (nn.CrossEntropyLoss if not use_xentropy else partial(CrossEntropyLoss, inplace_backward=True)) self.bert = BertModel(config) self.cls = BertPreTrainingHeads(config) self.mlm_loss = loss_cls(ignore_index=0) self.nsp_loss = loss_cls(ignore_index=-1) # Initialize weights and apply final processing self.apply(partial(_init_weights, initializer_range=config.initializer_range)) self.tie_weights() def tie_weights(self): self.cls.predictions.decoder.weight = self.bert.embeddings.word_embeddings.weight def forward(self, input_ids, position_ids=None, token_type_ids=None, attention_mask=None, labels=None, next_sentence_label=None): """ If labels are provided, they must be 0 for masked out tokens (as specified in the attention mask). Outputs: if `labels` and `next_sentence_label` are not `None`: Outputs the total_loss which is the sum of the masked language modeling loss and the next sentence classification loss. if `labels` or `next_sentence_label` is `None`: Outputs a tuple comprising - the masked language modeling logits of shape [batch_size, sequence_length, vocab_size], and - the next sentence classification logits of shape [batch_size, 2]. """ masked_tokens_mask = labels > 0 if (self.last_layer_subset and labels is not None) else None outputs = self.bert( input_ids, position_ids=position_ids, token_type_ids=token_type_ids, attention_mask=attention_mask.bool() if attention_mask is not None else None, masked_tokens_mask=masked_tokens_mask ) sequence_output, pooled_output = outputs.last_hidden_state, outputs.pooler_output if self.dense_seq_output and labels is not None: masked_token_idx = torch.nonzero(labels.flatten() > 0, as_tuple=False).flatten() if not self.last_layer_subset: sequence_output = index_first_axis(rearrange(sequence_output, 'b s d -> (b s) d'), masked_token_idx) prediction_scores, seq_relationship_score = self.cls(sequence_output, pooled_output) total_loss = None if labels is not None and next_sentence_label is not None: if self.dense_seq_output and labels is not None: # prediction_scores are already flattened masked_lm_loss = self.mlm_loss(prediction_scores, labels.flatten()[masked_token_idx]) else: masked_lm_loss = self.mlm_loss(rearrange(prediction_scores, '... v -> (...) v'), rearrange(labels, '... -> (...)')) next_sentence_loss = self.nsp_loss(rearrange(seq_relationship_score, '... t -> (...) t'), rearrange(next_sentence_label, '... -> (...)')) total_loss = masked_lm_loss.float() + next_sentence_loss.float() return BertForPreTrainingOutput( loss=total_loss, prediction_logits=prediction_scores, seq_relationship_logits=seq_relationship_score, ) def remap_state_dict(state_dict, config): # LayerNorm def key_mapping_ln_gamma_beta(key): key = re.sub(r'LayerNorm.gamma$', 'LayerNorm.weight', key) key = re.sub(r'LayerNorm.beta$', 'LayerNorm.bias', key) return key state_dict = OrderedDict((key_mapping_ln_gamma_beta(k), v) for k, v in state_dict.items()) # Layers def key_mapping_layers(key): return re.sub(r'^bert.encoder.layer.', 'bert.encoder.layers.', key) state_dict = OrderedDict((key_mapping_layers(k), v) for k, v in state_dict.items()) # LayerNorm def key_mapping_ln(key): key = re.sub(r'^bert.embeddings.LayerNorm.', 'bert.emb_ln.', key) key = re.sub(r'^bert.encoder.layers.(\d+).attention.output.LayerNorm.(weight|bias)', r'bert.encoder.layers.\1.norm1.\2', key) key = re.sub(r'^bert.encoder.layers.(\d+).output.LayerNorm.(weight|bias)', r'bert.encoder.layers.\1.norm2.\2', key) key = re.sub(r'^cls.predictions.transform.LayerNorm.(weight|bias)', r'cls.predictions.transform.layer_norm.\1', key) return key state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP def key_mapping_mlp(key): key = re.sub(r'^bert.encoder.layers.(\d+).intermediate.dense.(weight|bias)', r'bert.encoder.layers.\1.mlp.fc1.\2', key) key = re.sub(r'^bert.encoder.layers.(\d+).output.dense.(weight|bias)', r'bert.encoder.layers.\1.mlp.fc2.\2', key) return key state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention last_layer_subset = getattr(config, 'last_layer_subset', False) for d in range(config.num_hidden_layers): Wq = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.query.weight') Wk = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.key.weight') Wv = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.value.weight') bq = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.query.bias') bk = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.key.bias') bv = state_dict.pop(f'bert.encoder.layers.{d}.attention.self.value.bias') if not (last_layer_subset and d == config.num_hidden_layers - 1): state_dict[f'bert.encoder.layers.{d}.mixer.Wqkv.weight'] = torch.cat( [Wq, Wk, Wv], dim=0 ) state_dict[f'bert.encoder.layers.{d}.mixer.Wqkv.bias'] = torch.cat( [bq, bk, bv], dim=0 ) else: state_dict[f'bert.encoder.layers.{d}.mixer.Wq.weight'] = Wq state_dict[f'bert.encoder.layers.{d}.mixer.Wkv.weight'] = torch.cat( [Wk, Wv], dim=0 ) state_dict[f'bert.encoder.layers.{d}.mixer.Wq.bias'] = bq state_dict[f'bert.encoder.layers.{d}.mixer.Wkv.bias'] = torch.cat( [bk, bv], dim=0 ) def key_mapping_attn(key): return re.sub(r'^bert.encoder.layers.(\d+).attention.output.dense.(weight|bias)', r'bert.encoder.layers.\1.mixer.out_proj.\2', key) state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) def key_mapping_decoder_bias(key): return re.sub(r'^cls.predictions.bias', 'cls.predictions.decoder.bias', key) state_dict = OrderedDict((key_mapping_decoder_bias(k), v) for k, v in state_dict.items()) # Word embedding pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) if pad_vocab_size_multiple > 1: word_embeddings = state_dict['bert.embeddings.word_embeddings.weight'] state_dict['bert.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, config.vocab_size - word_embeddings.shape[0]) ) decoder_weight = state_dict['cls.predictions.decoder.weight'] state_dict['cls.predictions.decoder.weight'] = F.pad( decoder_weight, (0, 0, 0, config.vocab_size - decoder_weight.shape[0]) ) # If the vocab was padded, we want to set the decoder bias for those padded indices to be # strongly negative (i.e. the decoder shouldn't predict those indices). # TD [2022-05-09]: I don't think it affects the MLPerf training. decoder_bias = state_dict['cls.predictions.decoder.bias'] state_dict['cls.predictions.decoder.bias'] = F.pad( decoder_bias, (0, config.vocab_size - decoder_bias.shape[0]), value=-100.0 ) return state_dict
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/bert.py
# Copyright (c) 2023, Tri Dao. import math import re from collections import OrderedDict import torch import torch.nn.functional as F from einops import rearrange from transformers import GPT2Config, GPTNeoXConfig def remap_state_dict_hf_gpt_neox(state_dict, config): def key_mapping_layers(key): return re.sub(r'^gpt_neox.', 'transformer.', key) state_dict = OrderedDict((key_mapping_layers(k), v) for k, v in state_dict.items()) # Word embedding def key_mapping_emb(key): return re.sub(r'^transformer.embed_in.', 'transformer.embeddings.word_embeddings.', key) state_dict = OrderedDict((key_mapping_emb(k), v) for k, v in state_dict.items()) word_embeddings = state_dict.pop('transformer.embeddings.word_embeddings.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) state_dict['transformer.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, vocab_size - word_embeddings.shape[0]) ) if getattr(config, 'tie_word_embeddings'): state_dict['lm_head.weight'] = state_dict['transformer.embeddings.word_embeddings.weight'] else: output_embeddings = state_dict.pop('embed_out.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. state_dict['lm_head.weight'] = F.pad( output_embeddings, (0, 0, 0, vocab_size - output_embeddings.shape[0]) ) # LayerNorm def key_mapping_ln(key): key = re.sub(r'^transformer.final_layer_norm.', r'transformer.ln_f.', key) key = re.sub(r'^transformer.layers.(\d+).input_layernorm.', r'transformer.layers.\1.norm1.', key) key = re.sub(r'^transformer.layers.(\d+).post_attention_layernorm.', r'transformer.layers.\1.norm2.', key) return key state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP def key_mapping_mlp(key): key = re.sub(r'^transformer.layers.(\d+).mlp.dense_h_to_4h.', r'transformer.layers.\1.mlp.fc1.', key) key = re.sub(r'^transformer.layers.(\d+).mlp.dense_4h_to_h.', r'transformer.layers.\1.mlp.fc2.', key) return key state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention for l in range(config.n_layer): # We don't store these biases state_dict.pop(f'transformer.layers.{l}.attention.bias') state_dict.pop(f'transformer.layers.{l}.attention.masked_bias') # GPT-NeoX stores Wqkv as ((nheads 3 headdim), hidden_dim) # while we store Wqkv as ((3 nheads headdim), hidden_dim) headdim = config.hidden_size // config.num_attention_heads Wqkv = state_dict.pop(f'transformer.layers.{l}.attention.query_key_value.weight') state_dict[f'transformer.layers.{l}.mixer.Wqkv.weight'] = rearrange( Wqkv, '(nheads three headdim) ... -> (three nheads headdim) ...', three=3, headdim=headdim ) bqkv = state_dict.pop(f'transformer.layers.{l}.attention.query_key_value.bias') state_dict[f'transformer.layers.{l}.mixer.Wqkv.bias'] = rearrange( bqkv, '(nheads three headdim) -> (three nheads headdim)', three=3, headdim=headdim ) def key_mapping_attn(key): key = re.sub(r'^transformer.layers.(\d+).attention.dense.', r'transformer.layers.\1.mixer.out_proj.', key) key = re.sub(r'^transformer.layers.(\d+).attention.rotary_emb.', r'transformer.layers.\1.mixer.rotary_emb.', key) return key state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) return state_dict def gpt_neox_config_to_gpt2_config(gpt_neox_config: GPTNeoXConfig) -> GPT2Config: assert gpt_neox_config.rotary_emb_base == 10000 return GPT2Config( vocab_size=gpt_neox_config.vocab_size, n_positions=0, # No absolute position embedding n_embd=gpt_neox_config.hidden_size, n_layer=gpt_neox_config.num_hidden_layers, n_head=gpt_neox_config.num_attention_heads, n_inner=gpt_neox_config.intermediate_size, activation_function=gpt_neox_config.hidden_act, resid_pdrop=0.0, # No dropout embd_pdrop=0.0, attn_pdrop=0.0, layer_norm_epsilon=gpt_neox_config.layer_norm_eps, initializer_range=gpt_neox_config.initializer_range, bos_token_id=gpt_neox_config.bos_token_id, eos_token_id=gpt_neox_config.eos_token_id, # These are new arguments not in the original GPT2Config prenorm=True, parallel_block=gpt_neox_config.use_parallel_residual, parallel_block_tied_norm=False, rotary_emb_fraction=gpt_neox_config.rotary_pct, tie_word_embeddings=gpt_neox_config.tie_word_embeddings, )
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/gpt_neox.py
# Copyright (c) 2023, Tri Dao. import logging import math import re from functools import partial from collections import namedtuple, OrderedDict from collections.abc import Sequence import torch import torch.nn as nn import torch.nn.functional as F from transformers import GPT2Config from einops import rearrange from flash_attn.modules.mha import MHA, ParallelMHA from flash_attn.modules.mlp import Mlp, FusedMLP, ParallelFusedMLP from flash_attn.modules.block import Block, ParallelBlock from flash_attn.modules.embedding import GPT2Embeddings, ParallelGPT2Embeddings from flash_attn.utils.distributed import sync_shared_params, all_gather_raw from flash_attn.utils.pretrained import state_dict_from_pretrained from flash_attn.utils.generation import GenerationMixin from flash_attn.models.opt import remap_state_dict_hf_opt from flash_attn.models.gptj import remap_state_dict_hf_gptj from flash_attn.models.gpt_neox import remap_state_dict_hf_gpt_neox try: from flash_attn.ops.fused_dense import ColumnParallelLinear except ImportError: ColumnParallelLinear = None try: from flash_attn.ops.layer_norm import dropout_add_layer_norm except ImportError: dropout_add_layer_norm = None try: from flash_attn.ops.layer_norm import dropout_add_layer_norm_parallel_residual except ImportError: dropout_add_layer_norm_parallel_residual = None try: from flash_attn.ops.triton.mlp import FusedDenseSqreluDense, sqrelu_fwd except ImportError: FusedDenseSqreluDense = None sqrelu_fwd = None logger = logging.getLogger(__name__) def create_mixer_cls(config, layer_idx=None, process_group=None, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} head_dim = getattr(config, 'head_dim', config.hidden_size // config.num_attention_heads) softmax_scale = 1.0 if not config.scale_attn_weights else head_dim ** (-0.5) if config.scale_attn_by_inverse_layer_idx: assert layer_idx is not None softmax_scale /= float(layer_idx + 1) dwconv = getattr(config, 'attn_dwconv', False) if dwconv: assert process_group is None, 'TensorParallel MHA does not support dwconv yet' qkv_proj_bias = getattr(config, 'qkv_proj_bias', True) out_proj_bias = getattr(config, 'out_proj_bias', True) rotary_emb_dim = int(getattr(config, 'rotary_emb_fraction', 0.0) * head_dim) rotary_emb_scale_base = getattr(config, 'rotary_emb_scale_base', None) rotary_emb_interleaved = getattr(config, 'rotary_emb_interleaved', False) use_flash_attn = getattr(config, 'use_flash_attn', False) fused_bias_fc = getattr(config, 'fused_bias_fc', False) if not fused_bias_fc: assert process_group is None, 'TensorParallel MHA requires fused_bias_fc' mha_cls = MHA if process_group is None else ParallelMHA serial_kwargs = ({'fused_bias_fc': fused_bias_fc, 'dwconv': dwconv} if process_group is None else {}) parallel_kwargs = ({'process_group': process_group, 'sequence_parallel': getattr(config, 'sequence_parallel', True)} if process_group is not None else {}) mixer_cls = partial(mha_cls, num_heads=config.num_attention_heads, qkv_proj_bias=qkv_proj_bias, out_proj_bias=out_proj_bias, dropout=config.attn_pdrop, softmax_scale=softmax_scale, causal=True, layer_idx=layer_idx, rotary_emb_dim=rotary_emb_dim, rotary_emb_scale_base=rotary_emb_scale_base, rotary_emb_interleaved=rotary_emb_interleaved, use_flash_attn=use_flash_attn, **serial_kwargs, **parallel_kwargs, **factory_kwargs) return mixer_cls def create_mlp_cls(config, layer_idx=None, process_group=None, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} inner_dim = config.n_inner if config.n_inner is not None else 4 * config.hidden_size fused_mlp = getattr(config, 'fused_mlp', False) if fused_mlp: assert config.activation_function in ['gelu_new', 'gelu_fast', 'gelu_approx', 'relu', 'sqrelu'] fused_dense_sqrelu_dense = getattr(config, 'fused_dense_sqrelu_dense', False) if fused_dense_sqrelu_dense: assert config.activation_function == 'sqrelu', ('fused_dense_sqrelu_dense only ' 'supports approximate activation_function sqrelu') assert not (fused_dense_sqrelu_dense and fused_mlp) if process_group is not None: assert fused_mlp, 'Tensor Parallel is only implemented for FusedMLP' if not fused_mlp and not fused_dense_sqrelu_dense: assert config.activation_function in ['gelu_new', 'gelu_fast', 'gelu_approx', 'relu', 'sqrelu'] if config.activation_function == 'relu': activation = partial(F.relu, inplace=True) elif config.activation_function == 'sqrelu': assert sqrelu_fwd is not None, 'sqrelu_fwd is not implemented' activation = sqrelu_fwd else: approximate = ('tanh' if config.activation_function in ['gelu_new', 'gelu_fast', 'gelu_approx'] else 'none') activation=partial(F.gelu, approximate=approximate) mlp_cls = partial(Mlp, hidden_features=inner_dim, activation=activation, **factory_kwargs) else: mlp_checkpoint_lvl = getattr(config, 'mlp_checkpoint_lvl', 0) # mlp_checkpoint_lvl could be a list, which contains the checkpoint_lvl for each layer if isinstance(mlp_checkpoint_lvl, Sequence): assert layer_idx is not None mlp_checkpoint_lvl = mlp_checkpoint_lvl[layer_idx] if fused_mlp: if FusedMLP is None: raise ImportError('fused_dense is not installed') activation = ('gelu_approx' if config.activation_function in ['gelu_new', 'gelu_fast', 'gelu_approx'] else config.activation_function) mlp_cls = FusedMLP if process_group is None else ParallelFusedMLP parallel_kwargs = ({'process_group': process_group, 'sequence_parallel': getattr(config, 'sequence_parallel', True)} if process_group is not None else {}) mlp_cls = partial(mlp_cls, hidden_features=inner_dim, activation=activation, checkpoint_lvl=mlp_checkpoint_lvl, **parallel_kwargs, **factory_kwargs) elif fused_dense_sqrelu_dense: assert FusedDenseSqreluDense is not None mlp_cls = partial(FusedDenseSqreluDense, hidden_features=inner_dim, checkpoint_lvl=mlp_checkpoint_lvl, **factory_kwargs) else: raise RuntimeError('MLP type not supported') return mlp_cls def create_block(config, layer_idx=None, process_group=None, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} sequence_parallel = getattr(config, 'sequence_parallel', True) mixer_cls = create_mixer_cls(config, layer_idx, process_group=process_group, **factory_kwargs) mlp_cls = create_mlp_cls(config, layer_idx, process_group=process_group, **factory_kwargs) norm_cls = partial(nn.LayerNorm, eps=config.layer_norm_epsilon, **factory_kwargs) # TD [2022-07-30]: Force residual in fp32, seems to make fp16 training more stable residual_in_fp32 = getattr(config, 'residual_in_fp32', False) resid_dropout1 = config.resid_pdrop if layer_idx is None or layer_idx > 0 else config.embd_pdrop prenorm = getattr(config, 'prenorm', True) parallel_block = getattr(config, 'parallel_block', False) if not parallel_block: block = Block( config.hidden_size, mixer_cls, mlp_cls, norm_cls=norm_cls, prenorm=prenorm, resid_dropout1=resid_dropout1, resid_dropout2=config.resid_pdrop, fused_dropout_add_ln=getattr(config, 'fused_dropout_add_ln', False), residual_in_fp32=residual_in_fp32, sequence_parallel=sequence_parallel and process_group is not None, mark_shared_params=process_group is not None ) else: assert prenorm block = ParallelBlock( config.hidden_size, mixer_cls, mlp_cls, norm_cls=norm_cls, resid_dropout1=resid_dropout1, resid_dropout2=config.resid_pdrop, tied_norm=getattr(config, 'parallel_block_tied_norm', False), fused_dropout_add_ln=getattr(config, 'fused_dropout_add_ln', False), residual_in_fp32=residual_in_fp32, sequence_parallel=sequence_parallel and process_group is not None, mark_shared_params=process_group is not None ) block.layer_idx = layer_idx return block class GPTPreTrainedModel(nn.Module): """ An abstract class to handle weights initialization and a simple interface for dowloading and loading pretrained models. """ def __init__(self, config, *inputs, **kwargs): super().__init__() if not isinstance(config, GPT2Config): raise ValueError( "Parameter config in `{}(config)` should be an instance of class `GPT2Config`. " "To create a model from a Google pretrained model use " "`model = {}.from_pretrained(PRETRAINED_MODEL_NAME)`".format( self.__class__.__name__, self.__class__.__name__ )) self.config = config @classmethod def from_pretrained(cls, model_name, config, *args, strict=True, device=None, dtype=None, world_size=1, rank=0, **kwargs): """ Instantiate a GPTPreTrainedModel from a pre-trained model file or a pytorch state dict. Download and cache the pre-trained model file if needed. """ # Instantiate model. model = cls(config, *args, device=device, dtype=dtype, **kwargs) # Load state_dict in cpu because we already initialized the model in GPU, and we don't # want extra stuff taking up more GPU memory state_dict = state_dict_from_pretrained( model_name, device='cpu', dtype=dtype ) if model_name.startswith('gpt2'): state_dict = remap_state_dict_hf_gpt2(state_dict, config) elif model_name.startswith('facebook/opt'): state_dict = remap_state_dict_hf_opt(state_dict, config) elif model_name.startswith('EleutherAI/gpt-j-'): state_dict = remap_state_dict_hf_gptj(state_dict, config) strict = False # We have rotary_emb.inf_freq buffers not in the GPT-J checkpoint elif model_name.startswith('EleutherAI/gpt-neox-'): state_dict = remap_state_dict_hf_gpt_neox(state_dict, config) else: raise NotImplementedError(f'Model {model_name} not supported') if world_size > 1: state_dict = shard_state_dict_tp(state_dict, config, world_size, rank) load_return = model.load_state_dict(state_dict, strict=strict) logger.info(load_return) return model # https://github.com/huggingface/transformers/blob/c28d04e9e252a1a099944e325685f14d242ecdcd/src/transformers/models/gpt2/modeling_gpt2.py#L454 def _init_weights(module, n_layer, initializer_range=0.02, rescale_prenorm_residual=True): if isinstance(module, nn.Linear): nn.init.normal_(module.weight, std=initializer_range) if module.bias is not None: nn.init.zeros_(module.bias) elif isinstance(module, nn.Embedding): nn.init.normal_(module.weight, std=initializer_range) if rescale_prenorm_residual: # Reinitialize selected weights subject to the OpenAI GPT-2 Paper Scheme: # > A modified initialization which accounts for the accumulation on the residual path with model depth. Scale # > the weights of residual layers at initialization by a factor of 1/√N where N is the # of residual layers. # > -- GPT-2 :: https://openai.com/blog/better-language-models/ # # Reference (Megatron-LM): https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py for name, p in module.named_parameters(): if name in ["out_proj.weight", "fc2.weight"]: # Special Scaled Initialization --> There are 2 Layer Norms per Transformer Block nn.init.normal_(p, mean=0.0, std=initializer_range / math.sqrt(2 * n_layer)) class GPTModel(GPTPreTrainedModel): def __init__(self, config: GPT2Config, process_group=None, device=None, dtype=None): super().__init__(config) factory_kwargs = {'device': device, 'dtype': dtype} self.process_group = process_group self.sequence_parallel = getattr(config, 'sequence_parallel', True) assert config.activation_function in ['gelu', 'gelu_new', 'gelu_fast', 'gelu_approx', 'relu', 'sqrelu'] pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) # TD [2022-07-30]: Force residual in fp32, seems to make fp16 training more stable self.residual_in_fp32 = getattr(config, 'residual_in_fp32', False) # These 2 options are for OPT-350m self.prenorm = getattr(config, 'prenorm', True) word_embed_proj_dim = getattr(config, 'word_embed_proj_dim', None) # For GPT-J, GPT-NeoX self.parallel_block = getattr(config, 'parallel_block', False) if process_group is None: self.embeddings = GPT2Embeddings( config.hidden_size, vocab_size, config.max_position_embeddings, word_embed_proj_dim=word_embed_proj_dim, **factory_kwargs ) else: self.embeddings = ParallelGPT2Embeddings( config.hidden_size, vocab_size, config.max_position_embeddings, process_group=process_group, sequence_parallel=self.sequence_parallel, **factory_kwargs ) # We change the order of dropout, residual and layer norm: # Instead of LN -> Attn / MLP -> Dropout -> Add, we do: # Dropout -> Add -> LN -> Attn / MLP, returning both the residual branch (output of Add) and # the main branch (output of MLP). The model definition is unchanged, but the mapping of the # nn.Dropout probabilities are changed. # This is for performance reason: we can fuse dropout + add + layer_norm. self.layers = nn.ModuleList([create_block(config, layer_idx=i, process_group=process_group, **factory_kwargs) for i in range(config.num_hidden_layers)]) self.fused_dropout_add_ln = getattr(config, 'fused_dropout_add_ln', False) if self.fused_dropout_add_ln: if ((not self.parallel_block and dropout_add_layer_norm is None) or (self.parallel_block and dropout_add_layer_norm_parallel_residual is None)): raise ImportError('dropout_layer_norm is not installed') if self.prenorm: self.drop_f = nn.Dropout(config.resid_pdrop) self.ln_f = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_epsilon, **factory_kwargs) if process_group is not None: for p in self.ln_f.parameters(): # Mark the norm parameters as "shared_params" so that we sync their values at init. p._shared_params = True # Mark the norm params as "sequence_parallel" so we run all-reduce on their grads. if self.sequence_parallel: p._sequence_parallel = True self.apply(partial(_init_weights, n_layer=config.num_hidden_layers, initializer_range=config.initializer_range)) self.tie_weights() def tie_weights(self): if self.process_group is not None: sync_shared_params(self, self.process_group) def forward(self, input_ids, position_ids=None, inference_params=None): # If using Tensor Parallel with sequence parallel, we combine the batch and the seqlen # dimensions so that we can split on it easily, in case of small batch size. # Only the attention layers need to know the seqlen. embedding_kwargs = ({'combine_batch_seqlen_dim': True} if self.process_group is not None and self.sequence_parallel else {}) hidden_states = self.embeddings(input_ids, position_ids=position_ids, **embedding_kwargs) if self.parallel_block: hidden_states2 = None residual = None mixer_kwargs = ({'seqlen': input_ids.shape[1]} if self.process_group is not None and self.sequence_parallel else {}) if inference_params is not None: mixer_kwargs['inference_params'] = inference_params for layer in self.layers: if self.prenorm: if not self.parallel_block: hidden_states, residual = layer(hidden_states, residual, mixer_kwargs=mixer_kwargs) else: hidden_states, hidden_states2, residual = layer( hidden_states, hidden_states2, residual, mixer_kwargs=mixer_kwargs ) else: hidden_states = layer(hidden_states, mixer_kwargs=mixer_kwargs) if self.prenorm: if not self.fused_dropout_add_ln: dropped = self.drop_f(hidden_states) if not self.parallel_block: residual = (dropped + residual) if residual is not None else dropped else: dropped2 = self.drop_f(hidden_states2) residual = ((residual + dropped + dropped2) if residual is not None else dropped + dropped2) hidden_states = self.ln_f(residual.to(dtype=self.ln_f.weight.dtype)) else: # Set prenorm=False here since we don't need the residual if not self.parallel_block: hidden_states = dropout_add_layer_norm( hidden_states, residual, self.ln_f.weight, self.ln_f.bias, self.drop_f.p if self.training else 0.0, self.ln_f.eps, prenorm=False, residual_in_fp32=self.residual_in_fp32 ) else: hidden_states, _ = dropout_add_layer_norm_parallel_residual( hidden_states, hidden_states2, residual, self.ln_f.weight, self.ln_f.bias, None, None, self.drop_f.p if self.training else 0.0, self.ln_f.eps, prenorm=False, residual_in_fp32=self.residual_in_fp32 ) return hidden_states class GPTLMHeadModel(GPTPreTrainedModel, GenerationMixin): def __init__(self, config: GPT2Config, process_group=None, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} super().__init__(config) self.process_group = process_group self.transformer = GPTModel(config, process_group=process_group, **factory_kwargs) self.tie_word_embeddings = getattr(config, 'tie_word_embeddings', True) lm_head_bias = getattr(config, 'lm_head_bias', False) pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) # This option is for OPT-350m word_embed_proj_dim = getattr(config, 'word_embed_proj_dim', None) embed_dim = config.n_embd if word_embed_proj_dim is None else word_embed_proj_dim if word_embed_proj_dim is not None: self.project_out = nn.Linear(config.n_embd, embed_dim, bias=False, **factory_kwargs) else: self.project_out = None if process_group is None: self.lm_head = nn.Linear(embed_dim, vocab_size, bias=lm_head_bias, **factory_kwargs) else: if ColumnParallelLinear is None: raise ImportError('fused_dense_lib is not installed') self.lm_head = ColumnParallelLinear( embed_dim, vocab_size, process_group, bias=lm_head_bias, sequence_parallel=getattr(config, 'sequence_parallel', True), **factory_kwargs ) # Initialize weights and apply final processing self.apply(partial(_init_weights, n_layer=config.num_hidden_layers, initializer_range=config.initializer_range)) self.tie_weights() def tie_weights(self): if self.tie_word_embeddings: self.lm_head.weight = self.transformer.embeddings.word_embeddings.weight if self.process_group is not None: sync_shared_params(self, self.process_group) def forward(self, input_ids, position_ids=None, inference_params=None): """ inference_params: for generation. Adapted from Megatron-LM (and Apex) https://github.com/NVIDIA/apex/blob/3ff1a10f72ec07067c4e44759442329804ac5162/apex/transformer/testing/standalone_transformer_lm.py#L470 """ hidden_states = self.transformer(input_ids, position_ids=position_ids, inference_params=inference_params) if self.project_out is not None: hidden_states = self.project_out(hidden_states) lm_logits = self.lm_head(hidden_states) # During inference, we want the full logit for sampling if isinstance(self.lm_head, ColumnParallelLinear) and inference_params is not None: lm_logits, _ = all_gather_raw(lm_logits, self.lm_head.process_group) lm_logits = rearrange(lm_logits, '(n b) s d -> b s (n d)', b=hidden_states.shape[0]) CausalLMOutput = namedtuple('CausalLMOutput', ['logits']) return CausalLMOutput(logits=lm_logits) def load_state_dict(self, state_dict, strict=True): # Remapping from our checkpoints that used a different ordering of layers in the block # Previous: Attn / MLP -> Dropout -> Add -> LN # Current: Dropout -> Add -> LN -> Attn / MLP if 'transformer.ln_0.weight' in state_dict: n_layers = len(self.transformer.layers) ln_weight = state_dict.pop(f'transformer.layers.{n_layers - 1}.norm2.weight') ln_bias = state_dict.pop(f'transformer.layers.{n_layers - 1}.norm2.bias') state_dict['transformer.ln_f.weight'] = ln_weight state_dict['transformer.ln_f.bias'] = ln_bias for l in reversed(range(n_layers)): ln_weight = state_dict.pop(f'transformer.layers.{l}.norm1.weight') ln_bias = state_dict.pop(f'transformer.layers.{l}.norm1.bias') state_dict[f'transformer.layers.{l}.norm2.weight'] = ln_weight state_dict[f'transformer.layers.{l}.norm2.bias'] = ln_bias if l > 0: ln_weight = state_dict.pop(f'transformer.layers.{l - 1}.norm2.weight') ln_bias = state_dict.pop(f'transformer.layers.{l - 1}.norm2.bias') state_dict[f'transformer.layers.{l}.norm1.weight'] = ln_weight state_dict[f'transformer.layers.{l}.norm1.bias'] = ln_bias ln_weight = state_dict.pop('transformer.ln_0.weight') ln_bias = state_dict.pop('transformer.ln_0.bias') state_dict[f'transformer.layers.0.norm1.weight'] = ln_weight state_dict[f'transformer.layers.0.norm1.bias'] = ln_bias return super().load_state_dict(state_dict, strict=strict) def shard_state_dict_tp(state_dict, config, world_size, rank): """Convert the state_dict of a standard GPT model to the state_dict of a GPT model with tensor parallel. """ pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) assert vocab_size % world_size == 0 assert config.hidden_size % world_size == 0 inner_dim = config.n_inner if config.n_inner is not None else 4 * config.hidden_size assert inner_dim % world_size == 0 def shard_first_dim(state_dict, key): x = state_dict[key] dim = x.shape[0] // world_size state_dict[key] = x[rank * dim:(rank + 1) * dim] def shard_last_dim(state_dict, key): x = state_dict[key] dim = x.shape[-1] // world_size state_dict[key] = x[..., rank * dim:(rank + 1) * dim] def shard_qkv_headdim(state_dict, key): x = rearrange(state_dict[key], '(three d) ... -> three d ...', three=3) dim = x.shape[1] // world_size state_dict[key] = rearrange(x[:, rank * dim:(rank + 1) * dim], 'three d ... -> (three d) ...') shard_first_dim(state_dict, 'transformer.embeddings.word_embeddings.weight') if 'lm_head.weight' in state_dict: shard_first_dim(state_dict, 'lm_head.weight') if 'transformer.embeddings.position_embeddings.weight' in state_dict: shard_last_dim(state_dict, 'transformer.embeddings.position_embeddings.weight') for i in range(config.num_hidden_layers): shard_qkv_headdim(state_dict, f'transformer.layers.{i}.mixer.Wqkv.weight') shard_qkv_headdim(state_dict, f'transformer.layers.{i}.mixer.Wqkv.bias') shard_last_dim(state_dict, f'transformer.layers.{i}.mixer.out_proj.weight') if rank != 0: state_dict.pop(f'transformer.layers.{i}.mixer.out_proj.bias') shard_first_dim(state_dict, f'transformer.layers.{i}.mlp.fc1.weight') shard_first_dim(state_dict, f'transformer.layers.{i}.mlp.fc1.bias') shard_last_dim(state_dict, f'transformer.layers.{i}.mlp.fc2.weight') if rank != 0: state_dict.pop(f'transformer.layers.{i}.mlp.fc2.bias') return state_dict def combine_state_dicts_tp(state_dicts, config): """Convert the state_dict of a standard GPT model to the state_dict of a GPT model with tensor parallel. """ world_size = len(state_dicts) keys = state_dicts[0].keys() pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) assert vocab_size % world_size == 0 assert config.hidden_size % world_size == 0 inner_dim = config.n_inner if config.n_inner is not None else 4 * config.hidden_size assert inner_dim % world_size == 0 # The word embeddings from Megatron are weird, for each shard only the first # vocab_size // world_size coordinates are nonzero. def combine_word_embeddings(state_dicts, state_dict, key): assert all(s[key].shape[0] == vocab_size for s in state_dicts) state_dict[key] = torch.cat([s[key][:vocab_size // world_size] for s in state_dicts], dim=0) def combine_dim(state_dicts, state_dict, key, dim=-1): state_dict[key] = torch.cat([s[key] for s in state_dicts], dim=dim) def combine_qkv_headdim(state_dicts, state_dict, key): xs = [rearrange(s[key], '(three d) ... -> three d ...', three=3) for s in state_dicts] state_dict[key] = rearrange(torch.cat(xs, dim=1), 'three d ... -> (three d) ...') state_dict = state_dicts[0].copy() # don't modify state_dict[0] inplace combine_word_embeddings(state_dicts, state_dict, 'transformer.embeddings.word_embeddings.weight') if 'lm_head.weight' in state_dict: combine_word_embeddings(state_dicts, state_dict, 'lm_head.weight') if 'transformer.embeddings.position_embeddings.weight' in state_dict: combine_dim(state_dicts, state_dict, 'transformer.embeddings.position_embeddings.weight', -1) for i in range(config.num_hidden_layers): combine_qkv_headdim(state_dicts, state_dict, f'transformer.layers.{i}.mixer.Wqkv.weight') combine_qkv_headdim(state_dicts, state_dict, f'transformer.layers.{i}.mixer.Wqkv.bias') combine_dim(state_dicts, state_dict, f'transformer.layers.{i}.mixer.out_proj.weight', -1) combine_dim(state_dicts, state_dict, f'transformer.layers.{i}.mlp.fc1.weight', 0) combine_dim(state_dicts, state_dict, f'transformer.layers.{i}.mlp.fc1.bias', 0) combine_dim(state_dicts, state_dict, f'transformer.layers.{i}.mlp.fc2.weight', -1) return state_dict def remap_state_dict_hf_gpt2(state_dict, config): # Word embedding and position embedding def key_mapping_pos_emb(key): return re.sub(r'^wpe.', 'transformer.embeddings.position_embeddings.', key) state_dict = OrderedDict((key_mapping_pos_emb(k), v) for k, v in state_dict.items()) word_embeddings = state_dict.pop('wte.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) state_dict['transformer.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, vocab_size - word_embeddings.shape[0]) ) state_dict['lm_head.weight'] = state_dict['transformer.embeddings.word_embeddings.weight'] # LayerNorm def key_mapping_ln(key): key = re.sub(r'^ln_f.(weight|bias)', r'transformer.ln_f.\1', key) key = re.sub(r'^h.(\d+).ln_(1|2).(weight|bias)', r'transformer.layers.\1.norm\2.\3', key) return key state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP for d in range(config.num_hidden_layers): W1 = state_dict.pop(f'h.{d}.mlp.c_fc.weight') state_dict[f'transformer.layers.{d}.mlp.fc1.weight'] = W1.t() W2 = state_dict.pop(f'h.{d}.mlp.c_proj.weight') state_dict[f'transformer.layers.{d}.mlp.fc2.weight'] = W2.t() def key_mapping_mlp(key): key = re.sub(r'^h.(\d+).mlp.c_fc.bias', r'transformer.layers.\1.mlp.fc1.bias', key) key = re.sub(r'^h.(\d+).mlp.c_proj.bias', r'transformer.layers.\1.mlp.fc2.bias', key) return key state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention for d in range(config.num_hidden_layers): state_dict.pop(f'h.{d}.attn.bias') # We don't store this bias Wqkv = state_dict.pop(f'h.{d}.attn.c_attn.weight') state_dict[f'transformer.layers.{d}.mixer.Wqkv.weight'] = Wqkv.t() Wout = state_dict.pop(f'h.{d}.attn.c_proj.weight') state_dict[f'transformer.layers.{d}.mixer.out_proj.weight'] = Wout.t() def key_mapping_attn(key): key = re.sub(r'^h.(\d+).attn.c_attn.bias', r'transformer.layers.\1.mixer.Wqkv.bias', key) key = re.sub(r'^h.(\d+).attn.c_proj.bias', r'transformer.layers.\1.mixer.out_proj.bias', key) return key state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) return state_dict def remap_state_dict_megatron(state_dict, config): def key_mapping_transformer(key): key = re.sub(r'^language_model.encoder.', 'transformer.', key) key = re.sub(r'^language_model.', 'transformer.', key) return key state_dict = OrderedDict((key_mapping_transformer(k), v) for k, v in state_dict.items()) # Word embedding and position embedding def key_mapping_pos_emb(key): return re.sub(r'^wpe.', 'transformer.embeddings.position_embeddings.', key) state_dict = OrderedDict((key_mapping_pos_emb(k), v) for k, v in state_dict.items()) word_embeddings = state_dict.pop('transformer.embedding.word_embeddings.weight') # It's possible that vocab_size is padded to be a multiple of 8, for example. pad_vocab_size_multiple = getattr(config, 'pad_vocab_size_multiple', 1) vocab_size = (math.ceil(config.vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple) state_dict['transformer.embeddings.word_embeddings.weight'] = F.pad( word_embeddings, (0, 0, 0, vocab_size - word_embeddings.shape[0]) ) state_dict['lm_head.weight'] = state_dict['transformer.embeddings.word_embeddings.weight'] # LayerNorm def key_mapping_ln(key): key = re.sub(r'^transformer.final_layernorm.(weight|bias)', r'transformer.ln_f.\1', key) key = re.sub(r'^transformer.layers.(\d+).input_layernorm.(weight|bias)', r'transformer.layers.\1.norm1.\2', key) key = re.sub(r'^transformer.layers.(\d+).post_attention_layernorm.(weight|bias)', r'transformer.layers.\1.norm2.\2', key) return key state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items()) # MLP def key_mapping_mlp(key): key = re.sub(r'^transformer.layers.(\d+).mlp.dense_h_to_4h.(weight|bias)', r'transformer.layers.\1.mlp.fc1.\2', key) key = re.sub(r'^transformer.layers.(\d+).mlp.dense_4h_to_h.(weight|bias)', r'transformer.layers.\1.mlp.fc2.\2', key) return key state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items()) # Attention def key_mapping_attn(key): key = re.sub(r'^transformer.layers.(\d+).self_attention.rotary_emb.inv_freq', r'transformer.layers.\1.mixer.rotary_emb.inv_freq', key) key = re.sub(r'^transformer.layers.(\d+).self_attention.query_key_value.(weight|bias)', r'transformer.layers.\1.mixer.Wqkv.\2', key) key = re.sub(r'^transformer.layers.(\d+).self_attention.dense.(weight|bias)', r'transformer.layers.\1.mixer.out_proj.\2', key) return key state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items()) # Megatron stores Wqkv as ((nheads 3 headdim), hidden_dim) # while we store Wqkv as ((3 nheads headdim), hidden_dim) headdim = config.hidden_size // config.num_attention_heads for d in range(config.num_hidden_layers): Wqkv = state_dict.pop(f'transformer.layers.{d}.mixer.Wqkv.weight') state_dict[f'transformer.layers.{d}.mixer.Wqkv.weight'] = rearrange( Wqkv, '(nheads three headdim) ... -> (three nheads headdim) ...', three=3, headdim=headdim ) bqkv = state_dict.pop(f'transformer.layers.{d}.mixer.Wqkv.bias') state_dict[f'transformer.layers.{d}.mixer.Wqkv.bias'] = rearrange( bqkv, '(nheads three headdim) -> (three nheads headdim)', three=3, headdim=headdim ) return state_dict
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/models/gpt.py
# Copied from https://github.com/mlcommons/training_results_v1.1/blob/main/NVIDIA/benchmarks/bert/implementations/pytorch/model/layers/activations.py import math import torch import torch.nn as nn import torch.nn.functional as F # 1/sqrt(2*pi)-> 0.3989423 # 1/sqrt(2) -> 0.70710678 # sqrt(2/pi) -> 0.79788456 # this function is tanh approximation of gelu # actual gelu is: # x * 0.5 * (1.0 + torch.erf(x * 0.70710678)) @torch.jit.script def bias_gelu(y, bias): x = bias + y return (x * 0.5 * (1.0 + torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)))).to(dtype=y.dtype) # gradient of tanh approximation of gelu # gradient of actual gelu is: # 0.5 * (1. + torch.erf(x * 0.70710678)) + 0.3989423 * x * torch.exp(-0.5 * x * x) @torch.jit.script def bias_gelu_back(g, y, bias): """Assume that y has shape (B, D) and bias has shape (D) """ x = bias + y tanh_out = torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)) # sqrt(2/pi) * 3 * 0.044715 -> 0.1070322243 ff = 0.5 * x * ((1 - tanh_out * tanh_out) * (0.79788456 + 0.1070322243 * x * x)) + 0.5 * (1 + tanh_out) grad_y = ff * g return grad_y.to(dtype=y.dtype), grad_y.sum(dim=(0), dtype=bias.dtype) class GeLUFunction(torch.autograd.Function): @staticmethod # bias is an optional argument def forward(ctx, input, bias): ctx.save_for_backward(input, bias) return bias_gelu(input, bias) @staticmethod def backward(ctx, grad_output): input, bias = ctx.saved_tensors tmp = bias_gelu_back(grad_output, input, bias) return tmp, tmp bias_gelu_impl = GeLUFunction.apply # this function is tanh approximation of gelu # actual gelu is: # x * 0.5 * (1.0 + torch.erf(x * 0.70710678)) @torch.jit.script def gelu_fwd(x): return (x * 0.5 * (1.0 + torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)))).to(dtype=x.dtype) # gradient of tanh approximation of gelu # gradient of actual gelu is: # 0.5 * (1. + torch.erf(x * 0.70710678)) + 0.3989423 * x * torch.exp(-0.5 * x * x) @torch.jit.script def gelu_bwd(g, x): tanh_out = torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)) # sqrt(2/pi) * 3 * 0.044715 -> 0.1070322243 ff = 0.5 * x * ((1 - tanh_out * tanh_out) * (0.79788456 + 0.1070322243 * x * x)) + 0.5 * (1 + tanh_out) return (ff * g).to(dtype=x.dtype) class FastGeLUFunction(torch.autograd.Function): @staticmethod # bias is an optional argument def forward(ctx, input): ctx.save_for_backward(input) return gelu_fwd(input) @staticmethod def backward(ctx, grad_output): input, = ctx.saved_tensors tmp = gelu_bwd(grad_output, input) return tmp fast_gelu_impl = FastGeLUFunction.apply @torch.jit.script def relu_bwd(g, x): return torch.where(x >= 0, g, 0.0).to(dtype=x.dtype) @torch.jit.script def sqrelu_fwd(x): r = F.relu(x) return (r * r).to(dtype=x.dtype) @torch.jit.script def sqrelu_bwd(g, x): return (2.0 * g * F.relu(x)).to(dtype=x.dtype)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/activations.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/__init__.py
# Copyright (c) 2023, Tri Dao. # Inspired by https://github.com/NVIDIA/apex/blob/master/apex/fused_dense/fused_dense.py # We make it work with pytorch amp and with bfloat16. # The TensorParallel linear modules are inspired by https://github.com/NVIDIA/apex/blob/master/apex/transformer/tensor_parallel/layers.py from typing import Optional from functools import partial import torch import torch.nn as nn import torch.nn.functional as F from torch import Tensor from torch.distributed import ProcessGroup from torch.cuda.amp import custom_bwd, custom_fwd # import fused_dense_cuda # from apex import fused_dense_lib as fused_dense_cuda from flash_attn.ops.activations import gelu_bwd, relu_bwd, sqrelu_fwd, sqrelu_bwd from flash_attn.utils.distributed import all_gather_raw, reduce_scatter_raw, all_reduce_raw from flash_attn.utils.distributed import reduce_scatter, all_reduce class FusedDenseFunc(torch.autograd.Function): @staticmethod @custom_fwd def forward(ctx, x, weight, bias, return_residual=False, process_group=None, sequence_parallel=True): """ If process_group is not None and sequence_parallel=True, we're doing Tensor Parallel with sequence parallelism: we do an all_gather_raw of x before doing the matmul. """ ctx.compute_weight_gradient = weight.requires_grad ctx.return_residual = return_residual ctx.process_group = process_group ctx.sequence_parallel = sequence_parallel if torch.is_autocast_enabled(): x = x.to(dtype=torch.get_autocast_gpu_dtype()) x = x.contiguous() if process_group is not None and sequence_parallel: # We want to kick off the all_gather early, before weight dtype conversion total_x, handle_x = all_gather_raw(x, process_group, async_op=True) else: total_x = x if torch.is_autocast_enabled(): weight = weight.to(dtype=torch.get_autocast_gpu_dtype()) bias = bias.to(dtype=torch.get_autocast_gpu_dtype()) if bias is not None else None weight = weight.contiguous() if process_group is not None and sequence_parallel: handle_x.wait() batch_shape, n = total_x.shape[:-1], total_x.shape[-1] batch_dim = batch_shape.numel() # https://github.com/pytorch/pytorch/blob/5b51849b48a7dbccd297286cc0110def4706f9e7/aten/src/ATen/native/cuda/Blas.cpp#L174 if min(batch_dim, n, *weight.shape) > 65535 * 32: raise RuntimeError('fused_dense only supports matrix dims <= 2M') output = F.linear(total_x, weight, bias) if ctx.compute_weight_gradient: ctx.save_for_backward(x, weight) else: ctx.save_for_backward(weight) return output if not return_residual else (output, x) @staticmethod @custom_bwd def backward(ctx, grad_output, *args): grad_output = grad_output.contiguous() if ctx.return_residual: grad_input, = args grad_input = grad_input.contiguous() process_group = ctx.process_group sequence_parallel = ctx.sequence_parallel if ctx.compute_weight_gradient: x, weight = ctx.saved_tensors if process_group is not None and sequence_parallel: total_x, handle_x = all_gather_raw(x, process_group, async_op=True) else: total_x = x else: weight, = ctx.saved_tensors total_x = None batch_shape = grad_output.shape[:-1] batch_dim = batch_shape.numel() grad_output = grad_output.reshape(batch_dim, grad_output.shape[-1]) if ctx.needs_input_grad[0]: if not ctx.return_residual: grad_input = F.linear(grad_output, weight.t()) else: grad_input = torch.addmm(grad_input.reshape(batch_dim, grad_input.shape[-1]), grad_output, weight) grad_input = grad_input.reshape(*batch_shape, grad_input.shape[-1]) if process_group is not None: reduce_fn = reduce_scatter_raw if sequence_parallel else all_reduce_raw grad_input, handle_grad_input = reduce_fn(grad_input, process_group, async_op=True) else: grad_input = None if ctx.needs_input_grad[1]: assert ctx.compute_weight_gradient if process_group is not None and sequence_parallel: handle_x.wait() grad_weight, grad_bias = fused_dense_cuda.linear_bias_wgrad( total_x.reshape(batch_dim, total_x.shape[-1]), grad_output, ctx.needs_input_grad[2] ) else: grad_weight = None grad_bias = grad_output if ctx.needs_input_grad[2] else None if process_group is not None and ctx.needs_input_grad[0]: handle_grad_input.wait() return grad_input, grad_weight, grad_bias, None, None, None def fused_dense_func(x: Tensor, weight: Tensor, bias: Optional[Tensor] = None, return_residual: bool = False, process_group: Optional[ProcessGroup] = None, sequence_parallel: bool = True): dtype_eligible = (x.dtype in [torch.float16, torch.bfloat16] or (x.dtype == torch.float32 and torch.is_autocast_enabled())) if x.is_cuda and weight.is_cuda and (bias is None or bias.is_cuda) and dtype_eligible: return FusedDenseFunc.apply(x, weight, bias, return_residual, process_group, sequence_parallel) else: assert process_group is None out = F.linear(x, weight, bias) return out if not return_residual else (out, x) class FusedDense(nn.Linear): def __init__(self, in_features: int, out_features: int, bias: bool = True, return_residual: bool = False, device=None, dtype=None) -> None: super().__init__(in_features, out_features, bias=bias, device=device, dtype=dtype) self.return_residual = return_residual def forward(self, x, process_group=None): """ If process_group is not None, we're doing Tensor Parallel with sequence parallelism: we do an all_gather of x before doing the matmul. """ return fused_dense_func(x, self.weight, self.bias, return_residual=self.return_residual, process_group=process_group) class ColumnParallelLinear(nn.Linear): def __init__(self, in_features: int, out_features: int, process_group: ProcessGroup, bias: bool = True, sequence_parallel=True, device=None, dtype=None) -> None: world_size = torch.distributed.get_world_size(process_group) if out_features % world_size != 0: raise ValueError(f'out_features ({out_features}) must be divisible by ' f'world_size ({world_size})') super().__init__(in_features, out_features // world_size, bias=bias, device=device, dtype=dtype) self.process_group = process_group self.sequence_parallel = sequence_parallel def forward(self, x): # If self.sequence_parallel is True, we're doing Tensor Parallel with sequence parallelism: # we do an all_gather of x before doing the matmul. # If not, then the input is already gathered. return fused_dense_func(x, self.weight, self.bias, process_group=self.process_group, sequence_parallel=self.sequence_parallel) class RowParallelLinear(nn.Linear): def __init__(self, in_features: int, out_features: int, process_group: ProcessGroup, bias: bool = True, sequence_parallel=True, device=None, dtype=None) -> None: world_size = torch.distributed.get_world_size(process_group) rank = torch.distributed.get_rank(process_group) if in_features % world_size != 0: raise ValueError(f'in_features ({in_features}) must be divisible by ' f'world_size ({world_size})') # Only rank 0 will have bias super().__init__(in_features // world_size, out_features, bias=bias and rank == 0, device=device, dtype=dtype) self.process_group = process_group self.sequence_parallel = sequence_parallel def forward(self, x): """ We're doing Tensor Parallel with sequence parallelism: we do the matmul and then a reduce_scatter of the result. """ out = fused_dense_func(x, self.weight, self.bias) reduce_fn = reduce_scatter if self.sequence_parallel else all_reduce return reduce_fn(out, self.process_group) class FusedMLPFunc(torch.autograd.Function): @staticmethod @custom_fwd def forward(ctx, x, weight1, bias1, weight2, bias2, activation='gelu_approx', save_pre_act=True, return_residual=False, checkpoint_lvl=0, heuristic=0, process_group=None, sequence_parallel=True): """ If process_group is not None and sequence_parallel=True, we're doing Tensor Parallel with sequence parallelism: we do an all_gather of x before doing the matmul. If sequence_parallel=False, then the input is already gathered. checkpoint_lvl: 0: no recomputation in the bwd 1: recompute gelu_out / relu_out in the bwd 2: recompute pre_act and gelu_out / relu_out in the bwd """ assert -1 <= heuristic <= 4 assert activation in ['gelu_approx', 'relu', 'sqrelu'] if activation == 'sqrelu': assert heuristic == -1 if not save_pre_act: checkpoint_lvl = 2 assert checkpoint_lvl in [0, 1, 2] ctx.return_residual = return_residual ctx.process_group = process_group ctx.sequence_parallel = sequence_parallel ctx.checkpoint_lvl = checkpoint_lvl ctx.activation = activation ctx.heuristic = heuristic if torch.is_autocast_enabled(): x = x.to(dtype=torch.get_autocast_gpu_dtype()) x = x.contiguous() if process_group is not None and sequence_parallel: # We want to kick off the all_gather early, before weight dtype conversion total_x, handle_x = all_gather_raw(x, process_group, async_op=True) else: total_x = x if torch.is_autocast_enabled(): dtype = torch.get_autocast_gpu_dtype() weight1, weight2 = [a.to(dtype=dtype) for a in [weight1, weight2]] bias1 = bias1.to(dtype=dtype) if bias1 is not None else None bias2 = bias2.to(dtype=dtype) if bias2 is not None else None weight1 = weight1.contiguous() bias1 = bias1.contiguous() if bias1 is not None else None weight2 = weight2.contiguous() bias2 = bias2.contiguous() if bias2 is not None else None if process_group is not None and sequence_parallel: handle_x.wait() batch_shape, n = total_x.shape[:-1], total_x.shape[-1] batch_dim = batch_shape.numel() # https://github.com/pytorch/pytorch/blob/5b51849b48a7dbccd297286cc0110def4706f9e7/aten/src/ATen/native/cuda/Blas.cpp#L174 if min(batch_dim, n, *weight1.shape, *weight2.shape) > 65535 * 32: raise RuntimeError('fused_dense only supports matrix dims <= 2M') if heuristic == -1: pre_act = F.linear(total_x, weight1, bias1) activation_fn = (partial(F.gelu, approximate='tanh') if activation == 'gelu_approx' else (sqrelu_fwd if activation == 'sqrelu' else F.relu)) with torch.jit.fuser('fuser2'): output1 = activation_fn(pre_act) # This is before adding bias1 # pre_act = F.linear(total_x.reshape(batch_dim, n), weight1) # with torch.jit.fuser('fuser2'): # output1 = bias_gelu(pre_act, bias1) else: is_gelu = activation == 'gelu_approx' output1, *rest = fused_dense_cuda.linear_act_forward( total_x.reshape(batch_dim, n), weight1, bias1, is_gelu, save_pre_act, heuristic ) if save_pre_act: pre_act = rest[0] output2 = F.linear(output1, weight2, bias2) if checkpoint_lvl == 0 or (checkpoint_lvl == 1 and activation == 'relu'): # For RELU the pre_act is very small (just a bit-mask) so we just save it ctx.save_for_backward(x, weight1, weight2, pre_act, output1) elif checkpoint_lvl == 1: ctx.save_for_backward(x, weight1, weight2, pre_act) elif checkpoint_lvl == 2: ctx.save_for_backward(x, weight1, weight2, bias1) output2 = output2.reshape(*batch_shape, output2.shape[-1]) return output2 if not return_residual else (output2, x) @staticmethod @custom_bwd def backward(ctx, grad_output, *args): grad_output = grad_output.contiguous() checkpoint_lvl = ctx.checkpoint_lvl activation = ctx.activation activation_fn = (partial(F.gelu, approximate='tanh') if activation == 'gelu_approx' else (sqrelu_fwd if activation == 'sqrelu' else F.relu)) if ctx.return_residual: grad_input, = args grad_input = grad_input.contiguous() process_group = ctx.process_group sequence_parallel = ctx.sequence_parallel x, weight1, weight2, *rest = ctx.saved_tensors if process_group is None or not sequence_parallel: total_x = x batch_shape = grad_output.shape[:-1] batch_dim = batch_shape.numel() if checkpoint_lvl in [0, 1]: if process_group is not None and sequence_parallel: total_x, handle_x = all_gather_raw(x, process_group, async_op=True) if checkpoint_lvl == 0 or (checkpoint_lvl == 1 and activation == 'relu'): pre_act, output1 = rest elif checkpoint_lvl == 1: pre_act, = rest with torch.jit.fuser('fuser2'): output1 = activation_fn(pre_act) elif checkpoint_lvl == 2: bias1, = rest if process_group is not None and sequence_parallel: total_x, _ = all_gather_raw(x, process_group) if ctx.heuristic == -1: pre_act = F.linear(total_x, weight1, bias1) with torch.jit.fuser('fuser2'): output1 = activation_fn(pre_act) else: output1, pre_act = fused_dense_cuda.linear_act_forward( total_x.reshape(batch_dim, total_x.shape[-1]), weight1, bias1, activation == 'gelu_approx', True, ctx.heuristic ) grad_output = grad_output.reshape(batch_dim, grad_output.shape[-1]) output1 = output1.reshape(batch_dim, output1.shape[-1]) pre_act = pre_act.reshape(batch_dim, pre_act.shape[-1]) if ctx.needs_input_grad[3]: grad_weight2, grad_bias2 = fused_dense_cuda.linear_bias_wgrad( output1, grad_output, ctx.needs_input_grad[4] ) else: grad_weight2 = None grad_bias2 = grad_output if ctx.needs_input_grad[4] else None if ctx.heuristic == -1: # grad_pre_act = matmul_dgelu(grad_output, weight2, pre_act) grad_output1 = F.linear(grad_output, weight2.t()) activation_grad_fn = (gelu_bwd if activation == 'gelu_approx' else (sqrelu_bwd if activation == 'sqrelu' else relu_bwd)) with torch.jit.fuser('fuser2'): grad_pre_act = activation_grad_fn(grad_output1, pre_act) else: # The cublasLt epilogue has to compute both gelu/relu grad and bias grad, we can't # just compute gelu/relu grad grad_pre_act, grad_bias1 = fused_dense_cuda.bias_act_linear_dgrad_bgrad( weight2, grad_output, pre_act, activation == 'gelu_approx', ctx.heuristic ) if not ctx.needs_input_grad[2]: grad_bias1 = None if ctx.needs_input_grad[0]: if not ctx.return_residual: grad_input = F.linear(grad_pre_act, weight1.t()) else: grad_input = torch.addmm(grad_input.reshape(batch_dim, grad_input.shape[-1]), grad_pre_act, weight1) grad_input = grad_input.reshape(*batch_shape, grad_input.shape[-1]) if process_group is not None: reduce_fn = reduce_scatter_raw if sequence_parallel else all_reduce_raw grad_input, handle_grad_input = reduce_fn(grad_input, process_group, async_op=True) else: grad_input = None if ctx.heuristic == -1: if ctx.needs_input_grad[1]: if process_group is not None and sequence_parallel: handle_x.wait() grad_weight1, grad_bias1 = fused_dense_cuda.linear_bias_wgrad( total_x.reshape(batch_dim, total_x.shape[-1]), grad_pre_act, ctx.needs_input_grad[2] ) else: grad_weight1 = None grad_bias1 = grad_pre_act if ctx.needs_input_grad[2] else None else: if ctx.needs_input_grad[1]: if process_group is not None and sequence_parallel: handle_x.wait() grad_weight1 = F.linear(grad_pre_act.t(), total_x.reshape(batch_dim, total_x.shape[-1]).t()) else: grad_weight1 = None if process_group is not None and ctx.needs_input_grad[0]: handle_grad_input.wait() return (grad_input, grad_weight1, grad_bias1, grad_weight2, grad_bias2, None, None, None, None, None, None, None) def fused_mlp_func( x: Tensor, weight1: Tensor, weight2: Tensor, bias1: Optional[Tensor] = None, bias2: Optional[Tensor] = None, activation: str = 'gelu_approx', save_pre_act: bool = True, return_residual: bool = False, checkpoint_lvl: int = 0, heuristic: int = 0, process_group: Optional[ProcessGroup] = None, sequence_parallel: bool = True ): assert activation in ['gelu_approx', 'relu', 'sqrelu'] dtype_eligible = (x.dtype in [torch.float16, torch.bfloat16] or (x.dtype == torch.float32 and torch.is_autocast_enabled())) # If we save pre-activation, dimension must be divisible by 128 (relu) or 8 (gelu) dim_eligible = not save_pre_act or (x.shape[-1] % (128 if activation == 'relu' else 8) == 0) if (x.is_cuda and weight1.is_cuda and weight2.is_cuda and (bias1 is None or bias1.is_cuda) and (bias2 is None or bias2.is_cuda) and dtype_eligible and dim_eligible): return FusedMLPFunc.apply( x, weight1, bias1, weight2, bias2, activation, save_pre_act, return_residual, checkpoint_lvl, heuristic, process_group, sequence_parallel ) else: assert process_group is None pre_act = F.linear(x, weight1, bias1) activation_fn = (partial(F.gelu, approximate='tanh') if activation == 'gelu_approx' else partial(F.relu, inplace=True)) output1 = activation_fn(pre_act) output2 = F.linear(output1, weight2, bias2) return output2 if not return_residual else (output2, x) class FusedMLP(nn.Module): def __init__(self, in_features, hidden_features, out_features=None, bias1=True, bias2=True, activation='gelu_approx', return_residual=False, checkpoint_lvl=0, heuristic='auto', device=None, dtype=None): """ If process_group is not None, we're doing Tensor Parallel with sequence parallelism: we do an all_gather of x before doing the matmul, gelu, then matmul. Finally we do a reduce_scatter of the output. checkpoint_lvl (increasing lvl means slower but more memory saving): 0: no recomputation in the bwd 1: recompute gelu_out in the bwd 2: recompute pre_act and gelu_out in the bwd heuristic: -1: don't fuse gemm + gelu (separate kernel) 0..4: use this heuristic for the algo section in the fused gemm + gelu 'auto': heuristic will be picked automatically: For CUDA >= 11.8, we set heuristic=0 for both fp16 and bf16 for best perf. For CUDA <= 11.7, we set heuristic=1 for fp16 and heuristic=-1 for bf16. For H100, we set heuristic=-1 for both fp16 and bf16 as the fused cuBlasLt implementation is slower than the unfused version. return_residual: whether to return the input x along with the output. This is for performance reason: for post-norm architecture, returning the input allows us to fuse the backward of nn.Linear with the residual connection. """ assert checkpoint_lvl in [0, 1, 2] assert activation in ['gelu_approx', 'relu', 'sqrelu'] factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() if out_features is None: out_features = in_features self.activation = activation self.return_residual = return_residual self.checkpoint_lvl = checkpoint_lvl self.heuristic = heuristic if activation != 'sqrelu' else -1 self.fc1 = nn.Linear(in_features, hidden_features, bias=bias1, **factory_kwargs) self.fc2 = nn.Linear(hidden_features, out_features, bias=bias2, **factory_kwargs) def forward(self, x, process_group=None): dtype = x.dtype if not torch.is_autocast_enabled() else torch.get_autocast_gpu_dtype() if self.heuristic == 'auto': if self.activation == 'gelu_approx': if torch.cuda.get_device_capability('cuda') == (9, 0): heuristic = -1 else: cuda_ver = tuple(map(int, torch.version.cuda.split('.'))) heuristic = 0 if cuda_ver >= (11, 8) else (1 if dtype == torch.float16 else -1) else: heuristic = 0 else: heuristic = self.heuristic out = fused_mlp_func( x, self.fc1.weight, self.fc2.weight, self.fc1.bias, self.fc2.bias, activation=self.activation, save_pre_act=self.training, return_residual=self.return_residual, checkpoint_lvl=self.checkpoint_lvl, heuristic=heuristic, process_group=process_group ) if self.return_residual: out, x = out if process_group is not None: out = reduce_scatter(out, process_group) return out if not self.return_residual else (out, x) class ParallelFusedMLP(nn.Module): def __init__(self, in_features, hidden_features, out_features=None, activation='gelu_approx', process_group: ProcessGroup = None, bias1=True, bias2=True, sequence_parallel=True, checkpoint_lvl=0, heuristic='auto', device=None, dtype=None): """ process_group is required. We're doing Tensor Parallel with sequence parallelism: we do an all_gather of x before doing the matmul, gelu, then matmul. Finally we do a reduce_scatter of the output. checkpoint_lvl (increasing lvl means slower but more memory saving): 0: no recomputation in the bwd 1: recompute gelu_out in the bwd 2: recompute pre_act and gelu_out in the bwd heuristic: -1: don't fuse gemm + gelu (separate kernel) 0..4: use this heuristic for the algo section in the fused gemm + gelu 'auto': heuristic will be picked automatically: For CUDA >= 11.8, we set heuristic=0 for both fp16 and bf16 for best perf. For CUDA <= 11.7, we set heuristic=1 for fp16 and heuristic=-1 for bf16. """ assert checkpoint_lvl in [0, 1, 2] assert activation in ['gelu_approx', 'relu', 'sqrelu'] assert process_group is not None factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() if out_features is None: out_features = in_features self.activation = activation self.process_group = process_group self.sequence_parallel = sequence_parallel self.checkpoint_lvl = checkpoint_lvl self.heuristic = heuristic if activation != 'sqrelu' else -1 self.fc1 = ColumnParallelLinear(in_features, hidden_features, process_group, bias=bias1, **factory_kwargs) self.fc2 = RowParallelLinear(hidden_features, out_features, process_group, bias=bias2, **factory_kwargs) def forward(self, x): dtype = x.dtype if not torch.is_autocast_enabled() else torch.get_autocast_gpu_dtype() if self.heuristic == 'auto': if self.activation == 'gelu_approx': cuda_ver = tuple(map(int, torch.version.cuda.split('.'))) heuristic = 0 if cuda_ver >= (11, 8) else (1 if dtype == torch.float16 else -1) else: heuristic = 0 else: heuristic = self.heuristic out = fused_mlp_func( x, self.fc1.weight, self.fc2.weight, self.fc1.bias, self.fc2.bias, activation=self.activation, save_pre_act=self.training, checkpoint_lvl=self.checkpoint_lvl, heuristic=heuristic, process_group=self.process_group, sequence_parallel=self.sequence_parallel ) reduce_fn = reduce_scatter if self.sequence_parallel else all_reduce return reduce_fn(out, self.process_group)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/fused_dense.py
# Copyright (c) 2022, Tri Dao. # Adapted from https://github.com/NVIDIA/apex/blob/master/apex/contrib/layer_norm/layer_norm.py import torch from torch.nn import init from flash_attn.ops.layer_norm import DropoutAddLayerNormFn, DropoutAddLayerNormSubsetFn from flash_attn.ops.layer_norm import DropoutAddLayerNormParallelResidualFn def rms_norm(x, weight, epsilon): return DropoutAddLayerNormFn.apply(x, None, weight, None, None, None, 0.0, epsilon, False, False, True) def dropout_add_rms_norm(x0, residual, weight, bias, dropout_p, epsilon, rowscale=None, layerscale=None, prenorm=False, residual_in_fp32=False, return_dropout_mask=False): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormFn.apply( x0, residual, weight, bias, rowscale, layerscale, dropout_p, epsilon, residual_in_fp32, prenorm, True, return_dropout_mask ) def dropout_add_rms_norm_subset(x0, residual, weight, bias, dropout_p, epsilon, layerscale=None, x0_subset=None, out_subset=None, rowscale_const=1.0, out_numrows=0, prenorm=False, residual_in_fp32=False, return_dropout_mask=False): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormSubsetFn.apply( x0, residual, weight, bias, layerscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, residual_in_fp32, prenorm, True, return_dropout_mask ) def dropout_add_rms_norm_parallel_residual( x0, x1, residual, weight0, bias0, weight1, bias1, dropout_p, epsilon, prenorm=False, residual_in_fp32=False, return_dropout_mask=False ): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormParallelResidualFn.apply( x0, x1, residual, weight0, bias0, weight1, bias1, dropout_p, epsilon, residual_in_fp32, prenorm, True, return_dropout_mask ) class DropoutAddRMSNorm(torch.nn.Module): def __init__(self, hidden_size, prenorm=False, p=0.0, eps=1e-5, residual_in_fp32=False, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.prenorm = prenorm self.p = p self.epsilon = eps self.residual_in_fp32 = residual_in_fp32 self.weight = torch.nn.Parameter(torch.empty(hidden_size, **factory_kwargs)) self.register_parameter('bias', None) self.reset_parameters() def reset_parameters(self): init.ones_(self.weight) def forward(self, x0, residual=None): return dropout_add_rms_norm(x0, residual, self.weight, None, self.p if self.training else 0.0, self.epsilon, prenorm=self.prenorm, residual_in_fp32=self.residual_in_fp32)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/rms_norm.py
# Copyright (c) 2022, Tri Dao. # Adapted from https://github.com/NVIDIA/apex/blob/master/apex/contrib/layer_norm/layer_norm.py import torch from torch.nn import init import dropout_layer_norm def _dropout_add_layer_norm_forward(x0, residual, gamma, beta, rowscale, colscale, dropout_p, epsilon, residual_in_fp32=False, is_rms_norm=False): """ Assume that arguments are contiguous """ hidden_size = gamma.numel() x0mat = x0.view((-1, hidden_size)) residualmat = residual.view((-1, hidden_size)) if residual is not None else None rowscale = rowscale.view(-1) if rowscale is not None else None zmat, xmat, dmask, mu, rsigma = dropout_layer_norm.dropout_add_ln_fwd( x0mat, residualmat, gamma, beta, rowscale, colscale, None, None, dropout_p, epsilon, 1.0, 0, None, residual_in_fp32, is_rms_norm ) # dmask is None if dropout_p == 0.0 # xmat is None if dropout_p == 0.0 and residual is None and residual_dtype != input_dtype return zmat, xmat if xmat is not None else x0mat, dmask, mu, rsigma def _dropout_add_layer_norm_backward(dz, dx, x, x0, dmask, mu, rsigma, gamma, rowscale, colscale, dropout_p, has_residual, is_rms_norm=False): """ Assume that arguments are contiguous dx == None means that it was a post-norm architecture (x = drop(x0) + residual was not returned in the fwd). x0 must not be None if we have colscale. """ hidden_size = gamma.numel() xmat = x.view((-1, hidden_size)) dzmat = dz.view(xmat.shape) dxmat = dx.view(xmat.shape) if dx is not None else None x0mat = x0.view((-1, hidden_size)) if x0 is not None else None rowscale = rowscale.view(-1) if rowscale is not None else None if colscale is not None: assert x0 is not None, 'x0 is required to compute the gradient of colscale' dx0mat, dresidualmat, dgamma, dbeta, _, _, *rest = dropout_layer_norm.dropout_add_ln_bwd( dzmat, dxmat, xmat, x0mat, dmask, mu, rsigma, gamma, rowscale, colscale, None, None, dropout_p, 1.0, 0, has_residual, is_rms_norm ) # dresidualmat is None if not has_residual if colscale is None: return dx0mat, dresidualmat, dgamma, dbeta else: dcolscale = rest[0] return dx0mat, dresidualmat, dgamma, dbeta, dcolscale def _dropout_add_layer_norm_subset_forward(x0, residual, gamma, beta, colscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, residual_in_fp32=False, is_rms_norm=False): """ Assume that arguments are contiguous """ hidden_size = gamma.numel() x0mat = x0.view((-1, hidden_size)) residualmat = residual.view((-1, hidden_size)) if residual is not None else None x0_subset = x0_subset.view(-1) if x0_subset is not None else None out_subset = out_subset.view(-1) if out_subset is not None else None zmat, xmat, dmask, mu, rsigma = dropout_layer_norm.dropout_add_ln_fwd( x0mat, residualmat, gamma, beta, None, colscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, None, residual_in_fp32, is_rms_norm ) # dmask is None if dropout_p == 0.0 # xmat is None if dropout_p == 0.0 and residual is None and residual_dtype != input_dtype return zmat, xmat if xmat is not None else x0mat, dmask, mu, rsigma def _dropout_add_layer_norm_subset_backward(dz, dx, x, x0, dmask, mu, rsigma, gamma, colscale, x0_subset, out_subset, dropout_p, rowscale_const, x0_numrows, has_residual, is_rms_norm=False): """ Assume that arguments are contiguous dx == None means that it was a post-norm architecture (x = drop(x0) + residual was not returned in the fwd). x0 must not be None if we have colscale. """ hidden_size = gamma.numel() xmat = x.view((-1, hidden_size)) dzmat = dz.view(-1, hidden_size) dxmat = dx.view(xmat.shape) if dx is not None else None x0mat = x0.view((-1, hidden_size)) if x0 is not None else None x0_subset = x0_subset.view(-1) if x0_subset is not None else None out_subset = out_subset.view(-1) if out_subset is not None else None if colscale is not None: assert x0 is not None, 'x0 is required to compute the gradient of colscale' dx0mat, dresidualmat, dgamma, dbeta, _, _, *rest = dropout_layer_norm.dropout_add_ln_bwd( dzmat, dxmat, xmat, x0mat, dmask, mu, rsigma, gamma, None, colscale, x0_subset, out_subset, dropout_p, rowscale_const, x0_numrows, has_residual, is_rms_norm ) # dresidualmat is None if not has_residual if colscale is None: return dx0mat, dresidualmat, dgamma, dbeta else: dcolscale = rest[0] return dx0mat, dresidualmat, dgamma, dbeta, dcolscale def _dropout_add_layer_norm_parallel_residual_forward( x0, x1, residual, gamma0, beta0, gamma1, beta1, dropout_p, epsilon, residual_in_fp32=False, is_rms_norm=False ): """ Assume that arguments are contiguous """ hidden_size = gamma0.numel() x0mat = x0.view((-1, hidden_size)) x1mat = x1.view((-1, hidden_size)) if x1 is not None else None residualmat = residual.view((-1, hidden_size)) if residual is not None else None z0mat, z1mat, xmat, dmask0, dmask1, mu, rsigma = dropout_layer_norm.dropout_add_ln_parallel_residual_fwd( x0mat, x1mat, residualmat, gamma0, beta0, gamma1, beta1, dropout_p, epsilon, None, residual_in_fp32, is_rms_norm ) # dmask0 and dmask1 are None if dropout_p == 0.0 # xmat is None if dropout_p == 0.0 and residual is None and residual_dtype != input_dtype return z0mat, z1mat, xmat if xmat is not None else x0mat, dmask0, dmask1, mu, rsigma def _dropout_add_layer_norm_parallel_residual_backward( dz0, dz1, dx, x, dmask0, dmask1, mu, rsigma, gamma0, gamma1, dropout_p, has_x1, has_residual, is_rms_norm=False ): """ Assume that arguments are contiguous dx == None means that it was a post-norm architecture (x = drop(x0) + residual was not returned in the fwd). """ hidden_size = gamma0.numel() xmat = x.view((-1, hidden_size)) dz0mat = dz0.view(xmat.shape) dz1mat = dz1.view(xmat.shape) if dz1 is not None else None dxmat = dx.view(xmat.shape) if dx is not None else None dx0mat, dx1mat, dresidualmat, dgamma0, dbeta0, dgamma1, dbeta1, *rest = dropout_layer_norm.dropout_add_ln_parallel_residual_bwd( dz0mat, dz1mat, dxmat, xmat, dmask0, dmask1, mu, rsigma, gamma0, gamma1, dropout_p, has_x1, has_residual, is_rms_norm ) # dresidualmat is None if not has_residual return dx0mat, dx1mat, dresidualmat, dgamma0, dbeta0, dgamma1, dbeta1 class DropoutAddLayerNormFn(torch.autograd.Function): @staticmethod def forward(ctx, x0, residual, gamma, beta, rowscale, colscale, dropout_p, epsilon, residual_in_fp32=False, prenorm=False, is_rms_norm=False, return_dmask=False): x0 = x0.contiguous() residual = residual.contiguous() if residual is not None else None gamma = gamma.contiguous() beta = beta.contiguous() if beta is not None else None rowscale = rowscale.contiguous() if rowscale is not None else None colscale = colscale.contiguous() if colscale is not None else None zmat, xmat, dmask, mu, rsigma = _dropout_add_layer_norm_forward( x0, residual, gamma, beta, rowscale, colscale, dropout_p, epsilon, residual_in_fp32, is_rms_norm ) # Only need to save x0 if we need to compute gradient wrt colscale x0_saved = x0 if colscale is not None else None ctx.save_for_backward(xmat.view(x0.shape), x0_saved, dmask, gamma, mu, rsigma, rowscale, colscale) ctx.prenorm = prenorm ctx.dropout_p = dropout_p ctx.has_residual = residual is not None ctx.is_rms_norm = is_rms_norm ctx.has_beta = beta is not None if not return_dmask: return (zmat.view(x0.shape) if not prenorm else (zmat.view(x0.shape), xmat.view(x0.shape))) else: dmask = (dmask.view(x0.shape) if dropout_p > 0. else torch.ones(x0.shape, dtype=torch.uint8, device=x0.device)) ctx.mark_non_differentiable(dmask) return ((zmat.view(x0.shape), dmask) if not prenorm else (zmat.view(x0.shape), xmat.view(x0.shape), dmask)) @staticmethod def backward(ctx, dz, *args): # assert dz.is_contiguous() dz = dz.contiguous() # this happens! dx = args[0].contiguous() if ctx.prenorm else None x, x0, dmask, gamma, mu, rsigma, rowscale, colscale = ctx.saved_tensors # x0 is None if colscale is None dropout_p = ctx.dropout_p has_residual = ctx.has_residual dx0mat, dresidualmat, dgamma, dbeta, *rest = _dropout_add_layer_norm_backward( dz, dx, x, x0, dmask, mu, rsigma, gamma, rowscale, colscale, dropout_p, has_residual, ctx.is_rms_norm ) dx0 = dx0mat.view(x.shape) dresidual = dresidualmat.view(x.shape) if dresidualmat is not None else None dcolscale = rest[0] if colscale is not None else None return (dx0, dresidual, dgamma, dbeta if ctx.has_beta else None, None, dcolscale, None, None, None, None, None, None) class DropoutAddLayerNormSubsetFn(torch.autograd.Function): @staticmethod def forward(ctx, x0, residual, gamma, beta, colscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, residual_in_fp32=False, prenorm=False, is_rms_norm=False, return_dmask=False): x0 = x0.contiguous() residual = residual.contiguous() if residual is not None else None gamma = gamma.contiguous() beta = beta.contiguous() if beta is not None else None colscale = colscale.contiguous() if colscale is not None else None zmat, xmat, dmask, mu, rsigma = _dropout_add_layer_norm_subset_forward( x0, residual, gamma, beta, colscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, residual_in_fp32, is_rms_norm ) # Only need to save x0 if we need to compute gradient wrt colscale x0_saved = x0 if colscale is not None else None x_shape = (-1, *x0.shape[1:]) ctx.save_for_backward(xmat.view(x_shape), x0_saved, dmask, gamma, mu, rsigma, colscale, x0_subset, out_subset) ctx.prenorm = prenorm ctx.dropout_p = dropout_p ctx.rowscale_const = rowscale_const ctx.x0_numrows = x0.shape[:-1].numel() ctx.has_residual = residual is not None ctx.is_rms_norm = is_rms_norm ctx.has_beta = beta is not None z_shape = (-1, *x0.shape[1:]) if not return_dmask: return (zmat.view(z_shape) if not prenorm else (zmat.view(z_shape), xmat.view(x0.shape))) else: z = zmat.view(z_shape) dmask = (dmask.view(x0.shape) if dropout_p > 0. else torch.ones(x0.shape, dtype=torch.uint8, device=x0.device)) ctx.mark_non_differentiable(dmask) return ((z, dmask) if not prenorm else (z, xmat.view(x_shape), dmask)) @staticmethod def backward(ctx, dz, *args): # assert dz.is_contiguous() dz = dz.contiguous() # this happens! dx = args[0].contiguous() if ctx.prenorm else None x, x0, dmask, gamma, mu, rsigma, colscale, x0_subset, out_subset = ctx.saved_tensors # x0 is None if colscale is None dropout_p = ctx.dropout_p has_residual = ctx.has_residual dx0mat, dresidualmat, dgamma, dbeta, *rest = _dropout_add_layer_norm_subset_backward( dz, dx, x, x0, dmask, mu, rsigma, gamma, colscale, x0_subset, out_subset, dropout_p, ctx.rowscale_const, ctx.x0_numrows, has_residual, ctx.is_rms_norm ) dx0 = dx0mat.view(-1, *x.shape[1:]) dresidual = dresidualmat.view(x.shape) if dresidualmat is not None else None dcolscale = rest[0] if colscale is not None else None return (dx0, dresidual, dgamma, dbeta if ctx.has_beta else None, dcolscale, None, None, None, None, None, None, None, None, None, None) class DropoutAddLayerNormParallelResidualFn(torch.autograd.Function): @staticmethod def forward(ctx, x0, x1, residual, gamma0, beta0, gamma1, beta1, dropout_p, epsilon, residual_in_fp32=False, prenorm=False, is_rms_norm=False, return_dmask=False): x0 = x0.contiguous() x1 = x1.contiguous() if x1 is not None else None residual = residual.contiguous() if residual is not None else None gamma0 = gamma0.contiguous() beta0 = beta0.contiguous() if beta0 is not None else None gamma1 = gamma1.contiguous() if gamma1 is not None else None beta1 = beta1.contiguous() if beta1 is not None else None z0mat, z1mat, xmat, dmask0, dmask1, mu, rsigma = _dropout_add_layer_norm_parallel_residual_forward( x0, x1, residual, gamma0, beta0, gamma1, beta1, dropout_p, epsilon, residual_in_fp32, is_rms_norm ) ctx.save_for_backward(xmat.view(x0.shape), dmask0, dmask1, gamma0, gamma1, mu, rsigma) ctx.prenorm = prenorm ctx.dropout_p = dropout_p ctx.has_x1 = x1 is not None ctx.has_residual = residual is not None ctx.is_rms_norm = is_rms_norm ctx.has_beta = beta0 is not None z = (z0mat.view(x0.shape), z1mat.view(x0.shape) if z1mat is not None else None) if not return_dmask: return z if not prenorm else (*z, xmat.view(x0.shape)) else: dmask0 = (dmask0.view(x0.shape) if dropout_p > 0. else torch.ones(x0.shape, dtype=torch.uint8, device=x0.device)) dmask1 = (dmask1.view(x0.shape) if dropout_p > 0. and x1 is not None else torch.ones(x0.shape, dtype=torch.uint8, device=x0.device)) ctx.mark_non_differentiable(dmask0) ctx.mark_non_differentiable(dmask1) return (*z, dmask0, dmask1) if not prenorm else (*z, xmat.view(x0.shape), dmask0, dmask1) @staticmethod def backward(ctx, dz0, dz1, *args): dz0 = dz0.contiguous() # this happens! dz1 = dz1.contiguous() if dz1 is not None else None dx = args[0].contiguous() if ctx.prenorm else None x, dmask0, dmask1, gamma0, gamma1, mu, rsigma = ctx.saved_tensors dropout_p = ctx.dropout_p has_x1 = ctx.has_x1 has_residual = ctx.has_residual dx0mat, dx1mat, dresidualmat, dgamma0, dbeta0, dgamma1, dbeta1 = _dropout_add_layer_norm_parallel_residual_backward( dz0, dz1, dx, x, dmask0, dmask1, mu, rsigma, gamma0, gamma1, dropout_p, has_x1, has_residual, ctx.is_rms_norm ) dx0 = dx0mat.view(x.shape) dx1 = dx1mat.view(x.shape) if dx1mat is not None else None dresidual = dresidualmat.view(x.shape) if dresidualmat is not None else None return (dx0, dx1, dresidual, dgamma0, dbeta0 if ctx.has_beta else None, dgamma1, dbeta1 if ctx.has_beta else None, None, None, None, None, None, None) def layer_norm(x, weight, bias, epsilon): return DropoutAddLayerNormFn.apply(x, None, weight, bias, None, None, 0.0, epsilon, False) def dropout_add_layer_norm(x0, residual, weight, bias, dropout_p, epsilon, rowscale=None, layerscale=None, prenorm=False, residual_in_fp32=False, return_dropout_mask=False): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormFn.apply( x0, residual, weight, bias, rowscale, layerscale, dropout_p, epsilon, residual_in_fp32, prenorm, False, return_dropout_mask ) def dropout_add_layer_norm_subset(x0, residual, weight, bias, dropout_p, epsilon, layerscale=None, x0_subset=None, out_subset=None, rowscale_const=1.0, out_numrows=0, prenorm=False, residual_in_fp32=False, return_dropout_mask=False): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormSubsetFn.apply( x0, residual, weight, bias, layerscale, x0_subset, out_subset, dropout_p, epsilon, rowscale_const, out_numrows, residual_in_fp32, prenorm, False, return_dropout_mask ) def dropout_add_layer_norm_parallel_residual( x0, x1, residual, weight0, bias0, weight1, bias1, dropout_p, epsilon, prenorm=False, residual_in_fp32=False, return_dropout_mask=False ): """residual_in_fp32 only has an effect if residual is None. Otherwise residual dtype is residual.dtype. """ return DropoutAddLayerNormParallelResidualFn.apply( x0, x1, residual, weight0, bias0, weight1, bias1, dropout_p, epsilon, residual_in_fp32, prenorm, False, return_dropout_mask ) class DropoutAddLayerNorm(torch.nn.Module): def __init__(self, hidden_size, prenorm=False, p=0.0, eps=1e-5, residual_in_fp32=False, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.prenorm = prenorm self.p = p self.epsilon = eps self.residual_in_fp32 = residual_in_fp32 self.weight = torch.nn.Parameter(torch.empty(hidden_size, **factory_kwargs)) self.bias = torch.nn.Parameter(torch.empty(hidden_size, **factory_kwargs)) self.reset_parameters() def reset_parameters(self): init.ones_(self.weight) init.zeros_(self.bias) def forward(self, x0, residual=None): return dropout_add_layer_norm(x0, residual, self.weight, self.bias, self.p if self.training else 0.0, self.epsilon, prenorm=self.prenorm, residual_in_fp32=self.residual_in_fp32)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/layer_norm.py
# Adapted on https://github.com/ELS-RD/kernl/blob/main/src/kernl/implementations/linear_layer.py # and https://github.com/openai/triton/blob/master/python/triton/ops/matmul.py from typing import Optional import torch import triton import triton.language as tl from torch.autograd.function import FunctionCtx from torch.cuda.amp import custom_fwd from triton.ops.matmul_perf_model import early_config_prune, estimate_matmul_time from flash_attn.ops.triton.k_activations import gelu, gelu_grad, gelu_approx, gelu_approx_grad, squared_relu, squared_relu_grad # CREDITS: Initially inspired by the Triton tutorial on matrix multiplications def init_to_zero(name): return lambda nargs: nargs[name].zero_() def get_configs_io_bound(): configs = [] for num_stages in [2, 3, 4, 5, 6]: for block_m in [16, 32]: for block_k in [32, 64]: for block_n in [32, 64, 128, 256]: num_warps = 2 if block_n <= 64 else 4 configs.append( triton.Config( {"BLOCK_M": block_m, "BLOCK_N": block_n, "BLOCK_K": block_k, "SPLIT_K": 1}, num_stages=num_stages, num_warps=num_warps, ) ) # split_k not used # for split_k in [2, 4, 8, 16]: # configs.append(triton.Config( # {'BLOCK_M': block_m, 'BLOCK_N': block_n, 'BLOCK_K': block_k, 'SPLIT_K': split_k}, # num_stages=num_stages, num_warps=num_warps, pre_hook=init_to_zero('C'))) return configs @triton.autotune( configs=[ triton.Config({"BLOCK_M": 128, "BLOCK_N": 256, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 256, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 32, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 32, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=5, num_warps=2), # good for int8 triton.Config({"BLOCK_M": 128, "BLOCK_N": 256, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 128, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 256, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 32, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 32, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=5, num_warps=2), ] + get_configs_io_bound(), key=["CACHE_KEY_M", "CACHE_KEY_N", "CACHE_KEY_K"], prune_configs_by={"early_config_prune": early_config_prune, "perf_model": estimate_matmul_time, "top_k": 10}, ) @triton.heuristics( { "EVEN_K": lambda args: args["K"] % (args["BLOCK_K"] * args["SPLIT_K"]) == 0, } ) @triton.jit def kernel_fwd( C, # Pointers to matrices ACT_INPUT, A, B, bias, # Matrix dimensions M, N, K, CACHE_KEY_M, CACHE_KEY_N, CACHE_KEY_K, # The stride variables represent how much to increase the ptr by when moving by 1 # element in a particular dimension. E.g. stride_am is how much to increase a_ptr # by to get the element one row down (A has M rows) stride_cm, # stride_cn, # Assume that stride_cn == 1 stride_am, stride_ak, stride_bn, stride_bk, # Meta-parameters BLOCK_M: tl.constexpr, GROUP_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, # split k not used, not performant with activation, kept because early_config_prune is expecting it SPLIT_K: tl.constexpr, EVEN_K: tl.constexpr, A_ROWMAJOR: tl.constexpr, B_COLMAJOR: tl.constexpr, BIAS: tl.constexpr, SAVE_ACT_INPUT: tl.constexpr, ACTIVATION: tl.constexpr, ): """ Kernel for computing Out = activation(A x W + C) - Input has shape (M, K) - Weight has shape (K, N) - Bias has shape (N,) - Output has shape (M, N) - ActInputs (optional) has shape (M, N) 'ActInputs' optionally saves the A x W + C intermediate for backward computations This kernel will consolidate over K """ pid = tl.program_id(axis=0) grid_m = (M + BLOCK_M - 1) // BLOCK_M grid_n = (N + BLOCK_N - 1) // BLOCK_N # re-order program ID for better L2 performance width = GROUP_M * grid_n group_id = pid // width group_size = min(grid_m - group_id * GROUP_M, GROUP_M) pid_m = group_id * GROUP_M + (pid % group_size) pid_n = (pid % width) // (group_size) # now compute the block that each program will go through # rm (resp. rn) denotes a range of indices # for rows (resp. col) of C rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) # trick to avoid masking on M and N axis ram = tl.max_contiguous(tl.multiple_of(rm % M, BLOCK_M), BLOCK_M) rbn = tl.max_contiguous(tl.multiple_of(rn % N, BLOCK_N), BLOCK_N) rk = tl.arange(0, BLOCK_K) if A_ROWMAJOR: A = A + (ram[:, None] * stride_am + rk[None, :]) else: A = A + (ram[:, None] * stride_am + rk[None, :] * stride_ak) if B_COLMAJOR: B = B + (rk[:, None] + rbn[None, :] * stride_bn) else: B = B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn) acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32) for k in range(K, 0, -BLOCK_K): if EVEN_K: a = tl.load(A) b = tl.load(B) else: a = tl.load(A, mask=rk[None, :] < k, other=0.0) b = tl.load(B, mask=rk[:, None] < k, other=0.0) acc += tl.dot(a, b) if A_ROWMAJOR: A += BLOCK_K else: A += BLOCK_K * stride_ak if B_COLMAJOR: B += BLOCK_K else: B += BLOCK_K * stride_bk # Putting bias after the matmul (instead of before) is faster, idk why if BIAS: bias = tl.load(bias + rn, mask=rn < N, other=0.0).to(tl.float32) acc += bias[None, :] # optional: save the activation inputs if SAVE_ACT_INPUT: # act_in_ptrs = ACT_INPUT + ram[:, None] * stride_cm + rbn[None, :] * stride_cn act_in_ptrs = ACT_INPUT + ram[:, None] * stride_cm + rbn[None, :] tl.store(act_in_ptrs, acc) # optional: fused activation (while the data is in shared memory) if ACTIVATION == "gelu": acc = gelu(acc) elif ACTIVATION == "gelu_approx": acc = gelu_approx(acc) elif ACTIVATION == "squared_relu": acc = squared_relu(acc) # rematerialize rm and rn to save registers rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) # write back result # C = C + rm[:, None] * stride_cm + rn[None, :] * stride_cn C = C + rm[:, None] * stride_cm + rn[None, :] mask = (rm < M)[:, None] & (rn < N)[None, :] tl.store(C, acc) def triton_linear_act( x: torch.Tensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, activation: str = 'id', save_act_input: bool = False, ) -> torch.Tensor: """ Compute e = activation(x @ weight.T + bias). This wrapper kicks the `kernel_fwd` Triton kernel :param x: input tensor :param weight: weight matrix :param bias: an optional bias tensor :param activation: Activation name. Needs to be a Triton kernel. :param act_input: an optional tensor to save the activation inputs (for backward) :return: result tensor """ # if torch.is_autocast_enabled(): # dtype = torch.get_autocast_gpu_dtype() # x, weight, bias = [a.to(dtype=dtype) for a in [x, weight, bias]] assert activation in ['id', 'gelu', 'gelu_approx', 'squared_relu'] batch_shape, n = x.shape[:-1], x.shape[-1] batch_dim = batch_shape.numel() x_reshaped = x.reshape(batch_dim, n) if x_reshaped.stride(0) > 1 and x_reshaped.stride(1) > 1: x_reshaped = x_reshaped.contiguous() if weight.stride(0) > 1 and weight.stride(1) > 1: weight = weight.contiguous() bias = bias.contiguous() if bias is not None else None assert x.dtype == weight.dtype, f"Input and weight must have the same dtype, got {x.dtype} and {weight.dtype}" if bias is not None: assert x.dtype == bias.dtype, f"Input and bias must have the same dtype, got {x.dtype} and {bias.dtype}" assert x_reshaped.shape[1] == weight.shape[1], f"Incompatible dimensions: {x_reshaped.shape} - {weight.shape}" assert bias is None or bias.shape[0] == weight.shape[0], "Incompatible dimensions in between weight and bias" M, K = x_reshaped.shape N, K = weight.shape output = torch.empty((M, N), device=x.device, dtype=x.dtype) act_input = torch.empty_like(output) if save_act_input else None # 1D launch kernel where each block gets its own program. grid = lambda META: (triton.cdiv(M, META["BLOCK_M"]) * triton.cdiv(N, META["BLOCK_N"]),) # noqa kernel_fwd[grid]( output, act_input, x_reshaped, weight, # data ptrs bias if bias is not None else x, # auto skip bias if not present M, # shapes N, K, M // 32, # key for triton cache (limit number of compilations) N // 32, K // 32, stride_cm=output.stride(0), # strides # stride_cn=output.stride(1), stride_am=x_reshaped.stride(0), stride_ak=x_reshaped.stride(1), stride_bk=weight.stride(1), stride_bn=weight.stride(0), BIAS=bias is not None, # optional fused bias SAVE_ACT_INPUT=save_act_input, # optional save activation inputs ACTIVATION=activation, # optional fused activation A_ROWMAJOR=x_reshaped.stride(1) == 1, B_COLMAJOR=weight.stride(1) == 1, GROUP_M=8, # speed optimization: group the programs ) if not save_act_input: return output.reshape(*batch_shape, output.shape[-1]) else: return (output.reshape(*batch_shape, output.shape[-1]), act_input.reshape(*batch_shape, act_input.shape[-1])) @triton.autotune( configs=[ triton.Config({"BLOCK_M": 128, "BLOCK_N": 256, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 256, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 32, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 32, "BLOCK_K": 32, "SPLIT_K": 1}, num_stages=5, num_warps=2), # good for int8 triton.Config({"BLOCK_M": 128, "BLOCK_N": 256, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 128, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=3, num_warps=8), triton.Config({"BLOCK_M": 256, "BLOCK_N": 64, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 256, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 128, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 128, "BLOCK_N": 32, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=4, num_warps=4), triton.Config({"BLOCK_M": 64, "BLOCK_N": 32, "BLOCK_K": 64, "SPLIT_K": 1}, num_stages=5, num_warps=2), ] + get_configs_io_bound(), key=["CACHE_KEY_M", "CACHE_KEY_N", "CACHE_KEY_K"], prune_configs_by={"early_config_prune": early_config_prune, "perf_model": estimate_matmul_time, "top_k": 10}, ) @triton.heuristics( { "EVEN_K": lambda args: args["K"] % (args["BLOCK_K"] * args["SPLIT_K"]) == 0, } ) @triton.jit def kernel_bwd( C, # Pointers to matrices ACT_INPUT, A, B, # Matrix dimensions M, N, K, CACHE_KEY_M, CACHE_KEY_N, CACHE_KEY_K, # The stride variables represent how much to increase the ptr by when moving by 1 # element in a particular dimension. E.g. stride_am is how much to increase a_ptr # by to get the element one row down (A has M rows) stride_cm, # stride_cn, # Assume that stride_cn == 1 stride_am, stride_ak, stride_bk, stride_bn, # Meta-parameters BLOCK_M: tl.constexpr, GROUP_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, # split k not used, not performant with activation, kept because early_config_prune is expecting it SPLIT_K: tl.constexpr, EVEN_K: tl.constexpr, ACTIVATION: tl.constexpr, ): """ Kernel for computing Out = activation(A x W + C) - Input has shape (M, K) - Weight has shape (K, N) - Output has shape (M, N) - ActInputs (optional) has shape (M, N) 'ActInputs' optionally saves the A x W + C intermediate for backward computations This kernel will consolidate over K """ pid = tl.program_id(axis=0) grid_m = (M + BLOCK_M - 1) // BLOCK_M grid_n = (N + BLOCK_N - 1) // BLOCK_N # re-order program ID for better L2 performance width = GROUP_M * grid_n group_id = pid // width group_size = min(grid_m - group_id * GROUP_M, GROUP_M) pid_m = group_id * GROUP_M + (pid % group_size) pid_n = (pid % width) // (group_size) # now compute the block that each program will go through # rm (resp. rn) denotes a range of indices # for rows (resp. col) of C rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) # trick to avoid masking on M and N axis ram = tl.max_contiguous(tl.multiple_of(rm % M, BLOCK_M), BLOCK_M) rbn = tl.max_contiguous(tl.multiple_of(rn % N, BLOCK_N), BLOCK_N) rk = tl.arange(0, BLOCK_K) A = A + (ram[:, None] * stride_am + rk[None, :] * stride_ak) B = B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn) acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32) for k in range(K, 0, -BLOCK_K): if EVEN_K: a = tl.load(A) b = tl.load(B) else: a = tl.load(A, mask=rk[None, :] < k, other=0.0) b = tl.load(B, mask=rk[:, None] < k, other=0.0) acc += tl.dot(a, b) A += BLOCK_K * stride_ak B += BLOCK_K * stride_bk # optional: fused activation (while the data is in shared memory) if ACTIVATION != 'id': act_in_ptrs = ACT_INPUT + ram[:, None] * stride_cm + rbn[None, :] act_input = tl.load(act_in_ptrs).to(acc.dtype) if ACTIVATION == "gelu": acc *= gelu_grad(act_input) elif ACTIVATION == "gelu_approx": acc *= gelu_approx_grad(act_input) elif ACTIVATION == "squared_relu": acc *= squared_relu_grad(act_input) # rematerialize rm and rn to save registers rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) # write back result C = C + rm[:, None] * stride_cm + rn[None, :] mask = (rm < M)[:, None] & (rn < N)[None, :] tl.store(C, acc, mask=mask) def triton_dgrad_act( grad_output: torch.Tensor, weight: torch.Tensor, activation: str = 'id', act_input: Optional[torch.Tensor] = None, ) -> torch.Tensor: """ Compute e = activation(grad_output @ weight + bias). This wrapper kicks the `kernel_fwd` Triton kernel :param grad_output: input tensor :param weight: weight matrix :param activation: Activation name. Needs to be a Triton kernel. :param act_input: an optional tensor to save the activation inputs (for backward) :return: result tensor """ assert activation in ['id', 'gelu', 'gelu_approx', 'squared_relu'] batch_shape, n = grad_output.shape[:-1], grad_output.shape[-1] batch_dim = batch_shape.numel() grad_output_reshaped = grad_output.reshape(batch_dim, n) if grad_output_reshaped.stride(0) > 1 and grad_output_reshaped.stride(1) > 1: grad_output_reshaped = grad_output_reshaped.contiguous() if weight.stride(0) > 1 and weight.stride(1) > 1: weight = weight.contiguous() assert grad_output.dtype == weight.dtype, f"grad_output and weight must have the same dtype, got {grad_output.dtype} and {weight.dtype}" assert grad_output_reshaped.shape[1] == weight.shape[0], f"Incompatible dimensions: {grad_output_reshaped.shape} - {weight.shape}" if activation != 'id': assert act_input is not None, f'act_input is required for activation {activation}' # M, N, K in bwd are different from M, N, K in fwd M, K = grad_output_reshaped.shape K, N = weight.shape grad_input = torch.empty((M, N), device=grad_output.device, dtype=grad_output.dtype) # 1D launch kernel where each block gets its own program. grid = lambda META: (triton.cdiv(M, META["BLOCK_M"]) * triton.cdiv(N, META["BLOCK_N"]),) # noqa kernel_bwd[grid]( grad_input, act_input, grad_output_reshaped, weight, # data ptrs M, # shapes N, K, M // 32, # key for triton cache (limit number of compilations) N // 32, K // 32, stride_cm=grad_input.stride(0), # strides # stride_cn=grad_input.stride(1), stride_am=grad_output_reshaped.stride(0), stride_ak=grad_output_reshaped.stride(1), stride_bk=weight.stride(0), stride_bn=weight.stride(1), ACTIVATION=activation, # optional fused activation GROUP_M=8, # speed optimization: group the programs ) return grad_input.reshape(*batch_shape, grad_input.shape[-1])
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/triton/linear.py
# Adapted from https://github.com/facebookresearch/xformers/blob/main/xformers/triton/k_activations.py # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # # This source code is licensed under the BSD license found in the # LICENSE file in the root directory of this source tree. import math from enum import Enum from typing import Optional import triton import triton.language as tl _sqrt2pi = math.sqrt(2.0 / math.pi) _sqrt1_2 = math.sqrt(1.0 / 2) _gaussian_pdf_normalization = 1.0 / math.sqrt(2 * math.pi) class Activation(str, Enum): SquaredReLU = "squared_relu" GeLU = "gelu" GeLUApprox = "gelu_approx" LeakyReLU = "leaky_relu" ReLU = "relu" def get_triton_activation_kernel(activation: Optional[Activation]): return ( { Activation.ReLU: relu, Activation.LeakyReLU: leaky_relu, Activation.GeLU: gelu, Activation.GeLUApprox: gelu_approx, Activation.SquaredReLU: squared_relu, }[activation] if activation else None ) def get_triton_activation_bwd_kernel(activation: Optional[Activation]): return ( { Activation.ReLU: relu_grad, Activation.LeakyReLU: leaky_relu_grad, Activation.GeLU: gelu_grad, Activation.GeLUApprox: gelu_approx_grad, Activation.SquaredReLU: squared_relu_grad, }[activation] if activation else None ) @triton.jit def tanh(x): # Tanh is just a scaled sigmoid return 2 * tl.sigmoid(2 * x) - 1 @triton.jit def cosh(x): exp_x = tl.exp(x) return (exp_x + 1.0 / exp_x) * 0.5 # a Triton implementation of the most used activations # See for instance http://arxiv.org/abs/1606.08415 for an overview # ReLU @triton.jit def relu(x): """ ReLU_ activation function .. _ReLU: https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html """ zero = 0.0 return tl.where(x >= 0, x, zero.to(x.dtype)) @triton.jit def relu_grad(x): # ReLU is different from other activations # in that it does not require the input to retrospectively compute its gradient # here the input is the downstream gradient, and we return the upstream gradient directly zero = 0.0 one = 1.0 return tl.where(x >= 0, one.to(x.dtype), zero.to(x.dtype)) @triton.jit def squared_relu(x): """ Squared ReLU activation, as proposed in the Primer_ paper. .. _Primer: https://arxiv.org/abs/2109.08668 """ x_ = relu(x) return (x_ * x_).to(x.dtype) @triton.jit def squared_relu_grad(x): return tl.where(x >= 0, 2.0 * x, 0.0) # Leaky ReLU @triton.jit def leaky_relu(x): """ LeakyReLU_ activation .. _LeakyReLU: https://pytorch.org/docs/stable/generated/torch.nn.LeakyReLU.html """ scale = 0.01 + 0.0 scale = scale.to(x.dtype) return tl.where(x >= 0, x, scale * x) @triton.jit def leaky_relu_grad(x): min_grad = 0.01 max_grad = 1 min_grad = min_grad.to(x.dtype) max_grad = max_grad.to(x.dtype) return tl.where(x >= 0, max_grad, min_grad) @triton.jit def gelu(x): """Gaussian Error Linear Unit (GELU)""" return x * 0.5 * (1.0 + tl.libdevice.erf(x * _sqrt1_2)) @triton.jit def gelu_grad(x): cdf = 0.5 * (1.0 + tl.libdevice.erf(x * _sqrt1_2)) pdf = tl.exp(-0.5 * x * x) * _gaussian_pdf_normalization return cdf + x * pdf @triton.jit def gelu_approx(x): """ GeLU_ activation - Gaussian error linear unit, with tanh approximation .. _GeLU: https://arxiv.org/pdf/1606.08415.pdf """ return 0.5 * x * (1.0 + tanh(_sqrt2pi * x * (1.0 + 0.044715 * x * x))) @triton.jit def gelu_approx_grad(x): # CREDITS: Fast implementation proposed in # https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/fused_bias_gelu.py#L30 tanh_out = tanh(0.79788456 * x * (1 + 0.044715 * x * x)) return 0.5 * x * ( (1 - tanh_out * tanh_out) * (0.79788456 + 0.1070322243 * x * x) ) + 0.5 * (1 + tanh_out)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/triton/k_activations.py
# The triton fused matmul + sqrelu is faster for fp16 but slower for bf16, compared # to naive implementation. import torch import torch.nn as nn import torch.nn.functional as F from torch.cuda.amp import custom_bwd, custom_fwd import fused_dense_lib as fused_dense_cuda from flash_attn.ops.triton.linear import triton_linear_act, triton_dgrad_act from flash_attn.ops.activations import sqrelu_fwd, sqrelu_bwd class FusedDenseSqreluDenseFunc(torch.autograd.Function): @staticmethod @custom_fwd def forward(ctx, x, weight1, bias1, weight2, bias2, checkpoint_lvl=0): """checkpoint_lvl: 0: no recomputation in the bwd 1: recompute gelu_out in the bwd 2: recompute act_input and gelu_out in the bwd """ if torch.is_autocast_enabled(): dtype = torch.get_autocast_gpu_dtype() x, weight1, bias1, weight2, bias2 = [a.to(dtype=dtype) for a in [x, weight1, bias1, weight2, bias2]] is_bf16 = x.dtype == torch.bfloat16 assert checkpoint_lvl in [0, 1, 2] x = x.contiguous() weight1 = weight1.contiguous() bias1 = bias1.contiguous() weight2 = weight2.contiguous() bias2 = bias2.contiguous() batch_shape, n = x.shape[:-1], x.shape[-1] batch_dim = batch_shape.numel() if is_bf16: act_input = fused_dense_cuda.linear_bias_forward(x.reshape(batch_dim, n), weight1, bias1) output1 = sqrelu_fwd(act_input) else: save_act_input = checkpoint_lvl != 2 result = triton_linear_act( x.reshape(batch_dim, n), weight1, bias1, activation='squared_relu', save_act_input=save_act_input ) if save_act_input: output1, act_input = result else: output1 = result output2 = fused_dense_cuda.linear_bias_forward(output1, weight2, bias2) ctx.checkpoint_lvl = checkpoint_lvl if checkpoint_lvl == 0: ctx.save_for_backward(x, weight1, bias1, weight2, act_input, output1) elif checkpoint_lvl == 1: ctx.save_for_backward(x, weight1, bias1, weight2, act_input) elif checkpoint_lvl == 2: ctx.save_for_backward(x, weight1, bias1, weight2) return output2.reshape(*batch_shape, output2.shape[-1]) @staticmethod @custom_bwd def backward(ctx, grad_output): grad_output = grad_output.contiguous() checkpoint_lvl = ctx.checkpoint_lvl x, weight1, bias1, weight2, *rest = ctx.saved_tensors batch_shape, n = x.shape[:-1], x.shape[-1] batch_dim = batch_shape.numel() is_bf16 = x.dtype == torch.bfloat16 if checkpoint_lvl == 0: act_input, output1 = rest elif checkpoint_lvl == 1: act_input, = rest output1 = sqrelu_fwd(act_input) elif checkpoint_lvl == 2: if is_bf16: act_input = fused_dense_cuda.linear_bias_forward(x.reshape(batch_dim, n), weight1, bias1) output1 = sqrelu_fwd(act_input) else: output1, act_input = triton_linear_act( x.reshape(batch_dim, n), weight1, bias1, activation='squared_relu', save_act_input=True ) if is_bf16: grad_output = grad_output.reshape(batch_dim, grad_output.shape[-1]) grad_weight2, grad_bias2 = fused_dense_cuda.linear_bias_wgrad(output1, grad_output) grad_output1 = grad_output @ weight2 grad_act_input = sqrelu_bwd(grad_output1, act_input) grad_input, grad_weight1, grad_bias1 = fused_dense_cuda.linear_bias_backward( x.reshape(batch_dim, n), weight1, grad_act_input ) else: grad_output = grad_output.reshape(batch_dim, grad_output.shape[-1]) grad_weight2, grad_bias2 = fused_dense_cuda.linear_bias_wgrad(output1, grad_output) grad_act_input = triton_dgrad_act(grad_output, weight2, activation='squared_relu', act_input=act_input) grad_input, grad_weight1, grad_bias1 = fused_dense_cuda.linear_bias_backward( x.reshape(batch_dim, n), weight1, grad_act_input ) return grad_input.reshape_as(x), grad_weight1, grad_bias1, grad_weight2, grad_bias2, None fused_dense_sqrelu_dense_function = FusedDenseSqreluDenseFunc.apply class FusedDenseSqreluDense(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, bias=True, checkpoint_lvl=0, device=None, dtype=None): """ checkpoint_lvl (increasing lvl means slower but more memory saving): 0: no recomputation in the bwd 1: recompute gelu_out in the bwd 2: recompute gelu_in and gelu_out in the bwd """ assert checkpoint_lvl in [0, 1, 2] factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features assert bias == True, "DenseSqreluDense module without bias is currently not supported" self.checkpoint_lvl = checkpoint_lvl self.fc1 = nn.Linear(in_features, hidden_features, bias=bias, **factory_kwargs) self.fc2 = nn.Linear(hidden_features, out_features, bias=bias, **factory_kwargs) def forward(self, x): assert x.is_cuda return fused_dense_sqrelu_dense_function(x, self.fc1.weight, self.fc1.bias, self.fc2.weight, self.fc2.bias, self.checkpoint_lvl)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/ops/triton/mlp.py
# Copyright (c) 2022, Tri Dao. import torch import torch.nn as nn from torch import Tensor from einops import rearrange from flash_attn.utils.distributed import reduce_scatter, all_reduce class GPT2Embeddings(nn.Module): def __init__(self, embed_dim, vocab_size, max_position_embeddings, padding_idx=None, word_embed_proj_dim=None, device=None, dtype=None): """ If max_position_embeddings <= 0, there's no position embeddings If word_embe_proj_dim is not None (e.g., OPT-350m), we embed to that dimension the project up to embed_dim """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() if word_embed_proj_dim is None: self.word_embeddings = nn.Embedding(vocab_size, embed_dim, padding_idx=padding_idx, **factory_kwargs) self.project_in = None else: self.word_embeddings = nn.Embedding(vocab_size, word_embed_proj_dim, padding_idx=padding_idx, **factory_kwargs) self.project_in = nn.Linear(word_embed_proj_dim, embed_dim, bias=False, **factory_kwargs) self.max_position_embeddings = max_position_embeddings if self.max_position_embeddings > 0: self.position_embeddings = nn.Embedding(max_position_embeddings, embed_dim, **factory_kwargs) def forward(self, input_ids, position_ids=None): """ input_ids: (batch, seqlen) position_ids: (batch, seqlen) """ batch_size, seqlen = input_ids.shape embeddings = self.word_embeddings(input_ids) if self.project_in is not None: embeddings = self.project_in(embeddings) if self.max_position_embeddings > 0: if position_ids is None: position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device) position_embeddings = self.position_embeddings(position_ids) embeddings = embeddings + position_embeddings return embeddings class BertEmbeddings(nn.Module): def __init__(self, embed_dim, vocab_size, max_position_embeddings, type_vocab_size, padding_idx=None, device=None, dtype=None): """ If max_position_embeddings <= 0, there's no position embeddings If type_vocab_size <= 0, there's no token type embeddings """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.word_embeddings = nn.Embedding(vocab_size, embed_dim, padding_idx=padding_idx, **factory_kwargs) self.max_position_embeddings = max_position_embeddings self.type_vocab_size = type_vocab_size if self.max_position_embeddings > 0: self.position_embeddings = nn.Embedding(max_position_embeddings, embed_dim, **factory_kwargs) if self.type_vocab_size > 0: self.token_type_embeddings = nn.Embedding(type_vocab_size, embed_dim, **factory_kwargs) def forward(self, input_ids, position_ids=None, token_type_ids=None): """ input_ids: (batch, seqlen) position_ids: (batch, seqlen) token_type_ids: (batch, seqlen) """ batch_size, seqlen = input_ids.shape embeddings = self.word_embeddings(input_ids) if self.max_position_embeddings > 0: if position_ids is None: position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device) position_embeddings = self.position_embeddings(position_ids) embeddings = embeddings + position_embeddings if self.type_vocab_size > 0: if token_type_ids is None: token_type_ids = torch.zeros(seqlen, dtype=torch.long, device=input_ids.device) token_type_embeddings = self.token_type_embeddings(token_type_ids) embeddings = embeddings + token_type_embeddings return embeddings class VocabParallelEmbedding(nn.Embedding): def __init__(self, num_embeddings, *args, process_group=None, padding_idx=None, **kwargs): self.process_group = process_group if process_group is not None: world_size = torch.distributed.get_world_size(process_group) if num_embeddings % world_size != 0: raise ValueError(f'num_embeddings ({num_embeddings}) must be divisible by ' f'world_size ({world_size})') if world_size > 1 and padding_idx is not None: raise RuntimeError('ParallelEmbedding does not support padding_idx') else: world_size = 1 super().__init__(num_embeddings // world_size, *args, padding_idx=padding_idx, **kwargs) def forward(self, input: Tensor) -> Tensor: if self.process_group is None: return super().forward(input) else: rank = torch.distributed.get_rank(self.process_group) vocab_size = self.num_embeddings vocab_start_index, vocab_end_index = rank * vocab_size, (rank + 1) * vocab_size # Create a mask of valid vocab ids (1 means it needs to be masked). input_ids_mask = (input < vocab_start_index) | (input >= vocab_end_index) input = input - vocab_start_index input[input_ids_mask] = 0 embeddings = super().forward(input) embeddings[input_ids_mask] = 0.0 return embeddings class ColumnParallelEmbedding(nn.Embedding): def __init__(self, num_embeddings, embedding_dim, *args, process_group=None, **kwargs): self.process_group = process_group if process_group is not None: world_size = torch.distributed.get_world_size(process_group) if embedding_dim % world_size != 0: raise ValueError(f'embedding_dim ({embedding_dim}) must be divisible by ' f'world_size ({world_size})') else: world_size = 1 super().__init__(num_embeddings, embedding_dim // world_size, *args, **kwargs) class ParallelGPT2Embeddings(nn.Module): def __init__(self, embed_dim, vocab_size, max_position_embeddings, process_group, padding_idx=None, sequence_parallel=True, device=None, dtype=None): """ If max_position_embeddings <= 0, there's no position embeddings """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.process_group = process_group self.sequence_parallel = sequence_parallel self.word_embeddings = VocabParallelEmbedding( vocab_size, embed_dim, padding_idx=padding_idx, process_group=process_group, **factory_kwargs ) self.max_position_embeddings = max_position_embeddings if self.max_position_embeddings > 0: self.position_embeddings = ColumnParallelEmbedding( max_position_embeddings, embed_dim, process_group=process_group, **factory_kwargs ) def forward(self, input_ids, position_ids=None, combine_batch_seqlen_dim=False): """ input_ids: (batch, seqlen) position_ids: (batch, seqlen) """ batch_size, seqlen = input_ids.shape world_size = torch.distributed.get_world_size(self.process_group) embeddings = self.word_embeddings(input_ids) if self.max_position_embeddings > 0: if position_ids is None: position_ids = torch.arange(seqlen, dtype=torch.long, device=input_ids.device) position_embeddings = self.position_embeddings(position_ids) if world_size <= 1: embeddings = embeddings + position_embeddings else: partition_dim = self.position_embeddings.embedding_dim rank = torch.distributed.get_rank(self.process_group) embeddings[..., rank * partition_dim:(rank + 1) * partition_dim] += position_embeddings if combine_batch_seqlen_dim: embeddings = rearrange(embeddings, 'b s d -> (b s) d') reduce_fn = reduce_scatter if self.sequence_parallel else all_reduce return embeddings if world_size <= 1 else reduce_fn(embeddings, self.process_group)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/modules/embedding.py
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/modules/__init__.py
# Copyright (c) 2022, Tri Dao. import torch import torch.nn as nn import torch.nn.functional as F try: from flash_attn.ops.fused_dense import FusedMLP, ParallelFusedMLP except ImportError: FusedMLP, ParallelFusedMLP = None, None class Mlp(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, activation=F.gelu, return_residual=False, device=None, dtype=None): factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features * 4 self.return_residual = return_residual self.fc1 = nn.Linear(in_features, hidden_features, **factory_kwargs) self.activation = activation self.fc2 = nn.Linear(hidden_features, out_features, **factory_kwargs) def forward(self, x): y = self.fc1(x) y = self.activation(y) y = self.fc2(y) return y if not self.return_residual else (y, x)
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/modules/mlp.py
# Copyright (c) 2022, Tri Dao. from typing import Optional from functools import partial import torch import torch.nn as nn import torch.nn.functional as F from torch import Tensor from torchvision.ops import StochasticDepth from flash_attn.modules.mha import MHA from flash_attn.modules.mlp import Mlp try: from flash_attn.ops.layer_norm import dropout_add_layer_norm except ImportError: dropout_add_layer_norm = None try: from flash_attn.ops.layer_norm import dropout_add_layer_norm_parallel_residual except ImportError: dropout_add_layer_norm_parallel_residual = None class Block(nn.Module): def __init__(self, dim, mixer_cls=None, mlp_cls=None, norm_cls=nn.LayerNorm, dropout_cls=nn.Dropout, prenorm=True, resid_dropout1=0., resid_dropout2=0., drop_path1=0., drop_path2=0., fused_dropout_add_ln=False, return_residual=False, residual_in_fp32=False, sequence_parallel=False, mark_shared_params=False): """ For prenorm=True, this Block has a slightly different structure compared to a regular prenorm Transformer block. The standard block is: LN -> MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add. [Ref: https://arxiv.org/abs/2002.04745] Here we have: Dropout -> Add -> LN -> MHA -> Dropout -> Add -> LN -> MLP, returning both the hidden_states (output of the MLP) and the residual. This is for performance reasons, as we can fuse the dropout, add and LayerNorm. The residual needs to be provided (except for the very first block). For prenorm=False, this Block has the same structure as a regular postnorm Transformer block: MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add -> LN. return_residual: whether each of the sub-layers (mixer and mlp) will return the residual. This is for performance reason: for post-norm architecture, returning the input allows us to fuse the backward of nn.Linear with the residual connection. """ super().__init__() self.prenorm = prenorm self.fused_dropout_add_ln = fused_dropout_add_ln self.return_residual = return_residual self.residual_in_fp32 = residual_in_fp32 if self.residual_in_fp32: assert self.prenorm, 'residual_in_fp32 is only compatible with prenorm=True' if mixer_cls is None: mixer_cls = partial(MHA, num_heads=dim // 64) if mlp_cls is None: mlp_cls = partial(Mlp, hidden_features=4 * dim) self.mixer = mixer_cls(dim) self.dropout1 = dropout_cls(resid_dropout1) self.drop_path1 = StochasticDepth(drop_path1, mode='row') self.norm1 = norm_cls(dim) self.mlp = mlp_cls(dim) if not isinstance(self.mlp, nn.Identity): self.dropout2 = dropout_cls(resid_dropout2) self.drop_path2 = StochasticDepth(drop_path2, mode='row') self.norm2 = norm_cls(dim) if self.fused_dropout_add_ln: assert dropout_add_layer_norm is not None, 'dropout_layer_norm is not installed' assert isinstance(self.norm1, nn.LayerNorm) and isinstance(self.dropout1, nn.Dropout) # TD [2023-01-07]: TODO: During training, if sequence_parallel is False and dropout != 0.0, # then the input to each worker in the tensor parallel group will be different. # This would produce wrong outputs? Somehow we'd need to sync the RNG state across workers. # For now this is not an issue because we always use sequence_parallel=True during training # and only use sequence_parallel=False during inference. # Mark the norm parameters as "sequence_parallel" so that we run all-reduce on their grads. if sequence_parallel: for p in self.norm1.parameters(): p._sequence_parallel = True if hasattr(self, 'norm2'): for p in self.norm2.parameters(): p._sequence_parallel = True # Mark the norm parameters as "shared_params" so that we sync their values at init. if mark_shared_params: for p in self.norm1.parameters(): p._shared_params = True if hasattr(self, 'norm2'): for p in self.norm2.parameters(): p._shared_params = True def forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None): r"""Pass the input through the encoder layer. Args: hidden_states: the sequence to the encoder layer (required). residual: if postnorm, residual=None, If prenorm, hidden_states = Attn/MLP(LN(residual)) mixer_subset: for cross-attention only. If not None, will take a subset of x before applying the query projection. Useful for e.g., ViT where we only care about the CLS token in the last layer. """ if self.prenorm: if not self.fused_dropout_add_ln: dropped = self.drop_path1(self.dropout1(hidden_states)) residual = (dropped + residual) if residual is not None else dropped hidden_states = self.norm1(residual.to(dtype=self.norm1.weight.dtype)) if self.residual_in_fp32: residual = residual.to(torch.float32) else: if self.drop_path1.p == 0 or not self.training: rowscale1 = None else: rowscale1 = self.drop_path1(torch.ones( hidden_states.shape[:-1], device=hidden_states.device, dtype=hidden_states.dtype) ) hidden_states, residual = dropout_add_layer_norm( hidden_states, residual, self.norm1.weight, self.norm1.bias, self.dropout1.p if self.training else 0.0, self.norm1.eps, rowscale=rowscale1, prenorm=True, residual_in_fp32=self.residual_in_fp32 ) if mixer_kwargs is None: mixer_kwargs = {} if mixer_subset is not None: mixer_kwargs['mixer_subset'] = mixer_subset hidden_states = self.mixer(hidden_states, **mixer_kwargs) if mixer_subset is not None: residual = residual[:, mixer_subset] if not isinstance(self.mlp, nn.Identity): if not self.fused_dropout_add_ln: dropped = self.drop_path2(self.dropout2(hidden_states)) residual = (dropped + residual) if residual is not None else dropped hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype)) if self.residual_in_fp32: residual = residual.to(torch.float32) else: if self.drop_path2.p == 0 or not self.training: rowscale2 = None else: rowscale2 = self.drop_path2(torch.ones( hidden_states.shape[:-1], device=hidden_states.device, dtype=hidden_states.dtype) ) hidden_states, residual = dropout_add_layer_norm( hidden_states, residual, self.norm2.weight, self.norm2.bias, self.dropout2.p if self.training else 0.0, self.norm2.eps, rowscale=rowscale2, prenorm=True, residual_in_fp32=self.residual_in_fp32 ) hidden_states = self.mlp(hidden_states) return hidden_states, residual else: assert residual is None mixer_out = self.mixer( hidden_states, **(mixer_kwargs if mixer_kwargs is not None else {}) ) if self.return_residual: # mixer out is actually a pair here mixer_out, hidden_states = mixer_out if not self.fused_dropout_add_ln: hidden_states = self.norm1((self.drop_path1(self.dropout1(mixer_out)) + hidden_states).to(dtype=self.norm1.weight.dtype)) else: if self.drop_path1.p == 0 or not self.training: rowscale1 = None else: rowscale1 = self.drop_path1(torch.ones( mixer_out.shape[:-1], device=mixer_out.device, dtype=mixer_out.dtype) ) hidden_states = dropout_add_layer_norm( mixer_out, hidden_states, self.norm1.weight, self.norm1.bias, self.dropout1.p if self.training else 0.0, self.norm1.eps, rowscale=rowscale1, prenorm=False ) if not isinstance(self.mlp, nn.Identity): mlp_out = self.mlp(hidden_states) if self.return_residual: # mlp out is actually a pair here mlp_out, hidden_states = mlp_out if not self.fused_dropout_add_ln: hidden_states = self.norm2((self.drop_path2(self.dropout2(mlp_out)) + hidden_states).to(dtype=self.norm2.weight.dtype)) else: if self.drop_path2.p == 0 or not self.training: rowscale2 = None else: rowscale2 = self.drop_path2(torch.ones( mlp_out.shape[:-1], device=mlp_out.device, dtype=mlp_out.dtype) ) hidden_states = dropout_add_layer_norm( mlp_out, hidden_states, self.norm2.weight, self.norm2.bias, self.dropout2.p if self.training else 0.0, self.norm2.eps, rowscale=rowscale2, prenorm=False ) return hidden_states class ParallelBlock(nn.Module): """The attention (mixer) and MLP blocks are done in parallel, similar to GPT-J, GPT-NeoX, and PaLM. """ def __init__(self, dim, mixer_cls=None, mlp_cls=None, norm_cls=nn.LayerNorm, dropout_cls=nn.Dropout, resid_dropout1=0., resid_dropout2=0., tied_norm=False, fused_dropout_add_ln=False, residual_in_fp32=False, sequence_parallel=False, mark_shared_params=False): """ This Block has a slightly different structure compared to a regular prenorm Transformer block. The standard block is: LN -> MHA / MLP -> Dropout -> Add. [Ref: https://arxiv.org/abs/2002.04745] Here we have: Dropout -> Add -> LN -> MHA / MLP, returning both the hidden_states (output1 of the MHA / MLP) and the residual. This is for performance reasons, as we can fuse the dropout, add and LayerNorm. The residual needs to be provided (except for the very first block). """ super().__init__() self.tied_norm = tied_norm self.fused_dropout_add_ln = fused_dropout_add_ln self.residual_in_fp32 = residual_in_fp32 if mixer_cls is None: mixer_cls = partial(MHA, num_heads=dim // 64) if mlp_cls is None: mlp_cls = partial(Mlp, hidden_features=4 * dim) self.mixer = mixer_cls(dim) self.dropout1 = dropout_cls(resid_dropout1) self.norm1 = norm_cls(dim) self.mlp = mlp_cls(dim) self.dropout2 = dropout_cls(resid_dropout2) if not self.tied_norm: self.norm2 = norm_cls(dim) if self.fused_dropout_add_ln: assert dropout_add_layer_norm_parallel_residual is not None, 'dropout_layer_norm is not installed' assert isinstance(self.norm1, nn.LayerNorm) and isinstance(self.dropout1, nn.Dropout) # TD [2023-01-07]: TODO: During training, if sequence_parallel is False and dropout != 0.0, # then the input to each worker in the tensor parallel group will be different. # This would produce wrong outputs? Somehow we'd need to sync the RNG state across workers. # For now this is not an issue because we always use sequence_parallel=True during training # and only use sequence_parallel=False during inference. # Mark the norm parameters as "sequence_parallel" so that we run all-reduce on their grads. if sequence_parallel: for p in self.norm1.parameters(): p._sequence_parallel = True if hasattr(self, 'norm2'): for p in self.norm2.parameters(): p._sequence_parallel = True # Mark the norm parameters as "shared_params" so that we sync their values at init. if mark_shared_params: for p in self.norm1.parameters(): p._shared_params = True if hasattr(self, 'norm2'): for p in self.norm2.parameters(): p._shared_params = True def forward(self, hidden_states1: Tensor, hidden_states2: Optional[Tensor] = None, residual: Optional[Tensor] = None, mixer_kwargs=None): r"""Pass the input through the encoder layer. Args: hidden_states1: the output of the previous attention (mixer) or embedding layer. hidden_states2: the output of the previous MLP layer (if None, will use hidden_states1). residual. """ if not self.fused_dropout_add_ln: dropped1 = self.dropout1(hidden_states1) # For the very 1st block, we only want 1 dropout, not two different dropouts if hidden_states2 is not None: dropped2 = self.dropout2(hidden_states2) residual = ((residual + dropped1 + dropped2) if residual is not None else dropped1 + dropped2) else: residual = (residual + dropped1) if residual is not None else dropped1 hidden_states1 = self.norm1(residual.to(dtype=self.norm1.weight.dtype)) hidden_states2 = (self.norm2(residual.to(dtype=self.norm2.weight.dtype)) if not self.tied_norm else hidden_states1) if self.residual_in_fp32: residual = residual.to(torch.float32) else: weight2, bias2 = ((self.norm2.weight, self.norm2.bias) if not self.tied_norm else (None, None)) hidden_states1, hidden_states2, residual = dropout_add_layer_norm_parallel_residual( hidden_states1, hidden_states2, residual, self.norm1.weight, self.norm1.bias, weight2, bias2, self.dropout1.p if self.training else 0.0, self.norm1.eps, prenorm=True, residual_in_fp32=self.residual_in_fp32 ) if self.tied_norm: hidden_states2 = hidden_states1 if mixer_kwargs is None: mixer_kwargs = {} hidden_states1 = self.mixer(hidden_states1, **mixer_kwargs) hidden_states2 = self.mlp(hidden_states2) return hidden_states1, hidden_states2, residual
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/modules/block.py
# Copyright (c) 2022, Tri Dao. import math from functools import partial import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange try: from flash_attn.flash_attn_interface import flash_attn_unpadded_qkvpacked_func from flash_attn.flash_attn_interface import flash_attn_unpadded_kvpacked_func except ImportError: flash_attn_unpadded_qkvpacked_func, flash_attn_unpadded_kvpacked_func = None, None try: from flash_attn.ops.flash_attn_triton import flash_attn_qkvpacked_func, flash_attn_kvpacked_func except ImportError: flash_attn_qkvpacked_func, flash_attn_kvpacked_func = None, None try: from flash_attn.ops.fused_dense import FusedDense, ColumnParallelLinear, RowParallelLinear except ImportError: FusedDense, ColumnParallelLinear, RowParallelLinear = None, None, None try: from flash_attn.layers.rotary import RotaryEmbedding except ImportError: RotaryEmbedding = None try: import ft_attention except ImportError: ft_attention = None class FlashSelfAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0) """ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0, triton=False): super().__init__() if attention_dropout != 0.0 or not triton: assert flash_attn_unpadded_qkvpacked_func is not None, 'FlashAttention is not installed' if attention_dropout == 0.0 and triton: assert flash_attn_qkvpacked_func is not None, 'FlashAttention Triton is not installed' self.causal = causal self.softmax_scale = softmax_scale self.dropout_p = attention_dropout self.triton = triton def forward(self, qkv, causal=None, cu_seqlens=None, max_seqlen=None): """Implements the multihead softmax attention. Arguments --------- qkv: The tensor containing the query, key, and value. If cu_seqlens is None and max_seqlen is None, then qkv has shape (B, S, 3, H, D). If cu_seqlens is not None and max_seqlen is not None, then qkv has shape (total, 3, H, D), where total is the sum of the sequence lengths in the batch. causal: if passed, will override self.causal cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into qkv. max_seqlen: int. Maximum sequence length in the batch. Returns: -------- out: (total, H, D) if cu_seqlens is not None and max_seqlen is not None, else (B, S, H, D). """ assert qkv.dtype in [torch.float16, torch.bfloat16] assert qkv.is_cuda causal = self.causal if causal is None else causal unpadded = cu_seqlens is not None if unpadded: assert cu_seqlens.dtype == torch.int32 assert max_seqlen is not None assert isinstance(max_seqlen, int) return flash_attn_unpadded_qkvpacked_func( qkv, cu_seqlens, max_seqlen, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) else: batch_size, seqlen = qkv.shape[0], qkv.shape[1] # Triton version doesn't support dropout if self.triton and (self.dropout_p == 0 or not self.training): output = flash_attn_qkvpacked_func(qkv, None, causal, self.softmax_scale) else: qkv = rearrange(qkv, 'b s ... -> (b s) ...') max_seqlen = seqlen cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32, device=qkv.device) output = flash_attn_unpadded_qkvpacked_func( qkv, cu_seqlens, max_seqlen, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) output = rearrange(output, '(b s) ... -> b s ...', b=batch_size) return output class FlashCrossAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0) """ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0, triton=False): super().__init__() if attention_dropout != 0.0 or not triton: assert flash_attn_unpadded_kvpacked_func is not None, 'FlashAttention is not installed' if attention_dropout == 0.0 and triton: assert flash_attn_kvpacked_func is not None, 'FlashAttention Triton is not installed' self.causal = causal self.softmax_scale = softmax_scale self.dropout_p = attention_dropout self.triton = triton def forward(self, q, kv, causal=None, cu_seqlens=None, max_seqlen=None, cu_seqlens_k=None, max_seqlen_k=None): """Implements the multihead softmax attention. Arguments --------- q: The tensor containing the query. (B, Sq, H, D) kv: The tensor containing the key and value. (B, Sk, 2, H, D) causal: if passed, will override self.causal cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into q. max_seqlen: int. Maximum sequence length in the batch of q. cu_seqlens_k: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into kv. max_seqlen_k: int. Maximum sequence length in the batch of k and v. """ assert q.dtype in [torch.float16, torch.bfloat16] assert q.is_cuda and kv.is_cuda causal = self.causal if causal is None else causal unpadded = cu_seqlens is not None if unpadded: assert cu_seqlens.dtype == torch.int32 assert max_seqlen is not None assert isinstance(max_seqlen, int) assert cu_seqlens_k is not None assert cu_seqlens_k.dtype == torch.int32 assert max_seqlen_k is not None assert isinstance(max_seqlen, int) return flash_attn_unpadded_kvpacked_func( q, kv, cu_seqlens, cu_seqlens_k, max_seqlen, max_seqlen_k, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) else: batch_size, seqlen_q = q.shape[0], q.shape[1] seqlen_k = kv.shape[1] assert kv.shape[0] == batch_size and kv.shape[3] == q.shape[2] and kv.shape[4] == q.shape[3] if self.triton and (self.dropout_p == 0.0 or not self.training): # Triton version doesn't support dropout output = flash_attn_kvpacked_func(q, kv, None, causal, self.softmax_scale) else: q = rearrange(q, 'b s ... -> (b s) ...') kv = rearrange(kv, 'b s ... -> (b s) ...') cu_seqlens_q = torch.arange(0, (batch_size + 1) * seqlen_q, step=seqlen_q, dtype=torch.int32, device=q.device) cu_seqlens_k = torch.arange(0, (batch_size + 1) * seqlen_k, step=seqlen_k, dtype=torch.int32, device=kv.device) output = flash_attn_unpadded_kvpacked_func( q, kv, cu_seqlens_q, cu_seqlens_k, seqlen_q, seqlen_k, self.dropout_p if self.training else 0.0, softmax_scale=self.softmax_scale, causal=causal ) output = rearrange(output, '(b s) ... -> b s ...', b=batch_size) return output class SelfAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0) """ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0): super().__init__() self.causal = causal self.softmax_scale = softmax_scale self.dropout_p = attention_dropout def forward(self, qkv, causal=None, key_padding_mask=None): """Implements the multihead softmax attention. Arguments --------- qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) causal: if passed, will override self.causal key_padding_mask: boolean mask to apply to the attention weights. True means to keep, False means to mask out. (B, S) """ batch_size, seqlen = qkv.shape[0], qkv.shape[1] causal = self.causal if causal is None else causal q, k, v = qkv.unbind(dim=2) softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1]) scores = torch.einsum('bthd,bshd->bhts', q, k * softmax_scale) if key_padding_mask is not None: padding_mask = torch.full((batch_size, seqlen), -10000.0, dtype=scores.dtype, device=scores.device) padding_mask.masked_fill_(key_padding_mask, 0.0) # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess) scores = scores + rearrange(padding_mask, 'b s -> b 1 1 s') if causal: # "triu_tril_cuda_template" not implemented for 'BFloat16' # So we have to construct the mask in float causal_mask = torch.triu(torch.full((seqlen, seqlen), -10000.0, device=scores.device), 1) # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess) scores = scores + causal_mask.to(dtype=scores.dtype) attention = torch.softmax(scores, dim=-1, dtype=v.dtype) attention_drop = F.dropout(attention, self.dropout_p if self.training else 0.0) output = torch.einsum('bhts,bshd->bthd', attention_drop, v) return output class CrossAttention(nn.Module): """Implement the scaled dot product attention with softmax. Arguments --------- softmax_scale: The temperature to use for the softmax attention. (default: 1/sqrt(d_keys) where d_keys is computed at runtime) attention_dropout: The dropout rate to apply to the attention (default: 0.0) """ def __init__(self, causal=False, softmax_scale=None, attention_dropout=0.0): super().__init__() self.causal = causal self.softmax_scale = softmax_scale self.dropout_p = attention_dropout def forward(self, q, kv, causal=None, key_padding_mask=None): """Implements the multihead softmax attention. Arguments --------- q: The tensor containing the query. (B, Sq, H, D) kv: The tensor containing the key and value. (B, Sk, 2, H, D) causal: if passed, will override self.causal key_padding_mask: boolean mask to apply to the attention weights. True means to keep, False means to mask out. (B, Sk) """ batch_size, seqlen_q = q.shape[0], q.shape[1] causal = self.causal if causal is None else causal seqlen_k = kv.shape[1] assert kv.shape[0] == batch_size and kv.shape[3] == q.shape[2] and kv.shape[4] == q.shape[3] k, v = kv.unbind(dim=2) softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1]) scores = torch.einsum('bthd,bshd->bhts', q, k * softmax_scale) if key_padding_mask is not None: padding_mask = torch.full((batch_size, seqlen_k), -10000.0, dtype=scores.dtype, device=scores.device) padding_mask.masked_fill_(key_padding_mask, 0.0) # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess) scores = scores + rearrange(padding_mask, 'b s -> b 1 1 s') if causal: # "triu_tril_cuda_template" not implemented for 'BFloat16' # So we have to construct the mask in float causal_mask = torch.triu(torch.full((seqlen_q, seqlen_k), -10000.0, device=scores.device), 1) # TD [2022-09-30]: Adding is faster than masked_fill_ (idk why, just better kernel I guess) scores = scores + causal_mask.to(dtype=scores.dtype) attention = torch.softmax(scores, dim=-1, dtype=v.dtype) attention_drop = F.dropout(attention, self.dropout_p if self.training else 0.0) output = torch.einsum('bhts,bshd->bthd', attention_drop, v) return output class LinearResidual(nn.Linear): """Wrap nn.Linear to return the residual as well. For compatibility with FusedDense. """ def forward(self, input: torch.Tensor) -> torch.Tensor: return super().forward(input), input def _update_kv_cache(kv, inference_params, layer_idx): """kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim) """ # Pre-allocate memory for key-values for inference. num_heads, head_dim = kv.shape[-2:] if layer_idx not in inference_params.key_value_memory_dict: kv_cache = torch.empty( inference_params.max_batch_size, inference_params.max_sequence_len, 2, num_heads, head_dim, dtype=kv.dtype, device=kv.device ) inference_params.key_value_memory_dict[layer_idx] = kv_cache else: if not inference_params.fused_ft_kernel: kv_cache = inference_params.key_value_memory_dict[layer_idx] else: # For FT, k_cache has shape (b, h, headdim / packsize, s, packsize) # where packsize = 4 if fp32, 8 if fp16 or bf16. # v_cache has shape (b, h, s, headdim) k_cache, v_cache = inference_params.key_value_memory_dict[layer_idx] kv_cache = None # Adjust key and value for inference batch_start = inference_params.batch_size_offset batch_end = batch_start + kv.shape[0] sequence_start = inference_params.sequence_len_offset sequence_end = sequence_start + kv.shape[1] assert batch_end <= (kv_cache.shape[0] if kv_cache is not None else v_cache.shape[0]) assert sequence_end <= (kv_cache.shape[1] if kv_cache is not None else v_cache.shape[2]) # Copy key and values. if not inference_params.fused_ft_kernel: assert kv_cache is not None kv_cache[batch_start:batch_end, sequence_start:sequence_end, ...] = kv kv = kv_cache[batch_start:batch_end, :sequence_end, ...] return kv else: assert inference_params.sequence_len_offset == 0 # FT kernel requires different layouts for the k_cache and v_cache. assert kv.dtype in [torch.float16, torch.bfloat16, torch.float32] packsize = 4 if kv.dtype == torch.float32 else 8 if kv_cache is not None: kv_cache[batch_start:batch_end, sequence_start:sequence_end, ...] = kv k_cache = rearrange(kv_cache[:, :, 0], 'b s h (d packsize) -> b h d s packsize', packsize=packsize).contiguous() v_cache = rearrange(kv_cache[:, :, 1], 'b s h d -> b h s d').contiguous() inference_params.key_value_memory_dict[layer_idx] = (k_cache, v_cache) else: k_cache[batch_start:batch_end, :, :, :sequence_end, :] = rearrange( kv[:, :, 0], 'b s h (d packsize) -> b h d s packsize', packsize=packsize ) v_cache[batch_start:batch_end, :, :sequence_end, :] = rearrange( kv[:, :, 1], 'b s h d -> b h s d' ) return kv class MHA(nn.Module): """Multi-head self-attention and cross-attention """ def __init__(self, embed_dim, num_heads, cross_attn=False, qkv_proj_bias=True, out_proj_bias=True, dropout=0.0, softmax_scale=None, causal=False, layer_idx=None, dwconv=False, rotary_emb_dim=0, rotary_emb_scale_base=None, rotary_emb_interleaved=False, fused_bias_fc=False, use_flash_attn=False, return_residual=False, checkpointing=False, device=None, dtype=None) -> None: """ return_residual: whether to return the input x along with the output. This is for performance reason: for post-norm architecture, returning the input allows us to fuse the backward of nn.Linear with the residual connection. """ factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.embed_dim = embed_dim self.cross_attn = cross_attn self.causal = causal self.layer_idx = layer_idx self.dwconv = dwconv self.rotary_emb_dim = rotary_emb_dim self.use_flash_attn = use_flash_attn self.return_residual = return_residual self.checkpointing = checkpointing self.num_heads = num_heads assert self.embed_dim % num_heads == 0, "self.kdim must be divisible by num_heads" self.head_dim = self.embed_dim // num_heads if self.rotary_emb_dim > 0: assert not cross_attn, 'MHA with rotary embedding does not support cross-attention yet' assert RotaryEmbedding is not None, 'rotary_emb is not installed' self.rotary_emb = RotaryEmbedding(self.rotary_emb_dim, scale_base=rotary_emb_scale_base, interleaved=rotary_emb_interleaved, device=device) if fused_bias_fc and FusedDense is None: raise ImportError('fused_dense is not installed') linear_cls = nn.Linear if not fused_bias_fc else FusedDense linear_resid_cls = (LinearResidual if not fused_bias_fc else partial(FusedDense, return_residual=True)) inner_attn_cls = FlashSelfAttention if use_flash_attn else SelfAttention inner_cross_attn_cls = FlashCrossAttention if use_flash_attn else CrossAttention if not self.cross_attn: if not self.return_residual: self.Wqkv = linear_cls(embed_dim, 3 * embed_dim, bias=qkv_proj_bias, **factory_kwargs) else: self.Wqkv = linear_resid_cls(embed_dim, 3 * embed_dim, bias=qkv_proj_bias, **factory_kwargs) if self.dwconv: self.dwconv_qkv = nn.Conv1d(3 * embed_dim, 3 * embed_dim, kernel_size=3, padding=2, groups=3 * embed_dim) else: self.Wq = linear_cls(embed_dim, embed_dim, bias=qkv_proj_bias, **factory_kwargs) if not self.return_residual: self.Wkv = linear_cls(embed_dim, 2 * embed_dim, bias=qkv_proj_bias, **factory_kwargs) else: self.Wkv = linear_resid_cls(embed_dim, 2 * embed_dim, bias=qkv_proj_bias, **factory_kwargs) if self.dwconv: self.dwconv_q = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, padding=2, groups=embed_dim) self.dwconv_kv = nn.Conv1d(2 * embed_dim, 2 * embed_dim, kernel_size=3, padding=2, groups=2 * embed_dim) self.inner_attn = inner_attn_cls(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout) self.inner_cross_attn = inner_cross_attn_cls(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout) self.out_proj = linear_cls(embed_dim, embed_dim, bias=out_proj_bias, **factory_kwargs) def _update_kv_cache(self, kv, inference_params): """kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim) """ assert not self.dwconv, 'Generation does not support dwconv yet' assert self.layer_idx is not None, 'Generation requires layer_idx in the constructor' return _update_kv_cache(kv, inference_params, self.layer_idx) def forward(self, x, x_kv=None, key_padding_mask=None, cu_seqlens=None, max_seqlen=None, mixer_subset=None, inference_params=None, **kwargs): """ Arguments: x: (batch, seqlen, hidden_dim) (where hidden_dim = num heads * head dim) if cu_seqlens is None and max_seqlen is None, else (total, hidden_dim) where total is the is the sum of the sequence lengths in the batch. x_kv: (batch, seqlen, hidden_dim), only applicable for cross-attention. If None, use x. cu_seqlens: (batch_size + 1,), dtype torch.int32. The cumulative sequence lengths of the sequences in the batch, used to index into x. Only applicable when using FlashAttention. max_seqlen: int. Maximum sequence length in the batch. key_padding_mask: boolean mask, True means to keep, False means to mask out. (batch, seqlen). Only applicable when not using FlashAttention. mixer_subset: for cross-attention only. If not None, will take a subset of x before applying the query projection. Useful for e.g., ViT where we only care about the CLS token in the last layer. inference_params: for generation. Adapted from Megatron-LM (and Apex) https://github.com/NVIDIA/apex/blob/3ff1a10f72ec07067c4e44759442329804ac5162/apex/transformer/testing/standalone_transformer_lm.py#L470 """ if cu_seqlens is not None: assert max_seqlen is not None assert key_padding_mask is None assert self.use_flash_attn assert not self.dwconv assert self.rotary_emb_dim == 0 if key_padding_mask is not None: assert cu_seqlens is None assert max_seqlen is None assert not self.use_flash_attn if inference_params is not None: assert key_padding_mask is None assert cu_seqlens is None and max_seqlen is None assert not self.dwconv kwargs = ({'cu_seqlens': cu_seqlens, 'max_seqlen': max_seqlen, **kwargs} if self.use_flash_attn else {'key_padding_mask': key_padding_mask, **kwargs}) if not self.cross_attn: assert x_kv is None and mixer_subset is None if not self.return_residual: qkv = self.Wqkv(x) else: qkv, x = self.Wqkv(x) if self.dwconv: qkv = rearrange(self.dwconv_qkv(rearrange(qkv, 'b s d -> b d s'))[..., :-2], 'b d s -> b s d').contiguous() qkv = rearrange(qkv, '... (three h d) -> ... three h d', three=3, d=self.head_dim) if inference_params is None: if self.rotary_emb_dim > 0: qkv = self.rotary_emb(qkv) if not self.checkpointing: context = self.inner_attn(qkv, **kwargs) else: context = torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, **kwargs) else: if (not inference_params.fused_ft_kernel) or inference_params.sequence_len_offset == 0: if self.rotary_emb_dim > 0: qkv = self.rotary_emb(qkv, seqlen_offset=inference_params.sequence_len_offset) q = qkv[:, :, 0] kv = self._update_kv_cache(qkv[:, :, 1:], inference_params) # If we're processing the prompt, causal=None (use self.causal). # If we're decoding, then causal=False. causal = None if inference_params.sequence_len_offset == 0 else False context = self.inner_cross_attn(q, kv, causal=causal) else: assert inference_params.fused_ft_kernel assert ft_attention is not None context = ft_attention.single_query_attention( *rearrange(qkv, 'b 1 three h d -> b three h d').unbind(dim=1), *inference_params.key_value_memory_dict[self.layer_idx], inference_params.lengths_per_sample, inference_params.sequence_len_offset, self.rotary_emb_dim, # neox_rotary_style (not self.rotary_emb.interleaved) if self.rotary_emb_dim > 0 else True ) context = rearrange(context, 'b h d -> b 1 h d') else: if not self.return_residual: q = self.Wq(x if mixer_subset is None else x[:, mixer_subset]) kv = self.Wkv(x_kv if x_kv is not None else x) else: if x_kv is not None: kv, x_kv = self.Wkv(x_kv) else: kv, x = self.Wkv(x) q = self.Wq(x if mixer_subset is None else x[:, mixer_subset]) q = rearrange(q, '... (h d) -> ... h d', d=self.head_dim) kv = rearrange(kv, '... (two h d) -> ... two h d', two=2, d=self.head_dim) if self.dwconv: q = rearrange(self.dwconv_q(rearrange(q, 'b s d -> b d s'))[..., :-2], 'b d s -> b s d').contiguous() kv = rearrange(self.dwconv_kv(rearrange(kv, 'b s d -> b d s'))[..., :-2], 'b d s -> b s d').contiguous() if inference_params is None: if not self.checkpointing: context = self.inner_cross_attn(q, kv, **kwargs) else: context = torch.utils.checkpoint.checkpoint(self.inner_cross_attn, q, kv, **kwargs) else: kv = self._update_kv_cache(kv) context = self.inner_cross_attn(q, kv, causal=False) out = self.out_proj(rearrange(context, '... h d -> ... (h d)')) return out if not self.return_residual else (out, x) class ParallelMHA(nn.Module): """Multi-head self-attention and cross-attention """ def __init__(self, embed_dim, num_heads, process_group, qkv_proj_bias=True, out_proj_bias=True, dropout=0.0, softmax_scale=None, causal=False, layer_idx=None, rotary_emb_dim=0, rotary_emb_scale_base=None, rotary_emb_interleaved=False, use_flash_attn=False, checkpointing=False, sequence_parallel=True, device=None, dtype=None) -> None: factory_kwargs = {'device': device, 'dtype': dtype} super().__init__() self.embed_dim = embed_dim self.causal = causal self.layer_idx = layer_idx self.rotary_emb_dim = rotary_emb_dim self.use_flash_attn = use_flash_attn self.checkpointing = checkpointing self.num_heads = num_heads assert self.embed_dim % num_heads == 0, "self.kdim must be divisible by num_heads" self.head_dim = self.embed_dim // num_heads if self.rotary_emb_dim > 0: assert RotaryEmbedding is not None, 'rotary_emb is not installed' self.rotary_emb = RotaryEmbedding(self.rotary_emb_dim, scale_base=rotary_emb_scale_base, interleaved=rotary_emb_interleaved, device=device) if ColumnParallelLinear is None or RowParallelLinear is None: raise ImportError('fused_dense is not installed') self.Wqkv = ColumnParallelLinear(embed_dim, 3 * embed_dim, process_group, bias=qkv_proj_bias, sequence_parallel=sequence_parallel, **factory_kwargs) inner_attn_cls = FlashSelfAttention if use_flash_attn else SelfAttention inner_cross_attn_cls = FlashCrossAttention if use_flash_attn else CrossAttention self.inner_attn = inner_attn_cls(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout) self.inner_cross_attn = inner_cross_attn_cls(causal=causal, softmax_scale=softmax_scale, attention_dropout=dropout) self.out_proj = RowParallelLinear(embed_dim, embed_dim, process_group, bias=out_proj_bias, sequence_parallel=sequence_parallel, **factory_kwargs) def forward(self, x, seqlen=None, inference_params=None, **kwargs): """ Arguments: x: (batch, seqlen, hidden_dim) (where hidden_dim = num heads * head dim) if seqlen=None. If seqlen is not None, x is (batch * seqlen, hidden_dim). This is so that when we split x during sequence parallel, we split the batch * seqlen dimension (in case batch is small). """ qkv = self.Wqkv(x) if seqlen is None: qkv = rearrange(qkv, 'b s (three h d) -> b s three h d', three=3, d=self.head_dim) else: qkv = rearrange(qkv, '(b s) (three h d) -> b s three h d', s=seqlen, three=3, d=self.head_dim) if inference_params is None: if self.rotary_emb_dim > 0: qkv = self.rotary_emb(qkv) if not self.checkpointing: context = self.inner_attn(qkv, **kwargs) else: context = torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, **kwargs) else: if (not inference_params.fused_ft_kernel) or inference_params.sequence_len_offset == 0: if self.rotary_emb_dim > 0: qkv = self.rotary_emb(qkv, seqlen_offset=inference_params.sequence_len_offset) q = qkv[:, :, 0] assert self.layer_idx is not None, 'Generation requires layer_idx in the constructor' kv = _update_kv_cache(qkv[:, :, 1:], inference_params, self.layer_idx) # If we're processing the prompt, causal=None (use self.causal). # If we're decoding, then causal=False. causal = None if inference_params.sequence_len_offset == 0 else False context = self.inner_cross_attn(q, kv, causal=causal) else: assert inference_params.fused_ft_kernel assert ft_attention is not None context = ft_attention.single_query_attention( *rearrange(qkv, 'b 1 three h d -> b three h d').unbind(dim=1), *inference_params.key_value_memory_dict[self.layer_idx], inference_params.lengths_per_sample, inference_params.sequence_len_offset, self.rotary_emb_dim, # neox_rotary_style (not self.rotary_emb.interleaved) if self.rotary_emb_dim > 0 else True ) context = rearrange(context, 'b h d -> b 1 h d') if seqlen is None: context = rearrange(context, 'b s h d -> b s (h d)') else: context = rearrange(context, 'b s h d -> (b s) (h d)') out = self.out_proj(context) return out
EXA-1-master
exa/modular_components/attentions/flash-attention/flash_attn/modules/mha.py
import math from functools import partial import torch import torch.nn.functional as F from torch import nn, einsum from local_attention import LocalMHA from einops import rearrange, repeat, pack, unpack # helper functions def exists(val): return val is not None def default(val, d): return val if exists(val) else d def divisible_by(numer, denom): return (numer % denom) == 0 def pack_one(t, pattern): return pack([t], pattern) def unpack_one(t, ps, pattern): return unpack(t, ps, pattern)[0] def pad_to_multiple(tensor, multiple, dim=-1, value=0): seq_len = tensor.shape[dim] m = seq_len / multiple if m.is_integer(): return tensor, seq_len remainder = math.ceil(m) * multiple - seq_len pad_offset = (0,) * (-1 - dim) * 2 padded_tensor = F.pad(tensor, (*pad_offset, 0, remainder), value = value) return padded_tensor, seq_len def batched_gather(x, indices): batch_range = create_batch_range(indices, indices.ndim - 1) return x[batch_range, indices] # tensor helpers def create_batch_range(t, right_pad_dims = 1): b, device = t.shape[0], t.device batch_range = torch.arange(b, device = device) pad_dims = ((1,) * right_pad_dims) return batch_range.reshape(-1, *pad_dims) # normalization 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): normed = F.normalize(x, dim = -1) return normed * self.scale * self.gamma # modules def FeedForward(dim, mult = 4): dim_hidden = int(dim * mult) return nn.Sequential( RMSNorm(dim), nn.Linear(dim, dim_hidden), nn.GELU(), nn.Linear(dim_hidden, dim) ) class Attention(nn.Module): def __init__( self, dim, dim_head = 64, heads = 8, multiply_keys_by_score = False ): super().__init__() self.heads = heads self.scale = dim_head ** -0.5 dim_hidden = dim_head * heads self.multiply_keys_by_score = multiply_keys_by_score self.norm = RMSNorm(dim) self.null_kv = nn.Parameter(torch.randn(2, heads, dim_head)) self.to_q = nn.Linear(dim, dim_hidden, bias = False) self.to_kv = nn.Linear(dim, dim_hidden * 2, bias = False) self.to_out = nn.Linear(dim_hidden, dim, bias = False) def forward( self, x, context = None, mask = None, normalized_scores_kv = None, normalized_scores_q = None ): """ einops: b - batch h - heads, or number of heads per route r - routing dimension, for routing different sets of key / values - should be more expressive n - sequence dimension d - head dimension i - input model dimension """ batch, h = x.shape[0], self.heads x = self.norm(x) if exists(context): context = self.norm(context) context = default(context, x) # if routing dimension is not there, unsqueeze for 1 routing dimension if context.ndim == 3: context = rearrange(context, 'b n d -> b 1 n d') if exists(normalized_scores_kv) and isinstance(normalized_scores_kv, torch.Tensor): if normalized_scores_kv.ndim == 2: normalized_scores_kv = rearrange(normalized_scores_kv, 'b n -> b 1 n') normalized_scores_kv = rearrange(normalized_scores_kv, 'b r n -> b r 1 n 1') num_kv_routes = context.shape[1] # get queries q = self.to_q(x) q = rearrange(q, 'b n (h d) -> b h n d', h = h) if exists(normalized_scores_q) and isinstance(normalized_scores_q, torch.Tensor): q = q * rearrange(normalized_scores_q, 'b n -> b 1 n 1') # handle key / values, with the routing dimension, dividing the number of heads in between the routes assert divisible_by(h, num_kv_routes), 'number of heads must be divisible by the number of key / value routes' heads_per_route = h // num_kv_routes kv_weight = rearrange(self.to_kv.weight, '(r h d) i -> r h d i', h = heads_per_route, r = num_kv_routes) kv = einsum('r h d i, b r n i -> b r h n d', kv_weight, context) k, v = kv.chunk(2, dim = -1) if exists(normalized_scores_kv): # in paper, not sure how they passed back the signal from heavy attention to normalized scores for key/values. just multiply the values by the normalized kv scores for now v = v * normalized_scores_kv if self.multiply_keys_by_score: k = k * normalized_scores_kv k, v = map(lambda t: rearrange(t, 'b r h n d -> b (r h) n d'), (k, v)) # null key values nk, nv = map(lambda t: repeat(t, 'h d -> b h 1 d', b = batch), self.null_kv) k = torch.cat((nk, k), dim = -2) v = torch.cat((nv, v), dim = -2) # scale and get similarity q = q * self.scale sim = einsum('b h i d, b h j d -> b h i j', q, k) # masking if exists(mask): if mask.ndim == 3: mask = repeat(mask, 'b r j -> b (r h) 1 j', h = heads_per_route) else: mask = rearrange(mask, 'b j -> b 1 1 j') mask = F.pad(mask, (1, 0), value = True) sim = sim.masked_fill(~mask, -torch.finfo(sim.dtype).max) # attention attn = sim.softmax(dim = -1) # aggregate out = einsum('b h i j, b h j d -> b h i d', attn, v) # merge heads out = rearrange(out, 'b h n d -> b n (h d)') return self.to_out(out) # routing related logic class DifferentiableTopKRouter(nn.Module): """ differentiable topk using cumulative softmax """ def __init__( self, dim, straight_through = True, temperature = 1., num_routing_tokens = 1 ): super().__init__() self.is_one_routing_token = num_routing_tokens == 1 self.num_routing_tokens = num_routing_tokens self.routing_token = nn.Parameter(torch.randn(num_routing_tokens, dim)) self.straight_through = straight_through self.temperature = temperature def route_back(self, src, routed_tokens, indices): batch_range = create_batch_range(routed_tokens) src[batch_range, indices] = routed_tokens return src def forward( self, x, *, num_tokens, mask = None ): num_routes = self.num_routing_tokens # eventual normalized score scores = einsum('b n d, r d -> b r n', x, self.routing_token) # merge routing dimension into batch x = repeat(x, 'b ... -> (b r) ...', r = num_routes) scores, ps = pack_one(scores, '* n') if exists(mask): mask = repeat(mask, 'b ... -> (b r) ...', r = num_routes) scores = scores / self.temperature if exists(mask): mask_value = -torch.finfo(scores.dtype).max scores = scores.masked_fill(~mask, mask_value) scores, indices = scores.sort(dim = -1) scores = scores - scores.amax(dim = -1, keepdim = True).detach() exp_scores = scores.exp() cum_softmax = exp_scores / exp_scores.cumsum(dim = -1).clamp(min = 1e-6) selected_scores, selected_indices = map(lambda t: t[:, -num_tokens:], (cum_softmax, indices)) if self.straight_through: # this would make sure all normalized scores returned are 1., but still differentiable using straight-through trick selected_scores = selected_scores + (1. - selected_scores).detach() if exists(mask): selected_mask = batched_gather(mask, selected_indices) selected_scores = selected_scores.masked_fill(~selected_mask, 0.) # split out routing dimension again if need be if not self.is_one_routing_token: selected_scores = unpack_one(selected_scores, ps, '* n') selected_indices = unpack_one(selected_indices, ps, '* n') return selected_scores, selected_indices # sinkhorn type routing, with ties to optimal transport from colt5_attention.sinkhorn import gumbel_sinkhorn, scatter_mean, log class SinkhornRouter(nn.Module): """ gumbel sinkhorn router """ """ ex. https://arxiv.org/abs/1910.09036 """ def __init__( self, dim, straight_through = True, n_iters = 8, temperature = 0.7, num_routing_tokens = 1 ): super().__init__() self.is_one_routing_token = num_routing_tokens == 1 self.num_routing_tokens = num_routing_tokens self.routing_token = nn.Parameter(torch.randn(num_routing_tokens, dim)) self.straight_through = straight_through self.gumbel_sinkhorn_fn = partial(gumbel_sinkhorn, temperature = temperature, n_iters = n_iters) def route_back(self, src, routed_tokens, indices): return scatter_mean(src, routed_tokens, indices, dim = 1) def forward( self, x, *, num_tokens, mask = None ): n, num_routes = x.shape[-2], self.num_routing_tokens num_tokens = min(n, num_tokens) # eventual normalized score scores = einsum('b n d, r d -> b r n', x, self.routing_token) # merge routing dimension into batch x = repeat(x, 'b ... -> (b r) ...', r = num_routes) scores, ps = pack_one(scores, '* n') if exists(mask): mask = repeat(mask, 'b ... -> (b r) ...', r = num_routes) # calculate scores scores = repeat(scores, '... j -> ... i j', i = num_tokens) if exists(mask): mask_value = -torch.finfo(scores.dtype).max sinkhorn_mask = rearrange(mask, 'b j -> b 1 j') scores = scores.masked_fill(~sinkhorn_mask, mask_value) # sinkhorn scores = self.gumbel_sinkhorn_fn(scores) # mask again just in case if exists(mask): scores = scores.masked_fill(~sinkhorn_mask, mask_value) selected_scores, selected_indices = scores.topk(1, dim = -1) selected_scores, selected_indices = map(lambda t: rearrange(t, '... 1 -> ...'), (selected_scores, selected_indices)) if self.straight_through: # this would make sure all normalized scores returned are 1., but still differentiable using straight-through trick selected_scores = selected_scores + (1. - selected_scores).detach() if exists(mask): selected_mask = batched_gather(mask, selected_indices) selected_scores = selected_scores.masked_fill(~selected_mask, 0.) # split out routing dimension again if need be if not self.is_one_routing_token: selected_scores = unpack_one(selected_scores, ps, '* n') selected_indices = unpack_one(selected_indices, ps, '* n') return selected_scores, selected_indices from colt5_attention.coor_descent import coor_descent class CoordinateDescentRouter(nn.Module): """ from Wright et al. https://arxiv.org/abs/1502.04759 then adopted by https://arxiv.org/abs/2211.01267 for multi-vector document retrieval by Qian et al finally, used successfully by this paper for routing to heavy branch attention / feedforward """ def __init__( self, dim, straight_through = True, n_iters = 50, # 50 iterations in the paper fetch_k_ratio = 9 / 8, # in the paper, they do a bit slightly higher k (times this ratio) for better learning eps = 1., # the epsilon for coordinate descent. in CoLT5 paper they used 1. apparently num_routing_tokens = 1 ): super().__init__() assert fetch_k_ratio >= 1. self.eps = eps self.n_iters = n_iters self.fetch_k_ratio = fetch_k_ratio self.is_one_routing_token = num_routing_tokens == 1 self.num_routing_tokens = num_routing_tokens self.routing_token = nn.Parameter(torch.randn(num_routing_tokens, dim)) self.straight_through = straight_through def route_back(self, src, routed_tokens, indices): batch_range = create_batch_range(routed_tokens) src[batch_range, indices] = routed_tokens return src def forward( self, x, *, num_tokens, mask = None ): n, device, eps, num_routes = x.shape[-2], x.device, self.eps, self.num_routing_tokens # s stands for eventual normalized score s = einsum('b n d, r d -> b r n', x, self.routing_token) # merge routing dimension into batch x = repeat(x, 'b ... -> (b r) ...', r = num_routes) s, ps = pack_one(s, '* n') if exists(mask): mask = repeat(mask, 'b ... -> (b r) ...', r = num_routes) # k, which controls the sparsity of the outputted scores from iterative coordinate descent effective_k = min(num_tokens * self.fetch_k_ratio, n) k = torch.tensor([effective_k], device = device) # coordinate descent scores = coor_descent(s, n_iters = self.n_iters, mask = mask, k = k, eps = eps) # get the topk scores and indices from the sparse matrix selected_scores, selected_indices = scores.topk(num_tokens, dim = -1) if self.straight_through: # this would make sure all normalized scores returned are 1., but still differentiable using straight-through trick selected_scores = selected_scores + (1. - selected_scores).detach() if exists(mask): selected_mask = batched_gather(mask, selected_indices) selected_scores = selected_scores.masked_fill(~selected_mask, 0.) # split out routing dimension again if need be if not self.is_one_routing_token: selected_scores = unpack_one(selected_scores, ps, '* n') selected_indices = unpack_one(selected_indices, ps, '* n') return selected_scores, selected_indices # all router types ROUTERS = dict( cum_softmax = DifferentiableTopKRouter, sinkhorn = SinkhornRouter, coor_descent = CoordinateDescentRouter ) # main classes class ConditionalRoutedFeedForward(nn.Module): def __init__( self, dim, *, num_heavy_tokens, light_ff_mult = 0.5, heavy_ff_mult = 4, router_straight_through = True, # would make sure all normalized scores are 1., still differentiable router_type = 'coor_descent', router_kwargs: dict = {} ): super().__init__() assert router_type in ROUTERS.keys() self.num_heavy_tokens = num_heavy_tokens self.router_type = router_type router_klass = ROUTERS.get(router_type) self.router = router_klass( dim = dim, straight_through = router_straight_through, **router_kwargs ) self.light_ff = FeedForward(dim, light_ff_mult) self.heavy_ff = FeedForward(dim, heavy_ff_mult) def forward( self, x, mask = None, num_heavy_tokens = None ): device, num_heavy_tokens = x.device, default(num_heavy_tokens, self.num_heavy_tokens) # light feedforward sees all the tokens (hidden dimension is only 1/2 of model dimensions) light_out = self.light_ff(x) # route tokens appropriately for heavy branch normalized_scores, indices = self.router(x, num_tokens = num_heavy_tokens, mask = mask) # select the tokens to be routed to heavier feedforward (hidden dimension is 4 times model dimensions) routed_tokens = batched_gather(x, indices) # do the heavier branch with only routed tokens routed_tokens_out = self.heavy_ff(routed_tokens) * rearrange(normalized_scores, '... -> ... 1') # scatter back the output of the heavy feedforward branch heavy_out = torch.zeros_like(x) heavy_out = self.router.route_back(heavy_out, routed_tokens_out, indices) # sum light and heavy branches return light_out + heavy_out class ConditionalRoutedAttention(nn.Module): def __init__( self, dim, *, num_heavy_tokens_q, num_heavy_tokens_kv, num_routed_kv = 1, light_dim_head = 64, light_heads = 8, light_window_size = 128, # each token would see ~ 64 tokens either way to left or right heavy_dim_head = 64, heavy_heads = 8, router_straight_through = True, # would make sure all normalized scores are 1., still differentiable router_type = 'coor_descent', router_kwargs: dict = {}, multiply_keys_by_score = False, multiply_queries_by_score = False ): super().__init__() assert router_type in ROUTERS.keys() self.router_type = router_type router_klass = ROUTERS.get(router_type) self.num_heavy_tokens_q = num_heavy_tokens_q self.num_heavy_tokens_kv = num_heavy_tokens_kv self.multiply_queries_by_score = multiply_queries_by_score self.light_attn = LocalMHA( dim = dim, dim_head = light_dim_head, heads = light_heads, window_size = light_window_size // 2, prenorm = True, causal = False, use_rotary_pos_emb = False, look_backward = 1, look_forward = 1 ) self.q_router = router_klass( dim = dim, straight_through = router_straight_through, **router_kwargs ) self.kv_router = router_klass( dim = dim, num_routing_tokens = num_routed_kv, straight_through = router_straight_through, **router_kwargs ) self.heavy_attn = Attention( dim = dim, dim_head = heavy_dim_head, heads = heavy_heads, multiply_keys_by_score = multiply_keys_by_score ) def forward( self, x, *, num_heavy_tokens_q = None, num_heavy_tokens_kv = None, mask = None ): batch, device = x.shape[0], x.device num_heavy_tokens_q = default(num_heavy_tokens_q, self.num_heavy_tokens_q) num_heavy_tokens_kv = default(num_heavy_tokens_kv, self.num_heavy_tokens_kv) # light local attention sees all tokens in a limited context light_out = self.light_attn(x, mask = mask) # route tokens appropriately for heavy branch normalized_scores_q, indices_q = self.q_router(x, num_tokens = num_heavy_tokens_q, mask = mask) normalized_scores_kv, indices_kv = self.kv_router(x, num_tokens = num_heavy_tokens_kv, mask = mask) # select the tokens to be routed to full attention routed_tokens_q = batched_gather(x, indices_q) kv_batch_range = create_batch_range(x, right_pad_dims = indices_kv.ndim - 1) routed_tokens_kv = batched_gather(x, indices_kv) # calculate key padding mask routed_tokens_kv_mask = None if exists(mask): routed_tokens_kv_mask = mask[kv_batch_range, indices_kv] # do the heavier branch with only routed tokens routed_tokens_out = self.heavy_attn( routed_tokens_q, mask = routed_tokens_kv_mask, context = routed_tokens_kv, normalized_scores_kv = normalized_scores_kv, normalized_scores_q = normalized_scores_q if self.multiply_queries_by_score else None ) routed_tokens_out = routed_tokens_out * rearrange(normalized_scores_q, '... -> ... 1') # scatter back the output of the heavy branch heavy_out = torch.zeros_like(x) heavy_out = self.q_router.route_back(heavy_out, routed_tokens_out, indices_q) # sum light and heavy branches return light_out + heavy_out # improvised conditionally routed autoregressive attention class ConditionalRoutedAutoregressiveAttention(nn.Module): def __init__( self, dim, *, num_heavy_tokens_q, num_heavy_tokens_kv, num_routed_kv = 1, light_dim_head = 64, light_heads = 8, light_window_size = 128, # each token would see ~ 64 tokens either way to left or right heavy_window_size = None, heavy_dim_head = 64, heavy_heads = 8, router_straight_through = True, # would make sure all normalized scores are 1., still differentiable router_type = 'coor_descent', router_kwargs: dict = {}, multiply_keys_by_score = False, multiply_queries_by_score = False ): super().__init__() assert router_type in ROUTERS.keys() self.router_type = router_type router_klass = ROUTERS.get(router_type) self.num_heavy_tokens_q = num_heavy_tokens_q self.num_heavy_tokens_kv = num_heavy_tokens_kv self.multiply_queries_by_score = multiply_queries_by_score self.heavy_window_size = default(heavy_window_size, light_window_size) self.light_attn = LocalMHA( dim = dim, dim_head = light_dim_head, heads = light_heads, window_size = light_window_size, prenorm = True, causal = True, use_rotary_pos_emb = False ) self.q_router = router_klass( dim = dim, straight_through = router_straight_through, **router_kwargs ) self.kv_router = router_klass( dim = dim, num_routing_tokens = num_routed_kv, straight_through = router_straight_through, **router_kwargs ) self.heavy_attn = Attention( dim = dim, dim_head = heavy_dim_head, heads = heavy_heads, multiply_keys_by_score = multiply_keys_by_score ) def forward( self, x, *, num_heavy_tokens_q = None, num_heavy_tokens_kv = None ): batch, device = x.shape[0], x.device num_heavy_tokens_q = default(num_heavy_tokens_q, self.num_heavy_tokens_q) num_heavy_tokens_kv = default(num_heavy_tokens_kv, self.num_heavy_tokens_kv) # light local attention sees all tokens in a limited context light_out = self.light_attn(x) # pad sequence to multiple of the heavy window size # routing will take place within each heavy window block size window_size = self.heavy_window_size x, seq_len = pad_to_multiple(x, window_size, dim = -2) padded_seq_len = x.shape[-2] # construct mask, and make sure not to attend to padding q_mask = torch.ones((batch, seq_len), dtype = torch.bool, device = device) q_mask = F.pad(q_mask, (0, padded_seq_len - seq_len), value = False) # block the sequence and mask into windows for the queries q = rearrange(x, 'b (n w) d -> (b n) w d', w = window_size) q_mask = rearrange(q_mask, 'b (n w) -> (b n) w', w = window_size) # each block of queries attend to sequences that are causally masked out appropriately windows = padded_seq_len // window_size kv = repeat(x, 'b n d -> (b m) n d', m = windows) kv_mask = torch.ones((windows, windows), dtype = torch.bool, device = device).tril(-1) kv_mask = repeat(kv_mask, 'm n -> (b m) (n w)', b = batch, w = window_size) # route tokens appropriately for heavy branch, if need be should_route_q = q.shape[-2] > num_heavy_tokens_q should_route_kv = kv.shape[-2] > num_heavy_tokens_kv if should_route_q: normalized_scores_q, indices_q = self.q_router(q, num_tokens = num_heavy_tokens_q, mask = q_mask) routed_tokens_q = batched_gather(q, indices_q) else: normalized_scores_q = 1. routed_tokens_q = q if should_route_kv: normalized_scores_kv, indices_kv = self.kv_router(kv, num_tokens = num_heavy_tokens_kv, mask = kv_mask) routed_tokens_kv = batched_gather(kv, indices_kv) routed_tokens_kv_mask = batched_gather(kv_mask, indices_kv) else: normalized_scores_kv = 1. routed_tokens_kv = kv routed_tokens_kv_mask = kv_mask # do the heavier branch with only routed tokens routed_tokens_out = self.heavy_attn( routed_tokens_q, mask = routed_tokens_kv_mask, context = routed_tokens_kv, normalized_scores_kv = normalized_scores_kv, normalized_scores_q = normalized_scores_q if self.multiply_queries_by_score else None ) if should_route_q: routed_tokens_out = routed_tokens_out * rearrange(normalized_scores_q, '... -> ... 1') # scatter back the output of the heavy branch heavy_out = torch.zeros_like(q) heavy_out = self.q_router.route_back(heavy_out, routed_tokens_out, indices_q) else: heavy_out = routed_tokens_out # un-window and slice out original sequence heavy_out = rearrange(heavy_out, '(b n) w d -> b (n w) d', b = batch) heavy_out = heavy_out[:, :seq_len] # sum light and heavy branches return light_out + heavy_out # adapting the conditional routed self attention to cross attention class ConditionalRoutedCrossAttention(nn.Module): def __init__( self, dim, *, num_tokens_q, num_tokens_kv, num_sets_kv = 1, # setting this greater than 1 would route multiple sets of key / values, each of size num_tokens_kv, using this many routing tokens dim_head = 64, heads = 8, router_straight_through = True, # would make sure all normalized scores are 1., still differentiable router_type = 'coor_descent', router_kwargs: dict = {}, kv_routing_tokens = 1, multiply_keys_by_score = False ): super().__init__() assert router_type in ROUTERS.keys() self.router_type = router_type router_klass = ROUTERS.get(router_type) self.num_tokens_q = num_tokens_q self.num_tokens_kv = num_tokens_kv self.q_router = router_klass( dim = dim, straight_through = router_straight_through, **router_kwargs ) self.kv_router = router_klass( dim = dim, straight_through = router_straight_through, num_routing_tokens = kv_routing_tokens, **router_kwargs ) self.heavy_attn = Attention( dim = dim, dim_head = dim_head, heads = heads, multiply_keys_by_score = multiply_keys_by_score ) def forward( self, x, context, *, num_tokens_q = None, num_tokens_kv = None, mask = None, context_mask = None ): batch, device = x.shape[0], x.device # route the queries query_length = x.shape[-2] num_tokens_q = default(num_tokens_q, self.num_tokens_q) routed_tokens_q = x should_route_queries = query_length > num_tokens_q if should_route_queries: normalized_scores_q, indices_q = self.q_router(x, num_tokens = num_tokens_q, mask = mask) routed_tokens_q = batched_gather(x, indices_q) # route the long contexts key_value_length = context.shape[-2] num_tokens_kv = default(num_tokens_kv, self.num_tokens_kv) routed_tokens_kv = context routed_tokens_kv_mask = context_mask normalized_scores_kv = None should_route_kv = key_value_length > num_tokens_kv if should_route_kv: normalized_scores_kv, indices_kv = self.kv_router(context, num_tokens = num_tokens_kv, mask = context_mask) routed_tokens_kv = batched_gather(x, indices_kv) routed_tokens_kv_mask = None if exists(context_mask): routed_tokens_kv_mask = batched_gather(context_mask, indices_kv) # do the heavier branch with only routed tokens routed_tokens_out = self.heavy_attn( routed_tokens_q, mask = routed_tokens_kv_mask, context = routed_tokens_kv, normalized_scores_kv = normalized_scores_kv ) if should_route_queries: routed_tokens_out = routed_tokens_out * rearrange(normalized_scores_q, '... -> ... 1') # early return if queries did not undergo routing if not should_route_queries: return routed_tokens_out # otherwise, scatter back the query outputs out = torch.zeros_like(x) out = self.q_router.route_back(out, routed_tokens_out, indices_q) return out # block class ConditionalRoutedTransformerBlock(nn.Module): def __init__( self, dim, *, num_heavy_attn_tokens_q, num_heavy_attn_tokens_kv, num_routed_kv = 1, num_heavy_ff_tokens, light_dim_head = 64, light_heads = 8, light_window_size = 128, heavy_dim_head = 64, heavy_heads = 8, light_ff_mult = 0.5, heavy_ff_mult = 4, router_straight_through = True, router_type = 'coor_descent', router_kwargs: dict = {}, multiply_keys_by_score = False, multiply_queries_by_score = False ): super().__init__() self.conditional_ff = ConditionalRoutedFeedForward( dim, num_heavy_tokens = num_heavy_ff_tokens, light_ff_mult = light_ff_mult, heavy_ff_mult = heavy_ff_mult, router_straight_through = router_straight_through, router_type = router_type, router_kwargs = router_kwargs ) self.conditional_attn = ConditionalRoutedAttention( dim, light_dim_head = light_dim_head, light_heads = light_heads, light_window_size = light_window_size, heavy_dim_head = heavy_dim_head, heavy_heads = heavy_heads, num_heavy_tokens_q = num_heavy_attn_tokens_q, num_heavy_tokens_kv = num_heavy_attn_tokens_kv, num_routed_kv = num_routed_kv, router_straight_through = router_straight_through, router_type = router_type, router_kwargs = router_kwargs, multiply_keys_by_score = multiply_keys_by_score, multiply_queries_by_score = multiply_queries_by_score ) def forward( self, x, mask = None, num_heavy_attn_tokens_q = None, num_heavy_attn_tokens_kv = None, num_heavy_ff_tokens = None ): x = self.conditional_attn(x, mask = mask, num_heavy_tokens_q = num_heavy_attn_tokens_q, num_heavy_tokens_kv = num_heavy_attn_tokens_kv) + x x = self.conditional_ff(x, mask = mask, num_heavy_tokens = num_heavy_ff_tokens) + x return x
EXA-1-master
exa/modular_components/attentions/conditional_flash_attention/other.py
EXA-1-master
exa/modular_components/attentions/conditional_flash_attention/coltflash.py
# from nebulaV4 import one_hot_encoding # from nebulaV4 import Nebula # import torch # import numpy as np # import matplotlib.pyplot as plt # import torch.nn as nn # class LossFunction: # def compute_loss(self, y_pred, y_true): # raise NotImplemented("compute_loss method must be implemented") # #implement specific loss functions that inherit from LossFunction # class L1Loss(LossFunction): # def __init__(self): # self.loss_function = nn.L1Loss() # def compute_loss(self, y_pred, y_true): # return self.loss_function(y_pred, y_true) # class MSELoss(LossFunction): # def __init__(self): # self.loss_function = nn.MSELoss() # def compute_loss(self, y_pred, y_true): # return self.loss_function(y_pred, y_true) # class CrossEntropyLoss(LossFunction): # def __init__(self): # self.loss_function = nn.CrossEntropyLoss() # def compute_loss(self, y_pred, y_true): # return self.loss_function(y_pred, y_true) # def prepare_targets(loss_function, y_true, num_classes=None): # if isinstance(loss_function, L1Loss) and num_classes is not None: # return one_hot_encoding(y_true, num_classes) # return y_true # def generate_classification_data(num_samples, num_classes): # y_true = torch.randint(0, num_classes, (num_samples,)) # y_pred = torch.rand(num_samples, num_classes) # return y_pred, y_true # def generate_regression_data(num_samples): # y_true = torch.randn(num_samples) # y_pred = torch.randn(num_samples) # return y_pred, y_true # def test_loss_functions(loss_functions, y_pred, y_true, num_classes=None): # results = [] # for loss_function in loss_functions: # prepared_y_true = prepare_targets(loss_function, y_true, num_classes) # loss = loss_function.compute_loss(y_pred, prepared_y_true) # results.append(loss.item()) # return results # def plot_loss_comparison(loss_functions, losses): # loss_function_names = [loss_function.__class__.__name__ for loss_function in loss_functions] # plt.bar(loss_function_names, losses) # plt.xlabel("Loss Functions") # plt.ylabel("Loss Value") # plt.title("Loss Function Comparison") # plt.show() # batch_size = 100 # num_classes = 5 # y_true_classification = torch.randint(0, num_classes, (batch_size,)) # num_classes = y_true_classification.max().item() + 1 # # Generate classification data # y_pred_classification, y_true_classification = generate_classification_data(num_classes, num_classes) # # Generate regression data # y_pred_regression, y_true_regression = generate_regression_data(num_classes) # # Loss functions to compare # loss_functions = [Nebula(), L1Loss(), MSELoss(), CrossEntropyLoss()] # # Test classification data # print("Classification Losses:") # classification_losses = test_loss_functions(loss_functions, y_pred_classification, y_true_classification, num_classes=num_classes) # # Test regression data # print("\nRegression Losses:") # regression_losses = test_loss_functions(loss_functions, y_pred_regression, y_true_regression) # # Plot comparison # print("\nLoss Comparison for Classification:") # plot_loss_comparison(loss_functions, classification_losses) # print("\nLoss Comparison for Regression:") # plot_loss_comparison(loss_functions, regression_losses) # from nebulaV4 import one_hot_encoding # from nebulaV4 import Nebula import torch import numpy as np import matplotlib.pyplot as plt import torch.nn as nn from nebulav2 import Nebula from nebulav2 import one_hot_encoding import torch.nn.functional as F def generate_multilabel_classification_data(num_samples, num_classes): y_true = torch.randint(0, 2, (num_samples, num_classes)).float() y_pred = torch.rand(num_samples, num_classes) return y_pred, y_true class LossFunction: def compute_loss(self, y_pred, y_true): raise NotImplemented("compute_loss method must be implemented") class L1Loss(LossFunction): def __init__(self): self.loss_function = nn.L1Loss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) class MSELoss(LossFunction): def __init__(self): self.loss_function = nn.MSELoss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) class CrossEntropyLoss(LossFunction): def __init__(self): self.loss_function = nn.CrossEntropyLoss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) class MultiLabelSoftMarginLoss(LossFunction): def __init__(self): self.loss_function = nn.MultiLabelSoftMarginLoss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) class PoissonNLLoss(LossFunction): def __init__(self): self.loss_function = nn.PoissonNLLLoss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) class KLDivLoss(LossFunction): def __init__(self): self.loss_function = nn.KLDivLoss() def compute_loss(self, y_pred, y_true): return self.loss_function(F.log_softmax(y_pred, dim=1), y_true) class PoissonNLLLoss(LossFunction): def __init__(self): self.loss_function = nn.PoissonNLLLoss() def compute_loss(self, y_pred, y_true): return self.loss_function(y_pred, y_true) # def prepare_targets(loss_function, y_true, num_classes=None): # if isinstance(loss_function, L1Loss) and num_classes is not None: # return one_hot_encoding(y_true, num_classes) # return y_true def prepare_targets(loss_function, y_true, num_classes=None): if (isinstance(loss_function, L1Loss) or isinstance(loss_function, MSELoss)) and num_classes is not None: return one_hot_encoding(y_true, num_classes) if isinstance(loss_function, PoissonNLLLoss): return y_true.view(-1, 1).expand(-1, num_classes) if isinstance(loss_function, KLDivLoss): return y_true.float() return y_true def generate_classification_data(num_samples, num_classes, for_poisson_nll=False): y_true = torch.randint(0, num_classes, (num_samples,)) if for_poisson_nll: y_true = y_true.view(-1, 1).expand(-1, num_classes).float() y_pred = torch.rand(num_samples, num_classes) return y_pred, y_true def generate_regression_data(num_samples): y_true = torch.abs(torch.randn(num_samples)) y_pred = torch.randn(num_samples) return y_pred, y_true # def test_loss_functions(loss_functions, y_pred, y_true, num_classes=None): # results = [] # for loss_function in loss_functions: # prepared_y_true = prepare_targets(loss_function, y_true, num_classes) # loss = loss_function.compute_loss(y_pred, prepared_y_true) # results.append(loss.item()) # return results def test_loss_functions(loss_functions, y_pred, y_true, num_classes=None): losses = [] for loss_function in loss_functions: for_poisson_nll = isinstance(loss_function, PoissonNLLLoss) if num_classes is not None and not for_poisson_nll: y_true = y_true.squeeze() elif for_poisson_nll: y_true = y_true.view(-1, 1).expand(-1, num_classes) prepared_y_true = prepare_targets(loss_function, y_true, num_classes) loss = loss_function.compute_loss(y_pred, prepared_y_true) losses.append(loss.item()) return losses def plot_loss_comparison(loss_functions, losses): loss_function_names = [loss_function.__class__.__name__ for loss_function in loss_functions] plt.bar(loss_function_names, losses) plt.xlabel("Loss Functions") plt.ylabel("Loss Value") plt.title("Loss Function Comparison") plt.show() batch_size = 100 num_classes = 5 y_true_classification = torch.randint(0, num_classes, (batch_size,)) num_classes = y_true_classification.max().item() + 1 # Generate classification data y_pred_classification, y_true_classification = generate_classification_data(batch_size, num_classes) y_pred_multilabel_classification, y_true_multilabel_classification = generate_multilabel_classification_data(batch_size, num_classes) # Generate regression data y_pred_regression, y_true_regression = generate_regression_data(batch_size) # Loss functions to compare loss_functions = [Nebula(), L1Loss(), MSELoss(), CrossEntropyLoss(), PoissonNLLoss(), KLDivLoss(), PoissonNLLLoss()] # Test classification data # # Test classification data print("Classification Losses:") classification_losses = test_loss_functions(loss_functions, y_pred_classification, y_true_classification, num_classes=num_classes) # Test regression data print("\nRegression Losses:") regression_losses = test_loss_functions(loss_functions, y_pred_regression, y_true_regression) # Plot comparison print("\nLoss Comparison for Classification:") plot_loss_comparison(loss_functions, classification_losses) print("\nLoss Comparison for Regression:") plot_loss_comparison(loss_functions, regression_losses) # Test multi-label classification data print("Multi-label Classification Losses:") multilabel_classification_losses = test_loss_functions(loss_functions, y_pred_multilabel_classification, y_true_multilabel_classification, num_classes=num_classes) # Plot comparison print("\nLoss Comparison for Multi-label Classification:") plot_loss_comparison(loss_functions, multilabel_classification_losses)
EXA-1-master
exa/modular_components/lossFunctions/nebula/test.py