-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathfloat_kernel.hip
More file actions
70 lines (66 loc) · 2.62 KB
/
Copy pathfloat_kernel.hip
File metadata and controls
70 lines (66 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "quant_kernel.h"
#include "bit_helper.hip"
// quantize a float into a floating point with [exp_bits] exponent and
// [man_bits] mantissa
__global__ void float_kernel_stochastic(float* __restrict__ a,
int* __restrict__ r,
float* o, int size,
int man_bits,
int exp_bits) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size) {
unsigned int rand_prob = (unsigned int) r[index];
unsigned int target,quantize_bits;
target = FLOAT_TO_BITS(&a[index]);
float quantized;
int target_exp = (target << 1 >> 1 >> 23) -127;
int min_exp = -((1 << (exp_bits - 1)) - 2);
bool subnormal = (target_exp < min_exp);
if (subnormal){
float shift_float,val;
int shift_bits = ((127+min_exp)<<23) | (target >> 31 <<31);
shift_float = BITS_TO_FLOAT(&shift_bits);
val=a[index]+shift_float;
target = FLOAT_TO_BITS(&val);
quantize_bits = round_bitwise_stochastic(target, rand_prob, man_bits);
quantized = BITS_TO_FLOAT(&quantize_bits) - shift_float;
}
else{
quantize_bits = round_bitwise_stochastic(target, rand_prob, man_bits);
quantize_bits = clip_exponent(exp_bits, man_bits, target, quantize_bits);
quantized = BITS_TO_FLOAT(&quantize_bits);
}
o[index] = quantized;
}
}
// quantize a float into a floating point with [exp_bits] exponent and
// [man_bits] mantissa
__global__ void float_kernel_nearest(float* __restrict__ a,
float* o, int size,
int man_bits,
int exp_bits) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size) {
unsigned int target,quantize_bits;
target = FLOAT_TO_BITS(&a[index]);
float quantized;
int target_exp = (target << 1 >> 1 >> 23) -127;
int min_exp = -((1 << (exp_bits - 1)) - 2);
bool subnormal = (target_exp < min_exp);
if (subnormal){
float shift_float,val;
int shift_bits = ((127+min_exp)<<23) | (target >> 31 <<31);
shift_float = BITS_TO_FLOAT(&shift_bits);
val=a[index]+shift_float;
target = FLOAT_TO_BITS(&val);
quantize_bits = round_bitwise_nearest(target, man_bits);
quantized = BITS_TO_FLOAT(&quantize_bits) - shift_float;
}
else{
quantize_bits = round_bitwise_nearest(target, man_bits);
quantize_bits = clip_exponent(exp_bits, man_bits, target, quantize_bits);
quantized = BITS_TO_FLOAT(&quantize_bits);
}
o[index] = quantized;
}
}