Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements for symbolic derivatives #358

Merged
merged 13 commits into from
Nov 10, 2023
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ADD_HEYOKA_BENCHMARK(taylor_ANN)
ADD_HEYOKA_BENCHMARK(taylor_ANN_2)
ADD_HEYOKA_BENCHMARK(apophis)
ADD_HEYOKA_BENCHMARK(stiff_equation)
ADD_HEYOKA_BENCHMARK(nn_diff)
ADD_HEYOKA_BENCHMARK(mascon_models)
ADD_HEYOKA_BENCHMARK(outer_ss_jet_benchmark)
ADD_HEYOKA_BENCHMARK(outer_ss_long_term)
Expand Down
59 changes: 59 additions & 0 deletions benchmark/nn_diff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020, 2021, 2022, 2023 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <iostream>
#include <stdexcept>

#include <boost/program_options.hpp>

#include <spdlog/spdlog.h>

#include <heyoka/expression.hpp>
#include <heyoka/kw.hpp>
#include <heyoka/logging.hpp>
#include <heyoka/math/tanh.hpp>
#include <heyoka/model/ffnn.hpp>

using namespace heyoka;

int main(int argc, char *argv[])
{
namespace po = boost::program_options;

create_logger();
set_logger_level_trace();

auto logger = spdlog::get("heyoka");

unsigned nn_layer{};

po::options_description desc("Options");

desc.add_options()("help", "produce help message")("nn_layer", po::value<unsigned>(&nn_layer)->default_value(10u),
"number of neurons per layer");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

if (vm.count("help") != 0u) {
std::cout << desc << "\n";
return 0;
}

if (nn_layer == 0u) {
throw std::invalid_argument("The number of neurons per layer cannot be zero");
}

auto [x, y] = make_vars("x", "y");
auto ffnn = model::ffnn(kw::inputs = {x, y}, kw::nn_hidden = {nn_layer, nn_layer, nn_layer}, kw::n_out = 2,
kw::activations = {heyoka::tanh, heyoka::tanh, heyoka::tanh, heyoka::tanh});

auto dt = diff_tensors(ffnn, kw::diff_args = diff_args::params);
auto dNdtheta = dt.get_jacobian();
}
2 changes: 1 addition & 1 deletion doc/advanced_tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Advanced tutorials

.. important::

More :ref:`tutorials <hypy:adv_tutorials>` and :ref:`examples <hypy:examples>` are available in the documentation
More tutorials and examples are available in the documentation
of heyoka's `Python bindings <https://bluescarni.github.io/heyoka.py>`__.

In this section we will show some of heyoka's more advanced functionalities,
Expand Down
2 changes: 1 addition & 1 deletion doc/basic_tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Basic tutorials

.. important::

More :ref:`tutorials <hypy:basic_tutorials>` and :ref:`examples <hypy:examples>` are available in the documentation
More tutorials and examples are available in the documentation
of heyoka's `Python bindings <https://bluescarni.github.io/heyoka.py>`__.

The code snippets in these tutorials assume the inclusion of the
Expand Down
5 changes: 4 additions & 1 deletion doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ New
Changes
~~~~~~~

- Substantial speedups in the computation of first-order derivatives
with respect to many variables/parameters
(`#358 <https://github.com/bluescarni/heyoka/pull/358>`__).
- Substantial performance improvements in the computation of
derivative tensors of large expressions with a high degree
of internal redundancy
Expand Down Expand Up @@ -154,7 +157,7 @@ Changes
~~~~~~~

- The step callbacks are now copied in :ref:`ensemble propagations <tut_ensemble>`
rather then being shared among threads. The aim of this change
rather than being shared among threads. The aim of this change
is to reduce the likelihood of data races
(`#334 <https://github.com/bluescarni/heyoka/pull/334>`__).
- Comprehensive overhaul of the expression system, including:
Expand Down
Loading