Skip to content

Commit

Permalink
fix: issue with session crashing with paddle demo on Colab's T4
Browse files Browse the repository at this point in the history
  • Loading branch information
vedpatwardhan committed Mar 7, 2024
1 parent 322064e commit c2169d1
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions examples_and_demos/dinov2_to_paddle.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,11 @@
"import numpy as np\n",
"from transformers import AutoImageProcessor, AutoModelForImageClassification\n",
"import ivy\n",
"device = \"gpu\" if paddle.device.cuda.device_count() else \"cpu\"\n",
"torch.manual_seed(0)\n",
"paddle.seed(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the default device, this step isn’t required if you’re transpiling on a CPU:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"ivy.set_default_device(\"gpu:0\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -153,7 +137,7 @@
"outputs": [],
"source": [
"processor = AutoImageProcessor.from_pretrained(\"facebook/dinov2-base-imagenet1k-1-layer\")\n",
"model = AutoModelForImageClassification.from_pretrained(\"facebook/dinov2-base-imagenet1k-1-layer\").to(device)\n",
"model = AutoModelForImageClassification.from_pretrained(\"facebook/dinov2-base-imagenet1k-1-layer\")\n",
"id2label = model.config.id2label"
]
},
Expand All @@ -178,7 +162,7 @@
}
],
"source": [
"inputs = processor(images=image, return_tensors=\"pt\").to(device)\n",
"inputs = processor(images=image, return_tensors=\"pt\")\n",
"outputs = model(**inputs)\n",
"logits = outputs.logits\n",
"logits_np = logits.detach().cpu().numpy()\n",
Expand All @@ -190,7 +174,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we will transpile the model to PaddlePaddle using ivy.transpile, here’s a simple [example](https://unify.ai/docs/ivy/demos/learn_the_basics/04_transpile_code.html) explaining the usage. If you’re transpiling on a GPU, then based on your machine specs you might have to set `FLAGS_fraction_of_gpu_memory_to_use` to `auto_growth`."
"Now, we will transpile the model to PaddlePaddle using ivy.transpile, here’s a simple [example](https://unify.ai/docs/ivy/demos/learn_the_basics/04_transpile_code.html) explaining the usage."
]
},
{
Expand Down Expand Up @@ -237,7 +221,7 @@
}
],
"source": [
"paddle_inputs = {\"pixel_values\": paddle.to_tensor(inputs[\"pixel_values\"].cpu().numpy(), stop_gradient=False)}\n",
"paddle_inputs = {\"pixel_values\": paddle.to_tensor(inputs[\"pixel_values\"].cpu().numpy(), stop_gradient=False, place=\"cpu\")}\n",
"outputs = transpiled_model(**paddle_inputs)\n",
"logits = outputs.logits\n",
"logits_np_transpiled = logits.numpy()\n",
Expand Down Expand Up @@ -352,6 +336,7 @@
}
],
"source": [
"transpiled_model = transpiled_model.to(device)\n",
"epochs = 5\n",
"loss_epoch_arr = []\n",
"\n",
Expand Down

0 comments on commit c2169d1

Please sign in to comment.