MCPcopy Create free account
hub / github.com/AnswerDotAI/gpu.cpp / KernelCode

Class KernelCode

gpu.hpp:291–403  ·  view source on GitHub ↗

* @brief KernelCode is the representation of WGSL GPU code with template * substitutions applied. It is a type around the code string with additional * metadata for workgroup size and precision since they are specified in the * WGSL code. Additionally, label and entryPoint are used by `createKernel()` * to specify the label and entry point of the kernel. */

Source from the content-addressed store, hash-verified

289 * to specify the label and entry point of the kernel.
290 */
291struct KernelCode {
292 /**
293 * @brief Constructor to create a code object from a template
294 * string and optional workgroup size and precision.
295 *
296 * @param[in] pData Shader template string with placeholders
297 * @param[in] workgroupSize Shape of the workgroup. Unlike tensor shapes which
298 * can be of arbitrary rank, workgroup size is always of rank 3 corresponding
299 * to x y and z. workgroupSize is stored as a field in the KernelCode instance
300 * that is returned by createShader().
301 * @param[in] precision Data type precision to be substituted for
302 * {{precision}} in the WGSL code. As with workgroupSize, precision is stored
303 * as a field in the KernelCode instance that is returned by createShader().
304 * @code
305 * KernelCode code = {kShaderText, {256, 1, 1}, kf32};
306 * @endcode
307 */
308 inline KernelCode(const std::string &pData = "", size_t workgroupSize = 256,
309 NumType precision = kf32)
310 : data(pData), workgroupSize({workgroupSize, 1, 1}),
311 precision(precision) {
312 if (precision == kf16) {
313 data = "enable f16;\n" + data;
314 }
315 replaceAll(data, "{{workgroupSize}}", toString({workgroupSize, 1, 1}));
316 replaceAll(data, "{{precision}}", toString(precision));
317 LOG(kDefLog, kTrace, "Shader code:\n%s", data.c_str());
318 }
319
320 /**
321 * @brief Overload of the constructor to create a code object from a template
322 * string and workgroup size. This overload takes a single size_t
323 * workgroupSize parameter instead of a 3D shape for the workgroup size and
324 * instantiates a 3D shape with the workgroupSize in the x dimension and 1 in
325 * the y and z dimensions.
326 *
327 * @param[in] pData Shader template string with placeholders @param[in]
328 * workgroupSize 3D Workgroup size
329 * @param[in] precision Data type precision for the shader
330 *
331 * @code KernelCode code = {kPuzzle1, 256, kf32}; @endcode
332 */
333 inline KernelCode(const std::string &pData, const Shape &workgroupSize =
334 {256, 1, 1}, NumType precision = kf32) : data(pData),
335 workgroupSize(workgroupSize), precision(precision) { if (precision == kf16) {
336 data = "enable f16;\n" + data; } replaceAll(data, "{{workgroupSize}}",
337 toString(workgroupSize)); replaceAll(data, "{{precision}}",
338 toString(precision)); LOG(kDefLog, kInfo, "Shader code:\n%s",
339 data.c_str()); }
340
341
342 /**
343 * @brief Overload of the constructor, adding totalWorkgroups parameter to
344 * perform a string replacement for the total number of workgroups in the
345 * kernel code.
346 *
347 * @param[in] pData Shader template string with placeholders
348 * @param[in] workgroupSize 3D Workgroup size

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected