-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Feat: Apertus model implementation #15852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
64add82
86cfc18
8c762a3
ffdfd1d
74dcf89
ab11d94
1b18472
1606a3c
eec384f
b2a92d0
ef9ef66
d009194
73bd64f
28f9086
2f68c03
4294dbf
b2aa4fb
86a239c
bd19026
13cc3be
40f2f80
dbe0ccb
0d36139
fc58d3b
db9eb29
dc1e4d5
6c843ce
f11ab3c
57e2263
62401d8
58e6e0f
5a02bd4
90f052d
6972404
d29cb45
2ca353b
66f37c0
78ac06b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,15 @@ static inline float op_sqrt(float x) { | |
return sqrtf(x); | ||
} | ||
|
||
static inline float op_xielu(float x, float alpha_n, float alpha_p, float beta, float eps) { | ||
if (x > 0.0f) { | ||
return alpha_p * x * x + beta * x; | ||
} else { | ||
const float min_x_eps = fminf(x, eps); | ||
return (expm1f(min_x_eps) - x) * alpha_n + beta * x; | ||
} | ||
} | ||
|
||
static inline float op_sin(float x) { | ||
return sinf(x); | ||
} | ||
|
@@ -64,8 +73,8 @@ static inline float op_log(float x) { | |
return logf(x); | ||
} | ||
|
||
template <float (*op)(float), typename src0_t, typename dst_t> | ||
static inline void vec_unary_op(int64_t n, dst_t * y, const src0_t * x) { | ||
template <typename Op, typename src0_t, typename dst_t> | ||
static inline void vec_unary_op(const Op& op, int64_t n, dst_t * y, const src0_t * x) { | ||
pwilkin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
constexpr auto src0_to_f32 = type_conversion_table<src0_t>::to_f32; | ||
constexpr auto f32_to_dst = type_conversion_table<dst_t >::from_f32; | ||
|
||
|
@@ -74,8 +83,8 @@ static inline void vec_unary_op(int64_t n, dst_t * y, const src0_t * x) { | |
} | ||
} | ||
|
||
template <float (*op)(float), typename src0_t, typename dst_t> | ||
static void apply_unary_op(const ggml_compute_params * params, ggml_tensor * dst) { | ||
template <typename Op, typename src0_t, typename dst_t> | ||
static void apply_unary_op(const Op& op, const ggml_compute_params * params, ggml_tensor * dst) { | ||
const ggml_tensor * src0 = dst->src[0]; | ||
|
||
GGML_ASSERT(ggml_is_contiguous_1(src0) && ggml_is_contiguous_1(dst) && ggml_are_same_shape(src0, dst)); | ||
|
@@ -95,25 +104,25 @@ static void apply_unary_op(const ggml_compute_params * params, ggml_tensor * dst | |
dst_t * dst_ptr = (dst_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 ); | ||
const src0_t * src0_ptr = (const src0_t *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01); | ||
|
||
vec_unary_op<op>(ne0, dst_ptr, src0_ptr); | ||
vec_unary_op<decltype(op), src0_t, dst_t>(op, ne0, dst_ptr, src0_ptr); | ||
} | ||
} | ||
|
||
// TODO: Use the 'traits' lookup table (for type conversion fns), instead of a mass of 'if' conditions with long templates | ||
template <float (*op)(float)> | ||
static void unary_op(const ggml_compute_params * params, ggml_tensor * dst) { | ||
template <typename Op> | ||
static void unary_op(const Op& op, const ggml_compute_params * params, ggml_tensor * dst) { | ||
|
||
const ggml_tensor * src0 = dst->src[0]; | ||
|
||
/* */ if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) { // all f32 | ||
apply_unary_op<op, float, float>(params, dst); | ||
apply_unary_op<decltype(op), float, float>(op, params, dst); | ||
} else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) { // all f16 | ||
apply_unary_op<op, ggml_fp16_t, ggml_fp16_t>(params, dst); | ||
apply_unary_op<decltype(op), ggml_fp16_t, ggml_fp16_t>(op, params, dst); | ||
} else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_BF16) { // all bf16 | ||
apply_unary_op<op, ggml_bf16_t, ggml_bf16_t>(params, dst); | ||
apply_unary_op<decltype(op), ggml_bf16_t, ggml_bf16_t>(op, params, dst); | ||
} else if (src0->type == GGML_TYPE_BF16 && dst->type == GGML_TYPE_F32) { | ||
apply_unary_op<op, ggml_bf16_t, float>(params, dst); | ||
apply_unary_op<decltype(op), ggml_bf16_t, float>(op, params, dst); | ||
} else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) { | ||
apply_unary_op<op, ggml_fp16_t, float>(params, dst); | ||
apply_unary_op<decltype(op), ggml_fp16_t, float>(op, params, dst); | ||
} else { | ||
fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s\n", __func__, | ||
ggml_type_name(dst->type), ggml_type_name(src0->type)); | ||
|
@@ -122,65 +131,89 @@ static void unary_op(const ggml_compute_params * params, ggml_tensor * dst) { | |
} | ||
|
||
void ggml_compute_forward_abs(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_abs>(params, dst); | ||
unary_op(op_abs, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_sgn(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_sgn>(params, dst); | ||
unary_op(op_sgn, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_neg(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_neg>(params, dst); | ||
unary_op(op_neg, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_step(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_step>(params, dst); | ||
unary_op(op_step, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_tanh(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_tanh>(params, dst); | ||
unary_op(op_tanh, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_elu(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_elu>(params, dst); | ||
unary_op(op_elu, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_relu(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_relu>(params, dst); | ||
unary_op(op_relu, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_sigmoid(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_sigmoid>(params, dst); | ||
unary_op(op_sigmoid, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_hardsigmoid(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_hardsigmoid>(params, dst); | ||
unary_op(op_hardsigmoid, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_exp(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_exp>(params, dst); | ||
unary_op(op_exp, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_hardswish(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_hardswish>(params, dst); | ||
unary_op(op_hardswish, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_sqr(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_sqr>(params, dst); | ||
unary_op(op_sqr, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_sqrt(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_sqrt>(params, dst); | ||
unary_op(op_sqrt, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_sin(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_sin>(params, dst); | ||
unary_op(op_sin, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_cos(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_cos>(params, dst); | ||
unary_op(op_cos, params, dst); | ||
} | ||
|
||
void ggml_compute_forward_log(const ggml_compute_params * params, ggml_tensor * dst) { | ||
unary_op<op_log>(params, dst); | ||
unary_op(op_log, params, dst); | ||
} | ||
|
||
static float softplus(float input, float beta=1.0f, float threshold=20.0f) { | ||
if (input * beta > threshold) return input; | ||
return (1/beta) * logf(1 + expf(beta * input)); | ||
} | ||
|
||
void ggml_compute_forward_xielu(const ggml_compute_params * params, ggml_tensor * dst) { | ||
// Get the XIELU parameters from the operation | ||
const float * op_params = (const float*)dst->op_params; | ||
float alpha_n = op_params[0]; | ||
float alpha_p = op_params[1]; | ||
const float beta = op_params[2]; | ||
const float eps = op_params[3]; | ||
|
||
// alpha_p = softplus(alpha_p); | ||
// alpha_n = beta + softplus(alpha_n); | ||
|
||
const auto xielu_op_params = [alpha_n, alpha_p, beta, eps](float f) { | ||
return op_xielu(f, alpha_n, alpha_p, beta, eps); | ||
}; | ||
|
||
unary_op(xielu_op_params, params, dst); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to put this along the rest of the unary ops like RELU, GELU, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really :>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ggerganov Done.