PIXELBANKv9.1.0
Menu

Problem Statement

Implement LeakyReLU: out = x if x >= 0 else slope * x, with slope a runtime scalar (default 0.01).

Background

tl.where(cond, a, b) selects elementwise. Combine it with the comparison x >= 0.

Your Task

Implement leaky_relu_kernel and run(n=1024, slope=0.01) comparing to torch.nn.functional.leaky_relu.

How it is tested

Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.

Example:

Input:
n = 1024, slope = 0.01
Output:
True
Reasoning:
  • The input values n = 1024 and slope = 0.01 are used to allocate an array of size n on the GPU and define the slope parameter for the LeakyReLU function.
  • The leaky_relu_kernel function is applied to the input array, using the tl.where function to element-wise select between the original value x and the scaled value slope * x, based on the condition x >= 0: out=xout = x if xโ‰ฅ0x \geq 0 else out=slopeโ‹…xout = slope \cdot x.
  • The resulting array from the leaky_relu_kernel function is compared to the output of torch.nn.functional.leaky_relu using the same input array and slope value.
  • The comparison is done using torch.allclose, which checks if the two arrays are element-wise equal within a certain tolerance, and returns True if they are equal, resulting in the output True.

Constraints:

  • Use tl.where(x >= 0, x, slope * x)
  • slope is a runtime scalar argument
solution.py

Test Results

0/0
Run code to see test results.
Triton LeakyReLU Kernel - Medium | PixelBank