torch_geometric.profile
A decorator to facilitate profiling a function, e.g., obtaining training runtime and memory statistics of a specific model on a specific dataset. |
|
A context decorator to facilitate timing a function, e.g., obtaining the runtime of a specific model on a specific dataset. |
|
Creates a summary of collected runtime and memory statistics. |
|
Given a |
|
Given a |
|
Given a |
|
Returns the used CPU memory in bytes, as reported by the Python garbage collector. |
|
Returns the used GPU memory in bytes, as reported by the Python garbage collector. |
|
Returns the free and used GPU memory in megabytes, as reported by |
|
Benchmark a list of functions |
- profileit()[source]
A decorator to facilitate profiling a function, e.g., obtaining training runtime and memory statistics of a specific model on a specific dataset. Returns a
Statsobject with the attributestime,max_active_cuda,max_reserved_cuda,max_active_cuda,nvidia_smi_free_cuda,nvidia_smi_used_cuda.@profileit() def train(model, optimizer, x, edge_index, y): optimizer.zero_grad() out = model(x, edge_index) loss = criterion(out, y) loss.backward() optimizer.step() return float(loss) loss, stats = train(model, x, edge_index, y)
- class timeit(log: bool = True, avg_time_divisor: int = 0)[source]
A context decorator to facilitate timing a function, e.g., obtaining the runtime of a specific model on a specific dataset.
@torch.no_grad() def test(model, x, edge_index): return model(x, edge_index) with timeit() as t: z = test(model, x, edge_index) time = t.duration
- Parameters
- get_stats_summary(stats_list: List[Stats])[source]
Creates a summary of collected runtime and memory statistics. Returns a
StatsSummaryobject with the attributestime_mean,time_std,max_active_cuda,max_reserved_cuda,max_active_cuda,min_nvidia_smi_free_cuda,max_nvidia_smi_used_cuda.- Parameters
stats_list (List[Stats]) – A list of
Statsobjects, as returned byprofileit().
- count_parameters(model: Module) int[source]
Given a
torch.nn.Module, count its trainable parameters.- Parameters
model (torch.nn.Model) – The model.
- get_model_size(model: Module) int[source]
Given a
torch.nn.Module, get its actual disk size in bytes.- Parameters
model (torch model) – The model.
- get_data_size(data: BaseData) int[source]
Given a
torch_geometric.data.Dataobject, get its theoretical memory usage in bytes.- Parameters
data (torch_geometric.data.Data or torch_geometric.data.HeteroData) – The
DataorHeteroDatagraph object.
- get_cpu_memory_from_gc() int[source]
Returns the used CPU memory in bytes, as reported by the Python garbage collector.
- get_gpu_memory_from_gc(device: int = 0) int[source]
Returns the used GPU memory in bytes, as reported by the Python garbage collector.
- Parameters
device (int, optional) – The GPU device identifier. (default:
1)
- get_gpu_memory_from_nvidia_smi(device: int = 0, digits: int = 2) Tuple[float, float][source]
Returns the free and used GPU memory in megabytes, as reported by
nivdia-smi.Note
nvidia-smiwill generally overestimate the amount of memory used by the actual program, see here.
- benchmark(funcs: List[Callable], args: Union[Tuple[Any], List[Tuple[Any]]], num_steps: int, func_names: Optional[List[str]] = None, num_warmups: int = 10, backward: bool = False)[source]
Benchmark a list of functions
funcsthat receive the same set of argumentsargs.- Parameters
funcs ([Callable]) – The list of functions to benchmark.
args ((Any, ) or [(Any, )]) – The arguments to pass to the functions. Can be a list of arguments for each function in
funcsin case their headers differ.num_steps (int) – The number of steps to run the benchmark.
func_names ([str], optional) – The names of the functions. If not given, will try to infer the name from the function itself. (default:
None)num_warmups (int, optional) – The number of warmup steps. (default:
10)backward (bool, optional) – If set to
True, will benchmark both forward and backward passes. (default:False)