@@ -634,6 +634,7 @@ struct server_metrics {
634634
635635struct server_context_impl {
636636 friend struct server_context ;
637+ friend struct server_routes ;
637638
638639public:
639640 // only use these pointers outside of this class:
@@ -683,7 +684,7 @@ struct server_context_impl {
683684 int n_empty_consecutive = 0 ;
684685
685686 std::unique_ptr<server_prompt_cache> prompt_cache;
686- server_tiered_cache tiered_cache;
687+ std::unique_ptr< server_tiered_cache> tiered_cache;
687688
688689 server_metrics metrics;
689690
@@ -858,7 +859,7 @@ struct server_context_impl {
858859
859860 // Initialize tiered cache if enabled
860861 if (params_base.kv_tiered_enabled ) {
861- tiered_cache = server_tiered_cache (params_base);
862+ tiered_cache = std::make_unique< server_tiered_cache> (params_base);
862863 SRV_INF (" tiered cache initialized, hot=%f%%, warm=%f%%, cold=%f%%\n " ,
863864 params_base.kv_tier_hot_pct , params_base.kv_tier_warm_pct , params_base.kv_tier_cold_pct );
864865 }
@@ -916,10 +917,10 @@ struct server_context_impl {
916917
917918 // initialize tier manager for slot if enabled
918919 if (params_base.kv_tiered_enabled ) {
919- if (!tiered_cache. init_slot (i, *model)) {
920+ if (!tiered_cache-> init_slot (i, *model)) {
920921 SRV_WRN (" failed to initialize tier manager for slot %d\n " , i);
921922 } else {
922- SLT_INF (slot, " tier manager initialized for slot\n " );
923+ SLT_INF (slot, " tier manager initialized for slot %s \n " , " " );
923924 }
924925 }
925926
@@ -2184,20 +2185,6 @@ struct server_context_impl {
21842185 continue ;
21852186 }
21862187
2187- // Try tiered cache eviction before context shift
2188- if (params_base.kv_tiered_enabled ) {
2189- auto * slot_tier = tiered_cache.get_slot_manager (slot.id );
2190- if (slot_tier && slot_tier->initialized ) {
2191- // Evict tokens to tiered cache before context shift
2192- int n_evict = std::min (n_discard, int (slot_tier->tiered_cache ->get_config ().cold_capacity ()));
2193- if (n_evict > 0 ) {
2194- if (tiered_cache.evict_from_slot (slot.id , n_evict)) {
2195- SLT_INF (slot, " tiered cache eviction: %d tokens\n " , n_evict);
2196- }
2197- }
2198- }
2199- }
2200-
22012188 // Shift context
22022189 int n_keep = slot.task ->params .n_keep < 0 ? slot.task ->n_tokens () : slot.task ->params .n_keep ;
22032190
@@ -2210,6 +2197,20 @@ struct server_context_impl {
22102197 const int n_left = slot.prompt .n_tokens () - n_keep;
22112198 const int n_discard = slot.task ->params .n_discard ? slot.task ->params .n_discard : (n_left / 2 );
22122199
2200+ // Try tiered cache eviction before context shift
2201+ if (params_base.kv_tiered_enabled ) {
2202+ auto * slot_tier = tiered_cache->get_slot_manager (slot.id );
2203+ if (slot_tier && slot_tier->initialized ) {
2204+ // Evict tokens to tiered cache before context shift
2205+ int n_evict = std::min (n_discard, int (slot_tier->tiered_cache ->get_config ().cold_capacity ()));
2206+ if (n_evict > 0 ) {
2207+ if (tiered_cache->evict_from_slot (slot.id , n_evict)) {
2208+ SLT_INF (slot, " tiered cache eviction: %d tokens\n " , n_evict);
2209+ }
2210+ }
2211+ }
2212+ }
2213+
22132214 SLT_WRN (slot, " slot context shift, n_keep = %d, n_left = %d, n_discard = %d\n " , n_keep, n_left, n_discard);
22142215
22152216 llama_memory_seq_rm (llama_get_memory (ctx), slot.id , n_keep , n_keep + n_discard);
@@ -3532,33 +3533,13 @@ void server_routes::init_routes() {
35323533 };
35333534
35343535 // Add tiered cache metrics if enabled
3535- if (params.kv_tiered_enabled ) {
3536- auto global_stats = tiered_cache.get_global_stats ();
3537- all_metrics_def[" counter" ].push ({
3538- {" name" , " tier_evictions_total" },
3539- {" help" , " Total number of tier evictions" },
3540- {" value" , global_stats.total_evictions }
3541- });
3542- all_metrics_def[" counter" ].push ({
3543- {" name" , " tier_migrations_total" },
3544- {" help" , " Total number of tier migrations" },
3545- {" value" , global_stats.total_migrations }
3546- });
3547- all_metrics_def[" counter" ].push ({
3548- {" name" , " tier_cache_hits_total" },
3549- {" help" , " Total cache hits" },
3550- {" value" , global_stats.total_cache_hits }
3551- });
3552- all_metrics_def[" counter" ].push ({
3553- {" name" , " tier_cache_misses_total" },
3554- {" help" , " Total cache misses" },
3555- {" value" , global_stats.total_cache_misses }
3556- });
3557- all_metrics_def[" gauge" ].push ({
3558- {" name" , " tier_migration_latency_seconds" },
3559- {" help" , " Total migration latency in seconds" },
3560- {" value" , global_stats.total_migration_latency_us / 1e6 }
3561- });
3536+ if (params.kv_tiered_enabled && ctx_server.tiered_cache ) {
3537+ auto global_stats = ctx_server.tiered_cache ->get_global_stats ();
3538+ { json m; m[" name" ] = " tier_evictions_total" ; m[" help" ] = " Total number of tier evictions" ; m[" value" ] = global_stats.total_evictions ; all_metrics_def[" counter" ].push_back (m); }
3539+ { json m; m[" name" ] = " tier_migrations_total" ; m[" help" ] = " Total number of tier migrations" ; m[" value" ] = global_stats.total_migrations ; all_metrics_def[" counter" ].push_back (m); }
3540+ { json m; m[" name" ] = " tier_cache_hits_total" ; m[" help" ] = " Total cache hits" ; m[" value" ] = global_stats.total_cache_hits ; all_metrics_def[" counter" ].push_back (m); }
3541+ { json m; m[" name" ] = " tier_cache_misses_total" ; m[" help" ] = " Total cache misses" ; m[" value" ] = global_stats.total_cache_misses ; all_metrics_def[" counter" ].push_back (m); }
3542+ { json m; m[" name" ] = " tier_migration_latency_seconds" ; m[" help" ] = " Total migration latency in seconds" ; m[" value" ] = global_stats.total_migration_latency_us / 1e6 ; all_metrics_def[" gauge" ].push_back (m); }
35623543 }
35633544
35643545 std::stringstream prometheus;
0 commit comments