Example #1
0
    def test_async_block(self):
        args = set_grpc_frontend_parser().parse_args([
            '--grpc_host',
            '127.0.0.1',
        ])

        p1_args = set_router_service_parser().parse_args([
            '--port_in',
            str(args.port_out),
            '--port_out',
            '8899',
            '--socket_in',
            str(SocketType.PULL_CONNECT),
            '--socket_out',
            str(SocketType.PUSH_CONNECT),
        ])

        p2_args = set_router_service_parser().parse_args([
            '--port_in',
            str(p1_args.port_out),
            '--port_out',
            str(args.port_in),
            '--socket_in',
            str(SocketType.PULL_BIND),
            '--socket_out',
            str(SocketType.PUSH_CONNECT),
        ])

        with Router1(p1_args), Router2(p2_args), GRPCFrontend(
                args), grpc.insecure_channel(
                    '%s:%s' % (args.grpc_host, args.grpc_port),
                    options=[
                        ('grpc.max_send_message_length', 70 * 1024 * 1024),
                        ('grpc.max_receive_message_length', 70 * 1024 * 1024)
                    ]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            id = 0
            with TimeContext('non-blocking call'
                             ):  # about 26s = 32s (total) - 3*2s (overlap)
                resp = stub.StreamCall(
                    RequestGenerator.train(self.all_bytes2, 1))
                for r in resp:
                    self.assertEqual(r.request_id, str(id))
                    id += 1

            id = 0
            with TimeContext('blocking call'):  # should be 32 s
                for r in RequestGenerator.train(self.all_bytes2, 1):
                    resp = stub.Call(r)
                    self.assertEqual(resp.request_id, str(id))
                    id += 1
Example #2
0
    def test_singleton_preprocessor_service_realdata(self):
        args = set_preprocessor_service_parser().parse_args(
            ['--yaml_path', self.singleton_img_pre_yaml])
        c_args = _set_client_parser().parse_args(
            ['--port_in',
             str(args.port_out), '--port_out',
             str(args.port_in)])
        all_zips = zipfile.ZipFile(os.path.join(self.dirname, 'imgs/test.zip'))
        all_bytes = [all_zips.open(v).read() for v in all_zips.namelist()]

        with PreprocessorService(args), ZmqClient(c_args) as client:
            for req in RequestGenerator.index(all_bytes):
                msg = gnes_pb2.Message()
                msg.request.index.CopyFrom(req.index)
                client.send_message(msg)
                r = client.recv_message()
                self.assertEqual(
                    r.envelope.routes[0].service,
                    'PreprocessorService:BaseSingletonPreprocessor')
                for d in r.request.index.docs:
                    self.assertEqual(len(d.chunks), 1)
                    self.assertEqual(len(blob2array(d.chunks[0].blob).shape),
                                     3)
                    self.assertEqual(blob2array(d.chunks[0].blob).shape[-1], 3)
                    self.assertEqual(
                        blob2array(d.chunks[0].blob).shape[0], 224)
                    self.assertEqual(
                        blob2array(d.chunks[0].blob).shape[1], 224)
                    print(blob2array(d.chunks[0].blob).dtype)
Example #3
0
    def test_pymode(self):
        args = set_frontend_parser().parse_args([])

        p_args = set_preprocessor_parser().parse_args([
            '--port_in', str(args.port_out),
            '--port_out', '5531',
            '--socket_in', str(SocketType.PULL_CONNECT),
            '--socket_out', str(SocketType.PUSH_BIND),
            '--yaml_path', '!UnaryPreprocessor {parameters: {doc_type: 1}}'
        ])

        e_args = set_encoder_parser().parse_args([
            '--port_in', str(p_args.port_out),
            '--port_out', str(args.port_in),
            '--socket_in', str(SocketType.PULL_CONNECT),
            '--socket_out', str(SocketType.PUSH_CONNECT),
            '--yaml_path', 'flair.yml',
        ])

        with ServiceManager(EncoderService, e_args), \
             ServiceManager(PreprocessorService, p_args), \
             FrontendService(args), \
             grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port),
                                   options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                                            ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            resp = stub.Call(list(RequestGenerator.index([b'hello world', b'goodbye!'], 1))[0])
            self.assertEqual(resp.request_id, '0')
Example #4
0
    def test_pymode(self):
        args = set_frontend_parser().parse_args([
            '--socket_in',
            str(SocketType.PULL_BIND),
            '--socket_out',
            str(SocketType.PUSH_BIND),
        ])

        p_args = set_preprocessor_parser().parse_args([
            '--port_in',
            str(args.port_out), '--port_out',
            str(args.port_in), '--socket_in',
            str(SocketType.PULL_CONNECT), '--socket_out',
            str(SocketType.PUSH_CONNECT), '--yaml_path', 'pipline.yml',
            '--py_path', 'mypreprocessor1.py', 'mypreprocessor2.py'
        ])

        with ServiceManager(PreprocessorService, p_args), \
             FrontendService(args), \
             grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port),
                                   options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                                            ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            resp = stub.Call(
                list(RequestGenerator.index([b'doc1:', b'doc2:'], 1))[0])
            self.assertEqual(resp.request_id, '0')
Example #5
0
    def test_grpc_frontend(self):
        args = set_grpc_frontend_parser().parse_args([
            '--grpc_host',
            '127.0.0.1',
        ])

        p_args = set_router_service_parser().parse_args([
            '--port_in',
            str(args.port_out),
            '--port_out',
            str(args.port_in),
            '--socket_in',
            str(SocketType.PULL_CONNECT),
            '--socket_out',
            str(SocketType.PUSH_CONNECT),
        ])

        with RouterService(p_args), GRPCFrontend(args), grpc.insecure_channel(
                '%s:%s' % (args.grpc_host, args.grpc_port),
                options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                         ('grpc.max_receive_message_length', 70 * 1024 * 1024)
                         ]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            with TimeContext('sync call'):  # about 5s
                resp = list(
                    stub.StreamCall(RequestGenerator.train(self.all_bytes,
                                                           1)))[-1]

            self.assertEqual(resp.request_id, str(len(
                self.all_bytes)))  # idx start with 0, but +1 for final FLUSH
Example #6
0
    def _test_grpc_multiple_pub(self, backend='thread', num_parallel=5):
        args = set_frontend_parser().parse_args([
            '--grpc_host',
            '127.0.0.1',
        ])

        p_args = set_router_parser().parse_args([
            '--port_in',
            str(args.port_out), '--port_out',
            str(args.port_in), '--socket_in',
            str(SocketType.PULL_CONNECT), '--socket_out',
            str(SocketType.PUSH_CONNECT), '--yaml_path', 'BaseRouter',
            '--num_parallel',
            str(num_parallel), '--parallel_backend', backend,
            '--parallel_type',
            str(ParallelType.PUB_BLOCK)
        ])

        with ServiceManager(
                RouterService,
                p_args), FrontendService(args), grpc.insecure_channel(
                    '%s:%d' % (args.grpc_host, args.grpc_port),
                    options=[
                        ('grpc.max_send_message_length', 70 * 1024 * 1024),
                        ('grpc.max_receive_message_length', 70 * 1024 * 1024)
                    ]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            resp = stub.Call(list(RequestGenerator.query(b'abc', 1))[0])
            self.assertEqual(resp.request_id, 0)
Example #7
0
    def test_pymode(self):
        args = set_frontend_parser().parse_args([])

        p_args = set_preprocessor_parser().parse_args([
            '--port_in',
            str(args.port_out), '--port_out', '5531', '--socket_in',
            str(SocketType.PULL_CONNECT), '--socket_out',
            str(SocketType.PUSH_BIND), '--yaml_path', 'SentSplitPreprocessor'
        ])

        e_args = set_encoder_parser().parse_args([
            '--port_in',
            str(p_args.port_out), '--port_out',
            str(args.port_in), '--socket_in',
            str(SocketType.PULL_CONNECT), '--socket_out',
            str(SocketType.PUSH_CONNECT), '--yaml_path', 'transformer.yml',
            '--py_path', 'distilbert.py'
        ])

        with ServiceManager(EncoderService, e_args), \
             ServiceManager(PreprocessorService, p_args), \
             FrontendService(args), \
             grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port),
                                   options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                                            ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            resp = stub.Call(
                list(
                    RequestGenerator.index([
                        b'hello world, good to see you.',
                        b'goodbye! wish to see you again!'
                    ], 1))[0])
            self.assertEqual(resp.request_id, 0)
Example #8
0
    def test_fasterrcnn_preprocessor(self):
        args = set_preprocessor_parser().parse_args(
            ['--yaml_path', self.fasterrcnn_yaml])
        c_args = _set_client_parser().parse_args(
            ['--port_in',
             str(args.port_out), '--port_out',
             str(args.port_in)])
        all_zips = zipfile.ZipFile(self.data_path)
        all_bytes = [all_zips.open(v).read() for v in all_zips.namelist()]

        with ServiceManager(PreprocessorService,
                            args), ZmqClient(c_args) as client:
            for req in RequestGenerator.index(all_bytes):
                msg = gnes_pb2.Message()
                msg.request.index.CopyFrom(req.index)
                client.send_message(msg)
                r = client.recv_message()
                for d in r.request.index.docs:
                    self.assertGreater(len(d.chunks), 0)
                    for _ in range(len(d.chunks)):
                        self.assertEqual(
                            len(blob2array(d.chunks[_].blob).shape), 3)
                        self.assertEqual(
                            blob2array(d.chunks[_].blob).shape[-1], 3)
                        self.assertEqual(
                            blob2array(d.chunks[_].blob).shape[0], 224)
                        self.assertEqual(
                            blob2array(d.chunks[_].blob).shape[1], 224)
                        print(blob2array(d.chunks[0].blob).dtype)
    def test_grpc_real_service(self):
        # to fix

        with DummyServer('%s:%d' % (self.s_args.grpc_host, self.s_args.grpc_port)), GRPCService(
                self.s_args), FrontendService(self.args), grpc.insecure_channel(
            '%s:%s' % (self.args.grpc_host, self.args.grpc_port),
            options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                     ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            resp = stub.Call(list(RequestGenerator.query(b'abc', 1))[0])
            self.assertEqual(resp.request_id, 0)  # idx start with 0, but +1 for final FLUSH
Example #10
0
 def query(self, all_bytes: List[bytes], stub):
     for idx, q in enumerate(all_bytes):
         for req in RequestGenerator.query(q,
                                           request_id_start=idx,
                                           top_k=self.args.top_k):
             resp = stub.Call(req)
             print(colored(req.search.query, 'green'))
             for k in resp.search.topk_results:
                 print(colored(k.doc.doc_id, 'magenta'))
                 print(colored(k.doc.raw_text, 'yellow'))
                 print(colored(k.score.value, 'blue'))
                 pprint.pprint(json.loads(k.score.explained))
                 input('press any key to continue...')
             input('on to next query, press any key to continue...')
    def test_video_cut_by_clustering(self):
        args = set_preprocessor_parser().parse_args(
            ['--yaml_path', self.yml_path_4])
        c_args = _set_client_parser().parse_args(
            ['--port_in',
             str(args.port_out), '--port_out',
             str(args.port_in)])

        with PreprocessorService(args), ZmqClient(c_args) as client:
            for req in RequestGenerator.index(self.video_bytes):
                msg = gnes_pb2.Message()
                msg.request.index.CopyFrom(req.index)
                client.send_message(msg)
                r = client.recv_message()
                for d in r.request.index.docs:
                    self.assertEqual(len(d.chunks), 6)
Example #12
0
    def test_pymode(self):
        os.unsetenv('http_proxy')
        os.unsetenv('https_proxy')
        args = set_frontend_parser().parse_args([])

        p_args = set_preprocessor_parser().parse_args([
            '--port_in',
            str(args.port_out), '--port_out', '5531', '--socket_in',
            str(SocketType.PULL_CONNECT), '--socket_out',
            str(SocketType.PUSH_BIND), '--yaml_path', 'SentSplitPreprocessor'
        ])

        e_args = set_indexer_parser().parse_args([
            '--port_in',
            str(p_args.port_out),
            '--port_out',
            str(args.port_in),
            '--socket_in',
            str(SocketType.PULL_CONNECT),
            '--socket_out',
            str(SocketType.PUSH_CONNECT),
            '--yaml_path',
            '!DictIndexer {gnes_config: {name: dummy_dict_indexer}}',
        ])

        with ServiceManager(IndexerService, e_args), \
             ServiceManager(PreprocessorService, p_args), \
             FrontendService(args), \
             grpc.insecure_channel('%s:%s' % (args.grpc_host, args.grpc_port),
                                   options=[('grpc.max_send_message_length', 70 * 1024 * 1024),
                                            ('grpc.max_receive_message_length', 70 * 1024 * 1024)]) as channel:
            stub = gnes_pb2_grpc.GnesRPCStub(channel)
            all_bytes = []
            with open(os.path.join(self.dirname, '26-doc-chinese.txt'),
                      'r',
                      encoding='utf8') as fp:
                for v in fp:
                    if v.strip():
                        all_bytes.append(v.encode())
            for r in stub.StreamCall(RequestGenerator.index(all_bytes)):
                print(r)

        bi = BaseIndexer.load('dummy_dict_indexer.bin')
        self.assertEqual(bi.size, 26)
        print(bi.query([0]))
    def test_video_preprocessor_service_realdata(self):
        args = set_preprocessor_parser().parse_args(
            ['--yaml_path', self.yml_path])

        c_args = _set_client_parser().parse_args(
            ['--port_in',
             str(args.port_out), '--port_out',
             str(args.port_in)])

        with PreprocessorService(args), ZmqClient(c_args) as client:
            for req in RequestGenerator.index(self.video_bytes):
                msg = gnes_pb2.Message()
                msg.request.index.CopyFrom(req.index)
                client.send_message(msg)
                r = client.recv_message()
                for d in r.request.index.docs:
                    self.assertGreater(len(d.chunks), 0)
                    for _ in range(len(d.chunks)):
                        shape = blob2array(d.chunks[_].blob).shape
                        self.assertEqual(shape, (168, 192, 3))
Example #14
0
    def test_video_decode_preprocessor(self):
        args = set_preprocessor_parser().parse_args(['--yaml_path', self.yml_path])
        c_args = _set_client_parser().parse_args([
            '--port_in', str(args.port_out),
            '--port_out', str(args.port_in)])
        video_bytes = [
            open(os.path.join(self.video_path, _), 'rb').read()
            for _ in os.listdir(self.video_path)
        ]

        with ServiceManager(PreprocessorService, args), ZmqClient(c_args) as client:
            for req in RequestGenerator.index(video_bytes):
                msg = gnes_pb2.Message()
                msg.request.index.CopyFrom(req.index)
                client.send_message(msg)
                r = client.recv_message()
                for d in r.request.index.docs:
                    self.assertGreater(len(d.chunks), 0)
                    for _ in range(len(d.chunks)):
                        shape = blob2array(d.chunks[_].blob).shape
                        self.assertEqual(shape[1:], (299, 299, 3))