Skip to content

5. Code quality: logging, constants, exceptions, protocols, architecture docs#10

Open
musicalplatypus wants to merge 10 commits into
TexasInstruments:mainfrom
musicalplatypus:pr/code-quality
Open

5. Code quality: logging, constants, exceptions, protocols, architecture docs#10
musicalplatypus wants to merge 10 commits into
TexasInstruments:mainfrom
musicalplatypus:pr/code-quality

Conversation

@musicalplatypus

Copy link
Copy Markdown

Summary

A collection of code quality improvements that make the codebase more maintainable, debuggable, and self-documenting. No behavioral changes — all improvements are structural.

Changes

1. Replace print() with logging module

  • Converted all print() calls in tinyml-modelmaker to use Python's logging module
  • Enables log level control, file output, and integration with external logging frameworks

2. Extract duplicated path resolution

  • Factored out identical path resolution logic from timeseries/runner.py and vision/runner.py into shared resolve_paths() utility
  • Eliminates 120+ lines of duplicated code

3. Replace magic strings with named constants

  • Added TRAINING_DEVICE_CPU, TRAINING_DEVICE_CUDA, TRAINING_DEVICE_MPS constants
  • Replaced hardcoded strings like 'cpu', 'cuda', 'tinyml_tinyverse' with constants

4. Replace assert/sys.exit/raise string with proper exceptions

  • assert statements used for input validation → raise ValueError
  • sys.exit() calls → raise RuntimeError
  • Bare string raises → typed exceptions with descriptive messages

5. Protocol definitions for component interfaces

  • Added protocols.py with typing.Protocol definitions for ModelRunner, Trainer, DatasetLoader
  • Enables static type checking and documents expected interfaces

6. Architecture documentation

  • Added ARCHITECTURE.md with codebase structure diagram, component descriptions, and improvement analysis

Files Changed (22 files)

All within tinyml-modelmaker

Testing

  • All changes are refactoring with no behavioral impact
  • Existing test suite passes without modification

@musicalplatypus musicalplatypus changed the title Code quality: logging, constants, exceptions, protocols, architecture docs 5. Code quality: logging, constants, exceptions, protocols, architecture docs Apr 7, 2026
Adithya-Thonse pushed a commit that referenced this pull request Jun 12, 2026
Merge in TINYML-ALGO/tinyml-agent-skills from 2026/pranav_a to main

* commit 'be83fc6267a898cc6eeab38ffe0c0a344528ab97':
  minor fixes
  removed assets, included autoMP quant
Adithya-Thonse added a commit that referenced this pull request Jun 12, 2026
de8af16d Pull request #45: https://jira.itg.ti.com/browse/TINYML_ALGO-698
REVERT: e48ef1a Pull request #14: TINYML_ALGO-711: fixing readme
REVERT: 16fc6a6 TINYML_ALGO-711: fixing readme
REVERT: e3639d2 Pull request #13: removing pycache
REVERT: f8bb3b7 removing pycache
REVERT: dd38428 Pull request #12: restructuring agent skill
REVERT: ff02a0e restructuring agent skill
REVERT: d26c6a5 Pull request #11: fixing tiny ml name
REVERT: 640ffd3 fixing tiny ml name
REVERT: 4ee3a19 Pull request #10: 2026/pranav a
REVERT: be83fc6 minor fixes
REVERT: e3a5700 removed assets, included autoMP quant
REVERT: 1af575a Pull request #9: correcting npu devices list
REVERT: 31e9eb1 correcting npu devices list
REVERT: 59b209b Pull request #8: improving readme
REVERT: 8c3260b improving readme
REVERT: 668916f Pull request #7: improving readme
REVERT: 68686b3 improving readme
REVERT: 814316e Pull request #6: fixes to readme and marketplace json
REVERT: e4bc0b4 fixes to readme and marketplace json
REVERT: 6a64208 Pull request #5: fixes to readme
REVERT: 0f9c868 fixes to readme
REVERT: 52f95ff Pull request #4: 2026/pranav a
REVERT: 443295d fixes to readme
REVERT: 1881112 fixes to readme and marketplace json
REVERT: 229ab57 Pull request #3: 2026/pranav a
REVERT: 6519104 minor readme fix
REVERT: 38e9f9f minor readme fix
REVERT: db81f81 Pull request #2: minor readme fix
REVERT: 1c0737a minor readme fix
REVERT: 0a0c02d Pull request #1: minor readme fix
REVERT: b682335 minor readme fix
REVERT: 062eb39 Initial Commit

git-subtree-dir: tinyml-agent-skills
git-subtree-split: de8af16d9e23de3e9bda3d811a0ebdece1178260
@musicalplatypus
musicalplatypus force-pushed the pr/code-quality branch 2 times, most recently from 6030cd3 to bf6dcd2 Compare July 25, 2026 11:37
t5fkg8d44d-beep and others added 10 commits July 26, 2026 12:22
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ve_paths()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TRAINING_BACKEND_TINYML_TINYVERSE, DATA_DIR_CLASSES, DATA_DIR_FILES,
DATA_DIR_IMAGES constants. Use existing TRAINING_DEVICE_CUDA in params.py
defaults instead of bare 'cuda' strings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…nyml-modelmaker

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…er, etc.)

Define typing.Protocol classes that formalize the implicit contracts already
followed by ModelRunner, ModelTraining, ModelCompilation, and DatasetHandling.
Uses structural subtyping so no existing classes need modification. Enables
static type checking and documents the interface contracts for future
implementations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ment analysis

Documents the full tinyml-tensorlab architecture including all four sub-repos,
pipeline flow, configuration system, model/quantization/NAS subsystems, a Mermaid
architecture diagram, and 12 design/implementation improvement recommendations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- dataset_utils.py: validate scalar split_factor in (0, 1) range; use
  split_factors list (not raw split_factor param) in post-normalization checks
  to avoid TypeError on float inputs
- misc_utils.py: guard input_data_path before os.path.basename (prevents
  TypeError when both dataset_name and input_data_path are unset); use
  absolutized projects_path as base for model_packaged_path; fix archive
  name for flat run_names (filter(None,...) removes empty leading segment
  so 'myrun' → myrun.zip instead of _myrun.zip)
- vision/runner.py: use v.get() for inference_time_us/sram/flash to avoid
  KeyError when a device entry is missing a metric

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sections 1 (No Test Suite) and 6 (No Abstract Base Classes or Protocols)
now accurately reflect what the PR stack addressed: the protocols module
in ai_modules/protocols.py and the test_protocols.py test suite.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…s refactor

0c1b23d moved path resolution out of ModelRunner and removed the
tinyml_torchmodelopt.quantization import along with it, but two uses remain in
run() at the packaging step. Any run that reaches model packaging raises
NameError: name 'TinyMLQuantizationVersion' is not defined, which makes
compilation fail on every platform.

Restore the import, matching how params.py, misc_utils.py and timeseries_base.py
already reference it.

Verified: compiling an exported ONNX for F28P55 now completes and produces
artifacts/mod.a, tvmgen_default.h and the deployment zip. All 20 Python files
changed on this branch import cleanly.
Companion to 3607b15. The resolve_paths refactor in 0c1b23d dropped this
import from both ai_modules/timeseries/runner.py and ai_modules/vision/runner.py;
only the timeseries one was restored. vision/runner.py still raises
NameError at lines 154 and 207 when a run reaches model packaging.

Found by running pyflakes across every file this branch touches — the earlier
import-only check could not catch it, since the name is referenced inside
run() and never evaluated at import time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants