A large model code generation bias mitigation method based on reasoning trajectory probe
Patent Information
- Application Number
- CN202611008184.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-08
- Publication Date
- 2026-10-16
AI Technical Summary
该类方法虽然能够在一定程度上减少代码偏见,但通常对所有生成过程施加统一约束,缺少对有偏推理轨迹的识别和选择性修复;同时,简单调整推理长度、采样温度或敏感属性在提示中的位置,往往存在效果不稳定、代码质量下降或对大型推理模型不敏感等问题
[0065] This invention proposes a bias mitigation method for large model code generation based on inference trajectory probes. It performs bias detection and bias mitigation intervention at the inference trajectory level, avoiding reliance solely on post-hoc corrections at the final code level. Simultaneously, it achieves low-cost detection through a lightweight adapter and maintains the original output for unbiased inference, thereby reducing interference with normal code generation capabilities.
Smart Images

Figure CN122924739A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of artificial intelligence-assisted software engineering, large model code generation, code fairness detection, and trustworthy code generation technology, and in particular to a method for mitigating bias in large model code generation based on inference trajectory probes. Background Technology
[0002] With the widespread application of large language models in software development, code generation tools based on large language models have been gradually applied to scenarios such as function implementation, program repair, test generation, code completion, and intelligent programming assistants. Developers can guide large language models to automatically generate target code using natural language requirements, function signatures, existing code snippets, or task descriptions, thereby improving software development efficiency.
[0003] However, the program code generated by large language models is not always fair and reliable. Existing research has shown that the program logic generated by the model may incorporate sensitive attributes such as gender, race, age, income, and insurance status into decision-making rules, leading to differential treatment of different groups in human-centered decision-making scenarios such as recruitment, education, and healthcare allocation. Unlike natural language bias, code bias is directly embedded in the conditional judgments, scoring rules, and control flow of executable programs. Once deployed in real-world systems, this can lead to unfair automated decision-making outcomes.
[0004] In existing technologies, research on code bias mainly focuses on bias detection and assessment at the final code level. For example, code bias assessment benchmarks such as FairCoder are used to evaluate social biases in code generated by large language models. These methods can determine whether the generated code contains differential decision-making logic based on sensitive attributes, but they typically do not analyze the intermediate reasoning trajectory before code generation, thus making it difficult to reveal how biases are formed during the model inference stage and affect the final code.
[0005] In recent years, large language models have gradually developed a reasoning-based code generation paradigm. Standard large language models can generate intermediate reasoning trajectories through chain-of-thought prompts, while large reasoning models typically generate the thought process natively during the reasoning stage before outputting the final code, such as the Qwen3 series models and the DeepSeek-R1 series models. Such reasoning trajectories affect the generation logic of the final code. If the reasoning trajectory contains stereotypes, group preferences, or unfair assumptions based on sensitive attributes, these may be further transformed into conditional judgments or scoring rules in the code; even if the reasoning trajectory superficially contains fairness considerations, the final code generation stage may still reintroduce differential decision-making logic based on sensitive attributes.
[0006] Existing methods for mitigating code generation bias often employ approaches such as fairness prompts at the cue level, few-sample examples, adjustments to generation configurations, or constraints at the final output level. For example, some studies have used cue-based strategies to mitigate code generation bias in large language models. While these methods can reduce code bias to some extent, they typically impose uniform constraints on all generation processes, lacking the identification and selective correction of biased inference trajectories. Furthermore, simply adjusting inference length, sampling temperature, or the position of sensitive attributes in the cue often results in unstable performance, decreased code quality, or insensitivity to large inference models.
[0007] Therefore, current bias mitigation techniques for large model code generation still have the following shortcomings: existing methods mostly constrain at the level of input prompts or final code output, lacking the identification and intervention of bias signals in the intermediate inference trajectory, making it difficult to block the propagation of biased reasoning to the final code; bias mitigation by only rewriting prompts or adjusting generation configurations can easily lead to a decline in code quality and is difficult to stably apply to different models and task scenarios; if bias mitigation is carried out by retraining or modifying the main parameters of the target large language model, it will bring high training and deployment costs. Summary of the Invention
[0008] To address the aforementioned problems in existing technologies, the technical problem this invention aims to solve is: how to perform lightweight bias detection through intermediate inference trajectories, accurately rewrite only biased inference trajectories, and simultaneously guide the generation of corresponding code.
[0009] To solve the above-mentioned technical problems, the present invention adopts the following technical solution:
[0010] A bias mitigation method for code generation in large models based on inference trajectory probes includes the following steps:
[0011] S1: Select several bias-sensitive code generation tasks from the FairCoder open-source code bias assessment benchmark and construct a sample set I of bias-sensitive code generation tasks;
[0012] The input fields for each bias-sensitive code generation task sample in I include a set of sensitive attributes and a set of non-sensitive attributes; the sensitive attributes refer to personal attributes that should not affect the decision-making results, and the non-sensitive attributes refer to attributes that are related to the task objectives.
[0013] S2: Select the i-th bias-sensitive code from I to generate a task sample. ,from Example of obtaining the unbiased function corresponding to the task sample. and code generation request ,and The corresponding set of sensitive attributes is The set of non-sensitive attributes is Then use and structure Corresponding code generation suggestions , The expression is as follows:
[0014]
[0015] in, Indicates will and Prompt the construction operation for splicing;
[0016] The This includes a natural language task description for the corresponding code and a code snippet to be continued, wherein the fields in the code snippet to be continued contain a set of sensitive attributes. Non-sensitive attribute set ;
[0017] S3: Select the target large model M, and... In input M, generate intermediate inference trajectories. and candidate code ;
[0018] Presuppose a bias judgment criterion, and define it. The corresponding inference trajectory bias label is ,Will , , Input the bias discrimination criterion into the large language model discriminator, and make a judgment. Does it contain biased reasoning features based on sensitive attributes? If so, assign... The biased reasoning trajectory is labeled and denoted as Conversely, it assigns Unbiased inference trajectory label and denoted as ;
[0019] S4: Iterate through I, repeating S2-S3, to obtain the intermediate inference trajectory, inference trajectory bias label, and candidate code corresponding to each bias-sensitive code generation task sample. All intermediate inference trajectories and inference trajectory bias labels constitute the inference bias sample set. The expression is as follows:
[0020]
[0021] in, This represents the total number of bias-sensitive code generation task samples in I;
[0022] S5: Set fixed bias indicator probe sentences The Depend on Composed of 10 tokens, the expression is as follows:
[0023]
[0024] in, express The first in Each word element, Indicates the total number of lexical units contained in the probe sentence;
[0025] Will spliced at Then the probe detection input is obtained. The expression is as follows:
[0026]
[0027] in, This indicates the analysis prompts / operations;
[0028] S6: Building a Probe-Based Bias Detector Using a Lightweight Adapter At the same time, construct a training method The loss function used ,Will As The input, using right Training is performed, and updates are made through backpropagation using gradient descent. Parameters, when training reaches the maximum number of iterations or Training stops when convergence is achieved, at which point the trained probe-based bias detector is obtained. ;
[0029] S7: Get input prompts for the code generation task to be processed ,Will Input target large language model generation Corresponding intermediate reasoning trajectory and initial code output ;
[0030] Will spliced at Then the probe detection input is obtained. Then enter From probe loss Let the threshold for determining inference bias be... ,like If the reasoning trajectory is biased, then it is determined that the reasoning trajectory is biased and denoted as . If the inference trajectory is not biased, proceed to the next step; otherwise, determine that the inference trajectory is unbiased and record it as such. At the same time As the final code, execute S9;
[0031] S8: Will Input the inference rewriting model and obtain the rewritten inference trajectory. ,Will and The combined code is input into the target large language model to generate the final code. And proceed to the next step. The generation process is represented as follows:
[0032]
[0033] in, This represents the code generation distribution of the target large language model under the conditions of original prompts and rewritten inference trajectories;
[0034] S9: Output the final code or .
[0035] Preferably, the target large model in S3 includes a standard large language model and a large reasoning model with native reasoning capabilities.
[0036] Preferably, intermediate inference trajectories are generated in S3. and candidate code The steps are as follows:
[0037] If M is a standard large language model, then in The appended chain-like thinking trigger prompts lead to the inference-based code generation hints. The expression is as follows:
[0038]
[0039] in, Indicates trigger phrases for chain thinking. Indicates a splicing operation;
[0040] Then As input to M, generate the corresponding and ;
[0041] If M is a large-scale reasoning model with native reasoning capabilities, then the corresponding reasoning generation mechanism is directly generated using M's default reasoning generation mechanism. and .
[0042] Preferably, in step S6, the constructed loss function as follows:
[0043] S6-1: Constructing Probe Loss The expression is as follows:
[0044]
[0045] in, Indicates the parameters of the lightweight adapter. This represents the sequence of lexical units preceding the t-th lexical unit in the probe sentence, i.e. , This indicates the generation of the first [word / phrase] under the given reasoning trajectory and the preceding lexical units of the probe sentence. probe terms The probability of;
[0046] S6-2: Will Transformed into a bias score via monotonic mapping The expression is as follows:
[0047]
[0048] in, Hyperparameters that control the steepness of the mapping curve. Represented by natural constant An exponential function with base 0;
[0049] S6-3: Based on and Calculate probe supervision loss The expression is as follows:
[0050]
[0051] Where N represents the number of training samples. express Reasoning trajectory bias label, This indicates a biased reasoning trajectory. Represents the trajectory of unbiased inference;
[0052] S6-4: In Add auxiliary self-supervised loss To obtain the final training objective The expression is as follows:
[0053]
[0054] in, This represents the weighting coefficients for balancing the probe supervision loss and the auxiliary self-supervision loss.
[0055] Preferably, the inference bias determination threshold in S7 is: The calculations are as follows:
[0056] Several code tasks are selected from FairCoder as a validation set, and the code task samples in the validation set do not overlap with the code task samples in I. Each code task sample in the validation set carries a biased or unbiased label. This biased or unbiased label is obtained by first generating intermediate inference trajectories corresponding to each code task sample in the validation set using the target large language model, and then obtaining the bias label of each intermediate inference trajectory using the method described in S3. The probe loss value is applied to the intermediate inference trajectory corresponding to each code task sample in the validation set, and finally calculated. ;
[0057] Define the candidate threshold set as The Includes There are 1 candidate thresholds, and the maximum and minimum values among all probe loss values are used as... The range of values for ;
[0058] from Choose one candidate threshold ,like Then predict the first The reasoning trajectory is a biased reasoning trajectory, meaning the label is biased; if Then predict the first The reasoning trajectory is an unbiased reasoning trajectory, meaning the label is unbiased;
[0059] Traverse the intermediate inference trajectories corresponding to all code task samples in the validation set and match them with... By comparing, we can obtain Corresponding true rate and false positive rate Then through and calculate Corresponding Youden index The calculation formula is as follows:
[0060]
[0061]
[0062] Wherein, TP represents the number of samples with biased inference trajectory labels and biased prediction labels, FN represents the number of samples with biased inference trajectory labels but unbiased prediction labels, FP represents the number of samples with unbiased inference trajectory labels but biased prediction labels, and TN represents the number of samples with unbiased inference trajectory labels and unbiased prediction labels.
[0063] Traversal All candidate thresholds are selected, and the Youden index corresponding to each candidate threshold is calculated. The candidate threshold with the maximum Youden index is used as the inference bias judgment threshold. .
[0064] Compared with the prior art, the present invention has at least the following advantages:
[0065] This invention proposes a bias mitigation method for large model code generation based on inference trajectory probes. It performs bias detection and bias mitigation intervention at the inference trajectory level, avoiding reliance solely on post-hoc corrections at the final code level. Simultaneously, it achieves low-cost detection through a lightweight adapter and maintains the original output for unbiased inference, thereby reducing interference with normal code generation capabilities.
[0066] 1. This invention addresses the problems of existing large model code generation methods, such as the difficulty in timely detection of social bias, the lack of interpretability in final code-level bias mitigation, and the tendency of suggestive bias mitigation to sacrifice code quality. It proposes a large model code generation bias mitigation method based on inference trajectory probes. This method utilizes inference trajectories as an intermediate step in bias formation, identifies biased inference through probe detection, and rewrites and performs guided code generation on the biased inference trajectory before outputting the final bias mitigation result, thereby reducing social bias in the generated code from the source.
[0067] 2. Compared with methods that directly detect the final code, this invention can identify bias signals in the inference stage in advance and intervene before the bias propagates to the code logic; compared with bias mitigation methods that only use prompts and reminders, this invention can selectively process inference trajectories that are detected as biased, reducing interference with unbiased inference and normal code generation processes; compared with the method of full-scale model fine-tuning, this invention uses a lightweight adapter to train the probe-type bias detector, keeping the main parameters of the target model frozen, resulting in lower deployment costs. Attached Figure Description
[0068] Figure 1 This is a schematic diagram illustrating the framework of the method of the present invention. Detailed Implementation
[0069] The present invention will now be described in further detail.
[0070] The method of this invention divides the code generation bias mitigation process into the inference trajectory sample construction stage, the probe-type bias detector training stage, the inference bias detection stage, and the inference rewriting and guided code generation stage.
[0071] See Figure 1 A bias mitigation method for code generation in large models based on inference trajectory probes includes the following steps:
[0072] S1: Select several bias-sensitive code generation tasks from the FairCoder public code bias evaluation benchmark to construct a bias-sensitive code generation task sample set I; FairCoder provides code generation requests, unbiased function examples, sensitive attribute information and non-sensitive attribute information, and the code generation requests are used to guide the target large language model to generate candidate code;
[0073] The input fields for each bias-sensitive code generation task sample in I include a set of sensitive attributes and a set of non-sensitive attributes; the sensitive attributes refer to personal attributes that should not affect the decision-making results, and the non-sensitive attributes refer to attributes that are related to the task objectives.
[0074] Sensitive attributes may include gender, race, age, parents' education level, parents' income, income level, insurance status, etc., while non-sensitive attributes may include GPA, skills, work experience, severity of illness, symptoms, resource availability, etc. Bias-sensitive code generation tasks are tasks that require generating program code for scoring, sorting, filtering, or resource allocation for individuals or groups. Task scenarios may include recruitment screening, university admissions, medical treatment priority allocation, etc.
[0075] S2: Select the i-th bias-sensitive code from I to generate a task sample. ,from Example of obtaining the unbiased function corresponding to the task sample. and code generation request ,and The corresponding set of sensitive attributes is The set of non-sensitive attributes is Then use and structure Corresponding code generation suggestions , The expression is as follows:
[0076]
[0077] in, Indicates will and The process involves constructing a concatenated suggestion; this includes setting a preset suggestion template, which includes an example area and a task request area: the example area is used to place examples of unbiased functions. This provides a reference implementation for making differentiated decisions without based on sensitive attributes; the task request area is used to place code generation requests. It includes a natural language task description, function signature, input field description, and code snippets to be continued;
[0078] The This includes a natural language task description for the corresponding code and a code snippet to be continued. The code snippet to be continued refers to a code template where the function declaration, input object fields, attribute list, initialization statements, or part of the function body have been given in the code generation request, but the complete decision logic has not yet been provided. The target large language model needs to generate the remaining conditional statements, score update statements, sorting rules, filtering rules, or resource allocation rules at the to-be-continued position in this code context, thereby forming a complete candidate code. The fields in the code snippet to be continued contain a set of sensitive attributes. Non-sensitive attribute set ; This represents an example function for making differentiated decisions without relying on sensitive attributes, serving as a reference for unbiased decision-making logic; the result obtained here... This is used as subsequent input to the target large language model, enabling the target large language model to generate target decision code based on task description, code snippets to be continued, and input attributes;
[0079] S3: Select the target large model M, and... In input M, generate intermediate inference trajectories. and candidate code ; wherein, the intermediate inference trajectory This refers to the task understanding, attribute analysis, decision-making basis, and code generation approach formed by the target large language model before generating candidate code; the candidate code This represents the code implementation of the target large language model based on code generation prompts and intermediate inference trajectories;
[0080] Presuppose a bias judgment criterion, and define it. The corresponding inference trajectory bias label is ,Will , , Input the bias discrimination criterion into the large language model discriminator, and make a judgment. Does it contain biased reasoning features based on sensitive attributes? If so, assign... The biased reasoning trajectory is labeled and denoted as Conversely, it grants Unbiased inference trajectory label and denoted as The specific content of the judgment made by the large language model is as follows: when the intermediate reasoning trajectory... The following situations indicate that the code contains biased reasoning features based on sensitive attributes: First, it explicitly adjusts the scoring, ranking, screening, or resource allocation results based on sensitive attributes such as gender, race, age, income level, insurance status, parents' education level, and parents' income; Second, it implicitly expresses group preferences that a certain sensitive attribute group is more or less suitable for obtaining the target decision result; Third, it introduces stereotypes, unfair assumptions, or differentiated decision-making criteria based on sensitive attributes; Fourth, although the final code is not directly given, its reasoning content guides the subsequent code generation stage to form conditional judgments, scoring updates, or resource allocation logic based on sensitive attributes; when the intermediate reasoning trajectory... When sensitive attributes are not used as the basis for differentiated decision-making, but only non-sensitive attributes related to the task objective are used for analysis, it is determined to be an unbiased inference trajectory.
[0081] For each Determine whether it contains unfair reasoning based on sensitive attributes, stereotypes, group preferences, or implicit differential treatment logic, and assign it a binary bias label. ;when When a scoring, ranking, filtering, or resource allocation result is adjusted based on sensitive attributes, whether explicitly or implicitly, it is marked as a biased inference trajectory; when an inference trajectory does not use sensitive attributes as the basis for differentiated decision-making and is analyzed only based on task-related non-sensitive attributes, it is marked as an unbiased inference trajectory.
[0082] In one optional implementation, the reasoning trajectory bias label is obtained using a large language model discrimination method: specifically, the intermediate reasoning trajectory, the set of sensitive attributes, the set of non-sensitive attributes, and the preset discrimination criteria are input into the large language model discriminator, which outputs a binary label indicating whether the reasoning trajectory contains biased reasoning features based on sensitive attributes; the preset discrimination criteria are provided through discrimination prompts and constrain the discrimination model to judge only based on whether there are differentiated decision-making bases, stereotypes, group preferences, or unfair assumptions based on sensitive attributes in the reasoning trajectory; to improve the reliability of the label, some discrimination results can be manually sampled and reviewed; if the manual review results are inconsistent with the large language model discrimination results, the corresponding label is corrected through manual review or majority voting.
[0083] Candidate code generated by the target large language model Static analysis can be performed to obtain candidate code bias labels: specifically, the code bias evaluation function checks whether sensitive attributes appear in conditional statements that directly change the decision logic, including performing score increase, score decrease, sorting adjustment, filtering, or resource allocation operations based on sensitive attributes; if sensitive attributes only appear in comments, variable declarations, or non-decision contexts that do not affect the decision result, they are not judged as code bias; if the decision scores corresponding to different sensitive attribute value groups are inconsistent, the candidate code... Mark as biased code; if there is no differentiated decision logic based on sensitive attributes, or if the decision scores for different sensitive attribute value groups are consistent, then the candidate code will be... Marked as unbiased code;
[0084] The target large model in S3 includes a standard large language model and a large reasoning model with native reasoning capabilities. The standard large language model may include Llama-3.1-8B-Instruct, Qwen2.5-Coder-7B-Instruct, DeepSeek-Coder-7B-Instruct, CodeLlama-7B-Instruct, etc.; the large reasoning model with native reasoning capabilities may include Qwen3-8B, Qwen3-14B, Qwen3-32B, DeepSeek-R1-Distill-Llama-8B, etc.
[0085] In S3, intermediate inference trajectories are generated. and candidate code The steps are as follows:
[0086] If M is a standard large language model, then in The appended chain-like thinking trigger prompts lead to the inference-based code generation hints. The expression is as follows:
[0087]
[0088] in, Indicates trigger phrases for chain thinking. This indicates a splicing operation; the chain-of-thought trigger is a trigger statement in the Chain-of-Thought (CoT) prompting technology, for example... It can be set to the form "Let's think step-by-step!";
[0089] Then As input to M, generate the corresponding and ;
[0090] If M is a large-scale reasoning model with native reasoning capabilities, then the corresponding reasoning generation mechanism is directly generated using M's default reasoning generation mechanism. and Large-scale inference models, in the process of directly generating inference trajectories and candidate codes, if the model output contains... If a thought block is defined, its content is extracted as an intermediate inference trajectory, and the code content following the thought block is used as candidate code. If the model uses other forms to represent the inference content, the equivalent intermediate reasoning content is extracted as an intermediate inference trajectory, and the code region content in the inference content is extracted as candidate code.
[0091] S4: Iterate through I, repeating S2-S3, to obtain the intermediate inference trajectory, inference trajectory bias label, and candidate code corresponding to each bias-sensitive code generation task sample. All intermediate inference trajectories and inference trajectory bias labels constitute the inference bias sample set. The expression is as follows:
[0092]
[0093] in, This represents the total number of bias-sensitive code generation task samples in I;
[0094] S5: Set fixed bias indicator probe sentences The q is used to measure whether an intermediate reasoning trajectory can easily and naturally lead to a bias judgment semantic such as "this reasoning contains bias based on sensitive attributes." For example, a bias indicator probe sentence could be: "Note that this reasoning contains bias based on sensitive attributes." Depend on Composed of 10 tokens, the expression is as follows:
[0095]
[0096] in, express The first in Each word element, Indicates the total number of lexical units contained in the probe sentence;
[0097] Will spliced at Then the probe detection input is obtained. The expression is as follows:
[0098]
[0099] in, This indicates the analysis prompts / operations;
[0100] S6: Building a Probe-Based Bias Detector Using a Lightweight Adapter At the same time, construct a training method The loss function used ,Will As The input, using right Training is performed, and updates are made through backpropagation using gradient descent. Parameters, when training reaches the maximum number of iterations or Training stops when convergence is achieved, at which point the trained probe-based bias detector is obtained. The lightweight adapter uses LoRA adapters as an existing technology.
[0101] In S6, the constructed loss function as follows:
[0102] S6-1: Constructing Probe Loss The expression is as follows:
[0103]
[0104] in, Indicates the parameters of the lightweight adapter. This represents the sequence of lexical units preceding the t-th lexical unit in the probe sentence, i.e. , This indicates the generation of the first [word / phrase] under the given reasoning trajectory and the preceding lexical units of the probe sentence. probe terms The probability of;
[0105] S6-2: Will Transformed into a bias score via monotonic mapping The expression is as follows:
[0106]
[0107] in, Hyperparameters that control the steepness of the mapping curve. Represented by natural constant An exponential function with base 0; due to Follow The biased reasoning trajectory decreases and increases, therefore biased reasoning trajectories correspond to higher bias scores, while unbiased reasoning trajectories correspond to lower bias scores.
[0108] S6-3: Based on and Calculate probe supervision loss The expression is as follows:
[0109]
[0110] Where N represents the number of training samples. express Reasoning trajectory bias label, This indicates a biased reasoning trajectory. Represents the trajectory of unbiased inference;
[0111] S6-4: In Add auxiliary self-supervised loss To obtain the final training objective The expression is as follows:
[0112]
[0113] in, This represents the weighting coefficients for balancing the probe-monitored loss and the auxiliary self-monitoring loss. The auxiliary self-monitoring loss is a prior art technique. The specific form belongs to the loss commonly used in the training of existing large language models, such as: next word prediction loss / negative log-likelihood loss, etc.
[0114] S7: Get input prompts for the code generation task to be processed ,Will Input target large language model generation Corresponding intermediate reasoning trajectory and initial code output ;
[0115] Will spliced at Then the probe detection input is obtained. Then enter From probe loss Let the threshold for determining reasoning bias be... ,like If the reasoning trajectory is biased, then it is determined that the reasoning trajectory is biased and denoted as . If the inference trajectory is not biased, proceed to the next step; otherwise, determine that the inference trajectory is unbiased and record it as such. At the same time As the final code, execute S9;
[0116] The threshold for determining inference bias in S7 is: The calculations are as follows:
[0117] Several code tasks are selected from FairCoder as a validation set. The code task samples in the validation set do not overlap with the code task samples in I. Each code task sample in the validation set carries a biased or unbiased label. This biased or unbiased label is obtained by first generating intermediate inference trajectories corresponding to each code task sample in the validation set using the target large language model, and then obtaining the bias label for each intermediate inference trajectory using the method described in S3. The probe loss value is applied to the intermediate inference trajectory corresponding to each code task sample in the validation set, and finally calculated. ;
[0118] Define the candidate threshold set as The Includes There are 1 candidate thresholds, and the maximum and minimum values among all probe loss values are used as... The range of values for ;
[0119] from Choose one candidate threshold ,like Then predict the first The reasoning trajectory is a biased reasoning trajectory, meaning the label is biased; if Then predict the first The reasoning trajectory is an unbiased reasoning trajectory, meaning the label is unbiased;
[0120] Traverse the intermediate inference trajectories corresponding to all code task samples in the validation set and match them with... By comparing, we can obtain Corresponding true rate and false positive rate Then through and calculate Corresponding Youden index The calculation formula is as follows:
[0121]
[0122]
[0123] Wherein, TP represents the number of samples with biased inference trajectory labels and biased prediction labels, FN represents the number of samples with biased inference trajectory labels but unbiased prediction labels, FP represents the number of samples with unbiased inference trajectory labels but biased prediction labels, and TN represents the number of samples with unbiased inference trajectory labels and unbiased prediction labels.
[0124] Traversal All candidate thresholds are selected, and the Youden index corresponding to each candidate threshold is calculated. The candidate threshold with the maximum Youden index is used as the inference bias judgment threshold. .
[0125] S8: Will Input the inference rewriting model and obtain the rewritten inference trajectory. ,Will and The combined code is input into the target large language model to generate the final code. And proceed to the next step. The generation process is represented as follows:
[0126]
[0127] in, This represents the code generation distribution of the target large language model under the conditions of original prompts and rewritten inference trajectories; With weight Combine and construct guided prompts to generate hints. Pre-filled into the inference regions of the target large language model (e.g.) (Thinking block) allows the target large language model to continue generating the final code under the condition of rewriting the reasoning trajectory;
[0128] The inference rewriting model is an existing technology and can be Qwen3-Max; other large language models with natural language understanding, inference correction and text rewriting capabilities can also be used.
[0129] The inference rewriting adopts a fair preservation rewriting approach, which is used to correct the intermediate inference trajectory used to guide code generation without directly modifying the final code; the specific rewriting process satisfies the following constraints:
[0130] First, delete or rewrite the reasoning steps that explicitly or implicitly adjust the scoring, sorting, filtering or resource allocation results based on sensitive attributes;
[0131] Second, retain as much of the task-related evidence, constraints and decision structure in the original reasoning trajectory as possible, so that the rewritten reasoning trajectory can still support the completion of the original code generation task.
[0132] Third, compress overly long, repetitive, or redundant reasoning into concise expressions to reduce the accumulation of errors and reasoning costs caused by excessive reasoning.
[0133] Thus, the fairness-preserving inference trajectory is obtained. The result obtained in this way The basis for differentiated decision-making is no longer based on sensitive attributes, while the non-sensitive attribute information and decision-making logic required for code generation tasks are still retained.
[0134] S9: Output the final code or .
[0135] Experimental content and results
[0136] To verify the effectiveness of this invention, experiments were conducted using the FairCoder bias-sensitive code generation benchmark, covering three decision-making scenarios: recruitment screening, university admission, and medical treatment. The results were evaluated on standard large language models and large-scale inference models. The experiments assessed the invention from two aspects: its ability to detect bias in inference trajectories, and its effectiveness in mitigating code bias and maintaining code quality.
[0137] For the inference trajectory bias detection task, Accuracy, Precision, Recall, and F1 score are used as evaluation metrics. Accuracy represents the proportion of correctly classified samples; Precision represents the proportion of actually biased inference trajectories among those judged as biased; Recall represents the proportion of successfully identified actually biased inference trajectories; and F1 score is the harmonic mean of Precision and Recall. The calculation methods are as follows:
[0138]
[0139]
[0140]
[0141] Where TP represents the number of samples correctly identified as biased, FP represents the number of unbiased samples incorrectly identified as biased, and FN represents the number of biased samples that were not identified.
[0142] For the code bias mitigation task, Bias Rate (BR), Preference Entropy (PE), Fairness Score (FS), and Code Quality were used as evaluation metrics. A lower BR indicates a lower proportion of bias in the generated code, while higher PE and FS indicate a more balanced distribution of group preferences and better overall fairness. Code Quality was measured using PyLint scores, with higher scores indicating better code quality. The definitions of each fairness metric are as follows:
[0143]
[0144] in Indicates the number of generated codes. This represents the set of possible values for a sensitive attribute. and This represents the decision scores for different groups.
[0145]
[0146] in, Indicates the first The percentage of each group's score in the total group score.
[0147]
[0148] Among them, FS takes into account both the proportion of bias occurrence and the balance of group preferences.
[0149] Table 1 presents the performance of the probe-based bias detector of this invention on the inference trajectory bias detection task, and compares it with rule-based methods, traditional machine learning methods, and fine-tuning models. The rule-based method, Keywords, identifies biased expressions in the inference trajectory through preset keywords or lexical patterns; traditional machine learning methods include TF-IDF+LR, TF-IDF+SVM, and FastText, which train binary classifiers based on text features or shallow text representations, respectively; fine-tuning models include CodeT5, CodeBERT, and CodeGen, which identify biased inference trajectories through supervised fine-tuning of pre-trained models. The ProbeDebias of this invention calculates the probe loss of the inference trajectory using probe sentences and a lightweight adapter, and determines whether the inference trajectory is biased based on this loss. The bolded values in the table indicate the best results for that column of metrics.
[0150] Table 1 Performance Comparison Results of Inference Trajectory Bias Detectors
[0151]
[0152] As shown in Table 1, the probe-based bias detector of the present invention achieves the highest results in Accuracy, Precision, and F1, with an F1 score of 87.76%, and requires only a lightweight adapter with an average size of 30.94 MB, indicating that it can achieve more accurate inference bias identification with lower parameter overhead.
[0153] Table 2 presents a comparison of the results of this invention with the original generation method and the prompting bias mitigation baseline in terms of code bias mitigation and code quality maintenance. Wherein, *Original* represents the original inference-based code generation result without any bias mitigation strategy; *BMfew-shot* represents a prompting bias mitigation method based on few-sample examples in existing research, which guides the model to generate fairer code by adding debiased examples to the prompts; *BMcot* represents a bias mitigation method based on chain-thinking prompts in existing research, which reduces code bias by adding a fairness thinking process to the prompts to guide the model; and *ProbeDebias* represents the method of this invention, which detects biased inference through inference trajectory probes and only rewrites and performs guided code generation on biased inference trajectories. Bold text in the table indicates the best result for that column of indicators, and underlined text indicates the second-best result for that column of indicators.
[0154] Table 2. Comparison of code bias and code quality using different bias mitigation methods.
[0155]
[0156] As shown in Table 2, this invention reduced the average bias rate from 0.56 to 0.09, a relative reduction of 83.73%; the fairness score increased from 0.37 to 0.85; and the code quality only slightly changed from 0.77 to 0.76. Overall, compared with few-shot and CoT-based suggestive bias mitigation methods, this invention better maintains code quality while reducing the bias rate, demonstrating a superior balance between overall bias mitigation and quality maintenance.
[0157] In summary, the experimental results demonstrate that this invention can effectively identify bias signals in intermediate inference trajectories and significantly reduce the risk of social bias in the final generated code by selectively rewriting biased inference trajectories, while essentially maintaining the quality of the generated code. Therefore, this invention can achieve a unified approach to bias detection, bias mitigation, and code quality maintenance during the inference stage without modifying the main parameters of the target large language model. It is applicable to bias-sensitive code generation scenarios such as recruitment screening, educational admissions, and medical resource allocation, and has high engineering application and promotional value.
[0158] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.
Claims
1. A method for mitigating bias in large model code generation based on inference trajectory probes, characterized in that: Includes the following steps: S1: Select several bias-sensitive code generation tasks from the FairCoder open-source bias assessment benchmark and construct a sample set I of bias-sensitive code generation tasks; The input fields for each bias-sensitive code generation task sample in I include a set of sensitive attributes and a set of non-sensitive attributes; the sensitive attributes refer to personal attributes that should not affect the decision-making results, and the non-sensitive attributes refer to attributes that are related to the task objectives. S2: Select the i-th bias-sensitive code from I to generate a task sample. ,from Example of obtaining the unbiased function corresponding to the task sample. and code generation request ,and The corresponding set of sensitive attributes is The set of non-sensitive attributes is Then use and structure Corresponding code generation suggestions , The expression is as follows: in, Indicates will and Prompt the construction operation for splicing; The This includes a natural language task description for the corresponding code and a code snippet to be continued, wherein the fields in the code snippet to be continued contain a set of sensitive attributes. Non-sensitive attribute set ; S3: Select the target large model M, and... In input M, generate intermediate inference trajectories. and candidate code ; Presuppose a bias judgment criterion, and define it. The corresponding inference trajectory bias label is ,Will , , Input the bias discrimination criterion into the large language model discriminator, and make a judgment. Does it contain biased reasoning features based on sensitive attributes? If so, assign... The biased reasoning trajectory is labeled and denoted as Conversely, it grants Unbiased inference trajectory label and denoted as ; S4: Iterate through I, repeating S2-S3, to obtain the intermediate inference trajectory, inference trajectory bias label, and candidate code corresponding to each bias-sensitive code generation task sample. All intermediate inference trajectories and inference trajectory bias labels constitute the inference bias sample set. The expression is as follows: in, This represents the total number of bias-sensitive code generation task samples in I; S5: Set fixed bias indicator probe sentences The Depend on Composed of 10 tokens, the expression is as follows: in, express The first in Each word element, Indicates the total number of lexical units contained in the probe sentence; Will spliced at Then the probe detection input is obtained. The expression is as follows: in, This indicates the analysis prompts / operations; S6: Building a Probe-Based Bias Detector Using a Lightweight Adapter At the same time, construct a training method The loss function used ,Will As The input, using right Training is performed, and updates are made through backpropagation using gradient descent. Parameters, when training reaches the maximum number of iterations or Training stops when convergence is achieved, at which point the trained probe-based bias detector is obtained. ; S7: Get input prompts for the code generation task to be processed ,Will Input target large language model generation Corresponding intermediate reasoning trajectory and initial code output ; Will spliced at Then the probe detection input is obtained. Then enter From probe loss Let the threshold for determining inference bias be... ,like If the reasoning trajectory is biased, then it is determined that the reasoning trajectory is biased and denoted as . If the inference trajectory is not biased, proceed to the next step; otherwise, determine that the inference trajectory is unbiased and record it as such. At the same time As the final code, execute S9; S8: Will Input the inference rewriting model and obtain the rewritten inference trajectory. ,Will and The combined code is input into the target large language model to generate the final code. And proceed to the next step. The generation process is represented as follows: in, This represents the code generation distribution of the target large language model under the conditions of original prompts and rewritten inference trajectories; S9: Output the final code or .
2. The method for mitigating bias in large model code generation based on inference trajectory probes as described in claim 1, characterized in that: The target large model in S3 includes a standard large language model and a large reasoning model with native reasoning capabilities.
3. The method for mitigating bias in large model code generation based on inference trajectory probes as described in claim 2, characterized in that: In S3, intermediate inference trajectories are generated. and candidate code The steps are as follows: If M is a standard large language model, then in The appended chain-like thinking trigger prompts lead to the inference-based code generation hints. The expression is as follows: in, Indicates a trigger for chain thinking. Indicates a splicing operation; Then As input to M, generate the corresponding and ; If M is a large-scale reasoning model with native reasoning capabilities, then the corresponding reasoning generation mechanism is directly generated using M's default reasoning generation mechanism. and .
4. The method for mitigating bias in large model code generation based on inference trajectory probes as described in claim 3, characterized in that: In S6, the constructed loss function as follows: S6-1: Constructing Probe Loss The expression is as follows: in, Indicates the parameters of the lightweight adapter. This represents the sequence of lexical units preceding the t-th lexical unit in the probe sentence, i.e. , This indicates the generation of the first [word / phrase] under the given reasoning trajectory and the preceding lexical units of the probe sentence. probe terms The probability of; S6-2: Will Transformed into a bias score via monotonic mapping The expression is as follows: in, Hyperparameters that control the steepness of the mapping curve. Represented by natural constant An exponential function with base 0; S6-3: Based on and Calculate probe supervision loss The expression is as follows: Where N represents the number of training samples. express Reasoning trajectory bias label, This indicates a biased reasoning trajectory. Represents the trajectory of unbiased inference; S6-4: In Add auxiliary self-supervised loss To obtain the final training objective The expression is as follows: in, This represents the weighting coefficients for balancing the probe supervision loss and the auxiliary self-supervision loss.
5. The method for mitigating bias in large model code generation based on inference trajectory probes as described in claim 4, characterized in that: The threshold for determining inference bias in S7 is: The calculations are as follows: Several code tasks are selected from FairCoder as a validation set. The code task samples in the validation set do not overlap with the code task samples in I. Each code task sample in the validation set carries a biased or unbiased label. This biased or unbiased label is obtained by first generating intermediate inference trajectories corresponding to each code task sample in the validation set using the target large language model, and then obtaining the bias label for each intermediate inference trajectory using the method described in S3. The probe loss value is applied to the intermediate inference trajectory corresponding to each code task sample in the validation set, and finally calculated. ; Define the candidate threshold set as The Includes There are 1 candidate thresholds, and the maximum and minimum values among all probe loss values are used as... The range of values for ; from Choose one candidate threshold ,like Then predict the first The reasoning trajectory is a biased reasoning trajectory, meaning the label is biased; if Then predict the first The reasoning trajectory is an unbiased reasoning trajectory, meaning the label is unbiased; Traverse the intermediate inference trajectories corresponding to all code task samples in the validation set and match them with... By comparing, we can obtain Corresponding true rate and false positive rate Then through and calculate Corresponding Youden index The calculation formula is as follows: Wherein, TP represents the number of samples with biased inference trajectory labels and biased prediction labels, FN represents the number of samples with biased inference trajectory labels but unbiased prediction labels, FP represents the number of samples with unbiased inference trajectory labels but biased prediction labels, and TN represents the number of samples with unbiased inference trajectory labels and unbiased prediction labels. Traversal All candidate thresholds are selected, and the Youden index corresponding to each candidate threshold is calculated. The candidate threshold with the maximum Youden index is used as the inference bias judgment threshold. .