{"record":{"id":"d4d8009d8f8cd882","repo":"huggingface/pytorch-image-models","slug":"tensor-lr-must-be-1-element","errorCode":null,"errorMessage":"Tensor lr must be 1-element","messagePattern":"Tensor lr must be 1-element","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/optim/adopt.py","lineNumber":87,"sourceCode":"            eps: float = 1e-6,\n            clip_exp: Optional[float] = 0.333,\n            weight_decay: float = 0.0,\n            decoupled: bool = False,\n            corrected_weight_decay: bool = False,\n            *,\n            caution: bool = False,\n            foreach: Optional[bool] = False,\n            maximize: bool = False,\n            capturable: bool = False,\n            differentiable: bool = False,\n    ):\n        if isinstance(lr, Tensor):\n            if foreach and not capturable:\n                raise ValueError(\n                    \"lr as a Tensor is not supported for capturable=False and foreach=True\"\n                )\n            if lr.numel() != 1:\n                raise ValueError(\"Tensor lr must be 1-element\")\n        if not 0.0 <= lr:\n            raise ValueError(f\"Invalid learning rate: {lr}\")\n        if not 0.0 <= eps:\n            raise ValueError(f\"Invalid epsilon value: {eps}\")\n        if not 0.0 <= betas[0] < 1.0:\n            raise ValueError(f\"Invalid beta parameter at index 0: {betas[0]}\")\n        if not 0.0 <= betas[1] < 1.0:\n            raise ValueError(f\"Invalid beta parameter at index 1: {betas[1]}\")\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            betas=betas,\n            eps=eps,\n            weight_decay=weight_decay,\n            clip_exp=clip_exp,\n            decoupled=decoupled,","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/optim/adopt.py#L69-L105","documentation":"ADOPT's constructor requires that a tensor learning rate contain exactly one element, since the update applies a single scalar lr across all parameters in the group.","triggerScenarios":"Constructing timm.optim.Adopt(params, lr=torch.tensor([1e-3, 1e-4])) — any tensor lr with lr.numel() != 1.","commonSituations":"Passing a per-layer or per-step lr schedule as a multi-element tensor instead of slicing out one scalar per step, or accidentally passing a batch-shaped tensor as lr.","solutions":["Pass a 1-element tensor, e.g. torch.tensor(1e-3) or a (1,)-shaped schedule slice","Better: pass a float lr and update optimizer.param_groups[i]['lr'] each step with the scalar from your scheduler"],"exampleFix":"# before\nopt = timm.optim.Adopt(model.parameters(), lr=torch.tensor([1e-3, 1e-4]))\n# after\nopt = timm.optim.Adopt(model.parameters(), lr=torch.tensor(1e-3))","handlingStrategy":"validation","validationCode":"if isinstance(lr, torch.Tensor):\n    assert lr.numel() == 1, 'tensor lr must be scalar'","typeGuard":"def is_scalar_lr(lr) -> bool:\n    return not isinstance(lr, torch.Tensor) or lr.numel() == 1","tryCatchPattern":null,"preventionTips":["Pass float lrs and update param_group['lr'] each step","Squeeze schedule tensors to one element before assignment"],"tags":["optimizer","adopt","tensor-lr","shape"],"backgroundTag":"optimizer-tensor-lr-unsupported","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}