blob: 50bdcf248203e6b739cf58d26b48edad98a511e2 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements; and to You under the Apache License, Version 2.0. "
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# SINGA Core Classes\n",
"\n",
"<img src=\"http://singa.apache.org/en/_static/images/singav1-sw.png\" width=\"500px\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Device\n",
"\n",
"A device instance represents a hardware device with multiple execution units, e.g.,\n",
"* A GPU which has multile cuda streams\n",
"* A CPU which has multiple threads\n",
"\n",
"All data structures (variables) are allocated on a device instance. Consequently, all operations are executed on the resident device.\n",
"\n",
"## Create a device instance"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<singa.singa_wrap.Device; proxy of <Swig Object of type 'std::shared_ptr< singa::Device > *' at 0x7f69a05ff330> >"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from singa import device\n",
"default_dev = device.get_default_device()\n",
"gpu = device.create_cuda_gpu() # the first gpu device\n",
"gpu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** NOTE: currently we can only call the creating function once due to the cnmem restriction.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gpu = device.create_cuda_gpu_on(1) # use the gpu device with the specified GPU ID\n",
"gpu_list1 = device.create_cuda_gpus(2) # the first two gpu devices\n",
"gpu_list2 = device.create_cuda_gpus([0,2]) # create the gpu instances on the given GPU IDs\n",
"opencl_gpu = device.create_opencl_device() # valid if SINGA is compiled with USE_OPENCL=ON"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"device.get_num_gpus()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(0, 1, 2)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"device.get_gpu_ids()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tensor\n",
"\n",
"A tensor instance represents a multi-dimensional array allocated on a device instance.\n",
"It provides linear algbra operations, like +, -, *, /, dot, pow ,etc\n",
"\n",
"NOTE: class memeber functions are inplace; global functions are out-of-place."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create tensor instances"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from singa import tensor\n",
"import numpy as np\n",
"a = tensor.Tensor((2, 3))\n",
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<singa.singa_wrap.Device; proxy of <Swig Object of type 'std::shared_ptr< singa::Device > *' at 0x7f69a02f8a50> >"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.device"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gb = tensor.Tensor((2, 3), gpu)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<singa.singa_wrap.Device; proxy of <Swig Object of type 'std::shared_ptr< singa::Device > *' at 0x7f69a05ff330> >"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gb.device"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Initialize tensor values"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a.set_value(1.2)\n",
"gb.gaussian(0, 0.1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### To and from numpy"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.20000005, 1.20000005, 1.20000005],\n",
" [ 1.20000005, 1.20000005, 1.20000005]], dtype=float32)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.to_numpy(a)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.24042693, -0.21337385, -0.0969397 ],\n",
" [-0.010797 , -0.07642138, -0.09220808]], dtype=float32)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.to_numpy(gb)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2,)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = tensor.from_numpy(np.array([1,2], dtype=np.float32))\n",
"c.shape"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3., 4.], dtype=float32)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.copy_from_numpy(np.array([3,4], dtype=np.float32))\n",
"tensor.to_numpy(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Move tensor between devices"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<singa.singa_wrap.Device; proxy of <Swig Object of type 'std::shared_ptr< singa::Device > *' at 0x7f69a05ff330> >"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gc = c.clone()\n",
"gc.to_device(gpu)\n",
"gc.device"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<singa.singa_wrap.Device; proxy of <Swig Object of type 'std::shared_ptr< singa::Device > *' at 0x7f69a02f8a50> >"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = gb.clone()\n",
"b.to_host() # the same as b.to_device(default_dev)\n",
"b.device"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operations\n",
"\n",
"**NOTE: tensors should be initialized if the operation would read the tensor values**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Summary"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.12169448286294937"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gb.l1()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.4898979663848877"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.l2()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e = tensor.Tensor((2, 3))\n",
"e.is_empty()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6L"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gb.size()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"24L"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gb.memsize()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# note we can only support matrix multiplication for tranposed tensors; \n",
"# other operations on transposed tensor would result in errors\n",
"c.is_transpose() "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"et=e.T()\n",
"et.is_transpose()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 2L)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"et.shape"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2L"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"et.ndim()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Member functions (in-place)\n",
"\n",
"These functions would change the content of the tensor"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.44042695, 0.98662621, 1.10306036],\n",
" [ 1.18920302, 1.12357867, 1.10779202]], dtype=float32)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a += b\n",
"tensor.to_numpy(a)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.20000005, 1.20000005, 1.20000005],\n",
" [ 1.20000005, 1.20000005, 1.20000005]], dtype=float32)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a -= b\n",
"tensor.to_numpy(a)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2.4000001, 2.4000001, 2.4000001],\n",
" [ 2.4000001, 2.4000001, 2.4000001]], dtype=float32)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a *= 2\n",
"tensor.to_numpy(a)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.80000007, 0.80000007, 0.80000007],\n",
" [ 0.80000007, 0.80000007, 0.80000007]], dtype=float32)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a /= 3\n",
"tensor.to_numpy(a)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.62944734, -0.72904599, 0.81158388], dtype=float32)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = tensor.Tensor((3,))\n",
"d.uniform(-1,1)\n",
"tensor.to_numpy(d)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.42944741, 0.07095408, 1.61158395],\n",
" [ 1.42944741, 0.07095408, 1.61158395]], dtype=float32)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.add_row(d)\n",
"tensor.to_numpy(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Global functions (out of place)\n",
"\n",
"These functions would not change the memory of the tensor, instead they return a new tensor\n",
"\n",
"**Unary functions**"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., -1., 1.], dtype=float32)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h = tensor.sign(d)\n",
"tensor.to_numpy(h)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.62944734, -0.72904599, 0.81158388], dtype=float32)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.to_numpy(d)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.62944734, 0.72904599, 0.81158388], dtype=float32)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h = tensor.abs(d)\n",
"tensor.to_numpy(h)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.62944734, 0. , 0.81158388], dtype=float32)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h = tensor.relu(d)\n",
"tensor.to_numpy(h)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L,)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = tensor.sum(a, 0)\n",
"g.shape"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2L,)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = tensor.sum(a, 1)\n",
"g.shape"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 0.], dtype=float32)"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.bernoulli(0.5, g)\n",
"tensor.to_numpy(g)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.12226005, -0.05827543], dtype=float32)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.gaussian(0, 0.2)\n",
"tensor.gaussian(0, 0.2, g)\n",
"tensor.to_numpy(g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Binary functions"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.66987431, -0.14241977, 1.51464427],\n",
" [ 1.41865039, -0.0054673 , 1.51937592]], dtype=float32)"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = a + b\n",
"tensor.to_numpy(f)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0.],\n",
" [ 0., 0., 0.]], dtype=float32)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = a < b\n",
"tensor.to_numpy(g)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 7.66987419, 5.85758018, 7.51464415],\n",
" [ 9.41865063, 7.99453259, 9.5193758 ]], dtype=float32)"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.add_column(2, c, 1, f) # f = 2 *c + 1* f\n",
"tensor.to_numpy(f)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### BLAS\n",
"\n",
"BLAS function may change the memory of input tensor"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.24042693, -0.21337385, -0.0969397 ],\n",
" [-0.010797 , -0.07642138, -0.09220808]], dtype=float32)"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.axpy(2, a, f) # f = 2a + f\n",
"tensor.to_numpy(b)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.17231143, -0.16945721],\n",
" [ 0.17231143, -0.16945721]], dtype=float32)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = tensor.mult(a, b.T())\n",
"tensor.to_numpy(f)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.51693428, -0.50837165],\n",
" [ 0.51693428, -0.50837165]], dtype=float32)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tensor.mult(a, b.T(), f, 2, 1) # f = 2a*b.T() + 1f\n",
"tensor.to_numpy(f)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Next: [SINGA model classes](./model.ipynb)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:pysinga]",
"language": "python",
"name": "conda-env-pysinga-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}