Skip to content

chore(ci): enforce the // Used by: convention, staleness included - #3167

Merged
inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:ci/1141-used-by-check
Nov 22, 2026
Merged

inureyes merged 1 commit into
lablup:mainfrom
Jaeyeong-CHOI:ci/1141-used-by-check

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

Adds scripts/ci/check_used_by.py, which holds the Used by: annotations on the top-level pub fns of mlxcel-core's utils.rs and layers.rs to the files that really call them, and wires it as make verify-used-by and a used-by CI job. It fails on the pre-#1121 create_causal_mask annotation (41 of 48 caller files unaccounted for) and it found seven incomplete annotations on main, which this PR fixes.

Related issues

Closes #1141

Type of change

  • feat — new user-visible feature
  • fix — bug fix
  • perf — performance improvement (include before/after numbers in the PR body)
  • refactor — internal restructuring without behavior change
  • chore — build, CI, dependencies, release infrastructure
  • docs — documentation only (the docs/code-guidelines.md section, and comment-only edits to seven annotations)
  • test — tests only

What the check does

Callers are resolved, not grepped. A file is a caller of utils.rs::softcap when, outside test code, it imports the function by name, names it by a path that resolves to it through the file's own use and mod declarations, or calls it bare under a glob import of its module. Test code is a #[cfg(test)] item or a file declared by a #[cfg(test)] mod, never a file name pattern.

Five rules.

  1. More than 5 caller files and no Used by: annotation fails.
  2. The annotation is a /// line starting with Used by: (a // comment on a public item fails).
  3. Above 12 caller files, every caller must be accounted for: named in the roster (a path, or a family name that matches the file stem once case and separators are ignored, so Qwen 3.5 names qwen3_5.rs), or listed by the regenerating grep -rln '\bNAME(' DIR --include='*.rs' one-liner the annotation carries, which the check re-runs.
  4. If the annotation records a count next to that grep (N non-test files), it must equal the number of files the grep lists.
  5. A paragraph starting with Not used by must not name a file that calls the function.

--report prints every function with its caller files and how each one is accounted for.

Staleness strategy

The issue asked for the options to be weighed and the choice recorded. The full reasoning is in the script's module docstring; in short:

  • Chosen: option 1 (the machine-checkable trailer), widened so that a complete roster also passes, plus option 2 (negative claims) as rule 5. The check maps each caller file to the names that would account for it. That direction is mechanical and needs no name-to-path table.
  • Why not option 1 as specified (trailer mandatory above the threshold): three functions on main carry rosters for 14, 17 and 33 caller files (prefill_sliding_window_mask, materialized_state_tail, prefill_causal_mask) and all 64 files are named. A mandatory trailer fails them for length alone.
  • Why not option 3 (roster name to candidate files, flag a listed family with no call site): prose does not say which names are claims. The softplus annotation names eight families in its Used by: paragraph to say they call the raw FFI function instead. That reads as a stale roster to this heuristic and is not one.
  • Why not option 2 alone: the pre-docs(core): annotate the three tracked shared functions in utils.rs #1121 text made no negative claim, so it would have passed.

The cost of this choice is stated under "Limits" below.

The check fails on the pre-#1121 annotation

Run against the real tree at f0bf3a2c^ (33322d66e), extracted with git archive f0bf3a2c^ Cargo.toml src:

$ python3 scripts/ci/check_used_by.py --root <git archive of f0bf3a2c^>
used-by: FAIL
  51 top-level pub fn in 2 shared files, 25 annotated, 384 caller-file references resolved, 1222 Rust files scanned; not gated, at or below the enumeration threshold of 12: 17 annotation(s) with 43 unnamed caller file(s), see --report.
  src/lib/mlxcel-core/src/utils.rs:94: `create_causal_mask`: 41 of 48 caller files are not accounted for by the `Used by:` annotation: src/distributed/disaggregated/handoff_impl.rs, src/lib/mlxcel-core/src/layers.rs, src/lib/mlxcel-core/src/lib.rs, src/models/afmoe.rs, src/models/bailing_moe_linear.rs, src/models/cohere2.rs, src/models/cohere2_moe.rs, src/models/diffusion_gemma/mod.rs, src/models/ernie4_5_moe_vl.rs, src/models/exaone4.rs, src/models/falcon_h1.rs, src/models/falcon_ocr.rs and 29 more.
  src/lib/mlxcel-core/src/utils.rs:767: `repeat_kv` has 14 caller files outside its own module (threshold 5) and no `/// Used by:` annotation. Callers: src/audio/qwen3_omni_moe/speech_layers.rs, src/models/deepseek_v2.rs, src/models/ernie4_5_moe_vl.rs, src/models/glm4v.rs, src/models/glm4v_moe.rs, src/models/hunyuan_vl.rs, src/models/minicpm3.rs, src/models/nemotron_nas.rs, src/models/paddleocr_vl.rs, src/models/qwen2_vl.rs, src/models/qwen3_vl.rs, src/models/qwen3_vl_moe.rs and 2 more.
  src/lib/mlxcel-core/src/utils.rs:829: `gelu_approx` has 7 caller files outside its own module (threshold 5) and no `/// Used by:` annotation; its doc block mentions `Used by` but no line starts with `Used by:`. Callers: src/models/gpt2.rs, src/models/gpt_bigcode.rs, src/models/gpt_neox.rs, src/models/phi.rs, src/models/phixtral.rs, src/vision/encoders/youtu_vl_layers.rs, src/vision/encoders/youtu_vl_merger.rs.
  src/lib/mlxcel-core/src/layers.rs:4035: `attention_from_ptr`: 128 of 128 caller files are not accounted for by the `Used by:` annotation: src/audio/qwen3_omni_moe/mod.rs, src/audio/qwen3_omni_moe/speech_layers.rs, src/lib/mlxcel-core/src/cache/paged_batch_decode.rs, src/models/afmoe.rs, src/models/apertus.rs, src/models/baichuan.rs, src/models/bailing_moe.rs, src/models/bailing_moe_linear.rs, src/models/bitnet.rs, src/models/cohere.rs, src/models/cohere2.rs, src/models/cohere2_moe.rs and 116 more.

A `Used by:` annotation has to account for every file that calls the
function. Name the caller in the roster (its file stem, as `FooBar` for
`foo_bar.rs`, or its path), or, when the set is too long to enumerate,
close the annotation with the command that regenerates it:
  `grep -rln '\bNAME(' src --include='*.rs' | grep -v 'tests\.rs$'`
Run this script with --report to see each caller and how it is
accounted for. See docs/code-guidelines.md and issue #1141.
exit=1

The roster's seven hits are DeepSeek (2 files), GLM4 (2), Exaone, Hunyuan and Llama (the tensor-parallel llama_runtime.rs). The 48 reconciles exactly with the issue's figures: the same grep lists 55 files on that tree, 51 once tests.rs names are dropped, and the three the check does not count are utils.rs itself, cache.rs (calls only inside its #[cfg(test)] module) and gemma4_mtp_target.rs (a comment mention). The repeat_kv line is an independent cross-check: PR #1121's description counted "all 14 callers" by hand.

The same text put back on today's tree (only that doc block reverted) also fails: create_causal_mask: 10 of 14 caller files are not accounted for. The test script carries the text verbatim as its stale-roster case.

softcap is not miscounted

On this branch 74 files contain the string softcap and grep -rln '\bsoftcap(' lists four. The check resolves one caller of utils.rs::softcap, src/models/recurrent_gemma.rs. The private fn softcap in src/lib/mlxcel-xla/src/emitter/model.rs, the three softcap_logits definitions and the softcap: f32 parameters do not resolve to it. The same mechanism separates utils.rs::silu (25 caller files) from mlxcel_core::silu, the FFI function re-exported at the crate root (grep '\bsilu(' lists 112 files), and utils.rs::stack_arrays from the stack_arrays in gemma3n_helpers.rs and the private one in sam3/geometry.rs.

Annotations fixed on main

The check failed seven annotations on main (5288442b3). All edits are comment-only (/// lines in utils.rs and layers.rs); each caller list was read from --report and spot-checked in the source.

Function What was wrong
utils.rs::create_causal_mask Two callers outside src/models were not named: src/distributed/disaggregated/handoff_impl.rs (named in prose only as "disaggregated handoff", now given as a path) and src/vision/dense/sam3/text_encoder.rs (missing). Also, found by reading the report and not by a failure: Gemma3 was listed as a production caller, but its only calls are in #[cfg(test)] mod gemma3_mask_tests. It moved to the test-only list. The count sentence (13) is unchanged and still matches.
utils.rs::array_to_vec_f32 Named the /v1/embeddings engine and mlxcel embed. 32 files call it (rerank, token classification, Flux2, dense vision and detection predictors, three model files). Rewritten as groups plus the regenerating grep.
layers.rs::attention Named Llama3, Qwen3, Gemma2 and Gemma3. None calls it directly: Llama3, Qwen3 and Gemma3 call cache.attend, Gemma2 calls attention_from_ptr and causal_attention. 33 other files call it. This is the #1110 shape. Rewritten with the indirect routes, the direct groups, a Not used by ... directly paragraph and the grep.
layers.rs::attention_from_ptr A one-line rule with no way to regenerate its 134 callers. Grep added.
layers.rs::validate_quantization_params 12 of 18 caller files unrecorded. Grep added.
layers.rs::infer_quantization_mode 8 of 13 unrecorded. Grep added.
layers.rs::validate_quantized_packing 13 of 16 unrecorded. Grep added.

The new annotations record no counts, so a new caller does not force an edit to them.

Test plan

Run on macOS (Apple M4, Python 3.14.3; the checker was also run under the system Python 3.9.6), and again on Linux (DGX Spark, aarch64, Python 3.12.3, Rust 1.97.1) on this PR's commit (tree 051017a830f1). The cargo items were run on the Linux host only.

  • python3 scripts/ci/check_used_by.py on this branch: exit 0.
  • The same checker against main's utils.rs and layers.rs (before the annotation fixes): exit 1, the seven failures above.
  • The checker against the tree at f0bf3a2c^: exit 1, output above.
  • bash scripts/ci/check_used_by_test.sh: 23 cases pass. They cover an absent annotation above the threshold, the pre-docs(core): annotate the three tracked shared functions in utils.rs #1121 roster verbatim, a complete roster and one that then gains an unnamed caller, the rule form with its grep, a drifted count, a contradicted Not used by, a // annotation, malformed commands, a stale UNANNOTATED_DEBT entry, a missing scoped file, and the softcap look-alikes.
  • The test script was checked against two deliberately broken checkers: one with rule 3 disabled (an absence-only checker) and one with path resolution bypassed (a bare-identifier matcher). It fails on both (4 and 12 assertions).
  • make verify-used-by (and make -n verify to confirm the ordering).
  • python3 scripts/insert_apache_header.py --check: clean.
  • python3 -m unittest discover -s tests -p 'test_*.py': 240 tests, OK, 30 skipped (includes test_script_executable_modes, which covers the new script's mode).
  • The other pure-Python gates still pass: check_crate_versions, check_kernel_dtype_keys and its test, check_env_var_docs, check_native_env_writes, check_kernel_port_dispatch and its test, check_binary_assets, check_benchmark_raw_logs, check_benchmark_csv_schema, check_windows_reserved_paths, check_cuda_arch_lists, check_llama_compat_manifest, check_cross_repo_refs.
  • .github/workflows/ci.yml parses with PyYAML and the used-by job has the expected steps.
  • On Linux: python3 scripts/ci/check_used_by.py exit 0 (3.0 s), bash scripts/ci/check_used_by_test.sh 23 cases, make verify-used-by, python3 scripts/insert_apache_header.py --check, and python3 -m unittest discover -s tests -p 'test_*.py' (240 tests, OK).
  • cargo fmt --all -- --check: clean (Linux).
  • cargo test -p mlxcel-core --profile test-fast --features cuda --lib -- --test-threads=1 create_causal_mask: 4 passed, create_causal_mask_roster_count_matches_repo among them. It reads the count sentence this PR leaves at 13; rule 4 computes the same 13.
  • cargo clippy -p mlxcel-core --features cuda --all-targets -- -D warnings: clean (Linux). mlxcel-core is the only crate with a Rust diff, and that diff is /// lines.
  • Workspace-wide cargo clippy and cargo test --workspace — not run: the Rust diff is comment-only.
  • cargo deny check — not run (no dependency change).
  • The used-by CI job itself and actionlint — not run (not installed locally; the job will first run on this PR).
  • Real checkpoint validation — not applicable (no inference change).

Runtime

Three runs each on the M4, real seconds:

run 1 run 2 run 3
check_used_by.py 2.93 2.86 2.85
check_kernel_dtype_keys.py 0.33 0.29 0.29
check_used_by_test.sh 1.60 1.59 1.58
check_kernel_dtype_keys_test.sh 4.76 4.71 4.71

The checker reads all 2,815 Rust files (50 MB) and lexes the few hundred that mention a scoped function, so it is about ten times the dtype checker. With their test scripts the two make targets take 4.5 s and 5.0 s. For scale, check_native_env_writes.py, already a step in the env-var-docs job, takes 27 s on the same machine.

Notes for reviewers

Limits, so a green check is not read as more than it is.

  • A roster that still names a family which has stopped calling the function passes, unless the name is in a Not used by paragraph. Only additions are caught mechanically. The Gemma3 entry above is an example of what it does not catch.
  • Rule 3 is gated above 12 callers ("past roughly a dozen" in the guidelines). Applied to every annotation it fails 28 on main; the 21 below the threshold are a mix of real omissions (pipeline_hint does not mention Nanochat) and accurate prose that names a caller by what it does (materialized_axis_tail says "RotatingKVCache's first fill" for cache/first_fill.rs). Gating those would have meant rewording correct annotations to suit the matcher, so they are counted in the summary line and listed by --report instead. I left them untouched, including the Nanochat one, to keep this PR to what the gate requires. ENUMERATION_THRESHOLD is the knob.
  • Name matching is loose in one direction: DeepSeek accounts for every deepseek_*.rs caller, a directory path accounts for everything under it, and a capitalised word equal to a file stem accounts for that file. It can hide a missing name. It cannot fail a correct annotation.
  • create_causal_mask has 14 callers today, two above the threshold. The pre-docs(core): annotate the three tracked shared functions in utils.rs #1121 text is caught on today's tree only because of that margin; on the tree it was written for the count was 48.

Decisions I would like checked.

  • UNANNOTATED_DEBT instead of one N. The issue says to pick N against the tree so the check starts green. slice_axis, silu and stack_arrays are unannotated with 71, 25 and 18 callers, so a single N would be 71 and the absence rule would apply to almost nothing. I set N to 5 (the next unannotated maximum, relu_squared) and grandfathered those three by name in a list that fails on an entry that no longer applies. That is a hand-kept list of functions, which the issue advises against for scoping. If you prefer the single N, it is a five-line change. I did not annotate the three here because that is new documentation, not a stale-annotation fix.
  • Scope. SCOPED_FILES is the two files the issue names. batched_decode.rs, also listed in the guidelines, has one top-level pub fn and is not included.
  • Overlap with the Rust test. create_causal_mask_roster_count_matches_repo in utils.rs (chore(core): the create_causal_mask caller roster is stale and has no guard #1767) checks the same count as rule 4, for that one function. I left it in place (it passes); it can be retired in a follow-up.
  • CONTRIBUTING.md. I added verify-used-by to the list of make verify prerequisites and bumped "ten" to "eleven". The list already omitted verify-cuda-runtime-dir before this PR, so the true count is twelve. I did not touch that.

An observation outside the gate. The gelu_approx annotation says "about 80 non-test call sites" and gives grep -rn 'gelu_approx('. Two files call utils.rs::gelu_approx; the rest call mlxcel_core::gelu_approx, the FFI function. The annotation is about the formula, so I left it, but it is the softcap kind of count.

Checklist

  • PR title uses a Conventional Commits prefix (feat:, fix:, etc.)
  • One logical change per PR (split unrelated changes)
  • Updated docs/ if user-facing behavior or supported models changed (docs/code-guidelines.md gains an Enforcement section; CONTRIBUTING.md lists the new target and job)
  • Updated // Used by: ... comments on any shared function I modified (see docs/code-guidelines.md)
  • No secrets, credentials, or .env files committed

docs/code-guidelines.md opens with the shared-function rule: a helper
that several models depend on records which of them call it. Nothing
checked it, and the failure that matters is a wrong annotation, not a
missing one. Issue lablup#1110 was filed against a `create_causal_mask`
roster naming Llama, Mixtral, Cohere, Phi, GLM4 and StarCoder2, none of
which had called it for several releases. A check for absence passes
that file.

scripts/ci/check_used_by.py reads every top-level `pub fn` in
mlxcel-core's utils.rs and layers.rs and resolves the files that really
call each one through their `use` declarations, so a private
`fn softcap`, a `softcap_logits`, a `softcap: f32` parameter and the
crate-root FFI re-exports of the same name are not counted. It then
enforces five rules:

1. More than 5 caller files needs a `Used by:` annotation.
2. The annotation is a `///` line that starts with `Used by:`.
3. Above 12 caller files, every caller is accounted for: named in the
   roster (by path, or by a family name that matches the file stem), or
   listed by the regenerating grep the annotation carries, which the
   check re-runs.
4. A count recorded next to that grep is the number of files it lists.
5. A `Not used by` paragraph names no file that calls the function.

Run against the tree before lablup#1121 (33322d6), rule 3 reports 41 of
`create_causal_mask`'s 48 caller files as unaccounted for. The module
docstring records why this combination was chosen over the three
options the issue listed, and what it does not catch.

On main the check failed seven annotations, fixed here as comment-only
edits:

- utils.rs `create_causal_mask`: handoff_impl.rs and the SAM 3 text
  encoder were missing from the callers outside src/models, and Gemma3
  was listed as a production caller although its only calls are in a
  test module.
- utils.rs `array_to_vec_f32`: named the embeddings engine only; 32
  files call it.
- layers.rs `attention`: named Llama3, Qwen3, Gemma2 and Gemma3, none
  of which calls it directly; 33 other files do.
- layers.rs `attention_from_ptr`, `validate_quantization_params`,
  `infer_quantization_mode`, `validate_quantized_packing`: 134, 12, 8
  and 13 caller files unrecorded. Each now closes with its regenerating
  grep.

`slice_axis`, `silu` and `stack_arrays` are unannotated with 71, 25 and
18 callers and are grandfathered in the script's UNANNOTATED_DEBT,
which fails on an entry that no longer applies. Below the threshold of
12, 21 annotations leave 39 callers unnamed; they are reported by
--report and counted in the summary line, not gated.

Wired as `make verify-used-by` (in `verify` and `verify-rocm`) and as
the `used-by` CI job, next to `kernel-dtype-keys`. The checker runs in
2.9 s on an M4 against 0.3 s for the dtype checker; with their test
scripts the two targets take 4.5 s and 5.0 s.
check_used_by_test.sh builds a synthetic workspace and asserts the
verdict on 23 cases, including the pre-lablup#1121 roster verbatim.
@inureyes inureyes added type:chore Maintenance tasks (build, CI, etc.) priority:low Low priority status:done Completed labels Nov 22, 2026
@inureyes
inureyes merged commit 80467eb into lablup:main Nov 22, 2026
33 checks passed
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority status:done Completed type:chore Maintenance tasks (build, CI, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore: Enforce the // Used by: convention in CI, staleness included

2 participants