import argparse import torch from model import DiChatForCausalLM, ModelConfig from tokenizer import DiChatTokenizer def main(): ap = argparse.ArgumentParser() ap.add_argument("--prompt", default="Привет!") ap.add_argument("--max-new-tokens", type=int, default=160) args = ap.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") ckpt = torch.load("model.pt", map_location=device, weights_only=False) cfg = ModelConfig(**ckpt["model_config"]) cfg.gradient_checkpointing = False model = DiChatForCausalLM(cfg).to(device) model.load_state_dict(ckpt["model"]) model.eval() tok = DiChatTokenizer("dichat.model") end = tok.token_id("<|end|>") ids = [tok.bos_id, tok.token_id("<|user|>"), *tok.encode(args.prompt), end, tok.token_id("<|assistant|>")] x = torch.tensor([ids], device=device) with torch.inference_mode(): out = model.generate(x, max_new_tokens=args.max_new_tokens, temperature=0.0, top_k=0, eos_token_id=end, repetition_penalty=1.08) generated = out[0, len(ids):].tolist() if generated and generated[-1] == end: generated = generated[:-1] print(tok.decode(generated).strip()) if __name__ == "__main__": main()