File size: 4,885 Bytes
0f775e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""TaskSpec — everything that varies between kernel-generation tasks.

Scaling this lane to ~150 kernels means writing ONLY the parts that are genuinely per-kernel. Everything
else (Dockerfile, measure.py, the grader with its anti-cheat, test.sh, task.toml, RUN.md, the precision +
faithfulness policy, the grading-transparency section) is generated from these fields by `build.py`.

A new task is therefore: a reference implementation, an input generator, a FLOP/byte formula, shape lists,
and a few paragraphs of prose. Everything in this file is data; nothing here runs on the GPU.
"""
from dataclasses import dataclass, field


@dataclass
class TaskSpec:
    # ---- identity -------------------------------------------------------------------------------
    name: str                       # directory + task name, e.g. "kda-forward"
    title: str                      # instruction.md H1, e.g. "Write a fast Kimi Delta Attention ... kernel"
    blurb: str                      # one-paragraph what/why, used in task.toml + RUN.md
    keywords: list[str] = field(default_factory=list)

    # ---- the graded entry point ------------------------------------------------------------------
    module: str = ""                # file the agent edits, e.g. "kda.py"
    func: str = ""                  # function the grader imports, e.g. "kda_forward"
    signature: str = ""             # e.g. "kda_forward(q, k, v, g, beta, scale=None)"
    returns_doc: str = ""           # markdown describing the return contract

    # ---- code the generator embeds verbatim ------------------------------------------------------
    reference_src: str = ""         # the reference implementation (also embedded into the grader)
    reference_imports: str = ""     # imports the reference needs, e.g. "import torch\nfrom einops import ..."
    make_inputs_src: str = ""       # def _mk(*shape, seed) -> tuple of tensors
    flops_src: str = ""             # def canonical_work(*shape) -> int   (or bytes, for the GB/s metric)
    flops_formula: str = ""         # OPTIONAL: the formula PRINTED in instruction.md. If left empty it is
                                    # scraped from flops_src's last `return`, which requires canonical_work
                                    # to come last and end in a single-expression return. Set this
                                    # explicitly whenever the work count uses helpers or several statements,
                                    # otherwise the instruction can show a partial/meaningless formula.

    # ---- grading ---------------------------------------------------------------------------------
    metric: str = "TFLOP/s"         # "TFLOP/s" | "GB/s" | "tokens/s"
    compare: str = "tensor"         # "tensor" | "tuple" | "rowwise"
    tol: float = 2e-2
    tuple_names: tuple = ()         # for compare="tuple", e.g. ("dq","dk","dv","dg","dbeta")
    row_pass: float = 0.98          # for compare="rowwise"
    grader_shapes: list = field(default_factory=list)
    measure_shapes: list = field(default_factory=list)
    measure_quick_shapes: list = field(default_factory=list)
    correct_shapes: list = field(default_factory=list)
    shape_names: tuple = ()         # for pretty-printing, e.g. ("B","T","H","K","V")

    # ---- prose (markdown, dropped into instruction.md) --------------------------------------------
    spec_md: str = ""               # "## The computation (this is the exact spec)" body
    contract_md: str = ""           # the fixed-contract table + notes
    regime_md: str = ""             # the graded shape regime paragraph
    perf_md: str = ""               # "## Where the performance comes from" body
    precision_md: str = ""          # task-specific opening of the precision section
    correctness_md: str = ""        # task-specific wording of the correctness gate

    # ---- environment ------------------------------------------------------------------------------
    base_image: str = "pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel"   # torch 2.11 + triton 3.6
    pip_extra: str = "einops nvidia-cutlass-dsl"
    gpus: int = 1
    agent_timeout_sec: float = 14400.0
    verifier_timeout_sec: float = 1800.0
    memory_mb: int = 65536

    def validate(self):
        assert self.name and self.module and self.func, "name/module/func are required"
        assert self.compare in ("tensor", "tuple", "rowwise"), self.compare
        assert self.metric in ("TFLOP/s", "GB/s", "tokens/s"), self.metric
        assert self.grader_shapes and self.correct_shapes, "shape lists are required"
        if self.compare == "tuple":
            assert self.tuple_names, "compare='tuple' needs tuple_names"
        for s in self.grader_shapes + self.correct_shapes + self.measure_shapes:
            assert len(s) == len(self.shape_names), f"shape {s} vs names {self.shape_names}"
        return self