帮助你将 PyTorch 模型迁移为带张量形状类型标注与校验的版本。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "port-model" 技能: 1. 下载 https://raw.githubusercontent.com/facebook/pyrefly/main/.claude/skills/port-model/SKILL.md 2. 保存为 ~/.claude/skills/port-model/SKILL.md 3. 装好后重载技能,告诉我可以用了
请把这个 PyTorch CNN 模型迁移为使用 pyrefly 张量形状类型系统的版本,为 forward 方法和关键中间张量添加类似 Tensor[B, C, H, W]、Dim[T] 的标注,并说明你修改了哪些地方。
返回带 pyrefly 形状类型标注的模型代码,并简要说明新增或调整的维度定义与注解位置。
我有一个 Transformer 编码器模块,请先按 pyrefly 的规范为它设计 batch、sequence、hidden 等维度类型,再把 forward 输入输出和内部主要张量完整标注出来,避免常见形状错误。
输出完成维度设计后的 Transformer 代码,包含输入输出及核心张量的形状类型标注。
请检查这个现有的 model.py,识别哪些层和张量适合加入 pyrefly 形状检查,然后把文件迁移成可进行张量维度追踪的版本,尽量保持原有逻辑不变。
给出迁移后的代码版本,并列出适合加入形状检查的模块与保持不变的实现逻辑。
You are porting a PyTorch model to use pyrefly's tensor shape type system.
This skill requires detailed step-by-step output. Each step produces a mandatory output artifact (a table, a checklist, a count). These artifacts are the deliverables — do not compress, summarize, or skip them. The next step's input depends on the previous step's output. If you don't have the artifact, you cannot proceed.
Create tasks for each gate and the module loop/verification phases that follow. Update them as you complete each stage — this gives visibility during long-running ports.
Complete these gates before writing any code.
Read shape_tracking_capabilities.md (this skill dir). It explains the
three shape-tracking mechanisms (shape-aware stubs, DSL functions, special
handlers) and how to check each one. You need this context to make the
Gate 1 audit meaningful — knowing whether an op exists in a stub is not
the same as knowing whether its shapes are tracked.
Do NOT read style_guide.md yet. It is comparison material for the
verification phase. Reading it now biases you toward known patterns
before you have empirically probed this model's shapes.
List every nn.Module subclass and torch/F. function called in the
model. Check each against the shape-aware torch stubs (.pyi files in
tensor-shapes/torch-stubs/) and any shape DSL declarations in those stubs.
The stubs attach DSL shape functions with @uses_shape_dsl(ir_fn), and the
IR functions live in tensor-shapes/torch-stubs/_shapes.pyi and are imported
from stub files as torch._shapes because torch-stubs provides the torch
package for type checking. Add any missing stubs or DSL functions BEFORE
starting the port. This step should take minutes — you are scanning the stub
file for the op and, only when a decorator points there, the corresponding IR
function.
Do NOT delegate this audit to code search agents or use web search for this
step. The tensor-shapes/torch-stubs/ package and its _shapes.pyi DSL
file are exhaustive for torch shape support. For each op in your list, check
whether it appears in the relevant stub file and whether it has a precise
generic signature, Self/Tensor[*S] return, or @uses_shape_dsl(...)
decorator that refines a bare declared return. Use targeted file reads or
repo-approved search scoped to known files/directories, not broad recursive
shell search. You need to confirm presence and spot missing attributes
(e.g., bias on Conv2d), not memorize every signature.
Paste your audit table in your response before proceeding to Gate 2:
## Gate 1: Ops audit
| Op | Stub location | Shape DSL decorator / IR fn (or "no decorator") | Status |
|----|---------------|------------------------------|--------|
| nn.Conv2d | tensor-shapes/torch-stubs/nn/__init__.pyi — generic [InC,OutC,K,S,P,D] | no decorator (stub generic) | ✓ tracked |
| F.adaptive_avg_pool2d | tensor-shapes/torch-stubs/nn/functional.pyi — bare declared return | `@uses_shape_dsl(adaptive_pool_ir)`, defined in `_shapes.pyi` | ✓ tracked (DSL) |
| ...
Filling the "Shape DSL decorator / IR fn" column requires checking whether
the stub declaration has a @uses_shape_dsl(...) decorator. If it does,
confirm the named IR function exists in tensor-shapes/torch-stubs/_shapes.pyi.
Write "no decorator" only after confirming the stub declaration has no
decorator — do not leave this column blank or write "check DSL".
If any op is missing or returns bare, fix stubs/DSL BEFORE proceeding.
Write the inventory as a comment block at the top of the port file, using this exact format:
# ## Inventory
# - [ ] ClassName.__init__ — Dims: param1, param2; int: param3
# - [ ] ClassName.forward
# - [ ] function_name — utility, no tensors
# ...
**Every class, function, and method in the original file must appear, and
…
帮助排查常见故障、版本兼容与会话流诊断问题,提升开发调试效率
帮助开发者完成 Meta AI 应用注册与设备权限接入流程配置。
指导你构建含会话创建、相机串流与拍照功能的完整 DAT 应用
帮助开发者管理会话与流状态,并处理暂停恢复及设备可用性监控
帮助开发者掌握 DAT SDK Android 的 Kotlin 约定、结果处理与会话能力规范。
帮助开发可视化界面,完成显示设备选择、控件配置与多媒体播放集成。
将任意机器学习模型快速封装为带校验与错误处理的 MCP 工具
提供 PyTorch 深度学习模式与最佳实践,帮助构建高效可复现的训练流程。
帮助开发者读取、编写并调试 Pysa 的 JSON 污点模型与 .models 输出
帮助用户快速掌握并实现常见 PyTorch 开发模式与训练代码范式。
让大模型安全执行 pandas 数据分析并生成交互式可视化结果。
用于占位演示技能配置,帮助快速搭建示例说明与提示词模板。