FlashRWKV2 0.1.0a12__py3-none-any.whl

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 (121) hide show
  1. flashrwkv2/__init__.py +97 -0
  2. flashrwkv2/_csrc/bindings.cpp +44 -0
  3. flashrwkv2/_csrc/bindings.h +46 -0
  4. flashrwkv2/_csrc/registration.cpp +8 -0
  5. flashrwkv2/_csrc/sm80/cmix/infer_fp16_forward_varlen.cpp +287 -0
  6. flashrwkv2/_csrc/sm80/cmix/infer_fp16_forward_varlen.cu +355 -0
  7. flashrwkv2/_csrc/sm80/cmix/internal/activation/infer_fp16_forward_varlen.cpp +20 -0
  8. flashrwkv2/_csrc/sm80/cmix/internal/activation/infer_fp16_forward_varlen.cu +52 -0
  9. flashrwkv2/_csrc/sm80/cmix/internal/dense/infer_fp16_forward_varlen.cpp +28 -0
  10. flashrwkv2/_csrc/sm80/cmix/internal/dense/infer_fp16_forward_varlen.cu +138 -0
  11. flashrwkv2/_csrc/sm80/cmix/internal/sparse/infer_fp16_forward_varlen.cpp +37 -0
  12. flashrwkv2/_csrc/sm80/cmix/internal/sparse/infer_fp16_forward_varlen.cu +825 -0
  13. flashrwkv2/_csrc/sm80/cmix/pretrain_bf16_backward.cpp +50 -0
  14. flashrwkv2/_csrc/sm80/cmix/pretrain_bf16_backward.cu +346 -0
  15. flashrwkv2/_csrc/sm80/cmix/pretrain_bf16_forward.cpp +46 -0
  16. flashrwkv2/_csrc/sm80/cmix/pretrain_bf16_forward.cu +330 -0
  17. flashrwkv2/_csrc/sm80/cmix/statetune_bf16_backward.cpp +71 -0
  18. flashrwkv2/_csrc/sm80/cmix/statetune_bf16_backward.cu +179 -0
  19. flashrwkv2/_csrc/sm80/cmix/statetune_bf16_forward.cpp +61 -0
  20. flashrwkv2/_csrc/sm80/cmix/statetune_bf16_forward.cu +116 -0
  21. flashrwkv2/_csrc/sm80/embedding/infer_fp16_forward_varlen.cpp +48 -0
  22. flashrwkv2/_csrc/sm80/embedding/infer_fp16_forward_varlen.cu +126 -0
  23. flashrwkv2/_csrc/sm80/head/l2wrap_ce/pretrain_bf16_forward.cpp +70 -0
  24. flashrwkv2/_csrc/sm80/head/l2wrap_ce/pretrain_bf16_forward.cu +292 -0
  25. flashrwkv2/_csrc/sm80/head/linear/infer_fp16_forward_varlen.cpp +53 -0
  26. flashrwkv2/_csrc/sm80/head/linear/infer_fp16_forward_varlen.cu +131 -0
  27. flashrwkv2/_csrc/sm80/internal/linear/backend.cuh +42 -0
  28. flashrwkv2/_csrc/sm80/internal/linear/infer_fp16_forward_varlen.cu +1529 -0
  29. flashrwkv2/_csrc/sm80/loss/l2wrap_ce/pretrain_bf16_backward.cpp +47 -0
  30. flashrwkv2/_csrc/sm80/loss/l2wrap_ce/pretrain_bf16_backward.cu +305 -0
  31. flashrwkv2/_csrc/sm80/loss/l2wrap_ce/pretrain_bf16_forward.cpp +43 -0
  32. flashrwkv2/_csrc/sm80/loss/l2wrap_ce/pretrain_bf16_forward.cu +307 -0
  33. flashrwkv2/_csrc/sm80/post_norm/infer_fp16_forward_varlen.cpp +66 -0
  34. flashrwkv2/_csrc/sm80/post_norm/infer_fp16_forward_varlen.cu +641 -0
  35. flashrwkv2/_csrc/sm80/sampling/infer_fp32_forward_varlen.cpp +253 -0
  36. flashrwkv2/_csrc/sm80/sampling/infer_fp32_forward_varlen.cu +1562 -0
  37. flashrwkv2/_csrc/sm80/tmix/a_gate/pretrain_bf16_backward.cpp +40 -0
  38. flashrwkv2/_csrc/sm80/tmix/a_gate/pretrain_bf16_backward.cu +157 -0
  39. flashrwkv2/_csrc/sm80/tmix/a_gate/pretrain_bf16_forward.cpp +41 -0
  40. flashrwkv2/_csrc/sm80/tmix/a_gate/pretrain_bf16_forward.cu +151 -0
  41. flashrwkv2/_csrc/sm80/tmix/kk_pre/pretrain_bf16_backward.cpp +110 -0
  42. flashrwkv2/_csrc/sm80/tmix/kk_pre/pretrain_bf16_backward.cu +330 -0
  43. flashrwkv2/_csrc/sm80/tmix/kk_pre/pretrain_bf16_forward.cpp +78 -0
  44. flashrwkv2/_csrc/sm80/tmix/kk_pre/pretrain_bf16_forward.cu +283 -0
  45. flashrwkv2/_csrc/sm80/tmix/readout/infer_fp16_forward_varlen.cpp +161 -0
  46. flashrwkv2/_csrc/sm80/tmix/readout/infer_fp16_forward_varlen.cu +386 -0
  47. flashrwkv2/_csrc/sm80/tmix/readout/internal/linear/infer_fp16_forward_varlen.cu +106 -0
  48. flashrwkv2/_csrc/sm80/tmix/readout/pretrain_bf16_backward.cpp +110 -0
  49. flashrwkv2/_csrc/sm80/tmix/readout/pretrain_bf16_backward.cu +414 -0
  50. flashrwkv2/_csrc/sm80/tmix/readout/pretrain_bf16_forward.cpp +98 -0
  51. flashrwkv2/_csrc/sm80/tmix/readout/pretrain_bf16_forward.cu +362 -0
  52. flashrwkv2/_csrc/sm80/tmix/tokenshift/infer_fp16_forward_varlen.cpp +200 -0
  53. flashrwkv2/_csrc/sm80/tmix/tokenshift/infer_fp16_forward_varlen.cu +446 -0
  54. flashrwkv2/_csrc/sm80/tmix/tokenshift/pretrain_bf16_backward.cpp +57 -0
  55. flashrwkv2/_csrc/sm80/tmix/tokenshift/pretrain_bf16_backward.cu +317 -0
  56. flashrwkv2/_csrc/sm80/tmix/tokenshift/pretrain_bf16_forward.cpp +44 -0
  57. flashrwkv2/_csrc/sm80/tmix/tokenshift/pretrain_bf16_forward.cu +292 -0
  58. flashrwkv2/_csrc/sm80/tmix/tokenshift/statetune_bf16_backward.cpp +82 -0
  59. flashrwkv2/_csrc/sm80/tmix/tokenshift/statetune_bf16_backward.cu +231 -0
  60. flashrwkv2/_csrc/sm80/tmix/tokenshift/statetune_bf16_forward.cpp +63 -0
  61. flashrwkv2/_csrc/sm80/tmix/tokenshift/statetune_bf16_forward.cu +119 -0
  62. flashrwkv2/_csrc/sm80/tmix/vres_gate/pretrain_bf16_backward.cpp +47 -0
  63. flashrwkv2/_csrc/sm80/tmix/vres_gate/pretrain_bf16_backward.cu +185 -0
  64. flashrwkv2/_csrc/sm80/tmix/vres_gate/pretrain_bf16_forward.cpp +46 -0
  65. flashrwkv2/_csrc/sm80/tmix/vres_gate/pretrain_bf16_forward.cu +171 -0
  66. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_chunk_bf16_forward_varlen.cpp +205 -0
  67. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_chunk_bf16_forward_varlen.cu +340 -0
  68. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_deltalog_fp16_forward_varlen.cpp +318 -0
  69. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_deltalog_fp16_forward_varlen.cu +881 -0
  70. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_deltalog_fp32io16_forward_varlen.cpp +309 -0
  71. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_deltalog_fp32io16_forward_varlen.cu +782 -0
  72. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_fp16_forward_varlen.cpp +202 -0
  73. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_fp16_forward_varlen.cu +1667 -0
  74. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_fp32io16_forward_varlen.cpp +478 -0
  75. flashrwkv2/_csrc/sm80/tmix/wkv7/infer_recurrent_fp32io16_forward_varlen.cu +1076 -0
  76. flashrwkv2/_csrc/sm80/tmix/wkv7/pretrain_recurrent_bf16_forward.cpp +63 -0
  77. flashrwkv2/_csrc/sm80/tmix/wkv7/pretrain_recurrent_bf16_forward.cu +486 -0
  78. flashrwkv2/_csrc/sm80/tmix/wkv7/recurrent_decay.cuh +48 -0
  79. flashrwkv2/_csrc/sm80/tmix/wkv7/rl_infctx_chunk_fp32io16_backward.cpp +136 -0
  80. flashrwkv2/_csrc/sm80/tmix/wkv7/rl_infctx_chunk_fp32io16_backward.cu +273 -0
  81. flashrwkv2/_csrc/sm80/tmix/wkv7/rl_infctx_chunk_fp32io16_forward.cpp +242 -0
  82. flashrwkv2/_csrc/sm80/tmix/wkv7/rl_infctx_chunk_fp32io16_forward.cu +923 -0
  83. flashrwkv2/_csrc/sm80/tmix/wkv7/rl_infctx_chunk_fp32io16_replay.cuh +43 -0
  84. flashrwkv2/_csrc/sm80/tmix/wkv7/statetune_recurrent_fp32io16_backward.cpp +82 -0
  85. flashrwkv2/_csrc/sm80/tmix/wkv7/statetune_recurrent_fp32io16_backward.cu +706 -0
  86. flashrwkv2/_csrc/sm80/tmix/wkv7/statetune_recurrent_fp32io16_forward.cpp +56 -0
  87. flashrwkv2/_csrc/sm80/tmix/wkv7/statetune_recurrent_fp32io16_forward.cu +244 -0
  88. flashrwkv2/_csrc/sm80/tmix/wkv_prepare/infer_fp16_forward_varlen.cpp +535 -0
  89. flashrwkv2/_csrc/sm80/tmix/wkv_prepare/infer_fp16_forward_varlen.cu +106 -0
  90. flashrwkv2/_csrc/sm80/tmix/wkv_prepare/internal/kk_a_gate/infer_fp16_forward_varlen.cu +272 -0
  91. flashrwkv2/_csrc/sm80/tmix/wkv_prepare/internal/lowrank/infer_fp16_forward_varlen.cu +906 -0
  92. flashrwkv2/_csrc/sm80/tmix/wkv_prepare/internal/lowrank/infer_vres_fp16_forward_varlen.cu +129 -0
  93. flashrwkv2/_csrc/validation/recurrent_metadata.cu +281 -0
  94. flashrwkv2/_csrc/validation.cpp +131 -0
  95. flashrwkv2/_csrc/validation.h +58 -0
  96. flashrwkv2/cmix/__init__.py +238 -0
  97. flashrwkv2/compile.py +355 -0
  98. flashrwkv2/embedding/__init__.py +33 -0
  99. flashrwkv2/head/l2wrap_ce/__init__.py +64 -0
  100. flashrwkv2/head/linear/__init__.py +63 -0
  101. flashrwkv2/loss/l2wrap_ce/__init__.py +54 -0
  102. flashrwkv2/post_norm/__init__.py +45 -0
  103. flashrwkv2/sampling/__init__.py +287 -0
  104. flashrwkv2/tmix/a_gate/__init__.py +47 -0
  105. flashrwkv2/tmix/kk_pre/__init__.py +74 -0
  106. flashrwkv2/tmix/readout/__init__.py +84 -0
  107. flashrwkv2/tmix/readout/pretrain.py +77 -0
  108. flashrwkv2/tmix/tokenshift/__init__.py +257 -0
  109. flashrwkv2/tmix/vres_gate/__init__.py +49 -0
  110. flashrwkv2/tmix/wkv7/__init__.py +1227 -0
  111. flashrwkv2/tmix/wkv7/chunk.py +131 -0
  112. flashrwkv2/tmix/wkv7/pretrain.py +133 -0
  113. flashrwkv2/tmix/wkv7/rl_infctx.py +162 -0
  114. flashrwkv2/tmix/wkv7/statetune.py +239 -0
  115. flashrwkv2/tmix/wkv_prepare/__init__.py +139 -0
  116. flashrwkv2-0.1.0a12.dist-info/METADATA +117 -0
  117. flashrwkv2-0.1.0a12.dist-info/RECORD +121 -0
  118. flashrwkv2-0.1.0a12.dist-info/WHEEL +5 -0
  119. flashrwkv2-0.1.0a12.dist-info/licenses/LICENSE +22 -0
  120. flashrwkv2-0.1.0a12.dist-info/licenses/NOTICE +131 -0
  121. flashrwkv2-0.1.0a12.dist-info/top_level.txt +1 -0
flashrwkv2/__init__.py ADDED
@@ -0,0 +1,97 @@
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ import sys as _sys
4
+
5
+ # PyTorch must be loaded before the private extensions, which link against its
6
+ # native libraries.
7
+
8
+
9
+ _C = None
10
+
11
+
12
+ def _extension():
13
+ global _C
14
+ if _C is None:
15
+ from .compile import load_extension
16
+
17
+ _C = load_extension().module
18
+ _sys.modules[f"{__name__}._C"] = _C
19
+ return _C
20
+
21
+ from .cmix import infer_cmix_forward_varlen, pretrain_cmix_bf16, statetune_cmix_bf16
22
+ from .embedding import infer_embedding_ln0_forward_varlen
23
+ from .head.l2wrap_ce import pretrain_head_l2wrap_ce_bf16
24
+ from .head.linear import (
25
+ infer_head_linear_all_forward_varlen,
26
+ infer_head_linear_last_forward_varlen,
27
+ )
28
+ from .loss.l2wrap_ce import pretrain_l2wrap_ce_bf16
29
+ from .post_norm import infer_post_norm_output_forward_varlen
30
+ from .sampling import (
31
+ infer_sampling_six_parameter_forward_varlen,
32
+ infer_sampling_temperature_topk_topp_forward_varlen,
33
+ setup_sampling_states,
34
+ )
35
+ from .tmix.a_gate import pretrain_tmix_a_gate_bf16
36
+ from .tmix.kk_pre import pretrain_tmix_kk_pre_bf16
37
+ from .tmix.readout import (
38
+ infer_tmix_readout_forward_varlen,
39
+ pretrain_tmix_readout_bf16,
40
+ )
41
+ from .tmix.tokenshift import (
42
+ infer_tmix_postnorm_tokenshift_forward_varlen,
43
+ pretrain_tmix_tokenshift_bf16,
44
+ statetune_tmix_tokenshift_bf16,
45
+ )
46
+ from .tmix.vres_gate import pretrain_tmix_vres_gate_bf16
47
+ from .tmix.wkv7 import (
48
+ infer_tmix_wkv7_chunk_bf16_forward_varlen,
49
+ infer_tmix_wkv7_recurrent_fp16_forward_varlen,
50
+ infer_tmix_wkv7_recurrent_fp32io16_forward_varlen,
51
+ prepare_tmix_wkv7_recurrent_fp16_state,
52
+ prepare_tmix_wkv7_recurrent_fp32io16_state,
53
+ prepare_tmix_wkv7_recurrent_fp32io16_state_from_tensor,
54
+ prepare_tmix_wkv7_recurrent_metadata,
55
+ pretrain_tmix_wkv7_recurrent_bf16,
56
+ rl_infctx_tmix_wkv7_chunk_fp32io16,
57
+ rl_infctx_tmix_wkv7_chunk_fp32io16_factor_recompute,
58
+ )
59
+ from .tmix.wkv7.statetune import statetune_tmix_wkv7_recurrent_fp32io16
60
+ from .tmix.wkv_prepare import infer_tmix_wkv_prepare_forward_varlen
61
+
62
+ __all__ = [
63
+ "infer_cmix_forward_varlen",
64
+ "infer_embedding_ln0_forward_varlen",
65
+ "infer_head_linear_all_forward_varlen",
66
+ "infer_head_linear_last_forward_varlen",
67
+ "infer_post_norm_output_forward_varlen",
68
+ "infer_sampling_six_parameter_forward_varlen",
69
+ "infer_sampling_temperature_topk_topp_forward_varlen",
70
+ "infer_tmix_postnorm_tokenshift_forward_varlen",
71
+ "infer_tmix_readout_forward_varlen",
72
+ "infer_tmix_wkv7_chunk_bf16_forward_varlen",
73
+ "infer_tmix_wkv7_recurrent_fp16_forward_varlen",
74
+ "infer_tmix_wkv7_recurrent_fp32io16_forward_varlen",
75
+ "infer_tmix_wkv_prepare_forward_varlen",
76
+ "prepare_tmix_wkv7_recurrent_fp16_state",
77
+ "prepare_tmix_wkv7_recurrent_fp32io16_state",
78
+ "prepare_tmix_wkv7_recurrent_fp32io16_state_from_tensor",
79
+ "prepare_tmix_wkv7_recurrent_metadata",
80
+ "pretrain_cmix_bf16",
81
+ "pretrain_head_l2wrap_ce_bf16",
82
+ "pretrain_l2wrap_ce_bf16",
83
+ "pretrain_tmix_a_gate_bf16",
84
+ "pretrain_tmix_kk_pre_bf16",
85
+ "pretrain_tmix_readout_bf16",
86
+ "pretrain_tmix_tokenshift_bf16",
87
+ "pretrain_tmix_vres_gate_bf16",
88
+ "pretrain_tmix_wkv7_recurrent_bf16",
89
+ "rl_infctx_tmix_wkv7_chunk_fp32io16",
90
+ "rl_infctx_tmix_wkv7_chunk_fp32io16_factor_recompute",
91
+ "setup_sampling_states",
92
+ "statetune_cmix_bf16",
93
+ "statetune_tmix_tokenshift_bf16",
94
+ "statetune_tmix_wkv7_recurrent_fp32io16",
95
+ ]
96
+
97
+ __version__ = "0.1.0a12"
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: Copyright contributors to the FlashRWKV2 project
3
+
4
+ #include "bindings.h"
5
+
6
+ void register_flashrwkv2_bindings(py::module_& module) {
7
+ register_infer_tmix_wkv7_recurrent_fp32io16_bindings(module);
8
+ register_infer_tmix_wkv7_recurrent_fp16_bindings(module);
9
+ register_infer_tmix_wkv7_recurrent_deltalog_fp16_bindings(module);
10
+ register_infer_tmix_wkv7_recurrent_deltalog_fp32io16_bindings(module);
11
+ register_tmix_tokenshift_bindings(module);
12
+ register_tmix_wkv_prepare_bindings(module);
13
+ register_tmix_readout_bindings(module);
14
+ register_cmix_bindings(module);
15
+ register_post_norm_bindings(module);
16
+ register_embedding_bindings(module);
17
+ register_head_linear_bindings(module);
18
+ register_sampling_bindings(module);
19
+ register_infer_tmix_wkv7_chunk_bindings(module);
20
+ register_pretrain_l2wrap_ce_forward_bindings(module);
21
+ register_pretrain_l2wrap_ce_backward_bindings(module);
22
+ register_pretrain_tmix_a_gate_forward_bindings(module);
23
+ register_pretrain_tmix_a_gate_backward_bindings(module);
24
+ register_pretrain_tmix_vres_gate_forward_bindings(module);
25
+ register_pretrain_tmix_vres_gate_backward_bindings(module);
26
+ register_pretrain_tmix_tokenshift_forward_bindings(module);
27
+ register_pretrain_tmix_tokenshift_backward_bindings(module);
28
+ register_statetune_tmix_tokenshift_forward_bindings(module);
29
+ register_statetune_tmix_tokenshift_backward_bindings(module);
30
+ register_pretrain_cmix_forward_bindings(module);
31
+ register_pretrain_cmix_backward_bindings(module);
32
+ register_statetune_cmix_forward_bindings(module);
33
+ register_statetune_cmix_backward_bindings(module);
34
+ register_pretrain_tmix_kk_pre_bindings(module);
35
+ register_pretrain_tmix_kk_pre_backward_bindings(module);
36
+ register_pretrain_tmix_readout_forward_bindings(module);
37
+ register_pretrain_tmix_readout_backward_bindings(module);
38
+ register_pretrain_head_l2wrap_ce_bindings(module);
39
+ register_pretrain_tmix_wkv7_recurrent_bindings(module);
40
+ register_statetune_tmix_wkv7_recurrent_forward_bindings(module);
41
+ register_statetune_tmix_wkv7_recurrent_backward_bindings(module);
42
+ register_rl_infctx_tmix_wkv7_chunk_forward_bindings(module);
43
+ register_rl_infctx_tmix_wkv7_chunk_backward_bindings(module);
44
+ }
@@ -0,0 +1,46 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: Copyright contributors to the FlashRWKV2 project
3
+
4
+ #pragma once
5
+
6
+ #include <torch/extension.h>
7
+
8
+ void register_flashrwkv2_bindings(py::module_&);
9
+ void register_infer_tmix_wkv7_recurrent_fp32io16_bindings(py::module_&);
10
+ void register_infer_tmix_wkv7_recurrent_fp16_bindings(py::module_&);
11
+ void register_infer_tmix_wkv7_recurrent_deltalog_fp16_bindings(py::module_&);
12
+ void register_infer_tmix_wkv7_recurrent_deltalog_fp32io16_bindings(
13
+ py::module_&);
14
+ void register_tmix_tokenshift_bindings(py::module_&);
15
+ void register_tmix_wkv_prepare_bindings(py::module_&);
16
+ void register_tmix_readout_bindings(py::module_&);
17
+ void register_cmix_bindings(py::module_&);
18
+ void register_post_norm_bindings(py::module_&);
19
+ void register_embedding_bindings(py::module_&);
20
+ void register_head_linear_bindings(py::module_&);
21
+ void register_sampling_bindings(py::module_&);
22
+ void register_pretrain_l2wrap_ce_forward_bindings(py::module_&);
23
+ void register_pretrain_l2wrap_ce_backward_bindings(py::module_&);
24
+ void register_infer_tmix_wkv7_chunk_bindings(py::module_&);
25
+ void register_pretrain_tmix_a_gate_forward_bindings(py::module_&);
26
+ void register_pretrain_tmix_a_gate_backward_bindings(py::module_&);
27
+ void register_pretrain_tmix_vres_gate_forward_bindings(py::module_&);
28
+ void register_pretrain_tmix_vres_gate_backward_bindings(py::module_&);
29
+ void register_pretrain_tmix_tokenshift_forward_bindings(py::module_&);
30
+ void register_pretrain_tmix_tokenshift_backward_bindings(py::module_&);
31
+ void register_statetune_tmix_tokenshift_forward_bindings(py::module_&);
32
+ void register_statetune_tmix_tokenshift_backward_bindings(py::module_&);
33
+ void register_pretrain_cmix_forward_bindings(py::module_&);
34
+ void register_pretrain_cmix_backward_bindings(py::module_&);
35
+ void register_statetune_cmix_forward_bindings(py::module_&);
36
+ void register_statetune_cmix_backward_bindings(py::module_&);
37
+ void register_pretrain_tmix_kk_pre_bindings(py::module_&);
38
+ void register_pretrain_tmix_kk_pre_backward_bindings(py::module_&);
39
+ void register_pretrain_tmix_readout_forward_bindings(py::module_&);
40
+ void register_pretrain_tmix_readout_backward_bindings(py::module_&);
41
+ void register_pretrain_head_l2wrap_ce_bindings(py::module_&);
42
+ void register_pretrain_tmix_wkv7_recurrent_bindings(py::module_&);
43
+ void register_statetune_tmix_wkv7_recurrent_forward_bindings(py::module_&);
44
+ void register_statetune_tmix_wkv7_recurrent_backward_bindings(py::module_&);
45
+ void register_rl_infctx_tmix_wkv7_chunk_forward_bindings(py::module_&);
46
+ void register_rl_infctx_tmix_wkv7_chunk_backward_bindings(py::module_&);
@@ -0,0 +1,8 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: Copyright contributors to the FlashRWKV2 project
3
+
4
+ #include "bindings.h"
5
+
6
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) {
7
+ register_flashrwkv2_bindings(module);
8
+ }
@@ -0,0 +1,287 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: Copyright contributors to the FlashRWKV2 project
3
+ // Albatross source revision: ee3308f6922e59f2166c7fac3c5a192340a2b48e.
4
+
5
+ #include "../../validation.h"
6
+
7
+ #include <torch/extension.h>
8
+
9
+ #include <cmath>
10
+ #include <cstdint>
11
+ #include <utility>
12
+ #include <vector>
13
+
14
+ torch::Tensor cmix_linear_ffn_key_dispatch_f16_cuda(
15
+ torch::Tensor x, torch::Tensor weight);
16
+ void cmix_tokenshift_forward_varlen_cuda(
17
+ int batch_size,
18
+ int total_tokens,
19
+ int channels,
20
+ int max_seqlen,
21
+ torch::Tensor x,
22
+ torch::Tensor shift_state,
23
+ torch::Tensor x_k,
24
+ torch::Tensor output,
25
+ torch::Tensor query_start_loc,
26
+ torch::Tensor state_indices,
27
+ torch::Tensor metadata_status,
28
+ torch::Tensor token_predecessor);
29
+ std::vector<torch::Tensor> cmix_res_ln_tokenshift_fused_forward_cuda(
30
+ torch::Tensor x,
31
+ torch::Tensor res,
32
+ torch::Tensor shift_state,
33
+ torch::Tensor weight,
34
+ torch::Tensor bias,
35
+ torch::Tensor x_k,
36
+ torch::Tensor token_predecessor,
37
+ torch::Tensor metadata_status,
38
+ double eps);
39
+ std::vector<torch::Tensor> post_norm_forward_varlen_cuda(
40
+ torch::Tensor x,
41
+ torch::Tensor res,
42
+ torch::Tensor weight,
43
+ torch::Tensor bias,
44
+ double eps,
45
+ int64_t batch_size);
46
+ torch::Tensor cmix_relu_square_forward_varlen(torch::Tensor x);
47
+ torch::Tensor cmix_linear_ffn_down_forward_varlen(
48
+ torch::Tensor x, torch::Tensor weight);
49
+ torch::Tensor cmix_sparse_down_relu_forward_varlen(
50
+ torch::Tensor preact,
51
+ torch::Tensor value_fc,
52
+ int64_t batch_size,
53
+ int64_t max_seqlen,
54
+ bool deterministic);
55
+
56
+ using flashrwkv2::validation::check_cuda_contiguous;
57
+ using flashrwkv2::validation::check_same_device;
58
+ using flashrwkv2::validation::prepare_recurrent_metadata_cuda;
59
+
60
+ namespace {
61
+
62
+ void check_half(
63
+ const torch::Tensor& tensor,
64
+ const torch::Tensor& reference,
65
+ const char* name) {
66
+ check_cuda_contiguous(tensor, name);
67
+ check_same_device(reference, tensor, name);
68
+ TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, name, " must be float16");
69
+ }
70
+
71
+ } // namespace
72
+
73
+ torch::Tensor cmix_tokenshift_forward_varlen(
74
+ torch::Tensor x,
75
+ torch::Tensor shift_state_pool,
76
+ torch::Tensor x_k,
77
+ torch::Tensor cu_seqlens,
78
+ torch::Tensor state_indices,
79
+ int64_t max_seqlen,
80
+ py::object validated_metadata) {
81
+ check_half(x, x, "x");
82
+ TORCH_CHECK(x.dim() == 2 && x.size(0) > 0 && x.size(1) > 0,
83
+ "x must have packed shape [total_tokens,C]");
84
+ const int64_t total_tokens = x.size(0);
85
+ const int64_t channels = x.size(1);
86
+ check_half(shift_state_pool, x, "shift_state_pool");
87
+ TORCH_CHECK(shift_state_pool.dim() == 2 && shift_state_pool.size(0) > 0 &&
88
+ shift_state_pool.size(1) == channels,
89
+ "shift_state_pool must have shape [slots,C]");
90
+ check_half(x_k, x, "x_k");
91
+ TORCH_CHECK(x_k.dim() == 1 && x_k.size(0) == channels,
92
+ "x_k must have shape [C]");
93
+ check_cuda_contiguous(cu_seqlens, "cu_seqlens");
94
+ check_cuda_contiguous(state_indices, "state_indices");
95
+ check_same_device(x, cu_seqlens, "cu_seqlens");
96
+ check_same_device(x, state_indices, "state_indices");
97
+ TORCH_CHECK(cu_seqlens.scalar_type() == torch::kInt32 &&
98
+ state_indices.scalar_type() == torch::kInt32 &&
99
+ cu_seqlens.dim() == 1 && state_indices.dim() == 1 &&
100
+ state_indices.numel() > 0 &&
101
+ cu_seqlens.numel() == state_indices.numel() + 1,
102
+ "invalid packed metadata");
103
+ TORCH_CHECK(channels % 2 == 0,
104
+ "CMix TokenShift requires an even channel count");
105
+ const int batch_size = static_cast<int>(state_indices.numel());
106
+ torch::Tensor launch_query_start_loc = cu_seqlens;
107
+ torch::Tensor launch_state_indices = state_indices;
108
+ torch::Tensor metadata_status;
109
+ torch::Tensor token_predecessor;
110
+ if (!validated_metadata.is_none()) {
111
+ validated_metadata.attr("_check_compatible")(
112
+ cu_seqlens, state_indices, total_tokens, shift_state_pool.size(0),
113
+ max_seqlen);
114
+ launch_query_start_loc = validated_metadata
115
+ .attr("_query_start_loc_snapshot")()
116
+ .cast<torch::Tensor>();
117
+ launch_state_indices = validated_metadata
118
+ .attr("_state_indices_snapshot")()
119
+ .cast<torch::Tensor>();
120
+ metadata_status = validated_metadata.attr("_status")().cast<torch::Tensor>();
121
+ token_predecessor = validated_metadata
122
+ .attr("_token_predecessor")()
123
+ .cast<torch::Tensor>();
124
+ max_seqlen = validated_metadata.attr("_max_seqlen")().cast<int64_t>();
125
+ } else {
126
+ if (max_seqlen <= 0) {
127
+ max_seqlen = 1;
128
+ }
129
+ auto prepared = prepare_recurrent_metadata_cuda(
130
+ cu_seqlens, state_indices, total_tokens, shift_state_pool.size(0));
131
+ launch_query_start_loc = std::move(prepared.query_start_loc);
132
+ launch_state_indices = std::move(prepared.state_indices);
133
+ metadata_status = std::move(prepared.status);
134
+ token_predecessor = std::move(prepared.token_predecessor);
135
+ }
136
+ auto output = torch::empty_like(x);
137
+ cmix_tokenshift_forward_varlen_cuda(
138
+ batch_size, static_cast<int>(total_tokens), static_cast<int>(channels),
139
+ static_cast<int>(max_seqlen), x, shift_state_pool, x_k, output,
140
+ launch_query_start_loc, launch_state_indices, metadata_status,
141
+ token_predecessor);
142
+ return output;
143
+ }
144
+
145
+ std::vector<torch::Tensor> cmix_postnorm_tokenshift_forward_varlen(
146
+ torch::Tensor x,
147
+ torch::Tensor res,
148
+ torch::Tensor shift_state_pool,
149
+ torch::Tensor weight,
150
+ torch::Tensor bias,
151
+ torch::Tensor x_k,
152
+ torch::Tensor cu_seqlens,
153
+ torch::Tensor state_indices,
154
+ int64_t max_seqlen,
155
+ double eps,
156
+ py::object validated_metadata) {
157
+ check_half(x, x, "x");
158
+ TORCH_CHECK(
159
+ x.dim() == 2 && x.size(0) > 0 && x.size(1) > 0 && x.size(1) % 2 == 0,
160
+ "CMix PostNorm TokenShift requires packed shape [total_tokens,C] with even C");
161
+ const int64_t total_tokens = x.size(0);
162
+ const int64_t channels = x.size(1);
163
+ check_half(res, x, "res");
164
+ TORCH_CHECK(res.sizes() == x.sizes(), "res shape mismatch");
165
+ check_half(shift_state_pool, x, "shift_state_pool");
166
+ TORCH_CHECK(
167
+ shift_state_pool.dim() == 2 && shift_state_pool.size(0) > 0 &&
168
+ shift_state_pool.size(1) == channels,
169
+ "shift_state_pool must have shape [slots,C]");
170
+ for (const auto& item : {
171
+ std::pair<const torch::Tensor*, const char*>{&weight, "weight"},
172
+ {&bias, "bias"},
173
+ {&x_k, "x_k"},
174
+ }) {
175
+ check_half(*item.first, x, item.second);
176
+ TORCH_CHECK(
177
+ item.first->dim() == 1 && item.first->size(0) == channels,
178
+ item.second, " must have shape [C]");
179
+ }
180
+ TORCH_CHECK(std::isfinite(eps) && eps > 0.0, "eps must be finite and positive");
181
+ check_cuda_contiguous(cu_seqlens, "cu_seqlens");
182
+ check_cuda_contiguous(state_indices, "state_indices");
183
+ check_same_device(x, cu_seqlens, "cu_seqlens");
184
+ check_same_device(x, state_indices, "state_indices");
185
+ TORCH_CHECK(
186
+ cu_seqlens.scalar_type() == torch::kInt32 &&
187
+ state_indices.scalar_type() == torch::kInt32 &&
188
+ cu_seqlens.dim() == 1 && state_indices.dim() == 1 &&
189
+ state_indices.numel() > 0 &&
190
+ cu_seqlens.numel() == state_indices.numel() + 1,
191
+ "invalid packed metadata");
192
+ const int64_t batch_size = state_indices.numel();
193
+ torch::Tensor launch_query_start_loc = cu_seqlens;
194
+ torch::Tensor launch_state_indices = state_indices;
195
+ torch::Tensor metadata_status;
196
+ torch::Tensor token_predecessor;
197
+ if (!validated_metadata.is_none()) {
198
+ validated_metadata.attr("_check_compatible")(
199
+ cu_seqlens, state_indices, total_tokens, shift_state_pool.size(0),
200
+ max_seqlen);
201
+ launch_query_start_loc = validated_metadata
202
+ .attr("_query_start_loc_snapshot")()
203
+ .cast<torch::Tensor>();
204
+ launch_state_indices = validated_metadata
205
+ .attr("_state_indices_snapshot")()
206
+ .cast<torch::Tensor>();
207
+ metadata_status = validated_metadata.attr("_status")().cast<torch::Tensor>();
208
+ token_predecessor = validated_metadata
209
+ .attr("_token_predecessor")()
210
+ .cast<torch::Tensor>();
211
+ max_seqlen = validated_metadata.attr("_max_seqlen")().cast<int64_t>();
212
+ } else {
213
+ if (max_seqlen <= 0) {
214
+ max_seqlen = 1;
215
+ }
216
+ auto prepared = prepare_recurrent_metadata_cuda(
217
+ cu_seqlens, state_indices, total_tokens, shift_state_pool.size(0));
218
+ launch_query_start_loc = std::move(prepared.query_start_loc);
219
+ launch_state_indices = std::move(prepared.state_indices);
220
+ metadata_status = std::move(prepared.status);
221
+ token_predecessor = std::move(prepared.token_predecessor);
222
+ }
223
+ if (FLASHRWKV_TARGET_SM == 120 && channels == 4096 && max_seqlen == 1 &&
224
+ total_tokens == batch_size) {
225
+ return cmix_res_ln_tokenshift_fused_forward_cuda(
226
+ x, res, shift_state_pool, weight, bias, x_k,
227
+ token_predecessor, metadata_status, eps);
228
+ }
229
+ auto post_norm = post_norm_forward_varlen_cuda(
230
+ x, res, weight, bias, eps, batch_size);
231
+ auto output = torch::empty_like(x);
232
+ cmix_tokenshift_forward_varlen_cuda(
233
+ static_cast<int>(batch_size), static_cast<int>(total_tokens),
234
+ static_cast<int>(channels), static_cast<int>(max_seqlen), post_norm[1],
235
+ shift_state_pool, x_k, output, launch_query_start_loc,
236
+ launch_state_indices, metadata_status, token_predecessor);
237
+ return {post_norm[0], output};
238
+ }
239
+
240
+ std::vector<torch::Tensor> cmix_forward_varlen(
241
+ torch::Tensor x,
242
+ torch::Tensor res,
243
+ torch::Tensor shift_state_pool,
244
+ torch::Tensor weight,
245
+ torch::Tensor bias,
246
+ torch::Tensor x_k,
247
+ torch::Tensor key_weight,
248
+ torch::Tensor value_weight,
249
+ torch::Tensor cu_seqlens,
250
+ torch::Tensor state_indices,
251
+ int64_t max_seqlen,
252
+ double eps,
253
+ py::object validated_metadata,
254
+ bool deterministic) {
255
+ int64_t launch_max_seqlen = max_seqlen;
256
+ if (!validated_metadata.is_none()) {
257
+ launch_max_seqlen =
258
+ validated_metadata.attr("_max_seqlen")().cast<int64_t>();
259
+ }
260
+ auto front = cmix_postnorm_tokenshift_forward_varlen(
261
+ x, res, shift_state_pool, weight, bias, x_k, cu_seqlens,
262
+ state_indices, max_seqlen, eps, validated_metadata);
263
+ auto preact = cmix_linear_ffn_key_dispatch_f16_cuda(front[1], key_weight);
264
+ torch::Tensor output;
265
+ if (FLASHRWKV_TARGET_SM == 120 && x.size(1) == 4096 && x.size(0) <= 19) {
266
+ output = cmix_sparse_down_relu_forward_varlen(
267
+ preact, value_weight, state_indices.numel(),
268
+ launch_max_seqlen, deterministic);
269
+ } else {
270
+ output = cmix_linear_ffn_down_forward_varlen(
271
+ cmix_relu_square_forward_varlen(preact), value_weight);
272
+ }
273
+ return {front[0], output};
274
+ }
275
+
276
+ void register_cmix_bindings(py::module_& module) {
277
+ module.def(
278
+ "cmix_forward_varlen", &cmix_forward_varlen,
279
+ "Complete packed Albatross ChannelMix inference island",
280
+ py::arg("x"), py::arg("res"), py::arg("shift_state_pool"),
281
+ py::arg("weight"), py::arg("bias"), py::arg("x_k"),
282
+ py::arg("key_weight"), py::arg("value_weight"),
283
+ py::arg("cu_seqlens"), py::arg("state_indices"),
284
+ py::arg("max_seqlen") = -1, py::arg("eps") = 1.0e-5,
285
+ py::arg("validated_metadata") = py::none(),
286
+ py::arg("deterministic") = false);
287
+ }