Skip to content

Commit b6bc9cd

Browse files
Fix output_dir of dtype generalization pass (#624)
* fix output_dir of dtype generalization pass * fix CodeStyle * gen graph_hash.txt and subgraph_sources.json * fix comment of dtype_generalizer.py
1 parent c1b9221 commit b6bc9cd

2 files changed

Lines changed: 53 additions & 34 deletions

File tree

graph_net/test/dtype_gen_test.sh

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,3 @@ EOF
4343
)
4444

4545

46-
# Step 3: Valiation
47-
SUCCESS_CNT=0
48-
FAIL_CNT=0
49-
50-
for model_path in "$OUTPUT_DIR"/*; do
51-
echo "[VALIDATE] $model_path"
52-
53-
output=$(python -m graph_net.torch.validate \
54-
--model-path "$model_path" 2>&1)
55-
56-
if echo "$output" | grep -q "Validation success, model_path="; then
57-
echo "SUCCESS"
58-
((SUCCESS_CNT++))
59-
else
60-
echo "FAIL"
61-
((FAIL_CNT++))
62-
fi
63-
done
64-
65-
echo "===================="
66-
echo "SUCCESS $SUCCESS_CNT"
67-
echo "FAIL $FAIL_CNT"

graph_net/torch/sample_pass/dtype_generalizer.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
from graph_net.sample_pass.sample_pass import SamplePass
4343
from graph_net.sample_pass.resumable_sample_pass_mixin import ResumableSamplePassMixin
4444

45+
from graph_net.hash_util import get_sha256_hash
46+
4547
# Weights that must remain float32 for numerical stability
4648
FLOAT32_PRESERVED_WEIGHTS = {
4749
"running_mean",
@@ -296,7 +298,7 @@ def sample_handled(self, rel_model_path: str) -> bool:
296298
def __call__(self, rel_model_path: str):
297299
self.resumable_handle_sample(rel_model_path)
298300

299-
def resume(self, model_path: str) -> List[str]:
301+
def resume(self, rel_model_path: str) -> List[str]:
300302
"""
301303
Apply dtype passes to generate new samples.
302304
@@ -308,24 +310,36 @@ def resume(self, model_path: str) -> List[str]:
308310
"""
309311
# Apply model_path_prefix if provided
310312
if self.model_path_prefix:
311-
model_path = str(Path(self.model_path_prefix) / model_path)
313+
abs_model_path = str(Path(self.model_path_prefix) / rel_model_path)
312314

313315
# Read pass names from graph_net.json
314-
dtype_pass_names = self._read_dtype_pass_names(model_path)
316+
dtype_pass_names = self._read_dtype_pass_names(abs_model_path)
315317

316318
if not dtype_pass_names:
317-
logging.warning(f"No dtype passes found in {model_path}/graph_net.json")
319+
logging.warning(f"No dtype passes found in {abs_model_path}/graph_net.json")
318320
return []
319321

320322
# Parse the computation graph
321-
traced_model = parse_immutable_model_path_into_sole_graph_module(model_path)
323+
traced_model = parse_immutable_model_path_into_sole_graph_module(abs_model_path)
324+
325+
# Copy the originl sample
326+
files_copied = [
327+
"model.py",
328+
"graph_hash.txt",
329+
"graph_net.json",
330+
"weight_meta.py",
331+
"input_meta.py",
332+
"input_tensor_constraints.py",
333+
"subgraph_sources.json",
334+
]
335+
self._copy_sample_files(rel_model_path, "float32", files_copied)
322336

323337
# Generate samples for each pass
324338
generated_samples = []
325339
for pass_name in dtype_pass_names:
326340
try:
327341
sample_dir = self._apply_pass_and_generate(
328-
model_path, traced_model, pass_name
342+
rel_model_path, traced_model, pass_name
329343
)
330344
generated_samples.append(sample_dir)
331345
logging.info(f"Generated sample: {sample_dir}")
@@ -388,8 +402,7 @@ def _apply_pass_and_generate(
388402
gm_modified = dtype_pass.rewrite(gm_copy)
389403

390404
# Generate output directory
391-
model_name = Path(model_path).name
392-
output_sample_dir = Path(self.output_dir) / f"{model_name}_{dtype}"
405+
output_sample_dir = Path(self.output_dir) / dtype / model_path
393406
output_sample_dir.mkdir(parents=True, exist_ok=True)
394407

395408
# Write modified model.py
@@ -398,11 +411,20 @@ def _apply_pass_and_generate(
398411
with open(output_sample_dir / "model.py", "w") as f:
399412
f.write(write_code)
400413

414+
# Write modified graph_hash.txt
415+
model_hash = get_sha256_hash(model_code)
416+
with open(output_sample_dir / "graph_hash.txt", "w") as f:
417+
f.write(model_hash)
418+
401419
# Copy metadata files
402-
for fname in ["graph_net.json", "weight_meta.py", "input_meta.py"]:
403-
src = Path(model_path) / fname
404-
if src.exists():
405-
shutil.copy(src, output_sample_dir / fname)
420+
files_copied = [
421+
"graph_net.json",
422+
"weight_meta.py",
423+
"input_meta.py",
424+
"input_tensor_constraints.py",
425+
"subgraph_sources.json",
426+
]
427+
self._copy_sample_files(model_path, dtype, files_copied)
406428

407429
# Update graph_net.json with dtype information
408430
self._update_sample_metadata(output_sample_dir, dtype)
@@ -429,6 +451,25 @@ def _update_sample_metadata(self, sample_dir: Path, dtype: str) -> None:
429451
update_json(graph_net_json_path, kDtypeGeneralizationPrecision, dtype)
430452
update_json(graph_net_json_path, kDtypeGeneralizationGenerated, True)
431453

454+
def _copy_sample_files(
455+
self, rel_model_path: str, dtype: str, files_copied: list
456+
) -> None:
457+
"""
458+
Copy files of sample.
459+
460+
Args:
461+
rel_model_path: relative model path
462+
"""
463+
# Generate output directory
464+
output_sample_dir = Path(self.output_dir) / dtype / rel_model_path
465+
output_sample_dir.mkdir(parents=True, exist_ok=True)
466+
467+
# Copy files of original sample
468+
for fname in files_copied:
469+
src = Path(rel_model_path) / fname
470+
if src.exists():
471+
shutil.copy(src, output_sample_dir / fname)
472+
432473

433474
class MultiDtypeFilter:
434475
"""

0 commit comments

Comments
 (0)