Skip to content

Commit 05acac4

Browse files
committed
support custom definations
1 parent e566c39 commit 05acac4

File tree

8 files changed

+96
-32
lines changed

8 files changed

+96
-32
lines changed

example/recognize/bin/inference.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'package:dartcv4/dartcv.dart' as cv;
66

77
mnn.Interpreter loadModel(String modelPath) {
88
final net = mnn.Interpreter.fromFile(modelPath);
9-
net.setSessionMode(mnn.SessionMode.Session_Backend_Auto);
10-
net.setSessionHint(mnn.HintMode.MAX_TUNING_NUMBER, 5);
9+
net.setSessionMode(mnn.SessionMode.Session_Backend_Fix);
10+
// net.setSessionHint(mnn.HintMode.MAX_TUNING_NUMBER, 5);
1111
return net;
1212
}
1313

example/recognize/bin/recognize.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main(List<String> arguments) {
6767

6868
final model = loadModel(modelPath!);
6969
final config = mnn.ScheduleConfig.create(
70-
type: mnn.ForwardType.MNN_FORWARD_AUTO,
70+
type: mnn.ForwardType.MNN_FORWARD_METAL,
7171
);
7272
final session = model.createSession(config: config);
7373
final result = inference(session, imagePaths, topK: topk);

example/recognize/pubspec.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ hooks:
2929
- contrib
3030
- calib3d
3131
- features2d
32-
# - dnn
32+
- dnn
3333
- highgui
3434
- flann
3535
- objdetect
3636
- photo
3737
- stitching
3838
- video
3939
- videoio
40+
mnn:
41+
defines:
42+
MNN_SUPPORT_BF16: ON
43+
MNN_BUILD_OPENCV: OFF
44+
MNN_LOW_MEMORY: OFF
45+
MNN_METAL: ON
46+
MNN_OPENCL: ON

hook/build.dart

+15-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import 'dart:io';
88

99
import 'package:logging/logging.dart';
10+
import 'package:mnn/src/hook_helpers/parse_user_define.dart';
1011
import 'package:native_assets_cli/native_assets_cli.dart';
1112
import 'package:native_toolchain_cmake/native_toolchain_cmake.dart';
1213

@@ -18,13 +19,23 @@ void main(List<String> args) async {
1819

1920
Future<void> _builder(BuildInput input, BuildOutputBuilder output) async {
2021
final packageName = input.packageName;
21-
final packagePath = await getPackagePath(packageName);
22-
final sourceDir = Uri.directory(packagePath).resolve('src/');
22+
final packagePath = Uri.directory(await getPackagePath(packageName));
23+
final sourceDir = packagePath.resolve('src/');
2324
// final outDir = Uri.directory(packagePath).resolve('build/');
2425
final logger = Logger("")
2526
..level = Level.ALL
2627
..onRecord.listen((record) => stderr.writeln(record.message));
27-
// ..onRecord.listen((record) => print(record.message));
28+
// ..onRecord.listen((record) => print(record.message));
29+
30+
final defsDefault = parseUserDefinedOptions(packagePath.resolve("pubspec.yaml").toFilePath());
31+
final defsUser = parseUserDefinedOptions(
32+
Platform.script.resolve('../../../../pubspec.yaml').toFilePath(),
33+
);
34+
final defsFinal = {...defsDefault, ...defsUser};
35+
36+
logger.info("default defines: $defsDefault");
37+
logger.info("user defines: $defsUser");
38+
logger.info("final defines: $defsFinal");
2839

2940
final builder = CMakeBuilder.create(
3041
name: packageName,
@@ -34,8 +45,7 @@ Future<void> _builder(BuildInput input, BuildOutputBuilder output) async {
3445
targets: ['install'],
3546
defines: {
3647
'CMAKE_INSTALL_PREFIX': input.outputDirectory.resolve('install').toFilePath(),
37-
'MNN_DEBUG': 'OFF',
38-
'MNNC_BUILD_TEST': 'OFF',
48+
...defsFinal,
3949
},
4050
buildLocal: true,
4151
);

lib/mnn.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ export 'src/backend.dart';
1010
export 'src/base.dart';
1111
export 'src/constant.dart';
1212
export 'src/exception.dart';
13-
export 'src/g/mnn.g.dart' show DimensionType, HalideTypeCode, HandleDataType, ErrorCode, MapType;
13+
export 'src/g/mnn.g.dart'
14+
show
15+
DimensionType,
16+
HalideTypeCode,
17+
HandleDataType,
18+
ErrorCode,
19+
MapType,
20+
StbirDataType,
21+
StbirEdge,
22+
StbirFilter,
23+
StbirPixelLayout;
1424
export 'src/halide_runtime.dart';
1525
export 'src/image/image.dart';
1626
export 'src/image_process.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:io';
2+
import 'package:yaml/yaml.dart';
3+
4+
/// Parse the user defined exclude modules from pubspec.yaml
5+
///
6+
/// Returns a list of excluded module names
7+
Map<String, String> parseUserDefinedOptions(String pubspecPath) {
8+
try {
9+
// Read the pubspec.yaml file
10+
final File file = File(pubspecPath);
11+
if (!file.existsSync()) {
12+
return {};
13+
}
14+
15+
// Parse the YAML content
16+
final String yamlContent = file.readAsStringSync();
17+
final dynamic yamlMap = loadYaml(yamlContent);
18+
19+
// Navigate to the hooks.user_defines.mnn.exclude_modules section
20+
if (yamlMap is YamlMap &&
21+
yamlMap['hooks'] is YamlMap &&
22+
yamlMap['hooks']['user_defines'] is YamlMap &&
23+
yamlMap['hooks']['user_defines']['mnn'] is YamlMap &&
24+
yamlMap['hooks']['user_defines']['mnn']['defines'] is YamlMap) {
25+
final YamlMap excludeModules = yamlMap['hooks']['user_defines']['mnn']['defines'] as YamlMap;
26+
27+
return excludeModules.map((k, v) => MapEntry(k.toString(), v.toString()));
28+
}
29+
30+
return {};
31+
} catch (e) {
32+
// Return empty list in case of any error
33+
print('Error parsing exclude_modules: $e');
34+
return {};
35+
}
36+
}

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies:
1313
native_assets_cli: ^0.13.0
1414
native_toolchain_cmake: ^0.0.5
1515
path: ^1.9.1
16+
yaml: ^3.1.3
1617
# native_toolchain_cmake:
1718
# path: ../native_toolchain_cmake
1819

src/CMakeLists.txt

+22-22
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,43 @@ endif ()
1818
set(MNN_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE)
1919
set(MNN_BUILD_TEST OFF CACHE BOOL "" FORCE)
2020
set(MNN_BUILD_TOOLS OFF CACHE BOOL "Build tools/cpp or not" FORCE)
21-
set(MNN_SUPPORT_BF16 ON CACHE BOOL "Enable MNN's bf16 op" FORCE)
21+
# set(MNN_SUPPORT_BF16 ON CACHE BOOL "Enable MNN's bf16 op" FORCE)
2222
set(MNN_SEP_BUILD OFF CACHE BOOL "Build MNN Backends and expression separately. Only works with MNN_BUILD_SHARED_LIBS=ON" FORCE)
2323
set(MNN_BUILD_SHARED_LIBS OFF CACHE BOOL "MNN build shared or static lib" FORCE)
24-
set(MNN_BUILD_OPENCV ON CACHE BOOL "Build OpenCV api in MNN." FORCE)
25-
set(MNN_USE_SYSTEM_LIB OFF CACHE BOOL "For opencl and vulkan, use system lib or use dlopen" FORCE)
26-
set(MNN_BUILD_HARD OFF CACHE BOOL "Build -mfloat-abi=hard or not" FORCE)
24+
# set(MNN_BUILD_OPENCV ON CACHE BOOL "Build OpenCV api in MNN." FORCE)
25+
# set(MNN_USE_SYSTEM_LIB OFF CACHE BOOL "For opencl and vulkan, use system lib or use dlopen" FORCE)
26+
# set(MNN_BUILD_HARD OFF CACHE BOOL "Build -mfloat-abi=hard or not" FORCE)
2727
set(MNN_WIN_RUNTIME_MT OFF CACHE BOOL "MNN use /MT on Windows dll" FORCE)
28-
set(MNN_FORBID_MULTI_THREAD OFF CACHE BOOL "Disable Multi Thread" FORCE)
29-
set(MNN_OPENMP OFF CACHE BOOL "Use OpenMP's thread pool implementation. Does not work on iOS or Mac OS" FORCE)
30-
set(MNN_USE_THREAD_POOL ON CACHE BOOL "Use MNN's own thread pool implementation" FORCE)
28+
# set(MNN_FORBID_MULTI_THREAD OFF CACHE BOOL "Disable Multi Thread" FORCE)
29+
# set(MNN_OPENMP OFF CACHE BOOL "Use OpenMP's thread pool implementation. Does not work on iOS or Mac OS" FORCE)
30+
# set(MNN_USE_THREAD_POOL ON CACHE BOOL "Use MNN's own thread pool implementation" FORCE)
3131
set(MNN_BUILD_TRAIN OFF CACHE BOOL "Build MNN's training framework" FORCE)
3232
set(MNN_BUILD_DEMO OFF CACHE BOOL "Build demo/exec or not" FORCE)
3333
set(MNN_BUILD_QUANTOOLS OFF CACHE BOOL "Build Quantized Tools or not" FORCE)
3434
set(MNN_EVALUATION OFF CACHE BOOL "Build Evaluation Tools or not" FORCE)
3535
set(MNN_BUILD_CONVERTER OFF CACHE BOOL "Build Converter" FORCE)
3636
set(MNN_SUPPORT_DEPRECATED_OP OFF CACHE BOOL "Enable MNN's tflite quantized op" FORCE)
37-
set(MNN_DEBUG_MEMORY OFF CACHE BOOL "MNN Debug Memory Access" FORCE)
38-
set(MNN_DEBUG_TENSOR_SIZE OFF CACHE BOOL "Enable Tensor Size" FORCE)
39-
set(MNN_GPU_TRACE OFF CACHE BOOL "Enable MNN Gpu Debug" FORCE)
40-
set(MNN_SUPPORT_RENDER OFF CACHE BOOL "Enable MNN Render Ops" FORCE)
41-
set(MNN_SUPPORT_TRANSFORMER_FUSE OFF CACHE BOOL "Enable MNN transformer Fuse Ops" FORCE)
37+
# set(MNN_DEBUG_MEMORY OFF CACHE BOOL "MNN Debug Memory Access" FORCE)
38+
# set(MNN_DEBUG_TENSOR_SIZE OFF CACHE BOOL "Enable Tensor Size" FORCE)
39+
# set(MNN_GPU_TRACE OFF CACHE BOOL "Enable MNN Gpu Debug" FORCE)
40+
# set(MNN_SUPPORT_RENDER OFF CACHE BOOL "Enable MNN Render Ops" FORCE)
41+
# set(MNN_SUPPORT_TRANSFORMER_FUSE OFF CACHE BOOL "Enable MNN transformer Fuse Ops" FORCE)
4242
set(NATIVE_LIBRARY_OUTPUT OFF CACHE BOOL "Native Library Path" FORCE)
4343
set(NATIVE_INCLUDE_OUTPUT OFF CACHE BOOL "Native Include Path" FORCE)
4444
set(MNN_AAPL_FMWK OFF CACHE BOOL "Build MNN.framework instead of traditional .a/.dylib" FORCE)
45-
set(MNN_WITH_PLUGIN OFF CACHE BOOL "Build with plugin op support." FORCE)
46-
set(MNN_BUILD_MINI OFF CACHE BOOL "Build MNN-MINI that just supports fixed shape models." FORCE)
47-
set(MNN_USE_SSE ON CACHE BOOL "Use SSE optimization for x86 if possiable" FORCE)
45+
# set(MNN_WITH_PLUGIN OFF CACHE BOOL "Build with plugin op support." FORCE)
46+
# set(MNN_BUILD_MINI OFF CACHE BOOL "Build MNN-MINI that just supports fixed shape models." FORCE)
47+
# set(MNN_USE_SSE ON CACHE BOOL "Use SSE optimization for x86 if possiable" FORCE)
4848
set(MNN_BUILD_CODEGEN OFF CACHE BOOL "Build with codegen" FORCE)
4949
set(MNN_ENABLE_COVERAGE OFF CACHE BOOL "Build with coverage enable" FORCE)
50-
set(MNN_BUILD_PROTOBUFFER ON CACHE BOOL "Build with protobuffer in MNN" FORCE)
51-
set(MNN_BUILD_LLM OFF CACHE BOOL "Build llm library based MNN." FORCE)
52-
set(MNN_BUILD_DIFFUSION OFF CACHE BOOL "Build diffusion demo based MNN." FORCE)
53-
set(MNN_INTERNAL OFF CACHE BOOL "Build with MNN internal features, such as model authentication, metrics logging" FORCE)
50+
# set(MNN_BUILD_PROTOBUFFER ON CACHE BOOL "Build with protobuffer in MNN" FORCE)
51+
# set(MNN_BUILD_LLM OFF CACHE BOOL "Build llm library based MNN." FORCE)
52+
# set(MNN_BUILD_DIFFUSION OFF CACHE BOOL "Build diffusion demo based MNN." FORCE)
53+
# set(MNN_INTERNAL OFF CACHE BOOL "Build with MNN internal features, such as model authentication, metrics logging" FORCE)
5454
set(MNN_JNI OFF CACHE BOOL "Build MNN Jni for java to use" FORCE)
55-
set(MNN_LOW_MEMORY OFF CACHE BOOL "Build MNN support low memory for weight quant model." FORCE)
56-
set(MNN_CPU_WEIGHT_DEQUANT_GEMM OFF CACHE BOOL "Build MNN CPU weight dequant related gemm kernels." FORCE)
57-
set(MNN_BUILD_AUDIO OFF CACHE BOOL "Build audio api in MNN." FORCE)
55+
# set(MNN_LOW_MEMORY OFF CACHE BOOL "Build MNN support low memory for weight quant model." FORCE)
56+
# set(MNN_CPU_WEIGHT_DEQUANT_GEMM OFF CACHE BOOL "Build MNN CPU weight dequant related gemm kernels." FORCE)
57+
# set(MNN_BUILD_AUDIO OFF CACHE BOOL "Build audio api in MNN." FORCE)
5858

5959
if(WIN32)
6060
set(MNN_WIN_RUNTIME_MT OFF CACHE BOOL "" FORCE)

0 commit comments

Comments
 (0)