﻿# PyTorch Triton operators: choosing the compiler visibility boundary

Choose direct Triton calls, triton_op, or an opaque custom_op according to tracing and subsystem needs rather than wrapping every GPU kernel.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-pytorch-triton-op-compiler-visibility/
Language: en

## Begin with the boundary the application needs

A custom GPU kernel does not automatically need a custom PyTorch operator. The [PyTorch library reference](https://docs.pytorch.org/docs/2.15/library.html) recommends direct Triton use when an operator wrapper adds no required behavior. Registration becomes useful when the application needs a stable operator identity or controlled interaction with tensor subclasses and dispatch modes. Start by naming that requirement, because unnecessary boundaries can obscure the implementation that compilation would otherwise inspect.

## Understand what opacity buys

A conventional custom_op gives an implementation an opaque operator boundary. This is useful for external computation that PyTorch cannot trace through, but it also prevents compilation from inspecting the internal implementation. Imagine a preprocessing kernel followed by ordinary tensor arithmetic: an opaque wrapper can conceal both pieces together. A correct output does not establish that the compiler still has the same optimization opportunities as it has with a visible implementation.

## Keep Triton-backed implementations traceable

The [user-defined Triton tutorial](https://docs.pytorch.org/tutorials/recipes/torch_compile_user_defined_triton_kernel_tutorial.html) shows triton_op with wrap_triton around kernel calls. The operator body must remain composed of PyTorch-understood operations and appropriately wrapped Triton launches. This is a contract for graph capture, not permission to place arbitrary Python side effects inside the function and expect export to reproduce them. Keep logging, filesystem access, and mutable application state outside the numerical operator.

## Compare the actual graph and result

Consider a decoder extension that allocates an output buffer and launches a custom activation kernel. First establish the simplest direct implementation. Add a registered operator only when the integration requires its identity or dispatch behavior. Compare eager and compiled outputs and inspect graph capture on the chosen deployment path. This separates a numerical defect from an integration defect, while avoiding an unsupported assumption that adding a decorator necessarily improves execution speed.

## Preserve the rest of the operator contract

Compiler visibility does not replace accurate mutation declarations or supported training behavior. The [custom-operator overview](https://docs.pytorch.org/tutorials/advanced/python_custom_ops.html) treats the operator as a public contract around its kernel. Record the wrapper choice with the supported subsystems and version. Existing guides on [compilation and graph replay](/journal/cuda-graphs-vs-torch-compile/) and [tensor axes](/journal/tensor-axes-batch-sequence-hidden/) provide context; the decision here is specifically which implementation remains visible across the operator boundary.
