11from 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
34
45try :
56 import numpy as np
89
910from .constants import Backend , Device , DType
1011from .utils import str_or_strsequence , to_string
11- from .tensor import Tensor , BlobTensor
12+ from . import convert
1213
1314
1415class Client (StrictRedis ):
@@ -45,13 +46,12 @@ def modelset(self,
4546 args += [data ]
4647 return self .execute_command (* args )
4748
48- def modelget (self , name : AnyStr ) -> dict :
49+ def modelget (self , name : AnyStr ) -> Model :
4950 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 ])))
5555
5656 def modeldel (self , name : AnyStr ) -> AnyStr :
5757 return self .execute_command ('AI.MODELDEL' , name )
@@ -68,71 +68,66 @@ def modelrun(self,
6868
6969 def tensorset (self ,
7070 key : AnyStr ,
71- tensor : Union [Tensor , np .ndarray , list , tuple ],
71+ tensor : Union [np .ndarray , list , tuple ],
7272 shape : Union [Sequence [int ], None ] = None ,
73- dtype : Union [DType , None ] = None ) -> Any :
73+ dtype : Union [DType , type , None ] = None ) -> Any :
7474 """
7575 Set the values of the tensor on the server using the provided Tensor object
7676 :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
8080 """
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
8381 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 ]
8584 elif isinstance (tensor , (list , tuple )):
8685 if shape is None :
8786 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 ]
9391 return self .execute_command (* args )
9492
9593 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 ]:
9896 """
9997 Retrieve the value of a tensor from the server. By default it returns the numpy array
10098 but it can be controlled using `as_type` argument and `meta_only` argument.
10199 :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
103103 :param meta_only: if true, then the value is not retrieved,
104104 only the shape and the type
105105 :return: an instance of as_type
106106 """
107- # TODO; We might remove Tensor & BlobTensor in the future and `tensorget` will return
108- # python list or numpy arrays or a namedtuple
109107 if meta_only :
110108 argname = 'META'
111- elif as_type is None :
112- argname = BlobTensor . ARGNAME
109+ elif as_numpy is True :
110+ argname = 'BLOB'
113111 else :
114- argname = as_type . ARGNAME
112+ argname = 'VALUES'
115113
116114 res = self .execute_command ('AI.TENSORGET' , key , argname )
117115 dtype , shape = to_string (res [0 ]), res [1 ]
118- dt = DType .__members__ [dtype .lower ()]
119116 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 )
123120 else :
124- return as_type . from_resp ( dt , shape , res [ 2 ] )
121+ return convert . to_sequence ( res [ 2 ] , shape , dtype )
125122
126123 def scriptset (self , name : AnyStr , device : Device , script : AnyStr ) -> AnyStr :
127124 return self .execute_command ('AI.SCRIPTSET' , name , device .value , script )
128125
129- def scriptget (self , name : AnyStr ) -> dict :
126+ def scriptget (self , name : AnyStr ) -> Script :
130127 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 ])))
136131
137132 def scriptdel (self , name ):
138133 return self .execute_command ('AI.SCRIPTDEL' , name )
0 commit comments