---
title: "gradCheck(): function in IR, TypeShade API reference"
description: "gradCheck, a 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…"
url: https://typeshade.dev/api/gradcheck/
language: en
---

# `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

```ts
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`](https://typeshade.dev/reference/errors/sd0118/)**

[`SD0118`](https://typeshade.dev/reference/errors/sd0118/) when `grad` refuses the function or a parameter.

## Description

Compare the derivative [`grad`](https://typeshade.dev/api/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

```ts
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

- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`grad`](https://typeshade.dev/api/grad/)

## Source

[`src/core/passes/grad-check.ts`, line 102](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/grad-check.ts#L102), at commit af54ddad5
