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_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" )
import argparse import os from smartredis import Client parser = argparse.ArgumentParser(description="SmartRedis ensemble consumer process.") parser.add_argument("--redis-port") args = parser.parse_args() # get model and set into database c = Client(address="127.0.0.1:"+str(args.redis_port), cluster=False) # Incoming entity prefixes are stored as a comma-separated list # in the env variable SSKEYIN keyin = os.getenv("SSKEYIN") data_sources = keyin.split(",") data_sources.sort() for key in data_sources: c.set_data_source(key) input_exists = c.poll_tensor("product", 100, 100) db_tensor = c.get_tensor("product") print(f"Tensor for {key} is:", db_tensor)
# 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 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")
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"]) output_exists = c.poll_tensor("torch_cnn_output", 100, 100) assert output_exists out_data = c.get_tensor("torch_cnn_output") assert out_data.shape == (1, 1, 1, 1)
""" # 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_bad_get_tensor(use_cluster): c = Client(None, use_cluster) with pytest.raises(RedisReplyError): c.get_tensor("not-a-key")