-
Notifications
You must be signed in to change notification settings - Fork 46
Adding jit support to Qwen3VL, readme fixes #170
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
Open
coder0143
wants to merge
3
commits into
jax-ml:main
Choose a base branch
from
coder0143:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -382,17 +382,16 @@ def test_rope_embedding(self): | |
| flax_visual = self.flax_model.model.visual | ||
|
|
||
| # Create grid_thw for a small image: 1 frame, 16x16 grid | ||
| grid_thw_np = np.array([[1, 16, 16]], dtype=np.int64) | ||
| grid_thw_pt = torch.tensor(grid_thw_np) | ||
| grid_thw_jax = jnp.array(grid_thw_np) | ||
| grid_thw_tuple = ((1, 16, 16),) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can rewrite these 3 lines as following: grid_thw_tuple = ((1, 16, 16),)
grid_thw_pt = torch.tensor(grid_thw_tuple, dtype=torch.long)and remove grid_thw_np as unused |
||
| grid_thw_pt = torch.tensor(grid_thw_tuple, dtype=torch.long) | ||
|
|
||
| with torch.inference_mode(): | ||
| pt_rope = pt_visual.rot_pos_emb(grid_thw_pt) | ||
| pt_emb = torch.cat([pt_rope, pt_rope], dim=-1) | ||
| pt_cos = pt_emb.cos().numpy() | ||
| pt_sin = pt_emb.sin().numpy() | ||
|
|
||
| flax_cos, flax_sin = flax_visual._rot_pos_emb(grid_thw_jax) | ||
| flax_cos, flax_sin = flax_visual._rot_pos_emb(grid_thw_tuple) | ||
|
|
||
| np.testing.assert_allclose(np.array(flax_cos), pt_cos, rtol=1e-5, atol=1e-5) | ||
| np.testing.assert_allclose(np.array(flax_sin), pt_sin, rtol=1e-5, atol=1e-5) | ||
|
|
@@ -409,9 +408,8 @@ def test_full_vision_encoder(self): | |
| grid_t, grid_h, grid_w = 1, 16, 16 | ||
| num_patches = grid_t * grid_h * grid_w # 256 patches before merge | ||
|
|
||
| grid_thw_np = np.array([[grid_t, grid_h, grid_w]], dtype=np.int64) | ||
| grid_thw_pt = torch.tensor(grid_thw_np) | ||
| grid_thw_jax = jnp.array(grid_thw_np) | ||
| grid_thw_tuple = ((grid_t, grid_h, grid_w),) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea here |
||
| grid_thw_pt = torch.tensor(grid_thw_tuple, dtype=torch.long) | ||
|
|
||
| key = jax.random.PRNGKey(42) | ||
| jx = jax.random.normal(key, (num_patches, per_patch_size), dtype=jnp.float32) | ||
|
|
@@ -428,7 +426,7 @@ def test_full_vision_encoder(self): | |
| else: | ||
| pt_out = pt_result[0].numpy() | ||
|
|
||
| flax_out, flax_deepstack = flax_visual(jx, grid_thw_jax) | ||
| flax_out, flax_deepstack = flax_visual(jx, grid_thw_tuple) | ||
| flax_out = np.array(flax_out) | ||
|
|
||
| # After spatial merge (2x2), 256 patches become 64 | ||
|
|
@@ -629,27 +627,27 @@ def test_position_embedding_output(self): | |
| """Check position embedding interpolation output matches.""" | ||
| inputs = self._create_dummy_image_input() | ||
| grid_thw_pt = inputs["image_grid_thw"] | ||
| grid_thw_jax = jnp.array(grid_thw_pt.numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in grid_thw_pt.long().tolist()) | ||
|
|
||
| with torch.inference_mode(): | ||
| pt_pos = self.pt_model.model.visual.fast_pos_embed_interpolate(grid_thw_pt).numpy() | ||
| flax_pos = np.array(self.flax_model.model.visual._fast_pos_embed_interpolate(grid_thw_jax)) | ||
| flax_pos = np.array(self.flax_model.model.visual._fast_pos_embed_interpolate(grid_thw_tuple)) | ||
|
|
||
| np.testing.assert_allclose(flax_pos, pt_pos, rtol=1e-5, atol=3e-5) | ||
|
|
||
| def test_rope_embedding(self): | ||
| """Check RoPE embedding output matches.""" | ||
| inputs = self._create_dummy_image_input() | ||
| grid_thw_pt = inputs["image_grid_thw"] | ||
| grid_thw_jax = jnp.array(grid_thw_pt.numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in grid_thw_pt.long().tolist()) | ||
|
|
||
| with torch.inference_mode(): | ||
| pt_rope = self.pt_model.model.visual.rot_pos_emb(grid_thw_pt) | ||
| pt_emb = torch.cat([pt_rope, pt_rope], dim=-1) | ||
| pt_cos = pt_emb.cos().numpy() | ||
| pt_sin = pt_emb.sin().numpy() | ||
|
|
||
| flax_cos, flax_sin = self.flax_model.model.visual._rot_pos_emb(grid_thw_jax) | ||
| flax_cos, flax_sin = self.flax_model.model.visual._rot_pos_emb(grid_thw_tuple) | ||
|
|
||
| np.testing.assert_allclose(np.array(flax_cos), pt_cos, rtol=1e-6, atol=2e-5) | ||
| np.testing.assert_allclose(np.array(flax_sin), pt_sin, rtol=1e-6, atol=2e-5) | ||
|
|
@@ -660,7 +658,7 @@ def test_vision_patch_plus_pos_output(self): | |
| pixel_values_pt = inputs["pixel_values"] | ||
| grid_thw_pt = inputs["image_grid_thw"] | ||
| pixel_values_jax = jnp.array(pixel_values_pt.numpy()) | ||
| grid_thw_jax = jnp.array(grid_thw_pt.numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in grid_thw_pt.long().tolist()) | ||
|
|
||
| # Get patch + pos embeddings | ||
| with torch.inference_mode(): | ||
|
|
@@ -669,7 +667,7 @@ def test_vision_patch_plus_pos_output(self): | |
| pt_hidden = (pt_patches + pt_pos).numpy() | ||
|
|
||
| flax_patches = self.flax_model.model.visual.patch_embed(pixel_values_jax) | ||
| flax_pos = self.flax_model.model.visual._fast_pos_embed_interpolate(grid_thw_jax) | ||
| flax_pos = self.flax_model.model.visual._fast_pos_embed_interpolate(grid_thw_tuple) | ||
| flax_hidden = np.array(flax_patches + flax_pos) | ||
|
|
||
| np.testing.assert_allclose(flax_hidden, pt_hidden, rtol=1e-5, atol=1e-5) | ||
|
|
@@ -680,7 +678,7 @@ def test_full_vision_output(self): | |
| pixel_values_pt = inputs["pixel_values"] | ||
| grid_thw_pt = inputs["image_grid_thw"] | ||
| pixel_values_jax = jnp.array(pixel_values_pt.numpy()) | ||
| grid_thw_jax = jnp.array(grid_thw_pt.numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in grid_thw_pt.long().tolist()) | ||
|
|
||
| with torch.inference_mode(): | ||
| pt_result = self.pt_model.model.visual(pixel_values_pt, grid_thw_pt) | ||
|
|
@@ -691,7 +689,7 @@ def test_full_vision_output(self): | |
| pt_out = self.pt_model.model.visual.merger(pt_hidden).numpy() | ||
| else: | ||
| pt_out = pt_result[0].numpy() | ||
| flax_out, _ = self.flax_model.model.visual(pixel_values_jax, grid_thw_jax) | ||
| flax_out, _ = self.flax_model.model.visual(pixel_values_jax, grid_thw_tuple) | ||
|
|
||
| self.assertEqual( | ||
| np.array(flax_out).shape, | ||
|
|
@@ -730,7 +728,7 @@ def test_vision_forward_with_numeric_check(self): | |
| input_ids_pt = inputs["input_ids"] | ||
|
|
||
| pixel_values_jax = jnp.array(pixel_values_pt.numpy()) | ||
| grid_thw_jax = jnp.array(grid_thw_pt.numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in grid_thw_pt.long().tolist()) | ||
| input_ids_jax = jnp.array(input_ids_pt.numpy()) | ||
|
|
||
| # Create token_type_ids: 1 for image tokens, 0 for text | ||
|
|
@@ -751,7 +749,7 @@ def test_vision_forward_with_numeric_check(self): | |
| cache = model_lib.init_cache(self.flax_config, batch, seq_len, 20) | ||
|
|
||
| flax_logits, cache = model_lib.forward_vision( | ||
| self.flax_model, cache, input_ids_jax, pixel_values_jax, grid_thw_jax, token_type_ids_jax | ||
| self.flax_model, cache, input_ids_jax, pixel_values_jax, grid_thw_tuple, token_type_ids_jax | ||
| ) | ||
|
|
||
| # Vision+text forward has larger tolerance due to accumulated numerical diffs | ||
|
|
@@ -785,7 +783,7 @@ def test_generation_with_vision_input(self): | |
| ) | ||
|
|
||
| pixel_values_jax = jnp.array(inputs["pixel_values"].numpy()) | ||
| grid_thw_jax = jnp.array(inputs["image_grid_thw"].numpy()) | ||
| grid_thw_tuple = tuple(tuple(row) for row in inputs["image_grid_thw"].long().tolist()) | ||
| input_ids_jax = jnp.array(inputs["input_ids"].numpy()) | ||
|
|
||
| # Create token_type_ids: 1 for image tokens, 0 for text | ||
|
|
@@ -797,7 +795,7 @@ def test_generation_with_vision_input(self): | |
|
|
||
| # Prefill with vision | ||
| logits, cache = model_lib.forward_vision( | ||
| self.flax_model, cache, input_ids_jax, pixel_values_jax, grid_thw_jax, token_type_ids_jax | ||
| self.flax_model, cache, input_ids_jax, pixel_values_jax, grid_thw_tuple, token_type_ids_jax | ||
| ) | ||
|
|
||
| # Verify cache position | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that finally we could not make the original code simple as we still need to iterate over its structure. I wonder if we can accept the type of image_grid_thw as
list[list[int]]instead oftuple[tuple[int, int, int]]? Can this work with jit and the type checker etc?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
list[list[int]]won't work directly withstatic_argnumsbecause lists are not hashable — JAX needs to hash static arguments for its compilation cache, and list raisesTypeError: unhashable type: 'list'.To make it simple we can do something like:
Which makes the callers simple:
@vfdev-5 Do tell if you want me to make the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally, I think it's fine to keep it everywhere as tuple of tuples. We have to do the conversion in tests only so it's ok