Source browser

examples/shaders/lenet_dense.cpp

Read the complete file below or download its original bytes. Line numbers provide stable links to a declaration or processing step.

File fingerprint
1// Apply a learned dense layer with an optional ReLU activation.
2#ifdef __cplusplus // Include the runtime only when building the CPU adapter.
3#include "shdr.h" // Share resource and math types with the application.
4#endif // Finish header inclusion for the generated C++ build.
5layout(local_size_x = 64) in; // Give each invocation one dense output feature.
6layout(std430, binding = 0) readonly buffer Inputs { // Read the complete preceding feature vector.
7    float input_values[]; // Store one contiguous feature vector per image.
8}; // Finish the input resource declaration.
9layout(std430, binding = 1) readonly buffer Parameters { // Read flattened model weights and biases.
10    float parameters[]; // Match the offline model parameter order.
11}; // Finish the parameter resource declaration.
12layout(std430, // Use tightly packed shader storage for the following port.
13       binding = 2) writeonly buffer Outputs { // Write this layer into distinct feature storage.
14    float output_values[]; // Keep one contiguous output vector per image.
15}; // Finish the output resource declaration.
16uniform uint batch_count; // Receive the number of independent images.
17uniform uint input_features; // Receive the preceding feature-vector length.
18uniform uint output_features; // Receive the learned output-vector length.
19uniform uint weight_offset; // Receive the first dense weight in the flat model.
20uniform uint bias_offset; // Receive the first dense bias in the flat model.
21uniform uint activation; // Select zero for logits and one for ReLU features.
22void main() { // Compute the dense output assigned to this invocation.
23    uint index = gl_GlobalInvocationID.x; // Locate this flattened batch and output feature.
24    if (index >= batch_count * output_features) // Ignore excess invocations beyond the dense output.
25        return; // Ignore unused invocations after the output vector.
26    uint row = index / output_features, // Decode the image index and its output feature index.
27         column = index % output_features; // Recover the image and output feature indices.
28    float value = parameters[bias_offset + column]; // Start with this learned output bias.
29    for (uint source = 0u; source < input_features; // Visit every component of this image's input feature vector.
30         ++source) { // Visit every feature contributing to this output.
31        value += // Accumulate one learned projection term.
32            parameters[weight_offset + column * input_features + source] * // Multiply one row-major learned weight by its source feature.
33            input_values[row * input_features + source]; // Accumulate one learned projection term.
34    } // Finish the dot product for this output feature.
35    output_values[index] = // Store the completed feature with the chosen activation.
36        activation != 0u ? max(value, 0.0) : value; // Apply ReLU only for the hidden dense layers.
37} // Finish this dense-layer invocation.