Test TIDE inference on a model.
(
model_name: str = "meta-llama/Llama-3.1-8B-Instruct",
prompt: str = "Explain quantum computing in simple terms:",
max_new_tokens: int = 256,
)
| 54 | timeout=3600, |
| 55 | ) |
| 56 | def test_inference( |
| 57 | model_name: str = "meta-llama/Llama-3.1-8B-Instruct", |
| 58 | prompt: str = "Explain quantum computing in simple terms:", |
| 59 | max_new_tokens: int = 256, |
| 60 | ): |
| 61 | """Test TIDE inference on a model.""" |
| 62 | import torch |
| 63 | from transformers import AutoModelForCausalLM, AutoTokenizer |
| 64 | from TIDE.config import TIDEConfig |
| 65 | from TIDE.runtime import TIDERuntime |
| 66 | |
| 67 | model = AutoModelForCausalLM.from_pretrained( |
| 68 | model_name, |
| 69 | torch_dtype=torch.float16, |
| 70 | device_map="auto", |
| 71 | cache_dir="/root/models", |
| 72 | ) |
| 73 | tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir="/root/models") |
| 74 | |
| 75 | safe_name = model_name.replace("/", "_") |
| 76 | router_path = f"/root/routers/{safe_name}_router.pt" |
| 77 | |
| 78 | runtime = TIDERuntime(model, router_path, config=TIDEConfig()) |
| 79 | |
| 80 | inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
| 81 | output = runtime.generate(inputs.input_ids, max_new_tokens=max_new_tokens, temperature=0) |
| 82 | text = tokenizer.decode(output[0], skip_special_tokens=True) |
| 83 | |
| 84 | return { |
| 85 | "output": text, |
| 86 | "stats": runtime.last_stats.summary() if runtime.last_stats else "N/A", |
| 87 | } |
nothing calls this directly
no test coverage detected