diff --git a/.github/workflows/amd_nightly.yml b/.github/workflows/amd_nightly.yml deleted file mode 100644 index 3131496ac49..00000000000 --- a/.github/workflows/amd_nightly.yml +++ /dev/null @@ -1,105 +0,0 @@ -name: AMD Nightly Kernel Tests - -on: - workflow_dispatch: - push: - branches: [main_perf] - schedule: - - cron: '0 0 * * *' # runs nightly at midnight UTC - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - Nightly-CDNA-AMD: - runs-on: ${{ matrix.runner }} - strategy: - matrix: - runner: [linux-mi300-gpu-1] - fail-fast: false # disables failing the entire job when one matrix entry fails - timeout-minutes: 720 # self hosted runners can run jobs for longer than the default of 360 minutes - container: - image: rocm/pytorch:latest - options: --device=/dev/kfd --device=/dev/dri --security-opt seccomp=unconfined --shm-size 16G --group-add video --user root - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Show Device Info - run: | - rocminfo | grep gfx - - - name: Uninstall Triton - run: | - pip uninstall -y triton - rm -rf ~/.triton - rm -rf ./triton/python/build - - - name: Install Triton - run: | - pip install triton==3.3.0 - - - name: Show Triton version - run: | - pip show triton - - - name: Build - run: | - FLASH_ATTENTION_TRITON_AMD_ENABLE="TRUE" python setup.py install - - - name: Install dependencies for bench and misc - run: | - pip install matplotlib pandas tabulate - - - name: AMD Internal Tests - run: | - FLASH_ATTENTION_TRITON_AMD_ENABLE="TRUE" FLASH_ATTENTION_TRITON_AMD_AUTOTUNE=0 pytest flash_attn/flash_attn_triton_amd/test.py - - - name: Flash Attention Tests - run: | - FLASH_ATTENTION_TRITON_AMD_ENABLE="TRUE" FLASH_ATTENTION_TRITON_AMD_AUTOTUNE=0 pytest -n 8 tests/test_flash_attn_triton_amd.py - - - name: AMD Bench - run: | - python flash_attn/flash_attn_triton_amd/bench.py -benchmark_fn flash_attn_func flash_attn_varlen_func flash_attn_with_kvcache - - Nightly-RDNA-AMD: - runs-on: ${{ matrix.runner }} - strategy: - matrix: - runner: [gfx1100] - fail-fast: false # disables failing the entire job when one matrix entry fails - timeout-minutes: 720 # self hosted runners can run jobs for longer than the default of 360 minutes - container: - image: rocm/pytorch:latest - options: --device=/dev/kfd --device=/dev/dri --security-opt seccomp=unconfined --group-add video --user root - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Show Device Info - run: | - rocminfo | grep gfx - - - name: Uninstall Triton - run: | - pip uninstall -y triton - rm -rf ~/.triton - rm -rf ./triton/python/build - - - name: Install Triton - run: | - pip install triton==3.3.0 - - - name: Show Triton version - run: | - pip show triton - - - name: Build - run: | - FLASH_ATTENTION_TRITON_AMD_ENABLE="TRUE" python setup.py install - - - name: Flash Attention Tests - run: | - FLASH_ATTENTION_TRITON_AMD_ENABLE="TRUE" FLASH_ATTENTION_TRITON_AMD_AUTOTUNE=0 pytest tests/test_flash_attn_triton_amd.py::test_flash_attn_output diff --git a/.github/workflows/amd_tests.yml b/.github/workflows/amd_tests.yml index 2f49567f960..2e3f061c78d 100644 --- a/.github/workflows/amd_tests.yml +++ b/.github/workflows/amd_tests.yml @@ -60,4 +60,6 @@ jobs: - name: AMD Bench run: | - python flash_attn/flash_attn_triton_amd/bench.py -benchmark_fn flash_attn_func flash_attn_varlen_func flash_attn_with_kvcache + python flash_attn/flash_attn_triton_amd/bench.py -benchmark_fn flash_attn_func + python flash_attn/flash_attn_triton_amd/bench.py -benchmark_fn flash_attn_varlen_func + python flash_attn/flash_attn_triton_amd/bench.py -benchmark_fn flash_attn_with_kvcache diff --git a/flash_attn/flash_attn_triton_amd/.gitignore b/flash_attn/flash_attn_triton_amd/.gitignore new file mode 100644 index 00000000000..21538fc4e4a --- /dev/null +++ b/flash_attn/flash_attn_triton_amd/.gitignore @@ -0,0 +1,2 @@ +bwd_prefill_fused.py +bwd_prefill_onekernel.py \ No newline at end of file diff --git a/flash_attn/flash_attn_triton_amd/bench.py b/flash_attn/flash_attn_triton_amd/bench.py index f2b2e7d11d6..e19de575c8c 100755 --- a/flash_attn/flash_attn_triton_amd/bench.py +++ b/flash_attn/flash_attn_triton_amd/bench.py @@ -5,6 +5,9 @@ import time import argparse import itertools +import logging +import warnings +import datetime import pandas as pd from logging import warning from typing import Dict, List, Literal, Optional, Tuple @@ -73,6 +76,10 @@ "flash_attn_with_kvcache": ["fwd"], } + +# Add a global variable for verbose mode +VERBOSE = False + @dataclass class EnvVariableConfig: key: str @@ -80,7 +87,7 @@ class EnvVariableConfig: backend: Optional[Literal["triton", "ck"]] = None ENV_VARIABLE_CONFIGS : List[EnvVariableConfig] = [ - # EnvVariableConfig(key="BWD_MODE", values=["split", "fused", "jingning"], backend="triton"), + # EnvVariableConfig(key="BWD_MODE", values=["split", "fused_atomics", "fused_no_atomics"], backend="triton"), ] class FunctionConfig: @@ -108,53 +115,6 @@ def __str__(self): def column_name(self): return f"{self}_ms" - - -@lru_cache() -def available_backends(): - available = [] - - # try to load each backend - for backend in ["triton", "ck"]: - try: - # try loading the module with this backend - flash_attn = load_flash_attn_module(backend) - - # if we got here, the backend loaded successfully - available.append(backend) - except Exception as e: - # backend not available, just continue - print(f"Backend {backend} not available. Error: {e}") - - # if no backends available, default to triton - if not available: - raise ValueError("No Backends available") - - return available - -@lru_cache() -def get_fn_params(fn_name): - # get params for fn - packing = get_packing_type(fn_name) - is_varlen = True if "varlen" in fn_name else False - is_fp8 = True if "fp8" in fn_name else False - supported_dtypes = SUPPORTED_DTYPES.get(fn_name, [torch.float16]) # default to float16 if not found - supported_backends = [backend for backend in SUPPORTED_BACKENDS.get(fn_name, ["triton"]) if backend in available_backends()] # default to triton backend - supports_backward = False if fn_name in ["flash_attn_with_kvcache"] else True - supported_modes = SUPPORTED_MODES.get(fn_name, ["fwd"]) - device = "cuda" - - # get supported env configs for each backend - supported_env_configs = {} - for backend in supported_backends: - supported_env_configs[backend] = get_env_value_combinations(backend) - - # check backward pass support - if not supports_backward: - warning(f"{fn_name} does not have a backward pass so benching forward pass only.") - - return is_varlen, is_fp8, packing, supported_dtypes, supported_backends, supported_modes, supported_env_configs, device - def generate_fn_inputs( fn_name: str, BATCH: int, @@ -858,11 +818,12 @@ def get_packing_type(fn_name: str) -> Optional[Literal["kv", "qkv"]]: return packing -def load_flash_attn_module(backend: Literal["triton", "ck"], env_configs: Dict = {}, verbose = False): +def load_flash_attn_module(backend: Literal["triton", "ck"], env_configs: Dict = {}): """ Load the flash_attn module with the specified backend configuration """ - + global VERBOSE + # remove any existing env variables first for key in ENV_FLAGS: if key in os.environ: @@ -881,7 +842,7 @@ def load_flash_attn_module(backend: Literal["triton", "ck"], env_configs: Dict = # add custom env configs add_env_configs(env_configs) - if verbose: + if VERBOSE: # Only print if both local and global verbose are True print(f"Loading flash_attn module with {backend} backend.") # Remove any existing flash_attn modules from sys.modules @@ -894,6 +855,10 @@ def load_flash_attn_module(backend: Literal["triton", "ck"], env_configs: Dict = # Import and return the module import flash_attn + + # disable triton printing from autotuning + if not VERBOSE: + os.environ["TRITON_PRINT_AUTOTUNING"] = "0" return flash_attn @@ -907,11 +872,8 @@ def run_benchmark(func_config: FunctionConfig, input_configs): """ Runs the benchmark for the provided function configuration with the given input configurations. """ - # print new line to seperate benchmark runs - print() - if DEBUG: - print("func_config:", func_config) - + global VERBOSE + # extract function configuration parameters fn_name = func_config.fn_name mode = func_config.mode @@ -919,13 +881,14 @@ def run_benchmark(func_config: FunctionConfig, input_configs): backend = func_config.backend # load flash attention module - flash_attn_module = load_flash_attn_module(backend, func_config.env_configs, verbose=True) + flash_attn_module = load_flash_attn_module(backend, func_config.env_configs) # start timing the benchmark start_time = time.time() - - # print bench fn - print(f"Benchmarking {func_config} ...") + if VERBOSE: + print(f"Benchmarking {func_config} ...") + else: + print(f"Running {fn_name} ({mode}, {backend})...", end='', flush=True) # Setup benchmark configurations bench_configs = [ @@ -966,16 +929,15 @@ def bench_function( ms = triton.testing.do_bench(benchmark_fn, warmup=25, rep=100) return ms - df = bench_function.run(save_path=".", print_data=True, return_df=True)[0] + df = bench_function.run(return_df=True)[0] # set the column name to reflect the function configuration df = df.rename(columns={"Time (ms)": func_config.column_name()}) # calculate and print elapsed time elapsed_time = time.time() - start_time - print(f"Total time for benchmarking {fn_name} in {mode} mode with {dtype}: {elapsed_time:.2f} seconds") - return df + return df, elapsed_time def filter_modes(requested_modes, fn_name, supported_modes_for_fn): modes_to_run = [] @@ -1025,26 +987,88 @@ def get_input_config_set(config_type): return input_configs -def filter_backends(requested_backends, supported_backends, fn_name): +def available_backends(): + """Check which backends are available by trying to load them.""" + available = [] + + for backend in ["triton", "ck"]: + try: + # try loading the module with this backend + load_flash_attn_module(backend) + available.append(backend) + except Exception as e: + # backend not available, just continue + if DEBUG: + print(f"Backend {backend} not available: {e}") + + if not available: + raise ValueError("No backends are available. Please check your flash_attn installation.") + + return available + +# 2. Simplify get_fn_params to remove the backend filtering logic here +@lru_cache() +def get_fn_params(fn_name): + # get params for fn + packing = get_packing_type(fn_name) + is_varlen = True if "varlen" in fn_name else False + is_fp8 = True if "fp8" in fn_name else False + supported_dtypes = SUPPORTED_DTYPES.get(fn_name, [torch.float16]) + supported_backends = SUPPORTED_BACKENDS.get(fn_name, ["triton"]) # just get what the function supports + supports_backward = False if fn_name in ["flash_attn_with_kvcache"] else True + supported_modes = SUPPORTED_MODES.get(fn_name, ["fwd"]) + device = "cuda" + + # get supported env configs for each backend + supported_env_configs = {} + for backend in supported_backends: + supported_env_configs[backend] = get_env_value_combinations(backend) + + # check backward pass support + if not supports_backward: + warning(f"{fn_name} does not have a backward pass so benching forward pass only.") + + return is_varlen, is_fp8, packing, supported_dtypes, supported_backends, supported_modes, supported_env_configs, device + +# 3. Create a new simpler function to validate and filter backends +def validate_backends(requested_backends, supported_backends, fn_name): + """Validate that requested backends are available and supported.""" + # get actually available backends + available = available_backends() + + # determine which backends to use if requested_backends: - selected = [] - for be in requested_backends: - if be in supported_backends: - selected.append(be) - else: - warning( - f"backend '{be}' requested but not supported by " - f"function '{fn_name}'. skipping this back-end." - ) - return selected + # user specified backends - validate them + valid_backends = [] + for backend in requested_backends: + if backend not in available: + warning(f"Backend '{backend}' is not available on this system. Skipping.") + continue + if backend not in supported_backends: + warning(f"Backend '{backend}' is not supported by function '{fn_name}'. Skipping.") + continue + valid_backends.append(backend) + + if not valid_backends: + raise ValueError(f"None of the requested backends {requested_backends} are available and supported for {fn_name}") + + return valid_backends else: - return supported_backends - + # no backends specified - use all available and supported + valid_backends = [b for b in supported_backends if b in available] + + if not valid_backends: + raise ValueError(f"No available backends found for {fn_name}. Function supports {supported_backends} but only {available} are available.") + + return valid_backends +# 4. Update process_args to use the new validate_backends function def process_args(): """ Parses command-line arguments and returns function configs and input configs. """ + global VERBOSE + # create parser parser = argparse.ArgumentParser( prog="Benchmark FlashAttention", @@ -1064,16 +1088,35 @@ def process_args(): type=str, nargs='*', choices=VALID_MODES, - default=None, - help=f"Benchmarking mode(s) to run. If omitted, runs all supported modes for each function.", + default=["fwd", "bwd"], + help=f"Benchmarking mode(s) to run. Default: fwd, bwd", ) parser.add_argument( "--backend", type=str, nargs='*', choices=["triton", "ck"], - default=None, - help="Back-end(s) to run (triton, ck). Omit to run every back-end that is both available and supported by the function.", + default=["triton"], + help="Backend(s) to run. Default: triton", + ) + parser.add_argument( + "--output", + type=str, + choices=["ms", "tflops"], + default="tflops", + help="Output metric type: ms (milliseconds) or tflops (TFLOPS). Default: tflops", + ) + parser.add_argument( + "--format", + type=str, + choices=["csv", "markdown"], + default="csv", + help="Output file format: csv or markdown. Default: csv", + ) + parser.add_argument( + "--verbose", "-v", + action="store_true", + help="Enable verbose output (show autotuning details)", ) # config parser.add_argument("-b", type=int, default=None, help="Batch size") @@ -1087,15 +1130,21 @@ def process_args(): # parse args args = parser.parse_args() + + # Set global verbose flag + VERBOSE = args.verbose # parse function args benchmark_fns = args.benchmark_fn requested_modes = args.mode requested_backends = args.backend + output_type: Literal["ms", "tflops"] = args.output + output_format: Literal["csv", "markdown"] = args.format - # fenerate function configurations and input configurations separately + # generate function configurations and input configurations separately all_function_configs = [] all_input_configs = {} # Maps function config -> input configs + for fn_name in benchmark_fns: is_varlen, is_fp8, packing, supported_dtypes, supported_backends, supported_modes_for_fn, supported_env_configs, device = get_fn_params(fn_name) @@ -1115,10 +1164,7 @@ def process_args(): dropout = args.dropout if args.dropout is not None else 0.0 input_configs = [(batch, hq, hk, sq, sk, d_head, causal, dropout)] else: - if True: - input_configs = get_input_config_set("llama") - else: - input_configs = generate_benchmark_configs(is_varlen, packing) + input_configs = get_input_config_set("llama") # filter by mode modes_to_run = filter_modes(requested_modes, fn_name, supported_modes_for_fn) @@ -1126,12 +1172,11 @@ def process_args(): warning(f"No valid modes to run for function '{fn_name}' based on request and function support. Skipping this function.") continue - # filter by backend - backends_to_run = filter_backends(requested_backends, - supported_backends, - fn_name) - if not backends_to_run: - warning(f"no valid back-ends left for '{fn_name}'. skipping.") + # validate and filter backends + try: + backends_to_run = validate_backends(requested_backends, supported_backends, fn_name) + except ValueError as e: + warning(str(e)) continue # create a function config for each backend and dtype combination @@ -1149,7 +1194,7 @@ def process_args(): all_input_configs[func_config] = fn_inputs - return all_function_configs, all_input_configs + return all_function_configs, all_input_configs, output_type, output_format def check_environment_variables(): for key in ENV_FLAGS: @@ -1202,10 +1247,24 @@ def add_tflops_columns(df: pd.DataFrame, func_cfg: FunctionConfig) -> pd.DataFra df[tf_col] = flops / df[ms_col] * 1e-9 return df +def generate_output_filename(function_configs, output_type, output_format): + # create a timestamp + timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + + # simple filename format + base_filename = f"benchmark_{timestamp}" + + if output_format == "csv": + return base_filename + ".csv" + else: # markdown + return base_filename + ".md" + def main(): """ Main function to run benchmarks. """ + global VERBOSE + # check environment variables check_environment_variables() @@ -1213,19 +1272,37 @@ def main(): total_start_time = time.time() # process args to get function configs and input configs - function_configs, all_input_configs = process_args() + function_configs, all_input_configs, output_type, output_format = process_args() + + # Print summary of what will be benchmarked (always show this) + print(f"\nBenchmarking {len(function_configs)} configuration(s):") + unique_fns = set(fc.fn_name for fc in function_configs) + print(f" Functions: {', '.join(unique_fns)}") + unique_backends = set(fc.backend for fc in function_configs) + print(f" Backends: {', '.join(unique_backends)}") + unique_modes = set(fc.mode for fc in function_configs) + print(f" Modes: {', '.join(unique_modes)}") + print() # run benchmarks for each function configuration combined_ms_df = None combined_tf_df = None input_cols = ["BATCH", "HQ", "HK", "N_CTX_Q", "N_CTX_K", "D_HEAD", "CAUSAL", "DROPOUT"] - for func_config in function_configs: + + for i, func_config in enumerate(function_configs, 1): + # Progress indicator + if not VERBOSE: + print(f"[{i}/{len(function_configs)}] ", end='') + # run benchmark with the input configs for this function config input_configs = all_input_configs[func_config] - df = run_benchmark(func_config, input_configs) - df = add_tflops_columns(df, func_config) + df, elapsed_time = run_benchmark(func_config, input_configs) + + if VERBOSE: + print(f"Total time for benchmarking {func_config.fn_name} in {func_config.mode} mode with {func_config.dtype}: {elapsed_time:.2f} seconds") # add to combined table + df = add_tflops_columns(df, func_config) ms_cols = [c for c in df.columns if c.endswith('_ms')] tf_cols = [c for c in df.columns if c.endswith('_tflops')] @@ -1244,7 +1321,7 @@ def main(): # print total time for all benchmarks total_elapsed_time = time.time() - total_start_time - print(f"Total time for all benchmarks: {total_elapsed_time:.2f} seconds") + print(f"Total benchmark time: {total_elapsed_time:.1f} seconds") # save combined data and make comparisons if we have multiple function configs has_multiple_func_configs = False # len(function_configs) > 1 @@ -1282,19 +1359,33 @@ def main(): print(f"Comparison Results (triton vs ck):") print(f"Ratio values: values > 1 mean triton is faster (by that factor), values < 1 mean ck is faster") - if combined_ms_df is not None: - print("\nCombined wall‑time (ms) table:") - print(combined_ms_df) - combined_ms_df.to_csv("benchmark_ms.csv", index=False) - with open("benchmark_ms.md", 'w') as f: - f.write(combined_ms_df.to_markdown(index=False, floatfmt=".2f")) - - if combined_tf_df is not None: - print("\nCombined throughput (TFLOPs) table:") - print(combined_tf_df) - combined_tf_df.to_csv("benchmark_tflops.csv", index=False) - with open("benchmark_tflops.md", 'w') as f: - f.write(combined_tf_df.to_markdown(index=False, floatfmt=".2f")) + # output based on selected metric + if output_type == "ms": + if combined_ms_df is not None: + filename = generate_output_filename(function_configs, "ms", output_format) + print(f"\nCombined wall-time (ms) table:") + print(combined_ms_df) + + if output_format == "csv": + combined_ms_df.to_csv(filename, index=False) + print(f"Results saved to: {filename}") + else: # markdown + with open(filename, 'w') as f: + f.write(combined_ms_df.to_markdown(index=False, floatfmt=".2f")) + print(f"Results saved to: {filename}") + else: # output_type == "tflops" + if combined_tf_df is not None: + filename = generate_output_filename(function_configs, "tflops", output_format) + print(f"\nCombined throughput (TFLOPs) table:") + print(combined_tf_df) + + if output_format == "csv": + combined_tf_df.to_csv(filename, index=False) + print(f"Results saved to: {filename}") + else: # markdown + with open(filename, 'w') as f: + f.write(combined_tf_df.to_markdown(index=False, floatfmt=".2f")) + print(f"Results saved to: {filename}") if __name__ == "__main__": main() \ No newline at end of file diff --git a/flash_attn/flash_attn_triton_amd/bwd_prefill.py b/flash_attn/flash_attn_triton_amd/bwd_prefill.py deleted file mode 100644 index 7d3faef1b25..00000000000 --- a/flash_attn/flash_attn_triton_amd/bwd_prefill.py +++ /dev/null @@ -1,813 +0,0 @@ -from typing import Literal, Optional -import torch -import triton -import triton.language as tl -from .utils import DEBUG, DROPOUT_USE_PYTORCH, DROPOUT_DUMP, compute_fp8_scaling_factors, get_shapes_from_layout, get_strides_from_layout, is_fp8, write_dropout_mask, create_dropout_mask - -# TODO: move this into utils.py so it's shared among kernels -# NOTE: triton fails to import tl.constexprs so create them here for the file -tl_DROPOUT_USE_PYTORCH: tl.constexpr = triton.language.constexpr(DROPOUT_USE_PYTORCH) -tl_DROPOUT_DUMP: tl.constexpr = triton.language.constexpr(DROPOUT_DUMP) - -@triton.jit -def _bwd_preprocess( - Out, - DO, - Delta, - stride_oz, stride_oh, stride_om, stride_ok, - stride_doz, stride_doh, stride_dom, stride_dok, - stride_deltaz, stride_deltah, stride_deltam, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - DESCALE_do, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - ACTUAL_BLOCK_DMODEL: tl.constexpr, - N_CTX_Q: tl.constexpr, - Z: tl.constexpr, - H: tl.constexpr, - IS_VARLEN: tl.constexpr, - IS_FP8: tl.constexpr, -): - pid_bh = tl.program_id(0) - pid_m = tl.program_id(1) - - # Compute batch and head indices - off_z = pid_bh // H - off_h = pid_bh % H - - if IS_VARLEN: - # Compute sequence lengths for the current batch - q_start = tl.load(cu_seqlens_q + off_z) - q_end = tl.load(cu_seqlens_q + off_z + 1) - k_start = tl.load(cu_seqlens_k + off_z) - k_end = tl.load(cu_seqlens_k + off_z + 1) - - # Compute actual sequence lengths - N_CTX_Q = q_end - q_start - N_CTX_K = k_end - k_start - else: - q_start = 0 - k_start = 0 - N_CTX_Q = max_seqlen_q - N_CTX_K = max_seqlen_k - - off_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) - off_d = tl.arange(0, BLOCK_DMODEL) - - # create masks - mask_m = off_m < N_CTX_Q - mask_d = off_d < ACTUAL_BLOCK_DMODEL - - # compute offsets - o_offset = Out + off_z * stride_oz + off_h * stride_oh + q_start * stride_om - do_offset = DO + off_z * stride_oz + off_h * stride_oh + q_start * stride_om - - # compute pointers - out_ptrs = o_offset + off_m[:, None] * stride_om + off_d[None, :] * stride_ok - do_ptrs = do_offset + off_m[:, None] * stride_dom + off_d[None, :] * stride_dok - - # load - o = tl.load(out_ptrs, mask=mask_m[:, None] & mask_d[None, :], other=0.0) - do = tl.load(do_ptrs, mask=mask_m[:, None] & mask_d[None, :], other=0.0) - - # compute delta - if IS_FP8: - stride_descale_q_z = H - descale_do = tl.load(DESCALE_do + off_z * stride_descale_q_z + off_h) - - # NOTE: do is scaled into the fp8 range and o is in fp8 but should be in the same scale as fp32 - delta = tl.sum(o.to(tl.float32) * (do.to(tl.float32) * descale_do), axis=1) - else: - delta = tl.sum(o.to(tl.float32) * do.to(tl.float32), axis=1) - - # write-back delta - delta_offset = Delta + off_z * stride_deltaz + off_h * stride_deltah + q_start * stride_deltam - delta_ptrs = delta_offset + off_m * stride_deltam - tl.store(delta_ptrs, delta, mask=mask_m) - - -@triton.jit -def _bwd_kernel_one_col_block( - Q, - K, - V, - sm_scale, - Out, - DO, - DQ, - DK, - DV, - L, - D, - q_offset, - k_offset, - v_offset, - do_offset, - dq_offset, - dk_offset, - dv_offset, - l_offset, - delta_offset, - dropout_offset, - stride_dq_all, - stride_qz, - stride_qh, - stride_qm, - stride_qk, - stride_kz, - stride_kh, - stride_kn, - stride_kk, - stride_vz, - stride_vh, - stride_vn, - stride_vk, - stride_deltaz, - stride_deltah, - stride_deltam, - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn, - N_CTX_Q, - N_CTX_K, - start_n, - num_block_m, - num_block_n, - dropout_p, - philox_seed, - batch_philox_offset, - descale_q, - descale_k, - descale_v, - descale_do, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - ACTUAL_BLOCK_DMODEL: tl.constexpr, - BLOCK_N: tl.constexpr, - SEQUENCE_PARALLEL: tl.constexpr, - CAUSAL: tl.constexpr, - DROPOUT: tl.constexpr, - USE_EXP2: tl.constexpr, - GROUP_SIZE: tl.constexpr, - IS_FP8: tl.constexpr, - FP8_MAX: tl.constexpr, -): - if CAUSAL: - # TODO: Causal can skip more blocks with something like lo = start_m * BLOCK_M - lo = 0 - else: - lo = 0 - - # initialize col and head offsets - offs_n = start_n * BLOCK_N + tl.arange(0, BLOCK_N) - offs_d = tl.arange(0, BLOCK_DMODEL) - - # masks - mask_n = offs_n < N_CTX_K - mask_d = offs_d < ACTUAL_BLOCK_DMODEL - kv_mask = mask_n[:, None] & mask_d[None, :] - - - # initialize grad accumulators - dv = tl.zeros([BLOCK_N, BLOCK_DMODEL], dtype=tl.float32) - dk = tl.zeros([BLOCK_N, BLOCK_DMODEL], dtype=tl.float32) - - # load k and v once per column block - k_ptrs = k_offset + offs_n[:, None] * stride_kn + offs_d[None, :] * stride_kk - v_ptrs = v_offset + offs_n[:, None] * stride_vn + offs_d[None, :] * stride_vk - k = tl.load(k_ptrs, mask=kv_mask, other=0.0) - kT = tl.trans(k) - vT = tl.trans(tl.load(v_ptrs, mask=kv_mask, other=0.0)) - - # loop over rows - for start_m in range(lo, num_block_m): - offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) - q_ptrs = q_offset + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk - dq_ptrs = dq_offset + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk - do_ptrs = do_offset + offs_m[:, None] * stride_qm + offs_d[None, :] * stride_qk - - # update mask as row block changes - mask_m = offs_m < N_CTX_Q - q_mask = mask_m[:, None] & mask_d[None, :] - - # load q, k, v, do on-chip - q = tl.load(q_ptrs, mask=q_mask, other=0.0) - do = tl.load(do_ptrs, mask=q_mask, other=0.0) - - # recompute p = softmax(qk, dim=-1).T - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - if IS_FP8: - qk += (tl.dot(q, kT) * descale_q * descale_k) - else: - qk += tl.dot(q, kT) - - if CAUSAL: - col_offset = N_CTX_Q - N_CTX_K - causal_mask = offs_m[:, None] >= (col_offset + offs_n[None, :]) - qk = tl.where(causal_mask, qk, float("-inf")) - - l_ptrs = l_offset + offs_m * stride_deltam - l_i = tl.load(l_ptrs, mask=mask_m) - - # compute p - if USE_EXP2: - RCP_LN2: tl.constexpr = 1.4426950408889634 - qk *= sm_scale * RCP_LN2 - l_i *= RCP_LN2 - p = tl.math.exp2(qk - l_i[:, None]) - else: - qk *= sm_scale - p = tl.math.exp(qk - l_i[:, None]) - - # mask block in the cases where the data is smaller the block size - p_mask = mask_m[:, None] & mask_n[None, :] - p = tl.where(p_mask, p, 0.0) - - if DROPOUT: - # NOTE: must create a new var p_drop to prevent p (which is used later to compute ds) from changing - philox_offset = batch_philox_offset + offs_m[:, None] * stride_dropoutm + offs_n[None, :] * stride_dropoutn - # print("philox_seed:", philox_seed) - # print("philox_offset:", philox_offset) - if tl_DROPOUT_USE_PYTORCH: - dropout_ptrs = dropout_offset + offs_m[:, None] * stride_dropoutm + offs_n[None, :] * stride_dropoutn - dropout_mask = tl.load(dropout_ptrs, mask=p_mask) - else: - rand_vals = tl.rand(philox_seed, philox_offset) - dropout_mask = rand_vals > dropout_p - dropout_scale = 1/ (1 - dropout_p) - - if tl_DROPOUT_DUMP: - dropout_ptrs = dropout_offset + offs_m[:, None] * stride_dropoutm + offs_n[None, :] * stride_dropoutn - tl.store(dropout_ptrs, dropout_mask, mask=p_mask) - - # apply dropout mask - p_drop = tl.where(dropout_mask, p, 0.0) - p_drop_scaled = p_drop * dropout_scale - - # compute dv - if IS_FP8: - scale_p_dropout, descale_p_dropout = compute_fp8_scaling_factors(p_drop_scaled, FP8_MAX) - dv += (tl.dot(tl.trans(p_drop_scaled * scale_p_dropout).to(do.type.element_ty), do) * descale_p_dropout * descale_do) - else: - dv += tl.dot(tl.trans(p_drop_scaled).to(do.type.element_ty), do) - - # compute dp - if IS_FP8: - dp_drop_scaled = (tl.dot(do, vT) * descale_do * descale_v) - else: - dp_drop_scaled = tl.dot(do, vT) - dp = tl.where(dropout_mask, dp_drop_scaled, 0.0) * dropout_scale - else: - - # compute dv - if IS_FP8: - scale_p, descale_p = compute_fp8_scaling_factors(p, FP8_MAX) - dv += (tl.dot(tl.trans(p * scale_p).to(do.type.element_ty), do) * descale_p * descale_do) - else: - dv += tl.dot(tl.trans(p).to(do.type.element_ty), do) - - # compute dp - if IS_FP8: - dp = (tl.dot(do, vT) * descale_do * descale_v) - else: - dp = tl.dot(do, vT) - - - # load delta - delta_ptrs = delta_offset + offs_m * stride_deltam - delta_i = tl.load(delta_ptrs, mask=mask_m) - - # compute ds - dscores_scaled = (p * (dp - delta_i[:, None])) - ds = dscores_scaled * sm_scale - ds = tl.where(p_mask, ds, 0.0) - - # compute descale_ds - if IS_FP8: - scale_ds, descale_ds = compute_fp8_scaling_factors(ds, FP8_MAX) - else: - scale_ds, descale_ds = 1.0, 1.0 - - # compute dk - if IS_FP8: - dk += (tl.dot(tl.trans(ds * scale_ds).to(q.type.element_ty), q) * descale_ds * descale_q) - else: - dk += tl.dot(tl.trans(ds).to(q.type.element_ty), q) - - # compute dq - if SEQUENCE_PARALLEL: - if IS_FP8: - dq = (tl.dot((ds * scale_ds).to(k.type.element_ty), k) * descale_ds * descale_k) - else: - dq = tl.dot(ds.to(k.type.element_ty), k) - else: - dq = tl.load(dq_ptrs, mask=q_mask, other=0.0) - if IS_FP8: - dq += (tl.dot((ds * scale_ds).to(k.type.element_ty), k) * descale_ds * descale_k) - else: - dq += tl.dot(ds.to(k.type.element_ty), k) - tl.store(dq_ptrs, dq.to(Q.dtype.element_ty), mask=q_mask) - - # write-back dv and dk - dk_ptrs = dk_offset + offs_n[:, None] * stride_kn + offs_d[None, :] * stride_kk - dv_ptrs = dv_offset + offs_n[:, None] * stride_vn + offs_d[None, :] * stride_vk - - # write-back - if GROUP_SIZE != 1: - # use atomic_add to properly accumulate gradients from multiple query heads - tl.atomic_add(dk_ptrs, dk.to(K.dtype.element_ty), mask=kv_mask) - tl.atomic_add(dv_ptrs, dv.to(V.dtype.element_ty), mask=kv_mask) - else: - tl.store(dk_ptrs, dk.to(K.dtype.element_ty), mask=kv_mask) - tl.store(dv_ptrs, dv.to(V.dtype.element_ty), mask=kv_mask) - -@triton.jit -def _bwd_kernel( - Q, - K, - V, - sm_scale, - Out, - DO, - DQ, - DK, - DV, - L, - Delta, - Dropout_mask, - DESCALE_q, - DESCALE_k, - DESCALE_v, - DESCALE_do, - stride_dq_all, - stride_qz, - stride_qh, - stride_qm, - stride_qk, - stride_kz, - stride_kh, - stride_kn, - stride_kk, - stride_vz, - stride_vh, - stride_vn, - stride_vk, - stride_deltaz, - stride_deltah, - stride_deltam, - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn, - Z, - HQ, - HK, - num_block_m, - num_block_n, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p, - philox_seed, - philox_offset_base, - BLOCK_M: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - ACTUAL_BLOCK_DMODEL: tl.constexpr, - BLOCK_N: tl.constexpr, - SEQUENCE_PARALLEL: tl.constexpr, - CAUSAL: tl.constexpr, - DROPOUT: tl.constexpr, - USE_EXP2: tl.constexpr, - IS_VARLEN: tl.constexpr, - GROUP_SIZE: tl.constexpr, - IS_FP8: tl.constexpr, - FP8_MAX: tl.constexpr, -): - # program ids - off_zh = tl.program_id(0) - if SEQUENCE_PARALLEL: - start_n = tl.program_id(1) - off_z = off_zh // HQ - off_hq = off_zh % HQ - - # check if GQA/MQA - if GROUP_SIZE != 1: - off_hk = off_hq // GROUP_SIZE - else: - off_hk = off_hq - - if IS_VARLEN: - # Compute sequence lengths for the current batch - q_start = tl.load(cu_seqlens_q + off_z) - q_end = tl.load(cu_seqlens_q + off_z + 1) - k_start = tl.load(cu_seqlens_k + off_z) - k_end = tl.load(cu_seqlens_k + off_z + 1) - - # Compute actual sequence lengths - N_CTX_Q = q_end - q_start - N_CTX_K = k_end - k_start - else: - q_start = 0 - k_start = 0 - N_CTX_Q = max_seqlen_q - N_CTX_K = max_seqlen_k - - # input tensor offsets - q_offset = Q + off_z * stride_qz + off_hq * stride_qh + q_start * stride_qm - k_offset = K + off_z * stride_kz + off_hk * stride_kh + k_start * stride_kn - v_offset = V + off_z * stride_vz + off_hk * stride_vh + k_start * stride_vn - do_offset = DO + off_z * stride_qz + off_hq * stride_qh + q_start * stride_qm - l_offset = L + off_z * stride_deltaz + off_hq * stride_deltah + q_start * stride_deltam - delta_offset = Delta + off_z * stride_deltaz + off_hq * stride_deltah + q_start * stride_deltam - - if DROPOUT: - batch_philox_offset = philox_offset_base + off_z * stride_dropoutz + off_hq * stride_dropouth #+ q_start * stride_dropoutm - dropout_offset = Dropout_mask + off_z * stride_dropoutz + off_hq * stride_dropouth #+ q_start * stride_dropoutm - else: - batch_philox_offset = 0 - dropout_offset = 0 - - if IS_FP8: - stride_descale_q_z = HQ - stride_descale_kv_z = HK - - descale_q = tl.load(DESCALE_q + off_z * stride_descale_q_z + off_hq) - descale_k = tl.load(DESCALE_k + off_z * stride_descale_kv_z + off_hk) - descale_v = tl.load(DESCALE_v + off_z * stride_descale_kv_z + off_hk) - descale_do = tl.load(DESCALE_do + off_z * stride_descale_q_z + off_hq) - else: - descale_q, descale_k, descale_v, descale_do = 1.0, 1.0, 1.0, 1.0 - - # output tensor offsets - dk_offset = DK + off_z * stride_kz + off_hk * stride_kh + k_start * stride_kn - dv_offset = DV + off_z * stride_vz + off_hk * stride_vh + k_start * stride_vn - if SEQUENCE_PARALLEL: - dq_offset = DQ + start_n * stride_dq_all + off_z * stride_qz + off_hq * stride_qh + q_start * stride_qm - else: - dq_offset = DQ + off_z * stride_qz + off_hq * stride_qh + q_start * stride_qm - - # inner loop - if SEQUENCE_PARALLEL: - _bwd_kernel_one_col_block( - Q, - K, - V, - sm_scale, - Out, - DO, - DQ, - DK, - DV, - L, - Delta, - q_offset, - k_offset, - v_offset, - do_offset, - dq_offset, - dk_offset, - dv_offset, - l_offset, - delta_offset, - dropout_offset, - stride_dq_all, - stride_qz, - stride_qh, - stride_qm, - stride_qk, - stride_kz, - stride_kh, - stride_kn, - stride_kk, - stride_vz, - stride_vh, - stride_vn, - stride_vk, - stride_deltaz, - stride_deltah, - stride_deltam, - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn, - N_CTX_Q, - N_CTX_K, - start_n, - num_block_m, - num_block_n, - dropout_p, - philox_seed, - batch_philox_offset, - descale_q, - descale_k, - descale_v, - descale_do, - BLOCK_M=BLOCK_M, - BLOCK_DMODEL=BLOCK_DMODEL, - ACTUAL_BLOCK_DMODEL=ACTUAL_BLOCK_DMODEL, - BLOCK_N=BLOCK_N, - SEQUENCE_PARALLEL=SEQUENCE_PARALLEL, - CAUSAL=CAUSAL, - DROPOUT=DROPOUT, - USE_EXP2=USE_EXP2, - GROUP_SIZE=GROUP_SIZE, - IS_FP8=IS_FP8, - FP8_MAX=FP8_MAX - ) - else: - for start_n in range(0, num_block_n): - _bwd_kernel_one_col_block( - Q, - K, - V, - sm_scale, - Out, - DO, - DQ, - DK, - DV, - L, - Delta, - q_offset, - k_offset, - v_offset, - do_offset, - dq_offset, - dk_offset, - dv_offset, - l_offset, - delta_offset, - dropout_offset, - stride_dq_all, - stride_qz, - stride_qh, - stride_qm, - stride_qk, - stride_kz, - stride_kh, - stride_kn, - stride_kk, - stride_vz, - stride_vh, - stride_vn, - stride_vk, - stride_deltaz, - stride_deltah, - stride_deltam, - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn, - N_CTX_Q, - N_CTX_K, - start_n, - num_block_m, - num_block_n, - dropout_p, - philox_seed, - batch_philox_offset, - descale_q, - descale_k, - descale_v, - descale_do, - BLOCK_M=BLOCK_M, - BLOCK_DMODEL=BLOCK_DMODEL, - ACTUAL_BLOCK_DMODEL=ACTUAL_BLOCK_DMODEL, - BLOCK_N=BLOCK_N, - SEQUENCE_PARALLEL=SEQUENCE_PARALLEL, - CAUSAL=CAUSAL, - DROPOUT=DROPOUT, - USE_EXP2=USE_EXP2, - GROUP_SIZE=GROUP_SIZE, - IS_FP8=IS_FP8, - FP8_MAX=FP8_MAX - ) - - -# NOTE: smaller blocks have lower accuracy. more accumlation error probably 128 * 128 seems good but leads to oom. 64 * 64 has accumlation errors but no oom. -def attention_prefill_backward_triton_impl( - do: torch.Tensor, - q: torch.Tensor, - k: torch.Tensor, - v: torch.Tensor, - o: torch.Tensor, - softmax_lse: torch.Tensor, - dq: torch.Tensor, - dk: torch.Tensor, - dv: torch.Tensor, - sm_scale: float, - alibi_slopes: Optional[torch.Tensor], - causal: bool, - layout: Literal["bshd", "bhsd", "thd"], - cu_seqlens_q: Optional[torch.Tensor], - cu_seqlens_k: Optional[torch.Tensor], - max_seqlen_q: int, - max_seqlen_k: int, - dropout_p: float, - philox_seed: Optional[int], - philox_offset: Optional[int], - use_exp2: bool, - sequence_parallel: bool = True, - # fp8 - descale_q: Optional[torch.Tensor] = None, - descale_k: Optional[torch.Tensor] = None, - descale_v: Optional[torch.Tensor] = None, - descale_do: Optional[torch.Tensor] = None, -): - if DEBUG: - print() - print("attention_prefill_backward_triton_impl") - print("do:", do, do.shape) - print("q:", q, q.shape) - print("k:", k, k.shape) - print("v:", v, v.shape) - print("o:", o, o.shape) - print("softmax_lse:", softmax_lse, softmax_lse.shape) - print("dq:", dq, dq.shape if dq is not None else None) - print("dk:", dk, dk.shape if dk is not None else None) - print("dv:", dv, dv.shape if dv is not None else None) - print("sm_scale:", sm_scale) - print("alibi_slopes:", alibi_slopes) - print("causal:", causal) - print("layout:", layout) - print("cu_seqlens_q:", cu_seqlens_q) - print("cu_seqlens_k:", cu_seqlens_k) - print("max_seqlen_q:", max_seqlen_q) - print("max_seqlen_k:", max_seqlen_k) - print("dropout_p:", dropout_p) - print("philox_seed:", philox_seed) - print("philox_offset:", philox_offset) - print("use_exp2:", use_exp2) - print("sequence_parallel:", sequence_parallel) - print("descale_q:", descale_q) - print("descale_k:", descale_k) - print("descale_v:", descale_v) - print("descale_do:", descale_do) - - IS_FP8 = is_fp8(q) - if IS_FP8: - FP8_MAX=torch.finfo(q.dtype).max - else: - FP8_MAX=None - - # make contigious - q = q.contiguous() - k = k.contiguous() - v = v.contiguous() - softmax_lse = softmax_lse.contiguous() - - # get strides and shape - batch, nheads_q, nheads_k, head_size, max_seqlen_q, max_seqlen_k = get_shapes_from_layout(q, k, layout, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k) - q_strides, k_strides, v_strides, o_strides = get_strides_from_layout(q, k, v, o, layout) - stride_qz, stride_qh, stride_qm, stride_qk = q_strides - stride_kz, stride_kh, stride_kn, stride_kk = k_strides - stride_vz, stride_vh, stride_vn, stride_vk = v_strides - stride_oz, stride_oh, stride_om, stride_ok = o_strides - is_varlen = layout == "thd" - group_size = nheads_q // nheads_k - use_dropout = (dropout_p > 0.0) - - # FIXME: some configs lead to oom for some reason when using 64 x 64 blocks - if max_seqlen_q <= 32 or max_seqlen_k <= 32: - BLOCK_M = 32 - BLOCK_N = 32 - else: - BLOCK_M = 64 - BLOCK_N = 64 - if DEBUG: - print("BLOCK_M:", BLOCK_M) - print("BLOCK_N:", BLOCK_N) - - num_warps = 4 # NOTE: originial is 8. changing it to 1 caused issues be careful - num_stages = 1 - waves_per_eu = 1 - - # divide up the problem - num_blocks_m = triton.cdiv(max_seqlen_q, BLOCK_M) - num_blocks_n = triton.cdiv(max_seqlen_k, BLOCK_N) - - # get closest power of 2 over or equal to 32. - padded_d_model = 1 << (head_size - 1).bit_length() - padded_d_model = max(padded_d_model, 16) - BLOCK_DMODEL = padded_d_model - ACTUAL_BLOCK_DMODEL = head_size - - do = do.contiguous() - - # deal with dq - if sequence_parallel: - dq = dq.unsqueeze(0).repeat(num_blocks_n, *([1] * len(q.shape))) # we do repeat instead of expand because we need to write data so views are not enough - stride_dq_all = dq.stride()[0] - - # assert contigious - assert do.is_contiguous() - assert q.is_contiguous() - assert k.is_contiguous() - assert v.is_contiguous() - assert o.is_contiguous() - assert softmax_lse.is_contiguous() - - # init delta - delta = torch.zeros_like(softmax_lse) - if is_varlen: - stride_deltam, stride_deltah = delta.stride() - stride_deltaz = 0 - else: - stride_deltaz, stride_deltah, stride_deltam = delta.stride() - - # dropout mask tensor for debugging. We dump the dropout mask created in the kernel for testing - if use_dropout: - if DROPOUT_USE_PYTORCH: - dropout_mask = create_dropout_mask(dropout_p, (batch, nheads_q, max_seqlen_q, max_seqlen_k), seed = philox_seed) - else: - dropout_mask = torch.zeros((batch, nheads_q, max_seqlen_q, max_seqlen_k), device=q.device, - dtype=torch.float32) - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn = (dropout_mask.stride(0), dropout_mask.stride(1), dropout_mask.stride(2), dropout_mask.stride(3)) - else: - dropout_mask = None - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn = (0, 0 , 0 , 0) - - - _bwd_preprocess[(batch * nheads_q, num_blocks_m)]( - o, - do, - delta, - stride_oz, stride_oh, stride_om, stride_ok, - stride_oz, stride_oh, stride_om, stride_ok, # FIXME: don't share strides with derivatives this was causing a lot of issues - stride_deltaz, stride_deltah, stride_deltam, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - descale_do, - BLOCK_M=BLOCK_M, - BLOCK_DMODEL=BLOCK_DMODEL, - ACTUAL_BLOCK_DMODEL=ACTUAL_BLOCK_DMODEL, - N_CTX_Q=max_seqlen_q, - Z=batch, - H=nheads_q, - IS_VARLEN=is_varlen, - IS_FP8=IS_FP8 - ) - - if DEBUG: - print("delta:", delta, delta.shape) - print("group_size:", group_size) - - _bwd_kernel[(batch * nheads_q, num_blocks_n if sequence_parallel else 1)]( - q, - k, - v, - sm_scale, - o, - do, - dq, - dk, - dv, - softmax_lse, - delta, - dropout_mask, - descale_q, - descale_k, - descale_v, - descale_do, - stride_dq_all, - stride_qz, stride_qh, stride_qm, stride_qk, # FIXME: don't share strides with derivatives this was causing a lot of issues - stride_kz, stride_kh, stride_kn, stride_kk, - stride_vz, stride_vh, stride_vn, stride_vk, - stride_deltaz, stride_deltah, stride_deltam, - stride_dropoutz, stride_dropouth, stride_dropoutm, stride_dropoutn, - batch, - nheads_q, - nheads_k, - num_blocks_m, - num_blocks_n, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p, philox_seed, philox_offset, - BLOCK_M=BLOCK_M, - BLOCK_N=BLOCK_N, - BLOCK_DMODEL=BLOCK_DMODEL, - ACTUAL_BLOCK_DMODEL=ACTUAL_BLOCK_DMODEL, - SEQUENCE_PARALLEL=sequence_parallel, - CAUSAL=causal, - DROPOUT=use_dropout, - USE_EXP2=use_exp2, - num_warps=num_warps, - num_stages=num_stages, - waves_per_eu = waves_per_eu, - IS_VARLEN=is_varlen, - GROUP_SIZE=group_size, - IS_FP8=IS_FP8, - FP8_MAX=FP8_MAX - ) - - if sequence_parallel: - dq = dq.sum(dim=0) - - if DEBUG: - print("attention_prefill_backward_triton_impl outputs") - print("dv:", dv, dv.shape) - print("dk:", dk, dk.shape) - print("dq:", dq, dq.shape) - if use_dropout: - print("dropout_mask:", dropout_mask, dropout_mask.shape if dropout_mask is not None else None) - print("dropout_fraction bwd:", 1.0 - (dropout_mask.sum()/ dropout_mask.numel()).item()) - write_dropout_mask(dropout_mask, "dropout_mask_bwd") - - return delta diff --git a/flash_attn/flash_attn_triton_amd/bwd_prefill_fused.py b/flash_attn/flash_attn_triton_amd/bwd_prefill_fused_atomics.py similarity index 55% rename from flash_attn/flash_attn_triton_amd/bwd_prefill_fused.py rename to flash_attn/flash_attn_triton_amd/bwd_prefill_fused_atomics.py index af3f8790026..e969a3770b8 100644 --- a/flash_attn/flash_attn_triton_amd/bwd_prefill_fused.py +++ b/flash_attn/flash_attn_triton_amd/bwd_prefill_fused_atomics.py @@ -1,793 +1,10 @@ import torch import triton import triton.language as tl +from flash_attn.flash_attn_triton_amd.utils import compute_fp8_scaling_factors from typing import Optional, Tuple -@triton.jit -def cdiv_fn(x, y): - return (x + y - 1) // y - -@triton.jit -def compute_fp8_scaling_factors(x, fp8_max: tl.constexpr): - # compute fp8 scaling and descaling factor for a block - x_amax = tl.max(tl.abs(x)) # NOTE: abs deals with negative values - x_amax = tl.where(x_amax <= 1e-9, 1e-9, x_amax) - scale_x = fp8_max / x_amax - descale_x = x_amax / fp8_max - return scale_x, descale_x - -def is_fp8(x): - if x.dtype in {torch.float8_e4m3fnuz, torch.float8_e4m3fn, torch.float8_e5m2, torch.float8_e5m2fnuz}: - if arch_supports_fp8(): - return True - else: - raise RuntimeError("This device does not support fp8") - else: - return False - - -def cast_to_fp8( - x: torch.Tensor, - fp8_dtype, - layout, - clamp_val=1e-9, -): - if len(x.shape) != 4: - raise ValueError(f"'bshd' tensor should have shape [batch, seqlen, heads, dim], got {x.shape}") - reduce_dims = (1, 3) # seq_len and dim dimensions - - # Compute the absolute max along reduce_dims, clamped to avoid 0-scale - x_abs_max = x.abs().amax(dim=reduce_dims) - x_abs_max = torch.maximum(x_abs_max, x.new_tensor(clamp_val)) - - # Unsqueeze back to a shape suitable for broadcast - unsqueeze_dims = sorted(reduce_dims) - for d in unsqueeze_dims: - x_abs_max = x_abs_max.unsqueeze(d) - - # compute scale and descale - fp8_max = torch.finfo(fp8_dtype).max - scale = fp8_max / x_abs_max - descale_factor = x_abs_max / fp8_max - - # cast to FP8, optionally setting requires_grad - x_fp8 = (x * scale).to(fp8_dtype) - - return x_fp8, descale_factor - - -def cast_varlen_to_fp8( - x: torch.Tensor, - fp8_dtype: torch.dtype, - cu_seqlens, - clamp_val: float = 1e-9, -) -> tuple[torch.Tensor, torch.Tensor]: - # validate tensor shape - if len(x.shape) != 3: - raise ValueError(f"tensor should have shape [total_seqlen, heads, dim], got {x.shape}") - num_heads = x.shape[1] - - # Get batch size from cu_seqlens - batch = cu_seqlens.shape[0] - 1 - fp8_max = torch.finfo(fp8_dtype).max - - # Compute scale and descale factors per sequence - x_fp8 = torch.zeros_like(x, dtype=fp8_dtype) - descale_factors = torch.zeros((batch, num_heads), device=x.device, dtype=torch.float32) - - for i in range(batch): - start = cu_seqlens[i] - end = cu_seqlens[i + 1] - x_slice = x[start:end] # Slice for current sequence - - # Standard tensor (0: seq_len, 2: head_dim) - x_abs_max = x_slice.abs().amax(dim=(0, 2)) # [heads] - - # apply minimum clamping - x_abs_max = torch.maximum(x_abs_max, x.new_tensor(clamp_val)) - - # compute scale and descale factors - scale_i = fp8_max / x_abs_max - descale_i = x_abs_max / fp8_max - - # store descale factors - descale_factors[i, :] = descale_i - - scale_reshape = scale_i.reshape(1, num_heads, 1) - - # scale and cast to FP8 - x_fp8[start:end] = (x_slice * scale_reshape).to(fp8_dtype) - - return x_fp8, descale_factors - - -#TODO Move this to a common folder. Will need to add future arch list -def get_arch(): - return triton.runtime.driver.active.get_current_target().arch - -def is_hip(): - return triton.runtime.driver.active.get_current_target().backend == "hip" - -def arch_supports_fp8(): - return is_hip() and get_arch() in ('gfx942') - -@triton.jit -def load_fn(ptrs, offset_first, offset_second, boundary_first, boundary_second): - if offset_first is not None and offset_second is not None: - mask = (offset_first[:, None] < boundary_first) & \ - (offset_second[None, :] < boundary_second) - tensor = tl.load(ptrs, mask=mask, other=0.0) - elif offset_first is not None: - mask = offset_first[:, None] < boundary_first - tensor = tl.load(ptrs, mask=mask, other=0.0) - elif offset_second is not None: - mask = offset_second[None, :] < boundary_second - tensor = tl.load(ptrs, mask=mask, other=0.0) - else: - tensor = tl.load(ptrs) - return tensor - -@triton.jit -def compute_alibi_block(alibi_slope, seqlen_q, seqlen_k, offs_m, offs_n, transpose=False): - # when seqlen_k and seqlen_q are different we want the diagonal to stick to the bottom right of the attention matrix - # for casual mask we want something like this where (1 is kept and 0 is masked) - # seqlen_q = 2 and seqlen_k = 5 - # 1 1 1 1 0 - # 1 1 1 1 1 - # seqlen_q = 5 and seqlen_k = 2 - # 0 0 - # 0 0 - # 0 0 - # 1 0 - # 1 1 - # for alibi the diagonal is 0 indicating no penalty for attending to that spot and increasing penalty for attending further from the diagonal - # e.g. alibi_slope = 1, seqlen_q = 2, seqlen_k = 5, offs_m = [0, 1, 2, 3], offs_n = [0, 1, 2, 3, 4], transpose = False - # 1. offs_m[:,None] = [[0], - # [1], - # 2. offs_m[:,None] + seqlen_k = [[5], - # [6], - # 3. offs_m[:,None] + seqlen_k - seqlen_q = [[3], - # [4], - # 4. offs_m[:,None] + seqlen_k - seqlen_q - offs_n[None,:] = [[3], - [[0, 1, 2, 3, 4]] = [[ 3, 2, 1, 0,-1], - # [4], [ 4, 3, 2, 1, 0]] - # 5. -1 * alibi_slope * tl.abs(relative_pos_block) = [[ -3, -2, -1, 0,-1], - # [ -4, -3, -2, -1, 0]], - relative_pos_block = offs_m[:, None] + seqlen_k - seqlen_q - offs_n[None, :] - alibi_block = -1 * alibi_slope * tl.abs(relative_pos_block) - if transpose: - return alibi_block.T - else: - return alibi_block - -@triton.jit -def _attn_fwd_inner( - acc, - l_i, - m_i, - q, - k_ptrs, - v_ptrs, - stride_kn, - stride_vk, - stride_sn, - start_m, - seqlen_k, - seqlen_q, - dropout_p, - sd_mask_ptrs, - dropout_mask_ptrs, - philox_seed, - philox_ptrs, - block_min, - block_max, - offs_n_causal, - masked_blocks, - n_extra_tokens, - alibi_slope, - descale_q, - descale_k, - descale_v, - OFFS_M: tl.constexpr, - OFFS_N: tl.constexpr, - BLOCK_M: tl.constexpr, - BLOCK_N: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - BLOCK_DMODEL_POW2: tl.constexpr, - SM_SCALE: tl.constexpr, - IS_CAUSAL: tl.constexpr, - MASK_STEPS: tl.constexpr, - ENABLE_DROPOUT: tl.constexpr, - RETURN_SCORES: tl.constexpr, - PADDED_HEAD: tl.constexpr, - IS_FP8: tl.constexpr, - FP8_MAX: tl.constexpr, -): - RCP_LN2: tl.constexpr = 1.4426950408889634 - - # loop over k, v, and update accumulator - - for start_n in range(block_min, block_max, BLOCK_N): - # For padded blocks, we will overrun the tensor size if - # we load all BLOCK_N. For others, the blocks are all within range. - if MASK_STEPS: - k_offs_n = start_n + tl.arange(0, BLOCK_N) - else: - k_offs_n = None - k_offs_k = None if not PADDED_HEAD else tl.arange(0, BLOCK_DMODEL_POW2) - k = load_fn(k_ptrs, k_offs_k, k_offs_n, BLOCK_DMODEL, seqlen_k) - - qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - # We start from end of seqlen_k so only the first iteration would need - # to be checked for padding if it is not a multiple of block_n - # TODO: This can be optimized to only be true for the padded block. - if MASK_STEPS: - # If this is the last block / iteration, we want to - # mask if the sequence length is not a multiple of block size - # a solution is to always do BLOCK_M // BLOCK_N + 1 steps if not is_modulo_mn. - # last step might get wasted but that is okay. check if this masking works For - # that case. - if (start_n + BLOCK_N == block_max) and (n_extra_tokens != 0): - boundary_m = tl.full([BLOCK_M], seqlen_k, dtype=tl.int32) - size_n = start_n + OFFS_N[None, :] - mask = size_n < boundary_m[:, None] - qk = tl.where(mask, qk, float("-inf")) - - # compute masks - q_mask = (OFFS_M[:, None] < seqlen_q) - k_mask = ((start_n + tl.arange(0, BLOCK_N))[None, :] < seqlen_k) - p_mask = q_mask & k_mask - - # -- compute qk ---- - if IS_FP8: - qk += (tl.dot(q, k) * descale_q * descale_k) - else: - qk += tl.dot(q, k) - qk_scaled = qk * SM_SCALE - if IS_CAUSAL: - causal_boundary = start_n + offs_n_causal - causal_mask = OFFS_M[:, None] >= causal_boundary[None, :] - qk_scaled = tl.where(causal_mask, qk_scaled, float("-inf")) - - if alibi_slope is not None: - # Compute the global position of each token within the sequence - global_m_positions = start_m * BLOCK_M + tl.arange(0, BLOCK_M) - global_n_positions = start_n + tl.arange(0, BLOCK_N) - alibi_block = compute_alibi_block(alibi_slope, seqlen_q, seqlen_k, global_m_positions, - global_n_positions) - qk_scaled += alibi_block - # get max scores so far - m_ij = tl.maximum(m_i, tl.max(qk_scaled, 1)) - - # scale and subtract max - q_shifted = qk_scaled - m_ij[:, None] - - # Compute scaled QK and softmax probabilities - p = tl.math.exp2(q_shifted * RCP_LN2) - - # CAVEAT: Must update l_ij before applying dropout - l_ij = tl.sum(p, 1) - if ENABLE_DROPOUT: - rng_output = tl.rand(philox_seed, philox_ptrs) # TODO: use tl.randint for better performance - dropout_mask = rng_output > dropout_p - tl.store(dropout_mask_ptrs, dropout_mask, mask=p_mask) - - # return scores with negative values for dropped vals - sd_mask = tl.where(dropout_mask, p, -p) - tl.store(sd_mask_ptrs, sd_mask, mask=p_mask) - - # apply dropout mask in place - p = tl.where(dropout_mask, p, 0.0) - elif RETURN_SCORES: - # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that - tl.store(sd_mask_ptrs, p, mask=p_mask) - - # -- update output accumulator -- - # alpha is an adjustment factor for acc and li as we loop and find new maxes - # store the diff in maxes to adjust acc and li as we discover new maxes - m_diff = m_i - m_ij - alpha = tl.math.exp2(m_diff * RCP_LN2) - acc = acc * alpha[:, None] - v = load_fn(v_ptrs, k_offs_n, k_offs_k, seqlen_k, BLOCK_DMODEL) - # -- update m_i and l_i - l_i = l_i * alpha + l_ij - # update m_i and l_i - m_i = m_ij - - if IS_FP8: - scale_p, descale_p = compute_fp8_scaling_factors(p, FP8_MAX) - acc += (tl.dot((p * scale_p).to(v.type.element_ty), v) * descale_p * descale_v) - else: - acc += tl.dot(p.to(v.type.element_ty), v) - - k_ptrs += BLOCK_N * stride_kn - v_ptrs += BLOCK_N * stride_vk - if RETURN_SCORES: - sd_mask_ptrs += BLOCK_N * stride_sn - - if ENABLE_DROPOUT: - dropout_mask_ptrs += BLOCK_N * stride_sn - philox_ptrs += BLOCK_N * stride_sn - - return acc, l_i, m_i - - -@triton.jit -def _attn_fwd(q_ptr: torch.Tensor, - k_ptr: torch.Tensor, - v_ptr: torch.Tensor, - descale_q_ptr: torch.Tensor, - descale_k_ptr: torch.Tensor, - descale_v_ptr: torch.Tensor, - out_ptr: torch.Tensor, - alibi_slopes_ptr: torch.Tensor, - s_dmask_ptr: torch.Tensor, - dropout_mask_ptr: torch.Tensor, - softmax_lse_ptr: torch.Tensor, - stride_qz, stride_qh, stride_qm, stride_qk, - stride_kz, stride_kh, stride_kn, stride_kk, - stride_vz, stride_vh, stride_vn, stride_vk, - stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, - stride_oz, stride_oh, stride_om, stride_on, - stride_alibi_z, stride_alibi_h, - stride_sd_z, stride_sd_h, stride_sd_m, stride_sd_n, - stride_lse_z, stride_lse_h, stride_lse_m, - sm_scale, - cu_seqlens_q, - cu_seqlens_k, - dropout_p, - philox_seed, - philox_offset, - SEQLEN_Q: tl.constexpr, - SEQLEN_K: tl.constexpr, - IS_CAUSAL: tl.constexpr, - NUM_Q_HEADS: tl.constexpr, - NUM_K_HEADS: tl.constexpr, - BLOCK_M: tl.constexpr, - BLOCK_N: tl.constexpr, - BLOCK_DMODEL: tl.constexpr, - BLOCK_DMODEL_POW2: tl.constexpr, - RETURN_SCORES: tl.constexpr, - ENABLE_DROPOUT: tl.constexpr, - IS_FP8: tl.constexpr, - FP8_MAX: tl.constexpr, - VARLEN: tl.constexpr, -): - #calculate offsets - off_z = tl.program_id(0) #batch - off_q_head = tl.program_id(1) #num_q_heads - start_m = tl.program_id(2) #seqlen_q - - 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_POW2) - - if VARLEN: - cu_seqlens_q_start = tl.load(cu_seqlens_q + off_z) - cu_seqlens_q_end = tl.load(cu_seqlens_q + off_z + 1) - - seqlen_q = cu_seqlens_q_end - cu_seqlens_q_start - # We have a one-size-fits-all grid in id(0). Some seqlens might be too - # small for all start_m so for those we return early. - if start_m * BLOCK_M > seqlen_q: - return - cu_seqlens_k_start = tl.load(cu_seqlens_k + off_z) - cu_seqlens_k_end = tl.load(cu_seqlens_k + off_z + 1) - seqlen_k = cu_seqlens_k_end - cu_seqlens_k_start - else: - cu_seqlens_q_start = 0 - cu_seqlens_k_start = 0 - seqlen_q = SEQLEN_Q - seqlen_k = SEQLEN_K - - n_blocks = cdiv_fn(seqlen_k, BLOCK_N) - - # Now we compute whether we need to exit early due to causal masking. - # This is because for seqlen_q > seqlen_k, M rows of the attn scores - # are completely masked, resulting in 0s written to the output, and - # inf written to LSE. We don't need to do any GEMMs in this case. - # This block of code determines what N is, and if this WG is operating - # on those M rows. - if (IS_CAUSAL): - # If seqlen_q == seqlen_k, the attn scores are a square matrix. - # If seqlen_q != seqlen_k, attn scores are rectangular which means - # the causal mask boundary is bottom right aligned, and ends at either - # the top edge (seqlen_q < seqlen_k) or left edge. - - # This captures the decrease in n_blocks if we have a rectangular attn matrix - n_blocks_seqlen = cdiv_fn((start_m + 1) * BLOCK_M + seqlen_k - seqlen_q, BLOCK_N) - - # This is what adjusts the block_max for the current WG, only - # if IS_CAUSAL. Otherwise we want to always iterate through all n_blocks - n_blocks = min(n_blocks, n_blocks_seqlen) - - # If we have no blocks after adjusting for seqlen deltas, this WG is part of - # the blocks that are all 0. We exit early. - if n_blocks <= 0: - offs_out = (off_z * stride_oz + - off_q_head * stride_oh + - cu_seqlens_q_start * stride_om + - offs_m[:, None] * stride_om + - offs_d[None, :] * stride_on) - acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_POW2], dtype=out_ptr.type.element_ty) - out_mask = (offs_m[:, None] < seqlen_q) & (offs_d < BLOCK_DMODEL) - tl.store(out_ptr + offs_out, acc, mask=out_mask) - - if softmax_lse_ptr is not None: - offs_lse = (off_z * stride_lse_z + - off_q_head * stride_lse_h + - cu_seqlens_q_start * stride_lse_m + - offs_m*stride_lse_m - ) - lse_mask = offs_m < SEQLEN_Q - lse = tl.full([BLOCK_M], value=0.0, dtype=tl.float32) - tl.store(softmax_lse_ptr + offs_lse, lse, mask=lse_mask) - # TODO: Should dropout and return encoded softmax be handled here too? - - return - - grp_sz:tl.constexpr = NUM_Q_HEADS // NUM_K_HEADS - if grp_sz != 1: #Grouped Query Attention - off_k_head = off_q_head // grp_sz - else: - off_k_head = off_q_head - - #q,k,v offsets - q_offs = (off_z * stride_qz + - off_q_head * stride_qh + - cu_seqlens_q_start * stride_qm + - offs_m[:, None] * stride_qm + offs_d[None, :]*stride_qk - ) - q_ptrs = q_ptr + q_offs - - k_offs = (off_z * stride_kz + - off_k_head * stride_kh + - cu_seqlens_k_start * stride_kn + - offs_d[:, None] * stride_kk + offs_n[None, :]*stride_kn - ) - k_ptrs = k_ptr + k_offs - - v_offs = (off_z * stride_vz + - off_k_head * stride_vh + - cu_seqlens_k_start * stride_vn + - offs_n[:, None] * stride_vn + offs_d[None, :]*stride_vk - ) - v_ptrs = v_ptr + v_offs - - #alibi slopes - if alibi_slopes_ptr is not None: - alibi_offs = off_z * stride_alibi_z + off_q_head * stride_alibi_h - alibi_slope = tl.load(alibi_slopes + alibi_offs) - else: - alibi_slope = None - - #s_dmask (return_scores) - if s_dmask_ptr is not None: - s_dmask_offs = (off_z * stride_sd_z + - off_q_head * stride_sd_h + - offs_m[:, None] * stride_sd_m + - offs_n[None, :] * stride_sd_n - ) - s_dmask_ptrs = s_dmask_ptr + s_dmask_offs - else: - s_dmask_ptrs = None - - #dropout - if dropout_mask_ptr is not None: - dropout_mask_offs = (off_z * stride_sd_z + - off_q_head * stride_sd_h + - offs_m[:, None] * stride_sd_m + - offs_n[None, :] * stride_sd_n - ) - dropout_mask_ptrs = dropout_mask_ptr + dropout_mask_offs - philox_ptrs = (philox_offset + - off_z * stride_sd_z + - off_q_head * stride_sd_h + - offs_m[:, None] * stride_sd_m + - offs_n[None, :] * stride_sd_n - ) - else: - dropout_mask_ptrs = None - philox_ptrs = None - - m_i = tl.full([BLOCK_M], float("-inf"), dtype=tl.float32) - l_i = tl.full([BLOCK_M], 1.0, dtype=tl.float32) - acc = tl.zeros([BLOCK_M, BLOCK_DMODEL_POW2], dtype=tl.float32) - if (BLOCK_DMODEL == BLOCK_DMODEL_POW2): - q_mask = (offs_m[:, None] < seqlen_q) - else: - q_mask = (offs_m[:, None] < seqlen_q) & (offs_d[None, :] < BLOCK_DMODEL) - q = tl.load(q_ptrs, mask=q_mask, other=0.0) - if IS_FP8: - descale_q = tl.load(descale_q_ptr + off_z * stride_descale_q_z + off_q_head) - descale_k = tl.load(descale_k_ptr + off_z * stride_descale_k_z + off_k_head) - descale_v = tl.load(descale_v_ptr + off_z * stride_descale_v_z + off_k_head) - else: - descale_q, descale_k ,descale_v = 1.0, 1.0, 1.0 - - n_extra_tokens = 0 - if seqlen_k < BLOCK_N: - n_extra_tokens = BLOCK_N -seqlen_k - elif seqlen_k % BLOCK_N: - n_extra_tokens = seqlen_k % BLOCK_N - - #if CAUSAL, then determine masked_blocks and full blocks - # Here we compute how many full and masked blocks we have. - padded_block_k = n_extra_tokens != 0 - is_modulo_mn = not padded_block_k and (seqlen_q % BLOCK_M == 0) - if IS_CAUSAL: - # There are always at least BLOCK_M // BLOCK_N masked blocks. - # Additionally there might be one more due to dissimilar seqlens. - masked_blocks = BLOCK_M // BLOCK_N + (not is_modulo_mn) - else: - # Padding on Q does not need to be masked in the FA loop. - masked_blocks = padded_block_k - # if IS_CAUSAL, not is_modulo_mn does not always result in an additional block. - # In this case we might exceed n_blocks so pick the min. - masked_blocks = min(masked_blocks, n_blocks) - n_full_blocks = n_blocks - masked_blocks - block_min = 0 - block_max = n_blocks * BLOCK_N - # Compute for full blocks. Here we set causal to false regardless of its actual - # value because there is no masking. Similarly we do not need padding. - if n_full_blocks > 0: - block_max = (n_blocks - masked_blocks) * BLOCK_N - acc, l_i, m_i = _attn_fwd_inner(acc, - l_i, - m_i, - q, - k_ptrs, - v_ptrs, - stride_kn, - stride_vn, - stride_sd_n, - start_m, - seqlen_k, - seqlen_q, - dropout_p, - s_dmask_ptrs, dropout_mask_ptrs, philox_seed, philox_ptrs, - block_min, block_max, 0, 0, 0, alibi_slope, - descale_q, descale_k, descale_v, - offs_m, offs_n, BLOCK_M, BLOCK_N, BLOCK_DMODEL,BLOCK_DMODEL_POW2, - sm_scale, False, MASK_STEPS=False, ENABLE_DROPOUT=ENABLE_DROPOUT, - RETURN_SCORES=RETURN_SCORES, PADDED_HEAD=BLOCK_DMODEL!=BLOCK_DMODEL_POW2, - IS_FP8=IS_FP8, FP8_MAX=FP8_MAX - ) - block_min = block_max - block_max = n_blocks * BLOCK_N - - # Remaining blocks, if any, are full / not masked. - if (masked_blocks > 0): - if IS_CAUSAL: - offs_n_causal = offs_n + (seqlen_q - seqlen_k) - else: - offs_n_causal = 0 - k_ptrs += n_full_blocks * BLOCK_N * stride_kn - v_ptrs += n_full_blocks * BLOCK_N * stride_vn - if RETURN_SCORES: - s_dmask_ptrs += n_full_blocks * BLOCK_N * stride_sd_n - if ENABLE_DROPOUT: - dropout_mask_ptrs += n_full_blocks * BLOCK_N * stride_sd_n - acc, l_i, m_i = _attn_fwd_inner(acc, - l_i, - m_i, - q, - k_ptrs, - v_ptrs, - stride_kn, stride_vn, stride_sd_n, - start_m, seqlen_k, seqlen_q, - dropout_p, - s_dmask_ptrs, dropout_mask_ptrs, philox_seed, philox_ptrs, - block_min, block_max, offs_n_causal, masked_blocks, n_extra_tokens, alibi_slope, - descale_q, descale_k, descale_v, - offs_m, offs_n, BLOCK_M, BLOCK_N, BLOCK_DMODEL,BLOCK_DMODEL_POW2, - sm_scale, IS_CAUSAL, MASK_STEPS=True, ENABLE_DROPOUT=ENABLE_DROPOUT, - RETURN_SCORES=RETURN_SCORES, PADDED_HEAD=BLOCK_DMODEL!=BLOCK_DMODEL_POW2, - IS_FP8=IS_FP8, FP8_MAX=FP8_MAX - ) - # epilogue - # This helps the compiler do Newton Raphson on l_i vs on acc which is much larger. - l_recip = 1 / l_i[:, None] - acc = acc * l_recip - if ENABLE_DROPOUT: - dropout_scale = 1 / (1 - dropout_p) - acc = acc * dropout_scale - # If seqlen_q > seqlen_k but the delta is not a multiple of BLOCK_M, - # then we have one block with a row of all NaNs which come from computing - # softmax over a row of all -infs (-inf - inf = NaN). We check for that here - # and store 0s where there are NaNs as these rows should've been zeroed out. - end_m_idx = (start_m + 1) * BLOCK_M - start_m_idx = start_m * BLOCK_M - causal_start_idx = seqlen_q - seqlen_k - if IS_CAUSAL: - if causal_start_idx > start_m_idx and causal_start_idx < end_m_idx: - out_mask_boundary = tl.full((BLOCK_DMODEL_POW2, ), causal_start_idx, dtype=tl.int32) - mask_m_offsets = start_m_idx + tl.arange(0, BLOCK_M) - out_ptrs_mask = mask_m_offsets[:, None] >= out_mask_boundary[None, :] - z = 0.0 - acc = tl.where(out_ptrs_mask, acc, z.to(acc.type.element_ty)) - - # write back LSE(Log Sum Exponents), the log of the normalization constant - overflow_size = end_m_idx - seqlen_q - if softmax_lse_ptr is not None: - RCP_LN2: tl.constexpr = 1.4426950408889634 - LN2: tl.constexpr = 0.6931471824645996 - # compute log-sum-exp in base 2 units - mi_base2 = m_i * RCP_LN2 - softmax_lse = mi_base2 + tl.math.log2(l_i) - # convert back to natural units - softmax_lse *= LN2 - - if IS_CAUSAL: - # zero out nans caused by -infs when doing causal - lse_causal_mask = (start_m_idx + tl.arange(0, BLOCK_M)) < causal_start_idx - softmax_lse = tl.where(lse_causal_mask, 0.0, softmax_lse) - - # If seqlen_q not multiple of BLOCK_M, we need to mask out the last few rows. - # This is only true for the last M block. For others, overflow_size will be -ve - offs_lse = off_z * stride_lse_z + off_q_head * stride_lse_h + cu_seqlens_q_start * stride_lse_m + offs_m*stride_lse_m - if overflow_size > 0: - boundary = tl.full((BLOCK_M, ), BLOCK_M - overflow_size, dtype=tl.int32) - lse_mask = tl.arange(0, BLOCK_M) < boundary - tl.store(softmax_lse_ptr + offs_lse, softmax_lse, mask=lse_mask) # the log of the normalization constant - else: - tl.store(softmax_lse_ptr + offs_lse, softmax_lse) # the log of the normalization constant - - # write back O - offs_out = (off_z * stride_oz + - off_q_head * stride_oh + - cu_seqlens_q_start * stride_om + - offs_m[:, None] * stride_om + - offs_d[None, :] * stride_on) - out_mask = tl.full([BLOCK_M, BLOCK_DMODEL_POW2], 1, dtype=tl.int1) - if overflow_size > 0: - out_mask = out_mask & (offs_m[:, None] < seqlen_q) - if BLOCK_DMODEL != BLOCK_DMODEL_POW2: - out_mask = out_mask & (offs_d[None, :] < BLOCK_DMODEL) - op = acc.to(out_ptr.dtype.element_ty) - tl.store(out_ptr + offs_out, op, mask=out_mask) - -def _flash_attn_forward( - q: torch.Tensor, - k: torch.Tensor, - v: torch.Tensor, - dropout_p: float, - softmax_scale: float, - causal: bool, - window_size_left: int, - window_size_right: int, - alibi_slopes: Optional[torch.Tensor], - return_lse: bool, - return_softmax: bool, - max_seqlen_q: int, - max_seqlen_k: int, - cu_seqlens_q: Optional[torch.Tensor] = None, - cu_seqlens_k: Optional[torch.Tensor] = None, - descale_q: Optional[torch.Tensor] = None, - descale_k: Optional[torch.Tensor] = None, - descale_v: Optional[torch.Tensor] = None, -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - - #FP8 - IS_FP8 = is_fp8(q) - FP8_MAX: tl.constexpr=torch.finfo(q.dtype).max - is_varlen = True if cu_seqlens_q is not None else False - - if IS_FP8: - o = torch.zeros_like(q, dtype=torch.float32) - else: - o = torch.zeros_like(q) - if is_varlen: - #Layout for q,k,v is thd ie [total_tokens, num_head, head_dim] - batch, seqlen_q, num_q_heads, head_sz = len(cu_seqlens_q) - 1, max_seqlen_q, q.shape[1], q.shape[2] - seqlen_k, num_k_heads = max_seqlen_k, k.shape[1] - q_strides = (0, q.stride(1), q.stride(0), q.stride(2)) - k_strides = (0, k.stride(1), k.stride(0), k.stride(2)) - v_strides = (0, v.stride(1), v.stride(0), v.stride(2)) - o_strides = (0, o.stride(1), o.stride(0), o.stride(2)) - else: - #Layout for q,k,v is bshd ie [batch, seq_len, num_head, head_dim] - batch, seqlen_q, num_q_heads, head_sz = q.shape - seqlen_k = k.shape[1] - num_k_heads = k.shape[2] - q_strides = (q.stride(0), q.stride(2), q.stride(1), q.stride(3)) - k_strides = (k.stride(0), k.stride(2), k.stride(1), k.stride(3)) - v_strides = (v.stride(0), v.stride(2), v.stride(1), v.stride(3)) - o_strides = (o.stride(0), o.stride(2), o.stride(1), o.stride(3)) - - #padding for head_dim. Power of 2 or 16 - BLOCK_DMODEL_POW2 = triton.next_power_of_2(head_sz) - BLOCK_DMODEL_POW2 = max(BLOCK_DMODEL_POW2, 16) - - #softmax_lse [batch, num_q_heads, seqlen_q] - if return_lse: - if is_varlen: - softmax_lse = torch.zeros((q.shape[0], num_q_heads), device=q.device, dtype=torch.float32) - stride_lse_z, stride_lse_h, stride_lse_m = 0, softmax_lse.stride(1), softmax_lse.stride(0) - else: - softmax_lse = torch.zeros((batch, num_q_heads, max_seqlen_q), device=q.device, dtype=torch.float32) - stride_lse_z, stride_lse_h, stride_lse_m = softmax_lse.stride() - else: - softmax_lse = None - - #exp_scores [batch, num_q_heads, seqlen_q, seqlen_k] - enable_dropout = dropout_p > 0.0 - if enable_dropout: - philox_seed = torch.randint(0, 0xffffff, (1,))[0].item() #No specific reason to restrict range to 0xffffff - philox_offset = torch.randint(0, 0xffffff, (1,))[0].item() #Pass in an int, not Tensor - else: - philox_seed = 0 - philox_offset = 0 - if return_softmax or enable_dropout: - s_dmask = torch.zeros((batch, num_q_heads, max_seqlen_q, max_seqlen_k), device=q.device, dtype=torch.float32) - dropout_mask = torch.zeros((batch, num_q_heads, max_seqlen_q, max_seqlen_k), device=q.device, dtype=torch.float32) - else: - s_dmask = None - dropout_mask = None - - - # Best config from ROCm/triton/python/perf-kernels/flash_attention.py::attn_fwd autotuning is BLOCK_M: 128, BLOCK_N: 64, waves_per_eu: 2, num_warps: 4, num_ctas: 1, num_stages: 1 - # Tuned for MI300x - config = { - 'BLOCK_M': 128, - 'BLOCK_N': 64, - 'waves_per_eu': 2, - 'num_warps': 4, - 'num_ctas': 1, - 'num_stages': 1, - } - - grid = lambda META:(batch, num_q_heads, triton.cdiv(seqlen_q, META['BLOCK_M'])) - _attn_fwd[grid](q, - k, - v, - descale_q, - descale_k, - descale_v, - o, - alibi_slopes, - s_dmask, - dropout_mask, - softmax_lse, - *q_strides, - *k_strides, - *v_strides, - descale_q.stride(0) if descale_q is not None else 0, - descale_k.stride(0) if descale_k is not None else 0, - descale_v.stride(0) if descale_v is not None else 0, - *o_strides, - alibi_slopes.stride(0) if alibi_slopes is not None else 0, - alibi_slopes.stride(1) if alibi_slopes is not None else 0, - s_dmask.stride(0) if s_dmask is not None else 0, - s_dmask.stride(1) if s_dmask is not None else 0, - s_dmask.stride(2) if s_dmask is not None else 0, - s_dmask.stride(3) if s_dmask is not None else 0, - stride_lse_z if softmax_lse is not None else 0, - stride_lse_h if softmax_lse is not None else 0, - stride_lse_m if softmax_lse is not None else 0, - softmax_scale, - cu_seqlens_q, - cu_seqlens_k, - dropout_p, - philox_seed, - philox_offset, - SEQLEN_Q=max_seqlen_q, - SEQLEN_K=max_seqlen_k, - IS_CAUSAL=causal, - NUM_Q_HEADS=num_q_heads, - NUM_K_HEADS=num_k_heads, - BLOCK_DMODEL=head_sz, - BLOCK_DMODEL_POW2=BLOCK_DMODEL_POW2, - RETURN_SCORES=return_softmax, - ENABLE_DROPOUT=enable_dropout, - IS_FP8=IS_FP8, - FP8_MAX=FP8_MAX, - VARLEN=is_varlen, - **config - ) - - return o, softmax_lse, s_dmask, philox_seed, philox_offset - # This function computes delta given output Out and gradient DO # Here is the I/O shape: # Out: (batch, nhead_q, max_seqlens_q, headDim) @@ -2261,7 +1478,7 @@ def _bwd_kernel_dq_noncausal( dq *= sm_scale tl.store(DQ + adj_dq + offs_dq, dq, mask=mask_q) -def _flash_attn_backward( +def attention_prefill_backward_triton_fused_atomics_impl( do: torch.Tensor, q: torch.Tensor, k: torch.Tensor, @@ -2589,702 +1806,4 @@ def _flash_attn_backward( waves_per_eu=WAVES_PER_EU, ) - return delta - - -class FlashAttnFunc(torch.autograd.Function): - @staticmethod - def forward( - ctx, - q, - k, - v, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_softmax, - is_grad_enabled, - fused_backward, - ): - is_grad = is_grad_enabled and any( - x.requires_grad for x in [q,k,v] - ) - if softmax_scale is None: - softmax_scale = q.shape[-1] ** (-0.5) - - - head_size_og = q.size(3) - if head_size_og % 8 != 0: - q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8]) - k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8]) - v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8]) - out_padded, softmax_lse, S_dmask, philox_seed, philox_offset = _flash_attn_forward( - q, - k, - v, - dropout_p, - softmax_scale, - causal=causal, - window_size_left=window_size[0], - window_size_right=window_size[1], - alibi_slopes=alibi_slopes, - return_lse=return_lse, - return_softmax=return_softmax and dropout_p > 0, - max_seqlen_q=q.shape[1], - max_seqlen_k=k.shape[1], - ) - - if is_grad: - ctx.save_for_backward(q, k, v, out_padded, softmax_lse) - ctx.philox_seed = philox_seed - ctx.philox_offset = philox_offset - ctx.dropout_p = dropout_p - ctx.softmax_scale = softmax_scale - ctx.causal = causal - ctx.window_size = window_size - ctx.alibi_slopes = alibi_slopes - ctx.deterministic = deterministic - ctx.fused_backward = fused_backward - - - out = out_padded[..., :head_size_og] - result = [out] - if return_lse: - result.append(softmax_lse) - if return_softmax: - result.append(S_dmask) - - return tuple(result) - - @staticmethod - def backward(ctx, do, *args): - q, k, v, out, softmax_lse = ctx.saved_tensors - dq, dk, dv = torch.zeros_like(q), torch.empty_like(k), torch.empty_like(v) - head_size_v_og = do.size(3) - do_padded = do - if head_size_v_og % 8 != 0: - do_padded = torch.nn.functional.pad(do, [0, 8 - head_size_v_og % 8]) - _flash_attn_backward( - do_padded, - q, - k, - v, - out, - softmax_lse, - dq, - dk, - dv, - ctx.softmax_scale, - ctx.alibi_slopes, - ctx.causal, - None, - None, - max_seqlen_q=q.shape[1], - max_seqlen_k=k.shape[1], - dropout_p=ctx.dropout_p, - philox_seed=ctx.philox_seed, - philox_offset=ctx.philox_offset, - fused=ctx.fused_backward, - ) - dq = dq[..., : q.shape[-1]] # We could have padded the head dimension - dk = dk[..., : k.shape[-1]] - dv = dv[..., : v.shape[-1]] - return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None - -def flash_attn_func( - q, - k, - v, - dropout_p=0.0, - softmax_scale=None, - causal=False, - window_size=(-1,-1), - alibi_slopes=None, - deterministic=True, - return_lse=False, - return_attn_probs=False, - fused_backward=False, -): - """dropout_p should be set to 0.0 during evaluation - Supports multi-query and grouped-query attention (MQA/GQA) by passing in KV with fewer heads - than Q. Note that the number of heads in Q must be divisible by the number of heads in KV. - For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head - 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V. - - If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix. - For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is: - 1 1 1 1 0 - 1 1 1 1 1 - If seqlen_q = 5 and seqlen_k = 2, the causal mask is: - 0 0 - 0 0 - 0 0 - 1 0 - 1 1 - If the row of the mask is all zero, the output will be zero. - - If window_size != (-1, -1), implements sliding window local attention. Query at position i - will only attend to keys between - [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive. - - Arguments: - q: (batch_size, seqlen, nheads, headdim) - k: (batch_size, seqlen, nheads_k, headdim) - v: (batch_size, seqlen, nheads_k, headdim) - 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). - window_size: (left, right). If not (-1, -1), implements sliding window local attention. - alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of - (-alibi_slope * |i + seqlen_k - seqlen_q - j|) - is added to the attention score of query i and key j. - deterministic: bool. Whether to use the deterministic implementation of the backward pass, - which is slightly slower and uses more memory. The forward pass is always deterministic. - 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). - Return: - out: (batch_size, seqlen, nheads, headdim). - softmax_lse [optional, if return_lse=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, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_attn_probs, - torch.is_grad_enabled(), - fused_backward, - ) - - -class FlashAttnFP8Func(torch.autograd.Function): - @staticmethod - def forward( - ctx, - q, - k, - v, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_softmax, - is_grad_enabled, - fused_backward, - ): - is_grad = is_grad_enabled and any( - x.requires_grad for x in [q,k,v] - ) - if softmax_scale is None: - softmax_scale = q.shape[-1] ** (-0.5) - head_size_og = q.size(3) - if head_size_og % 8 != 0: - q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8]) - k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8]) - v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8]) - - # cast input to fp8 - fp8_dtype = torch.float8_e4m3fnuz - q_fp8, descale_q = cast_to_fp8(q, fp8_dtype, "bshd") - k_fp8, descale_k = cast_to_fp8(k, fp8_dtype, "bshd") - v_fp8, descale_v = cast_to_fp8(v, fp8_dtype, "bshd") - - out_padded, softmax_lse, S_dmask, philox_seed, philox_offset = _flash_attn_forward( - q, - k, - v, - dropout_p, - softmax_scale, - causal=causal, - window_size_left=window_size[0], - window_size_right=window_size[1], - alibi_slopes=alibi_slopes, - return_lse=return_lse, - return_softmax=return_softmax and dropout_p > 0, - max_seqlen_q=q.shape[1], - max_seqlen_k=k.shape[1], - cu_seqlens_q=None, - cu_seqlens_k=None, - descale_q=descale_q, - descale_k=descale_k, - descale_v=descale_v, - ) - - if is_grad: - ctx.save_for_backward(q_fp8, k_fp8, v_fp8, out_padded, softmax_lse, descale_q, descale_k, descale_v) - ctx.philox_seed = philox_seed - ctx.philox_offset = philox_offset - ctx.dropout_p = dropout_p - ctx.softmax_scale = softmax_scale - ctx.causal = causal - ctx.window_size = window_size - ctx.alibi_slopes = alibi_slopes - ctx.fused_backward = fused_backward - - out = out_padded[..., :head_size_og] - result = [out] - if return_lse: - result.append(softmax_lse) - if return_softmax: - result.append(S_dmask) - - return tuple(result) - - @staticmethod - def backward(ctx, do, *args): - q_fp8, k_fp8, v_fp8, out, softmax_lse, descale_q, descale_k, descale_v = ctx.saved_tensors - dq, dk, dv = torch.zeros_like(q_fp8, dtype=torch.float32), torch.zeros_like(k_fp8, dtype=torch.float32), torch.zeros_like(v_fp8, dtype=torch.float32) - head_size_v_og = do.size(3) - do_padded = do - if head_size_v_og % 8 != 0: - do_padded = torch.nn.functional.pad(do, [0, 8 - head_size_v_og % 8]) - - fp8_dtype = torch.float8_e4m3fnuz - do_padded_fp8, descale_do = cast_to_fp8(do_padded, fp8_dtype, "bshd") - _flash_attn_backward( - do_padded_fp8, - q_fp8, - k_fp8, - v_fp8, - out, - softmax_lse, - dq, - dk, - dv, - ctx.softmax_scale, - ctx.alibi_slopes, - ctx.causal, - None, - None, - max_seqlen_q=q_fp8.shape[1], - max_seqlen_k=k_fp8.shape[1], - dropout_p=ctx.dropout_p, - philox_seed=ctx.philox_seed, - philox_offset=ctx.philox_offset, - descale_q=descale_q, - descale_k=descale_k, - descale_v=descale_v, - descale_do=descale_do, - fused=ctx.fused_backward, - ) - #dq = dq[..., : q_fp8.shape[-1]] # We could have padded the head dimension - #dk = dk[..., : k_fp8.shape[-1]] - #dv = dv[..., : v_fp8.shape[-1]] - return dq, dk, dv, None, None, None, None, None, None, None, None, None, None - -def flash_attn_fp8_func( - q, - k, - v, - dropout_p=0.0, - softmax_scale=None, - causal=False, - window_size=(-1, -1), # -1 means infinite context window - alibi_slopes=None, - deterministic=False, - return_lse=False, - return_attn_probs=False, - fused_backward=False, -): - return FlashAttnFP8Func.apply( - q, - k, - v, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_attn_probs, - torch.is_grad_enabled(), - fused_backward, - ) - -class FlashAttnVarlenFunc(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, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_softmax, - block_table, - is_grad_enabled, - fused_backward, - ): - is_grad = is_grad_enabled and any( - x.requires_grad for x in [q, k, v] - ) - if softmax_scale is None: - softmax_scale = q.shape[-1] ** (-0.5) - head_size_og = q.size(2) - if head_size_og % 8 != 0: - q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8]) - k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8]) - v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8]) - out_padded, softmax_lse, S_dmask, philox_seed, philox_offset = _flash_attn_forward( - q, - k, - v, - dropout_p, - softmax_scale, - causal=causal, - window_size_left=window_size[0], - window_size_right=window_size[1], - alibi_slopes=alibi_slopes, - return_lse=return_lse, - return_softmax=return_softmax and dropout_p > 0.0, - max_seqlen_q=max_seqlen_q, - max_seqlen_k=max_seqlen_k, - cu_seqlens_q=cu_seqlens_q, - cu_seqlens_k=cu_seqlens_k, - ) - if is_grad: - ctx.save_for_backward(q, k, v, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k) - ctx.max_seqlen_q = max_seqlen_q - ctx.max_seqlen_k = max_seqlen_k - ctx.philox_seed = philox_seed - ctx.philox_offset = philox_offset - ctx.dropout_p = dropout_p - ctx.softmax_scale = softmax_scale - ctx.causal = causal - ctx.window_size = window_size - ctx.alibi_slopes = alibi_slopes - ctx.fused_backward = fused_backward - out = out_padded[..., :head_size_og] - - result = [out] - if return_lse: - result.append(softmax_lse) - if return_softmax: - result.append(S_dmask) - - return tuple(result) - - @staticmethod - def backward(ctx, do, *args): - q, k, v, out, softmax_lse, cu_seqlens_q, cu_seqlens_k = ctx.saved_tensors - dq, dk, dv = torch.zeros_like(q), torch.empty_like(k), torch.empty_like(v) - head_size_og = do.size(2) - do_padded = do - if head_size_og % 8 != 0: - do_padded = torch.nn.functional.pad(do, [0, 8 - head_size_og % 8]) - _flash_attn_backward( - do_padded, - q, - k, - v, - out, - softmax_lse, - dq, - dk, - dv, - ctx.softmax_scale, - ctx.alibi_slopes, - ctx.causal, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q=ctx.max_seqlen_q, - max_seqlen_k=ctx.max_seqlen_k, - dropout_p=ctx.dropout_p, - philox_seed=ctx.philox_seed, - philox_offset=ctx.philox_offset, - fused=ctx.fused_backward, - ) - dq = dq[..., : q.shape[-1]] # We could have padded the head dimension - dk = dk[..., : k.shape[-1]] - dv = dv[..., : v.shape[-1]] - return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None - - -def flash_attn_varlen_func( - q, - k, - v, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p=0.0, - softmax_scale=None, - causal=False, - window_size=(-1,-1), - alibi_slopes=None, - deterministic=False, - return_lse=False, - return_attn_probs=False, - block_table=None, - fused_backward=False, -): - """dropout_p should be set to 0.0 during evaluation - Supports multi-query and grouped-query attention (MQA/GQA) by passing in K, V with fewer heads - than Q. Note that the number of heads in Q must be divisible by the number of heads in KV. - For example, if Q has 6 heads and K, V have 2 heads, head 0, 1, 2 of Q will attention to head - 0 of K, V, and head 3, 4, 5 of Q will attention to head 1 of K, V. - - If causal=True, the causal mask is aligned to the bottom right corner of the attention matrix. - For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = masked out) is: - 1 1 1 1 0 - 1 1 1 1 1 - If seqlen_q = 5 and seqlen_k = 2, the causal mask is: - 0 0 - 0 0 - 0 0 - 1 0 - 1 1 - If the row of the mask is all zero, the output will be zero. - - If window_size != (-1, -1), implements sliding window local attention. Query at position i - will only attend to keys between - [i + seqlen_k - seqlen_q - window_size[0], i + seqlen_k - seqlen_q + window_size[1]] inclusive. - - Arguments: - q: (total_q, nheads, headdim), where total_q = total number of query tokens in the batch. - k: (total_k, nheads_k, headdim), where total_k = total number of key tokens in the batch. - v: (total_k, nheads_k, 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). - window_size: (left, right). If not (-1, -1), implements sliding window local attention. - alibi_slopes: (nheads,) or (batch_size, nheads), fp32. A bias of - (-alibi_slope * |i + seqlen_k - seqlen_q - j|) - is added to the attention score of query i and key j. - deterministic: bool. Whether to use the deterministic implementation of the backward pass, - which is slightly slower and uses more memory. The forward pass is always deterministic. - 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). - Return: - out: (total, nheads, headdim). - softmax_lse [optional, if return_attn_probs=True]: (nheads, total_q_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 FlashAttnVarlenFunc.apply( - q, - k, - v, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_attn_probs, - block_table, - torch.is_grad_enabled(), - fused_backward, - ) - - -class FlashAttnVarlenFP8Func(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, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_softmax, - block_table, - is_grad_enabled, - fused_backward, - ): - is_grad = is_grad_enabled and any( - x.requires_grad for x in [q, k, v] - ) - if softmax_scale is None: - softmax_scale = q.shape[-1] ** (-0.5) - head_size_og = q.size(2) - if head_size_og % 8 != 0: - q = torch.nn.functional.pad(q, [0, 8 - head_size_og % 8]) - k = torch.nn.functional.pad(k, [0, 8 - head_size_og % 8]) - v = torch.nn.functional.pad(v, [0, 8 - head_size_og % 8]) - - # cast input to fp8 - fp8_dtype = torch.float8_e4m3fnuz - q_fp8, descale_q = cast_varlen_to_fp8(q, fp8_dtype, cu_seqlens=cu_seqlens_q) - k_fp8, descale_k = cast_varlen_to_fp8(k, fp8_dtype, cu_seqlens=cu_seqlens_k) - v_fp8, descale_v = cast_varlen_to_fp8(v, fp8_dtype, cu_seqlens=cu_seqlens_k) - - out_padded, softmax_lse, S_dmask, philox_seed, philox_offset = _flash_attn_forward( - q_fp8, - k_fp8, - v_fp8, - dropout_p, - softmax_scale, - causal=causal, - window_size_left=window_size[0], - window_size_right=window_size[1], - alibi_slopes=alibi_slopes, - return_lse=return_lse, - return_softmax=return_softmax and dropout_p > 0, - max_seqlen_q=max_seqlen_q, - max_seqlen_k=max_seqlen_k, - cu_seqlens_q=cu_seqlens_q, - cu_seqlens_k=cu_seqlens_k, - descale_q=descale_q, - descale_k=descale_k, - descale_v=descale_v, - fused_backward=fused_backward, - ) - if is_grad: - ctx.save_for_backward(q_fp8, k_fp8, v_fp8, out_padded, softmax_lse, cu_seqlens_q, cu_seqlens_k, descale_q, descale_k, descale_v) - ctx.max_seqlen_q = max_seqlen_q - ctx.max_seqlen_k = max_seqlen_k - ctx.philox_seed = philox_seed - ctx.philox_offset = philox_offset - ctx.dropout_p = dropout_p - ctx.softmax_scale = softmax_scale - ctx.causal = causal - ctx.window_size = window_size - ctx.alibi_slopes = alibi_slopes - ctx.fused_backward = fused_backward - out = out_padded[..., :head_size_og] - result = [out] - if return_lse: - result.append(softmax_lse) - if return_softmax: - result.append(S_dmask) - - return tuple(result) - - @staticmethod - def backward(ctx, do, *args): - q_fp8, k_fp8, v_fp8, out, softmax_lse, cu_seqlens_q, cu_seqlens_k, descale_q, descale_k, descale_v = ctx.saved_tensors - dq, dk, dv = torch.zeros_like(q_fp8, dtype=torch.float32), torch.zeros_like(k_fp8, dtype=torch.float32), torch.zeros_like(v_fp8, dtype=torch.float32) - head_size_v_og = do.size(3) - do_padded = do - if head_size_v_og % 8 != 0: - do_padded = torch.nn.functional.pad(do, [0, 8 - head_size_v_og % 8]) - - fp8_dtype = torch.float8_e4m3fnuz - do_padded_fp8, descale_do = cast_varlen_to_fp8(do_padded, fp8_dtype, "thd", cu_seqlens_q) - - _flash_attn_backward( - do_padded_fp8, - q_fp8, - k_fp8, - v_fp8, - out, - softmax_lse, - dq, - dk, - dv, - ctx.softmax_scale, - ctx.alibi_slopes, - ctx.causal, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q=ctx.max_seqlen_q, - max_seqlen_k=ctx.max_seqlen_k, - dropout_p=ctx.dropout_p, - philox_seed=ctx.philox_seed, - philox_offset=ctx.philox_offset, - descale_q=descale_q, - descale_k=descale_k, - descale_v=descale_v, - descale_do=descale_do - ) - dq = dq[..., : q_fp8.shape[-1]] # We could have padded the head dimension - dk = dk[..., : k_fp8.shape[-1]] - dv = dv[..., : v_fp8.shape[-1]] - return dq, dk, dv, None, None, None, None, None, None, None, None, None, None, None - -def flash_attn_varlen_fp8_func( - q, - k, - v, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p=0.0, - softmax_scale=None, - causal=False, - window_size=(-1, -1), # -1 means infinite context window - alibi_slopes=None, - deterministic=False, - return_lse=False, - return_attn_probs=False, - block_table=None, - fused_backward=False, -): - return FlashAttnVarlenFP8Func.apply( - q, - k, - v, - cu_seqlens_q, - cu_seqlens_k, - max_seqlen_q, - max_seqlen_k, - dropout_p, - softmax_scale, - causal, - window_size, - alibi_slopes, - deterministic, - return_lse, - return_attn_probs, - block_table, - torch.is_grad_enabled(), - fused_backward, - ) + return delta \ No newline at end of file diff --git a/flash_attn/flash_attn_triton_amd/bwd_prefill_onekernel.py b/flash_attn/flash_attn_triton_amd/bwd_prefill_fused_no_atomics.py similarity index 89% rename from flash_attn/flash_attn_triton_amd/bwd_prefill_onekernel.py rename to flash_attn/flash_attn_triton_amd/bwd_prefill_fused_no_atomics.py index 67f7498f083..8bdcfd10d6a 100644 --- a/flash_attn/flash_attn_triton_amd/bwd_prefill_onekernel.py +++ b/flash_attn/flash_attn_triton_amd/bwd_prefill_fused_no_atomics.py @@ -1,10 +1,12 @@ +import os import torch import triton # type: ignore import triton.language as tl # type: ignore from typing import Literal, Optional -from .utils import DEBUG, AUTOTUNE, DROPOUT_USE_PYTORCH, DROPOUT_DUMP, get_shapes_from_layout, compute_fp8_scaling_factors, \ - get_strides_from_layout, create_dropout_mask, create_dropout_mask_varlen, is_cdna, is_fp8, is_rdna +from .utils import AUTOTUNE, DROPOUT_USE_PYTORCH, DROPOUT_DUMP, compute_fp8_scaling_factors, \ + create_dropout_mask, create_dropout_mask_varlen, is_cdna, is_fp8, is_rdna, round_multiple +DEBUG= False # NOTE: triton fails to import tl.constexprs so create them here for the file tl_DROPOUT_USE_PYTORCH: tl.constexpr = triton.language.constexpr(DROPOUT_USE_PYTORCH) tl_DROPOUT_DUMP: tl.constexpr = triton.language.constexpr(DROPOUT_DUMP) @@ -109,7 +111,7 @@ def _bwd_preprocess( O, DO, # noqa: E741 Delta, stride_ob, stride_oh, stride_om, stride_od, - stride_deltab, stride_deltah, stride_deltam, + stride_delta_b, stride_delta_h, stride_delta_m, stride_descale_do_z, cu_seqlens_q, max_seqlen_q, Descale_do, @@ -160,8 +162,8 @@ def _bwd_preprocess( delta = tl.sum(o.to(tl.float32) * (do.to(tl.float32) * descale_do), axis=1) else: delta = tl.sum(o.to(tl.float32) * do.to(tl.float32), axis=1) - delta_offset = Delta + bid * stride_deltab + hid * stride_deltah + q_start * stride_deltam - tl.store(delta_offset + offs_m * stride_deltam, delta, mask=mask_m) + delta_offset = Delta + bid * stride_delta_b + hid * stride_delta_h + q_start * stride_delta_m + tl.store(delta_offset + offs_m * stride_delta_m, delta, mask=mask_m) # The main inner-loop logic for computing dK and dV. @@ -172,7 +174,7 @@ def _bwd_dkdv_inner( stride_qm, stride_qk, stride_dom, stride_dok, stride_dropoutm, stride_dropoutn, - stride_deltam, + stride_lse_m, stride_delta_m, BLOCK_M: tl.constexpr, # 16 BLOCK_N: tl.constexpr, # 128 HEAD_DIM: tl.constexpr, # @@ -243,7 +245,7 @@ def _bwd_dkdv_inner( dropout_mask = rand_vals > dropout_p dropout_scale = 1.0 / (1 - dropout_p) # Load m before computing qk to reduce pipeline stall. - m = tl.load(M + offs_m * stride_deltam, mask=mask_m, other=0.0) + m = tl.load(M + offs_m * stride_lse_m, mask=mask_m, other=0.0) if IS_FP8: qkT = (tl.dot(k, qT) * descale_q * descale_k) else: @@ -297,7 +299,7 @@ def _bwd_dkdv_inner( if start_n == 256: print(f"pT: {pT.shape}\n", pT) # D (= delta) is pre-divided by ds_scale. - Di = tl.load(D + offs_m * stride_deltam, mask=mask_m) + Di = tl.load(D + offs_m * stride_delta_m, mask=mask_m) # Compute dP and dS. if IS_FP8: dpT = (tl.dot(v, tl.trans(do)) * descale_v * descale_do) @@ -326,7 +328,8 @@ def _bwd_dq_inner( # shared by Q/K/V. stride_qm, stride_qk, stride_kn, stride_kk, stride_vn, stride_vk, stride_dropoutm, stride_dropoutn, # stride for dropout - stride_deltam, + stride_lse_m, + stride_delta_m, seqlen_q, seqlen_k, # BLOCK_M2: tl.constexpr, # BLOCK_N2: tl.constexpr, # @@ -359,7 +362,7 @@ def _bwd_dq_inner( kT_ptrs = K + offs_n[None, :] * stride_kn + offs_k[:, None] * stride_kk vT_ptrs = V + offs_n[None, :] * stride_vn + offs_k[:, None] * stride_vk # D (= delta) is pre-divided by ds_scale. - Di = tl.load(Delta + offs_m * stride_deltam, mask=mask_m, other=0.0) + Di = tl.load(Delta + offs_m * stride_delta_m, mask=mask_m, other=0.0) # BLOCK_M2 must be a multiple of BLOCK_N2, otherwise the code wouldn't work. tl.static_assert(BLOCK_M2 % BLOCK_N2 == 0) curr_n = start_n @@ -458,7 +461,8 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba stride_dqb, stride_dqh, stride_dqm, stride_dqd, stride_dkb, stride_dkh, stride_dkn, stride_dkd, stride_dvb, stride_dvh, stride_dvn, stride_dvd, - stride_deltab, stride_deltah, stride_deltam, + stride_lse_b, stride_lse_h, stride_lse_m, + stride_delta_b, stride_delta_h, stride_delta_m, stride_dob, stride_doh, stride_dom, stride_dod, stride_dropoutb, stride_dropouth, stride_dropoutm, stride_dropoutn, stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, stride_descale_do_z, @@ -568,10 +572,10 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba Q_ptr = Q + adj_q adj_do = bid * stride_dob + hqid * stride_doh + q_start * stride_dom DO_ptr = DO + adj_do - adj_delta = bid * stride_deltab + hqid * stride_deltah + \ - q_start * stride_deltam - M_ptr = M + adj_delta + adj_delta = bid * stride_delta_b + hqid * stride_delta_h + q_start * stride_delta_m Delta_ptr = Delta + adj_delta + adj_m = bid * stride_lse_b + hqid * stride_lse_h + q_start * stride_lse_m + M_ptr = M + adj_m if USE_ALIBI: alibi_offset = bid * stride_az + hqid * stride_ah @@ -614,7 +618,7 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba stride_qm, stride_qd, # strides for q stride_dom, stride_dod, # strides for o stride_dropoutm, stride_dropoutn, # strides for dropout - stride_deltam, + stride_lse_m, stride_delta_m, MASK_BLOCK_M1, BLOCK_N1, # block dim HEAD_DIM, ACTUAL_HEAD_DIM, # head dim dropout_p, philox_seed, batch_philox_offset, dropout_offset, @@ -644,7 +648,7 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba stride_qm, stride_qd, # strides for q stride_dom, stride_dod, # strides for o stride_dropoutm, stride_dropoutn, # strides for dropout - stride_deltam, + stride_lse_m, stride_delta_m, BLOCK_M1, BLOCK_N1, # block dim HEAD_DIM, ACTUAL_HEAD_DIM, # head dim dropout_p, philox_seed, batch_philox_offset, dropout_offset, @@ -705,8 +709,10 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba adj_q = bid * stride_qb + hqid * stride_qh + q_start * stride_qm adj_do = bid * stride_dob + hqid * stride_doh + q_start * stride_dom adj_delta = \ - bid * stride_deltab + hqid * stride_deltah + q_start * stride_deltam + bid * stride_delta_b + hqid * stride_delta_h + q_start * stride_delta_m Delta_ptr = Delta + adj_delta + adj_m = bid * stride_lse_b + hqid * stride_lse_h + q_start * stride_lse_m + M_ptr = M + adj_m if USE_ALIBI: alibi_offset = bid * stride_az + hqid * stride_ah @@ -726,7 +732,7 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba Dropout_mask + bid * stride_dropoutb + hqid * stride_dropouth q = tl.load(Q + adj_q + offs_q, mask=mask_q, other=0.0) do = tl.load(DO + adj_do + offs_do, mask=mask_q, other=0.0) - m = tl.load(M + adj_delta + offs_m * stride_deltam, + m = tl.load(M + adj_m + offs_m * stride_lse_m, mask=offs_m < seqlen_q) m = m[:, None] @@ -749,7 +755,8 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba q, K, V, do, m, Delta_ptr, sm_scale, stride_qm, stride_qd, stride_kn, stride_kd, stride_vn, stride_vd, stride_dropoutm, stride_dropoutn, - stride_deltam, + stride_lse_m, + stride_delta_m, seqlen_q, seqlen_k, BLOCK_M2, MASK_BLOCK_N2, HEAD_DIM, ACTUAL_HEAD_DIM, @@ -775,7 +782,8 @@ def bwd_kernel_causal( # grid = (nheads_k, tl.cdiv(max_seqlen_q // BLOCK_M2), ba q, K, V, do, m, Delta_ptr, sm_scale, stride_qm, stride_qd, stride_kn, stride_kd, stride_vn, stride_vd, stride_dropoutm, stride_dropoutn, - stride_deltam, + stride_lse_m, + stride_delta_m, seqlen_q, seqlen_k, BLOCK_M2, BLOCK_N2, HEAD_DIM, ACTUAL_HEAD_DIM, @@ -814,7 +822,8 @@ def bwd_kernel_noncausal( stride_dqb, stride_dqh, stride_dqm, stride_dqd, stride_dkb, stride_dkh, stride_dkn, stride_dkd, stride_dvb, stride_dvh, stride_dvn, stride_dvd, - stride_deltab, stride_deltah, stride_deltam, + stride_lse_b, stride_lse_h, stride_lse_m, + stride_delta_b, stride_delta_h, stride_delta_m, stride_dob, stride_doh, stride_dom, stride_dod, stride_dropoutb, stride_dropouth, stride_dropoutm, stride_dropoutn, stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, stride_descale_do_z, @@ -890,9 +899,10 @@ def bwd_kernel_noncausal( Q_ptr = Q + adj_q adj_do = bid * stride_dob + hqid * stride_doh + q_start * stride_dom DO_ptr = DO + adj_do - adj_delta = bid * stride_deltab + hqid * stride_deltah + q_start * stride_deltam - M_ptr = M + adj_delta + adj_delta = bid * stride_delta_b + hqid * stride_delta_h + q_start * stride_delta_m Delta_ptr = Delta + adj_delta + adj_m = bid * stride_lse_b + hqid * stride_lse_h + q_start * stride_lse_m + M_ptr = M + adj_m if USE_ALIBI: alibi_offset = bid * stride_az + hqid * stride_ah @@ -927,7 +937,8 @@ def bwd_kernel_noncausal( stride_qm, stride_qd, # strides for q stride_dom, stride_dod, # strides for o stride_dropoutm, stride_dropoutn, # strides for dropout - stride_deltam, + stride_lse_m, + stride_delta_m, BLOCK_M1, BLOCK_N1, # block dim HEAD_DIM, ACTUAL_HEAD_DIM, # head dim dropout_p, philox_seed, batch_philox_offset, dropout_offset, # @@ -974,8 +985,10 @@ def bwd_kernel_noncausal( adj_q = bid * stride_qb + hqid * stride_qh + q_start * stride_qm adj_do = bid * stride_dob + hqid * stride_doh + q_start * stride_dom adj_delta = \ - bid * stride_deltab + hqid * stride_deltah + q_start * stride_deltam + bid * stride_delta_b + hqid * stride_delta_h + q_start * stride_delta_m Delta_ptr = Delta + adj_delta + adj_m = bid * stride_lse_b + hqid * stride_lse_h + q_start * stride_lse_m + M_ptr = M + adj_m if USE_ALIBI: alibi_offset = bid * stride_az + hqid * stride_ah @@ -996,7 +1009,7 @@ def bwd_kernel_noncausal( q = tl.load(Q + adj_q + offs_q, mask=mask_q, other=0.0) do = tl.load(DO + adj_do + offs_do, mask=mask_q, other=0.0) - m = tl.load(M + adj_delta + offs_m * stride_deltam, + m = tl.load(M + adj_m + offs_m * stride_lse_m, mask=offs_m < seqlen_q) m = m[:, None] @@ -1019,7 +1032,8 @@ def bwd_kernel_noncausal( q, K, V, do, m, Delta_ptr, sm_scale, stride_qm, stride_qd, stride_kn, stride_kd, stride_vn, stride_vd, stride_dropoutm, stride_dropoutn, - stride_deltam, + stride_lse_m, + stride_delta_m, seqlen_q, seqlen_k, BLOCK_M2, BLOCK_N2, HEAD_DIM, ACTUAL_HEAD_DIM, @@ -1048,8 +1062,11 @@ def is_contiguous(x, name): else: print(f"{name} is not contiguous") return x.contiguous() + + +OLD_LSE = os.environ.get('OLD_LSE', '0').lower() in ('1', 'true', 'yes') -def attention_prefill_backward_triton_split_oneKernel_impl( +def attention_prefill_backward_triton_split_fused_no_atomics_impl( do: torch.Tensor, q: torch.Tensor, k: torch.Tensor, @@ -1120,27 +1137,44 @@ def attention_prefill_backward_triton_split_oneKernel_impl( stride_descale_q_z = stride_descale_k_z = stride_descale_v_z = stride_descale_o_z = stride_descale_do_z = None - # get strides and shape - batch, nheads_q, nheads_k, head_size, max_seqlen_q_final, max_seqlen_k_final = \ - get_shapes_from_layout( - q, k, layout, - cu_seqlens_q, cu_seqlens_k, - max_seqlen_q, max_seqlen_k - ) - q_strides, k_strides, v_strides, o_strides = \ - get_strides_from_layout(q, k, v, o, layout) - stride_qb, stride_qh, stride_qm, stride_qd = q_strides - stride_kb, stride_kh, stride_kn, stride_kd = k_strides - stride_vb, stride_vh, stride_vn, stride_vd = v_strides - stride_ob, stride_oh, stride_om, stride_od = o_strides - dq_strides, dk_strides, dv_strides, do_strides = \ - get_strides_from_layout(dq, dk, dv, do, layout) - stride_dqb, stride_dqh, stride_dqm, stride_dqd = dq_strides - stride_dkb, stride_dkh, stride_dkn, stride_dkd = dk_strides - stride_dvb, stride_dvh, stride_dvn, stride_dvd = dv_strides - stride_dob, stride_doh, stride_dom, stride_dod = do_strides + # get params, strides and shape IS_VARLEN = layout == "thd" use_dropout = (dropout_p > 0.0) + + # get shapes and strides + if IS_VARLEN: + # shape + _, nheads_q, head_size = q.shape + _, nheads_k, _ = k.shape + batch = len(cu_seqlens_q) - 1 + max_seqlen_q_final = max_seqlen_q + max_seqlen_k_final = max_seqlen_k + + # strides + stride_qb, stride_qh, stride_qm, stride_qd = 0, q.stride(1), q.stride(0), q.stride(2) + stride_kb, stride_kh, stride_kn, stride_kd = 0, k.stride(1), k.stride(0), k.stride(2) + stride_vb, stride_vh, stride_vn, stride_vd = 0, v.stride(1), v.stride(0), v.stride(2) + stride_ob, stride_oh, stride_om, stride_od = 0, o.stride(1), o.stride(0), o.stride(2) + stride_dqb, stride_dqh, stride_dqm, stride_dqd = 0, dq.stride(1), dq.stride(0), dq.stride(2) + stride_dkb, stride_dkh, stride_dkn, stride_dkd = 0, dk.stride(1), dk.stride(0), dk.stride(2) + stride_dvb, stride_dvh, stride_dvn, stride_dvd = 0, dv.stride(1), dv.stride(0), dv.stride(2) + stride_dob, stride_doh, stride_dom, stride_dod = 0, do.stride(1), do.stride(0), do.stride(2) + stride_lse_b, stride_lse_h, stride_lse_m = (0, softmax_lse.stride(0), softmax_lse.stride(1)) + else: + # shapes + batch, max_seqlen_q_final, nheads_q, head_size = q.shape + _, max_seqlen_k_final, nheads_k, _ = k.shape + + # strides + stride_qb, stride_qh, stride_qm, stride_qd = q.stride(0), q.stride(2), q.stride(1), q.stride(3) + stride_kb, stride_kh, stride_kn, stride_kd = k.stride(0), k.stride(2), k.stride(1), k.stride(3) + stride_vb, stride_vh, stride_vn, stride_vd = v.stride(0), v.stride(2), v.stride(1), v.stride(3) + stride_ob, stride_oh, stride_om, stride_od = o.stride(0), o.stride(2), o.stride(1), o.stride(3) + stride_dqb, stride_dqh, stride_dqm, stride_dqd = dq.stride(0), dq.stride(2), dq.stride(1), dq.stride(3) + stride_dkb, stride_dkh, stride_dkn, stride_dkd = dk.stride(0), dk.stride(2), dk.stride(1), dk.stride(3) + stride_dvb, stride_dvh, stride_dvn, stride_dvd = dv.stride(0), dv.stride(2), dv.stride(1), dv.stride(3) + stride_dob, stride_doh, stride_dom, stride_dod = do.stride(0), do.stride(2), do.stride(1), do.stride(3) + stride_lse_b, stride_lse_h, stride_lse_m = softmax_lse.stride() use_alibi, (stride_az, stride_ah) = (True, alibi_slopes.stride()) if alibi_slopes is not None else (False, (0, 0)) # get closest power of 2 over or equal to 32. @@ -1150,18 +1184,35 @@ def attention_prefill_backward_triton_split_oneKernel_impl( ACTUAL_HEAD_DIM = head_size # init delta - delta = torch.empty_like(softmax_lse) - if IS_VARLEN: - stride_deltab = 0 - stride_deltam, stride_deltah = delta.stride() + if OLD_LSE: + delta = torch.empty_like(softmax_lse) + if IS_VARLEN: + stride_delta_b, stride_delta_h, stride_delta_m = 0, delta.stride(0), delta.stride(1) + else: + stride_delta_b, stride_delta_h, stride_delta_m = delta.stride() else: - stride_deltab, stride_deltah, stride_deltam = delta.stride() + if IS_VARLEN: + # interface expects the varlen sequence dims to rounded like this. Not sure why. + batch_size = cu_seqlens_q.numel() - 1 + total_q, num_heads, _ = q.shape + total_q_rounded = total_q + 128 * batch_size + delta_padded = torch.zeros((nheads_q, total_q_rounded), device=q.device, dtype=torch.float32) + delta = delta_padded[:, :total_q] + stride_delta_b, stride_delta_h, stride_delta_m = 0, delta.stride(0), delta.stride(1) + else: + # the interface expects the sequence dimension to be rounded to 128 + max_seqlen_q_rounded = round_multiple(max_seqlen_q_final, 128) + delta_padded = torch.zeros((batch, nheads_q, max_seqlen_q_rounded), + device=softmax_lse.device, dtype=torch.float32) + delta = delta_padded[:, :, :max_seqlen_q_final] + stride_delta_b, stride_delta_h, stride_delta_m = delta.stride() + pre_grid = lambda META: (triton.cdiv(max_seqlen_q_final, META['PRE_BLOCK']), batch, nheads_q) _bwd_preprocess[pre_grid]( o, do, delta, stride_ob, stride_oh, stride_om, stride_od, - stride_deltab, stride_deltah, stride_deltam, + stride_delta_b, stride_delta_h, stride_delta_m, stride_descale_do_z, cu_seqlens_q, max_seqlen_q_final, descale_do, @@ -1214,7 +1265,8 @@ def attention_prefill_backward_triton_split_oneKernel_impl( stride_dqb, stride_dqh, stride_dqm, stride_dqd, stride_dkb, stride_dkh, stride_dkn, stride_dkd, stride_dvb, stride_dvh, stride_dvn, stride_dvd, - stride_deltab, stride_deltah, stride_deltam, + stride_lse_b, stride_lse_h, stride_lse_m, + stride_delta_b, stride_delta_h, stride_delta_m, stride_dob, stride_doh, stride_dom, stride_dod, stride_dropoutb, stride_dropouth, stride_dropoutm, stride_dropoutn, stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, stride_descale_do_z, @@ -1247,7 +1299,8 @@ def attention_prefill_backward_triton_split_oneKernel_impl( stride_dqb, stride_dqh, stride_dqm, stride_dqd, stride_dkb, stride_dkh, stride_dkn, stride_dkd, stride_dvb, stride_dvh, stride_dvn, stride_dvd, - stride_deltab, stride_deltah, stride_deltam, + stride_lse_b, stride_lse_h, stride_lse_m, + stride_delta_b, stride_delta_h, stride_delta_m, stride_dob, stride_doh, stride_dom, stride_dod, stride_dropoutb, stride_dropouth, stride_dropoutm, stride_dropoutn, stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, stride_descale_do_z, @@ -1271,4 +1324,7 @@ def attention_prefill_backward_triton_split_oneKernel_impl( DEBUG_TRITON_DETAIL=DEBUG_TRITON_DETAIL, ) - return delta \ No newline at end of file + if OLD_LSE: + return delta + else: + return delta_padded diff --git a/flash_attn/flash_attn_triton_amd/bwd_prefill_split.py b/flash_attn/flash_attn_triton_amd/bwd_prefill_split.py index c1e2ff5985f..56187ea71f0 100644 --- a/flash_attn/flash_attn_triton_amd/bwd_prefill_split.py +++ b/flash_attn/flash_attn_triton_amd/bwd_prefill_split.py @@ -2,9 +2,11 @@ import triton # type: ignore import triton.language as tl # type: ignore from typing import Literal, Optional -from .utils import DEBUG, DROPOUT_USE_PYTORCH, DROPOUT_DUMP, compute_fp8_scaling_factors, get_shapes_from_layout, \ +from .utils import DROPOUT_USE_PYTORCH, DROPOUT_DUMP, compute_fp8_scaling_factors, get_shapes_from_layout, \ get_strides_from_layout, create_dropout_mask, create_dropout_mask_varlen, is_fp8 +DEBUG = False + # NOTE: triton fails to import tl.constexprs so create them here for the file tl_DROPOUT_USE_PYTORCH: tl.constexpr = triton.language.constexpr(DROPOUT_USE_PYTORCH) tl_DROPOUT_DUMP: tl.constexpr = triton.language.constexpr(DROPOUT_DUMP) diff --git a/flash_attn/flash_attn_triton_amd/bwd_ref.py b/flash_attn/flash_attn_triton_amd/bwd_ref.py index 639211a51f6..8bdccb1d329 100644 --- a/flash_attn/flash_attn_triton_amd/bwd_ref.py +++ b/flash_attn/flash_attn_triton_amd/bwd_ref.py @@ -1,8 +1,9 @@ import torch import math from typing import Literal, Optional -from .utils import DEBUG, compute_alibi_tensor_ref +from .utils import compute_alibi_tensor_ref +DEBUG = False DEBUG_CORE = False def attention_backward_core_ref_impl( @@ -196,8 +197,8 @@ def attention_varlen_backward_pytorch_ref_impl( dq = torch.zeros_like(q) dk = torch.zeros_like(k) dv = torch.zeros_like(v) - # delta has the same shape as softmax_lse: [total_L_q, nheads_q] - delta = torch.zeros((total_L_q, nheads_q), dtype=torch.float32, device=o.device) + # delta has the same shape as softmax_lse + delta = torch.zeros_like(softmax_lse) for i in range(batch_size): # Get the start and end indices for the current sequence @@ -212,7 +213,7 @@ def attention_varlen_backward_pytorch_ref_impl( v_i = v[start_k:end_k, :, :] # [L_k_i, nheads_k, head_dim] do_i = do[start_q:end_q, :, :] # [L_q_i, nheads_q, head_dim] o_i = o[start_q:end_q, :, :] # [L_q_i, nheads_q, head_dim] - softmax_lse_i = softmax_lse[start_q:end_q, :] # [L_q_i, nheads_q] + softmax_lse_i = softmax_lse[:, start_q:end_q] # [nheads_q, L_q_i] if group_size != 1: # MQA or GQA case @@ -220,7 +221,7 @@ def attention_varlen_backward_pytorch_ref_impl( q_i = q_i.view(q_i.shape[0], nheads_k, group_size, head_dim) do_i = do_i.view(do_i.shape[0], nheads_k, group_size, head_dim) o_i = o_i.view(o_i.shape[0], nheads_k, group_size, head_dim) - softmax_lse_i = softmax_lse_i.view(softmax_lse_i.shape[0], nheads_k, group_size) + softmax_lse_i = softmax_lse_i.view(nheads_k, group_size, softmax_lse_i.shape[1]) # Expand k_i and v_i to match group_size k_i = k_i.unsqueeze(2).expand(-1, -1, group_size, -1) v_i = v_i.unsqueeze(2).expand(-1, -1, group_size, -1) @@ -228,16 +229,17 @@ def attention_varlen_backward_pytorch_ref_impl( q_i = q_i.reshape(q_i.shape[0], nheads_k * group_size, head_dim) do_i = do_i.reshape(do_i.shape[0], nheads_k * group_size, head_dim) o_i = o_i.reshape(o_i.shape[0], nheads_k * group_size, head_dim) - softmax_lse_i = softmax_lse_i.reshape(softmax_lse_i.shape[0], nheads_k * group_size) + softmax_lse_i = softmax_lse_i.reshape(nheads_k * group_size, softmax_lse_i.shape[2]) k_i = k_i.reshape(k_i.shape[0], nheads_k * group_size, head_dim) v_i = v_i.reshape(v_i.shape[0], nheads_k * group_size, head_dim) + # Permute to [nheads_total, L, head_dim] q_i = q_i.permute(1, 0, 2) k_i = k_i.permute(1, 0, 2) v_i = v_i.permute(1, 0, 2) do_i = do_i.permute(1, 0, 2) o_i = o_i.permute(1, 0, 2) - softmax_lse_i = softmax_lse_i.transpose(0, 1) + if alibi_slopes is not None: alibi_slopes_i = alibi_slopes[i] else: @@ -264,7 +266,6 @@ def attention_varlen_backward_pytorch_ref_impl( dq_i = dq_i.permute(1, 0, 2) # [L_q_i, nheads_total, head_dim] dk_i = dk_i.permute(1, 0, 2) # [L_k_i, nheads_total, head_dim] dv_i = dv_i.permute(1, 0, 2) # [L_k_i, nheads_total, head_dim] - delta_i = delta_i.transpose(1, 0) # [L_q_i, nheads_total] if group_size != 1: # Reshape dq_i and delta_i back to original shape @@ -286,7 +287,7 @@ def attention_varlen_backward_pytorch_ref_impl( dq[start_q:end_q, :, :] = dq_i dk[start_k:end_k, :, :] += dk_i # Accumulate gradients for shared keys dv[start_k:end_k, :, :] += dv_i # Accumulate gradients for shared values - delta[start_q:end_q, :] = delta_i + delta[:, start_q:end_q] = delta_i return dq, dk, dv, delta diff --git a/flash_attn/flash_attn_triton_amd/fwd_decode.py b/flash_attn/flash_attn_triton_amd/fwd_decode.py index 3f2d92c22d6..e165d714876 100644 --- a/flash_attn/flash_attn_triton_amd/fwd_decode.py +++ b/flash_attn/flash_attn_triton_amd/fwd_decode.py @@ -2,7 +2,9 @@ import triton import triton.language as tl from typing import Literal, Optional, Union -from .utils import AUTOTUNE, DEBUG, get_padded_headsize, get_shape_and_strides_from_layout, is_cdna +from .utils import AUTOTUNE, get_padded_headsize, get_shape_and_strides_from_layout, is_cdna + +DEBUG = False def get_cdna_autotune_configs(): return [ diff --git a/flash_attn/flash_attn_triton_amd/fwd_prefill.py b/flash_attn/flash_attn_triton_amd/fwd_prefill.py index 08a307e7669..e33982bb6a7 100644 --- a/flash_attn/flash_attn_triton_amd/fwd_prefill.py +++ b/flash_attn/flash_attn_triton_amd/fwd_prefill.py @@ -2,7 +2,9 @@ import triton import triton.language as tl from typing import Literal, Optional, Union -from .utils import DEBUG, DROPOUT_USE_PYTORCH, DROPOUT_DUMP, AUTOTUNE, compute_alibi_block, compute_fp8_scaling_factors, get_arch, get_shapes_from_layout, get_strides_from_layout, is_cdna, is_fp8, is_rdna, create_dropout_mask +from .utils import DROPOUT_USE_PYTORCH, DROPOUT_DUMP, AUTOTUNE, compute_alibi_block, compute_fp8_scaling_factors, get_arch, is_cdna, is_fp8, is_rdna, create_dropout_mask + +DEBUG = False # NOTE: triton fails to import tl.constexprs so create them here for the file tl_DROPOUT_USE_PYTORCH: tl.constexpr = triton.language.constexpr(DROPOUT_USE_PYTORCH) @@ -610,7 +612,7 @@ def attention_prefill_forward_triton_impl( stride_descale_q_z = stride_descale_k_z = stride_descale_v_z = stride_descale_o_z = None # check flags - is_varlen = layout == "thd" + IS_VARLEN = layout == "thd" use_alibi, (stride_az, stride_ah) = (True, alibi_slopes.stride()) if alibi_slopes is not None else (False, (0, 0)) is_inference = False if cache_seqlens is None else True if is_inference: @@ -622,8 +624,36 @@ def attention_prefill_forward_triton_impl( if (bias is not None): assert (bias.numel() < 2**31) - batch, nheads_q, nheads_k, head_size, seqlen_q, seqlen_k = get_shapes_from_layout(q, k, layout, cu_seqlens_q, cu_seqlens_k, max_seqlens_q, max_seqlens_k) - q_strides, k_strides, v_strides, o_strides = get_strides_from_layout(q, k, v, o, layout) + # get shape and strides + if IS_VARLEN: # thd layout + # shape + total_q, nheads_q, head_size = q.shape + _, nheads_k, _ = k.shape + batch = len(cu_seqlens_q) - 1 + + # softmax_lse is the log of the normalization constant / sum of expoential score(unnormalzied probablities) + softmax_lse = torch.zeros((nheads_q, total_q), device=q.device, dtype=torch.float32) + + # strides + stride_qb, stride_qh, stride_qm, stride_qd = 0, q.stride(1), q.stride(0), q.stride(2) + stride_kb, stride_kh, stride_kn, stride_kd = 0, k.stride(1), k.stride(0), k.stride(2) + stride_vb, stride_vh, stride_vn, stride_vd = 0, v.stride(1), v.stride(0), v.stride(2) + stride_ob, stride_oh, stride_om, stride_od = 0, o.stride(1), o.stride(0), o.stride(2) + stride_lse_z, stride_lse_h, stride_lse_m = 0, softmax_lse.stride(0), softmax_lse.stride(1) + else: # bshd layout + # shape + batch, seqlen_q, nheads_q, head_size = q.shape + _, _, nheads_k, _ = k.shape + + # softmax_lse is the log of the normalization constant / sum of expoential score(unnormalzied probablities) + softmax_lse = torch.zeros((batch, nheads_q, seqlen_q), device=q.device, dtype=torch.float32) + + # strides + stride_qb, stride_qh, stride_qm, stride_qd = q.stride(0), q.stride(2), q.stride(1), q.stride(3) + stride_kb, stride_kh, stride_kn, stride_kd = k.stride(0), k.stride(2), k.stride(1), k.stride(3) + stride_vb, stride_vh, stride_vn, stride_vd = v.stride(0), v.stride(2), v.stride(1), v.stride(3) + stride_ob, stride_oh, stride_om, stride_od = o.stride(0), o.stride(2), o.stride(1), o.stride(3) + stride_lse_z, stride_lse_h, stride_lse_m = softmax_lse.stride() # Get closest power of 2 over or equal to 32. padded_d_model = 1 << (head_size - 1).bit_length() @@ -650,34 +680,34 @@ def attention_prefill_forward_triton_impl( else: dropout_mask = torch.zeros((batch, nheads_q, max_seqlens_q, max_seqlens_k), device=q.device, dtype=torch.float32) - scores_strides = (sd_mask.stride(0), sd_mask.stride(1), sd_mask.stride(2), sd_mask.stride(3)) + stride_sz, stride_sh, stride_sm, stride_sn = (sd_mask.stride(0), sd_mask.stride(1), sd_mask.stride(2), sd_mask.stride(3)) else: sd_mask = None dropout_mask = None - scores_strides = (0, 0, 0, 0) + stride_sz, stride_sh, stride_sm, stride_sn = (0, 0, 0, 0) - # stores LSE the log of the normalization constant / sum of expoential score(unnormalzied probablities) - if is_varlen: - softmax_lse = torch.zeros((q.shape[0], nheads_q), device=q.device, dtype=torch.float32) - stride_lse_m, stride_lse_h = softmax_lse.stride() - stride_lse_z = 0 - else: - softmax_lse = torch.zeros((batch, nheads_q, max_seqlens_q), device=q.device, dtype=torch.float32) - stride_lse_z, stride_lse_h, stride_lse_m = softmax_lse.stride() if bias is not None: - bias_strides = (bias.stride(0), bias.stride(1),bias.stride(2), + stride_bz, stride_bh, stride_bm, stride_bn = (bias.stride(0), bias.stride(1),bias.stride(2), bias.stride(3)) else: - bias_strides = (0, 0, 0, 0) + stride_bz, stride_bh, stride_bm, stride_bn = (0, 0, 0, 0) attn_fwd[grid](q, k, v, bias, cache_seqlens, cache_batch_idx, descale_q, descale_k, descale_v, descale_o, stride_descale_q_z, stride_descale_k_z, stride_descale_v_z, stride_descale_o_z, - sm_scale, softmax_lse, o, *q_strides, *k_strides, *v_strides, *o_strides, - *bias_strides, stride_az, stride_ah, *scores_strides, stride_lse_z, stride_lse_h, stride_lse_m, cu_seqlens_q, cu_seqlens_k, + sm_scale, softmax_lse, o, + stride_qb, stride_qh, stride_qm, stride_qd, + stride_kb, stride_kh, stride_kn, stride_kd, + stride_vb, stride_vh, stride_vn, stride_vd, + stride_ob, stride_oh, stride_om, stride_od, + stride_bz, stride_bh, stride_bm, stride_bn, + stride_az, stride_ah, + stride_sz, stride_sh, stride_sm, stride_sn, + stride_lse_z, stride_lse_h, stride_lse_m, + cu_seqlens_q, cu_seqlens_k, dropout_p=dropout_p, philox_seed=philox_seed, philox_offset_base=philox_offset, sd_mask=sd_mask, dropout_mask=dropout_mask, alibi_slopes=alibi_slopes, HQ=nheads_q, HK=nheads_k, ACTUAL_BLOCK_DMODEL=head_size, MAX_SEQLENS_Q=max_seqlens_q, - MAX_SEQLENS_K=max_seqlens_k, IS_CAUSAL=causal, IS_VARLEN=is_varlen, IS_INFERENCE=is_inference, + MAX_SEQLENS_K=max_seqlens_k, IS_CAUSAL=causal, IS_VARLEN=IS_VARLEN, IS_INFERENCE=is_inference, BLOCK_DMODEL=padded_d_model, USE_BIAS=False if bias is None else True, USE_ALIBI=use_alibi, ENABLE_DROPOUT=dropout_p > 0.0, USE_EXP2=use_exp2, RETURN_SCORES=return_softmax, IS_FP8=IS_FP8, FP8_MAX=FP8_MAX, FP8_OUTPUT=FP8_OUTPUT, FLIP_GRID=FLIP_GRID) diff --git a/flash_attn/flash_attn_triton_amd/fwd_ref.py b/flash_attn/flash_attn_triton_amd/fwd_ref.py index baefb2410c1..6af99798ae9 100644 --- a/flash_attn/flash_attn_triton_amd/fwd_ref.py +++ b/flash_attn/flash_attn_triton_amd/fwd_ref.py @@ -1,8 +1,9 @@ import torch import math from typing import Literal, Optional -from .utils import DEBUG, compute_alibi_tensor_ref +from .utils import compute_alibi_tensor_ref +DEBUG = False DEBUG_CORE = False def attention_forward_core_ref_impl(q, k, v, sm_scale, causal, dropout_p, philox_seed, philox_offset, alibi_slopes, use_exp2): @@ -247,7 +248,7 @@ def attention_varlen_forward_pytorch_ref_impl( total_L_k = k.shape[0] o = torch.zeros((total_L_q, nheads_q, head_dim), dtype=q.dtype, device=q.device) - softmax_lse = torch.zeros((total_L_q, nheads_q), dtype=torch.float32, device=q.device) + softmax_lse = torch.zeros((nheads_q, total_L_q), dtype=torch.float32, device=q.device) sd_mask = torch.zeros((batch_size, nheads_q, max_seqlen_q, max_seqlen_k), dtype=torch.float32, device=q.device) # Compute group_size for MQA/GQA handling @@ -318,12 +319,11 @@ def attention_varlen_forward_pytorch_ref_impl( # Convert back to 'thd' layout o_i = o_i.permute(1, 0, 2) # [L_q_i, nheads_q, head_dim] - softmax_lse_i = softmax_lse_i.permute(1, 0) # [L_q_i, nheads_q] sd_mask_i = sd_mask_i # [nheads_q, L_q_i, L_k_i] # Place outputs in pre-allocated tensors o[start_q:end_q, :, :] = o_i - softmax_lse[start_q:end_q, :] = softmax_lse_i + softmax_lse[:, start_q:end_q] = softmax_lse_i sd_mask[i, :, :seqlen_q, :seqlen_k] = sd_mask_i return o, softmax_lse, sd_mask diff --git a/flash_attn/flash_attn_triton_amd/interface_fa.py b/flash_attn/flash_attn_triton_amd/interface_fa.py index a92b6f5d65d..9c10b7436c2 100644 --- a/flash_attn/flash_attn_triton_amd/interface_fa.py +++ b/flash_attn/flash_attn_triton_amd/interface_fa.py @@ -1,21 +1,20 @@ import torch import os from .fwd_prefill import attention_prefill_forward_triton_impl -from .bwd_prefill import attention_prefill_backward_triton_impl from .bwd_prefill_split import attention_prefill_backward_triton_split_impl -from .bwd_prefill_fused import _flash_attn_backward as attention_prefill_backward_triton_fused_impl -from .bwd_prefill_onekernel import attention_prefill_backward_triton_split_oneKernel_impl +from .bwd_prefill_fused_atomics import attention_prefill_backward_triton_fused_atomics_impl +from .bwd_prefill_fused_no_atomics import attention_prefill_backward_triton_split_fused_no_atomics_impl from .fwd_decode import attention_decode_forward_triton_impl from .fwd_ref import attention_forward_pytorch_ref_impl from .bwd_ref import attention_backward_pytorch_ref_impl -from .utils import DEBUG, USE_REF, MetaData, get_shapes_from_layout, is_fp8 +from .utils import DEBUG, USE_REF, MetaData, is_fp8 from einops import rearrange, repeat from flash_attn.layers.rotary import apply_rotary_emb from typing import Literal, Optional, Union USE_EXP2 = True -BWD_MODE = os.environ.get('BWD_MODE', 'jingning').lower() +BWD_MODE = os.environ.get('BWD_MODE', 'fused_no_atomics').lower() def fwd(q: torch.Tensor, k: torch.Tensor, @@ -67,8 +66,6 @@ def fwd(q: torch.Tensor, metadata.max_seqlens_q = q.shape[1] metadata.max_seqlens_k = k.shape[1] metadata.layout = "bshd" - if return_softmax: - metadata.return_scores = True # get shape batch, _ , nheads_q, _= q.shape @@ -85,11 +82,9 @@ def fwd(q: torch.Tensor, raise ValueError(f"Alibi can be (nheads,) or (batch_size, nheads). Given tensor with shape {alibi_slopes.shape}") metadata.need_alibi(alibi_slopes, batch, nheads_q) - if dropout_p > 0.0: - metadata.need_dropout(dropout_p) - rng_state = torch.as_tensor([metadata.philox_seed, metadata.philox_offset]) # as_tensors uses the underlying data and doesnot cast - else: - rng_state = None + # store rng state + metadata.need_dropout(dropout_p, return_softmax) + rng_state = torch.as_tensor([metadata.philox_seed, metadata.philox_offset]) # as_tensors uses the underlying data and doesnot cast # check arguments metadata.check_args(q, k, v, out) @@ -139,7 +134,7 @@ def fwd(q: torch.Tensor, metadata.dropout_p, metadata.philox_seed, metadata.philox_offset, - metadata.return_scores, + metadata.return_softmax, USE_EXP2, descale_q, descale_k, @@ -155,6 +150,7 @@ def fwd(q: torch.Tensor, print("descale_o:", descale_o, descale_o.shape if descale_o is not None else None) print("softmax_lse:", softmax_lse, softmax_lse.shape) print("sd_mask:", sd_mask, sd_mask.shape if sd_mask is not None else None ) + print("rng_state:", rng_state) return out, softmax_lse, sd_mask, rng_state @@ -304,8 +300,8 @@ def bwd( descale_dv, ) delta = delta_triton - elif BWD_MODE == "fused": - delta_triton = attention_prefill_backward_triton_fused_impl( + elif BWD_MODE == "fused_atomics": + delta_triton = attention_prefill_backward_triton_fused_atomics_impl( dout, q, k, @@ -332,8 +328,8 @@ def bwd( True, ) delta = delta_triton - elif BWD_MODE == "jingning": - delta_triton = attention_prefill_backward_triton_split_oneKernel_impl( + elif BWD_MODE == "fused_no_atomics": + delta_triton = attention_prefill_backward_triton_split_fused_no_atomics_impl( dout, q, k, @@ -438,8 +434,6 @@ def varlen_fwd( # Setup metadata metadata = MetaData(sm_scale=softmax_scale) - if return_softmax: - metadata.return_scores = True metadata.set_varlen_params(cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k) # set layout to "thd" and other metdata assert metadata.layout is not None @@ -459,11 +453,9 @@ def varlen_fwd( raise ValueError("Alibi can be (nheads,) or (batch_size, nheads).") metadata.need_alibi(alibi_slopes, batch, nheads_q) - if dropout_p > 0.0: - metadata.need_dropout(dropout_p) - rng_state = torch.as_tensor([metadata.philox_seed, metadata.philox_offset]) # as_tensors uses the underlying data and doesnot cast - else: - rng_state = None + # store rng state + metadata.need_dropout(dropout_p, return_softmax) + rng_state = torch.as_tensor([metadata.philox_seed, metadata.philox_offset]) # as_tensors uses the underlying data and doesnot cast # Check arguments metadata.check_args(q, k, v, out) @@ -513,7 +505,7 @@ def varlen_fwd( metadata.dropout_p, metadata.philox_seed, metadata.philox_offset, - metadata.return_scores, + metadata.return_softmax, USE_EXP2, descale_q, descale_k, @@ -681,8 +673,8 @@ def varlen_bwd( descale_dv, ) delta = delta_triton - elif BWD_MODE == "fused": - delta_triton = attention_prefill_backward_triton_fused_impl( + elif BWD_MODE == "fused_atomics": + delta_triton = attention_prefill_backward_triton_fused_atomics_impl( dout, q, k, @@ -709,8 +701,8 @@ def varlen_bwd( True, ) delta = delta_triton - elif BWD_MODE == "jingning": - delta_triton = attention_prefill_backward_triton_split_oneKernel_impl( + elif BWD_MODE == "fused_no_atomics": + delta_triton = attention_prefill_backward_triton_split_fused_no_atomics_impl( dout, q, k, @@ -904,7 +896,7 @@ def fwd_kvcache( metadata.dropout_p, metadata.philox_seed, metadata.philox_offset, - metadata.return_scores, + metadata.return_softmax, USE_EXP2, None, None, diff --git a/flash_attn/flash_attn_triton_amd/test.py b/flash_attn/flash_attn_triton_amd/test.py index ea82de065b5..f634103ca69 100644 --- a/flash_attn/flash_attn_triton_amd/test.py +++ b/flash_attn/flash_attn_triton_amd/test.py @@ -17,15 +17,18 @@ flash_attn_varlen_fp8_func, flash_attn_varlen_kvpacked_func, flash_attn_varlen_qkvpacked_func, - flash_attn_varlen_qkvpacked_fp8_func + flash_attn_varlen_qkvpacked_fp8_func, + flash_attn_with_kvcache ) -from .utils import DEBUG, input_helper, arch_supports_fp8 +from .utils import generate_bshd_kv_packed, generate_bshd_qkv_packed, generate_bshd_tensor, generate_varlen_kv_packed, generate_varlen_qkv_packed, input_helper, arch_supports_fp8, generate_varlen_tensor from .fwd_ref import attention_forward_pytorch_ref_impl from .fwd_prefill import attention_prefill_forward_triton_impl -from .bwd_prefill_onekernel import attention_prefill_backward_triton_split_oneKernel_impl +from .bwd_prefill_fused_no_atomics import attention_prefill_backward_triton_split_fused_no_atomics_impl from .bwd_ref import attention_backward_pytorch_ref_impl +DEBUG = False + # set print options # torch.set_printoptions(linewidth=5e5, edgeitems=10, sci_mode=False) # np.set_printoptions(linewidth=5000, threshold=1e4, suppress=True, precision=4) @@ -101,7 +104,7 @@ def test_op_prefill_fwd_impl(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dr metadata.need_causal(True) # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that - metadata.need_dropout(dropout_p) + metadata.need_dropout(dropout_p, True) # call Triton's forward implementation directly @@ -128,7 +131,7 @@ def test_op_prefill_fwd_impl(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dr metadata.dropout_p, metadata.philox_seed, metadata.philox_offset, - metadata.return_scores, + metadata.return_softmax, use_exp2, None, None, @@ -164,7 +167,7 @@ def test_op_prefill_fwd_impl(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dr print("Compare Triton Impl with refernce Pytorch Impl") # this can be set to true manually or when using dropout - if metadata.return_scores: + if metadata.return_softmax: if DEBUG: print("sd_mask_triton:", sd_mask_triton, sd_mask_triton.shape) print("sd_mask_ref:", sd_mask_ref, sd_mask_ref.shape) @@ -268,7 +271,7 @@ def test_op_prefill_bwd_impl(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dr q, k, v, do, metadata = input_helper(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dropout_p, dtype, layout=layout, device=device) # NOTE: the returned score is not the same as the reference because we need to adjust as we find new maxes per block. We are not doing that - metadata.need_dropout(dropout_p) + metadata.need_dropout(dropout_p, True) # =============================================== Reference ============================================================== # fwd @@ -334,7 +337,7 @@ def test_op_prefill_bwd_impl(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dr dq_triton = torch.zeros_like(q_triton, dtype=q.dtype) # NOTE: the kernel does inplace accumlation on dq so dq has to be zeros dk_triton = torch.zeros_like(k_triton, dtype=k.dtype) if DEBUG_INPUT else torch.empty_like(k_triton, dtype=k.dtype) dv_triton = torch.zeros_like(v_triton, dtype=v.dtype) if DEBUG_INPUT else torch.empty_like(v_triton, dtype=v.dtype) - delta_triton = attention_prefill_backward_triton_split_oneKernel_impl( + delta_triton = attention_prefill_backward_triton_split_fused_no_atomics_impl( do_triton, q_triton, k_triton, @@ -931,3 +934,189 @@ def test_ir(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD, causal, dropout_p, layout, for file, fp8_found in ttir_files_fp8_found_status.items(): assert fp8_found, f"{fp8_types} not found in {file}" + + +def clear_compile_cache(): + """Clear torch compile caches to prevent graph merging""" + if hasattr(torch._dynamo, 'reset'): + torch._dynamo.reset() + torch.cuda.synchronize() + + +@pytest.mark.parametrize( + "BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD", + [ + # (4, 8, 8, 128, 128, 64), # small test + (32, 32, 32, 531, 531, 128), # original test + # (16, 48, 16, 256, 512, 64), # MQA test (HQ > HK) + ], +) +def test_torch_compile(BATCH, HQ, HK, N_CTX_Q, N_CTX_K, D_HEAD): + print(f"\n\nTesting with BATCH={BATCH}, HQ={HQ}, HK={HK}, N_CTX_Q={N_CTX_Q}, N_CTX_K={N_CTX_K}, D_HEAD={D_HEAD}") + + try: + # Test 1: flash_attn_func + print("\n1. Testing flash_attn_func...") + clear_compile_cache() + + q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD) + k = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD) + v = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD) + + flash_attn_func_compiled = torch.compile(flash_attn_func) + o = flash_attn_func_compiled(q, k, v, causal=True) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_func SUCCESS") + + # cleanup + del q, k, v, o + torch.cuda.empty_cache() + + + # Test 2: flash_attn_varlen_func + print("\n2. Testing flash_attn_varlen_func...") + clear_compile_cache() + + q, cu_seqlens_q, max_seqlen_q = generate_varlen_tensor(BATCH * N_CTX_Q, HQ, D_HEAD, BATCH) + k, cu_seqlens_k, max_seqlen_k = generate_varlen_tensor(BATCH * N_CTX_K, HK, D_HEAD, BATCH) + v, _, _ = generate_varlen_tensor(BATCH * N_CTX_K, HK, D_HEAD, BATCH) + + flash_attn_varlen_func_compiled = torch.compile(flash_attn_varlen_func) + o = flash_attn_varlen_func_compiled( + q, k, v, cu_seqlens_q, cu_seqlens_k, + max_seqlen_q, max_seqlen_k, causal=True + ) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_varlen_func SUCCESS") + + # cleanup + del q, k, v, o, cu_seqlens_q, cu_seqlens_k + torch.cuda.empty_cache() + + + # Test 3: flash_attn_qkvpacked_func + print("\n3. Testing flash_attn_qkvpacked_func...") + clear_compile_cache() + + qkv = generate_bshd_qkv_packed(BATCH, N_CTX_Q, HQ, D_HEAD) + + flash_attn_qkvpacked_func_compiled = torch.compile(flash_attn_qkvpacked_func) + o = flash_attn_qkvpacked_func_compiled(qkv, causal=True) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_qkvpacked_func SUCCESS") + + # cleanup + del qkv, o + torch.cuda.empty_cache() + + + # Test 4: flash_attn_varlen_qkvpacked_func + print("\n4. Testing flash_attn_varlen_qkvpacked_func...") + clear_compile_cache() + + total_q = BATCH * N_CTX_Q + qkv, cu_seqlens, max_seqlen = generate_varlen_qkv_packed(total_q, HQ, D_HEAD, BATCH) + + flash_attn_varlen_qkvpacked_func_compiled = torch.compile(flash_attn_varlen_qkvpacked_func) + o = flash_attn_varlen_qkvpacked_func_compiled( + qkv, cu_seqlens, max_seqlen, causal=True + ) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_varlen_qkvpacked_func SUCCESS") + + # cleanup + del qkv, o, cu_seqlens + torch.cuda.empty_cache() + + + # Test 5: flash_attn_kvpacked_func + print("\n5. Testing flash_attn_kvpacked_func...") + clear_compile_cache() + + q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD) + kv = generate_bshd_kv_packed(BATCH, N_CTX_K, HK, D_HEAD) + + flash_attn_kvpacked_func_compiled = torch.compile(flash_attn_kvpacked_func) + o = flash_attn_kvpacked_func_compiled(q, kv, causal=True) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_kvpacked_func SUCCESS") + + # cleanup + del q, kv, o + torch.cuda.empty_cache() + + + # Test 6: flash_attn_varlen_kvpacked_func + print("\n6. Testing flash_attn_varlen_kvpacked_func...") + clear_compile_cache() + + q, cu_seqlens_q, max_seqlen_q = generate_varlen_tensor(BATCH * N_CTX_Q, HQ, D_HEAD, BATCH) + kv, cu_seqlens_k, max_seqlen_k = generate_varlen_kv_packed(BATCH * N_CTX_K, HK, D_HEAD, BATCH) + + flash_attn_varlen_kvpacked_func_compiled = torch.compile(flash_attn_varlen_kvpacked_func) + o = flash_attn_varlen_kvpacked_func_compiled( + q, kv, cu_seqlens_q, cu_seqlens_k, + max_seqlen_q, max_seqlen_k, causal=True + ) + print(f"Output shape: {o.shape}, dtype: {o.dtype}") + o.sum().backward() + print("✓ flash_attn_varlen_kvpacked_func SUCCESS") + + # cleanup + del q, kv, o, cu_seqlens_q, cu_seqlens_k + torch.cuda.empty_cache() + + + # Test 7: flash_attn_with_kvcache + print("\n7. Testing flash_attn_with_kvcache...") + clear_compile_cache() + + # setup cache dimensions + CACHE_SEQLEN = 1024 # max cache size + NEW_SEQLEN = 1 # for incremental decoding, usually 1 token at a time + + # create query for new tokens + q = generate_bshd_tensor(BATCH, NEW_SEQLEN, HQ, D_HEAD, dtype=torch.float16) + + # create kv cache using generators + k_cache = generate_bshd_tensor(BATCH, CACHE_SEQLEN, HK, D_HEAD, dtype=torch.float16) + v_cache = generate_bshd_tensor(BATCH, CACHE_SEQLEN, HK, D_HEAD, dtype=torch.float16) + + # cache sequence lengths + cache_seqlens = torch.full((BATCH,), 100, dtype=torch.int32, device='cuda') + + # new k,v to append to cache (optional) + k_new = generate_bshd_tensor(BATCH, NEW_SEQLEN, HK, D_HEAD, dtype=torch.float16) + v_new = generate_bshd_tensor(BATCH, NEW_SEQLEN, HK, D_HEAD, dtype=torch.float16) + + # Note: flash_attn_with_kvcache doesn't support backward pass + flash_attn_with_kvcache_compiled = torch.compile(flash_attn_with_kvcache) + + # Test with new k,v (append to cache and do attention) + with torch.no_grad(): + o = flash_attn_with_kvcache_compiled( + q, k_cache, v_cache, + k=k_new, v=v_new, + cache_seqlens=cache_seqlens, + causal=True + ) + print(f"Output shape (with new kv): {o.shape}, dtype: {o.dtype}") + + print("✓ flash_attn_with_kvcache SUCCESS") + + print("\n\n✅ ALL TESTS PASSED! ✅") + + except Exception as e: + print(f"\n❌ ERROR: {str(e)}") + # ensure we sync even on error to get proper error message + torch.cuda.synchronize() + raise e + finally: + # final cleanup + torch.cuda.empty_cache() + clear_compile_cache() diff --git a/flash_attn/flash_attn_triton_amd/utils.py b/flash_attn/flash_attn_triton_amd/utils.py index cc4f7fa624c..1795e0d1366 100644 --- a/flash_attn/flash_attn_triton_amd/utils.py +++ b/flash_attn/flash_attn_triton_amd/utils.py @@ -45,7 +45,7 @@ class MetaData(): cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None cache_batch_idx = None packing: Optional[bool] = None - return_scores: bool = False + return_softmax: bool = False dropout_p: float = 0.0 philox_seed: Optional[int] = None philox_offset : Optional[int]= None # if dropout_p > 0.0 seed the RNG so we get reproducible results for testing. @@ -72,7 +72,7 @@ def __repr__(self) -> str: f" cache_seqlens={self.cache_seqlens},\n" f" cache_batch_idx={self.cache_batch_idx},\n" f" dropout_p={self.dropout_p},\n" - f" return_scores={self.return_scores}\n" + f" return_softmax={self.return_softmax}\n" f")") def __init__(self, sm_scale=1.0): @@ -113,11 +113,10 @@ def need_rotary(self, sin, cos, rotary_interleaved, rotary_conjunction=False): self.rotary_interleaved = rotary_interleaved self.rotary_conjunction = rotary_conjunction - def need_dropout(self, dropout_p, return_scores = True): - if dropout_p > 0.0: - self.dropout_p = dropout_p - self.return_scores = return_scores - self.philox_seed, self.philox_offset = 0x1BF58, 0x1D4B49 + def need_dropout(self, dropout_p, return_softmax): + self.dropout_p = dropout_p + self.return_softmax = return_softmax + self.philox_seed, self.philox_offset = 0x1BF58, 0x1D4B49 def check_args(self, q, k, v, o): assert q.dim() == k.dim() and q.dim() == v.dim() @@ -130,7 +129,7 @@ def check_args(self, q, k, v, o): assert len(self.cu_seqlens_q) == len(self.cu_seqlens_k) # TODO: Remove once bias is supported with varlen assert self.bias is None - # assert not self.return_scores + # assert not self.return_softmax else: assert q.dim() == 4 assert self.max_seqlens_q > 0 and self.max_seqlens_k > 0 @@ -166,7 +165,7 @@ def generate_varlen_tensor( batch_size: Optional[int] = None, equal_seqlens: bool = False, device: str = "cuda", - dtype: torch.dtype = torch.float32, + dtype: torch.dtype = torch.float16, DEBUG_INPUT: bool = False ): if DEBUG: @@ -226,7 +225,7 @@ def generate_varlen_tensor( x.requires_grad_() return x, cu_seqlens, max_seqlen -def generate_bshd_tensor(BATCH, SEQ_LEN, NUM_HEADS, D_HEAD, dtype, device="cuda", DEBUG_INPUT=False): +def generate_bshd_tensor(BATCH, SEQ_LEN, NUM_HEADS, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): # save fp8 type is_fp8_dtype = is_dtype_fp8(dtype) if is_fp8_dtype: @@ -249,7 +248,7 @@ def generate_bshd_tensor(BATCH, SEQ_LEN, NUM_HEADS, D_HEAD, dtype, device="cuda" x.requires_grad_() return x -def generate_bhsd_tensor(BATCH, NUM_HEADS, SEQ_LEN, D_HEAD, dtype, device="cuda", DEBUG_INPUT=False): +def generate_bhsd_tensor(BATCH, NUM_HEADS, SEQ_LEN, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): # save fp8 type is_fp8_dtype = is_dtype_fp8(dtype) if is_fp8_dtype: @@ -273,6 +272,235 @@ def generate_bhsd_tensor(BATCH, NUM_HEADS, SEQ_LEN, D_HEAD, dtype, device="cuda" x.requires_grad_() return x +def generate_bshd_qkv_packed(BATCH, SEQ_LEN, NUM_HEADS, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): + """Generate QKV packed tensor with shape (BATCH, SEQ_LEN, 3, NUM_HEADS, D_HEAD)""" + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # gen tensor + tensor_shape = (BATCH, SEQ_LEN, 3, NUM_HEADS, D_HEAD) + if DEBUG_INPUT: + x = torch.arange(SEQ_LEN, dtype=dtype, device=device).view(1, SEQ_LEN, 1, 1, 1).expand(*tensor_shape).contiguous() + else: + x = torch.randn(tensor_shape, dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for QKV packing yet") + else: + x.requires_grad_() + return x + + +def generate_bshd_kv_packed(BATCH, SEQ_LEN, NUM_HEADS, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): + """Generate KV packed tensor with shape (BATCH, SEQ_LEN, 2, NUM_HEADS, D_HEAD)""" + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # gen tensor + tensor_shape = (BATCH, SEQ_LEN, 2, NUM_HEADS, D_HEAD) + if DEBUG_INPUT: + x = torch.arange(SEQ_LEN, dtype=dtype, device=device).view(1, SEQ_LEN, 1, 1, 1).expand(*tensor_shape).contiguous() + else: + x = torch.randn(tensor_shape, dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for KV packing yet") + else: + x.requires_grad_() + return x + + +def generate_bhsd_qkv_packed(BATCH, NUM_HEADS, SEQ_LEN, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): + """Generate QKV packed tensor with shape (BATCH, 3, NUM_HEADS, SEQ_LEN, D_HEAD)""" + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # gen tensor + tensor_shape = (BATCH, 3, NUM_HEADS, SEQ_LEN, D_HEAD) + if DEBUG_INPUT: + x = torch.arange(SEQ_LEN, dtype=dtype, device=device).view(1, 1, 1, SEQ_LEN, 1).expand(*tensor_shape).contiguous() + else: + x = torch.randn(tensor_shape, dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for QKV packing yet") + else: + x.requires_grad_() + return x + + +def generate_bhsd_kv_packed(BATCH, NUM_HEADS, SEQ_LEN, D_HEAD, dtype: torch.dtype = torch.float16, device="cuda", DEBUG_INPUT=False): + """Generate KV packed tensor with shape (BATCH, 2, NUM_HEADS, SEQ_LEN, D_HEAD)""" + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # gen tensor + tensor_shape = (BATCH, 2, NUM_HEADS, SEQ_LEN, D_HEAD) + if DEBUG_INPUT: + x = torch.arange(SEQ_LEN, dtype=dtype, device=device).view(1, 1, 1, SEQ_LEN, 1).expand(*tensor_shape).contiguous() + else: + x = torch.randn(tensor_shape, dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for KV packing yet") + else: + x.requires_grad_() + return x + + +def generate_varlen_qkv_packed( + total_seqlen: int, + num_heads: int, + head_size: int, + batch_size: Optional[int] = None, + equal_seqlens: bool = False, + device: str = "cuda", + dtype: torch.dtype = torch.float16, + DEBUG_INPUT: bool = False +): + """Generate varlen QKV packed tensor with shape (total_seqlen, 3, num_heads, head_size)""" + if DEBUG: + print("generate_varlen_qkv_packed") + print("total_seqlen", total_seqlen) + print("num_heads", num_heads) + print("head_size", head_size) + + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # get valid batch_size + if batch_size is None: + valid_batch_sizes = [bs for bs in [1, 2, 4, 8, 16, 32, 64] if bs <= total_seqlen] + batch_size = random.choice(valid_batch_sizes) + + # get seqlens + if equal_seqlens: + seqlens = torch.full( + (batch_size,), + total_seqlen // batch_size, + dtype=torch.int32, + device=device + ) + seqlens[-1] += total_seqlen % batch_size + else: + seqlens = random_seqlens_composition(total_seqlen, batch_size).to(device=device) + + # create cumulative sequence lengths + cu_seqlens = torch.cat([torch.tensor([0], dtype=torch.int32, device=device), seqlens.cumsum(dim=0)]).to(torch.int32).to(device=device) + max_seqlen = torch.max(seqlens).to(torch.int32).item() + + # create varlen qkv packed tensor + if DEBUG_INPUT: + x = torch.zeros(total_seqlen, 3, num_heads, head_size, dtype=dtype, device=device) + for i in range(batch_size): + start = cu_seqlens[i].item() + end = cu_seqlens[i+1].item() + length = end - start + + x[start:end, :, :, :] = ( + torch.arange(length, dtype=dtype, device=device) + .view(length, 1, 1, 1) + .expand(length, 3, num_heads, head_size) + ) + else: + x = torch.randn((total_seqlen, 3, num_heads, head_size), dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for QKV packing yet") + else: + x.requires_grad_() + return x, cu_seqlens, max_seqlen + + +def generate_varlen_kv_packed( + total_seqlen: int, + num_heads: int, + head_size: int, + batch_size: Optional[int] = None, + equal_seqlens: bool = False, + device: str = "cuda", + dtype: torch.dtype = torch.float16, + DEBUG_INPUT: bool = False +): + """Generate varlen KV packed tensor with shape (total_seqlen, 2, num_heads, head_size)""" + if DEBUG: + print("generate_varlen_kv_packed") + print("total_seqlen", total_seqlen) + print("num_heads", num_heads) + print("head_size", head_size) + + # save fp8 type + is_fp8_dtype = is_dtype_fp8(dtype) + if is_fp8_dtype: + og_fp8_dtype = dtype + dtype = torch.float32 + + # get valid batch_size + if batch_size is None: + valid_batch_sizes = [bs for bs in [1, 2, 4, 8, 16, 32, 64] if bs <= total_seqlen] + batch_size = random.choice(valid_batch_sizes) + + # get seqlens + if equal_seqlens: + seqlens = torch.full( + (batch_size,), + total_seqlen // batch_size, + dtype=torch.int32, + device=device + ) + seqlens[-1] += total_seqlen % batch_size + else: + seqlens = random_seqlens_composition(total_seqlen, batch_size).to(device=device) + + # create cumulative sequence lengths + cu_seqlens = torch.cat([torch.tensor([0], dtype=torch.int32, device=device), seqlens.cumsum(dim=0)]).to(torch.int32).to(device=device) + max_seqlen = torch.max(seqlens).to(torch.int32).item() + + # create varlen kv packed tensor + if DEBUG_INPUT: + x = torch.zeros(total_seqlen, 2, num_heads, head_size, dtype=dtype, device=device) + for i in range(batch_size): + start = cu_seqlens[i].item() + end = cu_seqlens[i+1].item() + length = end - start + + x[start:end, :, :, :] = ( + torch.arange(length, dtype=dtype, device=device) + .view(length, 1, 1, 1) + .expand(length, 2, num_heads, head_size) + ) + else: + x = torch.randn((total_seqlen, 2, num_heads, head_size), dtype=dtype, device=device) + + if is_fp8_dtype: + # cast to fp8 - need to handle the packed dimension + raise NotImplementedError("FP8 not supported for KV packing yet") + else: + x.requires_grad_() + return x, cu_seqlens, max_seqlen + +# Replace the existing input_helper function in utils.py with this updated version + def input_helper( BATCH: int, HQ: int, @@ -295,20 +523,42 @@ def input_helper( # set params TOTAL_SEQLENS_Q = BATCH * N_CTX_Q TOTAL_SEQLENS_K = BATCH * N_CTX_K - equal_seqlens=False + equal_seqlens = False - # gen tensors - # TODO: the gen functions should maybe have different gen modes like random, ones, increasing seqlen - if is_fp8_dtype: - q, cu_seqlens_q, max_seqlen_q, descale_q = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - k, cu_seqlens_k, max_seqlen_k, descale_k = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - v, _, _ , descale_v = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - do, _, _ , descale_do = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens) - else: - q, cu_seqlens_q, max_seqlen_q = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - k, cu_seqlens_k, max_seqlen_k = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - v, _, _ = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) - do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + # deal with packing + if packing is None: + # gen tensors + if is_fp8_dtype: + q, cu_seqlens_q, max_seqlen_q, descale_q = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + k, cu_seqlens_k, max_seqlen_k, descale_k = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + v, _, _, descale_v = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + do, _, _, descale_do = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens) + else: + q, cu_seqlens_q, max_seqlen_q = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + k, cu_seqlens_k, max_seqlen_k = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + v, _, _ = generate_varlen_tensor(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif packing == "kv": + # gen tensors with kv packing + if is_fp8_dtype: + raise ValueError("FP8 not supported for KV packing yet") + else: + q, cu_seqlens_q, max_seqlen_q = generate_varlen_tensor(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + kv, cu_seqlens_k, max_seqlen_k = generate_varlen_kv_packed(TOTAL_SEQLENS_K, HK, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif packing == "qkv": + # qkv packing - requires same sequence length for q and k + assert N_CTX_Q == N_CTX_K, "For QKV packing, Q and K must have same sequence length" + assert HQ == HK, "For QKV packing, Q and K must have same number of heads" + + if is_fp8_dtype: + raise ValueError("FP8 not supported for QKV packing yet") + else: + qkv, cu_seqlens_q, max_seqlen_q = generate_varlen_qkv_packed(TOTAL_SEQLENS_Q, HQ, D_HEAD, batch_size=BATCH, dtype=dtype, device=device, equal_seqlens=equal_seqlens, DEBUG_INPUT=DEBUG_INPUT) + cu_seqlens_k = cu_seqlens_q + max_seqlen_k = max_seqlen_q + # create dummy do for qkv case + do = torch.ones((TOTAL_SEQLENS_Q, HQ, D_HEAD), dtype=dtype, device=device) if DEBUG_INPUT else torch.randn((TOTAL_SEQLENS_Q, HQ, D_HEAD), dtype=dtype, device=device) # setup metadata if DEBUG_INPUT: @@ -318,31 +568,61 @@ def input_helper( metadata = MetaData(sm_scale=sm_scale) metadata.set_varlen_params(cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k) metadata.need_causal(CAUSAL) - metadata.need_dropout(DROPOUT_P) + metadata.need_dropout(DROPOUT_P, True) + elif layout == 'bshd' or layout == "bhsd": - # gen tensors - if layout == "bshd": + # deal with packing + if packing is None: + # gen tensors + if layout == "bshd": + if is_fp8_dtype: + q, descale_q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + k, descale_k = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + v, descale_v = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do, descale_do = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device) + else: + q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + k = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + v = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif layout == "bhsd": + if is_fp8_dtype: + q, descale_q = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + k, descale_k = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + v, descale_v = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do, descale_do = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device) + else: + q = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + k = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + v = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif packing == "kv": + # gen tensors with kv packing if is_fp8_dtype: - q, descale_q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - k, descale_k = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - v, descale_v = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - do, descale_do = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device) + raise ValueError("FP8 not supported for KV packing yet") else: - q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - k = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - v = generate_bshd_tensor(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) - elif layout == "bhsd": + if layout == "bshd": + q = generate_bshd_tensor(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + kv = generate_bshd_kv_packed(BATCH, N_CTX_K, HK, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif layout == "bhsd": + q = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + kv = generate_bhsd_kv_packed(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + elif packing == "qkv": + # qkv packing - requires same sequence length for q and k + assert N_CTX_Q == N_CTX_K, "For QKV packing, Q and K must have same sequence length" + assert HQ == HK, "For QKV packing, Q and K must have same number of heads" + if is_fp8_dtype: - q, descale_q = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - k, descale_k = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - v, descale_v = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - do, descale_do = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device) + raise ValueError("FP8 not supported for QKV packing yet") else: - q = generate_bhsd_tensor(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - k = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - v = generate_bhsd_tensor(BATCH, HK, N_CTX_K, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) - do = torch.ones_like(q) if DEBUG_INPUT else torch.randn_like(q) + if layout == "bshd": + qkv = generate_bshd_qkv_packed(BATCH, N_CTX_Q, HQ, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones((BATCH, N_CTX_Q, HQ, D_HEAD), dtype=dtype, device=device) if DEBUG_INPUT else torch.randn((BATCH, N_CTX_Q, HQ, D_HEAD), dtype=dtype, device=device) + elif layout == "bhsd": + qkv = generate_bhsd_qkv_packed(BATCH, HQ, N_CTX_Q, D_HEAD, dtype=dtype, device=device, DEBUG_INPUT=DEBUG_INPUT) + do = torch.ones((BATCH, HQ, N_CTX_Q, D_HEAD), dtype=dtype, device=device) if DEBUG_INPUT else torch.randn((BATCH, HQ, N_CTX_Q, D_HEAD), dtype=dtype, device=device) # setup metadata if DEBUG_INPUT: @@ -354,42 +634,22 @@ def input_helper( metadata.max_seqlens_k = N_CTX_K metadata.layout = layout metadata.need_causal(CAUSAL) - metadata.need_dropout(DROPOUT_P) + metadata.need_dropout(DROPOUT_P, True) else: raise ValueError(f"Unknown layout: {layout}") - # deal with packing + # return based on packing if packing is None: if is_fp8_dtype: return (q, descale_q), (k, descale_k), (v, descale_v), (do, descale_do), metadata else: return q, k, v, do, metadata elif packing == "kv": - # pack k and v - if layout in ["bhsd", "thd"]: - kv = torch.stack([k, v], dim=1) - elif layout == "bshd": - kv = torch.stack([k, v], dim=2) - else: - raise ValueError(f"Unknown layout: {layout}") - if is_fp8_dtype: raise ValueError("FP8 not supported kv packing yet") else: return q, kv, do, metadata elif packing == "qkv": - # qkv packing - requires same sequence length for q and k - assert N_CTX_Q == N_CTX_K, "For QKV packing, Q and K must have same sequence length" - assert HQ == HK, "For QKV packing, Q and K must have same number of heads" - - # pack q, k, and v - if layout in ["bhsd", "thd"]: - qkv = torch.stack([q, k, v], dim=1) - elif layout == "bshd": - qkv = torch.stack([q, k, v], dim=2) - else: - raise ValueError(f"Unknown layout: {layout}") - if is_fp8_dtype: raise ValueError("FP8 not supported qkv packing yet") else: @@ -694,6 +954,9 @@ def compute_alibi_tensor_ref(alibi_slopes, seqlen_q, seqlen_k): relative_pos = torch.abs(q_idx + seqlen_k - seqlen_q - k_idx) # (N_CTX_Q, N_CTX_K) return -1 * alibi_slopes.unsqueeze(-1).unsqueeze(-1) * relative_pos # (Z, H, N_CTX_Q, N_CTX_K) +def round_multiple(x, m): + return (x + m - 1) // m * m + # ------------------------------- # Dropouts # -------------------------------