twinkle-kit 0.0.1__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 (205) hide show
  1. twinkle_kit-0.0.1/LICENSE +201 -0
  2. twinkle_kit-0.0.1/PKG-INFO +411 -0
  3. twinkle_kit-0.0.1/README.md +370 -0
  4. twinkle_kit-0.0.1/pyproject.toml +56 -0
  5. twinkle_kit-0.0.1/setup.cfg +37 -0
  6. twinkle_kit-0.0.1/src/twinkle/__init__.py +30 -0
  7. twinkle_kit-0.0.1/src/twinkle/advantage/__init__.py +36 -0
  8. twinkle_kit-0.0.1/src/twinkle/advantage/base.py +27 -0
  9. twinkle_kit-0.0.1/src/twinkle/advantage/grpo.py +68 -0
  10. twinkle_kit-0.0.1/src/twinkle/advantage/rloo.py +63 -0
  11. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/__init__.py +30 -0
  12. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/base.py +124 -0
  13. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/hccl_checkpoint_engine.py +439 -0
  14. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/manager.py +135 -0
  15. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/mixin.py +51 -0
  16. twinkle_kit-0.0.1/src/twinkle/checkpoint_engine/nccl_checkpoint_engine.py +514 -0
  17. twinkle_kit-0.0.1/src/twinkle/data_format/__init__.py +6 -0
  18. twinkle_kit-0.0.1/src/twinkle/data_format/input_feature.py +38 -0
  19. twinkle_kit-0.0.1/src/twinkle/data_format/message.py +73 -0
  20. twinkle_kit-0.0.1/src/twinkle/data_format/output.py +26 -0
  21. twinkle_kit-0.0.1/src/twinkle/data_format/sampling.py +142 -0
  22. twinkle_kit-0.0.1/src/twinkle/data_format/trajectory.py +19 -0
  23. twinkle_kit-0.0.1/src/twinkle/dataloader/__init__.py +5 -0
  24. twinkle_kit-0.0.1/src/twinkle/dataloader/dataloader.py +126 -0
  25. twinkle_kit-0.0.1/src/twinkle/dataloader/device_mesh_fetcher.py +81 -0
  26. twinkle_kit-0.0.1/src/twinkle/dataloader/device_mesh_sampler.py +33 -0
  27. twinkle_kit-0.0.1/src/twinkle/dataloader/retry_sampler.py +61 -0
  28. twinkle_kit-0.0.1/src/twinkle/dataset/__init__.py +6 -0
  29. twinkle_kit-0.0.1/src/twinkle/dataset/base.py +251 -0
  30. twinkle_kit-0.0.1/src/twinkle/dataset/iterable_dataset.py +33 -0
  31. twinkle_kit-0.0.1/src/twinkle/dataset/iterable_packing_dataset.py +127 -0
  32. twinkle_kit-0.0.1/src/twinkle/dataset/lazy_dataset.py +43 -0
  33. twinkle_kit-0.0.1/src/twinkle/dataset/packing_dataset.py +127 -0
  34. twinkle_kit-0.0.1/src/twinkle/gym/__init__.py +2 -0
  35. twinkle_kit-0.0.1/src/twinkle/gym/base.py +10 -0
  36. twinkle_kit-0.0.1/src/twinkle/hub/__init__.py +2 -0
  37. twinkle_kit-0.0.1/src/twinkle/hub/hub.py +644 -0
  38. twinkle_kit-0.0.1/src/twinkle/infra/__init__.py +673 -0
  39. twinkle_kit-0.0.1/src/twinkle/infra/_ray/__init__.py +3 -0
  40. twinkle_kit-0.0.1/src/twinkle/infra/_ray/ray_helper.py +362 -0
  41. twinkle_kit-0.0.1/src/twinkle/infra/_ray/resource_manager.py +271 -0
  42. twinkle_kit-0.0.1/src/twinkle/kernel/__init__.py +72 -0
  43. twinkle_kit-0.0.1/src/twinkle/kernel/base.py +81 -0
  44. twinkle_kit-0.0.1/src/twinkle/kernel/function.py +174 -0
  45. twinkle_kit-0.0.1/src/twinkle/kernel/layer.py +119 -0
  46. twinkle_kit-0.0.1/src/twinkle/kernel/registry.py +183 -0
  47. twinkle_kit-0.0.1/src/twinkle/loss/__init__.py +21 -0
  48. twinkle_kit-0.0.1/src/twinkle/loss/base.py +8 -0
  49. twinkle_kit-0.0.1/src/twinkle/loss/chunked_cross_entropy.py +61 -0
  50. twinkle_kit-0.0.1/src/twinkle/loss/cross_entropy.py +14 -0
  51. twinkle_kit-0.0.1/src/twinkle/loss/grpo.py +556 -0
  52. twinkle_kit-0.0.1/src/twinkle/loss/mse.py +11 -0
  53. twinkle_kit-0.0.1/src/twinkle/loss/vocab_parallel_cross_entropy.py +38 -0
  54. twinkle_kit-0.0.1/src/twinkle/loss_scale/__init__.py +2 -0
  55. twinkle_kit-0.0.1/src/twinkle/loss_scale/base.py +6 -0
  56. twinkle_kit-0.0.1/src/twinkle/metric/__init__.py +6 -0
  57. twinkle_kit-0.0.1/src/twinkle/metric/accuracy.py +64 -0
  58. twinkle_kit-0.0.1/src/twinkle/metric/base.py +28 -0
  59. twinkle_kit-0.0.1/src/twinkle/metric/completion_and_reward.py +70 -0
  60. twinkle_kit-0.0.1/src/twinkle/metric/loss.py +72 -0
  61. twinkle_kit-0.0.1/src/twinkle/metric/train_metric.py +60 -0
  62. twinkle_kit-0.0.1/src/twinkle/model/__init__.py +26 -0
  63. twinkle_kit-0.0.1/src/twinkle/model/base.py +159 -0
  64. twinkle_kit-0.0.1/src/twinkle/model/megatron/__init__.py +28 -0
  65. twinkle_kit-0.0.1/src/twinkle/model/megatron/args.py +675 -0
  66. twinkle_kit-0.0.1/src/twinkle/model/megatron/megatron.py +1601 -0
  67. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/__init__.py +4 -0
  68. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/constant.py +35 -0
  69. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/gpt_bridge.py +1604 -0
  70. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/gpt_model.py +463 -0
  71. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/gpts/__init__.py +14 -0
  72. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/mm_gpt_model.py +135 -0
  73. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/mm_gpts/__init__.py +2 -0
  74. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/mm_gpts/qwen.py +121 -0
  75. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/mm_gpts/qwen3_vl.py +450 -0
  76. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/mm_gpts/utils.py +83 -0
  77. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/register.py +64 -0
  78. twinkle_kit-0.0.1/src/twinkle/model/megatron/model/rope.py +175 -0
  79. twinkle_kit-0.0.1/src/twinkle/model/megatron/multi_lora_megatron.py +272 -0
  80. twinkle_kit-0.0.1/src/twinkle/model/megatron/strategy/__init__.py +2 -0
  81. twinkle_kit-0.0.1/src/twinkle/model/megatron/strategy/megatron.py +191 -0
  82. twinkle_kit-0.0.1/src/twinkle/model/megatron/tuners/__init__.py +8 -0
  83. twinkle_kit-0.0.1/src/twinkle/model/megatron/tuners/lora.py +585 -0
  84. twinkle_kit-0.0.1/src/twinkle/model/megatron/tuners/utils.py +206 -0
  85. twinkle_kit-0.0.1/src/twinkle/model/megatron/utils/__init__.py +2 -0
  86. twinkle_kit-0.0.1/src/twinkle/model/megatron/utils/config.py +193 -0
  87. twinkle_kit-0.0.1/src/twinkle/model/megatron/utils/utils.py +32 -0
  88. twinkle_kit-0.0.1/src/twinkle/model/multi_lora.py +642 -0
  89. twinkle_kit-0.0.1/src/twinkle/model/transformers/__init__.py +3 -0
  90. twinkle_kit-0.0.1/src/twinkle/model/transformers/moe/__init__.py +4 -0
  91. twinkle_kit-0.0.1/src/twinkle/model/transformers/moe/expert_parallel.py +379 -0
  92. twinkle_kit-0.0.1/src/twinkle/model/transformers/multi_lora_transformers.py +249 -0
  93. twinkle_kit-0.0.1/src/twinkle/model/transformers/strategy/__init__.py +5 -0
  94. twinkle_kit-0.0.1/src/twinkle/model/transformers/strategy/accelerate.py +121 -0
  95. twinkle_kit-0.0.1/src/twinkle/model/transformers/strategy/native_fsdp.py +178 -0
  96. twinkle_kit-0.0.1/src/twinkle/model/transformers/strategy/sequence_parallel.py +1041 -0
  97. twinkle_kit-0.0.1/src/twinkle/model/transformers/transformers.py +1172 -0
  98. twinkle_kit-0.0.1/src/twinkle/module/__init__.py +0 -0
  99. twinkle_kit-0.0.1/src/twinkle/module/scheduler/__init__.py +3 -0
  100. twinkle_kit-0.0.1/src/twinkle/module/scheduler/cosine_warmup.py +26 -0
  101. twinkle_kit-0.0.1/src/twinkle/module/scheduler/linear_warmup.py +19 -0
  102. twinkle_kit-0.0.1/src/twinkle/patch/__init__.py +14 -0
  103. twinkle_kit-0.0.1/src/twinkle/patch/base.py +10 -0
  104. twinkle_kit-0.0.1/src/twinkle/patch/megatron_peft.py +35 -0
  105. twinkle_kit-0.0.1/src/twinkle/patch/vllm_lora_weights.py +144 -0
  106. twinkle_kit-0.0.1/src/twinkle/patch/vllm_moe_loader.py +129 -0
  107. twinkle_kit-0.0.1/src/twinkle/preprocessor/__init__.py +4 -0
  108. twinkle_kit-0.0.1/src/twinkle/preprocessor/base.py +15 -0
  109. twinkle_kit-0.0.1/src/twinkle/preprocessor/llm.py +118 -0
  110. twinkle_kit-0.0.1/src/twinkle/processor/__init__.py +3 -0
  111. twinkle_kit-0.0.1/src/twinkle/processor/base.py +388 -0
  112. twinkle_kit-0.0.1/src/twinkle/processor/grpo.py +34 -0
  113. twinkle_kit-0.0.1/src/twinkle/reward/__init__.py +5 -0
  114. twinkle_kit-0.0.1/src/twinkle/reward/base.py +10 -0
  115. twinkle_kit-0.0.1/src/twinkle/reward/format_reward.py +29 -0
  116. twinkle_kit-0.0.1/src/twinkle/reward/gsm8k.py +74 -0
  117. twinkle_kit-0.0.1/src/twinkle/reward/math_reward.py +95 -0
  118. twinkle_kit-0.0.1/src/twinkle/sampler/__init__.py +7 -0
  119. twinkle_kit-0.0.1/src/twinkle/sampler/base.py +107 -0
  120. twinkle_kit-0.0.1/src/twinkle/sampler/base_engine.py +106 -0
  121. twinkle_kit-0.0.1/src/twinkle/sampler/torch_sampler/__init__.py +1 -0
  122. twinkle_kit-0.0.1/src/twinkle/sampler/torch_sampler/torch_sampler.py +157 -0
  123. twinkle_kit-0.0.1/src/twinkle/sampler/torch_sampler/transformers_engine.py +298 -0
  124. twinkle_kit-0.0.1/src/twinkle/sampler/vllm_sampler/__init__.py +1 -0
  125. twinkle_kit-0.0.1/src/twinkle/sampler/vllm_sampler/vllm_engine.py +687 -0
  126. twinkle_kit-0.0.1/src/twinkle/sampler/vllm_sampler/vllm_sampler.py +443 -0
  127. twinkle_kit-0.0.1/src/twinkle/sampler/vllm_sampler/vllm_worker_extension.py +380 -0
  128. twinkle_kit-0.0.1/src/twinkle/server/__init__.py +15 -0
  129. twinkle_kit-0.0.1/src/twinkle/server/__main__.py +142 -0
  130. twinkle_kit-0.0.1/src/twinkle/server/launcher.py +361 -0
  131. twinkle_kit-0.0.1/src/twinkle/server/tinker/__init__.py +15 -0
  132. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/__init__.py +3 -0
  133. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/compat_base.py +144 -0
  134. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/datum.py +113 -0
  135. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/io_utils.py +181 -0
  136. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/megatron_model.py +188 -0
  137. twinkle_kit-0.0.1/src/twinkle/server/tinker/common/transformers_model.py +142 -0
  138. twinkle_kit-0.0.1/src/twinkle/server/tinker/model.py +630 -0
  139. twinkle_kit-0.0.1/src/twinkle/server/tinker/sampler.py +234 -0
  140. twinkle_kit-0.0.1/src/twinkle/server/tinker/server.py +684 -0
  141. twinkle_kit-0.0.1/src/twinkle/server/twinkle/__init__.py +5 -0
  142. twinkle_kit-0.0.1/src/twinkle/server/twinkle/common/__init__.py +1 -0
  143. twinkle_kit-0.0.1/src/twinkle/server/twinkle/common/io_utils.py +235 -0
  144. twinkle_kit-0.0.1/src/twinkle/server/twinkle/common/serialize.py +83 -0
  145. twinkle_kit-0.0.1/src/twinkle/server/twinkle/model.py +584 -0
  146. twinkle_kit-0.0.1/src/twinkle/server/twinkle/processor.py +188 -0
  147. twinkle_kit-0.0.1/src/twinkle/server/twinkle/sampler.py +308 -0
  148. twinkle_kit-0.0.1/src/twinkle/server/twinkle/server.py +270 -0
  149. twinkle_kit-0.0.1/src/twinkle/server/utils/__init__.py +7 -0
  150. twinkle_kit-0.0.1/src/twinkle/server/utils/adapter_manager.py +377 -0
  151. twinkle_kit-0.0.1/src/twinkle/server/utils/device_utils.py +40 -0
  152. twinkle_kit-0.0.1/src/twinkle/server/utils/io_utils.py +920 -0
  153. twinkle_kit-0.0.1/src/twinkle/server/utils/rate_limiter.py +239 -0
  154. twinkle_kit-0.0.1/src/twinkle/server/utils/state.py +609 -0
  155. twinkle_kit-0.0.1/src/twinkle/server/utils/task_queue.py +570 -0
  156. twinkle_kit-0.0.1/src/twinkle/server/utils/validation.py +66 -0
  157. twinkle_kit-0.0.1/src/twinkle/template/__init__.py +3 -0
  158. twinkle_kit-0.0.1/src/twinkle/template/base.py +441 -0
  159. twinkle_kit-0.0.1/src/twinkle/template/qwen3_vl.py +120 -0
  160. twinkle_kit-0.0.1/src/twinkle/template/utils.py +222 -0
  161. twinkle_kit-0.0.1/src/twinkle/utils/__init__.py +15 -0
  162. twinkle_kit-0.0.1/src/twinkle/utils/dequantizer.py +46 -0
  163. twinkle_kit-0.0.1/src/twinkle/utils/framework.py +237 -0
  164. twinkle_kit-0.0.1/src/twinkle/utils/grad_clip.py +95 -0
  165. twinkle_kit-0.0.1/src/twinkle/utils/import_utils.py +87 -0
  166. twinkle_kit-0.0.1/src/twinkle/utils/loader.py +74 -0
  167. twinkle_kit-0.0.1/src/twinkle/utils/logger.py +149 -0
  168. twinkle_kit-0.0.1/src/twinkle/utils/network.py +170 -0
  169. twinkle_kit-0.0.1/src/twinkle/utils/parallel.py +50 -0
  170. twinkle_kit-0.0.1/src/twinkle/utils/platform.py +766 -0
  171. twinkle_kit-0.0.1/src/twinkle/utils/safetensors.py +169 -0
  172. twinkle_kit-0.0.1/src/twinkle/utils/torch_utils.py +105 -0
  173. twinkle_kit-0.0.1/src/twinkle/utils/transformers_utils.py +188 -0
  174. twinkle_kit-0.0.1/src/twinkle/utils/unsafe.py +23 -0
  175. twinkle_kit-0.0.1/src/twinkle/utils/utils.py +79 -0
  176. twinkle_kit-0.0.1/src/twinkle/version.py +5 -0
  177. twinkle_kit-0.0.1/src/twinkle_client/__init__.py +58 -0
  178. twinkle_kit-0.0.1/src/twinkle_client/dataloader/__init__.py +11 -0
  179. twinkle_kit-0.0.1/src/twinkle_client/dataloader/dataloader.py +92 -0
  180. twinkle_kit-0.0.1/src/twinkle_client/dataset/__init__.py +15 -0
  181. twinkle_kit-0.0.1/src/twinkle_client/dataset/base.py +167 -0
  182. twinkle_kit-0.0.1/src/twinkle_client/dataset/iterable_dataset.py +105 -0
  183. twinkle_kit-0.0.1/src/twinkle_client/dataset/iterable_packing_dataset.py +94 -0
  184. twinkle_kit-0.0.1/src/twinkle_client/dataset/lazy_dataset.py +95 -0
  185. twinkle_kit-0.0.1/src/twinkle_client/dataset/packing_dataset.py +80 -0
  186. twinkle_kit-0.0.1/src/twinkle_client/http/__init__.py +22 -0
  187. twinkle_kit-0.0.1/src/twinkle_client/http/heartbeat.py +177 -0
  188. twinkle_kit-0.0.1/src/twinkle_client/http/http_utils.py +172 -0
  189. twinkle_kit-0.0.1/src/twinkle_client/http/utils.py +68 -0
  190. twinkle_kit-0.0.1/src/twinkle_client/manager.py +294 -0
  191. twinkle_kit-0.0.1/src/twinkle_client/model/__init__.py +11 -0
  192. twinkle_kit-0.0.1/src/twinkle_client/model/multi_lora_transformers.py +260 -0
  193. twinkle_kit-0.0.1/src/twinkle_client/processor/__init__.py +11 -0
  194. twinkle_kit-0.0.1/src/twinkle_client/processor/base.py +55 -0
  195. twinkle_kit-0.0.1/src/twinkle_client/processor/grpo.py +48 -0
  196. twinkle_kit-0.0.1/src/twinkle_client/reward/__init__.py +11 -0
  197. twinkle_kit-0.0.1/src/twinkle_client/reward/math_reward.py +56 -0
  198. twinkle_kit-0.0.1/src/twinkle_client/sampler/__init__.py +11 -0
  199. twinkle_kit-0.0.1/src/twinkle_client/sampler/vllm_sampler.py +120 -0
  200. twinkle_kit-0.0.1/src/twinkle_client/utils/patch_tinker.py +149 -0
  201. twinkle_kit-0.0.1/src/twinkle_kit.egg-info/PKG-INFO +411 -0
  202. twinkle_kit-0.0.1/src/twinkle_kit.egg-info/SOURCES.txt +204 -0
  203. twinkle_kit-0.0.1/src/twinkle_kit.egg-info/dependency_links.txt +1 -0
  204. twinkle_kit-0.0.1/src/twinkle_kit.egg-info/requires.txt +37 -0
  205. twinkle_kit-0.0.1/src/twinkle_kit.egg-info/top_level.txt +2 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,411 @@
1
+ Metadata-Version: 2.4
2
+ Name: twinkle-kit
3
+ Version: 0.0.1
4
+ Summary: Training API for large language models with efficient data handling and advanced optimization techniques.
5
+ Author-email: ModelScope <contact@modelscope.cn>
6
+ Requires-Python: <3.13,>=3.11
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: datasets<4.0,>=3.0
10
+ Requires-Dist: numpy!=2.4.0,<3.0.0,>=2.0.0
11
+ Requires-Dist: omegaconf<3.0.0,>=2.3.0
12
+ Requires-Dist: fastapi
13
+ Requires-Dist: modelscope[framework]>=1.34.0
14
+ Requires-Dist: safetensors
15
+ Requires-Dist: peft<=0.19.0,>=0.11.0
16
+ Requires-Dist: transformers
17
+ Provides-Extra: transformers
18
+ Requires-Dist: accelerate; extra == "transformers"
19
+ Requires-Dist: torch<3.0.0,>=2.6.0; extra == "transformers"
20
+ Requires-Dist: torchvision; extra == "transformers"
21
+ Provides-Extra: kernels
22
+ Requires-Dist: kernels; extra == "kernels"
23
+ Provides-Extra: megatron
24
+ Requires-Dist: megatron-core>=0.12.0; extra == "megatron"
25
+ Requires-Dist: transformer-engine[pytorch]; extra == "megatron"
26
+ Provides-Extra: vllm
27
+ Requires-Dist: vllm>=0.11; extra == "vllm"
28
+ Provides-Extra: ray
29
+ Requires-Dist: ray[serve]; extra == "ray"
30
+ Provides-Extra: docs
31
+ Requires-Dist: sphinx<6.0.0,>=5.3.0; extra == "docs"
32
+ Requires-Dist: docutils<0.17.0,>=0.16.0; extra == "docs"
33
+ Requires-Dist: myst_parser; extra == "docs"
34
+ Requires-Dist: recommonmark; extra == "docs"
35
+ Requires-Dist: sphinx-book-theme; extra == "docs"
36
+ Requires-Dist: sphinx-copybutton; extra == "docs"
37
+ Requires-Dist: sphinx-rtd-theme; extra == "docs"
38
+ Requires-Dist: sphinx_markdown_tables; extra == "docs"
39
+ Requires-Dist: sphinxcontrib-mermaid; extra == "docs"
40
+ Dynamic: license-file
41
+
42
+ <h1 align="center">Twinkle: Training workbench to make your model glow</h1>
43
+
44
+ <p align="center">
45
+ <img src="assets/slogan.png" width="200"/>
46
+ <p>
47
+ <p align="center">
48
+ by <a href="https://modelscope.cn/home">ModelScope</a>
49
+ <br>
50
+ English&nbsp | &nbsp<a href="README_ZH.md">中文</a>&nbsp
51
+ </p>
52
+
53
+ <p align="center">
54
+ <img src="https://img.shields.io/badge/python-3.11-5be.svg">
55
+ <img src="https://img.shields.io/badge/pytorch-%E2%89%A52.0-orange.svg">
56
+ <a href="https://pypi.org/project/twinkle/"><img src="https://badge.fury.io/py/twinkle.svg"></a>
57
+ <a href="https://github.com/modelscope/twinkle/blob/main/LICENSE"><img src="https://img.shields.io/github/license/modelscope/twinkle"></a>
58
+ <a href="https://pepy.tech/project/twinkle-kit"><img src="https://pepy.tech/badge/twinkle-kit"></a>
59
+ <a href="https://github.com/modelscope/twinkle/pulls"><img src="https://img.shields.io/badge/PR-welcome-55EB99.svg"></a>
60
+ </p>
61
+
62
+ <p align="center">
63
+ <a href="https://twinkle-kit.readthedocs.io/en/latest/">English Documentation</a> &nbsp | &nbsp <a href="https://twinkle-kit.readthedocs.io/zh-cn/latest/">中文文档</a> &nbsp
64
+ </p>
65
+
66
+ ## ✨ What is Twinkle?
67
+
68
+ Twinkle✨ is a lightweight, client-server training framework engineered
69
+ with modular, high-cohesion interfaces. Whether you are executing locally
70
+ with `torchrun`, or scaling training across Ray clusters,
71
+ Twinkle✨ eliminates infrastructure friction by encapsulating
72
+ training logic into standardized APIs. Beyond simple
73
+ abstraction, Twinkle✨ serves as a robust backend and gateway to enable serverless Training-as-a-Service (TaaS).
74
+ It offers interfaces that constitute a _superset_ of [Tinker](https://thinkingmachines.ai/tinker/) APIs,
75
+ thereby making it possible to access a Twinkle✨ training service via Tinker client or native Twinkle✨ client
76
+ which offers more functionalities.
77
+
78
+ 🧩 <b>Decoupled Architecture</b>: Standardized Interfaces, backward compatible with Tinker APIs.<br>
79
+ 🚀 <b>Multiple Runtime Modes</b>: torchrun / Ray / HTTP.<br>
80
+ 🔌 <b>Versatile Backends</b>: Transformers / Megatron.<br>
81
+ 👥 <b>Multi-Tenancy Training Service</b>: Train multiple LoRAs that share one base model deployment.<br>
82
+
83
+ Note: Twinkle✨is built by the team behind [ms-swift](https://github.com/modelscope/ms-swift), and
84
+ we expect the two projects to evolve together. We expect some fundamental components in Twinkle✨will likely
85
+ be reused in [ms-swift](https://github.com/modelscope/ms-swift).
86
+
87
+ | Twinkle Wechat Group |
88
+ |:------------------------------------------------------:|
89
+ | <img src="assets/wechat.jpg" width="200" height="200"> |
90
+
91
+ ## Installation
92
+
93
+ ### Install with package:
94
+
95
+ ```shell
96
+ pip install 'twinkle-kit'
97
+ ```
98
+
99
+ ### Install from Source:
100
+
101
+ ```shell
102
+ git clone https://github.com/modelscope/twinkle.git
103
+ cd twinkle
104
+ pip install -e .
105
+ ```
106
+
107
+ ## Tutorials
108
+
109
+ | Training Type | Model Framework | Cookbook Path |
110
+ | --------------------------------- | --------------- | ------------------------------------------------- |
111
+ | FSDP finetuning | transformers | [Script](cookbook/transformers/fsdp2.py) |
112
+ | FSDP MoE finetuning | transformers | [Script](cookbook/transformers/fsdp2_moe.py) |
113
+ | ep FSDP MoE finetuning | transformers | [Script](cookbook/transformers/ep_fsdp_qwen3_moe.py) |
114
+ | sp FSDP finetuning | transformers | [Script](cookbook/transformers/sp_fsdp_dense.py) |
115
+ | EP MoE finetuning | transformers | [Script](cookbook/transformers/ep_fsdp_qwen3_moe.py) |
116
+ | pp/tp/cp finetuning | megatron | [Script](cookbook/megatron/tp.py) |
117
+ | pp/tp/cp MoE finetuning | megatron | [Script](cookbook/megatron/tp_moe.py) |
118
+ | tinker client finetuning | megatron | [Script](cookbook/client/tinker/megatron) |
119
+ | tinker client finetuning/sampling | transformers | [Script](cookbook/client/tinker/transformer) |
120
+ | twinkle client finetuning | megatron | [Script](cookbook/client/twinkle/megatron) |
121
+ | twinkle client finetuning | transformer | [Script](cookbook/client/twinkle/transformer) |
122
+
123
+ ## Changelog
124
+
125
+ - 🎉2026-02-13 Initial version of Twinkle✨ released, including SFT/PT/RL support for text models and serverless training capabilities on [ModelScope](https://modelscope.cn).
126
+
127
+ ## Training as a Service on ModelScope
128
+
129
+ We are rolling out training service built atop Twinkle✨ on ModelScope. It is currently in _Beta_. You may
130
+ sign up for free access by joining the [Twinkle-Explorers](https://modelscope.cn/organization/twinkle-explorers) organization, and
131
+ train via API endpoint `base_url=https://www.modelscope.cn/twinkle`. For more details, please refer to
132
+ our [documentation](docs/source_en/Usage%20Guide/Train-as-a-Service.md).
133
+
134
+ ## Supported Hardware
135
+
136
+ | Hardware Environment | Notes |
137
+ | -------------------- | ---------------------------------------------------------------- |
138
+ | Nvidia GPUs | ✅ Support for BF16/Flash-Attn may be incomplete in earlier GPUs |
139
+ | Ascend NPU | ✅ Some operators may not supported |
140
+ | PPU | ✅ |
141
+ | CPU | Supports partial components like dataset, dataloader |
142
+
143
+ ## Supported Models
144
+
145
+ We will be adding support for more models as new models are released. The following table lists current models
146
+ supported on Twinkle✨ framework.
147
+
148
+ >[!Note]
149
+ > For serverless training service accessed via `base_url=https://www.modelscope.cn/twinkle`, it currently supports
150
+ > one training base at a time, and currently it is [Qwen3-30B-A3B-Instruct-2507](https://modelscope.cn/models/Qwen/Qwen3-30B-A3B-Instruct-2507).
151
+
152
+
153
+ | Model Type | Model ID on [ModelScope](https://modelscope.cn) | Requires | Megatron Support | HF Model ID |
154
+ | ------------------- |--------------------------------------------------------------------------------------------------------------------------| -------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------- |
155
+ | qwen3 series | [Qwen/Qwen3-0.6B-Base](https://modelscope.cn/models/Qwen/Qwen3-0.6B-Base)~32B | transformers>=4.51 | ✅ | [Qwen/Qwen3-0.6B-Base](https://huggingface.co/Qwen/Qwen3-0.6B-Base) |
156
+ | qwen3_moe series | [Qwen/Qwen3-30B-A3B-Base](https://modelscope.cn/models/Qwen/Qwen3-30B-A3B-Base) | transformers>=4.51 | ✅ | [Qwen/Qwen3-30B-A3B-Base](https://huggingface.co/Qwen/Qwen3-30B-A3B-Base) |
157
+ | | [Qwen/Qwen3-30B-A3B](https://modelscope.cn/models/Qwen/Qwen3-30B-A3B)~235B | transformers>=4.51 | ✅ | [Qwen/Qwen3-30B-A3B](https://huggingface.co/Qwen/Qwen3-30B-A3B) |
158
+ | qwen2 series | [Qwen/Qwen2-0.5B-Instruct](https://modelscope.cn/models/Qwen/Qwen2-0.5B-Instruct) ~72B | transformers>=4.37 | ✅ | [Qwen/Qwen2-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2-0.5B-Instruct) |
159
+ | | [Qwen/Qwen2.5-0.5B-Instruct](https://modelscope.cn/models/Qwen/Qwen2.5-0.5B-Instruct)~72B | transformers>=4.37 | ✅ | [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) |
160
+ | | [Qwen/Qwen2.5-0.5B](https://modelscope.cn/models/Qwen/Qwen2.5-0.5B)~72B | transformers>=4.37 | ✅ | [Qwen/Qwen2.5-0.5B](https://huggingface.co/Qwen/Qwen2.5-0.5B) |
161
+ | qwen2_moe series | [Qwen/Qwen1.5-MoE-A2.7B-Chat](https://modelscope.cn/models/Qwen/Qwen1.5-MoE-A2.7B-Chat) | transformers>=4.40 | ✅ | [Qwen/Qwen1.5-MoE-A2.7B-Chat](https://huggingface.co/Qwen/Qwen1.5-MoE-A2.7B-Chat) |
162
+ | chatglm4 series | [ZhipuAI/glm-4-9b-chat](https://modelscope.cn/models/ZhipuAI/glm-4-9b-chat) | transformers>=4.42 | ✘ | [zai-org/glm-4-9b-chat](https://huggingface.co/zai-org/glm-4-9b-chat) |
163
+ | | [ZhipuAI/LongWriter-glm4-9b](https://modelscope.cn/models/ZhipuAI/LongWriter-glm4-9b) | transformers>=4.42 | ✘ | [zai-org/LongWriter-glm4-9b](https://huggingface.co/zai-org/LongWriter-glm4-9b) |
164
+ | glm_edge series | [ZhipuAI/glm-edge-1.5b-chat](https://modelscope.cn/models/ZhipuAI/glm-edge-1.5b-chat) | transformers>=4.46 | ✘ | [zai-org/glm-edge-1.5b-chat](https://huggingface.co/zai-org/glm-edge-1.5b-chat) |
165
+ | | [ZhipuAI/glm-edge-4b-chat](https://modelscope.cn/models/ZhipuAI/glm-edge-4b-chat) | transformers>=4.46 | ✘ | [zai-org/glm-edge-4b-chat](https://huggingface.co/zai-org/glm-edge-4b-chat) |
166
+ | internlm2 series | [Shanghai_AI_Laboratory/internlm2-1_8b](https://modelscope.cn/models/Shanghai_AI_Laboratory/internlm2-1_8b) | transformers>=4.38 | ✘ | [internlm/internlm2-1_8b](https://huggingface.co/internlm/internlm2-1_8b) |
167
+ | | [Shanghai_AI_Laboratory/internlm2-chat-7b](https://modelscope.cn/models/Shanghai_AI_Laboratory/internlm2-chat-7b) | transformers>=4.38 | ✘ | [internlm/internlm2-chat-7b](https://huggingface.co/internlm/internlm2-chat-7b) |
168
+ | deepseek_v1 | [deepseek-ai/deepseek-vl-7b-chat](https://modelscope.cn/models/deepseek-ai/deepseek-vl-7b-chat) | transformers>=4.39.4 | ✅ | —— |
169
+ | | [deepseek-ai/DeepSeek-V2-Lite](https://modelscope.cn/models/deepseek-ai/DeepSeek-V2-Lite) | transformers>=4.39.3 | ✅ | [deepseek-ai/DeepSeek-V2-Lite](https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite) |
170
+ | | [deepseek-ai/DeepSeek-V2.5](https://modelscope.cn/models/deepseek-ai/DeepSeek-V2.5) | transformers>=4.39.3 | ✅ | [deepseek-ai/DeepSeek-V2.5](https://huggingface.co/deepseek-ai/DeepSeek-V2.5) |
171
+ | | [deepseek-ai/DeepSeek-R1](https://modelscope.cn/models/deepseek-ai/DeepSeek-R1) | transformers>=4.39.3 | ✅ | [deepseek-ai/DeepSeek-R1](https://huggingface.co/deepseek-ai/DeepSeek-R1) |
172
+ | deepSeek-r1-distill | [deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B](https://modelscope.cn/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B) ~32B | transformers>=4.37 | ✅ | [deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B) |
173
+
174
+ For more detailed model support list 👉 [Quick Start](docs/source_en/Usage%20Guide/Quick-Start.md)
175
+
176
+ ## Sample Code
177
+
178
+ ### Train with Ray
179
+
180
+ ```python
181
+ from peft import LoraConfig
182
+ import twinkle
183
+ from twinkle import DeviceMesh, DeviceGroup
184
+ from twinkle.dataloader import DataLoader
185
+ from twinkle.dataset import Dataset, DatasetMeta
186
+ from twinkle.model import TransformersModel
187
+ from twinkle.preprocessor import SelfCognitionProcessor
188
+
189
+ device_group = [DeviceGroup(name='default',ranks=8,device_type='cuda')]
190
+ device_mesh = DeviceMesh.from_sizes(fsdp_size=4, dp_size=2)
191
+ # local for torchrun
192
+ twinkle.initialize(mode='ray', groups=device_group, global_device_mesh=device_mesh)
193
+
194
+
195
+ def train():
196
+ # to load model from Hugging Face, use 'hf://...'
197
+ base_model = 'ms://Qwen/Qwen2.5-7B-Instruct'
198
+ # 1000 samples
199
+ dataset = Dataset(dataset_meta=DatasetMeta('ms://swift/self-cognition', data_slice=range(1000)))
200
+ # Set template to prepare encoding
201
+ dataset.set_template('Template', model_id=base_model)
202
+ # Preprocess the dataset to standard format
203
+ dataset.map(SelfCognitionProcessor('twinkle LLM', 'ModelScope Community'))
204
+ # Encode dataset
205
+ dataset.encode()
206
+ # Global batch size = 8, for GPUs, so 1 sample per GPU
207
+ dataloader = DataLoader(dataset=dataset, batch_size=8, min_batch_size=8)
208
+ # Use a TransformersModel
209
+ model = TransformersModel(model_id=base_model, remote_group='default')
210
+
211
+ lora_config = LoraConfig(
212
+ r=8,
213
+ lora_alpha=32,
214
+ target_modules='all-linear'
215
+ )
216
+
217
+ # Add a lora to model, with name `default`
218
+ # Comment this to use full-parameter training
219
+ model.add_adapter_to_model('default', lora_config, gradient_accumulation_steps=2)
220
+ # Add Optimizer for lora `default`
221
+ model.set_optimizer(optimizer_cls='AdamW', lr=1e-4)
222
+ # Add LRScheduler for lora `default`
223
+ model.set_lr_scheduler(scheduler_cls='CosineWarmupScheduler', num_warmup_steps=5,
224
+ num_training_steps=len(dataloader))
225
+ for step, batch in enumerate(dataloader):
226
+ # Do forward and backward
227
+ model.forward_backward(inputs=batch)
228
+ # Step
229
+ model.clip_grad_and_step()
230
+ if step % 20 == 0:
231
+ # Print metric
232
+ metric = model.calculate_metric(is_training=True)
233
+ print(f'Current is step {step} of {len(dataloader)}, metric: {metric}')
234
+ model.save(f'last-checkpoint')
235
+
236
+
237
+ if __name__ == '__main__':
238
+ train()
239
+ ```
240
+
241
+ ### Using Tinker-Like API
242
+
243
+ ```python
244
+ import os
245
+ from tqdm import tqdm
246
+ from tinker import types
247
+ from twinkle_client import init_tinker_compat_client
248
+ from twinkle.dataloader import DataLoader
249
+ from twinkle.dataset import Dataset, DatasetMeta
250
+ from twinkle.preprocessor import SelfCognitionProcessor
251
+ from twinkle.server.tinker.common import input_feature_to_datum
252
+
253
+ base_model = 'ms://Qwen/Qwen3-30B-A3B-Instruct-2507'
254
+ base_url='http://www.modelscope.cn/twinkle'
255
+ api_key=os.environ.get('MODELSCOPE_TOKEN')
256
+
257
+ # Use twinkle dataset to load the data
258
+ dataset = Dataset(dataset_meta=DatasetMeta('ms://swift/self-cognition', data_slice=range(500)))
259
+ dataset.set_template('Template', model_id=base_model, max_length=256)
260
+ dataset.map(SelfCognitionProcessor('twinkle Model', 'twinkle Team'), load_from_cache_file=False)
261
+ dataset.encode(batched=True, load_from_cache_file=False)
262
+ dataloader = DataLoader(dataset=dataset, batch_size=8)
263
+
264
+ # Initialize tinker client
265
+ service_client = init_tinker_compat_client(base_url, api_key)
266
+ training_client = service_client.create_lora_training_client(base_model=base_model[len('ms://'):], rank=16)
267
+
268
+ # Training loop: use input_feature_to_datum to transfer the input format
269
+ for epoch in range(3):
270
+ for step, batch in tqdm(enumerate(dataloader)):
271
+ input_datum = [input_feature_to_datum(input_feature) for input_feature in batch]
272
+
273
+ fwdbwd_future = training_client.forward_backward(input_datum, "cross_entropy")
274
+ optim_future = training_client.optim_step(types.AdamParams(learning_rate=1e-4))
275
+
276
+ fwdbwd_result = fwdbwd_future.result()
277
+ optim_result = optim_future.result()
278
+
279
+ training_client.save_state(f"twinkle-lora-{epoch}").result()
280
+ ```
281
+
282
+ ## Architecture Design
283
+
284
+ <img src="assets/framework.jpg" style="max-width: 500px; width: 100%;" />
285
+
286
+ **Twinkle✨** features a decoupled **Client-Server architecture** designed for maximum flexibility.
287
+ The client-side provides two distinct integration paths:
288
+
289
+ * **Twinkle✨ Native:** A conforming API that mirrors the server-side interface for seamless end-to-end integration.
290
+ * **Tinker Compatibility:** Full support for the native Tinker API, enabling developers to leverage Twinkle✨’s backend using Tinker client.
291
+
292
+ This dual-path design ensures access to Twinkle✨’s training services using Tinker API, with a simple modification of the Tinker base URL.
293
+
294
+ ## Multi-Tenancy
295
+
296
+ **Twinkle✨** supports simultaneous multi-tenant training on a shared base model. Leveraging a **LoRA Pool + Tenant Application** architecture, Twinkle enables up to **N tenants** to train in parallel with complete isolation. This design offers unprecedented flexibility: from the model's perspective, each tenant's session is distinct, supporting heterogeneous configurations including unique **data padding strategies, optimizers, and loss functions**—all running concurrently on the same base model.
297
+
298
+ *Note: This feature is currently optimized for [LoRA](https://github.com/huggingface/peft).*
299
+
300
+ <img src="assets/multi_lora.png" style="max-width: 500px; width: 100%;" />
301
+
302
+ For example:
303
+
304
+ - Tenant A: Load local private dataset locally, LoRA rank=8, using base model for SFT
305
+ - Tenant B: Load open-source dataset from Hub remotely, LoRA rank=32, using base model for PT
306
+ - Tenant C: Use base model for GRPO loss calculation, using Sampler for sampling
307
+ - Tenant D: Use base model for logps inference
308
+
309
+ These processes are executed concurrently on a single base model because the **Model and Sampler**
310
+ are integrated as **task-agnostic components** within the Twinkle✨ ecosystem.
311
+ Upon completion, checkpoints are automatically pushed to **ModelScope** or **HuggingFace** repositories
312
+ (private by default). On the server side, Twinkle✨ provides a robust multi-tenant suite
313
+ featuring **automated cluster management** and **dynamic scaling**, making it the
314
+ foundation for building customizable, enterprise-grade training services.
315
+
316
+ > As a modular framework, Twinkle✨ also supports remote temporary exclusive training, i.e., training in full-parameter mode.
317
+
318
+ ## 🛠️ Twinkle✨ Modular Ecosystem
319
+
320
+ <div align="center">
321
+ <table style="width: 100%; border-collapse: separate; border-spacing: 8px;">
322
+ <tr>
323
+ <td width="20%" bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
324
+ <p align="center"><b>Dataset</b><br><sub>Data loading and preprocessing</sub></p>
325
+ </td>
326
+ <td width="20%" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
327
+ <p align="center"><b>Template</b><br><sub>Encoding and decoding</sub></p>
328
+ </td>
329
+ <td width="20%" bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
330
+ <p align="center"><b>DataLoader</b><br><sub>Data distribution and batching</sub></p>
331
+ </td>
332
+ <td width="20%" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
333
+ <p align="center"><b>Preprocessor</b><br><sub>Data ETL</sub></p>
334
+ </td>
335
+ <td width="20%" bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
336
+ <p align="center"><b>InputProcessor</b><br><sub>Task-specific input processing</sub></p>
337
+ </td>
338
+ </tr>
339
+ <tr>
340
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
341
+ <p align="center"><b>Model</b><br><sub>Large models, supports multiple frameworks</sub></p>
342
+ </td>
343
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
344
+ <p align="center"><b>Sampler</b><br><sub>Sampler logic</sub></p>
345
+ </td>
346
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
347
+ <p align="center"><b>Loss</b><br><sub>Loss functions</sub></p>
348
+ </td>
349
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
350
+ <p align="center"><b>Metric</b><br><sub>Training metrics collection</sub></p>
351
+ </td>
352
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
353
+ <p align="center"><b>Reward</b><br><sub>Reward function</sub></p>
354
+ </td>
355
+ </tr>
356
+ <tr>
357
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
358
+ <p align="center"><b>Advantage</b><br><sub>Advantage function</sub></p>
359
+ </td>
360
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
361
+ <p align="center"><b>CheckpointEngine</b><br><sub>Weight synchronization</sub></p>
362
+ </td>
363
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
364
+ <p align="center"><b>Patch</b><br><sub>Patches for model fixes</sub></p>
365
+ </td>
366
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
367
+ <p align="center"><b>Module</b><br><sub>Components, e.g., Optimizer</sub></p>
368
+ </td>
369
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
370
+ <p align="center"><b>Kernel</b><br><sub>Operators</sub></p>
371
+ </td>
372
+ </tr>
373
+ <tr>
374
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
375
+ <p align="center"><b>Server</b><br><sub>Start backend cluster</sub></p>
376
+ </td>
377
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
378
+ <p align="center"><b>Client</b><br><sub>Client code</sub></p>
379
+ </td>
380
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
381
+ <p align="center"><b>Infra</b><br><sub>Isolate ray and torchrun differences</sub></p>
382
+ </td>
383
+ <td style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
384
+ <p align="center"><b>Plugin</b><br><sub>Use hub components</sub></p>
385
+ </td>
386
+ <td bgcolor="#f6f8fa" style="border: 1px solid #d0d7de; border-radius: 8px; padding: 12px;">
387
+ <p align="center"><b>Hub</b><br><sub>Interface with HF/MS libraries</sub></p>
388
+ </td>
389
+ </tr>
390
+ </table>
391
+ </div>
392
+
393
+ ## Community Components
394
+
395
+ | Component Type | Component Link | Component Function | Author |
396
+ | -------------- | -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | ------------------- |
397
+ | Patch | [qwen3_moe_transformers4_patch](https://www.modelscope.cn/models/twinkle-kit/qwen3_moe_transformers4_patch) | Fixes Qwen3 MoE model hang issue during FSDP2 training, effective for transformers==4.x | ModelScope Official |
398
+
399
+ ## Contributions
400
+
401
+ Twinkle✨ is a collaborative initiative put together by ModelScope in partnership
402
+ with the open-source community, with key contributions from strategic stakeholders
403
+ including China Merchants Bank Tech Team.
404
+
405
+ We are grateful to the open-source community, particularly the projects that inspired us,
406
+ including [Transformers](https://github.com/huggingface/transformers),
407
+ [MS-SWIFT](https://github.com/modelscope/swift),
408
+ [veRL](https://github.com/verl-project/verl), [Tinker](https://github.com/thinking-machines-lab/tinker), and many others.
409
+
410
+ We welcome
411
+ open contributions via [issues](https://github.com/modelscope/twinkle/issues) and [pull-requests](https://github.com/modelscope/twinkle/pulls).