File size: 1,612 Bytes
7586fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <omp.h>

void set_num_threads(int n_threads)
{
    omp_set_num_threads(n_threads);
}

int get_num_threads()
{
    return omp_get_num_threads();
}

void compress_int4_weight(void *weight, void *out, int n, int m)
{
    #pragma omp parallel for
	for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            (*(unsigned char*)(out + sizeof(unsigned char) * (i * m + j))) |= ((*(unsigned char*)(weight + sizeof(unsigned char) * (i * (m << 1) + (j << 1)))) << 4);
            (*(unsigned char*)(out + sizeof(unsigned char) * (i * m + j))) |= (((*(unsigned char*)(weight + sizeof(unsigned char) * (i * (m << 1) + ((j << 1) | 1)))) & 15));
        }
    }
}

void extract_int8_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
{
    #pragma omp parallel for
	for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            (*(float*)(out + sizeof(float) * (i * m + j))) = (*(float*)(scale_list + sizeof(float) * i)) * (*(char*)(weight + sizeof(char) * (i * m + j)));
    }
}

void extract_int4_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
{
    #pragma omp parallel for
	for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            (*(float*)(out + sizeof(float) * (i * (m << 1) + (j << 1)))) = (*(float*)(scale_list + sizeof(float) * i)) * ((*(char*)(weight + sizeof(char) * (i * m + j))) >> 4);
            (*(float*)(out + sizeof(float) * (i * (m << 1) + ((j << 1) | 1)))) = (*(float*)(scale_list + sizeof(float) * i)) * (((char)((*(unsigned char*)(weight + sizeof(char) * (i * m + j))) << 4))>> 4);
        }
    }
}