def __getitem__(self, key: str): """ Parse and return the attribute corresponding to the given key """ _xattr = self._xattr_map[key] assert _xattr.name == key if _xattr.type == 'UNDEFINED': return None elif _xattr.type == 'BOOL': return _xattr.b elif _xattr.type == 'INT': return _xattr.i elif _xattr.type == 'INTS': return IntVector(_xattr.ints) elif _xattr.type == 'INTS2D': return IntVector2D(_xattr.ints2d) elif _xattr.type == 'FLOAT': return _xattr.f elif _xattr.type == 'FLOATS': return FloatVector(_xattr.floats) elif _xattr.type == 'STRING': return _xattr.s elif _xattr.type == 'STRINGS': return StrVector(_xattr.strings) elif _xattr.type == 'MAP_STR_STR': return MapStrStr(_xattr.map_str_str) elif _xattr.type == 'MAP_STR_VSTR': return MapStrVectorStr(_xattr.map_str_vstr) else: raise NotImplementedError( "Unsupported attribute: {} of type: {}".format( _xattr, _xattr.type))
def test_set_value(self): ts = TensorShape(IntVector(lpx.IntVector([1, 2, 3, 4]))) ts.set_value(0, -1) assert len(ts) == 4 assert ts[0] == -1 assert ts[1] == 2
def test_get_set_item_slice(self): ts = TensorShape(IntVector(lpx.IntVector([-1, 2, 3, 4]))) ts_slice = ts[1:3] assert len(ts_slice) == 2 assert ts_slice == [2, 3] ts[1:3] = [3, 2] assert ts == [-1, 3, 2, 4]
def test_to_list(self): ts = TensorShape(IntVector(lpx.IntVector([1, 2, 3, 4]))) assert ts == [1, 2, 3, 4] lst = ts.tolist() lst2 = list(ts) ts[0] = -1 assert ts == [-1, 2, 3, 4] assert lst == [1, 2, 3, 4] assert lst2 == [1, 2, 3, 4]
def test_get_set_item(self): ts = TensorShape(IntVector(lpx.IntVector([-1, 2]))) assert len(ts) == 2 assert isinstance(ts[0], int) assert ts[0] == -1 assert ts[1] == 2 with self.assertRaises(IndexError): ts[3] ts[1] = 3 assert ts[1] == 3
def shapes(self): _shapes = self._xlayer.shapes _shapes_t = self._xlayer.shapes_t if _shapes_t == 'TensorShape' and len(_shapes) != 1: raise ValueError("TensorShape can only be one dimensional" " but got: {}".format(len(_shapes))) if _shapes_t == 'TensorShape': return TensorShape(IntVector(_shapes[0])) elif _shapes_t == 'TupleShape': return TupleShape(IntVector2D(_shapes)) else: raise ValueError("Unsupported shapes type: {}, should be" " TensorShape or TupleShape".format(_shapes_t))
def test_get_size(self): ts = TensorShape(IntVector(lpx.IntVector([-1, 2, 3, 4]))) assert ts.get_size() == [24]
def sizes(self): return IntVector(self._xlayer.sizes)
class OpaqueFunc(object): # TypeCode conversion functions # First: C++ -> Python # Second: Python -> C++ type_codes_ = { TypeCode.vInt: ( lambda arg_: IntVector(arg_.ints), lambda arg_: lpx.OpaqueValue(lpx.IntVector(arg_))), TypeCode.Str: ( lambda arg_: arg_.s, lambda arg_: lpx.OpaqueValue(arg_)), TypeCode.Byte: ( lambda arg_: arg_.bytes, lambda arg_: lpx.OpaqueValue(arg_)), TypeCode.vStr: ( lambda arg_: StrVector(arg_.strings), lambda arg_: lpx.OpaqueValue(lpx.StrVector(arg_))), TypeCode.StrContainer: ( lambda arg_: StrContainer.from_lib(arg_.str_c), lambda arg_: lpx.OpaqueValue(arg_._str_c)), TypeCode.BytesContainer: ( lambda arg_: BytesContainer.from_lib(arg_.bytes_c), lambda arg_: lpx.OpaqueValue(arg_._bytes_c)), TypeCode.XGraph: ( lambda arg_: XGraph._from_xgraph(arg_.xg), lambda arg_: lpx.OpaqueValue(arg_._xgraph)), TypeCode.XBuffer: ( lambda arg_: XBuffer.from_lib(arg_.xb), lambda arg_: lpx.OpaqueValue(arg_._xb)), TypeCode.vXBuffer: ( lambda arg_: [XBuffer.from_lib(e) for e in arg_.xbuffers], lambda arg_: lpx.OpaqueValue( lpx.XBufferHolderVector([xb._xb for xb in arg_]))), TypeCode.OpaqueFunc: ( lambda arg_: OpaqueFunc.from_lib(arg_.of), lambda arg_: lpx.OpaqueValue(arg_._of)) } def __init__(self, func: Callable = None, type_codes: List[TypeCode] = None) -> None: self._of = lpx.OpaqueFunc() if type_codes is None: type_codes = [] if func is not None: self.set_func(func, type_codes) @classmethod def from_lib(cls, _of: lpx.OpaqueFunc) -> 'OpaqueFunc': of = OpaqueFunc.__new__(cls) of._of = _of return of def set_func(self, func: Callable, type_codes: List[TypeCode]): # if type_codes is not None: for tc in type_codes: if tc not in OpaqueFunc.type_codes_: raise NotImplementedError("Function with argument of" " unsupported type: {} provided" .format(tc.name)) def opaque_func_wrapper(args): new_args = [] if type_codes is not None: args_type_codes = type_codes else: args_type_codes = [TypeCode(args[i].get_type_code_int()) for i in range(len(args))] for tc, arg_ in zip(args_type_codes, args): if tc not in OpaqueFunc.type_codes_: raise ValueError(f"Unsupported type code: {tc}") arg_ = OpaqueFunc.type_codes_[tc][0](arg_) new_args.append(arg_) func(*new_args) arg_type_codes_ = lpx.IntVector([tc.value for tc in type_codes]) self._of.set_func(opaque_func_wrapper, arg_type_codes_) def __call__(self, *args: Any) -> None: """ Call internal lib OpaqueFunc with provided args """ args_type_codes = self.get_arg_type_codes() if len(args) != len(args_type_codes): raise ValueError("Invalid number of arguments detected." " OpaqueFunc is expecting {} arguments" " but got: {}" .format(len(args_type_codes), len(args))) oa_v = [] for tc, arg_ in zip(args_type_codes, args): if tc not in OpaqueFunc.type_codes_: raise ValueError(f"Unsupported type code: {tc}") oa_v.append(OpaqueFunc.type_codes_[tc][1](arg_)) oa = lpx.OpaqueArgs(oa_v) self._of(oa) def get_arg_type_codes(self): return [TypeCode(i) for i in self._of.get_arg_type_codes()] def get_nb_type_codes(self): return len(self.get_arg_type_codes()) def __del__(self): pass
def test_int_vector(self): iv = lpx.IntVector([1, 2, 3]) ivx = IntVector(iv) assert ivx == iv assert ivx == [1, 2, 3] # List comprehension assert [i for i in ivx] == [1, 2, 3] # Append ivx.append(4) assert len(iv) == 4 assert len(ivx) == 4 # Contains assert 2 in iv assert 2 in ivx assert -1 not in iv assert -1 not in ivx with self.assertRaises(TypeError): assert 'a' not in ivx # Delete del ivx[3] assert ivx == [1, 2, 3] assert iv == lpx.IntVector([1, 2, 3]) # Equal assert ivx == [1, 2, 3] assert ivx == lpx.IntVector([1, 2, 3]) assert iv == lpx.IntVector([1, 2, 3]) # Add / Extend ivx.extend([4, 5]) assert len(iv) == 5 assert len(ivx) == 5 ivx += [6, 7] assert len(ivx) == 7 del ivx[6] del ivx[5] # Get item assert ivx[3] == 4 assert ivx[-1] == 5 assert len(ivx) == 5 assert ivx[:] == [1, 2, 3, 4, 5] with self.assertRaises(IndexError): ivx[6] # Index assert ivx.index(2) == 1 assert ivx.index(5) == 4 with self.assertRaises(ValueError): ivx.index('a') # Insert ivx.insert(0, -1) assert len(ivx) == 6 assert len(iv) == 6 assert ivx[0] == -1 del ivx[0] # Iter c = [1, 2, 3, 4, 5] for i, e in enumerate(ivx): assert e == c[i] for i, e in enumerate(iv): assert e == c[i] # Length assert len(ivx) == len(iv) assert len(ivx) == 5 # Not equal assert iv != lpx.IntVector([1, 2, 3]) assert ivx != [1, 2, 3, 4] # Repr assert repr(iv) == "IntVector[1, 2, 3, 4, 5]" assert repr(ivx) == "IntVector[1, 2, 3, 4, 5]" # Str assert str(iv) == "IntVector[1, 2, 3, 4, 5]" assert str(ivx) == "IntVector[1, 2, 3, 4, 5]" # Set ivx[0] = -1 assert ivx == [-1, 2, 3, 4, 5] assert iv == lpx.IntVector([-1, 2, 3, 4, 5]) with self.assertRaises(IndexError): ivx[6] = -1 ivx[:] = [-1, -2, -3, -4, -5] assert len(ivx) == 5 assert ivx == [-1, -2, -3, -4, -5]