()
| 13 | from TIDE import TIDE, TIDEConfig, calibrate |
| 14 | |
| 15 | def main(): |
| 16 | parser = argparse.ArgumentParser(description="TIDE quickstart") |
| 17 | parser.add_argument("--model", default="TinyLlama/TinyLlama-1.1B-Chat-v1.0") |
| 18 | parser.add_argument("--prompt", default="Explain how transformers work in simple terms:") |
| 19 | parser.add_argument("--max-tokens", type=int, default=128) |
| 20 | parser.add_argument("--threshold", type=float, default=0.85) |
| 21 | parser.add_argument("--calibration-samples", type=int, default=200) |
| 22 | parser.add_argument("--router-path", default="router.pt") |
| 23 | args = parser.parse_args() |
| 24 | |
| 25 | # ---- Step 1: Load model ---- |
| 26 | print(f"Loading {args.model}...") |
| 27 | model = AutoModelForCausalLM.from_pretrained( |
| 28 | args.model, torch_dtype=torch.float16, device_map="auto", |
| 29 | ) |
| 30 | tokenizer = AutoTokenizer.from_pretrained(args.model) |
| 31 | if tokenizer.pad_token is None: |
| 32 | tokenizer.pad_token = tokenizer.eos_token |
| 33 | |
| 34 | # ---- Step 2: Calibrate routers (skip if already exists) ---- |
| 35 | import os |
| 36 | if not os.path.exists(args.router_path): |
| 37 | print(f"Calibrating routers ({args.calibration_samples} samples)...") |
| 38 | config = TIDEConfig(calibration_samples=args.calibration_samples) |
| 39 | calibrate(model, tokenizer, config=config, save_path=args.router_path) |
| 40 | print(f"Saved to {args.router_path}") |
| 41 | else: |
| 42 | print(f"Using existing routers: {args.router_path}") |
| 43 | |
| 44 | # ---- Step 3: Wrap model with TIDE ---- |
| 45 | config = TIDEConfig(exit_threshold=args.threshold) |
| 46 | engine = TIDE(model, router_path=args.router_path, config=config) |
| 47 | |
| 48 | # ---- Step 4: Generate ---- |
| 49 | inputs = tokenizer(args.prompt, return_tensors="pt").to(model.device) |
| 50 | output = engine.generate(inputs.input_ids, max_new_tokens=args.max_tokens, temperature=0) |
| 51 | text = tokenizer.decode(output[0], skip_special_tokens=True) |
| 52 | |
| 53 | print(f"\n{'='*60}") |
| 54 | print(text) |
| 55 | print(f"{'='*60}") |
| 56 | print(f"\n{engine.last_stats.summary()}") |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | main() |
no test coverage detected