| { |
| "cells": [ |
| { |
| "cell_type": "markdown", |
| "id": "39627b82", |
| "metadata": {}, |
| "source": [ |
| "<!--- Licensed to the Apache Software Foundation (ASF) under one -->\n", |
| "<!--- or more contributor license agreements. See the NOTICE file -->\n", |
| "<!--- distributed with this work for additional information -->\n", |
| "<!--- regarding copyright ownership. The ASF licenses this file -->\n", |
| "<!--- to you under the Apache License, Version 2.0 (the -->\n", |
| "<!--- \"License\"); you may not use this file except in compliance -->\n", |
| "<!--- with the License. You may obtain a copy of the License at -->\n", |
| "\n", |
| "<!--- http://www.apache.org/licenses/LICENSE-2.0 -->\n", |
| "\n", |
| "<!--- Unless required by applicable law or agreed to in writing, -->\n", |
| "<!--- software distributed under the License is distributed on an -->\n", |
| "<!--- \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->\n", |
| "<!--- KIND, either express or implied. See the License for the -->\n", |
| "<!--- specific language governing permissions and limitations -->\n", |
| "<!--- under the License. -->\n", |
| "\n", |
| "# NDArray Contexts\n", |
| "\n", |
| "## Overview\n", |
| "This guide will introduce you to managing CPU versus GPU contexts for handling data.\n", |
| "\n", |
| "This content was extracted and simplified from the gluon tutorials in\n", |
| "[Dive Into Deep Learning](https://d2l.ai/).\n", |
| "\n", |
| "## Prerequisites\n", |
| "* [MXNet installed (with GPU support) in a Python environment](https://mxnet.apache.org/get_started).\n", |
| "* Python 2.7.x or Python 3.x\n", |
| "* **One or more GPUs**\n", |
| "\n", |
| "\n", |
| "## Managing Context\n", |
| "\n", |
| "In MXNet, every array has a context.\n", |
| "One context could be the CPU. Other contexts might be various GPUs.\n", |
| "Things can get even hairier when we deploy jobs across multiple servers.\n", |
| "By assigning arrays to contexts intelligently, we can minimize\n", |
| "the time spent transferring data between devices.\n", |
| "For example, when training neural networks on a server with a GPU,\n", |
| "we typically prefer for the model's parameters to live on the GPU.\n", |
| "If you have a GPU, let's try initializing an array on the first GPU.\n", |
| "Otherwise, use `ctx=mx.cpu()` in place of `ctx=gpu(0)`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "d49cd553", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "from mxnet import gpu\n", |
| "from mxnet import nd\n", |
| "z = nd.ones(shape=(3,3), ctx=gpu(0))\n", |
| "print(z)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "efd74ea1", |
| "metadata": {}, |
| "source": [ |
| "Given an NDArray on a given context, we can copy it to another context by using\n", |
| "the copyto() method. Skip this if you don't have a GPU at the moment." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "7246f821", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x_gpu = x.copyto(gpu(0))\n", |
| "print(x_gpu)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "26f06953", |
| "metadata": {}, |
| "source": [ |
| "The result of an operator will have the same context as the inputs." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "c79f2d97", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x_gpu + z" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "2d57ef4d", |
| "metadata": {}, |
| "source": [ |
| "## Watch out!\n", |
| "\n", |
| "Imagine that your variable z already lives on your second GPU\n", |
| "(`gpu(0)`). What happens if we call `z.copyto(gpu(0))`? It will make a copy and\n", |
| "allocate new memory, even though that variable already lives on the desired\n", |
| "device!\n", |
| "<!-- wouldn't the second GPU be gpu(1)? -->\n", |
| "\n", |
| "Often, we only want to make\n", |
| "a copy if the variable currently lives in the wrong context. In these cases, we\n", |
| "can call `as_in_context()`. If the variable is already on `gpu(0)` then this is\n", |
| "a no-op." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "7b20f6a9", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "print('id(z):', id(z))\n", |
| "z = z.copyto(gpu(0))\n", |
| "print('id(z):', id(z))\n", |
| "z = z.as_in_context(gpu(0))\n", |
| "print('id(z):', id(z))\n", |
| "print(z)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "9b0f6d6b", |
| "metadata": {}, |
| "source": [ |
| "## Next Up\n", |
| "\n", |
| "[Back to NDArray API Guides](../../../../api/legacy/ndarray/index.rst)" |
| ] |
| } |
| ], |
| "metadata": { |
| "language_info": { |
| "name": "python" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |