Susu / run_chat.py
SOYORiN's picture
Release Susu — Super small unit research checkpoint
a071dd2 verified
Raw History Blame Contribute Delete
2.11 kB
"""Susu standalone inference. No teacher/API fallback."""
import argparse
import json
from pathlib import Path
import torch
from safetensors.torch import load_file
from model import Config, MicroLM
from tokenizer import ChatTokenizer, BOS, USER, ASSISTANT, EOS
def main():
p = argparse.ArgumentParser()
p.add_argument('--prompt')
p.add_argument('--device', default='cpu')
p.add_argument('--seed', type=int, default=928371)
a = p.parse_args()
root = Path(__file__).resolve().parent
torch.set_num_threads(3)
torch.manual_seed(a.seed)
model = MicroLM(Config(**json.loads((root/'config.json').read_text('utf-8'))))
model.load_state_dict(load_file(str(root/'model.safetensors')))
model.to(a.device).eval()
tok = ChatTokenizer(root/'tokenizer_meta.json')
history = []
print('Susu — Super small unit | research checkpoint; chat quality not established')
while True:
user = a.prompt if a.prompt is not None else input('You> ')
if user.strip() in ['/exit', '/quit']:
break
if user.strip() == '/reset':
history.clear()
continue
if not user.strip():
continue
current = [USER] + tok.encode(user) + [EOS, ASSISTANT]
if len(current) + 1 + 48 > 512:
print('Message too long for the trained 512-token context.')
if a.prompt is not None:
break
continue
while history and 1 + sum(map(len, history)) + len(current) + 48 > 512:
history.pop(0)
ids = torch.tensor([[BOS] + sum(history, []) + current], device=a.device)
with torch.autocast('cuda', dtype=torch.bfloat16, enabled=a.device.startswith('cuda')):
generated, stopped = model.generate(ids, max_new_tokens=48, temperature=.7,
top_p=.9, forbidden_ids=(0, 1, 3, 4, 5), active_vocab=tok.size)
print('Susu> ' + tok.decode(generated))
history.append(current + generated + ([] if stopped else [EOS]))
if a.prompt is not None:
break
if __name__ == '__main__':
main()