From ed6e54c53ae68d28ea40b5b7547cde2c3882a00f Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Fri, 1 May 2020 12:39:54 +0200 Subject: [PATCH 1/3] test: add unit test for quantization --- tests/test_quant.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/test_quant.py diff --git a/tests/test_quant.py b/tests/test_quant.py new file mode 100644 index 0000000000000..7684e669186bf --- /dev/null +++ b/tests/test_quant.py @@ -0,0 +1,56 @@ +import os + +import numpy as np +from jina.drivers.helper import array2pb, pb2array +from jina.flow import Flow +from jina.proto import jina_pb2 +from tests import JinaTestCase + +replicas = 10 + +num_docs = 100 +chunks_per_doc = 100 +embed_dim = 1000 + + +def random_docs(): + c_id = 0 + np.random.seed(531) + for j in range(num_docs): + d = jina_pb2.Document() + for k in range(chunks_per_doc): + c = d.chunks.add() + c.embedding.CopyFrom(array2pb(np.random.random([embed_dim]))) + c.chunk_id = c_id + c.doc_id = j + c_id += 1 + yield d + + +def get_output(req): + np.random.seed(531) + + err = 0 + for d in req.docs: + for c in d.chunks: + recv = pb2array(c.embedding) + send = np.random.random([embed_dim]) + err += np.sum(np.abs(recv - send)) / embed_dim + + print(f'reconstruction error: {err / num_docs:.6f}') + + +class MyTestCase(JinaTestCase): + + def f1(self, quant): + os.environ['JINA_ARRAY_QUANT'] = quant + + f = Flow(callback_on_body=True).add(yaml_path='_forward').add(yaml_path='_forward').add( + yaml_path='_forward').add( + yaml_path='_forward').add(yaml_path='_forward').add(yaml_path='_forward').add(yaml_path='_forward') + with f as fl: + fl.index(random_docs, callback=get_output, in_proto=True) + + def test_quant(self): + for j in ('fp32', 'fp16', 'uint8'): + self.f1(j) From eaa43dfd8e35cfaa2e7c35fec3f0483b9a0fca68 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Fri, 1 May 2020 13:24:44 +0200 Subject: [PATCH 2/3] test: add unit test for quantization --- tests/test_quant.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_quant.py b/tests/test_quant.py index 7684e669186bf..aa60bf38a2ec3 100644 --- a/tests/test_quant.py +++ b/tests/test_quant.py @@ -20,7 +20,8 @@ def random_docs(): d = jina_pb2.Document() for k in range(chunks_per_doc): c = d.chunks.add() - c.embedding.CopyFrom(array2pb(np.random.random([embed_dim]))) + # force sending at non-quantization + c.embedding.CopyFrom(array2pb(np.random.random([embed_dim]), quantize=None)) c.chunk_id = c_id c.doc_id = j c_id += 1 From 8279bdc68d0ad5a751db5e2702c30f3f350fabc5 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Fri, 1 May 2020 14:37:18 +0200 Subject: [PATCH 3/3] fix: optimize helloworld network load --- jina/helloworld/__init__.py | 1 + jina/resources/helloworld.encoder.yml | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/jina/helloworld/__init__.py b/jina/helloworld/__init__.py index 243212385c1bd..857475da39f40 100644 --- a/jina/helloworld/__init__.py +++ b/jina/helloworld/__init__.py @@ -41,6 +41,7 @@ def hello_world(args): os.environ['REPLICAS'] = str(args.replicas) os.environ['HW_WORKDIR'] = args.workdir os.environ['WITH_LOGSERVER'] = str(args.logserver) + os.environ['JINA_ARRAY_QUANT'] = 'fp16' f = Flow().load_config(args.index_yaml_path) with f: diff --git a/jina/resources/helloworld.encoder.yml b/jina/resources/helloworld.encoder.yml index 7eb2f8db960a8..b036148758df4 100644 --- a/jina/resources/helloworld.encoder.yml +++ b/jina/resources/helloworld.encoder.yml @@ -1,4 +1,14 @@ !MyEncoder metas: name: myenc # a customized name - workspace: $HW_WORKDIR \ No newline at end of file + workspace: $HW_WORKDIR +requests: + on: + IndexRequest: + - !EncodeDriver {} + - !DocPruneDriver + with: + pruned: + - meta_info + SearchRequest: + - !EncodeDriver {}