LJTSG commited on
Commit
a87c238
·
verified ·
1 Parent(s): 0116d58

Upload shaders/bf16_to_f32.wgsl with huggingface_hub

Browse files
Files changed (1) hide show
  1. shaders/bf16_to_f32.wgsl +36 -0
shaders/bf16_to_f32.wgsl ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // bf16_to_f32.wgsl — convert BF16 weights to F32 in-place on GPU.
2
+ // BF16 is F32 with the lower 16 bits zeroed: bf16_as_u16 << 16 = f32_bits.
3
+ //
4
+ // Input: array of u32 where each u32 holds TWO bf16 values (packed little-endian).
5
+ // u32 = (bf16_high << 16) | bf16_low
6
+ // Output: array of f32, twice the length of the input u32 array.
7
+ //
8
+ // Dispatch: ceil(num_f32_elements / 2 / 64) workgroups.
9
+ // Each thread converts one u32 (2 bf16 values) → 2 f32 values.
10
+
11
+ const WG_SIZE: u32 = 64u;
12
+
13
+ struct Params {
14
+ num_pairs: u32, // number of u32 elements (each holds 2 bf16 values)
15
+ }
16
+
17
+ @group(0) @binding(0) var<storage, read> bf16_packed: array<u32>; // [num_pairs]
18
+ @group(0) @binding(1) var<storage, read_write> f32_out: array<f32>; // [num_pairs * 2]
19
+
20
+ @group(1) @binding(0) var<uniform> params: Params;
21
+
22
+ @compute @workgroup_size(WG_SIZE)
23
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
24
+ let i = gid.x;
25
+ if (i >= params.num_pairs) { return; }
26
+
27
+ let packed = bf16_packed[i];
28
+
29
+ // Low 16 bits = first bf16, high 16 bits = second bf16
30
+ let bf16_low = packed & 0xFFFFu;
31
+ let bf16_high = (packed >> 16u) & 0xFFFFu;
32
+
33
+ // BF16 → F32: shift left by 16 bits
34
+ f32_out[i * 2u] = bitcast<f32>(bf16_low << 16u);
35
+ f32_out[i * 2u + 1u] = bitcast<f32>(bf16_high << 16u);
36
+ }