From 4d8f76b907beaebd8148d0f566b07b10945777ff Mon Sep 17 00:00:00 2001 From: ShamSaleem Date: Sun, 21 Jun 2026 15:38:08 +0300 Subject: [PATCH] Fix inference mode silently lost in example notebooks (#577) The notebooks enable inference mode via `torch.inference_mode().__enter__()`. Nothing keeps a reference to the returned guard object, so Python garbage-collects it immediately and its destructor restores grad-enabled state. The notebook then runs with grad enabled, and the fused `addmm_act` kernel raises `ValueError: Expected grad to be disabled.` Use `torch.set_grad_enabled(False)`, which flips global state with no guard object to collect, so it persists for the rest of the notebook. Applied to all three affected notebooks (sam3_image_interactive, sam3_image_batched_inference, sam3_agent). Fixes #577 --- examples/sam3_agent.ipynb | 2 +- examples/sam3_image_batched_inference.ipynb | 2 +- examples/sam3_image_interactive.ipynb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/sam3_agent.ipynb b/examples/sam3_agent.ipynb index 23177e436..81d4064ad 100644 --- a/examples/sam3_agent.ipynb +++ b/examples/sam3_agent.ipynb @@ -53,7 +53,7 @@ "torch.autocast(\"cuda\", dtype=torch.bfloat16).__enter__()\n", "\n", "# inference mode for the whole notebook. Disable if you need gradients\n", - "torch.inference_mode().__enter__()" + "torch.set_grad_enabled(False)" ] }, { diff --git a/examples/sam3_image_batched_inference.ipynb b/examples/sam3_image_batched_inference.ipynb index dddbfd839..8cd123646 100644 --- a/examples/sam3_image_batched_inference.ipynb +++ b/examples/sam3_image_batched_inference.ipynb @@ -81,7 +81,7 @@ "torch.autocast(\"cuda\", dtype=torch.bfloat16).__enter__()\n", "\n", "# inference mode for the whole notebook. Disable if you need gradients\n", - "torch.inference_mode().__enter__()\n" + "torch.set_grad_enabled(False)\n" ] }, { diff --git a/examples/sam3_image_interactive.ipynb b/examples/sam3_image_interactive.ipynb index 7e1e76663..02a19c95d 100644 --- a/examples/sam3_image_interactive.ipynb +++ b/examples/sam3_image_interactive.ipynb @@ -75,7 +75,7 @@ "torch.autocast(\"cuda\", dtype=torch.bfloat16).__enter__()\n", "\n", "# inference mode for the whole notebook. Disable if you need gradients\n", - "torch.inference_mode().__enter__()" + "torch.set_grad_enabled(False)" ] }, {