-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
459 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import ctypes as ct | ||
import os | ||
from warnings import warn | ||
|
||
lib = ct.cdll.LoadLibrary(os.path.dirname(__file__) + '/libbitsandbytes.so') | ||
|
||
try: | ||
lib.cadam32bit_g32 | ||
COMPILED_WITH_CUDA = True | ||
except AttributeError: | ||
warn("The installed version of bitsandbytes was compiled without GPU support. " | ||
"8-bit optimizers and GPU quantization are unavailable.") | ||
COMPILED_WITH_CUDA = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <common.h> | ||
#include <float.h> | ||
|
||
void *quantize_block(void *arguments) { | ||
// 1. find absmax in block | ||
// 2. divide input value by absmax to normalize into [-1.0, 1.0] | ||
// 3. do binary search to find the closest value | ||
// 4. check minimal distance | ||
// 5. store index | ||
|
||
struct quantize_block_args *args = (quantize_block_args *) arguments; | ||
|
||
// 1. find absmax in block | ||
float absmax_block = -FLT_MAX; | ||
for (int i = args->block_idx; i < args->block_end; i++) | ||
absmax_block = fmax(absmax_block, fabs(args->A[i])); | ||
|
||
args->absmax[args->block_idx / BLOCK_SIZE] = absmax_block; | ||
|
||
for (int i = args->block_idx; i < args->block_end; i++) { | ||
// 2. divide input value by absmax to normalize into [-1.0, 1.0] | ||
// 3. do binary search to find the closest value | ||
float normed_value = args->A[i] / absmax_block; | ||
int idx = args->bin_searcher->scalar(normed_value); | ||
|
||
// 4. check minimal distance | ||
// The binary search returns always the value to the left, which might not be the closest value | ||
if (idx < 255) { | ||
float dist_left = fabs(normed_value - (args->code[idx])); | ||
float dist_right = fabs(normed_value - (args->code[idx + 1])); | ||
if (dist_right < dist_left) { idx += 1; } | ||
} | ||
|
||
// 5. store index | ||
args->out[i] = (unsigned char) idx; | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <BinSearch.h> | ||
|
||
#ifndef common | ||
#define common | ||
|
||
using namespace BinSearch; | ||
|
||
struct quantize_block_args { | ||
BinAlgo<Scalar, float, Direct2> *bin_searcher; | ||
float *code; | ||
float *A; | ||
float *absmax; | ||
unsigned char *out; | ||
int block_end; | ||
int block_idx; | ||
int threadidx; | ||
}; | ||
|
||
#define BLOCK_SIZE 4096 | ||
|
||
void *quantize_block(void *arguments); | ||
|
||
#endif |
Oops, something went wrong.