v9.1.1
Menu

CUDA ReLU Kernel

easyFREECore Compute KernelsCUDA Basics with Numba

Problem Statement

Implement the ReLU activation out = max(x, 0) as a CUDA kernel.

Background

There's no tl.maximum here — write the comparison yourself with a conditional. ReLU clamps negatives to zero and is the most common neural-network activation.

Your Task

Implement relu_kernel and run(n=1024) comparing to np.maximum(x, 0).

How it is tested

Your solution must define a top-level function run(...) that allocates the inputs, copies them to the GPU, launches your @cuda.jit kernel, and returns a Python bool from np.allclose(gpu_result, reference). The grader prints run(...); the expected output is True.

Example

Input
n = 1024
Output
True
Reasoning
  • The input n = 1024 determines the size of the input array x to be used for the ReLU activation function.
  • The relu_kernel function is launched on the GPU, applying the ReLU activation out = \max(x, 0) element-wise to the input array x, effectively clamping all negative values to 00.
  • The result from the GPU is compared to the reference result obtained by applying np.maximum(x, 0) to the input array x.
  • The run function returns True if the GPU result is close to the reference result, as verified by np.allclose(gpu_result, reference), indicating that the CUDA kernel implementation of the ReLU activation function is correct.

Constraints

  • out[i] = x[i] if x[i] > 0 else 0.0
  • Bounds-check the global index

More from CUDA Basics with Numba

solution.py

Test results

Sign in to run your code against the test cases.