Skip to content

Commit d1f48d0

Browse files
author
Olivier Chafik
committed
Merge remote-tracking branch 'origin/master' into tool-bench-prod
2 parents 7bcc5af + d7cfe1f commit d1f48d0

File tree

11 files changed

+671
-386
lines changed

11 files changed

+671
-386
lines changed

docs/function-calling.md

Lines changed: 390 additions & 0 deletions
Large diffs are not rendered by default.

examples/server/README.md

Lines changed: 3 additions & 374 deletions
Large diffs are not rendered by default.

examples/server/utils.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,13 @@ static json oaicompat_completion_params_parse(const json & body) {
521521
throw std::runtime_error("Only one completion choice is allowed");
522522
}
523523

524+
// Handle "echo" field
525+
if (json_value(body, "echo", false)) {
526+
throw std::runtime_error("Only no echo is supported");
527+
}
528+
524529
// Params supported by OAI but unsupported by llama.cpp
525-
static const std::vector<std::string> unsupported_params { "best_of", "echo", "suffix" };
530+
static const std::vector<std::string> unsupported_params { "best_of", "suffix" };
526531
for (const auto & param : unsupported_params) {
527532
if (body.contains(param)) {
528533
throw std::runtime_error("Unsupported param: " + param);

ggml/src/ggml-cpu/ggml-cpu-quants.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5265,6 +5265,7 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * r
52655265

52665266
#if defined(__ARM_FEATURE_SVE)
52675267

5268+
uint32_t aux[3];
52685269
uint32_t utmp[4];
52695270

52705271
const int8_t m32 = 32;
@@ -5276,7 +5277,6 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * r
52765277
const svuint8_t m1_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 1);
52775278
const svuint8_t m2_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 2);
52785279
const svuint8_t m3_sv = svlsl_n_u8_x(svptrue_b8(), m0_sv, 3);
5279-
svbool_t pred_s32 = svnot_b_z (svptrue_b32(), svptrue_pat_b32(SV_VL4));
52805280

52815281
float sum = 0;
52825282

@@ -5289,7 +5289,7 @@ void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * r
52895289
const int8_t * restrict q8_sv = y[i].qs;
52905290

52915291
// Set up scales
5292-
uint32_t * aux = &x[i].scales;
5292+
memcpy(aux, x[i].scales, 12);
52935293
utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
52945294
utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
52955295
utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 110 additions & 9 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#version 450
2+
3+
#include "generic_head.comp"
4+
#include "types.comp"
5+
6+
#extension GL_EXT_control_flow_attributes : enable
7+
#define BLOCK_SIZE 512
8+
9+
layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
10+
11+
layout (binding = 0) readonly buffer G {A_TYPE data_a[];};
12+
layout (binding = 1) readonly buffer X {B_TYPE data_b[];};
13+
layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
14+
15+
shared FLOAT_TYPE sum_xx[BLOCK_SIZE];
16+
shared FLOAT_TYPE sum_xg[BLOCK_SIZE];
17+
18+
void main() {
19+
const uint row = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
20+
const uint tid = gl_LocalInvocationID.x;
21+
22+
// Compute derivative of x[i]/norm(x) = g[i]/norm(x) - x[i] dot(x,g)/KX / norm(x)^1.5
23+
24+
// partial sums for thread in warp
25+
sum_xx[tid] = FLOAT_TYPE(0.0f);
26+
sum_xg[tid] = FLOAT_TYPE(0.0f);
27+
28+
[[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
29+
const FLOAT_TYPE gi = FLOAT_TYPE(data_a[row*p.KX + col]);
30+
const FLOAT_TYPE xi = FLOAT_TYPE(data_b[row*p.KX + col]);
31+
sum_xx[tid] += xi * xi;
32+
sum_xg[tid] += xi * gi;
33+
}
34+
35+
// sum up partial sums and write back result
36+
barrier();
37+
[[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
38+
if (tid < s) {
39+
sum_xx[tid] += sum_xx[tid + s];
40+
sum_xg[tid] += sum_xg[tid + s];
41+
}
42+
barrier();
43+
}
44+
45+
const FLOAT_TYPE eps = FLOAT_TYPE(p.param1);
46+
const FLOAT_TYPE mean = sum_xx[0] / FLOAT_TYPE(p.KX);
47+
const FLOAT_TYPE scale_g = inversesqrt(mean + eps);
48+
const FLOAT_TYPE scale_x = -scale_g * sum_xg[0] / (sum_xx[0] + FLOAT_TYPE(p.KX) * eps);
49+
50+
[[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
51+
data_d[row*p.KX + col] = D_TYPE(
52+
scale_g * FLOAT_TYPE(data_a[row*p.KX + col]) +
53+
scale_x * FLOAT_TYPE(data_b[row*p.KX + col]));
54+
}
55+
}

ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ layout (push_constant) uniform parameter {
2929
uint s1;
3030
uint s2;
3131
int sections[4];
32+
uint is_back;
3233
} p;
3334

3435
float rope_yarn_ramp(const float low, const float high, const uint i0) {
@@ -48,6 +49,10 @@ void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out
4849
// Get n-d magnitude scaling corrected for interpolation
4950
mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
5051
}
52+
// Backprogagation uses inverted rotation
53+
if (p.is_back != 0) {
54+
theta = -theta;
55+
}
5156
cos_theta = cos(theta) * mscale;
5257
sin_theta = sin(theta) * mscale;
5358
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#version 450
2+
3+
#include "generic_head.comp"
4+
#include "types.comp"
5+
6+
#extension GL_EXT_control_flow_attributes : enable
7+
8+
layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
9+
10+
layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
11+
layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
12+
13+
void main() {
14+
const uint i = gl_GlobalInvocationID.z * 262144 + gl_GlobalInvocationID.y * 512 + gl_GlobalInvocationID.x;
15+
16+
if (i >= p.KX) {
17+
return;
18+
}
19+
data_d[i] = D_TYPE(1. / (1 + exp(-1. *data_a[i])));
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#version 450
2+
3+
#include "generic_head.comp"
4+
#include "types.comp"
5+
6+
#extension GL_EXT_control_flow_attributes : enable
7+
8+
layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
9+
10+
layout (binding = 0) readonly buffer G {A_TYPE data_g[];};
11+
layout (binding = 1) readonly buffer X {B_TYPE data_x[];};
12+
layout (binding = 2) writeonly buffer D {D_TYPE data_d[];};
13+
14+
void main() {
15+
const uint i = gl_GlobalInvocationID.z * 262144 + gl_GlobalInvocationID.y * 512 + gl_GlobalInvocationID.x;
16+
17+
if (i >= p.KX) {
18+
return;
19+
}
20+
21+
// Compute derivative of SiLU(x): 1/(1+exp(-x)) - x*exp(-x)/(1+exp(-x))^2
22+
23+
const float xi = float(data_x[i]);
24+
const float s = 1.0f / (1.0f + exp(-xi));
25+
data_d[i] = D_TYPE(data_g[i] * (s + xi * s * (1 - s)));
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#version 450
2+
3+
#extension GL_EXT_control_flow_attributes : enable
4+
5+
#include "generic_head.comp"
6+
#include "types.comp"
7+
8+
layout(constant_id = 0) const uint BLOCK_SIZE = 32;
9+
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
10+
11+
// In this shader Y = softmax(X) and X is not provided as input.
12+
13+
layout (binding = 0) readonly buffer G {A_TYPE data_g[];};
14+
layout (binding = 1) readonly buffer Y {B_TYPE data_y[];};
15+
layout (binding = 2) buffer D {D_TYPE data_d[];};
16+
17+
shared FLOAT_TYPE sum_yg[BLOCK_SIZE];
18+
19+
void main() {
20+
const uint row = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
21+
const uint tid = gl_LocalInvocationID.x;
22+
23+
FLOAT_TYPE scale = p.param1;
24+
25+
// partial sums for thread in warp
26+
sum_yg[tid] = FLOAT_TYPE(0.0f);
27+
28+
[[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
29+
const FLOAT_TYPE gi = FLOAT_TYPE(data_g[row*p.KX + col]);
30+
const FLOAT_TYPE yi = FLOAT_TYPE(data_y[row*p.KX + col]);
31+
sum_yg[tid] += yi * gi;
32+
}
33+
34+
// sum up partial sums and write back result
35+
barrier();
36+
[[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
37+
if (tid < s) {
38+
sum_yg[tid] += sum_yg[tid + s];
39+
}
40+
barrier();
41+
}
42+
43+
const FLOAT_TYPE dot_yg = sum_yg[0];
44+
45+
[[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
46+
data_d[row*p.KX + col] = D_TYPE(scale
47+
* (FLOAT_TYPE(data_g[row*p.KX + col]) - dot_yg)
48+
* FLOAT_TYPE(data_y[row*p.KX + col]));
49+
}
50+
}

0 commit comments

Comments
 (0)