ling-aspec-runtime 0.1.0b0__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 (271) hide show
  1. aspec/__init__.py +66 -0
  2. aspec/_build_info.json +1 -0
  3. aspec/_environment.py +135 -0
  4. aspec/_target_worker.py +29 -0
  5. aspec/assets.py +59 -0
  6. aspec/calibration.py +629 -0
  7. aspec/cli.py +60 -0
  8. aspec/commands/__init__.py +15 -0
  9. aspec/commands/_checkpoint_audit.py +163 -0
  10. aspec/commands/_recipe_capacity.py +122 -0
  11. aspec/commands/_recipe_smoke.py +217 -0
  12. aspec/commands/convert_pretokenized_jsonl.py +236 -0
  13. aspec/commands/convert_to_hf.py +536 -0
  14. aspec/commands/doctor.py +161 -0
  15. aspec/commands/evaluate_draft.py +240 -0
  16. aspec/commands/prepare_quality_dataset.py +177 -0
  17. aspec/commands/prepare_selected_answers.py +192 -0
  18. aspec/commands/prepare_target_dataset.py +232 -0
  19. aspec/commands/quickstart.py +119 -0
  20. aspec/commands/recipes.py +547 -0
  21. aspec/commands/stitch_native_mtp.py +131 -0
  22. aspec/compat/__init__.py +15 -0
  23. aspec/compat/fa4_cutlass.py +159 -0
  24. aspec/compat/patches/sglang-v0.5.21.patch +689 -0
  25. aspec/compat/sglang_patch.py +114 -0
  26. aspec/config/__init__.py +27 -0
  27. aspec/config/dflash2_draft_config.json +33 -0
  28. aspec/config/dflash2_qwen3_8b_swa_draft_config.json +41 -0
  29. aspec/config/dflash_draft_config.json +19 -0
  30. aspec/config/dflash_qwen3_5_9b_dmtp_config.json +106 -0
  31. aspec/config/dflash_qwen3_8b_two_feature_draft_config.json +24 -0
  32. aspec/config/draft_algorithm.py +81 -0
  33. aspec/config/draft_attention.py +467 -0
  34. aspec/config/draft_execution.py +104 -0
  35. aspec/config/draft_structure.py +101 -0
  36. aspec/config/dspark_draft_config.json +23 -0
  37. aspec/config/dspark_ling31_flash_mla_moe_draft_config.json +53 -0
  38. aspec/config/dspark_ling3_flash_csa_dense_draft_config.json +40 -0
  39. aspec/config/dspark_ling3_flash_csa_indexer_draft_config.json +40 -0
  40. aspec/config/dspark_ling3_flash_csa_sparse_draft_config.json +40 -0
  41. aspec/config/dspark_ling3_flash_draft_config.json +33 -0
  42. aspec/config/dspark_ling3_flash_moe_draft_config.json +66 -0
  43. aspec/config/dspark_ling3_flash_moe_swa_draft_config.json +67 -0
  44. aspec/config/dspark_ling3_flash_qsa_dense_draft_config.json +41 -0
  45. aspec/config/dspark_ling3_flash_qsa_indexer_draft_config.json +41 -0
  46. aspec/config/dspark_ling3_flash_qsa_sparse_draft_config.json +41 -0
  47. aspec/config/dspark_ling3_flash_qwen_moe_draft_config.json +31 -0
  48. aspec/config/dspark_ling3_flash_swa_draft_config.json +34 -0
  49. aspec/config/dspark_ling3_tiny_draft_config.json +24 -0
  50. aspec/config/dspark_qwen3_5_9b_dmtp_config.json +110 -0
  51. aspec/config/dspark_qwen3_5_9b_draft_config.json +24 -0
  52. aspec/config/dspark_qwen3_8_27b_draft_config.json +24 -0
  53. aspec/config/dspark_qwen3_8b_dsa_dense_draft_config.json +34 -0
  54. aspec/config/dspark_qwen3_8b_dsa_indexer_draft_config.json +34 -0
  55. aspec/config/dspark_qwen3_8b_dsa_sparse_draft_config.json +34 -0
  56. aspec/config/dspark_qwen3_8b_mla_draft_config.json +40 -0
  57. aspec/config/dspark_qwen3_8b_moe_draft_config.json +37 -0
  58. aspec/config/eagle3_qwen3_8b_draft_config.json +41 -0
  59. aspec/config/eagle3_qwen3_8b_mla_draft_config.json +49 -0
  60. aspec/config/eagle3_qwen3_8b_pruned_draft_config.json +39 -0
  61. aspec/config/eagle4.py +174 -0
  62. aspec/config/eagle4_qwen3_8b_candidate_config.json +54 -0
  63. aspec/config/eagle4_qwen3_8b_draft_config.json +54 -0
  64. aspec/config/inference_config.py +232 -0
  65. aspec/config/ling_structure.py +87 -0
  66. aspec/config/mooncake_config.py +690 -0
  67. aspec/config/mtp_conditioning.py +103 -0
  68. aspec/config/qwen3_5_9b_mtp_draft_config.json +81 -0
  69. aspec/config/train_config.py +1150 -0
  70. aspec/config/utils.py +227 -0
  71. aspec/controller/__init__.py +37 -0
  72. aspec/controller/eval.py +357 -0
  73. aspec/controller/inference_manager.py +1324 -0
  74. aspec/controller/loop.py +2363 -0
  75. aspec/controller/packing.py +167 -0
  76. aspec/controller/setup.py +172 -0
  77. aspec/controller/shared_source.py +573 -0
  78. aspec/controller/training_controller.py +1921 -0
  79. aspec/data/__init__.py +56 -0
  80. aspec/data/dataset.py +714 -0
  81. aspec/data/parse.py +716 -0
  82. aspec/data/preprocessing.py +427 -0
  83. aspec/data/renderers/__init__.py +33 -0
  84. aspec/data/renderers/base.py +70 -0
  85. aspec/data/renderers/kimi_k3.py +303 -0
  86. aspec/data/renderers/registry.py +57 -0
  87. aspec/data/template.py +292 -0
  88. aspec/data/utils.py +766 -0
  89. aspec/draft_artifact.py +286 -0
  90. aspec/evidence.py +1638 -0
  91. aspec/feature_contract.py +403 -0
  92. aspec/inference/__init__.py +19 -0
  93. aspec/inference/concurrency.py +70 -0
  94. aspec/inference/context.py +299 -0
  95. aspec/inference/diagnostics.py +61 -0
  96. aspec/inference/engine/__init__.py +34 -0
  97. aspec/inference/engine/base.py +92 -0
  98. aspec/inference/engine/offline_replay_engine.py +240 -0
  99. aspec/inference/engine/sgl_engine.py +1043 -0
  100. aspec/inference/engine/sgl_engine_decode.py +424 -0
  101. aspec/inference/engine/target_actor.py +82 -0
  102. aspec/inference/factory.py +693 -0
  103. aspec/inference/managed.py +99 -0
  104. aspec/inference/service.py +495 -0
  105. aspec/inference/service_client.py +106 -0
  106. aspec/inference/service_protocol.py +129 -0
  107. aspec/inference/sglang_v0521.py +578 -0
  108. aspec/inference/target_prefill.py +326 -0
  109. aspec/inference/target_runtime.py +52 -0
  110. aspec/kernels/__init__.py +15 -0
  111. aspec/kernels/cutedsl/__init__.py +15 -0
  112. aspec/kernels/cutedsl/_primitives.py +195 -0
  113. aspec/kernels/cutedsl/cudnn_gqa/FENCE_AUDIT.md +114 -0
  114. aspec/kernels/cutedsl/cudnn_gqa/README.md +114 -0
  115. aspec/kernels/cutedsl/cudnn_gqa/__init__.py +1 -0
  116. aspec/kernels/cutedsl/cudnn_gqa/backward.py +3593 -0
  117. aspec/kernels/cutedsl/cudnn_gqa/backward_sm90.py +1465 -0
  118. aspec/kernels/cutedsl/cudnn_gqa/helpers.py +100 -0
  119. aspec/kernels/cutedsl/cudnn_gqa/postprocess.py +581 -0
  120. aspec/kernels/cutedsl/cudnn_gqa/preprocess.py +333 -0
  121. aspec/kernels/cutedsl/cudnn_gqa/upstream_sha256.json +9 -0
  122. aspec/kernels/cutedsl/dspark_qk_norm_rope.py +722 -0
  123. aspec/kernels/cutedsl/dspark_qk_norm_rope_bwd.py +1254 -0
  124. aspec/locality.py +93 -0
  125. aspec/models/__init__.py +31 -0
  126. aspec/models/dflash.py +1248 -0
  127. aspec/models/dflash2.py +134 -0
  128. aspec/models/dmtp.py +181 -0
  129. aspec/models/draft/__init__.py +42 -0
  130. aspec/models/draft/auto.py +121 -0
  131. aspec/models/draft/base.py +375 -0
  132. aspec/models/draft/csa.py +1984 -0
  133. aspec/models/draft/deepseek_eagle.py +575 -0
  134. aspec/models/draft/dflash.py +1518 -0
  135. aspec/models/draft/dflash2.py +514 -0
  136. aspec/models/draft/dsa.py +451 -0
  137. aspec/models/draft/dspark.py +484 -0
  138. aspec/models/draft/eagle4.py +198 -0
  139. aspec/models/draft/eagle4_moe.py +213 -0
  140. aspec/models/draft/keymap.py +68 -0
  141. aspec/models/draft/ling_moe.py +172 -0
  142. aspec/models/draft/llama3_eagle.py +2484 -0
  143. aspec/models/draft/moe.py +93 -0
  144. aspec/models/draft/mtp.py +202 -0
  145. aspec/models/draft/qsa.py +1543 -0
  146. aspec/models/dspark.py +636 -0
  147. aspec/models/eagle3.py +727 -0
  148. aspec/models/eagle4.py +38 -0
  149. aspec/models/mtp.py +136 -0
  150. aspec/models/ops/__init__.py +45 -0
  151. aspec/models/ops/acceptance.py +37 -0
  152. aspec/models/ops/anchors.py +266 -0
  153. aspec/models/ops/compact_varlen.py +258 -0
  154. aspec/models/ops/cudnn_flex_attention.py +526 -0
  155. aspec/models/ops/cudnn_gqa_backward.py +347 -0
  156. aspec/models/ops/direct_cp.py +1112 -0
  157. aspec/models/ops/distributed_sparse.py +273 -0
  158. aspec/models/ops/dspark_qk_norm_rope.py +419 -0
  159. aspec/models/ops/eagle4_candidate.py +54 -0
  160. aspec/models/ops/eagle_thd.py +315 -0
  161. aspec/models/ops/fa4_sparse.py +512 -0
  162. aspec/models/ops/flex_attention.py +503 -0
  163. aspec/models/ops/gradient_scaling.py +48 -0
  164. aspec/models/ops/loss.py +501 -0
  165. aspec/models/ops/loss_mask.py +115 -0
  166. aspec/models/ops/output_blocks.py +111 -0
  167. aspec/models/ops/thd_attention.py +220 -0
  168. aspec/models/ops/thd_sparse_metadata.py +186 -0
  169. aspec/models/ops/tiled_l1.py +209 -0
  170. aspec/models/target/__init__.py +34 -0
  171. aspec/models/target/eagle3_target_model.py +268 -0
  172. aspec/models/target/target_utils.py +365 -0
  173. aspec/models/vocab.py +45 -0
  174. aspec/multi_draft.py +843 -0
  175. aspec/offline/__init__.py +15 -0
  176. aspec/offline/dataset.py +318 -0
  177. aspec/offline/generate.py +311 -0
  178. aspec/offline/saving_actor.py +136 -0
  179. aspec/planning.py +2326 -0
  180. aspec/ray/__init__.py +19 -0
  181. aspec/ray/cluster_preflight.py +1302 -0
  182. aspec/ray/node_local_files.py +249 -0
  183. aspec/ray/owner_fence.py +151 -0
  184. aspec/ray/placement_group.py +717 -0
  185. aspec/ray/ray_actor.py +113 -0
  186. aspec/ray/train_group.py +365 -0
  187. aspec/reporting.py +379 -0
  188. aspec/resources/configs/recipes/ling31_flash_mla_moe_dspark.yaml +107 -0
  189. aspec/resources/configs/recipes/ling3_flash_dspark.yaml +102 -0
  190. aspec/resources/configs/recipes/ling3_tiny_dspark.yaml +100 -0
  191. aspec/resources/configs/recipes/quickstart_qwen3_8b.yaml +71 -0
  192. aspec/resources/configs/recipes/qwen3_5_9b_dmtp.yaml +106 -0
  193. aspec/resources/configs/recipes/qwen3_5_9b_mtp.yaml +91 -0
  194. aspec/resources/configs/recipes/qwen3_8_27b_256k_cudnn_cp2_nvlink.yaml +119 -0
  195. aspec/resources/configs/recipes/qwen3_8b_4k_multi_draft_nvlink.yaml +96 -0
  196. aspec/resources/configs/recipes/qwen3_8b_4k_multinode_node_local_nvlink.yaml +151 -0
  197. aspec/resources/configs/recipes/qwen3_8b_dflash.yaml +91 -0
  198. aspec/resources/configs/recipes/qwen3_8b_dspark.yaml +104 -0
  199. aspec/resources/configs/recipes/qwen3_8b_eagle3.yaml +98 -0
  200. aspec/resources/configs/recipes/qwen3_8b_eagle4.yaml +92 -0
  201. aspec/resources/configs/recipes/qwen3_8b_eagle4_candidate.yaml +93 -0
  202. aspec/resources/examples/data/sample_conversations.jsonl +1000 -0
  203. aspec/resources/requirements/draft.txt +12 -0
  204. aspec/resources/requirements/sglang-v0.5.21.txt +7 -0
  205. aspec/resources/tools/recipe_catalog.json +150 -0
  206. aspec/runtime_control.py +81 -0
  207. aspec/runtime_identity.py +119 -0
  208. aspec/topology.py +800 -0
  209. aspec/topology_runner.py +1243 -0
  210. aspec/train_entry.py +647 -0
  211. aspec/training/__init__.py +21 -0
  212. aspec/training/checkpoint.py +1132 -0
  213. aspec/training/data_fetcher.py +1279 -0
  214. aspec/training/dflash2_trainer.py +68 -0
  215. aspec/training/dflash_trainer.py +948 -0
  216. aspec/training/dspark_trainer.py +103 -0
  217. aspec/training/eagle3_trainer.py +695 -0
  218. aspec/training/eagle4_trainer.py +120 -0
  219. aspec/training/evaluation.py +62 -0
  220. aspec/training/fsdp.py +232 -0
  221. aspec/training/lr_scheduler.py +276 -0
  222. aspec/training/mtp_trainer.py +61 -0
  223. aspec/training/optimizer.py +182 -0
  224. aspec/training/trainer.py +1012 -0
  225. aspec/training/trainer_actor.py +257 -0
  226. aspec/transfer/__init__.py +21 -0
  227. aspec/transfer/mooncake/__init__.py +56 -0
  228. aspec/transfer/mooncake/buffers.py +385 -0
  229. aspec/transfer/mooncake/deferred_delete.py +306 -0
  230. aspec/transfer/mooncake/eagle_store.py +987 -0
  231. aspec/transfer/mooncake/helpers.py +116 -0
  232. aspec/transfer/mooncake/p2p.py +1706 -0
  233. aspec/transfer/mooncake/store.py +602 -0
  234. aspec/transfer/mooncake/utils.py +343 -0
  235. aspec/utils/__init__.py +21 -0
  236. aspec/utils/distributed.py +272 -0
  237. aspec/utils/env.py +90 -0
  238. aspec/utils/logging.py +276 -0
  239. aspec/utils/memory.py +96 -0
  240. aspec/utils/misc.py +129 -0
  241. aspec/utils/processing.py +91 -0
  242. aspec/utils/profiling.py +179 -0
  243. aspec/utils/tensor.py +31 -0
  244. aspec/utils/timeline.py +286 -0
  245. aspec/utils/train_dump.py +499 -0
  246. aspec/utils/types.py +52 -0
  247. aspec/utils/usp.py +115 -0
  248. aspec/utils/wandb.py +246 -0
  249. aspec/work_lifecycle.py +434 -0
  250. ling_aspec_runtime-0.1.0b0.dist-info/METADATA +229 -0
  251. ling_aspec_runtime-0.1.0b0.dist-info/RECORD +271 -0
  252. ling_aspec_runtime-0.1.0b0.dist-info/WHEEL +5 -0
  253. ling_aspec_runtime-0.1.0b0.dist-info/entry_points.txt +2 -0
  254. ling_aspec_runtime-0.1.0b0.dist-info/licenses/LICENSE +201 -0
  255. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/AngelSpec-LICENSE +474 -0
  256. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/DFlash-MIT.txt +21 -0
  257. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/DeepSpec-LICENSE +21 -0
  258. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/EAGLE-LICENSE +13 -0
  259. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/FlashAttention-AUTHORS +1 -0
  260. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/FlashAttention-BSD-3-Clause.txt +29 -0
  261. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/FlashLinearAttention-MIT.txt +19 -0
  262. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/Ling-V2-MIT.txt +21 -0
  263. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/NVIDIA-Megatron-LM-BSD-3-Clause.txt +24 -0
  264. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/NVIDIA-cuDNN-Frontend-MIT.txt +21 -0
  265. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/SGLang-LICENSE +201 -0
  266. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/SpecForge-MIT.txt +21 -0
  267. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/Speculators-LICENSE +201 -0
  268. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/TorchSpec-LICENSE +21 -0
  269. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/TorchSpec-THIRDPARTYNOTICES +460 -0
  270. ling_aspec_runtime-0.1.0b0.dist-info/licenses/licenses/Transformers-LICENSE +203 -0
  271. ling_aspec_runtime-0.1.0b0.dist-info/top_level.txt +1 -0
aspec/__init__.py ADDED
@@ -0,0 +1,66 @@
1
+ # Copyright (c) 2026 LightSeek Foundation
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+
21
+ """ASpec - Torch-native speculative decoding training framework."""
22
+
23
+ from importlib import import_module
24
+
25
+ _EXPORTS = {
26
+ "DFlashModel": "aspec.models.dflash",
27
+ "DFlash2Model": "aspec.models.dflash2",
28
+ "AutoDraftModelConfig": "aspec.models.draft",
29
+ "AutoEagle3DraftModel": "aspec.models.draft",
30
+ "DFlashConfig": "aspec.models.draft.dflash",
31
+ "DFlashDraftModel": "aspec.models.draft.dflash",
32
+ "DFlash2Config": "aspec.models.draft.dflash2",
33
+ "DFlash2DraftModel": "aspec.models.draft.dflash2",
34
+ "DSparkConfig": "aspec.models.draft.dspark",
35
+ "DSparkDraftModel": "aspec.models.draft.dspark",
36
+ "DSparkModel": "aspec.models.dspark",
37
+ "Eagle3Model": "aspec.models.eagle3"
38
+ }
39
+
40
+ __all__ = [
41
+ "Eagle3Model",
42
+ "DFlashModel",
43
+ "DFlashConfig",
44
+ "DFlashDraftModel",
45
+ "DFlash2Model",
46
+ "DFlash2Config",
47
+ "DFlash2DraftModel",
48
+ "DSparkModel",
49
+ "DSparkConfig",
50
+ "DSparkDraftModel",
51
+ "AutoDraftModelConfig",
52
+ "AutoEagle3DraftModel",
53
+ ]
54
+
55
+
56
+ def __getattr__(name):
57
+ """Defer model imports until the CLI has configured native libraries."""
58
+ if name not in _EXPORTS:
59
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
60
+ value = getattr(import_module(_EXPORTS[name]), name)
61
+ globals()[name] = value
62
+ return value
63
+
64
+
65
+ def __dir__():
66
+ return sorted(set(globals()) | set(__all__))
aspec/_build_info.json ADDED
@@ -0,0 +1 @@
1
+ {"dirty": true, "revision": "11f6c038fa3ad1794424844857e6bdeded071807"}
aspec/_environment.py ADDED
@@ -0,0 +1,135 @@
1
+ # Copyright 2026 Ant Group Co., Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Prepare this venv's native libraries before executing a fresh process."""
16
+ import hashlib
17
+ import importlib.metadata
18
+ import os
19
+ import sys
20
+ import sysconfig
21
+ from pathlib import Path
22
+
23
+
24
+ def is_target_environment():
25
+ marker = Path(sys.prefix) / ".aspec-role"
26
+ if marker.is_file() and marker.read_text().strip() == "target":
27
+ return True
28
+ try:
29
+ importlib.metadata.version("ling-aspec-target")
30
+ except importlib.metadata.PackageNotFoundError:
31
+ return False
32
+ return True
33
+
34
+
35
+ def require_draft_environment():
36
+ if is_target_environment():
37
+ raise SystemExit("Run training from the Draft venv (pip install ling-aspec); "
38
+ "this interpreter belongs to Target")
39
+
40
+
41
+ def _toolkit_view(toolkit, env):
42
+ """Expose pip's CUDA layout to JITs expecting a development toolkit.
43
+
44
+ NVIDIA wheels ship lib/libcudart.so.13, while SGLang links -lcudart from
45
+ CUDA_HOME/lib64. Keep the development aliases in ASpec's private cache;
46
+ the installed wheels and host toolkit remain untouched.
47
+ """
48
+ cache = Path(env.get("ASPEC_CACHE_HOME") or
49
+ Path(env.get("XDG_CACHE_HOME") or Path.home() / ".cache") / "aspec")
50
+ identity = hashlib.sha256(str(toolkit).encode()).hexdigest()[:16]
51
+ view = cache.expanduser().resolve() / "cuda-toolkit" / identity
52
+ libraries = view / "lib64"
53
+ libraries.mkdir(parents=True, exist_ok=True)
54
+
55
+ def link(destination, source):
56
+ try:
57
+ destination.symlink_to(source, target_is_directory=source.is_dir())
58
+ except FileExistsError:
59
+ if not destination.is_symlink() or destination.readlink() != source:
60
+ raise ValueError(f"Unexpected file in private CUDA toolkit view: {destination}")
61
+
62
+ for source in toolkit.iterdir():
63
+ if source.name != "lib64":
64
+ link(view / source.name, source)
65
+ for source in sorted((toolkit / "lib").iterdir(), key=lambda p: (len(p.name), p.name)):
66
+ link(libraries / source.name, source)
67
+ if ".so." in source.name:
68
+ alias = libraries / (source.name.split(".so.", 1)[0] + ".so")
69
+ if not alias.is_symlink():
70
+ link(alias, source)
71
+ return view
72
+
73
+
74
+ def command_environment():
75
+ if sys.platform != "linux" or sys.version_info[:2] < (3, 11):
76
+ raise ValueError("Use Linux with Python >=3.11.")
77
+ active = os.environ.get("VIRTUAL_ENV")
78
+ if not active or sys.prefix == sys.base_prefix or Path(active).resolve() != Path(sys.prefix).resolve():
79
+ raise ValueError("Activate the intended venv; python and VIRTUAL_ENV must agree.")
80
+ if os.environ.get("PYTHONPATH") or os.environ.get("PYTHONHOME"):
81
+ raise ValueError("Unset PYTHONPATH and PYTHONHOME; use the active isolated venv.")
82
+ settings = dict(line.lower().replace(" ", "").split("=", 1)
83
+ for line in (Path(sys.prefix) / "pyvenv.cfg").read_text().splitlines()
84
+ if "=" in line)
85
+ if settings.get("include-system-site-packages") != "false":
86
+ raise ValueError("Use include-system-site-packages=false.")
87
+ env = dict(os.environ)
88
+ paths = []
89
+ if directory := env.get("ASPEC_CUDA_COMPAT_DIR"):
90
+ path = Path(directory).resolve()
91
+ if not (path / "libcuda.so.1").is_file():
92
+ raise ValueError("ASPEC_CUDA_COMPAT_DIR must contain libcuda.so.1.")
93
+ paths.append(str(path))
94
+ purelib = Path(sysconfig.get_paths()["purelib"])
95
+ toolkit = purelib / "nvidia/cu13"
96
+ if not env.get("CUDA_HOME") and not env.get("CUDA_PATH") and (toolkit / "bin/nvcc").is_file():
97
+ view = _toolkit_view(toolkit, env)
98
+ env["CUDA_HOME"] = str(view)
99
+ env["ASPEC_AUTO_CUDA_HOME"] = str(view)
100
+ env["PATH"] = str(view / "bin") + os.pathsep + env.get("PATH", "")
101
+ previous = env.get("LIBRARY_PATH", "")
102
+ env["LIBRARY_PATH"] = str(view / "lib64") + (os.pathsep + previous if previous else "")
103
+ paths.append(str(view / "lib64"))
104
+ paths.extend(str(p) for p in (purelib / "nvidia/cuda_runtime/lib", purelib / "torch/lib")
105
+ if p.is_dir())
106
+ if (toolkit / "lib").is_dir():
107
+ paths.append(str(toolkit / "lib"))
108
+ if paths:
109
+ previous = env.get("LD_LIBRARY_PATH", "")
110
+ env["LD_LIBRARY_PATH"] = ":".join(paths + ([previous] if previous else []))
111
+ if directory := env.get("ASPEC_CACHE_HOME"):
112
+ root = Path(directory).resolve()
113
+ root.mkdir(parents=True, exist_ok=True)
114
+ for key, suffix in {
115
+ "XDG_CACHE_HOME": "", "TRITON_CACHE_DIR": "triton",
116
+ "TORCHINDUCTOR_CACHE_DIR": "torchinductor", "TORCH_EXTENSIONS_DIR": "torch-extensions",
117
+ "CUDA_CACHE_PATH": "cuda", "CUTE_DSL_CACHE_DIR": "cute",
118
+ "FLASHINFER_WORKSPACE_BASE": "flashinfer", "SGLANG_JIT_CACHE_DIR": "sglang",
119
+ }.items():
120
+ env[key] = str(root / suffix)
121
+ return env
122
+
123
+
124
+ def execute(argv):
125
+ if not argv:
126
+ raise ValueError("Expected a command.")
127
+ os.execvpe(argv[0], argv, command_environment())
128
+
129
+
130
+ if __name__ == "__main__":
131
+ try:
132
+ execute(sys.argv[1:])
133
+ except (ValueError, OSError) as error:
134
+ print(error, file=sys.stderr)
135
+ raise SystemExit(2) from error
@@ -0,0 +1,29 @@
1
+ # Copyright 2026 Ant Group Co., Ltd.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Bootstrap the optional managed-local Target with its own native libraries."""
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ from aspec._environment import execute
9
+
10
+
11
+ def main():
12
+ os.environ["VIRTUAL_ENV"] = sys.prefix
13
+ automatic_toolkit = os.environ.pop("ASPEC_AUTO_CUDA_HOME", None)
14
+ if automatic_toolkit and os.environ.get("CUDA_HOME") == automatic_toolkit:
15
+ os.environ.pop("CUDA_HOME", None)
16
+ os.environ["PATH"] = str(Path(sys.executable).parent) + os.pathsep + os.environ.get("PATH", "")
17
+ if cache := os.environ.get("ASPEC_CACHE_HOME"):
18
+ if Path(cache).name != "target":
19
+ os.environ["ASPEC_CACHE_HOME"] = str(Path(cache) / "target")
20
+ # The supervisor belongs to Draft. Never load its CUDA libraries in Target.
21
+ os.environ.pop("LD_LIBRARY_PATH", None)
22
+ os.environ.pop("LIBRARY_PATH", None)
23
+ os.environ.pop("PYTHONPATH", None)
24
+ os.environ.pop("PYTHONHOME", None)
25
+ execute([sys.executable, *sys.argv[1:]])
26
+
27
+
28
+ if __name__ == "__main__":
29
+ main()
aspec/assets.py ADDED
@@ -0,0 +1,59 @@
1
+ # Copyright 2026 Ant Group Co., Ltd.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software
7
+ # distributed under the License is distributed on an "AS IS" BASIS,
8
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and
10
+ # limitations under the License.
11
+
12
+ """Locate installed assets, or their canonical files in a source checkout."""
13
+
14
+ from pathlib import Path, PurePosixPath
15
+
16
+ PACKAGE_ROOT = Path(__file__).resolve().parent
17
+
18
+
19
+ def asset_root():
20
+ installed = PACKAGE_ROOT / "resources"
21
+ if installed.is_dir():
22
+ return installed
23
+ source = PACKAGE_ROOT.parent
24
+ if (source / "pyproject.toml").is_file():
25
+ return source
26
+ raise FileNotFoundError("ASpec resources are missing; reinstall the wheel")
27
+
28
+
29
+ def asset_path(relative):
30
+ path = PurePosixPath(relative)
31
+ if path.is_absolute() or ".." in path.parts:
32
+ raise ValueError("ASpec assets require a package-relative path")
33
+ if path.parts[:2] == ("aspec", "config"):
34
+ result = PACKAGE_ROOT.joinpath(*path.parts[1:])
35
+ else:
36
+ result = asset_root().joinpath(*path.parts)
37
+ if not result.is_file():
38
+ raise FileNotFoundError(f"Missing ASpec asset: {relative}")
39
+ return result
40
+
41
+
42
+ def recipe_config(relative):
43
+ """Resolve bundled input assets without moving outputs into site-packages."""
44
+ from omegaconf import OmegaConf
45
+
46
+ config = OmegaConf.to_container(OmegaConf.load(asset_path(relative)), resolve=False)
47
+
48
+ def resolve(value):
49
+ if isinstance(value, dict):
50
+ return {key: resolve(item) for key, item in value.items()}
51
+ if isinstance(value, list):
52
+ return [resolve(item) for item in value]
53
+ if isinstance(value, str):
54
+ value = value.replace("aspec/config/", str(PACKAGE_ROOT / "config") + "/")
55
+ value = value.replace("../../examples/data/sample_conversations.jsonl",
56
+ str(asset_path("examples/data/sample_conversations.jsonl")))
57
+ return value
58
+
59
+ return OmegaConf.create(resolve(config))