|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import shutil |
| 4 | +import json |
| 5 | +import yaml |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from config import O2O_DIR, BUILD_DIR |
| 9 | +from util import run_cmd |
| 10 | + |
| 11 | + |
| 12 | +def main(): |
| 13 | + parser = argparse.ArgumentParser(description="Export model to GGMA package") |
| 14 | + parser.add_argument("-s", |
| 15 | + "--script", |
| 16 | + required=True, |
| 17 | + help="Export script to use (e.g., tinyllama.py)") |
| 18 | + parser.add_argument("-p", |
| 19 | + "--pipeline", |
| 20 | + default="pipeline.yaml", |
| 21 | + help="Pipeline configuration file (default: pipeline.yaml)") |
| 22 | + args = parser.parse_args() |
| 23 | + |
| 24 | + export_script_name = args.script |
| 25 | + pipeline_file = args.pipeline |
| 26 | + |
| 27 | + # Change to build directory |
| 28 | + if not os.path.exists(BUILD_DIR): |
| 29 | + print(f"Error: {BUILD_DIR} directory does not exist. Run 'opm import' first.") |
| 30 | + return |
| 31 | + |
| 32 | + os.chdir(BUILD_DIR) |
| 33 | + |
| 34 | + # Load pipeline configuration |
| 35 | + pipeline_path = os.path.abspath(pipeline_file) |
| 36 | + if not os.path.exists(pipeline_path): |
| 37 | + print(f"Error: Pipeline file {pipeline_path} not found.") |
| 38 | + return |
| 39 | + |
| 40 | + with open(pipeline_path, "r") as f: |
| 41 | + pipeline_config = yaml.safe_load(f) |
| 42 | + |
| 43 | + # Find model directory by config.json |
| 44 | + model_dir = None |
| 45 | + for d in os.listdir("."): |
| 46 | + config_path = os.path.join(d, "config.json") |
| 47 | + if os.path.isdir(d) and os.path.exists(config_path): |
| 48 | + model_dir = d |
| 49 | + print(f"Using local model directory: {model_dir}") |
| 50 | + break |
| 51 | + |
| 52 | + if not model_dir: |
| 53 | + raise ValueError("No local model directory found (directory with config.json)") |
| 54 | + |
| 55 | + # Add o2o tools and the current directory (where pipeline scripts reside) to PATH |
| 56 | + env = os.environ.copy() |
| 57 | + cwd_path = os.path.abspath(os.getcwd()) |
| 58 | + o2o_path = os.path.abspath(os.path.join(cwd_path, "..", O2O_DIR)) |
| 59 | + env["PATH"] = f"{o2o_path}:{cwd_path}:{env['PATH']}" |
| 60 | + os.environ.update(env) |
| 61 | + |
| 62 | + # Use current python executable instead of finding venv python |
| 63 | + python_bin = sys.executable |
| 64 | + export_script = os.path.join("..", export_script_name) |
| 65 | + |
| 66 | + # 1. Generate prefill and decode circles |
| 67 | + print(f"Running {export_script_name} (prefill)...") |
| 68 | + run_cmd(f"{python_bin} {export_script} --mode prefill", env=env) |
| 69 | + |
| 70 | + print(f"Running {export_script_name} (decode)...") |
| 71 | + run_cmd(f"{python_bin} {export_script} --mode decode", env=env) |
| 72 | + |
| 73 | + # Helper to run pipeline command |
| 74 | + def run_pipeline_step(step_name): |
| 75 | + if step_name in pipeline_config: |
| 76 | + print(f"Running {step_name} pipeline...") |
| 77 | + cmd = pipeline_config[step_name] |
| 78 | + # If cmd is a multiline string (from YAML |), it might contain newlines. |
| 79 | + # We can replace newlines with spaces or let the shell handle it if it's a single command string. |
| 80 | + # For safety with pipes, we replace newlines with spaces if they are meant to be a single line command. |
| 81 | + # But YAML block scalar preserves newlines. |
| 82 | + # If the user wrote it with pipes at the start of lines, we should join them. |
| 83 | + cmd = cmd.replace("\n", " ") |
| 84 | + run_cmd(cmd, env=env) |
| 85 | + |
| 86 | + # 2. Pipeline (decode) |
| 87 | + run_pipeline_step("decode") |
| 88 | + |
| 89 | + # 3. Merge |
| 90 | + run_pipeline_step("merge") |
| 91 | + |
| 92 | + # 4. Create package directory and copy files |
| 93 | + # Find source directory with tokenizer.json |
| 94 | + source_dir = None |
| 95 | + for d in os.listdir("."): |
| 96 | + if os.path.isdir(d) and os.path.exists(os.path.join(d, "tokenizer.json")): |
| 97 | + source_dir = d |
| 98 | + break |
| 99 | + |
| 100 | + if source_dir: |
| 101 | + package_dir = "out" |
| 102 | + print(f"Creating package directory {package_dir}...") |
| 103 | + os.makedirs(package_dir, exist_ok=True) |
| 104 | + |
| 105 | + # Copy tokenizer and config files |
| 106 | + for filename in ["tokenizer.json", "tokenizer.model", "config.json"]: |
| 107 | + src = os.path.join(source_dir, filename) |
| 108 | + if os.path.exists(src): |
| 109 | + shutil.copy2(src, package_dir) |
| 110 | + |
| 111 | + # Move model.circle |
| 112 | + print(f"Moving model.circle to {package_dir}...") |
| 113 | + shutil.move("model.circle", os.path.join(package_dir, "model.circle")) |
| 114 | + else: |
| 115 | + print( |
| 116 | + "Warning: Could not find source directory (directory with tokenizer.json). Leaving model.circle in current dir." |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments