Skip to content

Commit

Permalink
update demos for nightly binaries workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Armstrong committed Aug 8, 2024
1 parent 150f0fa commit 15842b9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 96 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test-nightly-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
module: [
alexnet_demo_cpu,
resnet_demo_cpu,
lstm_tensorflow_to_torch_cpu,
lstm_torch_to_tensorflow_cpu,
kornia_demo,
]
Expand Down
2 changes: 1 addition & 1 deletion examples_and_demos/kornia_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}
],
"source": [
"tf_rgb_to_grayscale = ivy.source_to_source(kornia.color.rgb_to_grayscale, source=\"torch\", target=\"tensorflow\")"
"tf_rgb_to_grayscale = ivy.transpile(kornia.color.rgb_to_grayscale, source=\"torch\", target=\"tensorflow\")"
]
},
{
Expand Down
98 changes: 4 additions & 94 deletions examples_and_demos/lstm_torch_to_tensorflow_cpu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,107 +65,17 @@
}
],
"source": [
"# create torch lstm layer\n",
"TF_LSTM = ivy.transpile(torch.nn.LSTM, source=\"torch\", target=\"tensorflow\")\n",
"\n",
"torch_lstm = torch.nn.LSTM(2, 2, 1)\n",
"tf_lstm = TF_LSTM(2, 2, 1)\n",
"\n",
"# transpile lstm layer to tensorflow\n",
"x = torch.rand((5, 2, 2))\n",
"tf_lstm = ivy.transpile(torch_lstm, source=\"torch\", to=\"tensorflow\", args=(x,))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yPLdIj2CD7pA",
"outputId": "f43e3b59-2ca3-49f6-f6d0-0a5e8d14dd73"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get output of original torch lstm layer\n",
"x = torch.rand((20, 32, 2))\n",
"original_output = torch_lstm(x)\n",
"\n",
"# get output of transpiled tf lstm layer with the same input\n",
"x = tf.constant(x.cpu().numpy())\n",
"transpiled_output = tf_lstm(x)\n",
"\n",
"# verify the outputs are the same (with some tolerance)\n",
"np.allclose(original_output[0].detach().cpu(), transpiled_output[0].numpy(), atol=1e-7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "22XU0XD277Y4",
"outputId": "91c35ac9-b282-46a2-b33a-963c9dbc1cfa"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"transpiled tf time is 4.480074623755541x slower than torch's lstm\n",
"original tf lstm time is 2.362692848996253x slower than torch's lstm\n"
]
}
],
"source": [
"# run some benchmarks\n",
"from time import perf_counter\n",
"\n",
"x = torch.rand((20, 32, 2))\n",
"N_RUNS = 1000\n",
"\n",
"# time the original torch lstm\n",
"s = perf_counter()\n",
"for _ in range(N_RUNS):\n",
" torch_lstm(x)\n",
"original_torch_time = perf_counter() - s\n",
"\n",
"# compile transpiled tf lstm\n",
"x = tf.constant(x.cpu().numpy())\n",
"tf_lstm = tf.autograph.experimental.do_not_convert(tf_lstm)\n",
"compiled_tf_lstm = tf.function(tf_lstm)\n",
"compiled_tf_lstm(x)\n",
"\n",
"# time the transpiled tf lstm\n",
"s = perf_counter()\n",
"for _ in range(N_RUNS):\n",
" compiled_tf_lstm(x)\n",
"transpiled_tf_time = perf_counter() - s\n",
"\n",
"# time tf's own lstm layer (also compiled) for comparison\n",
"original_tf_lstm = tf.keras.layers.LSTM(2, time_major=True, return_sequences=True)\n",
"original_tf_lstm = tf.function(original_tf_lstm)\n",
"original_tf_lstm(x)\n",
"\n",
"s = perf_counter()\n",
"for _ in range(N_RUNS):\n",
" original_tf_lstm(x)\n",
"original_tf_time = perf_counter() - s\n",
"\n",
"# as we can see, the transpiled tf lstm has comparable performance to tf's own lstm layer\n",
"print(f'transpiled tf time is {transpiled_tf_time / original_torch_time}x slower than torch\\'s lstm')\n",
"print(f'original tf lstm time is {original_tf_time / original_torch_time}x slower than torch\\'s lstm')"
"assert np.allclose(original_output[0].detach().cpu().shape, transpiled_output[0].numpy().shape)"
]
}
],
Expand Down

0 comments on commit 15842b9

Please sign in to comment.