|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | +#include <algorithm> |
| 8 | +#include <cmath> |
| 9 | + |
| 10 | +namespace onnxruntime { |
| 11 | +namespace contrib { |
| 12 | + |
| 13 | +#if defined(_MSC_VER) |
| 14 | +#define FORCEINLINE __forceinline |
| 15 | +#else |
| 16 | +#define FORCEINLINE __attribute__((always_inline)) inline |
| 17 | +#endif |
| 18 | + |
| 19 | +typedef enum Bnb_DataType_t { |
| 20 | + FP4 = 0, |
| 21 | + NF4 = 1, |
| 22 | +} Bnb_DataType_t; |
| 23 | + |
| 24 | +FORCEINLINE uint8_t QuantizeOneFP4(float x) { |
| 25 | + // FP4 with bias of 3 |
| 26 | + // first bit is a sign |
| 27 | + // subnormals |
| 28 | + // 0b000 = 0 |
| 29 | + // 0b001 = 0.0625 |
| 30 | + // 0b110 = 2 |
| 31 | + // 0b111 = 3 |
| 32 | + // 0b100 = 4 |
| 33 | + // 0b101 = 6 |
| 34 | + // 0b010 = 8 |
| 35 | + // 0b011 = 12 |
| 36 | + |
| 37 | + // we do a binary search |
| 38 | + // the pivots are divided by 12 (the FP4 absmax) |
| 39 | + // since we assum input data is in [-1.0, 1.0] |
| 40 | + |
| 41 | + // !be careful here, its easy to make a mistake |
| 42 | + // that is difficult to noice if you add an extra |
| 43 | + // zero somewhere! |
| 44 | + |
| 45 | + uint8_t sign = x < 0 ? 0b1000 : 0b0000; |
| 46 | + x = fabsf(x); |
| 47 | + if (x > 0.29166667f) { |
| 48 | + if (x > 0.583333f) { |
| 49 | + if (x > 0.8333333f) { |
| 50 | + return 0b0011 + sign; |
| 51 | + } else { |
| 52 | + return 0b0010 + sign; |
| 53 | + } |
| 54 | + } else if (x > 0.4166667f) { |
| 55 | + return 0b101 + sign; |
| 56 | + } else { |
| 57 | + return 0b100 + sign; |
| 58 | + } |
| 59 | + } else if (x > 0.0859375f) { |
| 60 | + if (x > 0.20833333f) { |
| 61 | + return 0b0111 + sign; |
| 62 | + } else { |
| 63 | + return 0b0110 + sign; |
| 64 | + } |
| 65 | + } else if (x > 0.00260417f) { |
| 66 | + return 0b0001 + sign; |
| 67 | + } else { |
| 68 | + return 0b0000 + sign; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +FORCEINLINE uint8_t QuantizeOneNF4(float x) { |
| 73 | + if (x > 0.03979014977812767f) { |
| 74 | + if (x > 0.3893125355243683f) { // 1 |
| 75 | + if (x > 0.6427869200706482f) { // 11 |
| 76 | + if (x > 0.8614784181118011f) { // 111 |
| 77 | + return 0b1111; |
| 78 | + } else { |
| 79 | + return 0b1110; |
| 80 | + } |
| 81 | + } else if (x > 0.5016634166240692f) { // 110 |
| 82 | + return 0b1101; |
| 83 | + } else { |
| 84 | + return 0b1100; |
| 85 | + } |
| 86 | + } else if (x > 0.2035212516784668f) { // 10 |
| 87 | + if (x > 0.2920137718319893f) { // 101 |
| 88 | + return 0b1011; |
| 89 | + } else { |
| 90 | + return 0b1010; |
| 91 | + } |
| 92 | + } else if (x > 0.1202552504837513f) { // 100 |
| 93 | + return 0b1001; |
| 94 | + } else { |
| 95 | + return 0b1000; |
| 96 | + } |
| 97 | + } else if (x > -0.33967943489551544f) { // 0 |
| 98 | + if (x > -0.13791173323988914f) { // 01 |
| 99 | + if (x > -0.045525018125772476f) { // 011 |
| 100 | + return 0b0111; |
| 101 | + } else { |
| 102 | + return 0b0110; |
| 103 | + } |
| 104 | + } else if (x > -0.23460740596055984f) { // 010 |
| 105 | + return 0b0101; |
| 106 | + } else { |
| 107 | + return 0b0100; |
| 108 | + } |
| 109 | + } else if (x > -0.6106329262256622f) { // 00 |
| 110 | + if (x > -0.4599952697753906f) { // 001 |
| 111 | + return 0b0011; |
| 112 | + } else { |
| 113 | + return 0b0010; |
| 114 | + } |
| 115 | + } else if (x > -0.8480964004993439f) { // 000 |
| 116 | + return 0b0001; |
| 117 | + } else { |
| 118 | + return 0b0000; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +template <int32_t DATA_TYPE> |
| 123 | +FORCEINLINE uint8_t QuantizeOneBnb4(float x) { |
| 124 | + if constexpr (DATA_TYPE == FP4) |
| 125 | + return QuantizeOneFP4(x); |
| 126 | + else |
| 127 | + return QuantizeOneNF4(x); |
| 128 | +} |
| 129 | + |
| 130 | +template <typename T, int32_t block_size, int32_t DATA_TYPE> |
| 131 | +FORCEINLINE void QuantizeBlockBnb4(const T* src, uint8_t* dst, T& absmax_block, int32_t block_idx, int32_t numel) { |
| 132 | + float local_absmax = 0.0f; |
| 133 | + |
| 134 | + int32_t block_len = std::min(block_size, numel - block_idx * block_size); |
| 135 | + int32_t src_offset = block_idx * block_size; |
| 136 | + int32_t dst_offset = block_idx * block_size / 2; |
| 137 | + |
| 138 | + for (int32_t idx = 0; idx < block_len; idx++) { |
| 139 | + const float v = static_cast<float>(src[src_offset + idx]); |
| 140 | + local_absmax = fmaxf(local_absmax, fabsf(v)); |
| 141 | + } |
| 142 | + |
| 143 | + absmax_block = static_cast<T>(local_absmax); |
| 144 | + const float reciprocal_absmax = local_absmax ? 1.0f / local_absmax : 0.0f; |
| 145 | + |
| 146 | + for (int32_t idx = 0; idx < block_len; idx += 2) { |
| 147 | + const float v0 = static_cast<float>(src[src_offset + idx]) * reciprocal_absmax; |
| 148 | + const uint8_t vi0 = QuantizeOneBnb4<DATA_TYPE>(v0); |
| 149 | + |
| 150 | + const float v1 = (idx + 1 < block_len) ? static_cast<float>(src[src_offset + idx + 1]) * reciprocal_absmax : 0; |
| 151 | + const uint8_t vi1 = QuantizeOneBnb4<DATA_TYPE>(v1); |
| 152 | + |
| 153 | + dst[dst_offset + idx / 2] = (vi0 << 4) | vi1; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +static float fp4_qaunt_map[16] = {0.00000000f, 5.208333333e-03f, 0.66666667f, 1.00000000f, |
| 158 | + 0.33333333f, 0.50000000f, 0.16666667f, 0.25000000f, |
| 159 | + -0.00000000f, -5.208333333e-03f, -0.66666667f, -1.00000000f, |
| 160 | + -0.33333333f, -0.50000000f, -0.16666667f, -0.25000000f}; |
| 161 | + |
| 162 | +static float nf4_qaunt_map[16] = {-1.0f, |
| 163 | + -0.6961928009986877f, |
| 164 | + -0.5250730514526367f, |
| 165 | + -0.39491748809814453f, |
| 166 | + -0.28444138169288635f, |
| 167 | + -0.18477343022823334f, |
| 168 | + -0.09105003625154495f, |
| 169 | + 0.0f, |
| 170 | + 0.07958029955625534f, |
| 171 | + 0.16093020141124725f, |
| 172 | + 0.24611230194568634f, |
| 173 | + 0.33791524171829224f, |
| 174 | + 0.44070982933044434f, |
| 175 | + 0.5626170039176941f, |
| 176 | + 0.7229568362236023f, |
| 177 | + 1.0f}; |
| 178 | + |
| 179 | +template <typename T, int32_t DATA_TYPE> |
| 180 | +FORCEINLINE T DequantizeOneBnb4(uint8_t x) { |
| 181 | + if constexpr (DATA_TYPE == FP4) |
| 182 | + return static_cast<T>(fp4_qaunt_map[x]); |
| 183 | + else |
| 184 | + return static_cast<T>(nf4_qaunt_map[x]); |
| 185 | +} |
| 186 | + |
| 187 | +template <typename T, int32_t block_size, int32_t DATA_TYPE> |
| 188 | +FORCEINLINE void DequantizeBlockBnb4(const uint8_t* src, T* dst, T absmax_block, int32_t block_idx, int32_t numel) { |
| 189 | + int32_t block_len = std::min(block_size, numel - block_idx * block_size); |
| 190 | + int32_t src_offset = block_idx * block_size / 2; |
| 191 | + int32_t dst_offset = block_idx * block_size; |
| 192 | + |
| 193 | + for (int32_t idx = 0; idx < block_len; idx += 2) { |
| 194 | + const uint8_t val = src[src_offset + idx / 2]; |
| 195 | + |
| 196 | + dst[dst_offset + idx] = DequantizeOneBnb4<T, DATA_TYPE>(val >> 4) * absmax_block; |
| 197 | + if (idx + 1 < block_len) dst[dst_offset + idx + 1] = DequantizeOneBnb4<T, DATA_TYPE>(val & 0xF) * absmax_block; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace contrib |
| 202 | +} // namespace onnxruntime |
0 commit comments