singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [singa] dcslin commented on a change in pull request #662: CUDNN LSTM
Date Sat, 11 Apr 2020 04:26:21 GMT
dcslin commented on a change in pull request #662: CUDNN LSTM
URL: https://github.com/apache/singa/pull/662#discussion_r407015879
 
 

 ##########
 File path: python/singa/autograd.py
 ##########
 @@ -3239,176 +3238,152 @@ def __init__(self,
             bidirectional (bool): If True, becomes a bidirectional RNN. 
                 Default: False
         """
-        self.backend = backend
-        if backend == "singa":
-            self.nonlinearity = nonlinearity
-
-            Wx_shape = (input_size, hidden_size)
-            self.Wx = []
-            for i in range(4):
-                w = Tensor(shape=Wx_shape, requires_grad=True, stores_grad=True)
-                w.gaussian(0.0, 1.0)
-                self.Wx.append(w)
-
-            Wh_shape = (hidden_size, hidden_size)
-            self.Wh = []
-            for i in range(4):
-                w = Tensor(shape=Wh_shape, requires_grad=True, stores_grad=True)
-                w.gaussian(0.0, 1.0)
-                self.Wh.append(w)
-
-            Bx_shape = (hidden_size,)
-            self.Bx = []
-            for i in range(4):
-                b = Tensor(shape=Bx_shape, requires_grad=True, stores_grad=True)
-                b.set_value(0.0)
-                self.Bx.append(b)
-
-            self.Bh = []
-            for i in range(4):
-                b = Tensor(shape=Bx_shape, requires_grad=True, stores_grad=True)
-                b.set_value(0.0)
-                self.Bh.append(b)
-
-            self.params = self.Wx + self.Wh + self.Bx + self.Bh
-        elif backend == "cudnn":
-            if not singa.USE_CUDA:
-                raise Exception("Could not use cudnn without cuda compiled.\n")
-            if not inputs:
-                raise Exception("Input is required for init cudnn LSTM.\n")
-
-            CUDNN_LSTM_MODE = 2
-
-            cinputs = singa.VecTensor()
-            [cinputs.append(i.data) for i in inputs]
-            self.rnn_handle = singa.CudnnRNNHandle(cinputs, input_size,
-                                                   hidden_size, CUDNN_LSTM_MODE)
-        else:
-            raise Exception("Unsupported backend %s.\n" % backend)
-
-    def cpp_vec_tensor_to_py_tensor(self, cpp_vec_tensor):
-        py_tensors = list()
-        for cTensor in cpp_vec_tensor:
-            new_t = tensor.Tensor()
-            new_t.data = cTensor
-            new_t.shape = tuple(new_t.data.shape())
-            new_t.device = new_t.data.device()
-            new_t.dtype = new_t.data.data_type()
-            py_tensors.append(new_t)
-        return py_tensors
+        self.nonlinearity = nonlinearity
+
+        Wx_shape = (input_size, hidden_size)
+        self.Wx = []
+        for i in range(4):
+            w = Tensor(shape=Wx_shape, requires_grad=True, stores_grad=True)
+            w.gaussian(0.0, 1.0)
+            self.Wx.append(w)
+
+        Wh_shape = (hidden_size, hidden_size)
+        self.Wh = []
+        for i in range(4):
+            w = Tensor(shape=Wh_shape, requires_grad=True, stores_grad=True)
+            w.gaussian(0.0, 1.0)
+            self.Wh.append(w)
+
+        Bx_shape = (hidden_size,)
+        self.Bx = []
+        for i in range(4):
+            b = Tensor(shape=Bx_shape, requires_grad=True, stores_grad=True)
+            b.set_value(0.0)
+            self.Bx.append(b)
+
+        self.Bh = []
+        for i in range(4):
+            b = Tensor(shape=Bx_shape, requires_grad=True, stores_grad=True)
+            b.set_value(0.0)
+            self.Bh.append(b)
+
+        self.params = self.Wx + self.Wh + self.Bx + self.Bh
 
     def __call__(self, xs, h0_c0):
-        if self.backend == "singa":
-            # xs: a tuple or list of input tensors
-            # h0_c0: a tuple of (h0, c0)
-            h0, c0 = h0_c0
-            if not isinstance(xs, list):
-                xs = list(xs)
-            inputs = xs + list((h0, c0))
-            self.device_check(*inputs)
-            # self.device_check(inputs[0], *self.params)
-            self.device_check(inputs[0],
-                              *(self.Wx + self.Wh + self.Bx + self.Bh))
-            batchsize = xs[0].shape[0]
-            out = []
-            h, c = self.step_forward(xs[0], h0, c0, self.Wx, self.Wh, self.Bx,
+        # xs: a tuple or list of input tensors
+        # h0_c0: a tuple of (h0, c0)
+        h0, c0 = h0_c0
+        if not isinstance(xs, list):
+            xs = list(xs)
+        inputs = xs + list((h0, c0))
+        self.device_check(*inputs)
+        # self.device_check(inputs[0], *self.params)
+        self.device_check(inputs[0],
+                          *(self.Wx + self.Wh + self.Bx + self.Bh))
+        batchsize = xs[0].shape[0]
+        out = []
+        h, c = self.step_forward(xs[0], h0, c0, self.Wx, self.Wh, self.Bx,
+                                 self.Bh)
+        out.append(h)
+        for x in xs[1:]:
+            assert x.shape[0] == batchsize
+            h, c = self.step_forward(x, h, c, self.Wx, self.Wh, self.Bx,
                                      self.Bh)
             out.append(h)
-            for x in xs[1:]:
-                assert x.shape[0] == batchsize
-                h, c = self.step_forward(x, h, c, self.Wx, self.Wh, self.Bx,
-                                         self.Bh)
-                out.append(h)
-            return out, h, c
-        elif self.backend == "cudnn":
-            if not singa.USE_CUDA:
-                raise Exception("Could not use cudnn without cuda compiled.\n")
+        return out, h, c
 
-            cpp_x = singa.VecTensor()
-            [cpp_x.append(i.data) for i in xs]
+    def step_forward(self, x, h, c, Wx, Wh, Bx, Bh):
+        y1 = matmul(x, Wx[0])
+        y1 = add_bias(y1, Bx[0], axis=0)
+        y2 = matmul(h, Wh[0])
+        y2 = add_bias(y2, Bh[0], axis=0)
+        i = add(y1, y2)
+        i = sigmoid(i)
+
+        y1 = matmul(x, Wx[1])
+        y1 = add_bias(y1, Bx[1], axis=0)
+        y2 = matmul(h, Wh[1])
+        y2 = add_bias(y2, Bh[1], axis=0)
+        f = add(y1, y2)
+        f = sigmoid(f)
+
+        y1 = matmul(x, Wx[2])
+        y1 = add_bias(y1, Bx[2], axis=0)
+        y2 = matmul(h, Wh[2])
+        y2 = add_bias(y2, Bh[2], axis=0)
+        o = add(y1, y2)
+        o = sigmoid(o)
+
+        y1 = matmul(x, Wx[3])
+        y1 = add_bias(y1, Bx[3], axis=0)
+        y2 = matmul(h, Wh[3])
+        y2 = add_bias(y2, Bh[3], axis=0)
+        g = add(y1, y2)
+        g = tanh(g)
+
+        cout1 = mul(f, c)
+        cout2 = mul(i, g)
+        cout = add(cout1, cout2)
+
+        hout = tanh(cout)
+        hout = mul(o, hout)
+        return hout, cout
+
+
+class _RNN_cudnn(Operation):
+    def __init__(self, handle):
+        super(_RNN_cudnn, self).__init__()
+        self.handle = handle
 
-            self.W = Tensor(shape=(self.rnn_handle.weights_size,),
-                            requires_grad=True,
-                            stores_grad=True)
-            self.W.gaussian(0.0, 1.0)
+    def forward(self, x, W):
+        y = singa.GpuRNNForwardTraining(x, W, self.handle)
+        if training:
+            self.inputs = (x, W, y)
+        return y
 
-            cpp_y = singa.GpuRNNForwardTraining(cpp_x, self.W.data,
-                                                self.rnn_handle)
-            y = self.cpp_vec_tensor_to_py_tensor(cpp_y)
+    def backward(self, dy):
+        assert training is True and hasattr(
+            self, "inputs"), "Please set training as True before do BP. "
 
-            if training:
-                self.buffer = {"cpp_y": cpp_y, "cpp_x": cpp_x}
+        dx = singa.GpuRNNBackwardx(self.inputs[2], dy, self.inputs[1], self.handle)
+        dW = singa.GpuRNNBackwardW(self.inputs[0], self.inputs[2], self.handle)
+        return dx, dW
 
-            return y
-        else:
-            raise Exception("Unsupported backend %s.\n" % backend)
+class RNN_cudnn(Layer):
+    def __init__(self, input_size, hidden_size, rnn_mode=2):
+        """
+            Args:
+                input_size: input feature dim
+                hidden_size: hidden feature dim
+                rnn_mode: could be 0 - RNN RELU, 1 - RNN TANH, 2 - LSTM, 3 - GRU
+        """
+        if not singa.USE_CUDA:
+            raise Exception("Could not use cudnn without cuda compiled.\n")
 
-    def step_forward(self, x, h, c, Wx, Wh, Bx, Bh):
-        if self.backend == "singa":
-            y1 = matmul(x, Wx[0])
-            y1 = add_bias(y1, Bx[0], axis=0)
-            y2 = matmul(h, Wh[0])
-            y2 = add_bias(y2, Bh[0], axis=0)
-            i = add(y1, y2)
-            i = sigmoid(i)
-
-            y1 = matmul(x, Wx[1])
-            y1 = add_bias(y1, Bx[1], axis=0)
-            y2 = matmul(h, Wh[1])
-            y2 = add_bias(y2, Bh[1], axis=0)
-            f = add(y1, y2)
-            f = sigmoid(f)
-
-            y1 = matmul(x, Wx[2])
-            y1 = add_bias(y1, Bx[2], axis=0)
-            y2 = matmul(h, Wh[2])
-            y2 = add_bias(y2, Bh[2], axis=0)
-            o = add(y1, y2)
-            o = sigmoid(o)
-
-            y1 = matmul(x, Wx[3])
-            y1 = add_bias(y1, Bx[3], axis=0)
-            y2 = matmul(h, Wh[3])
-            y2 = add_bias(y2, Bh[3], axis=0)
-            g = add(y1, y2)
-            g = tanh(g)
-
-            cout1 = mul(f, c)
-            cout2 = mul(i, g)
-            cout = add(cout1, cout2)
-
-            hout = tanh(cout)
-            hout = mul(o, hout)
-            return hout, cout
-        elif self.backend == "cudnn":
-            if not singa.USE_CUDA:
-                raise Exception("Could not use cudnn without cuda compiled.\n")
-            raise Exception("Not avail for cudnn LSTM \n")
-        else:
-            raise Exception("Unsupported backend %s.\n" % backend)
+        self.rnn_mode = rnn_mode
+        self.input_size = input_size
+        self.hidden_size = hidden_size
 
-    def backward(self, dy):
-        if self.backend == "cudnn":
-            if not singa.USE_CUDA:
-                raise Exception("Could not use cudnn without cuda compiled.\n")
+    def __call__(self, x):
+        if not hasattr(self, "handle"):
+            cpp_x = singa.VecTensor()
+            [cpp_x.append(i.data) for i in x]
+
+            self.handle = singa.CudnnRNNHandle(cpp_x, self.input_size, self.hidden_size,
self.rnn_mode)
 
 Review comment:
   this is resolved

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message