Skip to content

feature: add padding side to allow pad from left#17

Merged
aamir-s18 merged 2 commits intomainfrom
feature/add-padding-side
Jul 12, 2025
Merged

feature: add padding side to allow pad from left#17
aamir-s18 merged 2 commits intomainfrom
feature/add-padding-side

Conversation

@huangrpablo
Copy link
Copy Markdown
Contributor

Add configurable padding side for batch processing

Problem

The batch processor only supported right padding, which works for encoder-decoder models but causes issues with decoder-only models (like GPT) that require left padding to maintain proper attention patterns.

Solution

Added a padding_side parameter throughout the inference pipeline to control where padding tokens are added when batching sequences of different lengths.

Changes

  • Core: Enhanced stack_features() to support left/right padding
  • Sync: Added padding_side parameter to ModelBatchProcessor and @dynamically decorator
  • Async: Added padding_side parameter to AsyncModelBatchProcessor and async @dynamically decorator
  • Tests: Comprehensive test coverage for both padding modes

Usage

# For decoder-only models (GPT, etc.)
processor = ModelBatchProcessor(
    batch_func,
    batch_size=32,
    pad_tokens={"input_ids": 0},
    padding_side="left"  # New parameter
)

# Works with decorators too
@dynamically(batch_size=32, pad_tokens={"input_ids": 0}, padding_side="left")
def gpt_inference(features):
    return model(**features)

Backward Compatibility

  • ✅ Fully backward compatible - defaults to "right" padding
  • ✅ No breaking changes to existing code
  • ✅ All existing tests continue to pass

Example

# Input: [1, 2, 3] and [4, 5]
# Right padding (default): [[1, 2, 3], [4, 5, 0]]
# Left padding (new):      [[1, 2, 3], [0, 4, 5]]

Fixes issues with decoder-only model batching while maintaining full backward compatibility.

Copy link
Copy Markdown
Contributor

@aamir-s18 aamir-s18 left a comment

Choose a reason for hiding this comment

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

lgtm

@aamir-s18 aamir-s18 merged commit 11de7d7 into main Jul 12, 2025
4 of 5 checks passed
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.

2 participants