E533 perf(face-swapper): optimize CoreML session configuration by laurigates · Pull Request #1656 · hacksider/Deep-Live-Cam · GitHub
[go: up one dir, main page]

Skip to content

perf(face-swapper): optimize CoreML session configuration#1656

Open
laurigates wants to merge 1 commit intohacksider:mainfrom
laurigates:pr/perf-coreml-session
Open

perf(face-swapper): optimize CoreML session configuration#1656
laurigates wants to merge 1 commit intohacksider:mainfrom
laurigates:pr/perf-coreml-session

Conversation

@laurigates
Copy link
Contributor
@laurigates laurigates commented Feb 22, 2026

Summary

The CoreML ExecutionProvider configuration for the face swapper model can be improved for Apple Silicon:

  1. RequireStaticShapes: 1 — The inswapper model has fixed-size inputs (1x3x128x128 face crop + 1x512 embedding). Enabling static shapes allows the CoreML compiler to optimize the graph at compile time rather than handling dynamic shapes at runtime.

  2. ModelCacheDirectory — Persists the compiled CoreML model to ~/.cache/deep-live-cam/coreml/. Without this, CoreML recompiles the ONNX → MLProgram conversion on every application startup, which takes 30-60 seconds on first run. With caching, subsequent startups load the pre-compiled model in ~1 second.

  3. Remove MaximumCacheSize — This is not a valid CoreML EP session option and was silently ignored.

Changes

  • modules/processors/frame/face_swapper.py: Update CoreML provider config in get_face_swapper()

Testing

  • macOS Apple Silicon: First run compiles model (30-60s), subsequent runs load from cache (~1s)
  • Face swap quality unchanged (same model, same inference)
  • Non-CoreML providers unaffected (config is gated on CoreMLExecutionProvider)
  • Tested on M4 Pro with onnxruntime-silicon 1.20

Generated with Claude Code

Summary by Sourcery

Optimize CoreML execution provider configuration for the face swapper model on Apple Silicon to improve startup performance and compilation behavior.

New Features:

  • Add persistent CoreML model cache directory for the face swapper on Apple Silicon.

Enhancements:

  • Enable static shape requirement in the CoreML provider config for the fixed-shape face swapper model.
  • Remove unsupported CoreML session option from the provider configuration.

- Enable RequireStaticShapes (inswapper inputs are fixed-size
  1x3x128x128 face + 1x512 embedding), allowing compile-time
  optimization of the CoreML graph
- Add ModelCacheDirectory to persist compiled CoreML models to
  ~/.cache/deep-live-cam/coreml/, avoiding 30-60s recompilation
  on every application restart
- Remove invalid MaximumCacheSize option (not a CoreML EP parameter)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link
Contributor
sourcery-ai bot commented Feb 22, 2026
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Optimizes the CoreMLExecutionProvider configuration for the face swapper on Apple Silicon by enabling static shapes, adding a persistent CoreML model cache directory, and removing an unsupported session option, while keeping non-CoreML providers unchanged.

Sequence diagram for CoreML face swapper initialization with model cache

sequenceDiagram
    participant App
    participant FaceSwapper as get_face_swapper
    participant ORT as OrtInferenceSession
    participant CoreMLExecutionProvider
    participant FileSystem as CoreMLModelCacheDirectory

    App->>FaceSwapper: get_face_swapper()
    FaceSwapper->>FileSystem: os.makedirs(coreml_cache_dir, exist_ok=True)
    FaceSwapper->>ORT: Create session with CoreMLExecutionProvider
    activate ORT
    ORT->>CoreMLExecutionProvider: Initialize with RequireStaticShapes=1
    CoreMLExecutionProvider->>FileSystem: Check for compiled model in cache
    alt First startup (no cached model)
        CoreMLExecutionProvider->>CoreMLExecutionProvider: Compile ONNX to MLProgram
        CoreMLExecutionProvider->>FileSystem: Persist compiled model to cache
    else Subsequent startups (cached model exists)
        CoreMLExecutionProvider->>FileSystem: Load compiled model from cache
    end
    ORT-->>FaceSwapper: Initialized inference session
    deactivate ORT
    FaceSwapper-->>App: Face swapper instance ready
Loading

File-Level Changes

Change Details Files
Optimize CoreMLExecutionProvider configuration for Apple Silicon face swapper inference.
  • Create a CoreML model cache directory under the user home (~/.cache/deep-live-cam/coreml) and ensure it exists at startup
  • Update the CoreMLExecutionProvider options to set RequireStaticShapes to 1 to leverage fixed-size inswapper inputs
  • Add ModelCacheDirectory to the CoreMLExecutionProvider configuration so compiled models are persisted between runs
  • Remove the unsupported MaximumCacheSize option from the CoreMLExecutionProvider configuration
  • Keep provider configuration changes gated behind CoreMLExecutionProvider and IS_APPLE_SILICON so other providers remain unaffected
modules/processors/frame/face_swapper.py

Possibly linked issues

  • #[Performance] Low FPS (1.5) in Live Camera Mode on macOS: PR optimizes CoreML face swapper configuration, likely improving macOS Apple Silicon face swapping FPS described in issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor
@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider moving the cache directory creation (coreml_cache_dir calculation and os.makedirs) outside the providers loop so it only runs once per get_face_swapper() call instead of once per provider.
  • Using ~/.cache/... directly may not be ideal on macOS; you might want to respect OS-specific cache locations (e.g. ~/Library/Caches or XDG_CACHE_HOME) to better align with user expectations and system conventions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider moving the cache directory creation (`coreml_cache_dir` calculation and `os.makedirs`) outside the providers loop so it only runs once per `get_face_swapper()` call instead of once per provider.
- Using `~/.cache/...` directly may not be ideal on macOS; you might want to respect OS-specific cache locations (e.g. `~/Library/Caches` or `XDG_CACHE_HOME`) to better align with user expectations and system conventions.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

0