Skip to content

On the CPU, a call before each fork keeps a fork tree on a few threads: 256 leaves get 3.5x from 16 threads instead of 14x #1475

Description

@wakamex

I hit this batching nanoGPT inference on the CPU (bend-bench ML.md): one generation per leaf, and each level of the tree forked the model's weight handles before forking the generations. 16 threads gave about 2x. Building every handle first, so the generation tree's forks follow each other directly, gave 6.1x on the same work.

Here's a small version. Each inner node runs k steps of a fork-free spin before it forks, and each leaf runs n steps:

import Base

type T is Type:
  Leaf{x: U32}
  Node{l: T, r: T}

def spin(+n: Nat, +acc: U32) -> U32:
  match n:
    case 0n:
      acc
    case 1n+p:
      spin(p, U32.add(U32.mul(acc, 1664525), 1013904223))

@unsafe
def tree(+d: Nat, +i: U32, +n: Nat, +k: Nat) -> T:
  match d:
    case 0n:
      Leaf{spin(n, i)}
    case 1n+e:
      node(e, n, k, spin(k, i))

@unsafe
def node(+e: Nat, +n: Nat, +k: Nat, +j: U32) -> T:
  x y = tree(e, U32.mul(j, 2), n, k) tree(e, U32.add(U32.mul(j, 2), 1), n, k)
  Node{x, y}

def total(t: T) -> U32:
  match t:
    case Leaf{x}:
      x
    case Node{l, r}:
      U32.xor(total(l), total(r))

@unsafe
def run(+d: Nat, +n: Nat, +k: Nat) -> U32:
  total(tree(d, 0, n, k))

@unsafe
def go(md: Maybe<&2, Nat>, mn: Maybe<&2, Nat>, mk: Maybe<&2, Nat>) -> IO(Unit):
  match md mn mk:
    case Some{+d} Some{+n} Some{+k}:
      IO.print(U32.show(run!(d, n, k)))
    case _ _ _:
      IO.print("usage: spread log2_leaves leaf_work level_work")

@unsafe
def dispatch(args: List<String>) -> IO(Unit):
  match args:
    case Con{p, Con{d, Con{n, Con{k, Nil{}}}}}:
      go(Nat.read(d), Nat.read(n), Nat.read(k))
    case _:
      IO.print("usage: spread log2_leaves leaf_work level_work")

@unsafe
def main() -> IO(Unit):
  do IO<Unit>:
    args : List<String> <- IO.args()
    dispatch(args)

taskset -c 0-15 ./spread 8 20000000 K --threads T --gpu off, so 256 leaves of 20M steps each. Main at d5fe656, Ryzen 9 3950X, median of 3 runs, each started with at most 1.5 CPUs busy with other work:

Steps before each fork (K) 1 thread 16 threads Speedup
0 4.89 s 0.34 s 14.6x
20,000 4.91 s 0.35 s 13.8x
200,000 5.16 s 0.38 s 13.7x
2,000,000 (a tenth of a leaf) 5.31 s 0.61 s 8.7x
10,000,000 7.47 s 1.79 s 4.2x
20,000,000 (a whole leaf) 10.15 s 2.86 s 3.5x

The extra work parallelizes fine in principle: the 255 inner nodes are as independent as the leaves, and the longest root-to-leaf chain is 9 × 20M steps, about 0.2 s. At K = 20M the 16-thread time should be close to 10.15 / 16 + 0.2 ≈ 0.8 s, not 2.86 s.

From reading comp.ts, I think this is how it happens:

  1. While the tree grows, a call to a fork-free def doesn't run: it's returned as a task, and the rest of the caller becomes its continuation (comp.ts#L2087).
  2. A growing lane skips fork-free tasks and pushes a returned one back (#L4470, #L4484), and row_grow stops after a pass with no fork (#L4679). So growth stops at the first spin(k) above a fork, and the forks under it are still inside its continuation.
  3. The drain runs each ring sequentially (#L4756). When a continuation forks there, task_deal with stride 0 puts the children back on the same ring (#L4493), past that drain's snapshot, so they wait for the next round of cube_run (#L5181). Each round only spreads what the last one exposed, and every round waits for its slowest thread.

The obvious small patch doesn't help. I let host lanes run fork-free tasks during growth instead of skipping them (DEVICE && on both checks in monk_step). Output is identical, and the times are too: 2.83 to 2.89 s at K = 20M, against 2.83 to 2.88 s before. Growth now gets past spin(k), but the whole tree starts in one row, so that row's thread does all 255 inner nodes' spins while the other rows stay empty. I think the real fix is a way for tasks that appear late to reach idle threads, such as idle threads stealing from other rows' rings. That's a bigger change to the pool loop, so I haven't tried it.

The workaround is to keep fork-free calls out of the levels of the tree. In my batch that meant building a tree of model handles first and then forking the generations over it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions