Commit 8b79816
Add onnxscript.nn module with Module and Parameter classes (#2819)
This pull request introduces a new PyTorch-like module interface for
building ONNX graphs, enabling users to define reusable neural network
components and manage parameters in a structured way. The main changes
add the `onnxscript.nn` package, expose its API, and implement core
classes for module and parameter management.
Addition of ONNX neural network module interface:
* Added new `onnxscript.nn` package and exposed it in the main
`onnxscript` API, allowing users to access neural network module
functionality. (`onnxscript/__init__.py`,
[[1]](diffhunk://#diff-a1562cf8b37bd59e756ae3802f64fb4b0712845c4dd5a747d7db2a4212f1bfa5R8)
[[2]](diffhunk://#diff-a1562cf8b37bd59e756ae3802f64fb4b0712845c4dd5a747d7db2a4212f1bfa5L130-R131)
* Introduced `Module` and `Parameter` classes in
`onnxscript/nn/_module.py` and `onnxscript/nn/_parameter.py`, providing
a PyTorch-like interface for defining ONNX graph modules, registering
parameters, and managing module hierarchies.
(`onnxscript/nn/_module.py`,
[[1]](diffhunk://#diff-26fd1ef9987845a4613d3d4b2e8d0c6d276b4946ba1a6a3e5e77b6e156e224beR1-R206);
`onnxscript/nn/_parameter.py`,
[[2]](diffhunk://#diff-94ffcabca05fa631e1902fbd4a2e967c00f9db0dca95baae367daef60cd8a7e1R1-R66)
* Created `onnxscript/nn/__init__.py` to expose `Module` and `Parameter`
as the public API of the new package. (`onnxscript/nn/__init__.py`,
[onnxscript/nn/__init__.pyR1-R9](diffhunk://#diff-db47492cd4dbb1a882423614592cb391e31abdec6b5aff85d6f1d2e2716507b4R1-R9))
Core module and parameter functionality:
* `Module` class supports automatic registration of parameters and child
modules, implements methods for iterating over parameters/modules, and
provides `state_dict`/`load_state_dict` for parameter
serialization/deserialization, mirroring PyTorch's API.
(`onnxscript/nn/_module.py`,
[onnxscript/nn/_module.pyR1-R206](diffhunk://#diff-26fd1ef9987845a4613d3d4b2e8d0c6d276b4946ba1a6a3e5e77b6e156e224beR1-R206))
* `Parameter` class subclasses `ir.Value`, allowing direct use in ONNX
ops and supporting initialization, realization, and representation of
parameter tensors. (`onnxscript/nn/_parameter.py`,
[onnxscript/nn/_parameter.pyR1-R66](diffhunk://#diff-94ffcabca05fa631e1902fbd4a2e967c00f9db0dca95baae367daef60cd8a7e1R1-R66))
---------
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent f69cf91 commit 8b79816
File tree
8 files changed
+1419
-59
lines changed- onnxscript
- _internal
- nn
8 files changed
+1419
-59
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
130 | | - | |
| 131 | + | |
131 | 132 | | |
132 | 133 | | |
133 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
| 187 | + | |
| 188 | + | |
188 | 189 | | |
189 | 190 | | |
190 | | - | |
| 191 | + | |
| 192 | + | |
191 | 193 | | |
192 | | - | |
| 194 | + | |
193 | 195 | | |
194 | 196 | | |
195 | 197 | | |
196 | 198 | | |
197 | | - | |
| 199 | + | |
198 | 200 | | |
199 | 201 | | |
200 | | - | |
| 202 | + | |
201 | 203 | | |
202 | 204 | | |
203 | 205 | | |
| |||
304 | 306 | | |
305 | 307 | | |
306 | 308 | | |
307 | | - | |
| 309 | + | |
308 | 310 | | |
309 | 311 | | |
310 | 312 | | |
| |||
322 | 324 | | |
323 | 325 | | |
324 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
325 | 333 | | |
326 | 334 | | |
327 | 335 | | |
328 | 336 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
337 | 345 | | |
338 | 346 | | |
339 | | - | |
340 | | - | |
| 347 | + | |
| 348 | + | |
341 | 349 | | |
342 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
343 | 359 | | |
344 | | - | |
345 | | - | |
346 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
347 | 363 | | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
352 | 401 | | |
353 | 402 | | |
354 | 403 | | |
| |||
0 commit comments