gradCheck
On this page

gradCheck()

Function in IR

Compare the derivative grad builds for fn with a central difference on the CPU oracle at f64, at each point of opts.at, for every component of every parameter of opts.wrt and every component of the result.

import { gradCheck } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

Syntax

function gradCheck(m: ModuleDecl, fn: string, opts: GradCheckOptions): GradCheckResult

Parameters

m ModuleDecl

the module holding fn.

fn string

the name of the function.

opts GradCheckOptions

the parameters, the points, the mode, the step and the tolerance.

Return value

GradCheckResult

whether every partial derivative agrees, how many were compared and the worst.

Exceptions

SD0118

SD0118 when grad refuses the function or a parameter.

Description

Compare the derivative grad builds for fn with a central difference on the CPU oracle at f64, at each point of opts.at, for every component of every parameter of opts.wrt and every component of the result. It builds the derivative in the mode opts.mode names, so it holds forward mode and reverse mode alike.

Examples

Example

import { compile, gradCheck } from 'typeshade'
const { module } = compile(`"use typeshade"
export function f(x: f32, k: f32): f32 {
return sin(k * x) * k
}`)
gradCheck(module, 'f', { wrt: ['x', 'k'], at: [[0.5, 2]], mode: 'reverse' }).ok // true

In the guide

See also

Source

src/core/passes/grad-check.ts, line 102, at commit af54ddad5

Edit this page Report a problem