def test_run_script(use_cluster): data = np.array([[1, 2, 3, 4, 5]]) c = Client(None, use_cluster) c.put_tensor("script-test-data", data) c.set_function("one-to-one", one_to_one) c.run_script("one-to-one", "one_to_one", ["script-test-data"], ["script-test-out"]) out = c.get_tensor("script-test-out") assert out == 5
def test_bad_function_execution(use_cluster): """Error raised inside function""" c = Client(None, use_cluster) c.set_function("bad-function", bad_function) data = np.array([1, 2, 3, 4]) c.put_tensor("bad-func-tensor", data) with pytest.raises(RedisReplyError): c.run_script("bad-function", "bad_function", ["bad-func-tensor"], ["output"])
def test_wrong_model_name(mock_data, mock_model, use_cluster): """User requests to run a model that is not there""" data = mock_data.create_data(1) model = mock_model.create_torch_cnn() c = Client(None, use_cluster) c.set_model("simple_cnn", model, "TORCH", "CPU") c.put_tensor("input", data[0]) with pytest.raises(RedisReplyError): c.run_model("wrong_cnn", ["input"], ["output"])
def test_wrong_model_name_from_file(mock_data, mock_model, use_cluster): """User requests to run a model that is not there that was loaded from file.""" try: data = mock_data.create_data(1) mock_model.create_torch_cnn(filepath="./torch_cnn.pt") c = Client(None, use_cluster) c.set_model_from_file("simple_cnn_from_file", "./torch_cnn.pt", "TORCH", "CPU") c.put_tensor("input", data[0]) with pytest.raises(RedisReplyError): c.run_model("wrong_cnn", ["input"], ["output"]) finally: os.remove("torch_cnn.pt")
def test_torch_inference(mock_model, use_cluster): # get model and set into database model = mock_model.create_torch_cnn() c = Client(None, use_cluster) c.set_model("torch_cnn", model, "TORCH") # setup input tensor data = torch.rand(1, 1, 3, 3).numpy() c.put_tensor("torch_cnn_input", data) # run model and get output c.run_model("torch_cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = c.get_tensor("torch_cnn_output") assert out_data.shape == (1, 1, 1, 1)
def test_run_script_multi(use_cluster): data = np.array([[1, 2, 3, 4]]) data_2 = np.array([[5, 6, 7, 8]]) c = Client(None, use_cluster) c.put_tensor("srpt-multi-out-data-1", data) c.put_tensor("srpt-multi-out-data-2", data_2) c.set_function("two-to-one", two_to_one) c.run_script( "two-to-one", "two_to_one", ["srpt-multi-out-data-1", "srpt-multi-out-data-2"], ["srpt-multi-out-output"], ) out = c.get_tensor("srpt-multi-out-output") expected = np.array([4, 8]) np.testing.assert_array_equal( out, expected, "Returned array from script not equal to expected result" )
# Connect a SmartRedis client db_address = "127.0.0.1:6379" client = Client(address=db_address, cluster=False) try: net = Net() example_forward_input = torch.rand(1, 1, 3, 3) # Trace a module (implicitly traces `forward`) and construct a # `ScriptModule` with a single `forward` method module = torch.jit.trace(net, example_forward_input) # Save the traced model to a file torch.jit.save(module, "./torch_cnn.pt") # Set the model in the Redis database from the file client.set_model_from_file("file_cnn", "./torch_cnn.pt", "TORCH", "CPU") # Put a tensor in the database as a test input data = torch.rand(1, 1, 3, 3).numpy() client.put_tensor("torch_cnn_input", data) # Run model and retrieve the output client.run_model("file_cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") finally: os.remove("torch_cnn.pt")
import numpy as np import argparse import time from smartredis import Client parser = argparse.ArgumentParser( description="SmartRedis ensemble producer process.") parser.add_argument("--redis-port") args = parser.parse_args() time.sleep(2) c = Client(address="127.0.0.1:" + str(args.redis_port), cluster=False) data = np.random.rand(1, 1, 3, 3) c.put_tensor("product", data)
import numpy as np from smartredis import Client # Connect a SmartRedis client to Redis database db_address = "127.0.0.1:6379" client = Client(address=db_address, cluster=False) # Send a 2D tensor to the database key = "2D_array" array = np.random.randint(-10, 10, size=(10, 10)) client.put_tensor(key, array) # Retrieve the tensor returned_array = client.get_tensor("2D_array")
keyout = os.getenv("SSKEYOUT") keyin = os.getenv("SSKEYIN") assert keyout in ["producer_0", "producer_1"] if keyout == "producer_0": c.set_data_source("producer_1" if args.exchange else "producer_0") data = torch.ones(1, 1, 3, 3).numpy() data_other = -torch.ones(1, 1, 3, 3).numpy() elif keyout == "producer_1": c.set_data_source("producer_0" if args.exchange else "producer_1") data = -torch.ones(1, 1, 3, 3).numpy() data_other = torch.ones(1, 1, 3, 3).numpy() # setup input tensor c.put_tensor("torch_cnn_input", data) input_exists = c.poll_tensor("torch_cnn_input", 100, 100) assert input_exists other_input = c.get_tensor("torch_cnn_input") if args.exchange: assert np.all(other_input == data_other) else: assert np.all(other_input == data) # run model and get output c.run_model("torch_cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"])
Two inputs to one output """ # return the highest element merged = torch.cat((data, data_2)) return merged.max(1)[0] # Connect a SmartRedis client to the Redis database db_address = "127.0.0.1:6379" client = Client(address=db_address, cluster=False) # Generate some test data to feed to the two_to_one function data = np.array([[1, 2, 3, 4]]) data_2 = np.array([[5, 6, 7, 8]]) # Put the test data into the Redis database client.put_tensor("script-data-1", data) client.put_tensor("script-data-2", data_2) # Put the function into the Redis database client.set_function("two-to-one", two_to_one) # Run the script using the test data client.run_script( "two-to-one", "two_to_one", ["script-data-1", "script-data-2"], ["script-multi-out-output"], ) # Retrieve the output of the test function out = client.get_tensor("script-multi-out-output")
def test_unsupported_type_put_tensor(use_cluster): """test an unsupported numpy type""" c = Client(None, use_cluster) data = np.array([1, 2, 3, 4]).astype(np.uint64) with pytest.raises(TypeError): c.put_tensor("key", data)
def test_bad_type_put_tensor(use_cluster): c = Client(None, use_cluster) with pytest.raises(TypeError): c.put_tensor("key", [1, 2, 3, 4])