-
Notifications
You must be signed in to change notification settings - Fork 668
Soft-RFDETR : An optionnal Mixture of Experts Module to RFDETR #245
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
base: develop
Are you sure you want to change the base?
Changes from all commits
d20e165
b710955
3481974
30538ab
26bb865
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ class ModelConfig(BaseModel): | |
| resolution: int = 560 | ||
| group_detr: int = 13 | ||
| gradient_checkpointing: bool = False | ||
| MoE: bool = False | ||
| MoE_params: List[int] = [32, 1] | ||
|
||
|
|
||
| class RFDETRBaseConfig(ModelConfig): | ||
| encoder: Literal["dinov2_windowed_small", "dinov2_windowed_base"] = "dinov2_windowed_small" | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||||||||
| from soft_moe import SoftMoELayerWrapper | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| from soft_moe import SoftMoELayerWrapper | |
| try: | |
| from soft_moe import SoftMoELayerWrapper | |
| except ImportError: # soft_moe is optional and only needed for MoE | |
| class SoftMoELayerWrapper: # type: ignore[no-redef] | |
| """Placeholder wrapper used when soft_moe is not installed. | |
| This is instantiated only if MoE functionality is enabled. If you hit this | |
| error, install the optional `soft_moe` dependency: | |
| pip install soft-moe | |
| """ | |
| def __init__(self, *args, **kwargs) -> None: | |
| raise ImportError( | |
| "soft_moe is required for MoE support in TransformerDecoderLayer. " | |
| "Please install the optional dependency with:\n\n" | |
| " pip install soft-moe\n" | |
| ) |
Copilot
AI
Feb 19, 2026
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.
The import statement for soft_moe should be placed after the standard library imports and before the local imports, following PEP 8 import order conventions. Currently it's between torch and torch.nn.functional imports. Additionally, since this is a conditional dependency (only used when MoE=True), consider either: 1) Making it an optional dependency in pyproject.toml and importing it conditionally within the init method where it's used, or 2) Documenting that soft_moe is required for MoE functionality.
| from soft_moe import SoftMoELayerWrapper | |
| import torch.nn.functional as F | |
| from torch import nn, Tensor | |
| import torch.nn.functional as F | |
| from torch import nn, Tensor | |
| from soft_moe import SoftMoELayerWrapper |
Copilot
AI
Feb 19, 2026
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.
The FFNBlock class is missing mandatory type hints for all parameters and the return type, as required by the coding guidelines. Additionally, it lacks a Google-style docstring explaining its purpose, parameters, and return value. This is required for all new classes according to CONTRIBUTING.md.
Copilot
AI
Feb 19, 2026
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.
Using mutable default arguments (list) is a Python anti-pattern that can lead to unexpected behavior. The default value MoE_params=[32,1] should be replaced with None and then initialized inside the function. This applies to both the Transformer and TransformerDecoderLayer classes.
Copilot
AI
Feb 19, 2026
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.
The parameter naming MoE and MoE_params is inconsistent with the existing codebase conventions. Throughout the codebase, configuration parameters use snake_case (e.g., two_stage, bbox_reparam, skip_self_attn). These should be renamed to use_moe (or enable_moe) and moe_params to maintain consistency with codebase naming conventions.
Copilot
AI
Feb 19, 2026
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.
Inconsistent spacing in list literal. The default value [32,1] should have spaces after commas as per PEP 8 style guide: [32, 1]. This applies to all occurrences in the code.
| MoE=False, MoE_params=[32,1]): | |
| MoE=False, MoE_params=[32, 1]): |
Copilot
AI
Feb 19, 2026
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.
Inconsistent spacing in list literal. The default value [32,1] should have spaces after commas as per PEP 8 style guide: [32, 1].
| skip_self_attn=False, MoE=False, MoE_params=[32,1]): | |
| skip_self_attn=False, MoE=False, MoE_params=[32, 1]): |
Copilot
AI
Feb 19, 2026
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.
The parameter naming MoE and MoE_params is inconsistent with the existing codebase conventions. Throughout the codebase, configuration parameters use snake_case (e.g., two_stage, bbox_reparam, skip_self_attn). These should be renamed to use_moe (or enable_moe) and moe_params to maintain consistency with codebase naming conventions.
Copilot
AI
Feb 19, 2026
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.
Using mutable default arguments (list) is a Python anti-pattern that can lead to unexpected behavior. The default value MoE_params=[32,1] should be replaced with None and then initialized inside the function.
| skip_self_attn=False, MoE=False, MoE_params=[32,1]): | |
| super().__init__() | |
| skip_self_attn=False, MoE=False, MoE_params=None): | |
| super().__init__() | |
| if MoE_params is None: | |
| MoE_params = [32, 1] |
Copilot
AI
Feb 19, 2026
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.
The comment "done by @LeosCtrt" contains contributor attribution that should be removed from the code. Git history and PR metadata already track authorship. Keep code comments focused on explaining what the code does and why, not who wrote it.
| # Implementation of Feedforward or the MoE Layer (done by @LeosCtrt) | |
| # Implementation of the feedforward (FFN) or the MoE layer |
Copilot
AI
Feb 19, 2026
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.
Comparing boolean values with == True is not Pythonic. Use if self.MoE: instead of if self.MoE == True:. This applies to both occurrences in the code.
| if self.MoE == True: | |
| if self.MoE: |
Copilot
AI
Feb 19, 2026
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.
Using print() statements for architecture notifications is not consistent with the project's logging practices. According to the coding guidelines, use from rfdetr.util.logger import get_logger and log messages with logger.info() for user-facing messages or logger.debug() for detailed information. This ensures consistent logging behavior and respects the LOG_LEVEL environment variable.
Copilot
AI
Feb 19, 2026
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.
There's no validation for the MoE_params parameter. The code assumes it's a list with exactly 2 elements and directly accesses MoE_params[0] and MoE_params[1]. If the user provides an invalid value (e.g., empty list, single element, non-list), this will raise an IndexError. Add validation to ensure MoE_params contains exactly 2 positive integers before using them.
Copilot
AI
Feb 19, 2026
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.
The MoE implementation will significantly increase memory usage and model parameters. With the default num_experts=32, the FFN layer is replicated 32 times per decoder layer. For a 3-layer decoder, this means 96 expert FFN blocks instead of 3 regular FFN blocks. This should be documented in the docstring or config to help users understand the memory/compute tradeoffs. Consider adding a warning or documentation about the expected memory increase based on the number of experts.
Copilot
AI
Feb 19, 2026
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.
The activation variable is only initialized when MoE=False, but if MoE=True, the activation parameter is passed but never used since it's not assigned. This could cause an AttributeError if code later tries to access self.activation when MoE is enabled. Consider either removing the activation parameter entirely when MoE is True or initializing self.activation regardless of the MoE flag to maintain consistency.
Copilot
AI
Feb 19, 2026
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.
Comparing boolean values with == True is not Pythonic. Use if self.MoE: instead of if self.MoE == True:.
| if self.MoE == True: | |
| if self.MoE: |
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.
The new dependency
soft_moeis added without any version pinning, meaning builds will always pull the latest mutable release from an external third-party package, which increases the risk of a malicious or compromised update being pulled into your supply chain. An attacker who gains control over thesoft_moepackage or its distribution channel could execute arbitrary code in your environment with the application's privileges. To reduce this risk, pinsoft_moeto a specific, vetted version (or hash) and update it intentionally after review.