feat(engine): Add Karmarkar-Karp algorithm#1151
feat(engine): Add Karmarkar-Karp algorithm#1151TaoZex wants to merge 22 commits intoinclusionAI:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements the Karmarkar-Karp (KK) sequence packing algorithm to enhance load balancing during training, adding configuration support in MicroBatchSpec and updating redistribution logic. Review feedback identifies that the new tests rely on inline implementations rather than the production code, which undermines their validity. Other suggestions include removing redundant or unused imports, updating outdated comments, and addressing a potential logic gap where the FFD fallback in the KK implementation may not respect the equal_size constraint.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the Karmarkar-Karp (KK) algorithm as an alternative sequence packing method to the existing First Fit Decreasing (FFD) algorithm. The KK algorithm, implemented with new utility classes and functions in areal/utils/seqpack.py, aims to provide more balanced micro-batch allocation, especially beneficial for variable-length sequences in large-scale RL training. The MicroBatchSpec in areal/api/cli_args.py is updated to allow configuration of the packing algorithm, and its usage is integrated into areal/infra/dist_rollout.py and areal/utils/data.py. Comprehensive documentation has been added to explain the new algorithms and their use cases, and extensive unit and end-to-end tests confirm the correctness and benefits of KK. Feedback includes improving the readability of nested attribute access in dist_rollout.py, clarifying the time complexity of the KK algorithm in the documentation, and refining the simulation logic in run_kk_vs_ffd.py to better mirror production code.
|
@garrett4wade Hi, would you please help me review code when you have some free time? Thanks~ |






Description
The core change adds a Karmarkar-Karp (Largest Differencing Method) partitioning algorithm as an alternative to the existing First Fit Decreasing (FFD) for sequence packing / micro-batch allocation. KK produces more balanced partitions (lower max-min spread across groups), which is especially beneficial for RL training workloads with highly variable sequence lengths and bimodal distributions.
The implementation is fully configurable: users can select "ffd" (default, backward-compatible) or "kk" via the new packing_algorithm field in MicroBatchSpec , either in YAML configs or programmatically.
Related Issue
Fixes #(issue)
Type of Change
Checklist
pre-commit run --all-files)./docs/build_all.sh)main/review-prcommand/create-prBreaking Change Details (if applicable):
Additional Context
Adds _KKSet , _KKState , _kk_partition (core KK Largest Differencing Method using a max-heap), kk_allocate (drop-in replacement for ffd_allocate with capacity safety net and FFD fallback), _compute_packing_metrics (diagnostic metrics: spread, CV, imbalance ratio, utilization, wasted tokens), and get_allocate_fn (algorithm registry / dispatch).
Need help? Check the Contributing Guide or ask in
GitHub Discussions!