* @brief Python-bindable 'run' function for RMSNorm. * * This function serves as the entry point from Python. It performs extensive * validation on the input tensors to ensure they meet the kernel's requirements. * It then allocates the output tensor and calls the CUDA kernel launcher. * * @param hidden_states Input tensor of shape [batch_size, 4096] and dtype bfloat16. * @param weight Weig
| 22 | * @return The output tensor with the same shape and dtype as hidden_states. |
| 23 | */ |
| 24 | torch::Tensor run( |
| 25 | const torch::Tensor& hidden_states, |
| 26 | const torch::Tensor& weight) { |
| 27 | |
| 28 | // --- Input Validation --- |
| 29 | TORCH_CHECK(hidden_states.dim() == 2, "hidden_states must be a 2D tensor, but got ", hidden_states.dim(), " dimensions"); |
| 30 | TORCH_CHECK(weight.dim() == 1, "weight must be a 1D tensor, but got ", weight.dim(), " dimensions"); |
| 31 | |
| 32 | const int64_t hidden_size = hidden_states.size(1); |
| 33 | |
| 34 | TORCH_CHECK(hidden_size == 4096, "hidden_size must be 4096, but got ", hidden_size); |
| 35 | TORCH_CHECK(weight.size(0) == hidden_size, "weight must have size ", hidden_size, ", but got ", weight.size(0)); |
| 36 | |
| 37 | check_tensor(hidden_states, "hidden_states"); |
| 38 | check_tensor(weight, "weight"); |
| 39 | |
| 40 | // --- Output Tensor Allocation --- |
| 41 | auto output = torch::empty_like(hidden_states); |
| 42 | |
| 43 | // --- Kernel Execution --- |
| 44 | const float eps = 1e-5f; |
| 45 | |
| 46 | // Get current CUDA stream from PyTorch's context |
| 47 | cudaStream_t stream = at::cuda::getCurrentCUDAStream(); |
| 48 | |
| 49 | // Launch the kernel via the C++ wrapper function in the .cu file |
| 50 | rmsnorm_h4096_launcher( |
| 51 | output, |
| 52 | hidden_states, |
| 53 | weight, |
| 54 | eps, |
| 55 | stream |
| 56 | ); |
| 57 | |
| 58 | return output; |
| 59 | } |
| 60 | |
| 61 | // --- Pybind11 Module Definition --- |
| 62 | // Exposes the 'run' function to Python, making it callable as a C++ extension. |
nothing calls this directly
no test coverage detected