Skip to content

2.0.40: after #2036, a BPE training loop takes 5.6x the L1 misses (+11% cycles) #2082

Open

Description

@nuxyel

This is the second of two regressions I found in the same program. The first is #2081, about the take on the host, and it accounts for the instructions. This one accounts for most of the cycles. The program is a BPE tokenizer ([email protected], 256 merges learned from a 4,096-word text, then a 32,768-word text encoded). The repro and a counting script are in https://github.com/nuxyel/bend-ml/blob/devel/docs/upstream/bpe_encode.bend and bpe_encode.sh; run it with EVENTS=instructions:u,cycles:u,mem_load_retired.l1_miss:u. The setup was one thread pinned to a P-core of a Core Ultra 7 155H, clang 23.1.3 with -std=c11 -O3, and perf stat in user mode. The output is the same on every build.

What I measured. I bisected between the tags on L1 load misses and it stopped at #2036 (3c1f0d3). Against its parent (4024f04):

What I could pin down. #2036 changes three functions in this program's C. merge.go and count.go now keep a list element's token (B{n}/M{k}) boxed and share it with term_keep, where before they took it apart and built a fresh node. bump.go reuses the taken Cnt node in place. I put one function at a time back to its pre-#2036 C, inside 2.0.40's own C:

The misses are not in merge.go itself, though. The sampled profile (mem_load_retired.l1_miss:pp) puts them in bump.go, the walk over the pair counters during training. There are two reads:

  • heap_alloc reading a free block's next pointer (ALC_AT(e, cls) = e.mem[h]): 118 M to 630 M.
  • term_peek reading a redirect cell: 7 M to 465 M.

The allocation counts barely move (class 0: 1.262 G to 1.153 G). How long a one-word block waits on its free chain before it is handed out again has almost the same histogram before and after.

What I haven't proven: why. My guess is the heap layout that each merge pass leaves for the counting pass after it. Before #2036, merge.go rebuilt every token node in list order. Now the old nodes stay where they were, and the counter list's nodes come from free chains in another order, spread over more cache lines. I haven't measured lines touched or data addresses, so take it as a lead. It may also just be what this one program does; I haven't looked for it in others.

Activity

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