示例#1
0
def test_add():
    channel = grpc.insecure_channel('localhost:5500')
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    request = ms_service_pb2.PredictRequest()

    x = request.data.add()
    x.tensor_shape.dims.extend([4])
    x.tensor_type = ms_service_pb2.MS_FLOAT32
    x.data = (np.ones([4]).astype(np.float32)).tobytes()

    y = request.data.add()
    y.tensor_shape.dims.extend([4])
    y.tensor_type = ms_service_pb2.MS_FLOAT32
    y.data = (np.ones([4]).astype(np.float32)).tobytes()

    result = stub.Predict(request)
    result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(
        result.result[0].tensor_shape.dims)
    print("ms client received: ")
    print(result_np)

    net = AddNet()
    net_out = net(Tensor(np.ones([4]).astype(np.float32)),
                  Tensor(np.ones([4]).astype(np.float32)))
    print("add net out: ")
    print(net_out)
    assert np.allclose(net_out.asnumpy(),
                       result_np,
                       0.001,
                       0.001,
                       equal_nan=True)
示例#2
0
def generate(s='', data_type=0):
    """generate"""
    if len(sys.argv) > 2:
        sys.exit("input error")
    channel_str = ""
    if len(sys.argv) == 2:
        split_args = sys.argv[1].split('=')
        if len(split_args) > 1:
            channel_str = split_args[1]
        else:
            channel_str = 'localhost:5500'
    else:
        channel_str = 'localhost:5500'

    serving_channel = grpc.insecure_channel(channel_str)
    stub_func = ms_service_pb2_grpc.MSServiceStub(serving_channel)
    request_module = ms_service_pb2.PredictRequest()

    _target_ids = np.ones(shape=(1, 128))
    _segment_ids = np.ones(shape=(1, 128))
    pad_mask = np.ones(shape=(1, 128))
    request_module, request_input_ids, request_segment_ids, request_pad_mask = input_construction(\
        request_module, _target_ids, _segment_ids, pad_mask)
    if data_type in [0, 1]:
        poetry = generate_random_poetry(s, stub_func, request_module, request_input_ids,\
                                        request_segment_ids, request_pad_mask)
    else:
        poetry = generate_hidden(s, stub_func, request_module,
                                 request_input_ids, request_segment_ids,
                                 request_pad_mask)

    print(poetry)
    return poetry
示例#3
0
def test_bert():
    MAX_MESSAGE_LENGTH = 0x7fffffff
    input_ids = np.random.randint(0, 1000, size=(2, 32), dtype=np.int32)
    segment_ids = np.zeros((2, 32), dtype=np.int32)
    input_mask = np.zeros((2, 32), dtype=np.int32)
    channel = grpc.insecure_channel(
        'localhost:5500',
        options=[('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
                 ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)])
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    request = ms_service_pb2.PredictRequest()

    x = request.data.add()
    x.tensor_shape.dims.extend([2, 32])
    x.tensor_type = ms_service_pb2.MS_INT32
    x.data = input_ids.tobytes()

    y = request.data.add()
    y.tensor_shape.dims.extend([2, 32])
    y.tensor_type = ms_service_pb2.MS_INT32
    y.data = segment_ids.tobytes()

    z = request.data.add()
    z.tensor_shape.dims.extend([2, 32])
    z.tensor_type = ms_service_pb2.MS_INT32
    z.data = input_mask.tobytes()

    result = stub.Predict(request)
    result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(
        result.result[0].tensor_shape.dims)
    print("ms client received: ")
    print(result_np)

    net = BertModel(bert_net_cfg, False)
    bert_out = net(Tensor(input_ids), Tensor(segment_ids), Tensor(input_mask))
    print("bert out: ")
    print(bert_out)
    bert_out_size = len(bert_out)
    for i in range(bert_out_size):
        result_np = np.frombuffer(result.result[i].data,
                                  dtype=np.float32).reshape(
                                      result.result[i].tensor_shape.dims)
        logger.info("i:{}, result_np:{}, bert_out:{}".format(
            i, result.result[i].tensor_shape.dims,
            bert_out[i].asnumpy().shape))
        assert np.allclose(bert_out[i].asnumpy(),
                           result_np,
                           0.001,
                           0.001,
                           equal_nan=True)
示例#4
0
def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    # request = ms_service_pb2.EvalRequest()
    # request.name = 'haha'
    # response = stub.Eval(request)
    # print("ms client received: " + response.message)

    request = ms_service_pb2.PredictRequest()
    request.data.tensor_shape.dims.extend([32, 1, 32, 32])
    request.data.tensor_type = ms_service_pb2.MS_FLOAT32
    request.data.data = (np.ones([32, 1, 32, 32]).astype(np.float32) * 0.01).tobytes()

    request.label.tensor_shape.dims.extend([32])
    request.label.tensor_type = ms_service_pb2.MS_INT32
    request.label.data = np.ones([32]).astype(np.int32).tobytes()

    result = stub.Test(request)
    #result_np = np.frombuffer(result.result.data, dtype=np.float32).reshape(result.result.tensor_shape.dims)
    print("ms client test call received: ")
示例#5
0
def run():
    channel = grpc.insecure_channel('localhost:5500')
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    request = ms_service_pb2.PredictRequest()

    x = request.data.add()
    x.tensor_shape.dims.extend([4])
    x.tensor_type = ms_service_pb2.MS_FLOAT32
    x.data = (np.ones([4]).astype(np.float32)).tobytes()

    y = request.data.add()
    y.tensor_shape.dims.extend([4])
    y.tensor_type = ms_service_pb2.MS_FLOAT32
    y.data = (np.ones([4]).astype(np.float32)).tobytes()

    result = stub.Predict(request)
    print(result)
    result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(
        result.result[0].tensor_shape.dims)
    print("ms client received: ")
    print(result_np)
示例#6
0
def run():
    if len(sys.argv) > 2:
        sys.exit("input error")
    channel_str = ""
    if len(sys.argv) == 2:
        split_args = sys.argv[1].split('=')
        if len(split_args) > 1:
            channel_str = split_args[1]
        else:
            channel_str = 'localhost:5500'
    else:
        channel_str = 'localhost:5500'

    channel = grpc.insecure_channel(channel_str)
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    request = ms_service_pb2.PredictRequest()

    x = request.data.add()
    x.tensor_shape.dims.extend([4])
    x.tensor_type = ms_service_pb2.MS_FLOAT32
    x.data = (np.ones([4]).astype(np.float32)).tobytes()

    y = request.data.add()
    y.tensor_shape.dims.extend([4])
    y.tensor_type = ms_service_pb2.MS_FLOAT32
    y.data = (np.ones([4]).astype(np.float32)).tobytes()

    try:
        result = stub.Predict(request)
        print(result)
        result_np = np.frombuffer(result.result[0].data,
                                  dtype=np.float32).reshape(
                                      result.result[0].tensor_shape.dims)
        print("ms client received: ")
        print(result_np)
    except grpc.RpcError as e:
        print(e.details())
        status_code = e.code()
        print(status_code.name)
        print(status_code.value)
示例#7
0
def test_bert():
    MAX_MESSAGE_LENGTH = 0x7fffffff
    input_ids = np.random.randint(0, 1000, size=(2, 32), dtype=np.int32)
    segment_ids = np.zeros((2, 32), dtype=np.int32)
    input_mask = np.zeros((2, 32), dtype=np.int32)

    # grpc visit
    channel = grpc.insecure_channel(
        'localhost:5500',
        options=[('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
                 ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)])
    stub = ms_service_pb2_grpc.MSServiceStub(channel)
    request = ms_service_pb2.PredictRequest()

    x = request.data.add()
    x.tensor_shape.dims.extend([2, 32])
    x.tensor_type = ms_service_pb2.MS_INT32
    x.data = input_ids.tobytes()

    y = request.data.add()
    y.tensor_shape.dims.extend([2, 32])
    y.tensor_type = ms_service_pb2.MS_INT32
    y.data = segment_ids.tobytes()

    z = request.data.add()
    z.tensor_shape.dims.extend([2, 32])
    z.tensor_type = ms_service_pb2.MS_INT32
    z.data = input_mask.tobytes()

    result = stub.Predict(request)
    grpc_result = np.frombuffer(result.result[0].data,
                                dtype=np.float32).reshape(
                                    result.result[0].tensor_shape.dims)
    print("ms grpc client received: ")
    print(grpc_result)

    # ms result
    net = BertModel(bert_net_cfg, False)
    bert_out = net(Tensor(input_ids), Tensor(segment_ids), Tensor(input_mask))
    print("bert out: ")
    print(bert_out[0])
    bert_out_size = len(bert_out)

    # compare grpc result
    for i in range(bert_out_size):
        grpc_result = np.frombuffer(result.result[i].data,
                                    dtype=np.float32).reshape(
                                        result.result[i].tensor_shape.dims)
        logger.info("i:{}, grpc_result:{}, bert_out:{}".format(
            i, result.result[i].tensor_shape.dims,
            bert_out[i].asnumpy().shape))
        assert np.allclose(bert_out[i].asnumpy(),
                           grpc_result,
                           0.001,
                           0.001,
                           equal_nan=True)

    # http visit
    data = {
        "tensor":
        [input_ids.tolist(),
         segment_ids.tolist(),
         input_mask.tolist()]
    }
    url = "http://127.0.0.1:5501"
    input_json = json.dumps(data)
    headers = {'Content-type': 'application/json'}
    response = requests.post(url, data=input_json, headers=headers)
    result = response.text
    result = result.replace('\r', '\\r').replace('\n', '\\n')
    result_json = json.loads(result, strict=False)
    http_result = np.array(result_json['tensor'])
    print("ms http client received: ")
    print(http_result[0][:200])

    # compare http result
    for i in range(bert_out_size):
        logger.info("i:{}, http_result:{}, bert_out:{}".format(
            i, np.shape(http_result[i]), bert_out[i].asnumpy().shape))
        assert np.allclose(bert_out[i].asnumpy(),
                           http_result[i],
                           0.001,
                           0.001,
                           equal_nan=True)