Files
vllm-omni/vllm_omni/diffusion/cache/base.py
T

112 lines
4.1 KiB
Python
Raw Normal View History

2025-12-12 09:59:56 +05:30
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""
Base cache backend interface for diffusion models.
2025-12-12 09:59:56 +05:30
This module defines the abstract base class that all cache backends must implement.
Cache backends provide a unified interface for applying different caching strategies
to transformer models.
Main cache backend implementations:
1. CacheDiTBackend: Implements cache-dit acceleration (DBCache, SCM, TaylorSeer) using
the cache-dit library. Inherits from CacheBackend. Used via cache_backend="cache_dit".
2. TeaCacheBackend: Hook-based backend for TeaCache acceleration. Inherits from
CacheBackend. Used via cache_backend="tea_cache".
All backends implement the same interface:
- enable(pipeline): Enable cache on the pipeline
- refresh(pipeline, num_inference_steps, verbose): Refresh cache state
- is_enabled(): Check if cache is enabled
2025-12-12 09:59:56 +05:30
"""
from abc import ABC, abstractmethod
from typing import Any
2025-12-22 06:49:04 +08:00
import torch.nn as nn
from vllm_omni.diffusion.data import DiffusionCacheConfig
2025-12-12 09:59:56 +05:30
class CacheBackend(ABC):
2025-12-12 09:59:56 +05:30
"""
Abstract base class for cache backends.
2025-12-12 09:59:56 +05:30
All cache backend implementations (CacheDiTBackend, TeaCacheBackend, etc.) inherit
from this base class and implement the enable() and refresh() methods to manage
cache lifecycle.
Cache backends apply caching strategies to transformer models to accelerate
inference. Different backends use different underlying mechanisms (e.g., cache-dit
library for CacheDiTBackend, hooks for TeaCacheBackend), but all share the same
unified interface.
2025-12-12 09:59:56 +05:30
Attributes:
config: DiffusionCacheConfig instance containing cache-specific configuration parameters
enabled: Boolean flag indicating whether cache is enabled (set to True after enable() is called)
2025-12-12 09:59:56 +05:30
"""
def __init__(self, config: DiffusionCacheConfig):
2025-12-12 09:59:56 +05:30
"""
Initialize cache backend with configuration.
2025-12-12 09:59:56 +05:30
Args:
config: DiffusionCacheConfig instance with cache-specific parameters
2025-12-12 09:59:56 +05:30
"""
self.config = config
self.enabled = False
2025-12-12 09:59:56 +05:30
@abstractmethod
def enable(self, pipeline: Any) -> None:
2025-12-12 09:59:56 +05:30
"""
Enable cache on the pipeline.
2025-12-12 09:59:56 +05:30
This method applies the caching strategy to the transformer(s) in the pipeline.
The specific implementation depends on the backend (e.g., hooks for TeaCacheBackend,
cache-dit library for CacheDiTBackend). Called once during pipeline initialization.
2025-12-12 09:59:56 +05:30
Args:
pipeline: Diffusion pipeline instance. The backend can extract:
- transformer: via pipeline.transformer
- model_type: via pipeline.__class__.__name__
2025-12-12 09:59:56 +05:30
"""
raise NotImplementedError("Subclasses must implement enable()")
2025-12-12 09:59:56 +05:30
@abstractmethod
def refresh(self, pipeline: Any, num_inference_steps: int, verbose: bool = True) -> None:
2025-12-12 09:59:56 +05:30
"""
Refresh cache state for new generation.
2025-12-12 09:59:56 +05:30
This method should clear any cached values and reset counters/accumulators.
Called at the start of each generation to ensure clean state.
Args:
pipeline: Diffusion pipeline instance. The backend can extract:
- transformer: via pipeline.transformer
num_inference_steps: Number of inference steps for the current generation.
May be used for cache context updates.
verbose: Whether to log refresh operations (default: True)
2025-12-12 09:59:56 +05:30
"""
raise NotImplementedError("Subclasses must implement refresh()")
def is_enabled(self) -> bool:
"""
Check if cache is enabled on this backend.
Returns:
True if cache is enabled, False otherwise.
"""
return self.enabled
2025-12-12 09:59:56 +05:30
def __repr__(self) -> str:
return f"{self.__class__.__name__}(config={self.config})"
2025-12-22 06:49:04 +08:00
class CachedTransformer(nn.Module):
def __init__(self, **kwargs):
super().__init__()
self.do_true_cfg = False
def __init_subclass__(cls, enable_separate_cfg: bool = True, **kwargs):
cls.enable_separate_cfg = enable_separate_cfg
super().__init_subclass__(**kwargs)