| 49 | } |
| 50 | |
| 51 | std::string build_kernel( |
| 52 | const std::string& func_name, |
| 53 | const std::string& header, |
| 54 | const std::string& source, |
| 55 | const std::vector<std::string>& input_names, |
| 56 | const std::vector<array>& inputs, |
| 57 | const std::vector<std::string>& output_names, |
| 58 | const std::vector<Dtype>& output_dtypes, |
| 59 | const std::vector<std::pair<std::string, TemplateArg>>& template_args, |
| 60 | const std::vector<std::tuple<bool, bool, bool>>& shape_infos) { |
| 61 | std::string kernel_source; |
| 62 | kernel_source.reserve(header.size() + source.size() + 8192); |
| 63 | kernel_source += default_header; |
| 64 | kernel_source += header; |
| 65 | kernel_source += |
| 66 | "namespace mlx::core::cu {\n\n" |
| 67 | "namespace cg = cooperative_groups;\n\n"; |
| 68 | |
| 69 | kernel_source += "__global__ void "; |
| 70 | kernel_source += func_name; |
| 71 | kernel_source += "(\n"; |
| 72 | |
| 73 | // Add inputs |
| 74 | for (int i = 0; i < inputs.size(); ++i) { |
| 75 | const auto& name = input_names[i]; |
| 76 | const auto& arr = inputs[i]; |
| 77 | kernel_source += " const "; |
| 78 | kernel_source += dtype_to_cuda_type(arr.dtype()); |
| 79 | kernel_source += "* "; |
| 80 | kernel_source += name; |
| 81 | kernel_source += ",\n"; |
| 82 | // Add input shape, strides and ndim if present in the source |
| 83 | if (arr.ndim() > 0) { |
| 84 | if (std::get<0>(shape_infos[i])) { |
| 85 | kernel_source += " const __grid_constant__ Shape "; |
| 86 | kernel_source += name; |
| 87 | kernel_source += "_shape,\n"; |
| 88 | } |
| 89 | if (std::get<1>(shape_infos[i])) { |
| 90 | kernel_source += " const __grid_constant__ Strides "; |
| 91 | kernel_source += name; |
| 92 | kernel_source += "_strides,\n"; |
| 93 | } |
| 94 | if (std::get<2>(shape_infos[i])) { |
| 95 | kernel_source += " const __grid_constant__ int "; |
| 96 | kernel_source += name; |
| 97 | kernel_source += "_ndim,\n"; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Add outputs |
| 103 | for (int i = 0; i < output_names.size(); ++i) { |
| 104 | const auto& name = output_names[i]; |
| 105 | const auto& dtype = output_dtypes[i]; |
| 106 | kernel_source += " "; |
| 107 | kernel_source += dtype_to_cuda_type(dtype); |
| 108 | kernel_source += "* "; |
no test coverage detected