Skip to content

Add allocator-bounded BPE tokenization into caller storage - #1148

Draft
Baiju Meswani (baijumeswani) wants to merge 2 commits into
mainfrom
baijumeswani/bounded-tokenizer
Draft

Baiju Meswani (baijumeswani) wants to merge 2 commits into
mainfrom
baijumeswani/bounded-tokenizer

Conversation

@baijumeswani

Copy link
Copy Markdown
Contributor

Summary

Add opt-in byte-level BPE tokenization into caller-owned output with an enforced per-call scratch allowance. This is a prerequisite for reserving media-preparation resources before tokenization in ONNX Runtime GenAI; it is independently usable and does not add video processing or change existing tokenizer APIs.

  • OrtxPrepareBoundedTokenization builds immutable tokenizer-owned lookup views during initialization.
  • OrtxTokenizeIntoBuffer propagates a per-call PMR allocator through the shared BPE implementation, including Unicode conversion, splitting, byte buffers, token output and merge-node storage.
  • Quota/capacity failures leave output unchanged and set the returned count to zero. A later adequately budgeted call can reuse the tokenizer.
  • Unsupported tokenizer backends/transforms and allocating STL-regex fallback reject explicitly. Legacy tokenization remains available.
  • Move tokenization results into their C API owner instead of deep-copying them.

The quota bounds requested live input-dependent scratch, not RSS. Caller buffers, immutable tokenizer-owned lookup data, allocator overhead and fixed error/status bookkeeping are excluded. There is no global allocator override or shared mutable per-call quota.

Validation

The exact committed slice was built separately from all video edits in a clean Linux C++17 Release worktree. All 163 tokenizer regression cases pass (two existing profiling cases are disabled), and both separately linked shared-library API cases pass. Coverage includes quota exhaustion/recovery, atomic output, concurrent independent quotas and unsupported backends.

The integrated GenAI candidate also passed 36 complete Qwen/Ling token-ID comparisons against the legacy path, 108 Qwen image/video cases, 39 Flash cases and the reduced-Ling lifecycle checks. Those are integration receipts, not proof of total Engine memory accounting or complete three-model qualification.

Draft status

Windows shared-library ABI execution remains to be run on a Windows machine. This draft does not claim complete Engine media-memory accounting, encoder numerical qualification, or full Ling model enablement.

Transfer the result buffers rather than deep-copying the rvalue argument. This removes two allocations for a single encoded row without changing its token IDs or lifetime.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a32500b6-be93-4206-a257-11406b00a3f3
Prepare immutable lookup views at model initialization, propagate per-call scratch allocators through the shared BPE algorithm, and reject unsupported fallback paths before encoding. Preserve atomic output and cover quota recovery, independent concurrent calls and shared-library access.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a32500b6-be93-4206-a257-11406b00a3f3
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.

1 participant