aetherflow-ai 0.2.0__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.
- aetherflow/__init__.py +322 -0
- aetherflow/agent_api.py +234 -0
- aetherflow/backends/__init__.py +69 -0
- aetherflow/backends/base.py +74 -0
- aetherflow/backends/capabilities.py +286 -0
- aetherflow/backends/contracts.py +278 -0
- aetherflow/backends/cuda/__init__.py +16 -0
- aetherflow/backends/cuda/backend.py +1023 -0
- aetherflow/backends/cuda/codegen.py +2874 -0
- aetherflow/backends/cuda/cublas.py +428 -0
- aetherflow/backends/cuda/cudnn.py +305 -0
- aetherflow/backends/cuda/cufft.py +384 -0
- aetherflow/backends/cuda/cusparse.py +381 -0
- aetherflow/backends/cuda/dialect.py +80 -0
- aetherflow/backends/cuda/memory.py +243 -0
- aetherflow/backends/device_attention.py +257 -0
- aetherflow/backends/device_backend.py +1569 -0
- aetherflow/backends/device_conv.py +970 -0
- aetherflow/backends/device_elementwise.py +629 -0
- aetherflow/backends/device_index.py +230 -0
- aetherflow/backends/device_linear.py +1301 -0
- aetherflow/backends/device_loss.py +109 -0
- aetherflow/backends/device_misc.py +249 -0
- aetherflow/backends/device_norm.py +197 -0
- aetherflow/backends/device_plan.py +1062 -0
- aetherflow/backends/device_pool.py +592 -0
- aetherflow/backends/device_reduce.py +512 -0
- aetherflow/backends/device_scan.py +457 -0
- aetherflow/backends/device_shape.py +108 -0
- aetherflow/backends/device_shared.py +1107 -0
- aetherflow/backends/device_sparse.py +373 -0
- aetherflow/backends/discovery.py +286 -0
- aetherflow/backends/hardware.py +784 -0
- aetherflow/backends/hip/__init__.py +15 -0
- aetherflow/backends/hip/backend.py +567 -0
- aetherflow/backends/hip/blas.py +227 -0
- aetherflow/backends/hip/codegen.py +109 -0
- aetherflow/backends/hip/dialect.py +62 -0
- aetherflow/backends/hip/memory.py +418 -0
- aetherflow/backends/hip/rtc.py +126 -0
- aetherflow/backends/hip/runtime.py +2018 -0
- aetherflow/backends/kernel_cache.py +240 -0
- aetherflow/backends/reference_backend.py +76 -0
- aetherflow/backends/registry.py +224 -0
- aetherflow/conformance.py +2791 -0
- aetherflow/deploy.py +259 -0
- aetherflow/dynamic.py +252 -0
- aetherflow/frontend/__init__.py +39 -0
- aetherflow/frontend/compat.py +118 -0
- aetherflow/frontend/eager.py +345 -0
- aetherflow/frontend/module.py +320 -0
- aetherflow/frontend/torch_export.py +2047 -0
- aetherflow/fuzz.py +1093 -0
- aetherflow/gguf.py +693 -0
- aetherflow/ir/__init__.py +144 -0
- aetherflow/ir/control.py +444 -0
- aetherflow/ir/grad.py +240 -0
- aetherflow/ir/grad_rules.py +1814 -0
- aetherflow/ir/graph.py +370 -0
- aetherflow/ir/liveness.py +227 -0
- aetherflow/ir/loop.py +437 -0
- aetherflow/ir/ops.py +6333 -0
- aetherflow/ir/second_order.py +111 -0
- aetherflow/ir/serialize.py +180 -0
- aetherflow/ir/shape_contract.py +146 -0
- aetherflow/ir/types.py +363 -0
- aetherflow/ir/verifier.py +130 -0
- aetherflow/ir/version.py +196 -0
- aetherflow/lower/__init__.py +202 -0
- aetherflow/lower/cstyle.py +354 -0
- aetherflow/lower/elementwise.py +346 -0
- aetherflow/lower/indexing.py +170 -0
- aetherflow/lower/kernel.py +448 -0
- aetherflow/lower/kernels/__init__.py +262 -0
- aetherflow/lower/kernels/attention.py +390 -0
- aetherflow/lower/kernels/common.py +95 -0
- aetherflow/lower/kernels/conv.py +2927 -0
- aetherflow/lower/kernels/elementwise.py +676 -0
- aetherflow/lower/kernels/image.py +97 -0
- aetherflow/lower/kernels/interpolate.py +381 -0
- aetherflow/lower/kernels/misc.py +154 -0
- aetherflow/lower/kernels/norm.py +675 -0
- aetherflow/lower/kernels/pool.py +650 -0
- aetherflow/lower/kernels/reduce.py +891 -0
- aetherflow/lower/kernels/scan.py +939 -0
- aetherflow/lower/kernels/sparse.py +680 -0
- aetherflow/lower/plan.py +884 -0
- aetherflow/lower/translate.py +382 -0
- aetherflow/models/__init__.py +227 -0
- aetherflow/models/cached_attention.py +1133 -0
- aetherflow/models/cnn.py +208 -0
- aetherflow/models/hf_export.py +363 -0
- aetherflow/models/llama.py +875 -0
- aetherflow/models/lm.py +2888 -0
- aetherflow/models/lm_bpe.py +812 -0
- aetherflow/models/lm_text.py +159 -0
- aetherflow/models/lm_tools.py +1048 -0
- aetherflow/models/lm_unigram.py +794 -0
- aetherflow/models/lm_wordpiece.py +429 -0
- aetherflow/models/mamba.py +917 -0
- aetherflow/models/mamba_reference.py +183 -0
- aetherflow/models/mlp.py +84 -0
- aetherflow/models/moe.py +158 -0
- aetherflow/models/quantize.py +775 -0
- aetherflow/models/rnn.py +157 -0
- aetherflow/models/snn.py +362 -0
- aetherflow/models/speculative.py +426 -0
- aetherflow/models/ssm.py +179 -0
- aetherflow/models/transformer.py +382 -0
- aetherflow/passes/__init__.py +73 -0
- aetherflow/passes/autocast.py +180 -0
- aetherflow/passes/base.py +92 -0
- aetherflow/passes/cse.py +85 -0
- aetherflow/passes/dce.py +57 -0
- aetherflow/passes/fold.py +158 -0
- aetherflow/passes/fusion.py +268 -0
- aetherflow/passes/layout.py +261 -0
- aetherflow/passes/registry.py +213 -0
- aetherflow/passes/simplify.py +42 -0
- aetherflow/plugin.py +75 -0
- aetherflow/runtime/__init__.py +48 -0
- aetherflow/runtime/bf16.py +83 -0
- aetherflow/runtime/device_control.py +472 -0
- aetherflow/runtime/events.py +251 -0
- aetherflow/runtime/executor.py +617 -0
- aetherflow/runtime/fp8.py +275 -0
- aetherflow/runtime/paged_cache.py +897 -0
- aetherflow/runtime/prefix_store.py +508 -0
- aetherflow/runtime/reference.py +27 -0
- aetherflow/runtime/telemetry.py +596 -0
- aetherflow/runtime/trace.py +256 -0
- aetherflow/serve/__init__.py +20 -0
- aetherflow/serve/openai.py +767 -0
- aetherflow/train/__init__.py +154 -0
- aetherflow/train/_arrays.py +150 -0
- aetherflow/train/amp.py +219 -0
- aetherflow/train/bn.py +224 -0
- aetherflow/train/checkpoint.py +480 -0
- aetherflow/train/data.py +219 -0
- aetherflow/train/dataset.py +544 -0
- aetherflow/train/distributed.py +394 -0
- aetherflow/train/ema.py +143 -0
- aetherflow/train/grads.py +293 -0
- aetherflow/train/host_collective.py +497 -0
- aetherflow/train/lm.py +117 -0
- aetherflow/train/loop.py +1860 -0
- aetherflow/train/loss.py +105 -0
- aetherflow/train/monitor.py +208 -0
- aetherflow/train/nccl.py +831 -0
- aetherflow/train/optim.py +456 -0
- aetherflow/train/parallel.py +1296 -0
- aetherflow/train/params.py +220 -0
- aetherflow/train/pipeline.py +532 -0
- aetherflow/train/recompute.py +344 -0
- aetherflow/train/schedule.py +212 -0
- aetherflow/train/shard_io.py +685 -0
- aetherflow/train/sharded.py +490 -0
- aetherflow/train/tensor_parallel.py +527 -0
- aetherflow/train/zero.py +847 -0
- aetherflow/train/zero2.py +410 -0
- aetherflow/train/zero3.py +529 -0
- aetherflow/tuning/__init__.py +95 -0
- aetherflow/tuning/cost.py +535 -0
- aetherflow/tuning/paired.py +991 -0
- aetherflow/tuning/policy.py +195 -0
- aetherflow/tuning/search.py +290 -0
- aetherflow/tuning/signature.py +90 -0
- aetherflow/tuning/space.py +124 -0
- aetherflow/tuning/store.py +280 -0
- aetherflow/weights.py +1271 -0
- aetherflow_ai-0.2.0.dist-info/METADATA +578 -0
- aetherflow_ai-0.2.0.dist-info/RECORD +175 -0
- aetherflow_ai-0.2.0.dist-info/WHEEL +5 -0
- aetherflow_ai-0.2.0.dist-info/licenses/LICENSE +201 -0
- aetherflow_ai-0.2.0.dist-info/top_level.txt +1 -0
aetherflow/__init__.py
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"""AetherFlow:统一深度学习基础设施。
|
|
2
|
+
|
|
3
|
+
架构约定(写在导入处,因为它是所有模块的公共前提):
|
|
4
|
+
|
|
5
|
+
1. 前端与后端之间只有一份契约——IR。任何后端都不得读取前端对象。
|
|
6
|
+
2. 每个算子都有一份不依赖加速库的参考语义,它是跨后端对齐的唯一真值。
|
|
7
|
+
3. 每个节点都带可移植性标注,跨后端共享的优化路径只允许 PORTABLE 节点。
|
|
8
|
+
|
|
9
|
+
当前实现范围:高层语义 IR + JSON 序列化与版本治理 + 参考语义执行器 + 参考后端 +
|
|
10
|
+
verifier + 中层优化 pass + torch.export 前端适配 + CUDA 后端 + 后端一致性套件 +
|
|
11
|
+
插件契约分层(准入面 + 可选契约)+ 算子注册与调度(含多实现按代价选择)+
|
|
12
|
+
插件算子扩展面 + 自动微分(语义 IR 层的反向展开)+
|
|
13
|
+
训练闭环(参数容器 + SGD/Adam + 图内损失)+ 模型构件(MLP / CNN / RNN(LSTM/GRU) /
|
|
14
|
+
多层 Transformer / Mamba block / 多层 SNN)+ eager 构图前端(语法糖,产物仍是静态 IR)
|
|
15
|
+
+ SNN 的稀疏事件表示与事件内存池(`runtime/events.py`,数据侧,不进 IR)
|
|
16
|
+
+ 自动调优(参数空间 + 网格搜索 + 配置库)+ 运行时数据上报 + 只读智能体查询面
|
|
17
|
+
+ zip 硬件包(`backends/hardware.py`,manifest 校验 + 安全解压 + 装完即用 +
|
|
18
|
+
内容摘要 / 依赖声明 / Ed25519 来源签名)+ 动态执行(`dynamic.DynamicRunner`,
|
|
19
|
+
按输入形状建图并复用;声明动态轴后一次构图多次重放,一次 run 内仍是静态图)
|
|
20
|
+
+ 部署包(`deploy.py`,图 JSON + 权重 + 清单校验和,加载即校验)
|
|
21
|
+
+ 激活重算(`train/recompute.py`,分段前向只留边界,反向逐段重放)。
|
|
22
|
+
尚未实现:完整的 define-by-run 执行(逐算子即时求值)、强化学习式的配置更新、
|
|
23
|
+
硬件包的公钥分发与撤销链。
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import sys as _sys
|
|
27
|
+
|
|
28
|
+
# 运行期版本守卫:pyproject 的 requires-python 只约束安装器,源码目录直接导入、
|
|
29
|
+
# 或旧解释器复用已装好的 site-packages 时都不会经过它。实测 3.8 能导入本包的一部分
|
|
30
|
+
# 模块,但行为没有保证——"能导入、半可用"正是本项目明确拒绝的形态,所以在包入口
|
|
31
|
+
# 显式拦一次,让错误落在版本上,而不是几十层调用之后。
|
|
32
|
+
if _sys.version_info < (3, 10): # pragma: no cover - 老解释器上无法执行测试
|
|
33
|
+
raise RuntimeError(
|
|
34
|
+
"AetherFlow 需要 Python 3.10 及以上,当前解释器是 "
|
|
35
|
+
f"{_sys.version.split()[0]}。请升级解释器,或用 py -3.14 之类的启动器"
|
|
36
|
+
"选择受支持的版本。"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
from .agent_api import (
|
|
40
|
+
AgentQueryError,
|
|
41
|
+
describe_backend,
|
|
42
|
+
describe_backends,
|
|
43
|
+
describe_operator,
|
|
44
|
+
performance_summary,
|
|
45
|
+
recommend_config,
|
|
46
|
+
snapshot,
|
|
47
|
+
)
|
|
48
|
+
from .backends.base import Backend, BackendCapabilityError, unsupported_ops
|
|
49
|
+
from .backends.capabilities import (
|
|
50
|
+
BackendCapabilities,
|
|
51
|
+
OpCapability,
|
|
52
|
+
capability_gaps,
|
|
53
|
+
to_capabilities,
|
|
54
|
+
)
|
|
55
|
+
from .backends.discovery import (
|
|
56
|
+
BackendUnavailableError,
|
|
57
|
+
get_backend,
|
|
58
|
+
register_backend,
|
|
59
|
+
registered_backends,
|
|
60
|
+
unregister_backend,
|
|
61
|
+
)
|
|
62
|
+
from .backends.hardware import (
|
|
63
|
+
HardwarePackageError,
|
|
64
|
+
HardwareRecord,
|
|
65
|
+
install_hardware,
|
|
66
|
+
installed_hardware,
|
|
67
|
+
search_roots,
|
|
68
|
+
uninstall_hardware,
|
|
69
|
+
)
|
|
70
|
+
from .backends.contracts import (
|
|
71
|
+
CONTRACTS,
|
|
72
|
+
AllocatorContract,
|
|
73
|
+
CodegenContract,
|
|
74
|
+
DeviceContract,
|
|
75
|
+
DeviceInfo,
|
|
76
|
+
DeviceManagerContract,
|
|
77
|
+
MemoryContract,
|
|
78
|
+
MemoryStats,
|
|
79
|
+
StreamContract,
|
|
80
|
+
TuningContract,
|
|
81
|
+
implemented_contracts,
|
|
82
|
+
supports,
|
|
83
|
+
)
|
|
84
|
+
from .backends.reference_backend import ReferenceBackend
|
|
85
|
+
from .backends.registry import KernelRegistry
|
|
86
|
+
from .conformance import Case, CaseResult, CheckResult, Report, default_cases, run_conformance
|
|
87
|
+
from .deploy import BundleError, Deployment, load_bundle, save_bundle
|
|
88
|
+
from .dynamic import DynamicRunner
|
|
89
|
+
from .weights import (
|
|
90
|
+
FP8Weight,
|
|
91
|
+
QuantizedWeight,
|
|
92
|
+
SafeTensorsError,
|
|
93
|
+
SafeTensorsFile,
|
|
94
|
+
TensorInfo,
|
|
95
|
+
build_fp8_weight,
|
|
96
|
+
build_quantized_weight,
|
|
97
|
+
load_fp8_linear,
|
|
98
|
+
load_into_store,
|
|
99
|
+
load_quantized_linear,
|
|
100
|
+
load_safetensors,
|
|
101
|
+
load_safetensors_dir,
|
|
102
|
+
)
|
|
103
|
+
from .gguf import (
|
|
104
|
+
GGML_TYPES,
|
|
105
|
+
GGUFError,
|
|
106
|
+
GGUFFile,
|
|
107
|
+
GGUFTensorInfo,
|
|
108
|
+
read_gguf,
|
|
109
|
+
)
|
|
110
|
+
from .ir import (
|
|
111
|
+
IR_VERSION,
|
|
112
|
+
UNREPLAYABLE_EFFECTS,
|
|
113
|
+
Effect,
|
|
114
|
+
GradCtx,
|
|
115
|
+
Graph,
|
|
116
|
+
IRVersionError,
|
|
117
|
+
MemoryBudget,
|
|
118
|
+
NoGradientError,
|
|
119
|
+
Op,
|
|
120
|
+
Portability,
|
|
121
|
+
TensorType,
|
|
122
|
+
backward,
|
|
123
|
+
hessian_vector_product,
|
|
124
|
+
Conditional,
|
|
125
|
+
ControlFlowError,
|
|
126
|
+
ControlRegion,
|
|
127
|
+
GraphRegion,
|
|
128
|
+
WhileLoop,
|
|
129
|
+
LoopIR,
|
|
130
|
+
LoopSplit,
|
|
131
|
+
LoopIRError,
|
|
132
|
+
loop_to_spec,
|
|
133
|
+
loop_from_spec,
|
|
134
|
+
optimize_loop,
|
|
135
|
+
from_spec,
|
|
136
|
+
to_spec,
|
|
137
|
+
bf16,
|
|
138
|
+
boolean,
|
|
139
|
+
c128,
|
|
140
|
+
c64,
|
|
141
|
+
dumps,
|
|
142
|
+
f16,
|
|
143
|
+
f8e4m3,
|
|
144
|
+
f8e5m2,
|
|
145
|
+
f32,
|
|
146
|
+
f64,
|
|
147
|
+
from_dict,
|
|
148
|
+
i8,
|
|
149
|
+
i32,
|
|
150
|
+
loads,
|
|
151
|
+
migrate,
|
|
152
|
+
migration_path,
|
|
153
|
+
plan_memory,
|
|
154
|
+
register_migration,
|
|
155
|
+
registered_migrations,
|
|
156
|
+
to_dict,
|
|
157
|
+
unregister_migration,
|
|
158
|
+
)
|
|
159
|
+
from .ir.verifier import Issue, VerifierError, require, verify, verify_portable_only
|
|
160
|
+
from .fuzz import FuzzCase, FuzzFailure, FuzzReport, random_case, run_fuzz
|
|
161
|
+
from .plugin import BUILTIN_OPS, plugin_ops, register_operator, unregister_operator
|
|
162
|
+
from .runtime.reference import ReferenceExecutor
|
|
163
|
+
from .runtime.telemetry import RunRecord, TelemetryRecorder, load_records
|
|
164
|
+
from .tuning import (
|
|
165
|
+
AllRejectedError,
|
|
166
|
+
ConfigStore,
|
|
167
|
+
SchedulePolicy,
|
|
168
|
+
TuningRecord,
|
|
169
|
+
TuningResult,
|
|
170
|
+
TuningSpace,
|
|
171
|
+
tune,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
__version__ = "0.2.0"
|
|
175
|
+
# 后端 / 硬件包契约的主版本。它与包版本分开:包可以频繁发版,但只要
|
|
176
|
+
# Backend 协议、IR 序列化格式与方言数据结构没变,aetherflow_api 就保持 1,
|
|
177
|
+
# 已安装的 zip 硬件包不需要跟着重发。
|
|
178
|
+
__api_version__ = "1"
|
|
179
|
+
|
|
180
|
+
__all__ = [
|
|
181
|
+
"Graph",
|
|
182
|
+
"DynamicRunner",
|
|
183
|
+
"SafeTensorsError",
|
|
184
|
+
"SafeTensorsFile",
|
|
185
|
+
"TensorInfo",
|
|
186
|
+
"QuantizedWeight",
|
|
187
|
+
"FP8Weight",
|
|
188
|
+
"load_safetensors",
|
|
189
|
+
"load_safetensors_dir",
|
|
190
|
+
"GGML_TYPES",
|
|
191
|
+
"GGUFError",
|
|
192
|
+
"GGUFFile",
|
|
193
|
+
"GGUFTensorInfo",
|
|
194
|
+
"read_gguf",
|
|
195
|
+
"build_quantized_weight",
|
|
196
|
+
"load_quantized_linear",
|
|
197
|
+
"build_fp8_weight",
|
|
198
|
+
"load_fp8_linear",
|
|
199
|
+
"load_into_store",
|
|
200
|
+
"BundleError",
|
|
201
|
+
"Deployment",
|
|
202
|
+
"load_bundle",
|
|
203
|
+
"save_bundle",
|
|
204
|
+
"IR_VERSION",
|
|
205
|
+
"IRVersionError",
|
|
206
|
+
"migrate",
|
|
207
|
+
"migration_path",
|
|
208
|
+
"register_migration",
|
|
209
|
+
"unregister_migration",
|
|
210
|
+
"registered_migrations",
|
|
211
|
+
"to_dict",
|
|
212
|
+
"from_dict",
|
|
213
|
+
"dumps",
|
|
214
|
+
"loads",
|
|
215
|
+
"TensorType",
|
|
216
|
+
"MemoryBudget",
|
|
217
|
+
"plan_memory",
|
|
218
|
+
"Op",
|
|
219
|
+
"Effect",
|
|
220
|
+
"UNREPLAYABLE_EFFECTS",
|
|
221
|
+
"Portability",
|
|
222
|
+
"backward",
|
|
223
|
+
"hessian_vector_product",
|
|
224
|
+
"ControlFlowError",
|
|
225
|
+
"ControlRegion",
|
|
226
|
+
"GraphRegion",
|
|
227
|
+
"Conditional",
|
|
228
|
+
"WhileLoop",
|
|
229
|
+
"LoopIR",
|
|
230
|
+
"LoopSplit",
|
|
231
|
+
"LoopIRError",
|
|
232
|
+
"loop_to_spec",
|
|
233
|
+
"loop_from_spec",
|
|
234
|
+
"optimize_loop",
|
|
235
|
+
"to_spec",
|
|
236
|
+
"from_spec",
|
|
237
|
+
"GradCtx",
|
|
238
|
+
"NoGradientError",
|
|
239
|
+
"f16",
|
|
240
|
+
"bf16",
|
|
241
|
+
"f8e4m3",
|
|
242
|
+
"f8e5m2",
|
|
243
|
+
"c64",
|
|
244
|
+
"c128",
|
|
245
|
+
"f32",
|
|
246
|
+
"f64",
|
|
247
|
+
"i8",
|
|
248
|
+
"i32",
|
|
249
|
+
"boolean",
|
|
250
|
+
"Backend",
|
|
251
|
+
"BackendCapabilityError",
|
|
252
|
+
"BackendCapabilities",
|
|
253
|
+
"OpCapability",
|
|
254
|
+
"capability_gaps",
|
|
255
|
+
"to_capabilities",
|
|
256
|
+
"BackendUnavailableError",
|
|
257
|
+
"get_backend",
|
|
258
|
+
"register_backend",
|
|
259
|
+
"registered_backends",
|
|
260
|
+
"unregister_backend",
|
|
261
|
+
"HardwarePackageError",
|
|
262
|
+
"HardwareRecord",
|
|
263
|
+
"install_hardware",
|
|
264
|
+
"uninstall_hardware",
|
|
265
|
+
"installed_hardware",
|
|
266
|
+
"search_roots",
|
|
267
|
+
"ReferenceBackend",
|
|
268
|
+
"KernelRegistry",
|
|
269
|
+
"unsupported_ops",
|
|
270
|
+
"CONTRACTS",
|
|
271
|
+
"DeviceContract",
|
|
272
|
+
"DeviceManagerContract",
|
|
273
|
+
"DeviceInfo",
|
|
274
|
+
"CodegenContract",
|
|
275
|
+
"TuningContract",
|
|
276
|
+
"MemoryContract",
|
|
277
|
+
"MemoryStats",
|
|
278
|
+
"StreamContract",
|
|
279
|
+
"AllocatorContract",
|
|
280
|
+
"implemented_contracts",
|
|
281
|
+
"supports",
|
|
282
|
+
"Case",
|
|
283
|
+
"CaseResult",
|
|
284
|
+
"CheckResult",
|
|
285
|
+
"Report",
|
|
286
|
+
"default_cases",
|
|
287
|
+
"run_conformance",
|
|
288
|
+
"Issue",
|
|
289
|
+
"VerifierError",
|
|
290
|
+
"verify",
|
|
291
|
+
"verify_portable_only",
|
|
292
|
+
"require",
|
|
293
|
+
"BUILTIN_OPS",
|
|
294
|
+
"plugin_ops",
|
|
295
|
+
"register_operator",
|
|
296
|
+
"unregister_operator",
|
|
297
|
+
"AllRejectedError",
|
|
298
|
+
"ConfigStore",
|
|
299
|
+
"SchedulePolicy",
|
|
300
|
+
"TuningRecord",
|
|
301
|
+
"TuningResult",
|
|
302
|
+
"TuningSpace",
|
|
303
|
+
"tune",
|
|
304
|
+
"RunRecord",
|
|
305
|
+
"TelemetryRecorder",
|
|
306
|
+
"load_records",
|
|
307
|
+
"ReferenceExecutor",
|
|
308
|
+
"FuzzCase",
|
|
309
|
+
"FuzzFailure",
|
|
310
|
+
"FuzzReport",
|
|
311
|
+
"random_case",
|
|
312
|
+
"run_fuzz",
|
|
313
|
+
"AgentQueryError",
|
|
314
|
+
"describe_backend",
|
|
315
|
+
"describe_backends",
|
|
316
|
+
"describe_operator",
|
|
317
|
+
"performance_summary",
|
|
318
|
+
"recommend_config",
|
|
319
|
+
"snapshot",
|
|
320
|
+
"__version__",
|
|
321
|
+
"__api_version__",
|
|
322
|
+
]
|
aetherflow/agent_api.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""智能体接口:给外部智能体/工具看的**只读**查询面。
|
|
2
|
+
|
|
3
|
+
它回答四类问题,全部返回可直接 JSON 序列化的字典:
|
|
4
|
+
|
|
5
|
+
1. 这个后端是什么、承接哪些算子(``describe_backend``);
|
|
6
|
+
2. 某个算子有几种实现、能不能调(``describe_operator``);
|
|
7
|
+
3. 最近跑得怎么样(``performance_summary``);
|
|
8
|
+
4. 当前形状下推荐哪条配置(``recommend_config``)。
|
|
9
|
+
|
|
10
|
+
**边界写死在这一层:不执行图、不接受代码、不改配置。**
|
|
11
|
+
|
|
12
|
+
- 没有任何"执行"入口。要看数据,先由调用方在正常流程里跑一遍并挂上
|
|
13
|
+
``TelemetryRecorder``;这一层只读记录。
|
|
14
|
+
- 不接受任意表达式或脚本字符串。查询对象只有"后端、算子名、形状、签名",
|
|
15
|
+
参数校验失败抛 ``AgentQueryError``,而不是把字符串送进 ``eval`` 之类的路径。
|
|
16
|
+
- 不写配置库。``recommend_config`` 只查;更新配置是 tuning 的事,且必须
|
|
17
|
+
先有实测。
|
|
18
|
+
|
|
19
|
+
为什么值得单独一层而不是让智能体直接 ``dir(backend)``:反射拿到的是
|
|
20
|
+
Python 对象,不是"系统承诺的查询面"。接口一旦稳定,内部结构可以自由改,
|
|
21
|
+
而智能体看到的字段语义不变;反过来,任何"顺手加个执行入口"的改动都会
|
|
22
|
+
在这里被评审看到。
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
from typing import Any, Dict, Iterable, Mapping, Optional
|
|
28
|
+
|
|
29
|
+
from .backends.contracts import implemented_contracts
|
|
30
|
+
from .runtime.telemetry import TelemetryRecorder
|
|
31
|
+
from .tuning.space import canonical_signature
|
|
32
|
+
from .tuning.store import ConfigStore
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"AgentQueryError",
|
|
36
|
+
"describe_backend",
|
|
37
|
+
"describe_backends",
|
|
38
|
+
"describe_operator",
|
|
39
|
+
"performance_summary",
|
|
40
|
+
"recommend_config",
|
|
41
|
+
"snapshot",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class AgentQueryError(ValueError):
|
|
46
|
+
"""查询不成立(算子不存在、后端不可调、签名不合法)。"""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _target(backend: object) -> object:
|
|
50
|
+
"""剥掉上报包装层:智能体问的是后端本身的能力。"""
|
|
51
|
+
return getattr(backend, "inner", backend)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _device(backend: object) -> Optional[Dict[str, Any]]:
|
|
55
|
+
info = getattr(backend, "device_info", None)
|
|
56
|
+
if info is None:
|
|
57
|
+
return None
|
|
58
|
+
try:
|
|
59
|
+
value = info()
|
|
60
|
+
except Exception: # noqa: BLE001 - 拿不到就报 None,不编造
|
|
61
|
+
return None
|
|
62
|
+
return {
|
|
63
|
+
"name": getattr(value, "name", str(value)),
|
|
64
|
+
"runtime": getattr(value, "runtime", None),
|
|
65
|
+
"total_memory_bytes": getattr(value, "total_memory_bytes", None),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _tunable_ops(backend: object) -> list:
|
|
70
|
+
names = getattr(backend, "tunable_ops", None)
|
|
71
|
+
if names is None:
|
|
72
|
+
return []
|
|
73
|
+
try:
|
|
74
|
+
return sorted(str(name) for name in names())
|
|
75
|
+
except Exception: # noqa: BLE001 - 能力查询失败等价于"没有可调项"
|
|
76
|
+
return []
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _variants(backend: object, op: str) -> list:
|
|
80
|
+
registry = getattr(backend, "kernels", None)
|
|
81
|
+
if registry is None:
|
|
82
|
+
return []
|
|
83
|
+
try:
|
|
84
|
+
variants = registry.variants(op)
|
|
85
|
+
except Exception: # noqa: BLE001
|
|
86
|
+
return []
|
|
87
|
+
return [
|
|
88
|
+
{"label": variant.label, "cost_ranked": variant.cost is not None}
|
|
89
|
+
for variant in variants
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def describe_operator(backend: object, op: str) -> Dict[str, Any]:
|
|
94
|
+
"""某个算子的实现候选与可调参数;算子未承接时 ``implemented=False``。"""
|
|
95
|
+
if not isinstance(op, str) or not op:
|
|
96
|
+
raise AgentQueryError(f"算子名必须是非空字符串,得到 {op!r}")
|
|
97
|
+
target = _target(backend)
|
|
98
|
+
capabilities = getattr(target, "capabilities", None)
|
|
99
|
+
implemented = bool(capabilities and op in capabilities())
|
|
100
|
+
|
|
101
|
+
tunable = None
|
|
102
|
+
if implemented:
|
|
103
|
+
space_of = getattr(target, "tuning_space", None)
|
|
104
|
+
if space_of is not None and op in _tunable_ops(target):
|
|
105
|
+
try:
|
|
106
|
+
tunable = space_of(op).to_dict()
|
|
107
|
+
except Exception as exc: # noqa: BLE001
|
|
108
|
+
raise AgentQueryError(f"查询 {op} 的调优空间失败:{exc}") from exc
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
"op": op,
|
|
112
|
+
"implemented": implemented,
|
|
113
|
+
"variants": _variants(target, op) if implemented else [],
|
|
114
|
+
"tunable": tunable,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def describe_backend(backend: object) -> Dict[str, Any]:
|
|
119
|
+
"""一个后端的完整静态画像。"""
|
|
120
|
+
target = _target(backend)
|
|
121
|
+
capabilities = getattr(target, "capabilities", None)
|
|
122
|
+
ops = sorted(capabilities()) if capabilities else []
|
|
123
|
+
return {
|
|
124
|
+
"name": getattr(backend, "name", "unknown"),
|
|
125
|
+
"capabilities": ops,
|
|
126
|
+
"contracts": list(implemented_contracts(target)),
|
|
127
|
+
"device": _device(target),
|
|
128
|
+
"tunable_ops": _tunable_ops(target),
|
|
129
|
+
# 只列变体,不重复 capabilities 里已有的算子名;单点查询用 describe_operator
|
|
130
|
+
"operators": {op: {"variants": _variants(target, op)} for op in ops},
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def describe_backends(backends: Iterable[object]) -> Dict[str, Any]:
|
|
135
|
+
"""多个后端一起描述;重名直接拒绝,否则结果里会静默少一个。"""
|
|
136
|
+
out: Dict[str, Any] = {}
|
|
137
|
+
for backend in backends:
|
|
138
|
+
item = describe_backend(backend)
|
|
139
|
+
name = item["name"]
|
|
140
|
+
if name in out:
|
|
141
|
+
raise AgentQueryError(f"后端名 {name!r} 重复,无法唯一定位")
|
|
142
|
+
out[name] = item
|
|
143
|
+
return out
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def performance_summary(recorder: TelemetryRecorder) -> Dict[str, Any]:
|
|
147
|
+
"""运行时上报的聚合摘要;没有记录时返回 ``runs=0`` 而不是抛错。"""
|
|
148
|
+
if not isinstance(recorder, TelemetryRecorder):
|
|
149
|
+
raise AgentQueryError(
|
|
150
|
+
f"performance_summary 需要 TelemetryRecorder,得到 {type(recorder).__name__}"
|
|
151
|
+
)
|
|
152
|
+
return recorder.summary()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def recommend_config(
|
|
156
|
+
store: ConfigStore,
|
|
157
|
+
backend: object,
|
|
158
|
+
op: str,
|
|
159
|
+
signature: Mapping[str, Any],
|
|
160
|
+
) -> Dict[str, Any]:
|
|
161
|
+
"""查配置库;未命中时给后端的默认配置,并标明来源。
|
|
162
|
+
|
|
163
|
+
``source`` 只有两个取值:``"store"``(实测过的最优)与 ``"default"``
|
|
164
|
+
(空间里第一个候选)。没有第三个——"猜一个"不是一个来源。
|
|
165
|
+
"""
|
|
166
|
+
if not isinstance(store, ConfigStore):
|
|
167
|
+
raise AgentQueryError(f"recommend_config 需要 ConfigStore,得到 {type(store).__name__}")
|
|
168
|
+
if not isinstance(signature, Mapping) or not signature:
|
|
169
|
+
raise AgentQueryError("signature 必须是非空映射")
|
|
170
|
+
target = _target(backend)
|
|
171
|
+
name = str(getattr(target, "name", "unknown"))
|
|
172
|
+
device = (_device(target) or {}).get("name") or "unknown"
|
|
173
|
+
normalized = canonical_signature(**dict(signature))
|
|
174
|
+
|
|
175
|
+
record = store.get(name, device, op, normalized)
|
|
176
|
+
if record is not None:
|
|
177
|
+
return {
|
|
178
|
+
"op": op,
|
|
179
|
+
"source": "store",
|
|
180
|
+
"config": dict(record.config),
|
|
181
|
+
"median_ms": record.median_ms,
|
|
182
|
+
"best_ms": record.best_ms,
|
|
183
|
+
"p95_ms": record.p95_ms,
|
|
184
|
+
"samples": record.samples,
|
|
185
|
+
"device": device,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
space_of = getattr(target, "tuning_space", None)
|
|
189
|
+
if space_of is None or op not in _tunable_ops(target):
|
|
190
|
+
raise AgentQueryError(f"{name} 的算子 {op} 没有可调参数,也没有配置库记录")
|
|
191
|
+
try:
|
|
192
|
+
space = space_of(op)
|
|
193
|
+
except Exception as exc: # noqa: BLE001
|
|
194
|
+
raise AgentQueryError(f"查询 {op} 的调优空间失败:{exc}") from exc
|
|
195
|
+
return {
|
|
196
|
+
"op": op,
|
|
197
|
+
"source": "default",
|
|
198
|
+
"config": space.default(),
|
|
199
|
+
"median_ms": None,
|
|
200
|
+
"best_ms": None,
|
|
201
|
+
"p95_ms": None,
|
|
202
|
+
"samples": 0,
|
|
203
|
+
"device": device,
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def snapshot(
|
|
208
|
+
backends: Iterable[object],
|
|
209
|
+
recorder: Optional[TelemetryRecorder] = None,
|
|
210
|
+
store: Optional[ConfigStore] = None,
|
|
211
|
+
) -> Dict[str, Any]:
|
|
212
|
+
"""一次取全:后端画像 + 运行摘要 + 配置库规模。"""
|
|
213
|
+
out: Dict[str, Any] = {"backends": describe_backends(backends)}
|
|
214
|
+
if recorder is not None:
|
|
215
|
+
out["performance"] = performance_summary(recorder)
|
|
216
|
+
if store is not None:
|
|
217
|
+
if not isinstance(store, ConfigStore):
|
|
218
|
+
raise AgentQueryError(f"store 必须是 ConfigStore,得到 {type(store).__name__}")
|
|
219
|
+
out["config_store"] = {
|
|
220
|
+
"records": len(store),
|
|
221
|
+
"entries": [
|
|
222
|
+
{
|
|
223
|
+
"op": record.op,
|
|
224
|
+
"device": record.device,
|
|
225
|
+
"config": dict(record.config),
|
|
226
|
+
"median_ms": record.median_ms,
|
|
227
|
+
"best_ms": record.best_ms,
|
|
228
|
+
"p95_ms": record.p95_ms,
|
|
229
|
+
"samples": record.samples,
|
|
230
|
+
}
|
|
231
|
+
for record in store.records()
|
|
232
|
+
],
|
|
233
|
+
}
|
|
234
|
+
return out
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""后端插件。"""
|
|
2
|
+
|
|
3
|
+
from .base import Backend, BackendCapabilityError, unsupported_ops
|
|
4
|
+
from .capabilities import (
|
|
5
|
+
BackendCapabilities,
|
|
6
|
+
OpCapability,
|
|
7
|
+
capability_gaps,
|
|
8
|
+
to_capabilities,
|
|
9
|
+
)
|
|
10
|
+
from .discovery import (
|
|
11
|
+
BackendUnavailableError,
|
|
12
|
+
get_backend,
|
|
13
|
+
register_backend,
|
|
14
|
+
registered_backends,
|
|
15
|
+
unregister_backend,
|
|
16
|
+
)
|
|
17
|
+
from .hardware import (
|
|
18
|
+
HardwarePackageError,
|
|
19
|
+
HardwareRecord,
|
|
20
|
+
install_hardware,
|
|
21
|
+
installed_hardware,
|
|
22
|
+
search_roots,
|
|
23
|
+
uninstall_hardware,
|
|
24
|
+
)
|
|
25
|
+
from .contracts import (
|
|
26
|
+
CONTRACTS,
|
|
27
|
+
CodegenContract,
|
|
28
|
+
DeviceContract,
|
|
29
|
+
DeviceInfo,
|
|
30
|
+
MemoryContract,
|
|
31
|
+
MemoryStats,
|
|
32
|
+
TuningContract,
|
|
33
|
+
implemented_contracts,
|
|
34
|
+
supports,
|
|
35
|
+
)
|
|
36
|
+
from .reference_backend import ReferenceBackend
|
|
37
|
+
from .registry import KernelRegistry
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"Backend",
|
|
41
|
+
"BackendCapabilityError",
|
|
42
|
+
"BackendCapabilities",
|
|
43
|
+
"OpCapability",
|
|
44
|
+
"capability_gaps",
|
|
45
|
+
"to_capabilities",
|
|
46
|
+
"BackendUnavailableError",
|
|
47
|
+
"get_backend",
|
|
48
|
+
"register_backend",
|
|
49
|
+
"registered_backends",
|
|
50
|
+
"unregister_backend",
|
|
51
|
+
"HardwarePackageError",
|
|
52
|
+
"HardwareRecord",
|
|
53
|
+
"install_hardware",
|
|
54
|
+
"uninstall_hardware",
|
|
55
|
+
"installed_hardware",
|
|
56
|
+
"search_roots",
|
|
57
|
+
"unsupported_ops",
|
|
58
|
+
"ReferenceBackend",
|
|
59
|
+
"KernelRegistry",
|
|
60
|
+
"CONTRACTS",
|
|
61
|
+
"CodegenContract",
|
|
62
|
+
"DeviceContract",
|
|
63
|
+
"DeviceInfo",
|
|
64
|
+
"MemoryContract",
|
|
65
|
+
"MemoryStats",
|
|
66
|
+
"TuningContract",
|
|
67
|
+
"implemented_contracts",
|
|
68
|
+
"supports",
|
|
69
|
+
]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""后端插件契约。
|
|
2
|
+
|
|
3
|
+
这里只定义最小可用的一套接口,刻意保持"薄":
|
|
4
|
+
|
|
5
|
+
- ``capabilities``:能力声明。后端必须如实列出自己承接的算子名。
|
|
6
|
+
能力协商的意义在于,小硬件插件不必硬着头皮实现全部算子——
|
|
7
|
+
前端可以根据这份声明决定拆分、降级或拒绝编译。
|
|
8
|
+
返回普通集合表示"只承诺算子名";返回 ``BackendCapabilities`` 可以附加
|
|
9
|
+
dtype / axis / layout / 反向 / 捕获 / 确定性等形态明细(见 capabilities.py)。
|
|
10
|
+
- ``execute``:执行一个图。
|
|
11
|
+
|
|
12
|
+
后续会在此基础上拆成三套独立契约(编译期 CodeGen、运行期 Runtime、
|
|
13
|
+
离线调优 Autotune),因为它们生命周期不同,混在一起会让插件接口过重。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from typing import Dict, FrozenSet, Iterable, Mapping, Protocol, runtime_checkable
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from ..ir.graph import Graph
|
|
23
|
+
from .capabilities import capability_gaps
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class BackendCapabilityError(RuntimeError):
|
|
27
|
+
"""目标后端无法承接给定图。"""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
#: 后端**未显式声明**布局时按这个最小承诺处理:只认 NCHW。
|
|
31
|
+
#:
|
|
32
|
+
#: 这一点刻意是"默认拒绝"而不是"默认放开":布局传播 pass
|
|
33
|
+
#: (``passes/layout.py``,DESIGN 5.246)会把卷积 / 池化翻成 NHWC 形态,
|
|
34
|
+
#: 若后端没说自己承接得了,翻出来的图就只能作废。参考后端与自研设备后端
|
|
35
|
+
#: 都已显式声明 ``{"nchw", "nhwc"}``(设备端自 5.334 起有原生 NHWC 内核);
|
|
36
|
+
#: 这里保留最小承诺作为第三方后端的兜底。
|
|
37
|
+
DEFAULT_SUPPORTED_LAYOUTS: FrozenSet[str] = frozenset({"nchw"})
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def supported_layouts_of(backend: object) -> FrozenSet[str]:
|
|
41
|
+
"""读取后端的布局声明;没声明就按最小承诺(只有 NCHW)。
|
|
42
|
+
|
|
43
|
+
用 ``getattr`` 而不是要求协议里必须写:布局声明是增量能力,第三方
|
|
44
|
+
后端不改代码也应当能继续工作——此时它的行为等于"只接 NCHW"。
|
|
45
|
+
"""
|
|
46
|
+
return frozenset(getattr(backend, "supported_layouts", DEFAULT_SUPPORTED_LAYOUTS))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@runtime_checkable
|
|
50
|
+
class Backend(Protocol):
|
|
51
|
+
"""后端插件必须实现的最小接口。"""
|
|
52
|
+
|
|
53
|
+
name: str
|
|
54
|
+
|
|
55
|
+
def capabilities(self) -> FrozenSet[str]:
|
|
56
|
+
"""返回该后端能原生执行的算子名集合。
|
|
57
|
+
|
|
58
|
+
可以是 ``frozenset``,也可以是 ``BackendCapabilities``——后者仍是
|
|
59
|
+
``frozenset`` 子类,同时能说明"支持哪些形态"。
|
|
60
|
+
"""
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
def execute(self, graph: Graph, inputs: Mapping[str, np.ndarray]) -> Dict[str, np.ndarray]:
|
|
64
|
+
"""执行图并返回输出。不支持的算子应抛 BackendCapabilityError。"""
|
|
65
|
+
...
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def unsupported_ops(graph: Graph, capabilities: Iterable[str]) -> list:
|
|
69
|
+
"""返回图中目标后端不支持的算子(按出现顺序去重)。
|
|
70
|
+
|
|
71
|
+
能力声明带形态明细时,dtype / axis / layout 不匹配也算不支持——
|
|
72
|
+
"声明了算子名"不等于"任何形态都接得住"。只有名字的声明行为不变。
|
|
73
|
+
"""
|
|
74
|
+
return [name for name, _ in capability_gaps(graph, capabilities)]
|