Views
No views yet
ChessPuzzlePipeline.__call__1pipeline(
2 themes: list[PuzzleTheme] | list[list[PuzzleTheme]] | None = None,
3 rating: float | list[float] = 1500.0,
4 partial_board: str | list[str] | None = None,
5 best_move: str | list[str] | None = None,
6 batch_size: int = 1,
7 steps: int = 256,
8 temperature: float = 1.0,
9 schedule: Schedule = Schedule.linear,
10 move_generation_order: MoveGenerationOrder = MoveGenerationOrder.simultaneous,
11) -> list[Position]themes (list[PuzzleTheme] | list[list[PuzzleTheme]], optional, default: None):PuzzleTheme enum members (e.g. [pipeline.Theme.mateIn2, pipeline.Theme.middlegame]). Can also be a list of theme lists per position when generating a batch (e.g. [[pipeline.Theme.fork], [pipeline.Theme.mateIn1]]).rating (float | list[float], optional, default: 1500.0):batch_size.partial_board (str | list[str], optional, default: None):? and empty squares with ..
Example: "?????rk./?????ppp/????????/????????/????????/???B????/????????/???????? w ??-- - ? ?"best_move (str | list[str], optional, default: None):"d3h7", "e7e8q", "e2??"). Can also contain ? for unknown characters.batch_size (int, optional, default: 1):steps (int, optional, default: 256):temperature (float, optional, default: 1.0):schedule (Schedule, optional, default: Schedule.linear):pipeline.Schedule enum:pipeline.Schedule.linearpipeline.Schedule.cosinepipeline.Schedule.geometricpipeline.Schedule.polynomialmove_generation_order (MoveGenerationOrder, optional, default: MoveGenerationOrder.simultaneous):MoveGenerationOrder.simultaneous: Board and move tokens are generated in a single phase.MoveGenerationOrder.first: Move tokens are generated first, then the board tokens.MoveGenerationOrder.last: Board tokens are generated first, then the move tokens.list[Position] of length batch_size, where each Position is a dataclass:1@dataclass
2class Position:
3 fen: str
4 move: str | None1position = results[0]
2print(position.fen) # "2nrb3/n2k2qp/1ppp4/4p3/5P2/R7/1PPBP1PP/R4NK1 w - - 0 22"
3print(position.move) # "a3a7"pipeline.Theme.<name> and passed as a list of theme enum objects:| Category | Available Themes |
|---|---|
| State-of-game | opening, middlegame, endgame |
| Type-of-endgame | pawnEndgame, bishopEndgame, knightEndgame, rookEndgame, queenEndgame, queenRookEndgame |
| Type-of-checkmate | mate, backRankMate, bodenMate, smotheredMate, hookMate, doubleBishopMate, arabianMate, dovetailMate, anastasiaMate, triangleMate, balestraMate, killBoxMate, blindSwineMate, cornerMate, vukovicMate |
| Length-of-checkmate | mateIn1, mateIn2, mateIn3, mateIn4, mateIn5 |
| Length-of-puzzle | oneMove, short, long, veryLong |
| Winning | crushing, advantage |
| Other | hangingPiece, fork, interference, kingsideAttack, zugzwang, exposedKing, skewer, pin, quietMove, discoveredAttack, sacrifice, deflection, advancedPawn, attraction, promotion, queensideAttack, defensiveMove, attackingF2F7, clearance, intermezzo, equality, trappedPiece, xRayAttack, capturingDefender, doubleCheck, enPassant, castling, underPromotion, master, masterVsMaster, superGM |
1import torch
2from diffusers import DiffusionPipeline
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5
6pipeline = DiffusionPipeline.from_pretrained(
7 "naapeli/chess-puzzle-generator",
8 trust_remote_code=True,
9)
10pipeline.to(device)
11
12# For exactly the same model as in the paper, use revision="paper":
13# pipeline = DiffusionPipeline.from_pretrained(
14# "naapeli/chess-puzzle-generator",
15# revision="paper",
16# trust_remote_code=True,
17# )
18# pipeline.to(device)
19
20themes = pipeline.Theme
21schedules = pipeline.Schedule
22move_generation_orders = pipeline.MoveGenerationOrder
23
24# 1. Unconditional generation conditioned on themes and rating
25results = pipeline(
26 themes=[themes.mateIn2, themes.middlegame],
27 rating=1800,
28 batch_size=1,
29 steps=64,
30 schedule=schedules.linear,
31 move_generation_order=move_generation_orders.first
32)
33print(results[0].fen, results[0].move)
34
35# 2. Condition on a partial board and a best move
36partial_fen = "?????rk?/?????ppp/????????/????????/????????/???B????/????????/???????? w ??-- - ? ?"
37best_move = "d3h7"
38results = pipeline(
39 themes=[themes.mate],
40 rating=1600,
41 partial_board=partial_fen,
42 best_move=best_move,
43 batch_size=1,
44 steps=256,
45 schedule=schedules.cosine,
46)
47print(results[0].fen, results[0].move)
48
49# 3. Per-position conditioning variables in batch generation
50results = pipeline(
51 themes=[themes.middlegame, themes.long, themes.sacrifice], # same themes
52 best_move=["???3", "???4", "???1"], # unique best_moves (not guaranteed, but used for conditioning)
53 rating=2000, # same ratings
54 batch_size=3,
55 steps=16,
56)
57for i, pos in enumerate(results, start=1):
58 print(f"Batch {i}: {pos.fen} | move: {pos.move}")