Skip to content

Instantly share code, notes, and snippets.

@unnonouno
Last active September 10, 2017 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnonouno/c8e10dfbd9b22aae894e98382d3a3175 to your computer and use it in GitHub Desktop.
Save unnonouno/c8e10dfbd9b22aae894e98382d3a3175 to your computer and use it in GitHub Desktop.
import cupy as xp
import cupy.prof
import chainer
from chainer.functions.activation import lstm
B = 1024
D = 2048
x_shape = (B, D * 4)
c_shape = (B, D)
x = chainer.Variable(xp.random.uniform(-1, 1, x_shape).astype('f'))
c_p = chainer.Variable(xp.random.uniform(-1, 1, c_shape).astype('f'))
c = chainer.Variable(xp.random.uniform(-1, 1, c_shape).astype('f'))
gc = chainer.Variable(xp.random.uniform(-1, 1, c_shape).astype('f'))
gh= chainer.Variable(xp.random.uniform(-1, 1, c_shape).astype('f'))
with cupy.prof.time_range('lstm forward', color_id=0):
c, h = lstm.LSTMGrad()(c_p, x, c, gc, gh)
cupy.cuda.Stream.null.synchronize()
l = chainer.functions.sum(c) + chainer.functions.sum(h)
print(l)
with cupy.prof.time_range('lstm backward', color_id=0):
l.backward()
cupy.cuda.Stream.null.synchronize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment