lmcache-cli 0.4.5.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lmcache/__init__.py +84 -0
- lmcache/_version.py +24 -0
- lmcache/cli/__init__.py +1 -0
- lmcache/cli/commands/__init__.py +34 -0
- lmcache/cli/commands/base.py +157 -0
- lmcache/cli/commands/bench/__init__.py +557 -0
- lmcache/cli/commands/bench/engine_bench/__init__.py +1 -0
- lmcache/cli/commands/bench/engine_bench/config.py +245 -0
- lmcache/cli/commands/bench/engine_bench/interactive/__init__.py +274 -0
- lmcache/cli/commands/bench/engine_bench/interactive/config.json +10 -0
- lmcache/cli/commands/bench/engine_bench/interactive/schema.py +352 -0
- lmcache/cli/commands/bench/engine_bench/interactive/state.py +327 -0
- lmcache/cli/commands/bench/engine_bench/interactive/terminal.py +291 -0
- lmcache/cli/commands/bench/engine_bench/progress.py +145 -0
- lmcache/cli/commands/bench/engine_bench/request_sender.py +232 -0
- lmcache/cli/commands/bench/engine_bench/stats.py +275 -0
- lmcache/cli/commands/bench/engine_bench/workloads/__init__.py +153 -0
- lmcache/cli/commands/bench/engine_bench/workloads/base.py +122 -0
- lmcache/cli/commands/bench/engine_bench/workloads/long_doc_permutator.py +435 -0
- lmcache/cli/commands/bench/engine_bench/workloads/long_doc_qa.py +281 -0
- lmcache/cli/commands/bench/engine_bench/workloads/multi_round_chat.py +337 -0
- lmcache/cli/commands/bench/engine_bench/workloads/random_prefill.py +178 -0
- lmcache/cli/commands/describe.py +310 -0
- lmcache/cli/commands/kvcache.py +133 -0
- lmcache/cli/commands/mock.py +75 -0
- lmcache/cli/commands/ping.py +113 -0
- lmcache/cli/commands/query/__init__.py +155 -0
- lmcache/cli/commands/query/prompt.py +134 -0
- lmcache/cli/commands/query/request.py +357 -0
- lmcache/cli/commands/server.py +99 -0
- lmcache/cli/commands/tool/__init__.py +63 -0
- lmcache/cli/commands/tool/cache_simulator.py +113 -0
- lmcache/cli/commands/trace/__init__.py +505 -0
- lmcache/cli/commands/trace/dispatch.py +249 -0
- lmcache/cli/commands/trace/driver.py +372 -0
- lmcache/cli/commands/trace/stats.py +289 -0
- lmcache/cli/documents/lmcache.txt +11 -0
- lmcache/cli/main.py +42 -0
- lmcache/cli/metrics/__init__.py +29 -0
- lmcache/cli/metrics/formatter.py +171 -0
- lmcache/cli/metrics/handler.py +94 -0
- lmcache/cli/metrics/metrics.py +161 -0
- lmcache/cli/metrics/section.py +77 -0
- lmcache/connections.py +173 -0
- lmcache/integration/__init__.py +2 -0
- lmcache/integration/base_service_factory.py +165 -0
- lmcache/integration/request_telemetry/__init__.py +1 -0
- lmcache/integration/request_telemetry/base.py +51 -0
- lmcache/integration/request_telemetry/factory.py +113 -0
- lmcache/integration/request_telemetry/fastapi.py +109 -0
- lmcache/integration/request_telemetry/noop.py +35 -0
- lmcache/integration/sglang/__init__.py +2 -0
- lmcache/integration/sglang/sglang_adapter.py +326 -0
- lmcache/integration/sglang/utils.py +39 -0
- lmcache/integration/vllm/__init__.py +1 -0
- lmcache/integration/vllm/lmcache_connector_v1.py +213 -0
- lmcache/integration/vllm/lmcache_connector_v1_085.py +150 -0
- lmcache/integration/vllm/lmcache_mp_connector_0180.py +1072 -0
- lmcache/integration/vllm/tests/test_mm_hash_utils.py +112 -0
- lmcache/integration/vllm/utils.py +433 -0
- lmcache/integration/vllm/vllm_multi_process_adapter.py +1090 -0
- lmcache/integration/vllm/vllm_service_factory.py +339 -0
- lmcache/integration/vllm/vllm_v1_adapter.py +1713 -0
- lmcache/logging.py +107 -0
- lmcache/native_storage_ops.pyi +230 -0
- lmcache/non_cuda_equivalents.py +1424 -0
- lmcache/observability.py +1958 -0
- lmcache/storage_backend/serde/__init__.py +1 -0
- lmcache/storage_backend/serde/cachegen_basics.py +210 -0
- lmcache/storage_backend/serde/cachegen_decoder.py +207 -0
- lmcache/storage_backend/serde/cachegen_encoder.py +394 -0
- lmcache/storage_backend/serde/serde.py +75 -0
- lmcache/tools/__init__.py +1 -0
- lmcache/tools/cache_simulator/README.md +392 -0
- lmcache/tools/cache_simulator/__init__.py +1 -0
- lmcache/tools/cache_simulator/docs/simulate_example.png +0 -0
- lmcache/tools/cache_simulator/docs/sweep_example.png +0 -0
- lmcache/tools/cache_simulator/gen_bench_dataset.py +360 -0
- lmcache/tools/cache_simulator/lru_cache.py +124 -0
- lmcache/tools/cache_simulator/plot_hit_rate.py +231 -0
- lmcache/tools/cache_simulator/simulator.py +795 -0
- lmcache/tools/controller_benchmark/README.md +161 -0
- lmcache/tools/controller_benchmark/__init__.py +1 -0
- lmcache/tools/controller_benchmark/__main__.py +331 -0
- lmcache/tools/controller_benchmark/benchmark.py +660 -0
- lmcache/tools/controller_benchmark/config.py +44 -0
- lmcache/tools/controller_benchmark/constants.py +10 -0
- lmcache/tools/controller_benchmark/handlers/__init__.py +46 -0
- lmcache/tools/controller_benchmark/handlers/admit.py +52 -0
- lmcache/tools/controller_benchmark/handlers/base.py +47 -0
- lmcache/tools/controller_benchmark/handlers/deregister.py +49 -0
- lmcache/tools/controller_benchmark/handlers/evict.py +52 -0
- lmcache/tools/controller_benchmark/handlers/heartbeat.py +56 -0
- lmcache/tools/controller_benchmark/handlers/p2p_lookup.py +47 -0
- lmcache/tools/controller_benchmark/handlers/register.py +56 -0
- lmcache/tools/mp_status_viewer/__init__.py +1 -0
- lmcache/tools/mp_status_viewer/__main__.py +95 -0
- lmcache/usage_context.py +417 -0
- lmcache/utils.py +665 -0
- lmcache/v1/__init__.py +2 -0
- lmcache/v1/api_server/__init__.py +2 -0
- lmcache/v1/api_server/__main__.py +537 -0
- lmcache/v1/basic_check.py +112 -0
- lmcache/v1/cache_controller/__init__.py +9 -0
- lmcache/v1/cache_controller/commands/__init__.py +15 -0
- lmcache/v1/cache_controller/commands/base.py +35 -0
- lmcache/v1/cache_controller/commands/full_sync.py +49 -0
- lmcache/v1/cache_controller/config.py +176 -0
- lmcache/v1/cache_controller/controller_manager.py +535 -0
- lmcache/v1/cache_controller/controllers/__init__.py +11 -0
- lmcache/v1/cache_controller/controllers/full_sync_tracker.py +473 -0
- lmcache/v1/cache_controller/controllers/kv_controller.py +439 -0
- lmcache/v1/cache_controller/controllers/registration_controller.py +282 -0
- lmcache/v1/cache_controller/executor.py +463 -0
- lmcache/v1/cache_controller/frontend/static/css/style.css +201 -0
- lmcache/v1/cache_controller/frontend/static/img/logo.png +0 -0
- lmcache/v1/cache_controller/frontend/static/index.html +234 -0
- lmcache/v1/cache_controller/frontend/static/js/controller_app.js +660 -0
- lmcache/v1/cache_controller/full_sync_sender.py +475 -0
- lmcache/v1/cache_controller/locks.py +149 -0
- lmcache/v1/cache_controller/message.py +828 -0
- lmcache/v1/cache_controller/observability.py +208 -0
- lmcache/v1/cache_controller/utils.py +679 -0
- lmcache/v1/cache_controller/worker.py +665 -0
- lmcache/v1/cache_engine.py +2058 -0
- lmcache/v1/cache_interface.py +19 -0
- lmcache/v1/check/__init__.py +74 -0
- lmcache/v1/check/check_mode_gen.py +86 -0
- lmcache/v1/check/check_mode_test_l2_adapter.py +284 -0
- lmcache/v1/check/check_mode_test_remote.py +155 -0
- lmcache/v1/check/check_mode_test_storage_manager.py +142 -0
- lmcache/v1/check/utils.py +571 -0
- lmcache/v1/compute/__init__.py +2 -0
- lmcache/v1/compute/attention/__init__.py +0 -0
- lmcache/v1/compute/attention/abstract.py +39 -0
- lmcache/v1/compute/attention/flash_attn.py +129 -0
- lmcache/v1/compute/attention/flash_infer_sparse.py +284 -0
- lmcache/v1/compute/attention/metadata.py +85 -0
- lmcache/v1/compute/attention/utils.py +14 -0
- lmcache/v1/compute/blend/__init__.py +7 -0
- lmcache/v1/compute/blend/blender.py +168 -0
- lmcache/v1/compute/blend/metadata.py +34 -0
- lmcache/v1/compute/blend/utils.py +63 -0
- lmcache/v1/compute/models/__init__.py +0 -0
- lmcache/v1/compute/models/base.py +141 -0
- lmcache/v1/compute/models/llama.py +9 -0
- lmcache/v1/compute/models/qwen3.py +24 -0
- lmcache/v1/compute/models/utils.py +68 -0
- lmcache/v1/compute/positional_encoding.py +199 -0
- lmcache/v1/config.py +848 -0
- lmcache/v1/config_base.py +848 -0
- lmcache/v1/distributed/api.py +248 -0
- lmcache/v1/distributed/config.py +321 -0
- lmcache/v1/distributed/error.py +64 -0
- lmcache/v1/distributed/eviction.py +192 -0
- lmcache/v1/distributed/eviction_policy/__init__.py +21 -0
- lmcache/v1/distributed/eviction_policy/factory.py +27 -0
- lmcache/v1/distributed/eviction_policy/lru.py +244 -0
- lmcache/v1/distributed/eviction_policy/noop.py +50 -0
- lmcache/v1/distributed/internal_api.py +170 -0
- lmcache/v1/distributed/l1_manager.py +835 -0
- lmcache/v1/distributed/l2_adapters/__init__.py +67 -0
- lmcache/v1/distributed/l2_adapters/base.py +360 -0
- lmcache/v1/distributed/l2_adapters/config.py +385 -0
- lmcache/v1/distributed/l2_adapters/factory.py +205 -0
- lmcache/v1/distributed/l2_adapters/fs_l2_adapter.py +747 -0
- lmcache/v1/distributed/l2_adapters/fs_native_l2_adapter.py +167 -0
- lmcache/v1/distributed/l2_adapters/mock_l2_adapter.py +516 -0
- lmcache/v1/distributed/l2_adapters/mooncake_store_l2_adapter.py +135 -0
- lmcache/v1/distributed/l2_adapters/native_connector_l2_adapter.py +468 -0
- lmcache/v1/distributed/l2_adapters/native_plugin_l2_adapter.py +199 -0
- lmcache/v1/distributed/l2_adapters/nixl_store_dynamic_l2_adapter.py +831 -0
- lmcache/v1/distributed/l2_adapters/nixl_store_l2_adapter.py +983 -0
- lmcache/v1/distributed/l2_adapters/plugin_l2_adapter.py +210 -0
- lmcache/v1/distributed/l2_adapters/resp_l2_adapter.py +176 -0
- lmcache/v1/distributed/memory_manager.py +179 -0
- lmcache/v1/distributed/storage_controller.py +39 -0
- lmcache/v1/distributed/storage_controllers/__init__.py +43 -0
- lmcache/v1/distributed/storage_controllers/eviction_controller.py +242 -0
- lmcache/v1/distributed/storage_controllers/prefetch_controller.py +830 -0
- lmcache/v1/distributed/storage_controllers/prefetch_policy.py +193 -0
- lmcache/v1/distributed/storage_controllers/store_controller.py +452 -0
- lmcache/v1/distributed/storage_controllers/store_policy.py +213 -0
- lmcache/v1/distributed/storage_manager.py +532 -0
- lmcache/v1/event_manager.py +145 -0
- lmcache/v1/exceptions/__init__.py +16 -0
- lmcache/v1/gpu_connector/__init__.py +126 -0
- lmcache/v1/gpu_connector/gpu_connectors.py +1906 -0
- lmcache/v1/gpu_connector/gpu_ops.py +85 -0
- lmcache/v1/gpu_connector/hpu_connector.py +326 -0
- lmcache/v1/gpu_connector/mock_gpu_connector.py +67 -0
- lmcache/v1/gpu_connector/utils.py +890 -0
- lmcache/v1/gpu_connector/xpu_connectors.py +916 -0
- lmcache/v1/health_monitor/__init__.py +1 -0
- lmcache/v1/health_monitor/base.py +587 -0
- lmcache/v1/health_monitor/checks/__init__.py +1 -0
- lmcache/v1/health_monitor/checks/remote_backend_check.py +304 -0
- lmcache/v1/health_monitor/constants.py +36 -0
- lmcache/v1/internal_api_server/__init__.py +0 -0
- lmcache/v1/internal_api_server/api_registry.py +59 -0
- lmcache/v1/internal_api_server/api_server.py +120 -0
- lmcache/v1/internal_api_server/common/__init__.py +1 -0
- lmcache/v1/internal_api_server/common/env_api.py +22 -0
- lmcache/v1/internal_api_server/common/loglevel_api.py +57 -0
- lmcache/v1/internal_api_server/common/metrics_api.py +29 -0
- lmcache/v1/internal_api_server/common/periodic_thread_api.py +138 -0
- lmcache/v1/internal_api_server/common/run_script_api.py +73 -0
- lmcache/v1/internal_api_server/common/thread_api.py +63 -0
- lmcache/v1/internal_api_server/controller/__init__.py +1 -0
- lmcache/v1/internal_api_server/controller/key_stats_api.py +81 -0
- lmcache/v1/internal_api_server/controller/worker_info_api.py +136 -0
- lmcache/v1/internal_api_server/utils.py +43 -0
- lmcache/v1/internal_api_server/vllm/__init__.py +1 -0
- lmcache/v1/internal_api_server/vllm/backend_api.py +221 -0
- lmcache/v1/internal_api_server/vllm/bypass_api.py +204 -0
- lmcache/v1/internal_api_server/vllm/cache_api.py +895 -0
- lmcache/v1/internal_api_server/vllm/chunk_statistics_api.py +141 -0
- lmcache/v1/internal_api_server/vllm/conf_api.py +147 -0
- lmcache/v1/internal_api_server/vllm/freeze_api.py +172 -0
- lmcache/v1/internal_api_server/vllm/hot_cache_api.py +184 -0
- lmcache/v1/internal_api_server/vllm/inference_api.py +65 -0
- lmcache/v1/internal_api_server/vllm/load_fs_chunks_api.py +320 -0
- lmcache/v1/internal_api_server/vllm/lookup_api.py +145 -0
- lmcache/v1/internal_api_server/vllm/version_api.py +25 -0
- lmcache/v1/kv_layer_groups.py +267 -0
- lmcache/v1/lazy_memory_allocator.py +284 -0
- lmcache/v1/lookup_client/__init__.py +25 -0
- lmcache/v1/lookup_client/abstract_client.py +77 -0
- lmcache/v1/lookup_client/async_lookup_message.py +50 -0
- lmcache/v1/lookup_client/chunk_statistics_lookup_client.py +200 -0
- lmcache/v1/lookup_client/factory.py +251 -0
- lmcache/v1/lookup_client/hit_limit_lookup_client.py +86 -0
- lmcache/v1/lookup_client/lmcache_async_lookup_client.py +407 -0
- lmcache/v1/lookup_client/lmcache_lookup_client.py +285 -0
- lmcache/v1/lookup_client/lmcache_lookup_client_bypass.py +99 -0
- lmcache/v1/lookup_client/mooncake_lookup_client.py +87 -0
- lmcache/v1/lookup_client/record_strategies/__init__.py +77 -0
- lmcache/v1/lookup_client/record_strategies/base.py +327 -0
- lmcache/v1/lookup_client/record_strategies/file_hash.py +130 -0
- lmcache/v1/lookup_client/record_strategies/memory_bloom_filter.py +81 -0
- lmcache/v1/manager.py +539 -0
- lmcache/v1/memory_management.py +2619 -0
- lmcache/v1/metadata.py +114 -0
- lmcache/v1/mp_observability/AGENTS.override.md +21 -0
- lmcache/v1/mp_observability/README.md +204 -0
- lmcache/v1/mp_observability/config.py +340 -0
- lmcache/v1/mp_observability/event.py +100 -0
- lmcache/v1/mp_observability/event_bus.py +313 -0
- lmcache/v1/mp_observability/otel_init.py +145 -0
- lmcache/v1/mp_observability/subscribers/__init__.py +28 -0
- lmcache/v1/mp_observability/subscribers/logging/__init__.py +19 -0
- lmcache/v1/mp_observability/subscribers/logging/l1.py +56 -0
- lmcache/v1/mp_observability/subscribers/logging/l2.py +73 -0
- lmcache/v1/mp_observability/subscribers/logging/lookup_hash.py +209 -0
- lmcache/v1/mp_observability/subscribers/logging/mp_server.py +90 -0
- lmcache/v1/mp_observability/subscribers/logging/sm.py +59 -0
- lmcache/v1/mp_observability/subscribers/metrics/__init__.py +20 -0
- lmcache/v1/mp_observability/subscribers/metrics/l0_lifecycle.py +290 -0
- lmcache/v1/mp_observability/subscribers/metrics/l1.py +55 -0
- lmcache/v1/mp_observability/subscribers/metrics/l1_lifecycle.py +166 -0
- lmcache/v1/mp_observability/subscribers/metrics/l2.py +121 -0
- lmcache/v1/mp_observability/subscribers/metrics/sm.py +69 -0
- lmcache/v1/mp_observability/subscribers/tracing/__init__.py +12 -0
- lmcache/v1/mp_observability/subscribers/tracing/mp_server.py +333 -0
- lmcache/v1/mp_observability/subscribers/tracing/span_registry.py +148 -0
- lmcache/v1/mp_observability/trace/__init__.py +50 -0
- lmcache/v1/mp_observability/trace/codecs.py +255 -0
- lmcache/v1/mp_observability/trace/decorator.py +147 -0
- lmcache/v1/mp_observability/trace/format.py +132 -0
- lmcache/v1/mp_observability/trace/lifecycle.py +83 -0
- lmcache/v1/mp_observability/trace/reader.py +167 -0
- lmcache/v1/mp_observability/trace/recorder.py +300 -0
- lmcache/v1/multiprocess/__init__.py +0 -0
- lmcache/v1/multiprocess/affinity_pool.py +102 -0
- lmcache/v1/multiprocess/blend_server_v2.py +891 -0
- lmcache/v1/multiprocess/config.py +253 -0
- lmcache/v1/multiprocess/custom_types.py +281 -0
- lmcache/v1/multiprocess/futures.py +194 -0
- lmcache/v1/multiprocess/gpu_context.py +511 -0
- lmcache/v1/multiprocess/http_server.py +235 -0
- lmcache/v1/multiprocess/mp_runtime_plugin_launcher.py +130 -0
- lmcache/v1/multiprocess/mq.py +732 -0
- lmcache/v1/multiprocess/protocol.py +86 -0
- lmcache/v1/multiprocess/protocols/README.md +213 -0
- lmcache/v1/multiprocess/protocols/__init__.py +127 -0
- lmcache/v1/multiprocess/protocols/base.py +89 -0
- lmcache/v1/multiprocess/protocols/blend.py +109 -0
- lmcache/v1/multiprocess/protocols/blend_v2.py +57 -0
- lmcache/v1/multiprocess/protocols/controller.py +53 -0
- lmcache/v1/multiprocess/protocols/debug.py +34 -0
- lmcache/v1/multiprocess/protocols/engine.py +146 -0
- lmcache/v1/multiprocess/protocols/observability.py +39 -0
- lmcache/v1/multiprocess/server.py +1134 -0
- lmcache/v1/multiprocess/session.py +190 -0
- lmcache/v1/multiprocess/token_hasher.py +441 -0
- lmcache/v1/offload_server/__init__.py +17 -0
- lmcache/v1/offload_server/abstract_server.py +37 -0
- lmcache/v1/offload_server/message.py +30 -0
- lmcache/v1/offload_server/zmq_server.py +122 -0
- lmcache/v1/periodic_thread.py +579 -0
- lmcache/v1/pin_monitor.py +246 -0
- lmcache/v1/plugin/__init__.py +0 -0
- lmcache/v1/plugin/runtime_plugin_launcher.py +211 -0
- lmcache/v1/protocol.py +317 -0
- lmcache/v1/rpc/__init__.py +17 -0
- lmcache/v1/rpc/transport.py +105 -0
- lmcache/v1/rpc/zmq_transport.py +213 -0
- lmcache/v1/rpc_utils.py +165 -0
- lmcache/v1/server/__init__.py +2 -0
- lmcache/v1/server/__main__.py +170 -0
- lmcache/v1/server/storage_backend/__init__.py +21 -0
- lmcache/v1/server/storage_backend/abstract_backend.py +80 -0
- lmcache/v1/server/storage_backend/local_backend.py +75 -0
- lmcache/v1/server/utils.py +21 -0
- lmcache/v1/standalone/__init__.py +1 -0
- lmcache/v1/standalone/__main__.py +583 -0
- lmcache/v1/standalone/manager.py +80 -0
- lmcache/v1/standalone/standalone_service_factory.py +86 -0
- lmcache/v1/storage_backend/__init__.py +313 -0
- lmcache/v1/storage_backend/abstract_backend.py +445 -0
- lmcache/v1/storage_backend/audit_backend.py +233 -0
- lmcache/v1/storage_backend/batched_message_sender.py +222 -0
- lmcache/v1/storage_backend/cache_policy/__init__.py +45 -0
- lmcache/v1/storage_backend/cache_policy/base_policy.py +87 -0
- lmcache/v1/storage_backend/cache_policy/fifo.py +58 -0
- lmcache/v1/storage_backend/cache_policy/lfu.py +105 -0
- lmcache/v1/storage_backend/cache_policy/lru.py +81 -0
- lmcache/v1/storage_backend/cache_policy/mru.py +61 -0
- lmcache/v1/storage_backend/connector/__init__.py +443 -0
- lmcache/v1/storage_backend/connector/audit_adapter.py +77 -0
- lmcache/v1/storage_backend/connector/audit_connector.py +320 -0
- lmcache/v1/storage_backend/connector/base_connector.py +379 -0
- lmcache/v1/storage_backend/connector/blackhole_adapter.py +21 -0
- lmcache/v1/storage_backend/connector/blackhole_connector.py +37 -0
- lmcache/v1/storage_backend/connector/eic_adapter.py +31 -0
- lmcache/v1/storage_backend/connector/eic_connector.py +757 -0
- lmcache/v1/storage_backend/connector/external_adapter.py +79 -0
- lmcache/v1/storage_backend/connector/fs_adapter.py +51 -0
- lmcache/v1/storage_backend/connector/fs_connector.py +403 -0
- lmcache/v1/storage_backend/connector/infinistore_adapter.py +56 -0
- lmcache/v1/storage_backend/connector/infinistore_connector.py +177 -0
- lmcache/v1/storage_backend/connector/instrumented_connector.py +219 -0
- lmcache/v1/storage_backend/connector/lm_adapter.py +31 -0
- lmcache/v1/storage_backend/connector/lm_connector.py +176 -0
- lmcache/v1/storage_backend/connector/mock_adapter.py +57 -0
- lmcache/v1/storage_backend/connector/mock_connector.py +349 -0
- lmcache/v1/storage_backend/connector/mooncakestore_adapter.py +43 -0
- lmcache/v1/storage_backend/connector/mooncakestore_connector.py +614 -0
- lmcache/v1/storage_backend/connector/redis_adapter.py +181 -0
- lmcache/v1/storage_backend/connector/redis_connector.py +828 -0
- lmcache/v1/storage_backend/connector/s3_adapter.py +59 -0
- lmcache/v1/storage_backend/connector/s3_connector.py +699 -0
- lmcache/v1/storage_backend/connector/sagemaker_hyperpod_adapter.py +233 -0
- lmcache/v1/storage_backend/connector/sagemaker_hyperpod_connector.py +987 -0
- lmcache/v1/storage_backend/connector/valkey_adapter.py +114 -0
- lmcache/v1/storage_backend/connector/valkey_connector.py +627 -0
- lmcache/v1/storage_backend/gds_backend.py +1199 -0
- lmcache/v1/storage_backend/job_executor/__init__.py +0 -0
- lmcache/v1/storage_backend/job_executor/base_executor.py +34 -0
- lmcache/v1/storage_backend/job_executor/pq_executor.py +235 -0
- lmcache/v1/storage_backend/local_cpu_backend.py +810 -0
- lmcache/v1/storage_backend/local_disk_backend.py +656 -0
- lmcache/v1/storage_backend/maru_backend.py +734 -0
- lmcache/v1/storage_backend/naive_serde/__init__.py +50 -0
- lmcache/v1/storage_backend/naive_serde/cachegen_basics.py +133 -0
- lmcache/v1/storage_backend/naive_serde/cachegen_decoder.py +135 -0
- lmcache/v1/storage_backend/naive_serde/cachegen_encoder.py +83 -0
- lmcache/v1/storage_backend/naive_serde/kivi_serde.py +22 -0
- lmcache/v1/storage_backend/naive_serde/naive_serde.py +18 -0
- lmcache/v1/storage_backend/naive_serde/serde.py +37 -0
- lmcache/v1/storage_backend/native_clients/connector_client_base.py +165 -0
- lmcache/v1/storage_backend/native_clients/resp_client.py +35 -0
- lmcache/v1/storage_backend/nixl_storage_backend.py +1400 -0
- lmcache/v1/storage_backend/p2p_backend.py +788 -0
- lmcache/v1/storage_backend/path_sharder.py +117 -0
- lmcache/v1/storage_backend/pd_backend.py +646 -0
- lmcache/v1/storage_backend/plugins/dax_backend.py +1443 -0
- lmcache/v1/storage_backend/plugins/rust_raw_block_backend.py +1361 -0
- lmcache/v1/storage_backend/remote_backend.py +624 -0
- lmcache/v1/storage_backend/resp_client.py +227 -0
- lmcache/v1/storage_backend/storage_backend_listener.py +19 -0
- lmcache/v1/storage_backend/storage_manager.py +1352 -0
- lmcache/v1/system_detection.py +110 -0
- lmcache/v1/token_database.py +551 -0
- lmcache/v1/transfer_channel/__init__.py +83 -0
- lmcache/v1/transfer_channel/abstract.py +285 -0
- lmcache/v1/transfer_channel/mock_memory_channel.py +156 -0
- lmcache/v1/transfer_channel/nixl_channel.py +639 -0
- lmcache/v1/transfer_channel/py_socket_channel.py +260 -0
- lmcache/v1/transfer_channel/transfer_utils.py +63 -0
- lmcache/v1/utils/__init__.py +1 -0
- lmcache/v1/utils/bloom_filter.py +109 -0
- lmcache/v1/utils/cache_utils.py +125 -0
- lmcache_cli-0.4.5.dev0.dist-info/METADATA +185 -0
- lmcache_cli-0.4.5.dev0.dist-info/RECORD +399 -0
- lmcache_cli-0.4.5.dev0.dist-info/WHEEL +5 -0
- lmcache_cli-0.4.5.dev0.dist-info/entry_points.txt +2 -0
- lmcache_cli-0.4.5.dev0.dist-info/licenses/LICENSE +201 -0
- lmcache_cli-0.4.5.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Standard
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import List, Optional
|
|
5
|
+
|
|
6
|
+
# Third Party
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class LMCBlendCommonMetadata:
|
|
12
|
+
"""
|
|
13
|
+
CommonMetadata (fixed hyperparams) for blending operations in LMCache.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
check_layers: List[int]
|
|
17
|
+
recomp_ratios: Optional[List[float]] = None
|
|
18
|
+
thresholds: Optional[List[float]] = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class LMCBlendMetadata:
|
|
23
|
+
"""
|
|
24
|
+
Metadata (determined during runtime) for blending operations in LMCache.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
imp_indices: Optional[torch.Tensor] = None
|
|
28
|
+
attn_mask: Optional[torch.Tensor] = None
|
|
29
|
+
positions: Optional[torch.Tensor] = None
|
|
30
|
+
|
|
31
|
+
def clean(self):
|
|
32
|
+
self.imp_indices = None
|
|
33
|
+
self.attn_mask = None
|
|
34
|
+
self.positions = None
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Standard
|
|
3
|
+
from typing import TYPE_CHECKING, Dict
|
|
4
|
+
|
|
5
|
+
# Third Party
|
|
6
|
+
from torch import nn
|
|
7
|
+
|
|
8
|
+
# First Party
|
|
9
|
+
from lmcache.logging import init_logger
|
|
10
|
+
from lmcache.v1.compute.blend.blender import LMCBlender
|
|
11
|
+
from lmcache.v1.compute.models.utils import VLLMModelTracker
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
# First Party
|
|
15
|
+
from lmcache.v1.cache_engine import LMCacheEngine
|
|
16
|
+
from lmcache.v1.config import LMCacheEngineConfig
|
|
17
|
+
from lmcache.v1.gpu_connector import GPUConnectorInterface
|
|
18
|
+
|
|
19
|
+
logger = init_logger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class LMCBlenderBuilder:
|
|
23
|
+
_blenders: Dict[str, LMCBlender] = {}
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def get_or_create(
|
|
27
|
+
cls,
|
|
28
|
+
instance_id: str,
|
|
29
|
+
cache_engine: "LMCacheEngine",
|
|
30
|
+
gpu_connector: "GPUConnectorInterface",
|
|
31
|
+
config: "LMCacheEngineConfig",
|
|
32
|
+
):
|
|
33
|
+
"""
|
|
34
|
+
Get or create a blender for the given instance_id.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
if instance_id not in cls._blenders:
|
|
38
|
+
logger.info(f"Creating blender for {instance_id}")
|
|
39
|
+
vllm_model = VLLMModelTracker.get_model(instance_id)
|
|
40
|
+
blender = LMCBlender(
|
|
41
|
+
cache_engine=cache_engine,
|
|
42
|
+
gpu_connector=gpu_connector,
|
|
43
|
+
vllm_model=vllm_model,
|
|
44
|
+
config=config,
|
|
45
|
+
)
|
|
46
|
+
cls._blenders[instance_id] = blender
|
|
47
|
+
else:
|
|
48
|
+
logger.info(
|
|
49
|
+
f"Blender for {instance_id} already exists, returning the original one."
|
|
50
|
+
)
|
|
51
|
+
return cls._blenders[instance_id]
|
|
52
|
+
|
|
53
|
+
@classmethod
|
|
54
|
+
def get(
|
|
55
|
+
cls,
|
|
56
|
+
instance_id: str,
|
|
57
|
+
) -> nn.Module:
|
|
58
|
+
"""
|
|
59
|
+
Get the blender by instance_id.
|
|
60
|
+
"""
|
|
61
|
+
if instance_id not in cls._blenders:
|
|
62
|
+
raise ValueError(f"Blender for {instance_id} not found.")
|
|
63
|
+
return cls._blenders[instance_id]
|
|
File without changes
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Standard
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
# Third Party
|
|
6
|
+
from torch import nn
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
# First Party
|
|
10
|
+
from lmcache.v1.compute.attention.utils import infer_attn_backend_from_vllm
|
|
11
|
+
from lmcache.v1.compute.positional_encoding import get_fused_rope
|
|
12
|
+
|
|
13
|
+
# TODO(Jiayi): A few things need to be tested/supported:
|
|
14
|
+
# TP, PP, Multimodal
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class LMCBaseModel(nn.Module, ABC):
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
vllm_model,
|
|
21
|
+
blender,
|
|
22
|
+
enable_sparse: bool = False,
|
|
23
|
+
):
|
|
24
|
+
super().__init__()
|
|
25
|
+
self.vllm_model = vllm_model
|
|
26
|
+
|
|
27
|
+
self.num_layers = len(vllm_model.model.layers)
|
|
28
|
+
|
|
29
|
+
self.vllm_attn_layers = []
|
|
30
|
+
self.lmc_attn_layers = []
|
|
31
|
+
for i in range(self.num_layers):
|
|
32
|
+
vllm_attn = vllm_model.model.layers[i].self_attn.attn
|
|
33
|
+
self.vllm_attn_layers.append(vllm_attn)
|
|
34
|
+
|
|
35
|
+
self.lmc_attn_layers.append(
|
|
36
|
+
infer_attn_backend_from_vllm(vllm_attn, enable_sparse)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# NOTE(Jiayi): better not to pass the blender in init
|
|
40
|
+
# if we want to make this LMCModel more general.
|
|
41
|
+
self.blender = blender
|
|
42
|
+
|
|
43
|
+
# remove hard code
|
|
44
|
+
rotary_emb = vllm_model.model.layers[0].self_attn.rotary_emb
|
|
45
|
+
head_dim = rotary_emb.head_size
|
|
46
|
+
max_position_embeddings = rotary_emb.max_position_embeddings
|
|
47
|
+
rope_scaling = None
|
|
48
|
+
base = rotary_emb.base
|
|
49
|
+
is_neox_style = rotary_emb.is_neox_style
|
|
50
|
+
dtype = rotary_emb.dtype
|
|
51
|
+
self.fused_rotary_emb = get_fused_rope(
|
|
52
|
+
head_dim,
|
|
53
|
+
rotary_dim=head_dim,
|
|
54
|
+
max_position=max_position_embeddings,
|
|
55
|
+
base=base,
|
|
56
|
+
rope_scaling=rope_scaling,
|
|
57
|
+
is_neox_style=is_neox_style,
|
|
58
|
+
dtype=dtype,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def _process_qkv(self, q, k, v, layer):
|
|
63
|
+
"""Process QKV tensors. Model-specific implementation."""
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
@torch.compile
|
|
67
|
+
def compute_layer(
|
|
68
|
+
self,
|
|
69
|
+
input_ids: torch.Tensor,
|
|
70
|
+
):
|
|
71
|
+
input_ids = input_ids.cuda()
|
|
72
|
+
hidden_states = self.vllm_model.get_input_embeddings(input_ids)
|
|
73
|
+
residual = None
|
|
74
|
+
|
|
75
|
+
attn_output = None
|
|
76
|
+
|
|
77
|
+
# TODO(Jiayi): Need to build `attn_metadata` more elegantly.
|
|
78
|
+
attn_metadata = self.lmc_attn_layers[0].init_attn_metadata(
|
|
79
|
+
input_ids=input_ids,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
for idx, layer in enumerate(
|
|
83
|
+
self.vllm_model.model.layers[
|
|
84
|
+
self.vllm_model.model.start_layer : self.vllm_model.model.end_layer
|
|
85
|
+
]
|
|
86
|
+
):
|
|
87
|
+
# TODO(Jiayi) The last layer doesn't have to be computed
|
|
88
|
+
# hidden_states, residual = layer(positions, hidden_states, residual)
|
|
89
|
+
|
|
90
|
+
# Self Attention
|
|
91
|
+
if residual is None:
|
|
92
|
+
residual = hidden_states
|
|
93
|
+
hidden_states = layer.input_layernorm(hidden_states)
|
|
94
|
+
else:
|
|
95
|
+
hidden_states, residual = layer.input_layernorm(hidden_states, residual)
|
|
96
|
+
# hidden_states = self.self_attn(positions=positions,
|
|
97
|
+
# hidden_states=hidden_states)
|
|
98
|
+
|
|
99
|
+
qkv, _ = layer.self_attn.qkv_proj(hidden_states)
|
|
100
|
+
q, k, v = qkv.split(
|
|
101
|
+
[
|
|
102
|
+
layer.self_attn.q_size,
|
|
103
|
+
layer.self_attn.kv_size,
|
|
104
|
+
layer.self_attn.kv_size,
|
|
105
|
+
],
|
|
106
|
+
dim=-1,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# Model-specific QKV processing
|
|
110
|
+
q, k, v = self._process_qkv(q, k, v, layer)
|
|
111
|
+
|
|
112
|
+
q, k, v, residual, attn_output, attn_metadata = self.blender.process_qkv(
|
|
113
|
+
q, k, v, residual, idx, attn_output, attn_metadata
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
num_heads = self.vllm_attn_layers[idx].num_heads
|
|
117
|
+
num_kv_heads = self.vllm_attn_layers[idx].num_kv_heads
|
|
118
|
+
head_size = self.vllm_attn_layers[idx].head_size
|
|
119
|
+
|
|
120
|
+
q = q.view(-1, num_heads, head_size)
|
|
121
|
+
k = k.view(-1, num_kv_heads, head_size)
|
|
122
|
+
v = v.view(-1, num_kv_heads, head_size)
|
|
123
|
+
attn_output = attn_output.view(-1, num_heads, head_size)
|
|
124
|
+
|
|
125
|
+
attn_output = self.lmc_attn_layers[idx].forward_contiguous(
|
|
126
|
+
q, k, v, attn_output, attn_metadata
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
attn_output = attn_output.view(-1, num_heads * head_size)
|
|
130
|
+
k = k.view(-1, num_kv_heads * head_size)
|
|
131
|
+
v = v.view(-1, num_kv_heads * head_size)
|
|
132
|
+
|
|
133
|
+
hidden_states, _ = layer.self_attn.o_proj(attn_output)
|
|
134
|
+
|
|
135
|
+
# Fully Connected
|
|
136
|
+
hidden_states, residual = layer.post_attention_layernorm(
|
|
137
|
+
hidden_states, residual
|
|
138
|
+
)
|
|
139
|
+
hidden_states = layer.mlp(hidden_states)
|
|
140
|
+
|
|
141
|
+
yield
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# First Party
|
|
3
|
+
from lmcache.v1.compute.models.base import LMCBaseModel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LMCLlamaModel(LMCBaseModel):
|
|
7
|
+
def _process_qkv(self, q, k, v, layer):
|
|
8
|
+
"""Process QKV tensors for LLaMa model (no additional processing)."""
|
|
9
|
+
return q, k, v
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# First Party
|
|
3
|
+
from lmcache.v1.compute.models.base import LMCBaseModel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LMCQwen3Model(LMCBaseModel):
|
|
7
|
+
def _process_qkv(self, q, k, v, layer):
|
|
8
|
+
"""Process QKV tensors for Qwen3 model with q_norm and k_norm layers."""
|
|
9
|
+
# Qwen3 has q_norm and k_norm layers
|
|
10
|
+
q_by_head = q.view(
|
|
11
|
+
*q.shape[:-1],
|
|
12
|
+
q.shape[-1] // layer.self_attn.head_dim,
|
|
13
|
+
layer.self_attn.head_dim,
|
|
14
|
+
)
|
|
15
|
+
q_by_head = layer.self_attn.q_norm(q_by_head)
|
|
16
|
+
q = q_by_head.view(q.shape)
|
|
17
|
+
k_by_head = k.view(
|
|
18
|
+
*k.shape[:-1],
|
|
19
|
+
k.shape[-1] // layer.self_attn.head_dim,
|
|
20
|
+
layer.self_attn.head_dim,
|
|
21
|
+
)
|
|
22
|
+
k_by_head = layer.self_attn.k_norm(k_by_head)
|
|
23
|
+
k = k_by_head.view(k.shape)
|
|
24
|
+
return q, k, v
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Standard
|
|
3
|
+
from typing import Dict
|
|
4
|
+
|
|
5
|
+
# Third Party
|
|
6
|
+
from torch import nn
|
|
7
|
+
|
|
8
|
+
# First Party
|
|
9
|
+
from lmcache.logging import init_logger
|
|
10
|
+
|
|
11
|
+
logger = init_logger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def infer_model_from_vllm(vllm_model, blender, enable_sparse: bool = False):
|
|
15
|
+
model_name = type(vllm_model).__name__
|
|
16
|
+
if model_name == "LlamaForCausalLM":
|
|
17
|
+
# First Party
|
|
18
|
+
from lmcache.v1.compute.models.llama import LMCLlamaModel
|
|
19
|
+
|
|
20
|
+
return LMCLlamaModel(vllm_model, blender, enable_sparse)
|
|
21
|
+
elif model_name == "Qwen2ForCausalLM":
|
|
22
|
+
# First Party
|
|
23
|
+
from lmcache.v1.compute.models.llama import LMCLlamaModel
|
|
24
|
+
|
|
25
|
+
return LMCLlamaModel(vllm_model, blender, enable_sparse)
|
|
26
|
+
elif model_name == "Qwen3ForCausalLM":
|
|
27
|
+
# First Party
|
|
28
|
+
from lmcache.v1.compute.models.qwen3 import LMCQwen3Model
|
|
29
|
+
|
|
30
|
+
return LMCQwen3Model(vllm_model, blender, enable_sparse)
|
|
31
|
+
else:
|
|
32
|
+
# TODO(Jiayi): Add support for more models
|
|
33
|
+
raise NotImplementedError(
|
|
34
|
+
f"Model type {model_name} is not supported in LMCache."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class VLLMModelTracker:
|
|
39
|
+
_vllm_models: Dict[str, nn.Module] = {}
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def register_model(
|
|
43
|
+
cls,
|
|
44
|
+
instance_id: str,
|
|
45
|
+
vllm_model: nn.Module,
|
|
46
|
+
):
|
|
47
|
+
"""
|
|
48
|
+
Register a vllm model by instance_id.
|
|
49
|
+
"""
|
|
50
|
+
logger.info(f"Registering vllm model for {instance_id}")
|
|
51
|
+
if instance_id not in cls._vllm_models:
|
|
52
|
+
cls._vllm_models[instance_id] = vllm_model
|
|
53
|
+
else:
|
|
54
|
+
logger.warning(
|
|
55
|
+
f"vllm model for {instance_id} already registered, doing nothing."
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
@classmethod
|
|
59
|
+
def get_model(
|
|
60
|
+
cls,
|
|
61
|
+
instance_id: str,
|
|
62
|
+
) -> nn.Module:
|
|
63
|
+
"""
|
|
64
|
+
Get the vllm model by instance_id.
|
|
65
|
+
"""
|
|
66
|
+
if instance_id not in cls._vllm_models:
|
|
67
|
+
raise ValueError(f"vllm model for {instance_id} not found.")
|
|
68
|
+
return cls._vllm_models[instance_id]
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Standard
|
|
3
|
+
from typing import Any, Callable, Dict, Optional
|
|
4
|
+
|
|
5
|
+
# Third Party
|
|
6
|
+
from vllm.model_executor.layers.rotary_embedding import get_rope as vllm_get_rope
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
# First Party
|
|
10
|
+
from lmcache.logging import init_logger
|
|
11
|
+
import lmcache.c_ops as lmc_ops
|
|
12
|
+
|
|
13
|
+
logger = init_logger(__name__)
|
|
14
|
+
|
|
15
|
+
# TODO(Jiayi): Add and test more types of rope
|
|
16
|
+
# (e.g., rope scaling, (non-)neox style, dtype, etc.)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BasicReverseRope:
|
|
20
|
+
def __init__(self, rope, rotary_dim, is_neox_style):
|
|
21
|
+
self.rope = rope
|
|
22
|
+
self.rotary_dim = rotary_dim
|
|
23
|
+
self.is_neox_style = is_neox_style
|
|
24
|
+
|
|
25
|
+
def do_shuffle(self, t):
|
|
26
|
+
original_shape = t.shape
|
|
27
|
+
t = t.reshape(t.shape[0], -1, self.rotary_dim)
|
|
28
|
+
|
|
29
|
+
if self.is_neox_style:
|
|
30
|
+
o1, o2 = torch.chunk(t, 2, dim=-1)
|
|
31
|
+
else:
|
|
32
|
+
o1 = t[..., ::2]
|
|
33
|
+
o2 = t[..., 1::2]
|
|
34
|
+
|
|
35
|
+
if self.is_neox_style:
|
|
36
|
+
return torch.cat((o2, o1), dim=-1).reshape(original_shape)
|
|
37
|
+
else:
|
|
38
|
+
return torch.stack((o2, o1), dim=-1).reshape(original_shape)
|
|
39
|
+
|
|
40
|
+
def reverse_encode(self, positions, q, k):
|
|
41
|
+
sq = self.do_shuffle(q)
|
|
42
|
+
sk = self.do_shuffle(k)
|
|
43
|
+
nq, nk = self.rope(positions, sq, sk)
|
|
44
|
+
fq = self.do_shuffle(nq)
|
|
45
|
+
fk = self.do_shuffle(nk)
|
|
46
|
+
return fq, fk
|
|
47
|
+
|
|
48
|
+
def __call__(self, positions, q, k):
|
|
49
|
+
return self.reverse_encode(positions, q, k)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class FusedRope:
|
|
53
|
+
"""
|
|
54
|
+
Directly use the fused kernel to ratate K cache from
|
|
55
|
+
the old positions to the new positions.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(self, rope, is_neox_style):
|
|
59
|
+
self.rope = rope
|
|
60
|
+
self.is_neox_style = is_neox_style
|
|
61
|
+
self.head_size = rope.head_size
|
|
62
|
+
self.cos_sin_cache = rope.cos_sin_cache
|
|
63
|
+
|
|
64
|
+
def fused_encode(self, old_positions, new_positions, k):
|
|
65
|
+
num_tokens = k.shape[0]
|
|
66
|
+
k = k.view(num_tokens, -1, self.head_size)
|
|
67
|
+
lmc_ops.rotary_embedding_k_fused(
|
|
68
|
+
old_positions,
|
|
69
|
+
new_positions,
|
|
70
|
+
k,
|
|
71
|
+
self.head_size,
|
|
72
|
+
self.cos_sin_cache.to(k.device),
|
|
73
|
+
self.is_neox_style,
|
|
74
|
+
)
|
|
75
|
+
k = k.view(num_tokens, -1)
|
|
76
|
+
return k
|
|
77
|
+
|
|
78
|
+
def __call__(self, old_positions, new_positions, k):
|
|
79
|
+
return self.fused_encode(old_positions, new_positions, k)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def validate_rope_params(
|
|
83
|
+
head_size: int,
|
|
84
|
+
rotary_dim: int,
|
|
85
|
+
max_position: int,
|
|
86
|
+
base: float,
|
|
87
|
+
is_neox_style: bool = True,
|
|
88
|
+
rope_scaling: Optional[Dict[str, Any]] = None,
|
|
89
|
+
dtype: Optional[torch.dtype] = None,
|
|
90
|
+
partial_rotary_factor: float = 1.0,
|
|
91
|
+
):
|
|
92
|
+
if rotary_dim != head_size:
|
|
93
|
+
logger.error("Currently KV blending only support rotary_dim == head_size.")
|
|
94
|
+
return False
|
|
95
|
+
|
|
96
|
+
if rope_scaling is not None:
|
|
97
|
+
logger.error("Currently KV blending do not support rope scaling.")
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
if partial_rotary_factor != 1.0:
|
|
101
|
+
logger.error(
|
|
102
|
+
"Currently KV blending do not support rotary factor other than 1.0."
|
|
103
|
+
)
|
|
104
|
+
return False
|
|
105
|
+
|
|
106
|
+
return True
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def validate_reverse_correctness(rope, reverse_rope, fused_rope, head_size) -> bool:
|
|
110
|
+
hidden_dim = head_size * 8
|
|
111
|
+
num_tokens = 10
|
|
112
|
+
|
|
113
|
+
dumb_q = torch.rand((num_tokens, hidden_dim), device="cuda", dtype=torch.bfloat16)
|
|
114
|
+
dumb_k = torch.rand((num_tokens, hidden_dim), device="cuda", dtype=torch.bfloat16)
|
|
115
|
+
positions = torch.arange(num_tokens, device="cuda")
|
|
116
|
+
|
|
117
|
+
q1 = dumb_q.clone()
|
|
118
|
+
k1 = dumb_k.clone()
|
|
119
|
+
q1, k1 = rope(positions, q1, k1)
|
|
120
|
+
q1, k1 = reverse_rope(positions, q1, k1)
|
|
121
|
+
|
|
122
|
+
max_q_error = (dumb_q - q1).abs().max()
|
|
123
|
+
max_k_error = (dumb_k - k1).abs().max()
|
|
124
|
+
|
|
125
|
+
logger.info(f"Max Q error: {max_q_error.item()}")
|
|
126
|
+
logger.info(f"Max K error: {max_k_error.item()}")
|
|
127
|
+
|
|
128
|
+
q_no_pos = dumb_q.clone()
|
|
129
|
+
k_no_pos = dumb_k.clone()
|
|
130
|
+
positions2 = torch.arange(100, 100 + num_tokens, device="cuda")
|
|
131
|
+
_, k_pos2 = rope(positions2, q_no_pos, k_no_pos)
|
|
132
|
+
|
|
133
|
+
k_no_pos = dumb_k.clone()
|
|
134
|
+
_, k_pos1 = rope(positions, q_no_pos, k_no_pos)
|
|
135
|
+
k_pos2_fused = fused_rope(positions, positions2, k_pos1)
|
|
136
|
+
|
|
137
|
+
max_k_error_fused = (k_pos2 - k_pos2_fused).abs().max()
|
|
138
|
+
|
|
139
|
+
logger.info(f"Max K error (fused): {max_k_error.item()}")
|
|
140
|
+
|
|
141
|
+
return max_q_error < 0.1 and max_k_error < 0.1 and max_k_error_fused < 0.1
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# Main interface
|
|
145
|
+
def get_fused_rope(
|
|
146
|
+
head_size: int,
|
|
147
|
+
rotary_dim: int,
|
|
148
|
+
max_position: int,
|
|
149
|
+
base: float,
|
|
150
|
+
is_neox_style: bool = True,
|
|
151
|
+
rope_scaling: Optional[Dict[str, Any]] = None,
|
|
152
|
+
dtype: Optional[torch.dtype] = None,
|
|
153
|
+
partial_rotary_factor: float = 1.0,
|
|
154
|
+
) -> Optional[Callable[..., Any]]:
|
|
155
|
+
# Validate the ROPE parameters
|
|
156
|
+
if not validate_rope_params(
|
|
157
|
+
head_size,
|
|
158
|
+
rotary_dim,
|
|
159
|
+
max_position,
|
|
160
|
+
base,
|
|
161
|
+
is_neox_style,
|
|
162
|
+
rope_scaling,
|
|
163
|
+
dtype,
|
|
164
|
+
partial_rotary_factor,
|
|
165
|
+
):
|
|
166
|
+
logger.warning(
|
|
167
|
+
"The rope parameters is not supported! Cannot use cacheblend in this case"
|
|
168
|
+
)
|
|
169
|
+
return None
|
|
170
|
+
|
|
171
|
+
new_rope_params = {
|
|
172
|
+
"rope_theta": base,
|
|
173
|
+
"partial_rotary_factor": partial_rotary_factor,
|
|
174
|
+
}
|
|
175
|
+
if rope_scaling is not None:
|
|
176
|
+
new_rope_params.update(rope_scaling)
|
|
177
|
+
if "type" in rope_scaling:
|
|
178
|
+
new_rope_params["rope_type"] = rope_scaling["type"]
|
|
179
|
+
|
|
180
|
+
rope = vllm_get_rope(
|
|
181
|
+
head_size=head_size,
|
|
182
|
+
max_position=max_position,
|
|
183
|
+
is_neox_style=is_neox_style,
|
|
184
|
+
rope_parameters=new_rope_params,
|
|
185
|
+
dtype=dtype,
|
|
186
|
+
dual_chunk_attention_config=None,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
reverse_rope = BasicReverseRope(rope, rotary_dim, is_neox_style)
|
|
190
|
+
fused_rope = FusedRope(rope, is_neox_style)
|
|
191
|
+
|
|
192
|
+
correct = validate_reverse_correctness(rope, reverse_rope, fused_rope, head_size)
|
|
193
|
+
if not correct:
|
|
194
|
+
logger.error(
|
|
195
|
+
"Fused/reverse rotary encoding is not correct! Will disable blending!"
|
|
196
|
+
)
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
return fused_rope
|