用途:该技能用于向 Pyrefly 的示例语料库 tensor-shapes/pyrefly-torch-stubs/examples/ 导入新的 PyTorch 模型。这些移植是供他人学习形状类型写法的测试参考材料,因此需产出完整交付物,而不仅是加注释的模型。
主要能力与要求:记录上游仓库与版本、具体文件或依赖闭包、配置、入口以及纳入的 train/eval/cache/export 模式;声明边界内的“完整”,省略的包装器或模式需列出。编辑前先浏览两三个架构最接近的现有示例,并挖掘上游源码中的形状注释、docstring、reshape/einsum 公式、运行时断言与测试,作为待验证假设而非抄写文本。需区分真正的 stub 缺口与不可用 overlay 符号、Any、声明的渐近返回、第三方代码或不可表示的动态构造;可保留有文档说明的窄精度 cast、类型接口或渐近边界,但不应掩盖易于修复的 stub 缺口。
使用方式:先按 tensor-shapes/skills/add-shape-types-to-torch-model/ 的 SKILL.md 端到端完成移植(pre-flight gates → per-module loop → verification)。语料工作下两个设置选项已确定,无需询问:使用 Buck 检查,并将 stub 改进视为范围内。文件写入 tensor-shapes/pyrefly-torch-stubs/examples/<model>.py。验证需在 fbcode/pyrefly 检出根目录运行,先确保共享虚拟环境存在(必要时加 --fwdproxy):
python3 tensor-shapes/bootstrap_venv.py
buck build fbcode//pyrefly/tensor-shapes:torch-stubs-search-path
SEARCH_ROOT="$(buck targets --show-output fbcode//pyrefly/tensor-shapes:torch-stubs-search-path | awk '{print $2}')"
VENV="${TENSOR_SHAPES_VENV:-$HOME/.tensor-shapes-venv}"
SITE="$("$VENV/bin/python" -c 'import site; print(site.getsitepackages()[0])')"
buck run fbcode//pyrefly:pyrefly -- check --config /dev/null
--python-version 3.13 --search-path "$SEARCH_ROOT"
--site-package-path "$SITE"
tensor-shapes/pyrefly-torch-stubs/examples/<model>.py
若模型导入 einops,还需加 --search-path tensor-shapes/pyrefly-einops-stubs,否则 einops 调用可能看似保留输入形状。结果需为 0 错误且无遗留 reveal_type。随后运行:
python3 tensor-shapes/pyrefly-torch-stubs/run_pyrefly.py --buck --suite torch-examples
限制:形状错误或 stub 签名无法表达的缺失形状属于 shape-DSL 变更,应参考 modify-shaped-array-dsl 技能,并对 DSL 逻辑做单元测试;不要为 stub 签名可表达的 shapes 使用 DSL。运行时重写只能提议,除非用户另行要求,否则不执行。