Skip to content

Commit 216467a

Browse files
author
Sherin Thomas
authored
Merge pull request #21 from RedisAI/new_commands
Tensor API changes and new commands
2 parents 7479bbf + f4f4a03 commit 216467a

File tree

12 files changed

+517
-260
lines changed

12 files changed

+517
-260
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
*.pyc
44
.venv/
55
redisai.egg-info
6-
.idea
6+
.idea
7+
.mypy_cache/

README.md

Lines changed: 196 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,214 @@ $ pip install ml2rt
3030
For a quick walk through, checkout this example
3131

3232
```python
33-
from redisai import Client
34-
from redisai import Tensor, BlobTensor, DType, Device, Backend
35-
import ml2rt
3633

34+
35+
print(client.tensorget('mul'))
36+
37+
# Try with a script
38+
script = ml2rt.load_script('test/testdata/script.txt')
39+
client.scriptset('ket', Device.cpu, script)
40+
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')
41+
```
42+
43+
## Documentation
44+
APIs available in redisai-py
45+
46+
### tensorset
47+
Set the values of the tensor on the server using the provided tensor object
48+
49+
##### Parameters
50+
- name: Key on which tensor is saved
51+
- tensor: a `Tensor` object
52+
- shape: Shape of the tensor
53+
- dtype: redisai.DType object represents data type of the tensor. Required if input is a `list`/`tuple`
54+
55+
##### Example
56+
```python
57+
from redisai import Client, DType
3758
client = Client()
38-
client.tensorset('x', Tensor(DType.float, [2], [2, 3]))
39-
t = client.tensorget('x')
40-
print(t.value)
59+
arr = np.array([2, 3])
60+
client.tensorset('x', arr)
61+
client.tensorget('y', [1, 2], dtype=DType.float)
62+
client.tensorget('z', [3, 4, 5, 6], dtype=DType.float, shape=(1, 2, 2))
63+
```
64+
65+
### tensorget
66+
67+
##### Parameters
68+
Retrieve the value of a tensor from the server. By default it returns the numpy array
69+
but it can be controlled using `as_type` and `meta_only` arguments
70+
- name: Key from where the tensor is saved
71+
- as_type: the resultant tensor type. Returns numpy array if None
72+
- meta_only: if true, then the value is not retrieved, only the shape and the type
73+
74+
##### Example
75+
```python
76+
from redisai import Tensor
77+
x = client.tensorget('x') # numpy array
78+
y = client.tensorget('y', as_type=Tensor) # A Tensor object
79+
z = client.tensorget('z', meta_only=True) # A Tensor object but without value
80+
```
81+
82+
### loadbackend
83+
RedisAI by default won't load any backends. User can either explicitly
84+
load the backend by using this function or let RedisAI load the required
85+
backend from the default path on-demand.
86+
87+
##### Parameters
88+
- identifier: String representing which backend. Allowed values - TF, TORCH & ONNX
89+
- path: Path to the shared object of the backend
90+
91+
##### Example
92+
```python
93+
client.loadbackend('TORCH', 'install-cpu/backends/redisai_torch/redisai_torch.so')
94+
```
95+
96+
97+
### modelset
98+
Store a model of Tensorflow/PyTorch/ONNX format in RedisAI
99+
100+
##### Parameters
101+
- name: Key on which model should be saved
102+
- backend: redisai.Backend object - tf, torch or onnx
103+
- device: redisai.Device object - cpu or gpu
104+
- data: model as a byte string. `ml2rt.load_model(path)` returns this.
41105

42-
model = ml2rt.load_model('test/testdata/graph.pb')
43-
client.tensorset('a', Tensor.scalar(DType.float, 2, 3))
44-
client.tensorset('b', Tensor.scalar(DType.float, 12, 10))
106+
##### Example
107+
Tensorflow requires the input and output nodes of the graph while storing the model. For
108+
exporting a normal tensorflow session to pb file, you could use `ml2rt` package
109+
```python
110+
import ml2rt
111+
ml2rt.save_tensorflow(sess, 'path/to/graph.pb', output_nodes)
112+
model = ml2rt.load_model('path/to/graph.pb')
45113
client.modelset('m', Backend.tf,
46114
Device.cpu,
47-
input=['a', 'b'],
48-
output='mul',
115+
input=['input_1', 'input_2'],
116+
output='output',
117+
data=model)
118+
```
119+
Torch doesn't need input and output node information. You could use ml2rt for exporting torch
120+
model as well but ml2rt needs torchscript model rather than normal torch model. Checkout
121+
the [document](https://pytorch.org/docs/stable/jit.html) to learn more.
122+
```python
123+
import ml2rt
124+
ml2rt.save_torch('optimized_graph', 'path/to/graph.pt')
125+
model = ml2rt.load_model('path/to/graph.pt')
126+
client.modelset('m', Backend.torch, Device.cpu, data=model)
127+
```
128+
129+
### modelget
130+
Fetch the stored model from RedisAI
131+
132+
##### Parameters
133+
name: the name of the model
134+
135+
##### Example
136+
```python
137+
mod_det = client.modelget('m')
138+
print(mod_det['backend'], mod_det['device'])
139+
model_binary = mod_det['data']
140+
```
141+
142+
### modeldel
143+
Delete a stored model from RedisAI
144+
145+
##### Parameters
146+
- name: Key of the model
147+
148+
##### Example
149+
```python
150+
client.modeldel('m')
151+
```
152+
153+
154+
### modelrun
155+
Execute a model. Required inputs must be present in RedisAI before calling `modelrun`
156+
157+
##### Parameters
158+
- name: Key of the model
159+
- inputs: Key of the input tensors. It can be a single key or a list of keys
160+
- outputs: Key on which the output tensors will be saved. It can be a single key or list of keys
161+
162+
##### Example
163+
```python
164+
client.tensorset('a', [2, 3], dtype=DType.float, shape=(2,))
165+
client.tensorset('b', [12, 10], dtype=DType.float)
166+
model = ml2rt.load_model('test/testdata/graph.pt')
167+
client.modelset('m', Backend.torch,
168+
Device.cpu,
169+
input=['input_1', 'input_2'],
170+
output='output',
49171
data=model)
50172
client.modelrun('m', ['a', 'b'], ['mul'])
51-
print(client.tensorget('mul').value)
173+
out = client.tensorget('mul')
174+
```
52175

53-
# Try with a script
54-
script = ml2rt.load_script('test/testdata/script.txt')
55-
client.scriptset('ket', Device.cpu, script)
56-
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')
176+
### scriptset
177+
Store a SCRIPT in RedisAI. SCRIPT is a subset of python language itself but will be executed
178+
on high performance C++ runtime. RedisAI uses TORCH runtime to execute SCRIPT and it must
179+
follow the format required by the [doc](https://pytorch.org/docs/stable/jit.html).
180+
181+
##### Parameters
182+
- name: Key on which SCRIPT should be saved
183+
- device: redisai.Device object - cpu or gpu
184+
- script: SCRIPT as defined in [TorchScript documentation](https://pytorch.org/docs/stable/jit.html). SCRIPT must have functions defined in it (you can have multiple functions).
185+
186+
##### Example
187+
```python
188+
script = """
189+
def myfunc(a, b):
190+
return a + b
191+
"""
192+
client.scriptset('script', Device.cpu, script)
193+
```
57194

58-
b1 = client.tensorget('c', as_type=BlobTensor)
59-
b2 = client.tensorget('c', as_type=BlobTensor)
60195

61-
client.tensorset('d', BlobTensor(DType.float, b1.shape, b1, b2))
196+
### scriptget
197+
Fetch a stored SCRIPT from RedisAI
62198

63-
tnp = b1.to_numpy()
64-
print(tnp)
199+
##### Parameters
200+
- name: Key from which SCRIPT can be retrieved
65201

202+
##### Example
203+
```python
204+
script_details = client.scriptget('script')
205+
device = script_details['device']
206+
script = script_details['script']
66207
```
67208

68209

210+
### scriptdel
211+
Delete a stored SCRIPT from RedisAI
212+
213+
##### Parameters
214+
- name: Key from which SCRIPT can be retrieved
215+
216+
##### Example
217+
```python
218+
client.scriptdel('script')
219+
```
220+
221+
222+
### scriptrun
223+
Execute a SCRIPT. Required inputs must be present in RedisAI before calling `modelrun`
224+
225+
##### Parameters
226+
- name: Key from which SCRIPT can be retrieved
227+
- function: name of the function to call. The function that you call can call other functions in the same SCRIPT
228+
- inputs: Key of the input tensors. It can be a single key or a list of keys
229+
- outputs: Key on which the output tensors will be saved. It can be a single key or list of keys
230+
231+
##### Example
232+
```python
233+
script = """
234+
def myfunc(a, b):
235+
return a + b
236+
"""
237+
client.scriptset('script', Device.cpu, script)
238+
client.tensorget('a', [1, 2], dtype=DType.float)
239+
client.tensorget('b', [3, 4], dtype=DType.float)
240+
client.scriptrun('script', 'mufunc', ['a', 'b'], 'out')
241+
out = client.tensorget('out') # => [4, 6]
242+
```
243+

example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from redisai import Client, Tensor, \
22
BlobTensor, DType, Device, Backend
3-
import mlut
3+
import ml2rt
44

55
client = Client()
66
client.tensorset('x', Tensor(DType.float, [2], [2, 3]))
77
t = client.tensorget('x')
88
print(t.value)
99

10-
model = mlut.load_model('test/testdata/graph.pb')
10+
model = ml2rt.load_model('test/testdata/graph.pb')
1111
client.tensorset('a', Tensor.scalar(DType.float, 2, 3))
1212
client.tensorset('b', Tensor.scalar(DType.float, 12, 10))
1313
client.modelset('m', Backend.tf,
@@ -19,7 +19,7 @@
1919
print(client.tensorget('mul').value)
2020

2121
# Try with a script
22-
script = mlut.load_script('test/testdata/script.txt')
22+
script = ml2rt.load_script('test/testdata/script.txt')
2323
client.scriptset('ket', Device.cpu, script)
2424
client.scriptrun('ket', 'bar', input=['a', 'b'], output='c')
2525

redisai/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from .version import __version__ # noqa
2-
from .client import (Client, Tensor, BlobTensor, DType, Device, Backend) # noqa
1+
from .version import __version__
2+
from .client import Client
3+
from .tensor import Tensor, BlobTensor
4+
from .constants import DType, Device, Backend

redisai/_util.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)