Extensions¶
This page describes forms that only some specs use. Each section covers one case, and each section can be read on its own. The fields most specs need are in Spec fields.
1. Argument types: optional inputs, nullable outputs and omittable parameters¶
A call-time input declares its argument type and default, as a construction parameter does. type lists the members the argument may hold. FusedMoESharedExpert is an example:
inputs:
correction_bias: {type: "Tensor | None", default: null, dtype: float32, shape: "[E]"}
shared_w_gate_up: {type: "Tensor | None", default: null, dtype: D, shape: "[2 * S, H]"}
shared_w_down: {type: "Tensor | None", default: null, dtype: D, shape: "[H, S]"}
outputs:
shared_output: {dtype: D, shape: "[T, H]", nullable: "present(shared_w_gate_up)"}
routed_output: {dtype: D, shape: "[T, H]"}
shape_rules:
- "present(shared_w_down) == present(shared_w_gate_up)"
Table 1 Members of an argument type
| No. | Member | Value it takes | Read as, on a branch narrowed to it |
|---|---|---|---|
| 1 | Tensor |
a tensor | the declaration's Tensor[T, s] |
| 2 | list[Tensor] |
a list or tuple of tensors | a tensor sequence, see § 11 |
| 3 | int |
an int that is not a bool |
Int, a call-time scalar |
| 4 | float |
a float; also an int (not a bool) when the type has no int or Number member |
a value, a call-time scalar |
| 5 | Number |
an int or a float |
a value, a call-time scalar |
| 6 | bool |
a bool |
Bool, a call-time scalar |
| 7 | None |
None |
None |
The rules for argument types:
typedefaults toTensor, which stands for the declaration'sTensor[dtype, shape].Numberalready holdsintandfloat, so it is never listed together with them.- An argument with a
Tensororlist[Tensor]member is a call-time input, declared ininputs; every other argument is a construction parameter, declared inparams. A tensor is never a construction parameter. - A value takes a member by its Python type:
NonetakesNone; a list or tuple takeslist[Tensor]; a tensor takesTensor; abooltakes onlybool; aninttakesint, elseNumber, elsefloat; afloattakesfloat, elseNumber. A value that no member takes is rejected. - An input without
defaultis passed on every call. An input withdefaultmay be omitted;forwardtakes thedefaultas its default, and omitting the input has the same effect as passing that value. - Among positional inputs, an input without
defaultnever follows one withdefault. Akw_onlyinput follows every positional one, and may be required. - A required argument that may be
Nonedeclares theNonemember and nodefault, for examplebias: {type: "Tensor | None", dtype: Wb, shape: "[N]"}. member(x)is the name of the memberxholds, such as'Tensor'or'Number', and each argument with several members is one discriminant with finitely many values.present(x)meansmember(x) != 'None', whether the caller passedxor itsdefaultapplied.member(x) == '<m>'narrowsxto that member,member(x) != '<m>'excludes it, andpresent(x)excludesNone. Narrowing composes throughnot,and,orand conditional expressions.- The tensor declarations
shape,dtype,contiguous,device,values,requiresand effects apply only on the branches where the argument holds a tensor member. An index used only by them is not used elsewhere: in the example above, whenshared_w_gate_upis not passed,Sis not used, and the corresponding workload row does not giveS.
Inputs that must be passed together or omitted together each declare type: "Tensor | None", default: null, and a refinement that reads only discriminants ties them, as present(shared_w_down) == present(shared_w_gate_up) does above. Such a refinement is a domain restriction, see § 2.
An output that may be None is declared with nullable: "<expression>". In the example above, the op returns shared_output when shared_w_gate_up is passed, and that output is None otherwise. The expression may consist only of discriminants with finitely many values: Bool and enum parameters, ADT tags and fields with finitely many values, present(...) and member(...).
The kind of an int | None parameter is Maybe[Int]. present(v) says whether it is given, and on a branch where present(v) holds, v itself is its value, for example R: "rotary_dim if present(rotary_dim) else D" in a let.
Call-time scalars. An argument narrowed to a scalar member is a call-time scalar. RoPECosSin's seqlen_offsets is one offset for every sequence or a tensor of one offset per sequence:
inputs:
seqlen_offsets: {type: "int | Tensor", default: 0, dtype: O, shape: "[B]"}
shape_rules:
- "member(seqlen_offsets) != 'int' or 0 <= seqlen_offsets <= P - L"
- A call-time scalar's value is read only in refinements and in
letentries; aletthat reads one, directly or through anotherlet, is likewise read only in refinements and in suchletentries. - Shapes, dtypes, type families,
nullableandmutatedconditions,values,requiresand roofline formulas never read a call-time scalar's value, and the validator rejects a spec that does. A value therefore decides no output shape and no branch. Construction parameters are not restricted this way. - The refinements and
letentries that read a call-time scalar run on every call. The Op plan is keyed by the member each argument holds and by a scalar's type, never by its value.
Workload rows.
somelists the arguments with several members that pass their tensor member in the row. Every input passed is listed, including inputs passed together: passing the shared expert is writtensome: [shared_w_gate_up, shared_w_down].- Any other argument with several members takes the row's scalar value or
null, else itsdefault. For example, the Clamp row{A: [4096, 4096], min: -0.5, max: 0.5, ...}passes both bounds as numbers. A required argument that may beNonewritesnullto passNone. - For an
implementedop, every member of every argument with several members that a domain restriction accepts is taken by at least one row.
Operator schema. For an op with a compile boundary, each input becomes one or more slots of the generated torch.library operator:
Table 2 Operator slots of an input
| No. | Member | Slot type | Slot name |
|---|---|---|---|
| 1 | Tensor, the only member |
Tensor |
<name> |
| 2 | list[Tensor] |
Tensor[] |
<name> |
| 3 | Tensor, among several members |
Tensor? |
<name> |
| 4 | int |
SymInt? |
<name>__int |
| 5 | float |
float? |
<name>__float |
| 6 | Number |
Scalar? |
<name>__number |
| 7 | bool |
bool? |
<name>__bool |
| 8 | None |
no slot | — |
- An argument with one member takes one slot under its own name. An argument with several members takes one nullable slot per member other than
None. - A call passes at most one slot of an argument, and passes none only when
Noneis a member; the operator and its fake function both check this. - A slot name equal to another argument's name or to an execution parameter's name is rejected.
2. Shapes that vary with parameters: type family¶
When a parameter determines the rank of a tensor or the order of its axes, the shape is described with a type family. A type family is defined in the spec's signature.types:
signature:
types:
Mat:
params: {t: Bool, R: Dim, C: Dim}
match: t
cases:
- {when: false, is: "[R, C]"}
- {when: true, is: "[C, R]"}
inputs:
a: {dtype: T, shape: "Mat[trans_a, M, K]"} # arguments map to Mat's t, R, C in order
- A tensor's
shapeis written<Family>[argument, ...], and the arguments map toparamsin declaration order. - The subject of
matchmust be a discriminant with finitely many values:Bool, an enum, an ADT,present(...),member(...), a boolean expression of these such asmember(min) == 'Tensor', or a tuple of these. When matching a tuple,whenis written as a list, for examplewhen: [true, false]. - A
Maybe[ADT]discriminant matches by its value; only_matches it when it is absent. casesmust have no gaps and no overlaps over all values the spec accepts.- When several tensors apply the same type family, they always take the same branch.
- References between type families must not form a cycle, and the validator reports an error if a type family is not used by any shape.
Clamp is an example of matching on argument members. min and max each hold a number, a tensor or None, and only a tensor bound takes part in broadcasting the output:
types:
ClampOut:
params: {A: Shape, L: Shape, U: Shape, tl: Bool, tu: Bool}
match: [tl, tu]
cases:
- {when: [true, true], is: "[*broadcast(A, L, U)]"}
- {when: [true, false], is: "[*broadcast(A, L)]"}
- {when: [false, true], is: "[*broadcast(A, U)]"}
- {when: [false, false], is: "[*A]"}
inputs:
input: {dtype: T, shape: "[*A]"}
min: {type: "Number | Tensor | None", default: null, dtype: T, shape: "[*L]"}
max: {type: "Number | Tensor | None", default: null, dtype: T, shape: "[*U]"}
outputs:
output: {dtype: T, shape: "ClampOut[A, L, U, member(min) == 'Tensor', member(max) == 'Tensor']"}
A refinement that reads only discriminants is called a domain restriction. A domain restriction is checked before a type family branch is selected, and values it excludes need no corresponding case. MoEPrePermute is an example:
types:
LayoutMetadata:
params: {layout: MGroupedLayout, E: Dim, P: Dim}
match: layout
cases:
- {when: {contiguous: {metadata_kind: physical_psum}}, is: "[E]"}
- {when: {contiguous: {metadata_kind: per_row}}, is: "[P]"}
shape_rules:
- "layout.kind == 'contiguous'"
layout.kind == 'contiguous' reads only the discriminant layout and excludes the masked layout, so the two cases already cover every value MoEPrePermute accepts. Whether a refinement reads only discriminants is decided by all the names that appear in it, independent of the order in which the operands are written. Clamp's present(min) or present(max), which requires at least one bound, is also a domain restriction.
3. Parameters with fields: ADT¶
When a parameter has a finite number of forms and each form carries its own fields, the parameter's type is described with an ADT. ADTs are defined in spec/types.yaml and shared by several specs:
adts:
MGroupedLayout:
sum:
contiguous:
python: tileops._ops.moe.contracts.ContiguousLayoutSpec
fields:
packing: {type: "'tight' | 'aligned'", python: tileops._ops.moe.contracts.ContiguousPacking}
metadata_kind: {type: "'physical_psum' | 'per_row'", python: tileops._ops.moe.contracts.ContiguousMetadata}
alignment: Dim
invariant: "(packing == 'tight') == (alignment == 1) and alignment >= 1"
masked:
python: tileops._ops.moe.contracts.MaskedLayoutSpec
fields: {max_m: Dim}
- Each constructor corresponds to a Python class, given by
python. The object'skindattribute is the constructor name, each field is an attribute of the same name, and the value of an enum field is the attribute's.value. - An ADT value is written
{constructor: {field: value}}, and workload rows use the same form. invariantis an optional refinement on a constructor, checked both at instantiation and at construction.- A type family can pattern-match on constructors:
{masked: _}matches any masked value, and{contiguous: {metadata_kind: per_row}}also constrains a field with finitely many values. A field specific to a constructor can be read only in a branch that matched that constructor, for examplelayout.max_min the masked branch. - ADTs are sealed: constructors and fields are fixed where the ADT is defined. Adding a constructor to a shared ADT is done by editing its definition directly; a spec that does not accept the new constructor can exclude it with a domain restriction, without changing its type families.
4. Computed quantities: let and primitive¶
When a shape or formula needs a quantity computed from indices, the quantity is defined as a let. MaxPool2d is an example:
let:
kH: "per_axis(kernel_size, 0, 2)"
sH: "per_axis(stride, 0, 2, fallback=kH)"
H_out: "pool.out(H_in, kH, sH, pH, dH, ceil_mode)"
outputs:
output: {dtype: T, shape: "[N, C, H_out, W_out]"}
- The value of a
letis computed from the signature: aletthat can be evaluated at construction is computed at construction, and the others are computed at call time. Workload rows do not giveletentries. - A
letcan refer to otherletentries, assHrefers tokH, but dependencies betweenletentries must not form a cycle.
A primitive is a built-in function callable in expressions, such as broadcast, reduced, per_axis and ceil_div.
- Primitives, generators and the predicates in
requiresare each a fixed set. The full list is intileops.manifest.primitives, and the docstring of each member describes what it computes. - Members used by only one family carry a family prefix, such as
pool.outandmoe.capacity. - When an argument is outside its domain, the primitive raises an error that points to the declaration that called it.
- Adding a member requires changing
tileops.manifest.primitives, with tests. - Every primitive that takes an axis argument handles axes by the same rule: for a zero-dimensional tensor, both
0and-1denote the single scalar axis; otherwise, the axis ranges over[-rank, rank).
5. Memory layout and device¶
- Every tensor is a call-time input. A list of values outside the type system is a construction parameter read only through its length; for example, LongRoPE's
rescale_factorsis alist[float] | Noneparameter, checked bynot present(rescale_factors) or len(rescale_factors) == D // 2. - A tensor that must be contiguous in memory declares
contiguous: true, for example the inputs and outputs of the MoE staged ops. Calls are supported with contiguous inputs (API Stability); a non-contiguous tensor without this declaration is not promised to work. - A tensor that must be on the CPU declares
device: cpu, for examplecu_seqlens_cpuinGDNFwdOp. - An op without call-time tensor inputs declares a
deviceparameter, for example Alibi. How the call device is determined is in Calls and validation § 4.
6. Writes to arguments: effect¶
An op without effect declarations only reads its inputs and allocates new tensors for its outputs. If an op writes to an argument, an effect is declared on the corresponding tensor in the signature. Effects determine the read and write counts of the roofline, and the operator schema generated for an op with a compile boundary.
Table 3 Effect declarations
| No. | Declaration | Meaning | Example |
|---|---|---|---|
| 1 | buffer: out on an output |
forward gets a keyword-only parameter out, default None, after all inputs that do not declare kw_only. When the caller passes out, the op writes the result into it and returns it; otherwise the op allocates a new tensor. out has the same shape and dtype as that output |
output of MoEGroupedGEMM |
| 2 | mutated: true on an input |
the op may write this input, and its contents before the call take part in the computation | |
| 3 | mutated: true and write_only: true on an input |
a result buffer that must be passed: the op overwrites it, and the result depends only on the other inputs; if the op returns None, outputs is empty |
no current op |
| 4 | mutated: "<discriminant expression>" on an input |
the op writes this input only when the expression is true | mutated: inplace of activation functions |
| 5 | alias: <input name> on an output |
when that input is written, this output is that input object itself | alias: input of activation functions |
| 6 | input out with mutated: true, write_only: true and kw_only: true |
the required output buffer of an op that cannot allocate its output: keyword-only without a default, last among the inputs; an output declares alias: out |
out of PagedKVCacheGather |
| 7 | overlaps: [<input>, ...] on a written input or a buffer: out output |
the written tensor may share bytes with the inputs listed | output of MoEExpertMLPFwdOp |
What a call returns follows from these declarations:
- an op given
outwrites into it and returns that object; - an op with several outputs returns a tuple with one slot per declared output, in declaration order, and an absent
nullableoutput isNonein its slot; - an op whose only effect is writing its inputs declares no outputs and returns
None.
A tensor a call writes, out or a written input, shares no byte with another call tensor unless it lists that input in overlaps; a call that breaks this raises CheckError before anything runs. The check compares each tensor's byte span, from its first element to the end of its last, so two interleaved views of one buffer overlap. An entry lists an input only when every implementation finishes reading it before writing the tensor, such as a composite op whose earlier stage alone reads it.
The inplace parameter of activation functions is an example:
params:
inplace: {type: bool, default: false, kw_only: true}
inputs:
input: {dtype: T, shape: "[*S]", mutated: inplace}
outputs:
output: {dtype: T, shape: "[*S]", alias: input}
Effect declarations must also satisfy the following rules, and the validator rejects specs that violate them:
- an effect is declared only on an input without a scalar member, and never on a tensor sequence;
write_only: truecan be used only together withmutated: true;aliasmust point to an input that is written;- an output that declares
aliascannot also declarebuffer; - at most one output declares
buffer: out; overlapssits on a written input or abuffer: outoutput, and names other inputs, each once.
For each effect branch, the validator checks that the operator schema, the aliasing and the roofline read and write counts agree.
7. Metadata tensors: generator and requires¶
Varlen, paged and similar ops use metadata tensors such as cu_seqlens and block_table. The type of a metadata tensor is written in the signature, and its values are generated at instantiation by a generator written in the tensor's values field. MeanPooling is an example:
forall: {B: Dim, S: Dim, H: Dim, D: Dim, NS: Dim, NC: Dim, T: "DType[...]", seq_lens: "Seq[Int]"}
inputs:
cu_seqlens: {dtype: int32, type: "Tensor | None", default: null, shape: "[NS + 1]",
values: "prefix_sum(seq_lens)", requires: ["prefix_offsets(S)"]}
chunk_indices: {dtype: int32, type: "Tensor | None", default: null, shape: "[NC, 2]",
values: "chunk_indices(seq_lens, chunk_size)"}
shape_rules:
- "present(chunk_indices) == present(cu_seqlens)"
The rules for generators:
- The arguments of a generator are given by the workload row, for example the list of lengths
seq_lens. A name of kindSeq[Int]inforallcan be used only as a generator argument. ASeq[Int]in a workload row can be written as a list of integers or as a call to a value primitive that returns a list, for exampleseq_lens: "repeat(512, 64)". - At instantiation, the generator's result is unified with the declared shape, which solves the other indices in the shape, so they are not given in the row. In the example above,
NSandNCare solved from the generatedcu_seqlensandchunk_indicesrespectively. In an actual call, these indices are likewise solved from the input tensors. - A generator is either deterministic or uses private random numbers derived from the workload seed, so the same row generates the same values every time.
- The rank of each generator result is fixed, or determined by its shape arguments.
- A generated tensor declares an integer dtype (
int32orint64). When an argument is outside its domain, or the result is outside the range of the declared dtype, the generator raises an error. - A generator argument can be a primitive that returns a list, for example
as_tensor(balanced_sizes(M, G))in GroupedGEMM.
The rules for requires:
requireslists predicates that constrain the contents of a metadata tensor. For example,prefix_offsets(S)requires the tensor's first element to be 0, the elements to be non-decreasing, and the last element to beS. The contents of the constrained tensor are the predicate's implicit first argument.- Each predicate reads the constrained tensor at a fixed rank; predicates that give elementwise bounds (such as
in_range) can apply to tensors of any rank. - A predicate argument can be another metadata tensor, so that one predicate constrains the relation between two tensors. For example,
batch_offsetsin GroupedGEMM declaresrequires: ["exclusive_prefix_of(batch_sizes)"], which requires it to be the exclusive prefix sum ofbatch_sizes. On every branch where the constrained tensor exists, the tensor used as the argument must also exist. - The validator checks
requiresat instantiation against the generated values. In an actual call, the caller guarantees these constraints and the op does not check tensor contents by default; the validator therefore also checks that the predicate is well defined on every branch where the constrained tensor exists. - A tensor that declares
requiresmust also declarevalues. - An op with an input whose
requiresholdsprefix_offsetsdeclaresvalidate_inputs: {type: bool, default: false, kw_only: true}inparams. An op whose inputs'requiresholdin_rangebut noprefix_offsetsmay declare it too. - When the caller sets
validate_inputs, a check generated from the manifest reads the inputs on the CPU before the implementation runs and checks theirprefix_offsetsandin_rangepredicates. Every other predicate stays the caller's obligation. - The check synchronizes the device, so it cannot run inside CUDA Graph capture.
8. dtype combinations and packed dtypes¶
When several DType indices allow only specific combinations, the spec lists every allowed combination in dtype_combos. Paged GQA is an example:
forall: {..., T: "DType[float16 | bfloat16 | float8_e4m3fn]", KV: "DType[float16 | bfloat16 | float8_e4m3fn]"}
dtype_combos:
- {T: float16, KV: float16}
- {T: bfloat16, KV: bfloat16}
- {T: float16, KV: float8_e4m3fn}
- {T: bfloat16, KV: float8_e4m3fn}
- {T: float8_e4m3fn, KV: float8_e4m3fn}
- Each row is a mapping from index to dtype. All rows have the same keys, the keys may include dtype parameters, and the rows are all distinct.
- The dtype values of a call must equal one of the rows exactly.
- Every column of
dtype_combosmust be used on every branch the spec accepts.
Packed dtypes such as fp4 and int4 are stored in a carrier dtype such as uint8, and the spec is written in terms of the carrier:
dtypeis the carrier dtype, andshapeis the carrier shape as PyTorch sees it, for examplepacked_weight: "[N, K // 2]"in GemmW4A16;- the logical dtype is given by a dtype parameter, or fixed in the spec;
- the roofline counts bytes by the carrier.
9. Scalar parameters whose value range depends on dtype¶
The valid values of some scalar parameters depend on the dtype at call time, for example alpha in Add. Such constraints are written as refinements with the primitives category and representable:
params:
alpha: {type: int | float, default: 1, kw_only: true}
shape_rules:
- "category(alpha) == 'int' or category(alpha) == category(T)"
- "representable(alpha, T)"
The generated call checks apply the same rule to TileOPs in-tree implementations and to implementations provided by a target.
10. Composite ops: composition¶
A composite op uses composition to record the sub-ops its in-tree implementation may hold, and the positions of its own kernels. FusedMoESharedExpert is an example:
composition:
kind: composite
stages:
- {name: route_select, op: FusedTopKFwdOp}
- {name: routed_experts, op: FusedMoEExpertsFwdOp}
- {name: shared_expert, op: SharedExpertMLPFwdOp, optional: true}
- Each stage refers either to an op in the manifest (
op) or to a key in the op's ownkernel_types(kernel). - A sub-op that is not held on every call is written as a stage with
optional: true; whether it is held is decided by code. - When and how many times sub-ops are constructed, how they are scheduled, and how forward executes are all decided by code.
- The manifest does not prescribe how the roofline of a composite op relates to the rooflines of its stages.
- For an
implementedop, the validator checks in order that theopstages agree with the class'sdelegate_typesand thekernelstages agree with the class'skernel_types. stagescannot be empty, stage names are all distinct, andoptionalis a boolean.
11. Tensor sequences¶
An argument that holds a list of tensors declares type: "list[Tensor]". Its element-wise indices have the kinds Seq[Dim], Seq[Shape] and Seq[DType[...]]. Concat is an example:
forall: {S: "Seq[Shape]", D: "Seq[DType[float16 | bfloat16 | float32]]"}
params:
dim: {type: int, default: 0}
inputs:
tensors: {type: "list[Tensor]", dtype: D, shape: "[*S]"}
outputs:
output: {dtype: "promote(D)", shape: "[*concat(S, dim)]"}
shape_rules:
- "len(S) >= 1"
workloads:
- {S: [[2048, 1024], [2048, 4096], [2048, 3000]], dim: 1, dtype_cases: [{D: [bfloat16, bfloat16, bfloat16]}], label: uneven-cols}
- In the
shapeanddtypeof alist[Tensor]declaration, every element-wise index stands for the element's item: elementihas shapeS[i]and dtypeD[i], andlen(S)there islen(S[i]). Every other index is shared by all elements. - Outside the declaration, an element-wise index is the whole list, readable in outputs, refinements,
letentries and roofline formulas. Aletis not expanded into the element. Seq[Dim]holds one axis length per element, while aShapeis one tuple of axes shared by all elements.- The sequence length is the common length of its element-wise indices, and a
list[Tensor]declaration reads at least one element-wise index. - Element-wise indices are solved on every element. A shared index is solved from the other tensors first; only when inference stalls is it solved from element 0, then checked on every element.
- An empty sequence gives its element-wise indices empty lists; a shared index that only the sequence could solve stays unsolved, and the call raises an error that names the declaration.
- A tensor sequence is passed on every call and is only read. It declares no effect,
values,requiresordevice, and is not a member of a union. - Choosing the call device, the 16-byte alignment copy and the alias and overlap checks visit each element. The derived byte count counts each element once, and
bytes(t)is the sum over the elements. - A workload row writes an element-wise index as a list with one item per element. A
Seq[DType]index is written indtype_casesas a list of dtypes, and its value in the case id joins them with., for exampleuneven-cols-bfloat16.bfloat16.bfloat16. - A
list[Tensor]output,Noneelements in a sequence,list[Tensor]as a member of a union, and an effect on a sequence are all rejected.