Skip to content

Commit aa6f413

Browse files
committed
move to c++17
1 parent 038b5fa commit aa6f413

File tree

5 files changed

+59
-65
lines changed

5 files changed

+59
-65
lines changed

Makefile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -958,14 +958,14 @@ OBJ_GGML = \
958958
$(DIR_GGML)/src/ggml-alloc.o \
959959
$(DIR_GGML)/src/ggml-backend.o \
960960
$(DIR_GGML)/src/ggml-backend-reg.o \
961-
$(DIR_GGML)/src/ggml-fp8_cpp11.o \
961+
$(DIR_GGML)/src/ggml-fp8.o \
962962
$(DIR_GGML)/src/ggml-opt.o \
963963
$(DIR_GGML)/src/ggml-quants.o \
964964
$(DIR_GGML)/src/ggml-threading.o \
965965
$(DIR_GGML)/src/ggml-cpu/ggml-cpu.o \
966-
$(DIR_GGML)/src/ggml-cpu/ggml-cpu_cpp11.o \
966+
$(DIR_GGML)/src/ggml-cpu/ggml-cpu_cpp.o \
967967
$(DIR_GGML)/src/ggml-cpu/ggml-cpu-aarch64.o \
968-
$(DIR_GGML)/src/ggml-cpu/ggml-cpu-fp8_cpp11.o \
968+
$(DIR_GGML)/src/ggml-cpu/ggml-cpu-fp8.o \
969969
$(DIR_GGML)/src/ggml-cpu/ggml-cpu-quants.o \
970970
$(OBJ_GGML_EXT)
971971

@@ -1106,13 +1106,10 @@ DEP_FILES = $(OBJ_GGML:.o=.d) $(OBJ_LLAMA:.o=.d) $(OBJ_COMMON:.o=.d)
11061106
# Default target
11071107
all: $(BUILD_TARGETS)
11081108

1109-
# for c++17 build
1110-
$(DIR_GGML)/%_cpp17.o: $(DIR_GGML)/%.cpp
1111-
$(CXX) $(CXXFLAGS) -MMD -std=c++17 -c $< -o $@
1112-
1113-
# for c++11 build
1114-
$(DIR_GGML)/%_cpp11.o: $(DIR_GGML)/%.cpp
1115-
$(CXX) $(CXXFLAGS) -MMD -std=c++11 -c $< -o $@
1109+
# force c++ build for source file that have same name as c file
1110+
# Note: need this exception because `ggml-cpu.c` and `ggml-cpu.cpp` both produce the same obj/dep files
1111+
$(DIR_GGML)/%_cpp.o: $(DIR_GGML)/%.cpp
1112+
$(CXX) $(CXXFLAGS) -MMD -c $< -o $@
11161113

11171114
# Rules for building object files
11181115
$(DIR_GGML)/%.o: $(DIR_GGML)/%.c

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var sources = [
2121
"ggml/src/ggml-threading.cpp",
2222
"ggml/src/ggml-quants.c",
2323
"ggml/src/ggml-fp8.cpp",
24+
"ggml/src/ggml-cpu/ggml-cpu-fp8.cpp",
2425
]
2526

2627
var resources: [Resource] = []
@@ -89,5 +90,5 @@ let package = Package(
8990
linkerSettings: linkerSettings
9091
)
9192
],
92-
cxxLanguageStandard: .cxx11
93+
cxxLanguageStandard: .cxx17
9394
)

ggml/src/ggml-cpu/ggml-cpu-fp8.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
#include "ggml-cpu-fp8.h"
99

1010
namespace fp8 {
11+
union fp32_int32 {
12+
float f;
13+
uint32_t bits;
14+
};
15+
1116
#ifdef GGML_USE_OPENMP_SIMD
1217
#pragma omp declare simd
1318
#endif
1419
template<int E>
1520
inline uint8_t from_float(float value) {
1621
FP8<E> out;
17-
union {
18-
float f;
19-
uint32_t bits;
20-
} in = {value};
22+
fp32_int32 in = {value};
2123
out.bits = (in.bits >> 24) & 0x80;
2224
in.bits &= 0x7fffffff;
23-
if (in.f >= FP8<E>::MAX()) {
25+
if (in.f >= FP8<E>::MAX) {
2426
out.bits |= 0x7E;
25-
} else if (in.f < FP8<E>::MIN()) { // => 0.
27+
} else if (in.f < FP8<E>::MIN) { // => 0.
2628
} else {
27-
in.f *= exp_m2<FP8<E>::E_BIAS()-127>();
28-
uint32_t eps = (0x3fffff>>FP8<E>::M()) + ((in.bits >> (23-FP8<E>::M())) & 0x1);
29+
in.f *= exp_f2<FP8<E>::E_BIAS-127>();
30+
uint32_t eps = (0x3fffff>>FP8<E>::M) + ((in.bits >> (23-FP8<E>::M)) & 0x1);
2931
in.bits += eps;
30-
out.bits |= (in.bits >> (23-FP8<E>::M())) & 0x7F;
32+
out.bits |= (in.bits >> (23-FP8<E>::M)) & 0x7F;
3133
}
3234
return out.bits;
3335
}
@@ -37,16 +39,13 @@ inline uint8_t from_float(float value) {
3739
#endif
3840
template<int E>
3941
inline float to_float(const FP8<E>& in) {
40-
union {
41-
float f;
42-
uint32_t bits;
43-
} out = {0};
42+
fp32_int32 out = {0};
4443
out.bits = in.bits & 0x80;
4544
out.bits <<= 24;
4645
uint32_t _bits = in.bits & 0x7F;
47-
_bits <<= (23-FP8<E>::M());
46+
_bits <<= (23-FP8<E>::M);
4847
out.bits |= _bits;
49-
out.f *= exp_p2<127-FP8<E>::E_BIAS()>();
48+
out.f *= exp_f2<127-FP8<E>::E_BIAS>();
5049
return out.f;
5150
}
5251
} // namespace fp8
@@ -91,8 +90,8 @@ static inline void conv(const float* x, bloc_fp8<E, QK>* y, int64_t size) {
9190
for (int64_t i=0; i<QK; i++) {
9291
m = std::max(std::abs(x[q*QK+i]),m);
9392
}
94-
const float D = FP8<E>::MAX()/m;
95-
y[q].d = m/FP8<E>::MAX();
93+
const float D = FP8<E>::MAX/m;
94+
y[q].d = m/FP8<E>::MAX;
9695
#ifdef GGML_USE_OPENMP_SIMD
9796
#pragma omp simd
9897
#endif
@@ -154,22 +153,22 @@ float dot_reg(const bloc_fp8<E, QK>* x, const _Y* y, int64_t size) {
154153
for(int64_t v=0; v<VECT_SIZE; ++v) { mantice_16bits[v] = mantice_8bits[v]; }
155154

156155
for(int64_t v=0; v<VECT_SIZE; ++v) { sign_16bits[v] <<= 8; }
157-
for(int64_t v=0; v<VECT_SIZE; ++v) { mantice_16bits[v] <<= (7-fp8_t::M()); }
156+
for(int64_t v=0; v<VECT_SIZE; ++v) { mantice_16bits[v] <<= (7-fp8_t::M); }
158157

159158
for(int64_t v=0; v<VECT_SIZE; ++v) { x_bf16[v] = sign_16bits[v] | mantice_16bits[v]; }
160159

161160
for(int64_t v=0; v<VECT_SIZE; ++v) { ux[v].bits = x_bf16[v]; }
162161
for(int64_t v=0; v<VECT_SIZE; ++v) { ux[v].bits <<= 16; }
163162

164-
for(int64_t v=0; v<VECT_SIZE; ++v) { X[v] = ux[v].f; } // * exp_p2<127-fp8_t::E_BIAS()>(); }
163+
for(int64_t v=0; v<VECT_SIZE; ++v) { X[v] = ux[v].f; } // * exp_f2<127-fp8_t::E_BIAS>(); }
165164
for(int64_t v=0; v<VECT_SIZE; ++v) { Y[v] = (float)y[q*QK+i+r*VECT_SIZE+v]; }
166165
for(int64_t v=0; v<VECT_SIZE; ++v) { Z0[r][v] += X[v]*Y[v]; }
167166
}
168167
}
169168
// apply scale
170169
for(int64_t r=0; r<NB_REG; ++r) {
171170
for(int64_t v=0; v<VECT_SIZE; ++v) {
172-
Z[r][v] += Z0[r][v]*(x[q]).d * exp_p2<127-fp8_t::E_BIAS()>();
171+
Z[r][v] += Z0[r][v]*(x[q]).d * exp_f2<127-fp8_t::E_BIAS>();
173172
}
174173
}
175174
}

ggml/src/ggml-fp8.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,50 @@
77

88
#include "ggml-fp8.h"
99

10+
union fp32_int32 {
11+
float f;
12+
uint32_t bits;
13+
};
14+
1015
template<int E>
1116
inline FP8<E> float_to_fp8(float value) {
1217
FP8<E> out;
13-
union {
14-
float f;
15-
uint32_t bits;
16-
} in = {value};
18+
fp32_int32 in = {value};
1719
// the sign
1820
out.bits = (in.bits >> 24) & 0x80;
1921
// value without sign
2022
in.bits &= 0x7fffffff;
2123
//GGML_ASSERT(in.bits < 0x7f800000); // +/- infinity or NAN
22-
if (in.f >= FP8<E>::MAX()) {
24+
if (in.f >= FP8<E>::MAX) {
2325
out.bits |= 0x7E;
24-
} else if (in.f < FP8<E>::MIN()) { // => 0.
26+
} else if (in.f < FP8<E>::MIN) { // => 0.
2527
// OK: S.0000000
2628
} else {
27-
in.f *= exp_m2<FP8<E>::E_BIAS()-127>();
29+
in.f *= exp_f2<FP8<E>::E_BIAS-127>();
2830
// - trunc
2931
//uint32_t eps = 0;
3032
// - rounding half away from zero
31-
//uint32_t eps = 0x400000>>FP8<E>::M();
33+
//uint32_t eps = 0x400000>>FP8<E>::M;
3234
// - rounding half toward zero
33-
//uint32_t eps = 0x3fffff>>FP8<E>::M();
35+
//uint32_t eps = 0x3fffff>>FP8<E>::M;
3436
// - rounding to nearest even
35-
uint32_t eps = (0x3fffff>>FP8<E>::M()) + ((in.bits >> (23-FP8<E>::M())) & 0x1);
37+
uint32_t eps = (0x3fffff>>FP8<E>::M) + ((in.bits >> (23-FP8<E>::M)) & 0x1);
3638
// shift mantissa.
3739
in.bits += eps;
38-
out.bits |= (in.bits >> (23-FP8<E>::M())) & 0x7F;
40+
out.bits |= (in.bits >> (23-FP8<E>::M)) & 0x7F;
3941
}
4042
return out;
4143
}
4244

4345
template<int E>
4446
inline float fp8_to_float(const FP8<E>& in) {
45-
union {
46-
float f;
47-
uint32_t bits;
48-
} out = {0};
47+
fp32_int32 out = {0};
4948
out.bits = in.bits & 0x80;
5049
out.bits <<= 24;
5150
uint32_t _bits = in.bits & 0x7F;
52-
_bits <<= (23-FP8<E>::M());
51+
_bits <<= (23-FP8<E>::M);
5352
out.bits |= _bits;
54-
out.f *= exp_p2<127-FP8<E>::E_BIAS()>();
53+
out.f *= exp_f2<127-FP8<E>::E_BIAS>();
5554
return out.f;
5655
}
5756

@@ -93,8 +92,8 @@ static inline void conv(const float* x, bloc_fp8<E, QK>* y, int64_t size) {
9392
for (int64_t i=0; i<QK; i++) {
9493
m = std::max(std::abs(x[q*QK+i]),m);
9594
}
96-
const float D = FP8<E>::MAX()/m;
97-
y[q].d = m/FP8<E>::MAX();
95+
const float D = FP8<E>::MAX/m;
96+
y[q].d = m/FP8<E>::MAX;
9897
for (int64_t i=0; i<QK; i++) {
9998
y[q].qs[i] = float_to_fp8<E>(x[q*QK+i]*D);
10099
}

ggml/src/ggml-fp8.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
// this is more a .inc.
22
#ifdef __cplusplus
33
template<int N>
4-
constexpr float exp_p2() {
5-
return exp_p2<N-1>()*2;
6-
}
7-
template<int N>
8-
constexpr float exp_m2() {
9-
return exp_m2<N+1>()/2;
10-
}
11-
template<int N>
124
constexpr int exp_i2() {
135
return 1 << N;
146
}
15-
template<> constexpr float exp_p2<0>() { return 1;}
16-
template<> constexpr float exp_m2<0>() { return 1;}
7+
8+
template<int N>
9+
constexpr float exp_f2() {
10+
if constexpr (N>0) return exp_f2<N-1>()*2;
11+
if constexpr (N<0) return exp_f2<N+1>()/2;
12+
if constexpr (N==0) return 1.;
13+
}
14+
1715

1816
template<int _E> //, int M=7-E> 1.7 bits!
1917
struct FP8 {
2018
uint8_t bits;
2119
using type = FP8<_E>;
22-
static constexpr int E() { return _E; }
23-
static constexpr int M() { return 7-_E; }
24-
static constexpr int E_BIAS() { return exp_i2<_E-1>()-1; }
25-
static constexpr float MAX() { return (2-exp_m2<-M()+1>())*exp_p2<exp_i2<_E-1>()>(); }
26-
static constexpr float MIN() { return exp_m2<-M()>()*exp_m2<2-exp_i2<_E-1>()>(); }
20+
static constexpr int E = _E;
21+
static constexpr int M = (7-_E);
22+
static constexpr int E_BIAS = exp_i2<E-1>()-1;
23+
static constexpr float MAX = (2-exp_f2<-M+1>())*exp_f2<exp_i2<E-1>()>();
24+
static constexpr float MIN = exp_f2<-M>()*exp_f2<2-exp_i2<E-1>()>();
2725
};
2826

2927
extern "C" {

0 commit comments

Comments
 (0)