Commit 498e11f
committed
Fix ModelPruning iterative reuse RuntimeError on non-leaf tensors
When ModelPruning with use_lottery_ticket_hypothesis=True is reused
across multiple trainer.fit() calls, setup() is called again each run.
After the first pass, _copy_param sets dst.data = src.data, making the
parameters non-leaf tensors. The subsequent deepcopy(module) then raises:
RuntimeError: Only Tensors created explicitly by the user (graph
leaves) support the deepcopy protocol at the moment
Fix by adding _deepcopy_for_pruning(), a helper that temporarily
replaces non-leaf parameters with detached clones before deepcopy and
restores the originals afterward. Also explicitly set _original_layers
to None before re-populating in setup() so the previous run's tensor
references are released before new copies are allocated.
Fixes #85421 parent 88352b7 commit 498e11f
2 files changed
Lines changed: 67 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
278 | 303 | | |
279 | 304 | | |
280 | 305 | | |
| |||
376 | 401 | | |
377 | 402 | | |
378 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
379 | 407 | | |
380 | 408 | | |
381 | 409 | | |
382 | 410 | | |
383 | 411 | | |
384 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
385 | 416 | | |
386 | 417 | | |
387 | 418 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
551 | 551 | | |
552 | 552 | | |
553 | 553 | | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
0 commit comments