Skip to content

Instantly share code, notes, and snippets.

@delta2323
Last active November 25, 2016 00:21
Show Gist options
  • Save delta2323/67a49511ca0c6a0c4401ee4ebc1bf45a to your computer and use it in GitHub Desktop.
Save delta2323/67a49511ca0c6a0c4401ee4ebc1bf45a to your computer and use it in GitHub Desktop.
import chainer
from chainer import links as L
from chainer import utils
import warnings
import time
import numpy as np
import cupy as cp
warnings.simplefilter('ignore')
def evaluate(f):
st = time.time()
for _ in range(100):
f()
ed = time.time()
print(ed - st)
def linear_function(xp, experimental=None):
if experimental is not None:
if experimental == 'no_api_name':
utils.experimental()
else:
utils.experimental(experimental)
x = chainer.Variable(xp.random.uniform(-1, 1, (128, 128)).astype(np.float32))
l = L.Linear(128, 128)
if xp is cp:
l.to_gpu()
l(x)
def f():
pass
def g():
utils.experimental('g')
def h():
utils.experimental()
evaluate(f)
evaluate(g)
evaluate(h)
print()
evaluate(lambda : linear_function(np))
evaluate(lambda : linear_function(np, 'foo'))
evaluate(lambda : linear_function(np, 'no_api_name'))
print()
linear_function(cp) # caching
evaluate(lambda : linear_function(cp))
evaluate(lambda : linear_function(cp, 'foo'))
evaluate(lambda : linear_function(cp, 'no_api_name'))
'''
6.67572021484375e-06
0.000202178955078125
0.05139327049255371
0.1634047031402588
0.1674811840057373
0.22064852714538574
0.11320042610168457
0.113677978515625
0.14753389358520508
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment