Skip to content
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

Quick Cleanup #37

Merged
merged 3 commits into from
May 7, 2020
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 .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.0
current_version = 1.0.1
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<release>[a-z]+)(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project = 'redisai-py'
copyright = '2020, RedisLabs'
author = 'RedisLabs'
release = '1.0.0'
release = '1.0.1'
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.extlinks',
Expand Down
2 changes: 1 addition & 1 deletion redisai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .client import Client

__version__ = '1.0.0'
__version__ = '1.0.1'
6 changes: 3 additions & 3 deletions redisai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def tensorget(self,
self.commands.extend(args)
self.commands.append("|>")
self.result_processors.append(partial(utils.tensorget_postprocessor,
as_numpy,
meta_only))
as_numpy=as_numpy,
meta_only=meta_only))
return self

def modelrun(self,
Expand Down Expand Up @@ -407,7 +407,7 @@ def tensorget(self,
"""
args = builder.tensorget(key, as_numpy, meta_only)
res = self.execute_command(*args)
return utils.tensorget_postprocessor(as_numpy, meta_only, res)
return utils.tensorget_postprocessor(res, as_numpy, meta_only)

def scriptset(self, key: AnyStr, device: str, script: str, tag: AnyStr = None) -> str:
"""
Expand Down
6 changes: 6 additions & 0 deletions redisai/command_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def modelset(self, name: AnyStr, backend: str, device: str, data: ByteString,
batch: int, minbatch: int, tag: AnyStr,
inputs: Union[AnyStr, List[AnyStr]],
outputs: Union[AnyStr, List[AnyStr]]) -> Sequence:
if device.upper() not in utils.allowed_devices:
raise ValueError(f"Device not allowed. Use any from {utils.allowed_devices}")
if backend.upper() not in utils.allowed_backends:
raise ValueError(f"Backend not allowed. Use any from {utils.allowed_backends}")
args = ['AI.MODELSET', name, backend, device]

if batch is not None:
Expand Down Expand Up @@ -87,6 +91,8 @@ def tensorget(self,
return args

def scriptset(self, name: AnyStr, device: str, script: str, tag: AnyStr = None) -> Sequence:
if device.upper() not in utils.allowed_devices:
raise ValueError(f"Device not allowed. Use any from {utils.allowed_devices}")
args = ['AI.SCRIPTSET', name, device]
if tag:
args += ['TAG', tag]
Expand Down
20 changes: 14 additions & 6 deletions redisai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
'uint32': 'UINT32',
'uint64': 'UINT64'}

allowed_devices = {'CPU', 'GPU'}
allowed_backends = {'TF', 'TFLITE', 'TORCH', 'ONNX'}


def numpy2blob(tensor: np.ndarray) -> tuple:
""" Convert the numpy input from user to `Tensor` """
"""Convert the numpy input from user to `Tensor`."""
try:
dtype = dtype_dict[str(tensor.dtype)]
except KeyError:
Expand All @@ -29,7 +32,7 @@ def numpy2blob(tensor: np.ndarray) -> tuple:


def blob2numpy(value: ByteString, shape: Union[list, tuple], dtype: str) -> np.ndarray:
""" Convert `BLOB` result from RedisAI to `np.ndarray` """
"""Convert `BLOB` result from RedisAI to `np.ndarray`."""
mm = {
'FLOAT': 'float32',
'DOUBLE': 'float64'
Expand All @@ -40,6 +43,7 @@ def blob2numpy(value: ByteString, shape: Union[list, tuple], dtype: str) -> np.n


def list2dict(lst):
"""Convert the list from RedisAI to a dict."""
if len(lst) % 2 != 0:
raise RuntimeError("Can't unpack the list: {}".format(lst))
out = {}
Expand All @@ -55,10 +59,8 @@ def list2dict(lst):
def recursive_bytetransform(arr: List[AnyStr], target: Callable) -> list:
"""
Recurse value, replacing each element of b'' with the appropriate element.
Function returns the same array after inplace operation which updates `arr`

:param target: Type of tensor | array
:param arr: The array with b'' numbers or recursive array of b''
Function returns the same array after inplace operation which updates `arr`
"""
for ix in range(len(arr)):
obj = arr[ix]
Expand All @@ -70,10 +72,16 @@ def recursive_bytetransform(arr: List[AnyStr], target: Callable) -> list:


def listify(inp: Union[str, Sequence[str]]) -> Sequence[str]:
"""Wrap the ``inp`` with a list if it's not a list already."""
return (inp,) if not isinstance(inp, (list, tuple)) else inp


def tensorget_postprocessor(as_numpy, meta_only, rai_result):
def tensorget_postprocessor(rai_result, as_numpy, meta_only):
"""Process the tensorget output.

If ``as_numpy`` is True, it'll be converted to a numpy array. The required
information such as datatype and shape must be in ``rai_result`` itself.
"""
rai_result = list2dict(rai_result)
if meta_only:
return rai_result
Expand Down
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

setup(
name='redisai',
version='1.0.0',
version='1.0.1',
description='RedisAI Python Client',
long_description=long_description,
long_description_content_type='text/markdown',
long_description_content_type='text/x-rst',
url='http://github.com/RedisAI/redisai-py',
author='RedisLabs',
author_email='oss@redislabs.com',
packages=find_packages(),
install_requires=['redis', 'hiredis', 'numpy'],
python_requires='>=3.2',
python_requires='>=3.6',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand All @@ -26,7 +26,6 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Database',
'Topic :: Software Development :: Testing'
'Topic :: Database'
]
)
12 changes: 12 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ def test_numpy_tensor(self):
with self.assertRaises(TypeError):
con.tensorset('trying', stringarr)

def test_modelset_errors(self):
model_path = os.path.join(MODEL_DIR, 'graph.pb')
model_pb = load_model(model_path)
con = self.get_client()
with self.assertRaises(ValueError):
con.modelset('m', 'tf', 'wrongdevice', model_pb,
inputs=['a', 'b'], outputs=['mul'], tag='v1.0')
with self.assertRaises(ValueError):
con.modelset('m', 'wrongbackend', 'cpu', model_pb,
inputs=['a', 'b'], outputs=['mul'], tag='v1.0')


def test_modelget_meta(self):
model_path = os.path.join(MODEL_DIR, 'graph.pb')
model_pb = load_model(model_path)
Expand Down