Skip to content

refactor: simplify and deduplicate task classes - #133

Merged
justinchuby merged 7 commits into
mainfrom
justinchu/task-simplify
Apr 10, 2026
Merged

justinchuby merged 7 commits into
mainfrom
justinchu/task-simplify

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Summary

Addresses all 10 simplification opportunities identified in the task audit.

Changes

P1 — Shared _build_decoder: Added build_decoder_from_embeds() helper to _cache_utils.py. Eliminated 5 near-duplicate _build_decoder methods from VisionLanguageTask, QwenVLTask, HybridQwenVLTask, SpeechLanguageTask, and Phi4MMMultiModalTask.

P2 — Add model_roles: Added model_roles to 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): Added ComponentSpec class to _base.py and 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 dead name = class attributes from 7 tasks (DenoisingTask, ControlNetTask, AudioFeatureExtractionTask, AdapterTask, VideoDenoisingTask, VAETask, QwenImageVAETask).

P6 — Fix ModelPackage(config=): Fixed inconsistent construction in 5 tasks to pass config= consistently.

P7/P8 — Shared _build_embedding: Added build_embedding_from_features() helper shared by VisionLanguageTask and SpeechLanguageTask. Added name= 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

2322 passed, 29 skipped in 17.37s

Lint: clean (lintrunner)

…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]>
@github-actions

github-actions Bot commented Apr 9, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing d31f871 → 735e2cb

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 61 61 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 53 53 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 61 61 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 360 KB 360 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 103 103 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 61 61 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 61 61 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 409 409 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 174 174 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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=...), and ModelPackage(..., 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.

Comment thread src/mobius/tasks/_speech_language.py Resolved
Comment thread src/mobius/tasks/_tts.py Resolved
@github-actions

github-actions Bot commented Apr 9, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing d31f871 → 735e2cb

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

justinchuby and others added 5 commits April 8, 2026 22:22
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]>
@justinchuby justinchuby added the ai Created by an AI agent label Apr 10, 2026
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

codecov Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.64286% with 24 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/tasks/_task_test.py 92.80% 19 Missing ⚠️
src/mobius/tasks/_base.py 92.06% 4 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@justinchuby
justinchuby merged commit 1a6e048 into main Apr 10, 2026
20 of 22 checks passed
@justinchuby
justinchuby deleted the justinchu/task-simplify branch April 10, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Created by an AI agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants