Skip to content

Feat/coreml#308

Open
Skyline-23 wants to merge 40 commits intoazooKey:mainfrom
Skyline-23:feat/coreml
Open

Feat/coreml#308
Skyline-23 wants to merge 40 commits intoazooKey:mainfrom
Skyline-23:feat/coreml

Conversation

@Skyline-23
Copy link
Copy Markdown

PR Summary (feat/coreml)

Overview

This branch isolates CoreML integration (no actor refactor) and makes it buildable/usable via the CLI with a CoreML-only flag. Sync APIs remain primary; async wrappers are thin bridges.

Key changes

  • Sendable fixes for CoreML path
    • Zenz and ZenzCoreMLService marked @unchecked Sendable to unblock async bridging.
    • blockingAsync now accepts @Sendable closures via Task.detached; CoreML personalization handle is mapped explicitly to a tuple.
  • macOS version handling for CoreML
    • When ZenzaiCoreML trait is used, macOS linker gets a -platform_version macos 15.0 15.0 hint to silence xcframework version warnings while keeping the default macOS 13 baseline for non-CoreML builds.
  • CLI installer improvements
    • Supports --zenzai-coreml (plus optional --debug).
    • Copies the required resource bundle AzooKeyKanaKanjiConverter_KanaKanjiConverterModuleWithDefaultDictionary.bundle into /usr/local/bin/.
    • For CoreML builds, skips copying llama.framework; for Zenzai/ZenzaiCPU builds, still expects it.
      Add Support Page for English/Korean

How to build and install (CoreML)

sudo rm -rf .build   # clear stale caches if needed
swift build --traits ZenzaiCoreML
./install_cli.sh --zenzai-coreml --debug   # drop --debug for release

Known warnings / notes

  • updateIfRequired(options:) in KanaKanjiConverter is deprecated but behavior is unchanged; can be refactored later.
  • Minor warnings remain (unused intention in InputTable.swift, redundant try in ZenzContext+CoreML.swift) with no behavioral impact.
  • CoreML xcframeworks are built for macOS 15.5; the trait-specific linker hint mitigates version mismatch noise.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +120 to +123
#else
package func getModel(modelURL: URL) -> Zenz? {
self.zenzStatus = "zenz-v2 model unavailable on this platform"
return nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Non-CoreML builds never load zenz models

When ZenzaiCoreML is not set, getModel now always marks the model as unavailable and returns nil, but the rest of the pipeline still checks zenzaiMode.enabled. On Zenzai/ZenzaiCPU builds this means the llama backend is never loaded and convertToLattice falls through to the non-zenz path, so enabling zenzaiMode no longer produces any zenz candidates or next-character predictions even though the trait and weight URL are provided—a regression from the previous behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

@Skyline-23 Skyline-23 Nov 25, 2025

Choose a reason for hiding this comment

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

It might be resolved. in 5a07f7d

@Skyline-23 Skyline-23 force-pushed the feat/coreml branch 3 times, most recently from aaf2dd8 to 32560bc Compare March 8, 2026 15:44
Adds support for CoreML-based Zenzai conversion, leveraging the `ZenzCoreMLBackend` for improved performance and efficiency on compatible devices.

This includes:
- Integration of the CoreML model for stateful generation.
- A new `ZenzContext+CoreML.swift` to manage the CoreML inference process.
- Updates to `Package.swift` to include `swift-transformers` dependency.
- Conditional compilation to switch between CoreML and CPU/Metal implementations based on the `ZenzaiCoreML` flag.
- Adds tokenizer files and marks mlpackage for LFS.

The changes aim to provide a faster and more resource-efficient Zenzai conversion method on Apple platforms with Neural Engine support.
Cause of swift-transformers package uses combine, and it not compatible with windows
Enables building the application with CoreML support for enhanced performance on Apple devices.

This allows users to leverage CoreML acceleration by using the `--zenzai-coreml` flag during installation.
Removes the explicit LFS bootstrap step from CI workflows as it is no longer required due to changes in how dependencies are managed. This simplifies the CI configuration and reduces build times.

Adds documentation for development setup in multiple languages, covering build instructions, testing and devcontainer usage. This enhances the developer experience by providing clear and comprehensive instructions in English, Japanese and Korean.

Improves Zenzai integration, adding CoreML support and cross-platform compatibility with Swift 6 concurrency. This allows for high-precision neural Kana-Kanji conversion on Apple devices using CoreML, while ensuring compatibility and performance on both Darwin and Linux platforms.
- replace the blockingAsync result capture with a lock-protected sendable box so Task.detached no longer trips Swift 6.3 data-race checks
- align the CoreML macOS linker platform_version hint with the 15.5 XCFramework minimum to remove stale version mismatch warnings
- keep the existing CoreML sync bridge behavior intact while restoring a successful ZenzaiCoreML build
- reintroduce the CoreML-facing request option shims and converter entry points that upstream main now expects from AncoSession and prediction flows
- reconnect the Zenz prompt/context helpers after rebasing so llama-only helpers stay out of CoreML builds while shared APIs remain available
- remove the duplicate fixed-size heap implementation and expose tokenizer APIs needed by the CoreML context so both default and ZenzaiCoreML builds succeed
- remove the zenz-CoreML Swift package dependency and load the stateful 8-bit CoreML assets directly from the Skyline23/zenz-coreml Hugging Face repo
- download tokenizer and mlpackage assets into a local cache, compile the mlpackage to mlmodelc, and reuse the compiled model on subsequent launches
- stop install_cli.sh from expecting a copied CoreML framework and document that CoreML assets are fetched from Hugging Face at runtime
- switch the default HF CoreML artifact selection to the working stateful FP16 package while keeping 8-bit available via environment override
- make the loader follow the current Hugging Face main artifact layout and materialize mlpackage resources into a local cache before compiling them to mlmodelc
- align the CoreML stateful decode path with the current app reference so fresh-cache zenz_evaluate produces stable text instead of collapsing into repeated replacement tokens
- move CoreML and llama-specific model lifecycle helpers out of KanaKanjiConverter.swift so the main converter flow reads as backend-agnostic logic
- switch ZenzCoreMLService from NSLock-based state management to an actor to make the CoreML path easier to reason about during review
- keep behavior unchanged while making the backend split more explicit for follow-up cleanup around shared Zenz APIs
- reintroduce the upstream session-scoped predictive input and stable prediction candidate caches inside KanaKanjiConverter so prediction view behavior matches main again
- wire the existing CoreML backend path into the restored session state flow instead of bypassing the cache-aware converter logic
- fix the failing AncoSession and Scenario prediction stability tests that were breaking the latest macOS and ubuntu CI runs
- pin swift-transformers to the revision with the Android localized string fallback\n- make Android CI resolve a deterministic compatibility commit instead of a moving branch head\n- preserve the existing CoreML and llama.cpp package structure
- pin swift-transformers to the latest main revision e5e227b\n- keep the repo on the upstream Android-compatible Hub/Tokenizer stack\n- retain the existing CoreML and llama.cpp integration in this branch
- remove CoreML-only stub returns from the Zenz wrapper type\n- gate unsupported predictive input and typo generation at the converter call sites\n- keep backend capability differences local to the integration layer for easier review
- revert the latest-main pin that regressed non-mac CI\n- keep the known-good Android-compatible revision for this PR\n- defer the upstream swift-transformers main upgrade until its cross-platform issues are resolved
@Skyline-23
Copy link
Copy Markdown
Author

Follow-up notes after the latest cleanup pass:

Future cleanup items that still fit this PR's scope:

  • Remove the remaining sync-to-async bridge in KanaKanjiConverter+ZenzBackend.swift and replace the NSLock-backed blockingAsync helper with a structured actor-based handoff.
  • Move more Zenz-specific session/cache state out of KanaKanjiConverter.swift into a smaller backend/session container so the converter body stays focused on the shared flow.
  • Keep backend capability differences at the integration boundary only. I already moved the CoreML unsupported predictive-input / typo-generation behavior out of Zenz.swift, but the same rule should continue to guide any follow-up cleanup.

I also tested upgrading swift-transformers to the latest main revision, but I intentionally did not keep that change in this PR because it regressed non-mac CI.

Exact failure reasons from the latest-main attempt (ebc18d3):

  • Swift Build and Test in DevContainer and Swift 6.1 on ubuntu-24.04 failed in the new dependency graph around swift-numerics / _NumericsShims module resolution while compiling transitive packages such as EventSource / Algorithms.
  • Android (x86_64, aarch64, armv7) failed in swift-xet because Xet.swift referenced NIOTSEventLoopGroup, which is not available there.
  • Windows failed in async-http-client because CAsyncHTTPClient.c used strptime, strptime_l, and locale_t, which do not exist on that toolchain.

Because of that, the branch stays pinned to the known-good compatibility revision for swift-transformers so the CoreML PR itself remains reviewable and green.

@Skyline-23
Copy link
Copy Markdown
Author

@ensan-hcl Could you please re-review my CoreML branch?

I removed the actor-based refactoring and the README changes from this PR to keep the scope focused.

I will submit those improvements in a separate follow-up PR.

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