{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Concise Implementation of Multi-GPU Computation"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:50:25.762210Z",
"start_time": "2019-07-03T22:50:22.940185Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "1"
}
},
"outputs": [],
"source": [
"import d2l\n",
"from mxnet import autograd, gluon, init, np, npx\n",
"from mxnet.gluon import nn\n",
"npx.set_np()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Initialize model parameters on multiple GPUs\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:50:32.537906Z",
"start_time": "2019-07-03T22:50:25.764263Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "3"
}
},
"outputs": [],
"source": [
"net = d2l.resnet18(10)\n",
"ctx = d2l.try_all_gpus()\n",
"net.initialize(init=init.Normal(sigma=0.01), ctx=ctx)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Test"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:50:32.772457Z",
"start_time": "2019-07-03T22:50:32.539859Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "4"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(array([[ 5.48149364e-06, -8.33710089e-07, -1.63167692e-06,\n",
" -6.36740765e-07, -3.82161761e-06, -2.35140669e-06,\n",
" -2.54695851e-06, -9.47824219e-08, -6.90335582e-07,\n",
" 2.57562374e-06],\n",
" [ 5.47108630e-06, -9.42463600e-07, -1.04940591e-06,\n",
" 9.80820687e-08, -3.32518266e-06, -2.48629135e-06,\n",
" -3.36428002e-06, 1.04560286e-07, -6.10012194e-07,\n",
" 2.03278501e-06]], ctx=gpu(0)),\n",
" array([[ 5.6176350e-06, -1.2837600e-06, -1.4605525e-06, 1.8302978e-07,\n",
" -3.5511648e-06, -2.4371018e-06, -3.5731791e-06, -3.0974837e-07,\n",
" -1.1016566e-06, 1.8909888e-06],\n",
" [ 5.1418701e-06, -1.3729926e-06, -1.1520079e-06, 1.1507450e-07,\n",
" -3.7372806e-06, -2.8289705e-06, -3.6477188e-06, 1.5781586e-07,\n",
" -6.0733169e-07, 1.9712008e-06]], ctx=gpu(1)))"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = np.random.uniform(size=(4, 1, 28, 28))\n",
"gpu_x = gluon.utils.split_and_load(x, ctx)\n",
"net(gpu_x[0]), net(gpu_x[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Evaluating accuracy on multiple GPUs"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:50:32.779294Z",
"start_time": "2019-07-03T22:50:32.774319Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "6"
}
},
"outputs": [],
"source": [
"def evaluate_accuracy_gpus(net, data_iter):\n",
" # Query the list of devices.\n",
" ctx_list = list(net.collect_params().values())[0].list_ctx()\n",
" metric = d2l.Accumulator(2) # num_corrected_examples, num_examples\n",
" for features, labels in data_iter:\n",
" Xs, ys = d2l.split_batch(features, labels, ctx_list)\n",
" pys = [net(X) for X in Xs] # run in parallel\n",
" metric.add(sum(float(d2l.accuracy(py, y)) for py, y in zip(pys, ys)), \n",
" labels.size)\n",
" return metric[0]/metric[1]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The training function"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:50:32.788932Z",
"start_time": "2019-07-03T22:50:32.780660Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "7"
}
},
"outputs": [],
"source": [
"def train(num_gpus, batch_size, lr):\n",
" train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)\n",
" ctx_list = [d2l.try_gpu(i) for i in range(num_gpus)]\n",
" net.initialize(init=init.Normal(sigma=0.01),\n",
" ctx=ctx_list, force_reinit=True)\n",
" trainer = gluon.Trainer(\n",
" net.collect_params(), 'sgd', {'learning_rate': lr})\n",
" loss = gluon.loss.SoftmaxCrossEntropyLoss()\n",
" timer, num_epochs = d2l.Timer(), 10\n",
" animator = d2l.Animator('epoch', 'test acc', xlim=[1, num_epochs])\n",
" for epoch in range(num_epochs):\n",
" timer.start()\n",
" for features, labels in train_iter:\n",
" Xs, ys = d2l.split_batch(features, labels, ctx_list)\n",
" with autograd.record():\n",
" ls = [loss(net(X), y) for X, y in zip(Xs, ys)]\n",
" for l in ls:\n",
" l.backward()\n",
" trainer.step(batch_size)\n",
" npx.waitall()\n",
" timer.stop()\n",
" animator.add(epoch+1, (evaluate_accuracy_gpus(net, test_iter),))\n",
" print('test acc: %.2f, %.1f sec/epoch on %s' % (\n",
" animator.Y[0][-1], timer.avg(), ctx_list))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Use a single GPU for training"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-03T22:52:56.382631Z",
"start_time": "2019-07-03T22:50:32.790237Z"
},
"attributes": {
"classes": [],
"id": "",
"n": "8"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test acc: 0.93, 13.2 sec/epoch on [gpu(0)]\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"