Skip to content

Commit 7b1c685

Browse files
committed
Add generation of buckets and groups into the build of db.
1 parent ed2240b commit 7b1c685

4 files changed

Lines changed: 47 additions & 125 deletions

File tree

graph_net/tools/generate_subgraph_dataset.sh

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ function generate_subgraph_list() {
9393
| tee $sample_list
9494
}
9595

96-
function insert_graph_sample(){
97-
local target_dir="$1"
98-
local repo_uid="$2"
99-
local sample_type="$3"
100-
local sample_list="$4"
101-
echo ">>> [0] Inserting samples into database: ${DB_PATH}."
102-
echo ">>>"
103-
104-
if [ ! -f "$DB_PATH" ]; then
105-
echo "Fail ! No Database ! : $DB_PATH"
106-
exit 1
107-
fi
108-
109-
local order_value=0
110-
while IFS= read -r model_rel_path; do
111-
echo "insert : $model_rel_path"
112-
python3 "${GRAPH_NET_ROOT}/sqlite/graphsample_insert.py" \
113-
--model_path_prefix "${target_dir}" \
114-
--relative_model_path "$model_rel_path" \
115-
--repo_uid "${repo_uid}" \
116-
--sample_type "${sample_type}" \
117-
--order_value "$order_value" \
118-
--db_path "$DB_PATH"
119-
120-
((order_value++))
121-
122-
done < "$sample_list"
123-
}
124-
125-
12696
function rewrite_device() {
12797
echo ">>> [1] Rewrite devices for subgraph samples under ${GRAPH_NET_ROOT}."
12898
echo ">>>"
@@ -658,29 +628,6 @@ function generate_typical_subgraphs() {
658628
# generate_unittest_for_typical_subgraphs 2>&1 | tee ${DECOMPOSE_WORKSPACE}/log_unittests_typical_subgraphs_${suffix}.txt
659629
}
660630

661-
function generate_database() {
662-
timestamp=`date +%Y%m%d_%H%M`
663-
664-
# init database
665-
if [ ! -f ${DB_PATH} ]; then
666-
python ${GRAPH_NET_ROOT}/sqlite/init_db.py --db_path ${DB_PATH} 2>&1 | tee ${DECOMPOSE_WORKSPACE}/log_init_db_${timestamp}.txt
667-
fi
668-
669-
# full_graph
670-
insert_graph_sample ${GRAPH_NET_ROOT} "hf_torch_samples" "full_graph" ${model_list}
671-
672-
# fusible_graph, typical_graph
673-
for sample_type in fusible_graph typical_graph; do
674-
insert_graph_sample $OUTPUT_DIR/$sample_type "hf_torch_samples" $sample_type $OUTPUT_DIR/${sample_type}/sample_list.txt
675-
done
676-
677-
# insert buckets
678-
python ${GRAPH_NET_ROOT}/sqlite/graph_net_sample_bucket_generator.py --db_path ${DB_PATH}
679-
680-
# insert groups
681-
python ${GRAPH_NET_ROOT}/sqlite/graph_net_sample_groups_insert.py --db_path ${DB_PATH}
682-
}
683-
684631
function main() {
685632
do_common_generalzation_and_decompose
686633

@@ -693,8 +640,6 @@ function main() {
693640
generate_typical_subgraphs
694641
#cp -rf $DTYPE_GENERALIZED_TYPICAL_SUBGRAPH_DIR $OUTPUT_DIR/$sample_type
695642
#cp -rf $dtype_generalized_typical_subgraph_list $OUTPUT_DIR/$sample_type/sample_list.txt
696-
697-
#generate_database
698643
}
699644

700645
function summary() {

sqlite/build_db.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def main(args):
100100

101101
print("all done")
102102

103+
from graph_sample_bucket_generator import generate_buckets
104+
from graph_sample_groups_insert import generate_groups
105+
106+
generate_buckets(db_path)
107+
generate_groups(db_path, num_dtypes=3)
108+
103109

104110
if __name__ == "__main__":
105111
parser = argparse.ArgumentParser(

sqlite/graph_sample_bucket_generator.py

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -213,52 +213,36 @@ def save_bucket_results(
213213
return count
214214

215215

216-
def main():
217-
parser = argparse.ArgumentParser(
218-
description="Generate graph_net_sample_buckets from graph_sample"
219-
)
220-
parser.add_argument(
221-
"--db_path",
222-
type=str,
223-
required=True,
224-
help="Path to the SQLite database file",
225-
)
226-
parser.add_argument(
227-
"--dry_run",
228-
action="store_true",
229-
help="Only print what would be done, don't actually insert into database",
230-
)
231-
232-
args = parser.parse_args()
233-
234-
session = get_session(args.db_path)
235-
236-
print("=" * 70)
237-
print("Step 1: Generating bucket info from graph_sample...")
238-
sample_type_results, all_bucket_info_map = generate_sample_buckets(session)
239-
print(f" Total samples: {len(all_bucket_info_map)}")
240-
print(f" Number of sample_types: {len(sample_type_results)}")
241-
242-
print()
243-
for result in sorted(sample_type_results, key=lambda x: -len(x)):
244-
flag = " [sole-op]" if result.is_sole_op else ""
245-
print(f" {result.sample_type}{flag}: {len(result)} samples")
216+
def generate_buckets(db_path, dry_run=False):
217+
"""Generate buckets and save to DB."""
218+
session = get_session(db_path)
219+
try:
220+
print("=" * 70)
221+
print("Step 1: Generating bucket info from graph_sample...")
222+
_, bucket_info_map = generate_sample_buckets(session)
223+
if dry_run:
224+
print("Dry run mode - skipping database insert")
225+
print(
226+
f" Would insert {len(bucket_info_map)} records into graph_net_sample_buckets"
227+
)
228+
return 0
246229

247-
print("=" * 70)
248-
if args.dry_run:
249-
print("Dry run mode - skipping database insert")
250-
print(
251-
f" Would insert {len(all_bucket_info_map)} records into graph_net_sample_buckets"
252-
)
253-
else:
254230
print("Step 2: Saving to database...")
255-
count = save_bucket_results(session, all_bucket_info_map)
231+
count = save_bucket_results(session, bucket_info_map)
256232
print(f" Inserted {count} records into graph_net_sample_buckets")
233+
return count
234+
finally:
235+
session.close()
257236

258-
print("=" * 70)
259-
print("Done!")
260237

261-
session.close()
238+
def main():
239+
parser = argparse.ArgumentParser(
240+
description="Generate graph_net_sample_buckets from graph_sample"
241+
)
242+
parser.add_argument("--db_path", type=str, required=True)
243+
parser.add_argument("--dry_run", action="store_true")
244+
args = parser.parse_args()
245+
generate_buckets(args.db_path, dry_run=args.dry_run)
262246

263247

264248
if __name__ == "__main__":

sqlite/graph_sample_groups_insert.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from orm_models import get_session, GraphNetSampleGroup
88

99

10-
# ── Types ──
11-
1210
BucketGroup = namedtuple(
1311
"BucketGroup",
1412
["head_uid", "op_seq", "shapes", "sample_type", "all_uids_csv"],
@@ -20,9 +18,6 @@
2018
)
2119

2220

23-
# ── Helpers ──
24-
25-
2621
def _new_group_id():
2722
return str(uuid_module.uuid4())
2823

@@ -51,9 +46,6 @@ def _print_stats(stats):
5146
print(f"\n Total: {total_records} records, {total_groups} groups.")
5247

5348

54-
# ── Database Queries ──
55-
56-
5749
class DB:
5850
def __init__(self, path):
5951
self.path = path
@@ -229,39 +221,24 @@ def _insert_groups(session, rows, policy):
229221
return stats
230222

231223

232-
# ═══════════════════════════════════════════════════════════════════
233-
# Main
234-
# ═══════════════════════════════════════════════════════════════════
235-
236-
237-
def main():
238-
parser = argparse.ArgumentParser(
239-
description="Generate graph_net_sample_groups (v1 + v2)"
240-
)
241-
parser.add_argument("--db_path", type=str, required=True)
242-
parser.add_argument("--num_dtypes", type=int, default=3)
243-
args = parser.parse_args()
244-
245-
db = DB(args.db_path)
224+
def generate_groups(db_path, num_dtypes=3):
225+
"""Generate sample groups and save to DB."""
226+
db = DB(db_path)
246227
db.connect()
247-
session = get_session(args.db_path)
248-
228+
session = get_session(db_path)
249229
all_stats = defaultdict(lambda: {"records": 0, "groups": set()})
250-
251230
try:
252-
# V1
253231
buckets = query_bucket_groups(db)
254232
print(f"Bucket groups: {len(buckets)}")
255233
v1 = _insert_groups(session, generate_v1_groups(buckets), "bucket_policy_v1")
256234
_merge_stats(all_stats, v1)
257235

258-
# V2
259236
candidates = query_v2_candidates(db)
260237
print(f"V2 candidates: {len(candidates)}")
261238
if candidates:
262239
v2 = _insert_groups(
263240
session,
264-
generate_v2_groups(candidates, args.num_dtypes),
241+
generate_v2_groups(candidates, num_dtypes),
265242
"bucket_policy_v2",
266243
)
267244
_merge_stats(all_stats, v2)
@@ -276,7 +253,17 @@ def main():
276253

277254
print("=" * 60)
278255
_print_stats(all_stats)
279-
print("\nDone!")
256+
return all_stats
257+
258+
259+
def main():
260+
parser = argparse.ArgumentParser(
261+
description="Generate graph_net_sample_groups (v1 + v2)"
262+
)
263+
parser.add_argument("--db_path", type=str, required=True)
264+
parser.add_argument("--num_dtypes", type=int, default=3)
265+
args = parser.parse_args()
266+
generate_groups(args.db_path, args.num_dtypes)
280267

281268

282269
if __name__ == "__main__":

0 commit comments

Comments
 (0)