-
Notifications
You must be signed in to change notification settings - Fork 43
【Hackathon 9th Sprint No.8】Add RangeDecomposerBackend #372
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
7 commits
Select commit
Hold shift + click to select a range
af92b86
add test
fangfangssj b59dc33
add backend
fangfangssj a47d29a
split
fangfangssj 457c4af
Merge branch 'develop' into spilt
fangfangssj e5abd5f
fix
fangfangssj fbdf572
Delete graph_net/test/split_points.py
fangfangssj dab3759
Update help text for configuration argumentf
fangfangssj 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,46 @@ | ||
| #!/bin/bash | ||
|
|
||
| GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(os.path.dirname(graph_net.__file__)))") | ||
|
|
||
| MODEL1="$GRAPH_NET_ROOT/samples/torchvision/resnet18" | ||
| MODEL2="$GRAPH_NET_ROOT/samples/torchvision/resnet34" | ||
| MODEL_LIST_FILE=$(mktemp) | ||
| echo "$MODEL1" > "$MODEL_LIST_FILE" | ||
| echo "$MODEL2" >> "$MODEL_LIST_FILE" | ||
|
|
||
| python3 -m graph_net.torch.typical_sequence_split_points \ | ||
| --model-list "$MODEL_LIST_FILE" \ | ||
| --device "cuda" \ | ||
| --window-size 10 \ | ||
| --output-json "$GRAPH_NET_ROOT/split_results.json" | ||
|
|
||
| rm -f "$MODEL_LIST_FILE" | ||
|
|
||
|
|
||
| MODEL_PATH_IN_SAMPLES=/torchvision/resnet18 | ||
| MODEL_NAME=$(basename "$MODEL_PATH_IN_SAMPLES") | ||
|
|
||
| decomposer_config_json_str=$(cat <<EOF | ||
| { | ||
| "split_results_path": "$GRAPH_NET_ROOT/split_results.json", | ||
| "workspace_path": "$GRAPH_NET_ROOT/decompose_workspace", | ||
| "chain_style": "True" | ||
| } | ||
| EOF | ||
| ) | ||
| DECOMPOSER_CONFIG=$(echo $decomposer_config_json_str | base64 -w 0) | ||
|
|
||
| python3 -m graph_net.torch.test_compiler --model-path $GRAPH_NET_ROOT/samples/$MODEL_PATH_IN_SAMPLES --compiler range_decomposer --device cuda --config=$DECOMPOSER_CONFIG | ||
|
|
||
|
|
||
| DECOMPOSE_PATH=$GRAPH_NET_ROOT/decompose_workspace | ||
| cp -r "$GRAPH_NET_ROOT/samples/$MODEL_PATH_IN_SAMPLES" "$DECOMPOSE_PATH/" | ||
|
|
||
| python3 -m graph_net.torch.test_compiler \ | ||
| --model-path $DECOMPOSE_PATH/$MODEL_NAME \ | ||
| --compiler range_decomposer_validator \ | ||
| --device cuda > "$DECOMPOSE_PATH/log.log" 2>&1 | ||
|
|
||
| python3 -m graph_net.plot_ESt \ | ||
| --benchmark-path $DECOMPOSE_PATH/log.log \ | ||
| --output-dir $DECOMPOSE_PATH \ |
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,94 @@ | ||
| import base64 | ||
| import json | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import Any, Dict | ||
|
|
||
| import torch | ||
| import graph_net | ||
|
|
||
|
|
||
| def convert_to_dict(config_str): | ||
| if config_str is None: | ||
| return {} | ||
| config_str = base64.b64decode(config_str).decode("utf-8") | ||
| config = json.loads(config_str) | ||
| assert isinstance(config, dict), f"config should be a dict. {config_str=}" | ||
| return config | ||
|
|
||
|
|
||
| def encode_config(config: Dict[str, Any]) -> str: | ||
| json_str = json.dumps(config) | ||
| return base64.b64encode(json_str.encode("utf-8")).decode("utf-8") | ||
|
|
||
|
|
||
| def load_json(file_path): | ||
| with open(file_path, "r", encoding="utf-8") as file: | ||
| data_dict = json.load(file) | ||
| return data_dict | ||
|
|
||
|
|
||
| class RangeDecomposerBackend: | ||
| def __init__(self): | ||
| self.graph_net_root = Path(graph_net.__file__).parent | ||
|
|
||
| def __call__(self, model: torch.nn.Module) -> torch.nn.Module: | ||
| config = convert_to_dict(self.config) | ||
| workspace_path = Path(config["workspace_path"]) | ||
| chain_style = config["chain_style"] | ||
|
|
||
| model_file_path = Path(model.__class__.__graph_net_file_path__) | ||
| model_name = model_file_path.parent.name | ||
|
|
||
| model_info = load_json(config["split_results_path"])[model_name] | ||
| model_path = model_info["path"] | ||
| split_points = model_info["split_points"] | ||
|
|
||
| model_output_dir = workspace_path / f"{model_name}_decomposed" | ||
| model_output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| config_dict = { | ||
| "decorator_path": str(self.graph_net_root / "torch/extractor.py"), | ||
| "decorator_config": { | ||
| "name": model_name, | ||
| "custom_extractor_path": str( | ||
| self.graph_net_root / "torch/naive_graph_decomposer.py" | ||
| ), | ||
| "custom_extractor_config": { | ||
| "output_dir": str(model_output_dir), | ||
| "split_positions": split_points, | ||
| "group_head_and_tail": True, | ||
| "filter_path": str( | ||
| self.graph_net_root / "torch/naive_subgraph_filter.py" | ||
| ), | ||
| "filter_config": {}, | ||
| "chain_style": chain_style, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| encoded_config = encode_config(config_dict) | ||
|
|
||
| cmd = [ | ||
| sys.executable, | ||
| "-m", | ||
| "graph_net.torch.run_model", | ||
| "--model-path", | ||
| model_path, | ||
| "--decorator-config", | ||
| encoded_config, | ||
| ] | ||
|
|
||
| try: | ||
| subprocess.run(cmd, check=True) | ||
| print(f"[Success] Saved to {model_output_dir}") | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"[Error] Process failed: {e}") | ||
| except Exception as e: | ||
| print(f"[Error] Unexpected: {e}") | ||
| return model | ||
|
|
||
| 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
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
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.