Skip to content

Fix ten PTX functional-simulation bugs (f16 rounding, 64-bit immediates, bfi/mad/mul.hi, conversions, signed loads, zero signs) - #154

Open
shatalov-alex wants to merge 10 commits into
accel-sim:devfrom
shatalov-alex:fix-ptx-functional-bugs
Open

shatalov-alex wants to merge 10 commits into
accel-sim:devfrom
shatalov-alex:fix-ptx-functional-bugs

Conversation

@shatalov-alex

Copy link
Copy Markdown

Ten independent fixes to the functional (PTX) simulation, one commit each. In every case GPGPU-Sim computed a different value from the one PTX specifies and NVIDIA GPUs produce. These are not timing-model changes.

I found them by running the same CUDA programs on GPGPU-Sim (dev, CUDA 12.6, PTX_SIM_MODE_FUNC=1) and as C++ on the CPU, comparing outputs element by element, then reducing each mismatch to single instructions.

Commit Instruction(s) Before Example
Round f16 results to nearest even cvt.rn.f16.f32, f16 arithmetic half.h/half.hpp configured for truncation and ties away from zero cvt.rn.f16.f32 of 1 + 2^-11 gave 0x3c01, not 0x3c00
Fix bfi.b64 for fields above bit 31 bfi.b64 32-bit masks for both widths sinf(200000.0f) returned 0 (large-argument reduction)
Form 64-bit products in mad.wide and mad.hi mad.wide/.hi.{u,s}32 product formed in 32 bits; wide addend read at 32 bits mad.wide.u32 d, 0xdeadbeef, 0xcafebabe, 2^32 gave 0x0000000088cf5b62
Keep 64-bit integer immediates whole all instructions with 64-bit immediates lexer stores literals in an int mov.u64 %rd, 4294967296 loaded 0
Convert the source in 64-bit integer to float conversions cvt.{f32,f64}.{s,u}64 converted the uninitialised result register cvt.rn.f64.s64 gave 0
Read the shift amount of 64-bit shifts as 32 bits shl/shr.b64, shr.s64 shifted by the register's full 64 bits shr.u64 d, 0xf000000000000000, 40 gave 0
Convert f32 to 64-bit integers without a 32-bit intermediate cvt.r*i.{s,u}64.f32 clamped to the 32-bit range 2^40 became 0xffffffff (u64)
Sign-extend ld.s8 and signed vector loads ld.s8, ld.v*.s* zero-extended; vector elements kept stale upper bits byte 0x8e loaded as 142, not -114
Implement mul.hi.u64 and mul.hi.s64 mul.hi.{u,s}64 assertion / low half returned 64-bit division by a constant aborted or was wrong
Get the sign of zero right in float abs and neg abs.f32/f64, neg.f16/f32/f64 x < 0 ? -x : x and 0 - x abs(-0.0) gave -0.0; neg(+0.0) gave +0.0

Several of these combine in nvcc's code for sinf/cosf with |x| > 105615: mov.u64 of a 64-bit constant, mad.wide.u32, bfi.b64, cvt.rn.f64.s64. Before this PR that path returned 0. It now matches the CPU's sinf within 1 ulp in the cases I checked.

Testing

  • Each commit builds on its own, with the Makefile flow, on aarch64 (CUDA 12.6.3, gcc 13.2), so the series bisects. clang-format 16.0.6 reports no changes.
  • The single-instruction checks and a sweep of 26 integer and float conversions (4,096 values each, device against host C semantics) are below. On dev the program aborts in mul.hi.s64; with this PR every check passes.
  • Unmodified CUDA kernels from llama.cpp and SAM 2, plus 300 randomly generated kernels, give the same outputs on GPGPU-Sim as on the CPU reference (720 test cases for the former).
  • I have not built or run these changes on x86, and I could not run the short-tests regressions (self-hosted runner).

Building on aarch64 also needs #153, which drops x86-only flags from the AccelWattch Makefile, like #150 did for CMake.

Reproducer (nvcc -O2 -fmad=false -cudart shared -gencode arch=compute_75,code=compute_75, run with PTX_SIM_MODE_FUNC=1)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define N 4096
__host__ __device__ inline unsigned fbits(float f) { unsigned u; memcpy(&u, &f, 4); return u; }
__host__ __device__ inline float bitsf(unsigned u) { float f; memcpy(&f, &u, 4); return f; }
#define OPS(X) \
  X(0, (unsigned long long)(unsigned short)(signed char)v) \
  X(1, (unsigned long long)(unsigned int)(signed char)v) \
  X(2, (unsigned long long)(short)(signed char)v) \
  X(3, (unsigned long long)(unsigned short)(short)v) \
  X(4, (unsigned long long)(unsigned int)(short)v) \
  X(5, (unsigned long long)(long long)(int)v) \
  X(6, (unsigned long long)(long long)(signed char)v) \
  X(7, (unsigned long long)(long long)(short)v) \
  X(8, (unsigned long long)(unsigned char)(short)v) \
  X(9, (unsigned long long)(~(unsigned int)(unsigned short)(signed char)v)) \
  X(10, (unsigned long long)fbits((float)(long long)v)) \
  X(11, (unsigned long long)fbits((float)(unsigned long long)v)) \
  X(12, (unsigned long long)fbits((float)(int)v)) \
  X(13, (unsigned long long)fbits((float)(unsigned int)v)) \
  X(14, (unsigned long long)(long long)(float)(int)(v >> 40)) \
  X(15, (unsigned long long)(long long)bitsf((unsigned)v & 0xde7fffffu)) \
  X(16, (unsigned long long)(unsigned long long)bitsf((unsigned)v & 0x5e7fffffu)) \
  X(17, (unsigned long long)(int)bitsf((unsigned)v & 0xceffffffu)) \
  X(18, (unsigned long long)(unsigned int)bitsf((unsigned)v & 0x4f7fffffu)) \
  X(19, (unsigned long long)((long long)v / ((long long)(v >> 33) | 1))) \
  X(20, (unsigned long long)((long long)v % ((long long)(v >> 33) | 1))) \
  X(21, (unsigned long long)((unsigned long long)v / ((v >> 29) | 1))) \
  X(22, (unsigned long long)((long long)v >> (v & 63))) \
  X(23, (unsigned long long)(short)((short)v * (short)(v >> 16))) \
  X(24, (unsigned long long)(signed char)((signed char)v + (signed char)(v >> 8))) \
  X(25, (unsigned long long)((int)v >> (v >> 59)))
#define COUNT 26
#if defined(__CUDACC__) && !defined(HOST_ONLY)
#define FN __host__ __device__
#else
#define FN
#endif
FN inline void eval(unsigned long long v, unsigned long long* o) {
#define X(i, e) o[i] = e;
  OPS(X)
#undef X
}
__global__ void k(const unsigned long long* in, unsigned long long* out) {
  unsigned i = blockIdx.x * blockDim.x + threadIdx.x; if (i < N) eval(in[i], out + i * COUNT);
}
// Fixed-value checks of single PTX instructions (results computed independently).
__global__ void asm_checks(unsigned long long* o, const unsigned* in) {
  unsigned a = in[0], b = in[1], s = in[2]; unsigned long long r;
  asm("bfi.b64 %0, %1, %2, 32, 32;" : "=l"(r) : "l"((unsigned long long)a), "l"((unsigned long long)b)); o[0] = r;
  asm("mad.wide.u32 %0, %1, %2, %3;" : "=l"(r) : "r"(a), "r"(b), "l"(0x100000000ull)); o[1] = r;
  asm("shr.u64 %0, %1, %2;" : "=l"(r) : "l"(0xF000000000000000ull), "r"(s)); o[2] = r;
  long long w; asm("mul.hi.s64 %0, %1, %2;" : "=l"(w) : "l"(-0x123456789abcdefll), "l"(0x7edcba9876543210ll)); o[3] = w;
  asm("mul.hi.u64 %0, %1, %2;" : "=l"(r) : "l"(0xfedcba9876543210ull), "l"(0x123456789abcdef1ull)); o[4] = r;
  float z; asm("abs.f32 %0, %1;" : "=f"(z) : "f"(-0.0f)); o[5] = fbits(z);
  asm("neg.f32 %0, %1;" : "=f"(z) : "f"(0.0f)); o[6] = fbits(z);
  o[7] = fbits(sinf(__uint_as_float(in[3])));  // 200000.0f: the large-argument path
}
int main() {
  int failures = 0;
  { // asm checks
    const unsigned in[4] = {0xDEADBEEFu, 0xCAFEBABEu, 40u, 0x48435000u};
    const unsigned long long want[8] = {0xdeadbeefcafebabeull, 0xb092ab7c88cf5b62ull, 0x0000000000f00000ull,
      0xff6fa8b3175e0fb5ull, 0x121fa00ad77d7423ull, 0x00000000ull, 0x80000000ull, 0};
    unsigned *di; unsigned long long o[8], *dout;
    cudaMalloc(&di, sizeof in); cudaMalloc(&dout, sizeof o); cudaMemcpy(di, in, sizeof in, cudaMemcpyHostToDevice);
    asm_checks<<<1, 1>>>(dout, di); cudaDeviceSynchronize(); cudaMemcpy(o, dout, sizeof o, cudaMemcpyDeviceToHost);
    const char* names[8] = {"bfi.b64", "mad.wide.u32", "shr.u64", "mul.hi.s64", "mul.hi.u64", "abs.f32 -0", "neg.f32 +0", "sinf(200000)"};
    // sinf is accurate to 2 ulp (CUDA's documented bound), not correctly rounded: compare with the host's.
    long long ulps = (long long)o[7] - (long long)fbits(sinf(200000.0f));
    if (ulps < -4 || ulps > 4) { printf("SELFTEST FAIL sinf(200000): %016llx, %lld ulp from the host\n", o[7], ulps); ++failures; }
    for (int i = 0; i < 7; ++i) if (o[i] != want[i]) { printf("SELFTEST FAIL %s: %016llx, want %016llx\n", names[i], o[i], want[i]); ++failures; }
  }
  { // conversion and integer sweep: device against host C semantics
    static unsigned long long in[N], out[N * COUNT], ref[COUNT];
    unsigned long long x = 88172645463325252ull;
    for (int i = 0; i < N; ++i) { x ^= x << 13; x ^= x >> 7; x ^= x << 17; in[i] = i < 256 ? (unsigned long long)(long long)(i - 128) * (i % 3 ? 1 : 0x10001) : x; }
    unsigned long long *din, *dout; cudaMalloc(&din, sizeof in); cudaMalloc(&dout, sizeof out);
    cudaMemcpy(din, in, sizeof in, cudaMemcpyHostToDevice); k<<<N / 128, 128>>>(din, dout); cudaDeviceSynchronize();
    cudaMemcpy(out, dout, sizeof out, cudaMemcpyDeviceToHost);
    for (int j = 0; j < COUNT; ++j) {
      int bad = 0; unsigned long long v = 0, want = 0, got = 0;
      for (int i = 0; i < N; ++i) { eval(in[i], ref); if (ref[j] != out[i * COUNT + j] && !bad++) { v = in[i]; want = ref[j]; got = out[i * COUNT + j]; } }
      if (bad) { printf("SELFTEST FAIL sweep op %d: %d of %d differ (v=%016llx: %016llx, want %016llx)\n", j, bad, N, v, got, want); ++failures; }
    }
  }
  printf(failures ? "SELFTEST FAILED\n" : "SELFTEST PASSED\n");
  return failures ? 1 : 0;
}

half.h and half.hpp were configured with HALF_ROUND_STYLE -1 (truncation)
and HALF_ROUND_TIES_TO_EVEN 0 (ties away from zero). cvt.rn.f16.f32 therefore
rounded ties away from zero, and f16 arithmetic truncated, while PTX .rn and
NVIDIA hardware round to nearest even. Example: cvt.rn.f16.f32 of
1 + 2^-11 gave 0x3c01 instead of 0x3c00.
bfi_impl used 32-bit masks and values for both widths, so bfi.b64 lost every
bit at position 32 and above. nvcc emits bfi.b64 to assemble 64-bit values;
CUDA's sinf/cosf use it in their large-argument (|x| > 105615) reduction,
which therefore returned 0.
mad_def multiplied two 32-bit operands in 32 bits before widening
(t.u64 = a.u32 * b.u32), so mad.wide.{u,s}32 and mad.hi.{u,s}32 lost the
upper half of the product; mul_impl already casts first. The addend of
mad.wide is now read at the doubled width as well.

Example: mad.wide.u32 d, 0xdeadbeef, 0xcafebabe, 0x100000000 gave
0x0000000088cf5b62 instead of 0xb092ab7c88cf5b62.
The PTX lexer stored integer literals in an int (atoi, sscanf %x), so
immediates beyond 32 bits were truncated: mov.u64 %rd, 4294967296 loaded 0.
nvcc emits such immediates for 64-bit constants, e.g. in CUDA's sinf/cosf
argument reduction. Literals are now parsed and stored as 64-bit values;
values that fit in 32 bits are unchanged.
In s2f and u2f the 64-bit branch converted y.s64 / y.u64, but y was never
initialised there; the 32-bit branch copies the source first. cvt.rn.f32.s64,
cvt.rn.f64.s64 and their unsigned forms returned garbage (often 0).
The shift amount of shl/shr is always a .u32 operand, but the 64-bit cases
shifted by b.u64, which includes whatever the register's upper bits hold.
shr.u64 d, 0xf000000000000000, 40 gave 0 instead of 0xf00000.
f2x converted through an int / unsigned int for every destination width, so
cvt.rzi.s64.f32 and cvt.rzi.u64.f32 were clamped to the 32-bit range: 2^40
became 0x7fffffff (s64) or 0xffffffff (u64). 64-bit destinations now round by
the instruction's mode and saturate to the 64-bit range, with NaN converting
to 0, as PTX specifies.
ld_exec sign-extended only .s16 and .s32 scalar loads, so ld.global.s8 into a
16- or 32-bit register was zero-extended (a byte 0x8e read as 142, not -114).
Vector loads read each element into an uninitialised ptx_reg_t, leaving stale
upper bits, and never sign-extended. Elements are now zeroed before the read
and signed elements are sign-extended.
mul.hi.u64 hit assert(!pI->is_hi()), and mul.hi.s64 (whose assertion is
commented out) returned the low half of the product. nvcc emits both for
64-bit integer division and remainder by constants. They now return the upper
64 bits of the 128-bit product. The signed low product is computed in
unsigned arithmetic to avoid signed-overflow undefined behaviour.
abs.f32/f64 used x < 0 ? -x : x, which returns -0.0 for -0.0, and
neg.f16/f32/f64 computed 0 - x, which returns +0.0 for +0.0. On NVIDIA GPUs
abs clears and neg flips the sign bit: abs(-0.0) is +0.0, neg(+0.0) is -0.0.
@shatalov-alex

Copy link
Copy Markdown
Author

Update: checked against real hardware. The reproducer in the description (unchanged, compiled to SASS for sm_75) passes every check on a Tesla T4 (driver 580.178.04, CUDA 12.6.3). So the expected values in the table are what the hardware produces, not only what the PTX spec says. On dev the same program aborts in mul.hi.s64; with this PR it passes on GPGPU-Sim as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant