OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Reduce host overhead of prefill-after-decode shape changes (#39033)
Eddy Kim86fc05b3b26e
diff --git a/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp b/src/plugins/intel_gpu/include/intel_gpu/graph/program.hppindex 75bcb1e916a..896427c8e0c 100644--- a/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/graph/program.hpp@@ -283,6 +283,16 @@ public:using ImplementationsCache = cldnn::LruCacheThreadSafe<kernel_impl_params, std::shared_ptr<primitive_impl>, kernel_impl_params::Hasher>;ImplementationsCache& get_implementations_cache() const { return *_impls_cache; }+ // Primitive types that ever had a static impl added to the implementations cache (entries may be evicted later,+ // the type stays). A cache lookup for any other type cannot hit.+ void add_static_impl_type(primitive_type_id type) {+ std::lock_guard<std::mutex> lock(_static_impl_types_mutex);+ _static_impl_types.insert(type);+ }+ bool has_static_impl_type(primitive_type_id type) const {+ std::lock_guard<std::mutex> lock(_static_impl_types_mutex);+ return _static_impl_types.count(type) != 0;+ }ICompilationContext& get_compilation_context() const { return *_compilation_context; }std::shared_ptr<ICompilationContext> get_compilation_context_ptr() const { return _compilation_context; }void cancel_compilation_context();@@ -318,6 +328,8 @@ private:// if subgraph can be optimized if it consists of only inputs and corresponding outputsbool _can_be_optimized;std::unique_ptr<ImplementationsCache> _impls_cache;+ mutable std::mutex _static_impl_types_mutex;+ std::set<primitive_type_id> _static_impl_types;std::shared_ptr<ICompilationContext> _compilation_context;bool _loaded_from_cache = false;diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hppindex 5d739ff2562..302ce178e35 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp@@ -200,6 +200,7 @@ static constexpr Property<int32_t, ov::PropertyMutability::RW> dump_batch_limit{static constexpr Property<std::set<int64_t>, ov::PropertyMutability::RW> dump_iterations{"GPU_DUMP_ITERATIONS"};static constexpr Property<size_t, ov::PropertyMutability::RW> host_time_profiling{"GPU_HOST_TIME_PROFILING"};static constexpr Property<size_t, ov::PropertyMutability::RW> impls_cache_capacity{"GPU_IMPLS_CACHE_CAPACITY"};+static constexpr Property<size_t, ov::PropertyMutability::RW> output_reclaim_min_size{"GPU_OUTPUT_RECLAIM_MIN_SIZE"};static constexpr Property<bool, ov::PropertyMutability::RW> disable_async_compilation{"GPU_DISABLE_ASYNC_COMPILATION"};static constexpr Property<bool, ov::PropertyMutability::RW> disable_runtime_buffer_fusing{"GPU_DISABLE_RUNTIME_BUFFER_FUSING"};static constexpr Property<bool, ov::PropertyMutability::RW> disable_memory_reuse{"GPU_DISABLE_MEMORY_REUSE"};diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inlindex b059c66aab9..0e2e8357925 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl@@ -72,6 +72,7 @@ OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, enable_sdpa_micro, true, "EnablOV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, use_cm, true, "Enable/Disable CM for usage for particular model/platform")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, max_kernels_per_batch, 8, "Controls how many kernels we combine into batch for more efficient ocl compilation")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, impls_cache_capacity, 300, "Controls capacity of LRU implementations cache that is created for each program object for dynamic models")+OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, output_reclaim_min_size, 32 * 1024 * 1024, "Output buffers of dynamic primitives up to this many bytes are kept when the shape shrinks instead of being reallocated")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, asym_dynamic_quantization, false, "Enforce asymmetric mode for dynamically quantized activations")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, could_use_flashattn_v2, true, "Enable/Disable SDPA primitive executing with FlashAttenV2 online softmax tricks.")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, dynamic_quantization_threshold, 64, "Apply dynamic quantization only when batch size is larger than this value in OneDNN")diff --git a/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h b/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.hindex 77810dea9ae..8814fea0196 100644--- a/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h+++ b/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h@@ -49,6 +49,7 @@ public:static std::vector<layout> calc_output_layouts(fully_connected_node const& /*node*/, const kernel_impl_params& impl_param);static layout calc_output_layout(fully_connected_node const& node, kernel_impl_params const& impl_param);static kernel_impl_params get_fake_aligned_params(kernel_impl_params const& orig_impl_param);+ static bool has_fake_alignment() { return true; }static std::string to_string(fully_connected_node const& node);static bool can_apply_single_batch_optimization(const kernel_impl_params& impl_param);diff --git a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h b/src/plugins/intel_gpu/src/graph/include/primitive_inst.hindex d2a1f7fa444..4ea6a54ef83 100644--- a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h+++ b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h@@ -630,6 +630,9 @@ public:static kernel_impl_params get_fake_aligned_params(kernel_impl_params const& orig_impl_param) {return std::move(orig_impl_param);}+ // Must return true in every type that overrides get_fake_aligned_params(), otherwise realloc_outputs() allocates+ // its outputs without the fake alignment+ static bool has_fake_alignment() { return false; }typed_primitive_inst_base(network& network, typed_node const& node): typed_primitive_inst_base(network, node, do_allocate_memory(node)) {}diff --git a/src/plugins/intel_gpu/src/graph/include/primitive_type.h b/src/plugins/intel_gpu/src/graph/include/primitive_type.hindex 6eb8e0c0ec4..422d7641d80 100644--- a/src/plugins/intel_gpu/src/graph/include/primitive_type.h+++ b/src/plugins/intel_gpu/src/graph/include/primitive_type.h@@ -48,6 +48,7 @@ struct primitive_type {virtual layout calc_output_layout(const program_node& node, const kernel_impl_params& params) const = 0;virtual std::vector<layout> calc_output_layouts(const program_node& node, const kernel_impl_params& impl_param) const = 0;virtual kernel_impl_params get_fake_aligned_params(kernel_impl_params const& orig_impl_param) const = 0;+ virtual bool has_fake_alignment() const = 0;virtual std::string to_string(const program_node& node) const = 0;};} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/include/primitive_type_base.h b/src/plugins/intel_gpu/src/graph/include/primitive_type_base.hindex d826d2a4321..c95d33b17cb 100644--- a/src/plugins/intel_gpu/src/graph/include/primitive_type_base.h+++ b/src/plugins/intel_gpu/src/graph/include/primitive_type_base.h@@ -230,6 +230,10 @@ struct primitive_type_base : primitive_type {return typed_primitive_inst<PType>::get_fake_aligned_params(orig_impl_param);}+ bool has_fake_alignment() const override {+ return typed_primitive_inst<PType>::has_fake_alignment();+ }+std::string to_string(const cldnn::program_node& node) const override {OPENVINO_ASSERT(node.type() == this, "[GPU] primitive_type_base::to_string: primitive type mismatch");return typed_primitive_inst<PType>::to_string(node);diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cppindex 9607ea5a236..7253e652542 100644--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp@@ -6,6 +6,7 @@#include <algorithm>#include <memory>+#include <optional>#include <sstream>#include <string>#include <vector>@@ -941,10 +942,16 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {}}- // Update param if fake_alignment is available- auto updated_params = get_fake_aligned_params_if_possible(get_node(), *_impl_params);+ // Update param if fake_alignment is available. For the types without a fake alignment the params would be a plain copy,+ // so these nodes copy just the output layouts and build full params only if they allocate.+ const bool use_fake_aligned_params = get_node().type()->has_fake_alignment();+ // Both branches are prvalues, so nothing is copied (kernel_impl_params has no move constructor); the default one is cheap.+ auto fake_aligned_params = use_fake_aligned_params ? get_fake_aligned_params_if_possible(get_node(), *_impl_params) : kernel_impl_params();+ // A copy, not _impl_params->output_layouts itself: the code below enlarges these layouts to the allocation size (FC users'+ // fake-aligned input, dynamic quantize scales, preallocation), while the users read _impl_params' layouts as their input shapes.+ auto updated_output_layouts = use_fake_aligned_params ? fake_aligned_params.output_layouts : _impl_params->output_layouts;- const auto& actual_layouts = updated_params.output_layouts;+ const auto& actual_layouts = updated_output_layouts;OPENVINO_ASSERT(actual_layouts[0].is_static(), "[GPU] Can't realloc mem for dynamic layout");if (try_bind_remote_permute_output(actual_layouts[0])) {@@ -1107,8 +1114,12 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {// Update output layout with respect to FC's fake alignmentauto updated_layouts = actual_layouts;+ // A skipped node takes its input's memory right below and returns, so the sizing would be thrown away; its FC users+ // then run update_shape() themselves.+ const bool reuses_input_memory =+ !get_node().is_type<concatenation>() && (get_node().is_runtime_skippable() || get_node().is_type<crop>()) && can_be_optimized();std::vector<cldnn::primitive_inst*> user_insts;- {+ if (!reuses_input_memory) {const auto& user_insts_origin = get_user_insts();for (const auto& user : user_insts_origin) {auto uid = user->id();@@ -1187,7 +1198,7 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {GPU_DEBUG_TRACE_DETAIL << "update layout of dynamic quantize output[" << i << "] "<< dyn_quan_layouts[i].to_short_string() << std::endl;updated_layouts[i] = dyn_quan_layouts[i];- updated_params.output_layouts[i] = dyn_quan_layouts[i];+ updated_output_layouts[i] = dyn_quan_layouts[i];}}}@@ -1270,6 +1281,9 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {}int32_t tmp_prealloc_count = get_prealloc_iter_num();+ // Small buffers are kept: when the shape alternates (e.g. LLM prefill <-> decode), reallocating them on every switch+ // costs more host time than the memory it frees.+ const size_t reclaim_min_size = get_config().get_output_reclaim_min_size();// If we allocated too large memory, reclaim the memory.for (size_t i = 0; i < updated_layouts.size(); ++i) {bool reclaim = false;@@ -1286,7 +1300,10 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {} else {required_buffer_size = (updated_layouts[i].get_linear_size());}- if (required_buffer_size * 10 < _max_output_layout_count[i]) {+ // Bit width, not dt_sizes_in_B: that one rounds sub-byte types up to 1 byte+ const size_t max_output_bytes = (_max_output_layout_count[i] * ov::element::Type(actual_layouts[i].data_type).bitwidth() + 7) >> 3;+ const bool small_buffer = !get_node().is_type<kv_cache>() && max_output_bytes <= reclaim_min_size;+ if (required_buffer_size * 10 < _max_output_layout_count[i] && !small_buffer) {reclaim = true;}if (reclaim) {@@ -1322,6 +1339,8 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {}}+ // allocate_output() params of the nodes that don't use fake_aligned_params, built on the first allocation+ std::optional<kernel_impl_params> alloc_params;for (size_t i = 0; i < actual_layouts.size(); ++i) {bool can_reuse_buffer = (_outputs[i] && updated_layouts[i].get_linear_size() <= _max_output_layout_count[i]);std::pair<bool, ov::Shape> prealloc_info;@@ -1336,10 +1355,10 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {prealloc_info = sp.predict_preallocation_shape(id(), updated_layouts[i], can_reuse_buffer, i, tmp_prealloc_count);}if (prealloc_info.first && sp.can_preallocate(ov::shape_size(prealloc_info.second) * (dt_sizes_in_B[i]))) {- updated_params.output_layouts[i] = updated_layouts[i].clone_with_other_shape(prealloc_info.second);+ updated_output_layouts[i] = updated_layouts[i].clone_with_other_shape(prealloc_info.second);}- if (updated_params.output_layouts[i].get_linear_size() < updated_layouts[i].get_linear_size()) {- updated_params.output_layouts[i] = updated_layouts[i];+ if (updated_output_layouts[i].get_linear_size() < updated_layouts[i].get_linear_size()) {+ updated_output_layouts[i] = updated_layouts[i];}// Check for external output memory block (zero-copy dynamic output path).@@ -1348,7 +1367,7 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {if (is_output()) {auto* ext_block = get_network().get_output_memory_block(id());if (ext_block) {- ext_block->resize(updated_params.output_layouts[i]);+ ext_block->resize(updated_output_layouts[i]);_outputs[i] = get_network().get_engine().reinterpret_buffer(*ext_block->memory(), actual_layouts[i]);_max_output_layout_count[i] = ext_block->memory()->get_mem_tracker()->size() / cldnn::data_type_traits::size_of(actual_layouts[i].data_type);GPU_DEBUG_TRACE_DETAIL << id() << ": ext output memory block for output[" << i << "] - " << actual_layouts[i].get_linear_size() << "/"@@ -1383,10 +1402,16 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {GPU_DEBUG_TRACE_DETAIL << id() << ": realloc output memory. " << std::endl;GPU_DEBUG_TRACE_DETAIL << " outputs[" << i << "] " << get_node().id() << " Current buffer_size=" << _max_output_layout_count[i]<< " Requested buffer_size=" << updated_layouts[i].get_linear_size() << std::endl;+ if (!use_fake_aligned_params && !alloc_params) {+ alloc_params = *_impl_params;+ }+ // allocate_output() sizes the buffer from these params, so they must carry the edited layouts+ auto& output_alloc_params = use_fake_aligned_params ? fake_aligned_params : *alloc_params;+ output_alloc_params.output_layouts = updated_output_layouts;_outputs[i] = allocate_output(get_network().get_engine(),get_network().get_memory_pool(),*_node,- updated_params,+ output_alloc_params,_runtime_memory_dependencies,get_network_id(),get_network().is_internal(),@@ -1395,7 +1420,7 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {is_output_buffer(this, true),output_memory_ptr(i).get(),true);- _max_output_layout_count[i] = updated_params.output_layouts[i].get_linear_size();+ _max_output_layout_count[i] = updated_output_layouts[i].get_linear_size();set_flag(ExecutionFlags::MEMORY_CHANGED);GPU_DEBUG_CODE(std::string memalloc_info);GPU_DEBUG_CODE(memalloc_info +=@@ -3020,6 +3045,7 @@ void primitive_inst::update_weights() {reorder_inst->set_impl(reorder_impl->clone());cache.add(*reorder_kernel_params, reorder_impl->clone());+ get_network().get_program()->add_static_impl_type(reorder_kernel_params->desc->type);}auto& stream = get_network().get_stream();@@ -3529,31 +3555,45 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_pauto& prog = *inst.get_network().get_program();auto& kernels_cache = prog.get_kernels_cache();- // Update param if fake_alignment is available- auto updated_params = inst.get_fake_aligned_params_if_possible(inst.get_node(), params);- // Change weights layout of `updated_params` to original one to have valid information- // in _impl->_weights_reorder_params about required weights format after impl selection- if (inst.get_node().is_type<fully_connected>() || inst.get_node().is_type<convolution>() || inst.get_node().is_type<deconvolution>()) {- const auto weights_idx = inst.get_node().get_primitive()->input.size();- const auto original_weights_memory = inst.dep_memory_ptr(weights_idx);- updated_params.weights_layout = optional_layout(original_weights_memory->get_layout());- }+ // Static impl lookup/compilation params: fake-aligned if available, weights with the original layout, no dynamic pad masks+ auto get_updated_params = [&inst, ¶ms]() {+ // Update param if fake_alignment is available+ auto updated_params = inst.get_fake_aligned_params_if_possible(inst.get_node(), params);+ // Change weights layout of `updated_params` to original one to have valid information+ // in _impl->_weights_reorder_params about required weights format after impl selection+ if (inst.get_node().is_type<fully_connected>() || inst.get_node().is_type<convolution>() || inst.get_node().is_type<deconvolution>()) {+ const auto weights_idx = inst.get_node().get_primitive()->input.size();+ const auto original_weights_memory = inst.dep_memory_ptr(weights_idx);+ updated_params.weights_layout = optional_layout(original_weights_memory->get_layout());+ }- for (auto& i : updated_params.input_layouts) {- i.data_padding._dynamic_dims_mask = padding::EMPTY_MASK;- }- for (auto& o : updated_params.output_layouts) {- o.data_padding._dynamic_dims_mask = padding::EMPTY_MASK;- }+ for (auto& i : updated_params.input_layouts) {+ i.data_padding._dynamic_dims_mask = padding::EMPTY_MASK;+ }+ for (auto& o : updated_params.output_layouts) {+ o.data_padding._dynamic_dims_mask = padding::EMPTY_MASK;+ }+ return updated_params;+ };++ // Only types that were ever added to the static impls cache can hit it; the others (e.g. primitives that run a+ // shape-agnostic impl) skip the lookup, and the params copy too unless an async compilation task needs it.+ const bool lookup_static_impl = prog.has_static_impl_type(node->type());+ const bool compile_async = use_async_compilation && ((inst.get_impl() != nullptr && inst.get_impl()->is_dynamic()) || inst.get_flag(ExecutionFlags::SHAPE_CHANGED));+ const bool has_updated_params = lookup_static_impl || compile_async;+ // Both branches are prvalues, so nothing is copied (kernel_impl_params has no move constructor); the default one is cheap.+ auto updated_params = has_updated_params ? get_updated_params() : kernel_impl_params();// 1. If we have static impl in the cache - use it- auto cached_impl = m_static_impls_cache.get(updated_params);- if (cached_impl) {- return cached_impl->clone();+ if (lookup_static_impl) {+ auto cached_impl = m_static_impls_cache.get(updated_params);+ if (cached_impl) {+ return cached_impl->clone();+ }}// 2. If async compilation is enabled and dynamic impl exists or shape changed - try to compile asynchronously- if (use_async_compilation && ((inst.get_impl() && inst.get_impl()->is_dynamic()) || inst.get_flag(ExecutionFlags::SHAPE_CHANGED))) {+ if (compile_async) {auto& compilation_context = prog.get_compilation_context();compilation_context.push_task(updated_params, [&inst, &compilation_context, updated_params, find_impl]() {if (compilation_context.is_stopped()) {@@ -3577,6 +3617,7 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_p}if (impl) {cache.add(updated_params, std::move(impl));+ _program.add_static_impl_type(updated_params.desc->type);}});}@@ -3608,6 +3649,9 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_p}// 6. Finally, if no impl found so far, we just enforce static impl compilation+ if (!has_updated_params) {+ updated_params = get_updated_params();+ }auto static_impl = find_impl(node, updated_params, shape_types::static_shape);if (!static_impl) {auto stringify_layouts = [](const std::vector<layout>& layouts) {@@ -3644,6 +3688,7 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_pauto kernels = kernels_cache.compile(updated_params, static_impl->get_kernels_source());static_impl->set_kernels(std::move(kernels));m_static_impls_cache.add(updated_params, static_impl->clone());+ prog.add_static_impl_type(node->type());}return static_impl;