Skip to content

expose memory-related session options via config - #3503

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
negbie:master
Apr 13, 2026
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
negbie:master

Conversation

@negbie

@negbie negbie commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

As discussed here #3500

Summary by CodeRabbit

  • New Features
    • Added configuration options to control memory pattern and CPU memory arena behavior; setting either option to 0 disables the corresponding runtime memory optimization, enabling finer-grained memory tuning.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 12, 2026
@coderabbitai

coderabbitai Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7e0a6a75-3d41-4761-aa0b-a35f96095f15

📥 Commits

Reviewing files that changed from the base of the PR and between 2f74f11 and 55046be.

📒 Files selected for processing (1)
  • sherpa-onnx/csrc/session.cc
✅ Files skipped from review due to trivial changes (1)
  • sherpa-onnx/csrc/session.cc

📝 Walkthrough

Walkthrough

GetSessionOptionsImpl now checks two optional integer config keys—EnableMemPattern and EnableCpuMemArena—disabling the corresponding ONNX Runtime session options when the value is 0 and removing those keys from the config map.

Changes

Cohort / File(s) Summary
Session Configuration Enhancement
sherpa-onnx/csrc/session.cc
Added handling for EnableMemPattern and EnableCpuMemArena integer keys: if present with value 0, call DisableMemPattern() / DisableCpuMemArena() on SessionOptions and erase the keys from the config map.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 In code I nibble, toggles small and bright,
When zeros whisper, arenas lose their light,
Patterns paused, memory paths set free,
A tidy config, hopped into harmony.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: exposing memory-related session options through configuration.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces configuration options to disable memory pattern optimization and the CPU memory arena in ONNX sessions. The review feedback suggests renaming the new configuration keys from snake_case to PascalCase to maintain consistency with existing parameters in the codebase.

Comment thread sherpa-onnx/csrc/session.cc Outdated
Comment thread sherpa-onnx/csrc/session.cc Outdated
@negbie

negbie commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

Can you tell me if I will be able to use this config in one of your language bindings (for example go) like:

config.ModelConfig.Provider = "cpu:/path/to/config.txt"

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@sherpa-onnx/csrc/session.cc`:
- Around line 179-195: The code currently treats any non-zero integer returned
by ToIntOrDefault for "enable_mem_pattern" and "enable_cpu_mem_arena" as
enabled; update both branches to explicitly validate that the parsed int is
either 0 or 1 (for keys "enable_mem_pattern" and "enable_cpu_mem_arena"), and if
it is outside that range call a clear failure path (e.g., log an error via your
logger and abort/throw) instead of silently proceeding; keep the existing calls
to sess_opts.DisableMemPattern() and sess_opts.DisableCpuMemArena() when the
value is 0 and still erase the config key after validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 311dcf67-f10d-446c-871c-66cbe04c4ff6

📥 Commits

Reviewing files that changed from the base of the PR and between aacfe96 and 2f74f11.

📒 Files selected for processing (1)
  • sherpa-onnx/csrc/session.cc

Comment thread sherpa-onnx/csrc/session.cc Outdated
Comment on lines +179 to +195
if (config.find("enable_mem_pattern") != config.end()) {
int32_t enable_mem_pattern =
ToIntOrDefault(config["enable_mem_pattern"], 1);
if (enable_mem_pattern == 0) {
sess_opts.DisableMemPattern();
}
config.erase("enable_mem_pattern");
}

if (config.find("enable_cpu_mem_arena") != config.end()) {
int32_t enable_cpu_mem_arena =
ToIntOrDefault(config["enable_cpu_mem_arena"], 1);
if (enable_cpu_mem_arena == 0) {
sess_opts.DisableCpuMemArena();
}
config.erase("enable_cpu_mem_arena");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Validate memory flags strictly to avoid silent misconfiguration.

At Line 181 and Line 190, non-0/1 values are implicitly treated as enabled, so invalid config can pass silently. Please validate accepted values explicitly (0 or 1) and log/abort on invalid input.

Suggested tightening
   if (config.find("enable_mem_pattern") != config.end()) {
-    int32_t enable_mem_pattern =
-        ToIntOrDefault(config["enable_mem_pattern"], 1);
+    int32_t enable_mem_pattern =
+        ToIntOrDefault(config["enable_mem_pattern"], -1);
+    if (enable_mem_pattern != 0 && enable_mem_pattern != 1) {
+      SHERPA_ONNX_LOGE("Invalid enable_mem_pattern: %s (expected 0 or 1)",
+                       config["enable_mem_pattern"].c_str());
+      SHERPA_ONNX_EXIT(-1);
+    }
     if (enable_mem_pattern == 0) {
       sess_opts.DisableMemPattern();
     }
     config.erase("enable_mem_pattern");
   }

   if (config.find("enable_cpu_mem_arena") != config.end()) {
-    int32_t enable_cpu_mem_arena =
-        ToIntOrDefault(config["enable_cpu_mem_arena"], 1);
+    int32_t enable_cpu_mem_arena =
+        ToIntOrDefault(config["enable_cpu_mem_arena"], -1);
+    if (enable_cpu_mem_arena != 0 && enable_cpu_mem_arena != 1) {
+      SHERPA_ONNX_LOGE("Invalid enable_cpu_mem_arena: %s (expected 0 or 1)",
+                       config["enable_cpu_mem_arena"].c_str());
+      SHERPA_ONNX_EXIT(-1);
+    }
     if (enable_cpu_mem_arena == 0) {
       sess_opts.DisableCpuMemArena();
     }
     config.erase("enable_cpu_mem_arena");
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (config.find("enable_mem_pattern") != config.end()) {
int32_t enable_mem_pattern =
ToIntOrDefault(config["enable_mem_pattern"], 1);
if (enable_mem_pattern == 0) {
sess_opts.DisableMemPattern();
}
config.erase("enable_mem_pattern");
}
if (config.find("enable_cpu_mem_arena") != config.end()) {
int32_t enable_cpu_mem_arena =
ToIntOrDefault(config["enable_cpu_mem_arena"], 1);
if (enable_cpu_mem_arena == 0) {
sess_opts.DisableCpuMemArena();
}
config.erase("enable_cpu_mem_arena");
}
if (config.find("enable_mem_pattern") != config.end()) {
int32_t enable_mem_pattern =
ToIntOrDefault(config["enable_mem_pattern"], -1);
if (enable_mem_pattern != 0 && enable_mem_pattern != 1) {
SHERPA_ONNX_LOGE("Invalid enable_mem_pattern: %s (expected 0 or 1)",
config["enable_mem_pattern"].c_str());
SHERPA_ONNX_EXIT(-1);
}
if (enable_mem_pattern == 0) {
sess_opts.DisableMemPattern();
}
config.erase("enable_mem_pattern");
}
if (config.find("enable_cpu_mem_arena") != config.end()) {
int32_t enable_cpu_mem_arena =
ToIntOrDefault(config["enable_cpu_mem_arena"], -1);
if (enable_cpu_mem_arena != 0 && enable_cpu_mem_arena != 1) {
SHERPA_ONNX_LOGE("Invalid enable_cpu_mem_arena: %s (expected 0 or 1)",
config["enable_cpu_mem_arena"].c_str());
SHERPA_ONNX_EXIT(-1);
}
if (enable_cpu_mem_arena == 0) {
sess_opts.DisableCpuMemArena();
}
config.erase("enable_cpu_mem_arena");
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@sherpa-onnx/csrc/session.cc` around lines 179 - 195, The code currently
treats any non-zero integer returned by ToIntOrDefault for "enable_mem_pattern"
and "enable_cpu_mem_arena" as enabled; update both branches to explicitly
validate that the parsed int is either 0 or 1 (for keys "enable_mem_pattern" and
"enable_cpu_mem_arena"), and if it is outside that range call a clear failure
path (e.g., log an error via your logger and abort/throw) instead of silently
proceeding; keep the existing calls to sess_opts.DisableMemPattern() and
sess_opts.DisableCpuMemArena() when the value is 0 and still erase the config
key after validation.

@csukuangfj

Copy link
Copy Markdown
Collaborator

Can you tell me if I will be able to use this config in one of your language bindings (for example go) like:

config.ModelConfig.Provider = "cpu:/path/to/config.txt"

Yes, you can do that after this PR gets merged.

@negbie

negbie commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

Nice, thank you for the confirmation and the help earlier!

@csukuangfj csukuangfj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants