Model inference method and apparatus, electronic device, storage medium, and program product

By selectively smoothing the feature extraction module of the content generation model, the problem of attention distribution distortion caused by the difference in sensitivity of different modules to the reduction of numerical precision is solved, thereby improving the quality and consistency of the generated content.

CN123180380APending Publication Date: 2026-11-27MOORE THREADS TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611712470.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-30
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

In existing content generation models, different feature extraction modules have varying sensitivities to reduced numerical precision. A uniform low-precision quantization strategy leads to distortion of attention distribution and deterioration of generated content quality, which accumulates layer by layer as the model depth increases.

Method used

By selectively smoothing the feature extraction module in the content generation model, smoothing is performed only on modules with large quantization errors using the self-attention layer, generating a smoothed vector matrix to reduce the content quality degradation caused by the accumulation of quantization errors.

Benefits of technology

It improves the stability of attention calculation and the accuracy of generated content, enhances the consistency between frames or in time, and reduces information distortion caused by excessive or insufficient smoothing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123180380A_ABST
    Figure CN123180380A_ABST
Patent Text Reader

Abstract

The present disclosure provides a model inference method and device, an electronic device, a storage medium and a program product, wherein the method comprises: converting an input feature sequence corresponding to a first feature extraction module in a content generation model into an input vector matrix; in response to determining that the first feature extraction module belongs to a module to be smoothed based on a quantization error corresponding to the first feature extraction module, inputting the input vector matrix into a self-attention layer of the first feature extraction module for smoothing to generate a smoothed vector matrix; and determining a first output vector matrix based on the smoothed vector matrix through the self-attention layer, wherein the first output vector matrix is used to determine an output feature sequence corresponding to the first feature extraction module for generating target content.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of artificial intelligence large model reasoning technology, and particularly to the field of content generation model reasoning technology, specifically to a model reasoning method, model reasoning device, electronic device, computer-readable storage medium, and computer program product. Background Technology

[0002] As content generation models continue to grow in scale, attention computation in Transformer / Diffusion Transformer (DiT) architectures has gradually become a bottleneck for inference efficiency in large models. To accelerate inference, low-bit or low-precision attention quantization schemes are commonly used to reduce the numerical precision of attention computation, aiming to achieve faster content generation speeds with limited computing resources. However, different feature extraction modules in content generation models have varying sensitivities to reduced numerical precision. Applying a uniform low-precision quantization strategy indiscriminately to all layers (i.e., all feature processing modules) often leads to significant distortions in the attention distribution of some layers. These distortions accumulate layer by layer with increasing model depth, ultimately resulting in degraded content quality.

[0003] Therefore, in order to mitigate the numerical distribution distortion caused by quantization, a smoothing scheme that fully considers the differences between different layers in the content generation model, i.e., feature extraction modules, is needed to suppress the degradation of generated content quality caused by the accumulation of layer-by-layer quantization errors. Summary of the Invention

[0004] This disclosure provides a model inference method, a model inference apparatus, an electronic device, a computer-readable storage medium, and a computer program product.

[0005] In a first aspect, embodiments of this disclosure propose a model inference method, comprising: converting the input feature sequence corresponding to a first feature extraction module in a content generation model into an input vector matrix; in response to determining that the first feature extraction module belongs to a module to be smoothed based on the quantization error corresponding to the first feature extraction module, inputting the input vector matrix into the self-attention layer of the first feature extraction module for smoothing, generating a smoothed vector matrix; and determining a first output vector matrix based on the smoothed vector matrix through the self-attention layer, wherein the first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

[0006] Secondly, embodiments of this disclosure propose a model inference apparatus, comprising: a processing unit, a generation unit, and a determination unit. The processing unit is configured to convert the input feature sequence corresponding to a first feature extraction module in a content generation model into an input vector matrix; the generation unit is configured to, in response to determining that the first feature extraction module belongs to a module requiring smoothing based on the quantization error corresponding to the first feature extraction module, input the input vector matrix into the self-attention layer of the first feature extraction module for smoothing, generating a smoothed vector matrix; the determination unit is configured to determine a first output vector matrix based on the smoothed vector matrix through the self-attention layer, wherein the first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

[0007] Thirdly, embodiments of this disclosure provide an electronic device comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to implement the model inference method described in any of the implementations of the first aspect above.

[0008] Fourthly, embodiments of this disclosure provide a non-transitory computer-readable storage medium storing computer instructions that enable a processor to implement the model inference method as described in any of the implementations in the first aspect.

[0009] Fifthly, embodiments of this disclosure provide a computer program product including a computer program that, when executed by a processor, can implement the model reasoning method as described in any of the implementations in the first aspect.

[0010] According to the scheme provided in this embodiment, the input feature sequence corresponding to the current feature extraction module (i.e., the first feature extraction module) in the content generation model can be transformed to obtain the corresponding input vector matrix. In response to the determination that the first feature extraction module belongs to a module requiring smoothing based on its quantization error, the self-attention layer in the feature extraction module smooths the corresponding input vector matrix. Based on the smoothed vector matrix, an output feature vector is obtained to determine the output feature sequence corresponding to the feature extraction module and used to generate the desired content (i.e., the target content). Thus, by selectively smoothing each feature extraction module in the content generation model instead of uniformly applying smoothing to all feature extraction modules, information distortion caused by over-smoothing or under-smoothing is effectively avoided. Simultaneously, precise smoothing is provided for feature extraction modules with urgent smoothing needs, and the required output vector matrix is ​​further determined based on the smoothing result. By implementing selective smoothing during the generation of the desired content, content quality degradation caused by the accumulation of quantization errors can be reduced, thereby improving the stability of attention calculation, helping to improve inter-frame or temporal consistency, and increasing the accuracy of the final output content of the content generation model.

[0011] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of this disclosure, nor is it intended to limit the scope of this disclosure. Other features of this disclosure will become readily apparent from the following description. Attached Figure Description

[0012] Other features, objects, and advantages of this disclosure will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings: Figure 1 A flowchart of a model reasoning method provided in this embodiment of the disclosure; Figure 2 A flowchart of another model reasoning method provided in this disclosure embodiment; Figure 3 A flowchart of a model inference method in an application scenario provided by an embodiment of this disclosure; Figure 4 A flowchart illustrating a method for filtering block sizes for quantization processing, provided as an embodiment of this disclosure; Figure 5 This disclosure provides a schematic diagram of a quantization processing framework; Figure 6 This disclosure provides a schematic diagram of another quantization processing framework; Figure 7 A structural block diagram of a model inference device provided in an embodiment of this disclosure; Figure 8This is a schematic diagram of the structure of an electronic device suitable for performing a model inference method, provided as an embodiment of this disclosure. Detailed Implementation

[0013] The exemplary embodiments of this disclosure are described below with reference to the accompanying drawings, including various details of the embodiments to aid understanding; these should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of this disclosure. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description. It should be noted that, unless otherwise specified, the embodiments and features described in this disclosure can be combined with each other.

[0014] It should be noted that the collection, acquisition, storage, processing, transmission, provision, disclosure, and application of user personal information (such as user image information used in large model inference) involved in the technical solution disclosed herein are all carried out with the user's knowledge and explicit authorization, comply with the provisions of relevant laws and regulations, and do not violate public order and good morals.

[0015] As content generation models continue to grow in scale, attention computation in Transformer / DiT-based content generation models has gradually become a bottleneck for inference in large models. In related technologies, attention computation mainly includes the following steps:

[0016] Where Q represents the input query vector matrix, K represents the input key vector matrix, and V represents the input value vector matrix, S represents the attention score matrix, P represents the intermediate variable attention probability matrix or weight matrix, O represents the output matrix, and K... T Let K be the transpose of K, softmax() represents the normalization operation, and d is the channel size of Q, which is generally related to the model.

[0017] To accelerate inference, low-bit or low-precision attention quantization schemes are commonly used for attention computation. For example, attention quantization based on 8-bit floating-point (FP8) representation (referred to as FP8 attention quantization) can be used. In this scheme, the inputs Q, K, V and the intermediate variable attention probability matrix P are mainly quantized with low precision using FP8. However, problems such as decreased precision, inter-layer instability and non-uniform quantization noise still exist.

[0018] To optimize the above attention quantization scheme, for example, the SAGE attention mechanism (a low-bit quantization attention mechanism) can be used to provide the following quantization optimization strategies: (1) performing per-thread granular quantization on Q and K; (2) using a smoothing mechanism to improve the quantization error generated before and after FP8 attention quantization; (3) performing partial quantization operations after the softmax() operation, such as quantization processing of the intermediate variable P. This quantization optimization scheme has a significant improvement effect on static image generation tasks, but the following problems still exist in the application of the above content generation model: (1) The smoothing scheme for Q and K provided by SAGE does not consider the following aspects, which leads to unstable content generation quality and poor quantization effect of high-sensitivity layers, i.e. layers with large quantization errors, in the content generation task: the distribution of Q and K in specific types of content generation models; analysis of the averaging dimension of Q and K; the impact of the distribution of Q and K in different denoising steps and different Transformer blocks on the smoothing strategy of Q and K.

[0019] (2) There is still room for optimization in the combination of quantization granularities used for Q, K, V and P respectively, that is, which quantization granularity combination of Q, K, V and P will result in better attention quantization effect.

[0020] (3) The impact of the maximum sequence length or number of tokens (block size) that can be processed at one time on the quantization error is not considered, but in fact the average (avg) quantization error will increase rapidly as the block size increases.

[0021] (4) The quantization errors of different attention layers or Transformer blocks vary significantly under the FP8 attention quantization scheme. Existing attention computation does not consider such inter-layer or inter-block quantization errors, such as the distribution differences of FP8 attention quantization errors between layers and the significant peak values ​​of quantization errors in some layers. In addition, existing attention computation does not consider the sensitivity of specific layers to block size. These factors can lead to instability in the inference process and the accumulation of errors in local layers, thereby affecting the overall consistency of the generated content.

[0022] Therefore, a solution is needed that is more suitable for the above content generation model, has low complexity, and can steadily improve the accuracy of the generated content while improving the model's inference performance.

[0023] The following description of several optional embodiments illustrates the technical solutions of this disclosure and the technical effects produced by these solutions. It should be noted that the following embodiments can be referenced, learned from, or combined with each other. Identical terms, similar features, and similar implementation steps in different embodiments will not be repeated.

[0024] Please refer to Figure 1 , Figure 1 A flowchart of a model inference method provided in this disclosure embodiment, wherein process 100 includes the following steps: Step 101: Convert the input feature sequence corresponding to the first feature extraction module in the content generation model into an input vector matrix.

[0025] This step aims to transform the input feature sequence corresponding to the first feature extraction module in the content generation model by the execution entity of the model inference method, such as a server, to obtain the corresponding input vector matrix, thereby providing a unified data structure for subsequent processing. Optionally, the content generation model in this embodiment may include, but is not limited to, video generation models, image generation models, speech generation models, text generation models, etc., without specific limitations, depending on specific needs. Optionally, the content generation model may include, but is not limited to, a diffusion model based on a DiffusionTransformer (DiT) architecture, in which case the feature extraction module in the content generation model may also be called a DiT block or a Transformer block. Optionally, when the content generation model is a video generation model, it may include text-to-video models such as T2V diffusion models (Text-to-Video Diffusion Models), image-to-video (I2V) models, video-to-video (V2V) models, etc.

[0026] Optionally, the content generation model in this embodiment may include multiple stacked feature extraction modules, i.e., feature extraction modules repeated multiple times. These multiple stacked feature extraction modules can be used to form the backbone network of the content generation model, for example, to be responsible for semantic understanding and noise estimation in the denoising process. The output of the first feature extraction module in the multiple stacked feature extraction modules can be used as the input of the next feature extraction module. Optionally, these multiple feature extraction modules may also be referred to as multiple feature extraction layers. The first feature extraction module mentioned above can be any one of the multiple stacked feature extraction modules.

[0027] Optionally, the input feature sequence in this embodiment may refer to a spatiotemporal token sequence obtained by transforming the random noise tensor in the input of the content generation model, which is updated as the number of denoising steps changes. This sequence represents an intermediate representation within the model at a corresponding time. The random noise tensor may be a noise tensor sampled from a Gaussian distribution, used to represent the initial or potential content that has not been denoised, such as videos, images, or text.

[0028] Optionally, each feature extraction module in this embodiment, including the first feature extraction module described above, can be a neural network structure unit including a linear layer. This linear layer can then convert the input feature sequence corresponding to each feature extraction module into a corresponding input vector matrix. Specifically, feature dimension transformation, feature combination, projection, etc., can be performed using learnable weight matrices and biases. This linear layer may include, but is not limited to, a fully connected layer.

[0029] Step 102: In response to determining that the first feature extraction module belongs to the module to be smoothed based on the quantization error corresponding to the first feature extraction module, the input vector matrix is ​​input into the self-attention layer of the first feature extraction module for smoothing to generate a smoothed vector matrix.

[0030] In this embodiment, each feature extraction module of the content generation model may further include a self-attention layer. During content generation, this self-attention layer can force the same object to maintain consistency in appearance and multiple motion trajectories across different frames or moments, thereby reducing the degradation of content generation quality, such as reducing artifacts like flickering and distortion during video generation. In the content generation model, not all feature extraction modules have the same sensitivity to changes in numerical distribution. The self-attention layer in each feature extraction module only smooths the corresponding input vector matrix when the corresponding feature extraction module belongs to the module requiring smoothing. That is, it only smooths the corresponding input vector matrix when it is accurately determined, based on the quantization error corresponding to the first feature extraction module, that it belongs to the module requiring smoothing, thus achieving selective smoothing and generating a vector matrix with calibrated numerical distribution, i.e., the smoothed vector matrix. The module requiring smoothing can refer to the module whose corresponding input vector matrix's numerical distribution is determined to have a large offset or outliers; failure to smooth these outliers would lead to instability in the self-attention layer's calculations or a decrease in output quality. By smoothing the input vector matrix corresponding to the first feature extraction module, the numerical differences between dimensions or positions in the input vector matrix can be reduced, outliers that are too large or too small can be suppressed, and the overall distribution can be made more concentrated. This reduces computational overhead, increases inference throughput, and reduces accuracy loss caused by over-smoothing or under-smoothing, while also helping to reduce quantization errors.

[0031] Optionally, the self-attention layer in each feature extraction module in this embodiment can be a multi-head attention layer.

[0032] Step 103: Determine the first output vector matrix based on the smoothed vector matrix through the self-attention layer.

[0033] The first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating the target content.

[0034] In this embodiment, the self-attention layer in the first feature extraction module can further determine the output vector matrix, i.e., the first output vector matrix, based on the smoothed vector matrix. This output vector matrix can be used to determine the output feature sequence corresponding to the feature extraction module for generating the final target content. The first output vector matrix can serve as a carrier for the corresponding feature extraction module to pass core information to the next stage. Optionally, the output feature sequence corresponding to the current feature extraction module (i.e., the first feature extraction module) can be the noise prediction feature corresponding to the current denoising step, and the feature output sequence corresponding to the first feature extraction module can be used as the input to the next feature extraction module. Alternatively, if the first feature extraction module is the last module among multiple stacked feature extraction modules in the content generation model, the final target content to be generated can be constructed based on the output feature sequence, wherein the type of the target content corresponds to the type of the content generation model.

[0035] Optionally, in the embodiments of this disclosure, each feature extraction module of the content generation model may also include a feedforward network such as an MLP, which calculates the output vector matrix corresponding to the self-attention layer to obtain the corresponding output feature sequence of the target content required by the user for the final generation.

[0036] According to the model inference method provided in this disclosure, the input feature sequence corresponding to the current feature extraction module (i.e., the first feature extraction module) in the content generation model can be transformed to obtain the corresponding input vector matrix. In response to the determination that the first feature extraction module belongs to a module requiring smoothing based on its quantization error, the self-attention layer in the feature extraction module smooths the corresponding input vector matrix. Based on the smoothed vector matrix, an output feature vector is obtained to determine the output feature sequence corresponding to the feature extraction module used to generate the desired content (i.e., the target content). Thus, by selectively smoothing each feature extraction module in the content generation model instead of uniformly applying smoothing to all feature extraction modules, information distortion caused by over-smoothing or under-smoothing is effectively avoided. Simultaneously, precise smoothing is provided for feature extraction modules with urgent smoothing needs, and the required output vector matrix is ​​further determined based on the smoothing result. By selectively smoothing during the generation of the desired content, content quality degradation caused by the accumulation of quantization errors can be reduced, thereby improving the stability of attention calculation, helping to improve inter-frame or temporal consistency, and increasing the accuracy of the final output content of the content generation model.

[0037] Please refer to Figure 2 , Figure 2 A flowchart of another model inference method provided in this disclosure embodiment, wherein process 200 includes the following steps: Step 201: Convert the input feature sequence corresponding to the first feature extraction module in the content generation model into an input vector matrix.

[0038] The above steps 201 and as follows Figure 1 The steps shown are the same as 101. For the same parts, please refer to the corresponding parts of the previous embodiment. They will not be repeated here.

[0039] Step 202: In response to determining that the first feature extraction module belongs to the module to be smoothed based on the quantization error corresponding to the first feature extraction module, the input vector matrix is ​​processed in the channel dimension through the self-attention layer of the first feature extraction module to obtain the channel dimension smoothing value.

[0040] This step aims to enable the execution entity of the model inference method, such as a server, to determine, in response to the quantization error corresponding to the current feature extraction module of the content generation model (i.e., the first feature extraction module), that it belongs to the module that needs smoothing, and to perform channel dimension processing when smoothing the input vector matrix through the self-attention layer in the feature extraction module, so as to obtain the channel dimension smoothing value, thereby achieving fine-grained smoothing of the corresponding input vector matrix, and thus reducing quantization noise in a more granular manner.

[0041] Optionally, in this embodiment, the mean of all position values ​​in the channel dimension of the input vector matrix can be calculated through the self-attention layer of each feature extraction module to obtain the corresponding channel dimension smoothing value. That is, the channel dimension smoothing value is a statistic in the channel dimension, where the channel dimension can refer to the dimension in the input vector matrix corresponding to the feature channel.

[0042] Step 203: The input vector matrix is ​​smoothed by a self-attention layer based on the channel dimension smoothing value to generate a smoothed vector matrix.

[0043] This step aims to smooth the input vector matrix by applying a smoothed value based on the channel dimension of the input vector matrix through a self-attention layer. This smoothed value is then used to further smooth the input vector matrix, adjusting the data distribution and resulting in a smoothed vector matrix. By smoothing each channel sequentially, the amplitude of each channel in the vector matrix can be brought to a similar level, effectively suppressing amplitude differences between channels and eliminating overall channel offsets. This significantly compresses the dynamic range of the input vector matrix, suppresses the interference of outliers, and helps prevent outlier channels from dominating the global quantization parameters, thus providing a more stable numerical environment for subsequent attention calculations.

[0044] Optionally, the input vector matrix can be smoothed by the self-attention layer based on the channel dimension smoothing value in a decentralized manner, that is, the value at each position in the input vector matrix can be subtracted from the smoothing value corresponding to its channel, i.e., the channel dimension smoothing value.

[0045] Step 204: Determine the first output vector matrix based on the smoothed vector matrix through the self-attention layer.

[0046] The first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating the target content.

[0047] The above steps 204 and as follows Figure 1 The steps shown are the same as step 103. For the same parts, please refer to the corresponding parts of the previous embodiment. They will not be repeated here.

[0048] According to the model inference method provided in this disclosure, the input feature sequence corresponding to the current feature extraction module, i.e., the first feature extraction module, in the content generation model can be transformed to obtain the corresponding input vector matrix. In response to the determination that the first feature extraction module belongs to the module that needs to be smoothed based on the quantization error, the self-attention layer in the feature extraction module is used to smooth the corresponding input vector matrix based on the channel dimension smoothing value. Based on the smoothed vector matrix, the output feature vector is obtained to determine the output feature sequence corresponding to the feature extraction module and used to generate the required content, i.e., the target content. By smoothing each channel, the amplitude of each channel of the vector matrix can be brought to a similar level, effectively suppressing the amplitude difference between channels and eliminating the overall offset between channels. In this way, selective smoothing is achieved by applying smoothing values ​​based on the channel dimension to each feature extraction module in the content generation model. This effectively avoids information distortion caused by over-smoothing or under-smoothing. Furthermore, the required output vector matrix is ​​determined based on the results of fine-grained smoothing at the channel dimension. By implementing selective smoothing, the degradation of content quality caused by the accumulation of quantization errors can be reduced, thereby improving the stability of attention calculation. This, in turn, helps to improve the consistency between video frames or in time sequence and improves the accuracy of the final output content of the content generation model.

[0049] In some optional implementations of the embodiments of this disclosure, the above-described model inference method can also flexibly and accurately determine whether a feature extraction module, i.e., the first feature extraction module, in the content generation model belongs to a module that needs smoothing processing based on the quantization error corresponding to the current feature extraction module in the content generation model, in one of the following ways, so as to improve the effectiveness of smoothing processing or operation, avoid unnecessary redundant calculations, and thus enable the content generation model to obtain more stable content output quality during the inference process. Specifically, it may include the following: (1) If the first feature extraction module is in the first module list, the first feature extraction module is determined to be the module to be smoothed, wherein the first module list is used to statistically analyze modules whose quantization error is greater than or equal to the first error threshold. Conversely, if the first feature extraction module is not in the first module list, it can be determined that it does not belong to the module to be smoothed.

[0050] In this embodiment, it is possible to determine whether each feature extraction module in the content generation model is located in the first module list, which is a statistical list of modules that need to be smoothed and is predetermined with a first error threshold as the threshold. This allows for efficient and accurate determination of whether the current feature extraction module needs to smooth its corresponding input vector matrix, with low computational overhead.

[0051] Optionally, the first module list can be associated with a specific type of content generation model. Different statistical lists can be set for different types of content generation models, or the same module list can be used to count modules requiring smoothing for multiple different types of content generation models. Furthermore, it can be maintained based on experience in performing inference tasks for various content generation models, such as periodically maintaining the modules included in the corresponding module list. Specifically, if the quantization error of each feature extraction module is determined to be greater than or equal to the aforementioned first error threshold, the corresponding feature extraction module can be added to the corresponding statistical list. Optionally, the corresponding feature extraction module can be added to the corresponding statistical list only if the same result is determined in multiple model inferences.

[0052] Further optionally, in response to determining that the identifier of the first feature extraction module is located in the first module list, the first feature extraction module can be identified as the module to be smoothed.

[0053] (2) If the quantization error corresponding to the first feature extraction module is greater than or equal to the second error threshold, the first feature extraction module is determined to be a module to be smoothed. Conversely, if the quantization error corresponding to the first feature extraction module is less than the second error threshold, it can be determined that it is not a module to be smoothed.

[0054] In this embodiment, by determining the relationship between the real-time quantization error of the current feature extraction module (i.e., the first feature extraction module) and its corresponding error threshold in the content generation model, it is possible to evaluate in real time whether the current feature extraction module belongs to the module to be smoothed. This results in better fit with the content generation task currently being performed by the content generation model, and is more conducive to improving the accuracy of the generated content. Optionally, the quantization error value corresponding to each feature extraction module can be obtained by running the content generation model on a small calibration dataset and recording the quantization error module by module. Then, it can be compared with the aforementioned second error threshold to adaptively and dynamically identify sensitive modules according to the actual data distribution.

[0055] It should be noted that the specific values ​​of the first error threshold and the second error threshold mentioned above can be set according to the specific circumstances of the content generation model. They can be the same or different, and no specific limitation is made here.

[0056] Optionally, the quantization error in this embodiment of the disclosure can refer to the degree of deviation or accuracy loss that occurs when the input vector matrix or intermediate calculation result is quantized compared to the high-precision result before quantization. For example, the quantization error can be measured by indicators such as mean absolute error, root mean square error, or maximum error.

[0057] Based on any of the above embodiments, the model inference method provided in this disclosure can also identify modules with high sensitivity to quantization error that do not require quantization processing in one of the following ways, so as to avoid a decrease in the quality of generated content due to loss of accuracy: determining a second feature extraction module with a quantization error greater than or equal to a third error threshold from the content generation model, or determining a second feature extraction module located in the second module list from the content generation model, wherein the second feature extraction module belongs to the modules that do not require quantization processing.

[0058] In this embodiment, it is possible to identify whether each feature extraction module in the content generation model needs quantization processing. On one hand, based on the relationship between the real-time quantization error corresponding to each feature extraction module and the corresponding error threshold (i.e., the third error threshold), it can be determined in real time whether the current feature extraction module belongs to the module that needs quantization processing. Specifically, when the real-time quantization error corresponding to each feature extraction module exceeds the corresponding error threshold, it indicates that its numerical distribution is more sensitive to quantization processing, and quantization operation will introduce precision loss. Therefore, this module should be classified as a feature extraction module that does not need quantization processing. Conversely, if the real-time quantization error corresponding to each feature extraction module is less than the third error threshold, it belongs to the feature extraction module that does not need quantization processing. Alternatively, on the other hand, based on a pre-compiled list of feature extraction modules that need quantization processing (i.e., the second module list), it is possible to accurately and efficiently determine whether each feature extraction module in the content generation model belongs to the module that needs quantization processing by looking up the table. If it can be found in the table, it means that the corresponding feature extraction module needs quantization processing; if it cannot be found, it means that the module does not need quantization processing. The second module list can be a pre-built set of identifiers recording feature extraction modules deemed highly sensitive to quantization. Optionally, it can be obtained through offline experiments or empirical analysis. For example, during the development phase, low-precision quantization can be applied to each module or layer of the model, and its impact on the quality of the final generated content can be evaluated. Modules that cause a decrease in the quality of the generated content can be recorded in the list. Optionally, the second module list can be associated with a fourth error threshold, that is, the fourth error threshold can be used as a threshold to pre-screen whether modules in the content generation model need to undergo quantization processing.

[0059] Further, optionally, attention calculation can be performed using different processing methods based on the judgment result of whether the current feature extraction module of the content generation model, i.e., the first feature extraction module, belongs to the module with high sensitivity to quantization error. This allows the model to achieve a better balance between inference speed (i.e., efficiency) and accuracy. For example, the steps described above, such as determining the first output vector matrix based on the smoothed vector matrix through the self-attention layer, can be used to achieve this. Figure 1 Step 103 in the corresponding embodiment or Figure 2 Step 204 in the corresponding embodiment can be executed as follows: (1) When the second feature extraction module includes the first feature extraction module, the first output vector matrix is ​​determined by the self-attention layer based on the smoothed vector matrix and using the first numerical precision format.

[0060] In this embodiment, if it is determined that the current feature extraction module of the content generation model, i.e., the first feature extraction module, is a module with high sensitivity to quantization errors and does not require quantization processing, high-precision attention calculation can be performed on the self-attention layer of the first feature extraction module based on the smoothed vector matrix. That is, the first output vector matrix is ​​determined using a first numerical precision format. This reduces the quantization error peak in the highly sensitive feature extraction module or layer with minimal computational and storage overhead, thereby preventing error accumulation, maintaining the quality of model inference, and especially ensuring the spatiotemporal consistency of content generation, thus achieving a better precision-efficiency balance overall. The first numerical precision format may include, but is not limited to, BF16, FP16 full-precision formats, etc.

[0061] Optionally, in other alternative implementations, where the second feature extraction module includes the first feature extraction module and the first feature extraction module is not a module to be smoothed, the self-attention layer can determine the corresponding output vector matrix based on the input vector matrix using the first numerical precision format.

[0062] (2) In the case that the second feature extraction module does not include the first feature extraction module, the first output vector matrix is ​​determined by the self-attention layer based on the smoothed vector matrix and using the second numerical precision format, wherein the bit width corresponding to the second numerical precision format is smaller than the bit width corresponding to the first numerical precision format.

[0063] In this embodiment, if it is determined that the current feature extraction module of the content generation model, i.e., the first feature extraction module, is not a module with high sensitivity to quantization errors and requires quantization processing, low-precision attention calculation can be performed based on the smoothed vector matrix through the self-attention layer of the first feature extraction module. That is, the first output vector matrix is ​​determined using a second numerical precision format with a bit width or data width smaller than the first numerical precision format mentioned above, which significantly reduces computational complexity and inference latency, and can also reduce memory usage. The second numerical precision format may include, but is not limited to, FP8 full-precision format, 8-bit signed integer type (INT8), etc.

[0064] Optionally, in other alternative implementations, if the second feature extraction module does not include the first feature extraction module and the first feature extraction module is not a module to be smoothed, the self-attention layer can determine the corresponding output vector matrix based on the input vector matrix using the second numerical precision format.

[0065] It should be noted that the specific values ​​of the third and fourth error thresholds can be set according to the specific circumstances of the content generation model, and no specific limitation is made here. They can be the same or different, and no specific limitation is made here. Also, the third error threshold can be different from the second error threshold mentioned above, and the fourth error threshold can be different from the first error threshold mentioned above, so as to determine whether each feature extraction module needs to be smoothed and quantized based on different quantization error sensitivities.

[0066] Optionally, in this embodiment of the disclosure, the quantization error value corresponding to each feature extraction module used for comparison with the first to fourth error thresholds can correspond to a low-bit-width numerical precision format (such as FP8), that is, the error corresponding to low-precision or low-bit quantization (such as FP8 attention quantization).

[0067] In some optional implementations of any of the above embodiments disclosed herein, the input vector matrix may include, but is not limited to, the query vector matrix Q and the key vector matrix K. The smoothed vector matrix includes the smoothed query vector matrix and the smoothed key vector matrix, that is, at least the query vector matrix and the key vector matrix can be smoothed to reduce quantization error, eliminate outlier dominance, etc., wherein the smoothed query vector matrix and the smoothed key vector matrix may be obtained by smoothing based on the above channel dimension smoothing value.

[0068] Optionally, the process described above, where the self-attention layer determines the first output vector matrix based on the smoothed vector matrix using a second numerical precision format, can be implemented by the following process when the first feature extraction module does not require quantization: This allows for a shift from higher to lower precision, achieving more stable error convergence through quantization and further enabling attention calculation. Specifically, the self-attention layer of the first feature extraction module uses the second numerical precision format to quantize the smoothed query vector matrix and the smoothed key vector matrix, respectively, to obtain quantized query vector matrices and quantized key vector matrices. The self-attention layer then determines the attention score matrix based on these matrices. Finally, the self-attention layer determines the first output vector matrix based on the attention score matrix. The attention score matrix can characterize the matching degree between the query vector and the key vector. For example, it can be obtained by performing a dot product operation on the transpose of the quantized query vector matrix and the quantized key vector matrix. This reduces data storage and computational overhead, and improves the efficiency of interactive computation based on query and key vectors while ensuring numerical precision.

[0069] In some optional implementations of the embodiments of this disclosure, the process of using the self-attention layer of the first feature extraction module to quantize the smoothed query vector matrix and the smoothed key vector matrix respectively using the second numerical precision format to obtain the quantized query vector matrix and the quantized key vector matrix can be executed as follows: using the self-attention layer to quantize the smoothed query vector matrix and the smoothed key vector matrix respectively according to the first dimension using the second numerical precision format to obtain the quantized query vector matrix and the quantized key vector matrix, wherein the first dimension may include, but is not limited to, per-block dimension, per-group dimension, or per-thread dimension.

[0070] In this embodiment, the self-attention layer of the current feature extraction module of the content generation model, namely the first feature extraction module, can adopt a second numerical precision format with low bit width or data width. The smoothed query vector matrix and the smoothed key vector matrix can be quantized according to each block dimension, each group dimension, or each thread dimension to obtain the corresponding quantized query vector matrix and quantized key vector matrix. In this low numerical precision quantization process, such as FP8 quantization, better or optimal precision and performance can be obtained. That is, while ensuring computational efficiency, the quantization operation can better adapt to the numerical distribution characteristics inside the vector matrix.

[0071] Optionally, the first dimension used to quantize the vector matrix can refer to the organization method used to divide quantization units or granularities during the quantization process. Different dimension selections correspond to different quantization granularities. Specifically, when the first dimension adopts a per-block dimension, the smoothed vector matrix can be divided into multiple non-overlapping blocks during the quantization process or operation. Each block shares the same set of quantization parameters, such as scaling factors and zeros. The size of the block can be pre-set, and it can be a rectangular block obtained by dividing according to sequence length and / or channel dimension, so as to capture local statistical characteristics at the block level. Compared with the per-tensor granularity of a single quantization parameter for the entire matrix, it can reduce quantization noise caused by uneven numerical distribution.

[0072] When the first dimension uses each group of dimensions, the quantization unit or granularity can be a continuous group of values ​​divided according to a pre-set fixed step size. Each group shares the same set of quantization parameters. For example, elements can be grouped along a single direction of the vector matrix, i.e., the sequence length direction or the channel direction, and the number of elements in each group is fixed.

[0073] When the first dimension is per-thread, the quantization process or operation can be performed by having each parallel thread independently handle a portion of the elements in the matrix. Quantization based on the per-thread dimension has strong adaptability and can fit the local changes in the data to the greatest extent.

[0074] In some optional implementations of the embodiments of this disclosure, when the first dimension is a block dimension, the process of quantizing the smoothed query vector matrix and the smoothed key vector matrix according to the first dimension using a second numerical precision format through a self-attention layer to obtain quantized query vector matrices and quantized key vector matrices can be executed as follows: For multiple candidate sequence lengths, the smoothed query vector matrix and the smoothed key vector matrix are quantized according to each block dimension using a second numerical precision format through a self-attention layer to obtain candidate query vector matrices and candidate key vector matrices corresponding to each of the multiple candidate sequence lengths; the candidate quantization error corresponding to each of the multiple candidate sequence lengths is determined by the self-attention layer based on the candidate query vector matrix and the candidate key vector matrix; the quantized query vector matrix and the quantized key vector matrix are determined by the self-attention layer based on the candidate quantization error from the candidate query vector matrix and the candidate key vector matrix, respectively.

[0075] Alternatively, the process described above, which uses a self-attention layer to determine the quantized query vector matrix and the quantized key vector matrix from the candidate query vector matrix and the candidate key vector matrix based on the candidate quantization error, can be executed as follows: the candidate query vector matrix and the candidate key vector matrix corresponding to the smallest candidate quantization error are determined as the quantized query vector matrix and the quantized key vector matrix described above.

[0076] In this embodiment, during the quantization process of each vector matrix according to each block dimension, an adaptive selection mechanism based on the candidate sequence length (or candidate block size) can be used to determine the sequence length with the best quantization effect, which is the candidate sequence length corresponding to the quantized query vector matrix and the quantized key vector matrix. Specifically, for each candidate sequence length, the self-attention layer uses the second numerical precision format to perform block-by-block quantization on the smoothed query vector matrix and the smoothed key vector matrix according to each candidate sequence length, thereby obtaining the candidate query vector matrix and the candidate key vector matrix corresponding to each candidate sequence length. Based on this, the candidate quantization error corresponding to each candidate sequence length is further evaluated, and the optimal set of candidate query vector matrices and candidate key vector matrices is selected from these candidate quantization errors as the final quantized query vector matrix and quantized key vector matrix used for subsequent calculations.

[0077] Thus, by polling the lengths of multiple candidate sequences and quantizing each candidate sequence length according to each dimension through the self-attention layer, the corresponding quantization error, i.e., the quantization error of each candidate, can be obtained. Then, the candidate sequence length with the smallest error can be accurately selected, and the corresponding quantized candidate query vector matrix and candidate key vector matrix can be determined as the final quantized query vector matrix and quantized key vector matrix, so as to further reduce the quantization error and achieve a balance between accuracy and efficiency.

[0078] It should be noted that the length of the above candidate sequences can be set according to actual needs, such as values ​​of 16, 32, 64, 128, etc., and no specific limit is made here.

[0079] In some optional implementations of the embodiments of this disclosure, the aforementioned channel dimension smoothing value may include, but is not limited to, the first channel dimension smoothing value corresponding to the query vector matrix and the second channel dimension smoothing value corresponding to the key vector matrix; and the aforementioned process of determining the attention score matrix by the self-attention layer based on the quantized query vector matrix and the quantized key vector matrix may be executed as follows: the result of the dot product calculation of the quantized query vector matrix and the quantized key vector matrix by the self-attention layer is dequantized to obtain the dequantized vector matrix; the self-attention layer performs a precision compensation operation on the dequantized vector matrix based on the first channel dimension smoothing value, the second channel dimension smoothing value, and the key vector matrix; and the self-attention layer determines the attention score matrix based on the channel size corresponding to the query vector matrix and the result of the precision compensation operation on the dequantized vector matrix.

[0080] In this embodiment, to obtain a high-precision intermediate value that can be used for subsequent numerical calibration, the result of the dot product calculation of the quantized query vector matrix and the quantized key vector matrix can be dequantized through a self-attention layer to obtain a dequantized vector matrix. Dequantization is the inverse operation of quantization, which can restore the low-bit discrete representation to a high-precision floating-point number, thus recovering an approximation close to the original numerical range. Furthermore, the dequantized vector matrix corresponds to an approximation of the result of the dot product calculation of the unsmoothed query vector and key vector matrix. Further, to eliminate the systematic bias introduced by smoothing, a precision compensation operation can be performed on the dequantized vector matrix to ensure the numerical accuracy of the attention score matrix calculation. Thus, through precision compensation in dequantization and further quantization precision compensation based on the smoothed values ​​of the first channel dimension, the smoothed values ​​of the second channel dimension, and the key vector matrix, quantization errors can be reduced, achieving high-fidelity, low-bit attention quantization with improved accuracy.

[0081] In the above Figure 1 or Figure 2 Based on the corresponding embodiments, in some optional implementations of the embodiments of this disclosure, the above model inference method may further include the following: in response to determining that the first feature extraction module belongs to a module that does not require smoothing based on the quantization error corresponding to the first feature extraction module, a second output vector matrix is ​​determined by a self-attention layer based on the input vector matrix, wherein the second output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating the target content.

[0082] In this embodiment, when it is determined that the current feature extraction module (i.e., the first feature extraction module) belongs to a module that does not require smoothing based on the quantization error, i.e., when it is not necessary to smooth the corresponding input vector matrix through the self-attention layer in the feature extraction module, the output vector matrix (i.e., the second output vector matrix) of the feature extraction module can be determined by the self-attention layer based on the unsmoothed vector matrix (i.e., the initial input vector matrix). This second output vector matrix can be used to determine the output feature sequence of the feature extraction module for generating the final clear target content. Thus, by avoiding unnecessary processing when smoothing is not required, unnecessary computational overhead and data distortion are avoided.

[0083] Optionally, in the embodiments of this disclosure, the aforementioned corresponding feature extraction module needs to smooth the input vector matrix, indicating that the execution of the smoothing process is effective in reducing the content quality degradation caused by the accumulation of quantization errors; while the aforementioned corresponding feature extraction module does not need to smooth the input vector matrix, indicating that the execution of the smoothing process is not effective or ineffective in reducing the content quality degradation caused by the accumulation of quantization errors.

[0084] In some optional implementations of the embodiments of this disclosure, the input vector matrix further includes a value vector matrix; and the process of determining the first output vector matrix by the self-attention layer based on the attention score matrix can be executed as follows: the attention score matrix is ​​normalized by the self-attention layer to obtain the attention probability matrix; and the first output vector matrix is ​​determined by the self-attention layer based on the attention probability matrix and the value vector matrix.

[0085] In this embodiment, the input vector matrix may include a value vector matrix in addition to the query vector matrix and key vector matrix. Each row or position of the value vector matrix corresponds to a feature representation of that position in the input feature sequence. To unify the scale or numerical range and avoid gradient saturation or numerical instability due to excessively large or small scores, the attention score matrix determined based on the quantized query vector matrix and the quantized key vector matrix can be normalized. For example, a softmax function can be used to obtain a normalized result with values ​​between 0 and 1, resulting in an attention probability matrix. This matrix, combined with the value vector matrix, can then be used to determine the required output vector matrix, i.e., the first output vector matrix. Optionally, matrix multiplication can be performed between the attention probability matrix and the value vector matrix. This involves weighting and aggregating the information in the value vector matrix according to the values ​​in the attention probability matrix, so that each position in the output vector matrix incorporates information from all positions in the input feature sequence. This allows the content generation model to extract the most relevant features from the input. Thus, by normalizing the attention score matrix obtained through the above process during the process of obtaining the output vector matrix of the current feature extraction module, i.e. the first feature extraction module, of the content generation model, the stable calculation of attention can be guaranteed.

[0086] In some optional implementations of the embodiments of this disclosure, the process of determining the first output vector matrix by the self-attention layer based on the attention probability matrix and the value vector matrix can be executed as follows: quantizing the attention probability matrix by the self-attention layer according to a scaling factor to obtain a quantized attention probability matrix; quantizing the value vector matrix by the self-attention layer according to a second dimension to obtain a quantized value vector matrix, wherein the second dimension may include, but is not limited to, per-channel dimension, per-head dimension, or per group dimension; and determining the first output vector matrix by the self-attention layer based on the quantized attention probability matrix and the quantized value vector matrix.

[0087] In this embodiment, an optional quantization acceleration scheme is provided during the further processing of the attention probability matrix and value vector matrix to obtain the first output vector matrix. Specifically, the attention probability matrix can be quantized according to a scaling factor to obtain a quantized attention probability matrix. The scaling factor can be a pre-defined constant, such as 448 determined experimentally. After scaling, the dynamic range of the attention probability matrix is ​​expanded. Performing quantization operations on this basis (e.g., converting to FP8_E4M3 format, where E refers to the exponent and M refers to the mantissa) can retain more detailed information at a low bit width. Simultaneously, the value vector matrix can also be quantized according to a second dimension to obtain a quantized value vector matrix, ensuring information integrity. When using a per-channel dimension, quantization parameters can be independently calculated for each channel of the value vector matrix during quantization processing or operations, enabling fine-grained calibration of the numerical distribution for each channel. When using a per-attention head dimension, the quantization unit or granularity can be a submatrix corresponding to each attention head. Then, the required first output vector matrix can be determined based on the quantized attention probability matrix and the quantized value vector matrix. For example, matrix multiplication can be performed on the quantized attention probability matrix and the quantized value vector matrix to further accelerate model inference.

[0088] In some optional implementations of the embodiments of this disclosure, the combination of quantization granularities corresponding to each matrix can be optimized by performing quantization processing on the query vector matrix and the key vector matrix according to a first dimension, such as preferred per-block, performing quantization processing on the value vector matrix according to a second dimension, such as preferred per-channel, and performing quantization processing on the attention probability matrix according to a scaling factor, so as to significantly reduce quantization error and improve attention quantization performance and accuracy.

[0089] Alternatively, in the above scheme where the first feature extraction module is a module that does not require smoothing, the second output vector matrix is ​​determined by the self-attention layer based on the input vector matrix. In addition to eliminating the steps related to smoothing, such as the smoothing process based on the channel dimension smoothing value, the operation of precision compensation of the dequantized vector matrix can also be omitted in the process of determining the attention score matrix. Other operations related to determining the output vector matrix can refer to the process of determining the first output vector matrix, and will not be repeated here.

[0090] To enhance understanding, this disclosure also provides a specific implementation scheme for the model inference method in a concrete application scenario. Please refer to [example example]. Figure 3The process 300 shown uses a content generation model as the video generation model, specifically a Transformer block in a DiT-based video diffusion model (such as...). Figure 3 The process, illustrated by the dashed box in the diagram (where N can be repeated N times to represent N layers, and N is an integer greater than 1), is explained using this example. This process 300 includes, but is not limited to, the following: Step 301: Generate Q, K, and V through linear layer transformation. That is, when transmitting or sending the input feature sequence to the linear layer of the Transformer block (including or corresponding to the feature extraction module in the above embodiment), the input vector matrix Q, K, and V corresponding to the input feature sequence can be generated through linear layer transformation.

[0091] Step 302: Calculate the output vector matrix O through the self-attention layer of the Transformer block.

[0092] Optionally, in video diffusion models, the sensitivity of self-attention layers in different Transformer blocks to quantization errors varies significantly. Therefore, a layer-wise quantization strategy based on quantization error can be derived through quantization error performance analysis or profiling, including: (1) Calculate the quantization error of each FP8 layer (for example, the widely used l1-norm and root mean square error (RMSE) indicators can be used), including: avg error (average error, i.e., the average error in tensor) and max (maximum) error (the maximum error in tensor).

[0093] (2) Identify highly sensitive layers based on the FP8 quantization error of each layer, and obtain the identity (id) of the sensitive layer according to the layer-wise index analysis, such as layers 0, 33, 35, and 38 (corresponding to the second feature extraction module in the above embodiment).

[0094] (3) Select the most sensitive L layer (i.e., the top-L sensitive layer), retain the BF16 full precision (corresponding to the first numerical precision format in the above embodiment) for attention calculation, which does not require quantization, and use FP8 (corresponding to the second numerical precision format in the above embodiment) for attention calculation, which requires quantization.

[0095] Optionally, in FP8 attention computation, a mean-based Q / K smoothing method can be adopted, that is, a layer-sensitivity-aware per-channel mean smoothing mechanism is proposed, which realizes the dynamic configuration of smoothing strategies at the block or layer level to reduce the error amplification effect of FP8 quantization on the Q / K distribution of different layers.

[0096] Thus, for video generation models, such as the DiT architecture's textual video model, multiple highly sensitive layers, such as layers 0, 33, 38, and 16 during inference (corresponding to the modules to be smoothed in the above embodiments), can be considered. Given that the channel distribution of Q / K in these layers is more prone to generating quantization error peaks, attention calculation based on per-channel mean smoothing dynamically adjusted at the layer or block granularity can be performed. The per-channel mean is calculated for each layer's Q / K. Then, precision compensation is performed on Q and K by adding a General Matrix-Vector (GEMV) operation, i.e., subtracting μ in the following formula. per-channel ():

[0097] Where, μ per-channel This indicates that the average value is calculated along the channel dimension to obtain a channel-level smoothed value. This represents the quantized query vector matrix. This represents the quantized bond vector matrix, ΔS=μ per-channel (Q)×(K-μ per-channel (K)) T To compensate for accuracy, smoothing can be optionally replaced by other decentralized methods, such as low-dimensional linear transformations subtracting the mean or median, multiplying by a rotation matrix, etc., or high-dimensional linear transformations such as Hadmar's transform, to smooth out outliers. Here, the `quant` operation is the quantization operation; the granularity of this operation needs to be determined, and it can be per-channel, per-tensor, or per-block. `dequant` is the dequantization operation corresponding to `quant`. In the last two steps, the optional step is to quantize P and V, such as... Figure 5As shown, the attention calculation process without quantization of P and V corresponds to the basic configuration of FP8 attention quantization, aiming to preserve generation accuracy. Figure 6 The diagram illustrates the attention calculation process for quantizing P and V, corresponding to the advanced FP8 attention quantization configuration, designed to improve performance. The quantization operation on P yields the quantized attention probability matrix. During the process, a fixed scaling configuration can be used, for example, for... =(448 P).to(FP8_E4M3) is used to obtain the quantized value vector matrix for quantization of V. During the process, per-channel quantization granularity can be used, or it can be replaced with per-head or per-group to balance performance and accuracy.

[0098] Optionally, different smoothing strategies can be selected based on the error sensitivity of the current layer or Transformer block (driven by the quantization error calculated in real time or a pre-set experience table for statistically analyzing layers that need to be quantized), such as whether to perform smoothing, and by which dimension to perform smoothing.

[0099] In this embodiment, per-block quantization after Q / K smoothing can significantly reduce the average error without increasing the maximum error. Specifically, when performing per-block quantization on Q / K, the method of using per-block quantization can be referred to as follows: Figure 4 The process shown in 400 selects the block size with the smallest error for quantization, including the following: Step 401: Select a specific value from the candidate block size (corresponding to the candidate sequence length in the above embodiments). For example, the candidate block size may include values ​​such as 16, 32, 64, 128, etc., which can be set according to the specific situation.

[0100] Step 402: Calculate the error corresponding to Q and K by block dimension quantization.

[0101] Step 403: Based on the error under the current block size, compare it with the current minimum error. If the error under the current block size is less than the current minimum error, update the current minimum error and record the current block size.

[0102] Step 404: Iterate through all candidate block sizes, select the block size corresponding to the smallest error, and quantize Q and K by block dimension.

[0103] Optionally, for quantization of Q and K, per-block can be replaced with different granularities, such as per-group or per-thread.

[0104] Thus, by significantly reducing Q / K FP8 quantization error, the peak quantization error in highly sensitive layers can be significantly reduced, and a more stable error convergence trend can be observed in FP8 attention calculations for various layers or Transformer blocks. Furthermore, the memory usage for attention calculations during video generation can be reduced, improving the stability of attention calculations in low-bit quantization such as FP8. By constructing a unified Q / K / V / P quantization combination, both quantization error and model inference performance can be optimized, thereby improving end-to-end accuracy of generated videos, reducing accuracy loss, and increasing video generation efficiency. Lightweight algorithms rather than structural modifications also improve the model's deployability and adaptability.

[0105] Step 303: Perform MLP computation, that is, transmit the output vector matrix O to the MLP layer for computation and generate the output feature sequence for output.

[0106] Optionally, the output feature sequence can be used as the input feature sequence for the next Transformer block.

[0107] Further reference Figure 7 As an implementation of the methods shown in the above figures, this disclosure provides an embodiment of a model inference device, which is similar to... Figure 1 Corresponding to the method embodiments shown, the device can be specifically applied to various electronic devices (e.g., servers).

[0108] like Figure 7 As shown, the model reasoning apparatus 700 of this embodiment may include: a processing unit 701, a generation unit 702, and a determination unit 703.

[0109] The processing unit 701 is configured to convert the input feature sequence corresponding to the first feature extraction module in the content generation model into an input vector matrix; the generation unit 702 is configured to, in response to determining that the first feature extraction module belongs to the module to be smoothed based on the quantization error corresponding to the first feature extraction module, input the input vector matrix into the self-attention layer of the first feature extraction module for smoothing to generate a smoothed vector matrix; the determining unit 703 is configured to determine a first output vector matrix based on the smoothed vector matrix through the self-attention layer, wherein the first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

[0110] In this embodiment of the disclosure, the specific processing of the processing unit 701, the generation unit 702, and the determination unit 703 in the model inference device 700, and the resulting technical effects, can be referred to respectively. Figure 1 The relevant descriptions of steps 101-103 in the corresponding embodiments will not be repeated here.

[0111] In some optional implementations of the embodiments of this disclosure, the determining unit 703 may further be configured to: in response to determining that the first feature extraction module belongs to a module that does not require smoothing based on the quantization error corresponding to the first feature extraction module, determine a second output vector matrix through a self-attention layer based on the input vector matrix, wherein the second output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

[0112] In some optional implementations of the embodiments of this disclosure, the model inference device 700 may further include a first identification unit (not shown in the figure), which is configured to: determine the first feature extraction module as a module to be smoothed when the first feature extraction module is located in the first module list, wherein the first module list is used to count modules whose quantization error is greater than or equal to a first error threshold; or determine the first feature extraction module as a module to be smoothed when the quantization error corresponding to the first feature extraction module is greater than or equal to a second error threshold.

[0113] In some optional implementations of the embodiments of this disclosure, the generation unit 702 may be further configured to: process the input vector matrix in terms of channel dimension through a self-attention layer to obtain a channel dimension smoothing value; and smooth the input vector matrix based on the channel dimension smoothing value through a self-attention layer to generate a smoothed vector matrix.

[0114] In some optional implementations of the embodiments of this disclosure, the model inference device 700 may further include a second identification unit (not shown in the figure), which is configured to: determine a second feature extraction module from the content generation model whose quantization error is greater than or equal to a third error threshold, or determine a second feature extraction module from the content generation model that is located in a second module list, wherein the second feature extraction module is a module that does not require quantization processing; and the determination unit 703 may further be configured to: when the second feature extraction module includes the first feature extraction module, determine a first output vector matrix using a first numerical precision format based on a smoothed vector matrix through a self-attention layer; or when the second feature extraction module does not include the first feature extraction module, determine a first output vector matrix using a second numerical precision format based on a smoothed vector matrix through a self-attention layer, wherein the bit width corresponding to the second numerical precision format is smaller than the bit width corresponding to the first numerical precision format.

[0115] In some optional implementations of the embodiments of this disclosure, the input vector matrix includes a query vector matrix and a key vector matrix, and the smoothed vector matrix includes a smoothed query vector matrix and a smoothed key vector matrix; and the determining unit 703 may further be configured to: quantize the smoothed query vector matrix and the smoothed key vector matrix respectively by using a second numerical precision format through a self-attention layer to obtain a quantized query vector matrix and a quantized key vector matrix; determine an attention score matrix based on the quantized query vector matrix and the quantized key vector matrix through a self-attention layer; and determine a first output vector matrix based on the attention score matrix through a self-attention layer.

[0116] In some optional implementations of the embodiments of this disclosure, the determining unit 703 may further be configured to: quantize the smoothed query vector matrix and the smoothed key vector matrix respectively according to the first dimension by using the second numerical precision format through the self-attention layer, to obtain the quantized query vector matrix and the quantized key vector matrix, wherein the first dimension includes each block dimension, each group dimension or each thread dimension.

[0117] In some optional implementations of the embodiments of this disclosure, the determining unit 703 may further be configured to: for multiple candidate sequence lengths, quantize the smoothed query vector matrix and the smoothed key vector matrix according to each block dimension using a second numerical precision format through a self-attention layer to obtain candidate query vector matrices and candidate key vector matrices corresponding to each of the multiple candidate sequence lengths; determine the candidate quantization error corresponding to each of the multiple candidate sequence lengths through a self-attention layer based on the candidate query vector matrix and the candidate key vector matrix; and determine the quantized query vector matrix and the quantized key vector matrix from the candidate query vector matrix and the candidate key vector matrix respectively through a self-attention layer based on the candidate quantization error.

[0118] In some optional implementations of the embodiments of this disclosure, the aforementioned channel dimension smoothing value includes a first channel dimension smoothing value corresponding to the query vector matrix and a second channel dimension smoothing value corresponding to the key vector matrix; and the aforementioned determining unit 703 can further be configured to: perform dequantization processing on the result of the dot product calculation of the quantized query vector matrix and the quantized key vector matrix through the self-attention layer to obtain a dequantized vector matrix; perform a precision compensation operation on the dequantized vector matrix based on the first channel dimension smoothing value, the second channel dimension smoothing value, and the key vector matrix through the self-attention layer; and determine the attention score matrix based on the channel size corresponding to the query vector matrix and the result of the precision compensation operation on the dequantized vector matrix through the self-attention layer.

[0119] In some optional implementations of the embodiments of this disclosure, the input vector matrix further includes a value vector matrix; and the determining unit 703 may further be configured to: normalize the attention score matrix through a self-attention layer to obtain an attention probability matrix; and determine a first output vector matrix through a self-attention layer based on the attention probability matrix and the value vector matrix.

[0120] In some optional implementations of the embodiments of this disclosure, the determining unit 703 may further be configured to: quantize the attention probability matrix according to a scaling factor through a self-attention layer to obtain a quantized attention probability matrix; quantize the value vector matrix according to a second dimension through a self-attention layer to obtain a quantized value vector matrix, wherein the second dimension includes a per-channel dimension, a per-attention head dimension, or a per-group dimension; and determine a first output vector matrix through a self-attention layer based on the quantized attention probability matrix and the quantized value vector matrix.

[0121] In some optional implementations of the embodiments of this disclosure, the above content generation model includes a diffusion model based on the DiT architecture of the diffusion converter.

[0122] This embodiment exists as a model inference device 700 embodiment corresponding to the above method embodiment. The key steps or equivalent technical means that can be implemented by each component in the model inference device 700 embodiment provided in this embodiment, as well as the technical effects brought about by this device embodiment, can be referred to the relevant descriptions in the above method embodiment, and will not be repeated here.

[0123] According to embodiments of the present disclosure, the present disclosure also provides an electronic device, the electronic device comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to implement the model inference method described in any of the above embodiments when executed.

[0124] According to embodiments of this disclosure, this disclosure also provides a non-transitory computer-readable storage medium storing computer instructions that enable a processor to implement the model inference method described in any of the above embodiments when executed.

[0125] According to embodiments of this disclosure, this disclosure also provides a computer program product comprising a computer program that, when executed by a processor, can implement the model reasoning method described in any of the above embodiments.

[0126] Figure 8A schematic block diagram of an example electronic device 800 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the present disclosure described and / or claimed herein.

[0127] like Figure 8 As shown, the electronic device 800 includes a computing unit 801, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 802 or a computer program loaded from a storage unit 808 into a random access memory (RAM) 803. The RAM 803 may also store various programs and data required for the operation of the electronic device 800. The computing unit 801, ROM 802, and RAM 803 are interconnected via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0128] Multiple components in electronic device 800 are connected to I / O interface 805, including: input unit 806, such as keyboard, mouse, etc.; output unit 807, such as various types of displays, speakers, etc.; storage unit 808, such as disk, optical disk, etc.; and communication unit 809, such as network card, modem, wireless transceiver, etc. Communication unit 809 allows electronic device 800 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0129] The computing unit 801 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 801 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 801 performs the various methods and processes described above, such as model inference methods. For example, in some embodiments, the model inference method may be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 808. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 800 via ROM 802 and / or communication unit 809. When the computer program is loaded into RAM 803 and executed by the computing unit 801, one or more steps of the model inference method described above may be performed. Alternatively, in other embodiments, the computing unit 801 may be configured to perform model inference methods by any other suitable means (e.g., by means of firmware).

[0130] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0131] The program code used to implement the methods of this disclosure may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing apparatus, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0132] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0133] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0134] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as a data server), or computing systems that include middleware components (e.g., an application server), or computing systems that include frontend components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), and the Internet.

[0135] Computer systems can include clients and servers. Clients and servers are generally geographically separated and typically interact via communication networks. Client-server relationships are created by computer programs running on the respective computers and having a client-server relationship with each other. Servers can be cloud servers, distributed system servers, or servers incorporating blockchain technology. Cloud servers, also known as cloud computing servers or cloud hosts, are a hosting product within the cloud computing service ecosystem, designed to address the shortcomings of traditional physical hosts and Virtual Private Servers (VPS) services, such as high management difficulty and weak business scalability.

[0136] According to the technical solution of this disclosure, the input feature sequence corresponding to the current feature extraction module (i.e., the first feature extraction module) in the content generation model can be transformed to obtain the corresponding input vector matrix. In response to the determination that the first feature extraction module belongs to a module requiring smoothing based on the quantization error, the self-attention layer in the feature extraction module smooths the corresponding input vector matrix. Based on the smoothed vector matrix, an output feature vector is obtained to determine the output feature sequence corresponding to the feature extraction module and used to generate the desired content (i.e., the target content). Thus, by selectively smoothing each feature extraction module in the content generation model instead of uniformly applying smoothing to all feature extraction modules, information distortion caused by over-smoothing or under-smoothing is effectively avoided. Simultaneously, precise smoothing is provided for feature extraction modules with urgent smoothing needs, and the required output vector matrix is ​​further determined based on the smoothing result. By selectively smoothing during the generation of the desired content, content quality degradation caused by the accumulation of quantization errors can be reduced, thereby improving the stability of attention calculation, helping to improve inter-frame or temporal consistency, and increasing the accuracy of the final output content of the content generation model.

[0137] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.

[0138] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.

Claims

1. A model reasoning method, characterized in that, include: Convert the input feature sequence corresponding to the first feature extraction module in the content generation model into an input vector matrix; In response to determining that the first feature extraction module belongs to the module to be smoothed based on the quantization error corresponding to the first feature extraction module, the input vector matrix is ​​input into the self-attention layer of the first feature extraction module for smoothing to generate a smoothed vector matrix; The self-attention layer determines a first output vector matrix based on the smoothed vector matrix, wherein the first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

2. The method according to claim 1, wherein, The method further includes: In response to determining that the first feature extraction module is a module that does not require smoothing based on the quantization error corresponding to the first feature extraction module, the self-attention layer determines a second output vector matrix based on the input vector matrix, wherein the second output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating the target content.

3. The method according to claim 1, wherein, The method further includes: When the first feature extraction module is located in the first module list, the first feature extraction module is determined as the module to be smoothed, wherein the first module list is used to statistically analyze modules whose quantization error is greater than or equal to a first error threshold; or If the quantization error corresponding to the first feature extraction module is greater than or equal to the second error threshold, the first feature extraction module is determined as the module to be smoothed.

4. The method according to claim 1, wherein, The step of inputting the input vector matrix into the self-attention layer of the first feature extraction module for smoothing to generate a smoothed vector matrix includes: The input vector matrix is ​​processed by the self-attention layer to obtain a channel dimension smoothing value; The self-attention layer smooths the input vector matrix based on the channel dimension smoothing value to generate the smoothed vector matrix.

5. The method according to claim 4, wherein, The method further includes: A second feature extraction module is determined from the content generation model whose quantization error is greater than or equal to a third error threshold, or is located in the second module list from the content generation model, wherein the second feature extraction module is a module that does not require quantization processing; and The step of determining the first output vector matrix based on the smoothed vector matrix through the self-attention layer includes: When the second feature extraction module includes the first feature extraction module, the first output vector matrix is ​​determined by the self-attention layer based on the smoothed vector matrix using a first numerical precision format; or In the case where the second feature extraction module does not include the first feature extraction module, the first output vector matrix is ​​determined by the self-attention layer based on the smoothed vector matrix using a second numerical precision format, wherein the bit width corresponding to the second numerical precision format is smaller than the bit width corresponding to the first numerical precision format.

6. The method according to claim 5, wherein, The input vector matrix includes a query vector matrix and a key vector matrix, and the smoothed vector matrix includes a smoothed query vector matrix and a smoothed key vector matrix; as well as The step of determining the first output vector matrix using the self-attention layer based on the smoothed vector matrix and employing a second numerical precision format includes: The self-attention layer uses the second numerical precision format to quantize the smoothed query vector matrix and the smoothed key vector matrix respectively, to obtain the quantized query vector matrix and the quantized key vector matrix. The attention score matrix is ​​determined by the self-attention layer based on the quantized query vector matrix and the quantized key vector matrix; The first output vector matrix is ​​determined by the self-attention layer based on the attention score matrix.

7. The method according to claim 6, wherein, The step of quantizing the smoothed query vector matrix and the smoothed key vector matrix using the second numerical precision format through the self-attention layer to obtain the quantized query vector matrix and the quantized key vector matrix includes: The self-attention layer uses the second numerical precision format to quantize the smoothed query vector matrix and the smoothed key vector matrix according to the first dimension, respectively, to obtain the quantized query vector matrix and the quantized key vector matrix, wherein the first dimension includes a block dimension, a group dimension, or a thread dimension.

8. The method according to claim 7, wherein, When the first dimension is each block dimension, the step of quantizing the smoothed query vector matrix and the smoothed key vector matrix according to the first dimension using the second numerical precision format through the self-attention layer to obtain the quantized query vector matrix and the quantized key vector matrix includes: For multiple candidate sequence lengths, the smoothed query vector matrix and the smoothed key vector matrix are quantized according to each block dimension by the self-attention layer using the second numerical precision format, to obtain the candidate query vector matrix and candidate key vector matrix corresponding to each of the multiple candidate sequence lengths; The self-attention layer determines the candidate quantization error corresponding to each of the multiple candidate sequence lengths based on the candidate query vector matrix and the candidate key vector matrix. The self-attention layer determines the quantized query vector matrix and the quantized key vector matrix from the candidate query vector matrix and the candidate key vector matrix, respectively, based on the candidate quantization error.

9. The method according to claim 6, wherein, The channel dimension smoothing value includes the first channel dimension smoothing value corresponding to the query vector matrix and the second channel dimension smoothing value corresponding to the key vector matrix; as well as The step of determining the attention score matrix through the self-attention layer based on the quantized query vector matrix and the quantized key vector matrix includes: The self-attention layer performs dequantization on the result of the dot product calculation of the quantized query vector matrix and the quantized key vector matrix to obtain the dequantized vector matrix. The self-attention layer performs a precision compensation operation on the dequantized vector matrix based on the first channel dimension smoothing value, the second channel dimension smoothing value, and the key vector matrix. The attention score matrix is ​​determined by the self-attention layer based on the channel size corresponding to the query vector matrix and the result of the precision compensation operation performed on the dequantized vector matrix.

10. The method according to claim 6, wherein, The input vector matrix also includes a value vector matrix; as well as The step of determining the first output vector matrix based on the attention score matrix through the self-attention layer includes: The attention score matrix is ​​normalized by the self-attention layer to obtain the attention probability matrix; The first output vector matrix is ​​determined by the self-attention layer based on the attention probability matrix and the value vector matrix.

11. The method according to claim 10, wherein, The step of determining the first output vector matrix through the self-attention layer based on the attention probability matrix and the value vector matrix includes: The attention probability matrix is ​​quantized by the self-attention layer according to a scaling factor to obtain the quantized attention probability matrix. The value vector matrix is ​​quantized according to the second dimension by the self-attention layer to obtain the quantized value vector matrix, wherein the second dimension includes the per-channel dimension, the per-attention head dimension, or the per-group dimension. The self-attention layer determines the first output vector matrix based on the quantized attention probability matrix and the quantized value vector matrix.

12. The method according to any one of claims 1-11, wherein, The content generation model includes a diffusion model based on the DiT architecture of the diffusion converter.

13. A model reasoning device, characterized in that, include: The processing unit is configured to convert the input feature sequence corresponding to the first feature extraction module in the content generation model into an input vector matrix. The generation unit is configured to, in response to determining that the first feature extraction module belongs to the module to be smoothed based on the quantization error corresponding to the first feature extraction module, input the input vector matrix into the self-attention layer of the first feature extraction module for smoothing, and generate a smoothed vector matrix. The determining unit is configured to determine a first output vector matrix based on the smoothed vector matrix through the self-attention layer, wherein the first output vector matrix is ​​used to determine the output feature sequence corresponding to the first feature extraction module for generating target content.

14. An electronic device, characterized in that, include: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the model reasoning method according to any one of claims 1-12.

15. A non-transitory computer-readable storage medium storing computer instructions, characterized in that, The computer instructions are used to cause the processor to execute the model reasoning method according to any one of claims 1-12.

16. A computer program product, characterized in that, It includes a computer program that, when executed by a processor, implements the model reasoning method according to any one of claims 1-12.