Main Content

Generate Generic C/C++ Code for Deep Learning Networks

R2026b

With MATLAB® Coder™, you can generate generic C or C++ code for prediction from an already trained neural network. The generated C/C++ code does not depend on third-party libraries. The generated code implements a neural network with the architecture, layers, and parameters specified in the input dlnetwork (Deep Learning Toolbox) object. See Networks and Layers Supported for Code Generation.

Generate code by using one of these methods:

  • The standard codegen command for C/C++ code generation from MATLAB code.

  • The MATLAB Coder app.

Requirements

  • On Windows®, code generation for deep learning networks with the codegen function requires Microsoft® Visual Studio® or the MinGW® compiler.

  • MATLAB Coder Interface for Deep Learning. To install this support package, select it from the MATLAB Add-Ons menu.

  • Deep Learning Toolbox™.

Code Generation by Using codegen

  1. Write an entry-point function in MATLAB that:

    You can pass numeric inputs directly to the predict function without creating a dlarray object. To specify dimension labels for the network input and output, use the InputDataFormats and OutputDataFormats name-value arguments of the predict function. (since R2026b)

    For example:

    function out = my_predict(in) %#codegen
    
    persistent mynet;
    
    if isempty(mynet)
        mynet = coder.loadDeepLearningNetwork("myNetwork.mat");
    end
    
    out = predict(mynet, in, InputDataFormats="SSCB"); 

  2. Create a deep learning configuration object dlconfig that is configured for generating generic C/C++ code by using the coder.DeepLearningConfig function.

    dlconfig = coder.DeepLearningConfig(TargetLibrary="none");

    Create a code generation configuration object for MEX, executable, or static or dynamically linked library. To produce generic C++ code, in your code generation configuration object, set the TargetLang parameter to "C++". Set the DeepLearningConfig parameter to the previously created object dlconfig.

    cfg = coder.config("lib");
    cfg.TargetLang = "C++";
    cfg.DeepLearningConfig = dlconfig;
  3. Create a single-precision input by using coder.typeof. You can also use half-precision inputs by setting cfg.DeepLearningConfig.ComputePrecision = "FP16". Run the codegen command. Use the -config option to specify the configuration object. Use the -args option to specify the size and data type of the input.

    myInput = coder.typeof(single(0), [28 28 1 4]);
    codegen -config cfg my_predict -args {myInput} -report

Code Generation by Using the MATLAB Coder App

  1. Follow the usual steps for specifying the entry-point function and specifying input types. See Generate Deployable Standalone Code by Using the MATLAB Coder App.

  2. In the Generate Code step:

    • Set Language to either C or C++.

    • Click Settings. In the Deep Learning pane, set Deep learning library to None.

      Set deep learning target library to none

  3. Generate code.

Relocating DNN Constants

When you generate generic C/C++/CUDA deep learning code, the code generator writes the large constants for a deep neural network (DNN) to binary data files instead of embedding the constants in the generated code. To customize this behavior, set the configuration parameters LargeConstantGeneration and LargeConstantThreshold.

By default, the generated application looks for the binary data files in the codegen folder. If you are relocating the generated application and data files to a different location such as an embedded board, create an environment variable called CODER_DATA_PATH, whose value is the location of the relocated data files. The generated application will then look for the data files in this location.

For an example of this functionality, see Generate Code for a Deep Learning Network for x86-64 Platforms Using Advanced Vector Instructions.

See Also

| |

Topics