Actively identify and eliminate code duplication by extracting common patterns into reusable components. When you notice repeated code blocks, similar conditional logic, or duplicate class definitions, consolidate them through extraction techniques.
Key strategies:
vision_utils.py
, general utilities in utils.py
)Example of pattern extraction:
# Before: Repeated conditional logic
global_num_tokens_gpu=(
self.prefill_global_num_tokens_gpu
if hasattr(self, "capture_forward_mode")
and self.capture_forward_mode == ForwardMode.EXTEND
else self.global_num_tokens_gpu
),
global_num_tokens_for_logprob_gpu=(
self.prefill_global_num_tokens_for_logprob_gpu
if hasattr(self, "capture_forward_mode")
and self.capture_forward_mode == ForwardMode.EXTEND
else self.global_num_tokens_for_logprob_gpu
),
# After: Extract pattern into data structure
@dataclass
class TokenConfig:
global_num_tokens_gpu: Tensor
global_num_tokens_for_logprob_gpu: Tensor
config = prefill_config if self.capture_forward_mode == ForwardMode.EXTEND else default_config
This approach improves maintainability, reduces the chance of inconsistencies, and makes the codebase more readable and easier to modify.
Enter the URL of a public GitHub repository