跳转至

Pooling 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(...).

Average pooling

tileops.pool.AvgPool1dFwdOp

Average pooling over PyTorch-compatible NCL inputs.

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    ceil_mode=False,
    count_include_pad=True,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int]) –

    Window extent, an int or a one-element tuple.

  • stride (int | tuple[int] | None, default: None ) –

    Window step, an int or a one-element tuple; None steps by kernel_size.

  • padding (int | tuple[int], default: 0 ) –

    Implicit zero padding on both sides, an int or a one-element tuple.

  • ceil_mode (bool, default: False ) –

    Round the output extent up instead of down.

  • count_include_pad (bool, default: True ) –

    Count padded positions in a window's divisor.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled averages, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

tileops.pool.AvgPool2dFwdOp

Average pooling over PyTorch-compatible NCHW inputs.

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    ceil_mode=False,
    count_include_pad=True,
    divisor_override=None,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit zero padding on both sides, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • count_include_pad (bool, default: True ) –

    Count padded positions in a window's divisor.

  • divisor_override (int | None, default: None ) –

    Divide every window sum by this instead; None divides by the window's element count.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled averages, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

tileops.pool.AvgPool3dFwdOp

Average pooling over PyTorch-compatible NCDHW inputs.

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    ceil_mode=False,
    count_include_pad=True,
    divisor_override=None,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit zero padding on both sides, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • count_include_pad (bool, default: True ) –

    Count padded positions in a window's divisor.

  • divisor_override (int | None, default: None ) –

    Divide every window sum by this instead; None divides by the window's element count.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled averages, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

Max pooling

tileops.pool.MaxPool1dFwdOp

Max pooling over PyTorch-compatible NCL inputs (return_indices=False).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled maximums, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

tileops.pool.MaxPool1dIndicesFwdOp

Max pooling over PyTorch-compatible NCL inputs (return_indices=True).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input and return where each maximum came from.

Parameters:

  • input (Tensor) –

    (N, C, L_in), float16, bfloat16 or float32.

Returns:

  • Tensor –

    output (N, C, L_out) in the input dtype and indices, int64 of the same

  • shape ( Tensor ) –

    the flat position of each maximum within its (n, c) plane.

tileops.pool.MaxPool2dFwdOp

Max pooling over PyTorch-compatible NCHW inputs (return_indices=False).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled maximums, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

tileops.pool.MaxPool2dIndicesFwdOp

Max pooling over PyTorch-compatible NCHW inputs (return_indices=True).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input and return where each maximum came from.

Parameters:

  • input (Tensor) –

    (N, C, H_in, W_in), float16, bfloat16 or float32.

Returns:

  • Tensor –

    output (N, C, H_out, W_out) in the input dtype and indices, int64 of the same

  • shape ( Tensor ) –

    the flat position of each maximum within its (n, c) plane.

tileops.pool.MaxPool3dFwdOp

Max pooling over PyTorch-compatible NCDHW inputs (return_indices=False).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([N \times C]\) followed by ndim spatial axes.

Returns:

  • Tensor –

    A new tensor of the pooled maximums, \([N \times C]\) followed by the ndim

  • Tensor –

    pooled extents, in the dtype of input.

tileops.pool.MaxPool3dIndicesFwdOp

Max pooling over PyTorch-compatible NCDHW inputs (return_indices=True).

__init__

__init__(
    kernel_size,
    stride=None,
    padding=0,
    dilation=1,
    ceil_mode=False,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • kernel_size (int | tuple[int, ...]) –

    Window extent, an int or one per spatial axis.

  • stride (int | tuple[int, ...] | None, default: None ) –

    Window step, an int or one per spatial axis; None steps by kernel_size.

  • padding (int | tuple[int, ...], default: 0 ) –

    Implicit -inf padding on both sides, an int or one per spatial axis, at most half the window.

  • dilation (int | tuple[int, ...], default: 1 ) –

    Spacing between window elements, an int or one per spatial axis.

  • ceil_mode (bool, default: False ) –

    Round each output extent up instead of down.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input and return where each maximum came from.

Parameters:

  • input (Tensor) –

    (N, C, D_in, H_in, W_in), float16, bfloat16 or float32.

Returns:

  • Tensor –

    output (N, C, D_out, H_out, W_out) in the input dtype and indices, int64 of the same

  • shape ( Tensor ) –

    the flat position of each maximum within its (n, c) plane.

Adaptive pooling

tileops.pool.AdaptiveAvgPool2dFwdOp

Adaptive average pooling over PyTorch-compatible CHW/NCHW inputs.

__init__

__init__(
    output_size,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • output_size (int | None | tuple[int | None, int | None] | list[int | None]) –

    Output (H_out, W_out), or one int for both; a None extent keeps the input's.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([C \times H \times W]\) or \([N \times C \times H \times W]\).

Returns:

  • Tensor –

    A new tensor with the leading axes of input and output_size as its last

  • Tensor –

    two extents, in the dtype of input.

tileops.pool.AdaptiveMaxPool2dFwdOp

Adaptive max pooling over CHW/NCHW inputs (return_indices=False).

__init__

__init__(
    output_size,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • output_size (int | None | tuple[int | None, int | None] | list[int | None]) –

    Output (H_out, W_out), or one int for both; a None extent keeps the input's.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input.

Parameters:

  • input (Tensor) –

    Input, \([C \times H \times W]\) or \([N \times C \times H \times W]\).

Returns:

  • Tensor –

    A new tensor with the leading axes of input and output_size as its last

  • Tensor –

    two extents, in the dtype of input.

tileops.pool.AdaptiveMaxPool2dIndicesFwdOp

Adaptive max pooling over CHW/NCHW inputs (return_indices=True).

__init__

__init__(
    output_size,
    *,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • output_size (int | None | tuple[int | None, int | None] | list[int | None]) –

    Output (H_out, W_out), or one int for both; a None extent keeps the input's.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    input,
)

Pool input and return where each maximum came from.

Parameters:

  • input (Tensor) –

    (C, H_in, W_in) or (N, C, H_in, W_in), float16 or bfloat16.

Returns:

  • Tensor –

    output with the leading axes of input and output_size as its last two

  • Tensor –

    extents, in the input dtype, and indices, int64 of the same shape: the flat

  • tuple[Tensor, Tensor] –

    position of each maximum within its (H_in, W_in) plane.

Chunked sequence mean

The one op on this page with no PyTorch counterpart. It averages the sequence axis of a [batch, seq, heads, dim] tensor in fixed-size chunks, and can follow ragged sequence boundaries rather than a uniform split.

tileops.pool.MeanPoolingFwdOp

Chunked mean over the sequence axis of a [batch, seq, heads, dim] tensor.

Not a PyTorch pooling op, and torch has no counterpart. The sequence axis is cut into chunks of chunk_size and each chunk is averaged, giving one output row per chunk. Pass cu_seqlens and chunk_indices and the chunks follow the ragged sequence boundaries cu_seqlens describes instead of a uniform split; chunk_indices then names the (sequence, chunk-within-sequence) pair each output row belongs to, one row per chunk.

A sequence's last chunk may be shorter than chunk_size. It is divided by the count it actually holds, so no padding is averaged in. On a uniform split that makes the op equal to torch.nn.functional.avg_pool1d(kernel_size=chunk_size, stride=chunk_size, ceil_mode=True) over a view with the sequence axis last. Chunk sums accumulate in accum_dtype and are cast back to the input dtype at the boundary, so a float16 input with accum_dtype=torch.float32 does not lose the sum to rounding.

By default the op does not check the contents of cu_seqlens and chunk_indices. The kernel indexes cu_seqlens by chunk_indices and x by cu_seqlens without bounds checks, so an inconsistent map can cause out-of-bounds accesses or incorrect results. The caller guarantees a consistent map. validate_inputs=True checks cu_seqlens on every call, at the cost of a device synchronization, and cannot run inside CUDA Graph capture; it does not check chunk_indices.

Example
1
2
3
op = MeanPoolingFwdOp(chunk_size=64, accum_dtype=torch.float32)
chunk_means = op(x)  # uniform split
chunk_means = op(x, cu_seqlens, chunk_indices)  # ragged, one index row per chunk

__init__

__init__(
    chunk_size,
    accum_dtype,
    *,
    validate_inputs=False,
    target=None
)

Build the op. Shapes and dtype are taken from the first call.

Parameters:

  • chunk_size (int) –

    Tokens per chunk, a positive multiple of 32.

  • accum_dtype (dtype) –

    The dtype a chunk sum accumulates in.

  • validate_inputs (bool, default: False ) –

    Check cu_seqlens synchronously on each call. Disable during CUDA Graph capture; shapes and dtypes are always checked.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    cu_seqlens=None,
    chunk_indices=None,
)

Average each chunk of x's sequence axis.

Parameters:

  • x (Tensor) –

    Input tensor, [batch, seq, heads, dim], dtype float16, bfloat16 or float32. dim is at most 128 or a multiple of it, which is what the manifest declares rather than what the kernel needs.

  • cu_seqlens (Tensor | None, default: None ) –

    Sequence boundaries, [seq_num + 1], dtype int32. Passing it selects the ragged split, and it comes with chunk_indices.

  • chunk_indices (Tensor | None, default: None ) –

    One (sequence, chunk-within-sequence) pair per output chunk, [chunks, 2], dtype int32.

Returns:

  • Tensor –

    output, [batch, chunks, heads, dim], dtype as x. chunks is

  • Tensor –

    ceil(seq / chunk_size) for a uniform split and chunk_indices.shape[0] for a

  • Tensor –

    ragged one.

Raises:

  • ValueError –

    With validate_inputs=True, cu_seqlens does not partition the sequence axis.