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 Kernel Base

From Leeroopedia


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

Overview

Defines the DALI kernel API contract through an example kernel struct and provides the kernel::Setup and kernel::Run dispatch functions that invoke kernels with type-inferred argument packing.

Description

The kernel.h header establishes the canonical API that every DALI kernel must follow. A DALI kernel is any class that provides two non-overloaded member functions: Setup (which returns KernelRequirements describing output shapes and scratch memory needs) and Run (which performs the actual computation given pre-allocated outputs). The header includes an examples::Kernel struct that documents the expected signatures and argument ordering conventions.

The kernel namespace provides free function templates Setup and Run that serve as a uniform dispatch mechanism. These functions accept a kernel instance, a KernelContext, and tuples of inputs, outputs, and arguments. They use apply_all (from tuple_helpers) to unpack the tuples and forward them to the kernel's member functions. Before dispatch, check_kernel<Kernel>() is invoked to validate at compile time that the kernel type satisfies all API requirements.

The header also provides type aliases (kernel::inputs, kernel::outputs, kernel::args) that extract the input, output, and argument types from a kernel class using the traits defined in kernel_traits.h. Additionally, the standalone function GetCufftVersion() is declared here for querying the available cuFFT library version.

Usage

Use this header when implementing new DALI kernels or when writing generic code that needs to invoke kernels through the uniform kernel::Setup and kernel::Run interface. Kernel implementors should follow the argument ordering convention: Setup(KernelContext, inputs..., args...) and Run(KernelContext, outputs..., inputs..., args...). The check_kernel<MyKernel>() utility can be instantiated to get compile-time diagnostics if the kernel does not conform.

Code Reference

Source Location

Signature

int GetCufftVersion();

namespace kernels {

namespace examples {
template <typename OutputType, typename Input1, typename Input2>
struct Kernel {
  KernelRequirements Setup(
    KernelContext &context,
    const InListGPU<Input1, 3> &in1,
    const InTensorGPU<Input2, 4> &in2,
    const std::vector<float> &aux);

  void Run(
    KernelContext &context,
    const OutListGPU<OutputType, 3> &out,
    const InListGPU<Input1, 3> &in1,
    const InTensorGPU<Input2, 4> &in2,
    const std::vector<float> &aux);
};
}  // namespace examples

namespace kernel {

template <typename Kernel>
using inputs = kernel_inputs<Kernel>;

template <typename Kernel>
using outputs = kernel_outputs<Kernel>;

template <typename Kernel>
using args = kernel_args<Kernel>;

using Context = KernelContext;
using Requirements = KernelRequirements;

template <typename Kernel>
Requirements Setup(
    Kernel &instance,
    Context &context,
    const inputs<Kernel> &input,
    const args<Kernel> &args);

template <typename Kernel>
void Run(
    Kernel &instance,
    Context &context,
    const outputs<Kernel> &output,
    const inputs<Kernel> &input,
    const args<Kernel> &args);

}  // namespace kernel
}  // namespace kernels

Import

#include "dali/kernels/kernel.h"

I/O Contract

Inputs

Name Type Required Description
instance Kernel& Yes Reference to the kernel instance to invoke
context KernelContext& Yes Execution context with stream and scratchpad
input kernel::inputs<Kernel> Yes Tuple of input tensor views matching the kernel's expected input types
output kernel::outputs<Kernel> Yes (Run only) Tuple of output tensor views matching the kernel's expected output types
args kernel::args<Kernel> Yes Tuple of extra arguments matching the kernel's expected argument types

Outputs

Name Type Description
requirements KernelRequirements Returned by kernel::Setup; describes output shapes and scratch memory requirements
(void) void kernel::Run populates pre-allocated output tensors in-place

Usage Examples

Implementing a Minimal Kernel

#include "dali/kernels/kernel.h"

struct MyAddKernel {
  KernelRequirements Setup(
      KernelContext &ctx,
      const InListGPU<float, 2> &input,
      float scalar) {
    KernelRequirements req;
    req.output_shapes = {input.shape};
    return req;
  }

  void Run(
      KernelContext &ctx,
      const OutListGPU<float, 2> &output,
      const InListGPU<float, 2> &input,
      float scalar) {
    // Launch GPU kernel to add scalar to each element
  }
};

// Compile-time validation
check_kernel<MyAddKernel>();

Dispatching via kernel::Setup and kernel::Run

#include "dali/kernels/kernel.h"

MyAddKernel my_kernel;
KernelContext ctx;

// Setup
auto req = kernel::Setup(my_kernel, ctx,
    std::make_tuple(input_list),
    std::make_tuple(2.0f));

// Run (after allocating outputs based on req)
kernel::Run(my_kernel, ctx,
    std::make_tuple(output_list),
    std::make_tuple(input_list),
    std::make_tuple(2.0f));

Related Pages

Page Connections

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