Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Huggingface Diffusers Generate Model Tests

From Leeroopedia
Revision as of 13:03, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Huggingface_Diffusers_Generate_Model_Tests.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Testing, Code_Generation, Tooling
Last Updated 2026-02-13 21:00 GMT

Overview

Concrete tool for automatically generating test suite files for diffusers model classes by analyzing their mixins, attributes, and forward signatures using AST introspection.

Description

The generate_model_tests.py utility analyzes a model source file using Python's `ast` module to discover model classes that inherit from `ModelMixin`. It inspects each class for its base classes and private attributes (e.g., `_cp_plan`, `_supports_gradient_checkpointing`) to determine which test mixins to include. The `ModelAnalyzer` AST visitor extracts class names, base classes, `__init__` and `forward` parameters, and private attributes. Based on this analysis, it generates a complete test file with the appropriate test mixin hierarchy, including always-included testers (`ModelTesterMixin`, `MemoryTesterMixin`, `TorchCompileTesterMixin`) and optional testers for quantization (BnB, Quanto, TorchAO, GGUF, ModelOpt), caching (PAB, FBC, FasterCache), single-file loading, and IP-adapter support.

Usage

Run this script when adding a new model to the diffusers library to bootstrap its test file. Point it at the model source file and it generates a test file in the corresponding test directory with all necessary test mixins pre-configured.

Code Reference

Source Location

Signature

class ModelAnalyzer(ast.NodeVisitor):
    """AST visitor that extracts model class information."""
    def __init__(self): ...
    def visit_ClassDef(self, node: ast.ClassDef): ...
    def _extract_func_params(self, func_node: ast.FunctionDef) -> list[dict]: ...
    def _get_annotation_str(self, node) -> str: ...
    def _get_value(self, node): ...

def analyze_model_file(filepath: str) -> tuple[list[dict], set[str]]:
    """Parse a model file and return model class info and imports."""
    ...

def generate_test_file(
    model_info: dict,
    model_filepath: str,
    include_optional: list[str],
    imports: set[str],
) -> str:
    """Generate a complete test file for a model class."""
    ...

def get_test_output_path(model_filepath: str) -> str:
    """Determine the output test file path from the model file path."""
    ...

def main():
    """CLI entry point for test generation."""
    ...

Import

# CLI script — not imported as a module:
# python utils/generate_model_tests.py src/diffusers/models/transformers/transformer_flux.py

I/O Contract

Inputs

Name Type Required Description
model_filepath str Yes Path to the model source file to analyze
output str No Custom output path for the generated test file
include list[str] No Optional testers to include (e.g., `bnb`, `quanto`, `single_file`, `all`)
class_name str No Specific model class to generate tests for (default: first found)
dry_run bool No Print generated code without writing to file

Outputs

Name Type Description
Test file File Complete Python test file with configured test mixin classes
stdout (dry-run) str Generated test code printed to console

Usage Examples

Basic Test Generation

# Generate tests for a transformer model
python utils/generate_model_tests.py src/diffusers/models/transformers/transformer_flux.py

# Output: tests/models/transformers/test_models_transformer_flux.py

With Optional Testers

# Include quantization and caching testers
python utils/generate_model_tests.py \
    src/diffusers/models/transformers/transformer_wan.py \
    --include bnb quanto torchao pab_cache fbc_cache

# Include all optional testers
python utils/generate_model_tests.py \
    src/diffusers/models/transformers/transformer_wan.py \
    --include all

Dry Run

# Preview generated code without writing
python utils/generate_model_tests.py \
    src/diffusers/models/transformers/transformer_flux.py \
    --dry-run

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment