smollm2-135m-tictactoe
A tiny chat model that plays tic-tac-toe. It is
SmolLM2-135M-Instruct
fine-tuned with LoRA and then merged back into the base weights, so it loads as a normal
transformers model with no PEFT dependency.
The model plays O. The human plays X and moves first. Each turn the model echoes the player's move, replies with its own, and redraws the board.
Usage
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = "Sexyparabola/smollm2-135m-tictactoe"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo)
msgs = [
{"role": "system", "content": "You are a tic-tac-toe game. The player is X and moves first; you are O."},
{"role": "user", "content": "hi"},
]
ids = tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt")
out = model.generate(ids, max_new_tokens=200, do_sample=False)
print(tok.decode(out[0][ids.shape[1]:], skip_special_tokens=True))
Greedy decoding is recommended. Keep the full conversation in msgs: each turn
appends the player's square number as a user message and the model's previous
reply as an assistant message.
Example exchange
Every board row is followed by a blank line, so markdown chat UIs show each row on its own line instead of merging them.
user: hi
assistant: Let's play tic-tac-toe! You are X and go first. Pick a square from 1 to 9.
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Your move (X):
user: 3
assistant: You play 3.
1 | 2 | X
4 | 5 | 6
7 | 8 | 9
I play 5 (center).
1 | 2 | X
4 | O | 6
7 | 8 | 9
Your move (X):
Replies are tagged with the reason for the move, such as (center), (corner)
and (block). The model announces the result when the game ends, and asks for
another square if the player picks one that is already taken.
Training
- Base model: HuggingFaceTB/SmolLM2-135M-Instruct
- Method: LoRA, rank 32, alpha 64, no dropout, on all attention and MLP
projections (
q/k/v/o/gate/up/down_proj), about 9.8M trainable parameters, merged after training - Data: synthetic conversations generated from games against a rule-based opponent. The data includes self-play games and exhaustive probes of positions, including blocking moves, taken squares and unclear input. Each game is rendered in several differently phrased conversation variants.
- Loss: only assistant tokens are trained. The tokens that decide the outcome (the chosen square, the move tag, the closing board block and the end token) are weighted 10x.
- Schedule: 747 steps of batch size 8 (one epoch over about 2,985 conversations), then retrained with the blank-line-per-row board layout: 187 steps at LR 1e-4, then 374 more steps at LR 1.5e-4, both continuing from the previous adapter.
Limitations
- It is a 135M-parameter model trained for one narrow task. It will not do general chat well.
- It only works with the system prompt and board format shown above. Other prompts or board layouts are out of distribution.
- It can still make mistakes, for example choosing a non-blocking move where a block is required. Do not rely on it as a perfect player.
License
Apache-2.0, inherited from the base model. This is a modified version of SmolLM2-135M-Instruct (LoRA fine-tuned and merged).