Hacker News

Top stories

Live mirror
30 storiesupdated just nowView source snapshot
  1. Human brain is two separate organs, Stanford Medicine-led research finds(stanford.edu ↗)
    115comments
  2. AI-generated posters don’t have to be horrible(john.hartnup.uk ↗)
    171comments
  3. “The Secret Life of Circuits” is here(coredump.cx ↗)
    26comments
  4. GPT-6 Astra Solves a WWI German Radio Cipher(prinzai.com ↗)
    81comments
  5. Android 17 is the first since 3.x to add new APIs without releasing to the AOSP(grapheneos.social ↗)
    450comments
  6. Laya the open source version of Jev(convaiinnovations.com ↗)
    3comments
  7. If math is more than proof, we need to better celebrate the rest of it(terrytao.wordpress.com ↗)
    109comments
  8. San Francisco Onion Futures Company(onionfutures.com ↗)
    77comments
  9. Communication by means of modulated Johnson noise(pnas.org ↗)
    2comments
  10. Cloudflare Quick Tunnels(cloudflare.com ↗)
    285comments
  11. How to Write with an LLM(sockpuppet.org ↗)
    342comments
  12. SDCC – Small Device C Compiler(sourceforge.net ↗)
    20comments
  13. Science Is Open Software(jepedersen.dk ↗)
    40comments
  14. Saving another 100TB of RAM(cloudflare.com ↗)
    83comments
  15. You can run Git on object storage if you re-make packfiles(tigrisdata.com ↗)
    19comments
  16. Why building a Rust LSP is hard(rust-glancer.github.io ↗)
    32comments
  17. How OpenAI Used Its Own LLMs to Design Its Jalapeño Chip(ieee.org ↗)
    94comments
  18. Ctenophores: Wonders of Biology(quantamagazine.org ↗)
    6comments
  19. NASA-IBM Lunar Foundation open-Source Geospatial AI Model(usra.edu ↗)
    4comments
  20. From Stonemasons to Carpenters(thelastsoftwareengineer.substack.com ↗)
    1comments
  21. The first new cat species discovered in 100 years(nationalgeographic.com ↗)
    108comments
  22. OpenJev(openjev.com ↗)
    270comments
  23. Show HN: Cactus Needle 3: 8-29MB automation models can match DeepSeek V4 Flash(cactuscompute.com ↗)
    89comments
  24. Goroutine Leak Profiles(go.dev ↗)
    4comments
  25. Photon-Emission-Guided Laser Fault Injection Enables RP2350 Secure Debug(ledger.com ↗)
    73comments
  26. Apple M6 Pro Achieves the Highest Single-Core CPU Score in Geekbench 7(geekbench.com ↗)
    83comments
  27. Veronese's Dogs(publicdomainreview.org ↗)
    1comments
  28. Cache-to-Cache: Direct Semantic Communication Between LLMs (2025)(arxiv.org ↗)
    14comments
  29. Warez: The Infrastructure and Aesthetics of Piracy (2021)(archive.org ↗)
    84comments
  30. Inside ZCode: Silently uploading your Git history to the cloud(ferstar.org ↗)
    102comments

Bugs in LLM Training – Gradient Accumulation Fix

81 pointsby 1y agounsloth.ai
16 comments
1y agoHN ↗

Oh hey! :) TLDR naively gradient accumulation was over-weighting short sequence lengths in LLM finetuning and training runs, and under-weighting long sequence lengths.

For eg a text with sequence lengths of [1, 100] would be scaled by 1/(100+1) in full batch training, but grad accum of 2 would weight [1] as 1/1 * 1/2 = 1/2, whilst [100] as 1/100 * 1/2 = 1/200. (1/2 since grad accum needs to divide by the # of grad accum steps)

1y agoHN ↗

Is this a general issue rather than unsloth specific. How wide is this problem? Sounds wild if it has been affecting everyones training.

1y agoHN ↗

Unfortunately it's not an Unsloth issue but a general issue affecting nearly all trainers which use grad accum. We worked with Huggingface so their trainers should be fixed now though in the main branch

1y agoHN ↗

> disadvantage of Transformers codebase using the copy-paste method for models, where this fix needs to be applied to every single model separately

What are the best tools we have available for tackling this kind of large scale copy-paste change?

https://github.com/huggingface/transformers/pull/34191/commi...

This feels too complex to tackle with PyCharm structural find and replace, even a more powerful structural find and replace like https://comby.dev/ feels underpowered here.

Sourcegraph batch changes? That solves broadcasting the change but doesn’t help with capturing the change to make.

Open rewrite? The python implementation is early stages, not prod ready as I understand it. Plus this change is too complex to use refaster templates even if we could use orw so you’d be debugging a fairly involved method visitor which in this case is probably orders of magnitude more time consuming than just making the changes manually.

What else is there that I don’t know about?

1y agoHN ↗

Ye a complete change was necessary for now - HF had to isolate the cross entropy loss and make another class for it, and it had to be applied to all model archs.

1y agoHN ↗

Unfortunately transformers is a general library for many models, and so there are tonnes of different architectures. Unfortunately copy paste and changing some parts of the arch is the only way feasible in the meantime.

1y agoHN ↗

Look from a different point of view: this is a feature, not a bug. With this, every example has equal weight, while with the fix, every token has equal weight.

1y agoHN ↗

Yes you're correct, but in normal full batch training without gradient accumulation, all tokens are weighted equally. Standard grad accum does not, and so the "fix" makes grad accum and full batch training finally mathematically equivalent

1y agoHN ↗

That makes it sound like it’s a choice, which it isn’t really. The way to look at it is from a probabilistic perspective: with the fix, you maximise the probability of the data. Without the fix, you fairly arbitrarily raise some probabilities to a power greater than one, and some to a power less than one.

1y agoHN ↗

Yes exactly- mathematically it was incorrect to begin with.

1y agoHN ↗

Although there may be uses for such a modified loss, based on the tone of the writeup it feels like this was an unintended bug in their training code. Training llms with variable max sequence length on different GPU is a recipe for inefficient training anyways, so careful optimizion of MFU at scale, or fixed max sequence length per batch, would have avoided this “bug”.

1y agoHN ↗

Ye one way to fix it is to use fixed sequence lengths, but it'll still be a tad bit off. Packing say 1000 small sequences to fit a large sequence lengths still will incur the same issue since the denominator will be off by 1000, but yes the problem is much less pronounced.

1y agoHN ↗

Not sure what you meant here; of course one needs to still correctly estimate the cross-entropy loss in the end (in order to keep their sanity, or compare to runs with different total batch size), but each mini-batch term has the same relative contribution to the entropy.

Edit: oh, I guess you probably meant that the code was previously not averaging correctly even for the case of same total mini-batch length... I haven't looked at this code.

1y agoHN ↗

Yes so making the sequences all the same ie through packing them into one still introduces issues since packing them into one has an unpadded token at the end due to the autoregressive nature of the training process.

1y agoHN ↗

Not sure what you mean. There is always an end to a batch. It doesn't have to be the end of a document entry, otherwise the model might get lazy and learn something related to the position of the text (i.e. look into the position encoding and call it a day).