Hardware-accelerated computation of the sigmoid (logistic) function

My apologies if this is redundant, but to the best of my recollection this has not been discussed in these forums previously.

The sigmoid function (or more accurately, the logistic function, as sigmoid functions are a class of functions) 1/ (1 + exp (-x)) can easily be computed with extremely high throughput by making use of the hardware-based computation of reciprocal and exponential base-2 (MUFU.RCP and MUFU.EX2 machine instructions). This leads to an instruction sequence comprising just four instructions in total, of which two are handled by the multi-function unit.

On some recent architectures, NVIDIA has reduced the throughput of multi-function unit operations relative to the throughput of FFMA, from 1:4 to 1:8. Having a function implementation based on two MUFU operations seems like a potential bottleneck in that new world. Luckily, a while back (with the Turing architecture, best I know) NVIDIA added a new MUFU.TANH instruction, and this can be used to compute the sigmoid function with just a single MUFU operation causing throughput to almost double: sigmoid(x) = (1 + tanh (x / 2)) / 2.

Optimizing for performance usually involves trade-offs, and that is also the case here. The classical computation via MUFU.RCP and MUFU.EX2 is very accurate, with a maximum error of 2 ulps in the positive half-plane and 30 ulps in the negative half-plane (prior to hitting the underflow boundary). The computation via MUFU.TANH incurs a maximum error of 67 ulps in the positive half-plane. Even worse, in the negative half-plane it suffers from subtractive cancellation, causing relative error and ulp error to grow rapidly ever larger with increasing magnitude of the argument.

Clearly the tanh-based computation should be used only on GPUs whose architecture supports MUFU.TANH. Below I experimented with a __tanhf() device function intrinsic that provides both a native implementation and emulation code. The use of the emulation code path results in good performance, but will make the sigmoid computation via __tanhf() achieve about 15% lower throughput than when using the classical computation, as measured on my Turing based Quadro RTX 4000.

/*
  Copyright 2023, Norbert Juffa

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* like copysignf(); when first argument is known to be positive */
__forceinline__ __device__ float copysignf_pos (float a, float b)
{
    return __int_as_float (__float_as_int (a) | (__float_as_int (b) & 0x80000000));
}

/* device function intrinsic for hyperbolic tangent */
__forceinline__ __device__ float __tanhf (float a)
{
#if (__CUDACC_VER_MAJOR__ >= 11) && (__CUDA_ARCH__ >= 750)
    // maxulperr = 133.95290, maxrelerr = 1.1126e-5
    float r;
    asm ("tanh.approx.f32 %0,%1; \n\t" : "=f"(r) : "f"(a));
#else
    // maxulperr = 108.82848, maxrelerr = 9.3450e-6
    const float L2E = 1.442695041f; // log2(exp(1))
    float e, r, s, t, d;
    s = fabsf (a);
    t = -L2E * 2.0f * s;
    asm ("ex2.approx.ftz.f32 %0,%1;\n\t" : "=f"(e) : "f"(t));
    d = e + 1.0f;
    asm ("rcp.approx.ftz.f32 %0,%1;\n\t" : "=f"(r) : "f"(d));
    r = fmaf (e, -r, r);
    if (s < 4.997253418e-3f) r = a;
    if (!isnan (a)) r = copysignf_pos (r, a);
#endif
    return r;
}

/* compute the sigmoid function 1/(1+exp(-x)) */
__forceinline__ __device__ float my_sigmoidf (float a)
{
#if USE_TANH
    return fmaf (0.5, __tanhf (0.5f * a), 0.5f);
#else // USE_TANH
    const float L2E = 1.442695041f; // log2(exp(1))
    float t, d, e, r;
    t = -L2E * a;
    asm ("ex2.approx.ftz.f32 %0,%1;\n\t" : "=f"(e) : "f"(t));
    d = e + 1.0f;
    asm ("rcp.approx.ftz.f32 %0,%1;\n\t" : "=f"(r) : "f"(d));
    return r;
#endif // USE_TANH
}
2 Likes