A large language model dynamic control method and device based on activation space semantics
By using dynamic filtering and weighted aggregation activation layers, combined with an adaptive injection strength approach, the problem of insufficient control stability and adaptability in large language models is solved. This achieves efficient semantic control in different inputs and contexts, avoiding control mismatch caused by fixed layers and strengths.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HARBIN INSTITUTE OF TECHNOLOGY (SHENZHEN) (INSTITUTE OF SCIENCE AND TECHNOLOGY INNOVATION HARBIN INSTITUTE OF TECHNOLOGY SHENZHEN)
- Filing Date
- 2026-03-26
- Publication Date
- 2026-07-31
AI Technical Summary
Existing activation intervention schemes for large language models have shortcomings in terms of control stability and adaptability. Fixed action layers and fixed injection strength lead to poor control effects and make it difficult to maintain consistency and accuracy across different inputs and contexts.
By acquiring target semantic data, constructing a sample set and performing forward inference, extracting layer-by-layer representation data, calculating layer-by-layer semantic shift vectors, screening candidate layers and performing weighted aggregation, and combining adaptive injection intensity for dynamic control, the activation space semantic intervention of the model is realized.
It achieves stable and accurate semantic control under different inputs and contexts, reduces control mismatch problems, improves the consistency and robustness of control performance, and does not require modification of model parameters.
Smart Images

Figure CN122491529A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing technology, and in particular to a dynamic control method and apparatus for a large language model based on activation space semantics. Background Technology
[0002] The mainstream approach to large language models is based on a stacked Transformer structure. It transforms sequence representations layer by layer through multi-layer self-attention and feedforward networks, and incorporates residual connections and normalization mechanisms to improve training stability and expressive power. As model and data scales increase, LLMs demonstrate a strong ability to "directly specify tasks through input prompts" (i.e., tasks can be completed through text interaction without parameter updates), making "prompt word control / contextual example control" one of the most commonly used output control methods.
[0003] However, in real-world deployment scenarios, "controllability" often means more than just "being able to be guided." It emphasizes: clear control direction, adjustable control strength, stability across inputs, repeatability, and the ability to quickly switch control targets, while maintaining the original generation quality and general capabilities as much as possible. Around this goal, academia and industry have gradually formed a layered framework of "training-stage control + inference-stage control" in Controllable Text Generation (CTG). Training-stage solutions include full-scale fine-tuning, efficient parameter fine-tuning (PEFT), preference alignment, and reward modeling; inference-stage solutions include cue word engineering, decoding-time reweighting / constraint, external discriminator guidance, and activation / representation intervention.
[0004] In terms of control during the inference phase, activation engineering / representation steering is an important approach. For example, PPLM (Plug and Play Language Models) introduces differentiable attribute models or classifiers during generation, directly perturbing the hidden state through gradient iteration to achieve attribute control. Activation Addition (ActAdd) and its "contrastive" variants compare the differences in activations of positive / negative cues or positive / negative samples in intermediate layers to form a "steering vector," which is then added to the activations of specific layers during forward inference, thereby changing the output attributes without optimizing parameters. CAA (Contrastive Activation Addition) further explicitly constructs a steering vector using the "mean of the residual flow activation difference" to align behavioral dimensions such as conformity and hallucination tendencies. The GCAV framework (Generation with Concept Activation Vector) constructs positive and negative contrastive cues, extracts activation representations from each layer of the LLM, trains a classifier, and uses the classifier's normal vector as the direction vector of the target concept in the activation space. However, existing steering vector injection schemes typically exhibit two structural deficiencies when facing "cross-input stable control": First, the injection layer (or injection location) is often pre-fixed or selected through offline verification, while the representation levels of different semantic directions within the model are not consistent, and the "most effective layer" for the same semantic direction may also change under different input contexts; Second, the injection intensity is mostly based on fixed hyperparameters or simple grid search results, lacking adaptive adjustment to the sensitivity of the current input, which easily leads to undercontrol or overcontrol. Summary of the Invention
[0005] To address the technical problems of poor stability in prompt word control and the lack of dynamic adaptability in existing activation intervention schemes due to their fixed action layers and injection strengths, this invention provides a dynamic control method and apparatus for a large language model based on activation space semantics. The technical solution is as follows: This invention relates to the field of natural language processing technology, and in particular to a dynamic control method and apparatus for a large language model based on activation space semantics.
[0006] On the one hand, a dynamic control method for a large language model based on activation space semantics is provided. This method is implemented by a dynamic control device for a large language model based on activation space semantics, and includes: S1: Obtain target semantic data and construct a target semantic sample set and a validation set. Input the target semantic sample set into the frozen large language model for forward inference and extract the layer-by-layer representation data of each layer of the model at the target position. The target semantic is a single semantic item or multiple related but different sub-semantic directions divided by the upper semantic dimension. The target semantic sample set includes at least text content, target semantic labels and positive and negative labels. The positive and negative labels are used to represent the correlation between the text content and the target semantic. S2: The layer-by-layer representation data is processed by difference calculation to obtain the layer-by-layer semantic shift vector, and then normalized to obtain the normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. S3: Calculate the separability score of each layer of the model based on the validation set data, filter the candidate layer set of the target semantic direction based on the ranking, and then combine the normalized layer-by-layer semantic turning vector with the weighted aggregation operation to obtain the semantic aggregation vector corresponding to the target semantic direction. S4: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, construct the local low-rank basis space corresponding to the upper semantic dimension according to the semantic aggregation vector corresponding to the target semantic direction, and project the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector. S5: Receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, obtain online scores for each candidate layer, and after sorting or threshold filtering strategies, perform normalization processing to obtain the routing weights corresponding to each target layer. Through injection strength search, obtain adaptive injection strength, which is limited to the strength value that makes the output distribution change closest to the target distribution perturbation threshold in the search. S6: Based on the target layer set, the routing weight, and the adaptive injection strength, parameters are integrated to calculate the injection parameters and the turning vector to be injected. Combined with the target token position of the current generation step, the residual representation after injection is obtained. Subsequent decoding generation is performed to obtain a semantically controlled text result. The injection parameters include the target layer, injection weight, and injection strength. The decoding generation includes an autoregressive text generation process performed by a large language model. The turning vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
[0007] Preferably, in step S1, the target semantic data is acquired and a target semantic sample set and a validation set are constructed. The target semantic sample set is then input into a frozen large language model for forward inference, extracting layer-by-layer representation data of each layer of the model at the target location. The target semantic is a single semantic item, or multiple related but distinct sub-semantic directions divided by a higher-level semantic dimension. The target semantic sample set includes at least text content, target semantic labels, and positive and negative markers. The positive and negative markers are used to characterize the relevance between the text content and the target semantic, including: S11: Obtain target semantic sample data, determine semantic direction and label positive and negative samples, extract a predetermined number of samples to obtain a target semantic sample set, use the remaining independent samples as verification samples to obtain a verification set. The target semantics includes a single semantic item or multiple sub-semantic directions divided by a higher semantic dimension. The target semantic sample set includes text content, target semantic tags and positive and negative labels representing relevance. The positive and negative labels are used to represent the relevance between the text content and the target semantics. The multiple sub-semantic directions should satisfy the condition that they are related to each other but not the same. S12: Input the target semantic sample set into the frozen large language model for forward reasoning operation to obtain intermediate state data of each layer of the model. The intermediate state data includes the hidden representation of each layer of the model at the target location. S13: Extract features from the intermediate state data to obtain layer-by-layer representation data, which includes the hidden representation of each layer of the model at the target position, preferably the position of the last valid token.
[0008] Preferably, in step S2, the layer-by-layer representation data is processed by difference calculation to obtain a layer-by-layer semantic shift vector, and then normalized to obtain a normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer, including: S21: Based on the layer-by-layer representation data, the mean of positive sample hidden representation and the mean of negative sample hidden representation in each target semantic direction are compared and the difference is calculated to obtain the layer-by-layer semantic turning vector for each target semantic direction. The difference calculation includes calculating the difference between the mean of positive sample hidden representation and the mean of negative sample hidden representation in each layer. S22: The normalization process is used to calculate the layer-by-layer semantic turning vector to obtain the normalized layer-by-layer semantic turning vector; S23: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, construct the local low-rank basis space corresponding to the upper semantic dimension according to the semantic aggregation vector corresponding to the target semantic direction, and project the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector.
[0009] Preferably, in step S3, the separability score of each layer of the model is calculated based on the validation set data. Based on the ranking, a candidate layer set for the target semantic direction is selected. Then, combined with the normalized layer-by-layer semantic shift vector, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction, including: S31: Input the validation set data into the trained logistic regression classifier to obtain the separability score of each layer of the model. The separability score is determined based on the preset classification performance index, which includes the validation set accuracy, F1 score, ROC-AUC and linear discriminant confidence. The trained logistic regression classifier is a logistic regression classifier trained using the hidden representations of each layer in the layer-by-layer representation data and their corresponding positive and negative labels. The hidden representations are used as input features and the positive and negative labels are used as supervision signals. S32: Based on the separability scores of each layer, after sorting and filtering from high to low, a candidate layer set for the target semantic direction is obtained, and the candidate layer set includes a preset number of layers with the highest scores. S33: Based on the normalized layer-by-layer semantic turning vector, and according to the candidate layer set and its corresponding separability score, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction.
[0010] Preferably, in step S4, when the target semantic direction belongs to one of multiple sub-semantic directions under the same upper-level semantic dimension, a local low-rank basis space corresponding to the upper-level semantic dimension is constructed based on the semantic aggregation vector corresponding to the target semantic direction, and the semantic aggregation vector is projected onto the local low-rank basis space to obtain the projected semantic vector, including: S41: When the upper semantic dimension contains multiple related sub-semantic directions, a set of semantic aggregation vectors corresponding to the upper semantic dimension is used to perform matrix arrangement and low-rank decomposition to obtain the local low-rank basis space corresponding to the upper semantic dimension. The low-rank decomposition algorithm includes principal component analysis or singular value decomposition. S42: Input the semantic aggregation vector into the local low-rank basis space for projection calculation to obtain the projected semantic vector; S43: When the target semantics does not belong to the higher semantic dimension, or does not satisfy the condition of multiple related sub-semantic directions, skip the local low-rank basis space projection calculation step.
[0011] Preferably, in step S5, the received input prompt is performed using the candidate layer set. Probing injection is conducted on the input prompt to obtain online scores for each candidate layer. After sorting or threshold filtering, normalization is performed to obtain the routing weights corresponding to each target layer. An adaptive injection strength is obtained through injection strength search. The adaptive injection strength is defined as the strength value that makes the output distribution change closest to the target distribution perturbation threshold during the search, including: S51: Receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, and obtain online scores for each candidate layer. The exploratory injection includes obtaining output distribution data before and after injection. The online scores are obtained by quantifying the changes before and after injection. The change quantification calculation includes calculating the magnitude of output logits change or the divergence of output probability distribution. S52: Based on the online scores of each candidate layer, a target layer set is obtained through sorting or threshold filtering strategies, and the online scores of the target layer set are normalized to obtain the routing weights corresponding to each target layer. The target layer set includes one or more layers with higher scores selected from the candidate layer set. The routing weights are used for subsequent weighted injection of each target layer. S53: Based on the preset target distribution perturbation threshold and the preset intensity range, an adaptive injection intensity is obtained through injection intensity search. The adaptive injection intensity is limited to the intensity value that makes the output distribution change closest to the target distribution perturbation threshold in the search.
[0012] Preferably, in step S6, parameters are integrated based on the target layer set, the routing weights, and the adaptive injection strength to calculate injection parameters and the steering vector to be injected. Combined with the target token position of the current generation step, the injected residual representation is obtained, and subsequent decoding generation is performed to obtain a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector, including: S61: Based on the target layer set, the routing weight, and the adaptive injection strength, parameters are integrated to obtain injection parameters corresponding to the input prompt, and a turning vector to be injected is obtained. The injection parameters include target layer, injection weight, and injection strength. The turning vector to be injected includes at least one of semantic aggregation vector or projected semantic vector. S62: The steering vector to be injected is input into the residual stream of the target layer in the current step of text generation, according to the injection parameters and the target token position of the current generation step, to obtain the injected residual representation. The feature of inputting the steering vector into the residual stream of the target layer is that weighted injection is performed on the steering vector at the target token position. S63: Using the injected residual representation, perform subsequent decoding and generation to obtain semantically controlled text results. The decoding and generation includes an autoregressive or non-autoregressive text generation process performed by a large language model.
[0013] On the other hand, a dynamic control device for a large language model based on activation space semantics is provided. This device is applied to a dynamic control method for a large language model based on activation space semantics. The device includes: Semantic Sample Module: Used to acquire target semantic data and construct target semantic sample set and validation set. The target semantic sample set is input into the frozen large language model for forward inference to extract the layer-by-layer representation data of each layer of the model at the target position. The target semantic is a single semantic item or multiple related but different sub-semantic directions divided by the upper semantic dimension. The target semantic sample set includes at least text content, target semantic labels and positive and negative labels. The positive and negative labels are used to represent the correlation between text content and target semantic. The steering vector module is used to calculate the difference between the layer-by-layer representation data to obtain the layer-by-layer semantic steering vector, and then perform normalization processing to obtain the normalized layer-by-layer semantic steering vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. Semantic aggregation vector module: It is used to calculate the separability score of each layer of the model based on the validation set data, filter the candidate layer set of the target semantic direction based on the ranking, and then combine the normalized layer-by-layer semantic turning vector, and obtain the semantic aggregation vector corresponding to the target semantic direction through weighted aggregation operation. Projection calculation module: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, it constructs the local low-rank basis space corresponding to the upper semantic dimension based on the semantic aggregation vector corresponding to the target semantic direction, and projects the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector; Control Decision Module: Used to receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, obtain online scores for each candidate layer, perform normalization processing after sorting or threshold filtering strategies, obtain routing weights corresponding to each target layer, and obtain adaptive injection intensity through injection intensity search. The adaptive injection intensity is limited to the intensity value that makes the output distribution change closest to the target distribution perturbation threshold in the search. Vector Injection Module: This module integrates parameters based on the target layer set, the routing weights, and the adaptive injection strength. It calculates injection parameters and the steering vector to be injected, combines them with the target token position of the current generation step, obtains the residual representation after injection, performs subsequent decoding and generation, and obtains a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding and generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
[0014] On the other hand, a dynamic control device for a large language model based on activation spatial semantics is provided. The dynamic control device for a large language model based on activation spatial semantics includes: a processor; a memory, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the method described in any of the above-described dynamic control methods for a large language model based on activation spatial semantics is implemented.
[0015] The beneficial effects of the technical solutions provided in the embodiments of the present invention include at least the following: This invention achieves semantic control at a lower cost without modifying LLM parameters. Control is achieved during the inference phase by intervening in the activation space of the model's internal residual representation, eliminating the need for model retraining or parameter updates. Therefore, compared to parameter fine-tuning and alignment training methods, this invention offers advantages such as lower implementation cost, more flexible deployment, and easier switching of control targets, facilitating rapid switching and reuse across different semantic directions.
[0016] This invention improves the accuracy of control layer selection by combining "offline candidate layer screening with online dynamic layer routing." Instead of injecting steering signals into a fixed preset layer, it first filters candidate layers offline that better represent the target semantic direction, and then dynamically scores and sparsely routes these candidate layers based on the current input during the inference phase. Therefore, this invention enables control layer selection to simultaneously consider "semantic representation stability" and "input context adaptability," thereby significantly improving the stability and accuracy of control performance and reducing control mismatch problems caused by fixed layer injection.
[0017] This invention improves control consistency under different inputs through adaptive injection strength control. It dynamically determines a more suitable injection strength for the current input, rather than using a uniform fixed strength. Therefore, under different input prompts, different task types, and different contexts, this invention can effectively avoid problems such as unclear steering due to insufficient control signals, and excessive output offset and degraded generation quality due to excessive control signals, thereby improving the consistency and robustness of the control effect.
[0018] This invention improves the robustness of the target semantic direction representation by constructing semantic aggregation vectors. Instead of relying on single-layer, layer-by-layer semantic vectors for direct control, this invention first filters high-quality candidate layers through offline scoring, and then weights and aggregates them to form semantic aggregation vectors. This approach effectively reduces the impact of single-layer noise and random differences, making the obtained target semantic direction more robust, thereby improving the accuracy and repeatability of downstream steering. Attached Figure Description
[0019] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 This is a flowchart of a dynamic control method for a large language model based on activation space semantics provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the stages of a dynamic control method for a large language model based on activation space semantics provided in an embodiment of the present invention; Figure 3 This is a block diagram of a dynamic control device for a large language model based on activation space semantics provided in an embodiment of the present invention; Figure 4 This is a schematic diagram of the structure of a dynamic control device based on a large language model with activation space semantics, provided in an embodiment of the present invention. Detailed Implementation
[0021] The technical solution of the present invention will now be described with reference to the accompanying drawings.
[0022] In embodiments of the present invention, words such as "exemplarily," "for example," etc., are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" in the present invention should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the word "exemplary" is intended to present the concept in a concrete manner. Furthermore, in embodiments of the present invention, the meaning expressed by "and / or" can be both, or either one.
[0023] In the embodiments of this invention, the terms "image" and "picture" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning. Similarly, the terms "of," "corresponding (relevant)," and "corresponding" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning.
[0024] In this embodiment of the invention, sometimes a subscript such as W1 may be written in a non-subscript form such as W1. When the difference is not emphasized, the meaning they express is the same.
[0025] To make the technical problems, technical solutions and advantages of the present invention clearer, a detailed description will be given below in conjunction with the accompanying drawings and specific embodiments.
[0026] Preferably, target semantic data is acquired and a target semantic sample set and a validation set are constructed. The target semantic sample set is input into a frozen large language model for forward inference, and the layer-by-layer representation data of each layer of the model at the target position is extracted. The target semantic is a single semantic item, or multiple related but different sub-semantic directions divided by a higher-level semantic dimension. The target semantic sample set includes at least text content, target semantic labels, and positive and negative markers. The positive and negative markers are used to represent the relevance between the text content and the target semantic, including: Acquire target semantic sample data, determine semantic direction and label positive and negative samples, extract a predetermined number of samples to obtain a target semantic sample set, and use the remaining independent samples as verification samples to obtain a verification set. The target semantics includes a single semantic item or multiple sub-semantic directions divided by a higher semantic dimension. The target semantic sample set includes text content, target semantic tags and positive and negative labels representing relevance. The positive and negative labels are used to represent the relevance between the text content and the target semantics. The multiple sub-semantic directions should satisfy the condition that they are related to each other but not the same. The target semantic sample set is input into the frozen large language model for forward inference to obtain intermediate state data of each layer of the model. The intermediate state data includes the hidden representation of each layer of the model at the target location. Features are extracted from the intermediate state data to obtain layer-by-layer representation data, which includes the hidden representation of each layer of the model at the target position, preferably the position of the last valid token.
[0027] In some embodiments, positive and negative labels characterizing relevance categorize samples into positive and negative samples. Positive samples indicate that the text is relevant to the target semantics, while negative samples indicate that the text is irrelevant to the target semantics or forms a contrasting relationship with the target semantics.
[0028] When the target is a single semantic item, the target semantic sample set is constructed around that single semantic item. When the target is a higher-level semantic dimension, the higher-level semantic dimension is first divided into multiple sub-semantic directions, and then positive and negative sample sets are constructed for each sub-semantic direction. The multiple sub-semantic directions should satisfy the condition of being "related but not identical" in order to subsequently establish a shared low-rank structure for the higher-level semantic dimension.
[0029] It should be noted that the target semantic sample set provides a training basis for learning the internal representation differences of the target semantic direction, and provides data support for the subsequent construction of the local low-rank basis space of the upper semantic dimension.
[0030] It should be noted that extracting intermediate state data is to obtain the internal representation of the target semantics in different layers of the model, which provides a basis for subsequent calculation of layer-by-layer semantic vectors and selection of candidate layers.
[0031] The layer-by-layer representation data is processed by difference calculation to obtain a layer-by-layer semantic shift vector, and then normalized to obtain a normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. Preferably, the layer-by-layer representation data is processed by difference calculation to obtain a layer-by-layer semantic shift vector, and then normalized to obtain a normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer, including: Based on the layer-by-layer representation data, the mean of positive sample hidden representation and the mean of negative sample hidden representation in each target semantic direction are compared and the difference is calculated to obtain the layer-by-layer semantic turning vector for each target semantic direction. The difference calculation includes calculating the difference between the mean of positive sample hidden representation and the mean of negative sample hidden representation in each layer. The normalized semantic shift vector is calculated by applying a normalization process. When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, a local low-rank basis space corresponding to the upper semantic dimension is constructed based on the semantic aggregation vector corresponding to the target semantic direction, and the semantic aggregation vector is projected onto the local low-rank basis space to obtain the projected semantic vector.
[0032] In some embodiments, the layer-by-layer semantic vectors are normalized to eliminate scale differences. For higher-level semantic dimension scenarios, this step performs the above process on each of its sub-semantic directions to obtain layer-by-layer semantic vectors corresponding to multiple sub-semantic directions.
[0033] It should be noted that the difference in the internal representation of the target semantics in positive and negative samples is transformed into a semantic steering signal that can be used to control the direction of model generation.
[0034] Preferably, based on the validation set data, the separability scores of each layer of the model are calculated. Based on the ranking, a candidate layer set for the target semantic direction is selected. Then, combined with the normalized layer-by-layer semantic shift vector, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction, including: The validation set data is input into the trained logistic regression classifier to obtain the separability scores of each layer of the model. The separability scores are determined based on preset classification performance indicators, including validation set accuracy, F1 score, ROC-AUC, and linear discriminant confidence. The trained logistic regression classifier is a logistic regression classifier trained using the hidden representations corresponding to each layer in the layer-by-layer representation data and their corresponding positive and negative labels. The hidden representations are used as input features, and the positive and negative labels are used as supervision signals. Based on the separability scores of each layer, after sorting and filtering from high to low, a candidate layer set for the target semantic direction is obtained, and the candidate layer set includes a preset number of layers with the highest scores. Based on the normalized layer-by-layer semantic turning vector, and according to the candidate layer set and its corresponding separability score, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction.
[0035] In some embodiments, the separability score utilizes the identification of high-quality layers that better express the target semantics; avoids relying on a single layer, which leads to unstable semantic representations; provides a smaller and more efficient range of candidate layers for subsequent online dynamic layer routing; and obtains a more robust semantic aggregation vector as a candidate turning vector for the inference stage.
[0036] Preferably, when the target semantic direction belongs to one of multiple sub-semantic directions under the same higher semantic dimension, a local low-rank basis space corresponding to the higher semantic dimension is constructed based on the semantic aggregation vector corresponding to the target semantic direction, and the semantic aggregation vector is projected onto the local low-rank basis space to obtain the projected semantic vector, including: When the upper semantic dimension contains multiple related sub-semantic directions, a set of semantic aggregation vectors corresponding to the upper semantic dimension is used to perform matrix arrangement and low-rank decomposition to obtain the local low-rank basis space corresponding to the upper semantic dimension. The low-rank decomposition algorithm includes principal component analysis or singular value decomposition. The semantic aggregation vector is input into the local low-rank basis space for projection calculation to obtain the projected semantic vector; When the target semantics does not belong to the higher semantic dimension, or does not satisfy the conditions of multiple related sub-semantic directions, the local low-rank basis space projection calculation step is skipped.
[0037] In some embodiments, the construction of a local low-rank basis space of the upper semantic dimension is preferably enabled only when the following conditions are met: the upper semantic dimension contains multiple related sub-semantic directions; the low-rank decomposition can retain a high interpretation variance; and the projected semantic vectors do not significantly reduce the downstream control effect.
[0038] When the above conditions are not met, semantic aggregation vectors can be used directly without performing low-rank projection.
[0039] The purpose of this step is to: extract shared semantic structures from multiple related sub-semantic directions; reduce noise in a single semantic vector; improve the stability, generalization, and interpretability of the control of the higher-level semantic dimension; and avoid semantic confusion caused by different semantic types being uniformly compressed into the same space.
[0040] Upon receiving an input prompt, the candidate layer set is used to perform exploratory injection on the input prompt to obtain an online score for each candidate layer. After sorting or threshold filtering, normalization is performed to obtain the routing weight corresponding to each target layer. An adaptive injection strength is obtained through injection strength search. The adaptive injection strength is limited to the strength value that makes the output distribution change closest to the target distribution perturbation threshold in the search. Preferably, upon receiving an input prompt, the candidate layer set is used to perform exploratory injection based on the input prompt, obtaining online scores for each candidate layer. After sorting or threshold filtering, normalization is performed to obtain the routing weights corresponding to each target layer. An adaptive injection strength is obtained through injection strength search. The adaptive injection strength is defined as the strength value that makes the output distribution change closest to the target distribution perturbation threshold during the search, including: Upon receiving an input prompt, the candidate layer set is used to perform exploratory injection on the input prompt to obtain an online score for each candidate layer. The exploratory injection includes obtaining output distribution data before and after injection. The online score is obtained by quantifying the changes before and after injection. The change quantification calculation includes calculating the magnitude of the output logits change or the divergence of the output probability distribution. Based on the online scores of each candidate layer, a set of target layers is obtained through sorting or threshold filtering strategies. The online scores of the target layer set are then normalized to obtain the routing weights corresponding to each target layer. The set of target layers includes one or more layers with higher scores selected from the candidate layer set. The routing weights are used for subsequent weighted injection of each target layer. Based on a preset target distribution perturbation threshold and a preset intensity range, an adaptive injection intensity is obtained through injection intensity search. The adaptive injection intensity is defined as the intensity value that makes the output distribution change closest to the target distribution perturbation threshold during the search.
[0041] In some embodiments, for each candidate layer, a small exploratory injection is first performed, and then the changes in the output distribution before and after the injection are compared to obtain the effect of the layer under the current input. The online scoring comprehensively considers at least one of the following indicators: the magnitude of the change in output logits before and after injection; the divergence of the output probability distribution before and after injection; whether the output distribution perturbation is close to the preset target range; and the degree to which the output result shifts towards the target semantic direction.
[0042] It should be noted that, based on the online scoring results, the top M layers are selected from the candidate layer set as the target layer set corresponding to this input, and their scores are normalized to obtain the routing weight of each target layer.
[0043] It should be further explained that the role of online scoring results is to: adaptively select more effective layers based on the contextual semantics of the current input; avoid the failure of fixed-layer injection in different input scenarios; and concentrate steering signals on a few more effective layers, thereby improving control efficiency and accuracy. The role of adaptive injection strength is to: avoid undercontrol or overcontrol caused by fixed injection strength; improve the consistency of semantic steering under different input prompts; and balance the relationship between control effect and the original generation quality of the model.
[0044] The parameters are integrated based on the target layer set, the routing weights, and the adaptive injection strength. The injection parameters and the steering vector to be injected are calculated. Combined with the target token position of the current generation step, the residual representation after injection is obtained. Subsequent decoding and generation are then performed to obtain a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding and generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
[0045] Preferably, parameters are integrated based on the target layer set, the routing weights, and the adaptive injection strength to calculate injection parameters and the steering vector to be injected. Combined with the target token position of the current generation step, the injected residual representation is obtained, and subsequent decoding generation is performed to obtain a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector, including: The parameters are integrated based on the target layer set, the routing weight, and the adaptive injection strength to obtain the injection parameters corresponding to the input prompt and to obtain the turning vector to be injected. The injection parameters include the target layer, injection weight, and injection strength. The turning vector to be injected includes at least one of the semantic aggregation vector or the projected semantic vector. The steering vector to be injected is input into the residual stream of the target layer in the current step of text generation, according to the injection parameters and the target token position of the current generation step, to obtain the injected residual representation. The feature of inputting the steering vector into the residual stream of the target layer is that weighted injection is performed on the steering vector at the target token position. The injected residual representation is used for subsequent decoding and generation to obtain semantically controlled text results. The decoding and generation includes an autoregressive or non-autoregressive text generation process performed by a large language model.
[0046] In some embodiments, residual injection is performed only at the target token position corresponding to the current generation step to reduce perturbation of irrelevant positions. For any target layer, the injected residual representation can be expressed as:
[0047] in: Represented as the original residual; For routing weights; For adaptive injection strength; The turning vector is the turning vector corresponding to the target semantic direction. The turning vector can be a semantic aggregation vector or a projection vector in the local low-rank basis space of the higher semantic dimension.
[0048] In one specific embodiment, a 32-layer Transformer structure is used as the base model, with the target semantic direction being "positive sentiment". First, the separability score of each layer is calculated based on the positive and negative sentiment sample sets, and the top 6 layers are selected to form a candidate layer set {l14, l16, l18, l19, l21, l22}. The input prompt is "Please describe your mood today". Independent probing injection is performed only on the candidate layers, injecting a "positive sentiment" semantic shift vector with a fixed probing strength of 0.1. The divergence between the output distribution after injection and the original distribution is calculated as the online score, resulting in S(l14)=0.41, S(l16)=0.56, S(l18)=0.62, S(l19)=0.59, S(l21)=0.68, and S(l22)=0.64. A threshold T=0.58 is set to filter the target layer, resulting in {l18,l19,l21,l22}. Softmax normalization is performed on the online scores to obtain routing weights {w18≈0.247,w19≈0.239,w21≈0.262,w22≈0.252}. Finally, residual injection is performed based on the target layer and routing weights to generate text results that conform to positive sentiment.
[0049] In one specific embodiment, the preset injection intensity search interval is [0.1, 1.0], and the target distribution perturbation threshold range is set to divergence ∈ [0.8, 1.1]. After receiving the input prompt "Please describe your mood today", a grid search is performed within the preset intensity interval with a step size of 0.05: for each candidate intensity, it is combined with the target layer routing weight and semantic turning vector to perform weighted residual injection on the target layer, and the divergence between the injected model output distribution and the original non-injected distribution is calculated; after traversing the search, the intensity value that makes the divergence closest to the target range median of 1.0 is selected as the adaptive injection intensity, and finally β=0.55 (corresponding to DKL=1.03, which meets the target perturbation range requirement) is determined for subsequent semantic control text generation.
[0050] It should be noted that, as Figure 2 As shown, the semantic shift information learned offline is combined with the target layer and injection strength determined online to form dynamic, sparse, and fine-grained control over the internal representation of the model, thereby achieving effective guidance of the output semantic direction without modifying the model parameters.
[0051] The above is an introduction to the method embodiments. The following describes the solution described in this application through device embodiments.
[0052] Figure 3 This is a block diagram of a dynamic control device for a large language model based on activation space semantics, according to an exemplary embodiment. The device includes a semantic sample module, a turning vector module, a semantic aggregation vector module, a projection calculation module, a control decision module, and a vector injection module.
[0053] Semantic Sample Module: Used to acquire target semantic data and construct target semantic sample set and validation set. The target semantic sample set is input into the frozen large language model for forward inference to extract the layer-by-layer representation data of each layer of the model at the target position. The target semantic is a single semantic item or multiple related but different sub-semantic directions divided by the upper semantic dimension. The target semantic sample set includes at least text content, target semantic labels and positive and negative labels. The positive and negative labels are used to represent the correlation between text content and target semantic. The steering vector module is used to calculate the difference between the layer-by-layer representation data to obtain the layer-by-layer semantic steering vector, and then perform normalization processing to obtain the normalized layer-by-layer semantic steering vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. Semantic aggregation vector module: It is used to calculate the separability score of each layer of the model based on the validation set data, filter the candidate layer set of the target semantic direction based on the ranking, and then combine the normalized layer-by-layer semantic turning vector, and obtain the semantic aggregation vector corresponding to the target semantic direction through weighted aggregation operation. Projection calculation module: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, it constructs the local low-rank basis space corresponding to the upper semantic dimension based on the semantic aggregation vector corresponding to the target semantic direction, and projects the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector; Control Decision Module: Used to receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, obtain online scores for each candidate layer, perform normalization processing after sorting or threshold filtering strategies, obtain routing weights corresponding to each target layer, and obtain adaptive injection intensity through injection intensity search. The adaptive injection intensity is limited to the intensity value that makes the output distribution change closest to the target distribution perturbation threshold in the search. Vector Injection Module: This module integrates parameters based on the target layer set, the routing weights, and the adaptive injection strength. It calculates injection parameters and the steering vector to be injected, combines them with the target token position of the current generation step, obtains the residual representation after injection, performs subsequent decoding and generation, and obtains a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding and generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
[0054] A dynamic control device for a large language model based on activation spatial semantics, the device comprising: a processor; and a memory storing computer-readable instructions, wherein when the processor executes the computer-readable instructions, it implements the method described in any of the above-described dynamic control methods for a large language model based on activation spatial semantics.
[0055] Figure 4 This is a schematic diagram of the structure of a dynamic control device based on a large language model with activation space semantics, provided by an embodiment of the present invention. Figure 4 As shown, a dynamic control device based on a large language model with activation space semantics may include the above-mentioned features. Figure 3 The large language model dynamic control device based on activation spatial semantics is shown. Optionally, the large language model dynamic control device 410 based on activation spatial semantics may include a first processor 2001.
[0056] Optionally, the dynamic control device 410 based on the large language model of activation spatial semantics may also include a memory 2002 and a transceiver 2003.
[0057] The first processor 2001, memory 2002, and transceiver 2003 can be connected via a communication bus.
[0058] The following is combined Figure 4 A detailed description of each component of the dynamic control device 410 based on the large language model of activation space semantics is provided below: The first processor 2001 is the control center of the dynamic control device 410 based on the large language model of activation spatial semantics. It can be a single processor or a collective term for multiple processing elements. For example, the first processor 2001 can be one or more central processing units (CPUs), application-specific integrated circuits (ASICs), or one or more integrated circuits configured to implement embodiments of the present invention, such as one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs).
[0059] Optionally, the first processor 2001 can execute various functions of the large language model dynamic control device 410 based on activation spatial semantics by running or executing software programs stored in the memory 2002 and calling data stored in the memory 2002.
[0060] In a specific implementation, as one example, the first processor 2001 may include one or more CPUs, for example... Figure 4 CPU0 and CPU1 are shown in the diagram.
[0061] In a specific implementation, as one example, the dynamic control device 410 based on the large language model of activation space semantics may also include multiple processors, for example... Figure 4 The first processor 2001 and the second processor 2004 are shown in the diagram. Each of these processors can be a single-core processor or a multi-core processor. Here, a processor can refer to one or more devices, circuits, and / or processing cores used to process data (such as computer program instructions).
[0062] The memory 2002 is used to store the software program that executes the present invention, and is controlled by the first processor 2001 to execute it. The specific implementation method can be referred to the above method embodiment, and will not be repeated here.
[0063] Optionally, the memory 2002 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but not limited thereto. The memory 2002 may be integrated with the first processor 2001 or may exist independently, and may be controlled via the interface circuit of the large language model based on activation space semantics. Figure 4 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0064] The transceiver 2003 is used to communicate with network devices or with terminal devices.
[0065] Alternatively, transceiver 2003 may include a receiver and a transmitter. Figure 4 (Not shown separately). The receiver is used to implement the receiving function, and the transmitter is used to implement the transmitting function.
[0066] Optionally, the transceiver 2003 can be integrated with the first processor 2001 or exist independently, and can dynamically control the interface circuit of the device 410 through a large language model based on activation space semantics. Figure 4 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0067] It should be noted that, Figure 4 The structure of the large language model dynamic control device 410 based on activation space semantics shown in the figure does not constitute a limitation on the router. The actual knowledge structure recognition device may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0068] Furthermore, the technical effects of the dynamic control device 410 based on the large language model with activated spatial semantics can be referred to the technical effects of the dynamic control method based on the large language model with activated spatial semantics described in the above method embodiments, and will not be repeated here.
[0069] It should be understood that the first processor 2001 in the embodiments of the present invention may be a central processing unit (CPU), or it may be 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. The general-purpose processor may be a microprocessor or any conventional processor, etc.
[0070] It should also be understood that the memory in the embodiments of the present invention can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous DRAM (DDR SDRAM), enhanced synchronous DRAM (ESDRAM), synchronous linked DRAM (SLDRAM), and direct rambus RAM (DR RAM).
[0071] The above embodiments can be implemented, in whole or in part, by software, hardware (such as circuits), firmware, or any other combination thereof. When implemented using software, the above embodiments can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more sets of available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium. A semiconductor medium can be a solid-state drive.
[0072] It should be understood that the term "and / or" in this article is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. A and B can be singular or plural. Additionally, the character " / " in this article generally indicates an "or" relationship between the preceding and following related objects, but it can also represent an "and / or" relationship. Please refer to the context for a more accurate understanding.
[0073] In this invention, "at least one" means one or more, and "more than one" means two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of a single item or a plurality of items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be a single item or multiple items.
[0074] It should be understood that, in various embodiments of the present invention, the order of the above-mentioned process numbers does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0075] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0076] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0077] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0078] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0079] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0080] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0081] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A dynamic control method for a large language model based on activation space semantics, characterized in that, The method includes: S1: Obtain target semantic data and construct a target semantic sample set and a validation set. Input the target semantic sample set into the frozen large language model for forward inference and extract the layer-by-layer representation data of each layer of the model at the target position. The target semantic is a single semantic item or multiple related but different sub-semantic directions divided by the upper semantic dimension. The target semantic sample set includes at least text content, target semantic labels and positive and negative labels. The positive and negative labels are used to represent the correlation between the text content and the target semantic. S2: The layer-by-layer representation data is processed by difference calculation to obtain the layer-by-layer semantic shift vector, and then normalized to obtain the normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. S3: Calculate the separability score of each layer of the model based on the validation set data, filter the candidate layer set of the target semantic direction based on the ranking, and then combine the normalized layer-by-layer semantic turning vector with the weighted aggregation operation to obtain the semantic aggregation vector corresponding to the target semantic direction. S4: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, construct the local low-rank basis space corresponding to the upper semantic dimension according to the semantic aggregation vector corresponding to the target semantic direction, and project the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector. S5: Receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, obtain online scores for each candidate layer, and after sorting or threshold filtering strategies, perform normalization processing to obtain the routing weights corresponding to each target layer. Through injection strength search, obtain adaptive injection strength, which is limited to the strength value that makes the output distribution change closest to the target distribution perturbation threshold in the search. S6: Based on the target layer set, the routing weight, and the adaptive injection strength, parameters are integrated to calculate the injection parameters and the turning vector to be injected. Combined with the target token position of the current generation step, the residual representation after injection is obtained. Subsequent decoding generation is performed to obtain a semantically controlled text result. The injection parameters include the target layer, injection weight, and injection strength. The decoding generation includes an autoregressive text generation process performed by a large language model. The turning vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
2. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, S1 involves acquiring target semantic data and constructing a target semantic sample set and a validation set. The target semantic sample set is then input into a frozen large language model for forward inference, extracting layer-by-layer representation data of each layer at the target location. The target semantic is a single semantic item, or multiple related but distinct sub-semantic directions divided by a higher-level semantic dimension. The target semantic sample set includes at least text content, target semantic labels, and positive / negative markers. The positive / negative markers characterize the relevance between the text content and the target semantic, including: S11: Obtain target semantic sample data, determine semantic direction and label positive and negative samples, extract a predetermined number of samples to obtain a target semantic sample set, use the remaining independent samples as verification samples to obtain a verification set. The target semantics includes a single semantic item or multiple sub-semantic directions divided by a higher semantic dimension. The target semantic sample set includes text content, target semantic tags and positive and negative labels representing relevance. The positive and negative labels are used to represent the relevance between the text content and the target semantics. The multiple sub-semantic directions should satisfy the condition that they are related to each other but not the same. S12: Input the target semantic sample set into the frozen large language model for forward reasoning operation to obtain intermediate state data of each layer of the model. The intermediate state data includes the hidden representation of each layer of the model at the target location. S13: Extract features from the intermediate state data to obtain layer-by-layer representation data, which includes the hidden representation of each layer of the model at the target position, preferably the position of the last valid token.
3. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, The S2 process involves calculating the difference between the layer-by-layer representation data to obtain a layer-by-layer semantic shift vector, and then normalizing this vector to obtain a normalized layer-by-layer semantic shift vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer, including: S21: Based on the layer-by-layer representation data, the mean of positive sample hidden representation and the mean of negative sample hidden representation in each target semantic direction are compared and the difference is calculated to obtain the layer-by-layer semantic turning vector for each target semantic direction. The difference calculation includes calculating the difference between the mean of positive sample hidden representation and the mean of negative sample hidden representation in each layer. S22: The normalization process is used to calculate the layer-by-layer semantic turning vector to obtain the normalized layer-by-layer semantic turning vector; S23: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, construct the local low-rank basis space corresponding to the upper semantic dimension according to the semantic aggregation vector corresponding to the target semantic direction, and project the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector.
4. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, The S3 method calculates the separability scores of each layer of the model based on the validation set data. Based on ranking, it filters the candidate layer set for the target semantic direction. Then, combining this with the normalized layer-by-layer semantic shift vector, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction, including: S31: Input the validation set data into the trained logistic regression classifier to obtain the separability score of each layer of the model. The separability score is determined based on the preset classification performance index, which includes the validation set accuracy, F1 score, ROC-AUC and linear discriminant confidence. The trained logistic regression classifier is a logistic regression classifier trained using the hidden representations of each layer in the layer-by-layer representation data and their corresponding positive and negative labels. The hidden representations are used as input features and the positive and negative labels are used as supervision signals. S32: Based on the separability scores of each layer, after sorting and filtering from high to low, a candidate layer set for the target semantic direction is obtained, and the candidate layer set includes a preset number of layers with the highest scores. S33: Based on the normalized layer-by-layer semantic turning vector, and according to the candidate layer set and its corresponding separability score, a weighted aggregation operation is performed to obtain the semantic aggregation vector corresponding to the target semantic direction.
5. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, In step S4, when the target semantic direction belongs to one of multiple sub-semantic directions under the same upper semantic dimension, a local low-rank basis space corresponding to the upper semantic dimension is constructed based on the semantic aggregation vector corresponding to the target semantic direction, and the semantic aggregation vector is projected onto the local low-rank basis space to obtain the projected semantic vector, including: S41: When the upper semantic dimension contains multiple related sub-semantic directions, a set of semantic aggregation vectors corresponding to the upper semantic dimension is used to perform matrix arrangement and low-rank decomposition to obtain the local low-rank basis space corresponding to the upper semantic dimension. The low-rank decomposition algorithm includes principal component analysis or singular value decomposition. S42: Input the semantic aggregation vector into the local low-rank basis space for projection calculation to obtain the projected semantic vector; S43: When the target semantics does not belong to the higher semantic dimension, or does not satisfy the condition of multiple related sub-semantic directions, skip the local low-rank basis space projection calculation step.
6. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, The input prompt received in S5 is obtained by using the candidate layer set and performing exploratory injection on the input prompt to obtain the online score of each candidate layer. After sorting or threshold filtering, normalization is performed to obtain the routing weight corresponding to each target layer. An adaptive injection strength is obtained through injection strength search. The adaptive injection strength is limited to the strength value that makes the output distribution change closest to the target distribution perturbation threshold in the search, including: S51: Receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, and obtain online scores for each candidate layer. The exploratory injection includes obtaining output distribution data before and after injection. The online scores are obtained by quantifying the changes before and after injection. The change quantification calculation includes calculating the magnitude of output logits change or the divergence of output probability distribution. S52: Based on the online scores of each candidate layer, a target layer set is obtained through sorting or threshold filtering strategies, and the online scores of the target layer set are normalized to obtain the routing weights corresponding to each target layer. The target layer set includes one or more layers with higher scores selected from the candidate layer set. The routing weights are used for subsequent weighted injection of each target layer. S53: Based on the preset target distribution perturbation threshold and the preset intensity range, an adaptive injection intensity is obtained through injection intensity search. The adaptive injection intensity is limited to the intensity value that makes the output distribution change closest to the target distribution perturbation threshold in the search.
7. The dynamic control method for a large language model based on activation space semantics according to claim 1, characterized in that, S6 integrates parameters based on the target layer set, the routing weights, and the adaptive injection strength to calculate injection parameters and the steering vector to be injected. Combined with the target token position of the current generation step, it obtains the injected residual representation and performs subsequent decoding generation to obtain semantically controlled text results. The injection parameters include the target layer, injection weights, and injection strength. The decoding generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector, including: S61: Based on the target layer set, the routing weight, and the adaptive injection strength, parameters are integrated to obtain injection parameters corresponding to the input prompt, and a turning vector to be injected is obtained. The injection parameters include target layer, injection weight, and injection strength. The turning vector to be injected includes at least one of semantic aggregation vector or projected semantic vector. S62: The steering vector to be injected is input into the residual stream of the target layer in the current step of text generation, according to the injection parameters and the target token position of the current generation step, to obtain the injected residual representation. The feature of inputting the steering vector into the residual stream of the target layer is that weighted injection is performed on the steering vector at the target token position. S63: Using the injected residual representation, perform subsequent decoding and generation to obtain semantically controlled text results. The decoding and generation includes an autoregressive or non-autoregressive text generation process performed by a large language model.
8. A dynamic control device for a large language model based on activation spatial semantics, wherein the dynamic control device for a large language model based on activation spatial semantics is used to implement the dynamic control method for a large language model based on activation spatial semantics as described in any one of claims 1-7, characterized in that, The device includes: Semantic Sample Module: Used to acquire target semantic data and construct target semantic sample set and validation set. The target semantic sample set is input into the frozen large language model for forward inference to extract the layer-by-layer representation data of each layer of the model at the target position. The target semantic is a single semantic item or multiple related but different sub-semantic directions divided by the upper semantic dimension. The target semantic sample set includes at least text content, target semantic labels and positive and negative labels. The positive and negative labels are used to represent the correlation between text content and target semantic. The steering vector module is used to calculate the difference between the layer-by-layer representation data to obtain the layer-by-layer semantic steering vector, and then perform normalization processing to obtain the normalized layer-by-layer semantic steering vector. The difference calculation includes calculating the difference between the mean of the positive sample hidden representation and the mean of the negative sample hidden representation at each layer. Semantic aggregation vector module: It is used to calculate the separability score of each layer of the model based on the validation set data, filter the candidate layer set of the target semantic direction based on the ranking, and then combine the normalized layer-by-layer semantic turning vector, and obtain the semantic aggregation vector corresponding to the target semantic direction through weighted aggregation operation. Projection calculation module: When the target semantic direction belongs to one of the multiple sub-semantic directions under the same upper semantic dimension, it constructs the local low-rank basis space corresponding to the upper semantic dimension based on the semantic aggregation vector corresponding to the target semantic direction, and projects the semantic aggregation vector onto the local low-rank basis space to obtain the projected semantic vector; Control Decision Module: Used to receive input prompts, use the candidate layer set, perform exploratory injection on the input prompts, obtain online scores for each candidate layer, perform normalization processing after sorting or threshold filtering strategies, obtain routing weights corresponding to each target layer, and obtain adaptive injection intensity through injection intensity search. The adaptive injection intensity is limited to the intensity value that makes the output distribution change closest to the target distribution perturbation threshold in the search. Vector Injection Module: This module integrates parameters based on the target layer set, the routing weights, and the adaptive injection strength. It calculates injection parameters and the steering vector to be injected, combines them with the target token position of the current generation step, obtains the residual representation after injection, performs subsequent decoding and generation, and obtains a semantically controlled text result. The injection parameters include the target layer, injection weights, and injection strength. The decoding and generation includes an autoregressive text generation process performed by a large language model. The steering vector to be injected includes at least one of a semantic aggregation vector or a projected semantic vector.
9. A dynamic control device based on a large language model with activation space semantics, characterized in that, The large language model dynamic control processor based on activation space semantics; a memory storing computer-readable instructions, which, when executed by the processor, implement the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium contains program code that can be invoked by a processor to execute the method as described in any one of claims 1 to 7.