-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathgraph_net_sample_groups_insert.py
More file actions
executable file
·282 lines (227 loc) · 9.35 KB
/
graph_net_sample_groups_insert.py
File metadata and controls
executable file
·282 lines (227 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import argparse
import sqlite3
import uuid as uuid_module
from collections import defaultdict, namedtuple
from datetime import datetime
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"],
)
# ── 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:
def __init__(self, path):
self.path = path
def connect(self):
self.conn = sqlite3.connect(self.path)
self.conn.row_factory = sqlite3.Row
self.cursor = self.conn.cursor()
def query(self, sql, params=None):
self.cursor.execute(sql, params or ())
return self.cursor.fetchall()
def close(self):
self.conn.close()
def query_bucket_groups(db: DB) -> list[BucketGroup]:
sql = """
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;
"""
return [BucketGroup(*row) for row in db.query(sql)]
def query_v2_candidates(db: DB) -> list[Candidate]:
sql = """
SELECT
s.uuid,
s.sample_type,
b.op_seq_bucket_id,
b.input_shapes_bucket_id,
b.input_dtypes_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'
AND s.uuid NOT IN (
SELECT g.sample_uid
FROM graph_net_sample_groups g
WHERE g.group_policy = 'bucket_policy_v1'
AND g.deleted = 0
)
ORDER BY s.sample_type, b.op_seq_bucket_id, b.input_shapes_bucket_id,
b.input_dtypes_bucket_id, s.uuid;
"""
return [Candidate(*row) for row in db.query(sql)]
# ═══════════════════════════════════════════════════════════════════
# V1: Rule 1 (bucket-internal stride sampling) + Rule 2 (cross-shape)
# ═══════════════════════════════════════════════════════════════════
def generate_v1_groups(bucket_groups: list[BucketGroup]):
"""Yields (sample_type, uid, group_id, rule_name).
Rule 1: stride-16 sampling within each bucket, one group per sample.
Rule 2: aggregate all bucket heads sharing the same (sample_type, op_seq).
"""
# Rule 1
for bucket in bucket_groups:
members = bucket.all_uids_csv.split(",")
for uid in members[::16]:
if uid != bucket.head_uid:
yield bucket.sample_type, uid, _new_group_id(), "rule1"
# Rule 2
heads_by_type_op = defaultdict(list)
for bucket in bucket_groups:
heads_by_type_op[(bucket.sample_type, bucket.op_seq)].append(bucket.head_uid)
for (sample_type, _op), heads in heads_by_type_op.items():
gid = _new_group_id()
for uid in heads:
yield sample_type, uid, gid, "rule2"
# ═══════════════════════════════════════════════════════════════════
# V2: Rule 4 (dtype coverage) + Rule 3 (sparse sampling on remainder)
# ═══════════════════════════════════════════════════════════════════
def generate_v2_groups(candidates: list[Candidate], num_dtypes: int):
"""Yields (sample_type, uid, group_id, rule_name).
Rule 4 (first): per (sample_type, op_seq, shape), pick up to
num_dtypes samples with distinct dtypes.
Rule 3 (second): window-based sparse sampling on the remainder,
window_size = num_dtypes * 5, pick first num_dtypes.
"""
by_type_op = defaultdict(list)
for c in candidates:
by_type_op[(c.sample_type, c.op_seq)].append(c)
covered_uids = set()
# Rule 4: dtype coverage
for (sample_type, _op), group in by_type_op.items():
by_shape = defaultdict(list)
for c in group:
by_shape[c.shapes].append(c)
picked = []
for _shape, shape_group in by_shape.items():
seen_dtypes = set()
for c in shape_group:
if c.dtypes not in seen_dtypes and len(seen_dtypes) < num_dtypes:
seen_dtypes.add(c.dtypes)
picked.append(c.uid)
covered_uids.add(c.uid)
if picked:
gid = _new_group_id()
for uid in picked:
yield sample_type, uid, gid, "rule4"
# Rule 3: sparse sampling on remainder
window_size = num_dtypes * 5
for (sample_type, _op), group in by_type_op.items():
remaining = sorted(
(c for c in group if c.uid not in covered_uids),
key=lambda c: c.uid,
)
picked = [
c.uid for i, c in enumerate(remaining) if (i % window_size) < num_dtypes
]
if picked:
gid = _new_group_id()
for uid in picked:
yield sample_type, uid, gid, "rule3"
# ═══════════════════════════════════════════════════════════════════
# Insert
# ═══════════════════════════════════════════════════════════════════
def _insert_groups(session, rows, policy):
"""Consume a generator of (sample_type, uid, group_id, rule_name),
write to DB, and return per-(sample_type, rule) stats."""
stats = defaultdict(lambda: {"records": 0, "groups": set()})
for sample_type, uid, group_id, rule_name in rows:
session.add(
GraphNetSampleGroup(
sample_uid=uid,
group_uid=group_id,
group_type="ai4c",
group_policy=policy,
policy_version="1.0",
create_at=datetime.now(),
deleted=False,
)
)
stats[(sample_type, rule_name)]["records"] += 1
stats[(sample_type, rule_name)]["groups"].add(group_id)
session.commit()
return stats
# ═══════════════════════════════════════════════════════════════════
# Main
# ═══════════════════════════════════════════════════════════════════
def main():
parser = argparse.ArgumentParser(
description="Generate graph_net_sample_groups (v1 + v2)"
)
parser.add_argument("--db_path", type=str, required=True)
parser.add_argument("--num_dtypes", type=int, default=3)
args = parser.parse_args()
db = DB(args.db_path)
db.connect()
session = get_session(args.db_path)
all_stats = defaultdict(lambda: {"records": 0, "groups": set()})
try:
# V1
buckets = query_bucket_groups(db)
print(f"Bucket groups: {len(buckets)}")
v1 = _insert_groups(session, generate_v1_groups(buckets), "bucket_policy_v1")
_merge_stats(all_stats, v1)
# V2
candidates = query_v2_candidates(db)
print(f"V2 candidates: {len(candidates)}")
if candidates:
v2 = _insert_groups(
session,
generate_v2_groups(candidates, args.num_dtypes),
"bucket_policy_v2",
)
_merge_stats(all_stats, v2)
else:
print("No V2 candidates found. Skipping.")
except Exception:
session.rollback()
raise
finally:
session.close()
db.close()
print("=" * 60)
_print_stats(all_stats)
print("\nDone!")
if __name__ == "__main__":
main()