tilelang.language.reduce

The language interface for tl programs.

Functions

reduce(buffer, out, reduce_type, dim, clear)

Perform a reduction operation on a buffer along a specified dimension.

reduce_max(buffer, out[, dim, clear])

Perform reduce max on input buffer, store the result to output buffer

reduce_min(buffer, out[, dim, clear])

Perform reduce min on input buffer, store the result to output buffer.

reduce_sum(buffer, out[, dim, clear])

Perform reduce sum on input buffer, store the result to output buffer.

reduce_abssum(buffer, out[, dim])

Perform reduce absolute sum on input buffer, store the result to output buffer.

reduce_absmax(buffer, out[, dim, clear])

Perform reduce absolute max on input buffer, store the result to output buffer.

cumsum_fragment(src, dst, dim, reverse)

cumsum(src[, dst, dim, reverse])

Compute the cumulative sum of src along dim, writing results to dst.

finalize_reducer(reducer)

Finalize a reducer buffer by emitting the tl.finalize_reducer intrinsic.

Module Contents

tilelang.language.reduce.reduce(buffer, out, reduce_type, dim, clear)

Perform a reduction operation on a buffer along a specified dimension.

Parameters:
  • buffer (tir.Buffer) – Input buffer to reduce

  • out (tir.Buffer) – Output buffer to store results

  • reduce_type (str) – Type of reduction (‘max’, ‘min’, ‘sum’, ‘abssum’)

  • dim (int) – Dimension along which to perform reduction

  • clear (bool) – Whether to initialize the output buffer before reduction

Returns:

Handle to the reduction operation

Return type:

tir.Call

tilelang.language.reduce.reduce_max(buffer, out, dim=-1, clear=True)

Perform reduce max on input buffer, store the result to output buffer

Parameters:
  • buffer (Buffer) – The input buffer.

  • out (Buffer) – The output buffer.

  • dim (int) – The dimension to perform reduce on

  • clear (bool) – If set to True, the output buffer will first be initialized to -inf.

Returns:

handle

Return type:

PrimExpr

tilelang.language.reduce.reduce_min(buffer, out, dim=-1, clear=True)

Perform reduce min on input buffer, store the result to output buffer.

Parameters:
  • buffer (tir.Buffer) – The input buffer

  • out (tir.Buffer) – The output buffer

  • dim (int) – The dimension to perform reduce on

  • clear (bool, optional) – If True, output buffer will be initialized to inf. Defaults to True.

Returns:

Handle to the reduction operation

Return type:

tir.Call

tilelang.language.reduce.reduce_sum(buffer, out, dim=-1, clear=True)

Perform reduce sum on input buffer, store the result to output buffer.

Parameters:
  • buffer (tir.Buffer) – The input buffer

  • out (tir.Buffer) – The output buffer

  • dim (int) – The dimension to perform reduce on

  • clear (bool, optional) – If True, output buffer will be cleared before reduction. If False, results will be accumulated on existing values. Defaults to True.

Note: When clear=True, reduce_sum will not compute directly on the output buffer. This is because

during warp reduction, the same value would be accumulated multiple times (number of threads in the warp). Therefore, the implementation with clear=True follows these steps:

  1. create a temp buffer with same shape and dtype as out

  2. copy out to temp buffer

  3. call reduce_sum with temp buffer and out

  4. Add temp buffer to out

Returns:

Handle to the reduction operation

Return type:

tir.Call

Parameters:
  • buffer (tvm.tir.Buffer)

  • out (tvm.tir.Buffer)

  • dim (int)

  • clear (bool)

tilelang.language.reduce.reduce_abssum(buffer, out, dim=-1)

Perform reduce absolute sum on input buffer, store the result to output buffer.

Parameters:
  • buffer (tir.Buffer) – The input buffer

  • out (tir.Buffer) – The output buffer

  • dim (int) – The dimension to perform reduce on

Returns:

Handle to the reduction operation

Return type:

tir.Call

tilelang.language.reduce.reduce_absmax(buffer, out, dim=-1, clear=True)

Perform reduce absolute max on input buffer, store the result to output buffer.

Parameters:
  • buffer (tir.Buffer) – The input buffer

  • out (tir.Buffer) – The output buffer

  • dim (int) – The dimension to perform reduce on

  • clear (bool)

Returns:

Handle to the reduction operation

Return type:

tir.Call

tilelang.language.reduce.cumsum_fragment(src, dst, dim, reverse)
Parameters:
  • src (tvm.tir.Buffer)

  • dst (tvm.tir.Buffer)

  • dim (int)

  • reverse (bool)

Return type:

tvm.tir.PrimExpr

tilelang.language.reduce.cumsum(src, dst=None, dim=0, reverse=False)

Compute the cumulative sum of src along dim, writing results to dst.

Negative dim indices are normalized (Python-style). If dst is None, the operation is performed in-place into src. Raises ValueError when dim is out of bounds for src.shape. When src.scope() == “local.fragment”, this delegates to cumsum_fragment; otherwise it emits the tl.cumsum intrinsic.

Returns:

A handle to the emitted cumulative-sum operation.

Return type:

tir.Call

Parameters:
  • src (tvm.tir.Buffer)

  • dst (Optional[tvm.tir.Buffer])

  • dim (int)

  • reverse (bool)

tilelang.language.reduce.finalize_reducer(reducer)

Finalize a reducer buffer by emitting the tl.finalize_reducer intrinsic.

This returns a TVM tir.Call handle that finalizes the given reducer using its writable pointer. The call does not modify Python objects directly; it produces the low-level intrinsic call used by the IR.

Parameters:

reducer (tir.Buffer) – Reducer buffer whose writable pointer will be finalized.

Returns:

Handle to the finalize reducer intrinsic call.

Return type:

tir.Call