yp-edu/stockfish-debug
Viewer • Updated • 32.1M • 119 • 2
How to use yp-edu/gpt2-stockfish-debug with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="yp-edu/gpt2-stockfish-debug") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("yp-edu/gpt2-stockfish-debug")
model = AutoModelForCausalLM.from_pretrained("yp-edu/gpt2-stockfish-debug")How to use yp-edu/gpt2-stockfish-debug with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "yp-edu/gpt2-stockfish-debug"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "yp-edu/gpt2-stockfish-debug",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/yp-edu/gpt2-stockfish-debug
How to use yp-edu/gpt2-stockfish-debug with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "yp-edu/gpt2-stockfish-debug" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "yp-edu/gpt2-stockfish-debug",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "yp-edu/gpt2-stockfish-debug" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "yp-edu/gpt2-stockfish-debug",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use yp-edu/gpt2-stockfish-debug with Docker Model Runner:
docker model run hf.co/yp-edu/gpt2-stockfish-debug
See my blog post for additional details.
The model was trained during 1 epoch on the yp-edu/stockfish-debug dataset (no hyperparameter tuning done). The samples are:
{"prompt":"FEN: {fen}\nMOVE:", "completion": " {move}"}
Two possible simple extensions:
r2qk3/... -> r11qk111/... or equivalent{"prompt":"RES: {res}\nFEN: {fen}\nMOVE:", "completion": " {move}"}
The following code requires python-chess (in addition to transformers) which you can install using pip install python-chess.
import chess
from transformers import AutoModelForCausalLM, AutoTokenizer
def next_move(model, tokenizer, fen):
input_ids = tokenizer(f"FEN: {fen}\nMOVE:", return_tensors="pt")
input_ids = {k: v.to(model.device) for k, v in input_ids.items()}
out = model.generate(
**input_ids,
max_new_tokens=10,
pad_token_id=tokenizer.eos_token_id,
do_sample=True,
temperature=0.1,
)
out_str = tokenizer.batch_decode(out)[0]
return out_str.split("MOVE:")[-1].replace("<|endoftext|>", "").strip()
board = chess.Board()
model = AutoModelForCausalLM.from_pretrained("yp-edu/gpt2-stockfish-debug")
tokenizer = AutoTokenizer.from_pretrained("yp-edu/gpt2-stockfish-debug") # or "gpt2"
tokenizer.pad_token = tokenizer.eos_token
for i in range(100):
fen = board.fen()
move_uci = next_move(model, tokenizer, fen)
try:
print(move_uci)
move = chess.Move.from_uci(move_uci)
if move not in board.legal_moves:
raise chess.IllegalMoveError
board.push(move)
outcome = board.outcome()
if outcome is not None:
print(board)
print(outcome.result())
break
except chess.IllegalMoveError:
print(board)
print("Illegal move", i)
break
else:
print(board)