"""Workload analysis: classify the training workload and estimate needs. The analyzer is the input to scheduler decisions. It distinguishes measured facts (batch size, parameter count, dataloader settings read from the actual objects) from estimates (expected VRAM, expected throughput class), and every estimate is labeled as such in its output dict. """ from __future__ import annotations from dataclasses import dataclass, field from typing import Any, Dict, List, Optional from ..compat import require_torch @dataclass class WorkloadProfile: # measured facts --------------------------------------------------------- framework: str = "pytorch" task_class: str = "unknown" # sr_training | cnn_training | other param_count: int = 0 param_mb: float = 0.0 batch_size: Optional[int] = None input_shape: Optional[tuple] = None num_gpus_required_estimate: int = 1 # estimate, labeled below dataloader_settings: Dict[str, Any] = field(default_factory=dict) is_sequential_model: bool = False has_batchnorm: bool = False conv_fraction: float = 0.0 # conv params / total params # derived ---------------------------------------------------------------- estimates: Dict[str, str] = field(default_factory=dict) def summary(self) -> dict: return { "framework": self.framework, "task_class": self.task_class, "param_count": self.param_count, "param_mb": round(self.param_mb, 2), "batch_size": self.batch_size, "input_shape": list(self.input_shape) if self.input_shape else None, "dataloader_settings": self.dataloader_settings, "is_sequential_model": self.is_sequential_model, "has_batchnorm": self.has_batchnorm, "conv_fraction": round(self.conv_fraction, 3), "labeled_estimates": self.estimates, } def _infer_task_class(model, input_shape) -> str: """Classify by structure, honestly. Only SR is claimed when evidence fits: 4-D image input, output spatially larger than input (upsampling).""" if input_shape is None or len(input_shape) < 4: return "cnn_training" if input_shape and len(input_shape) == 4 else "other" return "cnn_training" def analyze_model(model, example_input=None, dataloader=None) -> WorkloadProfile: """Build a WorkloadProfile from REAL model/dataloader objects.""" torch = require_torch() nn = torch.nn profile = WorkloadProfile() params = list(model.parameters()) profile.param_count = sum(p.numel() for p in params) profile.param_mb = sum(p.numel() * p.element_size() for p in params) / (1024 * 1024) profile.is_sequential_model = isinstance(model, nn.Sequential) conv_params = sum( p.numel() for m in model.modules() if isinstance(m, nn.Conv2d) for p in m.parameters(recurse=False) ) profile.conv_fraction = (conv_params / profile.param_count) if profile.param_count else 0.0 profile.has_batchnorm = any(isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)) for m in model.modules()) if example_input is not None: if isinstance(example_input, (tuple, list)): first = example_input[0] elif isinstance(example_input, dict): first = next(iter(example_input.values())) else: first = example_input if hasattr(first, "shape"): shape = tuple(first.shape) profile.input_shape = shape if len(shape) == 4 and shape[-1] in (1, 3) and shape[-2] <= 512: # heuristic candidate for SR; refined by output check below profile.task_class = "cnn_image_training" if profile.task_class == "unknown": profile.task_class = _infer_task_class(model, profile.input_shape) # SR detection with real evidence: run a no-grad forward and compare shapes if example_input is not None and profile.input_shape is not None and len(profile.input_shape) == 4: try: was_training = model.training model.eval() with torch.no_grad(): device = next(params).device if params else torch.device("cpu") x = example_input if hasattr(x, "to"): x = x.to(device) elif isinstance(x, (tuple, list)): x = type(x)( (e.to(device) if hasattr(e, "to") else e) for e in x ) y = model(x) if isinstance(y, (tuple, list)): y = y[0] if hasattr(y, "shape") and len(y.shape) == 4: up = (y.shape[-1] > profile.input_shape[-1]) or ( y.shape[-2] > profile.input_shape[-2] ) if up: profile.task_class = "sr_training" if was_training: model.train() except Exception: # noqa: BLE001 - analysis must never break the user's run profile.estimates["task_class"] = "forward probe failed; class kept as cnn_training" if dataloader is not None: bs = getattr(dataloader, "batch_size", None) profile.batch_size = bs try: ds = dataloader.dataset profile.dataloader_settings = { "batch_size": bs, "num_workers": getattr(dataloader, "num_workers", None), "pin_memory": getattr(dataloader, "pin_memory", None), "persistent_workers": getattr(dataloader, "persistent_workers", False), "dataset_len": len(ds) if hasattr(ds, "__len__") else None, "dataset_kind": type(ds).__name__, } except Exception: # noqa: BLE001 profile.dataloader_settings = {"batch_size": bs} profile.estimates["num_gpus_required"] = "estimate; measured by scheduler probe" profile.estimates["param_mb_x_copies"] = "DP replicates param_mb per GPU each step" return profile