Skip to content

Add API dnnl_threadpool_interop_set_scratchpad_concurrency - #316

Draft
sunxiaoxia2022 wants to merge 2 commits into
v3.10_for_ie_masterfrom
xiaoxia/threads_for_scratchpad
Draft

Add API dnnl_threadpool_interop_set_scratchpad_concurrency#316
sunxiaoxia2022 wants to merge 2 commits into
v3.10_for_ie_masterfrom
xiaoxia/threads_for_scratchpad

Conversation

@sunxiaoxia2022

@sunxiaoxia2022 sunxiaoxia2022 commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Description

Problem

On CPU with adaptive threading, model load could fail in deconvolution descriptor creation because oneDNN reported a multi-GB scratchpad requirement for a single ConvolutionBackpropData node.

Root Cause

This issue only happens with TBB_ADAPTIVE when the CPU partitioner runs in AUTO mode.
The CPU plugin calls dnnl_threadpool_interop_set_max_concurrency(int max_concurrency) to set the number of threads for oneDNN.
In partitioner=AUTO mode, max_concurrency=real worker-thread * 32.
Some oneDNN primitives use the inflated virtual concurrency to decide implementation details and scratchpad size.
As a result, descriptors could be built against an inflated thread count, which amplified scratchpad requirements and caused large memory allocations.
Simply put, oneDNN should use virtual concurrency to decide implementation details, and use real worker-thread to calculate scratchpad size.

So this pull request introduces new functionality to control and query the maximum concurrency used by oneDNN when computing scratchpad sizes outside parallel calls. It adds thread-local APIs for setting and getting this "scratchpad concurrency," updates internal utilities to support the feature, and integrates the new API into convolution code paths that allocate scratchpad memory.

Fixes
Fix compilation of small UNet crashes with 2026.0 and later on 16GB MTL
CVS-187773

Dependency PR: PR#36435

Checklist

General

  • Do all unit and benchdnn tests (make test and make test_benchdnn_*) pass locally for each commit?
  • Have you formatted the code using clang-format?

Performance improvements

  • Have you submitted performance data that demonstrates performance improvements?

New features

  • Have you published an RFC for the new feature?
  • Was the RFC approved?
  • Have you added relevant tests?

Bug fixes

  • Have you included information on how to reproduce the issue (either in a github issue or in this PR)?
  • Have you added relevant regression tests?

RFC PR

  • Does RFC document follow the template?
  • Have you added a link to the rendered document?

CHECK(brgemm_convolution_bwd_utils::init_conf(jcp_, isa, desc_,
diff_dst_md_, weights_md_, diff_src_md_, bias_md_, attr_,
dnnl_get_max_threads()));
dnnl_get_max_threads_for_scratchpad()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using dnnl_get_current_num_threads() instead if I understand what this PR tries to solve.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think dnnl_get_current_num_threads() would be incorrect here because this path is used during init_conf()/scratchpad estimation, outside an active parallel region. In the threadpool runtime that can evaluate to 1 when no active threadpool is bound, which would underestimate scratchpad needs. Here we need the scratchpad-specific assumed concurrency, so dnnl_get_max_threads_for_scratchpad() is intentional.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Then I don't understand the technical problem in the first place. Why using dnnl_threadpool_interop_set_max_concurrency doesn't help to propagate the needed number of threads in here which gets accounted through original dnnl_get_max_threads()?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I updated the root cause of this issue in description. Please take a look.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for the updated description.
It looks like that the primitive allocates scratchpad per thread and the number of threads was overestimated due to the user call.
I believe the proposed solution is unreliable as scratchpad is not directly controlled by the user and tipping on how many threads will be actually used over the number passed through API as real worker-thread * 32 seems to me rather a hack than an actual fix.

Besides, the fix in its current way won't make to the upstream version and will live in this limbo forever which doesn't sound like a good resolution to me. I'm probably fine with this solution as a very temporary fix but I recommend to look into a longer-term resolution, for example thread-agnostic scratchpad allocation.

Comment thread src/common/dnnl_thread.hpp Outdated
CHECK(brgemm_convolution_bwd_utils::init_conf(jcp_, isa, desc_,
diff_dst_md_, weights_md_, diff_src_md_, bias_md_, attr_,
dnnl_get_max_threads()));
dnnl_get_max_threads_for_scratchpad()));

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.

Should all scratchpad-related sizing paths that currently use dnnl_get_max_threads() be switched to dnnl_get_max_threads_for_scratchpad() as well? Right now only jit_brgemm_conv_bwd_strided.cpp is updated.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think a full migration is much larger than replacing one API call. There are many scratchpad-sizing paths across different primitives and runtimes. To keep risk manageable and unblock the current bug, this PR applies a minimal targeted fix.

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.

Agreed that this PR is a temporary fix for the concrete issue at hand.

@liubo-intel liubo-intel 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.

@sunxiaoxia2022 could you please also fix the CI failed Clang-Format issue?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants