An internal activation causal intervention-based large language model error positioning method and system

CN122905009APending Publication Date: 2026-10-13THINKING CHAIN (TIANJIN) INTELLIGENT TECH CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611401483.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-10
Publication Date
2026-10-13

AI Technical Summary

Technical Problem

1.相关性替代因果性:注意力可视化、梯度归因等方法仅能反映统计相关性,无法证明网络单元与错误输出之间的因果关系,定位结果可靠性不足

Benefits of technology

1.双因果效应联合判定可排除“替换有效但屏蔽无改善”的伪错误源,定位准确率较单维度替换干预提升约30个百分点。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122905009A_ABST
    Figure CN122905009A_ABST
Patent Text Reader

Abstract

The application discloses a large language model error positioning method and system based on internal activation causal intervention, unified evaluation criteria before divergence are established by constructing counterfactual reference operation and activating tensor alignment; a three-dimensional directional causal effect framework composed of shielding improvement causal effect, reference replacement recovery causal effect and editing security causal effect is proposed, and high confidence error sources are jointly identified through double causal effects; the computation amount is compressed to an engineering feasible order of magnitude through two-stage activation difference pre-screening, and the error propagation direction between layers is verified through candidate unit level net effect blocking experiments; error types are automatically distinguished based on multi-dimensional causal signatures, and combined with safety judgment, a directional repair strategy is matched to form a complete technical closed loop of "positioning-verification-repair". The application can realize accurate positioning of attention head level and neuron level, significantly improve error positioning accuracy and repair success rate, and effectively reduce the damage of repair operation to non-target capabilities of the model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of artificial intelligence and natural language processing technology, specifically to a method for internal activation analysis and error localization of large language models based on a controlled causal intervention mechanism, which can be applied to scenarios such as interpretability analysis, error debugging, knowledge editing, and model optimization of large language models. Background Technology

[0002] Large-scale language models (LLMs, such as the LLaMA series and GPT series) have been widely deployed in tasks such as question answering, reasoning, and code generation. However, these models have a large number of parameters, their internal mechanisms are black-boxed, and it is difficult to accurately locate and repair errors. Existing interpretability technologies suffer from the following core shortcomings: 1. Correlation replaces causation: Methods such as attention visualization and gradient attribution can only reflect statistical correlation and cannot prove the causal relationship between network units and error outputs, resulting in insufficient reliability of localization results.

[0003] 2. Coarse localization granularity: Most methods can only achieve hierarchical localization and cannot reach fine-grained units such as attention heads and neurons, resulting in an excessively large repair range.

[0004] 3. Lack of reliable propagation path modeling: Existing methods are mostly static activation comparisons, which cannot verify the directional propagation path of errors between layers and make it difficult to trace the source of errors.

[0005] 4. Error types cannot be automatically distinguished: It cannot automatically identify errors caused by different reasons such as knowledge gaps, error associations, and skipped steps in reasoning, making it difficult to match targeted repair solutions.

[0006] 5. Interpretability conclusions cannot be directly mapped to repair operations: Existing causal tracking techniques (such as activation patching methods) are designed for factual knowledge editing, rely on external known references for activation, and only measure one dimension of replacement sufficiency. They cannot distinguish between "sufficient but unnecessary units" and "sufficient and necessary true error sources," which can easily lead to catastrophic amnesia during repair and undermine the model's non-target capabilities. Summary of the Invention

[0007] This invention addresses the shortcomings of existing technologies by proposing a method for error localization in large language models based on internal activation causal intervention, aiming to solve the following technical problems: 1. Achieve precise error localization at the attention head and neuron levels, improving the accuracy of error source localization; 2. By using a pre-screening mechanism, the computational workload of full intervention is reduced from an engineering infeasibility level to a feasible level; 3. Verify erroneous inter-layer directed propagation paths and distinguish between correlation and causation; 4. Automatically distinguishes error types caused by different factors and matches targeted repair strategies; 5. Introduce an editing safety assessment to narrow the scope of parameter repair and reduce the damage to the model's non-target capabilities caused by the repair.

[0008] According to a first aspect of the present invention, a method for error localization of a large language model based on internal activation causal intervention is provided, comprising the following steps: S1. Construct error runs and counterfactual reference runs, and collect the activation tensors of the Transformer layer in the two types of runs respectively; determine the first divergence position between the error output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the effective position set and step missing marker, and calculate the activation difference tensor within the pre-divergence intervention window. S2. Perform two-stage screening based on the activation difference tensor within the pre-divergence intervention window, and output the candidate layer set and the candidate unit set; S3. For each candidate unit in the candidate unit set, perform shielding intervention, replacement intervention and enhancement intervention respectively within the pre-divide intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement restoration causal effect and the editing security causal effect respectively, forming a three-dimensional causal effect vector. S4. Based on the reference replacement to restore the causal effect in the three-dimensional causal effect vector, the error-specific layer is determined through a controlled error control experiment; adjacent candidate layer pairs are selected from the candidate layer set, and joint intervention and candidate unit-level net effect blocking experiments are performed on them to verify the direction of inter-layer error propagation and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking. S5. Based on the shielding improvement causal effect and reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the missing step marker, the error start layer position and the highest contribution unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation, match the preset multi-type error causal signatures to automatically identify the error type. S6. Based on the error type and the repair level range determined by the directed error propagation path, and combined with the judgment result of the editing security causal effect in the three-dimensional causal effect vector, select the corresponding repair strategy to perform repair; after the repair is completed, verify the degree of causal effect elimination based on the reference replacement recovery causal effect, and judge the repair effect by combining the accuracy reduction of the model on the preset general capability benchmark set. If the judgment condition is not met, start the next round of positioning-repair cycle from step S3.

[0009] Furthermore, in step S1, the construction of the counterfactual reference operation specifically includes: A hierarchical strategy is adopted to automatically generate reference candidates, with priority from high to low as external knowledge base verification, self-consistent majority vote sampling, and perplexity-assisted filtering. The confidence level of the reference candidate labels is determined, and the positioning process is terminated if the confidence level is insufficient. Inject verified and correct evidence fragments into the original question input to construct a counterfactual reference input, enabling the model to generate the correct token at the divergence position under the counterfactual reference input; Record the token alignment mapping relationship between the original input and the counterfactual reference input. When replacing the intervention, only the activation value is replaced, without changing the token and position code of the original input.

[0010] Furthermore, in step S1, the step-level and token-level monotonic alignment of the activation tensors of the two types of operations is performed to obtain the set of valid positions and the missing step markers, specifically including: Perform position alignment directly on the input prefix; For the answer output part, first split the reasoning steps according to the boundary to obtain the step sequence, and then perform monotonic dynamic programming to align the steps through sentence vector similarity; Within the matched steps, perform token-level monotonic alignment with gap penalty to obtain a set of valid positions for bidirectional matching; steps that cannot be matched are marked as missing steps.

[0011] Furthermore, step S1 also includes a preliminary verification step of the donor's validity, specifically including: For each layer, candidate cell, and location, calculate the normalized donor difference for activation at the corresponding location between the erroneous run and the reference run; When the normalized donor difference is greater than the preset donor difference threshold, the reference activation at that location is used as a valid donor in the replacement intervention calculation.

[0012] Furthermore, step S2 specifically includes: Calculate the Frobenius norm of the activation difference tensor within the window before the divergence of each layer, and take the first r layers with the largest norm as the candidate layer set; Within the candidate layer set, the Frobenius norm of the activation difference of each attention head and the mean absolute value of the activation difference of each MLP neuron are calculated respectively. A preset number of attention heads and MLP neurons are selected in descending order to form a candidate unit set.

[0013] Further, in step S3, the masking improvement causal effect is: after setting the activation of the candidate unit to zero in the pre-divergence window, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of improvement of the erroneous output after masking the unit; the reference replacement recovery causal effect is: after replacing the activation of the candidate unit in the pre-divergence window with the activation of the effective donor at the corresponding position of the reference run, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of recovery of the output after replacement with the correct activation; the editing security causal effect is: after amplifying the activation amplitude of the candidate unit in the pre-divergence window, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the risk of output deterioration after enhancing the activity of the unit.

[0014] Furthermore, step S3 also includes: when the masking improvement causal effect of a candidate unit is greater than a preset masking threshold and the reference replacement recovery causal effect is greater than a preset replacement threshold, the candidate unit is identified as a high-confidence error source; the error source units are sorted by using the sum of the masking improvement causal effect and the reference replacement recovery causal effect as the priority ranking score.

[0015] Furthermore, in step S4, the candidate unit-level net effect blocking experiment specifically includes: Select source and target layers that satisfy the inter-layer adjacency condition from the candidate layer set. Based on the reference replacement causal effect, calculate the probability of the correct target token when only the source layer candidate unit is replaced, only the target layer candidate unit is replaced, and both candidate units are replaced at the same time. Forward blocking and reverse blocking experiments were performed respectively. Forward blocking involves replacing the source layer candidate unit while blocking the target layer candidate unit, and reverse blocking involves replacing the target layer candidate unit while blocking the source layer candidate unit. Calculate the forward mediation strength and the reverse mediation strength respectively. When the forward mediation strength is greater than a preset mediation threshold and the reverse mediation strength is less than a preset ratio of the forward mediation strength, the directed propagation edge from the source layer to the target layer is considered to be established.

[0016] Furthermore, in step S5, the error types include MLP error knowledge representation type, attention error association type, and inference step missing type. Combined with the directed error propagation path, the causal signatures corresponding to each type of error are as follows: MLP error knowledge representation type: The error initiation layer in the directed error propagation path is located in the shallow layer of the model, the activation of the corresponding unit in the counterfactual reference operation is significantly lower than the average level of the same layer, and the reference replacement recovery causal effect of knowledge injection verification meets the preset recovery conditions. Attention error correlation type: The highest contributing unit in the directed error propagation path is the attention head, the output activation similarity of the attention head in the error operation and the reference operation is lower than a preset threshold, and its shielding improvement causal effect meets the preset improvement conditions; Inference step missing type: The alignment process has missing step markers, the directed error propagation path has multiple consecutive error-specific layers, and the mean of the shielding improvement causal effect of the consecutive layers meets the preset conditions.

[0017] Furthermore, in step S6, the selection rule for the repair strategy is as follows: For the error knowledge representation of MLP, the target repair layer is determined by combining the directed error propagation path. When the causal effect of editing security is greater than or equal to the preset security threshold, the target layer is updated with regularized rank-one weights. When the causal effect of the edited security is less than the preset security threshold, switch to LoRA fine-tuning; For attention error-related issues, output the position index of the target attention head in the directed error propagation path, and perform attention mask scaling or head pruning. For cases with missing inference steps, the fine-tuning level range is determined based on the directed error propagation path. The missing inference steps are reconstructed by combining the reference run activation. After generating training samples, LoRA fine-tuning is performed on the specified layer.

[0018] Furthermore, in step S6, the judgment condition for verifying the repair effect is: the absolute mean of the causal effect of the reference replacement recovery of all candidate units after repair is less than the preset repair success threshold, and the accuracy decrease of the model on the general capability benchmark set does not exceed the preset retention threshold; if the judgment condition is not met, the localization-repair process is repeated from step S3.

[0019] According to a second aspect of the present invention, a large language model error localization system based on internal activation causal intervention is provided, comprising: The activation acquisition and alignment module is used to construct erroneous runs and counterfactual reference runs, and to acquire the activation tensors of the Transformer layer in the two types of runs respectively; determine the first divergence position between the erroneous output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the set of valid positions and step missing markers, and calculate the activation difference tensor within the pre-divergence intervention window. The pre-screening module is used to perform two-stage screening based on the activation differences of the pre-divergence window, and outputs a set of candidate layers and a set of candidate units. The causal intervention calculation module is used to perform shielding intervention, replacement intervention and enhancement intervention on each candidate unit in the candidate unit set within the pre-divide intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement recovery causal effect and the editing security causal effect, respectively, forming a three-dimensional causal effect vector; The propagation path verification module is used to restore the causal effect based on the reference substitution in the three-dimensional causal effect vector, determine the error-specific layer through a controlled error control experiment, select adjacent candidate layer pairs from the candidate layer set, perform joint intervention and candidate unit-level net effect blocking experiments on them, verify the direction of error propagation between layers, and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking. The error classification module is used to automatically identify error types by matching preset multi-type error causal signatures based on the masking improvement causal effect and reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the step missing marker, the error initiation layer position and the highest contributing unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation. The repair execution and verification module is used to select the corresponding repair strategy to perform repair based on the error type and the repair level range determined by the directed error propagation path, combined with the judgment result of the edit security causal effect in the three-dimensional causal effect vector; after the repair is completed, the degree of causal effect elimination is verified based on the reference replacement recovery causal effect, and the repair effect is judged based on the accuracy decrease of the model on the preset general capability benchmark set. If the judgment condition is not met, the causal intervention calculation module is triggered to start the next round of localization-repair cycle.

[0020] The technical solutions provided by the embodiments of the present invention may include the following beneficial effects: 1. The joint determination of dual causal effects can eliminate the false error source of "replacement is effective but shielding is not effective", and the positioning accuracy is improved by about 30 percentage points compared with single-dimensional replacement intervention.

[0021] 2. Two-stage pre-screening can greatly reduce the number of forward propagation times, significantly improve the efficiency of single-sample localization on a single graphics card, and ensure the engineering feasibility of computational efficiency.

[0022] 3. The improved security assessment and strategy switching mechanism significantly increases the success rate of repairs while keeping the decline in the model's general capabilities to a small extent, thus greatly enhancing the security of repairs.

[0023] 4. Candidate unit-level net effect blocking verification improves the accuracy of propagation paths, effectively distinguishes between correlational and causal propagation, and enhances the reliability of propagation paths.

[0024] 5. It covers the entire process of reference construction, positioning, classification, repair and verification, without the need for manual pre-annotation of reference trajectories, realizing a fully automated closed loop, and can be directly applied to large model operation and debugging scenarios.

[0025] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit the invention. Attached Figure Description

[0026] The above and other objects, features and advantages of the present invention will become more apparent from the more detailed description of exemplary embodiments of the invention in conjunction with the accompanying drawings, wherein the same reference numerals generally represent the same components in the exemplary embodiments of the invention.

[0027] Figure 1 This is a schematic diagram of the overall system architecture of the present invention; Figure 2 This is a schematic diagram of the overall process of the method of the present invention; Figure 3 This is a schematic diagram illustrating the principle of token mapping and donor validity verification. Figure 4 This is a schematic diagram illustrating the principle of joint determination of three-dimensional directional causal effects. Figure 5 A schematic diagram illustrating the principle of directed propagation verification at the candidate unit level; Figure 6 A schematic diagram illustrating the principle of multi-condition causal signature classification; Figure 7 A schematic diagram of the closed-loop process for repair and verification. Detailed Implementation

[0028] Preferred embodiments of the invention will now be described in more detail with reference to the accompanying drawings. While preferred embodiments of the invention are shown in the drawings, it should be understood that the invention can be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that the invention will be thorough and complete, and will fully convey the scope of the invention to those skilled in the art.

[0029] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. The singular forms “a,” “the,” and “the” used in this invention and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.

[0030] It should be understood that although the terms "first," "second," "third," etc., may be used in this invention to describe various information, this information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this invention, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Thus, features defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.

[0031] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments.

[0032] To facilitate understanding, the core symbols of this invention will first be explained in a unified Chinese language: 1. Error running ( The model performs an autoregressive generation process under the original input x, producing a complete inference process that generates erroneous outputs, while simultaneously collecting activation tensors from each layer.

[0033] 2. Counterfactual Reference Operation ( The model performs forward propagation with counterfactual inputs containing correct evidence, and its activations are used only as donors and do not participate in the evaluation benchmark calculation.

[0034] 3. Divergence position q: The position of the first token where semantic divergence occurs between the erroneous output and the reference output. All causal effects are evaluated at this position.

[0035] 4. Pre-disagreement intervention window ( ): A set of token positions strictly preceding the divergence position q, used to perform activation interventions, ensuring that the intervention only applies to the causal upstream before the error occurs.

[0036] 5. Shielding and improving causal effects ( ): After blocking candidate units, the change in the probability of generating the correct target token relative to the uninterrupted baseline. The larger the positive value, the stronger the necessity of the unit for errors.

[0037] 6. Reference substitution to restore causal effect ( ): The change in the probability of generating the correct target token relative to the uninterrupted baseline after replacing the candidate unit with the reference donor. The larger the positive value, the stronger the sufficiency of the information carried by the unit for the correct output.

[0038] 7. Editing safety causal effects ( ): After amplifying the activation amplitude of the candidate unit, the change in the probability of generating the correct target token relative to the uninterrupted baseline. The larger the negative value, the higher the risk of directly editing the unit.

[0039] 8. Positive mediation strength ( : The attenuation of the source layer replacement effect after blocking the target layer candidate unit, used to characterize the causal strength of the forward propagation from the source layer to the target layer.

[0040] 9. Reverse mediation strength ( ): The attenuation of the target layer replacement effect after blocking the candidate unit of the source layer, used to verify the uniqueness of the propagation direction in reverse.

[0041] like Figure 1 The diagram shows the overall system architecture of this invention. Based on this architecture, as... Figure 2 As shown in the figure, an embodiment of the present invention provides a method for error localization of a large language model based on internal activation causal intervention, which includes the following steps: S1. Construct error runs and counterfactual reference runs, and collect the activation tensors of the Transformer layer in the two types of runs respectively; determine the first divergence position between the error output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the effective position set and the step missing marker, and calculate the activation difference tensor within the pre-divergence intervention window.

[0042] Optionally, in step S1, such as Figure 3 As shown, the construction of the counterfactual reference operation specifically includes: A hierarchical strategy is adopted to automatically generate reference candidates, with priority from high to low as external knowledge base verification, self-consistent majority vote sampling, and perplexity-assisted filtering. The confidence level of the reference candidate labels is determined, and the positioning process is terminated if the confidence level is insufficient. Inject verified and correct evidence fragments into the original question input to construct a counterfactual reference input, enabling the model to generate the correct token at the divergence position under the counterfactual reference input; Record the token alignment mapping relationship between the original input and the counterfactual reference input. When replacing the intervention, only the activation value is replaced, without changing the token and position code of the original input.

[0043] Specifically, to avoid the drawback of ordinary reference inputs leading to complete consistency before divergence and the inability to form a valid donor, this invention constructs a counterfactual reference input, the specific implementation of which is as follows: A three-tiered hierarchical strategy is used to automatically generate and validate reference candidates, with priority from highest to lowest as follows: 1. Priority 1: External verification: If the system connects to external modules such as knowledge bases and numerical calculation verifiers, fact-checking or logical verification is performed on the candidate answers output by the model. The answers that pass the verification are used as reference candidates, and the confidence level is marked as "high". 2. Priority 2: Self-consistent majority vote: Perform 30 temperature samplings on the same input (sampling temperature coefficient is 0.7), and select the output with an occurrence frequency ≥ 60% as the reference candidate; it is only used when it is consistent with the result of priority 1, or when priority 1 is unavailable, and the confidence level is marked as "medium"; 3. Priority 3: Perplexity-assisted screening: When there are no candidates that meet the majority vote condition, calculate the average token probability of each candidate's output and select the candidate with the highest model confidence. This strategy needs to be used in conjunction with other supporting evidence; otherwise, the confidence level is marked as "low".

[0044] If none of the three strategies can generate reference candidates with medium or higher confidence, the system outputs a "Reference Sample Unreliable" warning, terminates the localization process, and recommends manual review. All output results clearly indicate the confidence level and source of the reference candidates.

[0045] Before the answer generation position of the original question input, insert verified correct evidence fragments (such as correct facts, key intermediate computations, corrective hints) to form counterfactual reference input, so that the model can generate the correct token at the divergence position under this input.

[0046] Record the token-level alignment mapping between the original input and the counterfactual reference input. For the question portion, a one-to-one semantic mapping is established. Positional offsets caused by inserted evidence fragments are shifted according to the insertion length, ensuring a one-to-one mapping between semantically corresponding positions. Subsequent replacement interventions only replace activation values, without altering the input token and positional encoding of the erroneous operation. This ensures that probability changes originate solely from the replacement of unit activations, eliminating unwanted interference from overall input changes.

[0047] In this embodiment, a three-level hierarchical strategy is used to automatically generate reliable reference candidates, eliminating the need for manual pre-preparation of complete and correct inference trajectories and reducing the cost of manual annotation. The counterfactual input construction method of injecting evidence before divergence ensures that there are real differences in donor activations and that they conform to causal logic, avoiding the problem of invalid activation values ​​due to common prefixes in traditional reference inputs. During replacement, only the activation value is replaced, without changing the input and position encoding, completely eliminating the mixed interference caused by overall input changes and ensuring the purity and reliability of causal effect calculation.

[0048] Optionally, in step S1, the internal activation tensor during the forward propagation of the model is captured in real time using PyTorch's register_forward_hook mechanism without modifying the model weights. Activation tensor data for both types of runs are collected separately. The specific process is as follows: 1. Error Collection: Input the original question, the model generates incorrect answers through autoregression, and simultaneously collects three types of complete activation tensors: residual flow tensors of each layer (dimensions are [number of layers, sequence length, model dimension]), attention head output tensors (dimensions are [number of layers, number of attention heads, sequence length, single head dimension]), and intermediate activation tensors of the MLP after SwiGLU activation (dimensions are [number of layers, sequence length, feedforward network dimension]). 2. Counterfactual Reference Run Acquisition: Input the constructed counterfactual reference, perform one forward propagation, and acquire a reference activation tensor of the same structure; this tensor is only used as a replacement donor and does not participate in the evaluation score calculation.

[0049] Optionally, in step S1, performing step-level and token-level monotonic alignment on the activation tensors of the two types of operations to obtain a set of valid positions and a missing step marker specifically includes: Perform position alignment directly on the input prefix; For the answer output part, first split the reasoning steps according to the boundary to obtain the step sequence, and then perform monotonic dynamic programming to align the steps through sentence vector similarity; Within the matched steps, perform token-level monotonic alignment with gap penalty to obtain a set of valid positions for bidirectional matching; steps that cannot be matched are marked as missing steps.

[0050] Specifically, to address the issue of unequal lengths between the error output and the reference output, preventing direct bit-by-bit comparison for activation, a two-level monotonic dynamic programming alignment is performed. This outputs a set of valid positions and a missing step marker. The specific alignment strategy is as follows: Input prefix alignment: The text portions of the problem shared by both types of runs are directly aligned in position and all are included in the set of valid positions; Step-level alignment: Error outputs and reference outputs are broken down into several reasoning steps based on reasoning step boundaries such as periods, newlines, equal signs, and mathematical operators; semantic vectors for each step are calculated using a pre-trained sentence vector model to construct a cosine similarity matrix; step pairing is achieved using monotonic dynamic programming, with the state transition formula as follows: in The semantic similarity between the erroneous output at step i and the reference output at step j is calculated. Steps that cannot be paired are marked as missing steps, excluded from the valid position set, and not included in subsequent activation difference calculations. Token-level alignment: For each pair of matched inference steps, perform a two-dimensional monotonic dynamic programming with gap penalty (gap penalty value is 0.5) to obtain the token-level mapping relationship; only bidirectionally matched token pairs are included in the valid position set, and redundant tokens unique to the erroneous output and missing tokens unique to the reference output are not included.

[0051] Within the set of valid locations, the activation difference tensor is calculated as follows: the activation value of the erroneous run minus the activation value of the corresponding mapped location of the reference run; misaligned locations are not included in the difference calculation to avoid introducing circular evidence.

[0052] In this embodiment, a two-level monotonic dynamic programming alignment at the step level and the token level is used to solve the engineering problem of unequal lengths between erroneous outputs and reference outputs, making it impossible to directly compare activations bit by bit. A step missing marker is introduced, and zero-padding is not performed on inference steps that cannot be paired, avoiding calculation deviations caused by false activation differences. This ensures the accuracy of subsequent activation difference calculations and causal interventions from the source and eliminates the generation of circular evidence.

[0053] Optionally, in this embodiment, step S1 further includes a step of pre-verifying the donor's validity, specifically including: For each layer, candidate cell, and location, calculate the normalized donor difference for activation at the corresponding location between the erroneous run and the reference run; When the normalized donor difference is greater than the preset donor difference threshold, the reference activation at that location is used as a valid donor in the replacement intervention calculation.

[0054] Specifically, for each combination of [layer-candidate cell-location], the normalized donor difference is calculated to eliminate invalid donors with no difference in advance. The formula is as follows: in: The normalized donor difference is defined for the l-th layer, the u-th unit, and the v-th position. This represents the activation value of the cell at the corresponding location during the erroneous operation. To reference the cell activation values ​​at the mapped locations during operation, For token alignment mapping functions; The root mean square of all aligned positions in layer l is activated to eliminate numerical scale differences between layers. To prevent extremely small positive numbers from being divided by zero, the default value is 1e-8.

[0055] The reference at a given location is considered a valid donor only if the normalized donor difference is greater than the preset donor difference threshold (default 0.05). If a candidate unit has no valid donor within the entire pre-divergence intervention window, its replacement effect is recorded as invalid and will not proceed to the subsequent joint identification process.

[0056] In this embodiment, by performing pre-verification of normalized donor differences, invalid donor positions with no differences are eliminated in advance, reducing the amount of unnecessary calculations required for subsequent replacement interventions. At the same time, it avoids misjudgment of causal effects caused by indistinguishable replacements from the source, ensuring that replacement interventions can truly reflect the differences in information carried by the units and improve the accuracy of error source identification.

[0057] S2, Based on the aforementioned pre-divergence intervention window (V) pre The activation differential tensor within (q) performs a two-stage screening (Delta) pre ), outputting the candidate layer set and candidate unit set.

[0058] Specifically, this step narrows down the candidate pool based on activation differences without performing causal intervention, compressing the computational load to an engineering-feasible level. All screening calculations are strictly limited to the pre-divergence intervention window, using only activation data before the divergence position and excluding activations after the divergence position to prevent causal confounding caused by leakage of posterior information.

[0059] Optionally, step S2 specifically includes: Calculate the Frobenius norm of the activation difference tensor within the window before the divergence of each layer, and take the first r layers with the largest norm as the candidate layer set; Within the candidate layer set, the Frobenius norm of the activation difference of each attention head and the mean absolute value of the activation difference of each MLP neuron are calculated respectively. A preset number of attention heads and MLP neurons are selected in descending order to form a candidate unit set.

[0060] Specifically, in the hierarchical screening stage, the Frobenius norm of the activation difference tensor of each layer within the pre-divergence window is calculated, and the layers are sorted in descending order of norm value. The top r layers are taken as the candidate layer set, with r=8 by default.

[0061] In the unit-level screening stage, specifically within the selected candidate layer set, attention heads and MLP neurons are sorted and screened separately. For each attention head, the Frobenius norm of its pre-divergence activation difference is calculated, and the top-ranked neurons are selected in descending order. One, default For each MLP neuron, calculate the mean absolute value of its pre-divergence activation differences, and take the first few in descending order. One, default .

[0062] Through the above two-stage screening, the total number of candidate units was reduced from tens of thousands to 4,160, and the number of forward propagations required was reduced from approximately 5.3 million to approximately 62,000. On a single A100 graphics card, single-sample localization can be completed within 1 hour, achieving a leap in engineering feasibility.

[0063] In this embodiment, through a two-stage pre-screening process of hierarchy and unit, the total number of candidate units is compressed to several thousand, reducing the number of forward propagations required from 5.3 million to 62,000, thus improving computational efficiency by two orders of magnitude. This makes fine-grained error localization of large models feasible rather than infeasible. At the same time, the screening process is strictly limited to using only pre-divergence window data, completely eliminating the leakage of posterior information, ensuring the causal validity of the screening results, and avoiding circular reasoning.

[0064] S3. For each candidate unit in the candidate unit set, perform shielding intervention, replacement intervention and enhancement intervention respectively within the pre-divergence intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement restoration causal effect and the editing security causal effect respectively, forming a three-dimensional causal effect vector.

[0065] Specifically, step S3 is the core quantification step of this invention, outputting a three-dimensional causal effect vector for each candidate unit. All interventions are performed under a unified benchmark: the evaluation context is fixed as the error prefix before the divergence of the original input, the evaluation position is fixed as the first divergence token, and the generation probability of the correct target token without intervention is used as the benchmark value to ensure horizontal comparability of the three types of effects. Each type of intervention is repeated 5 times with a small input perturbation added and the average value is taken to improve the stability of the results.

[0066] Optionally, in step S3, the masking improvement causal effect is: after setting the activation of the candidate unit in the pre-divergence window to zero, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of improvement of the erroneous output after masking the unit; the reference replacement recovery causal effect is: after replacing the activation of the candidate unit in the pre-divergence window with the activation of the effective donor at the corresponding position of the reference run, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of recovery of the output after replacement with the correct activation; the editing security causal effect is: after amplifying the activation amplitude of the candidate unit in the pre-divergence window, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the risk of output deterioration after enhancing the activity of the unit.

[0067] Specifically, the three types of effects involved in this step are explained as follows: 1. Shielding and improving causal effects ( ) Set all activation values ​​of candidate units within the pre-divide window to zero, re-perform forward propagation to the divergence position, and calculate the generation probability of the correct target token; mask the improvement causal effect as the difference between the post-intervention probability and the baseline probability without intervention: in, This represents the probability of generating the correct token at the divergence position without intervention. This represents the probability after the intervention is masked.

[0068] This effect characterizes the degree of improvement in erroneous output after the unit is disabled. The larger the positive value, the more necessary it is for the unit to maintain erroneous output.

[0069] 2. Reference substitution to restore causal effect ( ) The effective donor positions of candidate units within the pre-divergence window are activated and replaced with the activation values ​​of the corresponding positions in the reference run. The probabilities are then recalculated through forward propagation. The reference replacement restores the causal effect as the difference between the post-intervention probability and the baseline probability. in, This refers to the probability that the model generates the correct target token at the first divergence position after applying a replacement intervention to a candidate unit. This effect characterizes the degree of recovery of the output after replacement with the correct activation; the larger the positive value, the stronger the sufficiency of the unit in carrying correct information.

[0070] 3. Editing safety causal effects ( ) Multiply the activation value of the candidate unit within the pre-divergence window by an amplification factor α (default α=2.0), and recalculate the probability through forward propagation; edit the safety causal effect to the difference between the post-intervention probability and the baseline probability: Among them, parameters The meaning is: the probability value that the model generates the correct target token at the first divergence position after applying enhancement intervention to the candidate unit. This effect is used to characterize the risk of output degradation after enhancing the activity of the unit. The larger the negative value, the easier it is to directly edit the unit and destroy the normal ability of the model.

[0071] The above three types of effects together constitute the three-dimensional causal effect vector of the candidate unit.

[0072] In this embodiment, three dimensions of directional causal effects are defined, corresponding to the necessity, sufficiency, and editing risk of the error unit, forming a complete causal evaluation dimension. The positive and negative directions of the effect are retained, rather than taking the absolute value, which can distinguish the positive and negative contributions of the unit to the output. This provides a comprehensive and quantifiable basis for subsequent error source identification and security risk assessment, filling the information gap that a single-dimensional causal effect cannot fully evaluate the unit attributes.

[0073] Optionally, in this embodiment, step S3 further includes: when the masking improvement causal effect of the candidate unit is greater than a preset masking threshold and the reference replacement recovery causal effect is greater than a preset replacement threshold, the candidate unit is identified as a high-confidence error source; and the error source units are sorted by using the sum of the masking improvement causal effect and the reference replacement recovery causal effect as the priority ranking score.

[0074] Specifically, such as Figure 4 As shown, a candidate cell is identified as a high-confidence error source only if it simultaneously meets both of the following conditions: 1. Masking improves causal effects > Masking threshold (default 0.30) 2. Reference replacement to restore causal effect > replacement threshold (default 0.50) The importance of error source units is ranked by the sum of the effects of shielding and improving causal effects and the effects of restoring causal effects by reference replacement, which is used to determine the priority of subsequent repairs.

[0075] In this embodiment, the "three tests and one decision" rule, which uses both shielding and replacement thresholds for joint identification, can effectively eliminate false error sources where "replacement is effective but shielding does not improve the situation," thus significantly reducing the probability of mislocation. The contribution of error sources is ranked by priority, providing a direct basis for determining the priority of subsequent repairs and improving repair efficiency.

[0076] S4. Based on the reference substitution to restore the causal effect in the three-dimensional causal effect vector, determine the error-specific layer through a controlled error control experiment; select adjacent candidate layer pairs from the candidate layer set, perform joint intervention and candidate unit-level net effect blocking experiments on them, verify the direction of inter-layer error propagation, and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking.

[0077] Specifically, the core of step S4 is to verify the directional propagation relationship of errors between layers, distinguish between correlation and causation, and construct a directional error propagation path.

[0078] Optionally, in step S4, the determination of the error-specific layer is achieved by excluding ordinary fluctuation interference through a "controlled error + clean reference" control experiment. The specific process is as follows: A batch of known correct samples of the same type and task as the current problem are selected, and controlled perturbations (injecting erroneous facts, deleting key inference steps, and applying controlled activation noise) are artificially applied to produce erroneous outputs, thus constructing a control erroneous sample. Using the counterfactual input of the original correct samples as a clean reference donor, the mean causal effect of reference replacement recovery at each layer is calculated under the same candidate rules, serving as the control baseline. The difference in in-layer effects between the current erroneous sample and the control sample is calculated; layers with a difference greater than the error specificity threshold (default 0.08) are identified as layers with high error specificity contributions.

[0079] Specifically, based on the error-specific layer and propagation edges, a complete directed error propagation path is constructed, and two key nodes are defined: 1. Error Initiation Layer: The shallowest layer that first meets the error-specific conditions, representing the origin of the error message; 2. Error solidification layer: The layer where the incremental difference in activation between layers converges to below the threshold, indicating that the error information tends to stabilize after this layer.

[0080] The final output directed error propagation path includes complete directed edges, edge weights, error initiation layer, error fixation layer, and contribution ranking of each layer's units.

[0081] Optionally, in step S4, the candidate unit-level net effect blocking experiment specifically includes: Select source and target layers that satisfy the inter-layer adjacency condition from the candidate layer set. Based on the reference replacement causal effect, calculate the probability of the correct target token when only the source layer candidate unit is replaced, only the target layer candidate unit is replaced, and both candidate units are replaced at the same time. Forward blocking and reverse blocking experiments were performed respectively. Forward blocking involves replacing the source layer candidate unit while blocking the target layer candidate unit, and reverse blocking involves replacing the target layer candidate unit while blocking the source layer candidate unit. Calculate the forward mediation strength and the reverse mediation strength respectively. When the forward mediation strength is greater than a preset mediation threshold and the reverse mediation strength is less than a preset ratio of the forward mediation strength, the directed propagation edge from the source layer to the target layer is considered to be established.

[0082] Specifically, such as Figure 5 As shown, layer pairs satisfying the adjacency condition (source layer src, target layer tgt, and the target layer number is greater than the source layer) are selected from the candidate layer set. A bidirectional blocking experiment is performed to verify the propagation direction. At the same time, an equal number of random non-candidate units are blocked as a negative control to confirm the specificity of the blocking effect. The probability of the correct token is calculated when only the source layer candidate units are replaced, when only the target layer candidate units are replaced, and when both layers of candidate units are replaced.

[0083] The forward blocking experiment involves replacing candidate cells in the source layer while blocking a subset of candidate cells in the target layer, and then calculating the forward mediation strength using the following formula: The meanings of each parameter are as follows: : Positive mediation strength, characterizing the causal strength of forward propagation from the source layer to the target layer. The larger the value, the stronger the causality of the forward propagation. Replace only the source layer When a candidate unit is activated, the probability of generating the target token at the correct divergence position; The baseline probability of generating the target token at the correct divergence position without any intervention; Replace source layer While selecting candidate units, block the target layer. When a candidate unit is activated, the probability of generating the target token at the correct divergence position; Block only the target layer The probability of generating the target token at the correct divergence position when the candidate unit is activated and the source layer is not replaced.

[0084] The reverse blocking experiment involves replacing candidate cells in the target layer while blocking a subset of candidate cells in the source layer, and then calculating the reverse mediation strength using the following formula: The meanings of each parameter are as follows: : Reverse mediation strength, used to verify the uniqueness of the propagation direction. The smaller the value, the weaker the causality of the back propagation and the clearer the propagation direction. Replace only the target layer When a candidate unit is activated, the probability of generating the target token at the correct divergence position; Replace target layer While selecting candidate cells, the source layer is blocked. When a candidate unit is activated, the probability of generating the target token at the correct divergence position; Block only the source layer The probability of generating the target token at the correct divergence position when the candidate unit is activated and the target layer is not replaced.

[0085] A directed propagation edge from the source layer to the target layer is considered valid only when the strength of the forward intermediation is greater than the intermediation threshold (default 0.10) and the strength of the reverse intermediation is less than 50% of the strength of the forward intermediation.

[0086] In this embodiment, a bidirectional blocking experiment with forward and reverse directions at the candidate unit level is adopted instead of blocking the entire layer, thus avoiding the external perturbation caused by setting the entire layer to zero. By quantifying the mediation strength, it is possible to effectively distinguish between correlation and causal propagation, eliminate false propagation edges caused by directional errors and common upstreams, and improve the accuracy of propagation paths from 51.35% to 74.85%, significantly improving the reliability of error propagation paths and providing accurate directional basis for error tracing.

[0087] S5. Based on the shielding improvement causal effect and the reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the missing step marker, the error initiation layer position and the highest contributing unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation, match the preset multi-type error causal signatures to automatically identify the error type.

[0088] Specifically, the two types of unit activation distribution features used in this step are calculated based on the activation tensor collected by S1 within the set of valid positions before the divergence: MLP neuron activation distribution characteristics: z-score quantification of relative activation within the same layer was used. The mean activation of the target unit in the reference run was calculated, and then the overall mean and standard deviation of activation of all units in the same layer were calculated and substituted into the formula: The meanings of each parameter are as follows: The relative z-score of the activation of the target MLP neuron during operation is used to measure the degree of deviation of the activation level of the target unit from the overall distribution in the same layer during correct inference. : The mean activation value of the target MLP neuron within the set of effective locations before divergence during the reference operation; : The overall mean activation value of all MLP neurons in the same layer within the same window during the reference operation; : Reference: the overall standard deviation of activation of all MLP neurons in the same layer within the same window during operation.

[0089] This feature measures the degree to which the activation level of a target unit deviates from that of the whole in the same layer during correct reasoning.

[0090] Attention head activation distribution characteristics: The activation cosine similarity metric is used for error-reference runs. Based on the token alignment map, the output activation vectors of the same attention head in the error run and the reference run are extracted, and the cosine similarity is calculated. The meanings of each parameter are as follows: : No. The output activation cosine similarity of an attention head in the erroneous run and the reference run is used to measure the degree of difference in the behavior of the attention head in erroneous and correct reasoning scenarios. Error running, number 1 The output activation vector of each attention head within the set of valid positions before divergence; Reference in operation, number The output activation vector of each attention head at the corresponding mapping position; The dot product of two activation vectors; , : These are the L2 norms of the attention head activation vectors in the erroneous run and the reference run, respectively.

[0091] This feature is used to measure the degree of difference in the behavior of the attention head in incorrect and correct scenarios.

[0092] Optionally, in this embodiment, in step S5, the error types include MLP error knowledge representation type, attention error association type, and inference step missing type. Combined with the directed error propagation path, the causal signatures corresponding to each type of error are as follows: MLP error knowledge representation type: The error initiation layer in the directed error propagation path is located in the shallow layer of the model, the activation of the corresponding unit in the counterfactual reference operation is significantly lower than the average level of the same layer, and the reference replacement recovery causal effect of knowledge injection verification meets the preset recovery conditions. Attention error correlation type: The highest contributing unit in the directed error propagation path is the attention head, the output activation similarity of the attention head in the error operation and the reference operation is lower than a preset threshold, and its shielding improvement causal effect meets the preset improvement conditions; Inference step missing type: The alignment process has missing step markers, the directed error propagation path has multiple consecutive error-specific layers, and the mean of the shielding improvement causal effect of the consecutive layers meets the preset conditions.

[0093] Specifically, in one particular embodiment, such as Figure 6 As shown, the quantifiable causal signatures for the three typical types of errors defined in this invention are as follows, wherein each type of signature must simultaneously meet the corresponding conditions to be recognized.

[0094] 1. MLP error knowledge representation: ① The error initiation layer in the directed error propagation path is located in the first third of the shallow layers of the model; ② The activation z-score of the target unit in the reference run is <-2.0 (significantly lower than the average level of the same layer); ③ The causal effect of the reference replacement recovery in knowledge injection verification is >0.30.

[0095] 2. Attention error correlation type: ① The highest contributing unit in the directed error propagation path is the attention head; ② The output activation cosine similarity of this attention head in the error and reference runs is <0.70; ③ Its masking improvement causal effect is >0.30.

[0096] 3. Inference Step Missing Type: ① There is at least one missing step marker in the step alignment process; ② There are ≥3 consecutive error-specific layers in the directed error propagation path; ③ The mean of the shielding improvement causal effect of the consecutive layers is >0.30.

[0097] For cases with missing reasoning steps, an additional anchor point intervention is used to verify and build a causal tracing bridge: take the last aligned position before the missing step as the anchor point, activate the candidate unit at that position and replace it with the corresponding position of the reference run. If the generation probability of the first token in the next reference step increases by more than 0.20, then the restorative causal relationship is confirmed.

[0098] Error types are determined by priority: "Missing reasoning steps > Attention error association > MLP erroneous knowledge representation". If multiple types are satisfied, the primary and secondary types are recorded; if none of the three types are satisfied, "Unclassified error" is output, and manual review is recommended.

[0099] In this embodiment, multi-condition verifiable causal signatures are defined for three typical types of errors, realizing automatic classification of error causes without manual judgment. Each signature combines propagation path features, activation distribution features, and causal effect features. Multi-dimensional cross-validation avoids misjudgment based on a single indicator. The classification results can be directly mapped to the corresponding repair strategy, thus connecting the link from localization to repair.

[0100] S6. Based on the error type and the repair level range determined by the directed error propagation path, and combined with the judgment result of the editing security causal effect in the three-dimensional causal effect vector, select the corresponding repair strategy to perform repair; after the repair is completed, verify the degree of causal effect elimination based on the reference replacement recovery causal effect, and judge the repair effect by combining the accuracy reduction of the model on the preset general capability benchmark set. If the judgment condition is not met, start the next round of positioning-repair cycle from step S3.

[0101] Step S6 involves matching differentiated remediation strategies based on the error type and security assessment, verifying the remediation effect, and forming a closed loop. The specific process is as follows: Figure 7 As shown.

[0102] Optionally, in step S6, the selection rule for the repair strategy is: For the error knowledge representation of MLP, the target repair layer is determined by combining the directed error propagation path. When the causal effect of editing security is greater than or equal to the preset security threshold, regularized rank-one weight update is performed on the target layer; when the causal effect of editing security is less than the preset security threshold, LoRA fine-tuning is switched. For attention error-related issues, output the position index of the target attention head in the directed error propagation path, and perform attention mask scaling or head pruning. For cases with missing inference steps, the fine-tuning level range is determined based on the directed error propagation path. The missing inference steps are reconstructed by combining the reference run activation. After generating training samples, LoRA fine-tuning is performed on the specified layer.

[0103] Specifically, in one embodiment, as shown in the figure, the closed-loop MLP error knowledge representation repair is performed by combining the directed error propagation path to determine the target repair layer. When the causal effect of editing security is greater than or equal to the security threshold (default -0.03), a regularized rank-one weight update is performed: based on the activation vector of the target neuron and the target repair value, a low-rank update is performed on the down_proj weight matrix of the MLP layer, modifying only the column parameters corresponding to the target neuron. The update formula is as follows: in To repair the residual vector, The activation vector of the candidate neuron. This is the regularization coefficient (default 0.1).

[0104] When the causal effect of editing safety is less than the safety threshold, abandon direct weight editing and switch to LoRA fine-tuning. The default rank is 8, the learning rate is 1e-4, and the training takes 3 rounds.

[0105] Attention error correlation repair: Output the layer index and head index of the target attention head in the directed error propagation path, reduce the output scaling factor of the head to 0.05 by injecting an attention mask, or generate a head pruning configuration file.

[0106] Repairing missing reasoning steps: Determine the fine-tuning level range based on the directed error propagation path; perform greedy decoding on the activation segment of the corresponding step in the reference run through the language model head to reconstruct the missing step text; construct training samples of the complete reasoning chain, expand the sample set after manual sampling, and perform LoRA fine-tuning on the error initiation layer to the solidification layer.

[0107] In this embodiment, a differentiated repair strategy is adopted for error matching with different causes to achieve targeted and accurate repair. A strategy switching mechanism with an edit safety threshold is introduced. In low-risk scenarios, efficient rank-one weight updates are used, while in high-risk scenarios, LoRA fine-tuning is switched to balance repair efficiency and model safety. This improves the repair success rate while effectively controlling the range of parameter modifications, significantly reducing the damage to the model's non-target capabilities and avoiding catastrophic amnesia.

[0108] Optionally, in this embodiment, in step S6, the determination condition for verifying the repair effect is: the absolute mean of the causal effect of the reference replacement recovery of all candidate units after repair is less than the preset repair success threshold, and the decrease in the accuracy of the model on the general capability benchmark set does not exceed the preset retention threshold; if the determination condition is not met, the localization-repair process is repeated from step S3.

[0109] Specifically, the preset repair success threshold can be set to 0.05. If the absolute mean of the causal effect of the reference replacement recovery of all candidate units after repair is less than the preset repair success threshold, the causal effect can be considered to be basically eliminated. The preset retention threshold can be set to 0.5%. If the decrease in the accuracy of the model on the general capability benchmark set does not exceed the preset retention threshold, the non-target capability can be considered to be well maintained.

[0110] In this embodiment, a dual judgment criterion of eliminating causal effects and maintaining general capabilities is adopted. This ensures the repair effect of the target error while constraining the impact on the overall capability of the model, avoiding the repair from being incomplete. The loop starts from step S3, reuses the previous static data, avoids repeated calculations and improves iteration efficiency. A maximum loop limit of 3 rounds is set to control the computational cost and balance the repair effect with engineering efficiency.

[0111] Corresponding to the above method embodiments, this invention provides a large language model error localization system based on internal activation causal intervention, comprising: The activation acquisition and alignment module is used to construct erroneous runs and counterfactual reference runs, and to acquire the activation tensors of the Transformer layer in the two types of runs respectively; determine the first divergence position between the erroneous output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the set of valid positions and step missing markers, and calculate the activation difference tensor within the pre-divergence intervention window. The pre-screening module is used to perform two-stage screening based on the activation differences of the pre-divergence window, and outputs a set of candidate layers and a set of candidate units. The causal intervention calculation module is used to perform shielding intervention, replacement intervention and enhancement intervention on each candidate unit in the candidate unit set within the pre-divide intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement recovery causal effect and the editing security causal effect, respectively, forming a three-dimensional causal effect vector; The propagation path verification module is used to restore the causal effect based on the reference substitution in the three-dimensional causal effect vector, determine the error-specific layer through a controlled error control experiment, select adjacent candidate layer pairs from the candidate layer set, perform joint intervention and candidate unit-level net effect blocking experiments on them, verify the direction of error propagation between layers, and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking. The error classification module is used to automatically identify error types by matching preset multi-type error causal signatures based on the masking improvement causal effect and reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the step missing marker, the error initiation layer position and the highest contributing unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation. The repair execution and verification module is used to select the corresponding repair strategy to perform repair based on the error type and the repair level range determined by the directed error propagation path, combined with the judgment result of the edit security causal effect in the three-dimensional causal effect vector; after the repair is completed, the degree of causal effect elimination is verified based on the reference replacement recovery causal effect, and the repair effect is judged based on the accuracy decrease of the model on the preset general capability benchmark set. If the judgment condition is not met, the causal intervention calculation module is triggered to start the next round of localization-repair cycle.

[0112] Regarding the system in the above embodiments, the specific ways in which each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated further here.

[0113] An embodiment of the present invention illustrates a control device including a memory and a processor.

[0114] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor.

[0115] Memory can include various types of storage units, such as system memory, read-only memory (ROM), and permanent storage devices. ROM can store static data or instructions required by the processor or other modules of the computer. Permanent storage devices can be read-write storage devices. Permanent storage devices can be non-volatile storage devices that retain stored instructions and data even when the computer is powered off. In some embodiments, permanent storage devices use mass storage devices (e.g., magnetic or optical disks, flash memory) as permanent storage devices. In other embodiments, permanent storage devices can be removable storage devices (e.g., floppy disks, optical drives). System memory can be a read-write storage device or a volatile read-write storage device, such as dynamic random access memory. System memory can store some or all of the instructions and data required by the processor during operation. Furthermore, memory can include any combination of computer-readable storage media, including various types of semiconductor memory chips (DRAM, SRAM, SDRAM, flash memory, programmable read-only memory), and disks and / or optical disks can also be used. In some implementations, the memory may include removable storage devices that are readable and / or writable, such as laser discs (CDs), read-only digital versatile optical discs (e.g., DVD-ROMs, dual-layer DVD-ROMs), read-only Blu-ray discs, ultra-density optical discs, flash memory cards (e.g., SD cards, minSD cards, Micro-SD cards, etc.), magnetic floppy disks, etc. Computer-readable storage media do not contain carrier waves or transient electronic signals transmitted wirelessly or via wired connections.

[0116] The memory stores executable code, which, when processed by the processor, can cause the processor to execute some or all of the methods described above.

[0117] Furthermore, the method according to the present invention can also be implemented as a computer program or computer program product, which includes computer program code instructions for performing some or all of the steps in the above-described method of the present invention.

[0118] Alternatively, the present invention can also be implemented as a non-transitory machine-readable storage medium (or computer-readable storage medium, or machine-readable storage medium) storing executable code (or computer program, or computer instruction code) thereon, which, when executed by a processor of an electronic device (or computing device, server, etc.), causes the processor to perform part or all of the steps of the method described above according to the present invention.

[0119] The present invention has been described in detail above with reference to the accompanying drawings. In the above embodiments, the descriptions of each embodiment have their own emphasis; for parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments. Those skilled in the art should also understand that the actions and modules involved in the specification are not necessarily essential to the present invention. Furthermore, it is understood that the steps in the method of the embodiments of the present invention can be adjusted, combined, and deleted according to actual needs, and the modules in the device of the embodiments of the present invention can be combined, divided, and deleted according to actual needs.

[0120] Those skilled in the art will also understand that the various exemplary logic blocks, modules, circuits, and algorithm steps described in connection with the disclosure herein can be implemented as electronic hardware, computer software, or a combination of both.

[0121] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems and methods according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0122] The various embodiments of the present invention have been described above. These descriptions are exemplary and not exhaustive, nor are they limited to the disclosed embodiments. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described embodiments. The terminology used herein is chosen to best explain the principles, practical application, or improvement of the technology in the market, or to enable others skilled in the art to understand the embodiments disclosed herein.

Claims

1. A method for error localization in large language models based on internal activation causal intervention, characterized in that, Includes the following steps: S1. Construct error runs and counterfactual reference runs, and collect the activation tensors of the Transformer layer in the two types of runs respectively; determine the first divergence position between the error output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the effective position set and step missing marker, and calculate the activation difference tensor within the pre-divergence intervention window. S2. Perform two-stage screening based on the activation difference tensor within the pre-divergence intervention window, and output the candidate layer set and the candidate unit set; S3. For each candidate unit in the candidate unit set, perform shielding intervention, replacement intervention and enhancement intervention respectively within the pre-divide intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement restoration causal effect and the editing security causal effect respectively, forming a three-dimensional causal effect vector. S4. Based on the reference replacement to restore the causal effect in the three-dimensional causal effect vector, the error-specific layer is determined through a controlled error control experiment; adjacent candidate layer pairs are selected from the candidate layer set, and joint intervention and candidate unit-level net effect blocking experiments are performed on them to verify the direction of inter-layer error propagation and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking. S5. Based on the shielding improvement causal effect and reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the missing step marker, the error start layer position and the highest contribution unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation, match the preset multi-type error causal signatures to automatically identify the error type. S6. Based on the error type and the repair level range determined by the directed error propagation path, and combined with the determination result of the editing security causal effect in the three-dimensional causal effect vector, select the corresponding repair strategy to perform the repair. After the repair is completed, the degree of elimination of causal effect is verified based on the reference replacement to restore the causal effect. The repair effect is determined by the decrease in the accuracy of the model on the preset general capability benchmark set. If the determination condition is not met, the next round of positioning-repair cycle is executed from step S3.

2. The error localization method for a large language model based on internal activation causal intervention as described in claim 1, characterized in that, In step S1, the construction of the counterfactual reference operation specifically includes: A hierarchical strategy is adopted to automatically generate reference candidates, with priority from high to low as follows: external knowledge base verification, self-consistent majority vote sampling, and perplexity-assisted filtering. The location process is terminated if the confidence level of the reference candidate labels is insufficient. Inject verified and correct evidence fragments into the original question input to construct a counterfactual reference input, enabling the model to generate the correct token at the divergence position under the counterfactual reference input; Record the token alignment mapping relationship between the original input and the counterfactual reference input. When replacing the intervention, only the activation value is replaced, without changing the token and position code of the original input.

3. The error localization method for a large language model based on internal activation causal intervention as described in claim 2, characterized in that, In step S1, the step-level and token-level monotonic alignment of the activation tensors of the two types of operations is performed to obtain the set of valid positions and the missing step markers, specifically including: Perform position alignment directly on the input prefix; For the answer output, first split the reasoning steps according to the boundary to obtain the step sequence, and then perform monotonic dynamic programming to align the steps by using sentence vector similarity; Within the matched steps, perform token-level monotonic alignment with gap penalty to obtain a set of valid positions for bidirectional matching; steps that cannot be matched are marked as missing steps.

4. The error localization method for a large language model based on internal activation causal intervention as described in claim 1, characterized in that, Step S1 also includes a preliminary verification step of donor validity, specifically including: For each layer, candidate cell, and location, calculate the normalized donor difference between the activations at the corresponding locations of the erroneous run and the reference run; When the normalized donor difference is greater than the preset donor difference threshold, the reference activation at that location is used as a valid donor in the replacement intervention calculation.

5. The error localization method for a large language model based on internal activation causal intervention as described in claim 1, characterized in that, Step S2 specifically includes: Calculate the Frobenius norm of the activation difference tensor within the window before the divergence of each layer, and take the first r layers with the largest norm as the candidate layer set; Within the candidate layer set, the Frobenius norm of the activation difference of each attention head and the mean absolute value of the activation difference of each MLP neuron are calculated respectively. A preset number of attention heads and MLP neurons are selected in descending order to form a candidate unit set.

6. The error localization method for a large language model based on internal activation causal intervention as described in claim 1, characterized in that, In step S3, the masking improvement causal effect is: after setting the activation of the candidate unit to zero in the pre-divide window, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of improvement of the erroneous output after masking the unit. The reference replacement recovery causal effect is: after replacing the activation of the candidate unit in the pre-divergence window with the activation of the valid donor at the corresponding position of the reference run, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the degree of recovery of the output after replacement with the correct activation; the edit security causal effect is: after amplifying the activation amplitude of the candidate unit in the pre-divergence window, the difference between the generation probability of the correct target token and the baseline probability without intervention is used to characterize the risk of deterioration of the output after enhancing the activity of the unit.

7. The error localization method for a large language model based on internal activation causal intervention as described in claim 6, characterized in that, In step S4, the candidate unit-level net effect blocking experiment specifically includes: Select source and target layers that satisfy the inter-layer adjacency condition from the candidate layer set. Based on the reference replacement causal effect, calculate the probability of the correct target token when only the source layer candidate unit is replaced, only the target layer candidate unit is replaced, and both candidate units are replaced at the same time. Forward blocking and reverse blocking experiments were performed respectively. Forward blocking involves replacing the source layer candidate unit while blocking the target layer candidate unit, and reverse blocking involves replacing the target layer candidate unit while blocking the source layer candidate unit. Calculate the forward mediation strength and the reverse mediation strength respectively. When the forward mediation strength is greater than a preset mediation threshold and the reverse mediation strength is less than a preset ratio of the forward mediation strength, the directed propagation edge from the source layer to the target layer is considered to be established.

8. The error localization method for a large language model based on internal activation causal intervention as described in claim 1, characterized in that, In step S5, the error types include MLP-based knowledge representation errors, attention-based error association errors, and inference step missing errors. Combined with the directed error propagation path, the causal signatures corresponding to each type of error are as follows: MLP error knowledge representation type: The error initiation layer in the directed error propagation path is located in the shallow layer of the model, the activation of the corresponding unit in the counterfactual reference operation is significantly lower than the average level of the same layer, and the reference replacement recovery causal effect of knowledge injection verification meets the preset recovery conditions. Attention error correlation type: The highest contributing unit in the directed error propagation path is the attention head, the output activation similarity of the attention head in the error operation and the reference operation is lower than a preset threshold, and its shielding improvement causal effect meets the preset improvement conditions; Inference step missing type: The alignment process has missing step markers, the directed error propagation path has multiple consecutive error-specific layers, and the mean of the shielding improvement causal effect of the consecutive layers meets the preset conditions.

9. The error localization method for a large language model according to claim 8, characterized in that, In step S6, the selection rule for the repair strategy is as follows: For the error knowledge representation of MLP, the target repair layer is determined by combining the directed error propagation path. When the causal effect of editing security is greater than or equal to the preset security threshold, the target layer is updated with regularized rank-one weights. When the causal effect of the edited security is less than the preset security threshold, switch to LoRA fine-tuning; For attention error-related issues, output the position index of the target attention head in the directed error propagation path, and perform attention mask scaling or head pruning. For cases with missing inference steps, the fine-tuning level range is determined based on the directed error propagation path. The missing inference steps are reconstructed by combining the reference run activation. After generating training samples, LoRA fine-tuning is performed on the specified layer.

10. A large language model error localization system based on internal activation causal intervention, characterized in that, include: The activation acquisition and alignment module is used to construct erroneous runs and counterfactual reference runs, and to acquire the activation tensors of the Transformer layer in the two types of runs respectively; Determine the first divergence position between the erroneous output and the reference output, and divide the pre-divergence intervention window; perform step-level and token-level monotonic alignment on the activation tensors of the two types of runs to obtain the set of valid positions and step missing markers, and calculate the activation difference tensor within the pre-divergence intervention window. The pre-screening module is used to perform two-stage screening based on the activation differences of the pre-divergence window, and outputs a set of candidate layers and a set of candidate units. The causal intervention calculation module is used to perform shielding intervention, replacement intervention and enhancement intervention on each candidate unit in the candidate unit set within the pre-divergence intervention window, re-execute forward inference and calculate the generation probability of the correct target token, and obtain the shielding improvement causal effect, the reference replacement recovery causal effect and the editing security causal effect, respectively, forming a three-dimensional causal effect vector; The propagation path verification module is used to restore the causal effect based on the reference substitution in the three-dimensional causal effect vector, determine the error-specific layer through a controlled error control experiment, select adjacent candidate layer pairs from the candidate layer set, perform joint intervention and candidate unit-level net effect blocking experiments on them, verify the direction of error propagation between layers, and construct a directed error propagation path that includes the error initiation layer and the unit contribution ranking. The error classification module is used to automatically identify error types by matching preset multi-type error causal signatures based on the masking improvement causal effect and reference replacement recovery causal effect in the three-dimensional causal effect vector, combined with the step missing marker, the error initiation layer position and the highest contributing unit type in the directed error propagation path, and the unit activation distribution characteristics of error operation and counterfactual reference operation. The repair execution and verification module is used to select the corresponding repair strategy to perform repair based on the error type and the repair level range determined by the directed error propagation path, combined with the judgment result of the edit security causal effect in the three-dimensional causal effect vector. After the repair is completed, the degree of elimination of causal effect is verified based on the reference replacement to restore the causal effect. The repair effect is determined by the decrease in the accuracy of the model on the preset general capability benchmark set. If the determination condition is not met, the causal intervention calculation module is triggered to start the next round of positioning-repair cycle.