-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
272 additions
and
41 deletions.
There are no files selected for viewing
Submodule kizunapi
updated
from c4dbe5 to f04ca7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
#ifndef SRC_BINDINGS_H_ | ||
#define SRC_BINDINGS_H_ | ||
|
||
#include <mlx/mlx.h> | ||
#include <kizunapi.h> | ||
|
||
namespace mx = mlx::core; | ||
|
||
void InitDevice(napi_env env, napi_value exports); | ||
void InitStream(napi_env env, napi_value exports); | ||
void InitArray(napi_env env, napi_value exports); | ||
|
||
#endif // SRC_BINDINGS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "src/device.h" | ||
#include "src/util.h" | ||
|
||
namespace ki { | ||
|
||
template<> | ||
struct TypeBridge<mx::Device> { | ||
static inline void Finalize(mx::Device* ptr) { | ||
delete ptr; | ||
} | ||
}; | ||
|
||
// static | ||
napi_status Type<mx::Device::DeviceType>::ToNode( | ||
napi_env env, mx::Device::DeviceType type, napi_value* result) { | ||
return ConvertToNode(env, static_cast<int>(type), result); | ||
} | ||
|
||
// static | ||
std::optional<mx::Device::DeviceType> Type<mx::Device::DeviceType>::FromNode( | ||
napi_env env, napi_value value) { | ||
std::optional<int> type = ki::FromNode<int>(env, value); | ||
if (!type) | ||
return std::nullopt; | ||
if (*type == static_cast<int>(mx::Device::DeviceType::cpu)) | ||
return mx::Device::DeviceType::cpu; | ||
if (*type == static_cast<int>(mx::Device::DeviceType::gpu)) | ||
return mx::Device::DeviceType::gpu; | ||
return std::nullopt; | ||
} | ||
|
||
// static | ||
mx::Device* Type<mx::Device>::Constructor(mx::Device::DeviceType type, | ||
int index) { | ||
return new mx::Device(type, index); | ||
} | ||
|
||
// static | ||
void Type<mx::Device>::Define(napi_env env, | ||
napi_value constructor, | ||
napi_value prototype) { | ||
DefineProperties(env, prototype, | ||
Property("type", Getter(&mx::Device::type))); | ||
} | ||
|
||
// static | ||
napi_status Type<mx::Device>::ToNode(napi_env env, | ||
mx::Device device, | ||
napi_value* result) { | ||
return ManagePointerInJSWrapper( | ||
env, new mx::Device(std::move(device)), result); | ||
} | ||
|
||
// static | ||
std::optional<mx::Device> Type<mx::Device>::FromNode(napi_env env, | ||
napi_value value) { | ||
// Try creating a Device when value is a DeviceType. | ||
auto type = ki::FromNode<mx::Device::DeviceType>(env, value); | ||
if (type) | ||
return mx::Device(*type); | ||
// Otherwise try converting from Device. | ||
return NodeObjToCppValue<mx::Device>(env, value); | ||
} | ||
|
||
} // namespace ki | ||
|
||
void InitDevice(napi_env env, napi_value exports) { | ||
ki::Set(env, exports, | ||
"cpu", mx::Device::DeviceType::cpu, | ||
"gpu", mx::Device::DeviceType::gpu, | ||
"Device", ki::Class<mx::Device>(), | ||
"defaultDevice", mx::default_device, | ||
"setDefaultDevice", mx::set_default_device); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef SRC_DEVICE_H_ | ||
#define SRC_DEVICE_H_ | ||
|
||
#include "src/bindings.h" | ||
|
||
namespace ki { | ||
|
||
template<> | ||
struct Type<mx::Device::DeviceType> { | ||
static constexpr const char* name = "DeviceType"; | ||
static napi_status ToNode(napi_env env, | ||
mx::Device::DeviceType type, | ||
napi_value* result); | ||
static std::optional<mx::Device::DeviceType> FromNode(napi_env env, | ||
napi_value value); | ||
}; | ||
|
||
template<> | ||
struct Type<mx::Device> { | ||
static constexpr const char* name = "Device"; | ||
|
||
static mx::Device* Constructor(mx::Device::DeviceType type, int index); | ||
static void Define(napi_env env, | ||
napi_value constructor, | ||
napi_value prototype); | ||
|
||
static napi_status ToNode(napi_env env, | ||
mx::Device device, | ||
napi_value* result); | ||
static std::optional<mx::Device> FromNode(napi_env env, | ||
napi_value value); | ||
}; | ||
|
||
} // namespace ki | ||
|
||
#endif // SRC_DEVICE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "src/stream.h" | ||
#include "src/util.h" | ||
|
||
namespace ki { | ||
|
||
template<> | ||
struct TypeBridge<mx::Stream> { | ||
static inline void Finalize(mx::Stream* ptr) { | ||
delete ptr; | ||
} | ||
}; | ||
|
||
// static | ||
mx::Stream* Type<mx::Stream>::Constructor(int index, const mx::Device& device) { | ||
return new mx::Stream(index, device); | ||
} | ||
|
||
// static | ||
void Type<mx::Stream>::Define(napi_env env, | ||
napi_value constructor, | ||
napi_value prototype) { | ||
DefineProperties(env, prototype, | ||
Property("device", Getter(&mx::Stream::device))); | ||
} | ||
|
||
// static | ||
napi_status Type<mx::Stream>::ToNode(napi_env env, | ||
mx::Stream stream, | ||
napi_value* result) { | ||
return ManagePointerInJSWrapper( | ||
env, new mx::Stream(std::move(stream)), result); | ||
} | ||
|
||
// static | ||
std::optional<mx::Stream> Type<mx::Stream>::FromNode(napi_env env, | ||
napi_value value) { | ||
return NodeObjToCppValue<mx::Stream>(env, value); | ||
} | ||
|
||
// static | ||
std::optional<mx::StreamOrDevice> Type<mx::StreamOrDevice>::FromNode( | ||
napi_env env, | ||
napi_value value) { | ||
std::optional<mx::Stream> stream = Type<mx::Stream>::FromNode(env, value); | ||
if (stream) | ||
return *stream; | ||
std::optional<mx::Device> device = Type<mx::Device>::FromNode(env, value); | ||
if (device) | ||
return *device; | ||
return std::nullopt; | ||
} | ||
|
||
} // namespace ki | ||
|
||
void InitStream(napi_env env, napi_value exports) { | ||
ki::Set(env, exports, | ||
"Stream", ki::Class<mx::Stream>(), | ||
"defaultStream", mx::default_stream, | ||
"setDefaultStream", mx::set_default_stream, | ||
"newStream", mx::new_stream); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef SRC_STREAM_H_ | ||
#define SRC_STREAM_H_ | ||
|
||
#include "src/device.h" | ||
|
||
namespace ki { | ||
|
||
template<> | ||
struct Type<mx::Stream> { | ||
static constexpr const char* name = "Stream"; | ||
|
||
static mx::Stream* Constructor(int index, const mx::Device& device); | ||
static void Define(napi_env env, | ||
napi_value constructor, | ||
napi_value prototype); | ||
|
||
static napi_status ToNode(napi_env env, | ||
mx::Stream stream, | ||
napi_value* result); | ||
static std::optional<mx::Stream> FromNode(napi_env env, | ||
napi_value value); | ||
}; | ||
|
||
template<> | ||
struct Type<mx::StreamOrDevice> { | ||
static constexpr const char* name = "StreamOrDevice"; | ||
static std::optional<mx::StreamOrDevice> FromNode(napi_env env, | ||
napi_value value); | ||
}; | ||
|
||
} // namespace ki | ||
|
||
#endif // SRC_STREAM_H_ |
Oops, something went wrong.