dlarray Limitations for Code Generation
Recommended Usage
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® as shown in this
code.
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
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
Udimension of adlarrayobject must be of fixed size.If the
dlarraydata formatfmtcontains only one character, the corresponding data arrayXcan have only one variable-size dimension. All other dimensions ofXmust be singleton.For operations between a
dlarrayobject and a numeric array that might implicitly expand either operands, do not combine a fixed sizeUdimension of thedlarrayobject with a variable-size dimension of the numeric array.For unary operations such as
max,min, andmeanon a variable-sizedlarrayobject, specify the intended working dimension explicitly as a constant value. See Incompatibility with MATLAB for Default Dimension Selection.
Limitations
For deep learning arrays, code generation has the following limitations:
The data format argument of the
dlarrayobject 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
dlarrayobject. The size is always displayed as1x1.
In MATLAB,
dlarrayenforces the order of labels'SCBTU'. This enforcement eliminates ambiguous semantics in operations, which implicitly match labels between inputs. This behavior is mimicked during MEX code generation. However, for standalone code generation such as static, dynamic libraries, or executables, the data format follows the specification of thefmtargument of thedlarrayobject. As a result, if the input or output of an entry-point function is adlarrayobject and its order of labels is not'SCBTU', then the data layout will be different between the MATLAB environment and standalone code.For example, consider a function
foowith adlarrayobject as an output.function dlA = foo() rng default dlA = dlarray(rand(5,4), 'BC'); end
In MATLAB,
dlAis4(C)-by-5(B).dlA = 4(C) × 5(B) dlarray 0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 0.2785 0.5469 0.9575 0.9649 0.1576 0.9706 0.9572 0.4854 0.8003 0.1419 0.4218 0.9157 0.7922 0.9595For standalone code generation,
dlAis5(B)-by-4(C).For code generation, the
dlarrayinput to thepredictmethod of thedlnetworkobject must besingledata type.
See Also
Objects
Topics
- Generate Digit Images on NVIDIA GPU Using Variational Autoencoder
- Code Generation for dlarray
- Define Custom Training Loops, Loss Functions, and Networks (Deep Learning Toolbox)
- Train Network Using Custom Training Loop (Deep Learning Toolbox)
- Make Predictions Using dlnetwork Object (Deep Learning Toolbox)