| 28 | )"; |
| 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | printf("\033[2J\033[1;1H"); |
| 32 | printf("\nHello float16!\n"); |
| 33 | printf("--------------\n\n"); |
| 34 | |
| 35 | Context ctx = createContext( |
| 36 | {}, {}, |
| 37 | /*device descriptor, enabling f16 in WGSL*/ |
| 38 | { |
| 39 | .requiredFeatureCount = 1, |
| 40 | .requiredFeatures = std::array{WGPUFeatureName_ShaderF16}.data(), |
| 41 | }); |
| 42 | static constexpr size_t N = 10000; |
| 43 | std::array<half, N> inputArr, outputArr; |
| 44 | for (int i = 0; i < N; ++i) { |
| 45 | inputArr[i] = half(static_cast<float>(i) / 10.0f); // dummy input data |
| 46 | } |
| 47 | Tensor input = createTensor(ctx, Shape{N}, kf16, inputArr.data()); |
| 48 | Tensor output = createTensor(ctx, Shape{N}, kf16); |
| 49 | std::promise<void> promise; |
| 50 | std::future<void> future = promise.get_future(); |
| 51 | Kernel op = createKernel(ctx, {kGelu, 256, kf16}, Bindings{input, output}, |
| 52 | {cdiv(N, 256), 1, 1}); |
| 53 | dispatchKernel(ctx, op, promise); |
| 54 | wait(ctx, future); |
| 55 | toCPU(ctx, output, outputArr.data(), sizeof(outputArr)); |
| 56 | |
| 57 | for (int i = 0; i < 12; ++i) { |
| 58 | // Cast to float32 for printing to the screen |
| 59 | printf(" gelu(%.2f) = %.2f\n", static_cast<float>(inputArr[i]), |
| 60 | static_cast<float>(outputArr[i])); |
| 61 | } |
| 62 | |
| 63 | printf(" ...\n\n"); |
| 64 | printf("Computed %zu float16 values of GELU(x: float16)\n\n", N); |
| 65 | return 0; |
| 66 | |
| 67 | } |
nothing calls this directly
no test coverage detected