Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:NVIDIA DALI KernelTraits

From Leeroopedia


Knowledge Sources
Domains Kernels, GPU_Computing
Last Updated 2026-02-08 16:00 GMT

Overview

Provides compile-time type traits that introspect DALI kernel classes to extract their input, output, and argument types, and validates that kernels conform to the required API contract.

Description

The kernel_traits.h header implements a comprehensive set of template metaprogramming utilities that analyze kernel class signatures at compile time. It defines three primary type aliases -- kernel_inputs, kernel_outputs, and kernel_args -- that extract the respective parameter types from a kernel's Run and Setup methods. These traits support two modes: if the kernel defines explicit nested types (Kernel::Inputs, Kernel::Outputs, Kernel::Args), those are used directly; otherwise, the traits automatically infer the types by inspecting the Run method signature.

The inference mechanism works through a series of predicate templates (is_input, is_output, is_kernel_arg) that classify each parameter. TensorListView and TensorView with const element types are classified as inputs; with non-const element types as outputs; KernelContext is filtered out; and everything else is classified as a kernel argument. The filter template and tuple_cat_t are used to collect matching parameters into tuples.

The header also provides the check_kernel and is_kernel validation utilities. check_kernel<Kernel> performs a chain of static assertions verifying that the kernel has unique, non-static Run and Setup member functions, that Run returns void and is callable with the inferred argument tuples, and that Setup returns KernelRequirements. When the assert_ parameter is true (default), compilation fails with descriptive error messages; when false (is_kernel), it silently evaluates to a boolean constant.

Usage

Use kernel_inputs<MyKernel>, kernel_outputs<MyKernel>, and kernel_args<MyKernel> in generic code that operates on kernels without knowing their exact signatures. Use check_kernel<MyKernel>() during development to validate that a kernel class conforms to the DALI kernel API. Use is_kernel<MyKernel>::value in SFINAE or if constexpr contexts to conditionally enable code paths for valid kernel types.

Code Reference

Source Location

Signature

namespace detail {

template <typename T>
struct is_input : std::false_type {};

template <typename StorageBackend, typename T, int dim>
struct is_input<InList<StorageBackend, T, dim>> : std::true_type {};

template <typename StorageBackend, typename T, int dim>
struct is_input<InTensor<StorageBackend, T, dim>> : std::true_type {};

template <typename T>
struct is_output : std::false_type {};

template <typename StorageBackend, typename T, int dim>
struct is_output<TensorListView<StorageBackend, T, dim>>
  : std::integral_constant<bool, !std::is_const<T>::value> {};

template <typename T>
struct is_kernel_arg : std::true_type {};
// Specializations exclude TensorListView, TensorView, and KernelContext

template <typename F>
struct input_lists;   // Extracts input types from a member function pointer

template <typename F>
struct output_lists;  // Extracts output types from a member function pointer

template <typename F>
struct kernel_arg_params;  // Extracts argument types from a member function pointer

}  // namespace detail

template <typename Kernel>
using kernel_inputs = typename detail::KernelInputs<Kernel>::type;

template <typename Kernel>
using kernel_outputs = typename detail::KernelOutputs<Kernel>::type;

template <typename Kernel>
using kernel_args = typename detail::KernelArgs<Kernel>::type;

template <typename Kernel, bool assert_ = true>
using check_kernel = detail::check_kernel_has_run<Kernel, assert_>;

template <typename Kernel>
using is_kernel = check_kernel<Kernel, false>;

Import

#include "dali/kernels/kernel_traits.h"

I/O Contract

Inputs

Name Type Required Description
Kernel template parameter Yes The kernel class to introspect; must have Run and Setup member functions
assert_ bool (template parameter) No When true (default for check_kernel), triggers static_assert on failure; when false (is_kernel), silently evaluates to false

Outputs

Name Type Description
kernel_inputs<Kernel> tuple of input types Tuple of InList/InTensor parameter types from the kernel's Run method
kernel_outputs<Kernel> tuple of output types Tuple of non-const TensorListView/TensorView parameter types from Run
kernel_args<Kernel> tuple of argument types Tuple of extra argument types (everything except tensors and context)
check_kernel<Kernel> std::integral_constant<bool, ...> Compile-time boolean; true if the kernel satisfies the API contract
is_kernel<Kernel> std::integral_constant<bool, ...> Same as check_kernel but without static_assert diagnostics

Usage Examples

Compile-Time Kernel Validation

#include "dali/kernels/kernel_traits.h"

struct MyKernel {
  KernelRequirements Setup(KernelContext &ctx, const InListGPU<float, 3> &in, int param);
  void Run(KernelContext &ctx, const OutListGPU<float, 3> &out,
           const InListGPU<float, 3> &in, int param);
};

// This will produce a compile error with helpful messages if MyKernel
// does not conform to the DALI kernel API
check_kernel<MyKernel>();

// Use is_kernel for SFINAE without compilation failure
static_assert(is_kernel<MyKernel>::value, "MyKernel must be a valid DALI kernel");

Extracting Kernel Type Information

#include "dali/kernels/kernel_traits.h"

// Given a kernel type, extract its parameter types
using Inputs  = kernel_inputs<MyKernel>;   // std::tuple<const InListGPU<float, 3>&>
using Outputs = kernel_outputs<MyKernel>;  // std::tuple<const OutListGPU<float, 3>&>
using Args    = kernel_args<MyKernel>;     // std::tuple<int>

// Use in generic dispatch code
template <typename K>
void dispatch(K &kernel, KernelContext &ctx,
              const kernel_outputs<K> &out,
              const kernel_inputs<K> &in,
              const kernel_args<K> &args) {
  apply_all(std::mem_fn(&K::Run), kernel, ctx, out, in, args);
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment