Model code generation method and system based on computational graph

Through the model code generation method and system based on computational graph, the problems of insufficient flexibility and adaptability in the existing technology are solved, efficient and accurate model development and code generation are achieved, development efficiency is improved, and costs are reduced.

CN120255872BActive Publication Date: 2025-09-30启元实验室
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510748600.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-06
Publication Date
2025-09-30
Estimated Expiration
2045-06-06

AI Technical Summary

Technical Problem

Existing model code generation methods lack flexibility and adaptability when processing complex deep learning models, making it difficult to meet diverse model requirements. In addition, computational graph visualization tools lack interactive editing capabilities, resulting in inefficient development and frequent errors.

Method used

A computational graph-based model code generation method and system is provided. The target computational graph is created through a visual interface, the computational graph information is parsed, and mapped to the target model structure to generate the corresponding model code. The system includes computational graph visual editing, parsing and verification, structure mapping, and code generation modules to ensure the legality and logic of the computational graph.

Benefits of technology

It improves the efficiency and accuracy of model development, reduces development costs, enhances adaptability and flexibility to complex model structures, reduces the possibility of errors introduced by code modifications, and achieves a close connection between visual editing of computational graphs and code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120255872B_ABST
    Figure CN120255872B_ABST
Patent Text Reader

Abstract

The present invention provides a method and system for generating model code based on a computational graph, and relates to the technical field of model code generation. The model code generation method includes: responding to a user creation instruction, creating a target computational graph based on a visual interface; parsing the computational graph information of the target computational graph; mapping the graph structure of the target computational graph to the target structure of the target model based on the computational graph information; and generating corresponding model code based on the target structure of the target model. The present invention can realize interaction with the user based on a visual interface, thereby realizing visual editing of the computational graph. The present invention can accurately generate the model code of the target model based on the computational graph created by the user. The present invention can be closely linked with code generation through visual editing, thereby improving the efficiency and accuracy of model development, reducing development costs, and enhancing the flexibility of code generation and adaptability to complex model structures.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of model code generation, and in particular to a method and system for generating model code based on a computational graph. Background Art

[0002] With the rapid development of deep learning technology, the structure of deep learning models is becoming increasingly complex. In the field of deep learning model development, model code generation is an important research direction. Its goal is to reduce the workload of manual coding through automation, lower the development threshold, and reduce human errors.

[0003] Currently, existing model code generation methods mainly include rule-based code generation methods and template-based code generation methods.

[0004] The rule-based code generation method can convert a certain model representation (such as a computational graph, intermediate representation, etc.) into target code through pre-set conversion rules. However, this method has poor adaptability to complex model structures. When the computational logic or structure of the model changes, the rules often need to be redesigned or adjusted, otherwise the generated code may be incorrect or unable to run. The template-based code generation method uses pre-designed code templates (such as code snippets for specific network layers) to generate model code. Although this method improves development efficiency to a certain extent, it lacks flexibility and is difficult to cope with diverse model requirements. In addition, a large number of different templates need to be created and maintained for different deep learning frameworks, which increases development and maintenance costs.

[0005] Furthermore, regarding computational graph visualization, existing visualization tools primarily provide static displays of model structures and lack the ability to dynamically edit computational graphs. When developers build or adjust models, if they need to modify the computational graph (such as adding, removing, or adjusting network layers), they typically need to manually modify the underlying code, which is inefficient and prone to introducing errors. Furthermore, existing visualization tools are loosely integrated with the code generation process, preventing real-time synchronization between visual editing and code generation. This makes seamless development difficult, hindering development efficiency.

[0006] The contents of the background technology section are merely technologies known to the public and do not necessarily represent the existing technologies in this field. Summary of the Invention

[0007] The present invention provides a method and system for generating model code based on a computational graph, aiming to solve at least one of the above-mentioned technical problems.

[0008] According to one aspect of the present invention, the present invention provides a model code generation method based on a computational graph, comprising: responding to a user creation instruction, creating a target computational graph based on a visual interface; parsing the computational graph information of the target computational graph; based on the computational graph information, mapping the graph structure of the target computational graph to the target structure of the target model; and generating corresponding model code based on the target structure of the target model.

[0009] According to some embodiments of the present invention, the user creation instruction includes an edge creation instruction, and the model code generation method further includes: determining whether the edge creation instruction complies with a preset computational graph creation rule; if not, outputting a first prompt message on the visual interface.

[0010] According to some embodiments of the present invention, after parsing the computational graph information of the target computational graph, the model code generation method further includes: verifying whether the logic of the target computational graph is reasonable; if not, outputting a second prompt information on the visual interface.

[0011] According to some embodiments of the present invention, mapping the graph structure of the target computation graph to the target structure of the target model based on the computation graph information includes: mapping each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method.

[0012] According to some embodiments of the present invention, generating corresponding model code based on a target structure of a target model includes: combining various objects in the target structure into executable model code according to grammatical rules of the target model.

[0013] According to another aspect of the present invention, a model code generation system based on a computational graph is provided, comprising a computational graph visual editing module, a computational graph parsing and verification module, a model structure mapping module, and a model code generation module. The computational graph visual editing module responds to user creation instructions and creates a target computational graph based on a visual interface. The computational graph parsing and verification module parses the computational graph information of the target computational graph. The model structure mapping module maps the graph structure of the target computational graph to the target structure of the target model based on the computational graph information. The model code generation module generates corresponding model code based on the target structure of the target model.

[0014] According to some embodiments of the present invention, the user creation instruction includes an edge creation instruction, and the computation graph visualization editing module determines whether the edge creation instruction complies with the preset computation graph creation rules; if not, the computation graph visualization editing module outputs a first prompt information on the visualization interface.

[0015] According to some embodiments of the present invention, the computation graph parsing and verification module verifies whether the logic of the target computation graph is reasonable; if not, the computation graph parsing and verification module outputs a second prompt message on the visual interface.

[0016] According to some embodiments of the present invention, the model structure mapping module maps each node in the target computation graph to a corresponding structure of the target model based on a corresponding implementation.

[0017] According to some embodiments of the present invention, the model code generation module combines various objects in the target structure into executable model code according to the grammatical rules of the target model.

[0018] According to yet another aspect of the present invention, an electronic device is provided. The electronic device includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to implement the method described above.

[0019] According to another aspect of the present invention, a non-volatile computer-readable storage medium is provided, wherein a computer program is stored on the storage medium, and when the computer program is executed by a processor, the method described above can be implemented.

[0020] According to another aspect of the present invention, a computer program product is provided, which includes a computer program stored on a computer-readable storage medium; the computer program includes program instructions, which, when executed by a computer, cause the computer to execute the method described above.

[0021] Beneficial effects

[0022] The present invention provides a model code generation method based on a computational graph. The present invention can respond to user creation instructions, create a target computational graph based on a visual interface, and then parse the computational graph information of the target computational graph. Based on the computational graph information, the graph structure of the target computational graph can be mapped to the target structure of the target model, and the corresponding model code can be generated based on the target structure of the target model.

[0023] The present invention enables user interaction based on a visual interface, thereby enabling visual editing of computational graphs. It accurately generates model code for a target model based on the computational graph created by the user. By tightly integrating visual editing with code generation, the present invention improves the efficiency and accuracy of model development, reduces development costs, and enhances the flexibility of code generation and its adaptability to complex model structures. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0025] Figure 1 A schematic diagram showing a flow chart of a method for generating model code according to an embodiment of the present invention;

[0026] Figure 2 Another schematic flow chart showing the model code generation method according to an embodiment of the present invention;

[0027] Figure 3 Another schematic flow chart showing the model code generation method according to an embodiment of the present invention;

[0028] Figure 4 A structural diagram of a model code generation system according to an embodiment of the present invention is shown.

[0029] Description of reference numerals:

[0030] Model code generation system 1; computation graph visualization editing module 10; computation graph parsing and verification module 20; model structure mapping module 30; model code generation module 40. DETAILED DESCRIPTION

[0031] The following provides a clear and complete description of the technical solutions of the present invention in conjunction with the accompanying drawings of the embodiments of the present invention. It should be understood that the embodiments described are only a portion of the embodiments of the present invention, not all of them. All other embodiments derived by those skilled in the art based on the embodiments of the present invention without inventive effort are intended to fall within the scope of protection of the present invention.

[0032] Specifically, rule-based code generation methods have significant shortcomings when converting complex ONNX (Open Neural Network Exchange) computation graphs into Oneflow (an open source deep learning framework) code. For example, when the ONNX computation graph contains custom operations or combinations of operations with complex data dependencies, existing rules may not accurately map them to the corresponding code structure in Oneflow. This is because rules are typically designed for common operations and simple structures, and their coverage of complex cases is limited. Furthermore, because different deep learning frameworks may have different implementation details for the same operations, rule-based code generation methods are not sufficiently sophisticated in handling these differences. For example, ONNX computation graphs and Oneflow may have different requirements in terms of data type handling, memory management, and parallel computing. Rule-based conversion may not fully account for these factors, resulting in performance issues or errors in the generated Oneflow code at runtime.

[0033] On the other hand, for template-based code generation methods, when Oneflow code needs to be generated, if the template does not cover the specific model structure or operations, the code cannot be generated correctly. For example, for some new neural network architectures or models in specific fields, the template may not meet the requirements. In addition, the maintenance cost of templates is very high. With the continuous updates of deep learning frameworks and the introduction of new features, the templates need to be constantly modified and updated, otherwise the generated code may not be compatible with the new framework version. In terms of flexibility, template-based code generation methods also make it difficult to customize the generated code according to the specific needs of users. For example, users may want to add specific optimization strategies or customized functional modules to the generated code, but the fixed structure of the template limits this flexibility.

[0034] Therefore, current model code generation methods lack flexibility and adaptability to meet the complex and ever-changing needs of model development. Furthermore, computational graph visualization tools lack interactive editing capabilities and cannot support efficient visual modeling.

[0035] According to one aspect of the present invention, the present invention provides a model code generation method based on a computational graph. Figure 1 A flow chart of the model code generation method according to an embodiment of the present invention is shown. Figure 1 As shown, the model code generation method may include steps S100-S400.

[0036] Exemplarily, the model code generation method may be executed by a model code generation system having computing capabilities.

[0037] According to an example embodiment, in step S100 , the model code generation system creates a target computation graph based on a visual interface in response to a user creation instruction.

[0038] For example, the model code generation system can provide users with an intuitive visual interface through which users can input user creation instructions to create a target computational graph according to user requirements.

[0039] A computation graph is a directed graph structure used in deep learning to describe the flow of mathematical operations. It graphically represents the dependencies of the computational process. A computation graph can consist of nodes and edges. Nodes represent operations (such as convolution, pooling, activation functions, and fully connected operations), while edges represent the direction of data flow (such as the input-output relationship between operations).

[0040] The model code generation system can use graphics drawing technology and interactive design through a visual interface to present the nodes and edges of the created target computational graph in a graphical form based on user creation instructions.

[0041] For example, in the target computation graph, different computing operations can be distinguished by different icons or shapes, key parameter information of related computing operations can be marked at the nodes, and arrows can be used to indicate the direction of data flow.

[0042] According to an example embodiment, the user creation instruction includes at least operation instructions such as a node creation instruction, an edge creation instruction, a parameter configuration instruction, and a node dragging instruction.

[0043] For example, the model code generation system can provide users with a visual interface through which they can interact. For example, within this visual interface, users can use interactive tools (such as a mouse) to input user creation instructions to add, delete, or modify nodes and edges accordingly. After creating a node, the model code generation system can automatically generate default parameters. For existing nodes, the model code generation system can also respond to user parameter configuration instructions, allowing users to modify parameters through a parameter editing window. Furthermore, the model code generation system can respond to node dragging instructions, adjusting the layout of the target computation graph by dragging nodes.

[0044] According to an example embodiment, the target computation graph may be a simple linear model computation graph or a complex convolutional neural network computation graph.

[0045] As an embodiment, when the target computation graph is a simple linear model computation graph, the model code generation system may respond to a user creation instruction and create a simple linear model computation graph based on a visual interface.

[0046] Specifically, the user can select "Input Node" in the operation menu of the visual interface, and enter the node creation instruction through the interactive tool in the editable area to add an input node and label it as "Input". The user can then add a fully connected layer node at the appropriate location. The user can also double-click the fully connected layer node through the interactive tool to enter parameter configuration instructions, so that the node parameters (such as input size, output size, weight and bias parameters) can be set in the pop-up parameter editing window. Finally, the user can also add an "output node" at the appropriate location, connect the input node to the fully connected layer node through an edge, and then connect the fully connected layer node to the output node, thereby completing the creation of a simple linear model calculation graph.

[0047] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, the model code generation system can respond to user creation instructions and create a complex convolutional neural network computation graph based on a visual interface.

[0048] Specifically, users can select "Input Node" in the operation menu of the visual interface, and enter node creation instructions through the interactive tool in the editable area to add convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes in sequence.

[0049] Users can also double-click these nodes using the interactive tool to enter parameter configuration instructions, allowing them to set node parameters in the pop-up parameter editing window. For example, when adding a convolutional layer node, you can configure parameters such as the convolution kernel size (e.g., 3*3), stride (e.g., 1), padding (e.g., 1), and the number of input and output channels. When adding a pooling layer node, you can configure parameters such as the pooling type (e.g., max pooling) and pooling window size (e.g., 2*2). You can also choose the activation function, such as the ReLU (Rectified Linear Unit) function.

[0050] Users can also input node dragging instructions through the interactive tool to adjust the layout of the computational graph by dragging nodes, so that nodes such as convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes can be arranged in a certain order for easy viewing.

[0051] Figure 2 Another flowchart of the model code generation method according to an embodiment of the present invention is shown.

[0052] Alternatively, as Figure 2 As shown, the model code generation method may further include steps S510-520.

[0053] In step S510 , the model code generation system determines whether the edge creation instruction complies with preset computation graph creation rules.

[0054] In step S520 , if not, the model code generation system outputs first prompt information on the visual interface.

[0055] For example, the model code generation system can ensure the legality and validity of the created target computation graph through a verification mechanism. The preset computation graph creation rules may include: when adding an edge to the target computation graph, the data types of the source node and the target node connected by the edge are compatible.

[0056] Exemplarily, when adding the edge of the target computational graph, the model code generation system determines whether the data types of the source node and the target node are compatible in real time to determine whether the computational graph editing behavior is legal and valid, thereby avoiding data mismatches.

[0057] As an embodiment, when the target computation graph is a simple linear model computation graph, when adding an edge connection node of the target computation graph, the model code generation system checks in real time whether the tensor types of the source node and the target node are consistent. If the tensor types of the input node and the output node are the same, the model code generation system determines that the connection is legal and valid; if the tensor types of the input node and the output node are different, the model code generation system outputs a first prompt message on the visual interface to indicate that the user has edited the information incorrectly, thereby effectively avoiding data mismatches.

[0058] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, when adding edges to connect nodes in the target computation graph, the model code generation system checks in real time whether the tensor dimensions between the source and target nodes are consistent. If the tensor dimensions are inconsistent, the model code generation system outputs a first prompt message on the visual interface to indicate that the user has incorrectly edited the information, and then instructs the user to modify the parameters.

[0059] In step S200 , the model code generation system parses the computation graph information of the target computation graph.

[0060] For example, the model code generation system can parse the target computation graph. For example, the model code generation system can traverse each node and edge in the target computation graph, extract the node's operation type, parameter information, and the connection relationship between each node, thereby obtaining legal computation graph information.

[0061] Exemplarily, the target computation graph may be an ONNX computation graph. During the parsing process, the model code generation system may convert the target computation graph into an ONNX standard model representation format for subsequent processing.

[0062] For example, taking the convolution layer node as an example, the model code generation system can parse and obtain its operation type information (such as convolution operation), and extract parameter information such as convolution kernel size, step size and padding, and store this information as computational graph information in the corresponding node object in the internal data structure.

[0063] As an example, when the target computation graph is a simple linear model computation graph, the model code generation system traverses the nodes and edges of the simple linear model computation graph. For input nodes, the model code generation system can parse and obtain their data type information (e.g., a tensor of type float32) and dimension information (e.g., [batch_size, input_size]), and store this information in the corresponding node object in the internal data structure. For fully connected layer nodes, the model code generation system can parse and obtain their weight matrix (e.g., with dimensions [input_size, output_size]), bias vector (with dimensions [output_size]), and other parameter information, and create a node object to store this information. Where float32 is a 32-bit single-precision floating point number, input_size represents the dimension of the input vector, output_size represents the dimension of the output vector, and batch_size represents the number of samples input into the model at one time.

[0064] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, the model code generation system can use a depth-first algorithm to traverse the target computation graph. For example, it can start from the input node and visit each node in turn. For the convolution layer nodes, the model code generation system can parse and obtain parameter information such as its convolution kernel size, step size, padding, and input and output channels, and determine information such as the dimension and value of the convolution kernel weight data. For the pooling layer nodes, the model code generation system parses and obtains information such as the pooling type (such as maximum pooling) and pooling window size. For the activation function layer, the model code generation system parses and obtains its type (such as ReLU function), etc. For the fully connected layer nodes, the model code generation system can parse and obtain its weight matrix (such as the dimension [input_size, output_size]), bias vector (the dimension [output_size]), and other parameter information.

[0065] Figure 3 Another flowchart of the model code generation method according to an embodiment of the present invention is shown.

[0066] Alternatively, as Figure 3 As shown, the model code generation method may further include steps S610-S620.

[0067] In step S610 , the model code generation system verifies whether the logic of the target computation graph is reasonable.

[0068] In step S620 , if not, the model code generation system outputs a second prompt message on the visual interface.

[0069] For example, after parsing the target computation graph, the model code generation system verifies whether the logic of the entire target computation graph is reasonable.

[0070] For example, the model code generation system can verify whether the target computation graph has isolated nodes, circular dependencies, and other issues. If these issues exist, the model code generation system can output a second prompt message in the visual interface, indicating that the current target computation graph has a logical error, prompting the user to make reasonable modifications to the target computation graph.

[0071] As an example, when the target computation graph is a simple linear model computation graph, during the verification process, the model code generation system can check whether the edge connections are correct. For example, whether the output of the input node is correctly connected to the input of the fully connected layer node, whether the output of the fully connected layer node is connected to the input of the output node, etc. Furthermore, the model code generation system can also check whether there are isolated nodes or other logical errors in the entire target computation graph. If the model code generation system finds a logical error (such as an unconnected node or mismatched data type), the model code generation system will provide the user with a second prompt message (such as "The data types between the input node and the fully connected layer node are incompatible, please check") through the visualization page.

[0072] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the verification process, the model code generation system can check the logical structure of the entire target computation graph in addition to checking whether the logic of the edge connection is reasonable and whether the data dimensions match. For example, the model code generation system checks whether there is a circular dependency (for example, the output of a node returns to the node as input after a series of operations). If so, the model code generation system will feedback a second prompt message to the user through a visualization page (such as "There is a circular dependency in the target computation graph, please check"). In addition, the model code generation system can also check whether the parameters of each node are within a reasonable range (for example, the convolution kernel size cannot be a negative number, etc.).

[0073] In step S300 , the model code generation system maps the graph structure of the target computation graph to the target structure of the target model based on the computation graph information.

[0074] For example, the graph structure of the target computation graph may include operations and structures. The model code generation system accurately maps the operations and structures in the target computation graph to the target structure of the target model based on the parsed computation graph information.

[0075] Optionally, in step S300 , the model code generation system maps each node in the target computation graph to a corresponding structure of the target model based on a corresponding implementation method.

[0076] For example, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. For each node in the ONNX computation graph, there is a corresponding implementation in the Oneflow model.

[0077] For example, the model code generation system may map each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method, and the specific steps may include:

[0078] S11: Node type identification: the model code generation system identifies the node type of each node in the target computation graph (such as input layer, convolution layer, activation function layer, etc.);

[0079] S12: Parameter matching, the model code generation system converts the common parameters in the target computation graph (such as the number of channels, kernel size, etc.) into specific parameters of the target structure (i.e., framework) of the target model;

[0080] S13: Establishing connection relationships: The model code generation system establishes data flow relationships between layers in the target structure based on the topological structure of the target computation graph;

[0081] S14: Structural feature matching, the model code generation system processes the specific syntax requirements in the target structure to achieve the mapping of the graph structure of the target computation graph to the target structure of the target model.

[0082] As an example, for a convolutional layer node, the model code generation system can create a oneflow.nn.Conv2d object in the OneFlow model and then initialize the object based on the parsed computational graph information (e.g., parameters such as kernel size, stride, and padding) to map the node to the corresponding structure of the target model. Here, oneflow.nn.Conv2d represents a two-dimensional convolutional layer in the OneFlow model.

[0083] Furthermore, when processing complex computational graph structures (such as subgraphs containing multiple sequential operations or branching computational graphs within the target computational graph), the model code generation system can map them to corresponding structures within the Oneflow model. For example, for a computational graph consisting of convolutional layer nodes, activation function nodes, and pooling operation layer nodes, the model code generation system can create oneflow.nn.Sequential objects in the Oneflow model (including, in sequence, oneflow.nn.Conv2d, oneflow.nn.ReLU, and oneflow.nn.MaxPool2d or oneflow.nn.AvgPool2d objects) to map the model structure to the target computational graph. Among them, oneflow.nn.Sequential is the container module layer used to construct neural networks in the OneFlow model; oneflow.nn.ReLU is the activation function layer; oneflow.nn.MaxPool2d is the two-dimensional maximum pooling operation layer; and oneflow.nn.AvgPool2d is the two-dimensional average pooling operation layer.

[0084] According to example embodiments, during the mapping process, the model code generation system can fully consider the characteristics and limitations of the target model (e.g., the Oneflow model), including data type compatibility, memory management methods, and optimization strategies for specific operations. For example, based on the Oneflow model's data type requirements, the model code generation system can convert and check the data types of input and output tensors, thereby ensuring that the mapped model structure can run efficiently and stably in the target model.

[0085] As an example, if the target computation graph is a simple linear model computation graph, during the mapping process, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. For fully connected layer nodes, the model code generation system can create a oneflow.nn.Linear object in the Oneflow model. The object's weight and bias attributes are then initialized based on the computation graph information (such as the weight matrix and bias vector parameters). The model code generation system then maps the input nodes to the input tensors of the Oneflow model and the output nodes to the output tensors of the Oneflow model. The oneflow.nn.Linear module in the Oneflow model is used to implement linear transformations. "weight" represents the weight, and "bias" represents the bias.

[0086] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the mapping process, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. The model code generation system can combine the various nodes of the target computation graph based on the construction method of the Oneflow model, thereby constructing a complete Oneflow model structure. For example, by creating a oneflow.nn.Sequential object, the model code generation system can sequentially connect the convolution layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes, thereby mapping to form a trainable model.

[0087] In step S400 , the model code generation system generates corresponding model code based on the target structure of the target model.

[0088] For example, based on the structure information of the target structure of the generated target model, the model code generation system can generate model code of a complete target model (such as a Oneflow model).

[0089] Structural information is the structured information obtained after the target computation graph is parsed and mapped. It contains the parameter information required to build the target structure of the target model and serves as a bridge connecting the target computation graph and the final generated model code.

[0090] For example, structural information may include topological information, such as the hierarchical relationships of nodes (e.g., input layer-hidden layer-output layer), the connections between nodes (e.g., serial, parallel, and residual connections), and data flow and dependencies. Structural information may also include node attribute information, such as operation types (e.g., convolution, fully connected operations, and activation functions), parameter configurations (e.g., kernel size, stride, and number of channels), and special properties (e.g., whether weights are shared or whether they are trainable). Structural information may also include framework adaptation information, such as specific requirements for the target architecture, mapping rules, and grammatical constraints. Structural information may also include meta-information, such as identifiers such as model name, version, and author, input and output specifications (e.g., tensor shape and data type), and optimizer configurations (e.g., learning rate and loss function).

[0091] For example, the model code generation system can directly convert the nodes of the target computation graph into standard layers of the target model framework (such as the Oneflow model), while preserving the parameter naming and organizational structure of the target model framework. The model code generation system then predefines code templates for each node type and generates the corresponding model code after filling in the parameters.

[0092] Optionally, in step S400 , the model code generation system combines various objects in the target structure into executable model code according to the grammatical rules (ie, structural information) of the target model.

[0093] Exemplarily, the specific steps of generating the model code may include:

[0094] S21: Templated code generation, the model code generation system predefines a code template for each node type and fills the parameter placeholders in the template according to the node attributes;

[0095] S22: Topological Sorting and Code Organization, the model code generation system generates ordered code snippets based on the dependencies between nodes and organizes the code structure;

[0096] S23: Framework adaptation layer processing, the model code generation system processes the framework-specific syntax requirements of the target structure and inserts necessary initialization code (such as model compilation, parameter initialization, etc.);

[0097] S24: Optimization and post-processing: The model code generation system adds comments and documentation string information to improve readability, formats the code (such as indentation, import statement organization, etc.), and generates auxiliary functions (such as model inference logic) to obtain the corresponding model code.

[0098] For example, for a simple model structure, the generated model code may include necessary import statements such as "import oneflow" and "import oneflow.nn as nn." The model code generation system also defines an "_init_" function to initialize the model's various layer objects and a "forward" function to describe the model's forward propagation process. "import oneflow" and "import oneflow.nn as nn" are statements for the OneFlow model, the "_init_" function is the constructor, and the "forward" function is the forward propagation function.

[0099] Optionally, in step S400 , during the model code generation process, the model code generation system may further add comment information and document string information to improve the readability of the model code.

[0100] For example, in the _init_ function, the model code generation system adds detailed comments to the initialization parameters of each node layer object to explain the meaning and function of the parameters. Docstring information can be used to describe the target model's functionality, the format of input and output data, and the model's construction strategy.

[0101] As an embodiment, when the target computation graph is a simple linear model computation graph, during the model code generation process, the model code generation system generates a complete Oneflow model code based on the structural information of the target structure of the target model (such as the Oneflow model) mapped by the simple linear model computation graph.

[0102] For example, the model code generation system can add the necessary import statements import oneflow and importoneflow.nn as nn. The model code generation system then creates the SimpleLinearModel class (a neural network class), which inherits from nn.Module (the core class used to build and manage neural network models). In the _init_ function, the model code generation system initializes the oneflow.nn.Linear object according to the mapping information and adds annotations to explain the meaning of each parameter. In the forward function, the model code generation system defines the forward propagation of the data, which simply passes the input data through the fully connected layer nodes. The model code generation system can also add documentation string information to describe the function's functionality.

[0103] Exemplarily, the model code generation system may also add document string information at the beginning of the model code to describe information such as the function of the entire model, the format of input and output data, etc.

[0104] Exemplarily, the model code generation system can also generate sample code to instruct users on how to load data, how to train and evaluate models, etc., so that users can quickly understand the generated model code.

[0105] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the model code generation process, the model code generation system generates a complete Oneflow model code based on the structural information of the target structure of the target model (such as the Oneflow model) mapped by the complex convolutional neural network computation graph.

[0106] For example, the model code generation system can add the necessary import statements import oneflow and importoneflow.nn as nn. The model code generation system then creates the SimpleLinearModel class. In the _init_ function, the model code generation system initializes each node object according to the mapped target structure and adds annotations to explain the meaning of each parameter. In the forward function, the model code generation system processes the data through each layer in sequence based on the target model's computational flow and defines the forward propagation of the data.

[0107] The above description uses the Oneflow model as an example to illustrate the graph-computing-based model code method of the present invention. However, the target model in the present invention is not limited to the Oneflow model. For example, based on the same principles as the Oneflow model, the target model can also be other models such as PaddlePaddle (a deep learning programming framework) or PyTorch (a deep learning programming framework), and the present invention does not impose any restrictions on this. The present invention can support code generation for a variety of different framework models.

[0108] Through the above embodiments, the present invention provides a model code generation method based on a computational graph. The present invention can respond to user creation instructions, create a target computational graph based on a visual interface, and then parse the computational graph information of the target computational graph. Based on the computational graph information, the graph structure of the target computational graph can be mapped to the target structure of the target model, and the corresponding model code can be generated based on the target structure of the target model.

[0109] The present invention enables user interaction based on a visual interface, thereby enabling visual editing of computational graphs. It accurately generates model code for a target model based on the computational graph created by the user. By tightly integrating visual editing with code generation, the present invention improves the efficiency and accuracy of model development, reduces development costs, and enhances the flexibility of code generation and its adaptability to complex model structures.

[0110] On the one hand, the present invention uses visual editing to allow developers to create and edit computational graphs directly in a visual interface, eliminating the need for manual code modifications, thereby improving the efficiency of model building and modification. Compared with existing model code generation methods, the present invention can reduce the possibility of errors introduced by code modifications. Moreover, the present invention allows users to directly modify the parameter information of the computational graph through a visual interface, eliminating the need to search for and modify the relevant parameter configuration in the code. This allows the present invention to quickly adjust and optimize the model structure, saving a considerable amount of time and cost.

[0111] On the other hand, the present invention can process complex computational graph structures through computational graph parsing, verification and mapping (for example, for computational graphs containing multiple different types of operations and complex connection relationships, the present invention can accurately convert them into model code of the target model), thereby avoiding errors caused by the complexity of the model in existing model code generation methods and reducing model debugging time and cost.

[0112] According to another aspect of the present invention, the present invention provides a model code generation system based on a computational graph. Figure 4As shown, the model code generation system 1 may include a computation graph visualization editing module 10 , a computation graph parsing and verification module 20 , a model structure mapping module 30 and a model code generation module 40 .

[0113] According to an example embodiment, the computation graph visual editing module 10 creates a target computation graph based on a visual interface in response to a user creation instruction.

[0114] For example, the computation graph visualization editing module 10 may provide a user with an intuitive visualization interface through which the user may input user creation instructions to create a target computation graph according to user requirements.

[0115] A computation graph is a directed graph structure used in deep learning to describe the flow of mathematical operations. It graphically represents the dependencies of the computational process. A computation graph can consist of nodes and edges. Nodes represent operations (such as convolution, pooling, activation functions, and fully connected operations), while edges represent the direction of data flow (such as the input-output relationship between operations).

[0116] The computation graph visualization editing module 10 can present the nodes and edges of the created target computation graph in a graphical form based on user creation instructions by using graphics drawing technology and interactive design through a visualization interface.

[0117] For example, in the target computation graph, different computing operations can be distinguished by different icons or shapes, key parameter information of related computing operations can be marked at the nodes, and arrows can be used to indicate the direction of data flow.

[0118] According to an example embodiment, the user creation instruction includes at least operation instructions such as a node creation instruction, an edge creation instruction, a parameter configuration instruction, and a node dragging instruction.

[0119] For example, the computational graph visual editing module 10 can provide users with a visual interface through which they can perform interactive operations. For example, in this visual interface, users can input user creation instructions using an interactive tool (such as a mouse) to add, delete, or modify nodes and edges accordingly. After creating a node, the computational graph visual editing module 10 can automatically generate default parameters. For existing nodes, the computational graph visual editing module 10 can also respond to user parameter configuration instructions, allowing users to modify parameters through the parameter editing window. Furthermore, the computational graph visual editing module 10 can respond to node dragging instructions, adjusting the layout of the target computational graph by dragging nodes.

[0120] According to an example embodiment, the target computation graph may be a simple linear model computation graph or a complex convolutional neural network computation graph.

[0121] As an embodiment, when the target computation graph is a simple linear model computation graph, the computation graph visualization editing module 10 may respond to a user creation instruction and create a simple linear model computation graph based on a visualization interface.

[0122] Specifically, the user can select "Input Node" in the operation menu of the visual interface, and enter the node creation instruction through the interactive tool in the editable area to add an input node and label it as "Input". The user can then add a fully connected layer node at the appropriate location. The user can also double-click the fully connected layer node through the interactive tool to enter parameter configuration instructions, so that the node parameters (such as input size, output size, weight and bias parameters) can be set in the pop-up parameter editing window. Finally, the user can also add an "output node" at the appropriate location, connect the input node to the fully connected layer node through an edge, and then connect the fully connected layer node to the output node, thereby completing the creation of a simple linear model calculation graph.

[0123] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, the computation graph visualization editing module 10 can respond to a user creation instruction and create a complex convolutional neural network computation graph based on a visualization interface.

[0124] Specifically, users can select "Input Node" in the operation menu of the visual interface, and enter node creation instructions through the interactive tool in the editable area to add convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes in sequence.

[0125] Users can also double-click these nodes using the interactive tool to enter parameter configuration instructions, allowing them to set node parameters in the pop-up parameter editing window. For example, when adding a convolutional layer node, you can configure parameters such as the convolution kernel size (e.g., 3*3), stride (e.g., 1), padding (e.g., 1), and the number of input and output channels. When adding a pooling layer node, you can configure parameters such as the pooling type (e.g., max pooling) and pooling window size (e.g., 2*2). You can also choose the activation function, such as the ReLU (Rectified Linear Unit) function.

[0126] Users can also input node dragging instructions through the interactive tool to adjust the layout of the computational graph by dragging nodes, so that nodes such as convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes can be arranged in a certain order for easy viewing.

[0127] Optionally, the computation graph visualization editing module 10 determines whether the edge creation instruction complies with preset computation graph creation rules.

[0128] If not, the computation graph visualization editing module 10 outputs a first prompt message on the visualization interface.

[0129] For example, the computation graph visualization editing module 10 can ensure the legality and validity of the created target computation graph through a verification mechanism. The preset computation graph creation rules may include: when adding an edge to the target computation graph, the data types of the source node and the target node connected by the edge are compatible.

[0130] Exemplarily, when adding the edge of the target computation graph, the computation graph visualization editing module 10 determines whether the data types of the source node and the target node are compatible in real time to determine whether the computation graph editing behavior is legal and valid, thereby avoiding data mismatch.

[0131] As an embodiment, when the target computation graph is a simple linear model computation graph, when adding edge connection nodes of the target computation graph, the computation graph visualization editing module 10 checks in real time whether the tensor types of the source node and the target node are consistent. If the tensor types of the input node and the output node are the same, the computation graph visualization editing module 10 determines that the connection is legal and valid; if the tensor types of the input node and the output node are different, the computation graph visualization editing module 10 outputs a first prompt message on the visualization interface to indicate that the user has edited the information incorrectly, thereby effectively avoiding data mismatches.

[0132] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, when adding edges to connect nodes in the target computation graph, the computation graph visualization editing module 10 checks in real time whether the tensor dimensions between the source and target nodes are consistent. If the tensor dimensions are inconsistent, the computation graph visualization editing module 10 outputs a first prompt message on the visualization interface to indicate that the user has incorrectly edited the information, and then instructs the user to modify the parameters.

[0133] According to an example embodiment, the computation graph parsing and verification module 20 parses computation graph information of a target computation graph.

[0134] For example, the computation graph parsing and verification module 20 may parse the obtained target computation graph. For example, the computation graph parsing and verification module 20 may traverse each node and edge in the target computation graph, extract the node's operation type, parameter information, and the connection relationship between each node, thereby obtaining legal computation graph information.

[0135] Exemplarily, the target computation graph may be an ONNX computation graph. During the parsing process, the computation graph parsing and verification module 20 may convert the target computation graph into an ONNX standard model representation format for subsequent processing.

[0136] For example, taking the convolution layer node as an example, the computational graph parsing and verification module 20 can parse and obtain its operation type information (such as convolution operation), and extract parameter information such as convolution kernel size, step size and padding, and store this information as computational graph information in the corresponding node object in the internal data structure.

[0137] As an embodiment, when the target computation graph is a simple linear model computation graph, the computation graph parsing and verification module 20 traverses the nodes and edges of the simple linear model computation graph. For input nodes, the computation graph parsing and verification module 20 can parse and obtain its data type information (e.g., a tensor of type float32) and dimension information (e.g., [batch_size, input_size]), and store this information in the corresponding node object in the internal data structure. For fully connected layer nodes, the computation graph parsing and verification module 20 can parse and obtain its weight matrix (e.g., with dimensions of [input_size, output_size]), bias vector (with dimensions of [output_size]), and other parameter information, and create a node object to store this information. Where float32 is a 32-bit single-precision floating point number, input_size represents the dimension of the input vector, output_size represents the dimension of the output vector, and batch_size represents the number of samples input to the model at one time.

[0138] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, the computation graph parsing and verification module 20 can use a depth-first algorithm to traverse the target computation graph. For example, it can start from the input node and visit each node in turn. For the convolution layer nodes, the computation graph parsing and verification module 20 can parse and obtain parameter information such as its convolution kernel size, step size, padding, and input and output channels, and determine information such as the dimension and value of the convolution kernel weight data. For the pooling layer nodes, the computation graph parsing and verification module 20 parses and obtains information such as the pooling type (such as maximum pooling) and the pooling window size. For the activation function layer, the computation graph parsing and verification module 20 parses and obtains its type (such as ReLU), etc. For the fully connected layer nodes, the model code generation system can parse and obtain its weight matrix (such as the dimension [input_size, output_size]), bias vector (the dimension [output_size]), and other parameter information.

[0139] Optionally, the computation graph parsing and verification module 20 verifies whether the logic of the target computation graph is reasonable.

[0140] If not, the computation graph parsing and verification module 20 outputs a second prompt message on the visual interface.

[0141] For example, after parsing the target computation graph, the computation graph parsing and verification module 20 verifies whether the logic of the entire target computation graph is reasonable.

[0142] For example, the computation graph parsing and verification module 20 may verify whether the target computation graph has isolated nodes, circular dependencies, or other issues. If these issues exist, the computation graph parsing and verification module 20 may output a second prompt message in the visual interface, indicating that the current target computation graph has a logical error, prompting the user to make reasonable modifications to the target computation graph.

[0143] As an embodiment, when the target computation graph is a simple linear model computation graph, during the verification process, the computation graph parsing and verification module 20 can check whether the edge connections are correct. For example, whether the output of the input node is correctly connected to the input of the fully connected layer node, whether the output of the fully connected layer node is connected to the input of the output node, etc. In addition, the computation graph parsing and verification module 20 can also check whether there are isolated nodes or other logical errors in the entire target computation graph. If the computation graph parsing and verification module 20 finds a logical error (such as a node not being connected or a data type mismatch), the computation graph parsing and verification module 20 will feedback a second prompt message to the user through the visualization page (such as "The data types between the input node and the fully connected layer node are incompatible, please check").

[0144] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the verification process, the computation graph parsing and verification module 20 can check the logical structure of the entire target computation graph in addition to checking whether the logic of the edge connection is reasonable and whether the data dimensions match. For example, the computation graph parsing and verification module 20 checks whether there is a circular dependency (such as the output of a node returns to the node as input after a series of operations). If so, the computation graph parsing and verification module 20 will feedback a second prompt message to the user through a visualization page (such as "There is a circular dependency in the target computation graph, please check"). In addition, the computation graph parsing and verification module 20 can also check whether the parameters of each node are within a reasonable range (such as the convolution kernel size cannot be a negative number, etc.).

[0145] According to an example embodiment, the model structure mapping module 30 maps the graph structure of the target computation graph to the target structure of the target model based on the computation graph information.

[0146] For example, the graph structure of the target computation graph may include operations and structures. The model structure mapping module 30 accurately maps the operations and structures in the target computation graph to the target structure of the target model based on the computation graph information obtained by parsing.

[0147] Optionally, the model structure mapping module 30 maps each node in the target computation graph to a corresponding structure of the target model based on a corresponding implementation method.

[0148] For example, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. For each node in the ONNX computation graph, there is a corresponding implementation in the Oneflow model.

[0149] For example, the model structure mapping module 30 maps each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method, and the specific steps may include:

[0150] S11: Node type identification: the model structure mapping module 30 identifies the node type of each node in the target computation graph (such as input layer, convolution layer, activation function layer, etc.);

[0151] S12: Parameter matching, the model structure mapping module 30 converts the general parameters (such as the number of channels, kernel size, etc.) in the target computation graph into specific parameters of the target structure (i.e., framework) of the target model;

[0152] S13: Establishing a connection relationship: the model structure mapping module 30 establishes a data flow relationship between layers in the target structure according to the topological structure of the target computation graph;

[0153] S14: Structural feature matching, the model structure mapping module 30 processes the specific syntax requirements in the target structure to achieve mapping of the graph structure of the target computation graph to the target structure of the target model.

[0154] As an example, for a convolutional layer node, the model structure mapping module 30 may create a oneflow.nn.Conv2d object in the OneFlow model and then initialize the object based on the parsed computational graph information (e.g., parameters such as convolution kernel size, stride, and padding) to map the node to the corresponding structure of the target model. Here, oneflow.nn.Conv2d represents a two-dimensional convolutional layer in the OneFlow model.

[0155] In addition, when processing complex computation graph structures (such as a subgraph containing multiple sequential operations or a computation graph with a branching structure in the target computation graph), the model structure mapping module 30 can map them to corresponding structures in the Oneflow model. For example, for a computation graph including convolutional layer nodes, activation function nodes, and pooling operation layer nodes, the model structure mapping module 30 can create oneflow.nn.Sequential objects in the Oneflow model (including oneflow.nn.Conv2d, oneflow.nn.ReLU, and oneflow.nn.MaxPool2d or oneflow.nn.AvgPool2d objects in sequence) to map a model structure consistent with the graph structure of the target computation graph. Among them, oneflow.nn.Sequential is the container module layer used to construct a neural network in the OneFlow model; oneflow.nn.ReLU is the activation function layer in the OneFlow model; oneflow.nn.MaxPool2d is the two-dimensional maximum pooling operation layer in the OneFlow model; and oneflow.nn.AvgPool2d is the two-dimensional average pooling operation layer in the OneFlow model.

[0156] According to example embodiments, during the mapping process, the model structure mapping module 30 can fully consider the characteristics and limitations of the target model (e.g., the Oneflow model), including data type compatibility, memory management methods, and optimization strategies for specific operations. For example, based on the data type requirements of the Oneflow model, the model structure mapping module 30 can convert and check the data types of input and output tensors, thereby ensuring that the mapped model structure can run efficiently and stably in the target model.

[0157] As an embodiment, when the target computation graph is a simple linear model computation graph, during the mapping process, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. For fully connected layer nodes, the model structure mapping module 30 can create a oneflow.nn.Linear object in the Oneflow model. The weight and bias attributes of the object are then initialized based on the computation graph information (such as the weight matrix and bias vector parameters). The model structure mapping module 30 then maps the input nodes to the input tensors of the Oneflow model and the output nodes to the output tensors of the Oneflow model. Here, oneflow.nn.Linear is a module in the Oneflow model used to implement linear transformations. "weigh" represents the weight, and "bias" represents the bias.

[0158] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the mapping process, the target computation graph can be an ONNX computation graph, and the target model can be a Oneflow model. The model structure mapping module 30 can combine the various nodes of the target computation graph based on the construction method of the Oneflow model, thereby constructing a complete Oneflow model structure. For example, by creating a oneflow.nn.Sequential object, the model structure mapping module 30 can sequentially connect the convolution layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes, thereby mapping to form a trainable model.

[0159] According to an example embodiment, the model code generation module 40 generates corresponding model code based on the target structure of the target model.

[0160] For example, based on the structure information of the generated target structure of the target model, the model code generation module 40 may generate model code of a complete target model (such as a Oneflow model).

[0161] Structural information is the structured information obtained after the target computation graph is parsed and mapped. It contains the parameter information required to build the target structure of the target model and serves as a bridge connecting the target computation graph and the final generated model code.

[0162] For example, structural information may include topological information, such as the hierarchical relationships of nodes (e.g., input layer-hidden layer-output layer), the connections between nodes (e.g., serial, parallel, and residual connections), and data flow and dependencies. Structural information may also include node attribute information, such as operation types (e.g., convolution, fully connected operations, and activation functions), parameter configurations (e.g., kernel size, stride, and number of channels), and special properties (e.g., whether weights are shared or whether they are trainable). Structural information may also include framework adaptation information, such as specific requirements for the target architecture, mapping rules, and grammatical constraints. Structural information may also include meta-information, such as identifiers such as model name, version, and author, input and output specifications (e.g., tensor shape and data type), and optimizer configurations (e.g., learning rate and loss function).

[0163] For example, the model structure mapping module 30 can directly convert the nodes of the target computation graph into the standard layers of the target model framework (such as the Oneflow model), while preserving the parameter naming and organizational structure of the target model framework. The model code generation module 40 then predefines code templates for each node type and generates the corresponding model code after filling in the parameters.

[0164] Optionally, the model code generation module 40 combines the various objects in the target structure into executable model code according to the grammatical rules (ie, structural information) of the target model.

[0165] Exemplarily, the specific steps of generating the model code may include:

[0166] S21: Templated code generation: the model code generation module 40 predefines a code template for each node type and fills parameter placeholders in the template according to node attributes;

[0167] S22: Topological sorting and code organization, the model code generation module 40 generates ordered code fragments based on the dependency relationship between nodes and organizes the code structure;

[0168] S23: Framework adaptation layer processing, the model code generation module 40 processes the framework-specific syntax requirements of the target structure and inserts necessary initialization code (such as model compilation, parameter initialization, etc.);

[0169] S24: Optimization and post-processing, the model code generation module 40 adds comment information and document string information to improve readability, and performs code formatting (such as indentation, import statement organization, etc.) and generates auxiliary functions (such as model reasoning logic) to obtain the corresponding model code.

[0170] For example, for a simple model structure, the generated model code may include necessary import statements such as "import oneflow" and "import oneflow.nn as nn." The model code generation module 40 also defines an "_init_" function to initialize the various layer objects of the model, and a "forward" function to describe the forward propagation process of the model. Among them, "import oneflow" and "import oneflow.nn as nn" are statements for the OneFlow model, the "_init_" function is the constructor, and the "forward" function is the forward propagation function.

[0171] Optionally, during the model code generation process, the model code generation module 40 may also add comment information and document string information to improve the readability of the model code.

[0172] For example, in the _init_ function, the model code generation module 40 adds detailed comments to the initialization parameters of each node layer object to explain the meaning and function of the parameters. The document string information can be used to describe the function of the target model, the format of input and output data, and the construction ideas of the model.

[0173] As an embodiment, when the target computation graph is a simple linear model computation graph, during the model code generation process, the model code generation module 40 generates a complete Oneflow model code based on the structural information of the target structure of the target model (such as the Oneflow model) mapped by the simple linear model computation graph.

[0174] For example, the model code generation module 40 can add the necessary import statements import oneflow and importoneflow.nn as nn. The model code generation module 40 then creates the SimpleLinearModel class (a neural network class), which inherits from nn.Module (the core class used to build and manage neural network models). In the _init_ function, the model code generation module 40 initializes the oneflow.nn.Linear object according to the mapping information and adds annotation information to explain the meaning of each parameter. In the forward function, the model code generation module 40 defines the forward propagation process of the data, which simply passes the input data through the fully connected layer nodes. The model code generation module 40 can also add documentation string information to describe the function functionality.

[0175] Exemplarily, the model code generation module 40 may also add document string information at the beginning of the model code to describe information such as the function of the entire model, the format of input and output data, etc.

[0176] Exemplarily, the model code generation module 40 may also generate sample code to instruct the user on how to load data, how to train and evaluate the model, etc., so that the user can quickly understand the generated model code.

[0177] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, during the model code generation process, the model code generation module 40 generates a complete Oneflow model code based on the structural information of the target structure of the target model (such as the Oneflow model) mapped by the complex convolutional neural network computation graph.

[0178] For example, the model code generation module 40 can add the necessary import statements import oneflow and importoneflow.nn as nn. The model code generation module 40 then creates the SimpleLinearModel class. In the _init_ function, the model code generation module 40 initializes the objects of each node according to the mapped target structure and adds annotations to explain the meaning of each parameter. In the forward function, the model code generation module 40 processes the data through each layer in sequence based on the computational flow of the target model and defines the forward propagation process of the data.

[0179] The above description of the present invention uses the Oneflow model as an example, but the target model in the present invention is not limited to the Oneflow model. For example, based on the same principles as the Oneflow model, the target model can also be other models such as PaddlePaddle (a deep learning programming framework) or PyTorch (a deep learning programming framework), and the present invention does not limit this. The present invention can support code generation for a variety of different framework models.

[0180] Through the above embodiments, the present invention provides a model code generation method based on a computational graph. The present invention can respond to user creation instructions, create a target computational graph based on a visual interface, and then parse the computational graph information of the target computational graph. Based on the computational graph information, the graph structure of the target computational graph can be mapped to the target structure of the target model, and the corresponding model code can be generated based on the target structure of the target model.

[0181] The present invention enables user interaction based on a visual interface, thereby enabling visual editing of computational graphs. It accurately generates model code for a target model based on the computational graph created by the user. By tightly integrating visual editing with code generation, the present invention improves the efficiency and accuracy of model development, reduces development costs, and enhances the flexibility of code generation and its adaptability to complex model structures.

[0182] On the one hand, the present invention uses visual editing to allow developers to create and edit computational graphs directly in a visual interface, eliminating the need for manual code modifications, thereby improving the efficiency of model building and modification. Compared with existing model code generation methods, the present invention can reduce the possibility of errors introduced by code modifications. Moreover, the present invention allows users to directly modify the parameter information of the computational graph through a visual interface, eliminating the need to search for and modify the relevant parameter configuration in the code. This allows the present invention to quickly adjust and optimize the model structure, saving a considerable amount of time and cost.

[0183] On the other hand, the present invention can process complex computational graph structures through computational graph parsing, verification and mapping (for example, for computational graphs containing multiple different types of operations and complex connection relationships, the present invention can accurately convert them into model code of the target model), thereby avoiding errors caused by the complexity of the model in existing model code generation methods and reducing model debugging time and cost.

[0184] According to yet another aspect of the present invention, an electronic device is provided. The electronic device includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to implement the method described above.

[0185] According to another aspect of the present invention, a non-volatile computer-readable storage medium is provided, wherein a computer program is stored on the storage medium, and when the computer program is executed by a processor, the method described above can be implemented.

[0186] According to another aspect of the present invention, a computer program product is provided, which includes a computer program stored on a computer-readable storage medium; the computer program includes program instructions, which, when executed by a computer, cause the computer to execute the method described above.

[0187] Finally, it should be noted that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art will be able to modify the technical solutions of the aforementioned embodiments or substitute equivalents for some of the technical features therein. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.

Claims

1. A model code generation method based on a computational graph, characterized in that: include: Respond to user creation instructions and create the target calculation graph based on the visual interface; Parsing the computational graph information of the target computational graph; Based on the computation graph information, mapping the graph structure of the target computation graph to the target structure of the target model, and mapping each node in the target computation graph to a corresponding structure of the target model based on a corresponding implementation method, including: Node type identification, identifying the node type of each node in the target computation graph; Parameter matching, converting the universal parameters in the target computation graph into specific parameters of the target structure of the target model; Establishing a connection relationship, and establishing a data flow relationship between layers in the target structure according to the topological structure of the target computation graph; Structural feature matching, processing specific grammatical requirements in the target structure to achieve mapping of the graph structure of the target computation graph to the target structure of the target model; Generate corresponding model code based on the target structure of the target model, and combine various objects in the target structure into executable model code according to the grammatical rules of the target model, including: Templated code generation, pre-defined code templates for each node type, and parameter placeholders in the templates are filled according to node attributes; Topological sorting and code organization: generating ordered code snippets based on the dependencies between nodes and organizing code structures; Framework adaptation layer processing, handling the framework-specific syntax requirements of the target structure and inserting initialization code; Optimize and post-process, add comment information and document string information, format the code and generate auxiliary functions to obtain the corresponding model code.

2. The model code generation method according to claim 1, characterized in that: The user creation instruction includes an edge creation instruction, and the model code generation method further includes: Determining whether the edge creation instruction complies with preset computational graph creation rules; If not, the first prompt information is output on the visual interface.

3. The model code generation method according to claim 1, characterized in that: After parsing the computation graph information of the target computation graph, the model code generation method further includes: Verify whether the logic of the target calculation graph is reasonable; If not, a second prompt message is output on the visual interface.

4. A model code generation system based on computational graph, characterized in that: include: The computation graph visualization editing module responds to user creation instructions and creates the target computation graph based on the visualization interface; A computational graph parsing and verification module, which parses the computational graph information of the target computational graph; A model structure mapping module maps the graph structure of the target computation graph to the target structure of the target model based on the computation graph information. The model structure mapping module maps each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method, including: Node type identification, the model structure mapping module identifies the node type of each node in the target computation graph; Parameter matching, converting the universal parameters in the target computation graph into specific parameters of the target structure of the target model; Establishing a connection relationship, and establishing a data flow relationship between layers in the target structure according to the topological structure of the target computation graph; Structural feature matching, processing specific grammatical requirements in the target structure to achieve mapping of the graph structure of the target computation graph to the target structure of the target model; A model code generation module generates corresponding model code based on the target structure of the target model and combines the objects in the target structure into executable model code according to the grammatical rules of the target model, including: Templated code generation, pre-defined code templates for each node type, and parameter placeholders in the templates are filled according to node attributes; Topological sorting and code organization: generating ordered code snippets based on the dependencies between nodes and organizing code structures; Framework adaptation layer processing, handling the framework-specific syntax requirements of the target structure and inserting initialization code; Optimize and post-process, add comment information and document string information, format the code and generate auxiliary functions to obtain the corresponding model code.

5. The model code generation system according to claim 4, characterized in that: The user creation instruction includes an edge creation instruction, and the computation graph visualization editing module determines whether the edge creation instruction complies with a preset computation graph creation rule; If not, the computation graph visualization editing module outputs first prompt information on the visualization interface.

6. The model code generation system according to claim 4, characterized in that: The computation graph parsing and verification module verifies whether the logic of the target computation graph is reasonable; If not, the computation graph parsing and verification module outputs a second prompt message on the visualization interface.

Citation Information

Patent Citations

  • Visual programming of deep learning

    CN112148276A

  • AI application generation method, electronic equipment and storage medium

    CN114461184A