Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llava next image encoder to allow un-aligned patch / image sizes #2936

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion router/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ impl LlavaNext {
pub fn get_number_of_features(&self, height: usize, width: usize) -> usize {
let image_size = self.vision_config.image_size;
let patch_size = self.vision_config.patch_size;
assert!(image_size % patch_size == 0);
if image_size % patch_size != 0 {
warn!(
"Image size {} is not divisible by patch size {}, will round down",
image_size, patch_size
);
}
let npatches = image_size / patch_size;
// Dimensions are intentionally swapped to be bug-compatible with
// upstream: https://github.com/LLaVA-VL/LLaVA-NeXT/issues/59
Expand Down Expand Up @@ -271,4 +276,26 @@ mod test {
let slots = config.get_number_of_features(1067, 1600);
assert_eq!(slots, 2144);
}

#[test]
fn test_uneven_division() {
let config = LlavaNext {
text_config: TextConfig {},
vision_config: VisionConfig {
image_size: 337, // Intentionally uneven
patch_size: 14,
},
image_grid_pinpoints: vec![
(336, 672),
(672, 336),
(672, 672),
(1008, 336),
(336, 1008),
],
};

// Should still work even with uneven division
let slots = config.get_number_of_features(640, 640);
assert_eq!(slots, 2928);
}
}
11 changes: 7 additions & 4 deletions server/text_generation_server/models/vlm_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ def get_number_of_features(height: int, width: int, config) -> int:
image_size = config.vision_config.image_size
patch_size = config.vision_config.patch_size

assert image_size % patch_size == 0
if image_size % patch_size != 0:
logger.warning(
f"Image size {image_size} is not divisible by patch size {patch_size}"
)

npatches = image_size // patch_size

Expand Down Expand Up @@ -520,9 +523,9 @@ def forward(
cuda_graph["input_lengths"].zero_()
cuda_graph["input_lengths"][: input_lengths.shape[0]] = input_lengths
cuda_graph["cache_lengths"].zero_()
cuda_graph["cache_lengths"][
: cache_lengths_tensor.shape[0]
] = cache_lengths_tensor
cuda_graph["cache_lengths"][: cache_lengths_tensor.shape[0]] = (
cache_lengths_tensor
)

with self._forward_context(
block_tables=cuda_graph["block_tables"],
Expand Down