주요 콘텐츠

Code Generation for dlarray

R2026b

A deep learning array stores data with optional data format labels for custom training loops, and enables functions to compute and use derivatives through automatic differentiation. To learn more about custom training loops, automatic differentiation, and deep learning arrays, see Custom Training Using Automatic Differentiation (Deep Learning Toolbox).

Code generation supports both formatted and unformatted dlarray (Deep Learning Toolbox) objects as inputs to the predict (Deep Learning Toolbox) method of dlnetwork objects. dlarray objects containing gpuArrays are also supported for code generation. To generate C/C++ code using deep learning arrays, you need to install MATLAB Coder Interface for Deep Learning. For generating and deploying CUDA® code onto NVIDIA® GPUs, you need to install GPU Coder Interface for Deep Learning.

Note

You can pass numeric inputs directly to the predict function without creating a dlarray object. This approach simplifies integration with standalone code generation workflows, improves performance for MEX workflows, and allows you to pass numeric signals directly to MATLAB Function blocks in Simulink® without converting them to dlarray objects. For more information, see Generate CUDA Code for Deep Learning Networks. (since R2026b)

Define dlarray for Code Generation

For code generation, use the dlarray (Deep Learning Toolbox) function to create deep learning arrays. For example, suppose you have a pretrained dlnetwork (Deep Learning Toolbox) network object in the mynet.mat MAT file. To predict the responses for this network, create an entry-point function in MATLAB®.

There are two possible entry-point designs:

Design 1 (Recommended)

In this design example, the input and output of foo are of primitive datatypes and a formatted dlarray object is created within the function. The extractdata (Deep Learning Toolbox) method of the dlarray object returns the data in the dlarray dlA as the output of foo. The output a has the same data type as the underlying data type in dlA.

In this design, dimension labels are specified by creating a formatted dlarray object within the entry-point function.

function a = foo(in)
dlIn = dlarray(in, "SSC");

persistent dlnet;
if isempty(dlnet)
    dlnet = coder.loadDeepLearningNetwork("mynet.mat");
end

dlA = predict(dlnet, dlIn);

a = extractdata(dlA);

end

You can also simplify this workflow by using an unformatted dlarray object. (since R2026b)

function out = foo(in)

persistent dlnet;
if isempty(dlnet)
    dlnet = coder.loadDeepLearningNetwork("mynet.mat");
end

dlIn = dlarray(in);
dlOut = predict(dlnet, dlIn);
out = extractdata(dlOut);

end

To see an example of dlnetwork and dlarray usage with GPU Coder™, see Generate Digit Images on NVIDIA GPU Using Variational Autoencoder.

Design 2 (Not Recommended)

In this design example, the input and output of the entry-point function foo are of dlarray type. This design is not recommended for code generation for these reasons:

  • In Simulink, dlarray objects cannot propagate through signals in a MATLAB Function block.

  • The code generator maps the dlarray data type to a struct or class rather than a native type such as float. To interface with the generated entry-point function, you must instantiate the struct or class and copy inputs to its members.

function dlOut = foo(dlIn)

persistent dlnet;
if isempty(dlnet)
    dlnet = coder.loadDeepLearningNetwork("mynet.mat");
end

dlOut = predict(dlnet, dlIn);

end

Generate code for complex-valued dlarray objects

Code generation supports complex number functions with dlarray objects. You can pass complex-valued inputs to dlarray-supported complex number functions and generate C/C++ and CUDA code that does not depend on third-party libraries. You can implement the complex-valued dlarray support functionality in Simulink by using a MATLAB Function Block.

Code generation does not support passing complex-valued input to the predict method of dlnetwork object. For code generation, the dlarray input to the predict method of the dlnetwork object must be single data type.

You cannot pass a complex-valued input to a MEX function if the input is specified as real during code generation time. For more usage notes and limitations of code generation support for complex data, see Code Generation for Complex Data

Using Variable-Size dlarray

You can generate code for MATLAB code that uses variable-size dlarray objects.

For example, define this MATLAB design file:

function out = fooAdd(in1,in2) %#codegen
dlIn1_1 = dlarray(in1);
dlIn1_2 = dlarray(in2);
out = dlIn1_1 + dlIn1_2;
end

Specify the two inputs in1 and in2 to be unbounded two-dimensional arrays of single type. Create the appropriate code configuration object cfg to generate generic C MEX code for fooAdd. Generate MEX code and run the generated MEX.

t_in1 = coder.typeof(single(1),[inf inf],[1 1]);
t_in2 = coder.typeof(single(1),[inf inf],[1 1]);

codegen fooAdd -args {t_in1,t_in2} -report

out = fooAdd_mex(single(eye(4,4)),single(ones(4,1)));

When generating code for variable-size dlarray objects, adhere to these restrictions:

  • The U dimension of a dlarray object must be of fixed size.

  • For operations between a dlarray object and a numeric array that might implicitly expand either operands, do not combine a fixed size U dimension of the dlarray object with a variable-size dimension of the numeric array.

  • For unary operations such as max, min, and mean on a variable-size dlarray object, specify the intended working dimension explicitly as a constant value. See Incompatibility with MATLAB for Default Dimension Selection.

dlarray Object Functions with Code Generation Support

For code generation, you are restricted to the deep learning array object functions listed in this table. For more information of usage notes and limitations, see the extended capabilities section on the reference page.

dims (Deep Learning Toolbox)

Dimension labels for dlarray

extractdata (Deep Learning Toolbox)

Extract data from dlarray

finddim (Deep Learning Toolbox)

Find dimensions with specified label

stripdims (Deep Learning Toolbox)

Remove dlarray labels

Deep Learning Toolbox Functions with dlarray Code Generation Support

Deep Learning Operations

FunctionDescription
avgpool (Deep Learning Toolbox)The average pooling operation performs downsampling by dividing the input into pooling regions and computing the average value of each region.
batchnorm (Deep Learning Toolbox)The batch normalization operation normalizes the input data across all observations for each channel independently. To speed up training of the convolutional neural network and reduce the sensitivity to network initialization, use batch normalization between convolution and nonlinear operations such as relu (Deep Learning Toolbox).
dlconv (Deep Learning Toolbox)The convolution operation applies sliding filters to the input data. Use the dlconv (Deep Learning Toolbox) function for deep learning convolution, grouped convolution, and channel-wise separable convolution.
fullyconnect (Deep Learning Toolbox)

The fully connect operation multiplies the input by a weight matrix and then adds a bias vector.

gelu (Deep Learning Toolbox) (since R2026a)The Gaussian error linear unit (GELU) activation operation weights the input by its probability under a Gaussian distribution.
groupnorm (Deep Learning Toolbox)The group normalization operation normalizes the input data across grouped subsets of channels for each observation independently.
instancenorm (Deep Learning Toolbox)The instance normalization operation normalizes the input data across each channel for each observation independently.
layernorm (Deep Learning Toolbox)The layer normalization operation normalizes the input data across all channels for each observation independently.
leakyrelu (Deep Learning Toolbox)The leaky rectified linear unit (ReLU) activation operation performs a nonlinear threshold operation, where any input value less than zero is multiplied by a fixed scale factor.
lstm (Deep Learning Toolbox) (since R2026b)The long short-term memory (LSTM) operation allows a network to learn long-term dependencies between time steps in time series and sequence data.
maxpool (Deep Learning Toolbox)The maximum pooling operation performs downsampling by dividing the input into pooling regions and computing the maximum value of each region.
relu (Deep Learning Toolbox)The rectified linear unit (ReLU) activation operation performs a nonlinear threshold operation, where any input value less than zero is set to zero.
sigmoid (Deep Learning Toolbox)

The sigmoid activation operation applies the sigmoid function to the input data.

softmax (Deep Learning Toolbox)

The softmax activation operation applies the softmax function to the channel dimension of the input data.

Domain-Specific Functions with dlarray Support

Signal Processing

FunctionDescription
dlcwt (Wavelet Toolbox) (since R2026a)Compute continuous wavelet transform.
dlicwt (Wavelet Toolbox) (since R2026a)Compute inverse continuous wavelet transform.
dldwt (Wavelet Toolbox) (since R2026b)Compute 1-D and 2-D forward discrete wavelet transforms.
dlidwt (Wavelet Toolbox) (since R2026b)

Compute 1-D and 2-D inverse discrete wavelet transforms.

dlmodwt (Wavelet Toolbox) (since R2025a)Compute maximal overlap discrete wavelet transform and multiresolution analysis.
dlstft (Signal Processing Toolbox) (since R2025a)Compute short-time Fourier transform.
dlistft (Signal Processing Toolbox) (since R2025a)Compute inverse short-time Fourier transform.

Wireless Communications

FunctionDescription
awgn (Communications Toolbox)Filter a signal represented in a dlarray object through an additive white Gaussian noise (AWGN) channel.
bit2int (Communications Toolbox)Convert input bits represented in a dlarray object to integers.
genqammod (Communications Toolbox)Modulate a signal represented in a dlarray object using general quadrature amplitude modulation (QAM).
ofdmChannelResponse (Communications Toolbox)Calculate the frequency response of a time-varying channel represented in a dlarray object.
ofdmdemod (Communications Toolbox)Demodulate a time-domain signal represented in a dlarray object using orthogonal frequency division multiplexing (OFDM).
ofdmEqualize (Communications Toolbox)Equalize a frequency-domain OFDM signal represented in a dlarray object.
ofdmmod (Communications Toolbox)Modulate a frequency-domain signal represented in a dlarray object using orthogonal frequency division multiplexing (OFDM).

MATLAB Functions with dlarray Code Generation Support

Unary Element-wise Functions

FunctionNotes and Limitations
abs

The output dlarray has the same data format as the input dlarray.

acos
acosh
acot
acsc
angle
asec
asin
asinh
atan
atan2
atanh
conj
cos
cosh
cot
csc
exp
imag
real
reallog
realsqrt
log
sec
sign
sin
sinh
sqrt
tan
tanh
uplus, +
uminus, -
erf

Binary Element-wise Operators

FunctionNotes and Limitations
complex

For the one-input syntax, the output dlarray has the same data format as the input dlarray

For the two-input syntax, if dlarray inputs are formatted, their data formats must match.

minus, -

If the two dlarray inputs are formatted, then the output dlarray is formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, see Implicit Expansion with Data Formats (Deep Learning Toolbox).

mod (since R2026a)
plus, +
power, .^
rdivide, ./
realpow
rem (since R2026a)
times, .*

Reduction Functions

FunctionNotes and Limitations
mean
  • The output dlarray has the same data format as the input dlarray.

  • The 'omitnan' option is not supported.

  • The Weights argument is not supported.

  • If the input dlarray is on the GPU, the 'native' option is not supported.

median
  • The output dlarray has the same data format as the input dlarray.

  • The 'omitnan' option is not supported.

  • The Weights argument is not supported.

norm (since R2026b)

The output dlarray has the same data format as the input dlarray.

vecnorm
prod
  • The output dlarray has the same data format as the input dlarray.

  • The 'omitnan' option is not supported.

sum

Extrema Functions

FunctionNotes and Limitations
ceil

The output dlarray has the same data format as the input dlarray.

eps

  • The output dlarray has the same data format as the input dlarray.

  • Use eps(ones(‘like’, x)) to get a scalar epsilon value based on the data type of a dlarray x.

fix

The output dlarray has the same data format as the input dlarray.

floor

The output dlarray has the same data format as the input dlarray.

max
  • When you find the maximum or minimum elements of a single dlarray, the output dlarray has the same data format as the input dlarray.

  • When you find the maximum or minimum elements between two formatted dlarray inputs, the output dlarray has a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, see Implicit Expansion with Data Formats (Deep Learning Toolbox).

  • The index output argument is not traced and cannot be used with automatic differentiation. For more information, see Use Automatic Differentiation In Deep Learning Toolbox (Deep Learning Toolbox).

min
round

  • Only the syntax Y = round(X) is supported.

  • The output dlarray has the same data format as the input dlarray.

Fourier Analysis and Filtering

FunctionNotes and Limitations
fftOnly unformatted input arrays are supported.
ifft
  • Only unformatted input arrays are supported.

  • When you use the 'symmetric' option, ifft treats the input Y as exactly symmetric. If you compute the derivative using automatic differentiation, then the derivative is also exactly symmetric. If Y is non-symmetric, then the function and gradient behavior might not match. To ensure that function and gradient behavior match for non-symmetric inputs, explicitly symmetrize Y.

filterOnly unformatted input arrays are supported.

Other Math Operations

FunctionNotes and Limitations
colon, :
  • The supported operations are:

    • a:b

    • a:b:c

    For information on indexing into a dlarray, see Indexing (Deep Learning Toolbox).

  • All inputs must be real scalars. The output dlarray is unformatted.

interp2 (since R2026b)
  • method must be 'linear' or 'nearest'.

  • All dlarray inputs must be unformatted.

mtimes, *
  • One input can be a formatted dlarray only when the other input is an unformatted scalar. In this case, the output dlarray has the same data format as the formatted dlarray input.

  • Multiplying a dlarray with a non-dlarray sparse matrix is supported only when both inputs are non-scalar.

pagemtimes
  • One input can be a formatted dlarray only when the other input is unformatted, with scalar pages. In this case, the output dlarray has the same data format as the formatted dlarray input.

  • For code generation, each transpose option of pagemtimes must be constant.

pinv 
sort 
lsqminnorm

dlarray inputs must be unformatted.

pagelsqminnorm

dlarray inputs must be unformatted.

Logical Operations

FunctionNotes and Limitations
and, &

If the two dlarray inputs are formatted, then the output dlarray is formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, see Implicit Expansion with Data Formats (Deep Learning Toolbox).

eq, ==

If the two dlarray inputs are formatted, then the output dlarray is formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, see Implicit Expansion with Data Formats (Deep Learning Toolbox).

ge, >=
gt, >
le, <=
lt, <
ne, ~=
not, ~

The output dlarray has the same data format as the input dlarray.

or, |

If the two dlarray inputs are formatted, then the output dlarray is formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, see Implicit Expansion with Data Formats (Deep Learning Toolbox).

xor

Size Manipulation Functions

FunctionNotes and Limitations
reshape

The output dlarray is unformatted, even if the input dlarray is formatted.

For code generation, the size dimensions must be fixed size.

squeeze

Two-dimensional dlarray objects are unaffected by squeeze. If the input dlarray is formatted, the function removes dimension labels belonging to singleton dimensions. If the input dlarray has more than two dimensions and its third and above dimensions are singleton, then the function discards these dimensions and their labels.

repelem

If you use the u = repelem(v,n) syntax and specify the number of times to repeat each element in repelem, the output dlarray is unformatted even if the input dlarray is formatted.

If you use the B = repelem(A,r1,...,rN) syntax and specify the repetition factors for each dimension in repelem, the output dlarray has the same data format as the input dlarray.

repmat

The output dlarray has the same data format as the input dlarray.

Transposition Operations

FunctionNotes and Limitations
ctranspose, '

If the input dlarray is formatted, then the labels of both dimensions must be the same. The function performs transposition implicitly, and transposes directly only if necessary for other operations.

permute

If the input dlarray is formatted, then the permutation must be among only those dimensions that have the same label. The function performs permutations implicitly, and permutes directly only if necessary for other operations.

For code generation, the dimension order must be fixed size.

ipermute

If the input dlarray is formatted, then the permutation must be among only those dimensions that have the same label. The function performs permutations implicitly, and permutes directly only if necessary for other operations.

For code generation, the dimension order must be fixed size.

transpose, .'

If the input dlarray is formatted, then the labels of both dimensions must be the same. The function performs transposition implicitly, and transposes directly only if necessary for other operations.

Concatenation Functions

FunctionNotes and Limitations
cat

The dlarray inputs must have matching formats or be unformatted. Mixed formatted and unformatted inputs are supported. If any dlarray inputs are formatted, then the output dlarray is formatted with the same data format.

For code generation, the dimension order to cat function must be fixed size.

horzcat
vertcat

Conversion Functions

FunctionNotes and Limitations
cast
  • cast(dlA,newdatatype) copies the data in the dlarray dlA into a dlarray of the underlying data type newdatatype. The newdatatype option must be 'double', 'single', or 'logical'. The output dlarray is formatted with the same data format as dlA.

  • cast(A,'like',Y) returns an array of the same type as Y. If Y is a dlarray, then the output is a dlarray that has the same underlying data type as Y. If Y is on the GPU, then the output is on the GPU. If both A and Y are dlarray objects, then the output dlarray is formatted with the same data format as the input A.

double

The output is a dlarray that contains data of type double.

logicalThe output is a dlarray that contains data of type logical.
singleThe output is a dlarray that contains data of type single.

Comparison Functions

FunctionNotes and Limitations
isequal

  • The syntax with more than two input arguments is not supported.

  • Two dlarray inputs are equal if the numeric data they represent are equal and if they both are either formatted with the same data format or unformatted.

isequaln

  • The syntax with more than two input arguments is not supported.

  • Two dlarray inputs are equal if the numeric data they represent are equal (treating NaNs as equal) and if they both are either formatted with the same data format or unformatted.

Data Type and Value Identification Functions

FunctionNotes and Limitations
isdlarray (Deep Learning Toolbox)N/A
isfloat

The software applies the function to the underlying data of an input dlarray.

islogical
isnumeric
isreal 
underlyingTypeN/A
validateattributesIf input array A is a formatted dlarray, its dimensions are permuted to match the order "SCBTU". Size validation is applied after permutation.

Size Identification Functions

FunctionNotes and Limitations
iscolumnThis function returns true for a dlarray that is a column vector, where each dimension except the first is a singleton. For example, a 3-by-1-by-1 dlarray is a column vector.
ismatrixThis function returns true for dlarray objects with only two dimensions and for dlarray objects where each dimension except the first two is a singleton. For example, a 3-by-4-by-1 dlarray is a matrix.
isrowThis function returns true for a dlarray that is a row vector, where each dimension except the second is a singleton. For example, a 1-by-3-by-1 dlarray is a row vector.
isscalarN/A
isvectorThis function returns true for a dlarray that is a row vector or column vector. Note that isvector does not consider a 1-by-1-by-3 dlarray to be a vector.
lengthN/A
ndims

If the input dlarray dlX is formatted, then ndims(dlX) returns the number of dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

numelN/A
size

If the input dlarray dlX is formatted, then size(dlX) returns a vector of length equal to the number of dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

Creator Functions

FunctionNotes and Limitations
falseOnly the 'like' syntax is supported for dlarray.
Inf
NaN
ones
rand
true
zeros

Usage Notes and Limitations

For deep learning arrays, code generation has the following limitations:

  • The data format argument of the dlarray object must be a compile-time constant. For example,

    function out = foo()
    
    dlA = dlarray(ones(5,4),"SSC"); %fmt "SSC" is constant
     .
     .
     .
    end

  • The code generation report does not display the size of the dlarray object. The size is always displayed as 1x1.

    Screen shot of sample report generator showing dlarray size as 1-by-1

  • For code generation, the dlarray input to the predict method of the dlnetwork object must be single data type.

  • Code generation for dlarray indexing:

    • If you set dlY(idx1,...,idxn) = dlX, then dlY and dlX must be assignment compatible.

      • Size of the data must not change. Out-of-bounds assignment operation is not supported.

      • The assignment statement cannot add or drop U labels.

    • Code generation does not support deleting of parts of a dlarray object by using dlX(idx1,…,idxn) = [].

    • Indexed assignment into a dlarray object inside a parfor loop is not supported. Extract the underlying data using extractdata, perform the assignment on the numeric array, and then wrap the result in a new dlarray object.

See Also

Objects

Topics