BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / GPU kernels (simulated)14 of 24 in topic

685. Merge split-KV partial attention results

MediumGPU kernels (simulated) Pro

Why it matters Split-KV decoding kernels use this merge to parallelize one long sequence across many SMs, and it must give exactly the same result as one big softmax.

For long-context decoding, one query's keys are split across several thread blocks. Each block returns its local max m_j, local softmax denominator l_j = sum exp(s - m_j), and its locally normalized output o_j. A final step merges them exactly.

Write merge_partials(ms, ls, os). ms and ls have length S, os has shape (S, D). Let M = max over splits with l_j > 0 of m_j. Weight w_j = l_j * exp(m_j - M) (0 for splits with l_j = 0, which may have m_j = -inf). Return (o, lse) where o = sum_j w_j o_j / sum_j w_j and lse = M + log(sum_j w_j). If every split is empty raise ValueError.

Functiondef merge_partials(ms, ls, os)

Example 1

Input: ms [1, 3], ls [2, 1], os [[1, 0], [0, 1]]

Output: o [0.2130, 0.7870] and lse 3.2395 (rounded to 4 places)

Constraints

  • os has shape (S, D)
  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Rescale each split's denominator to the global max before weighting its output.

Hint 2

M is the max over non-empty splits.

Hint 3

w_j = l_j * exp(m_j - M).

Hint 4

o = (w[:, None] * os).sum(0) / w.sum().

Common mistakes
  • Ignoring the different local maxima.
  • Letting an empty split with m = -inf produce nan.
  • Averaging outputs with equal weights.
Understand the idea

Imagine three cashiers each counting part of a pile of foreign coins, each converting to rupees at their own exchange rate. To get the true total you must convert all three counts to one common rate first. In split attention the exchange rate is exp(m_j), the local max each block subtracted for safety.

Worked example

split 1 has max 1 and sum 2, split 2 has max 3 and sum 1. Global M = 3. Weights: 2 x exp(1 - 3) = 0.271 and 1 x exp(0) = 1. Output = (0.271 [1, 0] + 1 [0, 1]) / 1.271 = [0.213, 0.787]. lse = 3 + log(1.271) = 3.24.

w_j = l_j exp(m_j - M), o = sum w_j o_j / sum w_j

This lets long-context decode spread one query's keys over many blocks and still give the exact softmax result.

Shapes and data flow
ms (S,)  ls (S,)  os (S, D)  ->  o (D,)  lse scalar
Topics
GPU kernels (simulated)
Similar questions
  • Count global memory loads for naive vs tiled matmulMedium
  • Calculate SM occupancy from threads, registers and shared memoryMedium
  • Reduce a warp with butterfly XOR shufflesMedium
  • Find the argmax with a parallel tree reduction and lowest-index tiesMedium
All GPU kernels (simulated) problems
Report
685 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def merge_partials(ms, ls, os):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS