-
Notifications
You must be signed in to change notification settings - Fork 52
add graph_net_sample_groups_util.py and graph_net_sample_util.py #658
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
lixinqi
merged 2 commits into
PaddlePaddle:develop
from
ywh555hhh:feat-sample-groups-util-v3
Mar 3, 2026
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,49 @@ | ||
| # graph_net_sample_groups_util.py | ||
| from typing import List, Set, Dict | ||
| from collections import defaultdict | ||
|
|
||
| from sqlite.orm_models import get_session, GraphNetSampleGroup | ||
|
|
||
|
|
||
| def get_all_graph_net_sample_groups( | ||
| db_path: str, | ||
| group_types: List[str], | ||
| group_policies: List[str], | ||
| versions: List[str], | ||
| ) -> List[Set[str]]: | ||
| """ | ||
| Get all graph_net sample groups from database. | ||
|
|
||
| Viba: | ||
| get_all_graph_net_sample_groups := | ||
| list[set[$sample_uid str]] | ||
| <- $group_net_db_file_path str | ||
| <- $group_type list[str] | ||
| <- $group_policy list[str] | ||
| <- $version list[str] | ||
|
|
||
| Args: | ||
| db_path: Path to the SQLite database file. | ||
| group_types: List of group types to filter (e.g., ["shape_diversity", "dtype_diversity"]). | ||
| group_policies: List of group policies to filter (e.g., ["by_bucket"]). | ||
| versions: List of policy versions to filter (e.g., ["v0.1"]). | ||
|
|
||
| Returns: | ||
| List of sets, each set contains sample UIDs belonging to one group. | ||
| """ | ||
| session = get_session(db_path) | ||
|
|
||
| query = session.query(GraphNetSampleGroup).filter( | ||
| GraphNetSampleGroup.deleted.is_(False), | ||
| GraphNetSampleGroup.group_type.in_(group_types), | ||
| GraphNetSampleGroup.group_policy.in_(group_policies), | ||
| GraphNetSampleGroup.policy_version.in_(versions), | ||
| ) | ||
|
|
||
| groups_dict: Dict[str, List[str]] = defaultdict(list) | ||
| for row in query.all(): | ||
| groups_dict[row.group_uid].append(row.sample_uid) | ||
|
|
||
| session.close() | ||
|
|
||
| return [set(uids) for uids in groups_dict.values()] |
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,127 @@ | ||
| # graph_net_sample_util.py | ||
| import json | ||
| from typing import Dict, List | ||
|
|
||
| from sqlite.orm_models import get_session, GraphSample, SampleOpNameList | ||
|
|
||
|
|
||
| class GraphNetSampleTypeGetter: | ||
| """ | ||
| Get sample_type for a given sample_uid. | ||
|
|
||
| Viba: | ||
| GraphNetSampleTypeGetter := | ||
| # __call__ | ||
| $sample_type str | ||
| <- $sample_uid str | ||
| # __init__ | ||
| <- $group_net_db_file_path str | ||
| <- $fetch_cache dict[$sample_uid str, $sample_type str] | ||
| """ | ||
|
|
||
| def __init__(self, db_path: str): | ||
| self.db_path = db_path | ||
| self._cache: Dict[str, str] = {} | ||
|
|
||
| def __call__(self, sample_uid: str) -> str: | ||
| """Get sample_type for the given sample_uid.""" | ||
| if sample_uid in self._cache: | ||
| return self._cache[sample_uid] | ||
|
|
||
| session = get_session(self.db_path) | ||
| sample = ( | ||
| session.query(GraphSample).filter(GraphSample.uuid == sample_uid).first() | ||
| ) | ||
| session.close() | ||
|
|
||
| sample_type = sample.sample_type if sample else "" | ||
| self._cache[sample_uid] = sample_type | ||
| return sample_type | ||
|
|
||
| def bulk_get(self, sample_uids: List[str]) -> Dict[str, str]: | ||
| """Bulk get sample_types for multiple sample UIDs.""" | ||
| session = get_session(self.db_path) | ||
|
|
||
| samples = ( | ||
| session.query(GraphSample).filter(GraphSample.uuid.in_(sample_uids)).all() | ||
| ) | ||
|
|
||
| result = {} | ||
| for s in samples: | ||
| result[s.uuid] = s.sample_type | ||
| self._cache[s.uuid] = s.sample_type | ||
|
|
||
| for uid in sample_uids: | ||
| if uid not in result: | ||
| result[uid] = "" | ||
|
|
||
| session.close() | ||
| return result | ||
|
|
||
|
|
||
| class GraphNetSampleOpSeqGetter: | ||
| """ | ||
| Get op_seq for a given sample_uid. | ||
|
|
||
| Viba: | ||
| GraphNetSampleOpSeqGetter := | ||
| # __call__ | ||
| $sample_op_seq list[str] | ||
| <- $sample_uid str | ||
| # __init__ | ||
| <- $group_net_db_file_path str | ||
| <- $fetch_cache dict[$sample_uid str, $sample_op_seq list[str]] | ||
| """ | ||
|
|
||
| def __init__(self, db_path: str): | ||
| self.db_path = db_path | ||
| self._cache: Dict[str, List[str]] = {} | ||
|
|
||
| def __call__(self, sample_uid: str) -> List[str]: | ||
| """Get op_seq for the given sample_uid.""" | ||
| if sample_uid in self._cache: | ||
| return self._cache[sample_uid] | ||
|
|
||
| session = get_session(self.db_path) | ||
| op_list = ( | ||
| session.query(SampleOpNameList) | ||
| .filter(SampleOpNameList.sample_uuid == sample_uid) | ||
| .first() | ||
| ) | ||
| session.close() | ||
|
|
||
| if op_list and op_list.op_names_json: | ||
| op_data = json.loads(op_list.op_names_json) | ||
| op_seq = [op["op_name"] for op in op_data] | ||
| else: | ||
| op_seq = [] | ||
|
|
||
| self._cache[sample_uid] = op_seq | ||
| return op_seq | ||
|
|
||
| def bulk_get(self, sample_uids: List[str]) -> Dict[str, List[str]]: | ||
| """Bulk get op_seqs for multiple sample UIDs.""" | ||
| session = get_session(self.db_path) | ||
|
|
||
| op_lists = ( | ||
| session.query(SampleOpNameList) | ||
| .filter(SampleOpNameList.sample_uuid.in_(sample_uids)) | ||
| .all() | ||
| ) | ||
|
|
||
| result = {} | ||
| for op_list in op_lists: | ||
| if op_list.op_names_json: | ||
| op_data = json.loads(op_list.op_names_json) | ||
| op_seq = [op["op_name"] for op in op_data] | ||
| else: | ||
| op_seq = [] | ||
| result[op_list.sample_uuid] = op_seq | ||
| self._cache[op_list.sample_uuid] = op_seq | ||
|
|
||
| for uid in sample_uids: | ||
| if uid not in result: | ||
| result[uid] = [] | ||
|
|
||
| session.close() | ||
| return result |
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.
把这些文件都放到 sqlite/util/下吧。