@@ -232,7 +232,7 @@ def from_config(
232232 decoder_inputs : dict [str , str ] | None = None ,
233233 decoder_filename : str | None = None ,
234234 supports_in_place_kv_cache : bool | None = None ,
235- num_kv_cache_layers : int | None = None ,
235+ num_cache_layer_slots : int | None = None ,
236236 ) -> GenaiConfigGenerator :
237237 """Create a generator from a BaseModelConfig-like dataclass.
238238
@@ -242,16 +242,13 @@ def from_config(
242242 overridden since they are often not on the model config.
243243
244244 Args:
245- num_kv_cache_layers : Number of KV cache entries the exported
246- decoder graph actually has . Overrides
245+ num_cache_layer_slots : Number of globally indexed cache slots the
246+ exported decoder graph requires . Overrides
247247 ``config.num_hidden_layers`` for the ``num_hidden_layers``
248- field written to ``genai_config.json`` — ORT-GenAI uses that
249- field to decide how many ``past_key_values.%d.{key,value}``
250- bindings to create, so it must match the graph, not the
251- architecture. They differ for KV-layer-sharing models
252- (Gemma 3n / Gemma 4), where the last
253- ``num_kv_shared_layers`` layers borrow K,V from an earlier
254- layer and own no cache entry. ``None`` uses the config value.
248+ field written to ``genai_config.json``. This is smaller than
249+ the architecture count for KV-sharing models and retains global
250+ indices for hybrid KV/recurrent-cache models. ``None`` uses the
251+ config value.
255252 """
256253 pad = pad_token_id
257254 if pad is None :
@@ -268,13 +265,9 @@ def from_config(
268265 vocab_size = config .vocab_size ,
269266 hidden_size = config .hidden_size ,
270267 num_hidden_layers = (
271- config .num_hidden_layers
272- if model_type == "lfm2"
273- else (
274- num_kv_cache_layers
275- if num_kv_cache_layers is not None
276- else config .num_hidden_layers
277- )
268+ num_cache_layer_slots
269+ if num_cache_layer_slots is not None
270+ else config .num_hidden_layers
278271 ),
279272 num_attention_heads = config .num_attention_heads ,
280273 num_key_value_heads = config .num_key_value_heads ,
@@ -288,7 +281,11 @@ def from_config(
288281 decoder_filename = decoder_filename ,
289282 supports_in_place_kv_cache = supports_in_place_kv_cache ,
290283 layer_types = getattr (config , "layer_types" , None ),
291- conv_cache_size = getattr (config , "short_conv_kernel" , None ),
284+ conv_cache_size = (
285+ getattr (config , "short_conv_kernel" , 1 ) - 1
286+ if hasattr (config , "short_conv_kernel" )
287+ else None
288+ ),
292289 )
293290
294291 def with_vision (
@@ -479,7 +476,9 @@ def generate(self) -> dict[str, Any]:
479476 }
480477 if self .model_type == "lfm2" :
481478 decoder ["layer_types" ] = self ._layer_types or []
482- decoder ["conv_cache_size" ] = self ._conv_cache_size or 3
479+ decoder ["conv_cache_size" ] = (
480+ self ._conv_cache_size if self ._conv_cache_size is not None else 3
481+ )
483482 decoder ["inputs" ]["past_conv_names" ] = "past_key_values.%d.conv_state"
484483 decoder ["outputs" ]["present_conv_names" ] = "present.%d.conv_state"
485484
0 commit comments