Loading...
Searching...
No Matches
Derivative.h
Go to the documentation of this file.
#ifndef HALIDE_DERIVATIVE_H
#define HALIDE_DERIVATIVE_H
/** \file
* Automatic differentiation
*/
#include "Expr.h"
#include "Func.h"
#include <map>
#include <string>
#include <vector>
namespace Halide {
/**
* Helper structure storing the adjoints Func.
* Use d(func) or d(buffer) to obtain the derivative Func.
*/
class Derivative {
public:
// function name & update_id, for initialization update_id == -1
using FuncKey = std::pair<std::string, int>;
explicit Derivative(const std::map<FuncKey, Func> &adjoints_in)
: adjoints(adjoints_in) {
}
explicit Derivative(std::map<FuncKey, Func> &&adjoints_in)
: adjoints(std::move(adjoints_in)) {
}
// These all return an undefined Func if no derivative is found
// (typically, if the input Funcs aren't differentiable)
Func operator()(const Func &func, int update_id = -1) const;
Func operator()(const Buffer<> &buffer) const;
Func operator()(const Param<> &param) const;
Func operator()(const std::string &name) const;
private:
const std::map<FuncKey, Func> adjoints;
};
/**
* Given a Func and a corresponding adjoint, (back)propagate the
* adjoint to all dependent Funcs, buffers, and parameters.
* The bounds of output and adjoint need to be specified with pair {min, extent}
* For each Func the output depends on, and for the pure definition and
* each update of that Func, it generates a derivative Func stored in
* the Derivative.
*/
Derivative propagate_adjoints(const Func &output,
const Func &adjoint,
const Region &output_bounds);
/**
* Given a Func and a corresponding adjoint buffer, (back)propagate the
* adjoint to all dependent Funcs, buffers, and parameters.
* For each Func the output depends on, and for the pure definition and
* each update of that Func, it generates a derivative Func stored in
* the Derivative.
*/
Derivative propagate_adjoints(const Func &output,
const Buffer<float> &adjoint);
/**
* Given a scalar Func with size 1, (back)propagate the gradient
* to all dependent Funcs, buffers, and parameters.
* For each Func the output depends on, and for the pure definition and
* each update of that Func, it generates a derivative Func stored in
* the Derivative.
*/
Derivative propagate_adjoints(const Func &output);
} // namespace Halide
#endif
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt).
Func.h
Defines Func - the front-end handle on a halide function, and related classes.
Halide::Buffer<>
Halide::Derivative
Helper structure storing the adjoints Func.
Definition Derivative.h:21
Halide::Derivative::operator()
Func operator()(const std::string &name) const
Halide::Derivative::Derivative
Derivative(std::map< FuncKey, Func > &&adjoints_in)
Definition Derivative.h:29
Halide::Derivative::FuncKey
std::pair< std::string, int > FuncKey
Definition Derivative.h:24
Halide::Derivative::operator()
Func operator()(const Func &func, int update_id=-1) const
Halide::Derivative::operator()
Func operator()(const Buffer<> &buffer) const
Halide::Derivative::operator()
Func operator()(const Param<> &param) const
Halide::Derivative::Derivative
Derivative(const std::map< FuncKey, Func > &adjoints_in)
Definition Derivative.h:26
Halide::Func
A halide function.
Definition Func.h:706
Halide::Param
A scalar parameter to a halide pipeline.
Definition Param.h:22
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::propagate_adjoints
Derivative propagate_adjoints(const Func &output, const Func &adjoint, const Region &output_bounds)
Given a Func and a corresponding adjoint, (back)propagate the adjoint to all dependent Funcs,...
Halide::Region
std::vector< Range > Region
A multi-dimensional box.
Definition Expr.h:350