Program code protection method, optimization method, equipment, storage medium and product
Through compilation processing and operator replacement, the target program code is generated, and the risk of program code leakage is solved, and the code security and privacy protection effect is achieved.
Patent Information
- Application Number
- CN202411728585.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-02-28
AI Technical Summary
In scenarios where program code needs to be outgoing, there is a risk of program code leakage, especially when it is published to third-party institutions or users, an effective program code protection method needs to be provided.
The original program code is compiled through the compiler, converted from high-level code to intermediate representation code or low-level code, and replaced the operator code on this basis to generate the target program code. This target program code is used instead of the original program code and sent to an external device for tasks that have privacy protection requirements.
It reduces the risk of being reversed engineering, provides a preliminary barrier for code protection, and increases the difficulty of code reverse engineering through operator replacement, effectively reducing the risk of code leakage, thereby improving the security and privacy protection effect of code.
Smart Images

Figure CN119538268A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the field of terminal technology, and in particular, to a program code protection method, an optimization method, an electronic device, a computer-readable storage medium, and a computer program product. Background Art
[0002] Program codes independently developed by various enterprises or institutions are their important data assets. Currently, in many scenarios, such as those requiring data analysis or data sharing, program codes may need to be sent to external devices to perform specific tasks.
[0003] In scenarios where program codes need to be sent out, especially when released to third-party organizations or users, there is a risk of program code leakage. In order to protect the core assets and data security of an enterprise or organization, it is necessary to provide a program code protection method. Summary of the invention
[0004] In view of this, one or more embodiments of the present specification provide a program code protection method, an optimization method, an electronic device, a computer-readable storage medium, and a computer program product.
[0005] To achieve the above objectives, one or more embodiments of this specification provide the following technical solutions:
[0006] According to a first aspect of one or more embodiments of this specification, a method for protecting program code is proposed, including:
[0007] Get the original program code;
[0008] Compiling the original program code using a compiler to convert the original program code from a high-level code into another level of code, wherein the other level of code includes at least one of an intermediate representation code and a low-level code;
[0009] At least one operator code in the other level code is replaced with other operator code that can achieve similar functions, and the replaced other level code is used as the target program code; wherein the target program code is used to replace the original program code and sent to an external device to perform tasks with privacy protection requirements.
[0010] According to a second aspect of an embodiment of this specification, a method for optimizing a program code is provided, comprising:
[0011] Acquire a target program code to be optimized, wherein the target program code is obtained based on the program code protection method described in the first aspect;
[0012] Based on the target program code, generating a prompt word for use by a pre-trained model that has been trained; the prompt word includes the target program code and a processing requirement for the target program code, the processing requirement is used to instruct the pre-trained model to give optimization suggestions for the target program code;
[0013] Sending the prompt word to an external device that deploys the pre-trained model, so that the external device inputs the prompt word into the pre-trained model to generate the optimization suggestion;
[0014] Receive the optimization suggestion returned by the external device.
[0015] According to a third aspect of the embodiments of this specification, there is provided an electronic device, including:
[0016] processor;
[0017] a memory for storing processor-executable instructions;
[0018] When the processor executes the executable instructions, it is used to implement the method described in the first aspect.
[0019] According to a fourth aspect of the embodiments of this specification, there is provided a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the steps of any of the above methods are implemented.
[0020] According to a fifth aspect of the embodiments of this specification, a computer program product is provided, including a computer program, which implements the steps of any of the above methods when executed by a processor.
[0021] The technical solutions provided by the embodiments of this specification may have the following beneficial effects:
[0022] In the embodiments of this specification, a compiler is used to compile the original program code to convert the original program code from high-level code to intermediate representation code or low-level code, thereby reducing the risk of reverse engineering and providing a preliminary barrier for code protection. The original operator code is further replaced with other operator codes with similar functions through operator replacement, which increases the difficulty of reverse engineering and effectively reduces the risk of code leakage, thereby improving the security and privacy protection of the code.
[0023] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present specification. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 The present invention is a flowchart of a method for protecting program codes provided by an exemplary embodiment.
[0025] Figure 2 The present invention is a flowchart of a process for obtaining target program code provided by an exemplary embodiment.
[0026] Figure 3 The present invention is another processing flow chart of obtaining target program code provided by an exemplary embodiment.
[0027] Figure 4 It is another processing flow chart of obtaining target program code provided by an exemplary embodiment.
[0028] Figure 5 The present invention is a flowchart of a method for optimizing program code provided by an exemplary embodiment.
[0029] Figure 6 The present invention is a flowchart of another method for optimizing program code provided by an exemplary embodiment.
[0030] Figure 7 It is a schematic structural diagram of an electronic device provided by an exemplary embodiment. DETAILED DESCRIPTION
[0031] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with one or more embodiments of this specification. Instead, they are merely examples of devices and methods consistent with some aspects of one or more embodiments of this specification as detailed in the appended claims.
[0032] It should be noted that: in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, the steps included in the method may be more or less than those described in this specification. In addition, a single step described in this specification may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this specification may be combined into a single step for description in other embodiments.
[0033] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this manual are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0034] Here are some explanations of the terms mentioned in this manual:
[0035] 1. High-level code: refers to source code written in high-level programming languages (such as Java, C++, Python, etc.). High-level code is closer to human-readable form. It abstracts the details of the underlying hardware operation, allowing programmers to focus on the implementation of algorithms and logic without having to worry about specific machine instructions. The advantage of high-level code is that it is easy to write, read, and maintain, but it requires a compiler or interpreter to convert it into machine-executable instructions.
[0036] 2. Intermediate Representation (IR): It is an intermediate product in the compilation process. It is a low-level but platform-independent representation. IR is usually generated after the source code is parsed and optimized, and before the target code is generated. The purpose of IR is to provide a common format that the compiler can optimize and convert, which makes cross-language optimization and code generation possible.
[0037] 3. Low-level code: It is the code closest to the computer hardware, usually referring to machine code (machine language) or assembly language code. These codes directly control the computer's hardware resources, such as the CPU (Central Processing Unit), memory, and external devices. Assembly language is a low-level programming language that is closely related to a specific hardware architecture. It needs to be converted into machine code by an assembler, that is, binary instructions executed directly by the CPU. Machine language is a binary code that a computer can directly understand and execute. The advantage of low-level code is that it can provide precise control over hardware, but it is difficult to write and maintain, and is strongly bound to a specific platform, with poor portability.
[0038] In summary, high-level code is usually the code written by the programmer, which is converted into intermediate representation code or low-level code by the compiler. The intermediate representation code is used for optimization and cross-platform conversion, and will eventually be converted into low-level code before it can be executed on the computer.
[0039] 4. Pre-trained models, such as the Large Language Model (LLM), are a modeling method based on statistics and natural language processing techniques. They use high-capacity model architectures (such as Transformer) to train on massive amounts of text. Therefore, they can obtain a large amount of prior knowledge with the help of the above pre-training, and model question answering is the process of guiding it to give relevant knowledge through instructions.
[0040] 5. Prompt: Prompt can be understood as a way to specify the focus of LLM generation. It is a piece of text or sentence that guides LLM to generate output of a specific type, theme or format. A well-designed prompt is used to guide a basic model to respond in the desired a priori direction.
[0041] Based on the problem that there is a risk of program code leakage in scenarios where program code needs to be sent out, such as the risk of program code leakage in scenarios where a large language model deployed in an external device helps optimize program code, the embodiments of this specification provide a method for protecting program code, which can be executed by electronic devices, including but not limited to servers, server clusters, virtual servers, PCs (Personal Computers), tablet devices, laptops, PDAs (Personal Digital Assistants), smart phones / mobile phones, etc. Please refer to Figure 1 , the method comprising:
[0042] In S101, original program code is acquired.
[0043] Exemplarily, the original program code may be a high-level code written by a programmer based on a high-level programming language (such as Java, C++, Python, etc.). These original program codes may contain sensitive information and need to be protected to a certain extent before being sent to an external device.
[0044] In S102, the original program code is compiled by a compiler to convert the original program code from a high-level code into other-level codes, where the other-level codes include at least one of an intermediate representation code and a low-level code.
[0045] This step uses a compiler to convert high-level code into code forms at other levels. Among them, the intermediate representation code (compared with the high-level code, is closer to machine instructions and is not easy to be directly restored to readable high-level code. Low-level code (such as assembly code) is closer to machine code and is difficult to reverse restore, thereby providing preliminary protection. This embodiment compiles the original program code, converting the original program code from easy-to-read high-level code to difficult-to-understand intermediate or low-level code form, reducing the risk of reverse engineering and providing a preliminary barrier for code protection.
[0046] It is understandable that in actual application scenarios, when choosing to convert the original program code into an intermediate representation code or a low-level code, the conversion method to be used can be determined based on specific requirements and characteristics of the external device, and this embodiment does not impose any restrictions on this.
[0047] For example, in the program code optimization scenario, a large language model deployed in an external device is needed to help optimize the program code. This depends on whether the large language model can understand other levels of code. For example, if the large language model performs better in understanding the intermediate representation code, then the compiler can be used to convert the original program code from high-level code to intermediate representation code. The intermediate representation code has a moderate degree of abstraction and can hide more details while retaining the program logic, thereby achieving privacy protection. At the same time, the intermediate representation code is easier to be analyzed and processed by high-level optimization tools, and the large language model can generate more refined optimization suggestions based on this. Alternatively, if the large language model performs better in understanding low-level code and can give optimization suggestions from the bottom-level perspective, then the compiler can be used to convert the original program code from high-level code to low-level code. In S103, at least one operator code in other levels of code is replaced with other operator codes that can achieve similar functions, and the replaced other levels of code are used as target program code; wherein the target program code is used to replace the original program code and sent to the external device to perform tasks with privacy protection requirements.
[0048] In this step, some operators are replaced in the generated intermediate representation code or low-level code, and the original operators are replaced with other operators with similar functions. This replacement makes it more difficult for analysts to understand the actual logic even if the code is reverse engineered. The replaced code is the target program code, and can replace the original program code and be sent to an external device for task execution, such as program optimization task execution. By replacing operators at the intermediate representation code or low-level code level, the difficulty of code reverse engineering is effectively increased. Even if the attacker obtains the code, the diversity of operators and the complexity caused by the replacement will make it more difficult to analyze and understand the code logic, thereby improving the security and privacy protection of the code.
[0049] It is understandable that in some scenarios, such as code optimization scenarios, since the execution effect of the target program code finally generated is not required to be consistent with the execution effect of the original program code, when replacing operators, the embodiments of this specification do not require that the implementation logic of the two operators before and after the replacement is completely equivalent. The goal of the replacement is to replace one operator with another operator with similar functions, that is, they belong to the same type of operation, such as addition and subtraction, and their execution process and optimization direction are similar at a high level. Through this replacement method, even if the large language model cannot completely restore the calculation process before the replacement, it can still provide effective optimization suggestions based on the modified operator. The key point of this replacement is that although the replaced operator is no longer completely equivalent to the original operator, it still maintains the characteristics of the same type of operation, so as not to affect the understanding and optimization ability of the large model to the code. For example, addition and subtraction can be replaced in the same category of operators, because the large language model can understand the optimization methods of these operations without being troubled by the differences in the replacement itself. Such a replacement increases the privacy protection of the code, making it more difficult for analysts to understand the specific logic and implementation behind it even if the code is reverse engineered.
[0050] Of course, in other scenarios, such as scenarios where the execution effect of the target program code finally generated is required to be consistent with the execution effect of the original program code, when performing operator replacement, it is possible to consider replacing the original operator with an equivalent but different operator so that it still maintains its original function, thereby achieving the expected execution effect. This embodiment does not impose any limitation on this.
[0051] Exemplarily, during the process of replacing an operator code, the electronic device may determine at least one operator code to be replaced from other levels of code. For example, the operator to be replaced may be a commonly used operator that is easily identified and analyzed in the code, such as addition, multiplication, logical operations, etc. These operators have relatively fixed functions and are easy to be directly reverse engineered and understood. Selecting these operators as operators to be replaced can increase the complexity of reverse analysis and make it more difficult for the cracker to understand the logic of the entire program.
[0052] Then, for each operator code to be replaced, the electronic device obtains a first replacement set corresponding to the operator code to be replaced, the first replacement set includes multiple candidate operator codes with similar functions to the operator code to be replaced, and randomly selects a candidate operator code from the first replacement set to replace the operator code to be replaced. Since the replacement is random, the code generated each time the replacement operation is performed will be different. Even if the same original code is used, the generated target code will be diversified due to random selection. This randomness can increase the complexity of code protection.
[0053] The first replacement set corresponding to the operator code to be replaced can be predefined and stored in the electronic device. For example, the operator type to be replaced can be pre-specified, such as addition, subtraction, multiplication or logical operation, and the first replacement set corresponding to the specified operator type can be pre-set to perform the above-mentioned operator code replacement operation.
[0054] Taking the addition algorithm as an example, the first replacement set corresponding to the addition operator includes some other operators with similar functions to the addition operator, and these replacement operators can achieve similar calculation effects. For example, the first replacement set corresponding to the addition operator may include but is not limited to at least one of the following candidate operator codes:
[0055] (1) Using subtraction to implement addition: Addition is implemented by introducing an additional subtraction operation. For example, a+b can be written as a-(-b). Through this transformation, although the actual effect is addition, the code structure has been changed. Alternatively, in a scenario where the implementation logic of the two operators before and after the replacement is not required to be completely equivalent, a+b can be written as ab. Addition and subtraction belong to the same type of operation, and their execution process and optimization direction are similar at a high level.
[0056] (2) Use increment / decrement operations: Decompose addition into a combination of increment or decrement operations. For example, a+b can be decomposed into the process of increasing a by b times, or repeatedly increasing b to achieve a similar result.
[0057] (3) Use bitwise operations to implement addition: Addition is implemented with the help of bitwise operations. For example, two values can be added by shifting and bitwise OR operations. In some cases, bitwise operations can achieve similar results, but increase the complexity of the code. For example, a+b can be replaced by (a|b)+((a&b)<<1).
[0058] (4) Decomposition into compound addition: Decompose the addition into multiple parts. For example, a+b can be replaced by a+(b1+b2), where b1+b2=b.
[0059] (5) Implementation through Boolean logic conversion: Combined with Boolean logic operations, such as XOR, it can be used for addition logic conversion. This method increases the complexity of the code while ensuring that the function remains unchanged. For example, a+b can be replaced by (a^b)+2*(a&b) (applicable to the case of no carry).
[0060] Through the above-mentioned first replacement set corresponding to the addition operator, the electronic device can choose from different expressions when replacing the addition operator code, thereby introducing diversity into the code structure. This not only increases the difficulty of interpreting the code, but also significantly improves the protection level of the code.
[0061] In some embodiments, see Figure 2 To further improve the privacy protection level, before using the compiler to compile the original program code, the electronic device can change the data dimension of the input data and / or the data dimension of the output data defined in the original program code from the original dimension to another dimension, and then use the compiler to compile the changed original program code. This embodiment can further increase the difficulty of code reverse engineering by changing the dimensions of the input and output data, thereby enhancing the privacy protection effect.
[0062] For the modification of the data dimension of the input data, for example, a certain input data in the original program code can be expanded from a one-dimensional vector to a two-dimensional matrix, or scalar data can be packaged into a higher-dimensional structure. In this way, after the external device receives the code, the data dimension it sees is different from the original design. For example, if the input of the original program code is an array of length 10
[10] , it can be adjusted to a two-dimensional array[2][5].
[0063] Modifications to the data dimensions of output data, such as replacing a single scalar output with a matrix output containing the same result or redundant data structures (such as composite outputs of multiple values). Changing the output dimensions can make the code more difficult to interpret because analysts may need to understand additional data structures when trying to reverse engineer.
[0064] After the input and output data dimensions are adjusted, the changed original program code is passed to the compiler for compilation. The compiler will convert the changed original program code into an intermediate representation code or a low-level code, and then the electronic device will perform the operator code replacement process. The target program code generated in the end will be significantly different from the original program code. After the compilation process, the target program code can still achieve similar functions as the original program code, but the data structure and hierarchy have changed. This changed code structure is more complicated for external analysts and is not easy to understand directly.
[0065] Exemplarily, the original program code is used to describe the neural network model; while protecting the code security and privacy of the neural network model, the anti-reverse engineering capability of the model can be further improved by changing the dimensions of the input and output data of the neural network layer. Before the original program code is compiled by the compiler, the electronic device can change at least one of the data dimensions of the input data and the data dimensions of the output data of at least one neural network layer contained in the neural network model from the original dimension to other dimensions if the use conditions of the neural network model are met; wherein the use conditions indicate that the data dimension of the output data of the first neural network layer of two adjacent neural network layers and the data dimension of the input data of the second neural network layer are consistent. In this embodiment, by changing the dimensions of the input and output data, the code structure of the neural network model becomes unintuitive, which increases the difficulty of reverse analysis. Even if the code is captured by illegal elements, it is difficult to restore the original structure and logic of the model.
[0066] Specifically, after obtaining the original program code used to describe the neural network model, the electronic device first analyzes the neural network model to determine the neural network layer in the neural network model that can adjust the data dimension. General neural network layers include convolutional layers, pooling layers, fully connected layers, etc. When selecting a layer to change the dimension, it is necessary to ensure that the changed dimension meets the use conditions of the neural network model, that is, to ensure the consistency of the data dimension between two adjacent layers, so as to ensure that the neural network model is still effective in structure and will not affect the final function of the neural network model.
[0067] Regarding the change of input data dimension, for a selected neural network layer, the electronic device can change the input data dimension of the layer from the original dimension to another dimension. For example, if the original data is a 3*3 matrix, it can be converted into a 1*9 or 9*1 vector representation, or it can be converted into a 6*6 matrix. This operation changes the structure of the data, which can increase the difficulty of direct analysis of the original program code.
[0068] Regarding the change of output data dimension, for example, the output of the convolution layer is usually a multi-channel feature map, such as 8*8*32, and the output can be modified to a data representation of a different shape, such as 16*128, or changed to other different dimensions, such as 16*16*64, and the input dimension of the input data of the next neural network layer connected to the convolution layer is also changed to the same dimension to meet the use conditions of the neural network model. In this way, the complexity of the model data flow can be increased without affecting the input requirements of the next layer of the neural network.
[0069] After the input and output data dimensions are adjusted, the new neural network code is passed to the compiler for compilation. The compiler converts the new neural network code into an intermediate representation code or low-level code form, and then the electronic device performs the operator code replacement process. The generated target program code will be significantly different from the original code structure. The change in data dimension increases the ambiguity of the model and makes reverse engineering more challenging.
[0070] In some embodiments, see Figure 3 To further improve the privacy protection, before using the compiler to compile the original program code, the electronic device can divide the original program code into multiple code modules. Different code modules are used to implement different functions, and each code module implements a specific function or logic fragment, such as data processing, input and output, algorithm calculation, network communication, etc. This division can be based on functions, logical flows, or even the structure of a specific algorithm. For example, in a neural network model, the implementation of different neural network layers or activation functions, loss calculation and other functions can be divided into independent modules.
[0071] Then the electronic device replaces at least one of the multiple code modules with other code modules that can achieve similar functions. For example, among the multiple code modules, the electronic device selects at least one code module to be replaced, and for each code module to be replaced, other code modules with similar functions can be selected from a pre-designed set of candidate code modules for replacement. Each candidate code module in the set of candidate code modules implements similar functions, but differs in code structure, algorithm details, or operation path. Different candidate code modules can use different algorithms or implementation methods. For example, an encryption algorithm module can be replaced with an encryption module of another implementation method, and a data processing module can be replaced with a different sorting or filtering algorithm.
[0072] After the replacement is completed, the electronic device recombines multiple code modules to form a new program code. At this point, the new program code remains similar to the original program code in terms of function, and the code structure has changed because some code modules have been replaced. The electronic device can then use a compiler to compile the new program code to generate a code version with enhanced protection effects. In this embodiment, through code module replacement, the replaced new program code is no longer a direct translation of the original program code. The structural changes brought about by code module replacement make it additionally difficult for reverse engineering analysts to understand the code. Even if some modules are cracked, the logic of the entire code is not easy to restore.
[0073] For example, suppose that in a data analysis program, there is a set of code modules for data encryption, data processing, and communication. The data encryption module can be replaced with another encryption algorithm, and the data processing module can be replaced with a different sorting algorithm. In this way, even if the code is transmitted to an external device, it is difficult to restore the original code logic, which effectively improves the privacy protection level of the code.
[0074] Exemplarily, the original program code is used to describe a neural network model. The electronic device can divide the original program code to obtain multiple code modules, each of which is responsible for a specific operation process, such as an activation function, a normalization operation, a convolution operation, a pooling operation, etc. Then, the electronic device can determine at least one code module to be replaced from the multiple code modules, and the code module to be replaced is used to describe the activation function operation process or to describe the normalization operation process. Activation functions and normalization operations are core components of neural networks. Replacing them can hide the implementation details and design ideas of the original model and further protect the privacy of the model.
[0075] For each code module to be replaced, the electronic device obtains a second replacement set corresponding to the code module to be replaced, the second replacement set includes multiple candidate code modules with similar functions to the code module to be replaced, and randomly selects a candidate code module from the second replacement set to replace the code module to be replaced. This random replacement process can ensure the diversity of the code structure, so that each version of the code implementation is slightly different, which increases the complexity of the code.
[0076] For example, for a code module describing an activation function, its corresponding second replacement set may include a variety of different activation function implementations (such as ReLU, Leaky ReLU, Parametric ReLU, ELU, etc.). For a code module describing a normalization operation, its corresponding second replacement set includes multiple implementations of batch normalization, layer normalization, instance normalization, etc.
[0077] After the replacement operation is completed, the electronic device recombines multiple code modules to form a new neural network model code. Then it is compiled using a compiler to convert it from high-level code to other levels of code. The replaced and compiled code is diverse and non-intuitive, which effectively increases the difficulty of code analysis. For example, the new neural network model code can be compiled using an XLA (Accelerated Linear Algebra) compiler to convert it from high-level code to an intermediate representation HLO (High-Level Optimizer) code.
[0078] For example, suppose that in an image recognition neural network model, the original program code uses the sigmoid activation function and batch normalization module. The sigmoid activation function can be replaced with the tanh activation function, and the batch normalization can be replaced with the layer normalization module. These replacements will significantly change the code structure, making it difficult for outsiders to determine the specific design logic of the original model even if they obtain the code, thereby effectively improving the privacy protection and anti-reverse engineering capabilities of the code.
[0079] Understandably, see Figure 4 Before using the compiler to compile the original program code, the electronic device can change the data dimension of at least one of the input data and the output data in the original program code, and can further replace at least one code module among the multiple code modules obtained by dividing the original program code, and then use the compiler to compile the original program code after the above processing to obtain an intermediate representation code or a low-level code, and further replace at least one operator code in the intermediate representation code or the low-level code, and finally obtain the target program code, and the functional logic of the target program code is similar to the functional logic of the original program code.
[0080] In some embodiments, the target program code obtained by the above-mentioned program code protection method can replace the original program code and be sent to an external device to perform tasks that require privacy protection.
[0081] Exemplarily, the target program code may replace the original program code and be sent to an external device for program optimization.
[0082] See also Figure 5 as well as Figure 6 , shows a method for optimizing program code, which can be executed by electronic devices, including but not limited to servers, server clusters, virtual servers, PCs (Personal Computers), tablet devices, laptops, PDAs (Personal Digital Assistants), smart phones / mobile phones, etc. Exemplarily, the electronic device that executes the method for optimizing program code and the electronic device that executes the method for protecting program code can be the same device, or different devices belonging to the same organization, and this embodiment does not impose any restrictions on this. The method for optimizing program code includes:
[0083] In S501, a target program code to be optimized is obtained, where the target program code is obtained based on the above-mentioned program code protection method.
[0084] In this step, the target program code has gone through the above privacy protection process, ensuring that the optimization operation is targeted at the version that has been privacy protected or structurally changed. Even if the code is reversed, sensitive information will not be leaked during the optimization process, thus ensuring privacy protection and code security.
[0085] In S502, based on the target program code, prompt words are generated for use by the pre-trained model that has been trained; the prompt words include the target program code and processing requirements for the target program code, and the processing requirements are used to instruct the pre-trained model to give optimization suggestions for the target program code.
[0086] The processing requirements may be specifically set according to the actual application scenario, and this embodiment does not impose any restrictions on this. For example, the processing requirements may include but are not limited to: optimizing the execution efficiency of the code, reducing memory usage, reducing computational complexity, and increasing parallelism, etc. By clarifying the processing requirements, targeted optimization targets can be provided for the pre-trained model, thereby achieving efficient optimization of the code.
[0087] For example, see Figure 6 , the electronic device maintains a database containing multiple prompt word templates. These prompt word templates are predefined and designed according to different optimization goals and requirements. Each template includes the format and content of the prompt word, which can guide the pre-trained model to understand and perform specific optimization tasks. For example, the prompt word template can be designed for different optimization goals, such as performance improvement, memory optimization, calculation efficiency optimization, algorithm improvement, etc. The electronic device can select a prompt word template as a target prompt word template from a database containing several prompt word templates. This embodiment can automatically generate prompt words according to different scenarios and requirements by selecting a suitable prompt word template from the database, reducing the complexity of manual configuration.
[0088] In one possible implementation, each prompt word template includes a processing requirement and at least one processing example that meets the processing requirement; the processing example includes sample code and sample optimization suggestions for the sample code. The sample code is used to demonstrate how to implement a specific optimization task or processing procedure; for example, if the target program code is the code of a deep learning model, the sample code may include how to optimize the code of the convolution layer or how to reduce memory usage. The sample optimization suggestions may include code refactoring suggestions, algorithm optimization, performance improvement techniques, etc. This suggestion will tell the pre-trained model how to improve the target program code based on the implementation method of the sample code. Through such a structure, the prompt word template not only provides an optimization goal, but also provides a demonstration for the pre-trained model on how to achieve the goal, so that the pre-trained model can generate optimization suggestions based on a clearer direction.
[0089] When selecting a target prompt word template, the electronic device can select a prompt word template from a plurality of prompt word templates as the target prompt word template based on the difference between the code length of the target program code and the code length of the sample code included in each prompt word template in the database. Wherein, the difference between the code length of the sample code included in the target prompt word template and the code length of the target program code is less than the preset difference, or the difference between the code length of the sample code included in the target prompt word template and the code length of the target program code is the smallest. In this implementation, by selecting a suitable template according to the code length difference, it can be ensured that the selected prompt word template is more consistent with the target program code in structure, which helps to improve the relevance and practicality of generating optimization suggestions.
[0090] In another possible implementation, the target program code is used to describe a neural network model. Different prompt word templates among the prompt word templates included in the database correspond to different neural network architectures, and the different neural network architectures include but are not limited to convolutional neural networks, recurrent neural networks, fully connected neural networks, etc. Different neural network architectures have different optimization requirements. Designing different prompt word templates for different architectures can ensure the pertinence and effectiveness of the optimization suggestions.
[0091] When selecting a target prompt word template, the electronic device can select a target prompt word template with a matching architecture from a number of prompt word templates based on the neural network architecture of the neural network model described by the target program code. For example, if the target program code describes a convolutional neural network model, the electronic device will select a prompt word template specifically optimized for convolutional neural networks from a database, rather than other types of prompt word templates. In this implementation, by selecting the most matching prompt word template based on the neural network architecture, it can be ensured that the optimization suggestions are directly adapted to the specific requirements of the neural network model described by the target program code, avoiding irrelevant optimization strategies; and it can improve the efficiency of the optimization suggestions, reduce the waste of irrelevant optimization, and provide targeted improvement plans.
[0092] In another possible implementation, the target program code is used to describe the neural network model. Different prompt word templates among the several prompt word templates included in the database correspond to different neural network layers. For example, the database includes three prompt word templates: the first prompt word template corresponds to a small neural network (1-5 layers); the second prompt word template corresponds to a medium-sized neural network (6-20 layers), such as a deep convolutional or recurrent network for medium-level complex tasks; the third prompt word template corresponds to a large neural network (more than 20 layers), such as a large convolutional neural network or a complex recursive neural network, suitable for complex image, text processing or natural language processing tasks. Each prompt word template will contain optimization suggestions that match a specific number of layers, such as lightweight processing suggestions for small networks, computing acceleration solutions for medium-level networks, and memory optimization and inter-layer communication optimization suggestions for large-layer networks.
[0093] When selecting a target prompt word template, the electronic device can select a target prompt word template with a matching number of layers from a number of prompt word templates based on the number of neural network layers contained in the neural network model described by the target program code. Networks with different numbers of layers have obvious differences in computing resources and optimization requirements. Selecting a template based on the number of layers can match the optimization suggestions with the scale of the network, effectively adapt to the complexity of the neural network, and ensure the accuracy of the optimization measures.
[0094] Exemplarily, once a suitable prompt word template is selected, the electronic device can embed the target program code into the target prompt word template to generate prompt words for use by the pre-trained model that has been trained. The prompt word template has a placeholder or a designated area for inserting specific target program code. The embedding process ensures the integrity of the target program code, while other parts in the template provide necessary context information so that the pre-trained model can correctly understand and process the program code. By embedding the template, structured prompt words can be quickly generated, saving the time of manually writing and adjusting prompt words.
[0095] The generated prompt words include but are not limited to: (1) target program code, that is, the code that needs to be optimized, the program code after protection or modification. (2) processing requirements, that is, the specific optimization requirements for the target program code, indicating what types of optimizations the pre-trained model needs to perform (such as code efficiency, memory optimization, computing speed, etc.). (3) at least one processing example that meets the processing requirements; the processing example includes sample code and example optimization suggestions for the sample code. (4) role setting information. The role setting can help the pre-trained model understand the actions that should be taken in specific situations; for example, if the role is set to "machine learning algorithm expert", the pre-trained model will focus more on efficient and maintainable optimization solutions.
[0096] For example, the generated prompt is: You are a machine learning algorithm expert. This is an intermediate representation code of a machine learning algorithm: *****. Please make ** optimization suggestions for my algorithm ideas based on your understanding of this intermediate representation code. The following is the sample code and the sample optimization suggestions for the sample code*****.
[0097] In S503, the prompt word is sent to an external device that deploys the pre-trained model, so that the external device inputs the prompt word into the pre-trained model to generate optimization suggestions.
[0098] In this step, the generated prompt word is sent to the external device deployed with the pre-trained model. After receiving the prompt word, the external device will input it into the pre-trained model, and the pre-trained model will generate optimization suggestions based on the program code and optimization requirements provided in the prompt word. The pre-trained model can quickly identify potential bottlenecks in the code and propose corresponding optimization solutions through a large amount of training data and optimization experience. This embodiment does not impose any restrictions on the specific structure of the pre-trained model, and it can be specifically selected according to the actual application scenario.
[0099] In S504, the optimization suggestion returned by the external device is received.
[0100] In this step, the optimization suggestions returned by the external device are analyzed and applied to the target program code. The optimization suggestions can cover multiple aspects, such as: (1) algorithm optimization, replacing inefficient parts with more efficient algorithms or data structures. (2) memory optimization, reducing memory usage, optimizing memory access patterns, etc. (3) computing efficiency optimization, reducing redundant calculations, parallel processing, etc. But not limited to these.
[0101] For example, suppose the code of a deep learning model for image recognition needs to be optimized. In this case, the electronic device can select a prompt word template from the database that is specifically optimized for deep learning models. The template may include requirements for algorithm acceleration, memory optimization, batch computing, etc. After the model's program code is embedded in this template, the generated prompt words will be transmitted to the pre-trained model of the external device, and the external device will return corresponding optimization suggestions based on the prompt words, such as accelerating the model's reasoning process through more efficient convolution operations or memory optimization strategies.
[0102] It is understandable that the original program code cannot usually be directly exposed to external devices in scenarios where sensitive information protection and privacy security requirements are high. By generating a target program code to replace the original program code, the risk of leakage of the original code can be effectively avoided, while still being able to obtain code optimization suggestions from external devices. Since the logical functions executed by the target program code and the original program code are similar, the optimization suggestions obtained from the external device can be applied to the original program code directly or after a small amount of adjustment. Therefore, the above method can achieve optimization effects without compromising the security of the original code, and improve code execution efficiency and performance.
[0103] In other embodiments, the target program code generated by the above-mentioned program code protection method can also be applied to other scenarios.
[0104] In an exemplary application scenario, the target program code can be used as a case for programming training and education, facilitating the sharing of code ideas and implementation methods while effectively protecting the core logic and sensitive data of the original code.
[0105] The various technical features in the above embodiments can be arbitrarily combined as long as there is no conflict or contradiction between the combinations of features. However, due to space limitations, they are not described one by one. Therefore, any combination of the various technical features in the above embodiments also falls within the scope of this specification.
[0106] In some embodiments, the embodiments of this specification also provide an electronic device, including: a processor; a memory for storing processor executable instructions; wherein the processor implements any of the above methods by running the executable instructions.
[0107] Figure 7 is a schematic structural diagram of an electronic device provided by an exemplary embodiment. Figure 7 At the hardware level, the device includes a processor 702, an internal bus 704, a network interface 706, a memory 708, and a non-volatile memory 710, and may also include hardware required for other functions. One or more embodiments of this specification may be implemented based on software, such as the processor 702 reading the corresponding computer program from the non-volatile memory 710 into the memory 708 and then running it. Of course, in addition to the software implementation, one or more embodiments of this specification do not exclude other implementations, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0108] In some embodiments, the program code protection device can be applied to Figure 7In the electronic device shown in the figure, the technical solution of this specification is implemented. Among them, the protection device of the program code may include:
[0109] The code acquisition module is used to obtain the original program code.
[0110] The compiling module is used to compile the original program code using a compiler to convert the original program code from a high-level code into other-level codes, wherein the other-level codes include at least one of an intermediate representation code and a low-level code.
[0111] A replacement module is used to replace at least one operator code in the other level code with other operator code that can achieve similar functions, and use the replaced other level code as the target program code; wherein the target program code is used to replace the original program code and sent to an external device to perform tasks with privacy protection requirements.
[0112] In one implementation, the replacement module is specifically used to determine at least one operator code to be replaced from the other level codes; for each operator code to be replaced, obtain a first replacement set corresponding to the operator code to be replaced, the first replacement set includes multiple candidate operator codes with similar functions to the operator code to be replaced, and randomly select a candidate operator code from the first replacement set to replace the operator code to be replaced.
[0113] In one implementation, the replacement module is further used to change the data dimension of the input data and / or the data dimension of the output data defined in the original program code from the original dimension to another dimension.
[0114] In one implementation, the original program code is used to describe a neural network model.
[0115] A replacement module is specifically used to change the data dimension of the input data and / or the data dimension of the output data of at least one neural network layer contained in the neural network model from the original dimension to other dimensions when the use conditions of the neural network model are met; wherein the use conditions indicate that the data dimension of the output data of the previous neural network layer of two adjacent neural network layers and the data dimension of the input data of the next neural network layer remain consistent.
[0116] In one implementation, the replacement module is further used to divide the original program code to obtain multiple code modules, where different code modules are used to implement different functions; and replace at least one code module among the multiple code modules with other code modules that can implement similar functions.
[0117] In one implementation, the original program code is used to describe a neural network model.
[0118] A replacement module is specifically used to determine at least one code module to be replaced from the multiple code modules, wherein the code module to be replaced is used to describe an activation function operation process or a normalization operation process; for each of the code modules to be replaced, a second replacement set corresponding to the code module to be replaced is obtained, wherein the second replacement set includes multiple candidate code modules having similar functions to the code module to be replaced, and a candidate code module is randomly selected from the second replacement set to replace the code module to be replaced.
[0119] In other embodiments, the program code optimization device can be applied to Figure 7 In the electronic device shown in the figure, the technical solution of this specification is implemented. Among them, the optimization device of the program code may include:
[0120] The code acquisition module is used to acquire the target program code to be optimized, wherein the target program code is obtained based on the above-mentioned program code protection device.
[0121] A prompt word generation module is used to generate prompt words for use by a pre-trained model that has been trained based on the target program code; the prompt words include the target program code and processing requirements for the target program code, and the processing requirements are used to instruct the pre-trained model to give optimization suggestions for the target program code.
[0122] A prompt word sending module, used for sending the prompt word to an external device where the pre-trained model is deployed, so that the external device inputs the prompt word into the pre-trained model to generate the optimization suggestion;
[0123] The optimization suggestion receiving module is used to receive the optimization suggestion returned by the external device.
[0124] In one implementation, the prompt word generation module is specifically used to select a prompt word template as a target prompt word template from a database containing several prompt word templates; embed the target program code into the target prompt word template to generate prompt words for use by the pre-trained model that has been trained.
[0125] In one implementation, each of the prompt word templates includes the processing requirement and at least one processing example that meets the processing requirement; the processing example includes a sample code and a sample optimization suggestion for the sample code.
[0126] The prompt word generating module is specifically used to select a prompt word template from the plurality of prompt word templates as a target prompt word template based on the difference between the code length of the target program code and the code length of the sample code included in each prompt word template in the database; wherein the difference between the code length of the sample code included in the target prompt word template and the code length of the target program code is less than a preset difference, or the difference between the code length of the sample code included in the target prompt word template and the code length of the target program code is the smallest.
[0127] In one implementation, the target program code is used to describe a neural network model.
[0128] Exemplarily, different prompt word templates among the several prompt word templates included in the database correspond to different neural network architectures; wherein the target prompt word template is selected from the several prompt word templates based on the neural network architecture corresponding to the target program code.
[0129] Exemplarily, different prompt word templates among the several prompt word templates included in the database correspond to different numbers of neural network layers; wherein the target prompt word template is selected from the several prompt word templates based on the number of neural network layers contained in the neural network model described by the target program code.
[0130] The implementation process of the functions and effects of each module in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, which will not be repeated here.
[0131] Based on the same concept as the above method, this specification also provides an electronic device, including: a processor; a memory for storing processor executable instructions; wherein the processor implements the steps of the method described in any of the above embodiments by running the executable instructions.
[0132] Based on the same concept as the above method, this specification also provides a computer-readable storage medium on which computer instructions are stored. When the instructions are executed by a processor, the steps of the method described in any of the above embodiments are implemented.
[0133] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information by any method or technology. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0134] Based on the same concept as the above method, this specification also provides a computer program product, including a computer program / instruction, which implements the steps of the method described in any of the above embodiments when executed by a processor.
[0135] The above description is merely a preferred embodiment of one or more embodiments of the present specification and is not intended to limit one or more embodiments of the present specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of the present specification shall be included in the scope of protection of one or more embodiments of the present specification.
Claims
1. A method for protecting program code, comprising: Get the original program code; Compiling the original program code using a compiler to convert the original program code from a high-level code into another level of code, wherein the other level of code includes at least one of an intermediate representation code and a low-level code; At least one operator code in the other level code is replaced with other operator code that can achieve similar functions, and the replaced other level code is used as the target program code; wherein the target program code is used to replace the original program code and sent to an external device to perform tasks with privacy protection requirements.
2. According to the method of claim 1, replacing at least one operator code in the other level codes with other operator codes capable of implementing similar functions comprises: Determining at least one operator code to be replaced from the other level codes; For each of the operator codes to be replaced, a first replacement set corresponding to the operator code to be replaced is obtained, the first replacement set including multiple candidate operator codes having similar functions to the operator code to be replaced, and a candidate operator code is randomly selected from the first replacement set to replace the operator code to be replaced.
3. The method according to claim 1, before compiling the original program code using a compiler, further comprises: The data dimension of the input data and / or the data dimension of the output data defined in the original program code is changed from the original dimension to another dimension.
4. The method according to claim 3, wherein the original program code is used to describe a neural network model; The step of changing the data dimension of the input data and / or the data dimension of the output data defined in the original program code from the original dimension to another dimension comprises: When the use conditions of the neural network model are met, changing the data dimension of input data and / or the data dimension of output data of at least one neural network layer included in the neural network model from the original dimension to another dimension; Among them, the usage condition indicates that the data dimension of the output data of the previous neural network layer in two adjacent neural network layers is consistent with the data dimension of the input data of the next neural network layer.
5. The method according to claim 1, before compiling the original program code using a compiler, further comprises: Dividing the original program code to obtain a plurality of code modules, wherein different code modules are used to implement different functions; At least one code module among the multiple code modules is replaced with another code module capable of implementing similar functions.
6. The method according to claim 5, wherein the original program code is used to describe a neural network model; The step of replacing at least one of the plurality of code modules with another code module capable of implementing similar functions comprises: Determining at least one code module to be replaced from the multiple code modules, wherein the code module to be replaced is used to describe an activation function operation process or a normalization operation process; For each of the code modules to be replaced, a second replacement set corresponding to the code module to be replaced is obtained, the second replacement set includes multiple candidate code modules having similar functions to the code module to be replaced, and a candidate code module is randomly selected from the second replacement set to replace the code module to be replaced.
7. A method for optimizing a program code, comprising: Obtaining a target program code to be optimized, wherein the target program code is obtained based on the program code protection method according to any one of claims 1 to 6; Based on the target program code, generating a prompt word for use by a pre-trained model that has been trained; the prompt word includes the target program code and a processing requirement for the target program code, the processing requirement is used to instruct the pre-trained model to give optimization suggestions for the target program code; Sending the prompt word to an external device that deploys the pre-trained model, so that the external device inputs the prompt word into the pre-trained model to generate the optimization suggestion; Receive the optimization suggestion returned by the external device.
8. The method according to claim 7, wherein generating prompt words for use by the pre-trained model based on the target program code comprises: Selecting a prompt word template as a target prompt word template from a database containing a plurality of prompt word templates; The target program code is embedded in the target prompt word template to generate prompt words for use by the pre-trained model that has been trained.
9. The method according to claim 8, wherein each of the prompt word templates comprises the processing requirement and at least one processing example that satisfies the processing requirement; the processing example comprises a sample code and a sample optimization suggestion for the sample code; The step of selecting a prompt word template as a target prompt word template from a database containing a plurality of prompt word templates includes: Selecting a prompt word template from the plurality of prompt word templates as a target prompt word template based on a difference between a code length of the target program code and a code length of a sample code included in each prompt word template in the database; The difference between the code length of the example code included in the target prompt word template and the code length of the target program code is smaller than a preset difference, or the difference between the code length of the example code included in the target prompt word template and the code length of the target program code is minimal.
10. The method according to claim 8, wherein the target program code is used to describe a neural network model; Different prompt word templates among the prompt word templates included in the database correspond to different neural network architectures; wherein, The target prompt word template is selected from the plurality of prompt word templates based on a neural network architecture corresponding to the target program code; and / or, Different prompt word templates among the several prompt word templates included in the database correspond to different numbers of neural network layers; wherein the target prompt word template is selected from the several prompt word templates based on the number of neural network layers contained in the neural network model described by the target program code.
11. An electronic device, comprising: processor; A memory for storing processor-executable instructions; wherein the processor implements the steps of the method according to any one of claims 1 to 10 by executing the executable instructions.
12. A computer-readable storage medium having computer instructions stored thereon, which, when executed by a processor, implement the steps of the method according to any one of claims 1 to 10.
13. A computer program product comprising a computer program / instructions, which when executed by a processor implement the steps of the method according to any one of claims 1 to 10.
Citation Information
Patent Citations
Method for optimizing cue word and electronic equipment
CN117272988A
Code optimization method and device, electronic equipment and storage medium
CN117873489A
Model file protection method and device
CN118194352A