kohya-ss / kohya-ss/sd-scripts

[Feature Request] Dynamic padding to nearest multiple of 75 (e.g., 75/150.../750) for large token limits

Open
#2,256 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
7.2k
Forks
1.2k
Avg merge
11m
Merged PRs (30d)
2

Description

When training with large max_token_length values (e.g., 225, 375, 525, or even 750), the script currently pads all captions to the maximum defined length. For example, if I set max_token_length=750 but a batch only contains short captions (~100 tokens), padding them all to 750 (10 chunks) is extremely inefficient and causes OOM errors on limited VRAM.

I request a generalized dynamic padding feature that pads the batch to the nearest multiple of 75 based on the longest prompt in the current batch.

Logic:Let $L$ be the max token length in the current batch.The target padding length should be: $\lceil L / 75 \rceil \times 75$.

Examples:
Max length $\le$ 75 $\rightarrow$ Pad to 75.
Max length 100 $\rightarrow$ Pad to 150 (2 chunks).
Max length 300 $\rightarrow$ Pad to 300 (4 chunks).
Max length 700 $\rightarrow$ Pad to 750 (10 chunks).

This logic scales correctly for any max_token_length (e.g., 225, 375, 750) and ensures we always respect the CLIP model structure while maximizing VRAM efficiency.

I tried --no_token_padding, but it breaks the 75-token chunk structure required by CLIP (e.g., passing 80 tokens directly instead of 150), which causes quality degradation. Hardcoding fixed lengths like 225 is not flexible enough for longer context training.

Contributor guide

No contributing guide indexed for this repository

Research direction

No file or test is named. Locate the batch tokenization and padding entry point for max_token_length, then verify how the CLIP 75-token chunk structure is enforced. Done means each batch pads to ceil(L/75) × 75 without exceeding the configured limit and avoids the reported unnecessary VRAM use.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.