Skip to content

Commit 75d1829

Browse files
committed
a lot of work
1 parent 35b6d05 commit 75d1829

35 files changed

Lines changed: 1061 additions & 330 deletions

‎CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ include_directories(
7373
set(LIB_SOURCE_FILES
7474
src/objc.mm
7575
src/bridge.mm
76+
src/block.mm
7677
src/class.mm
7778
src/closure.mm
7879
src/bridged_class.mm
79-
src/bridged_method.mm
80+
src/native_call.mm
8081
src/method_cif.mm
8182
src/objc_bridge_data.mm
8283
src/type_conv.mm

‎deno.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
},
1919

2020
"fmt": {
21-
"exclude": ["build", "libffi", "node_modules"]
21+
"exclude": ["build", "libffi", "node_modules", "metadata"]
2222
}
2323
}
File renamed without changes.
File renamed without changes.

‎examples/foundation.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { classes } from "../index.js";
2+
3+
// const {
4+
// NSString,
5+
// } = classes;
6+
7+
// function measure(time) {
8+
// const before = performance.now();
9+
// NSTextAlignment.center;
10+
// const after = performance.now();
11+
// console.log(time, ((after - before) * 1000000).toFixed(2) + "ns");
12+
// }
13+
14+
// measure("first get");
15+
// measure("second get");
16+
// measure("third get");
17+
// measure("fourth get");
18+
// measure("fifth get");
19+
// measure("sixth get");
20+
21+
// console.log(NSString.stringWithUTF8String("Hello, macOS").length);
22+
23+
const { NSBundle } = classes;
24+
25+
// NSBundle.bundleWithPath("/System/Library/Frameworks/CoreML.framework").load();
26+
// NSBundle.bundleWithPath("/System/Library/Frameworks/Metal.framework").load();
27+
NSBundle.bundleWithPath("/System/Library/Frameworks/CoreFoundation.framework")
28+
.load();
29+
30+
console.log(CFAbsoluteTimeGetCurrent());
31+
const date = CFDateCreate(null, CFAbsoluteTimeGetCurrent());
32+
console.log(date);
33+
34+
// console.log(MTLCreateSystemDefaultDevice());
35+
36+
// const { MLNeuralEngineComputeDevice } = classes;
37+
38+
// console.log(Object.keys(MLNeuralEngineComputeDevice.prototype));
39+
40+
// const device = MLNeuralEngineComputeDevice.physicalDevice();
41+
42+
// console.log(device, device.totalCoreCount);

test/main.js renamed to examples/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const {
77

88
const str = NSString.stringWithUTF8String("Hello World");
99

10+
const str2 = new Proxy(str, {
11+
get: (target, prop) => {
12+
return target[prop];
13+
},
14+
});
15+
1016
bench("noop", () => {});
1117

1218
bench("[str length] (manual, direct)", () => {
@@ -17,6 +23,10 @@ bench("[str length] (dynamic, ffi)", () => {
1723
str.length;
1824
});
1925

26+
bench("[str length] (dynamic, proxy)", () => {
27+
str2.length;
28+
});
29+
2030
await run({
2131
avg: true, // enable/disable avg column (default: true)
2232
json: false, // enable/disable json output (default: false)

‎examples/mlc.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { classes, objc } from "../index.js";
2+
3+
objc.import("MLCompute");
4+
5+
const {
6+
MLCTensor,
7+
MLCTensorData,
8+
MLCGraph,
9+
MLCInferenceGraph,
10+
MLCFullyConnectedLayer,
11+
MLCConvolutionDescriptor,
12+
MLCDevice,
13+
} = classes;
14+
15+
const batchSize = 1;
16+
const shape = [batchSize, 28, 28, 1];
17+
const imageSize = shape[1] * shape[2];
18+
const layerSize = 128;
19+
20+
const tInput = MLCTensor.tensorWithShapeDataType(
21+
shape,
22+
MLCDataType.float32,
23+
);
24+
const tWeights = MLCTensor.tensorWithShapeRandomInitializerTypeDataType(
25+
[1, imageSize, layerSize, 1],
26+
MLCRandomInitializerType.glorotUniform,
27+
MLCDataType.float32,
28+
);
29+
const tBiases = MLCTensor.tensorWithShapeRandomInitializerTypeDataType(
30+
[1, layerSize, 1, 1],
31+
MLCRandomInitializerType.glorotUniform,
32+
MLCDataType.float32,
33+
);
34+
35+
const input = new Float32Array(imageSize * batchSize).fill(1);
36+
37+
const dataInput = MLCTensorData.dataWithBytesNoCopyLength(
38+
input,
39+
input.byteLength,
40+
);
41+
42+
const graph = MLCGraph.new();
43+
44+
const denseLayer = MLCFullyConnectedLayer.layerWithWeightsBiasesDescriptor(
45+
tWeights,
46+
tBiases,
47+
MLCConvolutionDescriptor
48+
.descriptorWithKernelWidthKernelHeightInputFeatureChannelCountOutputFeatureChannelCount(
49+
layerSize,
50+
imageSize,
51+
imageSize,
52+
layerSize,
53+
),
54+
);
55+
56+
graph.nodeWithLayerSources(denseLayer, [tInput]);
57+
58+
const device = MLCDevice.gpuDevice();
59+
60+
const inference = MLCInferenceGraph.graphWithGraphObjects([graph]);
61+
inference.addInputs({ input: tInput });
62+
63+
inference.compileWithOptionsDevice(
64+
MLCGraphCompilationOptions.debugLayers,
65+
device,
66+
);
67+
68+
const callback = objc.registerBlock("v@@d", (output, error, time) => {
69+
console.log(output, error, time);
70+
});
71+
72+
console.log(callback);
73+
74+
inference.executeWithInputsDataBatchSizeOptionsCompletionHandler(
75+
{ input: dataInput },
76+
batchSize,
77+
MLCExecutionOptions.synchronous,
78+
callback,
79+
);
File renamed without changes.

0 commit comments

Comments
 (0)