Skip to content

Commit 6c15983

Browse files
committed
ggml: metal unary exp & neg
There isn't much peformance gain though. Just for more op coverage Signed-off-by: Molly Sophia <[email protected]>
1 parent 5f4dc3e commit 6c15983

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ static void ggml_backend_metal_device_rel(struct ggml_backend_metal_device_conte
127127
GGML_METAL_KERNEL_TYPE_SCALE_4,
128128
GGML_METAL_KERNEL_TYPE_CLAMP,
129129
GGML_METAL_KERNEL_TYPE_TANH,
130+
GGML_METAL_KERNEL_TYPE_EXP,
131+
GGML_METAL_KERNEL_TYPE_NEG,
130132
GGML_METAL_KERNEL_TYPE_RELU,
131133
GGML_METAL_KERNEL_TYPE_SIGMOID,
132134
GGML_METAL_KERNEL_TYPE_GELU,
@@ -734,6 +736,8 @@ @implementation GGMLMetalClass
734736
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SCALE_4, scale_4, true);
735737
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_CLAMP, clamp, true);
736738
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_TANH, tanh, true);
739+
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_EXP, exp, true);
740+
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_NEG, neg, true);
737741
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RELU, relu, true);
738742
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SIGMOID, sigmoid, true);
739743
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GELU, gelu, true);
@@ -1173,6 +1177,8 @@ static bool ggml_metal_supports_op(const struct ggml_backend_metal_device_contex
11731177
case GGML_UNARY_OP_GELU_QUICK:
11741178
case GGML_UNARY_OP_SILU:
11751179
case GGML_UNARY_OP_ELU:
1180+
case GGML_UNARY_OP_EXP:
1181+
case GGML_UNARY_OP_NEG:
11761182
return ggml_is_contiguous(op->src[0]);
11771183
default:
11781184
return false;
@@ -1739,6 +1745,30 @@ static void ggml_metal_encode_node(
17391745

17401746
[encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
17411747
} break;
1748+
case GGML_UNARY_OP_EXP:
1749+
{
1750+
id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_EXP].pipeline;
1751+
1752+
[encoder setComputePipelineState:pipeline];
1753+
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
1754+
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
1755+
1756+
const int64_t n = ggml_nelements(dst);
1757+
1758+
[encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
1759+
} break;
1760+
case GGML_UNARY_OP_NEG:
1761+
{
1762+
id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_NEG].pipeline;
1763+
1764+
[encoder setComputePipelineState:pipeline];
1765+
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
1766+
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
1767+
1768+
const int64_t n = ggml_nelements(dst);
1769+
1770+
[encoder dispatchThreadgroups:MTLSizeMake(n, 1, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
1771+
} break;
17421772
case GGML_UNARY_OP_RELU:
17431773
{
17441774
id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_RELU].pipeline;

ggml/src/ggml-metal/ggml-metal.metal

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,22 @@ kernel void kernel_tanh(
840840
dst[tpig] = precise::tanh(x);
841841
}
842842

843+
kernel void kernel_exp(
844+
device const float * src0,
845+
device float * dst,
846+
uint tpig[[thread_position_in_grid]]) {
847+
device const float & x = src0[tpig];
848+
dst[tpig] = precise::exp(x);
849+
}
850+
851+
kernel void kernel_neg(
852+
device const float * src0,
853+
device float * dst,
854+
uint tpig[[thread_position_in_grid]]) {
855+
device const float & x = src0[tpig];
856+
dst[tpig] = -x;
857+
}
858+
843859
constant float GELU_COEF_A = 0.044715f;
844860
constant float GELU_QUICK_COEF = -1.702f;
845861
constant float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;

0 commit comments

Comments
 (0)