Skip to content

Commit 13f3201

Browse files
justinchubyCopilot
andcommitted
hy_mt1_5 example: drop reuse logic, always rebuild
The previous version wrote a .mobius-variant marker on build and tried to reuse --out across runs, refusing to silently swap variants. That added two helpers (_existing_variant, marker file management) for a small UX win, and was easy to get wrong (e.g. dirty --out from a half-finished build was silently reused). Always rebuilding is simpler and effectively just as fast for the demo: huggingface_hub already caches the safetensors (bf16) and GGUF (Q1_0) downloads, so 'always download' is a no-op on the network side and the in-process mobius graph construction runs every time anyway. As a bonus the --build flag is no longer needed, so it's gone. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent 02ecb01 commit 13f3201

1 file changed

Lines changed: 23 additions & 54 deletions

File tree

‎examples/hy_mt1_5.py‎

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@
4242
4343
Usage::
4444
45-
# One-step demo: downloads the GGUF from AngelSlim/Hy-MT1.5-1.8B-2bit-GGUF
46-
# on first use, builds the ONNX model into --out, and runs translation.
45+
# One-shot demo: downloads the GGUF from AngelSlim/Hy-MT1.5-1.8B-2bit-GGUF
46+
# (cached by huggingface_hub), builds the ONNX model into --out, and runs
47+
# translation. Re-running rebuilds the ONNX (the network download is cached).
4748
python examples/hy_mt1_5.py --variant Q1_0 --out ./hy-mt-q1_0-onnx \\
4849
--prompt 'Translate to Spanish: The cat is sleeping on the chair.'
4950
50-
# Subsequent runs reuse the build (no download, no rebuild)
51-
python examples/hy_mt1_5.py --variant Q1_0 --out ./hy-mt-q1_0-onnx
52-
5351
# Build from a local GGUF instead of letting mobius download it
54-
python examples/hy_mt1_5.py --variant Q1_0 --build \\
52+
python examples/hy_mt1_5.py --variant Q1_0 \\
5553
--gguf ./gguf/Hy-MT1.5-1.8B-2bit.gguf --out ./hy-mt-q1_0-onnx
5654
"""
5755

@@ -70,9 +68,6 @@
7068
_DEFAULT_PROMPT = "Translate the following Chinese text to English: 你好,世界!" # noqa: RUF001
7169

7270

73-
_VARIANT_MARKER = ".mobius-variant"
74-
75-
7671
def _build_bf16(output_dir: Path) -> None:
7772
"""Build the BF16 ONNX model + ORT-GenAI config in-process."""
7873
import mobius
@@ -82,7 +77,6 @@ def _build_bf16(output_dir: Path) -> None:
8277
pkg = mobius.build(HF_MODEL_ID, dtype="bf16", load_weights=True)
8378
pkg.save(str(output_dir))
8479
write_ort_genai_config(pkg, str(output_dir), hf_model_id=HF_MODEL_ID, ep="cpu")
85-
(output_dir / _VARIANT_MARKER).write_text("bf16\n")
8680
print(" genai_config.json + tokenizer files written")
8781

8882

@@ -105,46 +99,27 @@ def _build_q1_0(gguf_ref: str, output_dir: Path) -> None:
10599
pkg = build_from_gguf(gguf_ref, keep_quantized=True, dtype="f32", execution_provider="cpu")
106100
pkg.save(str(output_dir))
107101
write_ort_genai_config(pkg, str(output_dir), hf_model_id=HF_MODEL_ID, ep="cpu")
108-
(output_dir / _VARIANT_MARKER).write_text(f"Q1_0\t{gguf_ref}\n")
109102
print(" genai_config.json + tokenizer files written")
110103

111104

112-
def _existing_variant(output_dir: Path) -> str | None:
113-
"""Return the variant string recorded for an existing build, or None."""
114-
marker = output_dir / _VARIANT_MARKER
115-
if not marker.exists():
116-
return None
117-
return marker.read_text().split("\t", 1)[0].strip() or None
105+
def _build(args: argparse.Namespace) -> Path:
106+
"""Always (re)build the model into ``--out``.
118107
119-
120-
def _ensure_built(args: argparse.Namespace) -> Path:
108+
HuggingFace Hub caches the source (safetensors for bf16, GGUF for Q1_0),
109+
so re-runs are cheap on the network side; the in-process ONNX construction
110+
runs every time. This keeps the example dead-simple: no marker files, no
111+
reuse logic, no chance of silently running a stale or wrong-variant build.
112+
"""
121113
out = Path(args.out).resolve()
122-
existing_variant = _existing_variant(out)
123-
has_model = (out / "genai_config.json").exists()
124-
125-
# Refuse to silently reuse a directory built for a different variant.
126-
if has_model and existing_variant is not None and existing_variant != args.variant:
127-
sys.exit(
128-
f"{out} already contains a {existing_variant!r} build but "
129-
f"--variant is {args.variant!r}. Pick a different --out, "
130-
f"or remove the directory and re-run."
131-
)
132-
133-
# Default to building from the published HF GGUF so a fresh demo only
134-
# needs one ``python examples/hy_mt1_5.py ...`` invocation.
135-
need_build = args.build or not has_model
136-
if need_build:
137-
out.mkdir(parents=True, exist_ok=True)
138-
if args.variant == "bf16":
139-
_build_bf16(out)
140-
elif args.variant == "Q1_0":
141-
gguf_ref = args.gguf or GGUF_HF_REF
142-
_build_q1_0(gguf_ref, out)
114+
out.mkdir(parents=True, exist_ok=True)
115+
if args.variant == "bf16":
116+
_build_bf16(out)
117+
elif args.variant == "Q1_0":
118+
_build_q1_0(args.gguf or GGUF_HF_REF, out)
143119
if not (out / "genai_config.json").exists():
144120
sys.exit(
145-
f"Build did not produce {out / 'genai_config.json'}. The build step above "
146-
f"failed; check the preceding output. To force a rebuild from a clean state, "
147-
f"remove {out} and re-run with --build."
121+
f"Build did not produce {out / 'genai_config.json'}; check the "
122+
f"preceding output for the underlying failure."
148123
)
149124
return out
150125

@@ -237,24 +212,18 @@ def main() -> None:
237212
"--out",
238213
default=None,
239214
help=(
240-
"ONNX model directory. If it doesn't contain a built model yet, "
241-
"one is created automatically (downloading the GGUF for --variant Q1_0 "
242-
"when --gguf isn't given). Subsequent runs with the same --out reuse "
243-
"the build. Defaults to ./hy-mt-<variant>-onnx."
215+
"Directory to write the ONNX model + ORT GenAI config into. "
216+
"Always overwritten on each run (HuggingFace cache handles "
217+
"the underlying source download). Defaults to ./hy-mt-<variant>-onnx."
244218
),
245219
)
246-
parser.add_argument(
247-
"--build",
248-
action="store_true",
249-
help="Force rebuild even when --out already has a model",
250-
)
251220
parser.add_argument(
252221
"--gguf",
253222
default=None,
254223
help=(
255224
"Path to a local Tencent SEQ GGUF for --variant Q1_0. When omitted, "
256225
"mobius downloads the GGUF from AngelSlim/Hy-MT1.5-1.8B-2bit-GGUF "
257-
"on first use."
226+
"(cached locally by huggingface_hub)."
258227
),
259228
)
260229
parser.add_argument("--prompt", default=_DEFAULT_PROMPT)
@@ -275,7 +244,7 @@ def main() -> None:
275244
if args.out is None:
276245
args.out = f"./hy-mt-{args.variant.lower()}-onnx"
277246

278-
out = _ensure_built(args)
247+
out = _build(args)
279248
text = run_translation(
280249
out,
281250
args.prompt,

0 commit comments

Comments
 (0)