TensorTonic
Problems
Implement Leaky ReLU as a CUDA Kernel
Activation FunctionsGPU Programming
Easy

Apply Leaky ReLU to a vector of N float32 values on the GPU.

yi={xixi≥0αxixi<0y_i = \begin{cases} x_i & x_i \ge 0 \\ \alpha x_i & x_i < 0 \end{cases}yi​={xi​αxi​​xi​≥0xi​<0​

Here, x is the input vector, y is the output vector, i is an element index from 0 to N minus 1, and alpha is the supplied nonnegative slope for negative inputs.

Write all N results into the preallocated float32 output device buffer, preserving their order and leaving the input unchanged. The function returns no value.

Loading visualization...

Examples

Input: input = [-2, -1, 0, 1, 2], alpha = 0.1

Output: [-0.2, -0.1, 0.0, 1.0, 2.0]

Explanation: Only the negative values are scaled by 0.1.

Input: input = [-5, 5], alpha = 0.01

Output: [-0.05, 5.0]

Hint 1

Combine the block and thread indices to locate an element.

Hint 2

A conditional expression can select the positive or negative branch.

Hint 3

Check the element index before accessing either buffer.

Requirements

  • Compute the stated activation using the supplied alpha.
  • Write every output element without changing the input or accessing beyond the buffers.
  • Keep the supplied solve interface.

Constraints

  • N is between 1 and 10,000,000.
  • Input and output are contiguous device buffers containing N float32 values.
  • Alpha is a finite, nonnegative float32 scalar passed by value.
  • Input values are finite.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasySoftmax as a CUDA KernelMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasyGELU in TritonEasyReLU in TritonEasySiLU in TritonEasyMasked Triton GELU KernelEasy

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Implement Leaky ReLU as a CUDA Kernel

Implement Leaky ReLU as a CUDA Kernel

Activation FunctionsGPU Programming
Easy

Apply Leaky ReLU to a vector of N float32 values on the GPU.

yi={xixi≥0αxixi<0y_i = \begin{cases} x_i & x_i \ge 0 \\ \alpha x_i & x_i < 0 \end{cases}yi​={xi​αxi​​xi​≥0xi​<0​

Here, x is the input vector, y is the output vector, i is an element index from 0 to N minus 1, and alpha is the supplied nonnegative slope for negative inputs.

Write all N results into the preallocated float32 output device buffer, preserving their order and leaving the input unchanged. The function returns no value.

Loading visualization...

Examples

Input: input = [-2, -1, 0, 1, 2], alpha = 0.1

Output: [-0.2, -0.1, 0.0, 1.0, 2.0]

Explanation: Only the negative values are scaled by 0.1.

Input: input = [-5, 5], alpha = 0.01

Output: [-0.05, 5.0]

Hint 1

Combine the block and thread indices to locate an element.

Hint 2

A conditional expression can select the positive or negative branch.

Hint 3

Check the element index before accessing either buffer.

Requirements

  • Compute the stated activation using the supplied alpha.
  • Write every output element without changing the input or accessing beyond the buffers.
  • Keep the supplied solve interface.

Constraints

  • N is between 1 and 10,000,000.
  • Input and output are contiguous device buffers containing N float32 values.
  • Alpha is a finite, nonnegative float32 scalar passed by value.
  • Input values are finite.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Sigmoid as a CUDA KernelEasyImplement Exact GELU as a CUDA KernelEasyTanh as a CUDA KernelEasySoftmax as a CUDA KernelMediumVector Addition as a CUDA KernelEasyVector Subtraction as a CUDA KernelEasyGELU in TritonEasyReLU in TritonEasySiLU in TritonEasyMasked Triton GELU KernelEasy

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1