Operator testing method, apparatus, device, readable storage medium and program product
By converting the original neural network operators into the target software language and encapsulating them using decorators and target classes, the high difficulty of developing and testing neural network operators in existing technologies is solved, enabling rapid debugging and efficient testing.
Patent Information
- Application Number
- CN202411758978.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2044-11-29
AI Technical Summary
In existing technologies, using low-level languages such as C or OpenCL to describe neural network operators is difficult to develop and test, has low testing efficiency, and has a high barrier to entry, requiring a deep understanding of the NPU's hardware and software.
By acquiring the original neural network operators and converting them from a domain-specific language to the target software language, the operators are encapsulated and updated using decorators and target classes. The target software language interpreter is then used for testing, enabling rapid debugging and verification.
It significantly reduces the difficulty of developing and debugging neural network operators, improves testing efficiency, and enables users to directly develop and deploy on hardware devices using target software languages (such as Python).
Smart Images

Figure CN119576788B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer, and particularly, the present disclosure relates to an operator testing method and device, equipment, readable storage medium and program product. BACKGROUND
[0002] In the prior art, when deploying a deep learning model on an NPU (Neural network Processing Unit), neural network operator development and debugging are very important work. Generally, developing a neural network operator requires the cooperation of the entire compilation and deployment software stack, using C or OpenCL and other low-level languages to describe the neural network operator, and using a tool chain to deploy the neural network operator on the NPU. This way of using C or OpenCL and other low-level languages to describe the neural network operator has a very high threshold, and requires developers to have a deep understanding of the hardware and software of the NPU.
[0003] Python is a programming language with the advantage of low learning threshold. Many manufacturers provide a python-like DSL (Domain-Specific Language) to describe neural network operators in order to reduce the threshold of developing neural network operators. The syntax of this DSL is a subset of the python syntax, and the DSL includes primitives related to various hardware.
[0004] After the user completes the DSL description of the neural network operator, the DSL description is converted into C, OpenCL or hardware instructions and other low-level language descriptions by the tool chain provided by the manufacturer, and the low-level language description is deployed on the device. The user tests whether the DSL is correct by observing the generated C, OpenCL or hardware instructions and other low-level language descriptions; or the user tests the low-level language description by deploying the low-level language description; thus, the testing of the neural network operator is difficult, the testing process is long, and the testing efficiency of the neural network operator is low. SUMMARY
[0005] The present disclosure proposes an operator testing method, device, equipment, computer readable storage medium and computer program product to solve the problem of how to improve the testing efficiency of the neural network operator.
[0006] In a first aspect, the present disclosure provides an operator testing method, comprising:
[0007] The original neural network operator and a target software language description are acquired, the original neural network operator is described by a domain-specific language, and the original neural network operator includes a target function; the target software language description is obtained by language conversion of a hardware primitive in the target function into the target software language, and the hardware primitive is used to represent a primitive related to a hardware device;
[0008] A decorator of the target function is determined, the decorator of the target function includes a target object of a preset target class, and the target object of the target class is obtained by encapsulating the target function by the target class;
[0009] The original neural network operator is updated based on the target software language description, the decorator of the target function, and the target class to obtain an updated neural network operator;
[0010] The updated neural network operator is tested in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator.
[0011] In an embodiment, before the decorator of the original neural network operator is determined, the method further includes:
[0012] A target class is constructed, the target class includes a calling method, and the calling method is used to call an object of the target class by an interpreter of the target software language to run a function corresponding to the object of the target class in a multithreading mode.
[0013] In an embodiment, the original neural network operator is updated based on the target software language description, the decorator of the target function, and the target class to obtain the updated neural network operator, including:
[0014] The target software language description, the decorator of the target function, and the target class are written into the original neural network operator to obtain the updated neural network operator;
[0015] The decorator of the target function further includes a label of the object of the target class, and the label is used to represent that if a calling function is called in the updated neural network operator and the calling function is language-converted to obtain a bottom language description corresponding to the calling function, the target function is language-converted to obtain a bottom language description corresponding to the target function.
[0016] In an embodiment, the updated neural network operator is tested in a test mode of the target software language to determine a test result corresponding to the updated neural network operator, including:
[0017] The target object of the target class is called based on the calling method by an interpreter of the target software language to run the target function and the target software language description in a multithreading mode to obtain a test result corresponding to the target function, and the test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
[0018] In one embodiment, after determining the test result corresponding to the updated neural network operator, further comprising:
[0019] If the test result corresponding to the updated neural network operator is the same as the preset standard result, converting the target function, the target software language description, the decorator of the target function and the target class into an abstract syntax tree description through an interpreter of the target software language;
[0020] Converting the abstract syntax tree description into a bottom language description through the interpreter of the target software language;
[0021] Writing the bottom language description into the hardware device.
[0022] In one embodiment, the hardware device is a neural network processor, the domain-specific language includes hardware primitives, the syntax of the domain-specific language is a subset of the syntax of the target software language, and the target software language is Python.
[0023] In a second aspect, the present disclosure provides an operator testing device, comprising:
[0024] A first processing module configured to obtain an original neural network operator and a target software language description, wherein the original neural network operator is described by a domain-specific language, the original neural network operator includes a target function, the target software language description is obtained by language conversion of hardware primitives in the target function using a target software language, and the hardware primitives are used to represent primitives related to a hardware device;
[0025] A second processing module configured to determine a decorator of the target function, wherein the decorator of the target function includes a target object of a target class, and the target object of the target class is obtained by encapsulating the target function using the target class;
[0026] A third processing module configured to update the original neural network operator based on the target software language description, the decorator of the target function and the target class, to obtain an updated neural network operator;
[0027] A fourth processing module configured to test the updated neural network operator in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator.
[0028] In a third aspect, the present disclosure provides an electronic device, comprising: a processor, a memory and a bus;
[0029] The bus is configured to connect the processor and the memory;
[0030] The memory is configured to store operation instructions;
[0031] A processor is configured to execute the operator testing method of the first aspect of the present disclosure by invoking operation instructions.
[0032] In a fourth aspect, the present disclosure provides a computer-readable storage medium storing a computer program, and the computer program is configured to execute the operator testing method of the first aspect of the present disclosure.
[0033] In a fifth aspect, the present disclosure provides a computer program product comprising a computer program, and the computer program is configured to execute the steps of the operator testing method in the first aspect of the present disclosure when executed by a processor.
[0034] The technical solutions provided by the embodiments of the present disclosure have at least the following beneficial effects:
[0035] The original neural network operator and the target software language description are obtained, wherein the original neural network operator is described by a domain-specific language, and the original neural network operator includes a target function; the target software language description is obtained by language conversion of hardware primitives in the target function using a target software language, and the hardware primitives are used to represent primitives related to hardware devices; a decorator of the target function is determined, the decorator of the target function includes a target object of a preset target class, and the target object of the target class is obtained by encapsulating the target function by the target class; the original neural network operator is updated based on the target software language description, the decorator of the target function, and the target class to obtain an updated neural network operator; and the updated neural network operator is tested in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator; in this way, the updated neural network operator described by the domain-specific language is run using an interpreter of the target software language (such as Python), which can quickly debug and verify the correctness of the updated neural network operator, thereby significantly reducing the development difficulty and debugging difficulty of the neural network operator and improving the testing efficiency of the neural network operator, so that users can develop a neural network operator that can be deployed on a hardware device using the target software language. BRIEF DESCRIPTION OF DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present disclosure, the drawings needed in the description of the embodiments of the present disclosure will be briefly introduced.
[0037] Figure 1 The architecture diagram of the operator testing system provided by the embodiments of the present disclosure is shown in the figure.
[0038] Figure 2 The flowchart of the operator testing method provided by the embodiments of the present disclosure is shown in the figure.
[0039] Figure 3 The flowchart of the operator testing method provided by the embodiments of the present disclosure is shown in the figure.
[0040] Figure 4 A structural schematic diagram of an operator testing device provided by an embodiment of the present disclosure is shown in FIG. 1.
[0041] Figure 5 A structural schematic diagram of an electronic device provided by an embodiment of the present disclosure is shown in FIG. 2. DETAILED DESCRIPTION
[0042] Embodiments of the present disclosure will be described below in conjunction with the accompanying drawings. It should be understood that the embodiments described below in conjunction with the accompanying drawings are exemplary descriptions of the technical solutions of the embodiments of the present disclosure, and do not constitute a limitation on the technical solutions of the embodiments of the present disclosure.
[0043] Those skilled in the art can understand that the singular forms "a", "an" and "the" used herein include plural forms unless specifically stated otherwise. It should be further understood that the terms "comprise" and "include" used by the embodiments of the present disclosure mean that the corresponding features can be implemented as the presented features, information, data, steps, operations, elements and / or components, but do not exclude other features, information, data, steps, operations, elements, components and / or combinations thereof supported by the present technology. It should be understood that when we say that an element is "connected" or "coupled" to another element, the element can be directly connected or coupled to the other element, or it can mean that the element and the other element are connected through an intermediate element. In addition, "connected" or "coupled" used herein can include wireless connection or wireless coupling. The term "and / or" used herein means that at least one of the items defined by the term, for example, "A and / or B" means that "A" is implemented, or "B" is implemented, or "A and B" are implemented.
[0044] It can be understood that in the specific embodiments of the present disclosure, data related to operator testing is involved, and when the above embodiments of the present disclosure are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions.
[0045] In order to make the purposes, technical solutions and advantages of the present disclosure clearer, the embodiments of the present disclosure will be described in further detail below in conjunction with the accompanying drawings.
[0046] The embodiments of the present disclosure are an operator testing method provided by an operator testing system, which relates to the field of software testing and the like.
[0047] In order to better understand and illustrate the scheme of the embodiments of the present disclosure, some technical terms involved in the embodiments of the present disclosure will be briefly described below.
[0048] Neural network operator: a deep learning algorithm is composed of multiple computing units, each of which is a neural network operator; a neural network operator corresponds to the computing logic of a layer in a neural network, for example, a convolutional layer in a neural network is a neural network, and the weight summation process of a fully connected layer in a neural network is a neural network; for another example, tanh, ReLU, etc. are neural network operators used as activation functions in a neural network.
[0049] Decorator: a decorator is used to modify or enhance the behavior of a function or class; a decorator is essentially a function that takes another function or class as an argument and returns a new function or class; decorators are often used to add additional functionality without modifying the original code.
[0050] Python: Python provides efficient high-level data structures and simple and effective object-oriented programming; Python syntax, dynamic types, and the nature of an interpreted language make Python a programming language for scripting and rapid application development on most platforms.
[0051] OpenCL: OpenCL (Open Computing Language) is an open, royalty-free standard for cross-platform parallel programming of various accelerators in supercomputers, cloud servers, personal computers, mobile devices, and embedded platforms.
[0052] C: C is a procedural computer programming language; the design goal of C is to provide a programming language that can be compiled and processed in a simple way, handle low-level memory, produce only a small amount of machine code, and run without any runtime environment support.
[0053] NPU: NPU (Neural network Processing Unit) is an embedded neural network processor.
[0054] pdb: Python debugging module pdb (python debugger) is a standard library provided by Python; pdb can be used for program debugging.
[0055] The reasons why the prior art cannot run the DSL description of the neural network operator through the Python interpreter include:
[0056] (1) In general, it is difficult to determine whether the function call in the DSL description of the neural network operator needs to be converted into hardware device-side code (low-level language description) or only run on the host side (server);
[0057] (2) The DSL description of neural network operators will inevitably use a large number of hardware primitives, and the Python interpreter cannot know the specific implementation of these hardware primitives;
[0058] (3) For GPGPU (General-Purpose computing on Graphics Processing Units) type NPU, there will be multiple threads executing kernel functions. Each thread runs different data through threadIdx (built-in index). The Python interpreter directly calls and runs the DSL description only once. Therefore, the execution result of the Python interpreter and the execution result of the NPU's multi-threaded execution are inconsistent.
[0059] Based on this, the present disclosure provides an operator testing method, apparatus, device, computer-readable storage medium, and program product, the specific technical solutions of which will be described in detail below.
[0060] The solutions provided in this disclosure relate to operator testing technology. The technical solutions of this disclosure are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of this disclosure will now be described with reference to the accompanying drawings.
[0061] To better understand the solution provided in this disclosure, the solution will be described below in conjunction with a specific application scenario.
[0062] In one embodiment, Figure 1 The diagram shows an architecture schematic of an operator testing system applicable to embodiments of this disclosure. It is understood that the operator testing method provided in these embodiments can be applied to, but is not limited to, applications such as... Figure 1 In the application scenarios shown.
[0063] In this example, as Figure 1 As shown, the architecture of the operator testing system in this example may include, but is not limited to, server 10, terminal 20, and database 30. Server 10, terminal 20, and database 30 can interact via network 40.
[0064] The server 10 acquires an original neural network operator and a target software language description, wherein the original neural network operator is described by a domain-specific language, the original neural network operator includes an objective function, and the target software language description is obtained by language conversion of a hardware primitive in the objective function in a target software language, the hardware primitive is used to represent a primitive related to a hardware device; the server 10 determines a decorator of the objective function, the decorator of the objective function includes a target object of a target class, and the target object of the target class is obtained by encapsulating the objective function by the target class; the server 10 updates the original neural network operator based on the target software language description, the decorator of the objective function, and the target class, to obtain an updated neural network operator; the server 10 tests the updated neural network operator in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator. The server 10 sends the test result corresponding to the updated neural network operator to the terminal 20 for display, and the server 10 sends the test result corresponding to the updated neural network operator to the database 30 for storage.
[0065] It can be understood that the above is only an example, and the present embodiment is not limited thereto.
[0066] The terminal includes, but is not limited to, a smart phone (such as an Android phone, an iOS phone, etc.), a mobile phone simulator, a tablet computer, a notebook computer, a digital broadcast receiver, a MID (Mobile Internet Device), a PDA (Personal Digital Assistant), a smart voice interaction device, a smart home appliance, a vehicle-mounted terminal, etc.
[0067] The server can be a standalone physical server, a server cluster or a distributed system composed of multiple physical servers, or a cloud server or a server cluster providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs (Content Delivery Networks), and basic cloud computing services such as big data and artificial intelligence platforms.
[0068] The network can include, but is not limited to, a wired network including a local area network, a metropolitan area network, and a wide area network, and a wireless network including Bluetooth, Wi-Fi, and other wireless communication networks. The specific network can also be determined based on the actual application scenario requirements, and is not limited herein.
[0069] Referring to Figure 2 , Figure 2A flowchart of an operator testing method provided by an embodiment of the present disclosure is shown, wherein the method can be executed by any electronic device, such as a server. As an optional implementation, the method can be executed by a server. For the convenience of description, in the description of some optional embodiments below, the server will be taken as an example to illustrate the execution subject of the method. Figure 2 As shown, the operator testing method provided by the embodiment of the present disclosure includes the following steps:
[0070] S201, obtaining an original neural network operator and a target software language description, wherein the original neural network operator is described by a domain-specific language, and the original neural network operator includes a target function; the target software language description is obtained by language conversion of hardware primitives in the target function using a target software language, and the hardware primitives are used to represent primitives related to hardware devices.
[0071] Specifically, the domain-specific language includes hardware primitives, and the syntax of the domain-specific language is a subset of the syntax of the target software language, for example, python. The original neural network operator can include a plurality of target functions, and each target function in the plurality of target functions includes hardware primitives. For example, the hardware device manufacturer provides a target software language description corresponding to the hardware primitives in each target function, and the target software language description is, for example, a python description, which is a software implementation of the hardware primitives that can be run by a python interpreter; the hardware device manufacturer converts the hardware primitives in the target function into the corresponding python description using python, and the python description can be a piece of python code.
[0072] The hardware primitives are, for example, S.dma_copy and S.alloc_buffer; wherein S.dma_copy is used to copy data in global memory to a local SRAM buffer, and S.alloc_buffer is used to allocate a local SRAM buffer.
[0073] S202, determining a decorator of the target function, wherein the decorator of the target function includes a target object of a target class, and the target object of the target class is obtained by encapsulating the target function by the target class.
[0074] Specifically, the decorator of the target function includes the target object of the target class, a label of the object of the target class, and the like. For example, the decorator prim_func is as follows:
[0075] def prim_func(func=None): ...
[0077] obj=PyPrimFunc(func)
[0078] obj.hw_flag=True ...
[0080] return obj
[0081] It should be noted that the target function is such as func, the decorator is such as prim_func, the target class is such as PyPrimFunc, the target object of the target class is such as obj=PyPrimFunc(func), and the label of the object of the target class is such as obj.hw_flag=True.
[0082] For example, the target class PyPrimFunc is shown below:
[0083] class PyPrimFunc:
[0084] def__init__(self, py_func):
[0085] # The target function py_func described by the passed-in DSL;
[0086] self.py_func=py_func ...
[0088] def__call__(self, *args):
[0089] # The __call__ method needs to be implemented so that the Python interpreter can directly execute the target class;
[0090] # To simulate a GPGPU, multiple instances need to be run using multi-threading; ...
[0092] with context:
[0093] threads=[Thread(targets=self.py_func, args=args)for_in range(threads_size)]
[0094] It should be noted that the target function, for example, py_func, the calling method, for example, the __call__ method, if the target function py_func of the GPGPU is simulated to run in multiple threads, the __call__ method can start multiple threads, and the multiple threads run the target function py_func described by the DSL at the same time, so as to simulate the target function py_func of the GPGPU running in multiple threads, that is, the calling method __call__ method is used for the interpreter of the target software language python to call the object obj=PyPrimFunc(func) of the target class, so as to run the function func corresponding to the object obj=PyPrimFunc(func) of the target class in multiple threads, and func is py_func; threads represent threads, and threads-size represents the number of threads, for example, the number of threads is 10, that is, the number of multiple threads is 10.
[0095] In S203, based on the target software language description, the decorator of the target function, and the target class, the original neural network operator is updated to obtain an updated neural network operator.
[0096] Specifically, for example, the target software language description, the decorator of the target function, and the target class are written into the original neural network operator to obtain an updated neural network operator, and the updated neural network operator includes the original neural network operator, the target software language description, the decorator of the target function, and the target class.
[0097] In S204, the updated neural network operator is tested in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator.
[0098] Specifically, the target software language is, for example, python; for example, the updated neural network operator is tested in a test mode of python, that is, the updated neural network operator is run through the interpreter of python to obtain a test result corresponding to the updated neural network operator.
[0099] In the embodiments of the present disclosure, the original neural network operator and the target software language description are obtained, wherein the original neural network operator is described by a domain-specific language, the original neural network operator includes a target function, the target software language description is obtained by language conversion of a hardware primitive in the target function using a target software language, and the hardware primitive is used to represent a primitive related to a hardware device; a decorator of the target function is determined, the decorator of the target function includes a target object of a target class, and the target object of the target class is obtained by encapsulating the target function by the target class; the original neural network operator is updated based on the target software language description, the decorator of the target function and the target class to obtain an updated neural network operator; the updated neural network operator is tested in a test mode of the target software language to obtain a test result corresponding to the updated neural network operator; in this way, by obtaining the updated neural network operator, the updated neural network operator described by the domain-specific language is run using an interpreter of the target software language (for example, python), which can quickly debug and verify the correctness of the updated neural network operator, thereby significantly reducing the development difficulty and debugging difficulty of the neural network operator, improving the test efficiency of the neural network operator, and enabling the user to develop a neural network operator that can be deployed on a hardware device using the target software language.
[0100] In one embodiment, before determining the decorator of the original neural network operator, the method further includes:
[0101] The target class is constructed, and the target class includes a calling method, the calling method is used for an interpreter of the target software language to call an object of the target class to run a function corresponding to the object of the target class in a multi-threaded manner.
[0102] Specifically, for example, the target class PyPrimFunc is as follows:
[0103] class PyPrimFunc:
[0104] def__init__(self,py_func):
[0105] # The target function py_func described by the DSL is passed in;
[0106] self.py_func=py_func ...
[0108] def__call__(self,*args):
[0109] # The __call__ method needs to be implemented to enable the interpreter of python to directly execute the target class;
[0110] To simulate GPGPU, multiple instances need to be run in multiple threads; ...
[0112] with context:
[0113] threads=[Thread(targets=self.py_func,args=args)for_in range(threads_size)]
[0114] For example, the decorator prim_func is as follows:
[0115] def prim_func(func=None): ...
[0117] obj=PyPrimFunc(func)
[0118] obj.hw_flag=True ...
[0120] return obj
[0121] It should be noted that the target function, such as py_func, calls the method, such as the __call__ method, and if the GPGPU multi-threaded running target function py_func is to be simulated, the __call__ method can start multiple threads, and the multiple threads run the DSL described target function py_func at the same time, to simulate the multi-threaded running of the target function py_func of the GPGPU, that is, the calling method __call__ method is used for the target software language python interpreter to call the object obj=PyPrimFunc(func) of the target class, to run the function func corresponding to the object obj=PyPrimFunc(func) of the target class in multiple threads, and func is py_func; threads represent threads, and threads-size represents the number of threads, for example, the number of threads is 10, that is, the number of multi-threads is 10; the python interpreter in the prior art cannot directly call the object obj=PyPrimFunc(func) of the target class generated after the decorator prim_func is decorated, therefore, the __call__ method is implemented in the target class PyPrimFunc, so that the python interpreter can call the target function func.
[0122] In one embodiment, based on the target software language description, the decorator of the target function, and the target class, the original neural network operator is updated to obtain an updated neural network operator, including:
[0123] write the target software language description, the decorator of the target function and the target class into the original neural network operator to obtain an updated neural network operator;
[0124] The decorator of the target function further includes a label of the object of the target class, and the label is used to represent that if the target function is called by a calling function in the updated neural network operator and the calling function performs language conversion to obtain a bottom language description corresponding to the calling function, then the target function is subjected to language conversion to obtain a bottom language description corresponding to the target function.
[0125] Specifically, for example, the target software language description, the decorator of the target function and the target class are written into the original neural network operator to obtain an updated neural network operator, and the updated neural network operator includes the original neural network operator, the target software language description, the decorator of the target function and the target class.
[0126] For example, the decorator prim_func is as follows:
[0127] def prim_func(func=None): ...
[0129] obj=PyPrimFunc(func)
[0130] obj.hw_flag=True ...
[0132] return obj
[0133] It should be noted that the target function is, for example, func, the decorator is, for example, prim_func, the target class is, for example, PyPrimFunc, the target object of the target class is, for example, obj=PyPrimFunc(func), and the label of the object of the target class is, for example, obj.hw_flag=True; the decorator prim_func of the target function func further includes the label obj.hw_flag=True of the object obj=PyPrimFunc(func) of the target class, and the label obj.hw_flag=True is used to represent that if the target function func is called by a calling function in the updated neural network operator and the calling function performs language conversion to obtain a bottom language description corresponding to the calling function, then the target function func is subjected to language conversion to obtain a bottom language description corresponding to the target function.
[0134] For example, by decorating the target function func described by the DSL with a decorator prim_func, the decorator includes tagging the target function func with a tag obj.hw_flag=True, i.e., the tag obj.hw_flag=True of the object obj=PyPrimFunc(func) of the target class, the tag obj.hw_flag=True indicating that the target function func is called by a calling function and the calling function performs language conversion to obtain the underlying language description corresponding to the calling function, the target function func is language-converted to obtain the underlying language description corresponding to the target function, i.e., the target function func is converted into the underlying language description for execution on the NPU, so that the tag can be used to distinguish that some called functions (e.g., the target function func) need to be converted into C or hardware instructions, and other functions (e.g., ordinary python functions, ordinary python functions without hardware primitives, and ordinary python functions such as helper functions) only need to be evaluated on the host (server) and do not need to be converted into C or hardware instructions; the decorator also includes encapsulating the target function func into an object obj=PyPrimFunc(func) of a class, and the object obj=PyPrimFunc(func) of the class serves as a context for subsequent multi-threaded parallel running of the target function func.
[0135] In one embodiment, the updated neural network operator is tested in a test mode of the target software language, and a test result corresponding to the updated neural network operator is determined, including:
[0136] The target object of the target class is called based on the calling method through an interpreter of the target software language, and the target function and the target software language description are run in multiple threads to obtain a test result corresponding to the target function, and the test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
[0137] Specifically, for some hardware primitives, such as S.alloc_buffer, S.dma_copy, etc., these hardware primitives have no software implementation, and the Python interpreter will report an error when running to the statement of the hardware primitive without software implementation; therefore, the software implementation corresponding to the hardware primitive needs to be registered, and the software implementation of the hardware primitive needs to ensure that the behavior of the software implementation is consistent with the behavior of the hardware primitive, and the software implementation of the hardware primitive is described in the target software language; in this way, it is ensured that the python description corresponding to the hardware primitive used by the target function described by the DSL can be run on the Python interpreter.
[0138] For example, the software implementation of the registered hardware primitive S.dma_copy (target software language description, i.e., python description) adds an assert assertion, which is debugged in the software implementation of the hardware primitive to ensure that the specification limit of the hardware will still report an error when the software runs, for example, illegal input will report an error in the python interpreter running stage; in this way, the software implementation of the registered hardware primitive S.dma_copy can add corresponding checks, which can ensure the correctness of the parameters and do not need to perform corresponding checks during the running period of the hardware device (for example, NPU), thereby improving the test efficiency of the neural network operator.
[0139] For example, the software implementation of the registered hardware primitive S.dma_copy is as follows:
[0140] @register_ir_api
[0141] Def _py_dma_copy(dst, src, width, src_stride=None, times=1, dst_stride=None): ...
[0143] assert src_stride>=width, "Stride need greater equal than width" ...
[0145] It should be noted that dst: target memory address or array, data will be copied here; src: source memory address or array, data will be copied from here; width: data width (unit: element number) copied each time; src_stride: source data step (i.e., the number of elements that the source pointer moves after each copy), which is width by default if not specified; times: number of copies; dst_stride: target data step (i.e., the number of elements that the target pointer moves after each copy), which is width by default if not specified; the software implementation of the registered hardware primitive S.dma_copy adds an assert assertion, which is debugged in the software implementation of the hardware primitive.
[0146] For example, before the updated neural network operator is compiled into a bottom language, the updated neural network operator is run through a python interpreter. Since the updated neural network operator can be directly run on python, various debugging libraries on python, such as pdb (python debugger), can be reused, and the correct neural network operator can be debugged on python by using python breakpoints and other means, and then deployed to an NPU. Since the entire debugging process is implemented on python, the development and debugging threshold and difficulty of the neural network operator can be greatly reduced, thereby improving the testing efficiency of the neural network operator.
[0147] In one embodiment, after determining the test result corresponding to the updated neural network operator, the method further comprises:
[0148] If the test result corresponding to the updated neural network operator is the same as the preset standard result, the target function, the target software language description, the decorator of the target function, and the target class are converted into an abstract syntax tree description through an interpreter of the target software language;
[0149] The abstract syntax tree description is converted into a bottom language description through an interpreter of the target software language;
[0150] The bottom language description is written into the hardware device.
[0151] Specifically, the target software language is, for example, python, the bottom language is, for example, C or OpenCL, and the hardware device is, for example, an NPU.
[0152] For example, an interpreter of python can directly run the updated neural network operator, and can ensure that the software behavior or test result of each line of code of python is consistent with the hardware device. In this way, the user can directly debug the updated neural network operator as a normal python program, without the need to debug C code or hardware instructions. Since the software behavior is consistent with the corresponding hardware behavior, the software running result after debugging is correct, that is, the test result corresponding to the updated neural network operator is the same as the preset standard result, which means that the running result of the updated neural network operator on the hardware device is correct.
[0153] In one embodiment, the hardware device is a neural network processor, the domain-specific language includes hardware primitives, the syntax of the domain-specific language is a subset of the syntax of the target software language, and the target software language is python.
[0154] By applying the embodiments of the present disclosure, at least the following beneficial effects are achieved:
[0155] By obtaining the updated neural network operator, the updated neural network operator described in the domain-specific language is run by using an interpreter of a target software language (for example, python), and the correctness of the updated neural network operator can be quickly debugged and verified. Therefore, the development difficulty and the debugging difficulty of the neural network operator are significantly reduced, the test efficiency of the neural network operator is improved, and the user can develop the neural network operator that can be deployed on a hardware device by using the target software language.
[0156] In order to better understand the method provided by the embodiments of the present disclosure, the scheme of the embodiments of the present disclosure will be further described below in combination with examples of specific application scenarios.
[0157] The method provided by the embodiments of the present disclosure implements a python-like domain-specific language to quickly implement a neural network operator, and the neural network operator described in the DSL can be run by using a python interpreter to quickly debug or verify the correctness of the neural network operator. The method can be used in all application fields of NPU, for example, IoT devices (Internet of Things devices), mobile devices, automotive (autonomous driving systems), wearable devices, etc. The method can significantly reduce the development difficulty and the debugging difficulty of the neural network operator, and the user can develop the neural network operator that can be deployed on the NPU by using python. During debugging, the neural network operator described in the DSL can be run by using the python interpreter to debug or verify the result of the operator, thereby improving the development efficiency of the neural network operator.
[0158] In one specific application scenario embodiment, for example, a neural network operator test scenario, referring to Figure 3 , a processing flow of an operator test method is shown, as shown in Figure 3 , the processing flow of the operator test method provided by the embodiments of the present disclosure includes the following steps:
[0159] S301, the server obtains an original neural network operator described in a domain-specific language.
[0160] Specifically, for example, the original neural network operator includes an objective function, and the objective function includes a hardware primitive.
[0161] S302, the server constructs a target class, and the target class includes a calling method, the calling method is used for the python interpreter to call an object of the target class, and the object of the target class is used to run a function corresponding to the object in a multi-thread mode.
[0162] Specifically, for example, by implementing the __call__ method (call method) in the target class PyPrimFunc, so that the interpreter of python can call the function func (object of the target class PyPrimFunc).
[0163] S303, the server determines the decorator of the target function, the decorator of the target function including the target object of the target class and the label of the object of the target class.
[0164] Specifically, for example, the decorator prim_func of the target function func includes the target object obj=PyPrimFunc(func) of the target class, the label obj.hw_flag=True of the object of the target class, etc.
[0165] S304, the server obtains the software implementation of the hardware primitive in the target function.
[0166] Specifically, for example, the hardware device manufacturer provides the server with the python description (target software language description) corresponding to the hardware primitive in the target function, the python description being the software implementation of the hardware primitive that can be run by the interpreter of python, and the python description can be a piece of python code.
[0167] S305, the server updates the original neural network operator based on the software implementation of the hardware primitive in the target function, the decorator of the target function and the target class, to obtain an updated neural network operator.
[0168] Specifically, for example, the software implementation of the hardware primitive in the target function, the decorator of the target function and the target class are written into the original neural network operator to obtain the updated neural network operator, and the updated neural network operator includes the original neural network operator, the software implementation of the hardware primitive in the target function, the decorator of the target function, the target class, etc.
[0169] S306, the server tests the updated neural network operator through the interpreter of python to determine the test result corresponding to the updated neural network operator.
[0170] Specifically, the target object of the target class is called based on the call method through the interpreter of the target software language, and the target function and the target software language description are run in multithreading to obtain the test result corresponding to the target function, and the test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
[0171] S307, if the test result corresponding to the updated neural network operator is the same as the standard result, the server writes the underlying language description corresponding to the updated neural network operator into the hardware device.
[0172] Specifically, if the test result corresponding to the updated neural network operator is the same as the preset standard result, the target function, the target software language description, the decorator of the target function and the target class are converted into an abstract syntax tree description by an interpreter of the target software language; the abstract syntax tree description is converted into a bottom language description by the interpreter of the target software language; and the bottom language description is written into the hardware device.
[0173] By applying the embodiments of the present disclosure, at least the following beneficial effects are achieved:
[0174] By obtaining the updated neural network operator, the interpreter of the target software language (for example, python) is used to run the updated neural network operator described in the domain-specific language, which can quickly debug and verify the correctness of the updated neural network operator. In this way, the development difficulty and the debugging difficulty of the neural network operator are significantly reduced, the test efficiency of the neural network operator is improved, and the user can develop the neural network operator that can be deployed to the hardware device by using the target software language.
[0175] The embodiments of the present disclosure also provide an operator testing device. As shown in Figure 4 The operator testing device 60 includes a first processing module 601, a second processing module 602, a third processing module 603 and a fourth processing module 604.
[0176] The first processing module 601 is configured to obtain an original neural network operator and a target software language description. The original neural network operator is described by a domain-specific language, and the original neural network operator includes a target function. The target software language description is obtained by language conversion of hardware primitives in the target function by using a target software language. The hardware primitives are used to represent primitives related to a hardware device.
[0177] The second processing module 602 is configured to determine a decorator of the target function. The decorator of the target function includes a target object of a target class. The target object of the target class is obtained by encapsulating the target function by using the target class.
[0178] The third processing module 603 is configured to update the original neural network operator based on the target software language description, the decorator of the target function and the target class, to obtain an updated neural network operator.
[0179] The fourth processing module 604 is configured to test the updated neural network operator in a test mode of the target software language, to obtain a test result corresponding to the updated neural network operator.
[0180] In an embodiment, the second processing module 602 is further configured to:
[0181] The target class is constructed, and the target class includes a calling method, the calling method is used for calling an object of the target class by an interpreter of the target software language, and a function corresponding to the object of the target class is run in a multi-thread mode.
[0182] In an embodiment, the third processing module 603 is specifically configured to:
[0183] The target software language description, the decorator of the target function, and the target class are written into the original neural network operator to obtain an updated neural network operator.
[0184] The decorator of the target function further includes a label of the object of the target class, and the label is used to indicate that if the target function is called by a calling function in the updated neural network operator and the calling function is used for language conversion to obtain a bottom language description corresponding to the calling function, the target function is subjected to language conversion to obtain a bottom language description corresponding to the target function.
[0185] In an embodiment, the fourth processing module 604 is specifically configured to:
[0186] The target object of the target class is called based on the calling method by an interpreter of the target software language, and the target function and the target software language description are run in a multi-thread mode to obtain a test result corresponding to the target function, and the test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
[0187] In an embodiment, the fourth processing module 604 is further configured to:
[0188] If the test result corresponding to the updated neural network operator is the same as a preset standard result, the target function, the target software language description, the decorator of the target function, and the target class are converted into an abstract syntax tree description by an interpreter of the target software language.
[0189] The abstract syntax tree description is converted into a bottom language description by an interpreter of the target software language.
[0190] The bottom language description is written into a hardware device.
[0191] In an embodiment, the hardware device is a neural network processor, the domain-specific language includes hardware primitives, the syntax of the domain-specific language is a subset of the syntax of the target software language, and the target software language is Python.
[0192] By applying the embodiments of the present disclosure, at least the following beneficial effects are obtained:
[0193] The process involves obtaining the original neural network operator and its target software language description. The original neural network operator is described using a domain-specific language and includes an objective function. The target software language description is obtained by translating hardware primitives in the objective function into the target software language. These hardware primitives represent primitives related to hardware devices. The process also involves determining the decorator for the objective function, which includes a target object of a predefined target class. This target object encapsulates the objective function within the target class. Based on the target software language description, the decorator, and the target class, the original neural network operator is updated to obtain the updated neural network operator. The updated neural network operator is then tested using the target software language's testing methods to obtain the corresponding test results. This process enables the use of an interpreter of the target software language (e.g., Python) to run the updated neural network operator described in the domain-specific language. This allows for rapid debugging and verification of the correctness of the updated neural network operator, significantly reducing the development and debugging difficulty of neural network operators, improving testing efficiency, and enabling users to develop neural network operators that can be deployed on hardware devices using the target software language.
[0194] This disclosure also provides an electronic device, the structural schematic diagram of which is shown below. Figure 5 As shown, Figure 5 The illustrated electronic device 4000 includes a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, for example, via a bus 4002. Optionally, the electronic device 4000 may further include a transceiver 4004, which can be used for data interaction between the electronic device and other electronic devices, such as sending and / or receiving data. It should be noted that in practical applications, the transceiver 4004 is not limited to one type, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of this disclosure.
[0195] The processor 4001 can be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic device, transistor logic device, hardware component, or any combination thereof. It can implement or execute the various exemplary logical blocks, modules and circuits described in connection with the disclosure. The processor 4001 can also be a combination of computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.
[0196] The bus 4002 can include a path for transmitting information between the above-mentioned components. The bus 4002 can be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. The bus 4002 can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 In the figure, only one thick line is used to represent the bus, but it does not mean that there is only one bus or only one type of bus.
[0197] The memory 4003 can be a ROM (Read Only Memory) or other type of static storage device that can store static information and instructions, a RAM (Random Access Memory) or other type of dynamic storage device that can store information and instructions, an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, an optical disk storage (including a compact disk, a laser disk, an optical disk, a digital versatile disk, a Blu-ray disk, etc.), a magnetic disk storage medium, other magnetic storage device, or any other medium capable of carrying or storing computer programs and capable of being read by a computer, without limitation.
[0198] The memory 4003 is used to store computer programs that execute embodiments of the present disclosure, and is controlled by the processor 4001 to execute them. The processor 4001 is used to execute the computer programs stored in the memory 4003 to implement the steps shown in the foregoing method embodiments.
[0199] Electronic devices include, but are not limited to, servers.
[0200] Applying the embodiments of this disclosure has at least the following beneficial effects:
[0201] The process involves obtaining the original neural network operator and its target software language description. The original neural network operator is described using a domain-specific language and includes an objective function. The target software language description is obtained by translating hardware primitives in the objective function into the target software language. These hardware primitives represent primitives related to hardware devices. The process also involves determining the decorator for the objective function, which includes a target object of a predefined target class. This target object encapsulates the objective function within the target class. Based on the target software language description, the decorator, and the target class, the original neural network operator is updated to obtain the updated neural network operator. The updated neural network operator is then tested using the target software language's testing methods to obtain the corresponding test results. This process enables the use of an interpreter of the target software language (e.g., Python) to run the updated neural network operator described in the domain-specific language. This allows for rapid debugging and verification of the correctness of the updated neural network operator, significantly reducing the development and debugging difficulty of neural network operators, improving testing efficiency, and enabling users to develop neural network operators that can be deployed on hardware devices using the target software language.
[0202] This disclosure provides a computer-readable storage medium storing a computer program, which, when executed by a processor, can implement the steps and corresponding content of the aforementioned method embodiments.
[0203] This disclosure also provides a computer program product, including a computer program that, when executed by a processor, can implement the steps and corresponding content of the aforementioned method embodiments.
[0204] It should be understood that although arrows indicate various operation steps in the flowcharts of the embodiments of this disclosure, the order in which these steps are implemented is not limited to the order indicated by the arrows. Unless explicitly stated herein, in some implementation scenarios of the embodiments of this disclosure, the implementation steps in each flowchart can be executed in other orders as required. Furthermore, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on the actual implementation scenario. Some or all of these sub-steps or stages can be executed at the same time, and each sub-step or stage can also be executed at different times. In scenarios where execution times differ, the execution order of these sub-steps or stages can be flexibly configured as required, and the embodiments of this disclosure do not limit this.
[0205] The above description is only an optional implementation method for some implementation scenarios of this disclosure. It should be noted that for those skilled in the art, other similar implementation methods based on the technical concept of this disclosure without departing from the technical concept of this disclosure also fall within the protection scope of the embodiments of this disclosure.
Claims
1. An operator testing method, characterized in that, include: Obtain the original neural network operator and the target software language description, wherein the original neural network operator is described using a domain-specific language, the original neural network operator includes an objective function, and the target software language description is obtained by language conversion of hardware primitives in the objective function using the target software language, and the hardware primitives are used to characterize primitives related to hardware devices; The decorator of the target function is determined, and the decorator of the target function includes a target object of a preset target class, which is obtained by encapsulating the target function through the target class; Based on the target software language description, the decorator of the target function, and the target class, the original neural network operator is updated to obtain the updated neural network operator; The updated neural network operator is tested using the testing method of the target software language to obtain the test results corresponding to the updated neural network operator; The process of updating the original neural network operator based on the target software language description, the decorator of the target function, and the target class to obtain the updated neural network operator includes: The target software language description, the decorator of the target function, and the target class are written into the original neural network operator to obtain the updated neural network operator; The decorator of the target function also includes a label for the object of the target class. The label is used to indicate that if the target function is called by a calling function in the updated neural network operator and the calling function performs language conversion to obtain the underlying language description corresponding to the calling function, then the target function will be language converted to obtain the underlying language description corresponding to the target function. The step of testing the updated neural network operator using the testing method of the target software language to obtain the test results corresponding to the updated neural network operator includes: The target object of the target class is invoked by the interpreter of the target software language based on the invocation method. The target function and the target software language description are run in a multi-threaded manner to obtain the test result corresponding to the target function. The test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
2. The method according to claim 1, characterized in that, Before determining the decorator for the objective function, the following is also included: The target class is constructed, and the target class includes a calling method. The calling method is used by the interpreter of the target software language to call an object of the target class to run the function corresponding to the object of the target class in a multi-threaded manner.
3. The method according to claim 1, characterized in that, After obtaining the test results corresponding to the updated neural network operator, the method further includes: If the test result corresponding to the updated neural network operator is the same as the preset standard result, then the target function, the target software language description, the decorator of the target function, and the target class are converted into an abstract syntax tree description by the interpreter of the target software language. The abstract syntax tree description is converted into a low-level language description by the interpreter of the target software language; The underlying language description is written into the hardware device.
4. The method according to any one of claims 1-3, characterized in that, The hardware device is a neural network processor, the domain-specific language includes the hardware primitives, and the syntax of the domain-specific language is a subset of the syntax of the target software language, wherein the target software language is Python.
5. An operator testing device, characterized in that, include: The first processing module is used to obtain the original neural network operator and the target software language description, wherein the original neural network operator is described by a domain-specific language, the original neural network operator includes an objective function, and the target software language description is obtained by language conversion of hardware primitives in the objective function using the target software language, and the hardware primitives are used to characterize primitives related to hardware devices; The second processing module is used to determine the decorator of the target function. The decorator of the target function includes a target object of a preset target class. The target object of the target class is obtained by encapsulating the target function through the target class. The third processing module is used to update the original neural network operator based on the target software language description, the decorator of the target function, and the target class to obtain the updated neural network operator. The fourth processing module is used to test the updated neural network operator in the target software language testing method to obtain the test results corresponding to the updated neural network operator. The third processing module is specifically used for: The target software language description, the decorator of the target function, and the target class are written into the original neural network operator to obtain the updated neural network operator; The decorator of the target function also includes a label for the object of the target class. The label is used to indicate that if the target function is called by a calling function in the updated neural network operator and the calling function performs language conversion to obtain the underlying language description corresponding to the calling function, then the target function will be language converted to obtain the underlying language description corresponding to the target function. The fourth processing module is specifically used for: The target object of the target class is invoked by the interpreter of the target software language based on the invocation method. The target function and the target software language description are run in a multi-threaded manner to obtain the test result corresponding to the target function. The test result corresponding to the updated neural network operator includes the test result corresponding to the target function.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory, characterized in that, The processor executes the computer program to implement the steps of the method according to any one of claims 1-4.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-4.
8. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-4.
Citation Information
Patent Citations
Operator unloading and registering method and device, storage medium and electronic device
CN114398095A
Platform framework extension method and device and storage medium
CN116360712A