Implement the hyperbolic tangent activation element-wise with a CUDA kernel.
y=tanh(x)=ex+e−xex−e−xHere 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.
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]
CUDA provides tanhf for a single-precision hyperbolic tangent.
A direct ratio of exponentials can overflow even when the activation is finite.
The final block can contain threads whose indices are outside the vector.
Accepts: array
Submit a correct solution to see your kernel performance.
Implement the hyperbolic tangent activation element-wise with a CUDA kernel.
y=tanh(x)=ex+e−xex−e−xHere 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.
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]
CUDA provides tanhf for a single-precision hyperbolic tangent.
A direct ratio of exponentials can overflow even when the activation is finite.
The final block can contain threads whose indices are outside the vector.
Accepts: array
Submit a correct solution to see your kernel performance.