Automatic rewriting conversion method from function neural network operator / model to C code

By defining types and an automated translator in the Coq theorem prover, the incompatibility between tensor shapes and parameter constraints in deep learning frameworks is resolved, enabling automated conversion from function neural network operators/models to C code, and improving the detection capabilities and code generation efficiency during the compilation phase.

CN121523680APending Publication Date: 2026-02-13NANJING UNIV OF AERONAUTICS & ASTRONAUTICS
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511555127.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-29
Publication Date
2026-02-13

AI Technical Summary

Technical Problem

Existing deep learning frameworks struggle to detect incompatibility issues related to tensor shape and parameter constraints during the compilation phase, and existing methods lack automation in the conversion process from function neural network operators/models to C code, relying on manual derivation.

Method used

The Coq theorem prover defines finite sets, vectors, data, paths, expressions, and environment types. An automated translator generates intermediate imperative combinators and converts them into C code, enabling compatibility testing of tensor shape and parameter constraints and automated code generation.

Benefits of technology

The compiler automatically detects incompatibility issues related to tensor shape and parameter constraints during the compilation phase, generating highly reliable C code, lowering the development threshold and improving compilation efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121523680A_ABST
    Figure CN121523680A_ABST
Patent Text Reader

Abstract

The invention discloses an automatic rewriting conversion method from a function neural network operator / model to a C code, and belongs to the field of deep learning. A deep learning framework with tensor shape safety is constructed in a Coq theorem prover, and a corresponding C code is automatically generated. A neural network operator or a neural network model defined by the method can detect the tensor shape incompatibility problem and the parameter limitation incompatibility problem in the compiling stage, meanwhile, the C code is automatically generated, time-consuming and tedious manual derivation are avoided, and the development threshold is lowered.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of deep learning technology, and in particular to an automatic rewriting conversion method from function neural network operators / models to C code. Background Technology

[0002] As the scale of neural network models continues to expand, the complexity of deep learning frameworks also increases, leading to a significant rise in the risk of runtime errors. Among various error types, tensor shape errors are the most common. Taking the mainstream framework TensorFlow as an example, tensor shape errors account for 45% of all errors. However, current mainstream deep learning frameworks such as PyTorch and TensorFlow are built on the dynamically typed language Python, which lacks static checking mechanisms. This makes it difficult to detect potential tensor shape errors during the compilation phase, forcing developers to debug based on error messages thrown by the program at runtime. With the increasing cost of training, this significantly wastes time.

[0003] Currently, there are two main approaches to solving the tensor shape error problem in deep learning frameworks: one is to analyze the problem within the existing deep learning framework; the other is to rebuild the deep learning framework based on strongly typed functional languages. The approach of analyzing the problem within the existing framework, such as PyTea for PyTorch and Pythia for TensorFlow, struggles to effectively handle tensor dimension changes, complex loops, and unmodeled API operations, offering only partial tensor shape error detection capabilities. The approach of rebuilding the deep learning framework based on strongly typed functional languages, such as the DeepDS framework based on Scala, fails to fully verify the safety of internal operators and is difficult to formally verify. The ALT framework based on the Coq theorem prover has low automation, requiring manual intervention for complex recursive verification.

[0004] DPIA is a purely functional technique that generates efficient, parallel, low-level imperative code from high-level functional code. However, existing methods can only identify incompatibility issues between adjacent layers but not incompatibility issues related to parameter constraints. Furthermore, their rewrite transformation methods rely on manual derivation, lack automation methods, and make it difficult to directly convert defined neural network operators / models into C code. Summary of the Invention

[0005] This invention provides an automatic rewriting conversion method from function neural network operators / models to C code, in order to solve the problems of incompatibility due to parameter limitations and the reliance on manual derivation in the conversion process in the prior art.

[0006] An embodiment of the present invention provides an automatic rewriting conversion method from a function neural network operator / model to C code, including the following steps:

[0007] S1: Define a finite set type, a vector type, and a finite set conversion function in the Coq theorem prover;

[0008] S2: Define a data type, a data legality function, a path type, an expression type, an accessor type, an environment type, a command type, and an access function in the Coq theorem prover;

[0009] S3: Define a tensor type in the Coq theorem prover, and define a neural network operator and a neural network model;

[0010] S4: In the Coq theorem prover, transform the neural network operator and neural network model code defined in step S3 into intermediate imperative combinators, and then translate the intermediate imperative combinators into C code.

[0011] Optionally, in an embodiment of the present invention, step S1 specifically includes:

[0012] Define the finite set type fin m through a subset type, where it contains all natural numbers less than m, used to represent bounded indices to ensure the validity of the indices;

[0013] Define the finite set conversion function nat2fin, which is used to convert the natural number i and the proof i < m into a value of type fin m;

[0014] Define the vector type vector A n, where A represents the vector element type and n represents the vector length;

[0015] Define the constructor vector function v_optmk by processing index dependencies layer by layer and aggregating the results, to construct an optional vector with a mapping from a finite index to an optional value as a parameter.

[0016] Optionally, in an embodiment of the present invention, step S2 specifically includes:

[0017] Define the data type data, which is used to represent two basic types of data that the program may process: scalar numerical values and arrays with a fixed length and an internal data type;

[0018] Define the data legality function optdata, which is used to judge whether a data type contains an empty array at compile time, and returns an option data type to explicitly represent its validity;

[0019] Define the path type path, which is used to define a specific element in a nested data structure, and supports path combination through variable names and array indices;

[0020] Define an expression type exp that depends on the data type, used to map the data type to its specific expression type in the program;

[0021] Define an accessor type `acc` that depends on the data type, used to construct a corresponding accessor type for each data type, and to safely define and obtain an expression of type `exp` in a given environment;

[0022] Define the environment type env from path type to numeric expression type, and use the option constructor to explicitly indicate whether each address has been defined;

[0023] Define the command `comm` that maps from environment to environment, used for command execution and program state transitions;

[0024] Define an access function `access`, which first checks if the target data type is valid, and then calls the helper function `access_aux` to recursively search for an expression of the corresponding type in the environment based on the accessor.

[0025] Optionally, in one embodiment of the present invention, step S3 specifically includes:

[0026] Tensor types are defined by nesting exp types;

[0027] Neural network operators are defined based on expression type algorithms and tensor types, and then neural network operators are combined to form a neural network model.

[0028] Optionally, in one embodiment of the present invention, step S4 specifically includes:

[0029] Define an assignment translator A that accepts a pair of accessors and an expression and returns a command that assigns the value of the expression to the memory location specified by the accessor.

[0030] Define an expression translator C that takes an expression and a function to convert the expression to a command, and returns a command that generates subsequent operations by applying the expression to the remaining functions to convert the expression to a command.

[0031] This invention presents an automatic rewriting method for converting function neural network operators / models into C code. It constructs a tensor-shape-safe deep learning framework within the Coq theorem prover and automatically generates the corresponding C code. The neural network operators or models defined by this method can detect tensor shape incompatibility and parameter constraint incompatibility issues during the compilation phase. Simultaneously, the automated C code generation eliminates the time-consuming and tedious manual derivation, lowering the development threshold. This lays a solid foundation for building highly reliable AI compilers.

[0032] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0033] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein:

[0034] Figure 1 A flowchart illustrating an automatic rewriting conversion method from function neural network operators / models to C code according to an embodiment of the present invention;

[0035] Figure 2 This is a framework diagram of the automatic rewriting conversion method from function neural network operators / models to C code according to an embodiment of the present invention;

[0036] Figure 3 The translation rules of translator A, which have undergone formal verification, are as follows, according to an embodiment of the present invention;

[0037] Figure 4 The translation rules of the formally verified translator C in this embodiment of the invention;

[0038] Figure 5 The translation rules for intermediate imperative combinators are directly defined in this embodiment of the invention;

[0039] Figure 6 This invention provides an automatic control strategy for converting function neural network operators / models into C code using translation rules. Detailed Implementation

[0040] Embodiments of the present invention are described in detail below, examples of which are illustrated in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain the present invention, and should not be construed as limiting the present invention.

[0041] like Figure 1 and Figure 2 As shown, this automatic rewriting conversion method from function neural network operators / models to C code includes the following steps:

[0042] S1: Define the finite set type, vector type, and finite set conversion function in the Coq theorem prover.

[0043] Preferably, a finite set type fin m is defined through a subset type, which contains all natural numbers less than m, to represent a bounded index and ensure the validity of the index.

[0044] Preferably, a function nat2fin is defined to convert a natural number i and a proof i < m into a value of type fin m, implementing the wrapping of a safe index.

[0045] Optionally, a vector type vector A n is defined, where A represents the type of vector elements and n represents the vector length.

[0046] Preferably, a constructor vector function v_optmk is defined by processing index dependencies layer by layer and aggregating results, implementing the construction of an optional vector with a mapping from a finite index to an optional value as a parameter, ensuring that the construction is successful only when all elements actually exist, and avoiding logical errors or out-of-bounds access that may occur in manual construction.

[0047] S2: Define data types, data legality functions, path types, expression types, accessor types, environment types, command types, and access functions in the Coq theorem prover.

[0048] Optionally, a data type data is inductively defined to represent two basic data that a program may process: scalar numerical values and arrays with a fixed length and an internal data type.

[0049] Preferably, a data legality function optdata is recursively defined to determine at compile time whether a data type contains an empty array and return an option data type to explicitly represent its validity. This mechanism strictly distinguishes between valid data and invalid data (such as empty arrays) at the type level, preventing access to invalid memory regions from the source and enhancing memory safety.

[0050] Optionally, a path type path is inductively defined to define a specific element in a nested data structure, supporting path combination through variable names and array indices.

[0051] Optionally, an expression type exp that depends on the data type is recursively defined to map the data type to its specific expression type in the program.

[0052] Optionally, an accessor type acc that depends on the data type is recursively defined to construct a corresponding accessor type for each data type, used to safely define and obtain an expression of type exp in a given environment.

[0053] Preferably, an environment type env from the path type to the numerical expression type is defined, using the option constructor to explicitly represent whether each address is defined. This design completely eliminates the implicit behavior of undefined variables through function mapping and the option type, placing all possible undefined states under the strict supervision of the type system, significantly enhancing the reliability of the program.

[0054] Optionally, a command comm is defined as an environment-to-environment mapping, used for command execution, which is the transition of program state.

[0055] Preferably, a safe access function `access` is defined. It first checks if the target data type is valid, then calls the helper function `access_aux` to recursively search for the corresponding type expression in the environment based on the accessor. This function integrates type validity checking and path lookup, and its return type `option(exp d)` explicitly encapsulates both success and failure states, serving as a core guarantee for achieving safe memory access and preventing runtime out-of-bounds errors.

[0056] S3: Define tensor types, neural network operators, and neural network models in the Coq theorem prover.

[0057] Optionally, tensor types can be defined by nesting exp types.

[0058] Optionally, neural network operators are defined based on the expression type algorithm and tensor type, and then neural network operators are combined to form a neural network model.

[0059] S4: In the Coq theorem prover, the neural network operators and neural network model code defined in step S3 are transformed into intermediate imperative combinatorials, and then the intermediate imperative combinatorials are translated into C code.

[0060] In embodiments of the present invention, the above process is completed automatically through a Translated strategy.

[0061] Optionally, an assignment translator A is defined to accept a pair of accessors and an expression and return a command that performs the operation of assigning the value of the expression to the memory location specified by the accessor.

[0062] Optionally, an expression translator C is defined that takes an expression and a function to convert the expression to a command, and returns a command that generates subsequent operations by applying the expression to the remaining function to convert the expression to a command.

[0063] Preferred, such as Figure 3 and Figure 4 As shown, the semantic correctness of the translation rules of translators A and C is formally verified in Coq, ensuring that the generated command lines are consistent with the source program. Figure 1 The reliability of the translation process is guaranteed through the theorem proof, eliminating potential logical errors.

[0064] Optional, such as Figure 5As shown, a set of translation rules for intermediate imperative combinators is defined to convert high-level functional operations (such as mapping and folding) into low-level imperative code structures.

[0065] Preferred, such as Figure 6 As shown, an automatic translation framework is implemented, including multiple strategies (such as default_fold, Arw, CGrw, etc.) for handling array operations, control flow, environment parameters, and other specifications, and automating code generation. This framework achieves efficient and accurate translation from functional to imperative programs by integrating dedicated strategies and specification processors.

[0066] The automatic rewriting conversion method from function neural network operators / models to C code according to the present invention will be described in detail below through a specific embodiment.

[0067] Taking the LeNet-5 model as an example, the following explains its definition and C code generation.

[0068] S1: Define the finite set type, vector type, and finite set conversion function in the Coq theorem prover.

[0069] The finite set type fin is defined as:

[0070] Definition fin(m:nat):Set:={i:nat|i <m}.

[0071] Definition nat2fin{mi}(H:i <n):finm:=exist_iH.

[0072] The finite set type conversion function nat2fin is defined as follows:

[0073] Definition nat2fin{ni}(H:i <n):finn:=exist_iH.

[0074] The vector type is defined as:

[0075] Fixpoint vector(A:Set)(n:nat):Set:=match n with

[0076] |O=>unit|Sn=>((vecotrAn)*A)%type end.

[0077] The vector type v_optmk is defined as follows:

[0078]

[0079] S2: Define data types, data validity functions, path types, expression types, accessor types, environment types, command types, and access functions in the Coq theorem prover.

[0080] The data type `data` is defined as follows:

[0081] Inductive data:Set:=|num:data|arym(n:fle m)(d:data):data.

[0082] The data validity check function optdata is defined as follows:

[0083]

[0084] The path type is defined as:

[0085] Inductive path:=|Var:string->path|Ary(n:nat)(i:fin n):path->path.

[0086] The expression type exp is defined as follows:

[0087] Fixpoint exp(d:data):Set:=match d with|num=>R|aryn d=>vector(expd)n end.

[0088] The accessor type acc is defined as follows:

[0089] Fixpoint acc(d:data):Set:=match d with|num=>path|aryn d=>vector(acc d)n end.

[0090] The environment type env is defined as:

[0091] Definition env:=path->option(exp num).

[0092] The command type comm is defined as:

[0093] Definition comm:=env->env.

[0094] The access function is defined as follows:

[0095]

[0096] S3: Define the tensor types in the Coq theorem prover, and then define the neural network operators and neural network models. The one-dimensional / two-dimensional tensors Tensor1 / Tensor2 are defined as follows:

[0097] Definition Tensor1(m:nat):=exp(ary′{m}num).

[0098] Definition Tensor2(mn:nat):=exp(ary′{m}(ary′{n}num))

[0099] The two-dimensional convolution operator Conv2d is defined as follows:

[0100]

[0101]

[0102] When using the function Conv2d, it is necessary to explicitly provide proofs for n1≤m1+2*p and n2≤m2+2*p in order to avoid potential out-of-bounds issues during calculation.

[0103] The LeNet-5 neural network model is defined as follows:

[0104]

[0105] The definition process generates objectives to be proven, such as 5≤32+2*0, 0<2, and 5≤((32+2*0-5) / 1+1) / 2+2*0, which are then proven using the cbv;lia strategy. This avoids errors in the parameters of a certain operator in a certain layer of the neural network model.

[0106] S4: In the Coq theorem prover, the neural network / model code defined in step S3 is transformed into an intermediate imperative combinatorial unit, which is then translated into C code. The entire process is completed automatically using the Translated strategy.

[0107] Define assignment translator A as follows:

[0108] Definition A{d:data}(val:(acc d)*(expd)):comm:=fst val|:=snd val.

[0109] The expression translator C is defined as follows:

[0110] Definition C{d:data}(val: (expd)*(exp d->comm)):comm:=(snd val)(fstval)

[0111] The automatic translation strategy is as follows:

[0112] default_fold: Expanding it can be directly mapped to a higher-order functional definition of an imperative primitive.

[0113] Arw: Parses array operation reductions of the form CG_comm(A(_,_)).

[0114] Crw: Parses control flow reduction in the form CG_Comm(C(_,_)).

[0115] CGenvrw: Handles specifications containing environment parameters such as CG_Comm(fun eta:env=>_eta).

[0116] CGrw: General CG_Comm reduction processing, delegating complex subtargets to dedicated strategies.

[0117] Accrw: Translates the memory access operation specification CG_acc.

[0118] Exprw: Transformation expression computation reduction CG_exp.

[0119] applypsrw: The parsing process applies the specification applyps.

[0120] strnat_simpl: Simplifies strings and natural numbers / booleans.

[0121] default_rewrite: Rewrites the applicable definitions in the proof context to the current target.

[0122] CGrws: Iteratively processes all CG_Comm specifications.

[0123] AErws: Jointly handles the CG_acc and CG_exp reduction.

[0124] strSimpl: Unifies the handling of applyps, writeNat, and writeBool related specifications.

[0125] Translated: Top-level strategy: Coordinates sub-strategies to complete code generation.

[0126] The process of translating the LeNet-5 neural network model into C code using this automated framework is as follows:

[0127]

[0128] The following is an excerpt of the output:

[0129]

[0130]

[0131]

[0132] The automatic rewriting and conversion method from function neural network operators / models to C code proposed in this invention constructs a tensor shape-safe deep learning framework in the Coq theorem prover and automatically generates the corresponding C code. The neural network operators or models defined by this method can detect tensor shape incompatibility and parameter constraint incompatibility issues during the compilation stage. Simultaneously, the automatic generation of C code eliminates the time-consuming and tedious manual derivation, lowering the development threshold. This lays a solid foundation for building highly reliable AI compilers.

[0133] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0134] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this invention, "N" means at least two, such as two, three, etc., unless otherwise explicitly specified.

[0135] Any process or method description in the flowchart or otherwise herein can be understood as representing a module, segment, or portion of code comprising one or N executable instructions for implementing custom logic functions or processes, and the scope of preferred embodiments of the invention includes additional implementations in which functions may be performed not in the order shown or discussed, including substantially simultaneously or in reverse order depending on the functions involved, as will be understood by those skilled in the art to which embodiments of the invention pertain.

Claims

1. A method for automatically rewriting and converting function neural network operators / models into C code, characterized in that, It includes the following steps: S1: Define a finite set type, a vector type, and a finite set conversion function in the Coq theorem prover; S2: Define a data type, a data legality function, a path type, an expression type, an accessor type, an environment type, a command type, and an access function in the Coq theorem prover; S3: Define a tensor type in the Coq theorem prover, and define neural network operators and neural network models; S4: In the Coq theorem prover, transform the neural network operator and neural network model code defined in step S3 into intermediate imperative combinators, and then translate the intermediate imperative combinators into C code.

2. The method according to claim 1, characterized in that, Specifically, step S1 includes: Define the finite set type fin m through a subset type, where it contains all natural numbers less than m, used to represent bounded indices and ensure the validity of indices; Define the finite set conversion function nat2fin, which is used to convert the natural number i and the proof i < m into a value of type fin m; Define the vector type vector A n, where A represents the vector element type and n represents the vector length; Define the constructor vector function v_optmk by processing index dependencies layer by layer and aggregating results, and implement constructing an optional vector with a mapping from finite indices to optional values as parameters.

3. The method according to claim 1, characterized in that, Specifically, step S2 includes: Define the data type data, which is used to represent two basic data that the program may process: scalar numerical values and arrays with a fixed length and an internal data type; Define the data legality function optdata, which is used to judge whether a data type contains an empty array at compile time and returns an option data type to explicitly represent its validity; Define the path type path, which is used to define a specific element in a nested data structure and supports path combination through variable names and array indices; Define the expression type exp that depends on the data type, which is used to map the data type to its specific expression type in the program; Define the accessor type acc that depends on the data type, which is used to construct a corresponding accessor type for each data type and is used to safely define and obtain an expression of type exp in a given environment; Define the environment type env from the path type to the numerical expression type, and use the option constructor to explicitly represent whether each address is defined; Define the command comm that maps from one environment to another, which is used for command execution and the transformation of the program state; Define the access function access, which is used to first check whether the target data type is valid, and then call the auxiliary function access_aux to recursively find the corresponding type of expression in the environment according to the accessor.

4. The method according to claim 1, characterized in that, Specifically, step S3 includes: Define the tensor type through a nested exp type; Define neural network operators according to the expression type algorithm and the tensor type, and then combine the neural network operators into a neural network model.

5. The method according to claim 1, characterized in that, Specifically, step S4 includes: Define the assignment translator A, which is used to accept a pair of an accessor and an expression and return a command to perform the operation of assigning the expression value to the memory location specified by the accessor; Define an expression translator C that takes an expression and a function to convert the expression to a command, and returns a command that generates subsequent operations by applying the expression to the remaining functions to convert the expression to a command.