MCPcopy Create free account
hub / github.com/AlphaGPU/leetgpu-challenges / Challenge

Class Challenge

challenges/medium/43_count_array_element/challenge.py:8–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7
8class Challenge(ChallengeBase):
9 name = "Count Array Element"
10 atol = 1e-05
11 rtol = 1e-05
12 num_gpus = 1
13 access_tier = "free"
14
15 def reference_impl(self, input: torch.Tensor, output: torch.Tensor, N: int, K: int):
16 # Validate input types and shapes
17 assert input.shape == (N,)
18 assert output.shape == (1,)
19 assert input.dtype == torch.int32
20 assert output.dtype == torch.int32
21
22 # count the number of element with value k in an input array
23 equality_tensor = input == K
24 output[0] = torch.sum(equality_tensor)
25
26 def get_solve_signature(self) -> Dict[str, tuple]:
27 return {
28 "input": (ctypes.POINTER(ctypes.c_int), "in"),
29 "output": (ctypes.POINTER(ctypes.c_int), "out"),
30 "N": (ctypes.c_int, "in"),
31 "K": (ctypes.c_int, "in"),
32 }
33
34 def generate_example_test(self) -> Dict[str, Any]:
35 dtype = torch.int32
36 input = torch.tensor([1, 2, 3, 4, 1], device=self.device, dtype=dtype)
37 output = torch.empty(1, device=self.device, dtype=dtype)
38 return {
39 "input": input,
40 "output": output,
41 "N": 5,
42 "K": 1,
43 }
44
45 def generate_functional_test(self) -> List[Dict[str, Any]]:
46 dtype = torch.int32
47 tests = []
48
49 # basic_example
50 tests.append(
51 {
52 "input": torch.tensor([1, 2, 3, 4, 1], device=self.device, dtype=dtype),
53 "output": torch.empty(1, device=self.device, dtype=dtype),
54 "N": 5,
55 "K": 1,
56 }
57 )
58
59 # all_same_value
60 tests.append(
61 {
62 "input": torch.tensor([2] * 16, device=self.device, dtype=dtype),
63 "output": torch.empty(1, device=self.device, dtype=dtype),
64 "N": 16,
65 "K": 2,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected