Skip to content

[RNE Rewrite] Extract shared host-object try-lock helper into core - #1331

Open
msluszniak wants to merge 1 commit into
rne-rewritefrom
@ms/tokenizer-lock-dedup
Open

[RNE Rewrite] Extract shared host-object try-lock helper into core#1331
msluszniak wants to merge 1 commit into
rne-rewritefrom
@ms/tokenizer-lock-dedup

Conversation

@msluszniak

Copy link
Copy Markdown
Member

Description

Resolves the locking + boilerplate items of #1316. Extracts the try-lock + liveness-check block that was duplicated across TokenizerHostObject (5 sites), ModelHostObject (3 sites) and the tensor helpers into a single templated core::tryLock helper (cpp/core/host_object_lock.h), parameterized over the lock kind (std::unique_lock / std::shared_lock) and a liveness predicate. Error messages are preserved verbatim.

On the mutex question from #1316: TokenizerHostObject intentionally keeps an exclusive std::mutex rather than moving to std::shared_mutex. Although every tokenizer op is const, const is not thread-safe here — HF tokenizers encode via the PCRE2 fallback, and the vendored Pcre2Regex shares a single pcre2_match_data member across const find_all() calls, so concurrent encodes on one tokenizer would race. This is documented in tokenizer.h with a TODO pointing at follow-up #1330 (switch to shared_mutex once the vendored tokenizers lib includes the upstream per-call fix).

Introduces a breaking change?

  • Yes
  • No

Type of change

  • Bug fix (change which fixes an issue)
  • New feature (change which adds functionality)
  • Documentation update (improves or adds clarity to existing documentation)
  • Other (chores, tests, code style improvements etc.)

Tested on

  • iOS
  • Android

Testing instructions

Pure refactor with no behavior change: the extracted helper reproduces the exact same lock discipline and error strings as the inlined code. Verified the changed translation units compile (clang++ -std=c++20 -fsyntax-only) and pass clang-format.

Screenshots

Related issues

Resolves the locking/deduplication parts of #1316. Follow-up: #1330.

Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional notes

host_object_lock.h is a header-only template, so no CMake change is needed (sources are globbed and it is included where used).

Deduplicate the try-lock + liveness-check boilerplate repeated across
TokenizerHostObject (5 sites), ModelHostObject (3 sites) and the tensor
helpers into a single templated core::tryLock, parameterized over the lock
kind (unique/shared) and a liveness predicate.

Also document why TokenizerHostObject keeps an exclusive std::mutex despite
its ops being const: the vendored Pcre2Regex shares match data across const
find_all() calls, so concurrent encodes would race (see #1330).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RNE Rewrite] Clean up the tokenizer host object: locking, error unwrapping and native default arg

1 participant