-
Notifications
You must be signed in to change notification settings - Fork 45
[Feature Enhancement] Add resumable processing support to OpNamesExtr… #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Any, Dict, List | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from graph_net.sample_pass.resumable_sample_pass_mixin import ResumableSamplePassMixin | ||
| from graph_net.torch.fx_graph_module_util import get_torch_module_and_inputs | ||
| from graph_net.torch.fx_graph_parse_util import parse_sole_graph_module_without_varify | ||
| from graph_net.sample_pass.sample_pass import SamplePass | ||
|
|
||
|
|
||
| class OpNamesExtractor(SamplePass, ResumableSamplePassMixin): | ||
| def __init__(self, config=None): | ||
| super().__init__(config) | ||
| if config is None: | ||
| config = {} | ||
|
|
||
| self.config = self._make_config(**config) | ||
|
|
||
| def declare_config( | ||
| self, | ||
| model_path_prefix: str, | ||
| output_dir: str, | ||
| resume: bool = False, | ||
| limits_handled_models: int = None, | ||
| ): | ||
| pass | ||
|
|
||
| def sample_handled(self, rel_model_path: str) -> bool: | ||
| return self.naive_sample_handled( | ||
| rel_model_path, search_file_name="op_names.txt" | ||
| ) | ||
|
|
||
| def resume(self, rel_model_path: str): | ||
| torch.cuda.empty_cache() | ||
| model_path = os.path.join(self.config["model_path_prefix"], rel_model_path) | ||
| op_names = self._extract_ops(model_path) | ||
| output_path = self._get_output_path(rel_model_path) | ||
| output_path.write_text("\n".join(op_names)) | ||
| print(f"Save op-names to {str(output_path)}") | ||
|
|
||
| def __call__(self, rel_model_path: str): | ||
| self.resumable_handle_sample(rel_model_path) | ||
|
|
||
| def _make_config( | ||
| self, model_path_prefix: str, output_dir: str, resume: bool = False | ||
| ): | ||
| return { | ||
| "model_path_prefix": model_path_prefix, | ||
| "resume": resume, | ||
| "output_dir": output_dir, | ||
| } | ||
|
Comment on lines
+46
to
+53
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 去掉。没有地方用到了
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已删 |
||
|
|
||
| def _get_output_path(self, rel_model_path: str): | ||
| output_path_dir = Path(self.config["output_dir"]) / rel_model_path | ||
| output_path_dir.mkdir(parents=True, exist_ok=True) | ||
| output_path = output_path_dir / "op_names.txt" | ||
| return output_path | ||
|
|
||
| def _extract_ops(self, model_path: str) -> List[str]: | ||
| extractor = TypicalSequenceExtractor() | ||
| model, inputs = get_torch_module_and_inputs(model_path) | ||
| compiled_model, _ = parse_sole_graph_module_without_varify(model, inputs) | ||
| extractor.extract_compiler(compiled_model, inputs) | ||
| ops_info = extractor.extract_node | ||
|
|
||
| return [op["target_name"] for op in ops_info] | ||
|
|
||
|
|
||
| class TypicalSequenceExtractor: | ||
| def __init__(self): | ||
| self.extract_node = [] | ||
|
|
||
| def _extract_operators_from_graph( | ||
| self, gm: nn.Module, example_inputs: List[torch.Tensor] = None | ||
| ) -> List[Dict[str, Any]]: | ||
| operator_list = [] | ||
| named_modules = dict(gm.named_modules()) | ||
|
|
||
| for node in gm.graph.nodes: | ||
| if node.op not in ("call_method", "call_function", "call_module"): | ||
| continue | ||
|
|
||
| if node.op == "call_module": | ||
| target_name = type(named_modules[node.target]).__name__ | ||
| elif node.op == "call_method": | ||
| target_name = f"Tensor.{node.target}" | ||
| elif node.op == "call_function": | ||
| target_name = getattr(node.target, "__name__", str(node.target)) | ||
| else: | ||
| raise NotImplementedError() | ||
| operator_list.append( | ||
| { | ||
| "op_type": node.op, | ||
| "target": node.target, | ||
| "name": node.name, | ||
| "target_name": target_name, | ||
| } | ||
| ) | ||
|
|
||
| return operator_list | ||
|
|
||
| def extract_compiler(self, gm: torch.fx.GraphModule, inputs: List[torch.Tensor]): | ||
| operator = self._extract_operators_from_graph(gm, inputs) | ||
| self.extract_node = operator | ||
| return gm.forward | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉。基类已经处理了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的