Skip to content

Conversation

@oleksost
Copy link
Contributor

@oleksost oleksost commented Dec 19, 2025

✨ Description

  1. Improves memory efficiency of loss and grad calculation in rev. KL loss. Below lower lines is with these fixes, the upper ones without.
image
  1. Relies on sequence length instead of valid. token counts for loss & grad normalisation

🔍 Type of change

Select all that apply:

  • 🐛 Bug fix (non-breaking change that addresses a specific issue)
  • 🚀 New feature (non-breaking change that adds functionality)
  • ⚠️ Breaking change (a change that could affect existing functionality)
  • 📈 Performance improvement/optimization (improves speed, memory usage, or efficiency)
  • 🛠️ Code refactor (non-functional changes that improve code readability, structure, etc.)
  • 📦 Dependency bump (updates dependencies, including Dockerfile or package changes)
  • 📝 Documentation change (updates documentation, including new content or typo fixes)
  • 🔧 Infrastructure/Build change (affects build process, CI/CD, or dependencies)

📝 Changes

  • Manual rev. kl computation, with in-place operations
  • torch.compile for reverse KL loss
  • loss & grad normalisation using sequence length and not valid token count (see discussion [Prototype] Normalising by valid tokens #426)

✅ Checklist

Make sure the following tasks are completed before submitting the PR:

General

  • 📜 I have read and followed the contributing guidelines.
  • 🏷️ I am using a clear and descriptive PR title that summarizes the key change or feature introduced.
  • 🎉 The functionality is complete, and I have tested the changes.
  • 📝 I have updated the documentation if needed.
  • ⚠️ The change does not introduce any new issues (e.g., runtime warnings, type checker errors, linting problems, unhandled edge cases).
  • 🧩 I have commented my code, especially in hard-to-understand areas.

Dependencies and Configuration

  • 🐋 I have updated the Docker configuration or dependencies, if applicable.
  • 🔄 I have ensured compatibility with the existing setup after dependency changes.

Testing

  • 🧪 I have added or updated tests to cover my changes.
  • ✔️ New and existing tests pass locally with my changes.
  • 🚦 I have tested these changes on GPUs and verified training stability.
  • 🏋️ I have tested the changes on realistic training workloads, if applicable.

Performance Impact

  • 📊 I have run benchmarks where applicable to evaluate the performance impact.
  • ✅ The benchmarks show no performance regression.
  • 🚀 The benchmarks indicate a potential performance improvement.
  • ⚠️ The benchmarks indicate a potential performance degradation.
  • 📈 I have provided benchmark results and detailed any performance impact below, if applicable.

📊 Performance Impact Details

If there is any impact on performance, describe it and provide benchmark results, if applicable:


🗒️ Additional Notes

Include any additional context, information, or considerations here, such as known issues, follow-up tasks, or backward compatibility concerns.

Copy link
Collaborator

@jlamypoirier jlamypoirier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good idea, but have you tried with @torch.compile instead? It would speed things up in addition to the memory savings

@oleksost oleksost changed the title Efficient Rev. KL Reverse KL: more efficient implementation + normalisation by sequence length Dec 19, 2025
log_ratio = distributed_log_softmax(logits, group=group)

student_probs = log_ratio.exp()
log_ratio.sub_(teacher_log_probs) # In-place: log_ratio = student_log_probs - teacher_log_probs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.compile already handles in-place operations, so it's better to leave as out-of-place to avoid issues with torch.compile

@oleksost oleksost merged commit 44b14ac into main Dec 23, 2025
4 checks passed
@oleksost oleksost deleted the rev_kl_improvements branch December 23, 2025 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants