1
1
from redis import StrictRedis
2
- from typing import Union , Any , AnyStr , ByteString , Sequence , Type
2
+ from typing import Union , Any , AnyStr , ByteString , Sequence
3
+ from .containers import Script , Model , Tensor
3
4
4
5
try :
5
6
import numpy as np
8
9
9
10
from .constants import Backend , Device , DType
10
11
from .utils import str_or_strsequence , to_string
11
- from .tensor import Tensor , BlobTensor
12
+ from . import convert
12
13
13
14
14
15
class Client (StrictRedis ):
@@ -45,13 +46,12 @@ def modelset(self,
45
46
args += [data ]
46
47
return self .execute_command (* args )
47
48
48
- def modelget (self , name : AnyStr ) -> dict :
49
+ def modelget (self , name : AnyStr ) -> Model :
49
50
rv = self .execute_command ('AI.MODELGET' , name )
50
- return {
51
- 'backend' : Backend (to_string (rv [0 ])),
52
- 'device' : Device (to_string (rv [1 ])),
53
- 'data' : rv [2 ]
54
- }
51
+ return Model (
52
+ rv [2 ],
53
+ Device (to_string (rv [1 ])),
54
+ Backend (to_string (rv [0 ])))
55
55
56
56
def modeldel (self , name : AnyStr ) -> AnyStr :
57
57
return self .execute_command ('AI.MODELDEL' , name )
@@ -68,71 +68,66 @@ def modelrun(self,
68
68
69
69
def tensorset (self ,
70
70
key : AnyStr ,
71
- tensor : Union [Tensor , np .ndarray , list , tuple ],
71
+ tensor : Union [np .ndarray , list , tuple ],
72
72
shape : Union [Sequence [int ], None ] = None ,
73
- dtype : Union [DType , None ] = None ) -> Any :
73
+ dtype : Union [DType , type , None ] = None ) -> Any :
74
74
"""
75
75
Set the values of the tensor on the server using the provided Tensor object
76
76
:param key: The name of the tensor
77
- :param tensor: a `Tensor ` object
78
- :param shape: Shape of the tensor
79
- :param dtype: data type of the tensor. Required if input is a sequence of ints/floats
77
+ :param tensor: a `np.ndarray ` object or python list or tuple
78
+ :param shape: Shape of the tensor. Required if `tensor` is list or tuple
79
+ :param dtype: data type of the tensor. Required if `tensor` is list or tuple
80
80
"""
81
- # TODO: tensorset will not accept BlobTensor or Tensor object in the future.
82
- # Keeping it in the current version for compatibility with the example repo
83
81
if np and isinstance (tensor , np .ndarray ):
84
- tensor = BlobTensor .from_numpy (tensor )
82
+ tensor = convert .from_numpy (tensor )
83
+ args = ['AI.TENSORSET' , key , tensor .dtype .value , * tensor .shape , tensor .argname , tensor .value ]
85
84
elif isinstance (tensor , (list , tuple )):
86
85
if shape is None :
87
86
shape = (len (tensor ),)
88
- tensor = Tensor (dtype , shape , tensor )
89
- args = ['AI.TENSORSET' , key , tensor .type .value ]
90
- args += tensor .shape
91
- args += [tensor .ARGNAME ]
92
- args += tensor .value
87
+ if not isinstance (dtype , DType ):
88
+ dtype = DType .__members__ [np .dtype (dtype ).name ]
89
+ tensor = convert .from_sequence (tensor , shape , dtype )
90
+ args = ['AI.TENSORSET' , key , tensor .dtype .value , * tensor .shape , tensor .argname , * tensor .value ]
93
91
return self .execute_command (* args )
94
92
95
93
def tensorget (self ,
96
- key : AnyStr , as_type : Type [ Tensor ] = None ,
97
- meta_only : bool = False ) -> Union [Tensor , BlobTensor ]:
94
+ key : AnyStr , as_numpy : bool = True ,
95
+ meta_only : bool = False ) -> Union [Tensor , np . ndarray ]:
98
96
"""
99
97
Retrieve the value of a tensor from the server. By default it returns the numpy array
100
98
but it can be controlled using `as_type` argument and `meta_only` argument.
101
99
:param key: the name of the tensor
102
- :param as_type: the resultant tensor type. Returns numpy array if None
100
+ :param as_numpy: Should it return data as numpy.ndarray.
101
+ Wraps with namedtuple if False. This flag also decides how to fetch the
102
+ value from RedisAI server and could have performance implications
103
103
:param meta_only: if true, then the value is not retrieved,
104
104
only the shape and the type
105
105
:return: an instance of as_type
106
106
"""
107
- # TODO; We might remove Tensor & BlobTensor in the future and `tensorget` will return
108
- # python list or numpy arrays or a namedtuple
109
107
if meta_only :
110
108
argname = 'META'
111
- elif as_type is None :
112
- argname = BlobTensor . ARGNAME
109
+ elif as_numpy is True :
110
+ argname = 'BLOB'
113
111
else :
114
- argname = as_type . ARGNAME
112
+ argname = 'VALUES'
115
113
116
114
res = self .execute_command ('AI.TENSORGET' , key , argname )
117
115
dtype , shape = to_string (res [0 ]), res [1 ]
118
- dt = DType .__members__ [dtype .lower ()]
119
116
if meta_only :
120
- return Tensor ( dt , shape , [] )
121
- elif as_type is None :
122
- return BlobTensor . from_resp ( dt , shape , res [ 2 ]). to_numpy ( )
117
+ return convert . to_sequence ([] , shape , dtype )
118
+ if as_numpy is True :
119
+ return convert . to_numpy ( res [ 2 ] , shape , dtype )
123
120
else :
124
- return as_type . from_resp ( dt , shape , res [ 2 ] )
121
+ return convert . to_sequence ( res [ 2 ] , shape , dtype )
125
122
126
123
def scriptset (self , name : AnyStr , device : Device , script : AnyStr ) -> AnyStr :
127
124
return self .execute_command ('AI.SCRIPTSET' , name , device .value , script )
128
125
129
- def scriptget (self , name : AnyStr ) -> dict :
126
+ def scriptget (self , name : AnyStr ) -> Script :
130
127
r = self .execute_command ('AI.SCRIPTGET' , name )
131
- device = Device (to_string (r [0 ]))
132
- return {
133
- 'device' : device ,
134
- 'script' : to_string (r [1 ])
135
- }
128
+ return Script (
129
+ to_string (r [1 ]),
130
+ Device (to_string (r [0 ])))
136
131
137
132
def scriptdel (self , name ):
138
133
return self .execute_command ('AI.SCRIPTDEL' , name )
0 commit comments