Download inference.py from DiChat-models/DiChat-Mini-10M-Base: direct link, hf CLI and curl.
- Browser
- Download file 964 Bytes
-
https://huggingface.co/DiChat-models/DiChat-Mini-10M-Base/resolve/main/inference.py
- Command line
-
hf download hf://DiChat-models/DiChat-Mini-10M-Base/inference.py
-
curl -L -o inference.py https://huggingface.co/DiChat-models/DiChat-Mini-10M-Base/resolve/main/inference.py
964 Bytes
| import torch | |
| from pathlib import Path | |
| from tokenizers import Tokenizer | |
| from model import Config, DiChat10M | |
| root=Path(__file__).resolve().parent | |
| ck=torch.load(root/"model.pt",map_location="cpu",weights_only=True) | |
| tokenizer=Tokenizer.from_file(str(root/"tokenizer.json")) | |
| model=DiChat10M(Config(**ck["config"])) | |
| model.load_state_dict(ck["model"]) | |
| device="cuda" if torch.cuda.is_available() else "cpu" | |
| model.to(device).eval() | |
| prompt=input("Prompt (Russian): ") | |
| ids=tokenizer.encode(prompt).ids | |
| for _ in range(120): | |
| x=torch.tensor([ids[-model.config.context:]],device=device) | |
| with torch.inference_mode(): logits=model(x)[:,-1,:]/0.8 | |
| probs=torch.softmax(logits,dim=-1) | |
| sorted_p,sorted_i=torch.sort(probs,descending=True) | |
| cutoff=(sorted_p.cumsum(-1)-sorted_p)>0.9 | |
| sorted_p[cutoff]=0 | |
| sorted_p/=sorted_p.sum() | |
| next_id=sorted_i[torch.multinomial(sorted_p,1)].item() | |
| ids.append(next_id) | |
| print(tokenizer.decode(ids,skip_special_tokens=True)) | |