{"record":{"id":"c00774220570a708","repo":"huggingface/pytorch-image-models","slug":"it-is-highly-recommended-to-have-opt-einsum-inst","errorCode":null,"errorMessage":"It is highly recommended to have 'opt_einsum' installed for this optimizer.","messagePattern":"It is highly recommended to have 'opt_einsum' installed for this optimizer\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"timm/optim/kron.py","lineNumber":134,"sourceCode":"        preconditioner_update_probability: Optional[Union[Callable, float]] = None,\n        max_size_triangular: int = 2048,\n        min_ndim_triangular: int = 2,\n        memory_save_mode: Optional[str] = None,\n        momentum_into_precond_update: bool = True,\n        precond_lr: float = 0.1,\n        precond_init_scale: float = 1.0,\n        mu_dtype: Optional[torch.dtype] = None,\n        precond_dtype: Optional[torch.dtype] = None,\n        decoupled_decay: bool = False,\n        corrected_weight_decay: bool = False,\n        flatten: bool = False,\n        flatten_start_dim: int = 2,\n        flatten_end_dim: int = -1,\n        stochastic_weight_decay: bool = False,\n        deterministic: bool = False,\n    ):\n        if not has_opt_einsum:\n            warnings.warn(\"It is highly recommended to have 'opt_einsum' installed for this optimizer.\")\n\n        _validate_scalar(\"learning rate\", lr)\n        if not 0.0 <= momentum < 1.0:\n            raise ValueError(f\"Invalid beta parameter: {momentum}\")\n        if not 0.0 <= weight_decay:\n            raise ValueError(f\"Invalid weight_decay value: {weight_decay}\")\n\n        defaults = dict(\n            lr=lr,\n            momentum=momentum,\n            weight_decay=weight_decay,\n            preconditioner_update_probability=preconditioner_update_probability,\n            max_size_triangular=max_size_triangular,\n            min_ndim_triangular=min_ndim_triangular,\n            memory_save_mode=memory_save_mode,\n            momentum_into_precond_update=momentum_into_precond_update,\n            precond_lr=precond_lr,\n            precond_init_scale=precond_init_scale,","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/optim/kron.py#L116-L152","documentation":"The Kron optimizer (Kron / AdamW-style Kronecker-factored optimizer) uses torch.einsum-heavy update rules that are dramatically faster and lower-memory with the opt_einsum package's path optimization. If opt_einsum isn't installed, timm warns that you'll get slower/larger-footprint training.","triggerScenarios":"timm.optim.Kron(...) or create_optimizer_v2(..., opt='kron') when the opt_einsum package is not importable (has_opt_einsum False).","commonSituations":"Training environments that never installed opt_einsum; slim Docker images; users switching from AdamW to Kron hitting slow steps or higher memory and OOMs.","solutions":["pip install opt_einsum in the training environment","If OOMing/slow before installing, also try model.set_buffer(keep_weight_decay=...) options or reduce batch size — but the install is the fix","Verify with python -c \"import opt_einsum\" after installing"],"exampleFix":"# before (shell)\npython train.py --opt kron   # warns, slow\n# after\npip install opt_einsum\npython train.py --opt kron","handlingStrategy":"validation","validationCode":"try:\n    import opt_einsum  # noqa: F401\n    has = True\nexcept ImportError:\n    has = False\nif not has:\n    raise RuntimeError('pip install opt_einsum before using the Kron optimizer')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add opt_einsum to training environment requirements","Include it in Dockerfiles/base conda envs when using timm optimizers","Watch step time and memory after switching optimizers"],"tags":["timm","optimizer","kron","performance","einsum"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}