Download generate.py from SparkCodeAI/Spark-2.0-code: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/SparkCodeAI/Spark-2.0-code/resolve/6f9d714aeaea4579edacc10086dce0136773ce1f/generate.py
- Command line
-
hf download hf://SparkCodeAI/Spark-2.0-code@6f9d714aeaea4579edacc10086dce0136773ce1f/generate.py
-
curl -L -o generate.py https://huggingface.co/SparkCodeAI/Spark-2.0-code/resolve/6f9d714aeaea4579edacc10086dce0136773ce1f/generate.py
1.9 kB
| """ | |
| Spark 2.0-code: пример запуска. python generate.py "Write a function that checks if a number is prime." | |
| Нужна видеокарта NVIDIA (CUDA) и пакеты: torch, flash-linear-attention (fla), triton, tokenizers, safetensors. | |
| Формат запроса такой же, как при обучении: ### User\n<задание>\n\n### Assistant\n | |
| Генерация жадная (temperature 0): один и тот же запрос всегда даёт один и тот же ответ. | |
| """ | |
| import json, sys | |
| from pathlib import Path | |
| import torch | |
| from safetensors.torch import load_file | |
| from tokenizers import Tokenizer | |
| from spark.model import HybridLM, ModelConfig | |
| from spark.decode import Cache, GraphedStep, generate | |
| HERE = Path(__file__).resolve().parent | |
| TEMPLATE = "### User\n{content}\n\n### Assistant\n" | |
| def load(device="cuda"): | |
| cfg = ModelConfig(**json.loads((HERE / "config.json").read_text())) | |
| model = HybridLM(cfg) | |
| sd = load_file(str(HERE / "model.safetensors")) | |
| sd["lm_head.weight"] = sd["embed.weight"] # веса общие (tie_embeddings) | |
| model.load_state_dict(sd) | |
| model = model.to(device, torch.bfloat16).eval() | |
| tok = Tokenizer.from_file(str(HERE / "tokenizer.json")) | |
| return model, tok | |
| def ask(model, tok, task, max_new=1500): | |
| eos = tok.token_to_id("<|endoftext|>") | |
| cache = Cache(model, max_len=4096) | |
| graphed = GraphedStep(model, cache) | |
| ids = torch.tensor([tok.encode(TEMPLATE.format(content=task.strip())).ids], device="cuda") | |
| out = [] | |
| with torch.no_grad(): | |
| generate(model, cache, graphed, ids, max_new, 0.0, 1.0, eos, 1.0, on_token=out.append) | |
| return tok.decode(out) | |
| if __name__ == "__main__": | |
| task = " ".join(sys.argv[1:]) or "Write a Python function that checks whether a number is prime." | |
| model, tok = load() | |
| print(ask(model, tok, task)) | |