From 8258b4364a21a4da2572cb644d0926080c3268da Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:16:10 +0300 Subject: Add a CPU-only build option --- Makefile | 19 +- bitsandbytes/__init__.py | 11 +- bitsandbytes/cextension.py | 13 ++ bitsandbytes/functional.py | 52 ++--- bitsandbytes/optim/__init__.py | 20 +- bitsandbytes/optim/rmsprop.py | 2 +- csrc/common.cpp | 39 ++++ csrc/common.h | 23 +++ csrc/cpu_ops.cpp | 57 ++++++ csrc/cpu_ops.h | 9 + csrc/ops.cu | 451 +++++++++++++++++------------------------ csrc/ops.cuh | 10 - csrc/pythonInterface.c | 118 ++++++----- setup.py | 22 +- 14 files changed, 459 insertions(+), 387 deletions(-) create mode 100644 bitsandbytes/cextension.py create mode 100644 csrc/common.cpp create mode 100644 csrc/common.h create mode 100644 csrc/cpu_ops.cpp create mode 100644 csrc/cpu_ops.h diff --git a/Makefile b/Makefile index f21fde3..b58e233 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,10 @@ NVCC := $(CUDA_HOME)/bin/nvcc ########################################### CSRC := $(ROOT_DIR)/csrc -BUILD_DIR:= $(ROOT_DIR)/cuda_build +BUILD_DIR:= $(ROOT_DIR)/build FILES_CUDA := $(CSRC)/ops.cu $(CSRC)/kernels.cu -FILES_CPP := $(CSRC)/pythonInterface.c +FILES_CPP := $(CSRC)/common.cpp $(CSRC)/cpu_ops.cpp $(CSRC)/pythonInterface.c INCLUDE := -I $(CUDA_HOME)/include -I $(ROOT_DIR)/csrc -I $(CONDA_PREFIX)/include -I $(ROOT_DIR)/dependencies/cub -I $(ROOT_DIR)/include LIB := -L $(CUDA_HOME)/lib64 -lcudart -lcuda -lcublas -lcurand -lcusparse -L $(CONDA_PREFIX)/lib @@ -46,27 +46,30 @@ CC_CUDA11x += -gencode arch=compute_86,code=sm_86 all: $(ROOT_DIR)/dependencies/cub $(BUILD_DIR) env $(NVCC) $(COMPUTE_CAPABILITY) -Xcompiler '-fPIC' --use_fast_math -Xptxas=-v -dc $(FILES_CUDA) $(INCLUDE) $(LIB) --output-directory $(BUILD_DIR) $(NVCC) $(COMPUTE_CAPABILITY) -Xcompiler '-fPIC' -dlink $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o -o $(BUILD_DIR)/link.o - $(GPP) -std=c++14 -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + $(GPP) -std=c++14 -DBUILD_CUDA -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) cuda92: $(ROOT_DIR)/dependencies/cub $(BUILD_DIR) env $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA92) -Xcompiler '-fPIC' --use_fast_math -Xptxas=-v -dc $(FILES_CUDA) $(INCLUDE) $(LIB) --output-directory $(BUILD_DIR) $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA92) -Xcompiler '-fPIC' -dlink $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o -o $(BUILD_DIR)/link.o - $(GPP) -std=c++14 -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + $(GPP) -std=c++14 -DBUILD_CUDA -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) cuda10x: $(ROOT_DIR)/dependencies/cub $(BUILD_DIR) env $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA10x) -Xcompiler '-fPIC' --use_fast_math -Xptxas=-v -dc $(FILES_CUDA) $(INCLUDE) $(LIB) --output-directory $(BUILD_DIR) $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA10x) -Xcompiler '-fPIC' -dlink $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o -o $(BUILD_DIR)/link.o - $(GPP) -std=c++14 -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + $(GPP) -std=c++14 -DBUILD_CUDA -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) cuda110: $(BUILD_DIR) env $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA110) -Xcompiler '-fPIC' --use_fast_math -Xptxas=-v -dc $(FILES_CUDA) $(INCLUDE) $(LIB) --output-directory $(BUILD_DIR) $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA110) -Xcompiler '-fPIC' -dlink $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o -o $(BUILD_DIR)/link.o - $(GPP) -std=c++14 -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + $(GPP) -std=c++14 -DBUILD_CUDA -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) cuda11x: $(BUILD_DIR) env $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA11x) -Xcompiler '-fPIC' --use_fast_math -Xptxas=-v -dc $(FILES_CUDA) $(INCLUDE) $(LIB) --output-directory $(BUILD_DIR) $(NVCC) $(COMPUTE_CAPABILITY) $(CC_CUDA11x) -Xcompiler '-fPIC' -dlink $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o -o $(BUILD_DIR)/link.o - $(GPP) -std=c++14 -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + $(GPP) -std=c++14 -DBUILD_CUDA -shared -fPIC $(INCLUDE) $(BUILD_DIR)/ops.o $(BUILD_DIR)/kernels.o $(BUILD_DIR)/link.o $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so $(LIB) + +cpuonly: $(BUILD_DIR) env + $(GPP) -std=c++14 -shared -fPIC -I $(ROOT_DIR)/csrc -I $(ROOT_DIR)/include $(FILES_CPP) -o ./bitsandbytes/libbitsandbytes.so env: @echo "ENVIRONMENT" @@ -80,7 +83,7 @@ env: @echo "============================" $(BUILD_DIR): - mkdir -p cuda_build + mkdir -p build mkdir -p dependencies $(ROOT_DIR)/dependencies/cub: diff --git a/bitsandbytes/__init__.py b/bitsandbytes/__init__.py index 6e29322..22fb841 100644 --- a/bitsandbytes/__init__.py +++ b/bitsandbytes/__init__.py @@ -2,9 +2,14 @@ # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -from .optim import adam + from .nn import modules -__pdoc__ = {'libBitsNBytes' : False, +from cextension import COMPILED_WITH_CUDA + +if COMPILED_WITH_CUDA: + from .optim import adam + +__pdoc__ = {'libBitsNBytes': False, 'optim.optimizer.Optimizer8bit': False, 'optim.optimizer.MockArgs': False - } + } diff --git a/bitsandbytes/cextension.py b/bitsandbytes/cextension.py new file mode 100644 index 0000000..63d627e --- /dev/null +++ b/bitsandbytes/cextension.py @@ -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 diff --git a/bitsandbytes/functional.py b/bitsandbytes/functional.py index fbd7564..68b1d78 100644 --- a/bitsandbytes/functional.py +++ b/bitsandbytes/functional.py @@ -3,38 +3,38 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import ctypes as ct -import os import random from typing import Tuple import torch from torch import Tensor -lib = ct.cdll.LoadLibrary(os.path.dirname(__file__) + '/libbitsandbytes.so') +from cextension import lib, COMPILED_WITH_CUDA + name2qmap = {} -''' C FUNCTIONS FOR OPTIMIZERS ''' - -str2optimizer32bit = {} -str2optimizer32bit['adam'] = (lib.cadam32bit_g32, lib.cadam32bit_g16) -str2optimizer32bit['momentum'] = (lib.cmomentum32bit_g32, lib.cmomentum32bit_g16) -str2optimizer32bit['rmsprop'] = (lib.crmsprop32bit_g32, lib.crmsprop32bit_g16) -str2optimizer32bit['adagrad'] = (lib.cadagrad32bit_g32, lib.cadagrad32bit_g16) -str2optimizer32bit['lars'] = (lib.cmomentum32bit_g32, lib.cmomentum32bit_g16) -str2optimizer32bit['lamb'] = (lib.cadam32bit_g32, lib.cadam32bit_g16) - -str2optimizer8bit = {} -str2optimizer8bit['adam'] = (lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16) -str2optimizer8bit['momentum'] = (lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16) -str2optimizer8bit['rmsprop'] = (lib.crmsprop_static_8bit_g32, lib.crmsprop_static_8bit_g16) -str2optimizer8bit['lamb'] = (lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16) -str2optimizer8bit['lars'] = (lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16) - -str2optimizer8bit_blockwise = {} -str2optimizer8bit_blockwise['adam'] = (lib.cadam_8bit_blockwise_fp32, lib.cadam_8bit_blockwise_fp16) -str2optimizer8bit_blockwise['momentum'] = (lib.cmomentum_8bit_blockwise_fp32, lib.cmomentum_8bit_blockwise_fp16) -str2optimizer8bit_blockwise['rmsprop'] = (lib.crmsprop_8bit_blockwise_fp32, lib.crmsprop_8bit_blockwise_fp16) -str2optimizer8bit_blockwise['adagrad'] = (lib.cadagrad_8bit_blockwise_fp32, lib.cadagrad_8bit_blockwise_fp16) +if COMPILED_WITH_CUDA: + ''' C FUNCTIONS FOR OPTIMIZERS ''' + str2optimizer32bit = {} + str2optimizer32bit['adam'] = (lib.cadam32bit_g32, lib.cadam32bit_g16) + str2optimizer32bit['momentum'] = (lib.cmomentum32bit_g32, lib.cmomentum32bit_g16) + str2optimizer32bit['rmsprop'] = (lib.crmsprop32bit_g32, lib.crmsprop32bit_g16) + str2optimizer32bit['adagrad'] = (lib.cadagrad32bit_g32, lib.cadagrad32bit_g16) + str2optimizer32bit['lars'] = (lib.cmomentum32bit_g32, lib.cmomentum32bit_g16) + str2optimizer32bit['lamb'] = (lib.cadam32bit_g32, lib.cadam32bit_g16) + + str2optimizer8bit = {} + str2optimizer8bit['adam'] = (lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16) + str2optimizer8bit['momentum'] = (lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16) + str2optimizer8bit['rmsprop'] = (lib.crmsprop_static_8bit_g32, lib.crmsprop_static_8bit_g16) + str2optimizer8bit['lamb'] = (lib.cadam_static_8bit_g32, lib.cadam_static_8bit_g16) + str2optimizer8bit['lars'] = (lib.cmomentum_static_8bit_g32, lib.cmomentum_static_8bit_g16) + + str2optimizer8bit_blockwise = {} + str2optimizer8bit_blockwise['adam'] = (lib.cadam_8bit_blockwise_fp32, lib.cadam_8bit_blockwise_fp16) + str2optimizer8bit_blockwise['momentum'] = (lib.cmomentum_8bit_blockwise_fp32, lib.cmomentum_8bit_blockwise_fp16) + str2optimizer8bit_blockwise['rmsprop'] = (lib.crmsprop_8bit_blockwise_fp32, lib.crmsprop_8bit_blockwise_fp16) + str2optimizer8bit_blockwise['adagrad'] = (lib.cadagrad_8bit_blockwise_fp32, lib.cadagrad_8bit_blockwise_fp16) optimal_normal = [-0.9939730167388916, -0.8727636337280273, -0.8097418546676636, -0.7660024166107178, -0.7318882346153259, -0.6793879270553589, -0.657649040222168, -0.6385974884033203, -0.6211113333702087, -0.5901028513908386, -0.5762918591499329, -0.5630806684494019, -0.5509274005889893, -0.5394591689109802, -0.5283197164535522, -0.517780065536499, -0.5074946284294128, -0.4980469048023224, -0.48867011070251465, -0.48003149032592773, -0.47125306725502014, -0.4629971981048584, -0.4547359049320221, -0.446626216173172, -0.43902668356895447, -0.43158355355262756, -0.4244747757911682, -0.4173796474933624, -0.41038978099823, -0.4055633544921875, -0.4035947024822235, -0.39701032638549805, -0.39057496190071106, -0.38439232110977173, -0.3782760500907898, -0.3721940815448761, -0.3661896586418152, -0.3604033589363098, -0.354605108499527, -0.34892538189888, -0.34320303797721863, -0.3376772701740265, -0.3323028087615967, -0.3269782066345215, -0.32166096568107605, -0.316457599401474, -0.3112771809101105, -0.3061025142669678, -0.30106794834136963, -0.2961243987083435, -0.2912728488445282, -0.28644347190856934, -0.28165507316589355, -0.2769731283187866, -0.2722635865211487, -0.26779335737228394, -0.26314786076545715, -0.2586647868156433, -0.2541804611682892, -0.2496625930070877, -0.24527113139629364, -0.24097171425819397, -0.23659978806972504, -0.23218469321727753, -0.22799566388130188, -0.22380566596984863, -0.21965542435646057, -0.2154538631439209, -0.2113603949546814, -0.20735277235507965, -0.20334717631340027, -0.19932441413402557, -0.19530178606510162, -0.19136647880077362, -0.18736697733402252, -0.18337111175060272, -0.17951400578022003, -0.1757056713104248, -0.17182783782482147, -0.1680615097284317, -0.16431649029254913, -0.16053077578544617, -0.15685945749282837, -0.15298527479171753, -0.1493264138698578, -0.14566898345947266, -0.14188314974308014, -0.13819937407970428, -0.1344561129808426, -0.1306886374950409, -0.1271020770072937, -0.12346585839986801, -0.11981867253780365, -0.11614970862865448, -0.11256207525730133, -0.10889036953449249, -0.10525048524141312, -0.1016591489315033, -0.09824034571647644, -0.09469068050384521, -0.0911419615149498, -0.08773849159479141, -0.08416644483804703, -0.08071305602788925, -0.07720902562141418, -0.07371306419372559, -0.07019119709730148, -0.06673648208379745, -0.06329209357500076, -0.059800852090120316, -0.0564190037548542, -0.05296570807695389, -0.049522045999765396, -0.04609023034572601, -0.04262964054942131, -0.039246633648872375, -0.03577171266078949, -0.03236335143446922, -0.028855687007308006, -0.02542758360505104, -0.022069433704018593, -0.018754752352833748, -0.015386369079351425, -0.01194947212934494, -0.008439815603196621, -0.004995611496269703, -0.0016682245768606663, 0.0, 0.0015510577941313386, 0.005062474869191647, 0.008417150937020779, 0.011741090565919876, 0.015184164978563786, 0.018582714721560478, 0.02204744517803192, 0.025471193715929985, 0.02889077737927437, 0.0323684960603714, 0.03579240292310715, 0.039281025528907776, 0.0427563451230526, 0.04619763046503067, 0.04968220740556717, 0.05326594039797783, 0.05679265409708023, 0.060245808213949203, 0.06372645497322083, 0.06721872836351395, 0.0706876739859581, 0.0742349922657013, 0.07774098962545395, 0.08123527467250824, 0.08468879014253616, 0.08810535818338394, 0.09155989438295364, 0.09498448669910431, 0.0985206812620163, 0.10206405073404312, 0.10563778132200241, 0.10921968519687653, 0.11284469068050385, 0.11653254181146622, 0.12008969485759735, 0.12368203699588776, 0.1272617131471634, 0.13089501857757568, 0.134552001953125, 0.1382799744606018, 0.14194637537002563, 0.14563234150409698, 0.14930322766304016, 0.15303383767604828, 0.1567956507205963, 0.16050070524215698, 0.16431072354316711, 0.16813558340072632, 0.17204202711582184, 0.1758781224489212, 0.17973239719867706, 0.1836014688014984, 0.18753431737422943, 0.19138391315937042, 0.19535475969314575, 0.19931404292583466, 0.20333819091320038, 0.20738255977630615, 0.21152682602405548, 0.21568812429904938, 0.21978361904621124, 0.22393859922885895, 0.22814159095287323, 0.23241068422794342, 0.23675410449504852, 0.24123944342136383, 0.24569889903068542, 0.2500703036785126, 0.25904011726379395, 0.26349544525146484, 0.2682226300239563, 0.272907555103302, 0.2774306833744049, 0.28220856189727783, 0.2869136929512024, 0.2916390895843506, 0.29649388790130615, 0.30142995715141296, 0.3065022826194763, 0.3114383816719055, 0.31648796796798706, 0.3216581642627716, 0.32700115442276, 0.3322487473487854, 0.33778008818626404, 0.3431521952152252, 0.3487405776977539, 0.3543166518211365, 0.3601346015930176, 0.36605337262153625, 0.37217751145362854, 0.378179669380188, 0.3843980133533478, 0.3906566798686981, 0.39714935421943665, 0.40357843041419983, 0.4104187488555908, 0.4171563684940338, 0.42418959736824036, 0.43136918544769287, 0.4389212429523468, 0.44673123955726624, 0.45457619428634644, 0.4627031683921814, 0.47130417823791504, 0.4798591434955597, 0.48897242546081543, 0.4979848861694336, 0.5, 0.5076631307601929, 0.5177803635597229, 0.5282770991325378, 0.5392990112304688, 0.5506287813186646, 0.5632893443107605, 0.5764452815055847, 0.5903191566467285, 0.6051878333091736, 0.6209936141967773, 0.6382884979248047, 0.6573970913887024, 0.6795773506164551, 0.7037051916122437, 0.7327037453651428, 0.7677436470985413, 0.8111193776130676, 0.875165581703186, 1.0] @@ -138,7 +138,7 @@ def estimate_quantiles(A: Tensor, out: Tensor=None, offset: float=1/512) -> Tens elif A.dtype == torch.float16: lib.cestimate_quantiles_fp16(get_ptr(A), get_ptr(out), ct.c_float(offset), ct.c_int(A.numel())) else: - raise NotImplementError(f'Not supported data type {A.dtype}') + raise NotImplementedError(f'Not supported data type {A.dtype}') return out def quantize_blockwise(A: Tensor, code: Tensor=None, absmax: Tensor=None, rand=None, out: Tensor=None) -> Tensor: @@ -384,7 +384,7 @@ def optimizer_update_32bit(optimizer_name:str, g: Tensor, p: Tensor, state1: Ten param_norm = torch.norm(p.data.float()) if optimizer_name not in str2optimizer32bit: - raise NotImplementError(f'Optimizer not implemented: {optimizer_name}. Choices: {",".join(str2optimizer32bit.keys())}') + raise NotImplementedError(f'Optimizer not implemented: {optimizer_name}. Choices: {",".join(str2optimizer32bit.keys())}') if g.dtype == torch.float32 and state1.dtype == torch.float32: str2optimizer32bit[optimizer_name][0](get_ptr(g), get_ptr(p), get_ptr(state1), get_ptr(state2), get_ptr(unorm_vec), ct.c_float(max_unorm), diff --git a/bitsandbytes/optim/__init__.py b/bitsandbytes/optim/__init__.py index 5e73414..e833ecc 100644 --- a/bitsandbytes/optim/__init__.py +++ b/bitsandbytes/optim/__init__.py @@ -2,11 +2,15 @@ # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -from .adam import Adam, Adam8bit, Adam32bit -from .adamw import AdamW, AdamW8bit, AdamW32bit -from .sgd import SGD, SGD8bit, SGD32bit -from .lars import LARS, LARS8bit, LARS32bit, PytorchLARS -from .lamb import LAMB, LAMB8bit, LAMB32bit -from .rmsprop import RMSprop, RMSprop8bit, RMSprop32bit -from .adagrad import Adagrad, Adagrad8bit, Adagrad32bit -from .optimizer import GlobalOptimManager + +from bitsandbytes.cextension import COMPILED_WITH_CUDA + +if COMPILED_WITH_CUDA: + from .adam import Adam, Adam8bit, Adam32bit + from .adamw import AdamW, AdamW8bit, AdamW32bit + from .sgd import SGD, SGD8bit, SGD32bit + from .lars import LARS, LARS8bit, LARS32bit, PytorchLARS + from .lamb import LAMB, LAMB8bit, LAMB32bit + from .rmsprop import RMSprop, RMSprop8bit, RMSprop32bit + from .adagrad import Adagrad, Adagrad8bit, Adagrad32bit + from .optimizer import GlobalOptimManager diff --git a/bitsandbytes/optim/rmsprop.py b/bitsandbytes/optim/rmsprop.py index 7909d5d..0f1ffaa 100644 --- a/bitsandbytes/optim/rmsprop.py +++ b/bitsandbytes/optim/rmsprop.py @@ -31,6 +31,6 @@ class RMSprop32bit(Optimizer1State): if alpha == 0: raise NotImplementedError(f'RMSprop with alpha==0.0 is not supported!') if centered: - raise NotImplementError(f'Centered RMSprop is not supported!') + raise NotImplementedError(f'Centered RMSprop is not supported!') super(RMSprop32bit, self).__init__('rmsprop', params, lr, (alpha, momentum), eps, weight_decay, 32, args, min_8bit_size, percentile_clipping, block_wise) diff --git a/csrc/common.cpp b/csrc/common.cpp new file mode 100644 index 0000000..972602b --- /dev/null +++ b/csrc/common.cpp @@ -0,0 +1,39 @@ +#include +#include + +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; +} diff --git a/csrc/common.h b/csrc/common.h new file mode 100644 index 0000000..35f2463 --- /dev/null +++ b/csrc/common.h @@ -0,0 +1,23 @@ +#include + +#ifndef common +#define common + +using namespace BinSearch; + +struct quantize_block_args { + BinAlgo *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 \ No newline at end of file diff --git a/csrc/cpu_ops.cpp b/csrc/cpu_ops.cpp new file mode 100644 index 0000000..11a2615 --- /dev/null +++ b/csrc/cpu_ops.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace BinSearch; + +void dequantize_cpu(float *code, unsigned char *A, float *absmax, float *out, int n) { + for (int block_idx = 0; block_idx < n; block_idx += BLOCK_SIZE) { + int valid_items = n - block_idx >= BLOCK_SIZE ? BLOCK_SIZE : n - block_idx; + int block_end = block_idx + valid_items; + for (int i = block_idx; i < block_end; i++) + out[i] = code[A[i]] * absmax[block_idx / BLOCK_SIZE]; + } +} + +void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, int n) { + + // 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; + + int num_blocks = n / BLOCK_SIZE; + num_blocks += n % BLOCK_SIZE == 0 ? 0 : 1; + + pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * num_blocks); + struct quantize_block_args **args = (quantize_block_args **) malloc(num_blocks * sizeof(quantize_block_args *)); + + for (int i = 0; i < num_blocks; i++) + args[i] = (quantize_block_args *) malloc(sizeof(quantize_block_args)); + + const uint32 elements_code = 256; + BinAlgo bin_searcher(code, elements_code); + + for (int block_idx = 0; block_idx < n; block_idx += BLOCK_SIZE) { + int valid_items = n - block_idx >= BLOCK_SIZE ? BLOCK_SIZE : n - block_idx; + int block_end = block_idx + valid_items; + + struct quantize_block_args *arg = args[block_idx / BLOCK_SIZE]; + 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 / BLOCK_SIZE; + + pthread_create(&threads[block_idx / BLOCK_SIZE], NULL, &quantize_block, (void *) arg); + } + + for (int i = 0; i < num_blocks; i++) + int err = pthread_join(threads[i], NULL); + + free(threads); + for (int i = 0; i < num_blocks; i++) + free(args[i]); + free(args); +} \ No newline at end of file diff --git a/csrc/cpu_ops.h b/csrc/cpu_ops.h new file mode 100644 index 0000000..57145a9 --- /dev/null +++ b/csrc/cpu_ops.h @@ -0,0 +1,9 @@ +#ifndef BITSANDBYTES_CPU_OPS_H +#define BITSANDBYTES_CPU_OPS_H + + +void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, int n); + +void dequantize_cpu(float *code, unsigned char *A, float *absmax, float *out, int n); + +#endif diff --git a/csrc/ops.cu b/csrc/ops.cu index 9691241..464ea2e 100644 --- a/csrc/ops.cu +++ b/csrc/ops.cu @@ -8,251 +8,141 @@ #include #include #include +#include using namespace BinSearch; using std::cout; using std::endl; -#define BLOCK_SIZE 4096 - -struct quantize_block_args -{ - BinAlgo *bin_searcher; - float *code; - float *A; - float *absmax; - unsigned char *out; - int block_end; - int block_idx; - int threadidx; -}; - -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; +void histogramScatterAdd2D(float *histogram, int *index1, int *index2, float *src, int maxidx1, int n) { + int threads = 512; + int blocks = n / threads; + blocks = n % threads == 0 ? blocks : blocks + 1; + kHistogramScatterAdd2D<<>>(histogram, index1, index2, src, maxidx1, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, int n) -{ - - // 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; - - int num_blocks = n/BLOCK_SIZE; - num_blocks += n % BLOCK_SIZE == 0 ? 0 : 1; - - pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t)*num_blocks); - struct quantize_block_args **args = (quantize_block_args**)malloc(num_blocks*sizeof(quantize_block_args*)); - - for(int i = 0; i < num_blocks; i++) - args[i] = (quantize_block_args*)malloc(sizeof(quantize_block_args)); - - const uint32 elements_code = 256; - BinAlgo bin_searcher(code, elements_code); - - for(int block_idx = 0; block_idx < n; block_idx+=BLOCK_SIZE) - { - int valid_items = n-block_idx >= BLOCK_SIZE ? BLOCK_SIZE : n - block_idx; - int block_end = block_idx + valid_items; - - struct quantize_block_args *arg = args[block_idx/BLOCK_SIZE]; - 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/BLOCK_SIZE; - - pthread_create(&threads[block_idx/BLOCK_SIZE], NULL, &quantize_block, (void *)arg); - } - - for(int i = 0; i < num_blocks; i++) - int err = pthread_join(threads[i], NULL); - - free(threads); - for(int i = 0; i < num_blocks; i++) - free(args[i]); - free(args); -} - - -void dequantize_cpu(float *code, unsigned char *A, float *absmax, float *out, int n) -{ - for(int block_idx = 0; block_idx < n; block_idx+=BLOCK_SIZE) - { - int valid_items = n-block_idx >= BLOCK_SIZE ? BLOCK_SIZE : n - block_idx; - int block_end = block_idx + valid_items; - for (int i = block_idx; i < block_end; i++) - out[i] = code[A[i]]*absmax[block_idx/BLOCK_SIZE]; - } +template +void estimateQuantiles(T *A, float *code, float offset, int n) { + int blocks = n / 4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + CUDA_CHECK_RETURN(cudaMemset(code, 0, 256 * sizeof(float))); + kEstimateQuantiles < T ><<>>(A, code, offset, std::numeric_limits::max(), n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void histogramScatterAdd2D(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n) -{ - int threads = 512; - int blocks = n/threads; - blocks = n % threads == 0 ? blocks : blocks + 1; - kHistogramScatterAdd2D<<>>(histogram, index1, index2, src, maxidx1, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +void quantize(float *code, float *A, unsigned char *out, int n) { + int blocks = n / 1024; + blocks = n % 1024 == 0 ? blocks : blocks + 1; + kQuantize<<>>(code, A, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -template void estimateQuantiles(T *A, float *code, float offset, int n) -{ - int blocks = n/4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - CUDA_CHECK_RETURN(cudaMemset(code, 0, 256*sizeof(float))); - kEstimateQuantiles<<>>(A, code, offset, std::numeric_limits::max(), n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +void dequantize(float *code, unsigned char *A, float *out, int n) { + int blocks = n / 1024; + blocks = n % 1024 == 0 ? blocks : blocks + 1; + kDequantize<<>>(code, A, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void quantize(float *code, float *A, unsigned char *out, int n) -{ - int blocks = n/1024; - blocks = n % 1024 == 0 ? blocks : blocks + 1; - kQuantize<<>>(code, A, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template +void quantizeBlockwise(float *code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) { + int blocks = n / 4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + kQuantizeBlockwise < T, 4096, 4, STOCHASTIC ><<>>(code, A, absmax, out, rand, rand_offset, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void dequantize(float *code, unsigned char *A, float *out, int n) -{ - int blocks = n/1024; - blocks = n % 1024 == 0 ? blocks : blocks + 1; - kDequantize<<>>(code, A, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template +void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, T *out, int blocksize, const int n) { + int blocks = n / blocksize; + blocks = n % blocksize == 0 ? blocks : blocks + 1; + if (blocksize == 4096) + kDequantizeBlockwise < T, 4096, 1024, 4 ><<>>(code, A, absmax, out, n); + else if (blocksize == 2048) + kDequantizeBlockwise < T, 2048, 512, 4 ><<>>(code, A, absmax, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -template void quantizeBlockwise(float * code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) -{ - int blocks = n/4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - kQuantizeBlockwise<<>>(code, A, absmax, out, rand, rand_offset, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); -} - -template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, T *out, int blocksize, const int n) -{ - int blocks = n/blocksize; - blocks = n % blocksize == 0 ? blocks : blocks + 1; - if(blocksize == 4096) - kDequantizeBlockwise<<>>(code, A, absmax, out, n); - else if(blocksize == 2048) - kDequantizeBlockwise<<>>(code, A, absmax, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); -} - -template void optimizer32bit(T* g, T* p, - float* state1, float* state2, float *unorm, float max_unorm, float param_norm, - const float beta1, const float beta2, const float eps, const float weight_decay, - const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) -{ - int blocks = n/4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - switch(OPTIMIZER) - { - case ADAM: - if(max_unorm > 0.0f) - { - CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); - kPreconditionOptimizer32bit2State<<>>(g, p, state1, state2, unorm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - } - kOptimizer32bit2State<<>>(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - - if(max_unorm > 0.0f) - { - CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); - kPreconditionOptimizer32bit1State<<>>(g, p, state1, unorm, beta1, eps, weight_decay, step, lr, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - } - - kOptimizer32bit1State<<>>(g, p, state1, unorm, max_unorm, param_norm, beta1, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - } +template +void optimizer32bit(T *g, T *p, + float *state1, float *state2, float *unorm, float max_unorm, float param_norm, + const float beta1, const float beta2, const float eps, const float weight_decay, + const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) { + int blocks = n / 4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + switch (OPTIMIZER) { + case ADAM: + if (max_unorm > 0.0f) { + CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); + kPreconditionOptimizer32bit2State < T, OPTIMIZER, 4096, + 8 ><<>>(g, p, state1, state2, unorm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + } + kOptimizer32bit2State < T, + OPTIMIZER ><<>>(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + + if (max_unorm > 0.0f) { + CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); + kPreconditionOptimizer32bit1State < T, OPTIMIZER, 4096, + 8 ><<>>(g, p, state1, unorm, beta1, eps, weight_decay, step, lr, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + } + + kOptimizer32bit1State < T, + OPTIMIZER ><<>>(g, p, state1, unorm, max_unorm, param_norm, beta1, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + } } -template void optimizerStatic8bit(T* p, T* g, - unsigned char* state1, unsigned char* state2, - float *unorm, float max_unorm, float param_norm, - float beta1, float beta2, - float eps, int step, float lr, - float* quantiles1, float* quantiles2, - float* max1, float* max2, float* new_max1, float* new_max2, - float weight_decay, - const float gnorm_scale, int n) -{ - int blocks = n/4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - - if(max_unorm > 0.0f){ CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); } - - switch(OPTIMIZER) - { - case ADAM: - CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1*sizeof(float))); - CUDA_CHECK_RETURN(cudaMemset(new_max2, 0, 1*sizeof(float))); - kPreconditionOptimizerStatic8bit2State<<>>(p, g, state1, state2, unorm, beta1, beta2, eps, step, quantiles1, quantiles2, max1, max2, new_max1, new_max2, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - kOptimizerStatic8bit2State<<>>(p, g, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1*sizeof(float))); - kPreconditionOptimizerStatic8bit1State<<>>(p, g, state1, unorm, beta1, eps, step, quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - kOptimizerStatic8bit1State<<>>(p, g, state1, unorm, max_unorm, param_norm, beta1, eps, step, lr, - quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - default: - break; - } +template +void optimizerStatic8bit(T *p, T *g, + unsigned char *state1, unsigned char *state2, + float *unorm, float max_unorm, float param_norm, + float beta1, float beta2, + float eps, int step, float lr, + float *quantiles1, float *quantiles2, + float *max1, float *max2, float *new_max1, float *new_max2, + float weight_decay, + const float gnorm_scale, int n) { + int blocks = n / 4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + + if (max_unorm > 0.0f) { CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); } + + switch (OPTIMIZER) { + case ADAM: + CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1 * sizeof(float))); + CUDA_CHECK_RETURN(cudaMemset(new_max2, 0, 1 * sizeof(float))); + kPreconditionOptimizerStatic8bit2State < T, + OPTIMIZER ><<>>(p, g, state1, state2, unorm, beta1, beta2, eps, step, quantiles1, quantiles2, max1, max2, new_max1, new_max2, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + kOptimizerStatic8bit2State < T, + OPTIMIZER ><<>>(p, g, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1 * sizeof(float))); + kPreconditionOptimizerStatic8bit1State < T, + OPTIMIZER ><<>>(p, g, state1, unorm, beta1, eps, step, quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + kOptimizerStatic8bit1State < T, OPTIMIZER ><<>>(p, g, state1, unorm, max_unorm, param_norm, beta1, eps, step, lr, + quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + default: + break; + } } #define BLOCKSIZE_2STATE 2048 @@ -260,42 +150,43 @@ template void optimizerStatic8bit(T* p, T* g, #define BLOCKSIZE_1STATE 2048 #define NUM_1STATE 8 -template void optimizerStatic8bitBlockwise(T* p, T* g, - unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, - float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n) -{ - - int blocks = 0; - switch(OPTIMIZER) - { - case ADAM: - blocks = n/BLOCKSIZE_2STATE; - blocks = n % BLOCKSIZE_2STATE == 0 ? blocks : blocks + 1; - kOptimizerStatic8bit2StateBlockwise<<>>(p, g, state1, state2, beta1, beta2, eps, step, lr, - quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - blocks = n/BLOCKSIZE_1STATE; - blocks = n % BLOCKSIZE_1STATE == 0 ? blocks : blocks + 1; - kOptimizerStatic8bit1StateBlockwise<<>>(p, g, state1, beta1, beta2, eps, step, lr, - quantiles1, absmax1, weight_decay, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - } +template +void optimizerStatic8bitBlockwise(T *p, T *g, + unsigned char *state1, unsigned char *state2, float beta1, float beta2, float eps, int step, float lr, + float *quantiles1, float *quantiles2, float *absmax1, float *absmax2, float weight_decay, + const float gnorm_scale, bool skip_zeros, int n) { + + int blocks = 0; + switch (OPTIMIZER) { + case ADAM: + blocks = n / BLOCKSIZE_2STATE; + blocks = n % BLOCKSIZE_2STATE == 0 ? blocks : blocks + 1; + kOptimizerStatic8bit2StateBlockwise < T, OPTIMIZER, BLOCKSIZE_2STATE, NUM_2STATE ><<>>(p, g, state1, state2, beta1, beta2, eps, step, lr, + quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + blocks = n / BLOCKSIZE_1STATE; + blocks = n % BLOCKSIZE_1STATE == 0 ? blocks : blocks + 1; + kOptimizerStatic8bit1StateBlockwise < T, OPTIMIZER, BLOCKSIZE_1STATE, NUM_1STATE ><<>>(p, g, state1, beta1, beta2, eps, step, lr, + quantiles1, absmax1, weight_decay, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + } } - -template void percentileClipping(T * g, float *gnorm_vec, int step, const int n) -{ - int blocks = n/2048; - blocks = n % 2048 == 0 ? blocks : blocks + 1; - CUDA_CHECK_RETURN(cudaMemset(&gnorm_vec[step % 100], 0, 1*sizeof(float))); - kPercentileClipping<<>>(g, gnorm_vec, step, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template +void percentileClipping(T *g, float *gnorm_vec, int step, const int n) { + int blocks = n / 2048; + blocks = n % 2048 == 0 ? blocks : blocks + 1; + CUDA_CHECK_RETURN(cudaMemset(&gnorm_vec[step % 100], 0, 1 * sizeof(float))); + kPercentileClipping < T, 2048, 4 ><<>>(g, gnorm_vec, step, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } @@ -304,13 +195,23 @@ template void percentileClipping(T * g, float *gnorm_vec, int step, //============================================================== template void estimateQuantiles(half *A, float *code, float offset, int n); + template void estimateQuantiles(float *A, float *code, float offset, int n); -template void quantizeBlockwise(float * code, half *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); -template void quantizeBlockwise(float * code, float *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); -template void quantizeBlockwise(float * code, half *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); -template void quantizeBlockwise(float * code, float *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); +template void +quantizeBlockwise(float *code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); + +template void +quantizeBlockwise(float *code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); + +template void +quantizeBlockwise(float *code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); + +template void +quantizeBlockwise(float *code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); + template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n); + template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n); #define MAKE_optimizer32bit(name, gtype) \ @@ -320,12 +221,19 @@ template void optimizer32bit(gtype* g, gtype* p, \ const int step, const float lr, const float gnorm_scale, const bool skip_zeros, const int n); MAKE_optimizer32bit(ADAM, half) + MAKE_optimizer32bit(ADAM, float) + MAKE_optimizer32bit(MOMENTUM, half) + MAKE_optimizer32bit(MOMENTUM, float) + MAKE_optimizer32bit(RMSPROP, half) + MAKE_optimizer32bit(RMSPROP, float) + MAKE_optimizer32bit(ADAGRAD, half) + MAKE_optimizer32bit(ADAGRAD, float) #define MAKE_optimizerStatic8bit(name, gtype) \ @@ -338,11 +246,17 @@ template void optimizerStatic8bit(gtype* p, gtype* g, unsigned char float weight_decay, \ const float gnorm_scale, int n); \ + MAKE_optimizerStatic8bit(ADAM, half) + MAKE_optimizerStatic8bit(ADAM, float) + MAKE_optimizerStatic8bit(MOMENTUM, half) + MAKE_optimizerStatic8bit(MOMENTUM, float) + MAKE_optimizerStatic8bit(RMSPROP, half) + MAKE_optimizerStatic8bit(RMSPROP, float) #define MAKE_optimizerStatic8bitBlockwise(gtype, optim_name) \ @@ -350,14 +264,23 @@ template void optimizerStatic8bitBlockwise(gtype* p, gtype* g unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, \ float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n); \ + MAKE_optimizerStatic8bitBlockwise(half, ADAM); + MAKE_optimizerStatic8bitBlockwise(float, ADAM); + MAKE_optimizerStatic8bitBlockwise(half, MOMENTUM); + MAKE_optimizerStatic8bitBlockwise(float, MOMENTUM); + MAKE_optimizerStatic8bitBlockwise(half, RMSPROP); + MAKE_optimizerStatic8bitBlockwise(float, RMSPROP); + MAKE_optimizerStatic8bitBlockwise(half, ADAGRAD); + MAKE_optimizerStatic8bitBlockwise(float, ADAGRAD); -template void percentileClipping(float * g, float *gnorm_vec, int step, const int n); -template void percentileClipping(half * g, float *gnorm_vec, int step, const int n); +template void percentileClipping(float *g, float *gnorm_vec, int step, const int n); + +template void percentileClipping(half *g, float *gnorm_vec, int step, const int n); diff --git a/csrc/ops.cuh b/csrc/ops.cuh index 1bc13fb..8fb4cec 100644 --- a/csrc/ops.cuh +++ b/csrc/ops.cuh @@ -68,16 +68,6 @@ template void optimizerStatic8bitBlockwise(T* p, T* g template void percentileClipping(T * g, float *gnorm_vec, int step, const int n); -void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, int n); -void dequantize_cpu(float *code, unsigned char *A, float *absmax, float *out, int n); - void histogramScatterAdd2D(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n); #endif - - - - - - - diff --git a/csrc/pythonInterface.c b/csrc/pythonInterface.c index e0b0d59..229b7ed 100644 --- a/csrc/pythonInterface.c +++ b/csrc/pythonInterface.c @@ -3,7 +3,10 @@ // This source code is licensed under the MIT license found in the // LICENSE file in the root directory of this source tree. +#if BUILD_CUDA #include +#endif +#include // We cannot call templated code from C, so we wrap the template in a C compatible call here if necessary. // We use macro functions to expand all the different optimizers. Looks ugly, and is ugly, but its better than to @@ -12,6 +15,7 @@ // UNMANGLED CALLS //=================================================================================== +#if BUILD_CUDA void estimateQuantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles(A, code, offset, n); } void estimateQuantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles(A, code, offset, n); } @@ -34,15 +38,15 @@ MAKE_FUNC32(adagrad, ADAGRAD, half, 16) #define MAKE_FUNC8(fname, oname, gtype, gbits) \ void fname##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ - float *unorm, float max_unorm, float param_norm, \ + float *unorm, float max_unorm, float param_norm, \ float beta1, float beta2, \ float eps, int step, float lr, \ float* quantiles1, float* quantiles2, \ float* max1, float* max2, float* new_max1, float* new_max2, \ float weight_decay, float gnorm_scale, int n) \ { \ - optimizerStatic8bit(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ + optimizerStatic8bit(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ } \ MAKE_FUNC8(adam, ADAM, float, 32) @@ -78,39 +82,41 @@ void quantizeBlockwise_stochastic_fp32(float * code, float *A, float *absmax, un void dequantizeBlockwise_fp16(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n){ dequantizeBlockwise(code, A, absmax, out, blocksize, n); } \ void dequantizeBlockwise_fp32(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n){ dequantizeBlockwise(code, A, absmax, out, blocksize, n); } +#endif extern "C" { - void cestimate_quantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles_fp32(A, code, offset, n); } - void cestimate_quantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles_fp16(A, code, offset, n); } - void cquantize(float *code, float *A, unsigned char *out, int n){ quantize(code, A, out, n); } - void cdequantize(float *code, unsigned char *A, float *out, int n){ dequantize(code, A, out, n); } - void cquantize_blockwise_fp16(float * code, half *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp16(code, A, absmax, out, n); } - void cquantize_blockwise_fp32(float * code, float *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp32(code, A, absmax, out, n); } - void cquantize_blockwise_stochastic_fp16(float * code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp16(code, A, absmax, out, rand, rand_offset, n); } - void cquantize_blockwise_stochastic_fp32(float * code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp32(code, A, absmax, out, rand, rand_offset, n); } - - void cdequantize_blockwise_fp16(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n){ dequantizeBlockwise_fp16(code, A, absmax, out, blocksize, n); } - void cdequantize_blockwise_fp32(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n){ dequantizeBlockwise_fp32(code, A, absmax, out, blocksize, n); } - - #define MAKE_CFUNC32(name, gtype, gbits) \ - void c##name##32bit_g##gbits(gtype *g, gtype *p, \ - float* state1, float* state2, float *unorm, float max_unorm, float param_norm, \ - const float beta1, const float beta2, const float eps, const float weight_decay, \ - const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) \ - { name##32bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); } \ - - MAKE_CFUNC32(adam, float, 32) - MAKE_CFUNC32(adam, half, 16) - MAKE_CFUNC32(momentum, float, 32) - MAKE_CFUNC32(momentum, half, 16) - MAKE_CFUNC32(rmsprop, float, 32) - MAKE_CFUNC32(rmsprop, half, 16) - MAKE_CFUNC32(adagrad, float, 32) - MAKE_CFUNC32(adagrad, half, 16) - - #define MAKE_CFUNC8(name, gtype, gbits) \ - void c##name##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ +#if BUILD_CUDA +void cestimate_quantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles_fp32(A, code, offset, n); } +void cestimate_quantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles_fp16(A, code, offset, n); } +void cquantize(float *code, float *A, unsigned char *out, int n){ quantize(code, A, out, n); } +void cdequantize(float *code, unsigned char *A, float *out, int n){ dequantize(code, A, out, n); } +void cquantize_blockwise_fp16(float * code, half *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp16(code, A, absmax, out, n); } +void cquantize_blockwise_fp32(float * code, float *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp32(code, A, absmax, out, n); } +void cquantize_blockwise_stochastic_fp16(float * code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp16(code, A, absmax, out, rand, rand_offset, n); } +void cquantize_blockwise_stochastic_fp32(float * code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp32(code, A, absmax, out, rand, rand_offset, n); } + +void cdequantize_blockwise_fp16(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n){ dequantizeBlockwise_fp16(code, A, absmax, out, blocksize, n); } +void cdequantize_blockwise_fp32(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n){ dequantizeBlockwise_fp32(code, A, absmax, out, blocksize, n); } + +#define MAKE_CFUNC32(name, gtype, gbits) \ + void c##name##32bit_g##gbits(gtype *g, gtype *p, \ + float* state1, float* state2, float *unorm, float max_unorm, float param_norm, \ + const float beta1, const float beta2, const float eps, const float weight_decay, \ + const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) \ + { name##32bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); } \ + +MAKE_CFUNC32(adam, float, 32) +MAKE_CFUNC32(adam, half, 16) +MAKE_CFUNC32(momentum, float, 32) +MAKE_CFUNC32(momentum, half, 16) +MAKE_CFUNC32(rmsprop, float, 32) +MAKE_CFUNC32(rmsprop, half, 16) +MAKE_CFUNC32(adagrad, float, 32) +MAKE_CFUNC32(adagrad, half, 16) + +#define MAKE_CFUNC8(name, gtype, gbits) \ + void c##name##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ float *unorm, float max_unorm, float param_norm, \ float beta1, float beta2, \ float eps, int step, float lr, \ @@ -118,40 +124,40 @@ extern "C" float* max1, float* max2, float* new_max1, float* new_max2, \ float weight_decay, float gnorm_scale, int n) \ { \ - name##_static_8bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ + name##_static_8bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ } \ - MAKE_CFUNC8(adam, float, 32) - MAKE_CFUNC8(adam, half, 16) - MAKE_CFUNC8(momentum, float, 32) - MAKE_CFUNC8(momentum, half, 16) - MAKE_CFUNC8(rmsprop, float, 32) - MAKE_CFUNC8(rmsprop, half, 16) +MAKE_CFUNC8(adam, float, 32) +MAKE_CFUNC8(adam, half, 16) +MAKE_CFUNC8(momentum, float, 32) +MAKE_CFUNC8(momentum, half, 16) +MAKE_CFUNC8(rmsprop, float, 32) +MAKE_CFUNC8(rmsprop, half, 16) - #define MAKE_CBLOCKWISE8(fname, optim_name, gtype, gbits) \ +#define MAKE_CBLOCKWISE8(fname, optim_name, gtype, gbits) \ void c##fname##_8bit_blockwise_fp##gbits(gtype* p, gtype* g, \ unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, \ float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n) \ { fname##_8bit_blockwise_fp##gbits(p, g, state1, state2, beta1, beta2, eps, step, lr, quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); } \ - MAKE_CBLOCKWISE8(adam, ADAM, half, 16) - MAKE_CBLOCKWISE8(adam, ADAM, float, 32) - MAKE_CBLOCKWISE8(momentum, MOMENTUM, half, 16) - MAKE_CBLOCKWISE8(momentum, MOMENTUM, float, 32) - MAKE_CBLOCKWISE8(rmsprop, RMSPROP, half, 16) - MAKE_CBLOCKWISE8(rmsprop, RMSPROP, float, 32) - MAKE_CBLOCKWISE8(adagrad, ADAGRAD, half, 16) - MAKE_CBLOCKWISE8(adagrad, ADAGRAD, float, 32) +MAKE_CBLOCKWISE8(adam, ADAM, half, 16) +MAKE_CBLOCKWISE8(adam, ADAM, float, 32) +MAKE_CBLOCKWISE8(momentum, MOMENTUM, half, 16) +MAKE_CBLOCKWISE8(momentum, MOMENTUM, float, 32) +MAKE_CBLOCKWISE8(rmsprop, RMSPROP, half, 16) +MAKE_CBLOCKWISE8(rmsprop, RMSPROP, float, 32) +MAKE_CBLOCKWISE8(adagrad, ADAGRAD, half, 16) +MAKE_CBLOCKWISE8(adagrad, ADAGRAD, float, 32) - void cpercentile_clipping_g32(float * g, float *gnorm_vec, int step, const int n){ percentileClipping_g32(g, gnorm_vec, step, n); } - void cpercentile_clipping_g16(half * g, float *gnorm_vec, int step, const int n){ percentileClipping_g16(g, gnorm_vec, step, n); } +void cpercentile_clipping_g32(float * g, float *gnorm_vec, int step, const int n){ percentileClipping_g32(g, gnorm_vec, step, n); } +void cpercentile_clipping_g16(half * g, float *gnorm_vec, int step, const int n){ percentileClipping_g16(g, gnorm_vec, step, n); } +void chistogram_scatter_add_2d(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n){ histogramScatterAdd2D(histogram, index1, index2, src, maxidx1, n); } +#endif - void cquantize_blockwise_cpu_fp32(float *code, float *A, float *absmax, unsigned char *out, const int n){ quantize_cpu(code, A, absmax, out, n); } - void cdequantize_blockwise_cpu_fp32(float *code, unsigned char *A, float *absmax, float *out, const int n){ dequantize_cpu(code, A, absmax, out, n); } - - void chistogram_scatter_add_2d(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n){ histogramScatterAdd2D(histogram, index1, index2, src, maxidx1, n); } +void cquantize_blockwise_cpu_fp32(float *code, float *A, float *absmax, unsigned char *out, const int n){ quantize_cpu(code, A, absmax, out, n); } +void cdequantize_blockwise_cpu_fp32(float *code, unsigned char *A, float *absmax, float *out, const int n){ dequantize_cpu(code, A, absmax, out, n); } } diff --git a/setup.py b/setup.py index 59cd78e..2402c02 100644 --- a/setup.py +++ b/setup.py @@ -6,27 +6,27 @@ import os from setuptools import setup, find_packages - def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() +version = os.getenv("CUDA_VERSION", "cpu") + setup( - name = f"bitsandbytes-cuda{os.environ['CUDA_VERSION']}", - version = "0.26.0", - author = "Tim Dettmers", - author_email = "dettmers@cs.washington.edu", - description = ("8-bit optimizers and quantization routines."), - license = "MIT", - keywords = "gpu optimizers optimization 8-bit quantization compression", - url = "http://packages.python.org/bitsandbytes", + name="bitsandbytes", + version=f"0.26.0+{version}", + author="Tim Dettmers", + author_email="dettmers@cs.washington.edu", + description="8-bit optimizers and quantization routines.", + license="MIT", + keywords="gpu optimizers optimization 8-bit quantization compression", + url="http://packages.python.org/bitsandbytes", packages=find_packages(), package_data={'': ['libbitsandbytes.so']}, long_description=read('README.md'), - long_description_content_type = 'text/markdown', + long_description_content_type='text/markdown', classifiers=[ "Development Status :: 4 - Beta", 'Topic :: Scientific/Engineering :: Artificial Intelligence' ], ) - -- cgit v1.2.3 From e4cf33f2a38e54ca2a523bc5d5923237c68b0112 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:25:44 +0300 Subject: Fix imports --- bitsandbytes/__init__.py | 2 +- bitsandbytes/functional.py | 2 +- bitsandbytes/optim/__init__.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bitsandbytes/__init__.py b/bitsandbytes/__init__.py index 22fb841..02ca804 100644 --- a/bitsandbytes/__init__.py +++ b/bitsandbytes/__init__.py @@ -4,7 +4,7 @@ # LICENSE file in the root directory of this source tree. from .nn import modules -from cextension import COMPILED_WITH_CUDA +from .cextension import COMPILED_WITH_CUDA if COMPILED_WITH_CUDA: from .optim import adam diff --git a/bitsandbytes/functional.py b/bitsandbytes/functional.py index 68b1d78..ab4e565 100644 --- a/bitsandbytes/functional.py +++ b/bitsandbytes/functional.py @@ -9,7 +9,7 @@ from typing import Tuple import torch from torch import Tensor -from cextension import lib, COMPILED_WITH_CUDA +from .cextension import lib, COMPILED_WITH_CUDA name2qmap = {} diff --git a/bitsandbytes/optim/__init__.py b/bitsandbytes/optim/__init__.py index e833ecc..42b5bc0 100644 --- a/bitsandbytes/optim/__init__.py +++ b/bitsandbytes/optim/__init__.py @@ -13,4 +13,5 @@ if COMPILED_WITH_CUDA: from .lamb import LAMB, LAMB8bit, LAMB32bit from .rmsprop import RMSprop, RMSprop8bit, RMSprop32bit from .adagrad import Adagrad, Adagrad8bit, Adagrad32bit - from .optimizer import GlobalOptimManager + +from .optimizer import GlobalOptimManager -- cgit v1.2.3 From 31ce1b3708751016bf5e14beff7ae0a99c975991 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:36:30 +0300 Subject: Reduce diff --- csrc/common.h | 2 +- csrc/cpu_ops.cpp | 2 +- csrc/ops.cu | 290 ++++++++++++++++++++++++------------------------------- 3 files changed, 129 insertions(+), 165 deletions(-) diff --git a/csrc/common.h b/csrc/common.h index 35f2463..2f25a58 100644 --- a/csrc/common.h +++ b/csrc/common.h @@ -20,4 +20,4 @@ struct quantize_block_args { void *quantize_block(void *arguments); -#endif \ No newline at end of file +#endif diff --git a/csrc/cpu_ops.cpp b/csrc/cpu_ops.cpp index 11a2615..89de52d 100644 --- a/csrc/cpu_ops.cpp +++ b/csrc/cpu_ops.cpp @@ -54,4 +54,4 @@ void quantize_cpu(float *code, float *A, float *absmax, unsigned char *out, int for (int i = 0; i < num_blocks; i++) free(args[i]); free(args); -} \ No newline at end of file +} diff --git a/csrc/ops.cu b/csrc/ops.cu index 464ea2e..b2a1105 100644 --- a/csrc/ops.cu +++ b/csrc/ops.cu @@ -46,103 +46,100 @@ void dequantize(float *code, unsigned char *A, float *out, int n) { CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -template -void quantizeBlockwise(float *code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) { - int blocks = n / 4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - kQuantizeBlockwise < T, 4096, 4, STOCHASTIC ><<>>(code, A, absmax, out, rand, rand_offset, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template void quantizeBlockwise(float * code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) +{ + int blocks = n/4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + kQuantizeBlockwise<<>>(code, A, absmax, out, rand, rand_offset, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -template -void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, T *out, int blocksize, const int n) { - int blocks = n / blocksize; - blocks = n % blocksize == 0 ? blocks : blocks + 1; - if (blocksize == 4096) - kDequantizeBlockwise < T, 4096, 1024, 4 ><<>>(code, A, absmax, out, n); - else if (blocksize == 2048) - kDequantizeBlockwise < T, 2048, 512, 4 ><<>>(code, A, absmax, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, T *out, int blocksize, const int n) +{ + int blocks = n/blocksize; + blocks = n % blocksize == 0 ? blocks : blocks + 1; + if(blocksize == 4096) + kDequantizeBlockwise<<>>(code, A, absmax, out, n); + else if(blocksize == 2048) + kDequantizeBlockwise<<>>(code, A, absmax, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -template -void optimizer32bit(T *g, T *p, - float *state1, float *state2, float *unorm, float max_unorm, float param_norm, - const float beta1, const float beta2, const float eps, const float weight_decay, - const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) { - int blocks = n / 4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - switch (OPTIMIZER) { - case ADAM: - if (max_unorm > 0.0f) { - CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); - kPreconditionOptimizer32bit2State < T, OPTIMIZER, 4096, - 8 ><<>>(g, p, state1, state2, unorm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - } - kOptimizer32bit2State < T, - OPTIMIZER ><<>>(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - - if (max_unorm > 0.0f) { - CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); - kPreconditionOptimizer32bit1State < T, OPTIMIZER, 4096, - 8 ><<>>(g, p, state1, unorm, beta1, eps, weight_decay, step, lr, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - } - - kOptimizer32bit1State < T, - OPTIMIZER ><<>>(g, p, state1, unorm, max_unorm, param_norm, beta1, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - } +template void optimizer32bit(T* g, T* p, + float* state1, float* state2, float *unorm, float max_unorm, float param_norm, + const float beta1, const float beta2, const float eps, const float weight_decay, + const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) +{ + int blocks = n/4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + switch(OPTIMIZER) + { + case ADAM: + if(max_unorm > 0.0f) + { + CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); + kPreconditionOptimizer32bit2State<<>>(g, p, state1, state2, unorm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + } + kOptimizer32bit2State<<>>(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + + if(max_unorm > 0.0f) + { + CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); + kPreconditionOptimizer32bit1State<<>>(g, p, state1, unorm, beta1, eps, weight_decay, step, lr, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + } + + kOptimizer32bit1State<<>>(g, p, state1, unorm, max_unorm, param_norm, beta1, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + } } -template -void optimizerStatic8bit(T *p, T *g, - unsigned char *state1, unsigned char *state2, - float *unorm, float max_unorm, float param_norm, - float beta1, float beta2, - float eps, int step, float lr, - float *quantiles1, float *quantiles2, - float *max1, float *max2, float *new_max1, float *new_max2, - float weight_decay, - const float gnorm_scale, int n) { - int blocks = n / 4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - - if (max_unorm > 0.0f) { CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1 * sizeof(float))); } - - switch (OPTIMIZER) { - case ADAM: - CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1 * sizeof(float))); - CUDA_CHECK_RETURN(cudaMemset(new_max2, 0, 1 * sizeof(float))); - kPreconditionOptimizerStatic8bit2State < T, - OPTIMIZER ><<>>(p, g, state1, state2, unorm, beta1, beta2, eps, step, quantiles1, quantiles2, max1, max2, new_max1, new_max2, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - kOptimizerStatic8bit2State < T, - OPTIMIZER ><<>>(p, g, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1 * sizeof(float))); - kPreconditionOptimizerStatic8bit1State < T, - OPTIMIZER ><<>>(p, g, state1, unorm, beta1, eps, step, quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - kOptimizerStatic8bit1State < T, OPTIMIZER ><<>>(p, g, state1, unorm, max_unorm, param_norm, beta1, eps, step, lr, - quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - default: - break; - } +template void optimizerStatic8bit(T* p, T* g, + unsigned char* state1, unsigned char* state2, + float *unorm, float max_unorm, float param_norm, + float beta1, float beta2, + float eps, int step, float lr, + float* quantiles1, float* quantiles2, + float* max1, float* max2, float* new_max1, float* new_max2, + float weight_decay, + const float gnorm_scale, int n) +{ + int blocks = n/4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + + if(max_unorm > 0.0f){ CUDA_CHECK_RETURN(cudaMemset(unorm, 0, 1*sizeof(float))); } + + switch(OPTIMIZER) + { + case ADAM: + CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1*sizeof(float))); + CUDA_CHECK_RETURN(cudaMemset(new_max2, 0, 1*sizeof(float))); + kPreconditionOptimizerStatic8bit2State<<>>(p, g, state1, state2, unorm, beta1, beta2, eps, step, quantiles1, quantiles2, max1, max2, new_max1, new_max2, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + kOptimizerStatic8bit2State<<>>(p, g, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + CUDA_CHECK_RETURN(cudaMemset(new_max1, 0, 1*sizeof(float))); + kPreconditionOptimizerStatic8bit1State<<>>(p, g, state1, unorm, beta1, eps, step, quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + kOptimizerStatic8bit1State<<>>(p, g, state1, unorm, max_unorm, param_norm, beta1, eps, step, lr, + quantiles1, max1, new_max1, weight_decay, gnorm_scale, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + default: + break; + } } #define BLOCKSIZE_2STATE 2048 @@ -150,43 +147,42 @@ void optimizerStatic8bit(T *p, T *g, #define BLOCKSIZE_1STATE 2048 #define NUM_1STATE 8 -template -void optimizerStatic8bitBlockwise(T *p, T *g, - unsigned char *state1, unsigned char *state2, float beta1, float beta2, float eps, int step, float lr, - float *quantiles1, float *quantiles2, float *absmax1, float *absmax2, float weight_decay, - const float gnorm_scale, bool skip_zeros, int n) { - - int blocks = 0; - switch (OPTIMIZER) { - case ADAM: - blocks = n / BLOCKSIZE_2STATE; - blocks = n % BLOCKSIZE_2STATE == 0 ? blocks : blocks + 1; - kOptimizerStatic8bit2StateBlockwise < T, OPTIMIZER, BLOCKSIZE_2STATE, NUM_2STATE ><<>>(p, g, state1, state2, beta1, beta2, eps, step, lr, - quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - case MOMENTUM: - case RMSPROP: - case ADAGRAD: - blocks = n / BLOCKSIZE_1STATE; - blocks = n % BLOCKSIZE_1STATE == 0 ? blocks : blocks + 1; - kOptimizerStatic8bit1StateBlockwise < T, OPTIMIZER, BLOCKSIZE_1STATE, NUM_1STATE ><<>>(p, g, state1, beta1, beta2, eps, step, lr, - quantiles1, absmax1, weight_decay, gnorm_scale, skip_zeros, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); - break; - } +template void optimizerStatic8bitBlockwise(T* p, T* g, + unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, + float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n) +{ + + int blocks = 0; + switch(OPTIMIZER) + { + case ADAM: + blocks = n/BLOCKSIZE_2STATE; + blocks = n % BLOCKSIZE_2STATE == 0 ? blocks : blocks + 1; + kOptimizerStatic8bit2StateBlockwise<<>>(p, g, state1, state2, beta1, beta2, eps, step, lr, + quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + case MOMENTUM: + case RMSPROP: + case ADAGRAD: + blocks = n/BLOCKSIZE_1STATE; + blocks = n % BLOCKSIZE_1STATE == 0 ? blocks : blocks + 1; + kOptimizerStatic8bit1StateBlockwise<<>>(p, g, state1, beta1, beta2, eps, step, lr, + quantiles1, absmax1, weight_decay, gnorm_scale, skip_zeros, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); + break; + } } -template -void percentileClipping(T *g, float *gnorm_vec, int step, const int n) { - int blocks = n / 2048; - blocks = n % 2048 == 0 ? blocks : blocks + 1; - CUDA_CHECK_RETURN(cudaMemset(&gnorm_vec[step % 100], 0, 1 * sizeof(float))); - kPercentileClipping < T, 2048, 4 ><<>>(g, gnorm_vec, step, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); + +template void percentileClipping(T * g, float *gnorm_vec, int step, const int n) +{ + int blocks = n/2048; + blocks = n % 2048 == 0 ? blocks : blocks + 1; + CUDA_CHECK_RETURN(cudaMemset(&gnorm_vec[step % 100], 0, 1*sizeof(float))); + kPercentileClipping<<>>(g, gnorm_vec, step, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } @@ -195,23 +191,13 @@ void percentileClipping(T *g, float *gnorm_vec, int step, const int n) { //============================================================== template void estimateQuantiles(half *A, float *code, float offset, int n); - template void estimateQuantiles(float *A, float *code, float offset, int n); -template void -quantizeBlockwise(float *code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); - -template void -quantizeBlockwise(float *code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); - -template void -quantizeBlockwise(float *code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); - -template void -quantizeBlockwise(float *code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n); - +template void quantizeBlockwise(float * code, half *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); +template void quantizeBlockwise(float * code, float *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); +template void quantizeBlockwise(float * code, half *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); +template void quantizeBlockwise(float * code, float *A, float *absmax, unsigned char *out, float* rand, int rand_offset, const int n); template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n); - template void dequantizeBlockwise(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n); #define MAKE_optimizer32bit(name, gtype) \ @@ -221,19 +207,12 @@ template void optimizer32bit(gtype* g, gtype* p, \ const int step, const float lr, const float gnorm_scale, const bool skip_zeros, const int n); MAKE_optimizer32bit(ADAM, half) - MAKE_optimizer32bit(ADAM, float) - MAKE_optimizer32bit(MOMENTUM, half) - MAKE_optimizer32bit(MOMENTUM, float) - MAKE_optimizer32bit(RMSPROP, half) - MAKE_optimizer32bit(RMSPROP, float) - MAKE_optimizer32bit(ADAGRAD, half) - MAKE_optimizer32bit(ADAGRAD, float) #define MAKE_optimizerStatic8bit(name, gtype) \ @@ -246,17 +225,11 @@ template void optimizerStatic8bit(gtype* p, gtype* g, unsigned char float weight_decay, \ const float gnorm_scale, int n); \ - MAKE_optimizerStatic8bit(ADAM, half) - MAKE_optimizerStatic8bit(ADAM, float) - MAKE_optimizerStatic8bit(MOMENTUM, half) - MAKE_optimizerStatic8bit(MOMENTUM, float) - MAKE_optimizerStatic8bit(RMSPROP, half) - MAKE_optimizerStatic8bit(RMSPROP, float) #define MAKE_optimizerStatic8bitBlockwise(gtype, optim_name) \ @@ -264,23 +237,14 @@ template void optimizerStatic8bitBlockwise(gtype* p, gtype* g unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, \ float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n); \ - MAKE_optimizerStatic8bitBlockwise(half, ADAM); - MAKE_optimizerStatic8bitBlockwise(float, ADAM); - MAKE_optimizerStatic8bitBlockwise(half, MOMENTUM); - MAKE_optimizerStatic8bitBlockwise(float, MOMENTUM); - MAKE_optimizerStatic8bitBlockwise(half, RMSPROP); - MAKE_optimizerStatic8bitBlockwise(float, RMSPROP); - MAKE_optimizerStatic8bitBlockwise(half, ADAGRAD); - MAKE_optimizerStatic8bitBlockwise(float, ADAGRAD); -template void percentileClipping(float *g, float *gnorm_vec, int step, const int n); - -template void percentileClipping(half *g, float *gnorm_vec, int step, const int n); +template void percentileClipping(float * g, float *gnorm_vec, int step, const int n); +template void percentileClipping(half * g, float *gnorm_vec, int step, const int n); -- cgit v1.2.3 From 4d1d5b569f55dd613bea26714eb1ad931a10be35 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:40:02 +0300 Subject: Reduce diff --- csrc/pythonInterface.c | 115 ++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/csrc/pythonInterface.c b/csrc/pythonInterface.c index 229b7ed..1f690c5 100644 --- a/csrc/pythonInterface.c +++ b/csrc/pythonInterface.c @@ -38,15 +38,15 @@ MAKE_FUNC32(adagrad, ADAGRAD, half, 16) #define MAKE_FUNC8(fname, oname, gtype, gbits) \ void fname##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ - float *unorm, float max_unorm, float param_norm, \ + float *unorm, float max_unorm, float param_norm, \ float beta1, float beta2, \ float eps, int step, float lr, \ float* quantiles1, float* quantiles2, \ float* max1, float* max2, float* new_max1, float* new_max2, \ float weight_decay, float gnorm_scale, int n) \ { \ - optimizerStatic8bit(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ + optimizerStatic8bit(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ } \ MAKE_FUNC8(adam, ADAM, float, 32) @@ -86,37 +86,37 @@ void dequantizeBlockwise_fp32(float *code, unsigned char *A, float *absmax, floa extern "C" { -#if BUILD_CUDA -void cestimate_quantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles_fp32(A, code, offset, n); } -void cestimate_quantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles_fp16(A, code, offset, n); } -void cquantize(float *code, float *A, unsigned char *out, int n){ quantize(code, A, out, n); } -void cdequantize(float *code, unsigned char *A, float *out, int n){ dequantize(code, A, out, n); } -void cquantize_blockwise_fp16(float * code, half *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp16(code, A, absmax, out, n); } -void cquantize_blockwise_fp32(float * code, float *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp32(code, A, absmax, out, n); } -void cquantize_blockwise_stochastic_fp16(float * code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp16(code, A, absmax, out, rand, rand_offset, n); } -void cquantize_blockwise_stochastic_fp32(float * code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp32(code, A, absmax, out, rand, rand_offset, n); } - -void cdequantize_blockwise_fp16(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n){ dequantizeBlockwise_fp16(code, A, absmax, out, blocksize, n); } -void cdequantize_blockwise_fp32(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n){ dequantizeBlockwise_fp32(code, A, absmax, out, blocksize, n); } - -#define MAKE_CFUNC32(name, gtype, gbits) \ - void c##name##32bit_g##gbits(gtype *g, gtype *p, \ - float* state1, float* state2, float *unorm, float max_unorm, float param_norm, \ - const float beta1, const float beta2, const float eps, const float weight_decay, \ - const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) \ - { name##32bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); } \ - -MAKE_CFUNC32(adam, float, 32) -MAKE_CFUNC32(adam, half, 16) -MAKE_CFUNC32(momentum, float, 32) -MAKE_CFUNC32(momentum, half, 16) -MAKE_CFUNC32(rmsprop, float, 32) -MAKE_CFUNC32(rmsprop, half, 16) -MAKE_CFUNC32(adagrad, float, 32) -MAKE_CFUNC32(adagrad, half, 16) - -#define MAKE_CFUNC8(name, gtype, gbits) \ - void c##name##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ + if #BUILD_CUDA + void cestimate_quantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles_fp32(A, code, offset, n); } + void cestimate_quantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles_fp16(A, code, offset, n); } + void cquantize(float *code, float *A, unsigned char *out, int n){ quantize(code, A, out, n); } + void cdequantize(float *code, unsigned char *A, float *out, int n){ dequantize(code, A, out, n); } + void cquantize_blockwise_fp16(float * code, half *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp16(code, A, absmax, out, n); } + void cquantize_blockwise_fp32(float * code, float *A, float *absmax, unsigned char *out, const int n){ quantizeBlockwise_fp32(code, A, absmax, out, n); } + void cquantize_blockwise_stochastic_fp16(float * code, half *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp16(code, A, absmax, out, rand, rand_offset, n); } + void cquantize_blockwise_stochastic_fp32(float * code, float *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n){ quantizeBlockwise_stochastic_fp32(code, A, absmax, out, rand, rand_offset, n); } + + void cdequantize_blockwise_fp16(float *code, unsigned char *A, float *absmax, half *out, int blocksize, const int n){ dequantizeBlockwise_fp16(code, A, absmax, out, blocksize, n); } + void cdequantize_blockwise_fp32(float *code, unsigned char *A, float *absmax, float *out, int blocksize, const int n){ dequantizeBlockwise_fp32(code, A, absmax, out, blocksize, n); } + + #define MAKE_CFUNC32(name, gtype, gbits) \ + void c##name##32bit_g##gbits(gtype *g, gtype *p, \ + float* state1, float* state2, float *unorm, float max_unorm, float param_norm, \ + const float beta1, const float beta2, const float eps, const float weight_decay, \ + const int step, const float lr, const float gnorm_scale, bool skip_zeros, const int n) \ + { name##32bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, weight_decay, step, lr, gnorm_scale, skip_zeros, n); } \ + + MAKE_CFUNC32(adam, float, 32) + MAKE_CFUNC32(adam, half, 16) + MAKE_CFUNC32(momentum, float, 32) + MAKE_CFUNC32(momentum, half, 16) + MAKE_CFUNC32(rmsprop, float, 32) + MAKE_CFUNC32(rmsprop, half, 16) + MAKE_CFUNC32(adagrad, float, 32) + MAKE_CFUNC32(adagrad, half, 16) + + #define MAKE_CFUNC8(name, gtype, gbits) \ + void c##name##_static_8bit_g##gbits(gtype* p, gtype* g, unsigned char* state1, unsigned char* state2, \ float *unorm, float max_unorm, float param_norm, \ float beta1, float beta2, \ float eps, int step, float lr, \ @@ -124,40 +124,39 @@ MAKE_CFUNC32(adagrad, half, 16) float* max1, float* max2, float* new_max1, float* new_max2, \ float weight_decay, float gnorm_scale, int n) \ { \ - name##_static_8bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ - quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ + name##_static_8bit_g##gbits(g, p, state1, state2, unorm, max_unorm, param_norm, beta1, beta2, eps, step, lr, \ + quantiles1, quantiles2, max1, max2, new_max1, new_max2, weight_decay, gnorm_scale, n); \ } \ -MAKE_CFUNC8(adam, float, 32) -MAKE_CFUNC8(adam, half, 16) -MAKE_CFUNC8(momentum, float, 32) -MAKE_CFUNC8(momentum, half, 16) -MAKE_CFUNC8(rmsprop, float, 32) -MAKE_CFUNC8(rmsprop, half, 16) + MAKE_CFUNC8(adam, float, 32) + MAKE_CFUNC8(adam, half, 16) + MAKE_CFUNC8(momentum, float, 32) + MAKE_CFUNC8(momentum, half, 16) + MAKE_CFUNC8(rmsprop, float, 32) + MAKE_CFUNC8(rmsprop, half, 16) -#define MAKE_CBLOCKWISE8(fname, optim_name, gtype, gbits) \ + #define MAKE_CBLOCKWISE8(fname, optim_name, gtype, gbits) \ void c##fname##_8bit_blockwise_fp##gbits(gtype* p, gtype* g, \ unsigned char* state1, unsigned char* state2, float beta1, float beta2, float eps, int step, float lr, \ float* quantiles1, float* quantiles2, float* absmax1, float* absmax2, float weight_decay, const float gnorm_scale, bool skip_zeros, int n) \ { fname##_8bit_blockwise_fp##gbits(p, g, state1, state2, beta1, beta2, eps, step, lr, quantiles1, quantiles2, absmax1, absmax2, weight_decay, gnorm_scale, skip_zeros, n); } \ -MAKE_CBLOCKWISE8(adam, ADAM, half, 16) -MAKE_CBLOCKWISE8(adam, ADAM, float, 32) -MAKE_CBLOCKWISE8(momentum, MOMENTUM, half, 16) -MAKE_CBLOCKWISE8(momentum, MOMENTUM, float, 32) -MAKE_CBLOCKWISE8(rmsprop, RMSPROP, half, 16) -MAKE_CBLOCKWISE8(rmsprop, RMSPROP, float, 32) -MAKE_CBLOCKWISE8(adagrad, ADAGRAD, half, 16) -MAKE_CBLOCKWISE8(adagrad, ADAGRAD, float, 32) + MAKE_CBLOCKWISE8(adam, ADAM, half, 16) + MAKE_CBLOCKWISE8(adam, ADAM, float, 32) + MAKE_CBLOCKWISE8(momentum, MOMENTUM, half, 16) + MAKE_CBLOCKWISE8(momentum, MOMENTUM, float, 32) + MAKE_CBLOCKWISE8(rmsprop, RMSPROP, half, 16) + MAKE_CBLOCKWISE8(rmsprop, RMSPROP, float, 32) + MAKE_CBLOCKWISE8(adagrad, ADAGRAD, half, 16) + MAKE_CBLOCKWISE8(adagrad, ADAGRAD, float, 32) -void cpercentile_clipping_g32(float * g, float *gnorm_vec, int step, const int n){ percentileClipping_g32(g, gnorm_vec, step, n); } -void cpercentile_clipping_g16(half * g, float *gnorm_vec, int step, const int n){ percentileClipping_g16(g, gnorm_vec, step, n); } -void chistogram_scatter_add_2d(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n){ histogramScatterAdd2D(histogram, index1, index2, src, maxidx1, n); } -#endif + void cpercentile_clipping_g32(float * g, float *gnorm_vec, int step, const int n){ percentileClipping_g32(g, gnorm_vec, step, n); } + void cpercentile_clipping_g16(half * g, float *gnorm_vec, int step, const int n){ percentileClipping_g16(g, gnorm_vec, step, n); } + void chistogram_scatter_add_2d(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n){ histogramScatterAdd2D(histogram, index1, index2, src, maxidx1, n); } -void cquantize_blockwise_cpu_fp32(float *code, float *A, float *absmax, unsigned char *out, const int n){ quantize_cpu(code, A, absmax, out, n); } -void cdequantize_blockwise_cpu_fp32(float *code, unsigned char *A, float *absmax, float *out, const int n){ dequantize_cpu(code, A, absmax, out, n); } + #endif + void cquantize_blockwise_cpu_fp32(float *code, float *A, float *absmax, unsigned char *out, const int n){ quantize_cpu(code, A, absmax, out, n); } + void cdequantize_blockwise_cpu_fp32(float *code, unsigned char *A, float *absmax, float *out, const int n){ dequantize_cpu(code, A, absmax, out, n); } } - -- cgit v1.2.3 From 575aa698fa53df2f5c584413aed7bf7714f86039 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:41:48 +0300 Subject: Reduce diff --- csrc/ops.cu | 45 ++++++++++++++++++++------------------------- csrc/pythonInterface.c | 2 +- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/csrc/ops.cu b/csrc/ops.cu index b2a1105..dbb50be 100644 --- a/csrc/ops.cu +++ b/csrc/ops.cu @@ -15,35 +15,30 @@ using namespace BinSearch; using std::cout; using std::endl; -void histogramScatterAdd2D(float *histogram, int *index1, int *index2, float *src, int maxidx1, int n) { - int threads = 512; - int blocks = n / threads; - blocks = n % threads == 0 ? blocks : blocks + 1; - kHistogramScatterAdd2D<<>>(histogram, index1, index2, src, maxidx1, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); -} - -template -void estimateQuantiles(T *A, float *code, float offset, int n) { - int blocks = n / 4096; - blocks = n % 4096 == 0 ? blocks : blocks + 1; - CUDA_CHECK_RETURN(cudaMemset(code, 0, 256 * sizeof(float))); - kEstimateQuantiles < T ><<>>(A, code, offset, std::numeric_limits::max(), n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +void histogramScatterAdd2D(float* histogram, int *index1, int *index2, float *src, int maxidx1, int n) +{ + int threads = 512; + int blocks = n/threads; + blocks = n % threads == 0 ? blocks : blocks + 1; + kHistogramScatterAdd2D<<>>(histogram, index1, index2, src, maxidx1, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void quantize(float *code, float *A, unsigned char *out, int n) { - int blocks = n / 1024; - blocks = n % 1024 == 0 ? blocks : blocks + 1; - kQuantize<<>>(code, A, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +template void estimateQuantiles(T *A, float *code, float offset, int n) +{ + int blocks = n/4096; + blocks = n % 4096 == 0 ? blocks : blocks + 1; + CUDA_CHECK_RETURN(cudaMemset(code, 0, 256*sizeof(float))); + kEstimateQuantiles<<>>(A, code, offset, std::numeric_limits::max(), n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } -void dequantize(float *code, unsigned char *A, float *out, int n) { - int blocks = n / 1024; - blocks = n % 1024 == 0 ? blocks : blocks + 1; - kDequantize<<>>(code, A, out, n); - CUDA_CHECK_RETURN(cudaPeekAtLastError()); +void quantize(float *code, float *A, unsigned char *out, int n) +{ + int blocks = n/1024; + blocks = n % 1024 == 0 ? blocks : blocks + 1; + kQuantize<<>>(code, A, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); } template void quantizeBlockwise(float * code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) diff --git a/csrc/pythonInterface.c b/csrc/pythonInterface.c index 1f690c5..c2fed6b 100644 --- a/csrc/pythonInterface.c +++ b/csrc/pythonInterface.c @@ -86,7 +86,7 @@ void dequantizeBlockwise_fp32(float *code, unsigned char *A, float *absmax, floa extern "C" { - if #BUILD_CUDA + #if BUILD_CUDA void cestimate_quantiles_fp32(float *A, float *code, float offset, int n){ estimateQuantiles_fp32(A, code, offset, n); } void cestimate_quantiles_fp16(half *A, float *code, float offset, int n){ estimateQuantiles_fp16(A, code, offset, n); } void cquantize(float *code, float *A, unsigned char *out, int n){ quantize(code, A, out, n); } -- cgit v1.2.3 From 025824d29b38f6b981bbcea8a61bc23e7f2b3e02 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:42:58 +0300 Subject: Reduce diff --- csrc/ops.cu | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/csrc/ops.cu b/csrc/ops.cu index dbb50be..40c185c 100644 --- a/csrc/ops.cu +++ b/csrc/ops.cu @@ -41,6 +41,14 @@ void quantize(float *code, float *A, unsigned char *out, int n) CUDA_CHECK_RETURN(cudaPeekAtLastError()); } +void dequantize(float *code, unsigned char *A, float *out, int n) +{ + int blocks = n/1024; + blocks = n % 1024 == 0 ? blocks : blocks + 1; + kDequantize<<>>(code, A, out, n); + CUDA_CHECK_RETURN(cudaPeekAtLastError()); +} + template void quantizeBlockwise(float * code, T *A, float *absmax, unsigned char *out, float *rand, int rand_offset, const int n) { int blocks = n/4096; -- cgit v1.2.3 From fd750cd2370b3b12e216a9148b23aaae63a80989 Mon Sep 17 00:00:00 2001 From: Max Ryabinin Date: Fri, 1 Jul 2022 17:46:29 +0300 Subject: Update README.md --- compile_from_source.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compile_from_source.md b/compile_from_source.md index c6a8b18..71b0c09 100644 --- a/compile_from_source.md +++ b/compile_from_source.md @@ -1,12 +1,12 @@ # Compiling from source Basic steps. -1. `make cudaXXX` where `cudaXXX` is among `cuda92, cuda10x, cuda110, cuda11x` +1. `make [target]` where `[target]` is among `cuda92, cuda10x, cuda110, cuda11x, cpuonly` 2. `CUDA_VERSION=XXX python setup.py install` To run these steps you will need to have the nvcc compiler installed that comes with a CUDA installation. If you use anaconda (recommended) then you can figure out which version of CUDA you are using with PyTorch via the command `conda list | grep cudatoolkit`. Then you can install the nvcc compiler by downloading and installing the same CUDA version from the [CUDA toolkit archive](https://developer.nvidia.com/cuda-toolkit-archive). -For your convenience, there is a install script int he root directory that installs CUDA 11.1 locally and configures it automatically. After installing you should add the `bin` sub-directory to the `$PATH` variable to make the compiler visible to your system. To do this you can add this to your `.bashrc` by executing these commands: +For your convenience, there is an installation script in the root directory that installs CUDA 11.1 locally and configures it automatically. After installing you should add the `bin` sub-directory to the `$PATH` variable to make the compiler visible to your system. To do this you can add this to your `.bashrc` by executing these commands: ```bash echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/" >> ~/.bashrc echo "export PATH=$PATH:/usr/local/cuda/bin/" >> ~/.bashrc -- cgit v1.2.3