Instructions to use LLM-course/chess-Sunxt25 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LLM-course/chess-Sunxt25 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="LLM-course/chess-Sunxt25")# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("LLM-course/chess-Sunxt25", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use LLM-course/chess-Sunxt25 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "LLM-course/chess-Sunxt25" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LLM-course/chess-Sunxt25", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/LLM-course/chess-Sunxt25
- SGLang
How to use LLM-course/chess-Sunxt25 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "LLM-course/chess-Sunxt25" \ --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": "LLM-course/chess-Sunxt25", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
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 "LLM-course/chess-Sunxt25" \ --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": "LLM-course/chess-Sunxt25", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use LLM-course/chess-Sunxt25 with Docker Model Runner:
docker model run hf.co/LLM-course/chess-Sunxt25
Upload chess_tokenizer_custom.py
Browse files- chess_tokenizer_custom.py +128 -0
chess_tokenizer_custom.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from typing import Dict, List, Optional
|
| 5 |
+
# from transformers import PreTrainedTokenizer
|
| 6 |
+
from transformers import AutoTokenizer, PreTrainedTokenizer
|
| 7 |
+
|
| 8 |
+
class ChessTokenizer(PreTrainedTokenizer):
|
| 9 |
+
"""
|
| 10 |
+
符合评估脚本要求的 Chess Tokenizer。
|
| 11 |
+
1. 词表大小为 144 (4 special + 12 pieces + 64 from_sq + 64 to_sq)。
|
| 12 |
+
2. Decode 结果为紧凑格式(如 "WPe2e4"),确保 evaluate.py 的切片 [2:4] 和 [4:6] 正确。
|
| 13 |
+
3. 区分起始格和目标格语义。
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
model_input_names = ["input_ids", "attention_mask"]
|
| 17 |
+
vocab_files_names = {"vocab_file": "vocab.json"}
|
| 18 |
+
|
| 19 |
+
PAD_TOKEN = "[PAD]"
|
| 20 |
+
BOS_TOKEN = "[BOS]"
|
| 21 |
+
EOS_TOKEN = "[EOS]"
|
| 22 |
+
UNK_TOKEN = "[UNK]"
|
| 23 |
+
|
| 24 |
+
def __init__(self, vocab_file: Optional[str] = None, vocab: Optional[Dict[str, int]] = None, **kwargs):
|
| 25 |
+
special_tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
|
| 26 |
+
|
| 27 |
+
# 必须使用大写,以匹配 evaluate.py 生成的棋谱
|
| 28 |
+
self.colors_pieces = [f'{c}{p}' for c in ['W','B'] for p in ['P','N','B','R','Q','K']] # 12个
|
| 29 |
+
self.squares = [f'{f}{r}' for r in '12345678' for f in 'abcdefgh'] # 64个
|
| 30 |
+
|
| 31 |
+
if vocab is not None:
|
| 32 |
+
self._vocab = vocab
|
| 33 |
+
elif vocab_file is not None and os.path.exists(vocab_file):
|
| 34 |
+
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 35 |
+
self._vocab = json.load(f)
|
| 36 |
+
else:
|
| 37 |
+
# 构建 144 大小的词表
|
| 38 |
+
self._vocab = {t: i for i, t in enumerate(special_tokens)} # 0-3
|
| 39 |
+
|
| 40 |
+
# 4-15: Piece tokens
|
| 41 |
+
for cp in self.colors_pieces:
|
| 42 |
+
self._vocab[cp] = len(self._vocab)
|
| 43 |
+
|
| 44 |
+
# 16-79: From Square tokens (内部带后缀防止重名)
|
| 45 |
+
for sq in self.squares:
|
| 46 |
+
self._vocab[f"{sq}_f"] = len(self._vocab)
|
| 47 |
+
|
| 48 |
+
# 80-143: To Square tokens
|
| 49 |
+
for sq in self.squares:
|
| 50 |
+
self._vocab[f"{sq}_t"] = len(self._vocab)
|
| 51 |
+
|
| 52 |
+
self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
|
| 53 |
+
|
| 54 |
+
super().__init__(
|
| 55 |
+
pad_token=self.PAD_TOKEN,
|
| 56 |
+
bos_token=self.BOS_TOKEN,
|
| 57 |
+
eos_token=self.EOS_TOKEN,
|
| 58 |
+
unk_token=self.UNK_TOKEN,
|
| 59 |
+
**kwargs,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
@property
|
| 63 |
+
def vocab_size(self) -> int:
|
| 64 |
+
return len(self._vocab)
|
| 65 |
+
|
| 66 |
+
def get_vocab(self) -> Dict[str, int]:
|
| 67 |
+
return dict(self._vocab)
|
| 68 |
+
|
| 69 |
+
def _tokenize(self, text: str) -> List[str]:
|
| 70 |
+
"""将 WPe2e4 拆分为三个 token"""
|
| 71 |
+
tokens = []
|
| 72 |
+
# 处理可能的空格分隔(如历史棋谱)
|
| 73 |
+
moves = text.strip().split()
|
| 74 |
+
for move in moves:
|
| 75 |
+
# 过滤特殊 token 字符串
|
| 76 |
+
if move in [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]:
|
| 77 |
+
tokens.append(move)
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
if len(move) >= 6:
|
| 81 |
+
cp = move[:2] # 例如 "WP"
|
| 82 |
+
from_sq = move[2:4] + "_f" # 例如 "e2_f"
|
| 83 |
+
to_sq = move[4:6] + "_t" # 例如 "e4_t"
|
| 84 |
+
tokens.extend([cp, from_sq, to_sq])
|
| 85 |
+
return tokens
|
| 86 |
+
|
| 87 |
+
def _convert_token_to_id(self, token: str) -> int:
|
| 88 |
+
return self._vocab.get(token, self._vocab[self.UNK_TOKEN])
|
| 89 |
+
|
| 90 |
+
def _convert_id_to_token(self, index: int) -> str:
|
| 91 |
+
token = self._ids_to_tokens.get(index, self.UNK_TOKEN)
|
| 92 |
+
# 关键:在 decode 时去掉内部后缀,还原为 "e2", "e4"
|
| 93 |
+
return token.replace("_f", "").replace("_t", "")
|
| 94 |
+
|
| 95 |
+
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
| 96 |
+
"""
|
| 97 |
+
将 token 列表合并。
|
| 98 |
+
evaluate.py 要求输出如 "WPe2e4",因此这里不加空格。
|
| 99 |
+
"""
|
| 100 |
+
# 过滤特殊 token,只保留棋步内容
|
| 101 |
+
clean_tokens = [t for t in tokens if t not in self.all_special_tokens]
|
| 102 |
+
return "".join(clean_tokens)
|
| 103 |
+
|
| 104 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
|
| 105 |
+
if not os.path.isdir(save_directory):
|
| 106 |
+
os.makedirs(save_directory, exist_ok=True)
|
| 107 |
+
vocab_file = os.path.join(
|
| 108 |
+
save_directory,
|
| 109 |
+
(filename_prefix + "-" if filename_prefix else "") + "vocab.json"
|
| 110 |
+
)
|
| 111 |
+
with open(vocab_file, "w", encoding="utf-8") as f:
|
| 112 |
+
json.dump(self._vocab, f, ensure_ascii=False, indent=2)
|
| 113 |
+
return (vocab_file,)
|
| 114 |
+
|
| 115 |
+
@classmethod
|
| 116 |
+
def from_pretrained(cls, pretrained_model_name_or_path, **kwargs) -> "ChessTokenizer":
|
| 117 |
+
vocab_file = os.path.join(pretrained_model_name_or_path, "vocab.json")
|
| 118 |
+
if not os.path.exists(vocab_file):
|
| 119 |
+
return cls() # 如果没有文件则初始化默认的
|
| 120 |
+
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 121 |
+
vocab = json.load(f)
|
| 122 |
+
return cls(vocab=vocab, **kwargs)
|
| 123 |
+
|
| 124 |
+
# 在文件最末尾
|
| 125 |
+
try:
|
| 126 |
+
AutoTokenizer.register(ChessTokenizer, slow_tokenizer_class=ChessTokenizer)
|
| 127 |
+
except Exception:
|
| 128 |
+
pass
|