MCPcopy Create free account
hub / github.com/cli99/llm-analysis / get_activation_memory_per_layer

Method get_activation_memory_per_layer

llm_analysis/analysis.py:737–843  ·  view source on GitHub ↗

Get the memory (in bytes) required to store the activations of a transformer layer, given the batch size, sequence length, and whether it is inference or training, the activation recomputation strategy, and the activation data type. Refer to https://arxiv.org/abs/2205.05198 f

(
        self,
        batch_size: int,
        seq_len: int,
        is_inference: bool = True,
        activation_recomputation:
        ActivationRecomputation = ActivationRecomputation.NONE,
        layernorm_dtype_bytes: int = BYTES_FP32,
        flash_attn: bool = True,
        softmax_dropout: bool = False,
        mlp_activation_quant_bits: int = None,
        mlp_1linear_quant_bits: int = None,
        mlp_gelu_input_quant_bits: int = None,
        mlp_2linear_quant_bits: int = None,
        mlp_recompute_gelu: bool = False,
        mlp_gated_linear_units: bool = False,
        return_breakdown: bool = False,
    )

Source from the content-addressed store, hash-verified

735 return self.model_config.vocab_size * batch_size * seq_len * self.dtype_config.activation_bits / BITS_PER_BYTE / self.parallelism_config.tp_size
736
737 def get_activation_memory_per_layer(
738 self,
739 batch_size: int,
740 seq_len: int,
741 is_inference: bool = True,
742 activation_recomputation:
743 ActivationRecomputation = ActivationRecomputation.NONE,
744 layernorm_dtype_bytes: int = BYTES_FP32,
745 flash_attn: bool = True,
746 softmax_dropout: bool = False,
747 mlp_activation_quant_bits: int = None,
748 mlp_1linear_quant_bits: int = None,
749 mlp_gelu_input_quant_bits: int = None,
750 mlp_2linear_quant_bits: int = None,
751 mlp_recompute_gelu: bool = False,
752 mlp_gated_linear_units: bool = False,
753 return_breakdown: bool = False,
754 ) -> Union[float, tuple]:
755 """Get the memory (in bytes) required to store the activations of a
756 transformer layer, given the batch size, sequence length, and whether
757 it is inference or training, the activation recomputation strategy, and
758 the activation data type. Refer to https://arxiv.org/abs/2205.05198 for
759 details. For inference, this assumes the maximum tensor buffer reuse.
760
761 Args:
762 batch_size (int):
763 seq_len (int): sequence length
764 is_inference (bool, optional): whether it is inference or not. Return the max memory activation tensor size between layernorm/attn/mlp. Defaults to True.
765 activation_recomputation (ActivationRecomputation, optional): \
766 activation recomputation strategy. Defaults to ActivationRecomputation.NONE.
767 layernorm_dtype_bytes (int, optional): number of bytes in the data type for \
768 the layernorm activations. Defaults to BYTES_FP32. Often has to be FP32 in training to maintain model accuracy.
769 flash_attn (bool, optional): whether to use Flash Attention. Defaults to True.
770 softmax_dropout (bool, optional): whether to apply dropout after softmax. Defaults to False.
771 mlp_activation_quant_bits (int, optional): number of bits to quantize MLP activations; if set, override the values for mlp_1linear_quant_bits, mlp_gelu_input_quant_bits and mlp_2linear_quant_bits. Defaults to None.
772 mlp_1linear_quant_bits (int, optional): number of bits to quantize the input activations of the first linear layer. Defaults to None.
773 mlp_gelu_input_quant_bits (int, optional): number of bits to quantize the GELU input activations. Defaults to None.
774 mlp_2linear_quant_bits (int, optional): number of bits to quantize the input activations of the second linear layer. Defaults to None. mlp_recompute_gelu (bool, optional): whether to recompute the gelu activation in the MLP backward pass. Defaults to False.
775 mlp_gated_linear_units (bool, optional): whether to use gated linear units in the MLP. Defaults to False.
776 Returns:
777 Union[float, tuple]: the memory (in bytes) required to store the activations of a transformer layer or a tuple of its breakdown
778 """
779 if (not is_inference
780 ) and activation_recomputation == ActivationRecomputation.FULL:
781 activation_memory_per_layer = (seq_len * batch_size *
782 self.model_config.hidden_dim *
783 self.dtype_config.activation_bits /
784 BITS_PER_BYTE /
785 self.parallelism_config.tp_size)
786 if return_breakdown:
787 return activation_memory_per_layer, 0, 0, 0
788 else:
789 return activation_memory_per_layer
790
791 activation_memory_per_layer_attn = (
792 self.get_activation_memory_per_layer_attn(
793 batch_size,
794 seq_len,

Callers 2

inferenceMethod · 0.95
trainingMethod · 0.95

Tested by

no test coverage detected