Instructions to use MDaytek/chess-submission-v9_MDaytek with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MDaytek/chess-submission-v9_MDaytek with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="MDaytek/chess-submission-v9_MDaytek", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("MDaytek/chess-submission-v9_MDaytek", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use MDaytek/chess-submission-v9_MDaytek with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "MDaytek/chess-submission-v9_MDaytek" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MDaytek/chess-submission-v9_MDaytek", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/MDaytek/chess-submission-v9_MDaytek
- SGLang
How to use MDaytek/chess-submission-v9_MDaytek 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 "MDaytek/chess-submission-v9_MDaytek" \ --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": "MDaytek/chess-submission-v9_MDaytek", "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 "MDaytek/chess-submission-v9_MDaytek" \ --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": "MDaytek/chess-submission-v9_MDaytek", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use MDaytek/chess-submission-v9_MDaytek with Docker Model Runner:
docker model run hf.co/MDaytek/chess-submission-v9_MDaytek
V9 Lightning Upload
Browse files- README.md +11 -0
- config.json +18 -14
- model.py +72 -0
- model.safetensors +1 -1
- tokenizer.py +33 -0
README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags:
|
| 4 |
+
- chess
|
| 5 |
+
- llm-course
|
| 6 |
+
- chess-challenge
|
| 7 |
+
license: mit
|
| 8 |
+
---
|
| 9 |
+
# Chess Submission V9
|
| 10 |
+
**User:** MDaytek
|
| 11 |
+
**Model:** Transformer from Scratch
|
config.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
| 1 |
{
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ChessForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"dtype": "float32",
|
| 6 |
+
"model_type": "chess_lm",
|
| 7 |
+
"n_ctx": 256,
|
| 8 |
+
"n_embd": 96,
|
| 9 |
+
"n_head": 8,
|
| 10 |
+
"n_layer": 8,
|
| 11 |
+
"n_positions": 256,
|
| 12 |
+
"tie_word_embeddings": false,
|
| 13 |
+
"transformers_version": "4.57.6",
|
| 14 |
+
"vocab_size": 354,
|
| 15 |
+
"auto_map": {
|
| 16 |
+
"AutoConfig": "model.ChessConfig",
|
| 17 |
+
"AutoModelForCausalLM": "model.ChessForCausalLM"
|
| 18 |
+
}
|
| 19 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 4 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 5 |
+
|
| 6 |
+
class ChessConfig(PretrainedConfig):
|
| 7 |
+
model_type = "chess_lm"
|
| 8 |
+
|
| 9 |
+
def __init__(
|
| 10 |
+
self,
|
| 11 |
+
vocab_size=354,
|
| 12 |
+
n_positions=256,
|
| 13 |
+
n_embd=96,
|
| 14 |
+
n_layer=8,
|
| 15 |
+
n_head=8,
|
| 16 |
+
tie_word_embeddings=False,
|
| 17 |
+
**kwargs
|
| 18 |
+
):
|
| 19 |
+
super().__init__(**kwargs)
|
| 20 |
+
self.vocab_size = vocab_size
|
| 21 |
+
self.n_positions = n_positions
|
| 22 |
+
self.n_embd = n_embd
|
| 23 |
+
self.n_layer = n_layer
|
| 24 |
+
self.n_head = n_head
|
| 25 |
+
self.tie_word_embeddings = tie_word_embeddings
|
| 26 |
+
|
| 27 |
+
class ChessForCausalLM(PreTrainedModel):
|
| 28 |
+
config_class = ChessConfig
|
| 29 |
+
|
| 30 |
+
def __init__(self, config):
|
| 31 |
+
super().__init__(config)
|
| 32 |
+
|
| 33 |
+
self.token_emb = nn.Embedding(config.vocab_size, config.n_embd)
|
| 34 |
+
self.pos_emb = nn.Embedding(config.n_positions, config.n_embd)
|
| 35 |
+
|
| 36 |
+
encoder_layer = nn.TransformerEncoderLayer(
|
| 37 |
+
d_model=config.n_embd,
|
| 38 |
+
nhead=config.n_head,
|
| 39 |
+
dim_feedforward=4 * config.n_embd,
|
| 40 |
+
batch_first=True,
|
| 41 |
+
norm_first=True
|
| 42 |
+
)
|
| 43 |
+
self.transformer = nn.TransformerEncoder(encoder_layer, config.n_layer)
|
| 44 |
+
|
| 45 |
+
self.ln_f = nn.LayerNorm(config.n_embd)
|
| 46 |
+
self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
|
| 47 |
+
|
| 48 |
+
if config.tie_word_embeddings:
|
| 49 |
+
self.lm_head.weight = self.token_emb.weight
|
| 50 |
+
|
| 51 |
+
self.post_init()
|
| 52 |
+
|
| 53 |
+
def forward(self, input_ids, labels=None):
|
| 54 |
+
B, T = input_ids.shape
|
| 55 |
+
pos = torch.arange(T, device=input_ids.device)
|
| 56 |
+
|
| 57 |
+
x = self.token_emb(input_ids) + self.pos_emb(pos)
|
| 58 |
+
mask = torch.triu(torch.ones(T, T, device=x.device), diagonal=1).bool()
|
| 59 |
+
|
| 60 |
+
x = self.transformer(x, mask=mask)
|
| 61 |
+
x = self.ln_f(x)
|
| 62 |
+
logits = self.lm_head(x)
|
| 63 |
+
|
| 64 |
+
loss = None
|
| 65 |
+
if labels is not None:
|
| 66 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 67 |
+
loss = loss_fct(
|
| 68 |
+
logits[:, :-1].reshape(-1, logits.size(-1)),
|
| 69 |
+
labels[:, 1:].reshape(-1)
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return CausalLMOutputWithPast(loss=loss, logits=logits)
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 3960136
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d91cc00c285c8b97c03a50223cab9ab54ff39937492306dcc88ed8b6c75f793b
|
| 3 |
size 3960136
|
tokenizer.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, os, torch
|
| 2 |
+
|
| 3 |
+
class ChessTokenizer:
|
| 4 |
+
def __init__(self, vocab):
|
| 5 |
+
self.vocab = vocab
|
| 6 |
+
self.id_to_token = {v: k for k, v in vocab.items()}
|
| 7 |
+
self.pad_token_id = vocab["[PAD]"]
|
| 8 |
+
self.bos_token_id = vocab["[BOS]"]
|
| 9 |
+
self.eos_token_id = vocab["[EOS]"]
|
| 10 |
+
self.unk_token_id = vocab["[UNK]"]
|
| 11 |
+
|
| 12 |
+
@property
|
| 13 |
+
def vocab_size(self):
|
| 14 |
+
return len(self.vocab)
|
| 15 |
+
|
| 16 |
+
def _convert_token_to_id(self, token):
|
| 17 |
+
return self.vocab.get(token, self.unk_token_id)
|
| 18 |
+
|
| 19 |
+
def pad(self, encoded_inputs, padding=True, max_length=None, pad_to_multiple_of=None, return_tensors=None, **kwargs):
|
| 20 |
+
|
| 21 |
+
max_len = max(len(x["input_ids"]) for x in encoded_inputs)
|
| 22 |
+
padded = [
|
| 23 |
+
x["input_ids"] + [self.pad_token_id] * (max_len - len(x["input_ids"]))
|
| 24 |
+
for x in encoded_inputs
|
| 25 |
+
]
|
| 26 |
+
if return_tensors == "pt":
|
| 27 |
+
return {"input_ids": torch.tensor(padded, dtype=torch.long)}
|
| 28 |
+
return {"input_ids": padded}
|
| 29 |
+
|
| 30 |
+
def save_pretrained(self, path):
|
| 31 |
+
os.makedirs(path, exist_ok=True)
|
| 32 |
+
with open(os.path.join(path, "vocab.json"), "w") as f:
|
| 33 |
+
json.dump(self.vocab, f, indent=2)
|