-
Notifications
You must be signed in to change notification settings - Fork 50
group by v2 #680
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
base: develop
Are you sure you want to change the base?
group by v2 #680
Changes from all commits
95b4b92
6185419
91a7389
e444c2e
a13a356
b4a098a
238d966
489af43
05e5d0f
f784701
c25d8d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sqlite3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import uuid as uuid_module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import defaultdict, namedtuple | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from datetime import datetime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import namedtuple | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from collections import defaultdict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from orm_models import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get_session, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GraphNetSampleGroup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from orm_models import get_session, GraphNetSampleGroup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ── Types ── | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BucketGroup = namedtuple( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "BucketGroup", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["head_uid", "op_seq", "shapes", "sample_type", "all_uids_csv"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Candidate = namedtuple( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Candidate", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["uid", "sample_type", "op_seq", "shapes", "dtypes"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GraphNetSampleUid = str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GraphNetSampleType = str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BucketId = str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ── Helpers ── | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _new_group_id(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return str(uuid_module.uuid4()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _merge_stats(dst, src): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key, val in src.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dst[key]["records"] += val["records"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dst[key]["groups"].update(val["groups"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _print_stats(stats): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rule_order = ["rule1", "rule2", "rule4", "rule3"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sample_types = sorted({st for st, _ in stats}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_records = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_groups = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for sample_type in sample_types: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"\n [{sample_type}]") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for rule in rule_order: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key = (sample_type, rule) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if key in stats: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n_records = stats[key]["records"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n_groups = len(stats[key]["groups"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f" {rule}: {n_records} records, {n_groups} groups") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_records += n_records | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_groups += n_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"\n Total: {total_records} records, {total_groups} groups.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ── Database Queries ── | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class DB: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -23,117 +61,222 @@ def __init__(self, path): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def connect(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.conn = sqlite3.connect(self.path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.conn.row_factory = sqlite3.Row | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.cur = self.conn.cursor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.cursor = self.conn.cursor() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def query(self, sql, params=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.cur.execute(sql, params or ()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self.cur.fetchall() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def exec(self, sql, params=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.cur.execute(sql, params or ()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.conn.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.cursor.execute(sql, params or ()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self.cursor.fetchall() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def close(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.conn.close() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SampleBucketInfo = namedtuple( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "SampleBucketInfo", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "sample_uid", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "op_seq_bucket_id", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "input_shapes_bucket_id", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "input_dtypes_bucket_id", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "sample_type", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "sample_uids", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def query_bucket_groups(db: DB) -> list[BucketGroup]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sql = """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SELECT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MIN(sub.sample_uid) AS head_uid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sub.op_seq_bucket_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sub.input_shapes_bucket_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sub.sample_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group_concat(sub.sample_uid, ',') AS all_uids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SELECT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.uuid AS sample_uid, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.sample_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b.op_seq_bucket_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b.input_shapes_bucket_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM graph_sample s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JOIN graph_net_sample_buckets b ON s.uuid = b.sample_uid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WHERE s.deleted = 0 AND s.sample_type != 'full_graph' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ORDER BY s.create_at ASC, s.uuid ASC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) sub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GROUP BY sub.sample_type, sub.op_seq_bucket_id, sub.input_shapes_bucket_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+76
to
+93
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SELECT | |
| sub.sample_uid, | |
| sub.op_seq_bucket_id, | |
| sub.input_shapes_bucket_id, | |
| sub.sample_type, | |
| group_concat(sub.sample_uid, ',') AS all_uids | |
| FROM ( | |
| SELECT | |
| s.uuid AS sample_uid, | |
| s.sample_type, | |
| b.op_seq_bucket_id, | |
| b.input_shapes_bucket_id | |
| FROM graph_sample s | |
| JOIN graph_net_sample_buckets b ON s.uuid = b.sample_uid | |
| ORDER BY s.create_at ASC, s.uuid ASC | |
| ) sub | |
| GROUP BY sub.sample_type, sub.op_seq_bucket_id, sub.input_shapes_bucket_id; | |
| WITH buckets AS ( | |
| SELECT | |
| s.uuid AS sample_uid, | |
| s.sample_type, | |
| b.op_seq_bucket_id, | |
| b.input_shapes_bucket_id, | |
| FIRST_VALUE(s.uuid) OVER ( | |
| PARTITION BY s.sample_type, b.op_seq_bucket_id, b.input_shapes_bucket_id | |
| ORDER BY s.create_at ASC, s.uuid ASC | |
| ) AS head_uid | |
| FROM graph_sample s | |
| JOIN graph_net_sample_buckets b ON s.uuid = b.sample_uid | |
| ) | |
| SELECT | |
| MIN(head_uid) AS head_uid, | |
| op_seq_bucket_id, | |
| input_shapes_bucket_id, | |
| sample_type, | |
| group_concat(sample_uid, ',') AS all_uids | |
| FROM buckets | |
| GROUP BY sample_type, op_seq_bucket_id, input_shapes_bucket_id; |
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.
done
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.
query_bucket_groupsdoesn’t filter out deleted samples orfull_graphsamples (unlikequery_v2_candidates). Ifgraph_net_sample_bucketscontains rows for deleted/full_graph samples (e.g., from older runs), this script will generate groups for data that is supposed to be excluded. AddWHERE s.deleted = 0 AND s.sample_type != 'full_graph'(or equivalent) to keep v1/v2 selection consistent.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.
done