def test_ModelField_contact_signature_name_none(): name = config.default_model_name path = "/home/user/folder/model/cool/" signature = ModelSignature(inputs=[ ModelField(name="test", dtype=DataType.Name(2), shape=TensorShape()) ], outputs=[ ModelField(name="test", dtype=DataType.Name(2), shape=TensorShape()) ]) with pytest.raises( SignatureViolationException, match=r"Creating model without signature_name is not allowed.*"): _ = ModelVersionBuilder(name, path).with_signature(signature)
def shape_to_proto(user_shape): if isinstance(user_shape, dict): user_shape = user_shape.get("dims") if user_shape == "scalar": shape = TensorShape() elif user_shape is None: shape = None elif isinstance(user_shape, list) or isinstance(user_shape, tuple): if not all(isinstance(dim, numbers.Number) for dim in user_shape): raise TypeError("shape_list contains incorrect dim", user_shape) shape = TensorShape(dims=user_shape) else: raise ValueError("Invalid shape value", user_shape) return shape
def test_ModelField_dt_invalid_output(): name = config.default_model_name path = "/home/user/folder/model/cool/" signature = ModelSignature( signature_name="test", inputs=[ ModelField(name="test", dtype=DataType.Name(2), shape=TensorShape()) ], outputs=[ModelField(name="test", shape=TensorShape())]) with pytest.raises( SignatureViolationException, match=r"Creating model with invalid dtype in the signature output.*" ): _ = ModelVersionBuilder(name, path).with_signature(signature)
def tensor_shape_proto_from_list(shape: Iterable[int]) -> TensorShape: """ Helper function to transform shape in the form of a tuple (Numpy shape representation) into a TensorProtoShape :param shape: Shape in a tuple form :return: same shape but in a TensorShape object """ return TensorShape(dims=shape)
def test_correct_signature(self): self.generate_signature() path = os.path.abspath("test/models/calculator") runtime = GRPCServer(path) runtime.start(port="9090") try: time.sleep(1) channel = grpc.insecure_channel('localhost:9090') client = PredictionServiceStub(channel=channel) a, b = Tensor(), Tensor() a.ParseFromString( Tensor(dtype=DT_INT8, int_val=[3]).SerializeToString()) b.ParseFromString( Tensor(dtype=DT_INT8, int_val=[2]).SerializeToString()) request = PredictRequest(inputs={"a": a, "b": b}) result = client.Predict(request) expected = PredictResponse( outputs={ "sum": Tensor( dtype=DT_INT8, tensor_shape=TensorShape(), int_val=[5]) }) self.assertEqual(result, expected) finally: sys.path.remove(os.path.join(path, "files", "src")) del sys.modules['func_main'] runtime.stop()
def test_half_dtype_conversion(self, dtype): tensor_shape = TensorShape(dims=[3,1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: np.array([1.10, 2.20, 3.30], dtype=np.float16).view(np.uint16), "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto
def test_tensor_to_np_array_to_tensor_float(self, dtype): tensor_shape = TensorShape(dims=[3, 1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: [1.10, 2.20, 3.30], "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto
def scalar_to_tensor_proto(x: np.ScalarType) -> Tensor: """ Creates Tensor object from a scalar with a Numpy dtype with TensorProtoShape and DataType inferred from the latter :param x: Scalar value with a Numpy dtype :return: Same value but packed into a Tensor object """ proto_dtype = np_to_proto_dtype(type(x)) if proto_dtype == DT_HALF: proto_values = [np.array(x, dtype=np.float16).view(np.uint16)] elif proto_dtype == DT_STRING: proto_values = [x.encode("utf-8")] elif proto_dtype == DT_COMPLEX64 or proto_dtype == DT_COMPLEX128: proto_values = [x.real, x.imag] else: proto_values = [x] kwargs = { DTYPE_TO_FIELDNAME[proto_dtype]: proto_values, "dtype": proto_dtype, "tensor_shape": TensorShape() } return Tensor(**kwargs)
def convert_shape(shape): return TensorShape(dims=shape) if isinstance(shape, list) else TensorShape()
class TestConversion: @pytest.mark.parametrize("dtype", int_dtypes + float_types + [DT_STRING, DT_BOOL, DT_HALF]) def test_proto_dtype_to_np_to_proto(self, dtype): np_type = proto_to_np_dtype(dtype) restored_dtype = np_to_proto_dtype(np_type) assert dtype == restored_dtype @pytest.mark.parametrize("np_shape", [[100, 1], [-1, 100], [-1, 1], [1,], [10, 10, 10, 10,]]) def test_np_shape_to_proto_and_back(self, np_shape): proto_shape = tensor_shape_proto_from_list(np_shape) assert np_shape == proto_shape.dims @pytest.mark.parametrize("dtype", int_dtypes + float_types) def test_tensor_to_np_array_to_tensor_int_and_float(self, dtype): tensor_shape = TensorShape(dims=[3, 1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: [1, 2, 3], "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto @pytest.mark.parametrize("dtype", float_types) def test_tensor_to_np_array_to_tensor_float(self, dtype): tensor_shape = TensorShape(dims=[3, 1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: [1.10, 2.20, 3.30], "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto @pytest.mark.parametrize("dtype", [DT_HALF]) def test_half_dtype_conversion(self, dtype): tensor_shape = TensorShape(dims=[3,1]) tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: np.array([1.10, 2.20, 3.30], dtype=np.float16).view(np.uint16), "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto @pytest.mark.parametrize("dtype", [DT_HALF]) def test_half_dtype_scalar_conversion(self, dtype): tensor_shape = TensorShape() tp_kwargs = {DTYPE_TO_FIELDNAME[dtype]: np.array([1.1], dtype=np.float16).view(np.uint16), "dtype": dtype, "tensor_shape": tensor_shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) print(type(np_representation)) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto @pytest.mark.parametrize("shape", [TensorShape(), ]) def test_int_scalar_tensor_to_np_scalar_back_to_tensor(self, shape): tp_kwargs = {"int64_val": [1], "dtype": DT_INT64, "tensor_shape": shape} original_tensor_proto = Tensor(**tp_kwargs) np_representation = tensor_proto_to_np(original_tensor_proto) restored_tensor_proto = np_to_tensor_proto(np_representation) assert restored_tensor_proto == original_tensor_proto @pytest.mark.parametrize("np_dtype", supported_float_np_types + supported_int_np_types) def test_np_to_tensor_to_np(self, np_dtype): x = np.array([1.0, 2.0, 3.0], dtype=np_dtype) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored) @pytest.mark.xfail(strict=True, raises=TypeError) @pytest.mark.parametrize("np_dtype", unsupported_np_types) def test_unsupported_np_to_tensor_to_np(self, np_dtype): x = np.array([1.0, 2.0, 3.0], dtype=np_dtype) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored) def test_bool_np_to_tensor_to_np(self): x = np.array([True, False, True], dtype=np.bool) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored) def test_bool_scalar_to_tensor_and_back(self): x = np.bool() tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored def test_str_np_to_tensor_to_np(self): x = np.array(["a", "b", "c"], dtype=np.str) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored) def test_str_scalar_to_tensor_and_back(self): x = np.str("a") tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored @pytest.mark.parametrize("dt", supported_complex_np_types) def test_complex_np_to_tensor_to_np(self, dt): x = np.array([-1 - 1j, -1 + 1j, +1 - 1j, +1 + 1j], dtype=dt) tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert np.all(x == x_restored) @pytest.mark.parametrize("dt", supported_complex_np_types) def test_complex_scalar_to_tensor_to_np(self, dt): x = np.array([-1 - 1j], dtype=dt)[0] tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored @pytest.mark.parametrize("np_dtype", supported_float_np_types + supported_int_np_types) def test_np_scalar_to_tensor_to_np(self, np_dtype): x = np.array([1.0], dtype=np_dtype)[0] tensor_proto = np_to_tensor_proto(x) x_restored = tensor_proto_to_np(tensor_proto) assert x == x_restored def test_isinstance_namedtuple_namedtuple(self): Point = namedtuple('Point', ['x', 'y']) pt = Point(1.0, 5.0) assert isinstance_namedtuple(pt) def test_isinstance_namedtuple_tuple(self): pt = (1, 2, 3) assert not isinstance_namedtuple(pt) def test_isinstance_namedtuple_itertuples(self): d = {'col1': [1, 2], 'col2': [3, 4]} df = pd.DataFrame(data=d) for row in df.itertuples(): assert isinstance_namedtuple(row) def test_tensor_proto_to_py(self, servable_tensor): list_value = [int(random() * 1e5)] predictor = servable_tensor.predictor(return_type=PredictorDT.DICT_PYTHON) signature_field = find_in_list_by_name(some_list=predictor.signature.inputs, name="input") tensor_proto = list_to_tensor_proto(list_value, signature_field.dtype, signature_field.shape) value_again = tensor_proto_to_py(t=tensor_proto) assert list_value == value_again assert isinstance(value_again, list)