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.
Files changed (241) hide show
  1. {fla_core-0.4.1/fla_core.egg-info → fla_core-0.4.2}/PKG-INFO +1 -1
  2. {fla_core-0.4.1 → fla_core-0.4.2}/fla/__init__.py +1 -1
  3. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/activations.py +256 -68
  4. fla_core-0.4.2/fla/modules/conv/__init__.py +14 -0
  5. fla_core-0.4.2/fla/modules/conv/causal_conv1d.py +124 -0
  6. fla_core-0.4.2/fla/modules/conv/cp/__init__.py +6 -0
  7. fla_core-0.4.2/fla/modules/conv/cp/ops.py +251 -0
  8. fla_core-0.4.2/fla/modules/conv/cuda/__init__.py +9 -0
  9. fla_core-0.4.2/fla/modules/conv/cuda/ops.py +228 -0
  10. fla_core-0.4.2/fla/modules/conv/long_conv.py +165 -0
  11. fla_core-0.4.2/fla/modules/conv/short_conv.py +241 -0
  12. fla_core-0.4.2/fla/modules/conv/triton/__init__.py +19 -0
  13. fla_core-0.4.2/fla/modules/conv/triton/kernels.py +657 -0
  14. fla_core-0.4.2/fla/modules/conv/triton/ops.py +397 -0
  15. fla_core-0.4.2/fla/modules/convolution.py +38 -0
  16. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_norm_gate.py +71 -85
  17. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/l2norm.py +26 -30
  18. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/rotary.py +14 -1
  19. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/__init__.py +2 -0
  20. fla_core-0.4.2/fla/ops/attn/naive.py +64 -0
  21. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/parallel.py +12 -6
  22. fla_core-0.4.2/fla/ops/backends/__init__.py +168 -0
  23. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/__init__.py +3 -0
  24. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/chunk.py +31 -7
  25. fla_core-0.4.2/fla/ops/comba/naive.py +166 -0
  26. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/utils.py +6 -2
  27. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/wy_fast.py +9 -3
  28. fla_core-0.4.2/fla/ops/common/backends/__init__.py +12 -0
  29. fla_core-0.4.2/fla/ops/common/backends/intracard.py +93 -0
  30. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_delta_h.py +306 -103
  31. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h_parallel.py +6 -2
  32. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_o.py +40 -14
  33. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_scaled_dot_kkt.py +3 -1
  34. fla_core-0.4.2/fla/ops/common/intracard_cp.py +597 -0
  35. fla_core-0.4.2/fla/ops/cp/__init__.py +25 -0
  36. fla_core-0.4.2/fla/ops/cp/chunk_delta_h.py +1314 -0
  37. fla_core-0.4.2/fla/ops/cp/comm.py +162 -0
  38. fla_core-0.4.2/fla/ops/cp/context.py +165 -0
  39. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/chunk.py +23 -5
  40. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/wy_fast.py +10 -2
  41. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/forgetting_attn/__init__.py +2 -0
  42. fla_core-0.4.2/fla/ops/forgetting_attn/naive.py +43 -0
  43. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk.py +32 -5
  44. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_deltaproduct_h.py +6 -2
  45. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_deltaproduct_o.py +3 -1
  46. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/__init__.py +3 -0
  47. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/chunk.py +104 -7
  48. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/fused_recurrent.py +50 -17
  49. fla_core-0.4.2/fla/ops/gated_delta_rule/naive.py +156 -0
  50. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_rule/wy_fast.py +8 -4
  51. fla_core-0.4.2/fla/ops/gated_oja_rule/__init__.py +7 -0
  52. fla_core-0.4.2/fla/ops/gated_oja_rule/chunk.py +338 -0
  53. fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_h.py +807 -0
  54. fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_kkt.py +521 -0
  55. fla_core-0.4.2/fla/ops/gated_oja_rule/chunk_o.py +674 -0
  56. fla_core-0.4.2/fla/ops/gated_oja_rule/fused_recurrent.py +263 -0
  57. fla_core-0.4.2/fla/ops/gated_oja_rule/wy_fast.py +290 -0
  58. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk.py +126 -49
  59. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +178 -2
  60. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +174 -2
  61. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +3 -1
  62. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +3 -1
  63. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +9 -3
  64. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +3 -1
  65. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +3 -1
  66. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +7 -2
  67. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/chunk.py +18 -2
  68. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/wy_fast.py +7 -2
  69. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/chunk.py +49 -20
  70. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/chunk.py +29 -7
  71. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk.py +127 -223
  72. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_bwd.py +211 -15
  73. fla_core-0.4.2/fla/ops/kda/chunk_fwd.py +132 -0
  74. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_intra.py +363 -157
  75. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/chunk_intra_token_parallel.py +5 -3
  76. fla_core-0.4.2/fla/ops/kda/fused_recurrent.py +434 -0
  77. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/gate.py +184 -20
  78. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/wy_fast.py +10 -12
  79. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/utils.py +0 -1
  80. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk.py +17 -4
  81. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_cg_solver_bwd.py +3 -1
  82. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_cg_solver_fwd.py +3 -1
  83. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +4 -2
  84. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +4 -1
  85. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +3 -1
  86. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/compression.py +12 -6
  87. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/parallel.py +3 -2
  88. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/__init__.py +2 -0
  89. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/cumprod_householder_bwd.py +4 -1
  90. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/cumprod_householder_fwd.py +4 -1
  91. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_bwd.py +5 -2
  92. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +8 -4
  93. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/intra_chunk_preprocess_fwd.py +8 -5
  94. fla_core-0.4.2/fla/ops/path_attn/naive.py +92 -0
  95. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +5 -2
  96. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +5 -2
  97. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_bwd_intra.py +5 -2
  98. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel_path_fwd.py +4 -1
  99. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/prepare_k_cache.py +4 -2
  100. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/transform_q.py +6 -3
  101. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/chunk.py +36 -15
  102. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/recurrent_naive.py +0 -1
  103. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/chunk.py +17 -2
  104. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_k_update.py +11 -8
  105. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/chunk.py +21 -6
  106. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/parallel.py +18 -7
  107. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/chunk.py +39 -10
  108. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/__init__.py +0 -2
  109. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/index.py +24 -19
  110. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/op.py +16 -8
  111. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/pooling.py +6 -2
  112. {fla_core-0.4.1 → fla_core-0.4.2}/fla/utils.py +93 -29
  113. {fla_core-0.4.1 → fla_core-0.4.2/fla_core.egg-info}/PKG-INFO +1 -1
  114. {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/SOURCES.txt +32 -0
  115. {fla_core-0.4.1 → fla_core-0.4.2}/pyproject.toml +1 -1
  116. fla_core-0.4.1/fla/modules/convolution.py +0 -1183
  117. fla_core-0.4.1/fla/ops/kda/fused_recurrent.py +0 -112
  118. {fla_core-0.4.1 → fla_core-0.4.2}/LICENSE +0 -0
  119. {fla_core-0.4.1 → fla_core-0.4.2}/README.md +0 -0
  120. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/__init__.py +0 -0
  121. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/feature_map.py +0 -0
  122. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_bitlinear.py +0 -0
  123. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_cross_entropy.py +0 -0
  124. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_kl_div.py +0 -0
  125. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/fused_linear_cross_entropy.py +0 -0
  126. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/grpo.py +0 -0
  127. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/l2warp.py +0 -0
  128. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/layernorm.py +0 -0
  129. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/layernorm_gated.py +0 -0
  130. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/mlp.py +0 -0
  131. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/parallel.py +0 -0
  132. {fla_core-0.4.1 → fla_core-0.4.2}/fla/modules/token_shift.py +0 -0
  133. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/__init__.py +0 -0
  134. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/__init__.py +0 -0
  135. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/chunk.py +0 -0
  136. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/abc/naive.py +0 -0
  137. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/attn/decoding.py +0 -0
  138. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/__init__.py +0 -0
  139. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/fused_chunk.py +0 -0
  140. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/naive.py +0 -0
  141. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/based/parallel.py +0 -0
  142. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/comba/fused_recurrent.py +0 -0
  143. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/__init__.py +0 -0
  144. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h.py +0 -0
  145. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/chunk_h_split.py +0 -0
  146. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/fused_chunk.py +0 -0
  147. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/common/fused_recurrent.py +0 -0
  148. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/__init__.py +0 -0
  149. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/fused_chunk.py +0 -0
  150. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/fused_recurrent.py +0 -0
  151. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/naive.py +0 -0
  152. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/delta_rule/parallel.py +0 -0
  153. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/__init__.py +0 -0
  154. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/invcum.py +0 -0
  155. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/naive.py +0 -0
  156. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/deltaformer/parallel.py +0 -0
  157. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/forgetting_attn/parallel.py +0 -0
  158. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/__init__.py +0 -0
  159. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/chunk_ref.py +0 -0
  160. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gated_delta_product/naive.py +0 -0
  161. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/__init__.py +0 -0
  162. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/__init__.py +0 -0
  163. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +0 -0
  164. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/dplr/naive.py +0 -0
  165. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/__init__.py +0 -0
  166. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +0 -0
  167. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/generalized_delta_rule/iplr/naive.py +0 -0
  168. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/__init__.py +0 -0
  169. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/fused_chunk.py +0 -0
  170. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/fused_recurrent.py +0 -0
  171. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gla/naive.py +0 -0
  172. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/__init__.py +0 -0
  173. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/fused_recurrent.py +0 -0
  174. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/gsa/naive.py +0 -0
  175. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/__init__.py +0 -0
  176. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/chunk.py +0 -0
  177. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/fused_recurrent.py +0 -0
  178. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/hgrn/naive.py +0 -0
  179. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/__init__.py +0 -0
  180. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/kda/naive.py +0 -0
  181. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/__init__.py +0 -0
  182. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/chunk.py +0 -0
  183. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/lightning_attn/fused_recurrent.py +0 -0
  184. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/__init__.py +0 -0
  185. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/chunk.py +0 -0
  186. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/fused_chunk.py +0 -0
  187. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/fused_recurrent.py +0 -0
  188. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/linear_attn/naive.py +0 -0
  189. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/__init__.py +0 -0
  190. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/chunk.py +0 -0
  191. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/log_linear_attn/naive.py +0 -0
  192. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/__init__.py +0 -0
  193. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/chunk_h_fwd.py +0 -0
  194. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/decoding_one_step.py +0 -0
  195. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/mesa_net/naive.py +0 -0
  196. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/__init__.py +0 -0
  197. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/naive.py +0 -0
  198. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/nsa/utils.py +0 -0
  199. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/path_attn/parallel.py +0 -0
  200. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/__init__.py +0 -0
  201. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/naive.py +0 -0
  202. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rebased/parallel.py +0 -0
  203. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/__init__.py +0 -0
  204. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/chunk.py +0 -0
  205. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/fused_chunk.py +0 -0
  206. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/fused_recurrent.py +0 -0
  207. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/naive.py +0 -0
  208. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/retention/parallel.py +0 -0
  209. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv4/__init__.py +0 -0
  210. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv4/fused_recurrent.py +0 -0
  211. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/__init__.py +0 -0
  212. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/chunk_naive.py +0 -0
  213. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv6/fused_recurrent.py +0 -0
  214. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/__init__.py +0 -0
  215. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/channel_mixing.py +0 -0
  216. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_addcmul.py +0 -0
  217. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/fused_recurrent.py +0 -0
  218. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/rwkv7/gate_output_correction.py +0 -0
  219. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/__init__.py +0 -0
  220. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/fused_chunk.py +0 -0
  221. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/fused_recurrent.py +0 -0
  222. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/simple_gla/naive.py +0 -0
  223. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/__init__.py +0 -0
  224. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/log_impl.py +0 -0
  225. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/titans/naive.py +0 -0
  226. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/__init__.py +0 -0
  227. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/fused_chunk.py +0 -0
  228. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/ttt/naive.py +0 -0
  229. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/constant.py +0 -0
  230. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/cumsum.py +0 -0
  231. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/logcumsumexp.py +0 -0
  232. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/logsumexp.py +0 -0
  233. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/matmul.py +0 -0
  234. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/pack.py +0 -0
  235. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/softmax.py +0 -0
  236. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/softplus.py +0 -0
  237. {fla_core-0.4.1 → fla_core-0.4.2}/fla/ops/utils/solve_tril.py +0 -0
  238. {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/dependency_links.txt +0 -0
  239. {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/requires.txt +0 -0
  240. {fla_core-0.4.1 → fla_core-0.4.2}/fla_core.egg-info/top_level.txt +0 -0
  241. {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.1
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
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  __path__ = __import__('pkgutil').extend_path(__path__, __name__)
4
- __version__ = '0.4.1'
4
+ __version__ = '0.4.2'
@@ -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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, y_val.to(y.dtype.element_ty), mask=mask)
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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
63
- g_val = tl.load(dy + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, dx_val.to(dx.dtype.element_ty), mask=mask)
132
+ tl.store(dx + dx_off, dx_val.to(dx.dtype.element_ty), mask=mask)
67
133
 
68
134
 
69
- def sigmoid_fwd(x: torch.Tensor) -> torch.Tensor:
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 = torch.empty_like(x)
72
- sigmoid_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](x, y, T=T, D=D)
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
- def sigmoid_bwd(x: torch.Tensor, dy: torch.Tensor) -> torch.Tensor:
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 = torch.empty_like(x)
79
- sigmoid_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](x, dy, dx, T=T, D=D)
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 + o_i, mask=m_i, other=0.).to(tl.float32)
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 + o_i, b_y.to(y.dtype.element_ty), mask=m_i)
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
- b_x = tl.load(x + o_i, mask=m_i, other=0.).to(tl.float32)
152
- b_dy = tl.load(dy + o_i, mask=m_i, other=0.).to(tl.float32)
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 + o_i, b_dx.to(dx.dtype.element_ty), mask=m_i)
250
+ tl.store(dx + dx_off, b_dx.to(dx.dtype.element_ty), mask=m_i)
155
251
 
156
252
 
157
- def logsigmoid_fwd(x: torch.Tensor, temperature: float = 1.) -> torch.Tensor:
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 = torch.empty_like(x)
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
- def logsigmoid_bwd(x: torch.Tensor, dy: torch.Tensor, temperature: float = 1.) -> torch.Tensor:
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 = torch.empty_like(x)
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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, y_val.to(y.dtype.element_ty), mask=mask)
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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
249
- g_val = tl.load(dy + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, dx_val.to(dx.dtype.element_ty), mask=mask)
372
+ tl.store(dx + dx_off, dx_val.to(dx.dtype.element_ty), mask=mask)
253
373
 
254
374
 
255
- def swish_fwd(x: torch.Tensor) -> torch.Tensor:
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 = torch.empty_like(x)
258
- swish_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](x, y, T=T, D=D)
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
- def swish_bwd(x: torch.Tensor, dy: torch.Tensor) -> torch.Tensor:
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 = torch.empty_like(x)
265
- swish_bwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](x, dy, dx, T=T, D=D)
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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
422
- y_val = tl.load(y + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, z_val.to(z.dtype.element_ty), mask=mask)
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
- x_val = tl.load(x + offs, mask=mask, other=0.).to(tl.float32)
452
- y_val = tl.load(y + offs, mask=mask, other=0.).to(tl.float32)
453
- g_val = tl.load(g + offs, mask=mask, other=0.).to(tl.float32)
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 + offs, dx_val.to(dx.dtype.element_ty), mask=mask)
461
- tl.store(dy + offs, dy_val.to(dy.dtype.element_ty), mask=mask)
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 + offs, z_val.to(z.dtype.element_ty), mask=mask)
622
+ tl.store(z + z_off, z_val.to(z.dtype.element_ty), mask=mask)
465
623
 
466
624
 
467
- def swiglu_fwd(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
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 = torch.empty_like(x)
470
- swiglu_fwd_kernel[lambda meta: (triton.cdiv(T, meta['B']),)](x, y, z, T=T, D=D)
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
- def swiglu_fwdbwd(x: torch.Tensor, y: torch.Tensor, g: torch.Tensor, use_weight: bool = False):
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 = torch.empty_like(x)
477
- dy = torch.empty_like(x)
654
+ dx = _alloc_output(x, output_contiguous)
655
+ dy = _alloc_output(y, output_contiguous)
478
656
  if use_weight:
479
- # recomputed for weight grad
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']),)](x, y, g, dx, dy, z, T=T, D=D)
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
+ ]