| 1966 | self._onehot_helper(gpu_dev) |
| 1967 | |
| 1968 | def _inference_helper(self, dev): |
| 1969 | x = tensor.Tensor(shape=(2, 3, 3, 3), device=dev) |
| 1970 | x.gaussian(0.0, 1.0) |
| 1971 | |
| 1972 | conv1 = layer.Conv2d(1, 2) |
| 1973 | conv2 = layer.Conv2d(1, 2) |
| 1974 | |
| 1975 | class MyLayer(layer.Layer): |
| 1976 | |
| 1977 | def __init__(self, conv1, conv2): |
| 1978 | super(MyLayer, self).__init__() |
| 1979 | self.conv1 = conv1 |
| 1980 | self.conv2 = conv2 |
| 1981 | |
| 1982 | def forward(self, inputs): |
| 1983 | x = self.conv1(inputs) |
| 1984 | x = self.conv2(x) |
| 1985 | return x |
| 1986 | |
| 1987 | y = MyLayer(conv1, conv2)(x) |
| 1988 | x1 = conv1(x) |
| 1989 | # frontend |
| 1990 | model = sonnx.to_onnx([x], [y]) |
| 1991 | # print('The model is:\n{}'.format(model)) |
| 1992 | |
| 1993 | # backend |
| 1994 | sg_ir = sonnx.prepare(model, device=dev) |
| 1995 | sg_ir.is_graph = True |
| 1996 | y_t = sg_ir.run([x], last_layers=-1) |
| 1997 | |
| 1998 | np.testing.assert_array_almost_equal(tensor.to_numpy(x1), |
| 1999 | tensor.to_numpy(y_t[0]), |
| 2000 | decimal=5) |
| 2001 | |
| 2002 | def test_inference_cpu(self): |
| 2003 | self._inference_helper(cpu_dev) |