TensorTonic
Problems
Dropout as a CUDA Kernel
GPU ProgrammingNeural Networks
Easy

Implement inverted dropout on the GPU using an externally provided binary mask. For each element of the input array:

output[i]=input[i]⋅mask[i]1−p\text{output}[i] = \frac{\text{input}[i] \cdot \text{mask}[i]}{1 - p}output[i]=1−pinput[i]⋅mask[i]​

The mask\texttt{mask}mask buffer is a device array of 000 or 111 values: a 111 keeps the activation, a 000 drops it. The scalar p∈[0,1)p \in [0, 1)p∈[0,1) is the dropout probability and is passed by value to solve\texttt{solve}solve.

The 11−p\frac{1}{1 - p}1−p1​ factor is the inverted-dropout rescale: it keeps the expected value of each kept activation equal to the original input, so no rescaling is needed at inference time.

Input, mask, and output buffers are device pointers owned by the harness. The output array has the same length as the input.

Loading visualization...

Examples

Input: input = [1, 2, 3, 4], mask = [1, 0, 1, 0], p = 0.5

Output: [2.0, 0.0, 6.0, 0.0]

Kept elements are multiplied by 1/(1−0.5)=21 / (1 - 0.5) = 21/(1−0.5)=2; dropped elements become 000.

Input: input = [0.5, 1.0, 1.5, 2.0], mask = [1, 1, 0, 1], p = 0.25

Output: [0.6667, 1.3333, 0.0, 2.6667]

Each kept element is scaled by 1/0.75≈1.33331 / 0.75 \approx 1.33331/0.75≈1.3333.

Hint 1

Compute the scale 1.0f / (1.0f - p) once per thread before writing to output[idx].

Hint 2

The mask is supplied externally as a device array of 0 or 1 floats. You do not need any random number generation inside the kernel.

Hint 3

A single multiply chain works: output[idx] = input[idx] * mask[idx] * scale.

Requirements

  • Use one thread per output element
  • Multiply each input[i]\texttt{input[i]}input[i] by mask[i]\texttt{mask[i]}mask[i] to apply the drop pattern
  • Rescale the kept elements by 11−p\frac{1}{1 - p}1−p1​ (inverted dropout)
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • 0≤p<10 \le p < 10≤p<1
  • mask[i]∈{0,1}\texttt{mask}[i] \in \{0, 1\}mask[i]∈{0,1}
  • Input, mask, and output buffers are device pointers
  • Use 256 threads per block
  • Values fit in 32-bit float
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumDropout (Inverted Scaling) in TritonEasyCosine Similarity as a CUDA KernelMediumCross-Entropy Loss as a CUDA KernelMediumLayerNorm Forward in TritonMediumRMSNorm Forward in TritonMediumScaled Dot-Product Attention in CUDAHardLayerNorm Backward in TritonHardImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasy

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Dropout as a CUDA Kernel

Dropout as a CUDA Kernel

GPU ProgrammingNeural Networks
Easy

Implement inverted dropout on the GPU using an externally provided binary mask. For each element of the input array:

output[i]=input[i]⋅mask[i]1−p\text{output}[i] = \frac{\text{input}[i] \cdot \text{mask}[i]}{1 - p}output[i]=1−pinput[i]⋅mask[i]​

The mask\texttt{mask}mask buffer is a device array of 000 or 111 values: a 111 keeps the activation, a 000 drops it. The scalar p∈[0,1)p \in [0, 1)p∈[0,1) is the dropout probability and is passed by value to solve\texttt{solve}solve.

The 11−p\frac{1}{1 - p}1−p1​ factor is the inverted-dropout rescale: it keeps the expected value of each kept activation equal to the original input, so no rescaling is needed at inference time.

Input, mask, and output buffers are device pointers owned by the harness. The output array has the same length as the input.

Loading visualization...

Examples

Input: input = [1, 2, 3, 4], mask = [1, 0, 1, 0], p = 0.5

Output: [2.0, 0.0, 6.0, 0.0]

Kept elements are multiplied by 1/(1−0.5)=21 / (1 - 0.5) = 21/(1−0.5)=2; dropped elements become 000.

Input: input = [0.5, 1.0, 1.5, 2.0], mask = [1, 1, 0, 1], p = 0.25

Output: [0.6667, 1.3333, 0.0, 2.6667]

Each kept element is scaled by 1/0.75≈1.33331 / 0.75 \approx 1.33331/0.75≈1.3333.

Hint 1

Compute the scale 1.0f / (1.0f - p) once per thread before writing to output[idx].

Hint 2

The mask is supplied externally as a device array of 0 or 1 floats. You do not need any random number generation inside the kernel.

Hint 3

A single multiply chain works: output[idx] = input[idx] * mask[idx] * scale.

Requirements

  • Use one thread per output element
  • Multiply each input[i]\texttt{input[i]}input[i] by mask[i]\texttt{mask[i]}mask[i] to apply the drop pattern
  • Rescale the kept elements by 11−p\frac{1}{1 - p}1−p1​ (inverted dropout)
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • 0≤p<10 \le p < 10≤p<1
  • mask[i]∈{0,1}\texttt{mask}[i] \in \{0, 1\}mask[i]∈{0,1}
  • Input, mask, and output buffers are device pointers
  • Use 256 threads per block
  • Values fit in 32-bit float
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumDropout (Inverted Scaling) in TritonEasyCosine Similarity as a CUDA KernelMediumCross-Entropy Loss as a CUDA KernelMediumLayerNorm Forward in TritonMediumRMSNorm Forward in TritonMediumScaled Dot-Product Attention in CUDAHardLayerNorm Backward in TritonHardImplement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyOuter Product as a CUDA KernelEasy

Accepts: array

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