Add XNN Pack toggle switch for ONNX inference acceleration (#155)

* Initial plan

* Add XNN Pack switch for ONNX inference acceleration

Co-authored-by: xkeyC <39891083+xkeyC@users.noreply.github.com>

* Refactor Rust ONNX session creation to reduce code duplication

Co-authored-by: xkeyC <39891083+xkeyC@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: xkeyC <39891083+xkeyC@users.noreply.github.com>
This commit is contained in:
Copilot
2025-11-28 21:23:31 +08:00
committed by GitHub
parent db024f19bd
commit db89100402
26 changed files with 197 additions and 74 deletions

View File

@@ -199,8 +199,13 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
String get _localTranslateModelDir => "${appGlobalState.applicationSupportDir}/onnx_models";
bool get _isEnableOnnxXnnPack {
final userBox = Hive.box("app_conf");
return userBox.get("isEnableOnnxXnnPack", defaultValue: true);
}
OnnxTranslationProvider get _localTranslateModelProvider =>
onnxTranslationProvider(_localTranslateModelDir, _localTranslateModelName);
onnxTranslationProvider(_localTranslateModelDir, _localTranslateModelName, _isEnableOnnxXnnPack);
void _checkAutoTranslateOnInit() {
// 检查模型文件是否存在,不存在则关闭自动翻译
@@ -333,20 +338,21 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
@riverpod
class OnnxTranslation extends _$OnnxTranslation {
@override
bool build(String modelDir, String modelName) {
dPrint("[OnnxTranslation] Build provider for model: $modelName");
bool build(String modelDir, String modelName, bool useXnnPack) {
dPrint("[OnnxTranslation] Build provider for model: $modelName, useXnnPack: $useXnnPack");
ref.onDispose(disposeModel);
return false;
}
Future<String?> initModel() async {
dPrint("[OnnxTranslation] Load model: $modelName from $modelDir");
dPrint("[OnnxTranslation] Load model: $modelName from $modelDir, useXnnPack: $useXnnPack");
String? errorMessage;
try {
await ort.loadTranslationModel(
modelPath: "$modelDir/$modelName",
modelKey: modelName,
quantizationSuffix: "_q4f16",
useXnnpack: useXnnPack,
);
state = true;
} catch (e) {

View File

@@ -43,7 +43,7 @@ final class InputMethodDialogUIModelProvider
}
String _$inputMethodDialogUIModelHash() =>
r'51f1708f22a90f7c2f879ad3d2a87a8e2f81b9e9';
r'bd96c85ef2073d80de6eba71748b41adb8861e1c';
abstract class _$InputMethodDialogUIModel
extends $Notifier<InputMethodDialogUIState> {
@@ -73,7 +73,7 @@ final class OnnxTranslationProvider
extends $NotifierProvider<OnnxTranslation, bool> {
const OnnxTranslationProvider._({
required OnnxTranslationFamily super.from,
required (String, String) super.argument,
required (String, String, bool) super.argument,
}) : super(
retry: null,
name: r'onnxTranslationProvider',
@@ -115,7 +115,7 @@ final class OnnxTranslationProvider
}
}
String _$onnxTranslationHash() => r'4f3dc0e361dca2d6b00f557496bdf006cc6c235c';
String _$onnxTranslationHash() => r'd4946a47240ab42dd65c35fa3dda365e4c491462';
final class OnnxTranslationFamily extends $Family
with
@@ -124,7 +124,7 @@ final class OnnxTranslationFamily extends $Family
bool,
bool,
bool,
(String, String)
(String, String, bool)
> {
const OnnxTranslationFamily._()
: super(
@@ -135,23 +135,30 @@ final class OnnxTranslationFamily extends $Family
isAutoDispose: true,
);
OnnxTranslationProvider call(String modelDir, String modelName) =>
OnnxTranslationProvider._(argument: (modelDir, modelName), from: this);
OnnxTranslationProvider call(
String modelDir,
String modelName,
bool useXnnPack,
) => OnnxTranslationProvider._(
argument: (modelDir, modelName, useXnnPack),
from: this,
);
@override
String toString() => r'onnxTranslationProvider';
}
abstract class _$OnnxTranslation extends $Notifier<bool> {
late final _$args = ref.$arg as (String, String);
late final _$args = ref.$arg as (String, String, bool);
String get modelDir => _$args.$1;
String get modelName => _$args.$2;
bool get useXnnPack => _$args.$3;
bool build(String modelDir, String modelName);
bool build(String modelDir, String modelName, bool useXnnPack);
@$mustCallSuper
@override
void runBuild() {
final created = build(_$args.$1, _$args.$2);
final created = build(_$args.$1, _$args.$2, _$args.$3);
final ref = this.ref as $Ref<bool, bool>;
final element =
ref.element

View File

@@ -39,6 +39,13 @@ class SettingsUI extends HookConsumerWidget {
onSwitch: model.onChangeUseInternalDNS,
onTap: () => model.onChangeUseInternalDNS(!sate.isUseInternalDNS)),
const SizedBox(height: 12),
makeSettingsItem(const Icon(FluentIcons.processing, size: 20),
S.current.settings_item_onnx_xnn_pack,
subTitle: S.current.settings_item_onnx_xnn_pack_info,
switchStatus: sate.isEnableOnnxXnnPack,
onSwitch: model.onChangeOnnxXnnPack,
onTap: () => model.onChangeOnnxXnnPack(!sate.isEnableOnnxXnnPack)),
const SizedBox(height: 12),
makeSettingsItem(const Icon(FluentIcons.delete, size: 20),
S.current.setting_action_clear_translation_file_cache,
subTitle: S.current.setting_action_info_cache_clearing_info(

View File

@@ -26,6 +26,7 @@ abstract class SettingsUIState with _$SettingsUIState {
String? customGamePath,
@Default(0) int locationCacheSize,
@Default(false) bool isUseInternalDNS,
@Default(true) bool isEnableOnnxXnnPack,
}) = _SettingsUIState;
}
@@ -44,6 +45,7 @@ class SettingsUIModel extends _$SettingsUIModel {
await _loadLocationCacheSize();
await _loadToolSiteMirrorState();
await _loadUseInternalDNS();
await _loadOnnxXnnPackState();
}
Future<void> setGameLaunchECore(BuildContext context) async {
@@ -227,4 +229,17 @@ class SettingsUIModel extends _$SettingsUIModel {
userBox.get("isUseInternalDNS", defaultValue: false);
state = state.copyWith(isUseInternalDNS: isUseInternalDNS);
}
void onChangeOnnxXnnPack(bool? b) {
final userBox = Hive.box("app_conf");
userBox.put("isEnableOnnxXnnPack", b ?? true);
_initState();
}
Future _loadOnnxXnnPackState() async {
final userBox = await Hive.openBox("app_conf");
final isEnableOnnxXnnPack =
userBox.get("isEnableOnnxXnnPack", defaultValue: true);
state = state.copyWith(isEnableOnnxXnnPack: isEnableOnnxXnnPack);
}
}

View File

@@ -14,7 +14,7 @@ T _$identity<T>(T value) => value;
/// @nodoc
mixin _$SettingsUIState {
bool get isEnableToolSiteMirrors; String get inputGameLaunchECore; String? get customLauncherPath; String? get customGamePath; int get locationCacheSize; bool get isUseInternalDNS;
bool get isEnableToolSiteMirrors; String get inputGameLaunchECore; String? get customLauncherPath; String? get customGamePath; int get locationCacheSize; bool get isUseInternalDNS; bool get isEnableOnnxXnnPack;
/// Create a copy of SettingsUIState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -25,16 +25,16 @@ $SettingsUIStateCopyWith<SettingsUIState> get copyWith => _$SettingsUIStateCopyW
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is SettingsUIState&&(identical(other.isEnableToolSiteMirrors, isEnableToolSiteMirrors) || other.isEnableToolSiteMirrors == isEnableToolSiteMirrors)&&(identical(other.inputGameLaunchECore, inputGameLaunchECore) || other.inputGameLaunchECore == inputGameLaunchECore)&&(identical(other.customLauncherPath, customLauncherPath) || other.customLauncherPath == customLauncherPath)&&(identical(other.customGamePath, customGamePath) || other.customGamePath == customGamePath)&&(identical(other.locationCacheSize, locationCacheSize) || other.locationCacheSize == locationCacheSize)&&(identical(other.isUseInternalDNS, isUseInternalDNS) || other.isUseInternalDNS == isUseInternalDNS));
return identical(this, other) || (other.runtimeType == runtimeType&&other is SettingsUIState&&(identical(other.isEnableToolSiteMirrors, isEnableToolSiteMirrors) || other.isEnableToolSiteMirrors == isEnableToolSiteMirrors)&&(identical(other.inputGameLaunchECore, inputGameLaunchECore) || other.inputGameLaunchECore == inputGameLaunchECore)&&(identical(other.customLauncherPath, customLauncherPath) || other.customLauncherPath == customLauncherPath)&&(identical(other.customGamePath, customGamePath) || other.customGamePath == customGamePath)&&(identical(other.locationCacheSize, locationCacheSize) || other.locationCacheSize == locationCacheSize)&&(identical(other.isUseInternalDNS, isUseInternalDNS) || other.isUseInternalDNS == isUseInternalDNS)&&(identical(other.isEnableOnnxXnnPack, isEnableOnnxXnnPack) || other.isEnableOnnxXnnPack == isEnableOnnxXnnPack));
}
@override
int get hashCode => Object.hash(runtimeType,isEnableToolSiteMirrors,inputGameLaunchECore,customLauncherPath,customGamePath,locationCacheSize,isUseInternalDNS);
int get hashCode => Object.hash(runtimeType,isEnableToolSiteMirrors,inputGameLaunchECore,customLauncherPath,customGamePath,locationCacheSize,isUseInternalDNS,isEnableOnnxXnnPack);
@override
String toString() {
return 'SettingsUIState(isEnableToolSiteMirrors: $isEnableToolSiteMirrors, inputGameLaunchECore: $inputGameLaunchECore, customLauncherPath: $customLauncherPath, customGamePath: $customGamePath, locationCacheSize: $locationCacheSize, isUseInternalDNS: $isUseInternalDNS)';
return 'SettingsUIState(isEnableToolSiteMirrors: $isEnableToolSiteMirrors, inputGameLaunchECore: $inputGameLaunchECore, customLauncherPath: $customLauncherPath, customGamePath: $customGamePath, locationCacheSize: $locationCacheSize, isUseInternalDNS: $isUseInternalDNS, isEnableOnnxXnnPack: $isEnableOnnxXnnPack)';
}
@@ -45,7 +45,7 @@ abstract mixin class $SettingsUIStateCopyWith<$Res> {
factory $SettingsUIStateCopyWith(SettingsUIState value, $Res Function(SettingsUIState) _then) = _$SettingsUIStateCopyWithImpl;
@useResult
$Res call({
bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS
bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS, bool isEnableOnnxXnnPack
});
@@ -62,7 +62,7 @@ class _$SettingsUIStateCopyWithImpl<$Res>
/// Create a copy of SettingsUIState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? isEnableToolSiteMirrors = null,Object? inputGameLaunchECore = null,Object? customLauncherPath = freezed,Object? customGamePath = freezed,Object? locationCacheSize = null,Object? isUseInternalDNS = null,}) {
@pragma('vm:prefer-inline') @override $Res call({Object? isEnableToolSiteMirrors = null,Object? inputGameLaunchECore = null,Object? customLauncherPath = freezed,Object? customGamePath = freezed,Object? locationCacheSize = null,Object? isUseInternalDNS = null,Object? isEnableOnnxXnnPack = null,}) {
return _then(_self.copyWith(
isEnableToolSiteMirrors: null == isEnableToolSiteMirrors ? _self.isEnableToolSiteMirrors : isEnableToolSiteMirrors // ignore: cast_nullable_to_non_nullable
as bool,inputGameLaunchECore: null == inputGameLaunchECore ? _self.inputGameLaunchECore : inputGameLaunchECore // ignore: cast_nullable_to_non_nullable
@@ -70,6 +70,7 @@ as String,customLauncherPath: freezed == customLauncherPath ? _self.customLaunch
as String?,customGamePath: freezed == customGamePath ? _self.customGamePath : customGamePath // ignore: cast_nullable_to_non_nullable
as String?,locationCacheSize: null == locationCacheSize ? _self.locationCacheSize : locationCacheSize // ignore: cast_nullable_to_non_nullable
as int,isUseInternalDNS: null == isUseInternalDNS ? _self.isUseInternalDNS : isUseInternalDNS // ignore: cast_nullable_to_non_nullable
as bool,isEnableOnnxXnnPack: null == isEnableOnnxXnnPack ? _self.isEnableOnnxXnnPack : isEnableOnnxXnnPack // ignore: cast_nullable_to_non_nullable
as bool,
));
}
@@ -155,10 +156,10 @@ return $default(_that);case _:
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS)? $default,{required TResult orElse(),}) {final _that = this;
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS, bool isEnableOnnxXnnPack)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _SettingsUIState() when $default != null:
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS);case _:
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS,_that.isEnableOnnxXnnPack);case _:
return orElse();
}
@@ -176,10 +177,10 @@ return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.c
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS) $default,) {final _that = this;
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS, bool isEnableOnnxXnnPack) $default,) {final _that = this;
switch (_that) {
case _SettingsUIState():
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS);case _:
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS,_that.isEnableOnnxXnnPack);case _:
throw StateError('Unexpected subclass');
}
@@ -196,10 +197,10 @@ return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.c
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS)? $default,) {final _that = this;
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS, bool isEnableOnnxXnnPack)? $default,) {final _that = this;
switch (_that) {
case _SettingsUIState() when $default != null:
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS);case _:
return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.customLauncherPath,_that.customGamePath,_that.locationCacheSize,_that.isUseInternalDNS,_that.isEnableOnnxXnnPack);case _:
return null;
}
@@ -211,7 +212,7 @@ return $default(_that.isEnableToolSiteMirrors,_that.inputGameLaunchECore,_that.c
class _SettingsUIState implements SettingsUIState {
_SettingsUIState({this.isEnableToolSiteMirrors = false, this.inputGameLaunchECore = "0", this.customLauncherPath, this.customGamePath, this.locationCacheSize = 0, this.isUseInternalDNS = false});
_SettingsUIState({this.isEnableToolSiteMirrors = false, this.inputGameLaunchECore = "0", this.customLauncherPath, this.customGamePath, this.locationCacheSize = 0, this.isUseInternalDNS = false, this.isEnableOnnxXnnPack = true});
@override@JsonKey() final bool isEnableToolSiteMirrors;
@@ -220,6 +221,7 @@ class _SettingsUIState implements SettingsUIState {
@override final String? customGamePath;
@override@JsonKey() final int locationCacheSize;
@override@JsonKey() final bool isUseInternalDNS;
@override@JsonKey() final bool isEnableOnnxXnnPack;
/// Create a copy of SettingsUIState
/// with the given fields replaced by the non-null parameter values.
@@ -231,16 +233,16 @@ _$SettingsUIStateCopyWith<_SettingsUIState> get copyWith => __$SettingsUIStateCo
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _SettingsUIState&&(identical(other.isEnableToolSiteMirrors, isEnableToolSiteMirrors) || other.isEnableToolSiteMirrors == isEnableToolSiteMirrors)&&(identical(other.inputGameLaunchECore, inputGameLaunchECore) || other.inputGameLaunchECore == inputGameLaunchECore)&&(identical(other.customLauncherPath, customLauncherPath) || other.customLauncherPath == customLauncherPath)&&(identical(other.customGamePath, customGamePath) || other.customGamePath == customGamePath)&&(identical(other.locationCacheSize, locationCacheSize) || other.locationCacheSize == locationCacheSize)&&(identical(other.isUseInternalDNS, isUseInternalDNS) || other.isUseInternalDNS == isUseInternalDNS));
return identical(this, other) || (other.runtimeType == runtimeType&&other is _SettingsUIState&&(identical(other.isEnableToolSiteMirrors, isEnableToolSiteMirrors) || other.isEnableToolSiteMirrors == isEnableToolSiteMirrors)&&(identical(other.inputGameLaunchECore, inputGameLaunchECore) || other.inputGameLaunchECore == inputGameLaunchECore)&&(identical(other.customLauncherPath, customLauncherPath) || other.customLauncherPath == customLauncherPath)&&(identical(other.customGamePath, customGamePath) || other.customGamePath == customGamePath)&&(identical(other.locationCacheSize, locationCacheSize) || other.locationCacheSize == locationCacheSize)&&(identical(other.isUseInternalDNS, isUseInternalDNS) || other.isUseInternalDNS == isUseInternalDNS)&&(identical(other.isEnableOnnxXnnPack, isEnableOnnxXnnPack) || other.isEnableOnnxXnnPack == isEnableOnnxXnnPack));
}
@override
int get hashCode => Object.hash(runtimeType,isEnableToolSiteMirrors,inputGameLaunchECore,customLauncherPath,customGamePath,locationCacheSize,isUseInternalDNS);
int get hashCode => Object.hash(runtimeType,isEnableToolSiteMirrors,inputGameLaunchECore,customLauncherPath,customGamePath,locationCacheSize,isUseInternalDNS,isEnableOnnxXnnPack);
@override
String toString() {
return 'SettingsUIState(isEnableToolSiteMirrors: $isEnableToolSiteMirrors, inputGameLaunchECore: $inputGameLaunchECore, customLauncherPath: $customLauncherPath, customGamePath: $customGamePath, locationCacheSize: $locationCacheSize, isUseInternalDNS: $isUseInternalDNS)';
return 'SettingsUIState(isEnableToolSiteMirrors: $isEnableToolSiteMirrors, inputGameLaunchECore: $inputGameLaunchECore, customLauncherPath: $customLauncherPath, customGamePath: $customGamePath, locationCacheSize: $locationCacheSize, isUseInternalDNS: $isUseInternalDNS, isEnableOnnxXnnPack: $isEnableOnnxXnnPack)';
}
@@ -251,7 +253,7 @@ abstract mixin class _$SettingsUIStateCopyWith<$Res> implements $SettingsUIState
factory _$SettingsUIStateCopyWith(_SettingsUIState value, $Res Function(_SettingsUIState) _then) = __$SettingsUIStateCopyWithImpl;
@override @useResult
$Res call({
bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS
bool isEnableToolSiteMirrors, String inputGameLaunchECore, String? customLauncherPath, String? customGamePath, int locationCacheSize, bool isUseInternalDNS, bool isEnableOnnxXnnPack
});
@@ -268,7 +270,7 @@ class __$SettingsUIStateCopyWithImpl<$Res>
/// Create a copy of SettingsUIState
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? isEnableToolSiteMirrors = null,Object? inputGameLaunchECore = null,Object? customLauncherPath = freezed,Object? customGamePath = freezed,Object? locationCacheSize = null,Object? isUseInternalDNS = null,}) {
@override @pragma('vm:prefer-inline') $Res call({Object? isEnableToolSiteMirrors = null,Object? inputGameLaunchECore = null,Object? customLauncherPath = freezed,Object? customGamePath = freezed,Object? locationCacheSize = null,Object? isUseInternalDNS = null,Object? isEnableOnnxXnnPack = null,}) {
return _then(_SettingsUIState(
isEnableToolSiteMirrors: null == isEnableToolSiteMirrors ? _self.isEnableToolSiteMirrors : isEnableToolSiteMirrors // ignore: cast_nullable_to_non_nullable
as bool,inputGameLaunchECore: null == inputGameLaunchECore ? _self.inputGameLaunchECore : inputGameLaunchECore // ignore: cast_nullable_to_non_nullable
@@ -276,6 +278,7 @@ as String,customLauncherPath: freezed == customLauncherPath ? _self.customLaunch
as String?,customGamePath: freezed == customGamePath ? _self.customGamePath : customGamePath // ignore: cast_nullable_to_non_nullable
as String?,locationCacheSize: null == locationCacheSize ? _self.locationCacheSize : locationCacheSize // ignore: cast_nullable_to_non_nullable
as int,isUseInternalDNS: null == isUseInternalDNS ? _self.isUseInternalDNS : isUseInternalDNS // ignore: cast_nullable_to_non_nullable
as bool,isEnableOnnxXnnPack: null == isEnableOnnxXnnPack ? _self.isEnableOnnxXnnPack : isEnableOnnxXnnPack // ignore: cast_nullable_to_non_nullable
as bool,
));
}

View File

@@ -41,7 +41,7 @@ final class SettingsUIModelProvider
}
}
String _$settingsUIModelHash() => r'd19104d924f018a9230548d0372692fc344adacd';
String _$settingsUIModelHash() => r'72947d5ed36290df865cb010b056dc632f5dccec';
abstract class _$SettingsUIModel extends $Notifier<SettingsUIState> {
SettingsUIState build();