Function library linking method, device, equipment, storage medium and program product
By grouping function libraries and static and dynamic linking, the link failure problem caused by the long distance of the function library in the shared instruction space accelerator is solved, and the normal operation and efficient execution of large programs on the accelerator is achieved.
Patent Information
- Application Number
- CN202411470314.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-08-22
- Estimated Expiration
- 2044-10-21
AI Technical Summary
In shared instruction space accelerators, due to the far distance between function libraries, the link fails, and the existing technology is difficult to effectively solve the problem of function library calling in large programs.
Divide multiple function libraries into multiple function library groups, where the call distance between function libraries in the same group is limited, merge them into static link files through static linking, and dynamic linking is performed to form a dynamic link library to reduce the jump distance between function libraries.
It effectively avoids the call failure of the shared instruction space accelerator, supports large programs to run normally on the accelerator, and improves the execution efficiency and flexibility of the program.
Smart Images

Figure CN119440672B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a function library linking method, apparatus, device, storage medium, and program product. Background Art
[0002] An artificial intelligence tensor accelerator is a hardware device that can be installed in a computer and used in combination with the computer's CPU (Central Processing Unit). When the CPU and accelerator are used in combination, the CPU reads CPU-specific instructions and performs various general-purpose calculations according to the instructions, while the accelerator reads accelerator-specific instructions and processes tensor calculations in the artificial intelligence program according to the instructions. According to the different ways of reading instructions, accelerators can be divided into accelerators with private instruction memory and accelerators with shared instruction memory. Accelerators with shared instruction memory can read instructions directly from the memory space where CPU instructions are stored. Therefore, users can store CPU instructions and accelerator instructions in the same memory space. When executing an artificial intelligence program, the artificial intelligence program and the library containing the instructions corresponding to the program will be linked together. Due to the limited function jump range on the accelerator, the mixed storage of CPU instructions and accelerator instructions may cause the addresses between the accelerator instructions to be too far apart, resulting in a link failure and the failure of the program to run.
[0003] The above content is only used to assist in understanding the technical solution of this application and does not constitute an admission that the above content is prior art. Summary of the Invention
[0004] The main purpose of this application is to provide a function library linking method, device, equipment, storage medium and program product, aiming to solve the technical problem of shared instruction space accelerator call failure.
[0005] To achieve the above objectives, the present application proposes a function library linking method, which includes:
[0006] Dividing multiple function libraries into multiple function library groups, wherein a calling distance between a first function library and at least one second function library in the same function library group is limited, and a calling distance between function libraries in different function library groups is not limited, wherein the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library among the function libraries in the same function library group other than the first function library;
[0007] Static linking the function libraries within the multiple function library groups to obtain static link files of the multiple function libraries;
[0008] The static link files are dynamically linked to obtain a dynamic link library.
[0009] In one embodiment, the step of dividing the plurality of function libraries into a plurality of function library groups includes:
[0010] For any target function library among the multiple function libraries, if a call-type instruction exists in the target function library, traversing each of the call-type instructions in the target function library to determine a call start function and a call destination function of the call-type instruction, wherein the call start function is a function where the call-type instruction calls and a function where the call destination function of the call-type instruction calls;
[0011] If the function types of the calling start function and the calling target function are both accelerator functions, the target function library and the function library where the calling target function is located are divided into the same function library group.
[0012] In one embodiment, the step of statically linking the function libraries within the multiple function library groups to obtain the static link files of the multiple function libraries includes:
[0013] For any target function library group among the plurality of function library groups, inputting library files of each function library in the target function library group into a static linker, merging multiple segments with the same attributes in each library file into a target segment through the static linker, and copying each target segment to a preset target file to obtain a merged file;
[0014] Parsing each of the library files by the static linker to obtain each symbol in the library file, searching for each actual memory address of each of the symbols in the target end by the static linker, and constructing a static symbol table by the static linker based on each actual memory address of each of the symbols and each of the symbols;
[0015] The merged file and the static symbol table are packaged into an executable file by the static linker, and the packaged executable file is used as the static link file corresponding to the target function library group.
[0016] In one embodiment, before the step of statically linking the function libraries within the multiple function library groups to obtain the static link files of the multiple function libraries, the method further includes:
[0017] Creating an empty source file, and using the empty source file as a statically linked main program;
[0018] The step of copying each of the target segments to a preset target file to obtain a merged file includes:
[0019] Copy each of the target segments to the statically linked main program to obtain a merged file.
[0020] In one embodiment, the step of dynamically linking each of the static link files to obtain a dynamic link library includes:
[0021] Inputting each of the static link files into a dynamic linker, and allocating a space address to each of the static link files through the dynamic linker;
[0022] Reading each symbol in each of the static link files by the dynamic linker, and relocating each symbol based on the spatial address of each of the static link files to obtain dynamic address information of each symbol;
[0023] A dynamic symbol table is generated based on the symbols and the dynamic address information of the symbols, and the dynamic symbol table is added to a preset shared library to obtain a dynamic link library.
[0024] In one embodiment, if there is a function library from the packaged compiler, the step of dynamically linking each of the static link files to obtain a dynamic link library further includes:
[0025] Dynamically linking each of the static link files using a non-encapsulated compiler to obtain a basic link library;
[0026] The function library from the packaged compiler is linked to the basic link library to obtain a dynamic link library.
[0027] In addition, to achieve the above purpose, the present application also proposes a function library linking method, the method comprising:
[0028] a grouping module, configured to group multiple function libraries into multiple function library groups, wherein a calling distance between a first function library and at least one second function library in the same function library group is limited, and a calling distance between function libraries in different function library groups is not limited, wherein the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library among the function libraries in the same function library group other than the first function library;
[0029] a static link module, configured to statically link the function libraries within the plurality of function library groups to obtain respective static link files of the plurality of function libraries;
[0030] The dynamic link module is used to dynamically link the static link files to obtain a dynamic link library.
[0031] In addition, to achieve the above-mentioned purpose, the present application also proposes a function library linking device, which includes:
[0032] In addition, to achieve the above-mentioned purpose, the present application also proposes a function library linking device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the function library linking method described above.
[0033] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium. A computer program is stored on the storage medium, and when the computer program is executed by the processor, the steps of the function library linking method described above are implemented.
[0034] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps of the function library linking method described above are implemented.
[0035] In the present application, multiple function libraries are divided into multiple function library groups, wherein the calling distance between a first function library and at least one second function library in the same function library group is limited, and the calling distance between function libraries in different function library groups is not limited, the first function library is any function library among the various function libraries in the same function library group, and the second function library is a function library other than the first function library among the various function libraries in the same function library group; the function libraries within the multiple function library groups are statically linked to obtain static link files of the multiple function libraries; and each of the static link files is dynamically linked to obtain a dynamic link library.
[0036] This application divides each function library with limited calling distance into the same group, and statically links the function libraries in the same group. Static linking can merge these libraries into a single static link file. The libraries inside each static link file have been tightly combined together through static linking. The jump distance between libraries in the same function library group is limited to a smaller range, thereby reducing the jump distance between function libraries and avoiding the situation where a shared instruction space accelerator fails to call. At the same time, each static link file is dynamically linked to form a dynamic link library. Since these static link files have merged the code of the function libraries in the group, calls between static link files can be implemented through jumps within the dynamic link library without crossing the entire address space, thereby reducing the jump distance between function libraries and avoiding the situation where a shared instruction space accelerator fails to call. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0038] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0039] Figure 1 A schematic diagram of a scenario of a general function library linking method provided in an embodiment of the present application;
[0040] Figure 2 A flowchart of the first embodiment of the method for linking a function library of the present application is provided;
[0041] Figure 3 A schematic diagram of a function library grouping scenario of a function library linking method provided in an embodiment of the present application;
[0042] Figure 4 A schematic diagram of a function library merging scenario of a function library linking method provided in an embodiment of the present application;
[0043] Figure 5 A schematic diagram of a static linking scenario of a function library linking method provided in one embodiment of the present application;
[0044] Figure 6 This is a schematic diagram of the module structure of the function library linking device according to an embodiment of the present application;
[0045] Figure 7 This is a schematic diagram of the device structure of the hardware operating environment involved in the function library linking method in the embodiment of the present application.
[0046] The purpose, features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0047] It should be understood that the specific embodiments described herein are merely used to explain the technical solutions of the present application and are not intended to limit the present application.
[0048] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0049] The main solution of the embodiment of the present application is: by dividing multiple function libraries into multiple function library groups, wherein the calling distance between a first function library and at least one second function library in the same function library group is limited, and the calling distance between function libraries in different function library groups is not limited, the first function library is any function library among the various function libraries in the same function library group, and the second function library is a function library other than the first function library among the various function libraries in the same function library group; statically linking the function libraries within the multiple function library groups to obtain static link files of the multiple function libraries; and dynamically linking each of the static link files to obtain a dynamic link library.
[0050] For ease of presentation, this embodiment uses the library linking device as the execution entity. An AI tensor accelerator is a hardware device installed within a computer that, like the CPU, serves as the computer's computing unit. While the CPU can perform various general-purpose computations, it offers relatively low performance. The accelerator has limited functionality and can only handle tensor computations within AI programs, offering relatively high performance. By combining the CPU and AI accelerator, a complete AI program can be efficiently executed, integrating both tensor computations and peripheral general-purpose computations.
[0051] To mix the use of CPU and accelerator, the CPU needs to read CPU-specific instructions, and the accelerator needs to read the accelerator-specific instructions, and these instructions together constitute a complete program. According to the different ways of reading instructions, accelerators can be divided into accelerators with private instruction memory and accelerators with shared instruction memory. The embodiments of the present application are applied to accelerators with shared instruction memory. Such accelerators can read instructions directly from the memory space where CPU instructions are stored, without using a dedicated memory to store accelerator instructions, which simplifies the steps of reading instructions. When using such accelerators, users only need to store CPU instructions and accelerator instructions in the same memory space.
[0052] Different artificial intelligence programs need to perform the same or similar calculations. These same or similar calculations can be summarized into multiple functions and written in various function libraries (hereinafter referred to as libraries). The same library can be used by multiple artificial intelligence programs, and each artificial intelligence program may also use multiple libraries at the same time. Some libraries contain accelerator instructions executed on the accelerator, and some libraries contain CPU instructions executed on the CPU. To use multiple libraries in an artificial intelligence program, you need to link this program and these libraries together. Please refer to Figure 1 In the scenario diagram shown, during the linking process, the program (main program), library A, and library B are linked. The instructions in the linked program and library are encoded in a unified address space, where each instruction is assigned a unique address. During the linking process, the call addresses of each function in the program and library are rewritten to this unique address, allowing the functions in the program and library to call each other.
[0053] Linking can be categorized as static linking or dynamic linking. With static linking, the order of libraries in the address space is determined immediately. With dynamic linking, the order of libraries in the address space is determined on demand at runtime, and the specific order varies depending on the actual execution of the program. Python is a mainstream programming language for AI development. To use libraries in Python, dynamic linking is required between the Python interpreter (i.e., the main Python program) and the libraries.
[0054] Linking can be done in stages. For example, if you want to link library A, library B, and library C together, you can first link library A and library B into library X, and then link library X into library C. When linking in stages, you can use only static linking, only dynamic linking, or static linking first and then dynamic linking, but you cannot use dynamic linking first and then static linking.
[0055] Because accelerators sacrifice functionality for performance, the range of function jumps on many accelerators is limited. That is, if you want to call an instruction at address Y from an instruction at address X, the distance between X and Y cannot be too far. If there are many libraries to link, and library A and library B that call each other are distributed at opposite ends of the address space, the jump distance is likely to be too far, resulting in a link failure and, in turn, a program that cannot run. Because accelerators with shared instruction memory do not distinguish between libraries executed on the accelerator and those executed on the CPU, many CPU libraries will be inserted between multiple accelerator libraries after using a common linking method, further increasing the distance between accelerator libraries and exacerbating this problem.
[0056] Due to this problem, the universal linking method can only be used for small programs that only use a small number of libraries. However, the embodiment of the present application can avoid the problem of excessive jump distances by properly adjusting the order of the linked libraries, thereby enabling large programs that require linking a large number of libraries to run on the accelerator.
[0057] In an embodiment of the present application, a solution is provided, which is to divide each function library with limited calling distance into the same group, and statically link the function libraries in the same group. Static linking can merge these libraries into a single static link file. The libraries inside each static link file have been tightly combined together through static linking. The jump distance between libraries in the same function library group is limited to a smaller range, thereby reducing the jump distance between function libraries and avoiding the situation where a shared instruction space accelerator fails to call. At the same time, each static link file is dynamically linked to form a dynamic link library. Since these static link files have merged the code of the function libraries in the group, calls between static link files can be implemented through jumps within the dynamic link library without crossing the entire address space, thereby reducing the jump distance between function libraries and avoiding the situation where a shared instruction space accelerator fails to call.
[0058] It should be noted that the execution subject of this embodiment may be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, mobile phone, server, etc., or an electronic device or function library linking device capable of implementing the above functions. This embodiment and the following embodiments will be described below using a function library linking device as an example.
[0059] Based on this, the embodiment of the present application provides a function library linking method, referring to Figure 2 , Figure 2 This is a flow chart of the first embodiment of the function library linking method of this application.
[0060] In this embodiment, the function library linking method includes steps S10 to S30:
[0061] Step S10: divide multiple function libraries into multiple function library groups, wherein the calling distance between a first function library and at least one second function library in the same function library group is limited, and the calling distance between function libraries in different function library groups is not limited, the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library among the function libraries in the same function library group except the first function library.
[0062] It's important to note that a function library is a collection of pre-written functions that programs can call to implement specific functionality. A function library group groups multiple function libraries according to certain rules or standards for easier management and optimization. Call distance generally refers to the dependencies between functions or modules. Limited call distance means that calls between functions must meet specific conditions or rules.
[0063] In this embodiment, the calling relationships between function libraries are analyzed, and function libraries with close calling relationships are grouped into the same group, while ensuring that the calling distance within a group is limited, while the calling distance between groups is not limited. In a specific embodiment, the process of grouping multiple function libraries into multiple function library groups may include:
[0064] Step S101: For any target function library among the multiple function libraries, if there is a call-type instruction in the target function library, traverse each of the call-type instructions in the target function library to determine the call start function and the call destination function of the call-type instruction, wherein the call start function is the function where the call-type instruction calls and the function where the call destination of the call-type instruction calls.
[0065] It should be noted that the call-type instruction is an instruction or statement used in the program to call other functions. The call start function is the function that executes the call-type instruction, that is, the function that initiates the call, and the call destination function is the function called by the call-type instruction, that is, the target function that receives the call.
[0066] Functions can be classified into different function types according to their functions or uses. In this embodiment, they can be divided into accelerator functions and processor functions. Accelerator functions refer to functions executed by accelerators to process tensor calculations in artificial intelligence programs, and processor functions refer to functions executed by central processing units to process routine computing tasks.
[0067] In this implementation, a compiler or static analysis tool is used to scan the target library's source code, identifying all call instructions and recording their origin and destination functions. By accurately identifying the calling relationships between functions, the library can be more rationally grouped, ensuring optimal function call locality, potentially reducing program execution time and memory usage.
[0068] Step S102: If the function types of the calling start function and the calling target function are both accelerator functions, the target function library and the function library where the calling target function is located are divided into the same function library group.
[0069] After identifying the call origin and destination functions, the system checks their metadata or annotations to determine whether both are accelerator functions. If so, the function libraries containing them are grouped together. Grouping accelerator functions with the same functionality into the same group allows these libraries to more effectively utilize hardware acceleration or other optimization measures during compilation or linking, improving overall program performance. Proper grouping reduces jump distances between libraries and prevents call failures in accelerators with shared instruction space.
[0070] Step S20 , statically linking the function libraries within the multiple function library groups to obtain static link files for the multiple function libraries.
[0071] When the program is compiled, the required library files are directly embedded into the final executable file, so that the final executable file contains all the necessary codes.
[0072] The process of using a static linker to link all libraries within the same library group into a single static link file is not limited here and can be set up according to actual needs and system architecture. For example, you can use the static linking option of a compiler, such as using gcc (a compiler) on Linux systems, to compile the function libraries and the main program into a single executable file. Alternatively, you can set project properties in an IDE (Integrated Development Environment) to specify the libraries to be statically linked, and let the IDE automatically handle static linking during the build process.
[0073] Static linking can reduce runtime loading time because all necessary code is already included in the executable file, and it can also avoid version conflict problems.
[0074] Step S30: Dynamically link the static link files to obtain a dynamic link library.
[0075] Dynamic linking: library files are dynamically loaded and linked as needed when the program is running, rather than statically embedded at compile time.
[0076] A dynamic linker is used to convert the generated static link files into dynamic link libraries, which are loaded and linked when the program is running. Dynamic linking provides greater flexibility, allowing individual modules to be updated without recompiling the entire program. It also supports a plug-in architecture, which facilitates functional expansion and customization. The flexibility of dynamic linking is crucial for the Python language, especially when it is necessary to call libraries running on accelerators. Python is an interpreted language and it does not usually interact directly with hardware accelerators. However, through dynamic link libraries, Python can communicate with these accelerators efficiently. Dynamic link libraries allow Python to dynamically load and link to code on the accelerator at runtime, which means that Python programs can take advantage of the computing power provided by the accelerator without stopping or restarting. This instant loading and linking mechanism enables Python to easily integrate and utilize the latest algorithms and functions on the accelerator without recompiling the entire program. In addition, the use of dynamic link libraries also simplifies the interface design between Python and accelerators. Developers can call complex accelerator functions by writing concise Python code, thereby improving development efficiency and program maintainability. This flexibility and ease of use make Python one of the preferred languages for interacting with hardware accelerators in fields such as scientific computing, data analysis, and machine learning.
[0077] In this embodiment, by grouping the function libraries with limited call distances into the same group, statically linking the function libraries within the same group, static linking can merge these libraries into a single static link file. The libraries within each static link file have been tightly bound together through static linking, and the jump distances between libraries in the same function library group are limited to a smaller range, thereby reducing the jump distances between function libraries and avoiding call failures in the shared instruction space accelerator. At the same time, the static link files are dynamically linked to form dynamic link libraries. Since these static link files have merged the code of the function libraries within the group, calls between static link files can be implemented through jumps within the dynamic link library without having to cross the entire address space, thereby reducing the jump distances between function libraries and avoiding call failures in the shared instruction space accelerator.
[0078] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the first embodiment can be referred to above and will not be described in detail. On this basis, the step S20: statically linking the function libraries within the multiple function library groups to obtain the static link files of the multiple function libraries includes steps S201 to S203:
[0079] Step S201: For any target function library group among the multiple function library groups, the library files of each function library in the target function library group are input into a static linker, and multiple segments with the same attributes in each of the library files are merged into a target segment through the static linker, and each of the target segments is copied to a preset target file to obtain a merged file.
[0080] It should be noted that a static linker is a linker tool used to link multiple object files or library files into a single executable file or library file. A library file is a file containing compiled code that can be used by other programs. In an executable file, a segment is an area of memory allocated for specific types of data, such as the code segment or data segment. A target segment is a single memory area formed by merging multiple segments with the same attributes during the linking process.
[0081] The default target file is a binary file used to store the merged target segments during the linking process. It can be understood that the target file is used as the main program for linking and does not contain any content. The merged file is a file that contains all the target segments and is the intermediate product of the linking process.
[0082] The static linker first allocates contiguous memory space for the merged segments. For example, all .text code segments will be merged into a new larger .text segment, and similarly, all .data data segments will be merged into a new .data segment.
[0083] In a feasible implementation manner, before the step of statically linking the function libraries within the multiple function library groups to obtain the static link files of the multiple function libraries, the following steps may also be included:
[0084] Step A01, creating an empty source file, and using the empty source file as a statically linked main program;
[0085] It should be noted that during the static linking process, a main program or file is usually required as the starting point or container for the link. This main program itself may not contain any actual code or data, but it provides a framework or structure that allows the linker to merge the code and data from other object files or library files into this framework. It can be understood that by creating an empty source file and using it as the static link main program, unnecessary code and data interference is avoided during the linking process, making the linking process more efficient.
[0086] In this embodiment, the step of copying each of the target segments to a preset target file to obtain a merged file includes:
[0087] Step S2011: copy each of the target segments to the statically linked main program to obtain a merged file.
[0088] After the static linker merges the same attribute segments in the library files of multiple function libraries into target segments, these target segments need to be copied to a blank static link main program to form the final static link file. By copying these target segments to the static link main program, a new file (the merged file) containing all the necessary code and data is constructed. The merged file is an intermediate product of the static link process and is also the basis for subsequent steps (such as generating the final executable file or library file).
[0089] Step S202: Parsing each of the library files by the static linker to obtain each symbol in the library file, searching for each actual memory address of each of the symbols in the target end by the static linker, and constructing a static symbol table by the static linker based on each actual memory address of each of the symbols and each of the symbols.
[0090] In programming, symbols usually refer to the names of variables, functions, etc., which are used to refer to them in the code. The static symbol table is a table containing all symbols and their memory addresses during the static linking process.
[0091] The static linker first reads the metadata in the library file and identifies all symbol definitions and references. Then the static linker assigns a memory address to each symbol in the merged library file. The static linker creates a table based on the memory address of each symbol. This table will be used to quickly find the address of the symbol when the program is executed.
[0092] Step S203: Packing the merged file and the static symbol table into an executable file through the static linker, and using the packaged executable file as the static link file corresponding to the target function library group.
[0093] It should be noted that the code and data in all library files are merged into one file in a certain format, and this file is called the merged file.
[0094] The static linker uses the merged file and the static symbol table to generate the final executable file. During this process, the linker ensures that all symbol references are correctly resolved and point to the correct memory addresses. The resulting executable file, as a statically linked file, contains all the necessary code and resources. Statically linked files can run independently without the need for additional library files.
[0095] It should be noted that the generated executable file does not depend on external library files and can run on a system without the corresponding library installed. Developers only need to distribute a single executable file without worrying about library version issues in the user's system. Static linking reduces exposure to external dependencies and helps protect the security of the program.
[0096] In a feasible implementation, the step S30 of dynamically linking the static link files to obtain a dynamic link library includes steps S301 to S303:
[0097] Step S301: input each of the static link files into a dynamic linker, and allocate a space address to each of the static link files through the dynamic linker.
[0098] It should be noted that the dynamic linker is part of the operating system and is responsible for linking dynamic libraries into the program at runtime. The memory address is the location allocated to the program or library in memory, which is used to store the code and data in the statically linked file.
[0099] The dynamic linker allocates an address space in memory for each statically linked file. This address space is the base address when the program is running.
[0100] Step S302 : reading each symbol in each static link file through the dynamic linker, and relocating each symbol based on the spatial address of each static link file to obtain dynamic address information of each symbol.
[0101] Address relocation is the process of converting symbolic references in code into actual memory addresses when a program is loaded into memory. Dynamic address information is the actual address of a symbol in memory and is determined by the dynamic linker while the program is running. The dynamic linker reads the symbol information in the static link file and relocates the symbol based on the assigned memory address, ensuring that symbolic references in the program point to the correct memory location.
[0102] Step S303 : generating a dynamic symbol table based on the symbols and the dynamic address information of the symbols, and adding the dynamic symbol table to a preset shared library to obtain a dynamic link library.
[0103] The dynamic symbol table is a table containing all dynamic address information, which is used to resolve symbol references at runtime. A shared library is a library file that can be shared between multiple dynamically linked programs.
[0104] The dynamic linker generates a dynamic symbol table based on the dynamic address information of the symbols. This table records the runtime addresses of all symbols. The dynamic symbol table is added to the shared library to create a dynamic link library, which can be loaded and linked at runtime.
[0105] It is understandable that the addresses assigned by the dynamic linker allow the same library to be used in different programs without being unable to use it due to address conflicts. Multiple programs can share the same memory copy of the library file, reducing memory usage.
[0106] In a feasible implementation, if there is a function library from the packaged compiler, the step S30 of dynamically linking the static link files to obtain a dynamic link library further includes:
[0107] Step S304: dynamically linking the static link files using a non-encapsulated compiler to obtain a basic link library.
[0108] A packaged compiler refers to a compiler and its associated toolchain, which are packaged together for easy distribution and use. A base link library is an intermediate library formed by linking multiple static link files during the dynamic link process, serving as the basis for further linking.
[0109] The code and data in the static link file are merged through the non-encapsulated compiler to generate a basic link library to facilitate the subsequent dynamic link process.
[0110] Step S305 , linking the function library from the packaged compiler to the basic link library to obtain a dynamic link library.
[0111] The packaged compiler will link additional libraries. In this embodiment, the additional function library linked by the packaged compiler is linked with the basic link library, which may include steps such as address relocation and symbol resolution, and finally generate a dynamic link library that can be loaded by the program at runtime.
[0112] In this embodiment, the process of dynamically linking the static link file and dynamically linking the function library from the packaged compiler and the basic link library can refer to the specific implementation process of steps S301 to S303, which will not be described in detail here.
[0113] It is understandable that by creating a basic link library and then linking the encapsulated compiler-linked function library with the basic link library, it is possible to prevent linking these additional linked function libraries from interfering with the static link process, thereby improving the correctness of the static link process.
[0114] For example, to facilitate understanding of this embodiment obtained by combining the above-mentioned embodiment 1 and embodiment 2, in this embodiment, the function library linking process is generally divided into three steps:
[0115] 1. Group the linked libraries (that is, divide multiple function libraries into multiple function library groups, wherein the calling distance between the first function library and at least one second function library in the same function library group is limited, and the calling distance between function libraries in different function library groups is not limited, the first function library is any function library among the various function libraries in the same function library group, and the second function library is the function library other than the first function library among the various function libraries in the same function library group). This embodiment first finds out the libraries whose jump distance is limited when calling each other, and divides these libraries into groups. For example, if the calling between library A and library B is limited, and the calling between library C and library D is limited, then A and B are grouped into one group, and C and D are grouped into another group. Grouping is performed according to the following principles: a. Restricted calls should not cross groups; b. The libraries included in each group should be as small as possible. Please refer to Figure 3 As shown in the schematic diagram of the grouping scenario, this embodiment automatically traverses the instructions of all calling classes in the linked library, analyzes the function signatures of the calling function and the calling destination function, and analyzes from the function signatures whether the calling function and the destination function are CPU functions or accelerator functions respectively. If the calling function and the destination function are both accelerator functions, the jump of this call is restricted, and this embodiment marks the library where the calling function is located and the library where the destination function is located as a group. If at least one of the calling function or the destination function is a CPU function, the jump distance of this call is not restricted and the library is not marked. This embodiment uses a union-find algorithm to merge the above-mentioned marked libraries. Please refer to Figure 4In the merge scenario shown, if there are libraries A, B, and C, where A and B are already grouped together, and B and C are also grouped together, then the merge is performed so that A, B, and C are all grouped together. This merge is repeated automatically until no new merges can be performed.
[0116] 2. Link the libraries in each group in a static linking manner (that is, statically link the function libraries in the multiple function library groups to obtain the static link files of the multiple function libraries). After grouping, this embodiment links the libraries in each group together in a static linking manner. The specific method is: first create an empty source file as the main program used for linking. The file does not contain any content and is only used to meet the requirement that the compiler must have a main program when linking. Subsequently, call the compiler to statically link each library in the group to the main program. Please refer to Figure 5 As shown in the static link diagram, when statically linking, the packaged compiler (compiler wrapper) needs to be specially processed. When developing artificial intelligence programs, in addition to using the compiler directly, the packaged compiler will also be used to automatically set related functions during compilation. For example, the packaged compiler mpicc is used to automatically enable the MPI communication library function during compilation. However, the packaged compiler will link some additional libraries during linking. For example, mpicc will automatically link libmpi. When linking the libraries in each group, this embodiment will turn off this function to avoid introducing libraries that are not desired to be introduced into the group. These libraries will be postponed until the last dynamic link to be linked.
[0117] 3. Link each group in a dynamic linking manner (that is, dynamically link each of the static link files to obtain a dynamic link library). This embodiment links the link results of each group together in the form of dynamic linking to meet the Python language's requirements for dynamic linking. When performing dynamic linking, the libraries from the packaged compiler that are disabled during static linking are supplemented.
[0118] This embodiment avoids long call jump distances between libraries on the accelerator through group linking; automatically selects appropriate groups by analyzing the call relationships in the linked libraries; implements group linking by establishing an empty main program and performing special processing on the packaged compiler; and combines static linking with dynamic linking to achieve group linking while meeting the Python language's requirements for dynamic linking.
[0119] It should be noted that the above examples are only used to understand this application and do not constitute a limitation on the function library linking method of this application. More forms of simple transformations based on this technical concept are all within the scope of protection of this application.
[0120] This application also provides a function library linking device, please refer to Figure 6 , the function library linking device includes:
[0121] a grouping module 10, configured to divide a plurality of function libraries into a plurality of function library groups, wherein a calling distance between a first function library and at least one second function library in the same function library group is limited, and a calling distance between function libraries in different function library groups is not limited, wherein the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library among the function libraries in the same function library group other than the first function library;
[0122] A static link module 20 is used to statically link the function libraries within the multiple function library groups to obtain static link files for the multiple function libraries;
[0123] The dynamic link module 30 is used to dynamically link the static link files to obtain a dynamic link library.
[0124] Optionally, the grouping module 10 is further configured to:
[0125] For any target function library among the multiple function libraries, if a call-type instruction exists in the target function library, traversing each of the call-type instructions in the target function library to determine a call start function and a call destination function of the call-type instruction, wherein the call start function is a function where the call-type instruction calls and a function where the call destination function of the call-type instruction calls;
[0126] If the function types of the calling start function and the calling target function are both accelerator functions, the target function library and the function library where the calling target function is located are divided into the same function library group.
[0127] Optionally, the static link module 20 is further configured to:
[0128] For any target function library group among the plurality of function library groups, inputting library files of each function library in the target function library group into a static linker, merging multiple segments with the same attributes in each library file into a target segment through the static linker, and copying each target segment to a preset target file to obtain a merged file;
[0129] Parsing each of the library files by the static linker to obtain each symbol in the library file, searching for each actual memory address of each of the symbols in the target end by the static linker, and constructing a static symbol table by the static linker based on each actual memory address of each of the symbols and each of the symbols;
[0130] The merged file and the static symbol table are packaged into an executable file by the static linker, and the packaged executable file is used as the static link file corresponding to the target function library group.
[0131] Optionally, the device is further used for:
[0132] Creating an empty source file, and using the empty source file as a statically linked main program;
[0133] The static link module 20 is also used to:
[0134] Copy each of the target segments to the statically linked main program to obtain a merged file.
[0135] Optionally, the dynamic link module 30 is further configured to:
[0136] Inputting each of the static link files into a dynamic linker, and allocating a space address to each of the static link files through the dynamic linker;
[0137] Reading each symbol in each of the static link files by the dynamic linker, and relocating each symbol based on the spatial address of each of the static link files to obtain dynamic address information of each symbol;
[0138] A dynamic symbol table is generated based on the symbols and the dynamic address information of the symbols, and the dynamic symbol table is added to a preset shared library to obtain a dynamic link library.
[0139] Optionally, the dynamic link module 30 is further configured to:
[0140] Dynamically linking each of the static link files using a non-encapsulated compiler to obtain a basic link library;
[0141] The function library from the packaged compiler is linked to the basic link library to obtain a dynamic link library.
[0142] The library linking device provided in this application utilizes the library linking method described in the aforementioned embodiments to resolve the technical issue of failed calls to a shared instruction space accelerator. Compared to the prior art, the library linking device provided in this application offers the same beneficial effects as the library linking method described in the aforementioned embodiments. Other technical features of the library linking device are the same as those disclosed in the aforementioned embodiments and are not further elaborated upon here.
[0143] The present application provides a function library linking device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the function library linking method in the above embodiment.
[0144] Reference below Figure 7 , which shows a schematic diagram of the structure of a function library linking device suitable for implementing the embodiments of the present application. The function library linking device in the embodiments of the present application may include, but is not limited to, mobile terminals such as servers, mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 7 The function library linking device shown is merely an example and should not impose any limitations on the functions and scope of use of the embodiments of the present application.
[0145] like Figure 7As shown, the function library linking device may include a processing device 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes based on programs stored in a read-only memory (ROM) 1002 or programs loaded from a storage device 1003 into a random access memory (RAM) 1004. RAM 1004 also stores various programs and data required for the operation of the function library linking device. Processing device 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to I / O interface 1006: input devices 1007 including, for example, a touchscreen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage device 1003 including, for example, a magnetic tape, hard disk, etc.; and communication device 1009. Communication device 1009 can allow the library link device to carry out wireless or wired communication with other devices to exchange data. Although the figure shows the library link device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or have alternatively.
[0146] It should be noted that the library linking device may or may not include an artificial intelligence tensor accelerator, and this is not a limitation. When the library linking device includes an artificial intelligence tensor accelerator, after the library linking device executes the library linking method in the above embodiment, the program obtained by executing the library linking method can be directly run on the artificial intelligence tensor accelerator; when the library linking device does not include an artificial intelligence tensor accelerator, the program obtained by executing the library linking method can be sent to a device deployed with an artificial intelligence tensor accelerator, and the program obtained by executing the library linking method can be run on the device deployed with the artificial intelligence tensor accelerator.
[0147] In particular, according to the embodiments disclosed in the present application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0148] The library linking device provided in this application utilizes the library linking method described in the aforementioned embodiment to resolve the technical issue of failed calls to a shared instruction space accelerator. Compared to the prior art, the library linking device provided in this application achieves the same beneficial effects as the library linking method described in the aforementioned embodiment. Other technical features of the library linking device are the same as those disclosed in the aforementioned embodiment and are not further elaborated upon here.
[0149] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0150] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
[0151] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the function library linking method in the above embodiment.
[0152] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.
[0153] The computer-readable storage medium may be included in the function library linking device; or may exist independently without being assembled into the function library linking device.
[0154] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by a function library linking device, the function library linking device: divides multiple function libraries into multiple function library groups, wherein the calling distance between a first function library and at least one second function library in the same function library group is limited, and the calling distance between function libraries in different function library groups is not limited, the first function library is any function library among the various function libraries in the same function library group, and the second function library is a function library other than the first function library among the various function libraries in the same function library group; statically links the function libraries within the multiple function library groups to obtain static link files of the multiple function libraries; and dynamically links each of the static link files to obtain a dynamic link library.
[0155] The computer program code for performing the operations of the present application can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, conventional procedural programming languages such as "C" or similar programming languages, special programming languages required for writing programs running on artificial intelligence tensor accelerators, special variants of mainstream programming languages for artificial intelligence tensor accelerators, etc. The program code can be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (for example, through the Internet using an Internet service provider).
[0156] The flow charts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, program segment or a part of code, and the module, program segment or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flow chart, and the combination of the boxes in the block diagram and / or flow chart can be implemented by a dedicated hardware-based system that performs the specified function or operation, or can be implemented by a combination of dedicated hardware and computer instructions.
[0157] The modules described in the embodiments of the present application may be implemented in software or hardware, wherein the name of a module does not necessarily limit the unit itself.
[0158] The computer-readable storage medium provided in this application stores computer-readable program instructions (i.e., a computer program) for executing the aforementioned function library linking method, thereby resolving the technical issue of failed shared instruction space accelerator calls. Compared to the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the function library linking method provided in the aforementioned embodiments, and are not further elaborated here.
[0159] The present application also provides a computer program product, comprising a computer program, which implements the steps of the above-mentioned function library linking method when executed by a processor.
[0160] The computer program product provided in this application can solve the technical problem of failed calls to shared instruction space accelerators. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as those of the function library linking method provided in the above embodiment, and will not be repeated here.
[0161] The above description is only part of the embodiments of the present application and does not limit the patent scope of the present application. All equivalent structural transformations made by using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect application in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A function library linking method, characterized in that: The function library linking method includes: For any target function library among the multiple function libraries, if a call-type instruction exists in the target function library, traversing each of the call-type instructions in the target function library to determine a call start function and a call destination function of the call-type instruction, wherein the call start function is a function where the call-type instruction calls and a function where the call destination function of the call-type instruction calls; If the function types of the calling start function and the calling target function are both accelerator functions, the target function library and the function library where the calling target function is located are divided into the same function library group, wherein the calling distance between a first function library and at least one second function library in the same function library group is restricted, and the calling distance between function libraries in different function library groups is not restricted, the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library other than the first function library among the function libraries in the same function library group; Static linking the function libraries within the multiple function library groups to obtain static link files of the multiple function libraries; The static link files are dynamically linked to obtain a dynamic link library.
2. The function library linking method according to claim 1, wherein: The step of statically linking the function libraries within the multiple function library groups to obtain respective static link files of the multiple function libraries comprises: For any target function library group among the plurality of function library groups, inputting library files of each function library in the target function library group into a static linker, merging multiple segments with the same attributes in each library file into a target segment through the static linker, and copying each target segment to a preset target file to obtain a merged file; Parsing each of the library files by the static linker to obtain each symbol in the library file, searching for each actual memory address of each of the symbols in the target end by the static linker, and constructing a static symbol table by the static linker based on each actual memory address of each of the symbols and each of the symbols; The merged file and the static symbol table are packaged into an executable file by the static linker, and the packaged executable file is used as the static link file corresponding to the target function library group.
3. The function library linking method according to claim 2, wherein: Before the step of statically linking the function libraries within the multiple function library groups to obtain the static link files of the multiple function libraries, the method further includes: Creating an empty source file, and using the empty source file as a statically linked main program; The step of copying each of the target segments to a preset target file to obtain a merged file includes: Copy each of the target segments to the statically linked main program to obtain a merged file.
4. The function library linking method according to claim 1, wherein: The step of dynamically linking each of the static link files to obtain a dynamic link library comprises: Inputting each of the static link files into a dynamic linker, and allocating a space address to each of the static link files through the dynamic linker; Reading each symbol in each of the static link files by the dynamic linker, and relocating each symbol based on the spatial address of each of the static link files to obtain dynamic address information of each symbol; A dynamic symbol table is generated based on the symbols and the dynamic address information of the symbols, and the dynamic symbol table is added to a preset shared library to obtain a dynamic link library.
5. The function library linking method according to any one of claims 1 to 4, characterized in that: If there is a function library from the packaged compiler, the step of dynamically linking the static link files to obtain a dynamic link library further includes: Dynamically linking each of the static link files using a non-encapsulated compiler to obtain a basic link library; The function library from the packaged compiler is linked to the basic link library to obtain a dynamic link library.
6. A function library linking device, characterized in that: The function library linking device comprises: a grouping module configured to, for any target function library among a plurality of function libraries, traverse each of the call class instructions in the target function library if the target function library contains a call class instruction, and determine a call start function and a call destination function of the call class instruction, wherein the call start function is a function where the call class instruction calls and a function where the call class instruction calls; The grouping module further divides the target function library and the function library where the target function is located into the same function library group if the function types of the calling start function and the calling destination function are both accelerator functions, wherein the calling distance between a first function library and at least one second function library in the same function library group is restricted, and the calling distance between function libraries in different function library groups is not restricted, the first function library is any function library among the function libraries in the same function library group, and the second function library is a function library other than the first function library among the function libraries in the same function library group; a static link module, configured to statically link the function libraries within the plurality of function library groups to obtain respective static link files of the plurality of function libraries; The dynamic link module is used to dynamically link the static link files to obtain a dynamic link library.
7. A function library linking device, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the function library linking method according to any one of claims 1 to 5.
8. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the function library linking method according to any one of claims 1 to 5 are implemented.
9. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the function library linking method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Performance detection method and device for heterogeneous acceleration program and storage medium
CN116126669A
Data processing method, scanner, electronic equipment and storage medium
CN118484179A