(self, grad)
| 4879 | return last_y |
| 4880 | |
| 4881 | def backward(self, grad): |
| 4882 | assert training is True and hasattr( |
| 4883 | self, "inputs"), "Please set training as True before do BP. " |
| 4884 | |
| 4885 | # (seq, bs, hid) |
| 4886 | dy = None |
| 4887 | if self.return_sequences: |
| 4888 | assert grad.shape() == self.inputs['y'].shape(), ( |
| 4889 | "grad shape %s != y shape %s" % |
| 4890 | (grad.shape(), self.inputs['y'].shape())) |
| 4891 | dy = grad |
| 4892 | else: |
| 4893 | # grad (bs, directions*hidden) -> dy (seq, bs, directions*hidden) |
| 4894 | # empty space filled by zeros |
| 4895 | assert grad.shape() == (self.inputs['y'].shape()[1], |
| 4896 | self.inputs['y'].shape()[2]), ( |
| 4897 | "grad y shape %s != last y shape %s" % |
| 4898 | (grad.shape(), |
| 4899 | (self.inputs['y'].shape()[1], |
| 4900 | self.inputs['y'].shape()[2]))) |
| 4901 | dy = singa.Tensor(list(self.inputs['y'].shape()), grad.device()) |
| 4902 | dy.SetFloatValue(0.0) |
| 4903 | dst_offset = dy.Size() - grad.Size() |
| 4904 | singa.CopyDataToFrom(dy, grad, grad.Size(), dst_offset, 0) |
| 4905 | |
| 4906 | # states grad are zeros, since states are not used in forward pass |
| 4907 | dhy = singa.Tensor(list(self.inputs['hy'].shape()), grad.device()) |
| 4908 | dhy.SetFloatValue(0.0) |
| 4909 | dcy = singa.Tensor(list(self.inputs['cy'].shape()), grad.device()) |
| 4910 | dcy.SetFloatValue(0.0) |
| 4911 | |
| 4912 | if self.use_mask: |
| 4913 | (dx, dhx, |
| 4914 | dcx) = singa.GpuRNNBackwardxEx(self.inputs['y'], dy, dhy, dcy, |
| 4915 | self.inputs['w'], self.inputs['hx'], |
| 4916 | self.inputs['cx'], |
| 4917 | self.seq_lengths.data, self.handle) |
| 4918 | dW = singa.GpuRNNBackwardWEx(self.inputs['x'], self.inputs['hx'], |
| 4919 | self.inputs['y'], |
| 4920 | self.seq_lengths.data, self.handle) |
| 4921 | else: |
| 4922 | (dx, dhx, |
| 4923 | dcx) = singa.GpuRNNBackwardx(self.inputs['y'], dy, dhy, dcy, |
| 4924 | self.inputs['w'], self.inputs['hx'], |
| 4925 | self.inputs['cx'], self.handle) |
| 4926 | dW = singa.GpuRNNBackwardW(self.inputs['x'], self.inputs['hx'], |
| 4927 | self.inputs['y'], self.handle) |
| 4928 | |
| 4929 | return dx, dhx, dcx, dW |
| 4930 | |
| 4931 | |
| 4932 | class CosSim(Operator): |
nothing calls this directly
no test coverage detected