ggml icon indicating copy to clipboard operation
ggml copied to clipboard

Tanh is not implemented

Open okpatil4u opened this issue 1 year ago • 10 comments

Hi there,

Apparently tanh function is not implemented in the library. Is this by design ? Which function can I use to replace tanh function ?

Thank you for your help.

okpatil4u avatar May 29 '23 07:05 okpatil4u

It's just it hasn't been needed yet.

You can either submit a PR implementing it, or you can use the existing ggml_map_unary_f32() which allows you to write custom operators in your project:

https://github.com/ggerganov/ggml/blob/db5eef149d569604a98708f6059dce63c6b9af1d/include/ggml/ggml.h#L953-L958

ggerganov avatar May 29 '23 10:05 ggerganov

Is the following implementation correct ?

void tanh_op(const int size, float *out, const float *in) {
    for (int i = 0; i < size; i++) {
        out[i] = tanh(in[i]);
    }
}

inpL = ggml_map_unary_f32(ctx0, inpL, tanh_op);

okpatil4u avatar May 30 '23 04:05 okpatil4u

Is the following implementation correct ?

void tanh_op(const int size, float *out, const float *in) {
    for (int i = 0; i < size; i++) {
        out[i] = tanh(in[i]);
    }
}

inpL = ggml_map_unary_f32(ctx0, inpL, tanh_op);

Does it give the results you expect?

LoganDark avatar May 30 '23 06:05 LoganDark

Yes, it does. Not sure if it is an optimized code.

okpatil4u avatar May 30 '23 07:05 okpatil4u

It looks perfectly fine to me. The only way you could make it faster is by processing multiple elements at once. (for example multithreading or SIMD)

Basically it seems optimal to me.

LoganDark avatar May 30 '23 07:05 LoganDark

Thanks. I want it single threaded. Should I close this issue ?

okpatil4u avatar May 30 '23 07:05 okpatil4u

If you don't need ggml to include a built-in operator for this, sure

LoganDark avatar May 30 '23 07:05 LoganDark

Yes, it does. Not sure if it is an optimized code.

Most compilers will detect the simple for loop and might unroll it or use simd to make it faster for you in higher optimization levels. :) So, looks perfectly fine. The only way to go faster here might be to have a tanh() thats faster-but less accurate...

Green-Sky avatar May 30 '23 09:05 Green-Sky

Yes, it does. Not sure if it is an optimized code.

Most compilers will detect the simple for loop and might unroll it or use simd to make it faster for you in higher optimization levels. :) So, looks perfectly fine. The only way to go faster here might be to have a tanh() thats faster-but less accurate...

This is what float16 is for :) small enough you can use a lookup table

LoganDark avatar May 30 '23 19:05 LoganDark

From the looks of it, #316 fixes this?

goerch avatar Jul 02 '23 19:07 goerch