Skip to content

ICLabel's ONNX session: a thread count, or SessionOptions, for callers that pin their threads #763

Backlog

Description

@neuromechanist

At 871bc7f, iclabel runs the packaged network through onnxruntime (plugins/ICLabel/iclabel_net_onnx.py, from #386). _get_session builds one InferenceSession per process with no SessionOptions and caches it, so its intra-op and inter-op thread counts are both 0, which onnxruntime reads as its own default. A caller that pins every thread pool to one thread (threadpoolctl.threadpool_limits(1) and OMP_NUM_THREADS=1 set before import) cannot reach this pool: on a 10-core arm64 macOS machine the process goes from 1 thread to 7 when ICLabel runs under that pin, and the session still reports 0 and 0.

NEMAR's nemar-ica recipe runs a decomposition per recording with every pool pinned (nemarOrg/nemar-process#66, epic nemarOrg/nemar-cli#2324), because its runners share machines and its records state how many threads a run used. For now the recipe reads the session's options after the classification and records that ICLabel ran on onnxruntime's default pools, instead of claiming one thread. On the machine above, the network's output was bit-identical at 1, 2, 4 and 8 intra-op threads, so this is about resource use and an honest record, not about different probabilities; x86 was not checked.

Requests:

  1. Let a caller set the ICLabel session's thread count: a keyword on iclabel and pop_iclabel (for example num_threads, defaulting to today's behavior), an environment variable read when the session is built, or a public function that builds the session from caller-supplied SessionOptions. Since the session is cached, a different count needs a different cache entry, or a documented rule that the first call's options win.
  2. Expose the session's options, or the counts it was built with, through a public name, so a caller can record what ran without importing the private _get_session.

Once a commit includes either, nemar-process will re-pin eegprep, pass its pinned thread count to ICLabel, and drop its read of the private session.


Agentic AI tools were used. Workflows follow the Research Skills (doi:10.5281/zenodo.20696515). The author assumes full responsibility for this content and associated changes.

Activity

  1. added a commit that references this issue on Nov 27, 2026
  2. claude commented on Nov 27, 2026

    @claude
    Contributor

    🤖 Plan:

    • Fetch issue, research code, duplicate-work preflight (no open PR or issue overlaps)
    • Post Research + Proposed Fix
    • Human review of the API choices below before implementation. This is a new public option spanning iclabel_net_onnx, iclabel, pop_iclabel, history, help and docs (about 120-180 lines with tests and docs), so no PR was opened under the agent scope rules.

    Research

    _get_session() is a functools.cached zero-argument function. It builds InferenceSession(model_path, providers=['CPUExecutionProvider']) without SessionOptions, so intra_op_num_threads and inter_op_num_threads stay 0, which means onnxruntime picks its own defaults. onnxruntime builds its own thread pools and ignores threadpoolctl/OMP_NUM_THREADS, and no public parameter reaches the session. bids_preproc and the GUI menu action call iclabel/pop_iclabel with defaults, so leaving the default alone keeps their behavior unchanged.

    Relevant code

    Proposed Fix

    This covers both requests with an explicit keyword. AGENTS.md prefers explicit parameters over environment variables, so the env-var option is out.

    1. iclabel_net_onnx.py: rename _get_session() to public get_session(num_threads=None) and keep functools.cache, so each thread count gets its own cache entry and the "first call wins" problem goes away. None keeps today's session with no options. An integer n >= 1 sets SessionOptions.intra_op_num_threads = inter_op_num_threads = n. Any other value raises ValueError. Callers record what ran with get_session(n).get_session_options().intra_op_num_threads, which covers request 2 without a new accessor.
      @functools.cache
      def get_session(num_threads=None):
          ort = require_onnxruntime()
          options = ort.SessionOptions()
          if num_threads is not None:
              options.intra_op_num_threads = options.inter_op_num_threads = num_threads
          return ort.InferenceSession(model_path, sess_options=options, providers=['CPUExecutionProvider'])
    2. run_iclabel_net(..., num_threads=None) passes the count to get_session.
    3. iclabel(EEG, algorithm, engine, *, num_threads=None, feature=...) and pop_iclabel(..., *, num_threads=None) pass it through. A non-None value with engine='matlab'/'octave' raises ValueError, because the count would be silently ignored. The async and browser paths stay unchanged; ONNX Runtime Web manages its own threads.
    4. Tests in tests/test_iclabel.py (written first, no mocks): get_session(1).get_session_options() reports 1/1; get_session(1) is not get_session(); iclabel(EEG, num_threads=1) gives the same classifications as the default; invalid counts and engine plus num_threads raise.
    5. Docs: update resources/help/pop_iclabel.md, the ICLabel section of docs/source/user_guide/configuration.rst (how to pin threads and record the count), and changelog.rst.

    Open questions

    • Should pop_iclabel's history command include num_threads=n when it is set? It does not change the probabilities (the issue saw bit-identical output on arm64), so I would leave it out to keep the command identical to EEGLAB's pop_iclabel(EEG, 'default').
    • Should etc.ic_classification.ICLabel also record the thread count next to network? I would not: MATLAB EEGLAB has no such field, and request 2 is covered by get_session(n).get_session_options().
    • Should bids_preproc get a pass-through option too? I would leave it out of scope unless NEMAR needs it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions