Reduction Operators¶
Every op on this page is used the same way: construct it once, then call it. The
constructor takes what the kernel is compiled with; the call takes the tensors.
Both are documented under each op — __init__ and forward, where forward is
what runs when you call op(...).
Sums, means and extrema¶
tileops.reduction.SumFwdOp
¶
Sum over dim, following torch.sum.
__init__
¶
Construct the op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the reduction, and the output's;
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.MeanFwdOp
¶
Mean over dim, following torch.mean; an empty reduction is NaN.
__init__
¶
Construct the op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the reduction, and the output's;
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.ProdFwdOp
¶
Product over one axis, following torch.prod(input, dim, keepdim, *, dtype).
__init__
¶
Construct ProdFwdOp.
Parameters:
-
dim(int) –The axis to reduce.
-
keepdim(bool, default:False) –Whether the reduced axis stays as a length-1 axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the reduction, and the output's;
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.AmaxFwdOp
¶
Maximum over dim, following torch.amax.
__init__
¶
Construct a reduce op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.AminFwdOp
¶
Minimum over dim, following torch.amin.
__init__
¶
Construct a reduce op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
Variance and deviation¶
tileops.reduction.VarFwdOp
¶
Variance over dim, following torch.var.
__init__
¶
Construct a variance-family op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
correction(float | None, default:1) –Difference between the sample size and the degrees of freedom;
Nonemeans 1, as in torch. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.VarMeanFwdOp
¶
Variance and mean over dim, following torch.var_mean.
__init__
¶
Construct a variance-family op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
correction(float | None, default:1) –Difference between the sample size and the degrees of freedom;
Nonemeans 1, as in torch. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.StdFwdOp
¶
Standard deviation over dim, following torch.std.
__init__
¶
Construct a variance-family op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
correction(float | None, default:1) –Difference between the sample size and the degrees of freedom;
Nonemeans 1, as in torch. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
Arg reductions¶
tileops.reduction.ArgmaxFwdOp
¶
Index of the maximum along dim, following torch.argmax; returns int64.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | None, default:None) –Reduction axis.
None(the default) returns the index into the flattened input, astorch.argmax(x)does. -
keepdim(bool, default:False) –Whether to retain the reduced dimension as size 1.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.ArgminFwdOp
¶
Index of the minimum along dim, following torch.argmin; returns int64.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | None, default:None) –Reduction axis.
None(the default) returns the index into the flattened input, astorch.argmax(x)does. -
keepdim(bool, default:False) –Whether to retain the reduced dimension as size 1.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
Softmax¶
tileops.reduction.SoftmaxFwdOp
¶
Softmax along dim, following torch.nn.functional.softmax.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | None, default:None) –The axis to normalize along.
Nonetakes torch's implicit axis (0at rank 0, 1 or 3, else1); the in-tree implementation warns, as torch does. -
dtype(dtype | None, default:None) –The dtype the input is cast to first, and the output's;
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Normalize x along the configured axis.
Parameters:
-
x(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–A tensor of x's shape, in
dtypewhen one was passed.
tileops.reduction.LogSoftmaxFwdOp
¶
Log-softmax along dim, following torch.nn.functional.log_softmax.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | None, default:None) –The axis to normalize along.
Nonetakes torch's implicit axis (0at rank 0, 1 or 3, else1); the in-tree implementation warns, as torch does. -
dtype(dtype | None, default:None) –The dtype the input is cast to first, and the output's;
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Log-normalize input along the configured axis.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–A tensor of input's shape, in
dtypewhen one was passed.
tileops.reduction.LogSumExpFwdOp
¶
LogSumExp over dim, following torch.logsumexp; an empty reduction is -inf.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | list[int] | tuple[int, ...]) –Axes to reduce: an
intor a non-empty sequence of them. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
Vector norms¶
One op serves every order torch.linalg.vector_norm is called at here: pass
ord as 1, 2 or inf.
tileops.reduction.VectorNormFwdOp
¶
torch.linalg.vector_norm(x, ord, dim, keepdim, dtype=dtype).
ord selects the fold: 1 sums magnitudes, 2 takes the root of the sum of
squares, and inf takes the largest magnitude. A row holding a NaN yields NaN at
ord=inf, as in torch.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
ord(int | float, default:2) –Norm order; 1, 2 or
inf. -
dim(int | list[int] | tuple[int, ...] | None, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the reduction, and the output's; it may not narrow the input's.
Nonekeeps the input's. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Take the norm of x over the configured axes.
Parameters:
-
x(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The norm, shaped by
dimandkeepdim.
Cumulative¶
tileops.reduction.CumsumFwdOp
¶
Cumulative sum operator: output = cumsum(input, dim).
Output has the shape of input, and its dtype or dtype when one is passed.
Alignment padding is handled inside the kernel via masked loads.
A row one thread block can stage in shared memory takes the whole-row scan, unless it
moves at least 64 KiB and there are no more rows than 4 KiB parts of one row; those
rows and the other rows with M < 128 and N > 8192 are cut into segments, scanned
in two launches that spread a few rows over the SMs; every other shape takes the
tiled scan.
Example
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int) –The axis to scan, required as in torch. Negative values count from the last axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the scan, and the output's;
Nonekeeps the input's, as in torch. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run the scan.
One call to the operator this op registers: this is as far as dynamo traces.
Parameters:
-
input(Tensor) –Tensor to scan along
dim.
Returns:
-
Tensor–A new tensor of the shape of
input, indtypewhen given, else in the -
Tensor–dtype of
input.
tileops.reduction.CumprodFwdOp
¶
Cumulative product operator: output = cumprod(input, dim).
Output has the shape of input, and its dtype or dtype when one is passed.
Alignment padding is handled inside the kernel via masked loads. Rows are dispatched
as in :class:CumsumFwdOp.
The products are accumulated in float32 and associated in parallel pieces of the row, as torch's CUDA scan also does. Zeros, NaN, inf and signs in the input reach the output as a serial scan carries them. An over- or underflow does too while every factor of the row is on one side of 1; where a partial product over- or underflows that the serial running product does not, or the reverse, the output may hold an inf, a zero or a NaN where a serial scan does not.
Example
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int) –The axis to scan, required as in torch. Negative values count from the last axis.
-
dtype(dtype | None, default:None) –The dtype the input is cast to before the scan, and the output's;
Nonekeeps the input's, as in torch. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Run the scan.
One call to the operator this op registers: this is as far as dynamo traces.
Parameters:
-
input(Tensor) –Tensor to scan along
dim.
Returns:
-
Tensor–A new tensor of the shape of
input, indtypewhen given, else in the -
Tensor–dtype of
input.
Logical reductions¶
tileops.reduction.AllFwdOp
¶
Whether every element along dim is nonzero, following torch.all; returns bool.
An empty dim reduces nothing: the output is x != 0 with the input's shape.
__init__
¶
Construct a reduce op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.AnyFwdOp
¶
Whether any element along dim is nonzero, following torch.any; returns bool.
An empty dim reduces nothing: the output is x != 0 with the input's shape.
__init__
¶
Construct a reduce op.
Parameters:
-
dim(Dim, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
keepdim(bool, default:False) –Whether a reduced axis stays as a length-1 axis.
-
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.
tileops.reduction.CountNonzeroFwdOp
¶
Count of nonzero elements along dim, following torch.count_nonzero; returns int64.
There is no keepdim: a reduced axis always goes, as in torch.
__init__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
dim(int | list[int] | tuple[int, ...] | None, default:None) –Axes to reduce: an
int, a sequence of them, orNonefor all. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
Reduce input over the configured axes.
Parameters:
-
input(Tensor) –Input tensor of any rank.
Returns:
-
Tensor–The reduction, shaped by
dimandkeepdim.