Training method and device of large model for PLC code generation, equipment and medium
By introducing physical constraint coding and condition adaptation mechanisms into the large language model, reliable PLC code that meets the requirements of industrial control systems is trained and generated. This solves the problem that the generated code in the existing technology does not conform to physical laws and working condition adaptability, and realizes multi-working-condition adaptation and equipment compatibility.
Patent Information
- Application Number
- CN202610426450.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-04-02
- Publication Date
- 2026-08-25
- Estimated Expiration
- 2046-04-02
AI Technical Summary
Existing large language models generate PLC code in industrial control systems that lack adaptability to specific production conditions and conformity to physical laws, resulting in unreliable generated logic code.
By acquiring programmable logic controller (PLC) code and physical state data, and using physical constraint encoders and condition vectors, code space vectors and physical space vectors are determined. Physical constraint loss values and condition loss values are calculated, and the parameters of the large language model are updated until the total loss value is no greater than a preset value, so as to train and generate reliable code that conforms to the control logic.
This ensures that the generated PLC code follows physical laws, avoids exceeding safe operating ranges, achieves multi-condition adaptation and equipment compatibility, and improves the reliability and logical consistency of the generated code.
Smart Images

Figure CN121960628B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence, and in particular to a training method, apparatus, device, and medium for large models of PLC code generation. Background Technology
[0002] Large language models are trained on massive amounts of data, but in specific task scenarios, the results generated by large language models do not meet the requirements of these tasks. Therefore, it is necessary to fine-tune the training of large language models to optimize their application in specific scenarios.
[0003] Currently, fine-tuning training of large language models involves statistical learning and imitation of massive amounts of code data. However, in industrial control system applications, the logic code generated by large language models trained in this way lacks adaptability to specific production conditions and does not conform to physical laws; it cannot guarantee that the fine-tuned large language model generates reliable code that conforms to control logic.
[0004] Therefore, there is an urgent need for a training scheme for a large language model for PLC code generation that adapts to physical laws and multiple operating conditions. Summary of the Invention
[0005] This application provides a training method, apparatus, device, and medium for large-scale PLC code generation, which aims to ensure that the large language model generates reliable code that conforms to control logic.
[0006] In a first aspect, embodiments of this application provide a training method for a large model for PLC code generation, comprising:
[0007] Obtain the programmable logic controller (PLC) code generated by the current large language model, and determine the code space vector based on the PLC code and the physical constraint encoder;
[0008] The physical state data and equipment information corresponding to the industrial equipment are determined, whereby the equipment information is used to indicate the equipment operating environment and equipment data; based on the physical state data and physical constraint encoder, the physical space vector is determined; based on the equipment information, the condition vector corresponding to the equipment is determined.
[0009] Determine the physical constraint loss value based on the code space vector and physical space vector; determine the condition loss value based on the programmable logic controller code and condition vector.
[0010] The total loss value is determined based on the physical constraint loss value and the conditional loss value. If the total loss value is greater than the preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value.
[0011] Then return to the step of obtaining the programmable logic controller code generated by the current large language model; until the total loss value is no greater than the preset loss value, to complete the training of the large language model.
[0012] In some examples, the programmable logic controller code generated by the current large language model is obtained, including:
[0013] Input the natural language requirements into the current large language model to obtain the initial programmable logic controller code;
[0014] A rule mask is used to perform security checks on the initial programmable logic controller (PLC) code; if the initial PLC code passes the security checks, a syntax check is performed on the initial PLC code to obtain the check results.
[0015] Based on the inspection results and the initial programmable logic controller (PLC) code, the PLC code is determined.
[0016] In some examples, the code space vector is determined based on the programmable logic controller code and the physical constraint encoder, including:
[0017] The programmable logic controller code is segmented according to the word segmentation rules to obtain a word sequence;
[0018] The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into the physical constraint encoder to obtain the code space vector.
[0019] In some examples, the physical space vector is determined based on physical state data and a physical constraint encoder, including:
[0020] All physical quantities in the physical state data are vectorized to obtain multiple physical standard vectors; the multiple physical standard vectors are then concatenated to obtain the physical state vector.
[0021] Input the physical state vector into the physical constraint encoder to obtain the physical space vector.
[0022] In some examples, the conditional loss value is determined based on the programmable logic controller code and the condition vector, including:
[0023] Based on the word vector and the condition vector, the association feature vector is determined; whereby the association feature vector is used to indicate the relevance between the word vector and the condition vector.
[0024] A weighting function is used to calculate the weights of the associated feature vectors to obtain the attention weight matrix; based on the programmable logic controller code and natural language requirements, the code context information is determined.
[0025] The conditional loss value is determined based on the attention weight matrix, code context information, conditional vector, and historical word sequence.
[0026] In some examples, the total loss value is determined based on the physical constraint loss value and the conditional loss value. If the total loss value is greater than a preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value, including:
[0027] The total loss value is determined based on the physical constraint loss value, the conditional loss value, the preset model loss value, the first hyperparameter corresponding to the physical constraint loss value, and the second hyperparameter corresponding to the conditional loss value.
[0028] Determine whether the total loss value is greater than the preset loss value; if the total loss value is greater than the preset loss value, then update the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value.
[0029] In some examples, based on the total loss, physical constraint loss, and conditional loss, the preset model loss, first hyperparameter, and second hyperparameter are updated, including:
[0030] Based on the total loss value, the physical constraint loss value, and the conditional loss value, determine the proportion of physical loss value and the proportion of conditional loss value;
[0031] Determine whether the proportion of physical loss value is within the first preset proportion range. If the proportion of physical loss value is not within the first preset proportion range, update the first hyperparameter and the preset model loss value.
[0032] Determine whether the proportion of conditional loss value is within the second preset proportion range. If the proportion of conditional loss value is not within the second preset proportion range, then update the second hyperparameter.
[0033] Secondly, embodiments of this application provide a training device for a large model of PLC code generation, including: a first processing module, used to acquire programmable logic controller code generated by the current large language model, and determine a code space vector based on the programmable logic controller code and a physical constraint encoder;
[0034] The first processing module is also used to determine the physical state data and equipment information corresponding to the industrial equipment, wherein the equipment information is used to indicate the equipment operating environment and equipment data; based on the physical state data and physical constraint encoder, a physical space vector is determined; and based on the equipment information, a condition vector corresponding to the equipment is determined.
[0035] The first processing module is also used to determine the physical constraint loss value based on the code space vector and the physical space vector; and to determine the condition loss value based on the programmable logic controller code and the condition vector.
[0036] The update module is used to determine the total loss value based on the physical constraint loss value and the conditional loss value; if the total loss value is greater than the preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value.
[0037] The second processing module is used to execute and return the steps of obtaining the programmable logic controller code generated by the current large language model; until the total loss value is not greater than the preset loss value, so as to complete the training of the large language model.
[0038] In some examples, the first processing module is also used for:
[0039] Input the natural language requirements into the current large language model to obtain the initial programmable logic controller code;
[0040] A rule mask is used to perform security checks on the initial programmable logic controller (PLC) code; if the initial PLC code passes the security checks, a syntax check is performed on the initial PLC code to obtain the check results.
[0041] Based on the inspection results and the initial programmable logic controller (PLC) code, the PLC code is determined.
[0042] In some examples, the first processing module is also used for:
[0043] The programmable logic controller code is segmented according to the word segmentation rules to obtain a word sequence;
[0044] The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into the physical constraint encoder to obtain the code space vector.
[0045] In some examples, the first processing module is also used for:
[0046] All physical quantities in the physical state data are vectorized to obtain multiple physical standard vectors; the multiple physical standard vectors are then concatenated to obtain the physical state vector.
[0047] Input the physical state vector into the physical constraint encoder to obtain the physical space vector.
[0048] In some examples, the first processing module is also used for:
[0049] Based on the word vector and the condition vector, the association feature vector is determined; whereby the association feature vector is used to indicate the relevance between the word vector and the condition vector.
[0050] A weighting function is used to calculate the weights of the associated feature vectors to obtain the attention weight matrix; based on the programmable logic controller code and natural language requirements, the code context information is determined.
[0051] The conditional loss value is determined based on the attention weight matrix, code context information, conditional vector, and historical word sequence.
[0052] In some examples, the update module is also used for:
[0053] The total loss value is determined based on the physical constraint loss value, the conditional loss value, the preset model loss value, the first hyperparameter corresponding to the physical constraint loss value, and the second hyperparameter corresponding to the conditional loss value.
[0054] Determine whether the total loss value is greater than the preset loss value; if the total loss value is greater than the preset loss value, then update the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value.
[0055] In some examples, the update module is also used for:
[0056] Based on the total loss value, the physical constraint loss value, and the conditional loss value, determine the proportion of physical loss value and the proportion of conditional loss value;
[0057] Determine whether the proportion of physical loss value is within the first preset proportion range. If the proportion of physical loss value is not within the first preset proportion range, update the first hyperparameter and the preset model loss value.
[0058] Determine whether the proportion of conditional loss value is within the second preset proportion range. If the proportion of conditional loss value is not within the second preset proportion range, then update the second hyperparameter.
[0059] Thirdly, embodiments of this application provide an electronic device, including: a memory and a processor;
[0060] The memory stores the instructions that the computer executes;
[0061] The processor executes computer execution instructions stored in memory, causing the processor to perform the first aspect and / or various possible implementations of the first aspect as described above.
[0062] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the first aspect and / or various possible implementations of the first aspect.
[0063] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the first aspect and / or various possible implementations of the first aspect.
[0064] This application provides a training method, apparatus, device, and medium for a large-scale PLC code generation model. The method involves acquiring the programmable logic controller (PLC) code generated by the current large-scale language model, the physical state data of the industrial equipment, and equipment information. Based on the PLC code, physical state data, and physical constraint encoder, a code space vector and a physical space vector are determined, and based on the equipment information, a condition vector corresponding to the equipment is determined. A physical constraint loss value is determined based on the code space vector and the physical space vector. A condition loss value is determined based on the PLC code and the condition vector. A total loss value is determined based on the physical constraint loss value and the condition loss value. If the total loss value is greater than a preset loss value, the parameters of the large-scale language model are updated based on the physical constraint loss value, the condition loss value, and the total loss value. The process then returns to the step of acquiring the PLC code generated by the current large-scale language model. Training of the large-scale language model is completed until the total loss value is no greater than the preset loss value. This solution introduces a physical constraint encoding mechanism to encode the physical state data of the equipment into a spatial vector. This trains the large language model to perceive the physical state of the equipment, ensuring that the generated instructions follow physical laws and avoid exceeding the safe operating range. In addition, a condition adaptation mechanism encodes equipment information into condition vectors. This trains the large language model to automatically generate programmable logic controller (PLC) code that conforms to logical and physical constraints for different equipment types and operating conditions. This achieves multi-condition adaptation and equipment compatibility, improving the performance of the large language model in generating reliable code that conforms to control logic. Attached Figure Description
[0065] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0066] Figure 1 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 1 ;
[0067] Figure 2 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 2 ;
[0068] Figure 3 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 3 ;
[0069] Figure 4 A schematic diagram illustrating a training method for a large model of PLC code generation provided in this application;
[0070] Figure 5 A schematic diagram of the structure of a training device for a large model of PLC code generation provided in this application;
[0071] Figure 6 This is a schematic diagram of the structure of an electronic device provided in this application.
[0072] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0073] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0074] Large language models are trained on massive amounts of data, but in specific task scenarios, the results generated by large language models do not meet the requirements of these tasks. Therefore, it is necessary to fine-tune the training of large language models to optimize their application in specific scenarios.
[0075] Currently, fine-tuning training of large language models involves statistical learning and imitation of massive amounts of code data. However, in industrial control system applications, the logic code generated by large language models trained in this way lacks adaptability to specific production conditions and does not conform to physical laws; it cannot guarantee that the fine-tuned large language model generates reliable code that conforms to control logic.
[0076] Therefore, there is an urgent need for a training scheme for a large language model for PLC code generation that adapts to physical laws and multiple operating conditions.
[0077] This application provides a training method for a large-scale PLC code generation model. The method involves acquiring the programmable logic controller (PLC) code generated by the current large-scale language model, the physical state data of the industrial equipment, and equipment information. Based on the PLC code, physical state data, and physical constraint encoder, a code space vector and a physical space vector are determined. Based on the equipment information, a condition vector corresponding to the equipment is determined. A physical constraint loss value is determined based on the code space vector and the physical space vector. A condition loss value is determined based on the PLC code and the condition vector. A total loss value is determined based on the physical constraint loss value and the condition loss value. If the total loss value is greater than a preset loss value, the parameters of the large-scale language model are updated based on the physical constraint loss value, the condition loss value, and the total loss value. The method then returns to the step of acquiring the PLC code generated by the current large-scale language model. This process continues until the total loss value is no greater than the preset loss value, thus completing the training of the large-scale language model. This solution introduces a physical constraint encoding mechanism to encode the physical state data of the equipment into a spatial vector. This trains the large language model to perceive the physical state of the equipment, ensuring that the generated instructions follow physical laws and avoid exceeding the safe operating range. In addition, a condition adaptation mechanism encodes equipment information into condition vectors. This trains the large language model to automatically generate programmable logic controller (PLC) code that conforms to logical and physical constraints for different equipment types and operating conditions. This achieves multi-condition adaptation and equipment compatibility, improving the performance of the large language model in generating reliable code that conforms to control logic.
[0078] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0079] Figure 1 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 1 ,like Figure 1 As shown in this embodiment, the training method for a large model for PLC code generation includes:
[0080] S101, Obtain the programmable logic controller code generated by the current large language model, and determine the code space vector based on the programmable logic controller code and the physical constraint encoder;
[0081] Among them, programmable logic controller code refers to the program code used by a programmable logic controller (PLC).
[0082] A physics-constrained encoder is a technique that embeds physical laws or domain knowledge into a neural network encoder, aiming to improve the model's generalization ability, interpretability, and physical plausibility. Specifically, a physics encoder... It is a multi-layer fully connected network, with the input layer being a state vector. The output layer is a latent space vector. The latent space captures the implicit relationships between states, enabling the Large Language Model (LLM) to understand the operating rules of the device.
[0083] In some examples, Figure 2 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 2 ,like Figure 2 As shown, the specific process for obtaining the programmable logic controller code generated by the current large language model is as follows:
[0084] S201, Input the natural language requirements into the current large language model to obtain the initial programmable logic controller code;
[0085] S202 uses a rule mask to perform security checks on the initial programmable logic controller (PLC) code; if the initial PLC code passes the security checks, it performs a syntax check to obtain the check results.
[0086] To ensure that the generated programmable logic controller (PLC) code is both efficient and meets the security and executability requirements of industrial control, a progressive verification mechanism is employed to validate the PLC code generated from the large language model. Specifically, the progressive verification mechanism includes: firstly, performing security verification on the generated initial PLC code using a rule-based mask; then, performing syntax checks on the security-verified PLC code; and finally, obtaining the PLC code that passes the verification.
[0087] In this context, rule masking refers to converting rules such as display interlocks and prohibited actions into tokens using a rule table configured in the form of "condition-prohibited action". During decoding, illegal tokens are directly masked to achieve security verification of the PLC code. For example, if the configured rule table includes: "When the angle of robot arm joint 1 is equal to 170 degrees, prohibit joint 1 from continuing to rotate forward", the corresponding rule code is "{"constraint condition":"robot arm joint 1 angle = 170","prohibited instruction":["joint 1 continues to move forward","rotate joint 1 clockwise"]}". That is, when the constraint condition "robot arm joint 1 angle is equal to 170 degrees" is triggered, the following operation instruction "continue to move joint 1 forward and rotate joint 1 clockwise" is not allowed to be generated.
[0088] Understandably, rule-based masking is used to perform security checks on the initial programmable logic controller (PLC) code. Specifically, for each step of the code, a mask is applied based on the current context to check if there are any instructions with triggering conditions. If an instruction with a triggering condition is found, the token score of the corresponding prohibited instruction is set to negative infinity to avoid selecting that instruction.
[0089] For example, if the current angle of joint 1 of the robotic arm is 150 degrees, when generating a PLC operation instruction to continue driving joint 1, if the current angle of joint 1 is not equal to 170 degrees, the constraint condition is not triggered, and joint 1 can continue to move forward and rotate clockwise. If the current angle of joint 1 is 170 degrees, when generating a PLC operation instruction to continue driving joint 1, if the current angle of joint 1 is equal to 170 degrees, the constraint condition is triggered, prohibiting joint 1 from moving forward and rotating clockwise. Specifically, the fraction corresponding to the prohibition instruction is set to negative infinity, so the device will not select to execute this instruction when executing the PLC code. This ensures correctness at the safety level without increasing inference overhead.
[0090] After the PLC code passes security monitoring, a lightweight syntax checker is invoked to check the input / output port matching, variable scope correctness, and other aspects of the PLC code. The purpose of checking input / output port matching is to detect whether input and output ports are mixed up. For example, in the PLC code, "normally open contact of start button (I0.0) → directly connected to motor coil (I0.0)" will be found through syntax checking to be a case of mixed input and output ports. The reason is as follows: input ports (starting with I, such as I0.0, are button signals and can only "receive signals") and output ports (starting with Q, such as Q0.0, are motor coils and can only "output signals"); however, the PLC code writes the output port as "I0.0", confusing it with the input port. Variable scope correctness refers to detecting whether there are undefined variables in the code. By detecting syntax errors in the PLC code, equipment malfunctions caused by syntax errors in the PLC code can be avoided when the equipment is running. Furthermore, hidden errors not caught by rule masks can be discovered, thus improving logical correctness while ensuring performance.
[0091] S203. Based on the inspection results and the initial programmable logic controller (PLC) code, determine the PLC code. Specifically, for example, the PLC code undergoes a syntax check to obtain the inspection result; based on the inspection result, the PLC code is then syntax-corrected to obtain PLC code that meets safety and executable requirements. For instance, if the PLC code incorrectly uses "I0.0" for an output port, confusing it with an input port, then based on the inspection result, the PLC code "normally open contact of start button (I0.0) → directly connected to motor coil (I0.0)" is corrected to obtain the PLC code "normally open contact of start button (I0.0) → directly connected to motor coil (Q0.0)" that meets safety and executable requirements.
[0092] Understandably, rule masks can handle illegal behaviors such as explicit interlocks, prohibited actions, and conditional constraints, covering logical safety issues that syntax checks cannot catch, while syntax checks ensure the executability of the generated code. Compared to traditional one-time full checks, this step first performs security checks on the PLC code using rule masks, and then, if the security checks pass, performs syntax checks on the PLC code to verify its executability. This progressive verification mechanism not only improves the efficiency of PLC code generation but also reduces the risk of erroneous code being executed and the failure rate of industrial equipment.
[0093] In some examples, the code space vector is determined based on the programmable logic controller code and the physical constraint encoder. The specific process is as follows:
[0094] The programmable logic controller code is segmented according to the word segmentation rules to obtain a word sequence;
[0095] The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into the physical constraint encoder to obtain the code space vector.
[0096] Among them, code segmentation is to superdivide the code string into the smallest units (called tokens) with independent syntactic meaning; for example, for any code segment "{"condition":"joint_1_angle=170"}" in PLC code, the code segment is segmented according to the segmentation rules, and the resulting token sequence is: "{-start character", "condition-key name", ":-key value separator", "joint_1_angle=170-description condition", "}-end character".
[0097] The resulting token sequence is processed into feature vectors to obtain token vectors. =“{-start character→[1,0,0]”,“condition-key name→[0,1,0]”,“:-key value separator→[0,0, 1]”,“joint_1_angle=170-description condition→[0,0,1]”,“}-end character→[1,0,0]”. This is achieved through a physical encoder. The token vector is mapped to the latent space to establish an association between the token vector and the physical scene features of the device, thus obtaining the code space vector. :
[0098] Specifically, the physical space is defined as [joint ID, angle value, constraint state, structural symbol]. For each token vector, based on its role in the physical constraints, the token vector is mapped to this 4-dimensional physical space, resulting in the following spatial vectors: "{-start symbol → [1,0,0] → [0,0,0,1]", "condition-key name → [0,1,0] → [1,0,1,0]", ":-key value separator → [0,0,1] → [1,0,3,0]", "joint_1_angle=170-description condition → [0,0,1] → [1,170,2,0]", "}-end symbol → [1,0,0] → [0,0,0,2]". These spatial vectors are then integrated according to the token sequence to obtain the code space vector. .
[0099] S102, determine the physical state data and equipment information corresponding to the industrial equipment, wherein the equipment information is used to indicate the equipment operating environment and equipment data; determine the physical space vector based on the physical state data and physical constraint encoder; determine the condition vector corresponding to the equipment based on the equipment information;
[0100] The physical state data for industrial equipment includes temperature (T), pressure (P), flow rate (F), motor speed (ω), etc. Equipment information includes the equipment type, parameters, and operating information. The equipment operating environment refers to the environmental conditions under which the industrial equipment operates.
[0101] Equipment information can be encoded using one-hot encoding and mapped to a continuous vector. This continuous vector can then be standardized or normalized to serve as a numerical code. The numerical codes obtained from processing different equipment information can be converted into conditional vectors of the same dimension. The dimension of the condition vector is set according to the complexity of the industrial equipment and the amount of operating information.
[0102] In some examples, the physical space vector is determined based on physical state data and a physical constraint encoder, including:
[0103] All physical quantities in the physical state data are vectorized to obtain multiple physical standard vectors; the multiple physical standard vectors are then concatenated to obtain the physical state vector.
[0104] Input the physical state vector into the physical constraint encoder to obtain the physical space vector.
[0105] For example, vector standardization can be performed on physical quantities such as "temperature T, pressure P, flow rate F, and motor speed ω". This yields the standard vector corresponding to each physical quantity. .in, Indicates the corresponding physical quantity data, This indicates the minimum value of the physical quantity corresponding to the industrial equipment. This indicates the maximum value of the physical quantity corresponding to that industrial equipment; for example, the normal operating temperature range for industrial equipment 1 is: 50℃ to =150℃, the operating temperature of industrial equipment 1 is obtained. ℃, from which we can obtain the standard vector corresponding to temperature T. By concatenating the physical standard vectors corresponding to each physical quantity, the physical state vector is obtained. .
[0106] Similarly, as described above, the steps of inputting the token vector into the physical constraint encoder to obtain the code space vector are the same as those for inputting the physical state vector into the physical constraint encoder to obtain the physical space vector. This will not be elaborated upon here.
[0107] S103, determine the physical constraint loss value based on the code space vector and physical space vector; determine the condition loss value based on the programmable logic controller code and condition vector;
[0108] Specifically, the physical constraint loss value is calculated using a loss function based on the code space vector and the physical space vector; the loss function is as follows:
[0109] During the training of a large language model, by adding a physical constraint loss value, we can ensure that the large language model training has potential spatial alignment capabilities, which can generate physically consistent PLC code that conforms to the equipment status.
[0110] In some examples, the conditional loss value is determined based on the programmable logic controller code and the condition vector, including:
[0111] Based on the word vector and the condition vector, the association feature vector is determined; whereby the association feature vector is used to indicate the relevance between the word vector and the condition vector.
[0112] A weighting function is used to calculate the weights of the associated feature vectors to obtain the attention weight matrix; based on the programmable logic controller code and natural language requirements, the code context information is determined.
[0113] The conditional loss value is determined based on the attention weight matrix, code context information, conditional vector, and historical word sequence.
[0114] Specifically, through mapping functions Each token vector Mapping to the condition space, and the condition vector Each vector is matched using a dot product to obtain associated feature vectors; a weight function is then used to calculate the weights of the associated feature vectors to obtain the attention weight matrix. The weighting function, such as the softmax function, is used to calculate the attention weights as follows:
[0115]
[0116] Where d represents the dimension of the condition vector; matrix Each row represents the degree to which the generated token depends on the elements of the condition vector.
[0117] To ensure the token sequence generated for training the large language model is consistent with the conditional vector, a conditional loss value is introduced to train the large language model to generate sequences that conform to the conditional vector. Specifically, the code context information is determined through PLC code and the input natural language requirements; the conditional loss value is calculated based on the attention weight matrix, code context information, conditional vector, and historical word sequence; the historical word sequence refers to the token sequence generated in the previous round of large language model training; for example, if token sequence 1 was generated in the first round of large language model training, and token sequence 2 was generated in the second round of large language model training, then token sequence 1 serves as the historical word sequence for the second round of training.
[0118] The calculated loss function is as follows:
[0119]
[0120] in, This represents the token index in the generated sequence. Represents the condition vector element index, Represents the attention weight matrix. This represents the j-th token in the generated sequence. Let represent the conditional probability of the generative model predicting the j-th token given a preceding token, input context q, and condition vector c, where log represents the natural logarithm.
[0121] During training, gradient backpropagation is performed based on the conditional loss value to adjust the parameters θ of the large language model, thereby reducing the conditional loss and improving the ability of the large language model to generate sequences that conform to the conditional vectors.
[0122] S104. Determine the total loss value based on the physical constraint loss value and the conditional loss value. If the total loss value is greater than the preset loss value, update the parameters of the large language model based on the physical constraint loss value, the conditional loss value, and the total loss value.
[0123] The function for calculating the total loss value is as follows:
[0124]
[0125] Among them, The preset model loss value, These are adjustable hyperparameters used to balance logic generation with other conditional constraints; that is... This represents the first hyperparameter corresponding to the physical constraint loss value. This represents the second hyperparameter corresponding to the conditional loss value.
[0126] In some examples, the total loss value is determined based on the physical constraint loss value and the conditional loss value. If the total loss value is greater than a preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value. The specific process is as follows:
[0127] The total loss value is determined based on the physical constraint loss value, the conditional loss value, the preset model loss value, the first hyperparameter corresponding to the physical constraint loss value, and the second hyperparameter corresponding to the conditional loss value.
[0128] Determine whether the total loss value is greater than the preset loss value; if the total loss value is greater than the preset loss value, then update the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value.
[0129] The preset loss value is set according to requirements; specifically, the total loss value for this round of training is calculated using the total loss function. The system determines whether the total loss value is greater than the preset loss value. If the total loss value is greater than the preset loss value, it means that the current training effect of the large language model is not good and training still needs to continue. Based on the total loss value, physical constraint loss value, and conditional loss value, the preset model loss value, the first hyperparameter, and the second hyperparameter are updated, and the next round of training is performed on the large language model.
[0130] In some examples, Figure 3 A flowchart illustrating a training method for a large model for PLC code generation provided in this application. Figure 3 ,like Figure 3 As shown, based on the total loss value, physical constraint loss value, and conditional loss value, the preset model loss value, the first hyperparameter, and the second hyperparameter are updated. The specific process is as follows:
[0131] S301, based on the total loss value, physical constraint loss value, and conditional loss value, determine the proportion of physical loss value and the proportion of conditional loss value;
[0132] The formula for calculating the percentage of loss value is as follows:
[0133]
[0134]
[0135] S302, determine whether the proportion of physical loss value is within the first preset proportion range. If the proportion of physical loss value is not within the first preset proportion range, update the first hyperparameter and the preset model loss value.
[0136] For example, if the physical loss is set to a first preset percentage range of (0.7, 0.9), and the calculated physical loss percentage A is greater than 0.9, it indicates that the large language model is overly focused on physical alignment. In this case, the first hyperparameter should be decreased, and the preset model loss value should be increased to reduce the learning of physical constraints during training while simultaneously learning the syntax and logic of the PLC code. If the calculated physical loss percentage A is less than 0.7, it indicates that the current large language model is not affected by physical constraints. In this case, the first hyperparameter should be increased to improve the learning of physical constraints during training, so that the code generated by the large language model is more consistent with the device state at the time.
[0137] S303, determine whether the proportion of conditional loss value is within the second preset proportion range. If the proportion of conditional loss value is not within the second preset proportion range, then update the second hyperparameter.
[0138] For example, if the second preset percentage range corresponding to the set conditional loss value is (0.5, 0.8), and the calculated percentage B of the conditional loss is less than 0.5, it indicates that the token vector generated by the current large language model can match the environmental conditions of the device operation. In this case, the second hyperparameter needs to be reduced to ensure the diversity of generated tokens. If the calculated percentage B of the conditional loss is greater than 0.8, it indicates that the generated token vector and the condition vector have poor matching, and the generated PLC code is not suitable for the environmental conditions of the input device operation. In this case, the second hyperparameter should be increased to make the large language model pay more attention to conditional information.
[0139] S105, and return to the step of obtaining the programmable logic controller code generated by the current large language model; until the total loss value is not greater than the preset loss value, to complete the training of the large language model.
[0140] Understandably, when the calculated total loss value is greater than the preset loss value, it indicates that the training effect of this round of large language model is not good. Then, based on the total loss value, the first hyperparameter, the second hyperparameter, and the preset model loss value are updated, and the next round of training begins until the total loss value is no greater than the preset loss value, so as to complete the training of the large language model.
[0141] This embodiment provides a training method for a large-scale PLC code generation model. The method involves acquiring the programmable logic controller (PLC) code generated by the current large language model, the physical state data of the industrial equipment, and equipment information. Based on the PLC code, physical state data, and physical constraint encoder, a code space vector and a physical space vector are determined. Based on the equipment information, a condition vector corresponding to the equipment is determined. A physical constraint loss value is determined based on the code space vector and the physical space vector. A condition loss value is determined based on the PLC code and the condition vector. A total loss value is determined based on the physical constraint loss value and the condition loss value. If the total loss value is greater than a preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the condition loss value, and the total loss value. The method then returns to the step of acquiring the PLC code generated by the current large language model. This process continues until the total loss value is no greater than the preset loss value, thus completing the training of the large language model. This solution introduces a physical constraint encoding mechanism to encode the physical state data of the equipment into a spatial vector. This trains the large language model to perceive the physical state of the equipment, ensuring that the generated instructions follow physical laws and avoid exceeding the safe operating range. In addition, a condition adaptation mechanism encodes equipment information into condition vectors. This trains the large language model to automatically generate programmable logic controller (PLC) code that conforms to logical and physical constraints for different equipment types and operating conditions. This achieves multi-condition adaptation and equipment compatibility, improving the performance of the large language model in generating reliable code that conforms to control logic.
[0142] Currently, fine-tuning training of large language models involves statistical learning and imitation of massive amounts of code data. However, in industrial control system applications, the logic code generated by large language models trained in this way lacks adaptability to specific production conditions and does not conform to physical laws; it cannot guarantee that the fine-tuned large language model generates reliable code that conforms to control logic. Therefore, this solution uses an iterative fine-tuning method combining physical constraints and multi-condition adaptation (Physics-Constrained and Scenario-Adaptive Code Generation for PLC, abbreviated as PCSA-PLC) to fine-tune the training of large language models, ensuring that the fine-tuned large language model generates reliable code that conforms to control logic. Based on the above example, a large language model for PLC code generation was obtained after fine-tuning training. This model was then applied to an industrial equipment system after a series of tests.
[0143] Figure 4 A schematic diagram illustrating a training method for a large model of PLC code generation provided in this application; such as Figure 4 As shown, by inputting natural language requirements into the current large language model, initial PLC code is generated. After performing security checks on the initial PLC code using a rule mask, a syntax check is performed on the initial PLC code to check its executability, resulting in PLC code used for fine-tuning the training of the large language model. The physical constraint loss value is obtained based on the PLC code, physical state data, and physical constraint encoder; the conditional loss value is obtained using the token vector obtained from the PLC code, device information, code context information, and historical token sequences; the total loss value is obtained based on the physical constraint loss value and the conditional loss value; if the total loss value is greater than a preset loss value, the parameters of the large language model are updated, and the process returns to obtaining the current large language model and generating PLC code, until the total loss value is no greater than the preset loss value, at which point the training of the large language model ends.
[0144] Figure 5 A schematic diagram of the structure of a training device for a large model of PLC code generation provided in this application is shown below. Figure 5 As shown, the training device 500 for large models of PLC code generation provided in this embodiment includes:
[0145] The first processing module 501 is used to obtain the programmable logic controller code generated by the current large language model, and determine the code space vector based on the programmable logic controller code and the physical constraint encoder.
[0146] The first processing module 501 is also used to determine the physical state data and equipment information corresponding to the industrial equipment, wherein the equipment information is used to indicate the equipment operating environment and equipment data; to determine the physical space vector based on the physical state data and the physical constraint encoder; and to determine the condition vector corresponding to the equipment based on the equipment information.
[0147] The first processing module 501 is also used to determine the physical constraint loss value based on the code space vector and the physical space vector; and to determine the condition loss value based on the programmable logic controller code and the condition vector.
[0148] The update module 502 is used to determine the total loss value based on the physical constraint loss value and the conditional loss value; if the total loss value is greater than the preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value.
[0149] The second processing module 503 is used to execute and return the step of obtaining the programmable logic controller code generated by the current large language model; until the total loss value is not greater than the preset loss value, so as to complete the training of the large language model.
[0150] In some examples, the first processing module 501 is also used for:
[0151] Input the natural language requirements into the current large language model to obtain the initial programmable logic controller code;
[0152] A rule mask is used to perform security checks on the initial programmable logic controller (PLC) code; if the initial PLC code passes the security checks, a syntax check is performed on the initial PLC code to obtain the check results.
[0153] Based on the inspection results and the initial programmable logic controller (PLC) code, the PLC code is determined.
[0154] In some examples, the first processing module 501 is also used for:
[0155] The programmable logic controller code is segmented according to the word segmentation rules to obtain a word sequence;
[0156] The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into the physical constraint encoder to obtain the code space vector.
[0157] In some examples, the first processing module 501 is also used for:
[0158] All physical quantities in the physical state data are vectorized to obtain multiple physical standard vectors; the multiple physical standard vectors are then concatenated to obtain the physical state vector.
[0159] Input the physical state vector into the physical constraint encoder to obtain the physical space vector.
[0160] In some examples, the first processing module 501 is also used for:
[0161] Based on the word vector and the condition vector, the association feature vector is determined; whereby the association feature vector is used to indicate the relevance between the word vector and the condition vector.
[0162] A weighting function is used to calculate the weights of the associated feature vectors to obtain the attention weight matrix; based on the programmable logic controller code and natural language requirements, the code context information is determined.
[0163] The conditional loss value is determined based on the attention weight matrix, code context information, conditional vector, and historical word sequence.
[0164] In some examples, update module 502 is also used for:
[0165] The total loss value is determined based on the physical constraint loss value, the conditional loss value, the preset model loss value, the first hyperparameter corresponding to the physical constraint loss value, and the second hyperparameter corresponding to the conditional loss value.
[0166] Determine whether the total loss value is greater than the preset loss value; if the total loss value is greater than the preset loss value, then update the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value.
[0167] In some examples, update module 502 is also used for:
[0168] Based on the total loss value, the physical constraint loss value, and the conditional loss value, determine the proportion of physical loss value and the proportion of conditional loss value;
[0169] Determine whether the proportion of physical loss value is within the first preset proportion range. If the proportion of physical loss value is not within the first preset proportion range, update the first hyperparameter and the preset model loss value.
[0170] Determine whether the proportion of conditional loss value is within the second preset proportion range. If the proportion of conditional loss value is not within the second preset proportion range, then update the second hyperparameter.
[0171] This embodiment provides a training device for a large model for PLC code generation, which can execute the method provided in the above-described method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0172] Figure 6 This is a schematic diagram of the structure of an electronic device provided in this application. Figure 6As shown, the electronic device 60 provided in this embodiment includes at least one processor 601 and a memory 602. Optionally, the device 60 further includes a communication component 603. The processor 601, memory 602, and communication component 603 are connected via a bus 604.
[0173] In a specific implementation, at least one processor 601 executes computer execution instructions stored in memory 602, causing at least one processor 601 to perform the above-described method.
[0174] The specific implementation process of processor 601 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.
[0175] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0176] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage device.
[0177] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.
[0178] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.
[0179] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0180] The aforementioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0181] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and the readable storage medium can exist as discrete components in the device.
[0182] The division of units is merely a logical functional division; in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or units, and may be electrical, mechanical, or other forms.
[0183] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0184] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0185] If a function is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0186] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0187] Finally, it should be noted that other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein, and is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
Claims
1. A training method for a large model for PLC code generation, characterized in that, include: Obtain the programmable logic controller (PLC) code generated by the current large language model, and segment the PLC code according to the word segmentation rules to obtain a word sequence; The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into a physical constraint encoder to determine the code space vector. The physical state data and equipment information corresponding to the industrial equipment are determined, wherein the equipment information is used to indicate the equipment operating environment and equipment data; each physical quantity in the physical state data is vectorized to obtain the physical standard vector corresponding to each physical quantity; the physical standard vectors corresponding to each physical quantity are concatenated to obtain the physical state vector. The physical state vector is input into the physical constraint encoder to obtain a physical space vector; the device information is encoded and mapped into a continuous vector; the continuous vector is standardized or normalized and used as a numerical code; the numerical codes obtained from different device information processing are combined into a condition vector of the same dimension. Based on the code space vector and the physical space vector, determine the physical constraint loss value; based on the programmable logic controller code and the condition vector, determine the condition loss value; Based on the physical constraint loss value and the conditional loss value, a total loss value is determined; if the total loss value is greater than a preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value. Then return to the step of obtaining the programmable logic controller code generated by the current large language model; until the total loss value is not greater than the preset loss value, so as to complete the training of the large language model.
2. The method according to claim 1, characterized in that, The step of obtaining the programmable logic controller code generated by the current large language model includes: Input the natural language requirements into the current large language model to obtain the initial programmable logic controller code; A rule mask is used to perform security checks on the initial programmable logic controller (PLC) code; if the initial PLC code passes the security checks, a syntax check is performed on the initial PLC code to obtain the check results. Based on the inspection results and the initial programmable logic controller (PLC) code, the PLC code is determined.
3. The method according to claim 2, characterized in that, Determining the conditional loss value based on the programmable logic controller code and the condition vector includes: Based on the lexical vector and the conditional vector, an association feature vector is determined; wherein the association feature vector is used to indicate the relevance between the lexical vector and the conditional vector; A weighting function is used to calculate the weights of the associated feature vectors to obtain an attention weight matrix; based on the programmable logic controller code and the natural language requirements, the code context information is determined. The conditional loss value is determined based on the attention weight matrix, the code context information, the conditional vector, and the historical lexical sequence.
4. The method according to claim 1, characterized in that, The process of determining a total loss value based on the physical constraint loss value and the conditional loss value, and updating the parameters of the large language model based on the physical constraint loss value, the conditional loss value, and the total loss value when the total loss value is greater than a preset loss value, includes: The total loss value is determined based on the physical constraint loss value, the conditional loss value, the preset model loss value, the first hyperparameter corresponding to the physical constraint loss value, and the second hyperparameter corresponding to the conditional loss value. Determine whether the total loss value is greater than a preset loss value; if the total loss value is greater than the preset loss value, then update the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value.
5. The method according to claim 4, characterized in that, The step of updating the preset model loss value, the first hyperparameter, and the second hyperparameter based on the total loss value, the physical constraint loss value, and the conditional loss value includes: Based on the total loss value, the physical constraint loss value, and the conditional loss value, determine the proportion of physical loss value and the proportion of conditional loss value; Determine whether the proportion of the physical loss value is within a first preset proportion range. If the proportion of the physical loss value is not within the first preset proportion range, then update the first hyperparameter and the preset model loss value. Determine whether the proportion of the conditional loss value is within the second preset proportion range. If the proportion of the conditional loss value is not within the second preset proportion range, then update the second hyperparameter.
6. A training device for a large model for PLC code generation, characterized in that, include: The first processing module is used to obtain the programmable logic controller code generated by the current large language model, and to segment the programmable logic controller code according to the word segmentation rules to obtain a word sequence; The word sequence is processed into feature vectors to obtain word vectors; the word vectors are then input into a physical constraint encoder to determine the code space vector. The first processing module is further configured to determine the physical state data and equipment information corresponding to the industrial equipment, wherein the equipment information is used to indicate the equipment operating environment and equipment data; to perform vector standardization on each physical quantity in the physical state data to obtain the physical standard vector corresponding to each physical quantity; and to concatenate the physical standard vectors corresponding to each physical quantity to obtain the physical state vector. The physical state vector is input into the physical constraint encoder to obtain a physical space vector; the device information is encoded and mapped into a continuous vector; the continuous vector is standardized or normalized and used as a numerical code; the numerical codes obtained from different device information processing are combined into a condition vector of the same dimension. The first processing module is further configured to determine a physical constraint loss value based on the code space vector and the physical space vector; and to determine a condition loss value based on the programmable logic controller code and the condition vector. An update module is used to determine a total loss value based on the physical constraint loss value and the conditional loss value; if the total loss value is greater than a preset loss value, the parameters of the large language model are updated based on the physical constraint loss value, the conditional loss value, and the total loss value. The second processing module is used to execute the step of obtaining the programmable logic controller code generated by the current large language model until the total loss value is not greater than the preset loss value, so as to complete the training of the large language model.
7. An electronic device, characterized in that, include: Memory, processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the method as described in any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1-5.
9. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method described in any one of claims 1-5.
Citation Information
Patent Citations
Configuration method, device and equipment based on numerical control system and storage medium
CN116430800A
LLVM-based multi-language PLC configuration software component library extension method and system
CN121411741A