### Create and Start an Ensemble Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Demonstrates how to create an ensemble of models and start them. This example shows running 4 identical replicas of a model in parallel, with output summarizing the launch. ```python ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Start Orchestrator Experiment and Database in Python Source: https://www.craylabs.org/docs/versions/0.7.0/_sources/tutorials/getting_started/getting_started This Python code initializes a SmartSim experiment locally, creates and configures a single-node Orchestrator database, generates its configuration, and then starts the database. This setup is crucial for subsequent data operations. ```python # start a new Experiment for this section exp = Experiment("tutorial-smartredis", launcher="local") # create and start an instance of the Orchestrator database db = exp.create_database(db_nodes=1, port=REDIS_PORT, interface="lo") # create an output directory for the database log files exp.generate(db) # start the database exp.start(db) ``` -------------------------------- ### Start and Manage Orchestrator with SmartSim Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Demonstrates how to initialize an Experiment, generate and start an Orchestrator instance. The Orchestrator runs a single database instance when the launcher is set to 'local'. ```python from smartredis import Client from smartsim.database import Orchestrator import numpy as np REDIS_PORT=6899 exp = Experiment("tutorial-smartredis", launcher="local") # create and start a database orc = Orchestrator(port=REDIS_PORT) exp.generate(orc) exp.start(orc, block=False) ``` -------------------------------- ### Create and Run a Simple Model with SmartSim Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started Creates a SmartSim Model for executing a command. This involves defining run settings with the executable and its arguments, then creating the model instance within an experiment. The model is then started and can be optionally monitored for completion. ```python # settings to execute the command "echo hello!" settings = exp.create_run_settings(exe="echo", exe_args="hello!", run_command=None) # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) # Start the experiment and block until completion exp.start(M1, summary=True, block=True) ``` -------------------------------- ### Create and Run SmartSim Model Source: https://www.craylabs.org/docs/versions/0.6.0/_sources/tutorials/getting_started/getting_started Creates a SmartSim Model instance within an Experiment. This involves defining run settings using `exp.create_run_settings`, specifying the executable and its arguments. The model can then be started using `exp.start`, with options to display a summary and to block execution until the model completes. ```python # settings to execute the command "echo hello!" settings = exp.create_run_settings(exe="echo", exe_args="hello!", run_command=None) # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) # Start the experiment, display summary, and wait for completion exp.start(M1, summary=True, block=True) ``` -------------------------------- ### Create and Start Ensemble with Run Settings Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started This Python code demonstrates how to create a SmartSim Ensemble with specified run settings. It configures an ensemble to run a command (e.g., 'sleep 3') for multiple replicas and then starts the ensemble, printing a summary of the launch. ```python # define how we want each ensemble member to execute # in this case we create settings to execute "sleep 3" ers_settings = exp.create_run_settings(exe="sleep", exe_args="3") ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Run Experiment with SmartRedis Source: https://www.craylabs.org/docs/versions/0.6.1/_sources/tutorials/getting_started/getting_started This snippet executes an experiment previously configured with SmartRedis. It starts the necessary services, launches the defined models and ensembles, and provides a summary of the experiment's status. ```python exp.run() ``` -------------------------------- ### Create and start a replicated ensemble Source: https://www.craylabs.org/docs/versions/0.6.0/_sources/tutorials/getting_started/getting_started This code defines run settings for ensemble members and then creates an ensemble with multiple replicas. It starts the ensemble and prints a summary of the launch, showing the status of each replica. This is useful for running identical models in parallel. ```python # define how we want each ensemble member to execute # in this case we create settings to execute "sleep 3" ens_settings = exp.create_run_settings(exe="sleep", exe_args="3") ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Launch Experiment with SmartSim Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started This code snippet initiates the launch of an experiment configured with SmartSim, including ensembles and models. It also prints a summary of the launched experiment, detailing components like ensembles, models, and database status. Requires `exp` object. ```python exp.generate(fmt="docker") # Example generation step, actual launch is below exp.start() ``` -------------------------------- ### Initialize SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started This code initializes a SmartSim experiment named 'tutorial-smartredis-ensemble' using the 'local' launcher. This setup is often a precursor to configuring and running distributed applications or models that leverage SmartRedis for data management. ```python exp = Experiment("tutorial-smartredis-ensemble", launcher="local") ``` -------------------------------- ### Start SmartSim Experiment with Blocking and Summary Source: https://www.craylabs.org/docs/versions/0.7.0/_sources/tutorials/getting_started/getting_started Starts the SmartSim experiment, displaying a summary of the launch configuration before execution. The `block=True` argument ensures the function waits until all models in the experiment have completed, providing job monitoring capabilities. ```python # Start the experiment and wait for models to complete exp.start(summary=True, block=True) ``` -------------------------------- ### SmartSim Experiment Initialization Source: https://www.craylabs.org/docs/versions/0.4.1/tutorials/getting_started/getting_started This Python snippet initializes a new SmartSim experiment named 'tutorial-smartredis' using the 'local' launcher. This setup is suitable for demonstrating the SmartRedis Python client within a local workload. ```python # start a new Experiment for this section exp = Experiment("tutorial-smartredis", launcher="local") ``` -------------------------------- ### Start and Monitor SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.6.1/_sources/tutorials/getting_started/getting_started Starts the configured experiment and waits for all models to complete. Setting 'block=True' ensures the script pauses until execution finishes, providing monitoring capabilities. 'summary=True' displays a summary before launch. ```python # Start the experiment and wait for models to finish exp.start(block=True, summary=True) ``` -------------------------------- ### Initialize SmartSim Experiment Locally Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Initializes a SmartSim Experiment named 'getting-started' configured to run locally. This setup is suitable for single-node execution and testing. ```python # Init Experiment and specify to launch locally exp = Experiment(name="getting-started", launcher="local") ``` -------------------------------- ### Run Experiment with SmartRedis Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started A Python snippet to attach files, generate, and run an experiment using SmartRedis. This initiates the configured models and ensembles. ```python exp.start() ``` -------------------------------- ### Launch SmartRedis Experiment Source: https://www.craylabs.org/docs/versions/0.5.0/_sources/tutorials/getting_started/getting_started This snippet initiates the launch of a SmartRedis experiment, including ensembles and models. It generates necessary configuration files and starts the experiment execution on the configured launcher. The output provides a summary of the launched components. ```python exp.generate(db) exp.start() ``` -------------------------------- ### Run SmartSim Model with MPI using mpirun Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started This Python code configures and launches a SmartSim model using the Message Passing Interface (MPI) via `mpirun`. It specifies `mpirun` as the `run_command` in `RunSettings` and sets the number of tasks. This example requires OpenMPI to be installed on the system. ```python # settings to execute the command "mpirun -np 2 echo hello world!" openmpi_settings = exp.create_run_settings(exe="echo", exe_args="hello world!", run_command="mpirun") openmpi_settings.set_tasks(2) # create and start the MPI model ompi_model = exp.create_model("tutorial-model-mpirun", openmpi_settings) exp.start(ompi_model, summary=True) ``` -------------------------------- ### Create and Run a Simple Model Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started Creates a SmartSim Model to execute a command. It uses Experiment.create_run_settings to define execution parameters like the executable ('echo') and its arguments ('hello!'). The model is then created using Experiment.create_model and started. ```python # settings to execute the command "echo hello!" settings = exp.create_run_settings(exe="echo", exe_args="hello!", run_command=None) # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) # Start the experiment and wait for models to complete exp.start(M1, summary=True, block=True) ``` -------------------------------- ### Starting a Replicated Ensemble Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started This Python snippet demonstrates how to create and start a SmartSim ensemble with multiple replicas. It defines an ensemble named 'ensemble-replica' with 4 members and starts them using the experiment's start method. ```python ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Launch SmartRedis Experiment Source: https://www.craylabs.org/docs/versions/0.5.1/_sources/tutorials/getting_started/getting_started This snippet shows the final step of launching a SmartRedis experiment after defining the ensemble, models, and their configurations. It attaches necessary files, generates them, and initiates the experiment run, followed by a summary of the launched components. ```python exp.start(db) # ... (rest of the experiment setup code) ... print(exp.get_launch_summary()) ``` -------------------------------- ### Create and Start Orchestrator Database Source: https://www.craylabs.org/docs/versions/0.5.1/tutorials/getting_started/getting_started This snippet initializes and starts an Orchestrator database. It requires the `exp` module from SmartSim and specifies the number of database nodes, the port, and the network interface. The `exp.generate()` call prepares the necessary output directories. ```python import os from smartsim import Experiment REDIS_PORT = 6379 # Create an experiment object exp = Experiment("orchestrator-example", "./") # create and start an instance of the Orchestrator database db = exp.create_database(db_nodes=1, port=REDIS_PORT, interface="lo") # create an output directory for the database log files exp.generate(db) # start the database exp.start(db) ``` -------------------------------- ### Start Orchestrator with SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.3.2/tutorials/01_getting_started/01_getting_started Initializes a SmartSim `Experiment` with a local launcher and then creates and starts an `Orchestrator` instance. The `generate` method prepares the Orchestrator configuration, and `start` launches it without blocking execution. ```python exp = Experiment("tutorial-smartredis", launcher="local") # create and start a database orc = Orchestrator(port=REDIS_PORT) exp.generate(orc) exp.start(orc, block=False) ``` -------------------------------- ### Initialize SmartSim Client and Orchestrator Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started Initializes the SmartSim client and creates/starts an Orchestrator database instance. It sets up the necessary client connection and database configuration for data management within an experiment. ```python from smartredis import Client import numpy as np REDIS_PORT=6899 # start a new Experiment for this section exp = Experiment("tutorial-smartredis", launcher="local") # create and start an instance of the Orchestrator database db = exp.create_database(db_nodes=1, port=REDIS_PORT, interface="lo") # create an output directory for the database log files exp.generate(db) # start the database exp.start(db) ``` -------------------------------- ### Deploy and Run PyTorch Model with SmartRedis Client Source: https://www.craylabs.org/docs/versions/0.5.0/_sources/tutorials/getting_started/getting_started Demonstrates the process of creating a PyTorch model, tracing it for inference, saving it to a file, deploying it to the SmartRedis database, and then running inference using tensors stored in the database. This showcases ML model deployment capabilities. ```python import torch import torch.nn as nn # taken from https://pytorch.org/docs/master/generated/torch.jit.trace.html class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) module = torch.jit.trace(net, example_forward_input) # Save the traced model to a file torch.jit.save(module, "./torch_cnn.pt") ``` ```python # Set the model in the Redis database from the file client.set_model_from_file("tutorial-cnn", "./torch_cnn.pt", "TORCH", "CPU") ``` ```python # 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("tutorial-cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") ``` -------------------------------- ### Deploy and Run PyTorch Model with SmartRedis Source: https://www.craylabs.org/docs/versions/0.6.1/_sources/tutorials/getting_started/getting_started Shows the process of creating a PyTorch model, tracing it with `torch.jit.trace`, saving it to a file, and then deploying it to the SmartRedis database using `set_model_from_file`. It further illustrates how to prepare input data, run the model using `run_model`, and retrieve the output tensor. ```python import torch import torch.nn as nn # taken from https://pytorch.org/docs/master/generated/torch.jit.trace.html class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) module = torch.jit.trace(net, example_forward_input) # Save the traced model to a file torch.jit.save(module, "./torch_cnn.pt") ``` ```python # Set the model in the Redis database from the file client.set_model_from_file("tutorial-cnn", "./torch_cnn.pt", "TORCH", "CPU") ``` ```python # 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("tutorial-cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") ``` -------------------------------- ### RunSettings for Ensemble Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Defines the settings for running an ensemble, specifying the executable and its arguments. This is a prerequisite for creating and launching ensembles in SmartSim. ```python ens_settings = RunSettings(exe="sleep", exe_args="3") ``` -------------------------------- ### Start and Monitor SmartSim Experiment Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Starts the SmartSim experiment, displaying a launch summary for 10 seconds before execution. The 'block=True' argument ensures the script waits for all models to complete, providing real-time monitoring of their status. ```python # start the experiment and wait for completion exp.start(M1, summary=True, block=True) ``` -------------------------------- ### Create Ensemble with Replicas and Run Settings Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started This snippet illustrates creating an ensemble of models using SmartSim. It specifies the name, number of replicas, and run settings for the ensemble. It requires the `exp` object and a `REDIS_PORT` variable. ```python rs_prod = exp.create_run_settings("python", f"producer.py --redis-port {REDIS_PORT}") ensemble = exp.create_ensemble(name="producer", replicas=2, run_settings=rs_prod) ``` -------------------------------- ### Create and Run a Simple Model Source: https://www.craylabs.org/docs/versions/0.5.1/_sources/tutorials/getting_started/getting_started Creates a Model instance for execution and then starts it. This involves defining run settings, such as the executable command and its arguments. The 'run_command=None' indicates no specific MPI or job execution command is used. The 'block=True' ensures the script waits for the model to complete. ```python # settings to execute the command "echo hello!" settings = exp.create_run_settings(exe="echo", exe_args="hello!", run_command=None) # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) # Start the experiment and wait for the model to finish exp.start(M1, summary=True, block=True) ``` -------------------------------- ### Run Model with MPI using mpirun Source: https://www.craylabs.org/docs/versions/0.3.2/tutorials/01_getting_started/01_getting_started This example shows how to execute a model using a specific run command like 'mpirun' for parallel execution. It configures the number of processes using 'run_args' within the RunSettings. ```python openmpi_settings = RunSettings("echo", "hello world!", run_command="mpirun", run_args={"-"np": 2}) # note that for base ``RunSettings`` run_args passed literally ompi_model = exp.create_model("tutorial-model-mpirun", openmpi_settings) exp.start(ompi_model, summary=True) ``` -------------------------------- ### Create Ensemble and Model with SmartRedis Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started Illustrates the creation of an ensemble of models and a consumer model in Python using SmartRedis. This is used for managing distributed workloads and data flow. ```python rs_prod = exp.create_run_settings("python", f"producer.py --redis-port {REDIS_PORT}") ensemble = exp.create_ensemble(name="producer", replicas=2, run_settings=rs_prod) rs_consumer = exp.create_run_settings("python", f"consumer.py --redis-port {REDIS_PORT}") consumer = exp.create_model("consumer", run_settings=rs_consumer) ``` -------------------------------- ### Python: Start SmartSim Experiment Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Initiates the execution of the SmartSim experiment, including the ensemble and consumer applications, with an option to generate a summary of the job status. This command starts the distributed components defined in the experiment. ```python # start the models exp.start(ensemble, consumer, summary=True) ``` -------------------------------- ### Start SmartRedis Orchestrator Source: https://www.craylabs.org/docs/tutorials/getting_started/getting_started This command starts the SmartRedis orchestrator, initializing the database instance. It's used to bring up the database for an experiment. ```python exp.start(db) ``` -------------------------------- ### Create and Start Orchestrator Database Source: https://www.craylabs.org/docs/versions/0.4.2/tutorials/getting_started/getting_started Initializes and starts a database instance using the Orchestrator. This involves specifying the number of database nodes, the port, and the network interface. It also generates necessary output directories for log files. ```python db = exp.create_database(db_nodes=1, port=REDIS_PORT, interface="lo") exp.generate(db) exp.start(db) ``` -------------------------------- ### Attach Files, Generate, and Start SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.5.0/tutorials/getting_started/getting_started This code snippet prepares the SmartSim experiment by attaching necessary files ('producer.py', 'consumer.py') to the ensemble and model. It then generates the experiment configuration, overwriting existing files if necessary, and finally starts the ensemble and consumer models, including a summary of the launch. ```python ensemble.attach_generator_files(to_copy=['producer.py']) consumer.attach_generator_files(to_copy=['consumer.py']) exp.generate(ensemble, overwrite=True) exp.generate(consumer, overwrite=True) # start the models exp.start(ensemble, consumer, summary=True) ``` -------------------------------- ### Create and Configure a Simple Model Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Defines run settings for a model that executes the 'echo hello!' command. It then creates a SmartSim Model instance named 'tutorial-model' using these settings. The 'run_command=None' argument ensures no specific job launcher is used. ```python # settings to execute the command "echo hello!" settings = exp.create_run_settings(exe="echo", exe_args="hello!", run_command=None) # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) ``` -------------------------------- ### Initialize SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started Initializes a SmartSim Experiment, which is used to define and manage workflows. It requires a unique name and a launcher specification. The launcher determines how jobs are submitted (e.g., 'local' for single-node execution, 'slurm', 'pbs' for HPC schedulers). ```python from smartsim import Experiment # Init Experiment and specify to launch locally exp = Experiment(name="getting-started", launcher="local") ``` -------------------------------- ### Creating Run Settings for Ensemble Members Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started Python code to define execution settings for ensemble members in SmartSim. This example configures members to execute the 'sleep' command with an argument of '3'. ```python # define how we want each ensemble member to execute # in this case we create settings to execute "sleep 3" ens_settings = exp.create_run_settings(exe="sleep", exe_args="3") ``` -------------------------------- ### Display Launch Summary from SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.6.0/tutorials/getting_started/getting_started This code displays a detailed summary of the launched SmartSim experiment, including the experiment name, path, launcher type, and counts of ensembles, models, and the database status. It also provides specific details about the 'producer' ensemble (number of members) and the 'consumer' model (executable and arguments). ```python print("=== Launch Summary ===") print(f"Experiment: {exp.name}") print(f"Experiment Path: {exp.path}") print(f"Launcher: {exp.launcher}") print(f"Ensembles: {len(exp.ensembles)}") print(f"Models: {len(exp.models)}") print(f"Database Status: {db.get_status()}") print("\n=== Ensembles ===") for ensemble in exp.ensembles.values(): print(f"{ensemble.name}") print(f"Members: {len(ensemble.replicas)}") print(f"Batch Launch: {ensemble.batch_launch}") print("\n=== Models ===") for model in exp.models.values(): print(f"{model.name}") print(f"Executable: {model.executable}") print(f"Executable Arguments: {model.executable_args}") ``` -------------------------------- ### Deploy and Run PyTorch Model with SmartRedis Client (Python) Source: https://www.craylabs.org/docs/versions/0.7.0/_sources/tutorials/getting_started/getting_started Shows the process of creating, tracing, saving, and deploying a PyTorch model to the SmartRedis database. It then demonstrates how to run inference using the deployed model with a tensor input and retrieve the output. Requires PyTorch and NumPy. ```python import torch import torch.nn as nn # Define a simple CNN model class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) module = torch.jit.trace(net, example_forward_input) # Save the traced model to a file torch.jit.save(module, "./torch_cnn.pt") # Assuming 'client' is an initialized SmartRedis client instance # Set the model in the Redis database from the file client.set_model_from_file("tutorial-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("tutorial-cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") ``` -------------------------------- ### Start Ensemble of Models Source: https://www.craylabs.org/docs/versions/0.6.2/_sources/tutorials/getting_started/getting_started Creates and starts an ensemble of models using SmartSim. This snippet defines an ensemble named 'ensemble-replica' with 4 replicas, each running with the previously defined run settings. It then starts the ensemble and displays a summary of the experiment execution. ```python ensemble = exp.create_ensemble("ensemble-replica",\n replicas=4,\n run_settings=ens_settings)\n\nexp.start(ensemble, summary=True) ``` -------------------------------- ### Define, Store, and Run a Python Function Source: https://www.craylabs.org/docs/versions/0.5.1/tutorials/getting_started/getting_started This example demonstrates how to define a Python function, store it in the database using `set_function`, and then execute it remotely via `run_script` using a SmartRedis client. It shows passing data as tensors and retrieving the script's output. ```python from smartredis import Client import numpy as np def max_of_tensor(array): """Sample torchscript script that returns the highest element in an array. """ # return the highest element return array.max(1)[0] sample_array_1 = np.array([np.arange(9.)]) client.set_function("max-of-tensor", max_of_tensor) client.put_tensor("script-data-1", sample_array_1) client.run_script( "max-of-tensor", # key of our script "max_of_tensor", # function to be called ["script-data-1"], ["script-output"], ) out = client.get_tensor("script-output") print(out) ``` -------------------------------- ### Install SmartSim via Pip Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/installation Installs the SmartSim Python package using pip. This command installs SmartSim with PyTorch and TensorFlow backends by default. It is recommended to start with a fresh Python environment. ```bash pip install smartsim smart --device cpu ``` -------------------------------- ### Create Ensemble and Models with SmartSim Source: https://www.craylabs.org/docs/versions/0.6.2/_sources/tutorials/getting_started/getting_started This code demonstrates how to set up an ensemble of models using SmartSim. It includes creating run settings for a Python script and defining an ensemble with multiple replicas, as well as creating a separate consumer model. ```python rs_prod = exp.create_run_settings("python", f"producer.py --redis-port {REDIS_PORT}") ensemble = exp.create_ensemble(name="producer", replicas=2, run_settings=rs_prod) rs_consumer = exp.create_run_settings("python", f"consumer.py --redis-port {REDIS_PORT}") consumer = exp.create_model("consumer", run_settings=rs_consumer) ``` -------------------------------- ### Create and Run a Simple Model Instance in SmartSim Source: https://www.craylabs.org/docs/versions/0.5.0/tutorials/getting_started/getting_started This snippet demonstrates how to create a model instance using SmartSim's Experiment API and then start it. It includes options to display a summary before launch and to block execution until the model completes, acting as a job monitor. The output files (.out and .err) are then read and printed. ```python # create the simple model instance so we can run it. M1 = exp.create_model(name="tutorial-model", run_settings=settings) exp.start(M1, block=True, summary=True) outputfile = './tutorial-model.out' errorfile = './tutorial-model.err' print("Content of tutorial-model.out:") with open(outputfile, 'r') as fin: print(fin.read()) print("Content of tutorial-model.err:") with open(errorfile, 'r') as fin: print(fin.read()) ``` -------------------------------- ### Create and Start Orchestrator Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Initializes an Orchestrator for managing the experiment and then generates and starts the experiment. The Orchestrator is responsible for controlling the workflow and resources. It takes a port number as an argument. The experiment generation prepares the necessary files and configurations, while starting it initiates the execution. ```Python orc = Orchestrator(port=REDIS_PORT) exp.generate(orc) exp.start(orc, block=False) ``` -------------------------------- ### Define and Execute Python Function Remotely Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started This example defines a Python function `max_of_tensor` that finds the maximum value in a NumPy array. It then stores this function in the Redis database with the key 'max-of-tensor' using `client.set_function`. Subsequently, it prepares input data, runs the function remotely using `client.run_script`, and retrieves the output. ```python def max_of_tensor(array): """Sample torchscript script that returns the highest element in an array. """ # return the highest element return array.max(1)[0] sample_array_1 = np.array([np.arange(9.)]) print(sample_array_1) print("Max:") print(max_of_tensor(sample_array_1)) client.set_function("max-of-tensor", max_of_tensor) client.put_tensor("script-data-1", sample_array_1) client.run_script( "max-of-tensor", # key of our script "max_of_tensor", # function to be called ["script-data-1"], ["script-output"], ) out = client.get_tensor("script-output") print(out) ``` -------------------------------- ### Create SmartRedis Ensemble and Model Source: https://www.craylabs.org/docs/versions/0.5.1/_sources/tutorials/getting_started/getting_started This code defines how to create an ensemble of models and a consumer model within a SmartRedis experiment. It specifies the number of replicas for the ensemble, run settings including the script to execute and Redis port, and registers incoming entities to manage dependencies between models. ```python rs_prod = exp.create_run_settings("python", f"producer.py --redis-port {REDIS_PORT}") ensemble = exp.create_ensemble(name="producer", replicas=2, run_settings=rs_prod) rs_consumer = exp.create_run_settings("python", f"consumer.py --redis-port {REDIS_PORT}") consumer = exp.create_model("consumer", run_settings=rs_consumer) consumer.register_incoming_entity(ensemble.models[0]) consumer.register_incoming_entity(ensemble.models[1]) ``` -------------------------------- ### HTTP Request Example using Axios Source: https://www.craylabs.org/docs/versions/0.6.1/_sources/tutorials/ml_training/surrogate/train_surrogate This snippet shows how to make an HTTP GET request using the Axios library. Axios is a popular promise-based HTTP client for the browser and Node.js. Ensure Axios is installed (`npm install axios`). This example fetches data from a public API. ```javascript const axios = require('axios'); async function fetchData(url) { try { const response = await axios.get(url); console.log('Data fetched successfully:'); // console.log(response.data); return response.data; } catch (error) { console.error('Error fetching data:', error.message); return null; } } const apiUrl = 'https://jsonplaceholder.typicode.com/posts/1'; // Example API endpoint fetchData(apiUrl).then(data => { if (data) { console.log('Post Title:', data.title); } }); // Example of fetching from a non-existent URL const nonExistentUrl = 'https://jsonplaceholder.typicode.com/nonexistent'; fetchData(nonExistentUrl); ``` -------------------------------- ### Create and Start Orchestrator Database with SmartSim Source: https://www.craylabs.org/docs/versions/0.4.1/tutorials/getting_started/getting_started This snippet demonstrates the creation and initiation of an Orchestrator database instance using SmartSim. It specifies the number of database nodes, the port for communication, and the network interface. After creation, it generates necessary output directories and then starts the database. ```python db = exp.create_database(db_nodes=1, port=REDIS_PORT, interface="lo") # create an output directory for the database log files exp.generate(db) # start the database exp.start(db) ``` -------------------------------- ### Start Orchestrator with SmartRedis DB Source: https://www.craylabs.org/docs/versions/0.5.0/tutorials/getting_started/getting_started This code snippet starts the SmartSim Orchestrator, which includes a SmartRedis database. This database will be used for inter-model communication. Ensure the 'db' reference is already defined in your environment. ```python exp.start(db) ``` -------------------------------- ### Put and Get Tensors using SmartRedis Client (Python) Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started Stores a NumPy array (Tensor) in the SmartRedis database using a unique key and retrieves it. Requires the 'numpy' and 'redis' libraries. ```python import numpy as np # Assume 'client' is an initialized SmartRedis client instance # client = sr.Client(host="localhost", port=6379, decode_responses=True) send_tensor = np.ones((4,3,3)) client.put_tensor("tutorial_tensor_1", send_tensor) receive_tensor = client.get_tensor("tutorial_tensor_1") print('Receive tensor:\n\n', receive_tensor) ``` -------------------------------- ### Parameterized Python Script for Model Output Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started This Python script is designed to be executed as part of a parameterized model run. It includes a time delay and prints a message that incorporates placeholders for a tutorial name and parameter. These placeholders (`;tutorial_name;` and `;tutorial_parameter;`) are intended to be replaced by a simulation framework. ```python # contents of output_my_parameter.py import time time.sleep(2) print("Hello, my name is ;tutorial_name; " + "and my parameter is ;tutorial_parameter;") ``` -------------------------------- ### Create and Run Model Ensemble with SmartSim Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started This Python code demonstrates how to set up an ensemble of producer models and a consumer model using SmartSim. It configures run settings, creates the ensemble and consumer, registers incoming entities to manage key prefixes, and prepares for experiment execution. It requires 'producer.py' and 'consumer.py' scripts. ```python from smartsim import Experiment # Assume exp, REDIS_PORT are defined from previous steps # exp = Experiment(name="tutorial-smartredis") # REDIS_PORT = 6379 # Example port # Start the orchestrator if not already running # exp.start(db) # Create run settings for the producer ensemble rs_prod = exp.create_run_settings("python", f"producer.py --redis-port {REDIS_PORT}") ensemble = exp.create_ensemble(name="producer", replicas=2, run_settings=rs_prod) # Create run settings for the consumer model rs_consumer = exp.create_run_settings("python", f"consumer.py --redis-port {REDIS_PORT}") consumer = exp.create_model("consumer", run_settings=rs_consumer) # Register incoming entities to manage key prefixes consumer.register_incoming_entity(ensemble.models[0]) consumer.register_incoming_entity(ensemble.models[1]) # Attach files, generate, and run the experiment (this would typically be done after this cell) # exp.attach_files(ensemble, "./producer.py") # exp.generate(ensemble) # exp.generate(consumer) # exp.start(ensemble) # exp.start(consumer) ``` -------------------------------- ### Run Model with MPI using SmartSim Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started This Python snippet shows how to configure and run a SmartSim model using 'mpirun' for parallel execution. It defines 'RunSettings' with the executable, its arguments, and specifies 'mpirun' as the 'run_command' with '-np' argument for process count. This is suitable for launching parallel applications within the SmartSim framework, provided OpenMPI is installed. ```python openmpi_settings = RunSettings("echo", "hello world!", run_command="mpirun", run_args={"-np": 2}) # note that for base ``RunSettings`` run_args passed literally ompi_model = exp.create_model("tutorial-model-mpirun", openmpi_settings) exp.start(ompi_model, summary=True) ``` -------------------------------- ### Create Run Settings for a Specific Executable Source: https://www.craylabs.org/docs/versions/0.4.1/tutorials/getting_started/getting_started This snippet demonstrates how to create run settings for an experiment, specifying the executable and its arguments. This is useful for defining the basic execution parameters of a job. ```python ens_settings = exp.create_run_settings(exe="sleep", exe_args="3") ``` -------------------------------- ### Start SmartSim Models Source: https://www.craylabs.org/docs/tutorials/getting_started/getting_started Initiates the experiment's ensemble and consumer models. It uses the `exp.start` function, taking ensemble, consumer, and a summary flag as arguments. This is a core function for launching simulations. ```python exp.start(ensemble, consumer, summary=True) ``` -------------------------------- ### Deploy and Run PyTorch Model with SmartRedis (Python) Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Shows how to create, save, deploy, and run a PyTorch model using the SmartRedis client. It involves tracing the model, saving it to a file, setting it in the database, and then running inference with input tensors. ```python import torch import torch.nn as nn # taken from https://pytorch.org/docs/master/generated/torch.jit.trace.html class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) 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("tutorial-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("tutorial-cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") ``` -------------------------------- ### Connect SmartRedis Client to Orchestrator Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started Connects a SmartRedis client to the running Orchestrator instance. This client will be used to interact with the database for data operations. ```python # connect a SmartRedis client at the address supplied by the launched # Orchestrator instance. # Cluster=False as the Orchestrator was deployed on a single compute host (local) client = Client(address=db.get_address()[0], cluster=False) ``` -------------------------------- ### Put and Get Tensors with SmartRedis Client (Python) Source: https://www.craylabs.org/docs/versions/0.7.0/_sources/tutorials/getting_started/getting_started Demonstrates how to store NumPy arrays (Tensors) in the SmartRedis database using a unique key and retrieve them. This is fundamental for data exchange and persistence within the SmartRedis ecosystem. Requires the NumPy library. ```python import numpy as np # Assuming 'client' is an initialized SmartRedis client instance send_tensor = np.ones((4,3,3)) client.put_tensor("tutorial_tensor_1", send_tensor) receive_tensor = client.get_tensor("tutorial_tensor_1") print('Receive tensor:\n\n', receive_tensor) ``` -------------------------------- ### Reading Output File Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started This Python code snippet reads and prints the content of a specified output file, typically used to verify the results of parallel execution. ```python outputfile = './tutorial-model-mpirun.out' print("Content of tutorial-model-mpirun.out:") with open(outputfile, 'r') as fin: print(fin.read()) ``` -------------------------------- ### Attach Generator Files, Generate, and Start Experiment Source: https://www.craylabs.org/docs/versions/0.3.2/tutorials/01_getting_started/01_getting_started This final snippet prepares the SmartSim experiment by attaching necessary generator files ('producer.py', 'consumer.py'). It then generates the experiment configuration for both the ensemble and the consumer, and finally starts the entire experiment, including the models and ensemble, providing a summary upon completion. ```python ensemble.attach_generator_files(to_copy=['producer.py']) consumer.attach_generator_files(to_copy=['consumer.py']) exp.generate(ensemble, overwrite=True) exp.generate(consumer, overwrite=True) # start the models exp.start(ensemble, consumer, summary=True) ``` -------------------------------- ### Python Script with Placeholders Source: https://www.craylabs.org/docs/versions/0.5.0/tutorials/getting_started/getting_started This Python script is designed to be used as a template for parameterized ensembles. It includes placeholders like ';tutorial_name;' and ';tutorial_parameter;' that will be substituted during experiment generation. ```python # contents of output_my_parameter.py import time time.sleep(2) print("Hello, my name is ;tutorial_name; " + "and my parameter is ;tutorial_parameter;") ``` -------------------------------- ### Read Consumer Output Source: https://www.craylabs.org/docs/versions/0.4.1/_sources/tutorials/getting_started/getting_started This code snippet reads and prints the content of a consumer output file. It specifies the path to the output file and then opens and reads its entire content. ```python outputfile = './tutorial-smartredis/consumer/consumer.out' with open(outputfile, 'r') as fin: print(fin.read()) ``` -------------------------------- ### Import SmartSim Experiment Source: https://www.craylabs.org/docs/versions/0.6.0/tutorials/getting_started/getting_started Imports the necessary Experiment class from the SmartSim library to begin defining computational workflows. ```python from smartsim import Experiment ``` -------------------------------- ### Python API Interaction Example Source: https://www.craylabs.org/docs/versions/0.6.2/_sources/tutorials/online_analysis/lattice/online_analysis This Python snippet illustrates how to interact with a hypothetical API using the 'requests' library. It shows how to send a GET request and process the JSON response. Error handling for network issues and invalid responses is included. Ensure you have 'requests' installed (`pip install requests`). ```python import requests import json API_URL = "https://api.example.com/data" def fetch_api_data(endpoint): try: response = requests.get(f"{API_URL}/{endpoint}", timeout=10) response.raise_for_status() # Raise an exception for bad status codes return response.json() except requests.exceptions.RequestException as e: print(f"Error fetching data: {e}") return None except json.JSONDecodeError: print("Error decoding JSON response.") return None # Example usage api_data = fetch_api_data("users") if api_data: print("API Response:", json.dumps(api_data, indent=2)) ``` -------------------------------- ### Store and Run PyTorch Model with SmartRedis (Python) Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started Traces a PyTorch model, saves it, then uploads it to the SmartRedis database. The model can then be executed remotely using client.run_model. Requires 'torch' and 'numpy'. ```python import torch import torch.nn as nn # Assume 'client' is an initialized SmartRedis client instance # client = sr.Client(host="localhost", port=6379, decode_responses=True) class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) 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("tutorial-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("tutorial-cnn", inputs=["torch_cnn_input"], outputs=["torch_cnn_output"]) out_data = client.get_tensor("torch_cnn_output") ``` -------------------------------- ### Create and Start a Model Ensemble Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started This Python code defines run settings for ensemble members and then creates an ensemble of models using `Experiment.create_ensemble`. It specifies the number of replicas and the run settings. Finally, it starts the ensemble and prints a summary of the launch. This is useful for running multiple identical model instances in parallel. ```python # define how we want each ensemble member to execute # in this case we create settings to execute "sleep 3" ens_settings = exp.create_run_settings(exe="sleep", exe_args="3") ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Create and Execute a Basic Ensemble in Python Source: https://www.craylabs.org/docs/tutorials/getting_started/getting_started This snippet demonstrates how to create a basic ensemble of models in SmartSim. It first defines run settings for each ensemble member and then creates an ensemble with a specified number of replicas. The ensemble is then started, and its launch summary is printed. ```python # define how we want each ensemble member to execute # in this case we create settings to execute "sleep 3" ens_settings = exp.create_run_settings(exe="sleep", exe_args="3") ensemble = exp.create_ensemble("ensemble-replica", replicas=4, run_settings=ens_settings) exp.start(ensemble, summary=True) ``` -------------------------------- ### Run Producer Model Script (Python) Source: https://www.craylabs.org/docs/versions/0.3.2/tutorials/01_getting_started/01_getting_started Executes the producer Python script with specified Redis port. This script is responsible for generating data for the ensemble model. It requires a Python environment with SmartSim installed. ```python executable = '/Users/spartee/.virtualenvs/smartsim/bin/python' executable_arguments = ['producer.py', '--redis-port', '6899'] ``` -------------------------------- ### SmartSim Ensemble Generation and Execution Source: https://www.craylabs.org/docs/tutorials/getting_started/getting_started Python code using SmartSim to define run settings, create an ensemble with parameterization, attach generator files, and launch the simulation. It demonstrates using a custom tag '@' for parameter substitution. ```python rs = exp.create_run_settings(exe="python", exe_args="output_my_parameter_new_tag.py") params = { "tutorial_name": ["Ellie", "John"], "tutorial_parameter": [2, 11] } ensemble = exp.create_ensemble("ensemble_new_tag", params=params, run_settings=rs, perm_strategy="all_perm") config_file = "./output_my_parameter_new_tag.py" ensemble.attach_generator_files(to_configure=config_file) exp.generate(ensemble, overwrite=True, tag='@') exp.start(ensemble) ``` -------------------------------- ### Define and Set Python Function with SmartRedis (Python) Source: https://www.craylabs.org/docs/versions/0.4.2/_sources/tutorials/getting_started/getting_started Defines a Python function that operates on NumPy arrays and stores it in the SmartRedis database using a unique key. This function can then be called remotely. Requires 'numpy'. ```python import numpy as np # Assume 'client' is an initialized SmartRedis client instance # client = sr.Client(host="localhost", port=6379, decode_responses=True) def max_of_tensor(array): """Sample torchscript script that returns the highest element in an array. """ # return the highest element return array.max(1)[0] sample_array_1 = np.array([np.arange(9.)]) print(sample_array_1) print("Max:") print(max_of_tensor(sample_array_1)) # Set the function in the Redis database client.set_function("max-of-tensor", max_of_tensor) ``` -------------------------------- ### Store and Retrieve NumPy Tensors with SmartRedis Client Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Shows how to establish a connection to the Orchestrator using the SmartRedis Client and then store and retrieve NumPy tensors. Each tensor must be assigned a unique key for identification. ```python client = Client(address='127.0.0.1:'+str(REDIS_PORT), cluster=False) send_tensor = np.ones((4,3,3)) client.put_tensor("tutorial_tensor_1", send_tensor) receive_tensor = client.get_tensor("tutorial_tensor_1") print('Receive tensor:\n\n', receive_tensor) ``` -------------------------------- ### Inspect Model Output Files Source: https://www.craylabs.org/docs/versions/0.3.2/_sources/tutorials/01_getting_started/01_getting_started Lists the contents of the current directory and prints the contents of the standard output and standard error files generated by a SmartSim model. This is useful for verifying model execution and debugging. ```python os.listdir('.') outputfile = './tutorial-model.out' errorfile = './tutorial-model.err' print("Content of tutorial-model.out:") with open(outputfile, 'r') as fin: print(fin.read()) print("Content of tutorial-model.err") ``` -------------------------------- ### Import SmartSim Experiment Module Source: https://www.craylabs.org/docs/_sources/tutorials/getting_started/getting_started Imports the necessary Experiment class from the SmartSim library and the os module for operating system interactions. These are fundamental for setting up and managing simulations. ```python import os from smartsim import Experiment ``` -------------------------------- ### SmartRedis Test Build and Environment Setup Source: https://www.craylabs.org/docs/versions/0.4.0/developer Commands to set up the testing environment and build the SmartRedis test suite. Assumes base SmartRedis dependencies are already installed and GCC/CMake requirements are met. ```bash source setup_test_env.sh make build-tests ``` -------------------------------- ### Define and Trace PyTorch CNN Model Source: https://www.craylabs.org/docs/versions/0.3.2/tutorials/01_getting_started/01_getting_started Defines a simple one-layer Convolutional Neural Network using PyTorch, traces it with an example input to create a script module, and saves the traced model to a file named `torch_cnn.pt`. ```python import torch import torch.nn as nn # taken from https://pytorch.org/docs/master/generated/torch.jit.trace.html class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv = nn.Conv2d(1, 1, 3) def forward(self, x): return self.conv(x) net = Net() example_forward_input = torch.rand(1, 1, 3, 3) module = torch.jit.trace(net, example_forward_input) # Save the traced model to a file torch.jit.save(module, "./torch_cnn.pt") ```