-
Notifications
You must be signed in to change notification settings - Fork 45
Add MergeSubgraphSources sample pass to merge subgraph_sources.json when duplicate #504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from graph_net.sample_pass.sample_pass import SamplePass | ||
| import graph_net.subgraph_range_util as subgraph_range_util | ||
| from pathlib import Path | ||
| import json | ||
| from typing import Union | ||
|
|
||
|
|
||
| class MergeSubgraphSources(SamplePass): | ||
| def __init__(self, config=None): | ||
| super().__init__(config) | ||
|
|
||
| def declare_config( | ||
| self, | ||
| model_path_prefix: str = "", | ||
| source_model_path_prefixes: list = None, | ||
| subgraph_sources_json_file_name: str = "subgraph_sources.json", | ||
| ): | ||
| pass | ||
|
|
||
| def __call__(self, rel_model_path: str): | ||
| model_path_prefix = self.config.get("model_path_prefix", "") | ||
| target_model_path = ( | ||
| Path(model_path_prefix) / rel_model_path | ||
| if model_path_prefix | ||
| else Path(rel_model_path) | ||
| ) | ||
|
|
||
| source_model_path_prefixes = self.config.get("source_model_path_prefixes") or [] | ||
| source_model_paths = [ | ||
| Path(prefix) / rel_model_path for prefix in source_model_path_prefixes | ||
| ] | ||
|
|
||
| self.merge_sources_for_deduplication(target_model_path, source_model_paths) | ||
| print(f"Merged {len(source_model_paths)} sources into {target_model_path}") | ||
|
|
||
| def merge_sources_for_deduplication( | ||
| self, | ||
| target_model_path: Union[str, Path], | ||
| source_model_paths: list[Union[str, Path]], | ||
| ) -> dict[str, list[tuple[int, int]]]: | ||
| merged_sources = self._load_sources(target_model_path) | ||
| for source_path in source_model_paths: | ||
| source_sources = self._load_sources(source_path) | ||
| if source_sources: | ||
| merged_sources = subgraph_range_util.merge_subgraph_ranges( | ||
| merged_sources, source_sources | ||
| ) | ||
| self._save_sources(target_model_path, merged_sources) | ||
| return merged_sources | ||
|
|
||
| def _get_sources_file_path(self, model_path: Union[str, Path]) -> Path: | ||
| return Path(model_path) / self.config["subgraph_sources_json_file_name"] | ||
|
|
||
| def _load_sources( | ||
| self, model_path: Union[str, Path] | ||
| ) -> dict[str, list[tuple[int, int]]]: | ||
| file_path = self._get_sources_file_path(model_path) | ||
| if not file_path.exists(): | ||
| return {} | ||
| with open(file_path, "r", encoding="utf-8") as f: | ||
| return json.load(f) | ||
|
|
||
| def _save_sources( | ||
| self, | ||
| model_path: Union[str, Path], | ||
| sources: dict[str, list[tuple[int, int]]], | ||
| ) -> None: | ||
| file_path = self._get_sources_file_path(model_path) | ||
| with open(file_path, "w", encoding="utf-8") as f: | ||
| json.dump(sources, f, indent=4) | ||
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
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.
sample_pass目录下一般都能被apply_sample_pass直接调用。但咱们这里不完整。可以把代码放到tools目录下。