For vanilla GCG, top-k creates a conceptual pool of 32 x 256 = 8192 one-token edits. The code samples 512 position-token edits with replacement from that pool, applies each sampled edit to the current suffix, scores the resulting suffix proposals, and keeps the lowest-loss proposal. B is an outer batch of independent optimizer states; n is the candidate batch inside each state.