Download problems/kb1_012_matmul_with_diagonal_matrices__shape_rect.py from K1ta141k/kernel-train-problems: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/datasets/K1ta141k/kernel-train-problems/resolve/main/problems/kb1_012_matmul_with_diagonal_matrices__shape_rect.py
- Command line
-
hf download hf://datasets/K1ta141k/kernel-train-problems/problems/kb1_012_matmul_with_diagonal_matrices__shape_rect.py
-
curl -L -o kb1_012_matmul_with_diagonal_matrices__shape_rect.py https://huggingface.co/datasets/K1ta141k/kernel-train-problems/resolve/main/problems/kb1_012_matmul_with_diagonal_matrices__shape_rect.py
1.74 kB
| """Task kb1_012_matmul_with_diagonal_matrices__shape_rect (KernelBench Level 1 #12, split=train, category=matmul, dtype=fp32, variant=shape_rect).""" | |
| PROBLEM_ID = "kb1_012_matmul_with_diagonal_matrices__shape_rect" | |
| SOURCE_KB_ID = 12 | |
| CATEGORY = "matmul" | |
| SPLIT = "train" | |
| DTYPE = "fp32" | |
| VARIANT = "shape_rect" | |
| import torch | |
| import torch.nn as nn | |
| class Model(nn.Module): | |
| """ | |
| Simple model that performs a matrix multiplication of a diagonal matrix with another matrix. | |
| C = diag(A) * B | |
| """ | |
| def __init__(self): | |
| super(Model, self).__init__() | |
| def forward(self, A, B): | |
| """ | |
| Performs the matrix multiplication. | |
| Args: | |
| A (torch.Tensor): A 1D tensor representing the diagonal of the diagonal matrix. Shape: (N,). | |
| B (torch.Tensor): A 2D tensor representing the second matrix. Shape: (N, M). | |
| Returns: | |
| torch.Tensor: The result of the matrix multiplication. Shape: (N, M). | |
| """ | |
| # Logically equivalent to torch.diag(A) @ B | |
| # more efficient as no need to materialize a full N×N matrix | |
| return A.unsqueeze(1) * B | |
| M = 4096 | |
| N = 4096 | |
| # --- Task shape configuration (4GB-safe laptop envelope) --- | |
| M = 2048 | |
| N = 2048 | |
| M = 1536 | |
| N = 3072 | |
| def get_inputs(): | |
| A = torch.rand(N) | |
| B = torch.rand(N, M) | |
| return [A, B] | |
| def get_init_inputs(): | |
| return [] # No special initialization inputs needed | |
| if __name__ == "__main__": | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[2])) | |
| from verifier import verify_kernel, format_result | |
| res = verify_kernel(None, __file__, self_check=True) | |
| print(format_result(res)) | |
| sys.exit(0 if res["status"] == "PASS" else 1) | |