Profiling calculation

It can be interesting to know where a calculation is spending its time. To this end, rascaline includes self-profiling code that can record and display which part of the calculation takes time, and which function called long-running functions. All the example should output something similar to the table below.

╔════╦══════════════════════════════╦════════════╦═══════════╦══════════╦═════════════╗
║ id ║ span name                    ║ call count ║ called by ║ total    ║ mean        ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  2 ║ Full calculation             ║          1 ║         — ║ 660.58ms ║    660.58ms ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  3 ║ SoapPowerSpectrum::compute   ║          1 ║         2 ║ 584.02ms ║    584.02ms ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  1 ║ Calculator::prepare          ║          2 ║      3, 2 ║ 148.15ms ║     74.08ms ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  0 ║ NeighborsList                ║         20 ║         1 ║  20.82ms ║      1.04ms ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  5 ║ SphericalExpansion::compute  ║          1 ║         3 ║ 196.38ms ║    196.38ms ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  4 ║ GtoRadialIntegral::compute   ║      74448 ║         5 ║ 117.04ms ║      1.57µs ║
╠════╬══════════════════════════════╬════════════╬═══════════╬══════════╬═════════════╣
║  6 ║ SphericalHarmonics::compute  ║      74448 ║         5 ║   9.95ms ║ 133.00ns ⚠️ ║
╚════╩══════════════════════════════╩════════════╩═══════════╩══════════╩═════════════╝

In this table, the first columns assign a unique numeric identifier to each section of the code. The second one displays the name of the section. Then come the number of time this section of the code have been executed, which other function/section called the current one, and finally the total and mean time spent in this function.

The ⚠️ symbol is added when the mean cost of the function is close to the profiling overhead (30 to 80ns per function call), and thus the measurement might not be very reliable.

Some of the most important sections are:

  • Calculator::prepare: building the list of samples/properties that will be in the descriptor

  • XXX::compute: building blocks for the overall calculation

  • NeighborsList: construction of the list of neighbors

You can obtain a dataset for profiling from our website.

import chemfiles

import rascaline
from rascaline import SoapPowerSpectrum


def compute_soap(path):
    """Compute SOAP power spectrum.

    This is the same code as the 'compute-soap' example
    """
    with chemfiles.Trajectory(path) as trajectory:
        frames = [f for f in trajectory]

    HYPER_PARAMETERS = {
        "cutoff": 5.0,
        "max_radial": 6,
        "max_angular": 4,
        "atomic_gaussian_width": 0.3,
        "center_atom_weight": 1.0,
        "radial_basis": {
            "Gto": {},
        },
        "cutoff_function": {
            "ShiftedCosine": {"width": 0.5},
        },
    }

    calculator = SoapPowerSpectrum(**HYPER_PARAMETERS)
    descriptor = calculator.compute(frames, gradients=["positions"])
    descriptor = descriptor.keys_to_samples("center_type")
    descriptor = descriptor.keys_to_properties(["neighbor_1_type", "neighbor_2_type"])

    return descriptor

Run the calculation with profiling enabled.

with rascaline.Profiler() as profiler:
    descriptor = compute_soap("dataset.xyz")

Display the recorded profiling data as table.

print(profiler.as_short_table())
╔════╦══════════════════════════════════════════════╦════════════╦═══════════╦══════════╦══════════════╗
║ id ║ span name                                    ║ call count ║ called by ║ total    ║ mean         ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  4 ║ SoapPowerSpectrum::compute                   ║          1 ║         — ║ 420.09ms ║     420.09ms ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  5 ║ SphericalExpansion::compute                  ║          1 ║         4 ║ 197.61ms ║     197.61ms ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  1 ║ SoapRadialIntegralSpline::with_accuracy      ║          6 ║         5 ║  53.03ms ║       8.84ms ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  0 ║ GtoRadialIntegral::compute                   ║       3846 ║         1 ║  50.16ms ║      13.04µs ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  3 ║ Calculator::prepare                          ║          2 ║         4 ║  53.29ms ║      26.64ms ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  2 ║ NeighborsList                                ║         40 ║         3 ║  11.93ms ║     298.34µs ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  6 ║ SplinedRadialIntegral::compute               ║      35307 ║         5 ║  17.01ms ║ 481.00ns ⚠️  ║
╠════╬══════════════════════════════════════════════╬════════════╬═══════════╬══════════╬══════════════╣
║  7 ║ SphericalHarmonics::compute                  ║      35307 ║         5 ║  20.30ms ║ 574.00ns ⚠️  ║
╚════╩══════════════════════════════════════════════╩════════════╩═══════════╩══════════╩══════════════╝

You can also save this data as json for future usage

print(profiler.as_json())
{"timings":{"rascaline::calculators::soap::radial_integral::gto::GtoRadialIntegral::compute":{"id":0,"elapsed":"50.157631ms","called":3846},"rascaline::calculators::soap::radial_integral::spline::SoapRadialIntegralSpline::with_accuracy":{"id":1,"elapsed":"53.028376ms","called":6},"rascaline::systems::neighbors::NeighborsList":{"id":2,"elapsed":"11.93377ms","called":40},"rascaline::calculator::Calculator::prepare":{"id":3,"elapsed":"53.286337ms","called":2},"rascaline::calculators::soap::power_spectrum::SoapPowerSpectrum::compute":{"id":4,"elapsed":"420.089266ms","called":1},"rascaline::calculators::soap::spherical_expansion::SphericalExpansion::compute":{"id":5,"elapsed":"197.608947ms","called":1},"rascaline::calculators::soap::radial_integral::spline::SplinedRadialIntegral::compute":{"id":6,"elapsed":"17.007453ms","called":35307},"rascaline::math::spherical_harmonics::SphericalHarmonics::compute":{"id":7,"elapsed":"20.300349ms","called":35307}},"calls":[{"caller":0,"callee":1,"count":3846},{"caller":2,"callee":3,"count":40},{"caller":3,"callee":4,"count":1},{"caller":1,"callee":5,"count":1},{"caller":6,"callee":5,"count":1},{"caller":7,"callee":5,"count":1},{"caller":5,"callee":4,"count":1}]}