Repository navigation
refactor: simplify and deduplicate task classes - #133
Merged
Merged
Conversation
…encies
P1: Extract shared _build_decoder into build_decoder_from_embeds() in _cache_utils.py,
eliminating 5 near-duplicate _build_decoder methods across VisionLanguageTask,
QwenVLTask, HybridQwenVLTask, SpeechLanguageTask, and Phi4MMMultiModalTask.
P2: Add model_roles to 10 tasks that were missing it: TTSTask, AdapterTask,
VideoDenoisingTask, CodecTask, Seq2SeqTask, SpeechToTextTask, VAETask,
QwenImageVAETask, SSMCausalLMTask, SSM2CausalLMTask.
P3: Add ComponentSpec class to _base.py and wire it into 8 multi-model tasks:
VisionLanguageTask, QwenVLTask, HybridQwenVLTask, MllamaVisionLanguageTask,
SpeechLanguageTask, Phi4MMMultiModalTask, TTSTask, VAETask, QwenImageVAETask,
CodecTask, Seq2SeqTask, SpeechToTextTask.
P4: Deduplicate SSMCausalLMTask and SSM2CausalLMTask by extracting shared
_build_ssm_task() helper, eliminating ~65 lines of copy-pasted graph wiring.
P5: Remove orphaned name= class attributes from 7 tasks: DenoisingTask,
ControlNetTask, AudioFeatureExtractionTask, AdapterTask, VideoDenoisingTask,
VAETask, QwenImageVAETask.
P6: Fix ModelPackage construction in 5 tasks to pass config= consistently:
DenoisingTask, ControlNetTask, AdapterTask, VideoDenoisingTask,
AudioFeatureExtractionTask.
P7: Add build_embedding_from_features() helper in _cache_utils.py, shared by
VisionLanguageTask and SpeechLanguageTask.
P8: Add graph name argument to all _make_graph() calls that were missing it:
multimodal, vision_language, feature_extraction, image_classification,
object_detection tasks.
P9: Export ComponentSpec from mobius.tasks.__init__.
P10: Verified __pycache__/ is in .gitignore (already present).
Co-authored-by: Copilot <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Performance Comparison
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors mobius.tasks to reduce duplication and make task wiring more consistent by centralizing common graph-building logic, adding structured component validation for multi-model tasks, and standardizing graph/model metadata used downstream (e.g., optimization roles and graph names).
Changes:
- Added shared helpers (
build_decoder_from_embeds(),build_embedding_from_features(),_build_ssm_task()) to remove duplicated task graph wiring. - Introduced
ComponentSpec+ModelTask._validate_components()and applied it across multiple multi-component tasks. - Standardized
model_roles,_make_graph(..., name=...), andModelPackage(..., config=...)usage across tasks.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/mobius/tasks/_vision_language.py | Adds explicit graph name for the built model graph. |
| src/mobius/tasks/_vision_language_3model.py | Refactors VL 3-model tasks to shared helpers + adds ComponentSpec validation and model_roles. |
| src/mobius/tasks/_video_denoising.py | Adds model_roles, names the graph, and passes config= into ModelPackage. |
| src/mobius/tasks/_vae.py | Adds model_roles + ComponentSpec, validates components, standardizes ModelPackage(config=). |
| src/mobius/tasks/_tts.py | Adds model_roles + ComponentSpec, validates components. |
| src/mobius/tasks/_ssm_causal_lm.py | Deduplicates SSM/SSM2 wiring via shared _build_ssm_task() and adds model_roles. |
| src/mobius/tasks/_speech_to_text.py | Adds model_roles + ComponentSpec and validates nested encoder/decoder components. |
| src/mobius/tasks/_speech_language.py | Refactors to shared decoder/embedding helpers + adds ComponentSpec validation and model_roles. |
| src/mobius/tasks/_seq2seq.py | Adds model_roles + ComponentSpec and validates encoder/decoder components. |
| src/mobius/tasks/_qwen_image_vae.py | Adds model_roles + ComponentSpec, validates components, standardizes ModelPackage(config=). |
| src/mobius/tasks/_phi4mm_multimodal.py | Refactors decoder graph building to shared helper + adds ComponentSpec validation and model_roles. |
| src/mobius/tasks/_object_detection.py | Names the graph consistently. |
| src/mobius/tasks/_multimodal.py | Names the graph consistently. |
| src/mobius/tasks/_image_classification.py | Names the graph consistently. |
| src/mobius/tasks/_feature_extraction.py | Names the graph consistently. |
| src/mobius/tasks/_denoising.py | Removes unused class name, adds model_roles, standardizes ModelPackage(config=). |
| src/mobius/tasks/_controlnet.py | Removes unused class name, adds model_roles, standardizes ModelPackage(config=). |
| src/mobius/tasks/_codec.py | Adds model_roles + ComponentSpec and validates encoder/decoder components. |
| src/mobius/tasks/_cache_utils.py | Adds shared multi-component graph-builder helpers for decoder/embedding construction. |
| src/mobius/tasks/_base.py | Introduces ComponentSpec and component validation support in ModelTask. |
| src/mobius/tasks/_audio_feature_extraction.py | Removes unused class name, adds graph name, standardizes ModelPackage(config=). |
| src/mobius/tasks/_adapter.py | Removes unused class name, adds model_roles, adds graph name, standardizes ModelPackage(config=). |
| src/mobius/tasks/init.py | Exports ComponentSpec via __all__ and imports. |
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
1. Move build_decoder_from_embeds and build_embedding_from_features from _cache_utils.py to _base.py where graph-building helpers belong. _cache_utils.py now contains only KV cache and linear-attention utilities. Update the 3 callers to import from _base. Export both helpers from mobius.tasks.__init__. 2. Add comment in build_decoder_from_embeds explaining why _cache_utils is imported inside the function body (namespace clarity — no circular dependency exists after the move). 3. Fix SpeechToTextTask.build() to call _validate_components() before the isinstance config check, consistent with all other tasks. 4. Add unit tests for both shared helpers in _task_test.py: - TestBuildDecoderFromEmbeds: return type, graph name, I/O names, MRoPE shape - TestBuildEmbeddingFromFeatures: return type, graph name, I/O names, custom feature name 5. Update _cache_utils.py module docstring to accurately describe the (now narrower) scope: KV cache and linear attention utilities only. Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Fix three inline review bugs on PR #133: 1. _vision_language_3model.py: operator precedence in image_size and patch_size fallbacks. The pattern 'a.b or default if a else default' parses as 'a.b or (default if a else default)', accessing a.b before the guard. Fixed to '(a.b if a else None) or default'. 2. _speech_language.py: same precedence bug in output_dim and n_mels fallbacks that access config.audio.* before the 'if config.audio' guard. Fixed with identical pattern. 3. _tts.py: speaker_encoder was listed as a required sub-module in the docstring but treated as optional in build(). Clarified the docstring to state it is optional (matches the existing build() behaviour and ComponentSpec which already omits it). Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
…ation - TestBuildDecoderFromEmbedsHybrid: 4 tests covering hybrid=True branch (linear_attention state inputs, full_attention kv inputs, graph name) - TestVisionLanguageTaskNoVisionConfig: 2 tests for config.vision=None precedence fix (defaults to image_size=224) - TestSpeechLanguageTaskNoAudioConfig: 2 tests for config.audio=None precedence fix (defaults to n_mels=128) - TestTTSTaskSpeakerEncoderOptional: 2 tests verifying speaker_encoder is optional and excluded from ComponentSpec - TestComponentSpecValidation: 8 tests for single/nested/missing attrs, error messages, and per-task validate calls (VisionLanguage, Seq2Seq, VAE) Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
The builder overwrites model.graph.name with '{model_id}/{key}' for every
model in the package, so any name= set during construction is discarded.
Remove name="model" from single-model tasks and name="decoder" from
multi-model decoder graphs — both are noise. Sub-model names (vision,
encoder, audio_encoder, vae_encoder/decoder, talker, code_predictor,
embedding, speaker_encoder) are retained as they describe the component role.
Update two tests that were asserting the now-removed "decoder" name.
Co-authored-by: Copilot <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
The test TestTTSTaskSpeakerEncoderOptional.test_build_with_speaker_encoder_attribute called task.components.keys() which failed because ComponentSpec only exposed .items() but not .keys(). Add keys() (returns output model names) and __contains__ (enables 'x' in spec) to give ComponentSpec a consistent dict-like inspection API alongside .items(). Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Addresses all 10 simplification opportunities identified in the task audit.
Changes
P1 — Shared
_build_decoder: Addedbuild_decoder_from_embeds()helper to_cache_utils.py. Eliminated 5 near-duplicate_build_decodermethods from VisionLanguageTask, QwenVLTask, HybridQwenVLTask, SpeechLanguageTask, and Phi4MMMultiModalTask.P2 — Add
model_roles: Addedmodel_rolesto 10 tasks that were missing it: TTSTask, AdapterTask, VideoDenoisingTask, CodecTask, Seq2SeqTask, SpeechToTextTask, VAETask, QwenImageVAETask, SSMCausalLMTask, SSM2CausalLMTask. Fixes role-detection bugs for AdapterTask (encoder, not decoder) and VideoDenoisingTask.P3 —
ComponentSpec(from PR #73): AddedComponentSpecclass to_base.pyand wired into 12 multi-model tasks. Provides structured validation that required sub-modules exist before graph construction begins.P4 — SSM deduplication: Extracted shared
_build_ssm_task()helper, eliminating ~65 lines of copy-pasted graph wiring between SSMCausalLMTask and SSM2CausalLMTask.P5 — Remove orphaned
name=: Removed deadname =class attributes from 7 tasks (DenoisingTask, ControlNetTask, AudioFeatureExtractionTask, AdapterTask, VideoDenoisingTask, VAETask, QwenImageVAETask).P6 — Fix
ModelPackage(config=): Fixed inconsistent construction in 5 tasks to passconfig=consistently.P7/P8 — Shared
_build_embedding: Addedbuild_embedding_from_features()helper shared by VisionLanguageTask and SpeechLanguageTask. Addedname=to all_make_graph()calls missing it.P9 — Export
ComponentSpec: Added to__init__.py__all__and import.P10 —
__pycache__in.gitignore: Already present, no change needed.Test results
Lint: clean (lintrunner)