babalae/better-genshin-impact · error · InvalidEnumArgumentException

无效的推理设备

Error message

无效的推理设备

What it means

BgiOnnxFactory.GetProviders switches on InferenceDeviceType (Cpu, Dml, Gpu, OpenVino). Any other value reaches the default arm and throws InvalidEnumArgumentException('无效的推理设备'). This is the device-selection stage that decides which ONNX Runtime execution providers to try; an unknown device type means no providers can be chosen.

Source

Thrown at BetterGenshinImpact/Core/Recognition/ONNX/BgiOnnxFactory.cs:201

                    testSession = new SessionOptions();
                    testSession.AppendExecutionProvider("OpenVINO", GetOpenVinoProviderConfig(null));
                    testSession.GraphOptimizationLevel = GraphOptimizationLevel.ORT_DISABLE_ALL;
                    list.Add(ProviderType.OpenVino);
                }
                catch (Exception e)
                {
                    _logger.LogDebug("[init]无法加载OpenVino。可能不支持,跳过。({Err})", e.Message);
                }
                finally
                {
                    testSession?.Dispose();
                }

                list.Add(ProviderType.Cpu);
                return list.ToArray();
            }
            default:
                throw new InvalidEnumArgumentException("无效的推理设备");
        }
    }

    /// <summary>
    ///     自动嗅探并修改path以加载cuda
    /// </summary>
    private void AppendCudaPath()
    {
        var cudaVersion =
            Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\NVIDIA Corporation\GPU Computing Toolkit\CUDA",
                "FirstVersionInstalled", null)?.ToString() ?? "v12.8";
        string[] filePrefix = ["cudnn", "nvrtc", "cudart", "nvinfer", "cublas", "onnx"];
        string[] environmentVariableNames = ["PATH", "CUDA_PATH", "CUDNN_PATH", "LD_LIBRARY_PATH"];

        // 例如: CUDNN\v9.8\lib\12.8\x64
        var validPaths = environmentVariableNames.SelectMany(s => Environment
                // 获取所有可能包含CUDA/cuDNN路径的环境变量
                .GetEnvironmentVariable(s, EnvironmentVariableTarget.Process)?

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Set the inference device config to a supported value (Cpu, Dml, Gpu, or OpenVino).
  2. Validate InferenceDeviceType at startup and default to Cpu if invalid.
  3. Update the switch when adding a new device type.

Example fix

// before
var providers = factory.GetProviders(config.Device); // unknown device

// after
var device = Enum.IsDefined(config.Device) ? config.Device : InferenceDeviceType.Cpu;
var providers = factory.GetProviders(device);
Defensive patterns

Strategy: validation

Validate before calling

var device = config.Device is InferenceDeviceType.Cpu or InferenceDeviceType.Dml
    or InferenceDeviceType.Gpu or InferenceDeviceType.OpenVino
        ? config.Device
        : InferenceDeviceType.Cpu;
var providers = factory.GetProviders(device);

Type guard

static bool IsSupportedDevice(InferenceDeviceType d) => d is InferenceDeviceType.Cpu or InferenceDeviceType.Dml or InferenceDeviceType.Gpu or InferenceDeviceType.OpenVino;

Prevention

When it happens

Trigger: An InferenceDeviceType value outside the handled set; default(InferenceDeviceType) when 0 is not a valid device; a config value referencing a removed device.

Common situations: New device enum member added without a case; config file with a typo or stale device name; settings from a different app version.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/9e5cc247f1b00f43. Report an issue: GitHub.