TensorTonic
Problems
Tanh as a CUDA Kernel
Activation FunctionsGPU Programming
Easy

Implement the hyperbolic tangent activation element-wise with a CUDA kernel.

y=tanh⁡(x)=ex−e−xex+e−xy = \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}y=tanh(x)=ex+e−xex−e−x​

Here x is an input element, y is its hyperbolic tangent, e is the base of the natural logarithm, and tanh denotes the hyperbolic tangent function. N is the number of elements in each buffer.

The mathematical result lies strictly between negative one 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 = [-1,0,1,2]

Output: [-0.7616,0,0.7616,0.964]

Explanation: Zero maps to zero, equal-magnitude opposite inputs have opposite outputs, and two maps to approximately 0.964.

Input: input = [-3,-0.5,0.5,3]

Output: [-0.9951,-0.4621,0.4621,0.9951]

Hint 1

CUDA provides tanhf for a single-precision hyperbolic tangent.

Hint 2

A direct ratio of exponentials can overflow even when the activation is finite.

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 Sigmoid as a CUDA KernelEasyImplement Exact GELU 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.

Tanh as a CUDA Kernel

Tanh as a CUDA Kernel

Activation FunctionsGPU Programming
Easy

Implement the hyperbolic tangent activation element-wise with a CUDA kernel.

y=tanh⁡(x)=ex−e−xex+e−xy = \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}y=tanh(x)=ex+e−xex−e−x​

Here x is an input element, y is its hyperbolic tangent, e is the base of the natural logarithm, and tanh denotes the hyperbolic tangent function. N is the number of elements in each buffer.

The mathematical result lies strictly between negative one 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 = [-1,0,1,2]

Output: [-0.7616,0,0.7616,0.964]

Explanation: Zero maps to zero, equal-magnitude opposite inputs have opposite outputs, and two maps to approximately 0.964.

Input: input = [-3,-0.5,0.5,3]

Output: [-0.9951,-0.4621,0.4621,0.9951]

Hint 1

CUDA provides tanhf for a single-precision hyperbolic tangent.

Hint 2

A direct ratio of exponentials can overflow even when the activation is finite.

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 Sigmoid as a CUDA KernelEasyImplement Exact GELU 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