-
Notifications
You must be signed in to change notification settings - Fork 42
Validate the correctness of graph variable rename #439
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
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
90 changes: 90 additions & 0 deletions
90
graph_net/torch/backend/graph_variable_renamer_validator_backend.py
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,90 @@ | ||
| import torch | ||
| from pathlib import Path | ||
| from typing import Dict | ||
| from graph_net.tensor_meta import TensorMeta | ||
| import os | ||
| import importlib.util | ||
|
|
||
|
|
||
| class RenamedModelAdapter(torch.nn.Module): | ||
| def __init__(self, renamed_model: torch.nn.Module, mapping: Dict[str, str]): | ||
| super().__init__() | ||
| self.model = renamed_model | ||
| self.mapping = mapping | ||
| if hasattr(renamed_model, "__graph_net_file_path__"): | ||
| self.__graph_net_file_path__ = renamed_model.__graph_net_file_path__ | ||
|
|
||
| def forward(self, **kwargs): | ||
| new_kwargs = self._convert_by_name_mapping(kwargs) | ||
| return self.model(**new_kwargs) | ||
|
|
||
| def _convert_by_name_mapping(self, kwargs): | ||
| new_kwargs = {} | ||
| for old_name, value in kwargs.items(): | ||
| if old_name in self.mapping: | ||
| new_name = self.mapping[old_name] | ||
| new_kwargs[new_name] = value | ||
| return new_kwargs | ||
|
|
||
|
|
||
| class GraphVariableRenamerValidatorBackend: | ||
| def _get_rename_mapping(self, model_dir: Path): | ||
| mapping = {} | ||
| for meta_file in ["input_meta.py", "weight_meta.py"]: | ||
| meta_path = model_dir / meta_file | ||
| if not meta_path.exists(): | ||
| continue | ||
| metas = TensorMeta.unserialize_from_py_file(str(meta_path)) | ||
| for m in metas: | ||
| if m.original_name: | ||
| mapping[m.original_name] = m.name | ||
| return mapping | ||
|
|
||
| def _load_model_instance(self, path: str, device: str) -> torch.nn.Module: | ||
| class_name = "GraphModule" | ||
| model_file = os.path.join(path, "model.py") | ||
|
|
||
| spec = importlib.util.spec_from_file_location(class_name, model_file) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
|
|
||
| ModelClass = getattr(module, class_name) | ||
| instance = ModelClass().to(device) | ||
| return instance | ||
|
|
||
| def _make_config( | ||
| self, | ||
| model_path_prefix: str, | ||
| renamed_root: str, | ||
| renamed_dentry: str = "_renamed", | ||
| ): | ||
| return { | ||
| "model_path_prefix": model_path_prefix, | ||
| "renamed_root": renamed_root, | ||
| "renamed_dentry": renamed_dentry, | ||
| } | ||
|
|
||
| def __call__(self, model: torch.nn.Module) -> torch.nn.Module: | ||
| config = self._make_config(**self.config) | ||
| model_path = os.path.dirname(model.__class__.__graph_net_file_path__) | ||
| model_name = os.path.basename(model_path) | ||
| renamed_dir_name = f"{model_name}_renamed" | ||
| renamed_model_dir = os.path.join(config["renamed_root"], renamed_dir_name) | ||
|
|
||
| print(f"[GraphVariableRenamerValidatorBackend] Processing: {model_name}") | ||
| print( | ||
| f"[GraphVariableRenamerValidatorBackend] Loading from: {renamed_model_dir}" | ||
| ) | ||
|
|
||
| device = model.__class__.__graph_net_device__ | ||
| renamed_model = self._load_model_instance(renamed_model_dir, device) | ||
| mapping = self._get_rename_mapping(Path(renamed_model_dir)) | ||
| assert ( | ||
| mapping | ||
| ), f"Mapping is empty for {renamed_dir_name} at {renamed_model_dir}" | ||
| adapter = RenamedModelAdapter(renamed_model, mapping) | ||
| return adapter.eval() | ||
|
|
||
| def synchronize(self): | ||
| if torch.cuda.is_available(): | ||
| torch.cuda.synchronize() | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.