Exemplo n.º 1
0
    def test_no_biases(self):
        cases = [
            # N, K, C, H, W, kH, kW
            (1, 4, 5, 7, 8, 3, 3),
            (4, 8, 1, 7, 8, 3, 2),
            (1, 1, 2, 3, 5, 1, 4),
        ]
        for case in cases:
            N, K, C, H, W, kH, kW = case
            input = np.random.rand(N, C, H, W).astype(np.float32)
            filters = np.random.rand(C, K, kH, kW).astype(np.float32)

            def build_network(network):
                h = network.add_input("input", turret.DataType.FLOAT,
                                      turret.Dimensions.CHW(C, H, W))
                h = L.deconvolution_2d(h, filters)
                network.mark_output("output", h)

            actual = execute_inference({"input": input}, build_network)

            expect = self._deconvolution_2d(input, filters,
                                            np.zeros(K, dtype=np.float32),
                                            (1, 1), (0, 0))
            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 2
0
    def test_tanh_default_output(self):
        cases = [
            ParameterSet(2, 10, 3, 20, 15)
        ]
        for case in cases:
            input = self._make_input(case)
            hidden = self._make_hidden(case)
            weights = self._make_weights(case)
            bias = self._make_bias(case)

            def build_network(network):
                h = network.add_input(
                    "input", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(
                        case.batch_size, case.seq_len, case.data_size))
                h_hidden = network.add_input(
                    "hidden", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(
                        case.batch_size, case.num_layers, case.hidden_size))
                h, _ = L.rnn_tanh_v2(h, case.seq_len, weights, bias,
                                     hidden_state=h_hidden)
                network.mark_output("output", h)
            actual = execute_inference(
                {"input": input, "hidden": hidden}, build_network)

            expect = unidirection_rnn(
                input, hidden, weights, bias, lambda x: np.tanh(x))
            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 3
0
    def test_min_ternary(self):
        N, C, H, W = 3, 5, 7, 11
        input0 = np.random.rand(N, C, H, W).astype(np.float32)
        input1 = np.random.rand(N, C, H, W).astype(np.float32)
        input2 = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h2 = network.add_input("input2", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h = L.min(h0, h1, h2)
            network.mark_output("output", h)

        actual = execute_inference(
            {
                "input0": input0,
                "input1": input1,
                "input2": input2
            }, build_network)

        expect = np.minimum(np.minimum(input0, input1), input2)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 4
0
    def test_stride_and_padding(self):
        cases = [
            # N, K, C, H, W, kH, kW, sH, sW, pH, pW
            (1, 4, 5, 7, 8, 3, 3, 3, 1, 0, 0),
            # ( 4, 8, 1, 7, 8,  3,  2,  1,  1,  2,  4),
            (1, 1, 2, 3, 5, 1, 4, 2, 2, 1, 2),
        ]
        for case in cases:
            N, K, C, H, W, kH, kW, sH, sW, pH, pW = case
            input = np.random.rand(N, C, H, W).astype(np.float32)
            filters = np.random.rand(C, K, kH, kW).astype(np.float32)
            biases = np.random.rand(K).astype(np.float32)

            def build_network(network):
                h = network.add_input("input", turret.DataType.FLOAT,
                                      turret.Dimensions.CHW(C, H, W))
                h = L.deconvolution_2d(h,
                                       filters,
                                       biases,
                                       stride=(sH, sW),
                                       padding=(pH, pW))
                network.mark_output("output", h)

            actual = execute_inference({"input": input}, build_network)

            expect = self._deconvolution_2d(input, filters, biases, (sH, sW),
                                            (pH, pW))
            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))
    def test_gamma_beta(self):
        N, C, H, W = 9, 3, 20, 30
        eps = 2e-5
        input = np.random.rand(N, C, H, W).astype(np.float32)
        input = np.ones((N, C, H, W), dtype=np.float32)
        mean = np.random.rand(C).astype(np.float32)
        var = np.random.rand(C).astype(np.float32)
        gamma = np.random.rand(C).astype(np.float32)
        beta = np.random.rand(C).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.batch_normalization(h,
                                      mean,
                                      var,
                                      gamma=gamma,
                                      beta=beta,
                                      eps=eps)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        bcast_mean = np.broadcast_to(mean, (W, H, C)).transpose()
        bcast_std = np.broadcast_to(var, (W, H, C)).transpose()**0.5
        bcast_gamma = np.broadcast_to(gamma, (W, H, C)).transpose()
        bcast_beta = np.broadcast_to(beta, (W, H, C)).transpose()
        expect = bcast_gamma * (input - bcast_mean) / (bcast_std +
                                                       eps) + bcast_beta
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 6
0
    def test_default_output(self):
        cases = [ParameterSet(2, 10, 3, 20, 15)]
        for case in cases:
            input = self._make_input(case)
            hidden = self._make_hidden(case)
            cell = self._make_cell(case)
            fwd_weights = self._make_weights(case)
            bwd_weights = self._make_weights(case)
            fwd_bias = self._make_bias(case)
            bwd_bias = self._make_bias(case)

            def build_network(network):
                h = network.add_input(
                    "input", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(case.seq_len, case.batch_size,
                                          case.data_size))
                h_hidden = network.add_input(
                    "hidden", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(case.num_layers, case.batch_size,
                                          case.hidden_size * 2))
                h_cell = network.add_input(
                    "cell", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(case.num_layers, case.batch_size,
                                          case.hidden_size * 2))
                h, _, _ = L.blstm(h,
                                  case.seq_len,
                                  fwd_weights,
                                  bwd_weights,
                                  fwd_bias,
                                  bwd_bias,
                                  hidden_state=h_hidden,
                                  cell_state=h_cell)
                network.mark_output("output", h)

            actual = execute_inference(
                {
                    "input": input,
                    "hidden": hidden,
                    "cell": cell
                },
                build_network,
                max_batch_size=1)

            t_input = input.transpose(0, 2, 1, 3)
            t_hidden = hidden.transpose(0, 2, 1, 3)
            t_cell = cell.transpose(0, 2, 1, 3)
            t_expect = bidirection_lstm(t_input, t_hidden, t_cell, fwd_weights,
                                        bwd_weights, fwd_bias, bwd_bias)
            expect = t_expect.transpose(0, 2, 1, 3)

            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 7
0
    def test_sum(self):
        N, C, H, W = 3, 5, 7, 11
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.reduce(h, turret.ReduceOperation.SUM, axes=(0, 1))
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.sum(input, axis=(1, 2), keepdims=False)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 8
0
    def test_neg(self):
        N, C, H, W = 3, 5, 7, 11
        input0 = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input0", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.neg(h)
            network.mark_output("output", h)

        actual = execute_inference({"input0": input0}, build_network)

        expect = -input0
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 9
0
    def test_axis(self):
        N, C, H, W = 3, 4, 7, 9
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.concat([h, h], axis=1)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.concatenate((input, input), axis=2)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 10
0
    def test_repeat(self):
        N, C, H, W = 10, 13, 11, 17
        repeats, axis = 4, 1
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.repeat(h, repeats, axis=axis)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.repeat(input, repeats, axis=axis + 1)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 11
0
    def test_default(self):
        N = 5
        C, H, W = 3, 20, 30
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.shuffle(h, (1, 2, 0))
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.transpose(input, (0, 2, 3, 1))
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 12
0
    def test_shift_uniform(self):
        N, C, H, W = 3, 4, 7, 9
        input = np.random.rand(N, C, H, W).astype(np.float32)
        shift = np.random.rand(1).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.scale(h, turret.ScaleMode.UNIFORM, shift=shift)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = input + shift
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 13
0
    def test_default(self):
        N, C, H, W = 5, 13, 17, 19
        k = 3
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.top_k(h, turret.TopKOperation.MIN, k, 1)
            network.mark_output("output", h[0])

        actual = execute_inference({"input": input}, build_network)

        expect = self._topk(input, k, 1)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 14
0
    def test_default(self):
        N = 5
        C_in, H_in, W_in = 3, 20, 30
        C_out, H_out, W_out = 6, 5, 60
        input = np.random.rand(N, C_in, H_in, W_in).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C_in, H_in, W_in))
            h = L.reshape(h, turret.Dimensions.CHW(C_out, H_out, W_out))
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.reshape(input, (N, C_out, H_out, W_out))
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 15
0
    def test_default(self):
        N = 5
        C, H, W = 3, 20, 30
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.leaky_relu(h, slope=0.5)
            network.mark_output("output", h)

        actual = execute_inference({"input": input.reshape((N, C, H, W))},
                                   build_network)

        expect = np.zeros((N, C, H, W), dtype=np.float32)
        expect = np.maximum(input, input * 0.5)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 16
0
    def test_constant(self):
        N, C, K, H, W = 5, 13, 17, 19, 23
        data = np.random.rand(C, H, W).astype(np.float32)
        indices = np.random.randint(0, C - 1, (N, K), dtype=np.int32)

        def build_network(network):
            d = network.add_constant(data, turret.Dimensions.CHW(C, H, W))
            i = network.add_input(
                "indices", turret.DataType.INT32,
                turret.Dimensions(((K, turret.DimensionType.INDEX), )))
            h = L.gather(d, i, 0)
            network.mark_output("output", h)

        actual = execute_inference({"indices": indices}, build_network)

        expect = self._gather(np.broadcast_to(data, (N, C, H, W)), indices, 0)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 17
0
    def test_shift_elementwise(self):
        N, C, H, W = 3, 4, 7, 9
        input = np.random.rand(N, C, H, W).astype(np.float32)
        shift = np.random.rand(C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.scale(h, turret.ScaleMode.ELEMENTWISE, shift=shift)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = np.copy(input)
        for i in range(N):
            expect[i] += shift
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 18
0
    def test_default(self):
        N, C, H, W = 5, 3, 20, 30
        window_size = (3, 2)
        stride = (2, 3)
        padding = (0, 1)
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.average_pooling_2d(h, window_size, stride, padding)
            network.mark_output("output", h)

        actual = execute_inference({"input": input.reshape((N, C, H, W))},
                                   build_network)

        expect = average_pooling_2d(input, window_size, stride, padding)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 19
0
    def test_negative(self):
        N, C, H, W = 3, 5, 20, 30
        pad_width = (-1, -2)
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.pad(h, pad_width)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        Hp = H + 2 * pad_width[0]
        Wp = W + 2 * pad_width[1]
        top, left = -pad_width[0], -pad_width[1]
        expect = input[:, :, top:top + Hp, left:left + Wp]

        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 20
0
    def test_different(self):
        N, C0, C1, H, W = 3, 4, 7, 7, 9
        input0 = np.random.rand(N, C0, H, W).astype(np.float32)
        input1 = np.random.rand(N, C1, H, W).astype(np.float32)

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C0, H, W))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C1, H, W))
            h = L.concat([h0, h1])
            network.mark_output("output", h)

        actual = execute_inference({
            "input0": input0,
            "input1": input1
        }, build_network)

        expect = np.concatenate((input0, input1), axis=1)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 21
0
    def test_split_c(self):
        N, C, H, W = 3, 10, 13, 11
        sections = [2, 5, 6, 8]
        input = np.random.rand(N, C, H, W).astype(np.float32)

        for i in range(len(sections) + 1):

            def build_network(network):
                h = network.add_input("input", turret.DataType.FLOAT,
                                      turret.Dimensions.CHW(C, H, W))
                h = L.split(h, sections, axis=0)
                network.mark_output("output", h[i])

            actual = execute_inference({"input": input}, build_network)

            expect = np.split(input, sections, axis=1)[i]
            self.assertEqual(expect.shape, actual.shape)
            if not np.allclose(expect, actual):
                print(expect)
                print(actual)
            self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 22
0
    def test_sum(self):
        N, C, H, W = 3, 4, 7, 9
        input0 = np.random.rand(N, C, H, W).astype(np.float32)
        input1 = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h = L.elementwise(h0, h1, turret.ElementWiseOperation.SUM)
            network.mark_output("output", h)

        actual = execute_inference({
            "input0": input0,
            "input1": input1
        }, build_network)

        expect = input0 + input1
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 23
0
    def test_div(self):
        N, C, H, W = 3, 5, 7, 11
        input0 = np.random.rand(N, C, H, W).astype(np.float32)
        input1 = np.random.rand(N, C, H, W).astype(np.float32) + 0.5

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.CHW(C, H, W))
            h = L.div(h0, h1)
            network.mark_output("output", h)

        actual = execute_inference({
            "input0": input0,
            "input1": input1
        }, build_network)

        expect = input0 / input1
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 24
0
    def test_positive(self):
        N, C, H, W = 3, 5, 20, 30
        pad_width = (1, 2)
        input = np.random.rand(N, C, H, W).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            h = L.pad(h, pad_width)
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        Hp = H + 2 * pad_width[0]
        Wp = W + 2 * pad_width[1]
        top, left = pad_width
        expect = np.zeros((N, C, Hp, Wp), dtype=np.float32)
        expect[:, :, top:H + top, left:W + left] = input

        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 25
0
    def test_no_biases(self):
        N, K, C = 5, 10, 100
        input = np.random.rand(N, C).astype(np.float32)
        weights = np.random.rand(K, C).astype(np.float32)

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(1, 1, C))
            h = L.fully_connected(h, weights)
            network.mark_output("output", h)

        actual = execute_inference({"input": input.reshape((N, 1, 1, C))},
                                   build_network)

        expect = np.zeros((N, K), dtype=np.float32)
        for i in range(N):
            for k in range(K):
                for c in range(C):
                    expect[i, k] += input[i, c] * weights[k, c]
        expect = expect.reshape((N, K, 1, 1))
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 26
0
    def test_default(self):
        N, C, K = 5, 3, 20
        input = np.random.rand(N, C, K).astype(np.float32)
        bounds = np.broadcast_to(
            np.asarray([[[10], [15], [20]]], dtype=np.int32), (N, C, 1))

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT,
                                  turret.Dimensions.HW(C, K))
            b = network.add_input("bounds", turret.DataType.INT32,
                                  turret.Dimensions.HW(C, 1))
            h = L.ragged_softmax(h, b)
            network.mark_output("output", h)

        actual = execute_inference({
            "input": input,
            "bounds": bounds
        }, build_network)

        expect = self._ragged_softmax(input, bounds)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 27
0
    def test_default(self):
        # AxB * BxC => AxC
        N, A, B, C = 5, 31, 23, 37
        input0 = np.random.rand(N, A, B).astype(np.float32)
        input1 = np.random.rand(N, B, C).astype(np.float32)

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.HW(A, B))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.HW(B, C))
            h = L.matrix_multiply(h0, False, h1, False)
            network.mark_output("output", h)

        actual = execute_inference({
            "input0": input0,
            "input1": input1
        }, build_network)

        expect = self._matrix_multiply(input0, input1)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 28
0
    def test_2d_indices(self):
        N, C, K1, K2, H, W = 5, 13, 17, 19, 23, 29
        data = np.random.rand(N, C, H, W).astype(np.float32)
        indices = np.random.randint(0, H - 1, (N, K1, K2), dtype=np.int32)

        def build_network(network):
            d = network.add_input("data", turret.DataType.FLOAT,
                                  turret.Dimensions.CHW(C, H, W))
            i = network.add_input(
                "indices", turret.DataType.INT32,
                turret.Dimensions(((K1, turret.DimensionType.INDEX),
                                   (K2, turret.DimensionType.INDEX))))
            h = L.gather(d, i, 1)
            network.mark_output("output", h)

        actual = execute_inference({
            "data": data,
            "indices": indices
        }, build_network)

        expect = self._gather(data, indices, 1)
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 29
0
    def test_transposed(self):
        # BxA^T * CxB^T => AxC
        N, A, B, C = 5, 31, 23, 37
        input0 = np.random.rand(N, B, A).astype(np.float32)
        input1 = np.random.rand(N, C, B).astype(np.float32)

        def build_network(network):
            h0 = network.add_input("input0", turret.DataType.FLOAT,
                                   turret.Dimensions.HW(B, A))
            h1 = network.add_input("input1", turret.DataType.FLOAT,
                                   turret.Dimensions.HW(C, B))
            h = L.matrix_multiply(h0, True, h1, True)
            network.mark_output("output", h)

        actual = execute_inference({
            "input0": input0,
            "input1": input1
        }, build_network)

        expect = self._matrix_multiply(input0.transpose((0, 2, 1)),
                                       input1.transpose((0, 2, 1)))
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))
Exemplo n.º 30
0
    def test_thru(self):
        N, C, H, W = 5, 10, 30, 40
        io_dims = turret.Dimensions.CHW(C, H, W)
        input = np.random.rand(N, C, H, W).astype(np.float32)
        test = self

        class ThruPlugin(turret.PluginBase):
            @classmethod
            def module_name(cls):
                return "thru"

            def serialize(self, stream):
                pass

            @classmethod
            def deserialize(cls, stream):
                return cls()

            def get_num_outputs(self):
                return 1

            def get_output_dimensions(self, in_dims):
                test.assertEqual(1, len(in_dims))
                test.assertEqual(io_dims, in_dims[0])
                return [io_dims]

            def configure(self, in_dims, out_dims, max_batch_size):
                test.assertEqual(1, len(in_dims))
                test.assertEqual(io_dims, in_dims[0])
                test.assertEqual(1, len(out_dims))
                test.assertEqual(io_dims, out_dims[0])

            def initialize(self):
                return 0

            def terminate(self):
                pass

            def get_workspace_size(self, max_batch_size):
                return 0

            def enqueue(self, batch_size, inputs, outputs, workspace, stream):
                test.assertEqual(N, batch_size)
                n = batch_size * io_dims.size
                func = _THRU_MODULE.get_function("thru")
                func(outputs[0],
                     inputs[0],
                     block=(1, 1, 1),
                     grid=(n, 1, 1),
                     stream=stream)
                return 0

        def build_network(network):
            h = network.add_input("input", turret.DataType.FLOAT, io_dims)
            h = L.plugin(h, ThruPlugin())
            network.mark_output("output", h)

        actual = execute_inference({"input": input}, build_network)

        expect = input
        self.assertEqual(expect.shape, actual.shape)
        self.assertTrue(np.allclose(expect, actual))