Method for compiling multi-level intermediate representation of heterogeneous neural network and electronic device
By using a hybrid neural network language to uniformly describe artificial neural network and spiking neural network components, generating an abstract syntax tree and discretizing it, the problem of low compilation efficiency of heterogeneous neural networks is solved, and efficient cross-computational paradigm optimization and stable numerical computation are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- XIAMEN UNIV
- Filing Date
- 2026-06-30
- Publication Date
- 2026-07-31
AI Technical Summary
Existing technologies struggle to uniformly represent the heterogeneous computational semantics of artificial neural networks and spiking neural networks within the same compilation framework, resulting in low compilation efficiency and an inability to achieve global collaborative optimization across computational paradigms.
A hybrid neural network language is used to uniformly describe artificial neural network components and spiking neural network components. An abstract syntax tree is generated through lexical analysis and syntactic analysis. Semantic analysis and discretization are performed to generate metadata containing heterogeneous neural networks, discretized neuron model descriptions, structured learning rule descriptions, and network topology. A multi-dialect mapping algorithm is used to convert it into a multi-level intermediate representation.
It improves the development efficiency of heterogeneous neural network modeling, automatically completes the identification of mathematical characteristics of differential equations and the selection of numerical methods, ensures the accuracy and global stability of numerical calculations, and provides the foundation for cross-boundary global joint optimization and efficient code generation.
Smart Images

Figure CN122489079A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and in particular to a method for compiling multi-level intermediate representations of heterogeneous neural networks and an electronic device. Background Technology
[0002] In recent years, artificial intelligence (AI) technology has developed rapidly, and traditional artificial neural networks (ANNs) have achieved great success in tasks such as image recognition and natural language processing. Meanwhile, spiking neural networks (SNNs), due to their low power consumption and event-driven characteristics, have demonstrated unique advantages in neuromorphic computing and edge intelligence. To balance high precision and energy efficiency, combining ANNs and SNNs to form heterogeneous neural networks has become an important development trend.
[0003] However, the computational models of ANNs and SNNs differ fundamentally. ANNs employ continuous-valued activation functions and synchronous computation, which can be represented as tensor operations, making them easily and efficiently executed on GPUs. SNNs, on the other hand, involve both temporal evolution and event-triggered computation, with neuron states described by ordinary differential equations (such as the membrane potential dynamics equations of LIF neurons), exhibiting a hybrid continuous-discrete dynamic characteristic. This fundamental difference presents significant challenges to the programming and deployment of heterogeneous neural networks.
[0004] In existing technologies, deep learning compilers are primarily designed for ANNs (Application-Oriented Neural Networks). Their intermediate representations lack the ability to express the temporal dynamics of SNNs, failing to describe the temporal evolution of neuronal states, the generation and propagation of spikes, and time-dependent synaptic plasticity learning rules. While spiking neural network simulation tools can support SNN modeling, they are simulators or interpreted execution environments, lacking systematic intermediate representation design and global compilation optimization capabilities. Furthermore, they are isolated from deep learning frameworks, making it difficult to integrate ANN components. In addition, a few PyTorch-based SNN libraries (such as Norse and SpikingJelly) adopt an "SNN as RNN" approach, but operate at the framework level rather than the compiler level, lacking high-level domain-specific language abstractions, making static analysis and global optimization difficult.
[0005] Therefore, existing hybrid solutions typically require developers to develop their respective parts using deep learning frameworks and SNN simulation tools, and then integrate them through manual programming. This solution has the following problems: (1) Low development efficiency: It requires familiarity with multiple program programming interfaces and manual management of encoding and decoding conversion between continuous values and impulse sequences; (2) Difficulty in global optimization: ANN and SNN parts are processed separately, lacking a global view across boundaries, and cannot perform optimizations such as joint time step adjustment, boundary operation fusion, and learning rate coordination; (3) High deployment complexity: It requires switching between different execution engines during runtime, introducing synchronization and data conversion overhead; (4) Poor maintainability: When modifying the network architecture, multiple code parts need to be modified simultaneously and consistency must be ensured, and maintenance costs continue to increase. Summary of the Invention
[0006] This disclosure provides a method for compiling multi-level intermediate representations of heterogeneous neural networks and an electronic device thereof.
[0007] According to one aspect of this disclosure, a method for compiling multi-level intermediate representations of heterogeneous neural networks is provided, comprising: Obtain the source code corresponding to the heterogeneous neural network. The source code is described using a hybrid neural network language, which is used to uniformly describe artificial neural network components and spiking neural network components. Lexical and syntactic analysis are performed on the source program to generate an abstract syntax tree corresponding to the source program; The abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree. The discretized intermediate representation includes metadata of the heterogeneous neural network, discretized neuron model description, structured learning rule description, network topology, and heterogeneous boundary transformation description. The network topology is used to describe the network connection relationship, and the heterogeneous boundary transformation description is used to define the data transformation rules of the heterogeneous network boundary. According to the multi-dialect mapping algorithm, the discretized intermediate representation is converted into a multi-level intermediate representation containing multiple dialects, including spiking neural network dialects, artificial neural network dialects, hybrid dialects, or network dialects.
[0008] According to the multi-level intermediate representation compilation method of heterogeneous neural networks according to at least one embodiment of the present disclosure, lexical analysis and syntax analysis are performed on the source program to generate an abstract syntax tree corresponding to the source program, including: The source program's character stream is converted into a token sequence by a lexical analyzer. The token sequence includes top-level keyword tokens, nested keyword tokens, differential equation tokens, time unit tokens, data type tokens, string literal tokens, operator tokens, delimiter tokens, and indentation tokens. The token sequence is parsed into an abstract syntax tree by a recursive descent parser. The abstract syntax tree includes expression nodes for representing computational expressions, statement nodes for representing program statements, and definition nodes for representing the top-level definition structure of a hybrid neural network language. The recursive descent parser includes a first parsing method for parsing neuron definitions, a second parsing method for parsing synapse definitions, a third parsing method for parsing operator definitions, and a fourth parsing method for parsing network definitions.
[0009] According to the multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of the present disclosure, during the parsing process of the recursive descent parser, the recursive descent parser further includes performing the following steps: When an unknown identifier is detected, the similarity between the unknown identifier and a known keyword is calculated. If the similarity is greater than or equal to a similarity threshold, an error message containing correction suggestions is generated. Alternatively, if a definition parsing error is detected, the parsing process for the current definition is skipped, and subsequent definitions are parsed instead.
[0010] According to the multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of this disclosure, the abstract syntax tree is subjected to semantic analysis and discretization processing to obtain a discretized intermediate representation corresponding to the abstract syntax tree, including: Mathematical property analysis and complexity assessment are performed on the differential equations in the abstract syntax tree to determine the mathematical properties and complexity of the differential equations. The mathematical properties include linear equations or rigid equations. Based on the mathematical properties and complexity of the differential equation, determine the corresponding discretization numerical processing method for the differential equation; Based on the determined discretization numerical processing method, a discrete update rule for the differential equation is generated. The discrete update rule includes the state variable name, the discrete update expression, the global time step, and the list of dependent variables.
[0011] According to the multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of the present disclosure, the abstract syntax tree is subjected to semantic analysis and discretization processing to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: The operator instantiation process iterates through the abstract syntax tree, performs name matching according to a preset artificial neural network operator whitelist, and if the match is successful, the operator instantiation is mapped to an artificial neural network layer model and recorded in the layer model part of the discretized intermediate representation; if the match fails, the operator instantiation is identified as a spiking neural network operator and the neuron or synapse model is converted. Based on the type of instantiation of each operator, the corresponding network type is determined, including artificial neural network type, spiking neural network type, or hybrid neural network type.
[0012] According to the multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of the present disclosure, the abstract syntax tree is subjected to semantic analysis and discretization processing to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: The learning rule declarations in the abstract syntax tree are converted into structured learning rule descriptions. The learning rules include local learning rules and global training methods. The structured learning rule descriptions include the name of the learning rule, policy type, algorithm identifier, plasticity type, time window parameter, enhancement or suppression parameter, weight constraint, trace dynamics parameter or update trigger condition.
[0013] According to the multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of the present disclosure, the abstract syntax tree is subjected to semantic analysis and discretization processing to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: Traverse the network definitions in the abstract syntax tree and construct a network topology containing nodes and edges; determine the connection mode of the connection declarations, including full connection, one-to-one connection, convergence connection or divergence connection; The network topology is verified, which includes compatibility verification of the source node output dimension and the target node input dimension for each edge, synapse number estimation, split pattern detection or convergence pattern detection; When a connection is detected between a child node of an artificial neural network and a child node of a spiking neural network, it is determined to be a heterogeneous boundary. In the discretization process, the corresponding edge is represented by adding source-end transformation attributes and target-end transformation attributes. The source-end transformation attributes and the target-end transformation attributes include transformation function names and related parameters.
[0014] A multi-level intermediate representation compilation method for heterogeneous neural networks according to at least one embodiment of the present disclosure, comprising converting the discretized intermediate representation into a multi-level intermediate representation containing multiple dialects according to a multi-dialect mapping algorithm, including: Traverse the discretized intermediate representation of the neuron model, and generate neuron model definition operations and neuron behavior function operations that conform to the dialect of spiking neural networks for each neuron model. The neuron behavior function operations include discrete update operations, threshold detection operations, and state setting operations. Traverse the discretized intermediate representation of the synaptic model, and generate synaptic model definition operations and learning rule definition operations that conform to the dialect of the spiking neural network for each synapse; Iterate through the discretized intermediate representation layer models and generate layer model definition operations that conform to the artificial neural network dialect for each artificial neural network layer model; Traverse the network topology of the discretized intermediate representation, and select network definition operations to generate spiking neural network dialect, artificial neural network dialect, hybrid dialect or network dialect according to the network type of the network topology. For hybrid network type, generate hybrid connection operations and boundary definition operations that conform to the hybrid dialect. The hybrid connection operations include source-end transformation attributes and target-end transformation attributes.
[0015] According to another aspect of this disclosure, an electronic device is provided, comprising: a memory storing execution instructions; and a processor executing the execution instructions stored in the memory, such that the processor performs a multi-level intermediate representation compilation method for heterogeneous neural networks according to any embodiment of this disclosure.
[0016] According to another aspect of this disclosure, a readable storage medium is provided, wherein executable instructions are stored therein, which, when executed by a processor, are used to implement a multi-level intermediate representation compilation method for heterogeneous neural networks according to any embodiment of this disclosure.
[0017] According to another aspect of this disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements a multi-level intermediate representation compilation method for heterogeneous neural networks according to any embodiment of this disclosure.
[0018] The beneficial technical effect of this application is that by obtaining the source code of artificial neural network components and spiking neural network components using a hybrid neural network language to uniformly describe them, performing lexical and syntactic analysis on the source code, and generating the corresponding abstract syntax tree, a unified and structured semantic parsing foundation is provided for heterogeneous neural networks. This avoids the tedious process of developers relying on multiple independent toolchains and manually writing data format conversion interfaces, thereby improving the development efficiency of heterogeneous neural network modeling.
[0019] Next, semantic analysis and discretization are performed on the abstract syntax tree, generating a discretized intermediate representation containing metadata of heterogeneous neural networks, discretized neuron model descriptions, structured learning rule descriptions, network topology, and heterogeneous boundary transformation descriptions. This can automatically complete the identification of mathematical characteristics of differential equations, adaptive optimization of numerical methods, and calculation of time steps that meet stability conditions. It transforms the traditional error-prone and difficult-to-portable learning rule code strings into standardized and verifiable structured fields, and explicitly records the data encoding / decoding and shape transformation operations between artificial neural networks and spiking neural networks. This completely bridges the semantic gap between continuous tensor computation and time-series event-driven computation, ensuring the accuracy and global stability of numerical computation.
[0020] Then, based on the multi-dialect mapping algorithm, the discretized intermediate representation is converted into a multi-level intermediate representation containing spiking neural network dialects, artificial neural network dialects, heterogeneous hybrid dialects, or network general dialects. This provides a unified and scalable intermediate representation foundation for subsequent cross-boundary global joint optimization (such as time step coordination, boundary operation fusion, heterogeneous training configuration collaboration) and efficient code generation for multiple hardware backends, thereby comprehensively improving the compilation quality, runtime deployment performance, and system maintainability of heterogeneous neural networks. Attached Figure Description
[0021] The accompanying drawings illustrate exemplary embodiments of the present disclosure and, together with the description thereof, serve to explain the principles of the present disclosure. These drawings are included to provide a further understanding of the present disclosure and are incorporated in and constitute a part of this specification.
[0022] Figure 1 This is a flowchart illustrating a multi-level intermediate representation compilation method for heterogeneous neural networks according to one embodiment of the present disclosure.
[0023] Figure 2 This is a flowchart illustrating step S120 of a multi-level intermediate representation compilation method for heterogeneous neural networks according to an embodiment of the present disclosure.
[0024] Figure 3 This is a flowchart illustrating the process of determining discrete update rules in a multi-level intermediate representation compilation method for heterogeneous neural networks according to one embodiment of the present disclosure.
[0025] Figure 4 This is a schematic diagram of the ANN operator recognition process in a multi-level intermediate representation compilation method for heterogeneous neural networks according to one embodiment of the present disclosure.
[0026] Figure 5 This is a flowchart illustrating the topology verification and heterogeneous boundary transformation in a multi-level intermediate representation compilation method for heterogeneous neural networks according to one embodiment of the present disclosure.
[0027] Figure 6 This is a flowchart illustrating step S140 of a multi-level intermediate representation compilation method for heterogeneous neural networks according to an embodiment of the present disclosure.
[0028] Figure 7 This is a schematic block diagram of a multi-level intermediate representation compilation device for a heterogeneous neural network according to one embodiment of the present disclosure.
[0029] Figure 8 This is a schematic structural block diagram of an electronic device according to one embodiment of the present disclosure. Detailed Implementation
[0030] The present disclosure will now be described in further detail with reference to the accompanying drawings and examples. It should be understood that the specific examples described herein are for illustrative purposes only and are not intended to limit the scope of the disclosure. Furthermore, it should be noted that, for ease of description, only the parts relevant to the present disclosure are shown in the accompanying drawings.
[0031] It should be noted that, where there is no conflict, the embodiments and features described in this disclosure can be combined with each other. The technical solutions of this disclosure will now be described in detail with reference to the accompanying drawings and embodiments.
[0032] Existing technologies struggle to uniformly represent the heterogeneous computational semantics of artificial neural networks and spiking neural networks within the same compilation framework, resulting in low compilation efficiency and an inability to achieve global collaborative optimization across computational paradigms.
[0033] To this end, the present disclosure proposes the following technical solution, wherein the source code of artificial neural network components and spiking neural network components is obtained by using a hybrid neural network language to uniformly describe them, and lexical and syntactic analysis is performed on the source code to generate the corresponding abstract syntax tree. This provides a unified and structured semantic parsing foundation for heterogeneous neural networks, avoids the cumbersome process of developers relying on multiple independent toolchains and manually writing data format conversion interfaces, and improves the development efficiency of heterogeneous neural network modeling.
[0034] Next, semantic analysis and discretization are performed on the abstract syntax tree, generating a discretized intermediate representation containing metadata of heterogeneous neural networks, discretized neuron model descriptions, structured learning rule descriptions, network topology, and heterogeneous boundary transformation descriptions. This can automatically complete the identification of mathematical characteristics of differential equations, adaptive optimization of numerical methods, and calculation of time steps that meet stability conditions. It transforms the traditional error-prone and difficult-to-portable learning rule code strings into standardized and verifiable structured fields, and explicitly records the data encoding / decoding and shape transformation operations between artificial neural networks and spiking neural networks. This completely bridges the semantic gap between continuous tensor computation and time-series event-driven computation, ensuring the accuracy and global stability of numerical computation.
[0035] Then, based on the multi-dialect mapping algorithm, the discretized intermediate representation is converted into a multi-level intermediate representation containing spiking neural network dialects, artificial neural network dialects, heterogeneous hybrid dialects, or network general dialects. This provides a unified and scalable intermediate representation foundation for subsequent cross-boundary global joint optimization (such as time step coordination, boundary operation fusion, heterogeneous training configuration collaboration) and efficient code generation for multiple hardware backends, thereby comprehensively improving the compilation quality, runtime deployment performance, and system maintainability of heterogeneous neural networks.
[0036] To facilitate description and make the technical solutions of this disclosure easier to understand, the terminology of this disclosure will be explained before describing the technical solutions of this disclosure.
[0037] Heterogeneous Neural Network: A hybrid neural network architecture that combines artificial neural network (ANN) and spiking neural network (SNN) components, combining the advantages of both types of neural networks to achieve a balance between high precision and high energy efficiency.
[0038] Abstract Syntax Tree (AST): A data structure that represents the syntactic structure of a source program in a tree structure. The nodes of the tree represent syntactic components (such as expressions, statements, definitions, etc.), and the edges of the tree represent the inclusion relationships between syntactic components.
[0039] Intermediate Representation (IR): An internal data structure used by the compiler to represent the source program. It lies between the source language and the target language, facilitating program analysis, optimization, and conversion.
[0040] Discrete Intermediate Representation (DIR): The intermediate representation layer proposed in this disclosure completes the numerical discretization of differential equations, time step calculation, learning rule structuring, network type recognition, boundary transformation representation, and other processing based on AST.
[0041] Multi-Level Intermediate Representation (MLIR): A multi-level intermediate representation framework proposed by the LLVM project, which supports defining domain-specific "dialects" to express domain-specific computational semantics and has good scalability.
[0042] Differential equation discretization: The numerical processing procedure that transforms continuous-time differential equations into discrete-time difference equations.
[0043] Numerical stability is a property of numerical methods that refers to the fact that the method does not diverge due to the accumulation of rounding or discretization errors during computation. Different numerical methods have different stability conditions; for example, the forward Euler method requires the time step to satisfy the CFL condition.
[0044] Stiff equations are differential equations containing both rapidly changing and slowly changing components. When solved using explicit methods, extremely small time steps are required to ensure stability, so implicit methods are usually used.
[0045] STDP (Spike-Timing-Dependent Plasticity): A learning rule that adjusts synaptic weights based on the temporal relationship of presynaptic and postsynaptic pulses. Presynaptic pulses enhance connectivity (LTP), while postsynaptic pulses weaken connectivity (LTD).
[0046] Surrogate Gradient: This approximates the gradient of an impulse function using a differentiable function, enabling SNNs to be trained using gradient-based backpropagation. Commonly used surrogate functions include the sigmoid function, the atan function, and trigonometric functions.
[0047] Spike encoding is the process of converting continuous values into a sequence of pulses. Common methods include rate encoding (using pulse frequency to represent numerical values), timing encoding (using pulse duration to represent numerical values), and direct encoding (using continuous values as input currents to neurons).
[0048] Spike decoding is the process of converting a sequence of pulses into a continuous value. Common methods include pulse counting (counting the number of pulses), firing rate calculation (calculating the number of pulses per unit time), and membrane potential readout (using the final membrane potential value).
[0049] Figure 1 This is a flowchart illustrating a multi-level intermediate representation compilation method for heterogeneous neural networks according to one embodiment of the present disclosure. This multi-level intermediate representation compilation method for heterogeneous neural networks can be applied to terminal devices or servers, wherein the terminal device includes, but is not limited to, one or more of smartphones, tablets, laptops, and desktop computers.
[0050] like Figure 1 As shown, the multi-level intermediate representation compilation method of the heterogeneous neural network preferably includes steps S110 to S140.
[0051] In step S110, the source program corresponding to the heterogeneous neural network is obtained. The source program is described using a hybrid neural network language, which is used to uniformly describe artificial neural network components and spiking neural network components.
[0052] In this implementation, Hybrid Neural Network Language (HyNNL) is used as the unified description language for heterogeneous neural networks. HyNNL supports declarative syntax for defining neuron models, synapse models, network structures, and learning rules, and can simultaneously express ANN layers, SNN layers, and their combinations. HyNNL employs an indentation-sensitive syntax style, combining readability and expressiveness. After obtaining the source code corresponding to the heterogeneous neural network, it is stored in memory as a string or character stream for subsequent processing.
[0053] In step S120, lexical analysis and syntax analysis are performed on the source program to generate an abstract syntax tree corresponding to the source program.
[0054] In this implementation, the HyNNL source program is converted into a structured tree representation through lexical and syntactic analysis. The AST fully preserves the semantic information of the source program, including neuron dynamics equations, network topology relationships, learning rule parameters, input and output declarations, etc., providing a unified data structure foundation for subsequent processing.
[0055] In some embodiments of this disclosure, step S120, performing lexical and syntactic analysis on the source program to generate an abstract syntax tree corresponding to the source program, preferably includes steps S121 to S122. Please refer to [reference needed]. Figure 2 .
[0056] In step S121, the character stream of the source program is converted into a token sequence by a lexical analyzer. The token sequence includes top-level keyword tokens, nested keyword tokens, differential equation tokens, time unit tokens, data type tokens, string literal tokens, operator tokens, delimiter tokens, and indentation tokens.
[0057] In step S122, the token sequence is parsed into an abstract syntax tree by a recursive descent parser. The abstract syntax tree includes expression nodes for representing computational expressions, statement nodes for representing program statements, and definition nodes for representing the top-level definition structure of a hybrid neural network language. The recursive descent parser includes a first parsing method for parsing neuron definitions, a second parsing method for parsing synapse definitions, a third parsing method for parsing operator definitions, and a fourth parsing method for parsing network definitions.
[0058] In this implementation, the lexical analyzer is responsible for converting the character stream of the source program into a sequence of tokens. Preferably, the lexical analyzer is implemented based on regular expression matching, and uses the longest match and priority strategy to handle token conflicts.
[0059] Preferably, the following tag categories are defined in the lexical analyzer: (1) Top-level keyword labeling: including four core keywords: Neuron (neuron definition), Synapse (synapse definition), OP (operator definition), and Network (network definition), used to identify the main structural units of the HyNNL program (i.e., the source program of heterogeneous neural networks).
[0060] (2) Nested keyword tags: including declare (declaration block), neuron_fc (neuron function block), synapse_fc (synaptic function block), launch (launch condition block), reset (reset mechanism block), validate (validation rule block), learning (learning rule block), structure (structure definition block), construct (construction logic block), properties (property block), etc., used to identify the various components within the definition block.
[0061] (3) Differential equation labeling: The lexical analyzer uses the regular expression "d[a-zA-Z_][a-zA-Z0-9_]" The " / dt" flag identifies differential expressions of the form "dv / dt" as DIFFERENTIAL type tags. This is the core syntactic structure of SNN dynamic equations and requires special handling at the lexical level to avoid being incorrectly parsed as division operations.
[0062] (4) Time unit markers: The lexical analyzer identifies numerical expressions with time units through the pattern "number + unit", such as "2 ms", "100 us", "0.5 s", etc. Supported time units include milliseconds (ms), seconds (s), microseconds (us), nanoseconds (ns), minutes (min), hours (hour), etc. Preferably, the recognition priority of time unit markers is higher than that of ordinary identifiers to ensure that "2 ms" is recognized as a single time value marker rather than a combination of numbers and identifiers.
[0063] (5) Data type tags: The lexical analyzer identifies commonly used numeric type keywords in the neural network, including float32, float64, int8, int16, int32, and int64. These type tags are matched using the pattern "(float|int)(8|16|32|64)", and have higher priority than ordinary identifiers. Data type information is crucial for subsequent numerical precision control and code generation.
[0064] (6) String literal marking: The lexical analyzer supports both double-quoted and single-quoted string syntaxes, and also supports Python-style f-string formatted strings. f-string markings are identified by the pattern "f"..." or "f'...", and are processed differently from ordinary strings.
[0065] (7) Operator and delimiter markers: The lexical analyzer recognizes various operators (arithmetic operators, comparison operators, assignment operators, etc.) and delimiters (parentheses, commas, colons, etc.). In particular, the arrow symbol "->" is recognized as an ARROW marker, used to indicate connection relationships (such as "pre ->post") and type annotations.
[0066] (8) Indentation Tags: HyNNL employs an indentation-sensitive syntax. The lexical analyzer generates INDENT (increased indentation) and DEDENT (decreased indentation) tags by tracking changes in the indentation level of each line. Preferably, the implementation maintains an indentation level stack. When the indentation level of a new line is greater than the top of the stack, a new level is pushed onto the stack and an INDENT tag is generated. When the indentation level is less than the top of the stack, levels are popped one by one and a corresponding number of DEDENT tags are generated. This mechanism allows HyNNL to adopt a concise code block syntax similar to Python, without the need for explicit parentheses or keywords to mark block boundaries.
[0067] Preferably, the lexical analyzer is implemented by compiling regular expressions. During initialization, all token patterns are compiled into regular expression objects, and at runtime, they are matched sequentially, with the longest match being selected. This implementation balances flexibility (easy to add new token types) and efficiency (avoiding repeated compilation of regular expressions).
[0068] Next, after obtaining the corresponding token sequence, a recursive descent parser is used to parse the token sequence and generate the corresponding abstract syntax tree.
[0069] In some embodiments of this disclosure, the abstract syntax tree contains node types including expression nodes, statement nodes, and definition nodes.
[0070] Specifically, expression nodes are used to represent computational expressions, including the following types: Identifier represents an identifier reference, recording the identifier name; Literal serves as the base class for literals, deriving subclasses such as IntegerLiteral, FloatLiteral, StringLiteral, and BooleanLiteral; BinaryOperation represents binary operations, recording the left operand, operator, and right operand; UnaryOperation represents unary operations, recording the operator and operand; FunctionCall represents function calls, recording the function name, positional parameter list, and keyword parameter dictionary; MemberAccess represents member access (e.g., "obj.member"), recording the object expression and member name; IndexAccess represents index access (e.g., "arr[i]"), recording the object expression and index expression; ListExpression represents list expressions, recording the list of elements; and DictExpression represents dictionary expressions, recording the list of key-value pairs.
[0071] Statement nodes are used to represent program statements and include the following types: Block represents a code block containing a list of statements; AssignmentStatement represents an assignment statement, recording the assignment target and the assignment value; ExpressionStatement represents an expression statement, encapsulating an independent expression; IfStatement represents a conditional statement, recording the conditional expression, the then branch, and the optional else branch; ForStatement represents a loop statement, recording the loop variable, the iterator object, and the loop body; TryStatement represents an exception handling statement, recording the try block and the catch clause list; AssertStatement represents an assertion statement, recording the assertion condition and the optional error message.
[0072] The definition nodes are used to represent the top-level definition structure of HyNNL. `NeuronDefinition` represents a neuron definition, containing a name, optional parent class, declaration section, `neuron_fc` section, `launch` section, `reset` section, and `validate` section. `SynapseDefinition` represents a synapse definition, containing a name, optional parent class, declaration section, `synapse_fc` section, `learning` section, and `validate` section. `OPDefinition` represents an operator definition, containing a name, optional generic type parameters (neuron type and synapse type), parameter list, declaration section, structure section, and `validate` section. `NetworkDefinition` represents a network definition, containing a name, optional parent class, declaration section, properties section, construct section, and `validate` section.
[0073] Each definition node provides a `get_all_sections()` method, which returns a dictionary of all non-empty sections for easy subsequent traversal. The `Program` node, as the root node of the AST, contains a list of definitions and an optional `run_code` block, and provides convenient methods such as `get_neurons()`, `get_synapses()`, `get_ops()`, and `get_networks()` to retrieve definitions of specific types.
[0074] Furthermore, the abstract syntax tree includes node types such as DifferentialEquation, TimeUnit, ConnectionDeclaration, and LearningRule to represent the syntactic structure of the SNN. The DifferentialEquation node represents a differential equation, recording the differential variable name (e.g., extracting "v" from "dv / dt") and the right-hand side expression. The TimeUnit node represents a time value with units, recording the numerical value and unit; the ConnectionDeclaration node represents a connection declaration, recording the types of the preceding and following neurons; and the LearningRule node represents a learning rule, recording the rule name and parameter dictionary.
[0075] The recursive descent parser disclosed herein employs a recursive descent method to parse the token sequence generated by the lexical analyzer into a hierarchical abstract syntax tree. Preferably, a parsing method is defined for each grammatical structure in the recursive descent parser, and nested matching of grammatical rules is achieved through recursive calls between these methods. Specifically, the recursive descent parser includes the following parsing methods: (1) Top-level parsing method `parse()`: This method parses the entire program. It iteratively processes the token sequence, calling the corresponding defined parsing method based on the current token type: `parse_neuron_definition()` is called when encountering a `Neuron` token, `parse_synapse_definition()` when encountering a `Synapse` token, `parse_op_definition()` when encountering an `OP` token, `parse_network_definition()` when encountering a `Network` token, and `parse_run_code()` when encountering a `Try` token to process the runtime code block. Comments, blank lines, and indentation change tokens are skipped during parsing.
[0076] (2) The neuron definition parsing method `parse_neuron_definition()` (i.e., the first parsing method): First, it consumes the `Neuron` and `Colon` tags, and then parses the neuron name. If it encounters the `EXTENDS` tag (used to indicate the tag type for defining inheritance), it parses the parent class name. Next, it calls `parse_neuron_sections()` to parse the individual segments of the neuron. The segment parsing method recognizes segment keywords such as `declare`, `neuron_fc`, `launch`, `reset`, and `validate`, and calls the corresponding block parsing method for each segment. The `neuron_fc` segment processes differential equation syntax, and when it encounters the `DIFFERENTIAL` tag, it calls `parse_differential_equation()` to parse the differential equation expression.
[0077] (3) Synapse definition parsing method parse_synapse_definition() (i.e., the second parsing method): The structure is similar to that of neuron definition, but the segments it processes are different, including declare, synapse_fc, learning, validate, etc. The parsing of the learning segment processes the learning rule declaration, identifying the rule name (such as stdp, hebbian) and parameter list (such as A_plus=0.01, tau_plus=20ms). The learning rule parameters may span multiple lines, and the parser implements a cross-line parameter merging mechanism to collect parameters from multiple lines into a single learning rule node.
[0078] (4) Operator definition parsing method parse_op_definition() (i.e., the third parsing method): supports generic syntax parsing. When encountering "OP<NeuronType, SynapseType> When defining a type in the form ":Name", the generic parameter list is extracted and stored in the generic_types attribute of the OPDefinition node. The structure section of the operator typically contains loop and connection declarations, and the parser recognizes for loop syntax and instantiation calls to neuron groups / synapses.
[0079] (5) The network definition parsing method `parse_network_definition()` (i.e., the fourth parsing method): processes the complete network definition, including the network type declaration (specified as `ann`, `snn`, or `mix` via the `type` attribute in the `declare` section), input / output port declarations, subnetwork references, and the network construction logic in the `construct` section. For heterogeneous neural networks, the parser identifies boundary transformation operations (such as `from_transform` and `to_transform` attributes) and records them in the edge attributes of the connections.
[0080] This effectively overcomes the technical shortcomings of traditional compilers when processing heterogeneous neural network source code, such as loss of domain semantics, difficulty in parsing temporal structures, and low grammatical tolerance. The specialized identification mechanism for differential equation markers, time unit markers, and indentation-sensitive syntax in the lexical analysis stage ensures that continuous-time dynamics descriptions and discrete code block structures are accurately separated and classified at the underlying lexical level. This fundamentally avoids grammatical ambiguity caused by misinterpreting dynamic differential markers as arithmetic operations, providing a lossless underlying marker foundation for subsequent numerical analysis. The syntax analysis stage employs a design combining a recursive descent algorithm with four dedicated parsing methods. This not only achieves unambiguous structured mapping of artificial neural network layer declarations, spiking neuron dynamics, synaptic plasticity rules, and hybrid network topologies, but also fully preserves the complex interaction logic and data flow between heterogeneous components through cross-line parameter merging, generic parameter extraction, and boundary transformation function identification mechanisms.
[0081] In some embodiments of this disclosure, during the parsing process of the recursive descent parser, the recursive descent parser further includes performing the following steps: When an unknown identifier is detected, the similarity between the unknown identifier and a known keyword is calculated. If the similarity is greater than or equal to a similarity threshold, an error message containing correction suggestions is generated. Alternatively, if a definition parsing error is detected, the parsing process for the current definition is skipped, and subsequent definitions are parsed instead.
[0082] In this implementation, to improve the robustness of the compiler, the recursive descent parser also provides a comprehensive error handling and recovery mechanism, specifically including the following: Error messages are represented in a structured way: parsing errors are encapsulated as ParseError objects, containing the following fields: message (error description), line and column (error location), line_text (source code text of the line containing the error), expected (expected syntax element), got (actually encountered syntax element), severity (error level, ERROR or WARNING), error_type (error type, syntax, semantic, or warning), and suggestion (repair suggestion). ParseError provides the format() method to generate a formatted error report, including location indicator arrows and repair suggestions.
[0083] Similarity Keyword Detection: When encountering an unknown top-level keyword (i.e., an unknown identifier), the recursive descent parser calculates the similarity between the unknown identifier and known keywords (such as Neuron, Synapse, Network, OP). Preferably, the similarity calculation is based on edit distance (Levenshtein distance) normalization. When the similarity exceeds a similarity threshold (e.g., 0.7), the recursive descent parser infers that the user may have made a spelling mistake and provides correction suggestions in the error message. For example, when a user enters "Netwrok", the recursive descent parser generates a correction suggestion: "Do you want to use 'Network'?"
[0084] Definition-level error recovery: When an error is encountered during the parsing of a definition, the recursive descent parser does not terminate immediately. Instead, it attempts to skip the current definition and continue parsing subsequent definitions. The recovery strategy is to scan the token stream forward until the next top-level keyword (Neuron, Synapse, OP, Network) or the end of the file is encountered. This allows the compiler to report multiple errors in a single run, improving debugging efficiency.
[0085] The parsing results are structured: the final parsed result is encapsulated as a ParseResult object, containing the following fields: success indicating whether the parsing was successful, ast storing the generated abstract syntax tree (if successful), errors storing the error list, warnings storing the warning list, definition_count recording the number of successfully parsed definitions, and source_lines storing source code lines (used for contextual display of error reports). ParseResult provides the to_dict() method to convert the result into a dictionary format, facilitating serialization and tool integration.
[0086] Please continue to refer to this. Figure 1 In step S130, the abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree. The discretized intermediate representation includes metadata of the heterogeneous neural network, a discretized neuron model description, a structured learning rule description, a network topology, and a heterogeneous boundary transformation description. The network topology is used to describe the network connection relationship, and the heterogeneous boundary transformation description is used to define the data transformation rules of the heterogeneous network boundary.
[0087] In this implementation, the definition nodes in the abstract syntax tree are traversed, and the differential equations of the neuron model are automatically analyzed and discretized to generate update rules. The learning rules are structurally transformed to generate normalized field descriptions. The network definition is topologically constructed and heterogeneous boundaries are identified. Finally, all processing results are summarized into a discretized intermediate representation (DIR) in the corresponding format (such as YAML) to provide explicit and verifiable intermediate data for subsequent MLIR generation.
[0088] In some embodiments of this disclosure, the discretized intermediate representation mainly includes the following parts: Metadata: This section records global information about the neural network, including version number (currently "1.0"), representation type description (type), source file information (source), generator identifier (generated_by), discretization method (discretization_method), time step (time_step_ms), number of neuron models (total_neuron_models), number of synapse models (total_synapse_models), number of operator structures (total_op_structures), number of ANN layer models (total_layer_models), number of networks (total_networks), network type mapping (network_types, where the key is the network name and the value is "ann" / "snn" / "mix"), whether it is a hybrid network (is_hybrid), whether it contains ANN (has_ann), whether it contains SNN (has_snn), number of network nodes (network_nodes), and number of network edges (network_edges), etc.
[0089] The neuron model section (neuron_models) stores the discretized descriptions of neuron behavior. Each neuron model contains the following fields: name and type record the model name and type; the state_variables list records the names of state variables (e.g., v represents membrane potential); parameters store parameter definitions, with each parameter recording its type, initial value, and whether it is constant; update_rules store a list of discrete update rules, with each rule recording the variable name and update expression (e.g., "v_next = v + 0.1"). ((-(v - v_rest) / tau))”), numerical method, time step, and dependency list; spike_detection stores the spike detection mechanism, recording the condition expression (e.g., “(v>v_th)”), threshold variable, detection method (e.g., threshold_crossing), and comparison operator; reset_mechanism stores the reset mechanism, recording the reset type (e.g., hard_reset) and reset assignment list (e.g., “v = v_rest”); refractory stores the optional refractory period configuration.
[0090] The synapse model section (synapse_models) stores the parameters and behaviors of synapses. Each synapse model contains: name and type recording the model name and type; a list of state variables recording the names of state variables (e.g., w represents weights); parameters storing parameter definitions, with the same format as the neuron model; update_rules storing a list of update rules (can be empty for synapses without dynamics); and transmission_rule storing transmission rules, including a type (e.g., structured_transmission) and a list of operations. Each operation records the type (e.g., assignment), target (target variable, e.g., post), and expression (e.g., "(pre...)"). w)”); learning_rule stores the structured description of learning rules; connection_declaration stores the connection type declaration, recording the pre-neuron type (pre) and post-neuron type (post).
[0091] The ANN layer model section (layer_models) stores the configuration information for each ANN layer. Each layer model includes: name (layer name); type (ann_op); op_name (operator type, such as Conv2D, MaxPool2D, etc.); category (operator category, such as convolution, pooling, etc.); parent_network (network to which it belongs); parameters (operator parameters, such as in_channels, out_channels, kernel_size, etc.); pytorch_class (PyTorch class name, such as nn.Conv2d); output_shape_formula (output shape calculation formula); trainable (trainable); and requires_discretization (discretization required, usually false for ANN layers).
[0092] The operator structure section (op_structures) stores the structure definitions of SNN operators. Each operator structure contains: name and op_type recording the operator name and type; parameters storing parameter definitions (such as input_size, output_size), with each parameter recording its type and default value; the neuron_group_creation list recording neuron group creation statements (such as "neurons = neuronGroup(LIF, output_size)"); and the synapse_group_creation list recording synapse group creation statements (such as "synapses = synapseGroup(Basic, (input_size)"). output_size); connection_pattern describes the connection pattern, including topology_type (e.g., custom), a list of iterations (describing the loop structure, each loop recording type, iterator, and range), and a list of connections (describing the specific connection, recording type, pre, and post); validation_rules stores a list of validation rules.
[0093] The network topology section (network_construction) describes network connections using a node-edge graph structure. `nodes` is a node dictionary, where each node records its id (fully qualified name, formatted as "network name.node name"), name (simple name), type (e.g., input_port, output_port, ann_layer, op_instance, subnetwork_ref), parent_network, network_type, and properties (including port direction, operator parameters, neuron / synapse type references, etc.). `edges` is a list of edges, where each edge records its source (source node id), target (target node id), connection_type (e.g., all_to_all), probability (connection probability, can be null), and properties (including simple name mapping, network to which it belongs, etc.). For heterogeneous boundaries, the heterogeneous boundary transformation description also includes `from_transform` and `to_transform` fields, with each `transform` recording its function (function name) and parameters (parameter dictionary). `topology_validation` stores topology validation information, including dimensionality check results, synapse number estimation, and split / convergence pattern detection.
[0094] The run_config section stores the parameter configurations for training and inference. `defaults` stores default parameters (such as `dt_ms`, `batch_size`, `layout`, `dtype`, etc.); `train` stores training configurations (such as `duration_ms`, `epochs`, `learning_rate`, `optimizer`, `loss`, `surrogate`, etc.); `infer` stores inference configurations (such as `duration_ms`, `batch_size`, `normalization`, etc.); and `subnetwork_config` stores the independent training configurations for each subnetwork (used in scenarios where different subnetworks in a heterogeneous network use different training methods).
[0095] Preferably, the DIR also includes the following components for supporting heterogeneous networks: The Subnetworks section stores subnetwork definition information. The network_io section stores network input / output port definitions. The network_properties section is used to store network properties (such as type declarations). The `encode_decode_ops` section stores the definitions of encoding / decoding operations.
[0096] In some embodiments of this disclosure, semantic analysis and discretization are performed on the abstract syntax tree to obtain a discretized intermediate representation corresponding to the abstract syntax tree. Preferably, this includes steps S310 to S330. Please refer to [reference needed]. Figure 3 .
[0097] In step S310, the mathematical properties and complexity of the differential equations in the abstract syntax tree are analyzed to determine the mathematical properties and complexity of the differential equations. The mathematical properties include linear equations or rigid equations.
[0098] In step S320, the discretization numerical processing method corresponding to the differential equation is determined based on the mathematical characteristics and complexity of the differential equation.
[0099] In step S330, a discrete update rule for the differential equation is generated according to the determined discretization numerical processing method. The discrete update rule includes the state variable name, discrete update expression, global time step, and list of dependent variables.
[0100] In this implementation, the mathematical properties of the differential equations in the abstract syntax tree are analyzed by the equation property analyzer (NumericalMethodSelector) and the discretization engine, and the optimal discretization numerical processing method is selected, thereby realizing the discretization processing of the differential equations.
[0101] Specifically, the NumericalMethodSelector is responsible for automatically identifying the mathematical properties of differential equations. The identification process first converts the expression nodes in the AST into string form, and then identifies the following properties through pattern matching: (1) Linearity detection: Detects the presence of nonlinear terms in differential equations using regular expressions. The nonlinearity patterns detected include: variable product terms (such as "v... w or v Polynomials in the form of "v"), exponential functions ("exp()"), trigonometric functions ("sin()", "cos()"), square root functions ("sqrt()"), exponentiation operations (such as "v"), etc. (in 2” form). If any nonlinear mode is detected, the differential equation is marked as nonlinear (is_linear = false).
[0102] (2) Rigidity detection: The rigidity of the differential equation is determined by analyzing the time constant in the differential equation. Preferably, a pattern of "tau = numerical value" or "tau_xxx = numerical value" is searched to extract the numerical value of the time constant. If the time constant is less than a time threshold (e.g., 0.1 ms), the differential equation is marked as a rigid equation (is_stiff = true). Rigid equations are characterized by containing both rapidly changing and slowly changing components. When using explicit methods, a very small time step is required to ensure stability, therefore an implicit method is needed.
[0103] (3) Complexity assessment: The number of arithmetic operators (addition, subtraction, multiplication, division) in the algorithm's statistical equations is used as a simple measure of complexity. The complexity value is between 1 and 5, used to weigh the trade-off between accuracy and efficiency.
[0104] Next, based on the characteristics and complexity of the equations, the optimal discretization numerical processing method is selected from the method library. Preferably, the discretization numerical processing method includes the forward Euler method, the Heun method (improved Euler method), the fourth-order Runge-Kutta method (RK4), and the implicit Euler method.
[0105] In one implementation, the selection strategy for the discretization numerical processing method is as follows: (a) If the differential equation is a rigid equation (is_stiff = true), choose the implicit Euler method. The implicit Euler method is unconditionally stable and will not cause numerical explosion for any time step, making it particularly suitable for handling rigid equations.
[0106] (b) If the differential equation is nonlinear and has high complexity (e.g., complexity >= 4), choose the fourth-order Runge-Kutta method. The RK4 method has fourth-order accuracy and approximates the exact solution by calculating a weighted average of four slope values, making it suitable for handling complex nonlinear equations.
[0107] (c) If the differential equation is of moderate complexity (4 > complexity >= 2), choose the Heun method. The Heun method is an improved Euler method that improves accuracy to second order through a two-step prediction-correction process with moderate computational cost.
[0108] (d) For simple linear equations, choose the forward Euler method. The forward Euler method has the highest computational efficiency, requiring only a linear function evaluation, and its accuracy is sufficient for simple linear equations.
[0109] After determining the discretization numerical processing method, the discretization engine is responsible for generating the specific discretization formulas. The discretization engine receives the time step. The discretization numerical processing method name is used as a configuration parameter to generate corresponding discrete update rules for each differential equation.
[0110] Specifically, for state variables The continuous-time differential equation can be uniformly expressed as: in, This represents the state variable to be updated, such as neuronal membrane potential, synaptic weights, or other dynamic state variables; Indicates the current continuous time; This represents the set of model parameters involved in the differential equation, such as time constant, resting potential, threshold, conductance parameter, or weight parameter; Represent the functional expression on the right-hand side of the original differential equation; Indicates the discretization time step; Indicates the first The state variables take values at each discrete time step; This indicates the value of the state variable in the next discrete time step.
[0111] The discretization formula for the forward Euler method is as follows: in, Indicates the first The time corresponding to each discrete time step. This method is a first-order explicit method, requiring only the function value at the current time step. The discretization engine converts it into the form of: The update expression, where, The function on the right side of the original differential equation The corresponding expression.
[0112] The discretization formula of the Heun method (improved Euler method) includes two steps: prediction and correction. in, This represents the slope in the current state. Indicates according to The predicted state obtained, This represents the corrected slope calculated under the predicted state. The discretization engine generates two auxiliary update expressions and one main update expression, for example: The discretization formula for the fourth-order Runge-Kutta method (RK4) is as follows: in, , , and These represent the slope values calculated at the current point, the intermediate estimated point, and the next time point, respectively. The discretization engine generates auxiliary update expressions based on these intermediate slopes and generates corresponding main update expressions to improve the discretization accuracy of complex nonlinear differential equations.
[0113] The discretization formula for the implicit Euler method is as follows: in, This represents the function value calculated at the state in the next time step. For general nonlinear equations, this formula is an implicit equation and requires an iterative solution. For linear differential equations, such as: in, and If the coefficients are linear equation coefficients and can be analytically obtained from the expression on the right-hand side of the differential equation, then the implicit Euler method can yield an analytical update form: in, Represents state variables linear coefficients, Indicates and Irrelevant constant terms or external input terms. The discretization engine determines whether the differential equation satisfies the linear form by matching expressions; if it satisfies the linear form, it generates an analytical update expression; if it does not satisfy the linear form, it generates an implicit update expression with iterative solution markers.
[0114] The discretization engine uses regular expressions to detect whether the equation is linear. If it is, it generates an analytical formula; otherwise, it generates an approximate formula with iterative solution annotations.
[0115] Time step calculation is a crucial step in the discretization process. The discretization engine calculates the numerically stable global time step based on the selected discretization numerical processing method and the time constant of the differential equation.
[0116] Preferably, for the forward Euler method, the stability condition requires... ( (where time is constant), the discretization engine uses a safety factor of 0.8, and is consistent with... Choose the smaller value as the global time step; for the RK4 method, a global time step of [value] is recommended. For the implicit Euler method, since it is unconditionally stable, a global time step of is recommended. (Based on accuracy considerations rather than stability); for the Heun method, a global time step of [value missing] is recommended. When the network contains multiple neuron types, the algorithm takes the minimum recommended time step for all neurons to ensure global stability.
[0117] Thus, continuous-time differential dynamics are losslessly and standardizedly converted into discrete-time difference computation instructions. This not only significantly lowers the threshold for developers to master numerical analysis expertise, but also provides high-precision, strongly typed, and statically verifiable underlying computational semantic support for the subsequent generation of multi-level intermediate representations, cross-boundary joint optimization, and efficient code generation for multiple hardware backends. Ultimately, this achieves automated and highly reliable conversion of heterogeneous neural networks from high-level declarative descriptions to executable discrete difference logic.
[0118] In some embodiments of this disclosure, the abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree. Preferably, steps S410 to S420 are further included. Please refer to [link / reference]. Figure 4 .
[0119] In step S410, the operator instantiation in the abstract syntax tree is traversed, and name matching is performed according to the preset artificial neural network operator whitelist. If the match is successful, the operator instantiation is mapped to an artificial neural network layer model and recorded in the layer model part of the discretized intermediate representation; if the match fails, the operator instantiation is identified as a spiking neural network operator and neuron or synapse model conversion is performed.
[0120] In step S420, the corresponding network type is determined according to the type of instantiation of each operator. The network type includes artificial neural network type, spiking neural network type, or hybrid neural network type.
[0121] In this implementation, an automatic identification and direct mapping mechanism for ANN operators is implemented during the AST to DIR conversion process. Specifically, a whitelist of ANN operators (BUILTIN_ANN_OPS) is pre-defined in the compiler, covering various layer types commonly used in deep learning. These include: Convolutional layer operators include Conv1D (one-dimensional convolution), Conv2D (two-dimensional convolution), Conv3D (three-dimensional convolution), and ConvTranspose2D (two-dimensional transpose / deconvolution). Each convolutional operator records its required parameters (in_channels, out_channels, kernel_size), optional parameters (stride, padding, dilation, groups, bias), the corresponding PyTorch class name (e.g., nn.Conv2d), and the formula for calculating the output shape.
[0122] Pooling layer operators include MaxPool1D, MaxPool2D, MaxPool3D (max pooling), AvgPool1D, AvgPool2D (average pooling), AdaptiveAvgPool1D, AdaptiveAvgPool2D, AdaptiveMaxPool2D (adaptive pooling), GlobalAvgPool2D, and GlobalMaxPool2D (global pooling). The key parameter for pooling operators is `kernel_size`, with optional parameters including `stride`, `padding`, `dilation`, and `ceil_mode`. Adaptive pooling is characterized by specifying the output size directly, rather than the kernel size.
[0123] Fully connected layer operators: including Linear (linear layer) and Dense (an equivalent alias to Linear). Required parameters are in_features and out_features, and the optional parameter is bias.
[0124] Normalization layer operators include BatchNorm1D, BatchNorm2D, BatchNorm3D (batch normalization), LayerNorm (layer normalization), GroupNorm (group normalization), and InstanceNorm2D (instance normalization). The key parameters for normalization layers vary by type: BatchNorm requires `num_features`, LayerNorm requires `normalized_shape`, and GroupNorm requires `num_groups` and `num_channels`.
[0125] Activation function operators include ReLU, LeakyReLU, PReLU, ELU, SELU (Modified Linear Unit and its variants), GELU, Sigmoid, Tanh (classical activation functions), Softmax, LogSoftmax (normalized activation function), Swish, SiLU, Mish, and Hardswish (activation functions proposed in recent years). Activation functions typically have only optional parameters (such as negative_sl ope for LeakyReLU and dim for Softmax).
[0126] Regularization layer operators include Dropout, Dropout2D, Dropout3D (normal Dropout), and AlphaDropout (Dropout that preserves the mean and variance). The key parameter is the dropout probability p.
[0127] Shape manipulation operators include Flatten and Unflatten. The parameters start_dim and end_dim of Flatten specify the range of dimensions to be flattened.
[0128] Embedding layer operators: including Embedding. The parameter num_embeddings specifies the size of the embedding dictionary, and embedding_dim specifies the dimension of the embedding vector.
[0129] Recurrent network layer operators include LSTM (Long Short-Term Memory), GRU (Gated Recurrent Unit), and RNN (Recurrent Neural Network). The parameters input_size and hidden_size specify the input and hidden dimensions, respectively. Optional parameters include num_layers, bidirectional, and dropout.
[0130] Transformer layer operators include MultiheadAttention, TransformerEncoderLayer, and TransformerDecoderLayer. Parameters include d_model (model dimension), nhead (number of attention heads), and dim_feedforward (feedforward network dimension).
[0131] During the AST to DIR conversion, when operator instantiation is detected, the system first checks if the operator name is in the ANN operator whitelist. If it is, discretization is not performed; instead, it is directly recorded as an ANN layer model in the `layer_models` part of the DIR, recording its operator type, category, parameter configuration, and PyTorch class mapping. If the operator name is not in the ANN operator whitelist, it is treated as an SNN operator and undergoes neuron or synapse model conversion and discretization.
[0132] Preferably, the automatic identification of network type is based on the analysis of all nodes in the network. If the network contains only ANN layer models (all operator instantiations are in the whitelist), the network type is labeled as Artificial Neural Network type "ann". If the network contains only SNN neuron or synapse models, the network type is labeled as Spiking Neural Network type "snn". If the network contains both ANN layer models and SNN neuron or synapse models, the network type is labeled as Hybrid Neural Network type "mix". This information is recorded in the metadata section of DIR to guide subsequent MLIR dialect selection and code generation strategies.
[0133] In some embodiments of this disclosure, the abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree. Preferably, the method further includes: converting the learning rule declarations in the abstract syntax tree into structured learning rule descriptions. The learning rules include local learning rules and global training methods. The structured learning rule descriptions include the name of the learning rule, policy type, algorithm identifier, plasticity type, time window parameter, enhancement or suppression parameter, weight constraint, trace dynamics parameter, or update triggering condition.
[0134] In this embodiment, unlike representing learning rules as code strings, this disclosure converts learning rules into structured field descriptions, giving them advantages such as high readability, good verifiability, high portability, and strong scalability.
[0135] Preferably, the learning rules include local learning rules and global training methods.
[0136] Specifically, local learning rules are event-driven synaptic plasticity mechanisms where updates depend only on local neural activity (the pulse timing of preceding and following neurons) and do not require global error signals. This disclosure supports the following structured description of local learning rules: STDP (Spike-Timing-Dependent Plasticity) rules are the most classic local learning rules. Its structured description includes the following fields: strategy_type is identified as "LocalLearningRule"; algorithm is identified as "STDP"; plasticity_type is identified as "spike_timing_dependent"; and time_window contains... (tau_plus_ms, the decay time constant of the prepulse trace) (tau_minus_ms, the decay time constant of the post-pulse trace), and the corresponding upper limits of the time window (window_limit_plus_ms and window_limit_minus_ms, usually set to 5 times the decay constant); potentiation includes the amplitude (i.e., The depression includes the depression type (decay_type, usually "exponential") and the depression constant (decay_constant_ms); depression includes the depression amplitude (i.e., The `weight_constraints` class contains the minimum (min), maximum (max), and clipping strategy. `trace_dynamics` contains the dynamic parameters for the pre-trace and post-trace variables, with each variable recording its initial value, spike trigger increment, decay time constant, and decay type. `update_triggers` describes the update trigger conditions, including `on_pre_spike` (when the current neuron fires a spike, check the history of the post-neuron and apply inhibition) and `on_post_spike` (when the post-neuron fires a spike, check the history of the pre-neuron and apply enhancement).
[0137] The structured description of the Hebbian learning rule includes: algorithm identified as "Hebbian"; plasticity_type identified as "rate_based"; rate_computation describing the distribution rate calculation method, including the calculation method (e.g., spike_count) and time window (time_window_ms); and update_rule describing the update rule, including the type (type, "product") and formula.
[0138] Oja rules are Hebbian learning with weight normalization. Their structured description includes not only the fields for Hebbian learning but also the `oja_parameters` field, which records the normalization coefficients. And the normalization method (weight-dependent), and the update formula.
[0139] The structured description of the BCM sliding threshold mechanism (Bienenstock-Cooper-Munro rule) includes a threshold_dynamics field that describes the sliding threshold. Dynamics: initial value, time constant The updated rules enable plasticity that depends on the postsynaptic activity history.
[0140] Triplet STDP is an extension of STDP that considers the temporal relationship between three pulses. Its structured description includes time constants (tau_plus, tau_minus, tau_x, tau_y) for three time scales, pair enhancement amplitude and triplet enhancement amplitude, and dynamic parameters for four trace variables (pre_trace_fast, pre_trace_slow, post_trace_fast, post_trace_slow).
[0141] Global training methods are gradient-based training approaches that require a loss function and a backpropagation mechanism. Preferably, this disclosure supports the following structured description of global training methods: Surrogate gradient is the most commonly used method for training SNNs. It achieves backpropagation by approximating the gradient of the impulse function with a differentiable function. Its structured description includes: `algorithm_type` identified as "backpropagation_through_spike"; `surrogate_function` describing the surrogate function, including its type (e.g., sigmoid, fast_sigmoid, triangle, atan, etc.), parameters (e.g., beta controls the steepness of the gradient), and differentiability flag; `backpropagation` describing the backpropagation configuration, including the method (e.g., "BPTT" for time-based backpropagation), unrolling steps (`unroll_steps`, None for full unrolling), and gradient clipping settings; `optimizer` describing the optimizer, including its type (e.g., adam, sgd, rmsprop) and parameters (e.g., learning rate, momentum, weight decay, etc.); and `loss_function` describing the loss function, including its type (e.g., cross_entropy, mse) and reduction method.
[0142] The structured description of TBPTT (Truncated BPTT) additionally includes a truncation field, which records the truncation length (length_steps), the number of overlap steps, and the hidden state detachment flag (detach_hidden). Truncated BPTT reduces memory consumption and computation by limiting the time range of gradient backpropagation.
[0143] A structured description of RTRL (Real-Time Recurrent Learning) includes the `rtrl_config` field, which records the eligibility traces using labels, the online updating of labels in the sensitivity matrix, and the approximation method (such as full or sparse). RTRL is an online learning method that updates weights as time steps progress.
[0144] e-prop (Eligibility Propagation) is an online learning method that approximates BPTT. Its structured description includes the eprop_config field, which records the type of eligibility trace (e.g., adaptive), time constant, type of learning signal (e.g., symmetric or random), and online learning label (online_learning).
[0145] The structured description of optimizer parameters varies depending on the optimizer type. Preferably, the Adam optimizer includes beta1, beta2, epsilon, and weight_decay parameters; the SGD optimizer includes momentum, damping, weight_decay, and nesterov parameters; and the RMSprop optimizer includes alpha, epsilon, weight_decay, and momentum parameters.
[0146] This effectively overcomes the technical shortcomings of existing spiking neural network compilation frameworks and heterogeneous integration schemes, which embed learning rules into business logic as hard-coded strings, resulting in semantic ambiguity, lack of static verification, difficulty in cross-platform portability, and severely limited scalability.
[0147] In some embodiments of this disclosure, semantic analysis and discretization are performed on the abstract syntax tree to obtain a discretized intermediate representation corresponding to the abstract syntax tree. Preferably, steps S510 to S530 are also included. Please refer to [link / reference]. Figure 5 .
[0148] In step S510, the network definitions in the abstract syntax tree are traversed and a network topology containing nodes and edges is constructed; the connection declarations are judged for connection mode, which includes full connection, one-to-one connection, convergence connection or divergence connection.
[0149] In step S520, the network topology is verified. The topology verification includes compatibility verification of the source node output dimension and the target node input dimension corresponding to each edge, synapse number estimation, split pattern detection or convergence pattern detection.
[0150] In step S530, when a connection between an artificial neural network sub-node and a spiking neural network sub-node is detected, it is determined to be a heterogeneous boundary. In the discretization process, source-end transformation attributes and target-end transformation attributes are added to the corresponding edges. The source-end transformation attributes and the target-end transformation attributes include transformation function names and related parameters.
[0151] In this implementation, the compiler traverses each network definition in the abstract syntax tree, constructing a node-edge graph network topology by parsing operator instantiation and connection declarations in the construct section. During construction, the connection pattern (including fully connected, one-to-one connected, convergent connected, and divergent connected) is automatically inferred based on the syntactic context of the connection statements. Then, topology verification is performed on each edge, including dimensionality compatibility checks and connection pattern rationality checks. When the source node and target node are found to belong to ANN and SNN types respectively, they are identified as heterogeneous boundaries, and the corresponding source-end transformation and target-end transformation attributes are extracted or automatically generated and recorded in the edge structure of the discretized intermediate representation.
[0152] Specifically, the `construct` section of the neural network is traversed to identify operator instantiation statements and connection declaration statements. For each operator instantiation, a corresponding node is created, recording its type (determined by the operator name whether it's an ANN layer model or an SNN operator) and attributes (neuron / synapse type reference, input / output dimensions, etc.). For each connection declaration (represented by "->" or the `interconnect` method), a corresponding edge is created, recording the source node, target node, and connection attributes. Input and output ports are treated as special nodes, labeled as `input_port` and `output_port` types, respectively.
[0153] Connection pattern inference automatically infers the connection pattern by analyzing the context of the connection declaration (such as whether it is in a for loop, the form of the index expression, etc.). Specifically, in fully connected mode, each neuron in the source neuron group is connected to all neurons in the target neuron group. In one-to-one connection mode, the first neuron in the source neuron group is connected to all neurons in the target neuron group. i The nth neuron is only connected to the nth neuron in the target neuron group. i There are several target neurons. In convergent connection mode, multiple source neurons connect to the same target neuron. In divergent connection mode, one source neuron connects to multiple target neurons. Custom mode is used for complex connection logic, such as defining connection relationships through explicit index calculation.
[0154] Topology verification is a critical step in ensuring network correctness. Preferably, topology verification includes: Dimension matching check: Verify whether the output dimension of the source node of each edge is compatible with the input dimension of the target node; Synapse number estimation: Calculate the expected number of synapses based on connection patterns and the number of neurons to assess memory requirements; Split pattern detection: Identify cases where a source node is connected to multiple target nodes, and verify whether the output dimension of the source node is equal to the sum of the input dimensions of all target nodes; Convergence pattern detection: Identifies situations where multiple source nodes are connected to the same target node, and verifies whether the sum of the output dimensions of all source nodes equals the input dimension of the target node.
[0155] Topology validation results are recorded in the topology_validation field of DIR, including the status (matched, dimension_mismatch, etc.) and details of each check.
[0156] When a type difference is detected between subnetwork references (e.g., an ANN subnetwork connecting to an SNN subnetwork), this is identified as a heterogeneous boundary, requiring data format conversion. Boundary transformations are recorded in the edge's `from_transform` and `to_transform` attributes. `from_transform` records the transformation performed at the source end, including `function` (function name, such as "flatten") and `parameters` (parameter dictionary); `to_transform` records the transformation performed at the destination end, including `function` (function name, such as "spike_encode") and `parameters` (parameter dictionary, such as `method="rate"`, `scale=1.0`).
[0157] Shape transformations (such as the Flatten operation) are typically recorded in `from_transform`, with the function "flatten" and parameters that may include `start_dim` and `end_dim`. Pulse code transformations are recorded in `to_transform`, with the function "spike_encode" and parameters that include `method` (encoding method, such as "rate" rate encoding, "temporal" timing encoding, or "direct" direct encoding) and related parameters (such as the scale factor).
[0158] This effectively overcomes the blind spots and runtime instability caused by the implicit dispersion of topology, the difficulty in capturing dimensionality errors in advance, and the hard-coded cross-paradigm data transformation logic in the application layer in existing heterogeneous neural network compilation and integration schemes.
[0159] Please continue to refer to this. Figure 1 In step S140, the discretized intermediate representation is converted into a multi-level intermediate representation containing multiple dialects according to the multi-dialect mapping algorithm. The multiple dialects include spiking neural network dialects, artificial neural network dialects, hybrid dialects, or network dialects.
[0160] In this implementation, a discretized intermediate representation is used as the input source. The model tracking set and node mapping table are established by initializing the conversion environment and reading metadata. Then, following a preset ten-step pipeline algorithm, the data domains such as neuron model, synapse model, learning rules, artificial neural network layer model, network topology, operator structure, encoding and decoding operations, and network definition are traversed in turn. The discretized structured fields of each part are converted one by one into discrete update and threshold detection operations of spiking neural network dialect, layer definition operations of artificial neural network dialect, boundary transformation and execution order operations of mixed dialect, and port and running configuration operations of network dialect according to the multi-dialect mapping algorithm. Finally, a multi-level intermediate representation code that conforms to the multi-dialect grammar specification is output.
[0161] In some embodiments of this disclosure, step S140, converting the discretized intermediate representation into a multi-level intermediate representation containing multiple dialects according to a multi-dialect mapping algorithm, preferably includes steps S141 to S144. Please refer to [reference needed]. Figure 6 .
[0162] In step S141, the discretized intermediate representation of the neuron model is traversed, and neuron model definition operations and neuron behavior function operations conforming to the dialect of spiking neural networks are generated for each neuron model. The neuron behavior function operations include discrete update operations, threshold detection operations, and state setting operations.
[0163] In step S142, the discretized intermediate representation of the synaptic model is traversed, and synaptic model definition operations and learning rule definition operations conforming to the dialect of the spiking neural network are generated for each synapse.
[0164] In step S143, the layer models of the discretized intermediate representation are traversed, and layer model definition operations conforming to the artificial neural network dialect are generated for each artificial neural network layer model.
[0165] In step S144, the network topology of the discretized intermediate representation is traversed, and network definition operations for generating spiking neural network dialect, artificial neural network dialect, hybrid dialect, or network dialect are selected according to the network type of the network topology. For hybrid network types, hybrid connection operations and boundary definition operations that conform to the hybrid dialect are generated. The hybrid connection operations include source-end transformation attributes and target-end transformation attributes.
[0166] In this embodiment, the present disclosure provides four dedicated multi-level intermediate representation dialects, including spiking neural network dialect, artificial neural network dialect, hybrid dialect, and network dialect, which together express the complete computational semantics of heterogeneous neural networks.
[0167] Specifically, the spiking neural network dialect defines the set of operations for expressing the semantics of spiking neural networks, mainly including the following operation types: The neuron model definition operation (snn.neuron_model) is used to declare the neuron type. Its syntax is: "%model = snn.neuron_model @ModelName { parameter = {...}, initial_state ={...}}". The parameter dictionary stores constant parameters (such as threshold, time constant, etc.), and the initial_state dictionary stores the initial values of state variables (such as the initial value of membrane potential). Each parameter / state records its name, type (f32 / f64 / i32, etc.), and value.
[0168] Neuron behavior function operation (snn.func): Used to define the computational behavior function of a neuron. Its syntax is: "snn.func @FuncName(%in : f64, %dt : f64) ->i1 attributes { params = #snn.params<...>, states = #snn.params<...>} { ...}". The function signature includes the input current parameter (%in), the time step parameter (%dt), and the return type (i1 represents a Boolean impulse flag). The computational logic within the function body is represented by basic blocks, and the parameters of the basic blocks correspond to the neuron's parameters and state variables.
[0169] The discrete update operation (snn.discrete_update) is the core operation of the spiking neural network dialect, used to express discrete update calculations of state variables. Its syntax is: '%result = snn.discrete_update “var_name” { expression = “...”, method = “...”, dependencies = [...]} (%dep1, %dep2, ...) : (...) ->type'. The `expression` attribute stores the update expression string, the `method` attribute stores the numeric method name (forward_euler / heun / runge_kutta_4 / implicit_euler), the `dependencies` attribute stores the list of dependency variable names, the operands correspond to the SSA values of the dependency variables, and the result type corresponds to the state variable type. This design preserves the readability of mathematical expressions while maintaining the SSA form and type system of MLIR.
[0170] The threshold detection operation (snn.threshold) is used to determine whether the membrane potential exceeds a threshold. Its syntax is: "%spike = snn.threshold %potential, %threshold : (type1, type2) ->i1". This operation takes the current membrane potential and the threshold as operands and returns a Boolean pulse flag.
[0171] The state setting operation (snn.set_state) is used to update the state variables of a neuron. Its syntax is: 'snn.set_state "var_name" %value : type'. This operation writes the specified value to the state variable with the specified name, and is used to implement a reset mechanism or state initialization.
[0172] The return operation (snn.return) is used to terminate the neuron behavior function and return the spike flag. Its syntax is: "snn.return %spike : i1".
[0173] The synaptic model definition operation (snn.synapse_model) is used to declare synapse types. Its syntax is: "%synapse = snn.synapse_model @ModelName { parameter = {...}, initial_state ={...}, transmission_rule = {...}, connection_types = {...}, learning_rule = @RuleName}". In addition to parameters and state, it also includes transmission rules (describing how impulses are transmitted from the preneuron to the postneuron), connection type declarations (recording the types of the pre- and post-neurons), and references to learning rules.
[0174] The learning rule definition operation (snn.learning_rule) is used to define the parameter configuration of the learning rule, converting the structured learning rule description in DIR into MLIR attributes. For STDP, attributes include strategy_type, algorithm, time_window (including tau_plus, tau_minus, etc.), potentialization (including amplitude), depression (including amplitude), and weight_constraints (including min, max, clipping). For the global training method, attributes include algorithm_type, surrogate_function configuration, optimizer configuration, and loss_function configuration.
[0175] The neuron group operation (snn.neuron_group) is used to create a specified number of groups of neurons of the same type. Its syntax is: "%group = snn.neuron_group @GroupName { neuron_model = @ModelRef,count = N}". This operation references a defined neuron model and creates a group of N neurons of that type.
[0176] Topology model operations (snn.topology_model): Used to define the connection topology between groups of neurons. Its syntax is: "%topo = snn.topology_model @TopoName { presynaptic_shape = [...],postsynaptic_shape = [...], parameter = {...}}". The shape attribute records the dimensions of the preceding and following neuron groups, while the parameter attribute can include configurations such as connection probabilities and weight initialization methods.
[0177] Dynamic topology operations (snn.dynamic_topology): used to handle connections with unknown dimensions at compile time (such as subnetwork references), with the syntax: "%topo = snn.dynamic_topology @TopoName { source = "...", target = "...", resolve_at_runtime = true}". These topologies are resolved at runtime based on the actual subnetwork dimensions.
[0178] Artificial neural network dialects define the set of operations for expressing traditional neural network layers, mainly including the following operation types: The layer model definition operation (ann.layer_model) is used to define the type of ANN layer. Its syntax is: "%layer_model = ann.layer_model @ModelName { op_type = "...", category = "...", pytorch_class = "...", parameters = {...}, shape_info = {...}, trainable =true / false, requires_discretization = false, connection_constraints = {...}}". `op_type` records the operator type name (e.g., Conv2D, Linear, etc.), `category` records the operator category (e.g., convolution, linear, etc.), `pytorch_class` records the corresponding PyTorch class name (used for code generation), `parameters` records the operator parameters, `shape_info` records the input and output shapes and the number of parameters, `trainable` indicates whether the layer is trainable, `requires_discretization` indicates whether discretization is required (usually false for ANN layers), and `connection_constraints` records the connection constraints (valid input type, output data type, etc.).
[0179] Layer instance operation (ann.layer): Used to instantiate ANN layers in the network and establish data flow relationships between layers. The layer instance operation references the layer model definition and specifies the specific input connections.
[0180] Encoding operations (ann.encode_op) and decoding operations (ann.decode_op): In pure ANN networks, they may be used for input preprocessing and output postprocessing, but their main use case is handling heterogeneous boundaries in mixed dialects.
[0181] The hybrid dialect defines the set of operations for expressing boundary transformations in ANN-SNN hybrid networks. It primarily supports the following operations: The `hybrid.network` operation is the top-level container for hybrid networks, with the syntax: `hybrid.network @NetworkName { ...}`. Internally, the network contains ANN layer definitions, SNN component definitions, input / output port definitions, and connection definitions.
[0182] Hybrid connection operations (hybrid.connection): used to express cross-type connections (ANN layer to SNN component or vice versa), and their syntax includes attributes such as source, target, source_type, target_type, from_transform, and to_transform. from_transform and to_transform define the data transformation operations on the source and target sides, respectively.
[0183] The `hybrid.transform` operation is used to express data format transformations and supports the following transformation types: `flatten` is used to flatten the shape, with parameters including `start_dim` and `end_dim`, `input_type` being `tensor_nd`, and `output_type` being `tensor_1d`; `spike_encode` is used to encode continuous values into a spike sequence, with `input_type` being `continuous`, `output_type` being `spike_train`, and parameters including `method` (rate / temporal / direct / latency), `scale`, `time_steps`, `threshold`, etc.
[0184] The `hybrid.execution_order` operation defines the execution order of components in a hybrid network. Its syntax is: `hybrid.execution_order { stages = [...], num_stages = N, ann_layer_count = M, snn_layer_count = K, subnetwork_count = L}`. Each stage records its stage_id, type (ANN / SNN / subnetwork), and a list of nodes.
[0185] The boundary definition operation (hybrid.boundaries) is used to explicitly record the boundaries of ANN-SNN transformations. Its syntax is: "hybrid.boundaries { transitions = [...], num_boundaries = N}". Each transition records the source, target, boundary_type (ann_to_snn / snn_to_ann), from_transform, to_transform, and require_time_conversion flags.
[0186] The encoding operation (hybrid.encode_op) and decoding operation (hybrid.decode_op) are independent encoding / decoding operation definitions, which record the network to which they belong, input / output, target node, method, and parameter configuration.
[0187] Network dialects define general network-level operations, not specific to ANNs or SNNs. They support the following operations: Input port operation (network.input_port): Used to define the input port of the network. Its syntax is: "%port = network.input_port @PortName { name = "...", data_type = "...", parent_network = "..."}.
[0188] Output port operation (network.output_port): Used to define the output port of the network, with a syntax similar to that of the input port.
[0189] Subnetwork reference operation (network.subnetwork_ref): Used to declare references to other networks. Its syntax is: "%ref = network.subnetwork_ref @RefName { ref_network = "...",network_type = "ann" / "snn", instance_name = "...", parent_network = "...",inputs = [...], outputs = [...], compute_mode = "continuous" / "discrete",requires_time_steps = true / false}.
[0190] The subnetwork instance operation (network.subnetwork_instance) is used to instantiate a subnetwork in a hybrid network. Its syntax is: "%subnet = network.subnetwork_instance @InstanceName { ref_network = "...", network_type = "...", parent_network = "..."}.
[0191] Network property operations (network.properties): Used to define network-level property configurations, such as computation mode (computation_mode).
[0192] The `network.run_config` operation defines the parameter configuration for training and inference. Its syntax is: "network.run_config { defaults = {...}, train = {...}, infer = {...}, subnetwork_config = {...}}".
[0193] This breaks down the technical barrier between the static tensor semantics of artificial neural networks and the temporal dynamics semantics of spiking neural networks in the existing compilation framework, and realizes high-fidelity coexistence and lossless conversion of heterogeneous computing paradigms under a unified intermediate representation system.
[0194] In some embodiments of this disclosure, the discretized intermediate representation to a multi-level intermediate representation is implemented by the DIRToMLIRConverter class, mainly including the following steps: The first step is initialization and metadata processing. The converter reads DIR data, initializes its internal state (the tracking set of the created model, node value mapping, network type information, etc.), and generates MLIR file header annotations (containing version information, generation time, source file information, etc.).
[0195] The second step is neuron model generation. It iterates through the neuron_models part of DIR, generating an snn.neuron_model operation and a corresponding snn.func behavior function for each neuron model. During behavior function generation, each update rule is converted into an snn.discrete_update operation, spike detection is converted into an snn.threshold operation, and reset mechanisms are converted into conditional branches and snn.set_state operations.
[0196] The third step is learning rule generation. The learning rules in the `synapse_models` part of the DIR are traversed, and an `snn.learning_rule` operation is generated for each learning rule. The structured learning rule description is converted into an MLIR attribute dictionary.
[0197] The fourth step is synapse model generation. Iterate through the `synapse_models` section of DIR, generating an `snn.synapse_model` operation for each synapse model, referencing the already created learning rules.
[0198] Step 5: ANN layer model generation. Iterate through the layer_models part of DIR and generate ann.layer_model operations for each ANN layer model, recording operator type, parameters, shape information, etc.
[0199] Step 6: Topology Model Generation. Analyze the edge list in the `network_construction` part of the DIR and generate an `snn.topology_model` operation for each unique dimension combination. Dimension information is extracted from the output dimension of the source node and the input dimension of the target node. For subnetwork references (with dimensions unknown at compile time), generate an `snn.dynamic_topology` operation.
[0200] Step 7: OP structure generation. If DIR contains an op_structures section, generate the snn.op_structure operation, recording the operator's parameter definitions, neuron group creation, synapse group creation, connection patterns, and validation rules.
[0201] Step 8: Generation of encoding / decoding operations. Traverse the encode_decode_ops part of DIR, select the appropriate dialect prefix (ann / snn / hybrid) according to the network type, and generate the encode_op and decode_op operations.
[0202] Step 9: Network Definition Generation. Select the appropriate network definition operation (snn.network / ann.network / hybrid.network) based on the network type to generate the top-level network structure. Internally, the network sequentially generates input ports (network.input_port), layer / group instances, connection definitions, and output ports (network.output_port). For hybrid networks, boundary connections use the hybrid.connection operation with the hybrid.transform attribute.
[0203] Step 10: Run the configuration generation. This generates the network.run_config operation, converting the run_config portion of the DIR into MLIR attributes.
[0204] Furthermore, during the conversion process, the converter maintains detailed logs and statistics, including the number of various models created, the number of connections, the number of boundary transformations, and warnings about potential problems (such as dimension mismatches, unknown type references, etc.). This information helps in debugging and optimizing the network definition.
[0205] In a specific application scenario, an image classification hybrid network has the following architecture: (1) an SNN component, which includes LIF neurons (containing membrane potential dynamics equations), Basic synapses (containing STDP learning rules), and SNN_FC fully connected operators; (2) an ANN sub-network ANN_Encoder, which uses a Conv2D(1→8, 3×3) → MaxPool2D(2×2) structure for feature extraction; (3) an SNN sub-network SNN_Classifier, which uses SNN_FC(1352→10) for pulse classification; and (4) a hybrid network Simple_Hybrid_Net, which connects ANN_Encoder and SNN_Classifier in sequence, and uses flatten+spike_encode transformation at the boundary. It is compiled through the following execution stages: Phase 1: Lexical Analysis and Syntax Analysis After lexical and syntactic analysis, the generated AST contains: a NeuronDefinition node (defining LIF neurons, containing DifferentialEquation nodes to represent membrane potential dynamics), a SynapseDefinition node (defining Basic synapses, containing LearningRule nodes), an OPDefinition node (defining SNN_FC operators, containing neuron_type and synapse_type generic parameters), and three NetworkDefinition nodes (defining ANN_Encoder, SNN_Classifier, and Simple_Hybrid_Net networks, respectively).
[0206] Phase Two: AST to DIR Conversion After the AST to DIR conversion, the generated DIR includes: (1) metadata marking the network type, where ANN_Encoder is ann, SNN_Classifier is snn, and Simple_Hybrid_Net is mix, and the time step is recorded. (According to LIF) (1) Calculated using the forward Euler method); (2) neuron_models contains the discretized description of the LIF model, and update_rules records the update expression "v_next = v + 0.1". ((-(v - v_rest) / tau))”, spike_detection records the threshold condition, reset_mechanism records the hard reset; (3) synapse_models contains the structured STDP learning rule description of the Basic synapse model; (4) layer_models contains two ANN layer models, Conv2D and MaxPool2D; (5) op_structures contains the parameter definition and connection mode of SNN_FC; (6) network_construction records the nodes and edges of the three networks, of which Simple_Hybrid_Net contains the boundary edge from ann_part to snn_part, with from_transform (flatten) and to_transform (spike_encode) attributes; (7) run_config records the training configuration.
[0207] Phase 3: DIR to MLIR Conversion After DIR to MLIR conversion, the generated MLIR code includes: (1) Spiking neural network dialect: snn.neuron_model @LIF definition, snn.func @LIF behavior function (including snn.discrete_update operation), snn.synapse_model @Basic definition; (2) Artificial neural network dialect: ann.layer_model definition (Conv2D, MaxPool2D); (3) Hybrid dialect: hybrid.transform definition (@flatten and @spike_encode), hybrid.boundaries boundary definition, hybrid.connection hybrid connection; (4) Network dialect: network.input_port / output_port, network.subnetwork_instance, network.run_config.
[0208] Figure 7 This is a schematic block diagram of a multi-layer intermediate representation compilation apparatus for a heterogeneous neural network according to one embodiment of the present disclosure. For ease of explanation, certain steps of the above method are described in correspondence with modules. It should be understood that the corresponding module performing one or more steps of the above method may be one or more hardware modules specifically configured to perform the corresponding steps, or implemented by a processor configured to perform the corresponding steps, or stored in a computer-readable medium for implementation by a processor, or implemented by some combination thereof.
[0209] like Figure 7 As shown, the multi-level intermediate representation compilation device for the heterogeneous neural network preferably includes an acquisition module 710, a first processing module 720, a second processing module 730, and a third processing module 740.
[0210] Specifically, the acquisition module 710 is used to acquire the source program corresponding to the heterogeneous neural network. The source program is described using a hybrid neural network language, which is used to uniformly describe artificial neural network components and spiking neural network components. The first processing module 720 is used to perform lexical analysis and syntactic analysis on the source program to generate an abstract syntax tree corresponding to the source program. The second processing module 730 is used to perform semantic analysis and discretization processing on the abstract syntax tree to obtain a discretized intermediate representation corresponding to the abstract syntax tree. The discretized intermediate representation includes metadata of the heterogeneous neural network, a discretized neuron model description, a structured learning rule description, a network topology, and a heterogeneous boundary transformation description. The network topology is used to describe the network connection relationship, and the heterogeneous boundary transformation description is used to define the data transformation rules of the heterogeneous network boundary. The third processing module 740 is used to convert the discretized intermediate representation into a multi-level intermediate representation containing multiple dialects according to a multi-dialect mapping algorithm. The multiple dialects include spiking neural network dialects, artificial neural network dialects, hybrid dialects, or network dialects.
[0211] The specific implementation of each module in the above-mentioned device can be referred to the implementation process of the corresponding steps in the above-mentioned method implementation method of this disclosure, and will not be repeated here.
[0212] Figure 8 This is a schematic structural block diagram of an electronic device according to one embodiment of the present disclosure.
[0213] like Figure 8As shown, the hardware structure of electronic device 1000 can be implemented using a bus architecture. The bus architecture can include any number of interconnect buses and bridges, depending on the specific application and overall design constraints of the hardware. Bus 1100 connects various circuits including one or more processors 1200, memory 1300, and / or hardware modules. Bus 1100 can also connect various other circuits 1400 such as peripherals, voltage regulators, power management circuits, external antennas, etc. Bus 1100 can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, only one connection line is used in this figure, but this does not indicate that there is only one bus or one type of bus.
[0214] This disclosure also provides a readable storage medium storing a computer program that, when executed by a processor, is used to implement the methods described above. A "readable storage medium" can be any means that can contain a program for storage, communication, propagation, or transmission for use by or in conjunction with an instruction execution system, apparatus, or device. More specific examples of a readable storage medium include: an electrical connection with one or more wires (electronic device), a portable computer disk drive (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and programmable read-only memory (EPROM or flash memory), fiber optic devices, and portable read-only memory (CDROM), etc.
[0215] This disclosure also provides a computer program product, the methods of which can be implemented wholly or partially through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented wholly or partially as a computer program product. The computer program product includes one or more computer programs or instructions. When the computer program or instructions are loaded and executed, all or part of the processes or functions of this disclosure are performed.
[0216] Computer programs or instructions can be stored in a readable storage medium or transferred from one readable storage medium to another. For example, the computer program or instructions can be transferred from one website, computer, server, or data center to another website, computer, server, or data center via wired or wireless means. The readable storage medium can be any available medium capable of access, or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium, such as a floppy disk, hard disk, or magnetic tape; an optical medium, such as a digital video optical disc; or a semiconductor medium, such as a solid-state drive. The computer-readable storage medium can be a volatile or non-volatile storage medium, or it can include both volatile and non-volatile types of storage media.
[0217] Those skilled in the art will understand that embodiments of this disclosure can be provided as methods, systems, or computer program products. Therefore, this disclosure can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this disclosure can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0218] This disclosure is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus, and computer program products according to this disclosure. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0219] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0220] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0221] In the description of this specification, the references to terms such as "one embodiment / mode," "some embodiments / modes," "example," "specific example," or "some examples," etc., refer to specific features, structures, or characteristics described in connection with that embodiment / mode or example, which are included in at least one embodiment / mode or example of this disclosure. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment / mode or example. Moreover, the specific features, structures, or characteristics described may be combined in any suitable manner in one or more embodiments / modes or examples. Furthermore, without contradiction, those skilled in the art can combine and integrate the different embodiments / modes or examples described in this specification, as well as the features of different embodiments / modes or examples.
[0222] Those skilled in the art should understand that the above embodiments are merely for illustrating the present disclosure and are not intended to limit the scope of the disclosure. Those skilled in the art can make other changes or modifications based on the above disclosure, and these changes or modifications still fall within the scope of the present disclosure.
Claims
1. A method for compiling multi-level intermediate representations of heterogeneous neural networks, characterized in that, include: Obtain the source code corresponding to the heterogeneous neural network. The source code is described using a hybrid neural network language, which is used to uniformly describe artificial neural network components and spiking neural network components. Lexical and syntactic analysis are performed on the source program to generate an abstract syntax tree corresponding to the source program; The abstract syntax tree is semantically analyzed and discretized to obtain a discretized intermediate representation corresponding to the abstract syntax tree. This discretized intermediate representation includes metadata of the heterogeneous neural network, a discretized neuron model description, a structured learning rule description, a network topology, and a heterogeneous boundary transformation description. The network topology describes the network connectivity, and the heterogeneous boundary transformation description defines the data transformation rules for the heterogeneous network boundaries. According to the multi-dialect mapping algorithm, the discretized intermediate representation is converted into a multi-level intermediate representation containing multiple dialects, including spiking neural network dialects, artificial neural network dialects, hybrid dialects, or network dialects.
2. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 1, characterized in that, Lexical and syntactic analysis are performed on the source program to generate an abstract syntax tree corresponding to the source program, including: The source program's character stream is converted into a token sequence using a lexical analyzer. This token sequence includes top-level keyword tokens, nested keyword tokens, differential equation tokens, time unit tokens, data type tokens, string literal tokens, operator tokens, delimiter tokens, and indentation tokens. The token sequence is parsed into an abstract syntax tree by a recursive descent parser. The abstract syntax tree includes expression nodes for representing computational expressions, statement nodes for representing program statements, and definition nodes for representing the top-level definition structure of a hybrid neural network language. The recursive descent parser includes a first parsing method for parsing neuron definitions, a second parsing method for parsing synapse definitions, a third parsing method for parsing operator definitions, and a fourth parsing method for parsing network definitions.
3. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 2, characterized in that, During the parsing process of the recursive descent parser, the recursive descent parser further includes performing the following steps: When an unknown identifier is detected, the similarity between the unknown identifier and a known keyword is calculated. If the similarity is greater than or equal to a similarity threshold, an error message containing correction suggestions is generated. Alternatively, if a definition parsing error is detected, the parsing process for the current definition is skipped, and subsequent definitions are parsed instead.
4. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 1, characterized in that, The abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree, including: Mathematical property analysis and complexity assessment are performed on the differential equations in the abstract syntax tree to determine the mathematical properties and complexity of the differential equations. The mathematical properties include linear equations or rigid equations. Based on the mathematical properties and complexity of the differential equation, determine the corresponding discretization numerical processing method for the differential equation; and Based on the determined discretization numerical processing method, a discrete update rule for the differential equation is generated. The discrete update rule includes the state variable name, the discrete update expression, the global time step, and the list of dependent variables.
5. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 4, characterized in that, The abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: The operator instantiation process iterates through the abstract syntax tree, performing name matching based on a pre-defined whitelist of artificial neural network operators. If a match is successful, the operator instantiation is mapped to an artificial neural network layer model and recorded in the layer model part of the discretized intermediate representation; if a match fails, the operator instantiation is identified as a spiking neural network operator and converted into a neuron or synapse model. Based on the type of instantiation of each operator, the corresponding network type is determined, including artificial neural network type, spiking neural network type, or hybrid neural network type.
6. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 4, characterized in that, The abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: The learning rule declarations in the abstract syntax tree are converted into structured learning rule descriptions. The learning rules include local learning rules and global training methods. The structured learning rule descriptions include the name of the learning rule, policy type, algorithm identifier, plasticity type, time window parameter, enhancement or suppression parameter, weight constraint, trace dynamics parameter or update trigger condition.
7. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 4, characterized in that, The abstract syntax tree is subjected to semantic analysis and discretization to obtain a discretized intermediate representation corresponding to the abstract syntax tree, and the method further includes: Traverse the network definitions in the abstract syntax tree and construct a network topology containing nodes and edges; determine the connection mode of the connection declarations, including full connection, one-to-one connection, convergence connection or divergence connection; The network topology is subjected to topology verification, which includes compatibility verification of the source node output dimension and the target node input dimension for each edge, synapse number estimation, split pattern detection, or convergence pattern detection; and When a connection is detected between a child node of an artificial neural network and a child node of a spiking neural network, it is determined to be a heterogeneous boundary. In the discretization process, the corresponding edge is represented by adding source-end transformation attributes and target-end transformation attributes. The source-end transformation attributes and the target-end transformation attributes include transformation function names and related parameters.
8. The multi-level intermediate representation compilation method for heterogeneous neural networks as described in claim 1, characterized in that, According to the multi-dialect mapping algorithm, the discretized intermediate representation is converted into a multi-level intermediate representation containing multiple dialects, including: Traverse the discretized intermediate representation of the neuron model, and generate neuron model definition operations and neuron behavior function operations that conform to the dialect of spiking neural networks for each neuron model. The neuron behavior function operations include discrete update operations, threshold detection operations, and state setting operations. Traverse the discretized intermediate representation of the synaptic model, and generate synaptic model definition operations and learning rule definition operations that conform to the dialect of the spiking neural network for each synapse; Iterate through the discretized intermediate representation layer models and generate layer model definition operations that conform to the artificial neural network dialect for each artificial neural network layer model; Traverse the network topology of the discretized intermediate representation, and select network definition operations to generate spiking neural network dialect, artificial neural network dialect, hybrid dialect or network dialect according to the network type of the network topology. For hybrid network type, generate hybrid connection operations and boundary definition operations that conform to the hybrid dialect. The hybrid connection operations include source-end transformation attributes and target-end transformation attributes.
9. An electronic device, characterized in that, include: The memory stores execution instructions; as well as A processor that executes the execution instructions stored in the memory, causing the processor to perform the multi-level intermediate representation compilation method for heterogeneous neural networks as described in any one of claims 1 to 8.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the multi-level intermediate representation compilation method for heterogeneous neural networks as described in any one of claims 1 to 8.