@triton.jit
def tanh(x):
    tanh_neg = (tl.math.exp(x * 2) - 1) / (tl.math.exp(x * 2) + 1)
    tanh_pos = (1 - tl.math.exp(-2 * x)) / (1 + tl.math.exp(-2 * x))
    tanh = tl.where(x > 0, tanh_pos, tanh_neg)
    return tanh
