-
Notifications
You must be signed in to change notification settings - Fork 33
【Hackathon 9th No.110】[Feature Enhancement] Add decomposer_validator #354
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
4 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
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,51 @@ | ||
| #!/bin/bash | ||
|
|
||
| GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(graph_net.__file__))") | ||
|
|
||
| if [ -z "$GRAPH_NET_DECOMPOSE_PATH" ]; then | ||
| GRAPH_NET_DECOMPOSE_PATH="$(pwd)/graphnet_decompose" | ||
| fi | ||
|
|
||
| MODEL_PATH_IN_SAMPLES=/timm/resnet18 | ||
| MODEL_NAME=$(basename "$MODEL_PATH_IN_SAMPLES") | ||
| OUTPUT_DIR="${GRAPH_NET_DECOMPOSE_PATH:-$(pwd)}" | ||
| cp -r "$GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES" "$OUTPUT_DIR/" | ||
|
|
||
| extractor_config_json_str=$(cat <<EOF | ||
| { | ||
| "custom_extractor_path": "$GRAPH_NET_ROOT/torch/naive_graph_decomposer.py", | ||
| "custom_extractor_config": { | ||
| "output_dir": "$OUTPUT_DIR/${MODEL_NAME}_decomposed", | ||
| "split_positions": [8, 16, 32], | ||
| "group_head_and_tail": true, | ||
| "chain_style": true | ||
| } | ||
| } | ||
| EOF | ||
| ) | ||
| EXTRACTOR_CONFIG=$(echo $extractor_config_json_str | base64 -w 0) | ||
| python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name $MODEL_NAME --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG | ||
|
|
||
| FILE_PATH=$GRAPH_NET_DECOMPOSE_PATH/decomposer | ||
| mkdir -p "$(dirname "$FILE_PATH/log.log")" | ||
| MODEL_PATH="$GRAPH_NET_DECOMPOSE_PATH/$MODEL_NAME" | ||
|
|
||
| python -m graph_net.torch.test_compiler \ | ||
| --model-path $MODEL_PATH \ | ||
| --compiler range_decomposer_validator \ | ||
| --device cuda > "$FILE_PATH/log.log" 2>&1 | ||
|
|
||
| python -m graph_net.log2json \ | ||
| --log-file "$FILE_PATH/log.log" \ | ||
| --output-dir "$FILE_PATH/JSON_results/" | ||
|
|
||
| python -m graph_net.plot_ESt \ | ||
| --benchmark-path "$FILE_PATH/JSON_results/" \ | ||
| --output-dir "$FILE_PATH" | ||
|
|
||
| echo "==================================================" | ||
| echo "Results saved in: $FILE_PATH/ES_result.png" | ||
| echo "" | ||
| echo "IMPORTANT: Please verify if the curve in ES_result.png is a straight line" | ||
| echo "If the curve is NOT a straight line, please check the log file: $FILE_PATH/log.log" | ||
| echo "==================================================" |
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
76 changes: 76 additions & 0 deletions
76
graph_net/torch/backend/range_decomposer_validator_backend.py
fangfangssj marked this conversation as resolved.
Show resolved
Hide resolved
|
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,76 @@ | ||
| import torch | ||
| import torch.nn as nn | ||
| import os | ||
| import sys | ||
| import inspect | ||
| import importlib.util | ||
| import itertools | ||
| from typing import List, Tuple, Dict, Any, Callable | ||
|
|
||
|
|
||
| class ComposedModel(nn.Module): | ||
| def __init__(self, subgraph: List[nn.Module]): | ||
| super().__init__() | ||
| self.subgraphs = nn.ModuleList(subgraph) | ||
|
|
||
| def forward(self, **kwargs): | ||
| subgraph_intput = { | ||
| key.replace("L", "l_l", 1): value | ||
| for key, value in kwargs.items() | ||
| if key.startswith("L") | ||
| } | ||
|
|
||
| output = None | ||
| for subgraph in self.subgraphs: | ||
| if output is None: | ||
| output = subgraph(**subgraph_intput) | ||
| else: | ||
| output = subgraph(*output) | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| class RangeDecomposerValidatorBackend: | ||
| 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 __call__(self, model: torch.nn.Module) -> torch.nn.Module: | ||
| model_file_path = model.__class__.__graph_net_file_path__ | ||
| model_dir = os.path.dirname(model_file_path) | ||
| decomposed_parent_dir = model_dir + "_decomposed" | ||
| subgraph_paths = [] | ||
| for name in sorted(os.listdir(decomposed_parent_dir)): | ||
| full_path = os.path.join(decomposed_parent_dir, name) | ||
| if os.path.isdir(full_path) and name[-1].isdigit(): | ||
| subgraph_paths.append(full_path) | ||
|
|
||
| print( | ||
| f"[RangeDecomposerValidatorBackend] Found subgraphs: {[os.path.basename(p) for p in subgraph_paths]}" | ||
| ) | ||
|
|
||
| device = model.__class__.__graph_net_device__ | ||
| subgraph_instances = [] | ||
|
|
||
| for path in subgraph_paths: | ||
| instance = self._load_model_instance(path, device) | ||
| subgraph_instances.append(instance) | ||
| dir_name = os.path.basename(path) | ||
| print( | ||
| f"[RangeDecomposerValidatorBackend] Loaded and instantiated '{dir_name}'" | ||
| ) | ||
|
|
||
| composed_model = ComposedModel(subgraph_instances) | ||
| return composed_model.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
Empty file.
91 changes: 0 additions & 91 deletions
91
todo_works/range_decomposer_validator/range_decomposer_validator.py
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
todo_works/range_decomposer_validator/test/simple_CNN/graph_hash.txt
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file removed
0
todo_works/range_decomposer_validator/test/simple_CNN/input_tensor_constraints.py
Empty file.
92 changes: 0 additions & 92 deletions
92
todo_works/range_decomposer_validator/test/simple_CNN/model.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.