RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • adamupdate
    • analyzeNetwork
    • bilstmLayer
    • classificationLayer
    • combvec
    • convolution1dLayer
    • crossentropy
    • dlarray
    • dlarray.minus
    • dlarray.mtimes
    • dlarray.plus
    • dlarray.rdivide
    • dlarray.sum
    • dlarray.times
    • dlfeval
    • dlgradient
    • dlnetwork
    • dlupdate
    • eluLayer
    • exportONNXNetwork
    • featureInputLayer
    • forward
    • fullyConnectedLayer
    • get.Learnables
    • get.State
    • globalAveragePooling1dLayer
    • layerGraph
    • layerNormalizationLayer
    • lstmLayer
    • padsequences
    • regressionLayer
    • reluLayer
    • sequenceInputLayer
    • set.Learnables
    • set.State
    • softmaxLayer
    • trainingOptions
    • trainnet
    • trainNetwork

dlgradient — Differentiate a scalar dlarray loss recorded inside dlfeval.

dlgradient(loss, targets...) walks the active reverse-mode tape. Each requested output is a gradient for the corresponding traced target; a dlarray target produces a dlarray gradient, while supported learnables structures preserve their Layer and Parameter metadata and replace Value leaves.

Syntax

g = dlgradient(loss, target)
[g1, g2, ...] = dlgradient(loss, target1, target2, ...)

Inputs

loss
Type
Any
Required
Yes
Description
Scalar dlarray produced on the active dlfeval tape.
targets
Type
Any
Required
Variadic
Description
One or more traced dlarray values, learnables trees, or dlnetwork objects.
NameTypeRequiredDefaultDescription
lossAnyYes—Scalar dlarray produced on the active dlfeval tape.
targetsAnyVariadic—One or more traced dlarray values, learnables trees, or dlnetwork objects.

Returns

gradients
Type
Any
Description
One gradient value or tree per requested target.
NameTypeDescription
gradientsAnyOne gradient value or tree per requested target.

Returned values from dlgradient depend on how many outputs the caller requests.

Errors

RunMat:deepLearning:InvalidInput
When
No target is supplied; more outputs than targets are requested; loss is not a scalar traced dlarray; or a target is not traced or has an unsupported structure.
Message
dlgradient requires a scalar traced loss and valid traced targets
RunMat:deepLearning:Unsupported
When
A provider-resident dlarray would require unavailable resident tape kernels, or a requested gradient reduction is outside the scalar-broadcast and supported-layer subset.
Message
dlgradient cannot differentiate this provider or operation form
IdentifierWhenMessage
RunMat:deepLearning:InvalidInputNo target is supplied; more outputs than targets are requested; loss is not a scalar traced dlarray; or a target is not traced or has an unsupported structure.dlgradient requires a scalar traced loss and valid traced targets
RunMat:deepLearning:UnsupportedA provider-resident dlarray would require unavailable resident tape kernels, or a requested gradient reduction is outside the scalar-broadcast and supported-layer subset.dlgradient cannot differentiate this provider or operation form

Traced inputs and outputs

Call dlgradient from a dlfeval callback. The loss must be a scalar dlarray produced on that callback's tape; a separate dlarray constructed outside the traced calculation is not a substitute. Request no more gradient outputs than supplied targets. A target can be a traced dlarray, supported dlnetwork learnables, or a structure/table/cell tree containing traced dlarray leaves. Nested cell gradients preserve the cell shape and element order. Real single and double dlarray leaves retain their class, shape, Format, and Labels.

Parameter tables use string Layer and Parameter columns and a cell Value column. Only Value is differentiated; metadata is preserved, including custom parameter names and repeated labels. Assigning gradients or values to a network separately validates that network's canonical typed parameter roles.

The current host tape records dlarray plus, minus, elementwise times and rdivide, matrix mtimes, scalar sum(..., 'all'), and admitted sequential network operations: fully connected, convolution1d, LSTM/BiLSTM, layer normalization, global average pooling, ReLU, ELU, and softmax. Prediction and requested recurrent State outputs share the execution trace. A real double or single target keeps its class, geometry, Format, and Labels. Logical Data are admitted through the arithmetic 0/1-to-double boundary and produce double gradients.

Current limits and residency

Scalar broadcasting has a reverse reduction. General higher-rank implicit-expansion gradient reduction, custom layers, and sparse or complex differentiation are not implemented. A scalar output of an executed provider-resident network can be differentiated through admitted fully connected, convolution1d, layer normalization, global average pooling, ReLU, ELU, and softmax stages in real single or double precision where the provider implements their adjoints. Connected resident operations without an adjoint are rejected before reverse provider work; disconnected targets receive zero gradients. Provider capability and exact ownership are checked without gathering explicit GPU data.

The callback and tape are execution state: nested callbacks keep separate graphs, and suspension does not expose one callback's tape to another invocation. Host tape primitives and the bounded resident network route have separate adjoints. When a resident single-precision execution consumes double-precision learnables, a capable provider restores requested double-precision parameter gradients through a native cast. An F32-only provider can still differentiate a single-precision input; it rejects a requested double-precision parameter gradient before reverse provider work. Generic resident arithmetic, resident recurrent adjoints, and GPU trainnet are not yet supported by this route.

Example

Differentiate an elementwise square loss

X = dlarray(single([1 2 3])); [loss, gradients] = dlfeval(@squareLoss, X); assert(isequal(gradients.Data, single([2 4 6])));
function [loss, gradients] = squareLoss(X)
    Y = X .* X;
    loss = sum(Y, 'all');
    gradients = dlgradient(loss, X);
end

Using dlgradient with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how dlgradient changes the result.

Run a small dlgradient example, explain the result, then change one input and compare the output.

FAQ

Can I differentiate a GPU-backed dlarray?⌄

For an admitted scalar provider-resident network output, the current resident tape differentiates fully connected, convolution1d, layer normalization, global average pooling, ReLU, ELU, and softmax stages in real single or double precision when the provider supplies the required adjoints. Other connected resident operations report a provider-adjoint gap; explicit GPU data are not implicitly downloaded.

What does dlgradient return for network learnables?⌄

A matching learnables tree: Layer and Parameter metadata stay in place and traced Value leaves become dlarray gradients.

Related Deep Learning functions

adamupdate · analyzeNetwork · bilstmLayer · classificationLayer · combvec · convolution1dLayer · crossentropy · dlarray · dlarray.minus · dlarray.mtimes · dlarray.plus · dlarray.rdivide · dlarray.sum · dlarray.times · dlfeval · dlnetwork · dlupdate · eluLayer · exportONNXNetwork · featureInputLayer · forward · fullyConnectedLayer · get.Learnables · get.State · globalAveragePooling1dLayer · layerGraph · layerNormalizationLayer · lstmLayer · padsequences · regressionLayer · reluLayer · sequenceInputLayer · set.Learnables · set.State · softmaxLayer · trainingOptions · trainnet · trainNetwork

Source you can read

Every RunMat function's source is public. Read exactly how dlgradient is executed, line by line, in Rust.

  • View the source for dlgradient in Rust on GitHub
  • Learn how the RunMat runtime works
  • Found a bug? Open an issue with a minimal reproduction.

About RunMat

RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Traced inputs and outputs
  • Current limits and residency
  • Example
  • Differentiate an elementwise square loss
  • Using dlgradient with coding agents
  • FAQ
  • Related Deep Learning functions
  • Source you can read
  • About RunMat