def wrap_nncf_model(
    model: nn.Module,
    config: dict,
    dataloader: DataLoader,
    init_state_dict: dict,
) -> tuple[CompressionAlgorithmController, NNCFNetwork]:
    """Wrap PyTorch model with NNCF compression.

    Args:
        model (nn.Module): Anomalib model to be compressed.
        config (dict): NNCF configuration dictionary.
        dataloader (DataLoader): DataLoader for NNCF model initialization.
        init_state_dict (dict): Initial state dictionary for model initialization.

    Returns:
        tuple[CompressionAlgorithmController, NNCFNetwork]: A tuple containing:
            - The compression controller
            - The compressed model

    Warning:
        Either ``dataloader`` or ``init_state_dict`` must be provided for proper quantizer initialization.

    Example:
        >>> import torch.nn as nn
        >>> from torch.utils.data import DataLoader, TensorDataset
        >>> model = nn.Linear(10, 2)
        >>> config = {"input_info": {"sample_size": [1, 10]}}
        >>> data = torch.randn(100, 10)
        >>> dataloader = DataLoader(TensorDataset(data))
        >>> controller, compressed = wrap_nncf_model(model, config, dataloader, {})
        >>> isinstance(compressed, NNCFNetwork)
        True
    """
    nncf_config = NNCFConfig.from_dict(config)

    if not dataloader and not init_state_dict:
        logger.warning(
            "Either dataloader or NNCF pre-trained "
            "model checkpoint should be set. Without this, "
            "quantizers will not be initialized",
        )

    compression_state = None
    resuming_state_dict = None
    if init_state_dict:
        resuming_state_dict = init_state_dict.get("model")
        compression_state = init_state_dict.get("compression_state")

    if dataloader:
        init_loader = InitLoader(dataloader)
        nncf_config = register_default_init_args(nncf_config, init_loader)

    nncf_ctrl, nncf_model = create_compressed_model(
        model=model,
        config=nncf_config,
        dump_graphs=False,
        compression_state=compression_state,
    )

    if resuming_state_dict:
        load_state(nncf_model, resuming_state_dict, is_resume=True)

    return nncf_ctrl, nncf_model
Wrap PyTorch model with NNCF compression.

    Args:
        model (nn.Module): Anomalib model to be compressed.
        config (dict): NNCF configuration dictionary.
        dataloader (DataLoader): DataLoader for NNCF model initialization.
        init_state_dict (dict): Initial state dictionary for model initialization.

    Returns:
        tuple[CompressionAlgorithmController, NNCFNetwork]: A tuple containing:
            - The compression controller
            - The compressed model

    Warning:
        Either ``dataloader`` or ``init_state_dict`` must be provided for proper quantizer initialization.

    Example:
        >>> import torch.nn as nn
        >>> from torch.utils.data import DataLoader, TensorDataset
        >>> model = nn.Linear(10, 2)
        >>> config = {"input_info": {"sample_size": [1, 10]}}
        >>> data = torch.randn(100, 10)
        >>> dataloader = DataLoader(TensorDataset(data))
        >>> controller, compressed = wrap_nncf_model(model, config, dataloader, {})
        >>> isinstance(compressed, NNCFNetwork)
        True
    
src/anomalib/callbacks/nncf/utils.py