Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 11, 2024
0 parents commit 7f008bb
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: build

on: push

jobs:
build:
runs-on: >-
${{
(matrix.os == 'mac' && matrix.arch == 'arm64') &&
'macos-14' ||
(fromJson('{"linux":"ubuntu-22.04","mac":"macos-13","win":"windows-2022"}')[matrix.os])
}}
continue-on-error: false

strategy:
fail-fast: false
matrix:
os: [linux, mac]
arch: [arm64, x64]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Build
if: matrix.os != 'linux' || matrix.arch == runner.arch
run: |
yarn
yarn build
- name: Set up QEMU
if: matrix.os == 'linux' && matrix.arch != runner.arch
uses: docker/setup-qemu-action@v3
with:
platform: matrix.arch

- name: Enable multi-arch containers in QEMU
if: matrix.os == 'linux' && matrix.arch != runner.arch
run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

- name: Build (in docker)
if: matrix.os == 'linux' && matrix.arch != runner.arch
uses: addnab/docker-run-action@v3
with:
image: node:lts-alpine
options: --platform=linux/${{ matrix.arch }} -v ${{ github.workspace }}:/build -w /build
run: |
yarn
yarn build
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# TypeScript compiled files
/dist/

# Everything else should keep same with .npmignore
*.swp
*.zip

yarn.lock
package-lock.json
npm-debug.log
yarn-error.log
/node_modules/
/build/
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "deps/mlx"]
path = deps/mlx
url = https://github.com/ml-explore/mlx
[submodule "deps/kizunapi"]
path = deps/kizunapi
url = https://github.com/photoionization/kizunapi
14 changes: 14 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Unused source files
/src/
.github
tsconfig.json

# Everything else should keep same with .gitignore
*.swp
*.zip

yarn.lock
package-lock.json
npm-debug.log
/node_modules/
/build/
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Required by cmake-js.
# https://github.com/cmake-js/cmake-js#node-api-and-node-addon-api
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)

# Define project name.
project(node-mlx)

# Use C++17.
set(CMAKE_CXX_STANDARD 17)

# Define NAPI_VERSION.
add_definitions(-DNAPI_VERSION=9)

# Use cmake-js.
add_library(${PROJECT_NAME} SHARED ${CMAKE_JS_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()

# Add source files.
file(GLOB SOURCE_FILES "src/*")
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_FILES})

# Use kizunapi.
target_include_directories(${PROJECT_NAME} PRIVATE "deps/kizunapi")

# Link with MLX.
option(MLX_BUILD_TESTS "Build tests for mlx" OFF)
option(MLX_BUILD_EXAMPLES "Build examples for mlx" OFF)
option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" ON)
add_subdirectory(deps/mlx)
target_link_libraries(${PROJECT_NAME} mlx)
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright © 2023 Apple Inc.
Copyright © 2024 zcbenz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# node-mlx

:construction:
1 change: 1 addition & 0 deletions deps/kizunapi
Submodule kizunapi added at f2fe76
1 change: 1 addition & 0 deletions deps/mlx
Submodule mlx added at 12d450
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require(__dirname + '/../build/Release/node-mlx.node')
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@frost-beta/mlx",
"version": "0.0.1",
"description": "Node-API bindings for MLX",
"main": "lib/index.js",
"scripts": {
"install": "prebuild-install -r napi || echo 'No prebuilt binary of MLX found.'",
"build": "cmake-js build"
},
"author": "zcbenz",
"license": "MIT",
"dependencies": {
"prebuild-install": "7.1.2"
},
"repository": {
"type": "git",
"url": "https://github.com/frost-beta/node-mlx"
},
"binary": {
"napi_versions": [
9
]
},
"devDependencies": {
"cmake-js": "7.3.0"
}
}
11 changes: 11 additions & 0 deletions src/bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <kizunapi.h>

namespace {

napi_value Init(napi_env env, napi_value exports) {
return exports;
}

} // namespace

NAPI_MODULE(mlx, Init)

0 comments on commit 7f008bb

Please sign in to comment.