Skip to content

tests: make the MLX boundary guard catch mlx_lm imports - #108

Merged
shuk777 merged 1 commit into
Edge0-AI:mainfrom
VKVstudio:fix/hygiene-mlx-lm-regex
Oct 16, 2026
Merged

shuk777 merged 1 commit into
Edge0-AI:mainfrom
VKVstudio:fix/hygiene-mlx-lm-regex

Conversation

@VKVstudio

Copy link
Copy Markdown
Contributor

What

tests/test_repo_hygiene.py::test_mlx_imports_stay_inside_the_backend enforces the rule stated in backends/__init__.py: MLX packages are imported only under edge0/backends/mlx/. Its regex requires a space or a dot right after mlx, so from mlx_lm.models... import ... never matches and the guard silently passes framework code that imports mlx_lm directly.

This PR tightens the pattern to ^\s*(?:from|import)\s+mlx(?:_lm)?\b.

What it finds

The stricter guard immediately flags src/edge0/streaming/layer.py:49:

from mlx_lm.models.switch_layers import _gather_sort, _scatter_unsort

Rather than turn CI red on an existing file, that path is listed in an explicit _MLX_IMPORT_KNOWN_VIOLATIONS set with a note on what should move into the framework. Remove the entry once the two helpers no longer come from mlx_lm. I'm happy to send that follow-up: they are a few lines of argsort/gather that can live next to moe/routing.py, which would also make the streaming layer importable on a non-MLX backend.

If you would rather have the guard fail loudly instead, drop the allowlist — the regex change alone is the fix.

Checks

  • pytest tests/test_repo_hygiene.py — 4 passed. The file imports nothing from edge0 or mlx, so this runs on any platform, same as the existing Linux hygiene job.
  • The tightened regex matches import mlx, import mlx.core as mx, from mlx_lm import x, from mlx_lm.models.base import ..., and does not match from edge0.backends.mlx._impl import ... or a hypothetical mlxfoo package.

Context

I'm working on a CUDA/PyTorch backend for the reserved backends/cuda/ slot (https://github.com/smartbulldog-pro/edge0-cuda/tree/cuda-backend). This import is the one thing that keeps streaming/layer.py from importing on a non-MLX backend, which is how I ran into the guard.

@shuk777 shuk777 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for catching this gap. The check should cover mlx_lm as well as mlx. Recording the existing streaming-layer import in an allowlist keeps CI green, and the change is worth taking.

The conflict is from the test file moving. Please don't restore tests/test_repo_hygiene.py at the repo root. Apply it on python/tests/test_repo_hygiene.py instead:

  • Use the regex ^\s*(?:from|import)\s+mlx(?:_lm)?\b
  • Put python/src/edge0/streaming/layer.py in the allowlist
  • Keep the scan on python/src/edge0, and skip allowlisted files

Moving _gather_sort and _scatter_unsort out of mlx_lm would change the streaming path, and it is not required for this PR. Our next step is a unified inference framework across all platforms, planned for November. It is a substantial rework of the current framework, so there is no need to do that move on the current tree. We will use your notes as a reference. Once the new framework is out, we will let you know right away.

Thanks again.

@VKVstudio

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Updated as requested:

  • Merged current main into the branch; the guard now lives at python/tests/test_repo_hygiene.py (root file not restored).
  • Regex is ^\s*(?:from|import)\s+mlx(?:_lm)?\b in a raw string. The previous commit contained a stray backspace byte instead of \b, which made the pattern match nothing; that is fixed too.
  • python/src/edge0/streaming/layer.py is in the allowlist, and the scan stays on python/src/edge0.
  • pytest python/tests/test_repo_hygiene.py: 6 passed.

I will not send the _gather_sort / _scatter_unsort follow-up. Glad the notes are useful, and I look forward to the unified framework in November.

The regex in test_mlx_imports_stay_inside_the_backend required a space or a dot right after mlx, so from mlx_lm... imports never matched. Tighten it to (from|import)\s+mlx(?:_lm)?\b and list python/src/edge0/streaming/layer.py as a known violation until the helpers move into the framework.
@VKVstudio
VKVstudio force-pushed the fix/hygiene-mlx-lm-regex branch from 063355b to 82e9f7a Compare October 16, 2026 13:06
@VKVstudio

Copy link
Copy Markdown
Contributor Author

Force-pushed to fix the commit author; the branch is now a single commit on top of current main, with the same content as before.

@shuk777 shuk777 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks for ur contribute!

@shuk777
shuk777 merged commit 5b32e63 into Edge0-AI:main Oct 16, 2026
2 checks passed
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