PIXELBANKv9.1.0
Menu

Problem Statement

Implement a clamp kernel: out = min(max(x, lo), hi) with runtime scalars lo and hi.

Background

Compose tl.maximum and tl.minimum. Clamping bounds activations or gradients.

Your Task

Implement clamp_kernel and run(n=1024, lo=-0.5, hi=0.5) comparing to torch.clamp.

How it is tested

Your solution must define a top-level function run(...) that allocates inputs on the GPU, launches your Triton kernel, and returns a boolean from torch.allclose(triton_out, torch_reference, ...). The grader prints run(...); the expected output is True.

Example:

Input:
n = 1024, lo = -0.5, hi = 0.5
Output:
True
Reasoning:
  • The input values are n=1024n = 1024, lo=โˆ’0.5lo = -0.5, and hi=0.5hi = 0.5, which represent the number of elements to clamp and the lower and upper bounds, respectively.
  • The clamp_kernel function is applied to an array of nn elements, effectively computing out=minโก(maxโก(x,lo),hi)out = \min(\max(x, lo), hi) for each element xx.
  • The result from the clamp_kernel function is then compared to the result from torch.clamp using torch.allclose, which checks if the two arrays are element-wise equal within a tolerance.
  • The comparison yields True if the results are close enough, indicating that the custom clamp_kernel implementation matches the PyTorch reference implementation.

Constraints:

  • out = tl.minimum(tl.maximum(x, lo), hi)
  • lo, hi are runtime scalars
๐Ÿ”’

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.

solution.py

Test Results

0/0
Run code to see test results.