Skip to content

Commit ddd3f97

Browse files
committed
add numpy-style array implementation
1 parent 7707fb1 commit ddd3f97

29 files changed

+7301
-3346
lines changed

example/image_recognization/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class _MyHomePageState extends State<MyHomePage> {
177177
pixData.addAll(pixB);
178178

179179
final host = input.map(mnn.MapType.MNN_MAP_TENSOR_WRITE, input.dimensionType);
180-
host.cast<mnn.f32>().asTypedList(pixData.length).setAll(0, pixData);
180+
host.cast<mnn.float32>().asTypedList(pixData.length).setAll(0, pixData);
181181
input.unmap(mnn.MapType.MNN_MAP_TENSOR_WRITE, input.dimensionType, host);
182182

183183
_net!.runSession(_session!);
@@ -194,7 +194,7 @@ class _MyHomePageState extends State<MyHomePage> {
194194
final type = outputUser.type;
195195

196196
if (type.code == mnn.HalideTypeCode.halide_type_float) {
197-
final values = outputUser.host.cast<mnn.f32>();
197+
final values = outputUser.host.cast<mnn.float32>();
198198
for (var i = 0; i < size; i++) {
199199
tempValues.add((i, values[i]));
200200
}

example/image_recognization/test/widget_test.dart

Lines changed: 0 additions & 30 deletions
This file was deleted.

example/main.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void main(List<String> args) async {
7373
print("image: ${im1.values.sublist(0, 7)}");
7474

7575
nchwTensor.setImage(i - 1, im1);
76-
print(nchwTensor.cast<mnn.f32>().asTypedList(7));
76+
print(nchwTensor.cast<mnn.float32>().asTypedList(7));
7777
}
7878
input.copyFromHost(nchwTensor);
7979
nchwTensor.dispose();
@@ -91,17 +91,17 @@ void main(List<String> args) async {
9191
final size = outputUser.getStride(0);
9292
final tempValues = <(int, double)>[];
9393
if (type.code == mnn.HalideTypeCode.halide_type_float) {
94-
final values = outputUser.host.cast<mnn.f32>() + batch * size;
94+
final values = outputUser.host.cast<mnn.float32>() + batch * size;
9595
for (var i = 0; i < size; i++) {
9696
tempValues.add((i, values[i]));
9797
}
9898
} else if (type.code == mnn.HalideTypeCode.halide_type_uint && type.bytes == 1) {
99-
final values = outputUser.host.cast<mnn.u8>() + batch * size;
99+
final values = outputUser.host.cast<mnn.uint8>() + batch * size;
100100
for (var i = 0; i < size; i++) {
101101
tempValues.add((i, values[i].toDouble()));
102102
}
103103
} else if (type.code == mnn.HalideTypeCode.halide_type_int && type.bytes == 1) {
104-
final values = outputUser.host.cast<mnn.i8>() + batch * size;
104+
final values = outputUser.host.cast<mnn.int8>() + batch * size;
105105
for (var i = 0; i < size; i++) {
106106
tempValues.add((i, values[i].toDouble()));
107107
}

example/recognize/bin/inference.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ List<List<(int, double)>> inference(
8686
final size = outputUser.getStride(0);
8787
final tempValues = <(int, double)>[];
8888
if (type.code == mnn.HalideTypeCode.halide_type_float) {
89-
final values = outputUser.cast<mnn.f32>() + batch * size;
89+
final values = outputUser.cast<mnn.float32>() + batch * size;
9090
for (var i = 0; i < size; i++) {
9191
tempValues.add((i, values[i]));
9292
}
9393
} else if (type.code == mnn.HalideTypeCode.halide_type_uint &&
9494
type.bytes == 1) {
95-
final values = outputUser.cast<mnn.u8>() + batch * size;
95+
final values = outputUser.cast<mnn.uint8>() + batch * size;
9696
for (var i = 0; i < size; i++) {
9797
tempValues.add((i, values[i].toDouble()));
9898
}
9999
} else if (type.code == mnn.HalideTypeCode.halide_type_int &&
100100
type.bytes == 1) {
101-
final values = outputUser.cast<mnn.i8>() + batch * size;
101+
final values = outputUser.cast<mnn.int8>() + batch * size;
102102
for (var i = 0; i < size; i++) {
103103
tempValues.add((i, values[i].toDouble()));
104104
}

ffigen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ enums:
8585
'stbir_datatype': 'StbirDataType'
8686

8787
preamble: |
88-
// dart format off
88+
// coverage:ignore-file
8989
// Copyright (c) 2025, rainyl. Please see the AUTHORS file
9090
// for details. All rights reserved. Use of this source code is governed by a
9191
// BSD-style license that can be found in the LICENSE file.

lib/numpy.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export 'src/expr/op.dart';
1+
export 'src/numpy/linalg.dart';
2+
export 'src/numpy/numpy.dart';
3+
export 'src/numpy/random.dart';

lib/src/base.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mixin ComparableMixin {
2626
/// Base class for wrapping C++ objects in Dart
2727
abstract class NativeObject with ComparableMixin implements ffi.Finalizable {
2828
/// Pointer to the underlying C++ object
29-
final ffi.Pointer<ffi.Void> _ptr;
29+
ffi.Pointer<ffi.Void> _ptr;
3030

3131
ffi.NativeFinalizer get finalizer;
3232

@@ -56,6 +56,14 @@ abstract class NativeObject with ComparableMixin implements ffi.Finalizable {
5656
@protected
5757
void release();
5858

59+
void reattach(ffi.Pointer<ffi.Void> ptr) {
60+
dispose();
61+
_ptr = ptr;
62+
if (attach) {
63+
finalizer.attach(this, _ptr, detach: this);
64+
}
65+
}
66+
5967
@mustCallSuper
6068
void dispose() {
6169
if (attach) {
@@ -97,13 +105,13 @@ void MnnAssert(bool condition, String message) {
97105
}
98106
}
99107

100-
typedef u8 = ffi.Uint8;
101-
typedef u16 = ffi.Uint16;
102-
typedef u32 = ffi.Uint32;
103-
typedef u64 = ffi.Uint64;
104-
typedef i8 = ffi.Int8;
105-
typedef i16 = ffi.Int16;
106-
typedef i32 = ffi.Int32;
107-
typedef i64 = ffi.Int64;
108-
typedef f32 = ffi.Float;
109-
typedef f64 = ffi.Double;
108+
typedef uint8 = ffi.Uint8;
109+
typedef uint16 = ffi.Uint16;
110+
typedef uint32 = ffi.Uint32;
111+
typedef uint64 = ffi.Uint64;
112+
typedef int8 = ffi.Int8;
113+
typedef int16 = ffi.Int16;
114+
typedef int32 = ffi.Int32;
115+
typedef int64 = ffi.Int64;
116+
typedef float32 = ffi.Float;
117+
typedef float64 = ffi.Double;

0 commit comments

Comments
 (0)