CUDA Clamp Kernel
Problem Statement
Clamp every element into a range: out = min(max(x, lo), hi) with runtime scalars lo and hi.
Background
Two conditionals in sequence bound the value from below and above. Clamping is used to bound activations, gradients, or pixel values.
Your Task
Implement clamp_kernel and run(n=1024, lo=-0.5, hi=0.5) comparing to np.clip(x, lo, hi).
How it is tested
Your solution must define a top-level function run(...) that allocates the inputs, copies them to the GPU, launches your @cuda.jit kernel, and returns a Python bool from np.allclose(gpu_result, reference). The grader prints run(...); the expected output is True.
Example:
n = 1024, lo = -0.5, hi = 0.5
True
- The input values
n = 1024,lo = -0.5, andhi = 0.5are used to generate an arrayxof lengthnand clamp its elements into the range [โ0.5,0.5]. - The
clamp_kernelfunction is applied toxusing CUDA, which applies the transformation out=min(max(x,โ0.5),0.5) to each element. - A reference solution is computed using
np.clip(x, -0.5, 0.5), which also clampsxinto the range [โ0.5,0.5]. - The final output is
Truebecause the result from the CUDA kernel matches the reference solution, as verified bynp.allclose(gpu_result, reference).
Constraints:
- Bound below by lo then above by hi
- lo and hi are runtime scalars
- Bounds-check the global index
Background Knowledge
The problem involves implementing a clamp kernel using CUDA, a parallel computing platform developed by NVIDIA. The clamp kernel is a fundamental operation in various fields, including computer vision, machine learning, and scientific computing. It is used to bound the values of an array within a specified range, defined by the lo and hi runtime scalars. This operation is essential in many applications, such as bounding activations, gradients, or pixel values.
In the context of CUDA, a kernel is a small program that runs on the GPU, executing a specific task in parallel across multiple threads. The @cuda.jit decorator is used to compile a Python function into a CUDA kernel. To implement the clamp kernel, you will need to understand how to launch a CUDA kernel, manage memory allocation and data transfer between the host (CPU) and device (GPU), and use CUDA's thread indexing to parallelize the computation.
The problem also involves comparing the result of the CUDA kernel with the np.clip function from the NumPy library, which provides a similar clamping operation. This comparison will help verify the correctness of the implemented kernel. To achieve this, you will need to understand how to allocate memory on the GPU, copy data between the host and device, and use CUDA's synchronization mechanisms to ensure that the kernel execution is complete before comparing the results.
Algorithm/Approach
The general approach to solving this problem involves the following steps:
- Define a CUDA kernel function that takes the input array, lo, and hi as parameters and applies the clamping operation to each element.
- Launch the CUDA kernel on the GPU, allocating the necessary memory and managing data transfer between the host and device.
- Compare the result of the CUDA kernel with the np.clip function to verify correctness.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.