Skip to content

Commit a558b09

Browse files
authored
[wasm] optimize default session options parsing (microsoft#15428)
### Description optimize default session options parsing. - do minimal property assignment to the passed in `options` object. - modify default value of `enableCpuMemArena` and `enableMemPattern` to `false`. We don't get benefits from enabling these 2 flags in web assembly
1 parent fbee457 commit a558b09

File tree

2 files changed

+24
-49
lines changed

2 files changed

+24
-49
lines changed

web/lib/wasm/session-options.ts

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,66 +81,39 @@ export const setSessionOptions = (options?: InferenceSession.SessionOptions): [n
8181
appendDefaultOptions(sessionOptions);
8282

8383
try {
84-
if (options?.graphOptimizationLevel === undefined) {
85-
sessionOptions.graphOptimizationLevel = 'all';
84+
const graphOptimizationLevel = getGraphOptimzationLevel(sessionOptions.graphOptimizationLevel ?? 'all');
85+
const executionMode = getExecutionMode(sessionOptions.executionMode ?? 'sequential');
86+
const logIdDataOffset =
87+
typeof sessionOptions.logId === 'string' ? allocWasmString(sessionOptions.logId, allocs) : 0;
88+
89+
const logSeverityLevel = sessionOptions.logSeverityLevel ?? 2; // Default to 2 - warning
90+
if (!Number.isInteger(logSeverityLevel) || logSeverityLevel < 0 || logSeverityLevel > 4) {
91+
throw new Error(`log serverity level is not valid: ${logSeverityLevel}`);
8692
}
87-
const graphOptimizationLevel = getGraphOptimzationLevel(sessionOptions.graphOptimizationLevel!);
8893

89-
if (options?.enableCpuMemArena === undefined) {
90-
sessionOptions.enableCpuMemArena = true;
94+
const logVerbosityLevel = sessionOptions.logVerbosityLevel ?? 0; // Default to 0 - verbose
95+
if (!Number.isInteger(logVerbosityLevel) || logVerbosityLevel < 0 || logVerbosityLevel > 4) {
96+
throw new Error(`log verbosity level is not valid: ${logVerbosityLevel}`);
9197
}
9298

93-
if (options?.enableMemPattern === undefined) {
94-
sessionOptions.enableMemPattern = true;
95-
}
96-
97-
if (options?.executionMode === undefined) {
98-
sessionOptions.executionMode = 'sequential';
99-
}
100-
const executionMode = getExecutionMode(sessionOptions.executionMode!);
101-
102-
let logIdDataOffset = 0;
103-
if (options?.logId !== undefined) {
104-
logIdDataOffset = allocWasmString(options.logId, allocs);
105-
}
106-
107-
if (options?.logSeverityLevel === undefined) {
108-
sessionOptions.logSeverityLevel = 2; // Default to warning
109-
} else if (
110-
typeof options.logSeverityLevel !== 'number' || !Number.isInteger(options.logSeverityLevel) ||
111-
options.logSeverityLevel < 0 || options.logSeverityLevel > 4) {
112-
throw new Error(`log serverity level is not valid: ${options.logSeverityLevel}`);
113-
}
114-
115-
if (options?.logVerbosityLevel === undefined) {
116-
sessionOptions.logVerbosityLevel = 0; // Default to 0
117-
} else if (typeof options.logVerbosityLevel !== 'number' || !Number.isInteger(options.logVerbosityLevel)) {
118-
throw new Error(`log verbosity level is not valid: ${options.logVerbosityLevel}`);
119-
}
120-
121-
if (options?.enableProfiling === undefined) {
122-
sessionOptions.enableProfiling = false;
123-
}
124-
125-
let optimizedModelFilePathOffset = 0;
126-
if (typeof options?.optimizedModelFilePath === 'string') {
127-
optimizedModelFilePathOffset = allocWasmString(options.optimizedModelFilePath, allocs);
128-
}
99+
const optimizedModelFilePathOffset = typeof sessionOptions.optimizedModelFilePath === 'string' ?
100+
allocWasmString(sessionOptions.optimizedModelFilePath, allocs) :
101+
0;
129102

130103
sessionOptionsHandle = wasm._OrtCreateSessionOptions(
131-
graphOptimizationLevel, !!sessionOptions.enableCpuMemArena!, !!sessionOptions.enableMemPattern!, executionMode,
132-
!!sessionOptions.enableProfiling!, 0, logIdDataOffset, sessionOptions.logSeverityLevel!,
133-
sessionOptions.logVerbosityLevel!, optimizedModelFilePathOffset);
104+
graphOptimizationLevel, !!sessionOptions.enableCpuMemArena, !!sessionOptions.enableMemPattern, executionMode,
105+
!!sessionOptions.enableProfiling, 0, logIdDataOffset, logSeverityLevel, logVerbosityLevel,
106+
optimizedModelFilePathOffset);
134107
if (sessionOptionsHandle === 0) {
135108
throw new Error('Can\'t create session options');
136109
}
137110

138-
if (options?.executionProviders) {
139-
setExecutionProviders(sessionOptionsHandle, options.executionProviders, allocs);
111+
if (sessionOptions.executionProviders) {
112+
setExecutionProviders(sessionOptionsHandle, sessionOptions.executionProviders, allocs);
140113
}
141114

142-
if (options?.extra !== undefined) {
143-
iterateExtraOptions(options.extra, '', new WeakSet<Record<string, unknown>>(), (key, value) => {
115+
if (sessionOptions.extra !== undefined) {
116+
iterateExtraOptions(sessionOptions.extra, '', new WeakSet<Record<string, unknown>>(), (key, value) => {
144117
const keyDataOffset = allocWasmString(key, allocs);
145118
const valueDataOffset = allocWasmString(value, allocs);
146119

web/lib/wasm/wasm-core-impl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export const createSessionFinalize =
5656
}
5757
} finally {
5858
wasm._free(modelData[0]);
59-
wasm._OrtReleaseSessionOptions(sessionOptionsHandle);
59+
if (sessionOptionsHandle !== 0) {
60+
wasm._OrtReleaseSessionOptions(sessionOptionsHandle);
61+
}
6062
allocs.forEach(wasm._free);
6163
}
6264

0 commit comments

Comments
 (0)