-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Fix modular edge case + modular sorting order #35562
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9e000ed
look-ahead negation
Cyrilvallez 9fbd5d8
re add examples by default
Cyrilvallez f9763db
Fix the bug in topological sort
Cyrilvallez a8ae281
Update create_dependency_mapping.py
Cyrilvallez ed4cbc1
start adding test
Cyrilvallez 76ebe75
finalize test
Cyrilvallez 2916d3d
more tests
Cyrilvallez 36824d3
style
Cyrilvallez d3810cf
style
Cyrilvallez 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
import sys | ||
import unittest | ||
|
||
|
||
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) | ||
sys.path.append(os.path.join(ROOT_DIR, "utils")) | ||
|
||
import create_dependency_mapping # noqa: E402 | ||
|
||
|
||
# This is equivalent to `all` in the current library state (as of 09/01/2025) | ||
MODEL_ROOT = os.path.join("src", "transformers", "models") | ||
FILES_TO_PARSE = [ | ||
os.path.join(MODEL_ROOT, "starcoder2", "modular_starcoder2.py"), | ||
os.path.join(MODEL_ROOT, "gemma", "modular_gemma.py"), | ||
os.path.join(MODEL_ROOT, "olmo2", "modular_olmo2.py"), | ||
os.path.join(MODEL_ROOT, "diffllama", "modular_diffllama.py"), | ||
os.path.join(MODEL_ROOT, "granite", "modular_granite.py"), | ||
os.path.join(MODEL_ROOT, "gemma2", "modular_gemma2.py"), | ||
os.path.join(MODEL_ROOT, "mixtral", "modular_mixtral.py"), | ||
os.path.join(MODEL_ROOT, "olmo", "modular_olmo.py"), | ||
os.path.join(MODEL_ROOT, "rt_detr", "modular_rt_detr.py"), | ||
os.path.join(MODEL_ROOT, "qwen2", "modular_qwen2.py"), | ||
os.path.join(MODEL_ROOT, "llava_next_video", "modular_llava_next_video.py"), | ||
os.path.join(MODEL_ROOT, "cohere2", "modular_cohere2.py"), | ||
os.path.join(MODEL_ROOT, "modernbert", "modular_modernbert.py"), | ||
os.path.join(MODEL_ROOT, "colpali", "modular_colpali.py"), | ||
os.path.join(MODEL_ROOT, "deformable_detr", "modular_deformable_detr.py"), | ||
os.path.join(MODEL_ROOT, "aria", "modular_aria.py"), | ||
os.path.join(MODEL_ROOT, "ijepa", "modular_ijepa.py"), | ||
os.path.join(MODEL_ROOT, "bamba", "modular_bamba.py"), | ||
os.path.join(MODEL_ROOT, "dinov2_with_registers", "modular_dinov2_with_registers.py"), | ||
os.path.join(MODEL_ROOT, "instructblipvideo", "modular_instructblipvideo.py"), | ||
os.path.join(MODEL_ROOT, "glm", "modular_glm.py"), | ||
os.path.join(MODEL_ROOT, "phi", "modular_phi.py"), | ||
os.path.join(MODEL_ROOT, "mistral", "modular_mistral.py"), | ||
os.path.join(MODEL_ROOT, "phi3", "modular_phi3.py"), | ||
os.path.join(MODEL_ROOT, "cohere", "modular_cohere.py"), | ||
] | ||
|
||
|
||
def appear_after(model1: str, model2: str, priority_list: list[str]) -> bool: | ||
"""Return True if `model1` appear after `model2` in `priority_list`.""" | ||
return priority_list.index(model1) > priority_list.index(model2) | ||
|
||
|
||
class ConversionOrderTest(unittest.TestCase): | ||
def test_conversion_order(self): | ||
# Find the order | ||
priority_list = create_dependency_mapping.find_priority_list(FILES_TO_PARSE) | ||
# Extract just the model names | ||
model_priority_list = [file.rsplit("modular_")[-1].replace(".py", "") for file in priority_list] | ||
|
||
# These are based on what the current library order should be (as of 09/01/2025) | ||
self.assertTrue(appear_after("mixtral", "mistral", model_priority_list)) | ||
self.assertTrue(appear_after("gemma2", "gemma", model_priority_list)) | ||
self.assertTrue(appear_after("starcoder2", "mistral", model_priority_list)) | ||
self.assertTrue(appear_after("olmo2", "olmo", model_priority_list)) | ||
self.assertTrue(appear_after("diffllama", "mistral", model_priority_list)) | ||
self.assertTrue(appear_after("cohere2", "gemma2", model_priority_list)) | ||
self.assertTrue(appear_after("cohere2", "cohere", model_priority_list)) | ||
self.assertTrue(appear_after("phi3", "mistral", model_priority_list)) |
This file contains 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 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
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.
Congrats, much easier to read and understand from an (almost) outsider perspective. maybe naive notes/questions:
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.
Hey @molbap, thanks for looking into it! You raise some good points, I should have provided a bit more details:
Indeed in our case we can never have cycles as models are added chronologically, ensuring it (if at time 't', i.e. now, we dont' have cycles, we will never have any by adding more models: adding a model is equivalent to adding a new non existing node with 0 in-degree and as much out-degree as modeling imported). For our past model refactors, we might need to be slightly wary of this; it is our responsibility to ensure consistency here and not add cycles. But I'm extremely confident this case will never appear in practice, as we usually rely on the old "copied from" to know from which model we can refactor, which has the same notion of chronology. Worst case scenario, the person refactoring an old model and adding a cycle by mistake will immediately notice that there is a bug, and will be able to switch the "origin model" for the refactor.
Thus, the topology of our current (and future) graph (directed acyclic graph) ensures that the algorithm will not get stuck 👌🏻
At each step, the order of the nodes with 0 out-degree does not matter, so it is easier to use sets here as they have nice property and we can easily take the difference with the "-" operator!
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.