Run this model with HF Jobs
Run replicate/sage_attention on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/replicate/sage_attention. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/replicate/sage_attention on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use replicate/sage_attention with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/sage_attention with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/sage_attention", version=1) - Notebooks
- Google Colab
- Kaggle
Update Builds
Browse filesThis view is limited to 50 files because it contains too many changes. Β See raw diff
Files changed (50)
- build.toml+107-78
- build/torch28-cxx11-cu126-x86_64-linux/__init__.pyAdded+11
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu126-x86_64-linux/_ops.pyRenamed+3-3
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu126-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu126-x86_64-linux/core.pyRenamed+1-1
- build/torch28-cxx11-cu126-x86_64-linux/metadata.jsonAdded+1
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu126-x86_64-linux/quant.pyRenamed
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu126-x86_64-linux/quant_per_thread.pyRenamed
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__init__.py+24-9
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
- build/torch28-cxx11-cu128-x86_64-linux/__init__.pyAdded+11
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu128-x86_64-linux/_ops.pyRenamed+3-3
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu128-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
- build/torch28-cxx11-cu126-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu128-x86_64-linux/core.pyRenamed+1-1
- build/torch28-cxx11-cu128-x86_64-linux/metadata.jsonAdded+1
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu128-x86_64-linux/quant.pyRenamed
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu128-x86_64-linux/quant_per_thread.pyRenamed
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__init__.py+24-9
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
- build/torch28-cxx11-cu129-x86_64-linux/__init__.pyAdded+11
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu129-x86_64-linux/_ops.pyRenamed+3-3
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu129-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
- build/torch28-cxx11-cu128-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu129-x86_64-linux/core.pyRenamed+1-1
- build/torch28-cxx11-cu129-x86_64-linux/metadata.jsonAdded+1
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu129-x86_64-linux/quant.pyRenamed
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu129-x86_64-linux/quant_per_thread.pyRenamed
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__init__.py+24-9
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
- build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
- build/torch29-cxx11-cu126-x86_64-linux/__init__.pyAdded+11
- build/torch29-cxx11-cu126-x86_64-linux/_ops.pyAdded+9
- build/torch29-cxx11-cu126-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soAdded+3
- build/torch29-cxx11-cu126-x86_64-linux/core.pyAdded+969
- build/torch29-cxx11-cu126-x86_64-linux/metadata.jsonAdded+1
- build/torch29-cxx11-cu126-x86_64-linux/quant.pyAdded+326
- build/torch29-cxx11-cu126-x86_64-linux/quant_per_thread.pyAdded+204
- build/torch29-cxx11-cu126-x86_64-linux/sage_attention/__init__.pyAdded+26
- build/torch29-cxx11-cu128-x86_64-linux/__init__.pyAdded+11
- build/torch29-cxx11-cu128-x86_64-linux/_ops.pyAdded+9
123456789101112131415161718192021222324252627282930313233342 unmodified lines3738394041424344454647484950515253545556575859606162636465666768697071727374757677785 unmodified lines8485868788899091922 unmodified lines95969798991001011021031041051061071081091101111121131141151161171182 unmodified lines1211221231241251261271281291301311321331341351361371381391401411422 unmodified lines145146147148149150[general]name = "sage_attention"universal = falsecuda-minver = "12.0"[torch]src = ["torch-ext/torch_binding.cpp","torch-ext/torch_binding.h",][kernel._qattn]depends = ["torch"]backend = "cuda"cuda-minver = "12.0"cuda-capabilities = ["8.0", "8.9", "9.0a"]src = ["sage_attention/cp_async.cuh","sage_attention/dispatch_utils.h","sage_attention/math.cuh","sage_attention/mma.cuh","sage_attention/numeric_conversion.cuh","sage_attention/permuted_smem.cuh","sage_attention/reduction_utils.cuh","sage_attention/wgmma.cuh","sage_attention/utils.cuh","sage_attention/cuda_tensormap_shim.cuh",]cxx-flags = ["-g", "-O3", "-fopenmp", "-lgomp", "-std=c++17", "-DENABLE_BF16"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174", # suppress the specific warning][kernel._qattn_sm80]depends = ["torch"]backend = "cuda"cuda-minver = "12.0"cuda-capabilities = ["8.0"]include = ["."]src = ["sage_attention/qattn/qk_int_sv_f16_cuda_sm80.cu","sage_attention/qattn/attn_cuda_sm80.h","sage_attention/qattn/attn_utils.cuh"]cxx-flags = ["-g", "-O3", "-fopenmp", "-lgomp", "-std=c++17", "-DENABLE_BF16"]cuda-flags = ["-O3","-std=c++17","-U__CUDA_NO_HALF_OPERATORS__","-U__CUDA_NO_HALF_CONVERSIONS__","--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174", # suppress the specific warning][kernel._qattn_sm89]depends = ["torch"]backend = "cuda"cuda-minver = "12.0"cuda-capabilities = ["8.9",]include = ["."]src = ["sage_attention/qattn/sm89_qk_int8_sv_f8_accum_f32_attn_inst_buf.cu",5 unmodified lines"sage_attention/qattn/sm89_qk_int8_sv_f8_accum_f16_fuse_v_scale_attn_inst_buf.cu","sage_attention/qattn/attn_cuda_sm89.h","sage_attention/qattn/qk_int_sv_f8_cuda_sm89.cuh","sage_attention/qattn/attn_utils.cuh"]cxx-flags = ["-g", "-O3", "-fopenmp", "-lgomp", "-std=c++17", "-DENABLE_BF16"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174", # suppress the specific warning][kernel._qattn_sm90]depends = ["torch"]backend = "cuda"cuda-minver = "12.0"cuda-capabilities = ["9.0a",]include = ["."]src = ["sage_attention/qattn/qk_int_sv_f8_cuda_sm90.cu","sage_attention/qattn/attn_cuda_sm90.h","sage_attention/qattn/attn_utils.cuh"]cxx-flags = ["-g", "-O3", "-fopenmp", "-lgomp", "-std=c++17", "-DENABLE_BF16"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174", # suppress the specific warning][kernel._fused]depends = ["torch"]backend = "cuda"cuda-minver = "12.0"cuda-capabilities = ["8.0", "8.9", "9.0a",]include = ["."]src = ["sage_attention/fused/fused.cu","sage_attention/fused/fused.h"]cxx-flags = ["-g", "-O3", "-fopenmp", "-lgomp", "-std=c++17", "-DENABLE_BF16"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174", # suppress the specific warning]123456789101112131415162 unmodified lines1920212223242526272829303132333435365 unmodified lines4243444546474849505152532 unmodified lines56575859606162636465666768697071727374757677787980818283848586872 unmodified lines909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561572 unmodified lines160161162163164165166167168169170171172173174175176177178179[general]name = "sage_attention"backends = ["cuda"][torch]src = ["torch-ext/torch_binding.cpp","torch-ext/torch_binding.h",][kernel._qattn_sm89]backend = "cuda"cuda-capabilities = ["8.9"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174",]cuda-minver = "12.6"cxx-flags = ["-g","-O3","-fopenmp","-lgomp","-std=c++17","-DENABLE_BF16",]depends = ["torch"]include = ["."]src = ["sage_attention/qattn/sm89_qk_int8_sv_f8_accum_f32_attn_inst_buf.cu",5 unmodified lines"sage_attention/qattn/sm89_qk_int8_sv_f8_accum_f16_fuse_v_scale_attn_inst_buf.cu","sage_attention/qattn/attn_cuda_sm89.h","sage_attention/qattn/qk_int_sv_f8_cuda_sm89.cuh","sage_attention/qattn/attn_utils.cuh",][kernel._qattn_sm90]backend = "cuda"cuda-capabilities = ["9.0a"]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174",]cuda-minver = "12.6"cxx-flags = ["-g","-O3","-fopenmp","-lgomp","-std=c++17","-DENABLE_BF16",]depends = ["torch"]include = ["."]src = ["sage_attention/qattn/qk_int_sv_f8_cuda_sm90.cu","sage_attention/qattn/attn_cuda_sm90.h","sage_attention/qattn/attn_utils.cuh",][kernel._qattn]backend = "cuda"cuda-capabilities = ["8.0","8.9","9.0a",]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174",]cuda-minver = "12.6"cxx-flags = ["-g","-O3","-fopenmp","-lgomp","-std=c++17","-DENABLE_BF16",]depends = ["torch"]src = ["sage_attention/cp_async.cuh","sage_attention/dispatch_utils.h","sage_attention/math.cuh","sage_attention/mma.cuh","sage_attention/numeric_conversion.cuh","sage_attention/permuted_smem.cuh","sage_attention/reduction_utils.cuh","sage_attention/wgmma.cuh","sage_attention/utils.cuh","sage_attention/cuda_tensormap_shim.cuh",][kernel._qattn_sm80]backend = "cuda"cuda-capabilities = ["8.0"]cuda-flags = ["-O3","-std=c++17","-U__CUDA_NO_HALF_OPERATORS__","-U__CUDA_NO_HALF_CONVERSIONS__","--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174",]cuda-minver = "12.6"cxx-flags = ["-g","-O3","-fopenmp","-lgomp","-std=c++17","-DENABLE_BF16",]depends = ["torch"]include = ["."]src = ["sage_attention/qattn/qk_int_sv_f16_cuda_sm80.cu","sage_attention/qattn/attn_cuda_sm80.h","sage_attention/qattn/attn_utils.cuh",][kernel._fused]backend = "cuda"cuda-capabilities = ["8.0","8.9","9.0a",]cuda-flags = ["-O3","-std=c++17",2 unmodified lines"--use_fast_math","--threads=1","-Xptxas=-v","-diag-suppress=174",]cuda-minver = "12.6"cxx-flags = ["-g","-O3","-fopenmp","-lgomp","-std=c++17","-DENABLE_BF16",]depends = ["torch"]include = ["."]src = ["sage_attention/fused/fused.cu","sage_attention/fused/fused.h",]
1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn
__all__ = [ "per_block_int8", "per_warp_int8", "sub_mean", "per_channel_fp8", "sageattn",]123456789import torchfrom . import _sage_attention_1369690_dirtyops = torch.ops._sage_attention_1369690_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_1369690_dirty::{op_name}"123456789import torchfrom . import _sage_attention_c08f355_dirtyops = torch.ops._sage_attention_c08f355_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_c08f355_dirty::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:636461b53d3b3c4c1cd1940bc6ecb32728cb0f80bb347cf52591afd0ea121c8csize 26037392123version https://git-lfs.github.com/spec/v1oid sha256:5797f12cc80b2abf6b4bc12ede219be759ae9264452ebabcd8617be7111e4550size 26042464
115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""print("########tensor_layout", tensor_layout)arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(
1{"python-depends":[]}1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn__all__ = ["per_block_int8","per_warp_int8","sub_mean","per_channel_fp8","sageattn",]1234567891011121314151617181920212223242526import ctypesimport sysimport importlibfrom pathlib import Pathfrom types import ModuleTypedef _import_from_path(file_path: Path) -> ModuleType:# We cannot use the module name as-is, after adding it to `sys.modules`,# it would also be used for other imports. So, we make a module name that# depends on the path for it to be unique using the hex-encoded hash of# the path.path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)module_name = path_hashspec = importlib.util.spec_from_file_location(module_name, file_path)if spec is None:raise ImportError(f"Cannot load spec for {module_name} from {file_path}")module = importlib.util.module_from_spec(spec)if module is None:raise ImportError(f"Cannot load module {module_name} from spec")sys.modules[module_name] = modulespec.loader.exec_module(module) # type: ignorereturn moduleglobals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn
__all__ = [ "per_block_int8", "per_warp_int8", "sub_mean", "per_channel_fp8", "sageattn",]123456789import torchfrom . import _sage_attention_1369690_dirtyops = torch.ops._sage_attention_1369690_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_1369690_dirty::{op_name}"123456789import torchfrom . import _sage_attention_c08f355_dirtyops = torch.ops._sage_attention_c08f355_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_c08f355_dirty::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:2bfc9cab8f63665571d07c111a94edb3bec9a17aba7721a4c67be5392db0841dsize 26553920123version https://git-lfs.github.com/spec/v1oid sha256:f5f9b9a3becda02514dcb9ed79c5ea2a157cde2b6e63e21be55aa0bc52383892size 26554912
115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""print("########tensor_layout", tensor_layout)arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(
1{"python-depends":[]}1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn__all__ = ["per_block_int8","per_warp_int8","sub_mean","per_channel_fp8","sageattn",]1234567891011121314151617181920212223242526import ctypesimport sysimport importlibfrom pathlib import Pathfrom types import ModuleTypedef _import_from_path(file_path: Path) -> ModuleType:# We cannot use the module name as-is, after adding it to `sys.modules`,# it would also be used for other imports. So, we make a module name that# depends on the path for it to be unique using the hex-encoded hash of# the path.path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)module_name = path_hashspec = importlib.util.spec_from_file_location(module_name, file_path)if spec is None:raise ImportError(f"Cannot load spec for {module_name} from {file_path}")module = importlib.util.module_from_spec(spec)if module is None:raise ImportError(f"Cannot load module {module_name} from spec")sys.modules[module_name] = modulespec.loader.exec_module(module) # type: ignorereturn moduleglobals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn
__all__ = [ "per_block_int8", "per_warp_int8", "sub_mean", "per_channel_fp8", "sageattn",]123456789import torchfrom . import _sage_attention_1369690_dirtyops = torch.ops._sage_attention_1369690_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_1369690_dirty::{op_name}"123456789import torchfrom . import _sage_attention_c08f355_dirtyops = torch.ops._sage_attention_c08f355_dirtydef add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_sage_attention_c08f355_dirty::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:fef61629b7537ad41d31cf5715a11a38ce3f7cc97b0d5bf26356492b36ad5c29size 26608048123version https://git-lfs.github.com/spec/v1oid sha256:e2eee18f99d9a6aa6d9179fbdd539c8653eba9a3692f35ad52a6fa07f99059f1size 26617232
115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(115 unmodified lines116117118119120121122115 unmodified lines- The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16``- All tensors must be on the same cuda device."""print("########tensor_layout", tensor_layout)arch = get_cuda_arch_versions()[q.device.index]if arch == "sm80":return sageattn_qk_int8_pv_fp16_cuda(
1{"python-depends":[]}1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn__all__ = ["per_block_int8","per_warp_int8","sub_mean","per_channel_fp8","sageattn",]1234567891011121314151617181920212223242526import ctypesimport sysimport importlibfrom pathlib import Pathfrom types import ModuleTypedef _import_from_path(file_path: Path) -> ModuleType:# We cannot use the module name as-is, after adding it to `sys.modules`,# it would also be used for other imports. So, we make a module name that# depends on the path for it to be unique using the hex-encoded hash of# the path.path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value)module_name = path_hashspec = importlib.util.spec_from_file_location(module_name, file_path)if spec is None:raise ImportError(f"Cannot load spec for {module_name} from {file_path}")module = importlib.util.module_from_spec(spec)if module is None:raise ImportError(f"Cannot load module {module_name} from spec")sys.modules[module_name] = modulespec.loader.exec_module(module) # type: ignorereturn moduleglobals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))
1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn
__all__ = [ "per_block_int8", "per_warp_int8", "sub_mean", "per_channel_fp8", "sageattn",]123456789import torchfrom . import _sage_attention_c08f355_dirtyops = torch.ops._sage_attention_c08f355_dirty
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_sage_attention_c08f355_dirty::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:8c8eeda7b9569b9b3d961438b0dee1920deb2324888ae2e15e27251f989ad4e8size 26046912123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969"""Copyright (c) 2024 by SageAttention team.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License."""
import torchimport torch.nn.functional as F
from ._ops import ops
from .quant import per_warp_int8 as per_warp_int8_cudafrom .quant import sub_meanfrom .quant import per_channel_fp8from .quant_per_thread import per_thread_int8 as per_thread_int8_triton
from typing import Any, List, Literal, Optional, Tuple, Unionimport warnings
import subprocessimport re
def get_cuda_version(): try: output = subprocess.check_output(["nvcc", "--version"]).decode() match = re.search(r"release (\d+)\.(\d+)", output) if match: major, minor = int(match.group(1)), int(match.group(2)) return major, minor except Exception as e: print("Failed to get CUDA version:", e) return None, None
def get_cuda_arch_versions(): cuda_archs = [] for i in range(torch.cuda.device_count()): major, minor = torch.cuda.get_device_capability(i) cuda_archs.append(f"sm{major}{minor}") return cuda_archs
def sageattn( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, tensor_layout: str = "HND", is_causal: bool = False, sm_scale: Optional[float] = None, return_lse: bool = False, **kwargs: Any,): """ Automatically selects the appropriate implementation of the SageAttention kernel based on the GPU compute capability.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
v : torch.Tensor The value tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
is_causal : bool Whether to apply causal mask to the attention matrix. Only applicable when qo_len == kv_len. Default: False.
sm_scale : Optional[float] The scale used in softmax, if not provided, will be set to ``1.0 / sqrt(head_dim)``.
return_lse : bool Whether to return the log sum of the exponentiated attention weights. Used for cases like Ring Attention. Default: False.
Returns ------- torch.Tensor The output tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
torch.Tensor The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). Shape: ``[batch_size, num_qo_heads, qo_len]``. Only returned if `return_lse` is True.
Note ---- - ``num_qo_heads`` must be divisible by ``num_kv_heads``. - The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - All tensors must be on the same cuda device. """ print("########tensor_layout", tensor_layout) arch = get_cuda_arch_versions()[q.device.index] if arch == "sm80": return sageattn_qk_int8_pv_fp16_cuda( q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, sm_scale=sm_scale, return_lse=return_lse, pv_accum_dtype="fp32", ) elif arch == "sm89": return sageattn_qk_int8_pv_fp8_cuda( q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, sm_scale=sm_scale, return_lse=return_lse, pv_accum_dtype="fp32+fp16", ) elif arch == "sm90": return sageattn_qk_int8_pv_fp8_cuda_sm90( q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, sm_scale=sm_scale, return_lse=return_lse, pv_accum_dtype="fp32+fp32", ) elif arch == "sm120": return sageattn_qk_int8_pv_fp8_cuda( q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, qk_quant_gran="per_warp", sm_scale=sm_scale, return_lse=return_lse, pv_accum_dtype="fp32+fp16", ) # sm120 has accurate fp32 accumulator for fp8 mma and triton kernel is currently not usable on sm120. else: raise ValueError(f"Unsupported CUDA architecture: {arch}")
@torch.compiler.disabledef sageattn_qk_int8_pv_fp16_cuda( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, tensor_layout: str = "HND", is_causal: bool = False, qk_quant_gran: str = "per_thread", sm_scale: Optional[float] = None, pv_accum_dtype: str = "fp32", smooth_k: bool = True, smooth_v: bool = False, return_lse: bool = False, **kwargs: Any,) -> torch.Tensor: """ SageAttention with INT8 quantization for Q and K, FP16 PV with FP16/FP32 accumulation, implemented using CUDA.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
v : torch.Tensor The value tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
is_causal : bool Whether to apply causal mask to the attention matrix. Only applicable when qo_len == kv_len. Default: False.
qk_quant_gran : str The granularity of quantization for Q and K, either "per_warp" or "per_thread". Default: "per_thread".
sm_scale : Optional[float] The scale used in softmax, if not provided, will be set to ``1.0 / sqrt(head_dim)``.
pv_accum_dtype : str The dtype of the accumulation of the product of the value tensor and the attention weights, either "fp16", "fp16+fp32" or "fp32". - "fp16": PV accumulation is done in fully in FP16. This is the fastest option but may lead to numerical instability. `smooth_v` option will increase the accuracy in cases when the value tensor has a large bias (like in CogVideoX-2b). - "fp32": PV accumulation is done in FP32. This is the most accurate option but may be slower than "fp16" due to CUDA core overhead. - "fp16+fp32": PV accumulation is done in FP16, but added to a FP32 buffer every few iterations. This offers a balance between speed and accuracy. Default: "fp32".
smooth_k : bool Whether to smooth the key tensor by subtracting the mean along the sequence dimension. Default: True.
smooth_v : bool Whether to smooth the value tensor by subtracting the mean along the sequence dimension. smooth_v will be ignored if pv_accum_dtype is "fp32" or "fp16+fp32". Default: False.
return_lse : bool Whether to return the log sum of the exponentiated attention weights. Used for cases like Ring Attention. Default: False.
Returns ------- torch.Tensor The output tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
torch.Tensor The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). Shape: ``[batch_size, num_qo_heads, qo_len]``. Only returned if `return_lse` is True.
Note ---- - ``num_qo_heads`` must be divisible by ``num_kv_heads``. - The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - All tensors must be on the same cuda device. - `smooth_k` will introduce slight overhead but will improve the accuracy under most circumstances. """
dtype = q.dtype assert q.is_cuda, "Input tensors must be on cuda." assert dtype in [torch.float16, torch.bfloat16], ( "Input tensors must be in dtype of torch.float16 or torch.bfloat16" ) assert qk_quant_gran in ["per_warp", "per_thread"], ( "qk_quant_gran must be either 'per_warp' or 'per_thread'." ) assert q.device == k.device == v.device, "All tensors must be on the same device." assert q.dtype == k.dtype == v.dtype, "All tensors must have the same dtype."
# FIXME(DefTruth): make sage attention work compatible with distributed # env, for example, xDiT which launch by torchrun. Without this workaround, # sage attention will run into illegal memory access error after first # inference step in distributed env for multi gpus inference. This small # workaround also make sage attention work compatible with torch.compile # through non-fullgraph compile mode. torch.cuda.set_device(v.device)
_tensor_layout = 0 if tensor_layout == "NHD" else 1 _is_caual = 1 if is_causal else 0 _qk_quant_gran = 3 if qk_quant_gran == "per_thread" else 2 _return_lse = 1 if return_lse else 0
head_dim_og = q.size(-1)
if head_dim_og < 64: q = torch.nn.functional.pad(q, (0, 64 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 64 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 64 - head_dim_og)) elif head_dim_og > 64 and head_dim_og < 128: q = torch.nn.functional.pad(q, (0, 128 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 128 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 128 - head_dim_og)) elif head_dim_og > 128: raise ValueError(f"Unsupported head_dim: {head_dim_og}")
# assert last dim is contiguous assert q.stride(-1) == 1 and k.stride(-1) == 1 and v.stride(-1) == 1, ( "Last dim of qkv must be contiguous." )
if sm_scale is None: sm_scale = head_dim_og**-0.5
seq_dim = 1 if _tensor_layout == 0 else 2 nh_dim = 2 if _tensor_layout == 0 else 1
if smooth_k: km = k.mean(dim=seq_dim, keepdim=True) nqheads = q.size(2) nkheads = k.size(2) q_per_kv_heads = nqheads // nkheads if q_per_kv_heads > 1: # nheads_k => nheads_q km_broadcast = torch.repeat_interleave(km, q_per_kv_heads, dim=nh_dim) else: km_broadcast = km if return_lse: if tensor_layout == "NHD": lse_correction = ( torch.matmul( q.transpose(1, 2), km_broadcast.transpose(1, 2).transpose(2, 3) ) .squeeze(-1) .to(torch.float32) ) else: lse_correction = ( torch.matmul(q, km_broadcast.transpose(2, 3)) .squeeze(-1) .to(torch.float32) ) else: km = None
if qk_quant_gran == "per_warp": q_int8, q_scale, k_int8, k_scale = per_warp_int8_cuda( q, k, km, tensor_layout=tensor_layout, BLKQ=128, WARPQ=(16 if (q.size(-1) == 128 and pv_accum_dtype == "fp16+fp32") else 32), BLKK=64, ) elif qk_quant_gran == "per_thread": q_int8, q_scale, k_int8, k_scale = per_thread_int8_triton( q, k, km, tensor_layout=tensor_layout, BLKQ=128, WARPQ=(16 if (q.size(-1) == 128 and pv_accum_dtype == "fp16+fp32") else 32), BLKK=64, WARPK=64, )
o = torch.empty(q.size(), dtype=dtype, device=q.device)
if pv_accum_dtype in ["fp32", "fp16+fp32"] and smooth_v: warnings.warn(f"pv_accum_dtype is {pv_accum_dtype}, smooth_v will be ignored.") smooth_v = False
if pv_accum_dtype == "fp32": v = v.to(torch.float16) lse = ops.qk_int8_sv_f16_accum_f32_attn( q_int8, k_int8, v, o, q_scale, k_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) elif pv_accum_dtype == "fp16": if smooth_v: smoothed_v, vm = sub_mean(v, tensor_layout=tensor_layout) lse = ops.qk_int8_sv_f16_accum_f16_fuse_v_mean_attn( q_int8, k_int8, smoothed_v, o, q_scale, k_scale, vm, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) else: v = v.to(torch.float16) lse = ops.qk_int8_sv_f16_accum_f16_attn( q_int8, k_int8, v, o, q_scale, k_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) elif pv_accum_dtype == "fp16+fp32": v = v.to(torch.float16) lse = ops.qk_int8_sv_f16_accum_f16_attn_inst_buf( q_int8, k_int8, v, o, q_scale, k_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) else: raise ValueError(f"Unsupported pv_accum_dtype: {pv_accum_dtype}")
o = o[..., :head_dim_og]
if return_lse: return ( o, lse / 1.44269504 + lse_correction * sm_scale if smooth_k else lse / 1.44269504, ) else: return o
@torch.compiler.disabledef sageattn_qk_int8_pv_fp8_cuda( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, tensor_layout: str = "HND", is_causal: bool = False, qk_quant_gran: str = "per_thread", sm_scale: Optional[float] = None, pv_accum_dtype: str = "fp32+fp16", smooth_k: bool = True, smooth_v: bool = False, return_lse: bool = False, **kwargs: Any,) -> torch.Tensor: """ SageAttention with INT8 quantization for Q and K, FP8 PV with FP32 accumulation, implemented using CUDA.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
v : torch.Tensor The value tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
is_causal : bool Whether to apply causal mask to the attention matrix. Only applicable when qo_len == kv_len. Default: False.
qk_quant_gran : str The granularity of quantization for Q and K, either "per_warp" or "per_thread". Default: "per_thread".
sm_scale : Optional[float] The scale used in softmax, if not provided, will be set to ``1.0 / sqrt(head_dim)``.
pv_accum_dtype : str The dtype of the accumulation of the product of the value tensor and the attention weights, either "fp32" or "fp32+fp32". - "fp32": PV accumulation is done in fully in FP32. However, due to the hardware issue, there are only 22 valid bits in the FP32 accumulator. - "fp32+fp32": PV accumulation is done in FP32 (actually FP22), but added to a FP32 buffer every few iterations. This offers a balance between speed and accuracy. Default: "fp32+fp32".
smooth_k : bool Whether to smooth the key tensor by subtracting the mean along the sequence dimension. Default: True.
smooth_v : bool Whether to smooth the value tensor by subtracting the mean along the sequence dimension. smooth_v will be ignored if pv_accum_dtype is "fp32+fp32". Default: False.
return_lse : bool Whether to return the log sum of the exponentiated attention weights. Used for cases like Ring Attention. Default: False.
Returns ------- torch.Tensor The output tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
torch.Tensor The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). Shape: ``[batch_size, num_qo_heads, qo_len]``. Only returned if `return_lse` is True.
Note ---- - ``num_qo_heads`` must be divisible by ``num_kv_heads``. - The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - All tensors must be on the same cuda device. - `smooth_k` will introduce slight overhead but will improve the accuracy under most circumstances. """
dtype = q.dtype assert q.is_cuda, "Input tensors must be on cuda." assert dtype in [torch.float16, torch.bfloat16], ( "Input tensors must be in dtype of torch.float16 or torch.bfloat16" ) assert qk_quant_gran in ["per_warp", "per_thread"], ( "qk_quant_gran must be either 'per_warp' or 'per_thread'." ) assert q.device == k.device == v.device, "All tensors must be on the same device." assert q.dtype == k.dtype == v.dtype, "All tensors must have the same dtype."
# cuda_major_version, cuda_minor_version = get_cuda_version() # if(cuda_major_version, cuda_minor_version) < (12, 8) and pv_accum_dtype == 'fp32+fp16': # warnings.warn("cuda version < 12.8, change pv_accum_dtype to 'fp32+fp32'") # pv_accum_dtype = 'fp32+fp32'
# FIXME(DefTruth): make sage attention work compatible with distributed # env, for example, xDiT which launch by torchrun. Without this workaround, # sage attention will run into illegal memory access error after first # inference step in distributed env for multi gpus inference. This small # workaround also make sage attention work compatible with torch.compile # through non-fullgraph compile mode. torch.cuda.set_device(v.device)
_tensor_layout = 0 if tensor_layout == "NHD" else 1 _is_caual = 1 if is_causal else 0 _qk_quant_gran = 3 if qk_quant_gran == "per_thread" else 2 _return_lse = 1 if return_lse else 0
head_dim_og = q.size(-1)
if head_dim_og < 64: q = torch.nn.functional.pad(q, (0, 64 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 64 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 64 - head_dim_og)) elif head_dim_og > 64 and head_dim_og < 128: q = torch.nn.functional.pad(q, (0, 128 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 128 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 128 - head_dim_og)) elif head_dim_og > 128: raise ValueError(f"Unsupported head_dim: {head_dim_og}")
# assert last dim is contiguous assert q.stride(-1) == 1 and k.stride(-1) == 1 and v.stride(-1) == 1, ( "Last dim of qkv must be contiguous." )
if sm_scale is None: sm_scale = head_dim_og**-0.5
seq_dim = 1 if _tensor_layout == 0 else 2 nh_dim = 2 if _tensor_layout == 0 else 1
if smooth_k: km = k.mean(dim=seq_dim, keepdim=True) nqheads = q.size(2) nkheads = k.size(2) q_per_kv_heads = nqheads // nkheads if q_per_kv_heads > 1: # nheads_k => nheads_q km_broadcast = torch.repeat_interleave(km, q_per_kv_heads, dim=nh_dim) else: km_broadcast = km if return_lse: if tensor_layout == "NHD": lse_correction = ( torch.matmul( q.transpose(1, 2), km_broadcast.transpose(1, 2).transpose(2, 3) ) .squeeze(-1) .to(torch.float32) ) else: lse_correction = ( torch.matmul(q, km_broadcast.transpose(2, 3)) .squeeze(-1) .to(torch.float32) ) else: km = None
if qk_quant_gran == "per_warp": q_int8, q_scale, k_int8, k_scale = per_warp_int8_cuda( q, k, km, tensor_layout=tensor_layout, BLKQ=128, WARPQ=32, BLKK=64 ) elif qk_quant_gran == "per_thread": q_int8, q_scale, k_int8, k_scale = per_thread_int8_triton( q, k, km, tensor_layout=tensor_layout, BLKQ=128, WARPQ=32, BLKK=64, WARPK=64 )
o = torch.empty(q.size(), dtype=dtype, device=q.device)
if pv_accum_dtype == "fp32+fp32" and smooth_v: warnings.warn("pv_accum_dtype is 'fp32+fp32', smooth_v will be ignored.") smooth_v = False
if pv_accum_dtype == "fp32+fp16" and smooth_v: warnings.warn("pv_accum_dtype is 'fp32+fp16', smooth_v will be ignored.") smooth_v = False
quant_v_scale_max = 448.0 if pv_accum_dtype == "fp32+fp16": quant_v_scale_max = 2.25
v_fp8, v_scale, vm = per_channel_fp8( v, tensor_layout=tensor_layout, scale_max=quant_v_scale_max, smooth_v=smooth_v ) print("before kernel call") if pv_accum_dtype == "fp32": if smooth_v: lse = ops.qk_int8_sv_f8_accum_f32_fuse_v_scale_fuse_v_mean_attn( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, vm, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) torch.cuda.synchronize() else: lse = ops.qk_int8_sv_f8_accum_f32_fuse_v_scale_attn( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) torch.cuda.synchronize() elif pv_accum_dtype == "fp32+fp32": lse = ops.qk_int8_sv_f8_accum_f32_fuse_v_scale_attn_inst_buf( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) torch.cuda.synchronize() elif pv_accum_dtype == "fp32+fp16": lse = ops.qk_int8_sv_f8_accum_f16_fuse_v_scale_attn_inst_buf( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) torch.cuda.synchronize() o = o[..., :head_dim_og] print("after kernel call") if return_lse: return ( o, lse / 1.44269504 + lse_correction * sm_scale if smooth_k else lse / 1.44269504, ) else: return o
@torch.compiler.disabledef sageattn_qk_int8_pv_fp8_cuda_sm90( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, tensor_layout: str = "HND", is_causal: bool = False, qk_quant_gran: str = "per_thread", sm_scale: Optional[float] = None, pv_accum_dtype: str = "fp32+fp32", smooth_k: bool = True, return_lse: bool = False, **kwargs: Any,) -> torch.Tensor: """ SageAttention with INT8 quantization for Q and K, FP8 PV with FP32 accumulation, implemented using CUDA.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
v : torch.Tensor The value tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
is_causal : bool Whether to apply causal mask to the attention matrix. Only applicable when qo_len == kv_len. Default: False.
qk_quant_gran : str The granularity of quantization for Q and K, either "per_warp" or "per_thread". Default: "per_thread".
sm_scale : Optional[float] The scale used in softmax, if not provided, will be set to ``1.0 / sqrt(head_dim)``.
pv_accum_dtype : str The dtype of the accumulation of the product of the value tensor and the attention weights, either "fp32" or "fp32+fp32". - "fp32": PV accumulation is done in fully in FP32. However, due to the hardware issue, there are only 22 valid bits in the FP32 accumulator. - "fp32+fp32": PV accumulation is done in FP32 (actually FP22), but added to a FP32 buffer every few iterations. This offers a balance between speed and accuracy. Default: "fp32+fp32".
smooth_k : bool Whether to smooth the key tensor by subtracting the mean along the sequence dimension. Default: True.
return_lse : bool Whether to return the log sum of the exponentiated attention weights. Used for cases like Ring Attention. Default: False.
Returns ------- torch.Tensor The output tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
torch.Tensor The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). Shape: ``[batch_size, num_qo_heads, qo_len]``. Only returned if `return_lse` is True.
Note ---- - ``num_qo_heads`` must be divisible by ``num_kv_heads``. - The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - All tensors must be on the same cuda device. - `smooth_k` will introduce slight overhead but will improve the accuracy under most circumstances. """
dtype = q.dtype assert q.is_cuda, "Input tensors must be on cuda." assert dtype in [torch.float16, torch.bfloat16], ( "Input tensors must be in dtype of torch.float16 or torch.bfloat16" ) assert qk_quant_gran in ["per_warp", "per_thread"], ( "qk_quant_gran must be either 'per_warp' or 'per_thread'." ) assert q.device == k.device == v.device, "All tensors must be on the same device." assert q.dtype == k.dtype == v.dtype, "All tensors must have the same dtype."
torch.cuda.set_device(v.device)
_tensor_layout = 0 if tensor_layout == "NHD" else 1 _is_caual = 1 if is_causal else 0 _qk_quant_gran = 3 if qk_quant_gran == "per_thread" else 2 _return_lse = 1 if return_lse else 0
head_dim_og = q.size(-1)
if head_dim_og < 64: q = torch.nn.functional.pad(q, (0, 64 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 64 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 64 - head_dim_og)) elif head_dim_og > 64 and head_dim_og < 128: q = torch.nn.functional.pad(q, (0, 128 - head_dim_og)) k = torch.nn.functional.pad(k, (0, 128 - head_dim_og)) v = torch.nn.functional.pad(v, (0, 128 - head_dim_og)) elif head_dim_og > 128: raise ValueError(f"Unsupported head_dim: {head_dim_og}")
# assert last dim is contiguous assert q.stride(-1) == 1 and k.stride(-1) == 1 and v.stride(-1) == 1, ( "Last dim of qkv must be contiguous." )
if sm_scale is None: sm_scale = head_dim_og**-0.5
seq_dim = 1 if _tensor_layout == 0 else 2 nh_dim = 2 if _tensor_layout == 0 else 1
if smooth_k: km = k.mean(dim=seq_dim, keepdim=True) nqheads = q.size(2) nkheads = k.size(2) q_per_kv_heads = nqheads // nkheads if q_per_kv_heads > 1: # nheads_k => nheads_q km_broadcast = torch.repeat_interleave(km, q_per_kv_heads, dim=nh_dim) else: km_broadcast = km if return_lse: if tensor_layout == "NHD": lse_correction = ( torch.matmul( q.transpose(1, 2), km_broadcast.transpose(1, 2).transpose(2, 3) ) .squeeze(-1) .to(torch.float32) ) else: lse_correction = ( torch.matmul(q, km_broadcast.transpose(2, 3)) .squeeze(-1) .to(torch.float32) ) else: km = None
if qk_quant_gran == "per_warp": q_int8, q_scale, k_int8, k_scale = per_warp_int8_cuda( q, k, km, tensor_layout=tensor_layout, BLKQ=64, WARPQ=16, BLKK=128 ) elif qk_quant_gran == "per_thread": q_int8, q_scale, k_int8, k_scale = per_thread_int8_triton( q, k, km, tensor_layout=tensor_layout, BLKQ=64, WARPQ=16, BLKK=128, WARPK=128, )
o = torch.empty(q.size(), dtype=dtype, device=q.device)
# pad v to multiple of 128 # TODO: modify per_channel_fp8 kernel to handle this kv_len = k.size(seq_dim) v_pad_len = 128 - (kv_len % 128) if kv_len % 128 != 0 else 0 if v_pad_len > 0: if tensor_layout == "HND": v = torch.cat( [ v, torch.zeros( v.size(0), v.size(1), v_pad_len, v.size(3), dtype=v.dtype, device=v.device, ), ], dim=2, ) else: v = torch.cat( [ v, torch.zeros( v.size(0), v_pad_len, v.size(2), v.size(3), dtype=v.dtype, device=v.device, ), ], dim=1, )
v_fp8, v_scale, _ = per_channel_fp8(v, tensor_layout=tensor_layout, smooth_v=False)
if pv_accum_dtype == "fp32": raise NotImplementedError("Please use pv_accum_dtype='fp32+fp32' for sm90.") lse = ops.qk_int8_sv_f8_accum_f32_fuse_v_scale_attn( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, ) elif pv_accum_dtype == "fp32+fp32": lse = ops.qk_int8_sv_f8_accum_f32_fuse_v_scale_attn_inst_buf_sm90( q_int8, k_int8, v_fp8, o, q_scale, k_scale, v_scale, _tensor_layout, _is_caual, _qk_quant_gran, sm_scale, _return_lse, )
o = o[..., :head_dim_og]
if return_lse: return ( o, lse / 1.44269504 + lse_correction * sm_scale if smooth_k else lse / 1.44269504, ) else: return o1{"python-depends":[]}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326"""Copyright (c) 2024 by SageAttention team.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License."""
import torchfrom typing import Optional
from ._ops import ops
def per_block_int8( q: torch.Tensor, k: torch.Tensor, km: Optional[torch.Tensor] = None, BLKQ: int = 128, BLKK: int = 64, sm_scale: Optional[float] = None, tensor_layout: str = "HND",): """ Quantize the query tensor `q` and the key tensor `k` with per block quantization.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
km : Optional[torch.Tensor] The mean tensor of `k` along the sequence length dimension. Shape: ``[batch_size, num_kv_heads, head_dim]``. Should be of the same dtype as `k` if provided. Default is None.
sm_scale : Optional[float] The scale factor for the softmax operation. Default is ``head_dim**-0.5``. It will be multiplied by ``1.44269504`` to work together with the triton attention kernel.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
Returns ------- Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor] A tuple containing: - The quantized query tensor. Shape: Same as `q` but with `int8` dtype. - The scale tensor of the query tensor. Shape: ``[batch_size, num_qo_heads, (qo_len + BLKQ - 1) // BLKQ]`` with `float32` dtype. - The quantized key tensor. Shape: Same as `k` but with `int8` dtype. - The scale tensor of the key tensor. Shape: ``[batch_size, num_kv_heads, (kv_len + BLKK - 1) // BLKK]`` with `float32` dtype.
Note ---- - The tensors `q` and `k` must have the dtype ``torch.float16`` or ``torch.bfloat16`` """
q_int8 = torch.empty(q.shape, dtype=torch.int8, device=q.device) k_int8 = torch.empty(k.shape, dtype=torch.int8, device=k.device)
if tensor_layout == "HND": b, h_qo, qo_len, head_dim = q.shape _, h_kv, kv_len, _ = k.shape
elif tensor_layout == "NHD": b, qo_len, h_qo, head_dim = q.shape _, kv_len, h_kv, _ = k.shape
else: raise ValueError(f"Unknown tensor layout: {tensor_layout}")
_tensor_layout = 0 if tensor_layout == "NHD" else 1
q_scale = torch.empty( (b, h_qo, (qo_len + BLKQ - 1) // BLKQ), device=q.device, dtype=torch.float32 ) k_scale = torch.empty( (b, h_kv, (kv_len + BLKK - 1) // BLKK), device=q.device, dtype=torch.float32 )
if sm_scale is None: sm_scale = head_dim**-0.5
sm_scale *= 1.44269504
ops.quant_per_block_int8_cuda(q, q_int8, q_scale, sm_scale, BLKQ, _tensor_layout) if km is not None: km = km.squeeze(1) if _tensor_layout == 0 else km.squeeze(2) ops.quant_per_block_int8_fuse_sub_mean_cuda( k, km, k_int8, k_scale, BLKK, _tensor_layout ) else: # The bound CUDA op expects an sm_scale argument; use 1.0 for K to avoid scaling ops.quant_per_block_int8_cuda(k, k_int8, k_scale, 1.0, BLKK, _tensor_layout)
return q_int8, q_scale, k_int8, k_scale
def per_warp_int8( q: torch.Tensor, k: torch.Tensor, km: Optional[torch.Tensor] = None, BLKQ: int = 128, WARPQ: int = 32, BLKK: int = 64, tensor_layout: str = "HND",): """ Quantize the query tensor `q` with per warp quantization and the key tensor `k` with per block quantization. Warp size of quantizing `q` is 16 or 32, with a block size of 64 or 128. Block size of quantizing `k` is 64 or 128.
Parameters ---------- q : torch.Tensor The query tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``.
k : torch.Tensor The key tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
km : Optional[torch.Tensor] The mean tensor of `k` along the sequence length dimension. Shape: ``[batch_size, num_kv_heads, head_dim]``. Should be of the same dtype as `k` if provided. Default is None.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
Returns ------- Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor] A tuple containing: - The quantized query tensor. Shape: Same as `q` but with `int8` dtype. - The scale tensor of the query tensor. Shape: ``[batch_size, num_qo_heads, (qo_len + BLKQ - 1) // BLKQ * (BLKQ // WARPQ)]`` with `float32` dtype. - The quantized key tensor. Shape: Same as `k` but with `int8` dtype. - The scale tensor of the key tensor. Shape: ``[batch_size, num_kv_heads, (kv_len + BLKK - 1) // BLKK]`` with `float32` dtype.
Note ---- - The tensors `q` and `k` must have the dtype ``torch.float16`` or ``torch.bfloat16`` """
q_int8 = torch.empty(q.shape, dtype=torch.int8, device=q.device) k_int8 = torch.empty(k.shape, dtype=torch.int8, device=k.device)
if tensor_layout == "HND": b, h_qo, qo_len, head_dim = q.shape _, h_kv, kv_len, _ = k.shape
elif tensor_layout == "NHD": b, qo_len, h_qo, head_dim = q.shape _, kv_len, h_kv, _ = k.shape
else: raise ValueError(f"Unknown tensor layout: {tensor_layout}")
_tensor_layout = 0 if tensor_layout == "NHD" else 1
q_scale = torch.empty( (b, h_qo, ((qo_len + BLKQ - 1) // BLKQ) * (BLKQ // WARPQ)), device=q.device, dtype=torch.float32, ) k_scale = torch.empty( (b, h_kv, (kv_len + BLKK - 1) // BLKK), device=q.device, dtype=torch.float32 )
ops.quant_per_warp_int8_cuda(q, q_int8, q_scale, BLKQ, WARPQ, _tensor_layout)
if km is not None: km = km.squeeze(1) if _tensor_layout == 0 else km.squeeze(2) ops.quant_per_block_int8_fuse_sub_mean_cuda( k, km, k_int8, k_scale, BLKK, _tensor_layout ) else: # The bound CUDA op expects an sm_scale argument; use 1.0 for K to avoid scaling ops.quant_per_block_int8_cuda(k, k_int8, k_scale, 1.0, BLKK, _tensor_layout)
return q_int8, q_scale, k_int8, k_scale
def sub_mean(v: torch.Tensor, tensor_layout: str = "HND"): """ Calculate the mean of the tensor `v` along the sequence length dimension and subtract it from `v`. Result is stored as fp16.
Parameters ---------- v : torch.Tensor The input tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
Returns ------- Tuple[torch.Tensor, torch.Tensor] A tuple containing: - The tensor `v_smoothed` with the mean subtracted and stored as fp16. Shape: Same as `v` with `float16` dtype. - The mean tensor of `v` along the sequence length dimension. Shape: ``[batch_size, num_kv_heads, head_dim]`` with dtype same as `v`.
Note ---- - The tensors `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - The returned tensor `v_smoothed` will have dtype ``torch.float16`` regardless of the input dtype. - The returned mean tensor will have the same dtype as the input tensor. """
_tensor_layout = 0 if tensor_layout == "NHD" else 1 vm = v.mean(dim=1 if _tensor_layout == 0 else 2)
v_smoothed = torch.empty(v.shape, dtype=torch.float16, device=v.device)
# subtract mean and store the result as fp16 ops.sub_mean_cuda(v, vm, v_smoothed, _tensor_layout)
return v_smoothed, vm
def per_channel_fp8( v: torch.Tensor, tensor_layout: str = "HND", scale_max: float = 448.0, smooth_v: bool = True,): """ Transpose, pad and permute the tensor `v` and quantize it to fp8 with per channel quantization. `v` is first transposed along the head dimension and the sequence length dimension, then padded to a multiple of 64. After that, the tensor is permuted along the sequence length dimension by ``[0, 1, 8, 9, 2, 3, 10, 11, 4, 5, 12, 13, 6, 7, 14, 15]``. The quantization is done per channel, with the scale value and smooth factor calculated per channel.
Parameters ---------- v : torch.Tensor The input tensor. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``.
tensor_layout : str The tensor layout, either "HND" or "NHD". Default: "HND".
scale_max : float The maximum scale value for the quantization. Default is 448.0 (upper bound of E4M3 data format).
smooth_v : bool Whether to smooth the quantized tensor. Default is True.
Returns ------- Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]] A tuple containing: - The quantized tensor `v_fp8`. Shape: - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, head_dim, (kv_len + 63) // 64 * 64]``, with `float8_e4m3fn` dtype. - If `tensor_layout` is "NHD": ``[batch_size, head_dim, num_kv_heads, (kv_len + 63) // 64 * 64]``, with `float8_e4m3fn` dtype. - The scale tensor of `v`. Shape: ``[batch_size, num_kv_heads, head_dim]`` with `float32` dtype. - The mean tensor of `v` along the sequence length dimension. Shape: ``[batch_size, num_kv_heads, head_dim]`` with `float32` dtype.
Note ---- - The tensors `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` - The returned mean tensor will be None if `smooth_v` is False. Otherwise it will have dtype ``torch.float32``. """
_tensor_layout = 0 if tensor_layout == "NHD" else 1
if tensor_layout == "HND": b, h_kv, kv_len, head_dim = v.shape padded_len = (kv_len + 63) // 64 * 64 v_transposed_permutted = torch.empty( (b, h_kv, head_dim, padded_len), dtype=v.dtype, device=v.device )
elif tensor_layout == "NHD": b, kv_len, h_kv, head_dim = v.shape padded_len = (kv_len + 63) // 64 * 64 v_transposed_permutted = torch.empty( (b, head_dim, h_kv, padded_len), dtype=v.dtype, device=v.device )
ops.transpose_pad_permute_cuda(v, v_transposed_permutted, _tensor_layout)
v_fp8 = torch.empty( v_transposed_permutted.shape, dtype=torch.float8_e4m3fn, device=v.device )
v_scale = torch.empty((b, h_kv, head_dim), dtype=torch.float32, device=v.device) vm = torch.empty((b, h_kv, head_dim), dtype=torch.float32, device=v.device)
if smooth_v: ops.mean_scale_fuse_quant_cuda( v_transposed_permutted, v_fp8, vm, v_scale, kv_len, scale_max, _tensor_layout, ) return v_fp8, v_scale, vm else: ops.scale_fuse_quant_cuda( v_transposed_permutted, v_fp8, v_scale, kv_len, scale_max, _tensor_layout ) return v_fp8, v_scale, None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204"""Copyright (c) 2024 by SageAttention team.
Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License."""
import torchimport tritonimport triton.language as tl
@triton.jitdef quant_query_per_thread_int8_kernel(Input, Output, Scale, L, stride_iz, stride_ih, stride_in, stride_oz, stride_oh, stride_on, stride_sz, stride_sh, C: tl.constexpr, BLK: tl.constexpr): off_blk = tl.program_id(0) // 8 off_tld = tl.program_id(0) % 8 off_h = tl.program_id(1) off_b = tl.program_id(2)
offs_n = off_blk * BLK + tl.arange(0, BLK // 8) * 8 + off_tld offs_k = tl.arange(0, C)
input_ptrs = Input + off_b * stride_iz + off_h * stride_ih + offs_n[:, None] * stride_in + offs_k[None, :] output_ptrs = Output + off_b * stride_oz + off_h * stride_oh + offs_n[:, None] * stride_on + offs_k[None, :] scale_ptrs = Scale + off_b * stride_sz + off_h * stride_sh + off_blk * 8 + off_tld
x = tl.load(input_ptrs, mask=offs_n[:, None] < L) x = x.to(tl.float32) scale = tl.max(tl.abs(x)) / 127. + 0.0000001 x_int8 = x / scale x_int8 += 0.5 * tl.where(x_int8 >= 0, 1, -1) x_int8 = x_int8.to(tl.int8) tl.store(output_ptrs, x_int8, mask=offs_n[:, None] < L) tl.store(scale_ptrs, scale)
@triton.jitdef quant_key_per_thread_int8_kernel(Input, Output, Scale, L, stride_iz, stride_ih, stride_in, stride_oz, stride_oh, stride_on, stride_sz, stride_sh, C: tl.constexpr, BLK: tl.constexpr): off_blk = tl.program_id(0) // 4 off_tld = tl.program_id(0) % 4 off_h = tl.program_id(1) off_b = tl.program_id(2)
# offs_n = off_blk * BLK + tl.cat(tl.arange(0, BLK // 8) * 8, tl.arange(0, BLK // 8) * 8 + 1, True) + off_tld * 2 # offs_k = tl.arange(0, C)
# input_ptrs = Input + off_b * stride_iz + off_h * stride_ih + offs_n[:, None] * stride_in + offs_k[None, :] # output_ptrs = Output + off_b * stride_oz + off_h * stride_oh + offs_n[:, None] * stride_on + offs_k[None, :] # scale_ptrs = Scale + off_b * stride_sz + off_h * stride_sh + off_blk * 4 + off_tld
# x = tl.load(input_ptrs, mask=offs_n[:, None] < L) # x = x.to(tl.float32) # scale = tl.max(tl.abs(x)) / 127. + 0.0000001 # x_int8 = x / scale # x_int8 += 0.5 * tl.where(x_int8 >= 0, 1, -1) # x_int8 = x_int8.to(tl.int8) # tl.store(output_ptrs, x_int8, mask=offs_n[:, None] < L) # tl.store(scale_ptrs, scale)
offs_n0 = off_blk * BLK + tl.arange(0, BLK // 8) * 8 + off_tld * 2 offs_n1 = off_blk * BLK + tl.arange(0, BLK // 8) * 8 + off_tld * 2 + 1 offs_k = tl.arange(0, C)
input_ptrs0 = Input + off_b * stride_iz + off_h * stride_ih + offs_n0[:, None] * stride_in + offs_k[None, :] input_ptrs1 = Input + off_b * stride_iz + off_h * stride_ih + offs_n1[:, None] * stride_in + offs_k[None, :] output_ptrs0 = Output + off_b * stride_oz + off_h * stride_oh + offs_n0[:, None] * stride_on + offs_k[None, :] output_ptrs1 = Output + off_b * stride_oz + off_h * stride_oh + offs_n1[:, None] * stride_on + offs_k[None, :] scale_ptrs = Scale + off_b * stride_sz + off_h * stride_sh + off_blk * 4 + off_tld
x0 = tl.load(input_ptrs0, mask=offs_n0[:, None] < L) x1 = tl.load(input_ptrs1, mask=offs_n1[:, None] < L) x0 = x0.to(tl.float32) x1 = x1.to(tl.float32) scale = max(tl.max(tl.abs(x0)), tl.max(tl.abs(x1))) / 127. + 0.0000001 x0_int8 = x0 / scale x1_int8 = x1 / scale x0_int8 += 0.5 * tl.where(x0_int8 >= 0, 1, -1) x1_int8 += 0.5 * tl.where(x1_int8 >= 0, 1, -1) x0_int8 = x0_int8.to(tl.int8) x1_int8 = x1_int8.to(tl.int8) tl.store(output_ptrs0, x0_int8, mask=offs_n0[:, None] < L) tl.store(output_ptrs1, x1_int8, mask=offs_n1[:, None] < L) tl.store(scale_ptrs, scale)
@triton.jitdef quant_query_per_thread_int4_kernel(Input, Output, Scale, L, stride_iz, stride_ih, stride_in, stride_oz, stride_oh, stride_on, stride_sz, stride_sh, C: tl.constexpr, BLK: tl.constexpr): off_blk = tl.program_id(0) // 8 off_tld = tl.program_id(0) % 8 off_h = tl.program_id(1) off_b = tl.program_id(2)
offs_n = off_blk * BLK + tl.arange(0, BLK // 8) * 8 + off_tld offs_k = tl.arange(0, C)
input_ptrs = Input + off_b * stride_iz + off_h * stride_ih + offs_n[:, None] * stride_in + offs_k[None, :] output_ptrs = Output + off_b * stride_oz + off_h * stride_oh + offs_n[:, None] * stride_on + offs_k[None, :] scale_ptrs = Scale + off_b * stride_sz + off_h * stride_sh + off_blk * 8 + off_tld
x = tl.load(input_ptrs, mask=offs_n[:, None] < L) x = x.to(tl.float32) scale = tl.max(tl.abs(x)) / 7. + 0.0000001 x_int8 = x / scale x_int8 += 0.5 * tl.where(x_int8 >= 0, 1, -1) x_int8 = x_int8.to(tl.int8) tl.store(output_ptrs, x_int8, mask=offs_n[:, None] < L) tl.store(scale_ptrs, scale)
@triton.jitdef quant_key_per_thread_int4_kernel(Input, Output, Scale, L, stride_iz, stride_ih, stride_in, stride_oz, stride_oh, stride_on, stride_sz, stride_sh, C: tl.constexpr, BLK: tl.constexpr): off_blk = tl.program_id(0) // 4 off_tld = tl.program_id(0) % 4 off_h = tl.program_id(1) off_b = tl.program_id(2)
offs_n = off_blk * BLK + tl.cat(tl.arange(0, BLK // 8) * 8, tl.arange(0, BLK // 8) * 8 + 1, True) + off_tld * 2 offs_k = tl.arange(0, C)
input_ptrs = Input + off_b * stride_iz + off_h * stride_ih + offs_n[:, None] * stride_in + offs_k[None, :] output_ptrs = Output + off_b * stride_oz + off_h * stride_oh + offs_n[:, None] * stride_on + offs_k[None, :] scale_ptrs = Scale + off_b * stride_sz + off_h * stride_sh + off_blk * 4 + off_tld
x = tl.load(input_ptrs, mask=offs_n[:, None] < L) x = x.to(tl.float32) scale = tl.max(tl.abs(x)) / 7. + 0.0000001 x_int8 = x / scale x_int8 += 0.5 * tl.where(x_int8 >= 0, 1, -1) x_int8 = x_int8.to(tl.int8) tl.store(output_ptrs, x_int8, mask=offs_n[:, None] < L) tl.store(scale_ptrs, scale)
def per_thread_int8(q, k, km=None, BLKQ=128, WARPQ=32, BLKK=64, WARPK=64, sm_scale=None, tensor_layout="HND"): q_int8 = torch.empty(q.shape, dtype=torch.int8, device=q.device) k_int8 = torch.empty(k.shape, dtype=torch.int8, device=k.device)
if km is not None: k = k - km
if tensor_layout == "HND": b, h_qo, qo_len, head_dim = q.shape _, h_kv, kv_len, _ = k.shape
stride_bz_q, stride_h_q, stride_seq_q = q.stride(0), q.stride(1), q.stride(2) stride_bz_qo, stride_h_qo, stride_seq_qo = q_int8.stride(0), q_int8.stride(1), q_int8.stride(2) stride_bz_k, stride_h_k, stride_seq_k = k.stride(0), k.stride(1), k.stride(2) stride_bz_ko, stride_h_ko, stride_seq_ko = k_int8.stride(0), k_int8.stride(1), k_int8.stride(2) elif tensor_layout == "NHD": b, qo_len, h_qo, head_dim = q.shape _, kv_len, h_kv, _ = k.shape
stride_bz_q, stride_h_q, stride_seq_q = q.stride(0), q.stride(2), q.stride(1) stride_bz_qo, stride_h_qo, stride_seq_qo = q_int8.stride(0), q_int8.stride(2), q_int8.stride(1) stride_bz_k, stride_h_k, stride_seq_k = k.stride(0), k.stride(2), k.stride(1) stride_bz_ko, stride_h_ko, stride_seq_ko = k_int8.stride(0), k_int8.stride(2), k_int8.stride(1) else: raise ValueError(f"Unknown tensor layout: {tensor_layout}")
q_scale = torch.empty((b, h_qo, (qo_len + BLKQ - 1) // BLKQ * (BLKQ // WARPQ) * 8), device=q.device, dtype=torch.float32) k_scale = torch.empty((b, h_kv, (kv_len + BLKK - 1) // BLKK * (BLKK // WARPK) * 4), device=q.device, dtype=torch.float32)
if sm_scale is None: sm_scale = head_dim**-0.5
grid = ((qo_len + BLKQ - 1) // BLKQ * (BLKQ // WARPQ) * 8, h_qo, b) quant_query_per_thread_int8_kernel[grid]( q, q_int8, q_scale, qo_len, stride_bz_q, stride_h_q, stride_seq_q, stride_bz_qo, stride_h_qo, stride_seq_qo, q_scale.stride(0), q_scale.stride(1), C=head_dim, BLK=WARPQ )
grid = ((kv_len + BLKK - 1) // BLKK * (BLKK // WARPK) * 4, h_kv, b) quant_key_per_thread_int8_kernel[grid]( k, k_int8, k_scale, kv_len, stride_bz_k, stride_h_k, stride_seq_k, stride_bz_ko, stride_h_ko, stride_seq_ko, k_scale.stride(0), k_scale.stride(1), C=head_dim, BLK=WARPK )
return q_int8, q_scale, k_int8, k_scale1234567891011121314151617181920212223242526import ctypesimport sys
import importlibfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))1234567891011from .quant import per_block_int8, per_warp_int8, sub_mean, per_channel_fp8from .core import sageattn
__all__ = [ "per_block_int8", "per_warp_int8", "sub_mean", "per_channel_fp8", "sageattn",]123456789import torchfrom . import _sage_attention_c08f355_dirtyops = torch.ops._sage_attention_c08f355_dirty
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_sage_attention_c08f355_dirty::{op_name}"build.toml+107-78
build/torch28-cxx11-cu126-x86_64-linux/__init__.pyAdded+11
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu126-x86_64-linux/_ops.pyRenamed+3-3
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu126-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu126-x86_64-linux/core.pyRenamed+1-1
build/torch28-cxx11-cu126-x86_64-linux/metadata.jsonAdded+1
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu126-x86_64-linux/quant.pyRenamed
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu126-x86_64-linux/quant_per_thread.pyRenamed
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__init__.py+24-9
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
build/torch28-cxx11-cu128-x86_64-linux/__init__.pyAdded+11
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu128-x86_64-linux/_ops.pyRenamed+3-3
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu128-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
build/torch28-cxx11-cu126-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu128-x86_64-linux/core.pyRenamed+1-1
build/torch28-cxx11-cu128-x86_64-linux/metadata.jsonAdded+1
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu128-x86_64-linux/quant.pyRenamed
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu128-x86_64-linux/quant_per_thread.pyRenamed
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__init__.py+24-9
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
build/torch28-cxx11-cu129-x86_64-linux/__init__.pyAdded+11
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/_ops.py β build/torch28-cxx11-cu129-x86_64-linux/_ops.pyRenamed+3-3
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/_sage_attention_1369690_dirty.abi3.so β build/torch28-cxx11-cu129-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soRenamed+2-2
build/torch28-cxx11-cu128-x86_64-linux/sage_attention/core.py β build/torch28-cxx11-cu129-x86_64-linux/core.pyRenamed+1-1
build/torch28-cxx11-cu129-x86_64-linux/metadata.jsonAdded+1
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/quant.py β build/torch28-cxx11-cu129-x86_64-linux/quant.pyRenamed
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/quant_per_thread.py β build/torch28-cxx11-cu129-x86_64-linux/quant_per_thread.pyRenamed
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__init__.py+24-9
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/__init__.cpython-313.pycDeleted
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/_ops.cpython-313.pycDeleted
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/core.cpython-313.pycDeleted
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/quant.cpython-313.pycDeleted
build/torch28-cxx11-cu129-x86_64-linux/sage_attention/__pycache__/quant_per_thread.cpython-313.pycDeleted
build/torch29-cxx11-cu126-x86_64-linux/__init__.pyAdded+11
build/torch29-cxx11-cu126-x86_64-linux/_ops.pyAdded+9
build/torch29-cxx11-cu126-x86_64-linux/_sage_attention_c08f355_dirty.abi3.soAdded+3
build/torch29-cxx11-cu126-x86_64-linux/core.pyAdded+969
build/torch29-cxx11-cu126-x86_64-linux/metadata.jsonAdded+1
build/torch29-cxx11-cu126-x86_64-linux/quant.pyAdded+326
build/torch29-cxx11-cu126-x86_64-linux/quant_per_thread.pyAdded+204
build/torch29-cxx11-cu126-x86_64-linux/sage_attention/__init__.pyAdded+26
build/torch29-cxx11-cu128-x86_64-linux/__init__.pyAdded+11
build/torch29-cxx11-cu128-x86_64-linux/_ops.pyAdded+9