MXNet: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Added Running job example of single convolution)
(Marked this version for translation)
Line 41: Line 41:
}}
}}


== Running a job ==
== Running a job == <!--T:13-->
A single Convolution layer:
A single Convolution layer:
{{File
{{File
Line 49: Line 49:
#!/bin/env python
#!/bin/env python


<!--T:14-->
import mxnet as mx
import mxnet as mx
import numpy as np
import numpy as np


<!--T:15-->
num_filter = 32
num_filter = 32
kernel = (3, 3)
kernel = (3, 3)
Line 57: Line 59:
shape = (32, 32, 256, 256)
shape = (32, 32, 256, 256)


<!--T:16-->
x = mx.sym.Variable('x')
x = mx.sym.Variable('x')
w = mx.sym.Variable('w')
w = mx.sym.Variable('w')
y = mx.sym.Convolution(data=x, weight=w, num_filter=num_filter, kernel=kernel, no_bias=True, pad=pad)
y = mx.sym.Convolution(data=x, weight=w, num_filter=num_filter, kernel=kernel, no_bias=True, pad=pad)


<!--T:17-->
device = mx.gpu() if mx.context.num_gpus() > 0 else mx.cpu()
device = mx.gpu() if mx.context.num_gpus() > 0 else mx.cpu()


<!--T:18-->
# On CPU will use MKLDNN, or will use cuDNN
# On CPU will use MKLDNN, or will use cuDNN
exe = y.simple_bind(device, x=shape)
exe = y.simple_bind(device, x=shape)


<!--T:19-->
exe.arg_arrays[0][:] = np.random.normal(size=exe.arg_arrays[0].shape)
exe.arg_arrays[0][:] = np.random.normal(size=exe.arg_arrays[0].shape)
exe.arg_arrays[1][:] = np.random.normal(size=exe.arg_arrays[1].shape)
exe.arg_arrays[1][:] = np.random.normal(size=exe.arg_arrays[1].shape)


<!--T:20-->
exe.forward(is_train=False)
exe.forward(is_train=False)
o = exe.outputs[0]
o = exe.outputs[0]
Line 75: Line 82:
}}
}}


<!--T:21-->
2. Edit the following submission script according to your needs.
2. Edit the following submission script according to your needs.
<tabs>
<tabs>
Line 84: Line 92:
#!/bin/bash
#!/bin/bash


<!--T:22-->
#SBATCH --job-name=mxnet-conv
#SBATCH --job-name=mxnet-conv
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
Line 90: Line 99:
#SBATCH --mem=20G                # adjust this according to the memory you need
#SBATCH --mem=20G                # adjust this according to the memory you need


<!--T:23-->
# Load modules dependencies
# Load modules dependencies
module load python/3.10
module load python/3.10


<!--T:24-->
# Generate your virtual environment in $SLURM_TMPDIR
# Generate your virtual environment in $SLURM_TMPDIR
virtualenv --no-download ${SLURM_TMPDIR}/env
virtualenv --no-download ${SLURM_TMPDIR}/env
source ${SLURM_TMPDIR}/env/bin/activate
source ${SLURM_TMPDIR}/env/bin/activate


<!--T:25-->
# Install MXNet and its dependencies
# Install MXNet and its dependencies
pip install --no-index mxnet==1.9.1
pip install --no-index mxnet==1.9.1


<!--T:26-->
# Will use MKLDNN
# Will use MKLDNN
python mxnet-conv-ex.py
python mxnet-conv-ex.py
Line 105: Line 118:
</tab>
</tab>


<!--T:27-->
<tab name="GPU">
<tab name="GPU">
{{File
{{File
Line 112: Line 126:
#!/bin/bash
#!/bin/bash


<!--T:28-->
#SBATCH --job-name=mxnet-conv
#SBATCH --job-name=mxnet-conv
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
Line 119: Line 134:
#SBATCH --gres=gpu:1              # adjust this to match the number of GPUs, unless distributed training, use 1
#SBATCH --gres=gpu:1              # adjust this to match the number of GPUs, unless distributed training, use 1


<!--T:29-->
# Load modules dependencies
# Load modules dependencies
module load python/3.10
module load python/3.10


<!--T:30-->
# Generate your virtual environment in $SLURM_TMPDIR
# Generate your virtual environment in $SLURM_TMPDIR
virtualenv --no-download ${SLURM_TMPDIR}/env
virtualenv --no-download ${SLURM_TMPDIR}/env
source ${SLURM_TMPDIR}/env/bin/activate
source ${SLURM_TMPDIR}/env/bin/activate


<!--T:31-->
# Install MXNet and its dependencies
# Install MXNet and its dependencies
pip install --no-index mxnet==1.9.1
pip install --no-index mxnet==1.9.1


<!--T:32-->
# Will use cuDNN
# Will use cuDNN
python mxnet-conv-ex.py
python mxnet-conv-ex.py
Line 135: Line 154:
</tabs>
</tabs>


<!--T:33-->
3. Submit the job to the scheduler.
3. Submit the job to the scheduler.
{{Command
{{Command

Revision as of 15:21, 13 July 2022

Other languages:

Apache MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to mix symbolic and imperative programming to maximize efficiency and productivity. At its core, MXNet contains a dynamic dependency scheduler that automatically parallelizes both symbolic and imperative operations on the fly. A graph optimization layer on top of that makes symbolic execution fast and memory efficient. MXNet is portable and lightweight, scalable to many GPUs and machines.

Available wheels

You can list available wheels using the avail_wheels command.

Question.png
[name@server ~]$ avail_wheels mxnet
name    version    python    arch
------  ---------  --------  ------
mxnet   1.9.1      cp39      avx2
mxnet   1.9.1      cp38      avx2
mxnet   1.9.1      cp310     avx2

Installing in a Python virtual environment

1. Create and activate a Python virtual environment.

[name@server ~]$ module load python/3.10
[name@server ~]$ virtualenv --no-download ~/env
[name@server ~]$ source ~/env/bin/activate


2. Install MXNet and its Python dependencies.

Question.png
(env) [name@server ~] pip install --no-index mxnet

3. Validate it.

Question.png
(env) [name@server ~] python -c "import mxnet as mx;print((mx.nd.ones((2, 3))*2).asnumpy());"
[[2. 2. 2.]
 [2. 2. 2.]]

Running a job

A single Convolution layer:

File : mxnet-conv-ex.py

#!/bin/env python

import mxnet as mx
import numpy as np

num_filter = 32
kernel = (3, 3)
pad = (1, 1)
shape = (32, 32, 256, 256)

x = mx.sym.Variable('x')
w = mx.sym.Variable('w')
y = mx.sym.Convolution(data=x, weight=w, num_filter=num_filter, kernel=kernel, no_bias=True, pad=pad)

device = mx.gpu() if mx.context.num_gpus() > 0 else mx.cpu()

# On CPU will use MKLDNN, or will use cuDNN
exe = y.simple_bind(device, x=shape)

exe.arg_arrays[0][:] = np.random.normal(size=exe.arg_arrays[0].shape)
exe.arg_arrays[1][:] = np.random.normal(size=exe.arg_arrays[1].shape)

exe.forward(is_train=False)
o = exe.outputs[0]
t = o.asnumpy()
print(t)


2. Edit the following submission script according to your needs.

File : mxnet-conv.sh

#!/bin/bash

#SBATCH --job-name=mxnet-conv
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
#SBATCH --time=01:00:00           # adjust this to match the walltime of your job
#SBATCH --cpus-per-task=2         # adjust this to match the number of cores
#SBATCH --mem=20G                 # adjust this according to the memory you need

# Load modules dependencies
module load python/3.10

# Generate your virtual environment in $SLURM_TMPDIR
virtualenv --no-download ${SLURM_TMPDIR}/env
source ${SLURM_TMPDIR}/env/bin/activate

# Install MXNet and its dependencies
pip install --no-index mxnet==1.9.1

# Will use MKLDNN
python mxnet-conv-ex.py


File : mxnet-conv.sh

#!/bin/bash

#SBATCH --job-name=mxnet-conv
#SBATCH --account=def-someprof    # adjust this to match the accounting group you are using to submit jobs
#SBATCH --time=01:00:00           # adjust this to match the walltime of your job
#SBATCH --cpus-per-task=2         # adjust this to match the number of cores
#SBATCH --mem=20G                 # adjust this according to the memory you need
#SBATCH --gres=gpu:1              # adjust this to match the number of GPUs, unless distributed training, use 1

# Load modules dependencies
module load python/3.10

# Generate your virtual environment in $SLURM_TMPDIR
virtualenv --no-download ${SLURM_TMPDIR}/env
source ${SLURM_TMPDIR}/env/bin/activate

# Install MXNet and its dependencies
pip install --no-index mxnet==1.9.1

# Will use cuDNN
python mxnet-conv-ex.py


3. Submit the job to the scheduler.

Question.png
[name@server ~]$ sbatch mxnet-conv.sh