Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ea77602
support checking model redundancy
lixinqi Jul 31, 2025
c3b3ea9
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Jul 31, 2025
c21cb49
revert change of vision_model_test
lixinqi Jul 31, 2025
ca9017f
reformat python code.
lixinqi Jul 31, 2025
52cc34d
reformat bert_model_test.py and utils.py
lixinqi Jul 31, 2025
d8c6213
minor fix
lixinqi Jul 31, 2025
6bd1370
fix failed check by comparing directories after os.path.realpath()
lixinqi Aug 4, 2025
5db0b63
merge paddle repo develop
lixinqi Aug 4, 2025
165ae4b
fix bugs in check_validate.sh
lixinqi Aug 4, 2025
6059328
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 4, 2025
d2d9e0b
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 5, 2025
2cfa175
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 5, 2025
3a75ddd
set dynamic=False in single_device_runner.py
lixinqi Aug 5, 2025
b394760
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 6, 2025
868b686
reset graph hash
lixinqi Aug 6, 2025
4c473cf
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 6, 2025
5530841
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 6, 2025
0caf96a
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 6, 2025
718cd39
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 6, 2025
d46c810
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 7, 2025
985d3cc
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 7, 2025
782858e
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 11, 2025
a7acb51
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 14, 2025
b49edb4
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 15, 2025
6be9482
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Aug 30, 2025
7b950b9
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
lixinqi Sep 6, 2025
979b4d1
add docstring for extractor
lixinqi Sep 6, 2025
51abcd4
fix a typo
lixinqi Sep 6, 2025
5306734
fix typo
lixinqi Sep 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ model = graph_net.torch.extract(name="model_name")(model)
# $GRAPH_NET_EXTRACT_WORKSPACE/model_name
```

For details, see docstring of `graph_net.torch.extract` defined in `graph_net/torch/extractor.py`

**graph_net.torch.validate**
```
# Verify that the extracted model meets requirements
Expand Down
80 changes: 78 additions & 2 deletions graph_net/torch/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,71 @@

def extract(name, dynamic=True, mut_graph_codes=None, placeholder_auto_rename=False):
"""
A decorator for PyTorch functions to capture the computation graph.
Extract computation graphs from PyTorch nn.Module.
The extracted computation graphs will be saved into directory of env var $GRAPH_NET_EXTRACT_WORKSPACE.

Args:
name (str): The name of the model, used as the directory name for saving.
dynamic (bool): Enable dynamic shape support in torch.compile.

Returns:
wrapper or decorector

Examples:
>>> # wrapper style:
>>> from graph_net.torch.extractor import extract
>>> import torch
>>> import os
>>> class Foo(torch.nn.Module):
... def forward(self, x):
... return x * 2 + 1
...
>>> os.environ['GRAPH_NET_EXTRACT_WORKSPACE'] = '/tmp'
>>> foo = extract("foo")(Foo())
>>> foo(torch.tensor([1, 2, 3]))
Graph and tensors for 'foo' extracted successfully to: /tmp/foo
tensor([3, 5, 7])
>>> print(open('/tmp/foo/model.py').read())
import torch

class GraphModule(torch.nn.Module):



def forward(self, s0 : torch.SymInt, L_x_ : torch.Tensor):
l_x_ = L_x_
mul = l_x_ * 2; l_x_ = None
add = mul + 1; mul = None
return (add,)

>>> # decorator style:
>>> from graph_net.torch.extractor import extract
>>> import torch
>>> import os
>>> os.environ['GRAPH_NET_EXTRACT_WORKSPACE'] = '/tmp'
>>> @extract('bar')
... class Bar(torch.nn.Module):
... def forward(self, x):
... return x * 2 + 1
...
>>> bar = Bar()
>>> bar(torch.tensor([1, 2, 3]))
Graph and tensors for 'bar' extracted successfully to: /tmp/bar
tensor([3, 5, 7])
>>> print(open("/tmp/bar/model.py").read())
import torch

class GraphModule(torch.nn.Module):



def forward(self, s0 : torch.SymInt, L_x_ : torch.Tensor):
l_x_ = L_x_
mul = l_x_ * 2; l_x_ = None
add = mul + 1; mul = None
return (add,)

>>>
"""

def wrapper(model: torch.nn.Module):
Expand Down Expand Up @@ -95,4 +155,20 @@ def try_rename_placeholder(node):

return compiled_model

return wrapper
def decorator(module_class):
def constructor(*args, **kwargs):
return wrapper(module_class(*args, **kwargs))

return constructor

def decorator_or_wrapper(obj):
if isinstance(obj, torch.nn.Module):
return wrapper(obj)
elif issubclass(obj, torch.nn.Module):
return decorator(obj)
else:
raise NotImplementedError(
"Only torch.nn.Module instance or subclass supported."
)

return decorator_or_wrapper
12 changes: 7 additions & 5 deletions graph_net/torch/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,31 @@ def main(args):


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="load and run model")
parser = argparse.ArgumentParser(
description="Validate a computation graph sample. return 0 if success"
)
parser.add_argument(
"--model-path",
type=str,
required=True,
help="Path to folder e.g '../../samples/torch/resnet18'",
help="Computation graph sample directory. e.g '../../samples/torch/resnet18'",
)
parser.add_argument(
"--graph-net-samples-path",
type=str,
required=False,
default=None,
help="Path to GraphNet samples folder. e.g '../../samples'",
help="GraphNet samples directory. used for redundancy check. e.g '../../samples'",
)
parser.add_argument(
"--no-check-redundancy",
action="store_true",
help="whether check model graph redundancy",
help="Diable redundancy check (default: False).",
)
parser.add_argument(
"--workspace",
default=os.environ.get("GRAPH_NET_EXTRACT_WORKSPACE", "./workspace"),
help="whether check model graph redundancy",
help="temporary directory for validation (default: env var GRAPH_NET_EXTRACT_WORKSPACE). ",
)
args = parser.parse_args()
os.environ["GRAPH_NET_EXTRACT_WORKSPACE"] = args.workspace
Expand Down