-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unused tensor literals from memory when saving in dynamo (#218)
* update loading saved model in torch backend to work with accelerate * remove unused tensor literals from memory
- Loading branch information
1 parent
33d4d63
commit af14cd9
Showing
3 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
py/torch_migraphx/dynamo/passes/remove_lowered_constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import itertools | ||
import torch | ||
|
||
def remove_lowered_constants(gm: torch.fx.GraphModule): | ||
used_literals = set() | ||
for node in gm.graph.nodes: | ||
if node.op == "get_attr": | ||
used_literals.add(node.name) | ||
|
||
unused_literals = set() | ||
for name, _ in itertools.chain(gm.named_parameters(), gm.named_buffers()): | ||
if not name in used_literals: | ||
if hasattr(gm, name): | ||
unused_literals.add(name) | ||
|
||
for name in unused_literals: | ||
delattr(gm, name) | ||
|
||
return gm |