Skip to content

GSoC 2022

Vassil Vassilev edited this page Feb 17, 2022 · 2 revisions

Project Description

Implement vector mode in forward mode automatic differentiation in Clad

In mathematics and computer algebra, automatic differentiation (AD) is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. Automatic differentiation is an alternative technique to Symbolic differentiation and Numerical differentiation (the method of finite differences). Clad is based on Clang which provides the necessary facilities for code transformation. The AD library is able to differentiate non-trivial functions, to find a partial derivative for trivial cases and has good unit test coverage.

Vector mode support will facilitate the computation of gradients using the forward mode AD in a single pass and thus without explicitly performing differentiation n times for n function arguments. The major benefit of using vector mode is that computationally expensive operations do not need to be recomputed n times for n function arguments.

For example, if we want to compute df/dx and df/dy of a function f(x, y) using the forward mode AD in Clad, then currently we need to explicitly differentiate f two times. Vector mode will allow the generation of f_d(x, y) such that we will be able to get partial derivatives with respect to all the function arguments (gradient) in a single call.

After successful completion of the project the code snippet should work as expected:

#include <clad/Differentiator/Differentiator.h>
#include <iostream>

double someComputationalIntensiveFn();

double fn(double x, double y) {
  double t = someComputationalIntensiveFn(); // should be computed only once
                                             // in the derived function.
  double res = 2 * t * x + 3 * t * x * y;
  return t;
}

int main() {
  auto d_fn = clad::differentiate(fn, "arr");
  double d_x = 0, d_y = 0;
  d_fn.execute(3, 5, &d_x, &d_y);
  std::cout << "Derivative of fn wrt d_x: " << d_x << "\n";
  std::cout << "Derivative of fn wrt d_y: " << d_y << "\n";
}

Task ideas and expected results:

Extend and generalize our ForwardModeVisitor to produce a single function with the directional derivatives; Add a new mode to the top-level clad interface clad::differentiate for vector mode; Extend the unit test coverage; Develop tutorials and documentation; Present the work at the relevant meetings and conferences.

Necessary skills: Intermediate C++; Understanding basic differential calculus; intermediate knowledge of clang and llvm.

Mentors: Vassil Vassilev (vgvassilev); Alexander Penev (alexander-penev)

Add support for differentiating with respect to multidimensional arrays (or pointers) in Clad.

In mathematics and computer algebra, automatic differentiation (AD) is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. Automatic differentiation is an alternative technique to Symbolic differentiation and Numerical differentiation (the method of finite differences). Clad is based on Clang which provides the necessary facilities for code transformation. The AD library is able to differentiate non-trivial functions, to find a partial derivative for trivial cases and has good unit test coverage.

Clad currently only supports differentiation with respect to single-dimensional arrays. Support for differentiation with respect to pointers is limited as well. This project aims to add support for multi-dimensional arrays (and pointers) in Clad.

After successful completion of the project the code snippet should work as expected:

#include <iostream>
#include "clad/Differentiator/Differentiator.h"

double fn(double arr[5][5]) {
  double res = 1 * arr[0][0] + 2 * arr[1][1] + 4 * arr[2][2];
  return res * 2;
}

int main() {
  auto d_fn = clad::gradient(fn);
  double arr[5][5] = {{1, 2, 3, 4, 5},
                      {6, 7, 8, 9, 10},
                      {11, 12, 13, 14, 15},
                      {16, 17, 18, 19, 20},
                      {21, 22, 23, 24, 25}};
  double d_arr[5][5] = {};
  d_fn.execute(arr, d_arr);
  std::cout << "Derivative of d_fn wrt arr[0][0]: " << d_arr[0][0] << "\n"; // 2
  std::cout << "Derivative of d_fn wrt arr[1][1]: " << d_arr[1][1] << "\n"; // 4
  return 0;
}

Necessary skills: Intermediate C++; Understanding basic differential calculus; intermediate knowledge of clang and llvm.

Mentors: Vassil Vassilev (vgvassilev); Alexander Penev (alexander-penev)

Add initial integration of Clad with Enzyme

In mathematics and computer algebra, automatic differentiation (AD) is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. Automatic differentiation is an alternative technique to Symbolic differentiation and Numerical differentiation (the method of finite differences). Clad is based on Clang which provides the necessary facilities for code transformation. The AD library is able to differentiate non-trivial functions, to find a partial derivative for trivial cases and has good unit test coverage. Enzyme is a prominent autodiff framework which works on LLVM IR.

Clad and Enzyme can be considered as a C++ frontend and a backend automatic differentiation framework. In many cases, when clad needs to fall back to numeric differentiation it can try configuring and using Enzyme to perform the automatic differentiation on lower level.

Task ideas and expected results:

Understand how both systems work. Define the Enzyme configuration requirements and enable Clad to communicate efficiently with Enzyme. That may require several steps: start building and using the optimization pass of Enzyme as part of the Clad toolchain; use Enzyme for cross-validation derivative results; etc. The student should be prepared to write a progress report and present the results.

Necessary skills: Intermediate C++; Understanding basic differential calculus; intermediate knowledge of clang and llvm.

Mentors: Vassil Vassilev (vgvassilev); William Moses (wsmoses)

If you have used clad and you have particular project proposal please contact vgvassilev.

Candidate Guidelines

If you have interest in working on the project there is a list of things to do in order to maximize your chances to get selected:

  1. Contact the mentors and express interest in the project. Make sure you attach your CV;
  2. Download the source code of the project, build it and run the demos;
  3. Start familiarizing yourself with the codebase;
  4. If you have questions you can always contact a mentor.

Candidate Evaluation

The mentors are interested in working with all candidates but unfortunately the rules allow only one to be selected. There are a few tasks which give bonus points to candidate's application:

  • Submit a valid bug -- demonstrates that the candidate has completed step 2 and 3 from the previous section.
  • Fix a bug -- demonstrates the technical skills of the candidate and shows he/she can work independently on the project. The mentors can suggest looking into these good first issues. Fixing one issue may be enough to become a successful candidate.

Good luck!