uzu

repository·main·Indexed 23 days ago

https://github.com/trymirai/uzu

A high-performance inference engine for AI models designed for local deployment with zero latency, full data privacy, and no inference costs. It supports multiple languages including Rust, Python, Swift, and TypeScript. The project includes several supporting crates: kiban for cross-platform OS primitives (Native and WASM), keisoku and kanka for Apple platform telemetry and private framework bindings, and mock-registry for offline integration testing.

Tokens
26.8K
Snippets
44
Records
143
Agent score
82%

What's inside uzu

  1. What is the kanka crate?

    main

    The kanka crate is an internal low-level helper crate used to provide generic dlsym-resolved bindings to private Apple framework functions. It is primarily used by the keisoku crate to access IOReport and SMC power/telemetry APIs that lack public headers.

    Note: This crate is intended for internal use. Most developers should depend on keisoku instead of kanka directly.

  2. Overview of the kiban crate

    main

    The kiban crate provides cross-platform OS primitives designed to allow the same code to run on both native targets and in the browser via WebAssembly (WASM). It abstracts away the differences between native runtimes and browser environments by providing a unified async API.

    • On Native Targets: Uses tokio for async tasks and filesystem operations.
    • On WASM Targets: Uses browser primitives such as wasm-bindgen-futures for task management and the Origin Private File System (OPFS) for storage.

    Downstream crates should depend on kiban rather than tokio or web-sys directly to maintain cross-platform compatibility.

  3. Use mock-registry for offline integration tests

    main

    The mock-registry crate provides a local model registry designed for integration-style tests in download-manager and uzu. It allows tests to run without internet access by simulating a remote registry endpoint and serving deterministic model files via local HTTP URLs.

    Key capabilities include:

    • Returning realistic shoji::Model data via a POST /fetch/models endpoint.
    • Serving model files through a lightweight TCP file server.
    • Simulating various network conditions using Behavior bitflags (e.g., corrupted bodies or throttled streaming).
    • Providing deterministic, in-memory files with real sizes and CRC32C hashes without requiring external fixture files.
  4. How Model State Reduction works

    main

    The system can aggregate the states of multiple individual file download tasks into a single ModelDownloadState. This is useful for representing the progress of a large model composed of many files.

    The reducer follows a strict priority order to determine the model-level state:

    1. Downloaded: All files are Downloaded.
    2. Downloading: Any file is currently Downloading.
    3. Paused: Any file is Paused.
    4. Error: Any file has an Error.
    5. Paused (Partial Progress): Some files are Downloaded AND total downloaded bytes > 0, but no files are actively downloading or paused. This represents a state where some components (e.g., config.json) are ready, but others (e.g., model.safetensors) are not.
    6. NotDownloaded: Default state if none of the above apply.
  5. Use kiban modules for cross-platform development

    main

    The kiban crate is organized into several modules that provide platform-agnostic functionality:

    • fs: Async filesystem operations. On native, this uses tokio::fs; on WASM, it uses OPFS. Includes PartFile for resumable, append-oriented writes (useful for partial downloads).
    • rt: Task runtime management.
    • time: Time-related primitives.
    • process: Process helpers.
    • maybe: Provides MaybeSend and MaybeSync marker traits. These traits require Send/Sync on native targets but impose no bounds on WASM (since WASM futures are single-threaded).

    Additionally, kiban exports the printf! and eprintf! macros. These route to println!/eprintln! on native platforms and to the browser console on WASM.

  6. Platform support for keisoku

    main

    The keisoku crate is designed exclusively for Apple platforms.

    • macOS: Supports both Device (instantaneous gauges) and interval_measurement (via IOReport counters).
    • iOS: Supports the Device instantaneous subset; interval_measurement (IOReport) is not available.
  7. Manage Multi-File Model Download States

    main

    When downloading a model composed of multiple files (e.g., config.json, tokenizer.json, model.safetensors), the Model Reducer aggregates individual file states into a single model-level state using a priority-based matching system.

    Example Logic: If a model has some files downloaded and others not downloaded, the reducer evaluates properties like all_downloaded, any_downloading, any_paused, and any_downloaded.

    Example Scenario:

    • config.json: Downloaded
    • tokenizer.json: Downloaded
    • model.safetensors: NotDownloaded

    Resulting Model State:

    • all_downloaded: false
    • any_downloading: false
    • any_paused: false
    • any_downloaded: true
    • downloaded_bytes: (Sum of downloaded files)
    • Priority Match: Paused (based on Some downloaded + progress > 0)
  8. Understand the File Download Task State Machine

    main

    The download-manager uses a three-stage pipeline to determine the state of a file download. This ensures data integrity, correct UI representation, and proper operational cleanup.

    1. Validation (reduce_to_checked_file_state)

    Determines if a file on disk is Valid, Invalid, or Missing using CRC (Cyclic Redundancy Check).

    • Optimization: Valid files receive a .crc cache file. This makes subsequent launches ~100x faster for large files by avoiding recalculation. The cache is invalidated if the file changes.

    2. Display State (reduce_to_file_download_state)

    Maps validation results to user-facing states for the UI:

    • Downloaded: Any file that is Valid is immediately shown as Downloaded.
    • Downloading/Paused/NotDownloaded: Derived from a combination of the checked_state, resume_state, task_state, and expected_bytes.

    3. Reconciliation (reconcile_to_internal_state)

    Performs side effects like file deletion or task cancellation to determine the InternalDownloadState for operations.

    • Valid Files: Always result in Downloaded state; unnecessary artifacts (like resume files) are cleaned up.
    • Invalid/Missing Files: May trigger file deletion, resume data production, or task cancellation based on whether the file is partial or corrupted.
  9. Understand kiban platform-specific filesystem behavior

    main

    When using the kiban::fs module, be aware of the following platform differences:

    • Native: Uses full tokio-backed implementations.
    • WASM: Uses the Origin Private File System (OPFS) for storage. Because OPFS does not support hard links, calling fs::hard_link on a WASM target will fall back to performing a copy operation instead.
  10. Simulate network behaviors with MockRegistry Behavior flags

    main

    When using mock-registry for testing, you can control the server's response characteristics using the Behavior bitflag. This is useful for testing how your application handles edge cases like network instability or data corruption.

    Supported scenarios include:

    • Corrupted bodies
    • Throttled streaming
  11. Use kanka macros for private Apple framework bindings

    main

    The kanka crate provides two primary macros for interacting with obfuscated or private Apple frameworks:

    1. ffi_table!: Declares a struct containing dlsym-resolved C function pointers from a specific framework. It includes a get() method that uses a OnceLock for caching. Symbol and framework names are obfuscated at compile time using obfstr to prevent easy discovery.
    2. opaque_cf_type!: Declares an opaque CoreFoundation handle type that is compatible with CFRetained.

    Platform Support: This crate is exclusive to Apple platforms (target_vendor = "apple"). On other platforms, the macros expand to nothing, making the crate effectively empty.

  12. Set up the environment for the download-manager WASM example

    main

    To run the browser-compatible WASM example for the download-manager crate, you must install the wasm32-unknown-unknown target for the nightly Rust toolchain and use Trunk as the web application bundler.

    Note: The crate uses the Origin Private File System (OPFS) for storage. For debugging OPFS storage in Chrome, you may find the OPFS Explorer extension useful.

    rustup target add wasm32-unknown-unknown --toolchain nightly