Profiling#
Ray Compiled Graph provides both PyTorch-based and Nsight-based profiling functionalities to better understand the performance of individual tasks, system overhead, and performance bottlenecks. You can pick your favorite profiler based on your preference.
PyTorch profiler#
To run PyTorch profiling on Compiled Graph, set the environment variable RAY_CGRAPH_ENABLE_TORCH_PROFILING=1 when you run the script. For example, for a Compiled Graph script in example.py, run the following command:
RAY_CGRAPH_ENABLE_TORCH_PROFILING=1 python3 example.py
After execution, Compiled Graph generates the profiling results in the compiled_graph_torch_profiles directory under the current working directory. Compiled Graph generates one trace file per actor.
To visualize the traces, use the Perfetto UI.
Nsight Systems profiler#
Compiled Graph builds on Ray’s profiling capabilities and uses Nsight Systems profiling.
To run Nsight profiling on Compiled Graph, specify the runtime environment for the actors involved, as described in Run Nsight on Ray. The following example shows this setup:
import ray
import torch
from ray.dag import InputNode
@ray.remote(num_gpus=1, runtime_env={"nsight": "default"})
class RayActor:
def send(self, shape, dtype, value: int):
return torch.ones(shape, dtype=dtype, device="cuda") * value
def recv(self, tensor):
return (tensor[0].item(), tensor.shape, tensor.dtype)
sender = RayActor.remote()
receiver = RayActor.remote()
Then, create a Compiled Graph as usual.
shape = (10,)
dtype = torch.float16
# Test normal execution.
with InputNode() as inp:
dag = sender.send.bind(inp.shape, inp.dtype, inp[0])
dag = dag.with_tensor_transport(transport="nccl")
dag = receiver.recv.bind(dag)
compiled_dag = dag.experimental_compile()
for i in range(3):
shape = (10 * (i + 1),)
ref = compiled_dag.execute(i, shape=shape, dtype=dtype)
assert ray.get(ref) == (i, shape, dtype)
Finally, run the script as usual.
python3 example.py
After execution, Compiled Graph generates the profiling results under the /tmp/ray/session_*/logs/{profiler_name} directory.
For fine-grained performance analysis of method calls and system overhead, set the environment variable RAY_CGRAPH_ENABLE_NVTX_PROFILING=1 when you run the script:
RAY_CGRAPH_ENABLE_NVTX_PROFILING=1 python3 example.py
This command uses the NVIDIA Tools Extension (NVTX) library to automatically annotate all methods called in the execution loops of the compiled graph.
To visualize the profiling results, follow the instructions in Nsight Profiling Result.
Visualization#
To visualize the graph structure, call the visualize method after calling experimental_compile on the graph.
import ray
from ray.dag import InputNode, MultiOutputNode
@ray.remote
class Worker:
def inc(self, x):
return x + 1
def double(self, x):
return x * 2
def echo(self, x):
return x
sender1 = Worker.remote()
sender2 = Worker.remote()
receiver = Worker.remote()
with InputNode() as inp:
w1 = sender1.inc.bind(inp)
w1 = receiver.echo.bind(w1)
w2 = sender2.double.bind(inp)
w2 = receiver.echo.bind(w2)
dag = MultiOutputNode([w1, w2])
compiled_dag = dag.experimental_compile()
compiled_dag.visualize()
By default, Ray generates a PNG image named compiled_graph.png and saves it in the current working directory. This requires graphviz.
The following image shows the visualization for the preceding code. Tasks that belong to the same actor are the same color.