Commit fed67b9
Refactor: Introduce OpBuilderBase/TapeBuilder as unified op-building interface (#2905)
## Summary
Introduces `OpBuilderBase` (ABC) and `TapeBuilder` (concrete
implementation) as the unified interface for building IR nodes across
the rewriter, optimizer, and version converter. This is a step towards
unifying these builders with the recently introduced GraphBuilder. As of
now, there is still some differences internally (as these are oriented
towards incrementally modifying an existing graph), but for end-users
(eg., writing rewrite-rules) we should be able to move towards same API.
## Changes
### New: `onnxscript/tape_builder.py`
- **`OpBuilderBase`** — Abstract base class providing the op-building
API:
- Dynamic dispatch: `op.Relu(x)`, `op.MatMul(a, b, _domain=...)`
- Explicit creation: `op.op("Conv", inputs, attributes, domain=...)`
- Initializer creation: `op.initializer(tensor, name=...)`
- Subclasses implement `_add_node`, `_add_initializer`, `_record_opset`
- **`TapeBuilder`** — Concrete subclass with list-based storage and
harvesting properties (`nodes`, `initializers`, `used_opsets`)
- Both are exported as public names from `onnxscript`
### Rewriter
- `rewriter/_context.py` slimmed to re-exports + local alias
`RewriterContext = OpBuilderBase`
- `_rewrite_rule.py` uses `TapeBuilder()` directly; harvests from the
context
- Deleted `_node_sink.py` (the intermediate NodeSink/TapeSink layer)
### Optimizer
- `optimizer/_constant_folding.py` defines `OptimizerContext =
OpBuilderBase` locally
- Uses `TapeBuilder()` instead of the old `_tape.Builder()`
### Version Converter
- `version_converter/_version_converter.py` defines `VCContext =
OpBuilderBase` locally
- Uses `TapeBuilder()` instead of the old `_tape.Builder()`
### Cleanup
- Deleted `onnxscript/ir/_tape.py` and `_tape_test.py` (fully
superseded)
- Removed incorrect `ir.Value` type annotations from `pattern()` method
signatures in rule files (pattern methods work with pattern-value
objects, not `ir.Value`)
## Design
```
OpBuilderBase (ABC) <- shared interface
_add_node() <- abstract
_add_initializer() <- abstract
_record_opset() <- abstract
__getattr__() <- dynamic dispatch (concrete)
op() <- explicit node creation (concrete)
initializer() <- initializer creation (concrete)
TapeBuilder(OpBuilderBase) <- list-backed implementation
nodes <- harvesting property
initializers <- harvesting property
used_opsets <- harvesting property
Aliases:
RewriterContext = OpBuilderBase (in rewriter)
OptimizerContext = OpBuilderBase (in optimizer)
VCContext = OpBuilderBase (in version converter)
```
This design allows future alternative implementations (e.g.,
graph-backed builder) by subclassing `OpBuilderBase` without changing
rule/evaluator code.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 38bec07 commit fed67b9
16 files changed
Lines changed: 428 additions & 232 deletions
File tree
- onnxscript
- _internal
- ir
- optimizer
- rewriter
- rules/common
- version_converter
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
| 136 | + | |
134 | 137 | | |
135 | 138 | | |
136 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
This file was deleted.
0 commit comments