Skip to content

Metal and CoreML Backends #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 427 commits into
base: master
Choose a base branch
from

Conversation

ChinChangYang
Copy link
Contributor

@ChinChangYang ChinChangYang commented Dec 16, 2023

Summary:

KataGo harnesses the advanced capabilities of Apple Silicon through the integration of the Metal Performance Shaders Graph and CoreML. This integration empowers KataGo with GPU acceleration and compatibility with the Neural Engine, ensuring exceptional performance levels.

Documentation for Metal and CoreML Backends in KataGo:

https://github.com/ChinChangYang/KataGo/blob/metal-coreml-stable/docs/CoreML_Backend.md

Release:

https://github.com/ChinChangYang/KataGo/releases

Resolve:

This commit adds the ability to draw black and white stones on the GobanView. The `drawBlackStone` and `drawWhiteStone` functions are implemented to draw the stones at specific coordinates. The `drawStones` function is added to the `GobanView` and calls the stone-drawing functions to draw several stones on the board.
1. Rename `StarPoint` struct to `BoardPoint` for clearer semantics.
2. Modify `drawStones` method to use ForEach for better maintainability.
3. Revise stone rendering with gradient and shadow optimizations.
- `CommandView` now uses a `messagesObject` environment object instead of a local state variable for managing messages.
- The `CommandView` no longer starts a thread in the `init()` method.
- The `CommandView` now retrieves messages from `messagesObject` and appends new messages to it.
- The `createMessageTask()` method has been moved to the `ContentView` and is now responsible for appending new messages to `messagesObject`.
- The `ContentView` now initializes and uses `stones` and `messagesObject` as environment objects.
- The `createMessageTask()` method in `ContentView` now retrieves messages from KataGo and appends them to `messagesObject`.

This commit introduces changes to improve the message management in the CommandView and ContentView structures.
…nd GobanView

The commit adds the stones and board objects as environment objects for the CommandView and GobanView structs in ContentView.swift. The stones object is added to the environment for CommandView, and the stones and board objects are added to the environment for GobanView. These environment objects allow these structs to access and update the state of the stones and board objects.
…banView to allow tapping on the board to make a move.
- Adjust the calculation of squareWidth and squareHeight to include an additional space for the board width and height respectively.
- Update the frame width and height of the Image in the GobanView.
Extract StoneView.swift from GobanView.swift to improve readability and maintainability.
The light effect in the StoneView component has been updated to include an additional color stop to create a more prominent effect. The start and end radii of the RadialGradient have also been adjusted for better visual appearance.

Also, the radius of the blur applied to the stone color circle has been reduced to improve the overall appearance of the StoneView component.

Additionally, the dimensions object has been assigned to a separate variable for better readability and code organization.
This commit adds the AnalysisView.swift file, which contains the code for visualizing the analysis data. The AnalysisView struct displays circles on the screen based on the analysis data. The size, position, color, and visibility of each circle are determined by the data. The AnalysisView_Previews struct is also defined to provide a preview of the view.
- The command view in this commit has been refactored to add a new state property called `isHidden`, which determines whether to hide the view or not.
- With the toggling functionality implemented, the code now checks the value of `isHidden` to determine whether to show the scroll view and the text field.

Note: The isHidden property is set to false on appear and true on disappear.
- Change maxTime value from 1 to 0.1 in default_gtp.cfg
The computeColorByVisits function is refactored to separate color computation from opacity calculation. This makes the code more readable and reusable.
This commit adds the BoardLineView.swift file which contains the implementation for drawing the board lines and star points on the game board. The BoardLineView struct takes in the dimensions of the board, the board width and height, and uses SwiftUI to create and render the board lines and star points.

The drawBoardBackground function draws the background texture of the board using the WoodImage.createTexture() method. The drawLines function uses the horizontalLine and verticalLine functions to draw the horizontal and vertical lines of the board, respectively.

The drawStarPoint, drawStarPointsForSize, and drawStarPoints functions are responsible for drawing the star points on the board. The drawStarPoints function checks the dimensions of the board and draws the appropriate star points for a 19x19, 13x13, or 9x9 board.

The BoardLineView_Previews struct is a preview provider for displaying the BoardLineView in a SwiftUI preview. It sets the dimensions of the board and creates an instance of the BoardLineView for previewing.
The BoardLineView.swift file has been added to the project and the GobanView has been updated to include the BoardLineView in the ZStack. This change reduces code complexity of GobanView.
Moved the tap gesture handling logic to a separate function to handle the conversion of the tapped location to a valid move. The function maps the x and y coordinates to the corresponding letters and numbers on the board. This logic is now more modular and reusable.
…swift

This commit adjusts the tab padding in ContentView.swift and reduces the displayed analysis data by limiting it to a maximum of 32 lines.
This commit adds more commands to the `ButtonView` view. The commands are passed as an array and are used to create `CommandButton` instances. Each button executes its corresponding command when tapped. The `ButtonView` is now more flexible and can display a variable number of buttons based on the commands provided.

In addition, the commit also updates the `CommandView` to pass a new set of commands to the `ButtonView`.

The `GobanView` has been updated as well to include a new set of commands in the `ButtonView`.

The changes aim to enhance the functionality and usability of the UI by allowing users to easily execute a wider range of commands.
- Resolved the next player issue related to square color rendering for ownerships.
- Enhanced visualization for ownership standard deviations.
- Set GTP message length cap at 200 characters to optimize command view performance.
- Pause KataGo analysis during command view display to prevent excessive text field refreshes.
- Addressed a race condition: incorporated next players in both playing and board display modes to handle rapid goban clicks.
- Optimized the efficiency of gathering KataGo analysis data.
- Adjusted KataGo analysis interval to 200ms for better compatibility with slower devices.
- Introduced a toggle for the analysis view.
- Added a control bar to the goban view for easier board actions: undo, pass, analyze, stop, and clear.
…nalysis moves

This commit introduces ConfigView.swift, which provides a view for configuring the maximum number of message characters and analysis moves. It includes two text fields to input the desired values and updates the configuration accordingly when the view disappears.
- Decreased analysisPVLen to 1 for limiting length of analysis output.
- Increased analysisWideRootNoise to 0.2 for exploring a wider variety of moves during analysis.
The code changes refactor the message struct to include a maximum length parameter and update the Message initializer to truncate the text if it exceeds the maximum length. This ensures that messages are truncated to the specified maximum length before being added to the messages list.
- Set model version to "s7436087296-d3643132126"
- Update model name to include the model version
- Set the directory for KataGo models
- Update path component to include the directory and compiled model name
- Create the directory for KataGo models if it doesn't exist
- Replace the model at the permanent location with the compiled model
…imensions, and messages objects

This commit adds the KataGoModel.swift file, which contains the implementation of various observable objects used in the KataGo iOS application. These objects include the ObservableBoard, Stones, PlayerObject, Analysis, Config, Dimensions, Message, and MessagesObject. The file also defines the PlayerColor and Ownership structs.
- Updated GobanView to use ObservableBoard and ObservableObject for board dimensions
- Updated AnalysisView to use ObservableBoard for board dimensions and utilize nextColorForAnalysis
- Updated button actions in GobanView to use nextColorForPlayCommand
Updated `convert_coreml_pytorch.py` to add a sparsity description for pruned models and modified the compression description for better clarity. Now includes default empty sparsity description when no pruning is applied.
- Introduced a new argument '-prune-to-zero' to allow users to prune all weights to zero, creating a null model during export.
- Updated the `write_weights` function to handle the new pruning logic, ensuring models can be exported as zero-weight models if desired.
…lity

- Added detailed docstrings to functions for better documentation.
- Separated version printing into a dedicated function.
- Consolidated argument parsing into a single function for clarity.
- Modularized model tracing and conversion logic for better separation of concerns.
- Improved handling of optional parameters with defaults.
- Enhanced error handling with try-except block in the main execution flow.
- Cleaned up variable names and function calls for readability.

This refactoring aims to improve maintainability and enhance the clarity of the code structure while preserving existing functionality.
- Updated nbits choices to include 6, 3, and additional granularity options.
- Changed the quantization mode to "linear" for improved accuracy.
- Enhanced the palettization configuration with 'kmeans' mode and per-grouped channel granularity for better performance.
- Removed unnecessary weight threshold parameter in quantization for cleaner code.

These changes optimize the quantization process, improving both accuracy and latency.
Updated the logic for determining the meta encoder version to handle cases where the metadata encoder is not present or the version is missing from the configuration. This ensures the correct version is set and prevents errors during conversion.
Enhanced the logic for determining the minimum deployment target based on model sparsity and the number of bits specified. The updated conditions provide clearer handling for different scenarios, ensuring compatibility with iOS16 for 8-bit models while maintaining support for iOS18 for others.
- Updated script calls in export_model_for_selfplay.sh, shuffle.sh, shuffle_loop.sh, and train.sh to use `python` instead of `python3` for better compatibility with Miniconda environment.
- Enhanced GPU handling in train.py to correctly utilize MPS (Metal Performance Shaders) for devices on MacOS.
Enhanced the `convert_coreml_pytorch.py` script by introducing an optional `-output` argument. This allows users to specify a custom path for the converted Core ML package, improving flexibility in model saving. Updated the `save_coreml_model` function to handle the new output path.
This update modifies the configuration files `gatekeeper1_maxsize9.cfg` and `selfplay1_maxsize9.cfg` to enhance performance when using the Metal backend in KataGo. The number of game threads has been reduced from 128 to 16 to optimize resource allocation for the Metal architecture. Additionally, the neural network maximum batch size has been decreased from 128 to 8. The number of neural network server threads per model has been increased from 1 to 2 to improve parallel execution. These adjustments aim to enhance training efficiency on Metal backend.
- Updated self-play, allowing specification of the Core ML model directory for loading.
- Enhanced Core ML backend to accept and utilize a model directory, ensuring more flexible model management.
- Modified various neural network backends to compile with the specified directory path.
- Added new arguments for Core ML model files in KataGoCommandLine:
  - coreMLModelFileArg for the core ML model file.
  - humanCoreMLModelFileArg for the human core ML model file.
- Refactored gatekeeper to initialize neural network evaluators with Core ML model paths provided by the user.
- Changed references from NNEvaluator to initializeCoreMLEvaluator for both test and accepted models.
…permanent URL

This change ensures that each CoreML model instance compiles to its own unique URL. Instead of checking for existing model digests to decide compilation, the model is always compiled and saved to a new URL. This resolves potential conflicts when multiple instances attempt to load from the same permanent URL, ensuring accurate predictions for each model instance. Updated the compileMLModel method accordingly.
Updated the gatekeeper1.cfg, gatekeeper1_maxsize9.cfg, selfplay1.cfg, and selfplay1_maxsize9.cfg configuration files to utilize the Neural Engine (NPU) instead of the GPU.

Key changes include:
- Reduced the number of game threads from 128 to 16 for better performance.
- Decreased the neural network maximum batch size from 128 to 8.
- Increased the number of neural network server threads per model from 1 to 2 for improved parallel processing.

These modifications aim to switch to neural engine during training and self-play processes.
Changed the Xcode path from version 15.0.1 to 15.4.0 to ensure compatibility and access to the latest features and bug fixes during the build process.
- Deleted tests that check for the existence of an old ML model, clean-up of URLs, and validation of model compilation.
- Removed unnecessary `misc.swift` file.
- Updated `CMakeLists.txt-macos` to exclude the `misc.swift` source from the build.
- Modified `singleNnScoreValuesResultElts` calculation in `InputBuffers` constructor to use `m.numScoreValueChannels` for the assertion.
- Updated `MetalProcess::processScoreValues` to correctly reference `singleNnScoreValuesResultElts` instead of the deprecated `singleScoreValuesResultElts`, resolving assertion failures related to score value channels.

These changes ensure compatibility with the latest stable branch and maintain the integrity of score value calculations.
This commit addresses a build problem in the Xcode project by removing references to the now-deleted `misc.swift` file. The issue arose after merging the latest version (1.16.0) of KataGo. The changes ensure that the project configuration is up to date and prevents build errors related to missing files.
- Added placeholder return for ACTIVATION_MISH_SCALE8 and default cases to resolve compilation issues in Ninja and Xcode.
- Updated CMakeLists.txt-macos to include the -Wno-c++17-extensions flag.
- Changed variable types from int to size_t in MetalProcess class for consistency.
- Implemented new merging functions for batch normalization scales and biases in SWBatchNormLayerDesc.
- Refactored tests to use merged scale and bias arrays instead of separate pointers.
Updated the compileMLModel method to utilize a permanent URL for storing the compiled CoreML model. This change aims to improve loading times on subsequent launches by checking the permanent URL rather than recompiling the model each time.

This change is marked for caution as it was previously removed due to an unstable crash. Use this version with care.
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