TensorTonic
Problems
Implement Sigmoid as a CUDA Kernel
Activation FunctionsGPU Programming
Easy

Implement the sigmoid (logistic) activation element-wise with a CUDA kernel.

y=11+e−xy = \frac{1}{1 + e^{-x}}y=1+e−x1​

Here x is an input element, y is its sigmoid activation, and e is the base of the natural logarithm. N is the number of elements in each buffer.

The mathematical result lies strictly between zero and one; single-precision rounding can produce either endpoint for sufficiently large input magnitudes.

The buffers are contiguous arrays of 32-bit floats in device memory. Write all N results into the preallocated output buffer; the solve function returns no value.

Loading visualization...

Examples

Input: input = [0,2,-2]

Output: [0.5,0.8808,0.1192]

Explanation: Zero maps to 0.5; the positive and negative inputs map above and below 0.5, respectively.

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

Output: [0.2689,0.5,0.7311,0.8808]

Hint 1

CUDA provides expf for a single-precision exponential.

Hint 2

The exponential uses the negative input; negation does not limit its magnitude.

Hint 3

The final block can contain threads whose indices are outside the vector.

Requirements

  • Compute the specified operation independently at every valid index and store the result in output.
  • Leave input buffers unchanged and do not access memory outside the supplied buffers.
  • Preserve the supplied CUDA C++ function signatures and host launch wrapper.

Constraints

  • N is an integer from 1 through 10,000,000.
  • The input and output buffers each contain N contiguous 32-bit floating-point elements in device memory.
  • Input values are finite and representable as 32-bit floats.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Implement Sigmoid as a CUDA Kernel

Implement Sigmoid as a CUDA Kernel

Activation FunctionsGPU Programming
Easy

Implement the sigmoid (logistic) activation element-wise with a CUDA kernel.

y=11+e−xy = \frac{1}{1 + e^{-x}}y=1+e−x1​

Here x is an input element, y is its sigmoid activation, and e is the base of the natural logarithm. N is the number of elements in each buffer.

The mathematical result lies strictly between zero and one; single-precision rounding can produce either endpoint for sufficiently large input magnitudes.

The buffers are contiguous arrays of 32-bit floats in device memory. Write all N results into the preallocated output buffer; the solve function returns no value.

Loading visualization...

Examples

Input: input = [0,2,-2]

Output: [0.5,0.8808,0.1192]

Explanation: Zero maps to 0.5; the positive and negative inputs map above and below 0.5, respectively.

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

Output: [0.2689,0.5,0.7311,0.8808]

Hint 1

CUDA provides expf for a single-precision exponential.

Hint 2

The exponential uses the negative input; negation does not limit its magnitude.

Hint 3

The final block can contain threads whose indices are outside the vector.

Requirements

  • Compute the specified operation independently at every valid index and store the result in output.
  • Leave input buffers unchanged and do not access memory outside the supplied buffers.
  • Preserve the supplied CUDA C++ function signatures and host launch wrapper.

Constraints

  • N is an integer from 1 through 10,000,000.
  • The input and output buffers each contain N contiguous 32-bit floating-point elements in device memory.
  • Input values are finite and representable as 32-bit floats.
Try Similar Problems
Implement Swish (SiLU) as a CUDA KernelEasyReLU as a CUDA KernelEasyImplement Leaky ReLU 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1