Model code generation method and system based on computational graph
Through visual editing and analytical methods based on computational graphs, deep learning model code is generated, which solves the problems of insufficient flexibility and low development efficiency in the existing technology, and realizes efficient and accurate model code generation.
Patent Information
- Application Number
- CN202510748600.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-06
- Publication Date
- 2025-07-04
- Estimated Expiration
- 2045-06-06
AI Technical Summary
Existing model code generation methods are not flexible and adaptable when dealing with complex deep learning models, which is difficult to meet the changing model development needs. In addition, the computing graph visualization tools lack interactive editing functions, resulting in inefficient development and frequent errors.
Provide a model code generation method based on calculation graphs. It creates a target calculation graph through a visual interface, analyzes the calculation graph information, and maps it to the target model structure to generate corresponding model codes, including calculation graph visual editing, analysis and verification, structure mapping and code generation modules.
It improves the efficiency and accuracy of model development, reduces development costs, enhances the flexibility of code generation and adaptability to complex model structures, and reduces errors and debugging time.
Smart Images

Figure CN120255872A_ABST
Abstract
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 has become increasingly complex. In the field of deep learning model development, model code generation is an important research direction, and its goal is to reduce the workload of manual coding, lower the development threshold, and reduce human errors through automated means.
[0003] Currently, the 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 form (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, it often requires re-designing or adjusting the rules, 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 the development efficiency to a certain extent, its flexibility is insufficient and it is difficult to meet diverse model requirements. In addition, a large number of different templates need to be created and maintained for different deep learning frameworks, increasing the development and maintenance costs.
[0005] In addition, in terms of computational graph visualization, existing visualization tools mainly provide static display functions of model structures and lack the ability to dynamically edit computational graphs. When developers need to modify the computational graph (such as adding, deleting, or adjusting network layers) during the construction or adjustment of the model, they usually need to manually modify the underlying code, which is not only inefficient but also prone to errors. In addition, the existing visualization tools are loosely combined with the code generation process and cannot achieve real-time synchronization from visual editing to code generation, resulting in a difficult seamless connection of the development process and thus affecting the development efficiency.
[0006] The content in the background art section is only the technology known to the applicant and does not necessarily represent the prior art 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 technical problems.
[0008] According to one aspect of the present invention, the present invention provides a method for generating model code based on a computational graph, including: in response 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 conforms to a pre-designed computational graph creation rule; if not, then 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, then outputting a second prompt message on the visual interface.
[0011] According to some embodiments of the present invention, based on the computational graph information, mapping the graph structure of the target computational graph to the target structure of the target model includes: mapping each node in the target computational graph to the corresponding structure of the target model based on the corresponding implementation manner.
[0012] According to some embodiments of the present invention, generating corresponding model code based on the target structure of the target model includes: combining each object in the target structure into executable model code according to the syntax rules of the target model.
[0013] According to another aspect of the present invention, the present invention provides a system for generating model code based on a computational graph, including 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 a user creation instruction 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 computational graph visual editing module determines whether the edge creation instruction conforms to a pre-designed computational graph creation rule; if not, then the computational graph visual editing module outputs a first prompt message on the visual interface.
[0015] According to some embodiments of the present invention, the computational graph parsing and verification module verifies whether the logic of the target computational graph is reasonable; if not, then the computational 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 the corresponding structure of the target model based on the corresponding implementation manner.
[0017] According to some embodiments of the present invention, the model code generation module combines each object in the target structure into an executable model code according to the syntax rules of the target model.
[0018] According to yet another aspect of the present invention, the present invention further provides an electronic device. The electronic device includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to implement the method as described above.
[0019] According to yet another aspect of the present invention, the present invention further provides a non-volatile computer-readable storage medium. A computer program is stored on the storage medium, and when the computer program is executed by a processor, the method as described above can be implemented.
[0020] According to another aspect of the present invention, the present invention further provides a computer program product. The computer program product includes: a computer program stored on a computer-readable storage medium; the computer program includes program instructions, and when the program instructions are executed by a computer, the computer is enabled to execute the method as described above.
[0021] Beneficial effects The present invention provides a method for generating model code based on a computation graph. The present invention can respond to a user creation instruction, create a target computation graph based on a visual interface, then parse the computation graph information of the target computation graph, map the graph structure of the target computation graph to the target structure of the target model based on the computation graph information, and generate corresponding model code based on the target structure of the target model.
[0022] The present invention can realize interaction with the user based on the visual interface, thereby realizing visual editing of the computation graph. The present invention can accurately generate the model code of the target model according to the computation graph created by the user. The present invention can build a close connection between visual editing and code generation, thereby improving the efficiency and accuracy of model development, reducing development costs, and enhancing the flexibility of code generation and the adaptability to complex model structures. Description of the drawings
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings based on these drawings without creative efforts.
[0024] Figure 1 A schematic flowchart showing a method for generating model code according to an embodiment of the present invention; Figure 2 Another schematic flowchart showing a method for generating model code according to an embodiment of the present invention; Figure 3 Another schematic flowchart showing a method for generating model code according to an embodiment of the present invention; Figure 4 A schematic structural diagram showing a model code generation system according to an embodiment of the present invention.
[0025] Explanation of reference numerals: Model code generation system 1; Computational graph visualization editing module 10; Computational graph parsing and verification module 20; Model structure mapping module 30; Model code generation module 40. Detailed implementation manners
[0026] Next, in combination with the accompanying drawings in the embodiments of the present invention, the technical solutions of the present invention will be clearly and completely described. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative efforts fall within the protection scope of the present invention.
[0027] Specifically, on the one hand, there are obvious deficiencies in the rule-based code generation method when converting a complex ONNX (Open Neural Network Exchange) computational graph into Oneflow (an open-source deep learning framework) code. For example, when the ONNX computational graph contains custom operations or a combination of operations with complex data dependencies, the existing rules may not be able to accurately map them to the corresponding code structures in Oneflow. This is because the rules are usually designed for common operations and simple structures, and the coverage of complex situations is limited. Also, due to the possible differences in the implementation details of the same operation in different deep learning frameworks, the rule-based code generation method is not fine enough in dealing with these differences. For example, in terms of data type processing, memory management, and parallel computing, the requirements of the ONNX computational graph and Oneflow may be different, and the rule-based conversion may not fully consider these factors, resulting in performance problems or errors in the generated Oneflow code during runtime.
[0028] On the other hand, for template-based code generation methods, when generating Oneflow code, if the template does not cover specific model structures or operations, the code cannot be generated correctly. For some new neural network architectures or models in specific fields, the template may not meet the requirements. Additionally, the maintenance cost of the template is very high. With the continuous update of deep learning frameworks and the introduction of new features, the template needs to be continuously modified and updated. Otherwise, the generated code may not be compatible with the new framework version. In terms of flexibility, it is also very difficult for template-based code generation methods to customize the generated code according to the specific needs of users. For example, users may hope to add specific optimization strategies or custom functional modules to the generated code, but the fixed structure of the template limits this flexibility.
[0029] Therefore, the current model code generation methods lack flexibility and adaptability and are difficult to meet the complex and ever-changing model development requirements. In addition, the computational graph visualization tool lacks interactive editing functions and cannot support efficient visual modeling, etc.
[0030] 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 flowchart showing the model code generation method according to an embodiment of the present invention is as follows. Figure 1 As shown, the model code generation method may include steps S100-S400.
[0031] Exemplarily, the model code generation method may be executed by a model code generation system with computing capabilities.
[0032] According to an exemplary embodiment, in step S100, the model code generation system responds to a user creation instruction and creates a target computational graph based on a visualization interface.
[0033] For example, the model code generation system may provide a user with an intuitive visualization interface through which the user can input a user creation instruction to create a target computational graph according to the user's needs.
[0034] A computational graph is a directed graph structure used in deep learning to describe the flow of mathematical operations, which graphically represents the dependency relationships of the computational process. The computational graph may include nodes and edges. The nodes may represent operation operations (such as convolution operations, pooling operations, activation functions, and fully connected operations, etc.), and the edges may represent the data flow direction (such as the input-output relationship between operation operations).
[0035] The model code generation system may use graphic drawing techniques and interaction design through the visualization interface to present the nodes and edges of the created target computational graph in a graphical form based on the user creation instruction.
[0036] Exemplarily, in the target computational graph, different arithmetic operations can be distinguished by different icons or shapes. Key parameter information of relevant arithmetic operations can be marked at nodes, and the data flow direction can be indicated by arrows.
[0037] According to the exemplary embodiment, the user creation instructions at least include operation instructions such as node creation instructions, edge creation instructions, parameter configuration instructions, and node dragging instructions.
[0038] Exemplarily, the model code generation system can provide a visualization interface for the user, through which the user can perform interactive operations. For example, in this visualization interface, the user can input user creation instructions through an interactive tool (such as a mouse) 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 the user's parameter configuration instructions, enabling the user to modify parameters through a parameter editing window. Additionally, the model code generation system can also respond to node dragging instructions to adjust the layout of the target computational graph by dragging nodes.
[0039] According to the exemplary embodiment, the target computational graph can be a simple linear model computational graph or a complex convolutional neural network computational graph.
[0040] As an embodiment, when the target computational graph is a simple linear model computational graph, the model code generation system can respond to the user creation instructions and create a simple linear model computational graph based on the visualization interface.
[0041] Specifically, the user can select "input node" in the operation menu of the visualization interface, input node creation instructions through an interactive tool in the editable area to add an input node and label it as "input". Then the user can add a fully connected layer node at an appropriate position. The user can also double-click on the fully connected layer node through the interactive tool to input parameter configuration instructions, so as to set node parameters (such as input size, output size, weight, and bias, etc.) in the popped-up parameter editing window. Finally, the user can add an "output node" at an appropriate position, 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, thus completing the creation of the simple linear model computational graph.
[0042] As another embodiment, when the target computational graph is a complex convolutional neural network computational graph, the model code generation system can respond to the user creation instructions and create a complex convolutional neural network computational graph based on the visualization interface.
[0043] Specifically, the user can select "input node" in the operation menu of the visualization interface and input node creation instructions through the interaction tool in the editable area to sequentially add nodes such as convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes.
[0044] The user can also double-click on these nodes through the interaction tool to input parameter configuration instructions, so that the node parameters can be set in the pop-up parameter editing window. For example, when adding a convolutional layer node, parameters such as the convolutional kernel size (e.g., 3*3), stride (e.g., 1), padding (e.g., 1), and the number of input and output channels can be configured. When adding a pooling layer node, parameters such as the pooling type (e.g., max pooling) and the pooling window size (e.g., 2*2) can be configured. The activation function can select functions such as ReLU (Rectified Linear Unit).
[0045] The user can also input node dragging instructions through the interaction tool and adjust the layout of the computational graph by dragging the 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, which is convenient for viewing.
[0046] Figure 2 Another flowchart showing the model code generation method according to an embodiment of the present invention.
[0047] Optionally, as Figure 2 shown, the model code generation method may further include steps S510 - S520.
[0048] In step S510, the model code generation system determines whether the edge creation instruction conforms to the pre-designed computational graph creation rules.
[0049] In step S520, if not, the model code generation system outputs a first prompt message on the visualization interface.
[0050] For example, the model code generation system can ensure the legality and effectiveness of the created target computational graph through a verification mechanism. The pre-designed computational graph creation rules may include: when adding an edge to the target computational graph, the data types of the source node and the target node connected by the edge are compatible.
[0051] Exemplarily, when adding an edge to the target computational graph, the model code generation system determines whether the computational graph editing behavior is legal and effective by real-time judging whether the data types of the source node and the target node are compatible, so as to avoid the situation of data mismatch.
[0052] As an example, when the target computation graph is a simple linear model computation graph, when adding the edge connection nodes 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 visualization interface to indicate the user's incorrect editing information, thus effectively avoiding the situation of data mismatch.
[0053] As another example, when the target computation graph is a complex convolutional neural network computation graph, when adding the edges to connect the nodes of the target computation graph, the model code generation system checks in real time whether the tensor dimensions between the source node and the target node are consistent. In the case where the tensor dimensions are inconsistent, the model code generation system outputs a first prompt message on the visualization interface to indicate the user's incorrect editing information, and further indicates the user to modify the parameters.
[0054] In step S200, the model code generation system parses the computation graph information of the target computation graph.
[0055] For example, the model code generation system can parse the obtained target computation graph. For example, the model code generation system can traverse each node and edge in the target computation graph, extract the operation type information of the node, parameter information, and the connection relationship between each node, so as to obtain legal computation graph information.
[0056] Exemplarily, the target computation graph can be an ONNX computation graph. In this parsing process, the model code generation system can convert the target computation graph into the ONNX standard model representation format for subsequent processing.
[0057] Exemplarily, taking the convolutional 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 the convolutional kernel size, stride, and padding, and store this information as computation graph information in the corresponding node object in the internal data structure.
[0058] As an example, in the case where the target computational graph is a simple linear model computational graph, the model code generation system traverses the nodes and edges of the simple linear model computational graph. For the input node, the model code generation system can parse and obtain its data type information (such as a tensor of type float32) and dimension information (such as [batch_size, input_size]), and store this information in the corresponding node object in the internal data structure. For the fully connected layer node, the model code generation system can parse and obtain its weight matrix (such as 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 in the node object. Here, 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.
[0059] As another example, in the case where the target computational graph is a complex convolutional neural network computational graph, the model code generation system can traverse the target computational graph using the depth-first algorithm. For example, it can start from the input node and visit each node in sequence. For the convolutional layer node, the model code generation system can parse and obtain its kernel size, stride, padding, and input / output channel and other parameter information, and determine information such as the dimension and value of the kernel weight data. For the pooling layer node, the model code generation system parses and obtains the pooling type (such as max pooling) and pooling window size and other information. For the activation function layer, the model code generation system parses and obtains its type (such as the ReLU function), etc. For the fully connected layer node, the model code generation system can parse and obtain its weight matrix (such as with dimensions [input_size, output_size]), bias vector (with dimensions [output_size]), and other parameter information.
[0060] Figure 3 Another flowchart showing the model code generation method according to an embodiment of the present invention.
[0061] Optionally, as Figure 3 shown, the model code generation method may further include steps S610 - S620.
[0062] In step S610, the model code generation system verifies whether the logic of the target computational graph is reasonable.
[0063] In step S620, if not, the model code generation system outputs a second prompt message on the visualization interface.
[0064] For example, after parsing the target computational graph, the model code generation system verifies whether the logic of the entire target computational graph is reasonable.
[0065] Exemplarily, the model code generation system can verify whether there are isolated nodes in the target computational graph, whether there are circular dependencies, etc. If there are such problems, the model code generation system can output a second prompt message in the visualization interface, indicating that there is a logic error in the current target computational graph, so as to prompt the user to modify the rationality of the target computational graph.
[0066] As an embodiment, when the target computational graph is a simple linear model computational graph, during the verification process, the model code generation system can check whether the connection of the edges is 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. Also, the model code generation system can check whether there are isolated nodes or other logic errors in the entire target computational graph. If the model code generation system discovers a logic error (such as a node not being connected or a data type mismatch), the model code generation system feeds back 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").
[0067] As another embodiment, when the target computational graph is a complex convolutional neural network computational graph, during the verification process, the model code generation system can, in addition to checking whether the logic of the edge connection is reasonable and whether the data dimensions match, also check the logical structure of the entire target computational graph. For example, the model code generation system checks whether there are circular dependencies (such as the output of a certain node returns to the node as input after a series of operations), if so, the model code generation system feeds back a second prompt message to the user through the visualization page (such as "There is a circular dependency in the target computational graph, please check"). Additionally, the model code generation system can also check whether the parameters of each node are within a reasonable range (such as the convolutional kernel size cannot be negative, etc.).
[0068] In step S300, the model code generation system maps the graph structure of the target computational graph to the target structure of the target model based on the computational graph information.
[0069] For example, the graph structure of the target computational graph can include operations and structures. The model code generation system accurately maps the operations and structures in the target computational graph to the target structure of the target model based on the parsed computational graph information.
[0070] Optionally, in step S300, the model code generation system maps each node in the target computational graph to the corresponding structure of the target model based on the corresponding implementation method.
[0071] 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 method in the OneFlow model.
[0072] For example, the specific steps for the model code generation system to map each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method can include: S11: Node type recognition. The model code generation system recognizes the node type of each node in the target computation graph (such as input layer, convolutional layer, activation function layer, etc.); S12: Parameter matching. The model code generation system converts the general parameters in the target computation graph (such as number of channels, kernel size, etc.) into specific parameters of the target structure (i.e., framework) of the target model; S13: Establishing connection relationships. The model code generation system establishes the data flow relationship between layers in the target structure according to the topological structure of the target computation graph; S14: Structure feature matching. The model code generation system processes the specific syntax requirements in the target structure to implement the mapping from the graph structure of the target computation graph to the target structure of the target model.
[0073] 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 according to the computation graph information parsed (such as parameter information such as convolutional kernel size, stride, padding, etc.) to complete the mapping of the node to the corresponding structure of the target model. Among them, oneflow.nn.Conv2d is the two-dimensional convolutional layer in the OneFlow model.
[0074] In addition, in the case of processing a complex computational graph structure (such as a subgraph in the target computational graph that includes multiple consecutive operations or a computational graph with a branch structure), the model code generation system can map it to the corresponding structure in the OneFlow model respectively. For example, for a computational graph that includes a convolutional layer node, an activation function node, and a pooling operation layer node, the model code generation system can create a oneflow.nn.Sequential object (successively including objects such as oneflow.nn.Conv2d, oneflow.nn.ReLU, and oneflow.nn.MaxPool2d or oneflow.nn.AvgPool2d, etc.) in the OneFlow model to map and obtain a model structure that is consistent with the graph structure of the target computational graph. Among them, oneflow.nn.Sequential is a container module layer in the OneFlow model for constructing neural networks; oneflow.nn.ReLU is an activation function layer in the OneFlow model; oneflow.nn.MaxPool2d is a two-dimensional maximum pooling operation layer in the OneFlow model; oneflow.nn.AvgPool2d is a two-dimensional average pooling operation layer in the OneFlow model.
[0075] According to the example embodiment, during the mapping process, the model code generation system can fully consider the characteristics and limitations of the target model (such as the OneFlow model), including data type compatibility, memory management methods, and optimization strategies for specific operations, etc. For example, according to the requirements of the OneFlow model for data types, the model code generation system can convert and check the data types of the input and output tensors, so as to ensure that the mapped model structure can run efficiently and stably in the target model.
[0076] As an embodiment, in the case where the target computational graph is a simple linear model computational graph, during the mapping process, the target computational graph can be an ONNX computational graph, and the target model can be the OneFlow model. For a fully connected layer node, the model code generation system can create a oneflow.nn.Linear object in the OneFlow model. Then, according to the computational graph information (such as the weight matrix and bias vector parameters), initialize the weight and bias attributes of this object. Then the model code generation system maps the input node to the input tensor of the OneFlow model, and maps the output node to the output tensor of the OneFlow model. Among them, oneflow.nn.Linear is a module in the OneFlow model for implementing linear transformation. weigh is the weight, and bias is the bias.
[0077] As another embodiment, in the case where the target computational graph is a complex convolutional neural network computational graph, during the mapping process, the target computational graph can be an ONNX computational graph, the target model can be a Oneflow model, and the model code generation system can combine the various nodes of the target computational 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 connect the convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes in sequence, thereby mapping to form a trainable model.
[0078] In step S400, the model code generation system generates corresponding model code based on the target structure of the target model.
[0079] For example, based on the structure information of the generated target structure of the target model, the model code generation system can generate the model code of the complete target model (such as a Oneflow model).
[0080] The structure information is the structured information obtained after parsing and mapping the target computational graph, which contains the parameter information required to construct the target structure of the target model and serves as a bridge connecting the target computational graph and the finally generated model code.
[0081] Exemplarily, the structure information can include topological structure information, such as the hierarchical relationship of nodes (such as input layer - hidden layer - output layer, etc.), the connection relationship between nodes (such as serial, parallel, and residual connections, etc.), and the data flow and dependency relationship, etc. The structure information can also include node attribute information, such as operation type (such as convolution operation, fully connected operation, and activation function, etc.), parameter configuration (such as convolution kernel size, stride, and number of channels, etc.), and special attributes (such as whether to share weights, whether to be trainable). The structure information can also include framework adaptation information, such as specific requirements of the target structure, mapping rules, and syntax constraints, etc. The structure information can also include meta information, such as identification information such as model name, version, and author, input and output specifications (such as tensor shape, data type), and optimizer configuration (such as learning rate, loss function), etc.
[0082] Exemplarily, the model code generation system can directly convert the nodes of the target computational graph into standard layers of the target model framework (such as a Oneflow model), and at the same time can retain the parameter naming and organizational structure of the target model framework. Then, the model code generation system pre - defines code templates for each node type, and can generate corresponding model code after filling in the parameters.
[0083] Optionally, in step S400, the model code generation system combines each object in the target structure into executable model code according to the syntax rules (i.e., structural information) of the target model.
[0084] Exemplarily, the specific steps of model code generation may include: S21: Template code generation. The model code generation system pre - defines code templates for each node type and fills the parameter placeholders in the templates according to node attributes; S22: Topological sorting and code organization. The model code generation system generates ordered code segments based on the dependency relationships between nodes and organizes the code structure; S23: Framework adaptation layer processing. The model code generation system processes the syntax requirements specific to the framework of the target structure and inserts necessary initialization code (such as model compilation, parameter initialization, etc.); S24: Optimization and post - processing. The model code generation system adds comment information and docstring information to improve readability, and performs code formatting (such as indentation, import statement arrangement, etc.) and generates auxiliary functions (such as model inference logic) to obtain the corresponding model code.
[0085] For example, for a simple model structure, the generated model code may include necessary import statements such as import oneflow, import oneflow.nn as nn, etc. The model code generation system also defines the __init__ function to initialize each layer object of the model, and the model code generation system also defines the forward function to describe the forward propagation process of the model. Among them, import oneflow, import oneflow.nn as nn are statements for the OneFlow model, the __init__ function is a constructor, and the forward function is a forward propagation function.
[0086] Optionally, in step S400, during the generation process of the model code, the model code generation system can also add comment information and docstring information to improve the readability of the model code.
[0087] 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 role of the parameters. The docstring information can be used to describe information such as the function of the target model, the format of input and output data, and the construction idea of the model.
[0088] As an example, when the target computation graph is a simple linear model computation graph, during the generation process of the model code, based on the structural information of the target structure of the target model (such as the Oneflow model) mapped from the simple linear model computation graph, the model code generation system generates the complete Oneflow model code.
[0089] For example, the model code generation system can add the necessary import statements import oneflow and import oneflow.nn as nn. Then the model code generation system creates the SimpleLinearModel class (a type of neural network class), which inherits from nn.Module (the core class for building and managing 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 annotation information to explain the meaning of each parameter. In the forward function, the model code generation system defines the forward propagation process of the data, that is, simply passes the input data through the fully connected layer node. And the model code generation system can also add docstring information to describe the function.
[0090] Exemplarily, the model code generation system can also add docstring information at the beginning of the model code to describe information such as the function of the entire model and the format of the input and output data.
[0091] Exemplarily, the model code generation system can also generate example code indicating how the user can load data, train and evaluate the model, etc., to facilitate the user's quick understanding of the generated model code.
[0092] As another example, when the target computation graph is a complex convolutional neural network computation graph, during the generation process of the model code, based on the structural information of the target structure of the target model (such as the Oneflow model) mapped from the complex convolutional neural network computation graph, the model code generation system generates the complete Oneflow model code.
[0093] For example, the model code generation system can add the necessary import statements import oneflow and import oneflow.nn as nn. Then the model code generation system creates the SimpleLinearModel class. In the _init_ function, the model code generation system initializes the objects of each node according to the mapped target structure and adds annotation information 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 computation flow of the target model and defines the forward propagation process of the data.
[0094] The above text takes the Oneflow model as an example to provide an exemplary introduction to the model code method based on graph computing 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 principle 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 the code generation of multiple different framework models.
[0095] Through the above embodiments, the present invention provides a model code generation method based on a computational graph. The present invention can respond to a user creation instruction, 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.
[0096] The present invention can realize the interaction with the user based on the visual interface, so as to realize the visual editing of the computational graph. The present invention can accurately generate the model code of the target model according to the computational graph created by the user. The present invention can build a close connection between visual editing and code generation, so as to improve the efficiency and accuracy of model development, reduce the development cost, and enhance the flexibility of code generation and the adaptability to complex model structures.
[0097] On the one hand, through visual editing, the present invention enables developers to directly create and edit the computational graph in the visual interface without manually modifying at the code level, thereby improving the efficiency of model construction and modification. Compared with the existing model code generation methods, the present invention can reduce the possibility of introducing errors due to code modification, and the present invention enables users to directly modify the parameter information of the computational graph through the visual interface without searching and modifying the relevant parameter configurations in the code. Therefore, the present invention can quickly realize the adjustment and optimization of the model structure, saving a large amount of time cost.
[0098] On the other hand, through computational graph parsing, verification and mapping, the present invention can process complex computational graph structures (such as for a computational graph containing various different types of operations and complex connection relationships, the present invention can accurately convert it into the model code of the target model), thereby avoiding the errors caused by the complexity of the model in the existing model code generation methods and reducing the model debugging time and cost.
[0099] According to another aspect of the present invention, the present invention provides a model code generation system based on a computational graph. As Figure 4As shown, the model code generation system 1 may include a computational graph visualization and editing module 10, a computational graph parsing and verification module 20, a model structure mapping module 30, and a model code generation module 40.
[0100] According to an example embodiment, the computational graph visualization and editing module 10 responds to a user creation instruction and creates a target computational graph based on a visualization interface.
[0101] For example, the computational graph visualization and editing module 10 may provide an intuitive visualization interface for the user, through which the user can input a user creation instruction to create a target computational graph according to the user's needs.
[0102] A computational graph is a directed graph structure used in deep learning to describe the flow of mathematical operations, which graphically represents the dependency relationships of the computational process. A computational graph may include nodes and edges. The nodes may represent operation operations (such as convolution operations, pooling operations, activation functions, and fully connected operations, etc.), and the edges may represent the data flow direction (such as the input-output relationship between operation operations).
[0103] The computational graph visualization and editing module 10 may, through the visualization interface, use graph drawing techniques and interaction design to present the nodes and edges of the created target computational graph in a graphical form based on the user creation instruction.
[0104] Exemplarily, in the target computational graph, different operation operations may be distinguished by different icons or shapes. Key parameter information of relevant operation operations may be marked at the nodes, and the data flow direction may be indicated by arrows.
[0105] According to an example embodiment, the user creation instruction at least includes operation instructions such as a node creation instruction, an edge creation instruction, a parameter configuration instruction, and a node dragging instruction.
[0106] Exemplarily, the computational graph visualization and editing module 10 may provide a visualization interface for the user, through which the user can perform interactive operations. For example, in the visualization interface, the user can input a user creation instruction through an interactive tool (such as a mouse) to add, delete, or modify nodes and edges accordingly. After creating a node, the computational graph visualization and editing module 10 may automatically generate default parameters. For existing nodes, the computational graph visualization and editing module 10 may also respond to the user's parameter configuration instruction, enabling the user to modify the parameters through a parameter editing window. Additionally, the computational graph visualization and editing module 10 may also respond to the node dragging instruction to adjust the layout of the target computational graph by dragging the nodes.
[0107] According to an example embodiment, the target computational graph may be a simple linear model computational graph or a complex convolutional neural network computational graph.
[0108] As an example, when the target computation graph is a simple linear model computation graph, the computation graph visualization editing module 10 can respond to a user creation instruction and create a simple linear model computation graph based on the visualization interface.
[0109] Specifically, the user can select "input node" in the operation menu of the visualization interface, and input a node creation instruction through the interaction tool in the editable area to add an input node and label it as "input". After that, the user can add a fully connected layer node at an appropriate position. The user can also double-click on the fully connected layer node through the interaction tool to input a parameter configuration instruction, so that the node parameters (such as input size, output size, weight, and bias, etc.) can be set in the popped-up parameter editing window. Finally, the user can also add an "output node" at an appropriate position, 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, thus completing the creation of the simple linear model computation graph.
[0110] As another example, 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 the visualization interface.
[0111] Specifically, the user can select "input node" in the operation menu of the visualization interface, and input a node creation instruction through the interaction tool in the editable area to sequentially add convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes, etc.
[0112] The user can also double-click on these nodes through the interaction tool to input a parameter configuration instruction, so that the node parameters can be set in the popped-up parameter editing window. For example, when adding a convolutional layer node, parameters such as the convolutional kernel size (such as 3*3), stride (such as 1), padding (such as 1), and the number of input and output channels can be configured. When adding a pooling layer node, parameters such as the pooling type (such as max pooling) and the pooling window size (such as 2*2) can be configured. The activation function can select the ReLU (Rectified Linear Unit) function, etc.
[0113] The user can also input a node dragging instruction through the interaction tool, and adjust the layout of the computation graph by dragging the 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, which is convenient for viewing.
[0114] Optionally, the computation graph visualization editing module 10 determines whether the edge creation instruction conforms to the pre-designed computation graph creation rules.
[0115] If the answer is no, the computation graph visualization editing module 10 outputs a first prompt message on the visualization interface.
[0116] 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 pre-designed 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.
[0117] Exemplarily, when adding an edge to the target computation graph, the computation graph visualization editing module 10 determines whether the computation graph editing behavior is legal and valid by determining in real time whether the data types of the source node and the target node are compatible, thereby avoiding data mismatch.
[0118] As an embodiment, when the target computation graph is a simple linear model computation graph, when adding an edge to connect nodes in 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 the same. 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 the user's incorrect editing information, thereby effectively avoiding data mismatch.
[0119] As another embodiment, when the target computation graph is a complex convolutional neural network computation graph, when adding an edge to connect each node in the target computation graph, the computation graph visualization editing module 10 checks in real time whether the tensor dimensions between the source node and the target node are the same. In the case of inconsistent tensor dimensions, the computation graph visualization editing module 10 outputs a first prompt message on the visualization interface to indicate the user's incorrect editing information, and further indicates the user to modify the parameters.
[0120] According to the example embodiment, the computation graph parsing and verification module 20 parses the computation graph information of the target computation graph.
[0121] For example, the computation graph parsing and verification module 20 can parse the obtained target computation graph. For example, the computation graph parsing and verification module 20 can traverse each node and edge in the target computation graph, extract the operation type, parameter information of the node, and the connection relationship between each node, so as to obtain legal computation graph information.
[0122] Exemplarily, the target computation graph can be an ONNX computation graph. During this parsing process, the computation graph parsing and verification module 20 can convert the target computation graph into the ONNX standard model representation format for subsequent processing.
[0123] Exemplarily, taking the convolutional layer node as an example, the computation graph parsing and verification module 20 can parse its operation type information (such as convolutional operation), extract parameter information such as the convolutional kernel size, stride, and padding, and store this information as computation graph information in the corresponding node object in the internal data structure.
[0124] As an embodiment, in the case where 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 the input node, the computation graph parsing and verification module 20 can parse its data type information (such as a tensor of type float32) and dimension information (such as [batch_size, input_size]), and store this information in the corresponding node object in the internal data structure. For the fully connected layer node, the computation graph parsing and verification module 20 can parse its weight matrix (such as with dimension [input_size, output_size]), bias vector (with dimension [output_size]), and other parameter information, and create a node object to store this information in the node object. Here, 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.
[0125] As another embodiment, in the case where the target computation graph is a complex convolutional neural network computation graph, the computation graph parsing and verification module 20 can traverse the target computation graph using a depth-first algorithm. For example, it can start from the input node and visit each node in sequence. For the convolutional layer node, the computation graph parsing and verification module 20 can parse its convolutional kernel size, stride, padding, and input and output channel parameter information, and determine information such as the dimension and value of the convolutional kernel weight data. For the pooling layer node, the computation graph parsing and verification module 20 parses the pooling type (such as max pooling) and pooling window size and other information. For the activation function layer, the computation graph parsing and verification module 20 parses its type (such as ReLU), etc. For the fully connected layer node, the model code generation system can parse its weight matrix (such as with dimension [input_size, output_size]), bias vector (with dimension [output_size]), and other parameter information.
[0126] Optionally, the computation graph parsing and verification module 20 verifies whether the logic of the target computation graph is reasonable.
[0127] If not, the computation graph parsing and verification module 20 outputs a second prompt message on the visualization interface.
[0128] For example, after parsing the target computational graph, the computational graph parsing and verification module 20 verifies whether the logic of the entire target computational graph is reasonable.
[0129] Exemplarily, the computational graph parsing and verification module 20 can verify whether there are isolated nodes or circular dependencies in the target computational graph. If there are such problems, the computational graph parsing and verification module 20 can output a second prompt message in the visualization interface, indicating that there is a logical error in the current target computational graph, so as to prompt the user to modify the rationality of the target computational graph.
[0130] As an embodiment, when the target computational graph is a simple linear model computational graph, during the verification process, the computational graph parsing and verification module 20 can check whether the connection of the edges is correct. For example, whether the output of the input node is correctly connected to the input of the fully connected layer node, and whether the output of the fully connected layer node is connected to the input of the output node, etc. In addition, the computational graph parsing and verification module 20 can also check whether there are isolated nodes or other logical errors in the entire target computational graph. If the computational graph parsing and verification module 20 discovers a logical error (such as a node not being connected or a data type mismatch), the computational graph parsing and verification module 20 feeds back 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").
[0131] As another embodiment, when the target computational graph is a complex convolutional neural network computational graph, during the verification process, the computational graph parsing and verification module 20 can, in addition to checking whether the logic of the edge connection is reasonable and whether the data dimensions match, also check the logical structure of the entire target computational graph. For example, the computational graph parsing and verification module 20 checks whether there is a circular dependency (such as the output of a certain node returns to the node as input after a series of operations), if so, the computational graph parsing and verification module 20 feeds back a second prompt message to the user through the visualization page (such as "There is a circular dependency in the target computational graph, please check"). In addition, the computational 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 negative, etc.).
[0132] According to the exemplary embodiment, the model structure mapping module 30 maps the graph structure of the target computational graph to the target structure of the target model based on the computational graph information.
[0133] For example, the graph structure of the target computational graph can include operations and structures. The model structure mapping module 30 accurately maps the operations and structures in the target computational graph to the target structure of the target model based on the parsed computational graph information.
[0134] Optionally, 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.
[0135] 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 method in the OneFlow model.
[0136] For example, the specific steps for the model structure mapping module 30 to map each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method can include: 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, convolutional layer, activation function layer, etc.); S12: Parameter matching. The model structure mapping module 30 converts the general parameters in the target computation graph (such as number of channels, kernel size, etc.) into the specific parameters of the target structure (i.e., framework) of the target model; S13: Establishing connection relationships. The model structure mapping module 30 establishes the data flow relationship between layers in the target structure according to the topological structure of the target computation graph; S14: Structure feature matching. The model structure mapping module 30 processes the specific syntax requirements in the target structure to implement the mapping from the graph structure of the target computation graph to the target structure of the target model.
[0137] As an example, for a convolutional layer node, the model structure mapping module 30 can create a oneflow.nn.Conv2d object in the OneFlow model, and then initialize the object according to the computation graph information parsed (such as parameter information such as convolutional kernel size, stride, padding, etc.) to complete the mapping of the node to the corresponding structure of the target model. Among them, oneflow.nn.Conv2d is the two-dimensional convolutional layer in the OneFlow model.
[0138] In addition, in the case of processing a complex computational graph structure (such as a sub-graph in the target computational graph that includes multiple consecutive operations or a computational graph with a branch structure), the model structure mapping module 30 can map it to the corresponding structure in the OneFlow model respectively. For example, for a computational graph that includes a convolutional layer node, an activation function node, and a pooling operation layer node, the model structure mapping module 30 can create a oneflow.nn.Sequential object (sequentially including objects such as oneflow.nn.Conv2d, oneflow.nn.ReLU, and oneflow.nn.MaxPool2d or oneflow.nn.AvgPool2d, etc.) in the OneFlow model to map and obtain a model structure that is consistent with the graph structure of the target computational graph. Among them, oneflow.nn.Sequential is a container module layer in the OneFlow model for constructing neural networks; oneflow.nn.ReLU is an activation function layer in the OneFlow model; oneflow.nn.MaxPool2d is a two-dimensional maximum pooling operation layer in the OneFlow model; oneflow.nn.AvgPool2d is a two-dimensional average pooling operation layer in the OneFlow model.
[0139] According to the exemplary embodiment, during the mapping process, the model structure mapping module 30 can fully consider the characteristics and limitations of the target model (such as the OneFlow model), including the compatibility of data types, the memory management method, and the optimization strategy for specific operations, etc. For example, according to the requirements of the OneFlow model for data types, the model structure mapping module 30 can convert and check the data types of the input and output tensors, so as to ensure that the mapped model structure can run efficiently and stably in the target model.
[0140] As an embodiment, in the case where the target computational graph is a simple linear model computational graph, during the mapping process, the target computational graph can be an ONNX computational graph, and the target model can be the OneFlow model. For a fully connected layer node, the model structure mapping module 30 can create a oneflow.nn.Linear object in the OneFlow model. Then, according to the computational graph information (such as the weight matrix and bias vector parameters), the weight and bias attributes of this object are initialized. Then, the model structure mapping module 30 maps the input node to the input tensor of the OneFlow model, and maps the output node to the output tensor of the OneFlow model. Among them, oneflow.nn.Linear is a module in the OneFlow model for implementing linear transformation. weigh is the weight, and bias is the bias.
[0141] As another embodiment, in the case where 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, the target model can be a Oneflow model, and the model structure mapping module 30 can combine the 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 connect the convolutional layer nodes, pooling layer nodes, activation function layer nodes, and fully connected layer nodes in sequence, thereby mapping and forming a trainable model.
[0142] According to the exemplary embodiment, the model code generation module 40 generates corresponding model code based on the target structure of the target model.
[0143] For example, based on the structure information of the generated target structure of the target model, the model code generation module 40 can generate the model code of the complete target model (such as a Oneflow model).
[0144] The structure information is the structured information obtained after parsing and mapping the target computation graph, which contains the parameter information required to construct the target structure of the target model and serves as a bridge connecting the target computation graph and the finally generated model code.
[0145] Exemplarily, the structure information can include topological structure information, such as the hierarchical relationship of nodes (such as input layer - hidden layer - output layer, etc.), the connection relationship between nodes (such as serial, parallel, and residual connections, etc.), and the data flow and dependency relationship, etc. The structure information can also include node attribute information, such as operation type (such as convolution operation, fully connected operation, and activation function, etc.), parameter configuration (such as convolution kernel size, stride, and number of channels, etc.), and special attributes (such as whether to share weights, whether to be trainable). The structure information can also include framework adaptation information, such as specific requirements, mapping rules, and syntax constraints of the target structure. The structure information can also include meta information, such as identification information such as model name, version, and author, input / output specifications (such as tensor shape, data type), and optimizer configuration (such as learning rate, loss function).
[0146] Exemplarily, the model structure mapping module 30 can directly convert the nodes of the target computation graph into standard layers of the target model framework (such as a Oneflow model), and at the same time can retain the parameter naming and organizational structure of the target model framework. Then, the model code generation module 40 pre - defines code templates for each node type and can generate corresponding model code after filling in the parameters.
[0147] Optionally, the model code generation module 40 combines each object in the target structure into executable model code according to the syntax rules (i.e., structure information) of the target model.
[0148] Exemplarily, the specific steps of model code generation may include: S21: Template code generation. The model code generation module 40 predefines code templates for each node type and fills the parameter placeholders in the templates according to the node attributes. S22: Topological sorting and code organization. The model code generation module 40 generates ordered code segments based on the dependency relationships between nodes and organizes the code structure. S23: Framework adaptation layer processing. The model code generation module 40 processes the syntax requirements specific to the framework of the target structure and inserts necessary initialization code (such as model compilation, parameter initialization, etc.). S24: Optimization and post-processing. The model code generation module 40 adds comment information and docstring information to improve readability, and performs code formatting (such as indentation, import statement sorting, etc.) and generates auxiliary functions (such as model inference logic) to obtain the corresponding model code.
[0149] For example, for a simple model structure, the generated model code may include necessary import statements such as import oneflow, import oneflow.nn as nn, etc. The model code generation module 40 also defines the _init_ function to initialize each layer object of the model, and the model code generation module 40 also defines the forward function to describe the forward propagation process of the model. Among them, import oneflow, 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.
[0150] Optionally, during the generation process of the model code, the model code generation module 40 can also add comment information and docstring information to improve the readability of the model code.
[0151] 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 role of the parameters. The docstring information can be used to describe information such as the function of the target model, the format of input and output data, and the construction idea of the model.
[0152] As an example, in the case where the target computational graph is a simple linear model computational graph, during the generation process of the model code, after the model code generation module 40 is based on the structural information of the target structure of the target model (such as the Oneflow model) mapped from the simple linear model computational graph, it generates the complete Oneflow model code.
[0153] For example, the model code generation module 40 can add the necessary import statements import oneflow and import oneflow.nn as nn. Then the model code generation module 40 creates the SimpleLinearModel class (a type of neural network class), which inherits from nn.Module (the core class for building and managing 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 at the same time 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, that is, simply passes the input data through the fully connected layer nodes. And the model code generation module 40 can also add docstring information to describe the function of the function.
[0154] Exemplarily, the model code generation module 40 can also add docstring information at the beginning of the model code to describe information such as the function of the entire model and the format of the input and output data.
[0155] Exemplarily, the model code generation module 40 can also generate example code to indicate to the user how to load data, how to train and evaluate the model, etc., so as to facilitate the user to quickly understand the generated model code.
[0156] As another example, in the case where the target computational graph is a complex convolutional neural network computational graph, during the generation process of the model code, after the model code generation module 40 is based on the structural information of the target structure of the target model (such as the Oneflow model) mapped from the complex convolutional neural network computational graph, it generates the complete Oneflow model code.
[0157] For example, the model code generation module 40 can add the necessary import statements import oneflow and import oneflow.nn as nn. Then the model code generation module 40 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 at the same time adds annotation information to explain the meaning of each parameter. In the forward function, the model code generation module 40 processes the data through each layer in turn based on the computational flow of the target model, and defines the forward propagation process of the data.
[0158] Taking the Oneflow model as an example above, the present invention has been introduced exemplarily. However, the target model in the present invention is not limited to the Oneflow model. For example, based on the same principle 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 the code generation of multiple different framework models.
[0159] Through the above embodiments, the present invention provides a method for generating model code based on a computational graph. The present invention can respond to a user's creation instruction, 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.
[0160] The present invention can achieve interaction with the user based on a visual interface, thereby enabling visual editing of the computational graph. The present invention can accurately generate the model code of the target model according to the computational graph created by the user. The present invention can build a close connection between visual editing and code generation, thereby improving the efficiency and accuracy of model development, reducing development costs, and enhancing the flexibility of code generation and the adaptability to complex model structures.
[0161] On the one hand, through visual editing, the present invention enables developers to directly create and edit the computational graph in the visual interface without manually modifying at the code level, thereby improving the efficiency of model construction and modification. Compared with the existing model code generation methods, the present invention can reduce the possibility of introducing errors due to code modification, and the present invention enables users to directly modify the parameter information of the computational graph through the visual interface without searching and modifying the relevant parameter configurations in the code. Thus, the present invention can quickly realize the adjustment and optimization of the model structure, saving a large amount of time costs.
[0162] On the other hand, through computational graph parsing, verification, and mapping, the present invention can handle complex computational graph structures (such as for a computational graph containing various different types of operations and complex connection relationships, the present invention can accurately convert it into the model code of the target model), thereby avoiding errors caused by the complexity of the model in the existing model code generation methods and reducing the model debugging time and costs.
[0163] According to another aspect of the present invention, the present invention further provides an electronic device. The electronic device includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are enabled to implement the method as described above.
[0164] According to another aspect of the present invention, the present invention further provides a non-volatile computer-readable storage medium. A computer program is stored on the storage medium, and when the computer program is executed by a processor, the method as described above can be implemented.
[0165] According to another aspect of the present invention, the present invention further provides a computer program product. The computer program product includes: a computer program stored on a computer-readable storage medium; the computer program includes program instructions, and when the program instructions are executed by a computer, the computer is enabled to execute the method as described above.
[0166] Finally, it should be noted that the above are only the preferred embodiments of the present invention and are not used to limit the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art can still modify the technical solutions of the foregoing embodiments, or perform equivalent replacements for some of the technical features. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for generating model code based on a computational graph, characterized in that Including: In response to a user creation instruction, create a target computation graph based on a visual interface; Parse the computation graph information of the target computation graph; Based on the computation graph information, map the graph structure of the target computation graph to the target structure of a target model; Generate corresponding model code based on the target structure of the target model.
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: Determine whether the edge creation instruction conforms to a pre-designed computation graph creation rule; If not, output a first prompt message on the visual interface.
3. The model code generation method according to claim 1, wherein 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 computation graph is reasonable; If not, output a second prompt message on the visual interface.
4. The model code generation method according to claim 1, wherein The mapping of the graph structure of the target computation graph to the target structure of the target model based on the computation graph information includes: Map each node in the target computation graph to the corresponding structure of the target model based on the corresponding implementation method.
5. The model code generation method according to claim 1, wherein The generation of corresponding model code based on the target structure of the target model includes: According to the syntax rules of the target model, combine the various objects in the target structure into executable model code.
6. A model code generation system based on a computational graph, characterized in that, Including: A computation graph visual editing module that, in response to a user creation instruction, creates a target computation graph based on a visual interface; A computation graph parsing and verification module that parses the computation graph information of the target computation graph; A model structure mapping module that, based on the computation graph information, maps the graph structure of the target computation graph to the target structure of a target model; A model code generation module that generates corresponding model code based on the target structure of the target model.
7. The model code generation system according to claim 6, wherein The user creation instruction includes an edge creation instruction, and the computation graph visual editing module determines whether the edge creation instruction conforms to a pre-designed computation graph creation rule; If not, the computation graph visual editing module outputs a first prompt message on the visual interface.
8. The model code generation system according to claim 6, 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 visual interface.
9. The model code generation system according to claim 6, wherein 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.
10. The model code generation system according to claim 6, wherein The model code generation module combines the various objects in the target structure into executable model code according to the syntax rules of the target model.
Citation Information
Patent Citations
Neural network calculation graph processing method, computer storage medium and electronic equipment
CN111723935A
Visual programming of deep learning
CN112148276A
AI application generation method, electronic equipment and storage medium
CN114461184A
Model processing method and device, electronic equipment and storage medium
CN117933334A
Object variable operation method and device, storage medium and electronic equipment
CN119292583A
Cited By
DCU-oriented RFCN model adaptive migration method
CN121524137A
A DCU-oriented rfcn model adaptation migration method
CN121524137B