{"record":{"id":"d0da30ff5d1a127e","repo":"tensorflow/models","slug":"swapping-weights-must-occur-under-a-tf-distribute","errorCode":null,"errorMessage":"Swapping weights must occur under a tf.distribute.Strategy.","messagePattern":"Swapping weights must occur under a tf\\.distribute\\.Strategy\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/modeling/optimization/ema_optimizer.py","lineNumber":176,"sourceCode":"    # Update moving average with the latest value.\n    for average, normal in zip(self._average_weights, self._model_weights):  # pyrefly: ignore[bad-argument-type]\n      strategy.extended.update(\n          average, _apply_moving, args=(normal,), group=False\n      )\n\n  def swap_weights(self):\n    \"\"\"Swap the average and moving weights.\n\n    This is a convenience method to allow one to evaluate the averaged weights\n    at test time. Loads the weights stored in `self._average` into the model,\n    keeping a copy of the original model weights. Swapping twice will return\n    the original weights.\n    \"\"\"\n    if tf.distribute.in_cross_replica_context():\n      strategy = tf.distribute.get_strategy()\n      strategy.run(self._swap_weights, args=())\n    else:\n      raise ValueError(\n          'Swapping weights must occur under a tf.distribute.Strategy.'\n      )\n\n  @tf.function\n  def _swap_weights(self):\n    def fn_0(a, b):\n      a.assign_add(b)\n      return a\n    def fn_1(b, a):\n      b.assign(a - b)\n      return b\n    def fn_2(a, b):\n      a.assign_sub(b)\n      return a\n\n    def _swap(strategy, a_and_b):\n      \"\"\"Swap `a` and `b` and mirror to all devices.\"\"\"\n      for a, b in a_and_b:","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/modeling/optimization/ema_optimizer.py#L158-L194","documentation":"Error \"Swapping weights must occur under a tf.distribute.Strategy.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/modeling/optimization/ema_optimizer.py:176 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}