coremltools

repository·main·Indexed 26 days ago

https://github.com/apple/coremltools

A Python package that enables developers to convert machine learning models from popular frameworks like TensorFlow and PyTorch into the Core ML format for deployment on Apple platforms. It includes a Generic Pattern Matching Infrastructure for implementing custom MIL (Model Intermediate Language) pattern passes to detect and transform sequences of operations within a model.

Tokens
205.6K
Snippets
569
Records
999
Agent score
85%

What's inside coremltools

  1. Overview of coremltools capabilities

    main

    coremltools is a Python package used to convert machine learning models from third-party libraries into the Core ML format.

    Supported Frameworks:

    • TensorFlow 1.x
    • TensorFlow 2.x
    • PyTorch
    • scikit-learn
    • XGBoost
    • LibSVM

    Key Functionalities:

    • Convert trained models to the Core ML format.
    • Read, write, and optimize Core ML models.
    • Verify conversion/creation (on macOS) by making predictions using Core ML.
  2. Overview of pybind11 features

    main

    pybind11 is a lightweight, header-only C++11 library designed to expose C++ types in Python and vice versa. It minimizes boilerplate by using compile-time introspection to infer type information.

    Core Mapping Capabilities:

    • Functions (value, reference, or pointer) and methods (instance and static).
    • Overloaded functions and attributes.
    • Exception types, enumerations, and callbacks.
    • Iterators, ranges, and custom operators.
    • Inheritance (single and multiple).
    • STL data structures and smart pointers (e.g., std::shared_ptr).
    • C++ classes with virtual/pure virtual methods.
    • Integrated NumPy support (Note: NumPy 2 requires pybind11 2.12+).

    Key Advantages:

    • Header-only: No need to link against additional libraries.
    • Efficiency: Uses C++11 move semantics and constexpr for precomputed function signatures, resulting in smaller binaries and faster execution.
    • NumPy Integration: Supports Python's buffer protocols for fast conversion (e.g., Eigen to NumPy) and automatic vectorization of functions.
  3. Overview of Core ML Tools

    main

    The coremltools Python package is used to convert third-party machine learning models into the Core ML model package format. This allows you to integrate models into Apple apps using the Core ML framework, which leverages the CPU, GPU, and Neural Engine (NE) for optimized on-device inference.

    Key capabilities include:

    • Converting trained models from frameworks like TensorFlow and PyTorch.
    • Reading, writing, and optimizing Core ML models to reduce storage, power consumption, and latency.
    • Verifying model conversion by performing predictions on macOS.
  4. Overview of Model Intermediate Language (MIL)

    main

    Model Intermediate Language (MIL) is an intermediate representation used by coremltools during the conversion process. When converting models from frameworks like TensorFlow or PyTorch, coremltools first builds a MIL program and then translates that representation into the Core ML protobuf representation.

    When to use MIL directly:

    • Handling unsupported operators: If you encounter an unsupported operator error during conversion, you can write a composite operator using the MIL builder.
    • Defining models from scratch: You can use MIL to define a model manually instead of starting from a source framework (e.g., for testing single operations).
  5. Overview of MIL Graph Passes

    main

    In Core ML Tools, the conversion process involves several stages. After the Frontend stage (e.g., PyTorch/TensorFlow to Model Intermediate Language), MIL-based Graph Optimizations are applied.

    These optimizations are implemented as passes that operate on a Program (a Pythonic representation of the model). A Program consists of a main function implemented as a Block, which contains a list of Operators. Passes are applied to the Program to simplify and canonicalize it.

    During conversion, passes are managed via a pass_pipeline (passed to ct.convert). You can use default pipelines, predefined specialized pipelines, or define your own custom optimization logic.

  6. Compare ML Programs and Neural Networks

    main

    Core ML supports two primary model representations: neuralnetwork and mlprogram. While neural networks use a computational graph of layers with embedded weights, ML programs represent models as programmatic operations with decoupled weights.

    Key differences:

    • Representation: Neural networks use layers in a computational graph; ML programs use operations (ops) in a programmatic representation.
    • Weights: Neural networks embed weights in layers; ML programs decouple and serialize weights separately.
    • Precision: Neural networks have implicit intermediate tensor types with limited precision control; ML programs use explicit, typed intermediate tensors for granular precision control.
    • Execution: ML programs support a GPU runtime (via Metal Performance Shaders Graph) that allows both float 16 and float 32 precision, whereas neural network GPU execution is typically limited to float 16.
  7. Understand the Core ML Model Format Specification

    main

    The Core ML model format is defined using Protocol Buffers (protobuf). The root of the model structure is the Model message, which is defined in Model.proto. The specification is composed of various protobuf message definitions that describe:

    • Data Structures and Types: Core data representations used within the model.
    • Feature Engineering: Definitions for feature types and engineering model types.
    • Predictive Model Types: Specific implementations including:
      • Neural Networks (NeuralNetwork.rst)
      • Classifiers (Classifiers.rst)
      • Regressors (Regressors.rst)
      • SVM (SVM.rst)
      • Tree Ensembles (TreeEnsemble.rst)
      • MIL (Model Intermediate Language) (MIL.rst)
      • Other Models (OtherModels.rst)
    • Identity: Definitions related to model identity.
  8. Use the ItemSimilarityRecommender model format

    main

    The ItemSimilarityRecommender is a model format that predicts similarity scores for items based on a list of input items and their scores, using a pre-computed table of item similarities.

    By default, the model predicts items that are most similar to the input set but are not part of the input set itself. The predicted score for an item $k$ is calculated as:

    $$\sum_{i \in \text{observed items}} \text{sim}(k,i) \times (\text{score}_i - \text{shift}_k)$$

    Where:

    • sim(k,i) is the similarity between item $k$ and observed item $i$.
    • shift_k is the itemScoreAdjustment used to counteract global biases for popular items (often set to zero).

    Note that sim(k,i) is often zero because only the most similar scores for each item are stored.

  9. Understand the Core ML Model structure

    main

    A Core ML model is composed of a specificationVersion, a ModelDescription, and a specific model Type. The specificationVersion determines compatibility with different OS versions (e.g., Version 1 for iOS 11, Version 6 for iOS 15).

    message Model {
        int32 specificationVersion = 1;
        ModelDescription description = 2;
        bool isUpdatable = 10;
        
        oneof Type {
            // Various model types (Pipeline, Regressors, Classifiers, etc.)
            PipelineClassifier pipelineClassifier = 200;
            NeuralNetwork neuralNetwork = 500;
            MILSpec.Program mlProgram = 502;
            // ...
        }
    }
  10. Understand precision differences between Neural Networks and ML Programs

    main

    Core ML models execute in either float 16 or float 32 precision. The way this is handled depends on the model type:

    • Neural Networks: Intermediate tensors are untyped. The runtime dynamically assigns precision based on the hardware: GPU and Neural Engine (NE) typically use float 16, while the CPU uses float 32. Precision is controlled by configuring allowed compute units (e.g., All, CPU&GPU, or CPUOnly).
    • ML Programs: Intermediate tensors are strongly typed within the model. The runtime respects these types as the minimum precision and will not reduce them. A float 32 typed ML program is guaranteed to run in float 32 precision across all hardware/software, including GPU and CPU.