33
44#pragma once
55
6+ #include < mutex>
7+ #include < any>
8+ #include < functional>
9+ #include < memory>
10+
611#include " module_genai/module_data_type.hpp"
712#include " module_genai/module_print_config.hpp"
813#include " module_genai/module_type.hpp"
@@ -52,14 +57,56 @@ class IBaseModuleDesc {
5257// map: module name -> module desc
5358using PipelineModulesDesc = std::unordered_map<std::string, IBaseModuleDesc::PTR>;
5459
60+ // Pipeline-scoped resource cache for sharing compiled models across modules
61+ // Resources are automatically released when PipelineDesc is destroyed
62+ class PipelineResourceCache {
63+ public:
64+ PipelineResourceCache () = default ;
65+ ~PipelineResourceCache () = default ;
66+
67+ // Non-copyable
68+ PipelineResourceCache (const PipelineResourceCache&) = delete ;
69+ PipelineResourceCache& operator =(const PipelineResourceCache&) = delete ;
70+
71+ // Get or create a resource with the given key
72+ // Returns {resource, was_cached} - the bool indicates whether the resource was already in the cache
73+ template <typename T>
74+ std::pair<std::shared_ptr<T>, bool > get_or_create (
75+ std::size_t cache_key,
76+ std::function<std::shared_ptr<T>()> creator) {
77+ std::lock_guard<std::mutex> lock (m_mutex);
78+ auto it = m_cache.find (cache_key);
79+ if (it != m_cache.end ()) {
80+ auto typed = std::any_cast<std::shared_ptr<T>>(&it->second );
81+ if (!typed) {
82+ OPENVINO_THROW (" PipelineResourceCache: cache key collision - entry exists with a different type for key " ,
83+ cache_key);
84+ }
85+ return {*typed, true };
86+ }
87+ auto resource = creator ();
88+ m_cache[cache_key] = resource;
89+ return {resource, false };
90+ }
91+
92+ void clear () {
93+ std::lock_guard<std::mutex> lock (m_mutex);
94+ m_cache.clear ();
95+ }
96+
97+ private:
98+ std::mutex m_mutex;
99+ std::unordered_map<std::size_t , std::any> m_cache;
100+ };
101+
55102class PipelineDesc {
56103protected:
57- PipelineDesc () = default ;
104+ PipelineDesc ();
58105 PipelineDesc (const PipelineDesc&) = delete ;
59106 PipelineDesc& operator =(const PipelineDesc&) = delete ;
60107
61108public:
62- ~PipelineDesc () = default ;
109+ ~PipelineDesc ();
63110 // global_context;
64111 std::string model_type;
65112
@@ -75,13 +122,18 @@ class PipelineDesc {
75122 return m_models_map;
76123 }
77124
125+ // Pipeline-scoped resource cache for modules to share resources
126+ // Resources are released when PipelineDesc is destroyed
127+ PipelineResourceCache& get_resource_cache ();
128+
78129 using PTR = std::shared_ptr<PipelineDesc>;
79130 static PTR create () {
80131 return std::shared_ptr<PipelineDesc>(new PipelineDesc ());
81132 }
82133
83134private:
84135 ConfigModelsMap m_models_map;
136+ std::unique_ptr<PipelineResourceCache> m_resource_cache;
85137};
86138
87139} // namespace module
0 commit comments