Skip to content

Commit 7948e2a

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

File tree

5 files changed

+44
-49
lines changed

5 files changed

+44
-49
lines changed

Makefile

+7-10
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ let package = Package(
8989
linkerSettings: linkerSettings
9090
)
9191
],
92-
cxxLanguageStandard: .cxx11
92+
cxxLanguageStandard: .cxx17
9393
)

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ inline uint8_t from_float(float value) {
2020
} in = {value};
2121
out.bits = (in.bits >> 24) & 0x80;
2222
in.bits &= 0x7fffffff;
23-
if (in.f >= FP8<E>::MAX()) {
23+
if (in.f >= FP8<E>::MAX) {
2424
out.bits |= 0x7E;
25-
} else if (in.f < FP8<E>::MIN()) { // => 0.
25+
} else if (in.f < FP8<E>::MIN) { // => 0.
2626
} 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);
27+
in.f *= exp_f2<FP8<E>::E_BIAS-127>();
28+
uint32_t eps = (0x3fffff>>FP8<E>::M) + ((in.bits >> (23-FP8<E>::M)) & 0x1);
2929
in.bits += eps;
30-
out.bits |= (in.bits >> (23-FP8<E>::M())) & 0x7F;
30+
out.bits |= (in.bits >> (23-FP8<E>::M)) & 0x7F;
3131
}
3232
return out.bits;
3333
}
@@ -44,9 +44,9 @@ inline float to_float(const FP8<E>& in) {
4444
out.bits = in.bits & 0x80;
4545
out.bits <<= 24;
4646
uint32_t _bits = in.bits & 0x7F;
47-
_bits <<= (23-FP8<E>::M());
47+
_bits <<= (23-FP8<E>::M);
4848
out.bits |= _bits;
49-
out.f *= exp_p2<127-FP8<E>::E_BIAS()>();
49+
out.f *= exp_f2<127-FP8<E>::E_BIAS>();
5050
return out.f;
5151
}
5252
} // namespace fp8
@@ -91,8 +91,8 @@ static inline void conv(const float* x, bloc_fp8<E, QK>* y, int64_t size) {
9191
for (int64_t i=0; i<QK; i++) {
9292
m = std::max(std::abs(x[q*QK+i]),m);
9393
}
94-
const float D = FP8<E>::MAX()/m;
95-
y[q].d = m/FP8<E>::MAX();
94+
const float D = FP8<E>::MAX/m;
95+
y[q].d = m/FP8<E>::MAX;
9696
#ifdef GGML_USE_OPENMP_SIMD
9797
#pragma omp simd
9898
#endif
@@ -154,22 +154,22 @@ float dot_reg(const bloc_fp8<E, QK>* x, const _Y* y, int64_t size) {
154154
for(int64_t v=0; v<VECT_SIZE; ++v) { mantice_16bits[v] = mantice_8bits[v]; }
155155

156156
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()); }
157+
for(int64_t v=0; v<VECT_SIZE; ++v) { mantice_16bits[v] <<= (7-fp8_t::M); }
158158

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

161161
for(int64_t v=0; v<VECT_SIZE; ++v) { ux[v].bits = x_bf16[v]; }
162162
for(int64_t v=0; v<VECT_SIZE; ++v) { ux[v].bits <<= 16; }
163163

164-
for(int64_t v=0; v<VECT_SIZE; ++v) { X[v] = ux[v].f; } // * exp_p2<127-fp8_t::E_BIAS()>(); }
164+
for(int64_t v=0; v<VECT_SIZE; ++v) { X[v] = ux[v].f; } // * exp_f2<127-fp8_t::E_BIAS>(); }
165165
for(int64_t v=0; v<VECT_SIZE; ++v) { Y[v] = (float)y[q*QK+i+r*VECT_SIZE+v]; }
166166
for(int64_t v=0; v<VECT_SIZE; ++v) { Z0[r][v] += X[v]*Y[v]; }
167167
}
168168
}
169169
// apply scale
170170
for(int64_t r=0; r<NB_REG; ++r) {
171171
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()>();
172+
Z[r][v] += Z0[r][v]*(x[q]).d * exp_f2<127-fp8_t::E_BIAS>();
173173
}
174174
}
175175
}

ggml/src/ggml-fp8.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ inline FP8<E> float_to_fp8(float value) {
1919
// value without sign
2020
in.bits &= 0x7fffffff;
2121
//GGML_ASSERT(in.bits < 0x7f800000); // +/- infinity or NAN
22-
if (in.f >= FP8<E>::MAX()) {
22+
if (in.f >= FP8<E>::MAX) {
2323
out.bits |= 0x7E;
24-
} else if (in.f < FP8<E>::MIN()) { // => 0.
24+
} else if (in.f < FP8<E>::MIN) { // => 0.
2525
// OK: S.0000000
2626
} else {
27-
in.f *= exp_m2<FP8<E>::E_BIAS()-127>();
27+
in.f *= exp_f2<FP8<E>::E_BIAS-127>();
2828
// - trunc
2929
//uint32_t eps = 0;
3030
// - rounding half away from zero
31-
//uint32_t eps = 0x400000>>FP8<E>::M();
31+
//uint32_t eps = 0x400000>>FP8<E>::M;
3232
// - rounding half toward zero
33-
//uint32_t eps = 0x3fffff>>FP8<E>::M();
33+
//uint32_t eps = 0x3fffff>>FP8<E>::M;
3434
// - rounding to nearest even
35-
uint32_t eps = (0x3fffff>>FP8<E>::M()) + ((in.bits >> (23-FP8<E>::M())) & 0x1);
35+
uint32_t eps = (0x3fffff>>FP8<E>::M) + ((in.bits >> (23-FP8<E>::M)) & 0x1);
3636
// shift mantissa.
3737
in.bits += eps;
38-
out.bits |= (in.bits >> (23-FP8<E>::M())) & 0x7F;
38+
out.bits |= (in.bits >> (23-FP8<E>::M)) & 0x7F;
3939
}
4040
return out;
4141
}
@@ -49,9 +49,9 @@ inline float fp8_to_float(const FP8<E>& in) {
4949
out.bits = in.bits & 0x80;
5050
out.bits <<= 24;
5151
uint32_t _bits = in.bits & 0x7F;
52-
_bits <<= (23-FP8<E>::M());
52+
_bits <<= (23-FP8<E>::M);
5353
out.bits |= _bits;
54-
out.f *= exp_p2<127-FP8<E>::E_BIAS()>();
54+
out.f *= exp_f2<127-FP8<E>::E_BIAS>();
5555
return out.f;
5656
}
5757

@@ -93,8 +93,8 @@ static inline void conv(const float* x, bloc_fp8<E, QK>* y, int64_t size) {
9393
for (int64_t i=0; i<QK; i++) {
9494
m = std::max(std::abs(x[q*QK+i]),m);
9595
}
96-
const float D = FP8<E>::MAX()/m;
97-
y[q].d = m/FP8<E>::MAX();
96+
const float D = FP8<E>::MAX/m;
97+
y[q].d = m/FP8<E>::MAX;
9898
for (int64_t i=0; i<QK; i++) {
9999
y[q].qs[i] = float_to_fp8<E>(x[q*QK+i]*D);
100100
}

ggml/src/ggml-fp8.h

+13-15
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)