diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md index 39fe08b0..86f8202b 100644 --- a/docs/MULTI_GPU.md +++ b/docs/MULTI_GPU.md @@ -65,7 +65,7 @@ number per card after the first, not a count of layers per card. With 4 cards an (or `[24, 36, 42]`) puts layers 0-23 on the first card, 24-35 on the second, 36-41 on the third and 42-47 on the last. The server checks it before the start and says what is wrong (0.1.39, #644). -**Card order with `"auto"`** (NVIDIA, #1352): the faster card (multiprocessors x max clock) goes **last** - the last stage runs the head, the draft layer and the verify, and a prompt chunk waits on it (a 4070 Ti SUPER + 5060 Ti read a 6K prompt in 50 s one way round and 15 s the other). Equal cards keep your order; a manual split keeps it too. `"gpu_order": "as_given"` keeps the order you wrote under `"auto"` as well. The engine log line `layer split: card order ...` says when it changed. +**Card order with `"auto"`** (NVIDIA, #1352): the faster card (multiprocessors x max clock) goes **last** - the last stage runs the head, the draft layer and the verify, and a prompt chunk waits on it (a 4070 Ti SUPER + 5060 Ti read a 6K prompt in 50 s one way round and 15 s the other). Cards whose scores are within 5% of each other (two of the same model) keep your order; a manual split keeps it too. `"gpu_order": "as_given"` keeps the order you wrote under `"auto"` as well. The engine log line `layer split: card order ...` says when it changed. **Skip the split when the first card holds everything** (opt-in, 0.1.31): `"split_skip_if_fits": true` in the config (engine flag `--split-skip-if-fits`, with `--layer-split auto`) runs on the first card alone when it holds every diff --git a/serve/server.py b/serve/server.py index 0caeb272..a0fb2b88 100644 --- a/serve/server.py +++ b/serve/server.py @@ -2453,7 +2453,7 @@ def ordered_gpus(cfg: dict, scores=None, vrams=None) -> list[int]: """#1352: the cards of a layer split in the order the engine stages them. With "layer_split": "auto" (the default) the faster card goes LAST - the last stage runs the head, the draft layer and the verify, and a prompt chunk waits on it (the reporter's 4070 Ti SUPER + 5060 Ti: a 6K prompt took 50 s one way round and 15 s the - other); "faster" is multiprocessors x max clock. Equal cards keep the config's order (the sort is stable), and + other); "faster" is multiprocessors x max clock. Equal cards - scores within 5% - keep the config's order, and so does anything we cannot measure. "gpu_order": "as_given" keeps the config's order whatever the cards. A manual "layer_split" ("24") also keeps it: the user placed the layers. #1576: the reorder is skipped when it would land the last stage on a card with less total VRAM than the one the config put there - that stage's head, @@ -2469,6 +2469,8 @@ def ordered_gpus(cfg: dict, scores=None, vrams=None) -> list[int]: sc = scores if scores is not None else (hip_speed_scores(gl) if hip else gpu_speed_scores(gl)) if not sc or any(i not in sc for i in gl): return gl + if max(sc[i] for i in gl) < 1.05 * min(sc[i] for i in gl): + return gl # #1760: cards within 5% of each other are the same card (two 5070 Ti read 1.2% apart on the clock): keep the order out = sorted(gl, key=lambda i: sc[i]) if out != gl: vram = vrams if vrams is not None else (None if scores is not None else gpu_vram_mib(gl, hip)) diff --git a/serve/test_server.py b/serve/test_server.py index d6b57b85..362cfa74 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -1631,6 +1631,8 @@ class GpuChoice(unittest.TestCase): self.assertEqual(ordered_gpus(base, fast_first), [2, 0]) self.assertEqual(ordered_gpus({"gpu": [2, 0], "args": []}, fast_first), [2, 0]) self.assertEqual(ordered_gpus(base, {0: 5.0, 2: 5.0}), [0, 2]) # identical cards + self.assertEqual(ordered_gpus(base, {0: 173740000.0, 2: 171640000.0}), [0, 2]) # #1760: two 5070 Ti, 1.2% apart + self.assertEqual(ordered_gpus(base, {0: 106.0, 2: 100.0}), [2, 0]) # 6% apart: a real difference self.assertEqual(ordered_gpus({**base, "gpu_order": "as_given"}, fast_first), [0, 2]) self.assertEqual(ordered_gpus({**base, "layer_split": "24"}, fast_first), [0, 2]) self.assertEqual(ordered_gpus({**base, "args": ["--layer-split", "24"]}, fast_first), [0, 2]) diff --git a/src/prefill/gemm.cu b/src/prefill/gemm.cu index 29d9292a..88ecb793 100644 --- a/src/prefill/gemm.cu +++ b/src/prefill/gemm.cu @@ -606,39 +606,63 @@ void Gemm::bf16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64 } #endif #if !defined(__HIPCC__) - if (const int path = K > 0 ? bf16_path() : 0; path == 1 && N > 1 && beta == 0.0f) { - // a single output row stays cuBLAS's GEMV, faster than the conversions; beta = 1: see above - const int64_t x_rows = std::max(1, std::min(T, kXSliceElems / K)); - if (grow(tc_w_, tc_w_elems_, N * K) && grow(tc_x_, tc_x_elems_, x_rows * K)) { - bf16_to_f16(W, tc_w_, N * K, (cudaStream_t) stream_); - for (int64_t t0 = 0; t0 < T; t0 += x_rows) { - const int64_t n = std::min(x_rows, T - t0); - bf16_to_f16(X + t0 * K, tc_x_, n * K, (cudaStream_t) stream_); - f16(tc_x_, tc_w_, Y + t0 * ldy, n, N, K, ldy, beta); - } - return; - } - } #if defined(STRATA_EXPERIMENTAL_SM60) - else if (path == 2) { - // Pascal: fp32 copies (2 elements of the 2-byte buffers each). Every tile is a disjoint block of Y, so each - // gets the caller's beta. - const int64_t x_rows = std::max(1, std::min(T, kXSliceElems / K)); - if (grow(tc_w_, tc_w_elems_, 2 * N * K) && grow(tc_x_, tc_x_elems_, 2 * x_rows * K)) { - float* const wf = reinterpret_cast(tc_w_); - float* const xf = reinterpret_cast(tc_x_); - bf16_to_f32(W, wf, N * K, (cudaStream_t) stream_); - for (int64_t t0 = 0; t0 < T; t0 += x_rows) { - const int64_t n = std::min(x_rows, T - t0); - bf16_to_f32(X + t0 * K, xf, n * K, (cudaStream_t) stream_); - ck(cublasSgemm((cublasHandle_t) handle_, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) n, (int) K, &alpha, - wf, (int) K, xf, (int) K, &beta, Y + t0 * ldy, (int) ldy), - "cublasSgemm (bf16 on Pascal)"); + constexpr bool kPascalPath = true; +#else + constexpr bool kPascalPath = false; +#endif + if (const int path = K > 0 ? bf16_path() : 0; (path == 1 && N > 1 && beta == 0.0f) || (path == 2 && kPascalPath)) { + // FP16 (path 1) or FP32 (path 2, Pascal) copies of the weight and of X, converted in tiles. Issue #1757: when + // the buffers did not fit in the VRAM that was left (a stage of a layer split keeps ~100 MiB), this fell through + // to the raw BF16 cuBLAS call, which a Pascal card answers with CUBLAS_STATUS_INTERNAL_ERROR (14). Now the + // weight is converted in tiles of N rows (the whole weight when it fits, as before) and X in slices; a card with + // no room even for a tile says so. A single output row stays cuBLAS's GEMV for path 1 (faster than the + // conversions). Every tile is a disjoint block of Y, so each gets the caller's beta. + const int mult = path == 2 ? 2 : 1; // 2-byte elements per converted value + int64_t w_rows = N; + int64_t x_rows = std::max(1, std::min(T, kXSliceElems / K)); + bool fit = false; + for (;;) { + if (grow(tc_w_, tc_w_elems_, mult * w_rows * K) && grow(tc_x_, tc_x_elems_, mult * x_rows * K)) { fit = true; break; } + if (w_rows <= 64 && x_rows <= 64) break; + w_rows = std::max(std::min(w_rows, 64), w_rows / 2); + x_rows = std::max(std::min(x_rows, 64), x_rows / 2); + } + if (!fit && path == 2) { // Pascal has no BF16 cuBLAS call to fall back to; Volta keeps its raw one below + std::fprintf(stderr, "prefill gemm: no GPU memory left for the %s conversion buffers of a %lld x %lld BF16 weight " + "(Pascal cards run the BF16 products through them); free VRAM on this card: raise " + "--vram-reserve-mib, lower --max-context or --kv-resident, or give it fewer layers\n", + "FP32", (long long) N, (long long) K); + std::exit(1); + } + if (fit) { + for (int64_t n0 = 0; n0 < N; n0 += w_rows) { + const int64_t nn = std::min(w_rows, N - n0); + if (path == 2) { +#if defined(STRATA_EXPERIMENTAL_SM60) + float* const wf = reinterpret_cast(tc_w_); + float* const xf = reinterpret_cast(tc_x_); + bf16_to_f32(W + n0 * K, wf, nn * K, (cudaStream_t) stream_); + for (int64_t t0 = 0; t0 < T; t0 += x_rows) { + const int64_t n = std::min(x_rows, T - t0); + bf16_to_f32(X + t0 * K, xf, n * K, (cudaStream_t) stream_); + ck(cublasSgemm((cublasHandle_t) handle_, CUBLAS_OP_T, CUBLAS_OP_N, (int) nn, (int) n, (int) K, &alpha, + wf, (int) K, xf, (int) K, &beta, Y + t0 * ldy + n0, (int) ldy), + "cublasSgemm (bf16 on Pascal)"); + } +#endif + } else { + bf16_to_f16(W + n0 * K, tc_w_, nn * K, (cudaStream_t) stream_); + for (int64_t t0 = 0; t0 < T; t0 += x_rows) { + const int64_t n = std::min(x_rows, T - t0); + bf16_to_f16(X + t0 * K, tc_x_, n * K, (cudaStream_t) stream_); + f16(tc_x_, tc_w_, Y + t0 * ldy + n0, n, nn, K, ldy, beta); + } + } } return; } } -#endif #endif // Column-major view: Y^T[N, T] = W[N, K] (stored K x N col-major, transposed) . X^T[K, T]. ck(cublasGemmEx((cublasHandle_t) handle_, CUBLAS_OP_T, CUBLAS_OP_N, (int) N, (int) T, (int) K, &alpha, W,