Skip to content

Commit 61f391f

Browse files
nico-martinxenova
andauthored
[v4] suppress console.error while creating InferenceSession (#1468)
* suppress console.error while creating InferenceSession * changed console suppress if not one of the misleading errors * set default logSeverityLevel and also match the ONNX_WEB.env.logLevel * indentation * small fix * some clean-up * Apply suggestions from code review Co-authored-by: Joshua Lochner <admin@xenova.com> * added LOG_LEVELS to the top of the file --------- Co-authored-by: Joshua Lochner <admin@xenova.com>
1 parent cb6c8b1 commit 61f391f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/backends/onnx.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({
4646
'webnn-cpu': { name: 'webnn', deviceType: 'cpu' }, // WebNN CPU
4747
});
4848

49+
/** @type {Array<'verbose' | 'info' | 'warning' | 'error' | 'fatal'>} */
50+
const LOG_LEVELS = ['verbose', 'info', 'warning', 'error', 'fatal'];
51+
4952
/**
5053
* The list of supported devices, sorted by priority/performance.
5154
* @type {import("../utils/devices.js").DeviceType[]}
@@ -149,6 +152,19 @@ let webInitChain = Promise.resolve();
149152
* @returns {Promise<import('onnxruntime-common').InferenceSession & { config: Object}>} The ONNX inference session.
150153
*/
151154
export async function createInferenceSession(buffer_or_path, session_options, session_config) {
155+
156+
/** @type {0|1|2|3|4} */
157+
const logSeverityLevel =
158+
typeof session_options.logSeverityLevel !== 'number' ||
159+
session_options.logSeverityLevel < 0 ||
160+
session_options.logSeverityLevel > 4
161+
? 4
162+
: session_options.logSeverityLevel;
163+
164+
ONNX_WEB.env.logLevel = LOG_LEVELS[logSeverityLevel];
165+
166+
session_options = { ...session_options, logSeverityLevel };
167+
152168
const load = () => InferenceSession.create(buffer_or_path, session_options);
153169
const session = await (IS_WEB_ENV ? (webInitChain = webInitChain.then(load)) : load());
154170
session.config = session_config;

src/models.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ async function getSession(pretrained_model_name_or_path, fileName, options, is_d
241241

242242
// Overwrite `executionProviders` if not specified
243243
session_options.executionProviders ??= executionProviders;
244+
// Set `logSeverityLevel` to 4 (fatal) if not specified
245+
session_options.logSeverityLevel ??= 4;
244246

245247
// Overwrite `freeDimensionOverrides` if specified in config and not set in session options
246248
const free_dimension_overrides = custom_config.free_dimension_overrides;

0 commit comments

Comments
 (0)