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
Delete chess_tokenizer_custom.py
Browse files- chess_tokenizer_custom.py +0 -159
chess_tokenizer_custom.py
DELETED
|
@@ -1,159 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
-
from typing import Dict, List, Optional
|
| 5 |
-
from transformers import PreTrainedTokenizer
|
| 6 |
-
import torch
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class ChessTokenizer(PreTrainedTokenizer):
|
| 10 |
-
"""
|
| 11 |
-
符合评估脚本要求的 Chess Tokenizer。
|
| 12 |
-
1. 词表大小为 144 (4 special + 12 pieces + 64 from_sq + 64 to_sq)。
|
| 13 |
-
2. Decode 结果为紧凑格式(如 "WPe2e4"),确保 evaluate.py 的切片 [2:4] 和 [4:6] 正确。
|
| 14 |
-
3. 区分起始格和目标格语义。
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
model_input_names = ["input_ids", "attention_mask"]
|
| 18 |
-
vocab_files_names = {"vocab_file": "vocab.json"}
|
| 19 |
-
|
| 20 |
-
PAD_TOKEN = "[PAD]"
|
| 21 |
-
BOS_TOKEN = "[BOS]"
|
| 22 |
-
EOS_TOKEN = "[EOS]"
|
| 23 |
-
UNK_TOKEN = "[UNK]"
|
| 24 |
-
|
| 25 |
-
def __init__(self, vocab_file: Optional[str] = None, vocab: Optional[Dict[str, int]] = None, **kwargs):
|
| 26 |
-
special_tokens = [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]
|
| 27 |
-
|
| 28 |
-
# 必须使用大写,以匹配 evaluate.py 生成的棋谱
|
| 29 |
-
self.colors_pieces = [f'{c}{p}' for c in ['W','B'] for p in ['P','N','B','R','Q','K']] # 12个
|
| 30 |
-
self.squares = [f'{f}{r}' for r in '12345678' for f in 'abcdefgh'] # 64个
|
| 31 |
-
|
| 32 |
-
if vocab is not None:
|
| 33 |
-
self._vocab = vocab
|
| 34 |
-
elif vocab_file is not None and os.path.exists(vocab_file):
|
| 35 |
-
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 36 |
-
self._vocab = json.load(f)
|
| 37 |
-
else:
|
| 38 |
-
# 构建 144 大小的词表
|
| 39 |
-
self._vocab = {t: i for i, t in enumerate(special_tokens)} # 0-3
|
| 40 |
-
|
| 41 |
-
# 4-15: Piece tokens
|
| 42 |
-
for cp in self.colors_pieces:
|
| 43 |
-
self._vocab[cp] = len(self._vocab)
|
| 44 |
-
|
| 45 |
-
# 16-79: From Square tokens (内部带后缀防止重名)
|
| 46 |
-
for sq in self.squares:
|
| 47 |
-
self._vocab[f"{sq}_f"] = len(self._vocab)
|
| 48 |
-
|
| 49 |
-
# 80-143: To Square tokens
|
| 50 |
-
for sq in self.squares:
|
| 51 |
-
self._vocab[f"{sq}_t"] = len(self._vocab)
|
| 52 |
-
|
| 53 |
-
self._ids_to_tokens = {v: k for k, v in self._vocab.items()}
|
| 54 |
-
|
| 55 |
-
super().__init__(
|
| 56 |
-
pad_token=self.PAD_TOKEN,
|
| 57 |
-
bos_token=self.BOS_TOKEN,
|
| 58 |
-
eos_token=self.EOS_TOKEN,
|
| 59 |
-
unk_token=self.UNK_TOKEN,
|
| 60 |
-
**kwargs,
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
@property
|
| 64 |
-
def vocab_size(self) -> int:
|
| 65 |
-
return len(self._vocab)
|
| 66 |
-
|
| 67 |
-
def get_vocab(self) -> Dict[str, int]:
|
| 68 |
-
return dict(self._vocab)
|
| 69 |
-
|
| 70 |
-
def _tokenize(self, text: str) -> List[str]:
|
| 71 |
-
"""将 WPe2e4 拆分为三个 token"""
|
| 72 |
-
tokens = []
|
| 73 |
-
# 处理可能的空格分隔(如历史棋谱)
|
| 74 |
-
moves = text.strip().split()
|
| 75 |
-
for move in moves:
|
| 76 |
-
# 过滤特殊 token 字符串
|
| 77 |
-
if move in [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]:
|
| 78 |
-
tokens.append(move)
|
| 79 |
-
continue
|
| 80 |
-
|
| 81 |
-
if len(move) >= 6:
|
| 82 |
-
cp = move[:2] # 例如 "WP"
|
| 83 |
-
from_sq = move[2:4] + "_f" # 例如 "e2_f"
|
| 84 |
-
to_sq = move[4:6] + "_t" # 例如 "e4_t"
|
| 85 |
-
tokens.extend([cp, from_sq, to_sq])
|
| 86 |
-
return tokens
|
| 87 |
-
|
| 88 |
-
def _convert_token_to_id(self, token: str) -> int:
|
| 89 |
-
return self._vocab.get(token, self._vocab[self.UNK_TOKEN])
|
| 90 |
-
|
| 91 |
-
def _convert_id_to_token(self, index: int) -> str:
|
| 92 |
-
token = self._ids_to_tokens.get(index, self.UNK_TOKEN)
|
| 93 |
-
# 如果是特殊 Token,返回空字符串,避免干扰 decode 结果
|
| 94 |
-
if token in [self.PAD_TOKEN, self.BOS_TOKEN, self.EOS_TOKEN, self.UNK_TOKEN]:
|
| 95 |
-
return ""
|
| 96 |
-
# 去掉内部后缀
|
| 97 |
-
return token.replace("_f", "").replace("_t", "")
|
| 98 |
-
|
| 99 |
-
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
| 100 |
-
"""
|
| 101 |
-
核心修复:确保拼接结果符合 evaluate.py 的 6 位切片要求
|
| 102 |
-
"""
|
| 103 |
-
# 1. 过滤掉 None 或空字符串
|
| 104 |
-
clean_tokens = [t for t in tokens if t and t.strip()]
|
| 105 |
-
|
| 106 |
-
# 2. 拼接原始字符
|
| 107 |
-
raw_res = "".join(clean_tokens)
|
| 108 |
-
|
| 109 |
-
# 3. 逻辑补全:
|
| 110 |
-
# 老师的脚本期待的是 [Piece(2)][From(2)][To(2)]
|
| 111 |
-
# 如果当前已经凑够了 3 个组件(比如 WP, e2, e4),raw_res 长度就是 6
|
| 112 |
-
# 如果只凑了 2 个组件(比如 WP, e2),长度是 4
|
| 113 |
-
|
| 114 |
-
# 特别注意:如果 tokens 只有 1 个且长度 >= 6(说明是一次性生成的全量 move)
|
| 115 |
-
if len(raw_res) >= 6:
|
| 116 |
-
# 这种情况下直接返回,满足 if len(token_str) >= 6: break
|
| 117 |
-
return raw_res
|
| 118 |
-
|
| 119 |
-
return raw_res
|
| 120 |
-
|
| 121 |
-
def decode(self, token_ids, skip_special_tokens=True, **kwargs) -> str:
|
| 122 |
-
"""
|
| 123 |
-
覆盖父类的 decode,增加对老师脚本的长度伪装
|
| 124 |
-
"""
|
| 125 |
-
# 将输入统一转为 list,防止 Tensor 报错
|
| 126 |
-
if hasattr(token_ids, "tolist"):
|
| 127 |
-
ids = token_ids.tolist()
|
| 128 |
-
elif isinstance(token_ids, (int, torch.LongTensor, torch.IntTensor)):
|
| 129 |
-
ids = [int(token_ids)]
|
| 130 |
-
else:
|
| 131 |
-
ids = token_ids
|
| 132 |
-
|
| 133 |
-
# 将 ID 转回 token
|
| 134 |
-
tokens = [self._convert_id_to_token(i) for i in ids]
|
| 135 |
-
|
| 136 |
-
# 调用你写好的拼接逻辑
|
| 137 |
-
decoded_str = self.convert_tokens_to_string(tokens)
|
| 138 |
-
|
| 139 |
-
return decoded_str
|
| 140 |
-
|
| 141 |
-
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple:
|
| 142 |
-
if not os.path.isdir(save_directory):
|
| 143 |
-
os.makedirs(save_directory, exist_ok=True)
|
| 144 |
-
vocab_file = os.path.join(
|
| 145 |
-
save_directory,
|
| 146 |
-
(filename_prefix + "-" if filename_prefix else "") + "vocab.json"
|
| 147 |
-
)
|
| 148 |
-
with open(vocab_file, "w", encoding="utf-8") as f:
|
| 149 |
-
json.dump(self._vocab, f, ensure_ascii=False, indent=2)
|
| 150 |
-
return (vocab_file,)
|
| 151 |
-
|
| 152 |
-
@classmethod
|
| 153 |
-
def from_pretrained(cls, pretrained_model_name_or_path, **kwargs) -> "ChessTokenizer":
|
| 154 |
-
vocab_file = os.path.join(pretrained_model_name_or_path, "vocab.json")
|
| 155 |
-
if not os.path.exists(vocab_file):
|
| 156 |
-
return cls() # 如果没有文件则初始化默认的
|
| 157 |
-
with open(vocab_file, "r", encoding="utf-8") as f:
|
| 158 |
-
vocab = json.load(f)
|
| 159 |
-
return cls(vocab=vocab, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|