cpu_ops.cpp 2.38 KB
Newer Older
Max Ryabinin's avatar
Max Ryabinin committed
1
2
#include <BinSearch.h>
#include <common.h>
3
#include <thread>
Max Ryabinin's avatar
Max Ryabinin committed
4
5
6

using namespace BinSearch;

7
8
9
10
11
12
void dequantize_cpu(float *code, unsigned char *A, float *absmax, float *out, long long blocksize, long long n) {
    for (long long block_idx = 0; block_idx < n; block_idx += blocksize) {
        long long valid_items = n - block_idx >= blocksize ? blocksize : n - block_idx;
        long long block_end = block_idx + valid_items;
        for (long long i = block_idx; i < block_end; i++)
            out[i] = code[A[i]] * absmax[block_idx / blocksize];
Max Ryabinin's avatar
Max Ryabinin committed
13
14
15
    }
}

16
17
void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, long long blocksize, long long n)
{
Max Ryabinin's avatar
Max Ryabinin committed
18
19
20
21

    // the default code is has range [-0.993, 1.0] which can cause an error in the binary search algorithm used below
    code[0] = -1.0f;

22
23
    long long num_blocks = n / blocksize;
    num_blocks += n % blocksize == 0 ? 0 : 1;
Max Ryabinin's avatar
Max Ryabinin committed
24
25
26
27

    const uint32 elements_code = 256;
    BinAlgo<Scalar, float, Direct2> bin_searcher(code, elements_code);

28
    int thread_wave_size = 256;
29
    // we chunk the threads into waves of 256 since the max limit is
30
31
32
    // between 16k and 64k on Linux (we reach this when running BLOOM-176B with a large batch size)
    for(long long offset = 0; offset < num_blocks; offset+=thread_wave_size)
    {
33
      long long valid_chunks = num_blocks - offset >= thread_wave_size ? thread_wave_size : num_blocks - offset;
34
35
      std::vector<std::thread> threads(valid_chunks);
      std::vector<quantize_block_args> args(valid_chunks);
Max Ryabinin's avatar
Max Ryabinin committed
36

37
38
39
40
41
42
      int chunks_processed = 0;
      for(long long block_idx = offset*blocksize; block_idx < n; block_idx += blocksize)
      {
          long long valid_items = n - block_idx >= blocksize ? blocksize : n - block_idx;
          long long block_end = block_idx + valid_items;

43
44
45
46
47
48
49
50
51
52
53
54
          struct quantize_block_args& arg = args[chunks_processed];
          arg.bin_searcher = &bin_searcher;
          arg.code = code;
          arg.A = A;
          arg.absmax = absmax;
          arg.out = out;
          arg.block_end = block_end;
          arg.block_idx = block_idx;
          arg.threadidx = block_idx / blocksize;
          arg.blocksize = blocksize;

          threads[chunks_processed] = std::thread([arg] { quantize_block(arg); });
55
          chunks_processed += 1;
56
          if(chunks_processed == valid_chunks){ break; }
57
58
      }

59
      for (int i = 0; i < valid_chunks; i++)
James Wyatt's avatar
James Wyatt committed
60
          threads[i].join();
61
    }
Max Ryabinin's avatar
Max Ryabinin committed
62

Max Ryabinin's avatar
Max Ryabinin committed
63
}