Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add cache_primitive_desc example #1977

Closed

Conversation

shu1chen
Copy link
Contributor

@shu1chen shu1chen commented Jun 26, 2024

Description

This C++ API example demonstrates how to implement a cache mechanism for primitive descriptors. The purpose of this cache is to prevent the repeated construction of the same primitive descriptor for identical shapes. This is particularly beneficial when creating and executing a large number of convolution primitives, as it can significantly reduce the overhead of construction of the primitive descriptors.

Key optimizations included in this example:

  • Utilizing hash values of keys for faster unordered_map queries.
  • A Least Recently Used (LRU) list to maintain key order and ensure frequently used primitive descriptors remain within the MAX_CACHE_SIZE limit.

An example of the output:

Conv primitive_desc newly constructed - IC: 32, Time for conv primitive_desc: 859.98 us
Conv primitive_desc newly constructed - IC: 64, Time for conv primitive_desc: 329.964 us
Conv primitive_desc newly constructed - IC: 128, Time for conv primitive_desc: 285.738 us
Conv primitive_desc cache hit - IC: 32, Time for conv primitive_desc: 3.322 us
Conv primitive_desc cache hit - IC: 64, Time for conv primitive_desc: 1.785 us
Conv primitive_desc cache hit - IC: 128, Time for conv primitive_desc: 1.258 us
Conv primitive_desc newly constructed - IC: 256, Time for conv primitive_desc: 352.398 us
Example passed on CPU.

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?

@shu1chen shu1chen requested a review from vpirogov June 26, 2024 07:57
@shu1chen shu1chen force-pushed the add_primitive_desc_cache_example branch 2 times, most recently from 37dafbc to 846474c Compare June 26, 2024 08:40
@shu1chen shu1chen force-pushed the add_primitive_desc_cache_example branch from 846474c to 9f40daf Compare June 26, 2024 09:10
@vpirogov
Copy link
Member

vpirogov commented Jul 3, 2024

oneDNN offers to options to amortize memory descriptor creation cost:

  • Retaining primitive descriptors on the application side.
  • Using primitive cache feature, which is enabled in default builds.

Introducing additional caching mechanism on the user side is not recommended, as it will inevitably rely on the library implementation details and thus would be fragile. It still worth investigating underlying performance issue in oneDNN that triggered this work.

@vpirogov vpirogov closed this Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants