8000 feat: unify the fields for sparse and dense array by nan-wang · Pull Request #2578 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: unify the fields for sparse and dense array #2578

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 15 commits into from
Jun 15, 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
2 changes: 1 addition & 1 deletion jina/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# do not change this line manually
# this is managed by proto/build-proto.sh and updated on every execution
__proto_version__ = '0.0.81'
__proto_version__ = '0.0.82'

__uptime__ = _datetime.datetime.now().isoformat()

Expand Down
4 changes: 2 additions & 2 deletions jina/proto/jina.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ message SparseNdArrayProto {
// A 1-D tensor of any type and shape [N], which supplies the values for each element in indices.
DenseNdArrayProto values = 2;

// A 1-D int64 tensor of shape [ndims], which specifies the dense_shape of the sparse tensor.
repeated int64 dense_shape = 3;
// A 1-D int64 tensor of shape [ndims], which specifies the shape of the sparse tensor.
repeated uint32 shape = 3;
}

/**
Expand Down
80 changes: 40 additions & 40 deletions jina/proto/jina_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions jina/types/ndarray/sparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ class BaseSparseNdArray(BaseNdArray):
"""

def __init__(self, *args, **kwargs):
"""Set constructor method."""
"""Set constructor method.

:param args: args passed to super().
:param kwargs: kwargs passed to super().
"""
super().__init__(*args, **kwargs)
self.is_sparse = True

def null_proto(self):
"""Get the new protobuf representation."""
def null_proto(self) -> 'jina_pb2.SparseNdArrayProto':
"""Get the new protobuf representation.

:return: An empty `SparseNdArrayProto`
"""
return jina_pb2.SparseNdArrayProto()

def sparse_constructor(
Expand All @@ -39,6 +46,8 @@ def sparse_constructor(
:param values: the values of the sparse array
:param shape: the shape of the sparse array
:return: Sparse NdArray

.. # noqa: DAR202
"""
raise NotImplementedError

Expand All @@ -50,23 +59,31 @@ def sparse_parser(

:param value: the sparse ndarray
:return: a Dict with three entries {'indices': ..., 'values':..., 'shape':...}

.. # noqa: DAR202
"""
raise NotImplementedError

@property
def value(self) -> AnySparseNdArray:
"""Get the value of protobuf message in :class:`SparseNdArray`."""
"""Get the value of protobuf message in :class:`SparseNdArray`.

:return: A :class:`SparseNdArray`.
"""
idx = DenseNdArray(self._pb_body.indices).value
val = DenseNdArray(self._pb_body.values).value
shape = self._pb_body.dense_shape
shape = self._pb_body.shape
if idx is not None and val is not None and shape:
return self.sparse_constructor(idx, val, shape)

@value.setter
def value(self, value: AnySparseNdArray):
"""Set the value of protobuf message with :param:`value` in :class:`SparseNdArray`."""
"""Set the value of protobuf message with :param:`value` in :class:`SparseNdArray`.

:param value: The :class:`SparseNdArray` to be set as the value of the cls.
"""
r = self.sparse_parser(value)
DenseNdArray(self._pb_body.indices).value = r['indices']
DenseNdArray(self._pb_body.values).value = r['values']
self._pb_body.ClearField('dense_shape')
self._pb_body.dense_shape.extend(r['shape'])
self._pb_body.ClearField('shape')
self._pb_body.shape.extend(r['shape'])
Loading
0