Skip to content

Instantly share code, notes, and snippets.

@mitmul
Last active September 3, 2017 09:57
Show Gist options
  • Save mitmul/b0041b4afd58eaf9489b423cb0345bbb to your computer and use it in GitHub Desktop.
Save mitmul/b0041b4afd58eaf9489b423cb0345bbb to your computer and use it in GitHub Desktop.
import chainer
import chainer.functions as F
import numpy as np
import cupy as cp
import time
from chainer import cuda
import sys
if sys.argv[1] == 'cpu':
xp = np
elif sys.argv[1] == 'gpu':
xp = cp
cuda.get_device_from_id(0).use()
print('Testing with:', sys.argv[1])
print('cuda:', chainer.cuda.available, 'cudnn:', chainer.cuda.cudnn_enabled)
x = xp.random.randn(16, 64, 32, 32).astype(np.float32)
x = chainer.Variable(x)
with chainer.using_config('use_cudnn', 'never'):
mpool = F.MaxPooling2D(2, 2)
y = mpool(x)
y.creator = None
mean = []
print('forward:')
def forward():
return F.upsampling_2d(y, mpool.indexes, 2)
forward()
forward()
forward()
forward()
forward()
for _ in range(10):
st = time.time()
yy = forward()
yy.to_cpu()
mean.append(time.time() - st)
print('mean:{:.04f} +- {:.04f} sec'.format(np.mean(mean), np.std(mean)))
if sys.argv[1] == 'gpu':
yy.to_gpu(0)
yy.grad = xp.zeros_like(yy.data).astype(xp.float32)
mean = []
print('backward:')
def backward():
yy.backward()
cuda.to_cpu(y.grad)
backward()
backward()
backward()
backward()
backward()
for _ in range(10):
st = time.time()
backward()
mean.append(time.time() - st)
print('mean: {:.04f} +- {:.04f} sec'.format(np.mean(mean), np.std(mean)))
print('')
#!/bin/bash
rm -rf result.txt
pip uninstall -y chainer
pip install git+git://github.com/chainer/chainer
echo "Chainer:master"
python check_performance_of_upsampling_2d.py cpu >> result.txt
python check_performance_of_upsampling_2d.py gpu >> result.txt
pip uninstall -y chainer
pip install git+git://github.com/mitmul/chainer@improve-upsampling-2d
echo "Chainer:simplify"
python check_performance_of_upsampling_2d.py cpu >> result.txt
python check_performance_of_upsampling_2d.py gpu >> result.txt
Testing with: cpu
cuda: True cudnn: True
forward:
mean:0.3773 +- 0.0086 sec
backward:
mean: 0.1923 +- 0.0210 sec
Testing with: gpu
cuda: True cudnn: True
forward:
mean:0.0027 +- 0.0005 sec
backward:
mean: 0.0011 +- 0.0000 sec
Testing with: cpu
cuda: True cudnn: True
forward:
mean:0.0153 +- 0.0004 sec
backward:
mean: 0.0110 +- 0.0003 sec
Testing with: gpu
cuda: True cudnn: True
forward:
mean:0.0023 +- 0.0004 sec
backward:
mean: 0.0008 +- 0.0000 sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment