TensorFlow/gpu.ipynb
2021-12-16 08:36:40 -06:00

1212 lines
36 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Tce3stUlHN0L"
},
"source": [
"##### Copyright 2018 The TensorFlow Authors.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"execution": {
"iopub.execute_input": "2021-09-24T01:22:00.805456Z",
"iopub.status.busy": "2021-09-24T01:22:00.804828Z",
"iopub.status.idle": "2021-09-24T01:22:00.807628Z",
"shell.execute_reply": "2021-09-24T01:22:00.808014Z"
},
"id": "tuOe1ymfHZPu"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MfBg1C5NB3X0"
},
"source": [
"# Use a GPU\n",
"\n",
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/gpu\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/gpu.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/gpu.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/gpu.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SoYIwe40vEPI"
},
"source": [
"TensorFlow code, and `tf.keras` models will transparently run on a single GPU with no code changes required.\n",
"\n",
"Note: Use `tf.config.list_physical_devices('GPU')` to confirm that TensorFlow is using the GPU.\n",
"\n",
"The simplest way to run on multiple GPUs, on one or many machines, is using [Distribution Strategies](distributed_training.ipynb).\n",
"\n",
"This guide is for users who have tried these approaches and found that they need fine-grained control of how TensorFlow uses the GPU. To learn how to debug performance issues for single and multi-GPU scenarios, see the [Optimize TensorFlow GPU Performance](gpu_performance_analysis.md) guide."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MUXex9ctTuDB"
},
"source": [
"## Setup\n",
"\n",
"Ensure you have the latest TensorFlow gpu release installed."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:00.816344Z",
"iopub.status.busy": "2021-09-24T01:22:00.815630Z",
"iopub.status.idle": "2021-09-24T01:22:03.130393Z",
"shell.execute_reply": "2021-09-24T01:22:03.130817Z"
},
"id": "IqR2PQG4ZaZ0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Num GPUs Available: 1\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZELutYNetv-v"
},
"source": [
"## Overview\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xHxb-dlhMIzW"
},
"source": [
"TensorFlow supports running computations on a variety of types of devices, including CPU and GPU. They are represented with string identifiers for example:\n",
"\n",
"* `\"/device:CPU:0\"`: The CPU of your machine.\n",
"* `\"/GPU:0\"`: Short-hand notation for the first GPU of your machine that is visible to TensorFlow.\n",
"* `\"/job:localhost/replica:0/task:0/device:GPU:1\"`: Fully qualified name of the second GPU of your machine that is visible to TensorFlow.\n",
"\n",
"If a TensorFlow operation has both CPU and GPU implementations, by default, the GPU device is prioritized when the operation is assigned. For example, `tf.matmul` has both CPU and GPU kernels and on a system with devices `CPU:0` and `GPU:0`, the `GPU:0` device is selected to run `tf.matmul` unless you explicitly request to run it on another device.\n",
"\n",
"If a TensorFlow operation has no corresponding GPU implementation, then the operation falls back to the CPU device. For example, since `tf.cast` only has a CPU kernel, on a system with devices `CPU:0` and `GPU:0`, the `CPU:0` device is selected to run `tf.cast`, even if requested to run on the `GPU:0` device."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UhNtHfuxCGVy"
},
"source": [
"## Logging device placement\n",
"\n",
"To find out which devices your operations and tensors are assigned to, put\n",
"`tf.debugging.set_log_device_placement(True)` as the first statement of your\n",
"program. Enabling device placement logging causes any Tensor allocations or operations to be printed."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.032946Z",
"iopub.status.busy": "2021-09-24T01:22:04.032064Z",
"iopub.status.idle": "2021-09-24T01:22:04.439357Z",
"shell.execute_reply": "2021-09-24T01:22:04.438868Z"
},
"id": "2Dbw0tpEirCd"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n",
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n",
"Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2021-12-14 11:02:14.613167: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2021-12-14 11:02:14.614359: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 7676 MB memory: -> device: 0, name: AMD Radeon RX 6600 XT, pci bus id: 0000:08:00.0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[22. 28.]\n",
" [49. 64.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"tf.debugging.set_log_device_placement(True)\n",
"\n",
"# Create some tensors\n",
"a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
"b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
"c = tf.matmul(a, b)\n",
"\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kKhmFeraTdEI"
},
"source": [
"The above code will print an indication the `MatMul` op was executed on `GPU:0`."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U88FspwGjB7W"
},
"source": [
"## Manual device placement\n",
"\n",
"If you would like a particular operation to run on a device of your choice\n",
"instead of what's automatically selected for you, you can use `with tf.device`\n",
"to create a device context, and all the operations within that context will\n",
"run on the same designated device."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.445467Z",
"iopub.status.busy": "2021-09-24T01:22:04.444764Z",
"iopub.status.idle": "2021-09-24T01:22:04.448541Z",
"shell.execute_reply": "2021-09-24T01:22:04.448883Z"
},
"id": "8wqaQfEhjHit"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0\n",
"tf.Tensor(\n",
"[[22. 28.]\n",
" [49. 64.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"tf.debugging.set_log_device_placement(True)\n",
"\n",
"# Place tensors on the CPU\n",
"with tf.device('/CPU:0'):\n",
" a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
" b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
"\n",
"# Run on the GPU\n",
"c = tf.matmul(a, b)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8ixO89gRjJUu"
},
"source": [
"You will see that now `a` and `b` are assigned to `CPU:0`. Since a device was\n",
"not explicitly specified for the `MatMul` operation, the TensorFlow runtime will\n",
"choose one based on the operation and available devices (`GPU:0` in this\n",
"example) and automatically copy tensors between devices if required."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ARrRhwqijPzN"
},
"source": [
"## Limiting GPU memory growth\n",
"\n",
"By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to\n",
"[`CUDA_VISIBLE_DEVICES`](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#env-vars)) visible to the process. This is done to more efficiently use the relatively precious GPU memory resources on the devices by reducing memory fragmentation. To limit TensorFlow to a specific set of GPUs, use the `tf.config.set_visible_devices` method."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.453982Z",
"iopub.status.busy": "2021-09-24T01:22:04.453433Z",
"iopub.status.idle": "2021-09-24T01:22:04.455736Z",
"shell.execute_reply": "2021-09-24T01:22:04.456091Z"
},
"id": "hPI--n_jhZhv"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 Physical GPUs, 1 Logical GPU\n"
]
}
],
"source": [
"gpus = tf.config.list_physical_devices('GPU')\n",
"if gpus:\n",
" # Restrict TensorFlow to only use the first GPU\n",
" try:\n",
" tf.config.set_visible_devices(gpus[0], 'GPU')\n",
" logical_gpus = tf.config.list_logical_devices('GPU')\n",
" print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPU\")\n",
" except RuntimeError as e:\n",
" # Visible devices must be set before GPUs have been initialized\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "N3x4M55DhYk9"
},
"source": [
"In some cases it is desirable for the process to only allocate a subset of the available memory, or to only grow the memory usage as is needed by the process. TensorFlow provides two methods to control this.\n",
"\n",
"The first option is to turn on memory growth by calling `tf.config.experimental.set_memory_growth`, which attempts to allocate only as much GPU memory as needed for the runtime allocations: it starts out allocating very little memory, and as the program gets run and more GPU memory is needed, the GPU memory region is extended for the TensorFlow process. Memory is not released since it can lead to memory fragmentation. To turn on memory growth for a specific GPU, use the following code prior to allocating any tensors or executing any ops."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.461315Z",
"iopub.status.busy": "2021-09-24T01:22:04.460715Z",
"iopub.status.idle": "2021-09-24T01:22:04.462631Z",
"shell.execute_reply": "2021-09-24T01:22:04.463062Z"
},
"id": "jr3Kf1boFnCO"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Physical devices cannot be modified after being initialized\n"
]
}
],
"source": [
"gpus = tf.config.list_physical_devices('GPU')\n",
"if gpus:\n",
" try:\n",
" # Currently, memory growth needs to be the same across GPUs\n",
" for gpu in gpus:\n",
" tf.config.experimental.set_memory_growth(gpu, True)\n",
" logical_gpus = tf.config.list_logical_devices('GPU')\n",
" print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n",
" except RuntimeError as e:\n",
" # Memory growth must be set before GPUs have been initialized\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "I1o8t51QFnmv"
},
"source": [
"Another way to enable this option is to set the environmental variable `TF_FORCE_GPU_ALLOW_GROWTH` to `true`. This configuration is platform specific.\n",
"\n",
"The second method is to configure a virtual GPU device with `tf.config.set_logical_device_configuration` and set a hard limit on the total memory to allocate on the GPU."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.468279Z",
"iopub.status.busy": "2021-09-24T01:22:04.467699Z",
"iopub.status.idle": "2021-09-24T01:22:04.470126Z",
"shell.execute_reply": "2021-09-24T01:22:04.469648Z"
},
"id": "2qO2cS9QFn42"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Virtual devices cannot be modified after being initialized\n"
]
}
],
"source": [
"gpus = tf.config.list_physical_devices('GPU')\n",
"if gpus:\n",
" # Restrict TensorFlow to only allocate 1GB of memory on the first GPU\n",
" try:\n",
" tf.config.set_logical_device_configuration(\n",
" gpus[0],\n",
" [tf.config.LogicalDeviceConfiguration(memory_limit=1024)])\n",
" logical_gpus = tf.config.list_logical_devices('GPU')\n",
" print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n",
" except RuntimeError as e:\n",
" # Virtual devices must be set before GPUs have been initialized\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Bsg1iLuHFoLW"
},
"source": [
"This is useful if you want to truly bound the amount of GPU memory available to the TensorFlow process. This is common practice for local development when the GPU is shared with other applications such as a workstation GUI."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B27_-1gyjf-t"
},
"source": [
"## Using a single GPU on a multi-GPU system\n",
"\n",
"If you have more than one GPU in your system, the GPU with the lowest ID will be\n",
"selected by default. If you would like to run on a different GPU, you will need\n",
"to specify the preference explicitly:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.475842Z",
"iopub.status.busy": "2021-09-24T01:22:04.475252Z",
"iopub.status.idle": "2021-09-24T01:22:04.478998Z",
"shell.execute_reply": "2021-09-24T01:22:04.479374Z"
},
"id": "wep4iteljjG1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
}
],
"source": [
"tf.debugging.set_log_device_placement(True)\n",
"\n",
"try:\n",
" # Specify an invalid GPU device\n",
" with tf.device('/device:GPU:2'):\n",
" a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
" b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
" c = tf.matmul(a, b)\n",
"except RuntimeError as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jy-4cCO_jn4G"
},
"source": [
"If the device you have specified does not exist, you will get a `RuntimeError`: `.../device:GPU:2 unknown device`.\n",
"\n",
"If you would like TensorFlow to automatically choose an existing and supported device to run the operations in case the specified one doesn't exist, you can call `tf.config.set_soft_device_placement(True)`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.484601Z",
"iopub.status.busy": "2021-09-24T01:22:04.483993Z",
"iopub.status.idle": "2021-09-24T01:22:04.488834Z",
"shell.execute_reply": "2021-09-24T01:22:04.489181Z"
},
"id": "sut_UHlkjvWd"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[22. 28.]\n",
" [49. 64.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"tf.config.set_soft_device_placement(True)\n",
"tf.debugging.set_log_device_placement(True)\n",
"\n",
"# Creates some tensors\n",
"a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
"b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
"c = tf.matmul(a, b)\n",
"\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sYTYPrQZj2d9"
},
"source": [
"## Using multiple GPUs\n",
"\n",
"Developing for multiple GPUs will allow a model to scale with the additional resources. If developing on a system with a single GPU, you can simulate multiple GPUs with virtual devices. This enables easy testing of multi-GPU setups without requiring additional resources."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.494782Z",
"iopub.status.busy": "2021-09-24T01:22:04.494197Z",
"iopub.status.idle": "2021-09-24T01:22:04.496633Z",
"shell.execute_reply": "2021-09-24T01:22:04.496176Z"
},
"id": "8EMGuGKbNkc6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Virtual devices cannot be modified after being initialized\n"
]
}
],
"source": [
"gpus = tf.config.list_physical_devices('GPU')\n",
"if gpus:\n",
" # Create 2 virtual GPUs with 1GB memory each\n",
" try:\n",
" tf.config.set_logical_device_configuration(\n",
" gpus[0],\n",
" [tf.config.LogicalDeviceConfiguration(memory_limit=1024),\n",
" tf.config.LogicalDeviceConfiguration(memory_limit=1024)])\n",
" logical_gpus = tf.config.list_logical_devices('GPU')\n",
" print(len(gpus), \"Physical GPU,\", len(logical_gpus), \"Logical GPUs\")\n",
" except RuntimeError as e:\n",
" # Virtual devices must be set before GPUs have been initialized\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xmNzO0FxNkol"
},
"source": [
"Once there are multiple logical GPUs available to the runtime, you can utilize the multiple GPUs with `tf.distribute.Strategy` or with manual placement."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IDZmEGq4j6kG"
},
"source": [
"#### With `tf.distribute.Strategy`\n",
"\n",
"The best practice for using multiple GPUs is to use `tf.distribute.Strategy`.\n",
"Here is a simple example:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.502289Z",
"iopub.status.busy": "2021-09-24T01:22:04.501648Z",
"iopub.status.idle": "2021-09-24T01:22:04.709402Z",
"shell.execute_reply": "2021-09-24T01:22:04.709845Z"
},
"id": "1KgzY8V2AvRv"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Using MirroredStrategy with devices ('/job:localhost/replica:0/task:0/device:GPU:0',)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op RandomUniform in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Sub in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Mul in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AddV2 in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Fill in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Fill in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',).\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:CPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',).\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:CPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Fill in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',).\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:CPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',).\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op ReadVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op Identity in device /job:localhost/replica:0/task:0/device:CPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op VarHandleOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op AssignVariableOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op NoOp in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
}
],
"source": [
"tf.debugging.set_log_device_placement(True)\n",
"gpus = tf.config.list_logical_devices('GPU')\n",
"strategy = tf.distribute.MirroredStrategy(gpus)\n",
"with strategy.scope():\n",
" inputs = tf.keras.layers.Input(shape=(1,))\n",
" predictions = tf.keras.layers.Dense(1)(inputs)\n",
" model = tf.keras.models.Model(inputs=inputs, outputs=predictions)\n",
" model.compile(loss='mse',\n",
" optimizer=tf.keras.optimizers.SGD(learning_rate=0.2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Dy7nxlKsAxkK"
},
"source": [
"This program will run a copy of your model on each GPU, splitting the input data\n",
"between them, also known as \"[data parallelism](https://en.wikipedia.org/wiki/Data_parallelism)\".\n",
"\n",
"For more information about distribution strategies, check out the guide [here](./distributed_training.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8phxM5TVkAY_"
},
"source": [
"#### Manual placement\n",
"\n",
"`tf.distribute.Strategy` works under the hood by replicating computation across devices. You can manually implement replication by constructing your model on each GPU. For example:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2021-09-24T01:22:04.717402Z",
"iopub.status.busy": "2021-09-24T01:22:04.716641Z",
"iopub.status.idle": "2021-09-24T01:22:04.722196Z",
"shell.execute_reply": "2021-09-24T01:22:04.721598Z"
},
"id": "AqPo9ltUA_EY"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op _EagerConst in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[22. 28.]\n",
" [49. 64.]], shape=(2, 2), dtype=float32)\n"
]
}
],
"source": [
"tf.debugging.set_log_device_placement(True)\n",
"\n",
"gpus = tf.config.list_logical_devices('GPU')\n",
"if gpus:\n",
" # Replicate your computation on multiple GPUs\n",
" c = []\n",
" for gpu in gpus:\n",
" with tf.device(gpu.name):\n",
" a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
" b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
" c.append(tf.matmul(a, b))\n",
"\n",
" with tf.device('/CPU:0'):\n",
" matmul_sum = tf.add_n(c)\n",
"\n",
" print(matmul_sum)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "gpu.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}