8000 fix: size assertion for first add call by maximilianwerk · Pull Request #1905 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: size assertion for first add call #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jina/executors/indexers/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def _validate_key_vector_shapes(self, keys, vectors):
elif self.dtype != vectors.dtype.name:
raise TypeError(
f'vectors\' dtype {vectors.dtype.name} does not match with indexers\'s dtype: {self.dtype}')
elif keys.shape[0] != vectors.shape[0]:

if keys.shape[0] != vectors.shape[0]:
raise ValueError(f'number of key {keys.shape[0]} not equal to number of vectors {vectors.shape[0]}')

def add(self, keys: Iterable[str], vectors: 'np.ndarray', *args, **kwargs) -> None:
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/executors/indexers/test_numpyindexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_numpy_indexer_long_ids(test_metas):
indexer.save()
assert os.path.exists(indexer.index_abspath)
save_abspath = indexer.save_abspath
# assert False

with BaseIndexer.load(save_abspath) as indexer:
assert isinstance(indexer, NumpyIndexer)
Expand All @@ -56,6 +55,16 @@ def test_numpy_indexer_long_ids(test_metas):
assert idx.shape == (num_query, 4)


def test_numpy_indexer_assert_shape_mismatch(test_metas):
with NumpyIndexer(metric='euclidean', index_filename='np.test.gz', compress_level=0,
metas=test_metas) as indexer:
indexer.batch_size = 4
vec_short = np.array([[1, 1, 1], [2, 2, 2]])
vec_keys = np.array([1, 2, 3])
with pytest.raises(ValueError):
indexer.add(vec_keys, vec_short)


@pytest.mark.parametrize('batch_size, compress_level', [(None, 0), (None, 1), (16, 0), (16, 1)])
def test_numpy_indexer_known(batch_size, compress_level, test_metas):
vectors = np.array([[1, 1, 1],
Expand Down
0