fla-core 0.4.1__tar.gz → 0.4.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fla_core-0.4.1/fla_core.egg-info → fla_core-0.4.2}/PKG-INFO +1 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/__init__.py +1 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/activations.py +256 -68
- fla_core-0.4.2/fla/modules/conv/__init__.py +14 -0
- fla_core-0.4.2/fla/modules/conv/causal_conv1d.py +124 -0
- fla_core-0.4.2/fla/modules/conv/cp/__init__.py +6 -0
- fla_core-0.4.2/fla/modules/conv/cp/ops.py +251 -0
- fla_core-0.4.2/fla/modules/conv/cuda/__init__.py +9 -0
- fla_core-0.4.2/fla/modules/conv/cuda/ops.py +228 -0
- fla_core-0.4.2/fla/modules/conv/long_conv.py +165 -0
- fla_core-0.4.2/fla/modules/conv/short_conv.py +241 -0
- fla_core-0.4.2/fla/modules/conv/triton/__init__.py +19 -0
- fla_core-0.4.2/fla/modules/conv/triton/kernels.py +657 -0
- fla_core-0.4.2/fla/modules/conv/triton/ops.py +397 -0
- fla_core-0.4.2/fla/modules/convolution.py +38 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_norm_gate.py +71 -85
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/l2norm.py +26 -30
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/rotary.py +14 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/__init__.py +2 -0
- fla_core-0.4.2/fla/ops/attn/naive.py +64 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/parallel.py +12 -6
- fla_core-0.4.2/fla/ops/backends/__init__.py +168 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/__init__.py +3 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/chunk.py +31 -7
- fla_core-0.4.2/fla/ops/comba/naive.py +166 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/utils.py +6 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/wy_fast.py +9 -3
- fla_core-0.4.2/fla/ops/common/backends/__init__.py +12 -0
- fla_core-0.4.2/fla/ops/common/backends/intracard.py +93 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_delta_h.py +306 -103
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h_parallel.py +6 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_o.py +40 -14
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_scaled_dot_kkt.py +3 -1
- fla_core-0.4.2/fla/ops/common/intracard_cp.py +597 -0
- fla_core-0.4.2/fla/ops/cp/__init__.py +25 -0
- fla_core-0.4.2/fla/ops/cp/chunk_delta_h.py +1314 -0
- fla_core-0.4.2/fla/ops/cp/comm.py +162 -0
- fla_core-0.4.2/fla/ops/cp/context.py +165 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/chunk.py +23 -5
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/wy_fast.py +10 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/forgetting_attn/__init__.py +2 -0
- fla_core-0.4.2/fla/ops/forgetting_attn/naive.py +43 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk.py +32 -5
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_deltaproduct_h.py +6 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_deltaproduct_o.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/__init__.py +3 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/chunk.py +104 -7
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/fused_recurrent.py +50 -17
- fla_core-0.4.2/fla/ops/gated_delta_rule/naive.py +156 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/wy_fast.py +8 -4
- fla_core-0.4.2/fla/ops/gated_oja_rule/__init__.py +7 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/chunk.py +338 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_h.py +807 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_kkt.py +521 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_o.py +674 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/fused_recurrent.py +263 -0
- fla_core-0.4.2/fla/ops/gated_oja_rule/wy_fast.py +290 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk.py +126 -49
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +178 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +174 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +9 -3
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +7 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/chunk.py +18 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/wy_fast.py +7 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/chunk.py +49 -20
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/chunk.py +29 -7
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk.py +127 -223
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_bwd.py +211 -15
- fla_core-0.4.2/fla/ops/kda/chunk_fwd.py +132 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_intra.py +363 -157
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_intra_token_parallel.py +5 -3
- fla_core-0.4.2/fla/ops/kda/fused_recurrent.py +434 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/gate.py +184 -20
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/wy_fast.py +10 -12
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/utils.py +0 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk.py +17 -4
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_cg_solver_bwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_cg_solver_fwd.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +4 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +4 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +3 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/compression.py +12 -6
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/parallel.py +3 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/__init__.py +2 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/cumprod_householder_bwd.py +4 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/cumprod_householder_fwd.py +4 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_bwd.py +5 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +8 -4
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_fwd.py +8 -5
- fla_core-0.4.2/fla/ops/path_attn/naive.py +92 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +5 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +5 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_intra.py +5 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_fwd.py +4 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/prepare_k_cache.py +4 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/transform_q.py +6 -3
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/chunk.py +36 -15
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/recurrent_naive.py +0 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/chunk.py +17 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_k_update.py +11 -8
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/chunk.py +21 -6
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/parallel.py +18 -7
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/chunk.py +39 -10
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/__init__.py +0 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/index.py +24 -19
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/op.py +16 -8
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/pooling.py +6 -2
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/utils.py +93 -29
- {fla_core-0.4.1 → fla_core-0.4.2/fla_core.egg-info}/PKG-INFO +1 -1
- {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/SOURCES.txt +32 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/pyproject.toml +1 -1
- fla_core-0.4.1/fla/modules/convolution.py +0 -1183
- fla_core-0.4.1/fla/ops/kda/fused_recurrent.py +0 -112
- {fla_core-0.4.1 → fla_core-0.4.2}/LICENSE +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/README.md +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/feature_map.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_bitlinear.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_cross_entropy.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_kl_div.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_linear_cross_entropy.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/grpo.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/l2warp.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/layernorm.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/layernorm_gated.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/mlp.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/token_shift.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/decoding.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h_split.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/invcum.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/forgetting_attn/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_ref.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_fwd.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/decoding_one_step.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/utils.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/parallel.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv4/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv4/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/chunk_naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/channel_mixing.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_addcmul.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/gate_output_correction.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/fused_recurrent.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/log_impl.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/__init__.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/fused_chunk.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/naive.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/constant.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/cumsum.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/logcumsumexp.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/logsumexp.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/matmul.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/pack.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/softmax.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/softplus.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/solve_tril.py +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/dependency_links.txt +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/requires.txt +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/top_level.txt +0 -0
- {fla_core-0.4.1 → fla_core-0.4.2}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fla-core
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.2
|
|
4
4
|
Summary: Core operations for flash-linear-attention
|
|
5
5
|
Project-URL: Homepage, https://github.com/fla-org/flash-linear-attention
|
|
6
6
|
Project-URL: Repository, https://github.com/fla-org/flash-linear-attention
|
|
@@ -8,14 +8,66 @@ import triton.language as tl
|
|
|
8
8
|
from fla.ops.utils.op import exp, log
|
|
9
9
|
from fla.utils import IS_AMD, autocast_custom_bwd, autocast_custom_fwd, autotune_cache_kwargs, input_guard
|
|
10
10
|
|
|
11
|
-
try:
|
|
12
|
-
from torch.distributed.tensor import DTensor
|
|
13
|
-
except (ImportError, AttributeError):
|
|
14
|
-
DTensor = None
|
|
15
|
-
|
|
16
11
|
NUM_WARPS_AUTOTUNE = [1, 2, 4, 8, 16] if IS_AMD else [1, 2, 4, 8, 16, 32]
|
|
17
12
|
|
|
18
13
|
|
|
14
|
+
def _get_stride(x: torch.Tensor) -> int:
|
|
15
|
+
"""Get the row stride for viewing a tensor as 2D (num_rows, D) where D = shape[-1].
|
|
16
|
+
|
|
17
|
+
Returns stride(-2) if the tensor is at least 2D, or 0 for 1D tensors.
|
|
18
|
+
The caller must ensure the tensor is "inner-contiguous" (stride(-1) == 1 and
|
|
19
|
+
higher dims are contiguous relative to dim -2) before using this value.
|
|
20
|
+
"""
|
|
21
|
+
if x.ndim < 2:
|
|
22
|
+
return 0
|
|
23
|
+
return x.stride(-2)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _is_inner_contiguous(x: torch.Tensor) -> bool:
|
|
27
|
+
"""Check if a tensor can be safely viewed as 2D (num_rows, D) with row stride = stride(-2).
|
|
28
|
+
|
|
29
|
+
This holds when stride(-1) == 1 and all dimensions above -2 are contiguous
|
|
30
|
+
with respect to the dimension below them.
|
|
31
|
+
"""
|
|
32
|
+
ndim = x.ndim
|
|
33
|
+
if ndim < 2:
|
|
34
|
+
return True
|
|
35
|
+
if x.stride(-1) != 1:
|
|
36
|
+
return False
|
|
37
|
+
if ndim == 2:
|
|
38
|
+
# 2D: any layout with stride(-1)==1 is valid (can view as (T, D))
|
|
39
|
+
return True
|
|
40
|
+
if ndim == 3:
|
|
41
|
+
# 3D (B, T, D): stride should be (T*D, D, 1)
|
|
42
|
+
return x.stride(0) == x.stride(-2) * x.shape[-2]
|
|
43
|
+
if ndim == 4:
|
|
44
|
+
# 4D (B, H, T, D): stride should be (H*T*D, T*D, D, 1)
|
|
45
|
+
if x.stride(1) != x.stride(-2) * x.shape[-2]:
|
|
46
|
+
return False
|
|
47
|
+
return x.stride(0) == x.stride(1) * x.shape[1]
|
|
48
|
+
# 5D+ fallback to loop
|
|
49
|
+
expected = x.stride(-2) * x.shape[-2]
|
|
50
|
+
for d in range(ndim - 3, -1, -1):
|
|
51
|
+
if x.stride(d) != expected:
|
|
52
|
+
return False
|
|
53
|
+
expected *= x.shape[d]
|
|
54
|
+
return True
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _ensure_inner_contiguous(x: torch.Tensor) -> torch.Tensor:
|
|
58
|
+
"""Make the tensor inner-contiguous if it isn't already."""
|
|
59
|
+
if _is_inner_contiguous(x):
|
|
60
|
+
return x
|
|
61
|
+
return x.contiguous()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _alloc_output(x: torch.Tensor, contiguous: bool = False) -> torch.Tensor:
|
|
65
|
+
"""Allocate output tensor: contiguous buffer or same layout as input."""
|
|
66
|
+
if contiguous:
|
|
67
|
+
return x.new_empty(x.shape)
|
|
68
|
+
return torch.empty_like(x)
|
|
69
|
+
|
|
70
|
+
|
|
19
71
|
@triton.autotune(
|
|
20
72
|
configs=[
|
|
21
73
|
triton.Config({'B': bs}, num_warps=num_warps)
|
|
@@ -29,15 +81,21 @@ NUM_WARPS_AUTOTUNE = [1, 2, 4, 8, 16] if IS_AMD else [1, 2, 4, 8, 16, 32]
|
|
|
29
81
|
def sigmoid_fwd_kernel(
|
|
30
82
|
x, y,
|
|
31
83
|
T,
|
|
32
|
-
B: tl.constexpr,
|
|
33
84
|
D: tl.constexpr,
|
|
85
|
+
stride_x_row,
|
|
86
|
+
stride_y_row,
|
|
87
|
+
B: tl.constexpr,
|
|
34
88
|
):
|
|
35
89
|
pid = tl.program_id(0)
|
|
36
90
|
offs = pid * B + tl.arange(0, B)
|
|
37
91
|
mask = offs < T
|
|
38
|
-
|
|
92
|
+
row = offs // D
|
|
93
|
+
col = offs % D
|
|
94
|
+
x_off = row * stride_x_row + col
|
|
95
|
+
y_off = row * stride_y_row + col
|
|
96
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
39
97
|
y_val = 1.0 / (1.0 + exp(-x_val))
|
|
40
|
-
tl.store(y +
|
|
98
|
+
tl.store(y + y_off, y_val.to(y.dtype.element_ty), mask=mask)
|
|
41
99
|
|
|
42
100
|
|
|
43
101
|
@triton.autotune(
|
|
@@ -53,41 +111,65 @@ def sigmoid_fwd_kernel(
|
|
|
53
111
|
def sigmoid_bwd_kernel(
|
|
54
112
|
x, dy, dx,
|
|
55
113
|
T,
|
|
56
|
-
B: tl.constexpr,
|
|
57
114
|
D: tl.constexpr,
|
|
115
|
+
stride_x_row,
|
|
116
|
+
stride_dy_row,
|
|
117
|
+
stride_dx_row,
|
|
118
|
+
B: tl.constexpr,
|
|
58
119
|
):
|
|
59
120
|
pid = tl.program_id(0)
|
|
60
121
|
offs = pid * B + tl.arange(0, B)
|
|
61
122
|
mask = offs < T
|
|
62
|
-
|
|
63
|
-
|
|
123
|
+
row = offs // D
|
|
124
|
+
col = offs % D
|
|
125
|
+
x_off = row * stride_x_row + col
|
|
126
|
+
dy_off = row * stride_dy_row + col
|
|
127
|
+
dx_off = row * stride_dx_row + col
|
|
128
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
129
|
+
g_val = tl.load(dy + dy_off, mask=mask, other=0.).to(tl.float32)
|
|
64
130
|
s = 1.0 / (1.0 + exp(-x_val))
|
|
65
131
|
dx_val = g_val * s * (1.0 - s)
|
|
66
|
-
tl.store(dx +
|
|
132
|
+
tl.store(dx + dx_off, dx_val.to(dx.dtype.element_ty), mask=mask)
|
|
67
133
|
|
|
68
134
|
|
|
69
|
-
|
|
135
|
+
@torch.compiler.disable
|
|
136
|
+
def sigmoid_fwd(x: torch.Tensor, output_contiguous: bool = False) -> torch.Tensor:
|
|
137
|
+
x = _ensure_inner_contiguous(x)
|
|
70
138
|
T, D = x.numel(), x.shape[-1]
|
|
71
|
-
y =
|
|
72
|
-
sigmoid_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
139
|
+
y = _alloc_output(x, output_contiguous)
|
|
140
|
+
sigmoid_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
141
|
+
x, y, T=T, D=D,
|
|
142
|
+
stride_x_row=_get_stride(x),
|
|
143
|
+
stride_y_row=_get_stride(y),
|
|
144
|
+
)
|
|
73
145
|
return y
|
|
74
146
|
|
|
75
147
|
|
|
76
|
-
|
|
148
|
+
@torch.compiler.disable
|
|
149
|
+
def sigmoid_bwd(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool = False) -> torch.Tensor:
|
|
150
|
+
x = _ensure_inner_contiguous(x)
|
|
151
|
+
dy = _ensure_inner_contiguous(dy)
|
|
77
152
|
T, D = x.numel(), x.shape[-1]
|
|
78
|
-
dx =
|
|
79
|
-
sigmoid_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
153
|
+
dx = _alloc_output(x, output_contiguous)
|
|
154
|
+
sigmoid_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
155
|
+
x, dy, dx, T=T, D=D,
|
|
156
|
+
stride_x_row=_get_stride(x),
|
|
157
|
+
stride_dy_row=_get_stride(dy),
|
|
158
|
+
stride_dx_row=_get_stride(dx),
|
|
159
|
+
)
|
|
80
160
|
return dx
|
|
81
161
|
|
|
82
162
|
|
|
83
163
|
class SigmoidFunction(torch.autograd.Function):
|
|
84
164
|
|
|
85
165
|
@staticmethod
|
|
166
|
+
@input_guard(no_guard_contiguous=True)
|
|
86
167
|
def forward(ctx, x):
|
|
87
168
|
ctx.save_for_backward(x)
|
|
88
169
|
return sigmoid_fwd(x)
|
|
89
170
|
|
|
90
171
|
@staticmethod
|
|
172
|
+
@input_guard(no_guard_contiguous=True)
|
|
91
173
|
def backward(ctx, dout):
|
|
92
174
|
x, = ctx.saved_tensors
|
|
93
175
|
return sigmoid_bwd(x, dout)
|
|
@@ -111,18 +193,24 @@ def logsigmoid_fwd_kernel(
|
|
|
111
193
|
y,
|
|
112
194
|
temperature,
|
|
113
195
|
T,
|
|
114
|
-
B: tl.constexpr,
|
|
115
196
|
D: tl.constexpr,
|
|
197
|
+
stride_x_row,
|
|
198
|
+
stride_y_row,
|
|
199
|
+
B: tl.constexpr,
|
|
116
200
|
):
|
|
117
201
|
i = tl.program_id(0)
|
|
118
202
|
o_i = i * B + tl.arange(0, B)
|
|
119
203
|
m_i = o_i < T
|
|
204
|
+
row = o_i // D
|
|
205
|
+
col = o_i % D
|
|
206
|
+
x_off = row * stride_x_row + col
|
|
207
|
+
y_off = row * stride_y_row + col
|
|
120
208
|
|
|
121
|
-
b_x = tl.load(x +
|
|
209
|
+
b_x = tl.load(x + x_off, mask=m_i, other=0.).to(tl.float32)
|
|
122
210
|
b_m = tl.minimum(0., b_x)
|
|
123
211
|
b_z = 1. + exp(-tl.abs(b_x))
|
|
124
212
|
b_y = (b_m - log(b_z)) / temperature
|
|
125
|
-
tl.store(y +
|
|
213
|
+
tl.store(y + y_off, b_y.to(y.dtype.element_ty), mask=m_i)
|
|
126
214
|
|
|
127
215
|
|
|
128
216
|
@triton.autotune(
|
|
@@ -141,35 +229,50 @@ def logsigmoid_bwd_kernel(
|
|
|
141
229
|
dy,
|
|
142
230
|
temperature,
|
|
143
231
|
T,
|
|
144
|
-
B: tl.constexpr,
|
|
145
232
|
D: tl.constexpr,
|
|
233
|
+
stride_x_row,
|
|
234
|
+
stride_dx_row,
|
|
235
|
+
stride_dy_row,
|
|
236
|
+
B: tl.constexpr,
|
|
146
237
|
):
|
|
147
238
|
i = tl.program_id(0)
|
|
148
239
|
o_i = i * B + tl.arange(0, B)
|
|
149
240
|
m_i = o_i < T
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
241
|
+
row = o_i // D
|
|
242
|
+
col = o_i % D
|
|
243
|
+
x_off = row * stride_x_row + col
|
|
244
|
+
dx_off = row * stride_dx_row + col
|
|
245
|
+
dy_off = row * stride_dy_row + col
|
|
246
|
+
|
|
247
|
+
b_x = tl.load(x + x_off, mask=m_i, other=0.).to(tl.float32)
|
|
248
|
+
b_dy = tl.load(dy + dy_off, mask=m_i, other=0.).to(tl.float32)
|
|
153
249
|
b_dx = b_dy * ((1. - tl.sigmoid(b_x)) / temperature)
|
|
154
|
-
tl.store(dx +
|
|
250
|
+
tl.store(dx + dx_off, b_dx.to(dx.dtype.element_ty), mask=m_i)
|
|
155
251
|
|
|
156
252
|
|
|
157
|
-
|
|
253
|
+
@torch.compiler.disable
|
|
254
|
+
def logsigmoid_fwd(x: torch.Tensor, temperature: float = 1., output_contiguous: bool = False) -> torch.Tensor:
|
|
255
|
+
x = _ensure_inner_contiguous(x)
|
|
158
256
|
T, D = x.numel(), x.shape[-1]
|
|
159
|
-
y =
|
|
257
|
+
y = _alloc_output(x, output_contiguous)
|
|
160
258
|
logsigmoid_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
161
259
|
x=x,
|
|
162
260
|
y=y,
|
|
163
261
|
temperature=temperature,
|
|
164
262
|
T=T,
|
|
165
263
|
D=D,
|
|
264
|
+
stride_x_row=_get_stride(x),
|
|
265
|
+
stride_y_row=_get_stride(y),
|
|
166
266
|
)
|
|
167
267
|
return y
|
|
168
268
|
|
|
169
269
|
|
|
170
|
-
|
|
270
|
+
@torch.compiler.disable
|
|
271
|
+
def logsigmoid_bwd(x: torch.Tensor, dy: torch.Tensor, temperature: float = 1., output_contiguous: bool = False) -> torch.Tensor:
|
|
272
|
+
x = _ensure_inner_contiguous(x)
|
|
273
|
+
dy = _ensure_inner_contiguous(dy)
|
|
171
274
|
T, D = x.numel(), x.shape[-1]
|
|
172
|
-
dx =
|
|
275
|
+
dx = _alloc_output(x, output_contiguous)
|
|
173
276
|
logsigmoid_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
174
277
|
x=x,
|
|
175
278
|
dx=dx,
|
|
@@ -177,6 +280,9 @@ def logsigmoid_bwd(x: torch.Tensor, dy: torch.Tensor, temperature: float = 1.) -
|
|
|
177
280
|
temperature=temperature,
|
|
178
281
|
T=T,
|
|
179
282
|
D=D,
|
|
283
|
+
stride_x_row=_get_stride(x),
|
|
284
|
+
stride_dx_row=_get_stride(dx),
|
|
285
|
+
stride_dy_row=_get_stride(dy),
|
|
180
286
|
)
|
|
181
287
|
return dx
|
|
182
288
|
|
|
@@ -184,14 +290,14 @@ def logsigmoid_bwd(x: torch.Tensor, dy: torch.Tensor, temperature: float = 1.) -
|
|
|
184
290
|
class LogSigmoidFunction(torch.autograd.Function):
|
|
185
291
|
|
|
186
292
|
@staticmethod
|
|
187
|
-
@input_guard
|
|
293
|
+
@input_guard(no_guard_contiguous=True)
|
|
188
294
|
def forward(ctx, x, temperature):
|
|
189
295
|
ctx.save_for_backward(x)
|
|
190
296
|
ctx.temperature = temperature
|
|
191
297
|
return logsigmoid_fwd(x, temperature)
|
|
192
298
|
|
|
193
299
|
@staticmethod
|
|
194
|
-
@input_guard
|
|
300
|
+
@input_guard(no_guard_contiguous=True)
|
|
195
301
|
def backward(ctx, dy):
|
|
196
302
|
x, = ctx.saved_tensors
|
|
197
303
|
return logsigmoid_bwd(x, dy, ctx.temperature), None
|
|
@@ -214,16 +320,22 @@ def logsigmoid(x: torch.Tensor, temperature: float = 1.) -> torch.Tensor:
|
|
|
214
320
|
def swish_fwd_kernel(
|
|
215
321
|
x, y,
|
|
216
322
|
T,
|
|
217
|
-
B: tl.constexpr,
|
|
218
323
|
D: tl.constexpr,
|
|
324
|
+
stride_x_row,
|
|
325
|
+
stride_y_row,
|
|
326
|
+
B: tl.constexpr,
|
|
219
327
|
):
|
|
220
328
|
pid = tl.program_id(0)
|
|
221
329
|
offs = pid * B + tl.arange(0, B)
|
|
222
330
|
mask = offs < T
|
|
223
|
-
|
|
331
|
+
row = offs // D
|
|
332
|
+
col = offs % D
|
|
333
|
+
x_off = row * stride_x_row + col
|
|
334
|
+
y_off = row * stride_y_row + col
|
|
335
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
224
336
|
s = 1.0 / (1.0 + exp(-x_val))
|
|
225
337
|
y_val = x_val * s
|
|
226
|
-
tl.store(y +
|
|
338
|
+
tl.store(y + y_off, y_val.to(y.dtype.element_ty), mask=mask)
|
|
227
339
|
|
|
228
340
|
|
|
229
341
|
@triton.autotune(
|
|
@@ -239,41 +351,65 @@ def swish_fwd_kernel(
|
|
|
239
351
|
def swish_bwd_kernel(
|
|
240
352
|
x, dy, dx,
|
|
241
353
|
T,
|
|
242
|
-
B: tl.constexpr,
|
|
243
354
|
D: tl.constexpr,
|
|
355
|
+
stride_x_row,
|
|
356
|
+
stride_dy_row,
|
|
357
|
+
stride_dx_row,
|
|
358
|
+
B: tl.constexpr,
|
|
244
359
|
):
|
|
245
360
|
pid = tl.program_id(0)
|
|
246
361
|
offs = pid * B + tl.arange(0, B)
|
|
247
362
|
mask = offs < T
|
|
248
|
-
|
|
249
|
-
|
|
363
|
+
row = offs // D
|
|
364
|
+
col = offs % D
|
|
365
|
+
x_off = row * stride_x_row + col
|
|
366
|
+
dy_off = row * stride_dy_row + col
|
|
367
|
+
dx_off = row * stride_dx_row + col
|
|
368
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
369
|
+
g_val = tl.load(dy + dy_off, mask=mask, other=0.).to(tl.float32)
|
|
250
370
|
s = 1.0 / (1.0 + exp(-x_val))
|
|
251
371
|
dx_val = g_val * s * (1.0 + x_val * (1.0 - s))
|
|
252
|
-
tl.store(dx +
|
|
372
|
+
tl.store(dx + dx_off, dx_val.to(dx.dtype.element_ty), mask=mask)
|
|
253
373
|
|
|
254
374
|
|
|
255
|
-
|
|
375
|
+
@torch.compiler.disable
|
|
376
|
+
def swish_fwd(x: torch.Tensor, output_contiguous: bool = False) -> torch.Tensor:
|
|
377
|
+
x = _ensure_inner_contiguous(x)
|
|
256
378
|
T, D = x.numel(), x.shape[-1]
|
|
257
|
-
y =
|
|
258
|
-
swish_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
379
|
+
y = _alloc_output(x, output_contiguous)
|
|
380
|
+
swish_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
381
|
+
x, y, T=T, D=D,
|
|
382
|
+
stride_x_row=_get_stride(x),
|
|
383
|
+
stride_y_row=_get_stride(y),
|
|
384
|
+
)
|
|
259
385
|
return y
|
|
260
386
|
|
|
261
387
|
|
|
262
|
-
|
|
388
|
+
@torch.compiler.disable
|
|
389
|
+
def swish_bwd(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool = False) -> torch.Tensor:
|
|
390
|
+
x = _ensure_inner_contiguous(x)
|
|
391
|
+
dy = _ensure_inner_contiguous(dy)
|
|
263
392
|
T, D = x.numel(), x.shape[-1]
|
|
264
|
-
dx =
|
|
265
|
-
swish_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
393
|
+
dx = _alloc_output(x, output_contiguous)
|
|
394
|
+
swish_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
395
|
+
x, dy, dx, T=T, D=D,
|
|
396
|
+
stride_x_row=_get_stride(x),
|
|
397
|
+
stride_dy_row=_get_stride(dy),
|
|
398
|
+
stride_dx_row=_get_stride(dx),
|
|
399
|
+
)
|
|
266
400
|
return dx
|
|
267
401
|
|
|
268
402
|
|
|
269
403
|
class SwishFunction(torch.autograd.Function):
|
|
270
404
|
|
|
271
405
|
@staticmethod
|
|
406
|
+
@input_guard(no_guard_contiguous=True)
|
|
272
407
|
def forward(ctx, x):
|
|
273
408
|
ctx.save_for_backward(x)
|
|
274
409
|
return swish_fwd(x)
|
|
275
410
|
|
|
276
411
|
@staticmethod
|
|
412
|
+
@input_guard(no_guard_contiguous=True)
|
|
277
413
|
def backward(ctx, dout):
|
|
278
414
|
x, = ctx.saved_tensors
|
|
279
415
|
return swish_bwd(x, dout)
|
|
@@ -412,17 +548,25 @@ sqrelu = SquaredReLUFunction.apply
|
|
|
412
548
|
def swiglu_fwd_kernel(
|
|
413
549
|
x, y, z,
|
|
414
550
|
T,
|
|
415
|
-
B: tl.constexpr,
|
|
416
551
|
D: tl.constexpr,
|
|
552
|
+
stride_x_row,
|
|
553
|
+
stride_y_row,
|
|
554
|
+
stride_z_row,
|
|
555
|
+
B: tl.constexpr,
|
|
417
556
|
):
|
|
418
557
|
pid = tl.program_id(0)
|
|
419
558
|
offs = pid * B + tl.arange(0, B)
|
|
420
559
|
mask = offs < T
|
|
421
|
-
|
|
422
|
-
|
|
560
|
+
row = offs // D
|
|
561
|
+
col = offs % D
|
|
562
|
+
x_off = row * stride_x_row + col
|
|
563
|
+
y_off = row * stride_y_row + col
|
|
564
|
+
z_off = row * stride_z_row + col
|
|
565
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
566
|
+
y_val = tl.load(y + y_off, mask=mask, other=0.).to(tl.float32)
|
|
423
567
|
s = 1.0 / (1.0 + exp(-x_val))
|
|
424
568
|
z_val = x_val * s * y_val
|
|
425
|
-
tl.store(z +
|
|
569
|
+
tl.store(z + z_off, z_val.to(z.dtype.element_ty), mask=mask)
|
|
426
570
|
|
|
427
571
|
|
|
428
572
|
@triton.heuristics({
|
|
@@ -441,46 +585,87 @@ def swiglu_fwd_kernel(
|
|
|
441
585
|
def swiglu_fwdbwd_kernel(
|
|
442
586
|
x, y, g, dx, dy, z,
|
|
443
587
|
T,
|
|
444
|
-
B: tl.constexpr,
|
|
445
588
|
D: tl.constexpr,
|
|
589
|
+
stride_x_row,
|
|
590
|
+
stride_y_row,
|
|
591
|
+
stride_g_row,
|
|
592
|
+
stride_dx_row,
|
|
593
|
+
stride_dy_row,
|
|
594
|
+
stride_z_row,
|
|
595
|
+
B: tl.constexpr,
|
|
446
596
|
HAS_WEIGHT: tl.constexpr,
|
|
447
597
|
):
|
|
448
598
|
pid = tl.program_id(0)
|
|
449
599
|
offs = pid * B + tl.arange(0, B)
|
|
450
600
|
mask = offs < T
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
601
|
+
row = offs // D
|
|
602
|
+
col = offs % D
|
|
603
|
+
x_off = row * stride_x_row + col
|
|
604
|
+
y_off = row * stride_y_row + col
|
|
605
|
+
g_off = row * stride_g_row + col
|
|
606
|
+
dx_off = row * stride_dx_row + col
|
|
607
|
+
dy_off = row * stride_dy_row + col
|
|
608
|
+
x_val = tl.load(x + x_off, mask=mask, other=0.).to(tl.float32)
|
|
609
|
+
y_val = tl.load(y + y_off, mask=mask, other=0.).to(tl.float32)
|
|
610
|
+
g_val = tl.load(g + g_off, mask=mask, other=0.).to(tl.float32)
|
|
454
611
|
|
|
455
612
|
s = 1.0 / (1.0 + exp(-x_val))
|
|
456
613
|
x_s = x_val * s
|
|
457
614
|
dx_val = g_val * s * (1.0 + x_val * (1.0 - s)) * y_val
|
|
458
615
|
dy_val = g_val * x_s
|
|
459
616
|
|
|
460
|
-
tl.store(dx +
|
|
461
|
-
tl.store(dy +
|
|
617
|
+
tl.store(dx + dx_off, dx_val.to(dx.dtype.element_ty), mask=mask)
|
|
618
|
+
tl.store(dy + dy_off, dy_val.to(dy.dtype.element_ty), mask=mask)
|
|
462
619
|
if HAS_WEIGHT:
|
|
620
|
+
z_off = row * stride_z_row + col
|
|
463
621
|
z_val = x_s * y_val
|
|
464
|
-
tl.store(z +
|
|
622
|
+
tl.store(z + z_off, z_val.to(z.dtype.element_ty), mask=mask)
|
|
465
623
|
|
|
466
624
|
|
|
467
|
-
|
|
625
|
+
@torch.compiler.disable
|
|
626
|
+
def swiglu_fwd(x: torch.Tensor, y: torch.Tensor, output_contiguous: bool = False) -> torch.Tensor:
|
|
627
|
+
assert x.shape == y.shape, f"swiglu_fwd: shape mismatch x={x.shape} y={y.shape}"
|
|
628
|
+
x = _ensure_inner_contiguous(x)
|
|
629
|
+
y = _ensure_inner_contiguous(y)
|
|
468
630
|
T, D = x.numel(), x.shape[-1]
|
|
469
|
-
z =
|
|
470
|
-
swiglu_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
631
|
+
z = _alloc_output(x, output_contiguous)
|
|
632
|
+
swiglu_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
633
|
+
x, y, z, T=T, D=D,
|
|
634
|
+
stride_x_row=_get_stride(x),
|
|
635
|
+
stride_y_row=_get_stride(y),
|
|
636
|
+
stride_z_row=_get_stride(z),
|
|
637
|
+
)
|
|
471
638
|
return z
|
|
472
639
|
|
|
473
640
|
|
|
474
|
-
|
|
641
|
+
@torch.compiler.disable
|
|
642
|
+
def swiglu_fwdbwd(
|
|
643
|
+
x: torch.Tensor,
|
|
644
|
+
y: torch.Tensor,
|
|
645
|
+
g: torch.Tensor,
|
|
646
|
+
use_weight: bool = False,
|
|
647
|
+
output_contiguous: bool = False,
|
|
648
|
+
):
|
|
649
|
+
assert x.shape == y.shape == g.shape, f"swiglu_fwdbwd: shape mismatch x={x.shape} y={y.shape} g={g.shape}"
|
|
650
|
+
x = _ensure_inner_contiguous(x)
|
|
651
|
+
y = _ensure_inner_contiguous(y)
|
|
652
|
+
g = _ensure_inner_contiguous(g)
|
|
475
653
|
T, D = x.numel(), x.shape[-1]
|
|
476
|
-
dx =
|
|
477
|
-
dy =
|
|
654
|
+
dx = _alloc_output(x, output_contiguous)
|
|
655
|
+
dy = _alloc_output(y, output_contiguous)
|
|
478
656
|
if use_weight:
|
|
479
|
-
|
|
480
|
-
z = torch.empty_like(x)
|
|
657
|
+
z = _alloc_output(x, output_contiguous)
|
|
481
658
|
else:
|
|
482
659
|
z = None
|
|
483
|
-
swiglu_fwdbwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
660
|
+
swiglu_fwdbwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](
|
|
661
|
+
x, y, g, dx, dy, z, T=T, D=D,
|
|
662
|
+
stride_x_row=_get_stride(x),
|
|
663
|
+
stride_y_row=_get_stride(y),
|
|
664
|
+
stride_g_row=_get_stride(g),
|
|
665
|
+
stride_dx_row=_get_stride(dx),
|
|
666
|
+
stride_dy_row=_get_stride(dy),
|
|
667
|
+
stride_z_row=_get_stride(z) if z is not None else 0,
|
|
668
|
+
)
|
|
484
669
|
if use_weight:
|
|
485
670
|
return dx, dy, z
|
|
486
671
|
return dx, dy
|
|
@@ -495,11 +680,13 @@ class SwiGLUFunction(torch.autograd.Function):
|
|
|
495
680
|
"""
|
|
496
681
|
|
|
497
682
|
@staticmethod
|
|
683
|
+
@input_guard(no_guard_contiguous=True)
|
|
498
684
|
def forward(ctx, x, y):
|
|
499
685
|
ctx.save_for_backward(x, y)
|
|
500
686
|
return swiglu_fwd(x, y)
|
|
501
687
|
|
|
502
688
|
@staticmethod
|
|
689
|
+
@input_guard(no_guard_contiguous=True)
|
|
503
690
|
def backward(ctx, dout):
|
|
504
691
|
x, y = ctx.saved_tensors
|
|
505
692
|
return swiglu_fwdbwd(x, y, dout)
|
|
@@ -516,22 +703,23 @@ class SwiGLULinearFunction(torch.autograd.Function):
|
|
|
516
703
|
"""
|
|
517
704
|
|
|
518
705
|
@staticmethod
|
|
706
|
+
@input_guard(no_guard_contiguous=True)
|
|
519
707
|
@autocast_custom_fwd
|
|
520
708
|
def forward(ctx, x, y, weight, bias):
|
|
521
|
-
z = swiglu_fwd(x, y)
|
|
709
|
+
z = swiglu_fwd(x, y, output_contiguous=True)
|
|
522
710
|
out = F.linear(z, weight, bias)
|
|
523
|
-
# We don't store z, will be recomputed in the backward pass to save memory
|
|
524
711
|
ctx.save_for_backward(x, y, weight)
|
|
525
712
|
ctx.linear_bias_is_none = bias is None
|
|
526
713
|
return out
|
|
527
714
|
|
|
528
715
|
@staticmethod
|
|
716
|
+
@input_guard(no_guard_contiguous=True)
|
|
529
717
|
@autocast_custom_bwd
|
|
530
718
|
def backward(ctx, dout, *args):
|
|
531
719
|
x, y, weight = ctx.saved_tensors
|
|
532
720
|
dout = dout.reshape(-1, dout.shape[-1])
|
|
533
721
|
dz = F.linear(dout, weight.t()).view_as(x)
|
|
534
|
-
dx, dy, z = swiglu_fwdbwd(x, y, dz, use_weight=True)
|
|
722
|
+
dx, dy, z = swiglu_fwdbwd(x, y, dz, use_weight=True, output_contiguous=True)
|
|
535
723
|
dlinear_weight = torch.einsum("bo,bi->oi", dout, z.reshape(-1, z.shape[-1]))
|
|
536
724
|
dlinear_bias = None if ctx.linear_bias_is_none else dout.sum(0)
|
|
537
725
|
return dx, dy, dlinear_weight, dlinear_bias
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
2
|
+
|
|
3
|
+
from .causal_conv1d import causal_conv1d
|
|
4
|
+
from .long_conv import ImplicitLongConvolution, LongConvolution, PositionalEmbedding, fft_conv
|
|
5
|
+
from .short_conv import ShortConvolution
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
'ImplicitLongConvolution',
|
|
9
|
+
'LongConvolution',
|
|
10
|
+
'PositionalEmbedding',
|
|
11
|
+
'ShortConvolution',
|
|
12
|
+
'causal_conv1d',
|
|
13
|
+
'fft_conv',
|
|
14
|
+
]
|