Cosine distance calculation unit, method, acceleration unit and electronic device

By designing a cosine distance calculation unit, using the combination of multiplier and adder layers, the problem of insufficient real-time cosine distance calculation in the prior art is solved, and real-time calculation and efficient calculation between vectors are realized.

CN114049506BActive Publication Date: 2025-08-29C SKY MICROSYST CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111349154.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-15
Publication Date
2025-08-29
Estimated Expiration
2041-11-15

AI Technical Summary

Technical Problem

The prior art has the problem of poor real-time performance when calculating the cosine distance between vectors, especially in application scenarios such as face matching and fingerprint matching, which leads to a large delay through offline software calculation.

Method used

A cosine distance calculation unit is adopted, including 2L multipliers and L adder layers. The product of vector elements is calculated by multiplier, and the product is summed by the adder layer to realize the cosine distance calculation between vectors.

Benefits of technology

Real-time calculation of cosine distances between vectors is realized, which improves computing efficiency and reduces dependence on high-performance cloud resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114049506B_ABST
    Figure CN114049506B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a cosine distance calculation unit, method, acceleration unit and electronic device, wherein the cosine distance calculation unit is used to calculate the cosine distance between a first normalized vector and a second normalized vector, including 2 L There are L multipliers and L adder layers. The i-th adder layer includes 2 L‑i The invention provides an i-th adder, each multiplier receiving corresponding vector elements of the first normalized vector and the second normalized vector as input. Each pair of multipliers forms a group corresponding to a first adder, and the outputs of the two multipliers in the group serve as the inputs of the first adder. For the i-th adder layer in the 1st to L-1th adder layers, each pair of i-th adders forms a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder. The cosine distance is determined based on the output of the Lth adder layer. This solution can improve the real-time performance of cosine distance calculation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of chip technology, and in particular to a cosine distance calculation unit, method, acceleration unit, and electronic device. Background Art

[0002] In application scenarios such as face matching and fingerprint matching, it is necessary to calculate the cosine distance (cosine similarity) between the target vector and multiple sample vectors, and then determine one or more sample vectors that match the target vector based on the calculated cosine distance. For example, in a face matching scenario, after obtaining the target user's target face vector, the cosine distance between the target face vector and each sample face vector in the feature library is calculated. Then, based on the calculated cosine distance, the faces corresponding to several sample face vectors with a larger cosine distance from the target face vector are obtained as candidate faces, completing a complete face detection.

[0003] Currently, when calculating the cosine distance between vectors, the cosine distance between two vectors is calculated by offline software.

[0004] However, the method of calculating the cosine distance through offline software has a large delay, resulting in poor real-time performance of the cosine distance calculation. Summary of the Invention

[0005] In view of this, an embodiment of the present application provides a cosine distance calculation solution to at least partially solve the above problem.

[0006] According to a first aspect of the embodiment of the present application, a cosine distance calculation unit is provided for calculating the cosine distance between a first normalized vector and a second normalized vector, comprising: L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, wherein 1≤i≤L, L is a positive integer, each multiplier receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input; the 2 L Every two multipliers in the multipliers are grouped together, corresponding to a first adder, and the outputs of the two multipliers in the group serve as the input of the first adder; for the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped together, corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the input of the i+1th adder; the cosine distance is determined based on the output of the L-th adder layer.

[0007] According to a second aspect of an embodiment of the present application, there is provided an acceleration unit, comprising:

[0008] The cosine distance calculation unit according to any one of the first aspects above;

[0009] The controller is used to control the operation of the cosine distance calculation unit.

[0010] According to a third aspect of the embodiments of the present application, there is provided an electronic device, including:

[0011] The acceleration unit according to any one of the second aspects above;

[0012] The processing unit is configured to normalize the first vector into the first normalized vector, normalize the second vector into the second normalized vector, and schedule the acceleration unit to perform a calculation of a cosine distance between the first normalized vector and the second normalized vector.

[0013] According to a fourth aspect of the embodiment of the present application, a cosine distance calculation method is provided, wherein a cosine distance calculation unit is used to calculate the cosine distance between a first normalized vector and a second normalized vector, and the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, wherein 1≤i≤L, L is a positive integer, and the method includes:

[0014] receiving as input, via each multiplier, a vector element of the first normalized vector and a corresponding vector element of the second normalized vector;

[0015] The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder;

[0016] For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder;

[0017] The cosine distance is determined according to the output of the Lth adder layer.

[0018] According to a fifth aspect of the embodiments of the present application, a matching method is provided, including:

[0019] Acquire first data and second data to be matched, wherein the first data and the second data are one of the following: image data, text data, and voice data;

[0020] Obtaining a first vector generated after performing feature extraction on the first data, and obtaining a second vector generated after performing feature extraction on the second data;

[0021] Normalizing the first vector into a first normalized vector, and normalizing the second vector into a second normalized vector;

[0022] Each multiplier included in the cosine distance calculation unit receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input, wherein the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, where 1≤i≤L, and L is a positive integer;

[0023] The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder;

[0024] For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder;

[0025] determining a cosine distance between the first normalized vector and the second normalized vector according to an output of the Lth adder layer;

[0026] When the cosine distance is less than a preset first distance threshold, it is determined that the first data matches the second data.

[0027] According to a sixth aspect of the embodiments of the present application, a clustering method is provided, including:

[0028] Obtain multiple data to be clustered and cluster centers;

[0029] Determine a first vector corresponding to the cluster center and a second vector corresponding to each of the data to be clustered;

[0030] For each data to be clustered, execute:

[0031] Normalizing the first vector into a first normalized vector, and normalizing the second vector corresponding to the data to be clustered into a second normalized vector;

[0032] Each multiplier included in the cosine distance calculation unit receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input, wherein the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, where 1≤i≤L, and L is a positive integer;

[0033] The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder;

[0034] For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder;

[0035] determining a cosine distance between the first normalized vector and the second normalized vector according to an output of the Lth adder layer;

[0036] If the cosine distance is less than a preset second distance threshold, it is determined that the data to be clustered belongs to the cluster group corresponding to the cluster center.

[0037] According to the cosine distance calculation scheme provided in the embodiment of the present application, since the moduli of the first normalized vector and the second normalized vector are both 1, according to the cosine distance calculation formula, it is only necessary to calculate the product of the corresponding vector elements in the first normalized vector and the second normalized vector, and then sum the products of the corresponding vector elements to obtain the cosine distance between the first normalized vector and the second normalized vector. LMultipliers and L adder layers. Each multiplier receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input, and calculates the product of the corresponding vector elements in the first normalized vector and the second normalized vector. After the products of the corresponding vector elements in the first normalized vector and the second normalized vector are obtained, the products need to be summed. However, the adder can generally only sum two inputs. Therefore, the products are summed through L adder layers to obtain the sum of the products. The adder pair in the first adder layer is used to sum the outputs of the corresponding two multipliers. The product of the outputs of the two multipliers included in each multiplier pair is uniquely reflected in the input of one first adder. The adders in the other adder layers sum the outputs of the corresponding two adders in the previous adder layer. The output of any adder in the previous adder layer is also uniquely reflected in the input of an adder in the current adder layer. In this way, the output of the only adder in the Lth adder layer is the result of summing the outputs of each multiplier. Therefore, after the multiplier multiplies the corresponding vector elements in the first normalized vector and the second normalized vector, and each adder sums the input, the cosine distance between the first normalized vector and the second normalized vector can be determined based on one or more outputs of the adders in the Lth adder layer, thereby realizing real-time calculation of the cosine distance between vectors, thereby improving the real-time performance of the cosine distance calculation without relying on high-performance cloud resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments recorded in the embodiments of the present application. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0039] Figure 1 This is a structural diagram of a data center used in one embodiment of the present application;

[0040] Figure 2 This is a diagram of the internal structure of a server in a data center according to an embodiment of the present application;

[0041] Figure 3 This is a diagram of the internal structure of a processing unit and an acceleration unit within a server according to one embodiment of the present application;

[0042] Figure 4 This is a diagram of the internal structure of a cosine distance calculation unit according to one embodiment of the present application;

[0043] Figure 5 is an internal structure diagram of a cosine distance calculation unit according to another embodiment of the present application;

[0044] Figure 6 This is a diagram of the internal structure of a cosine distance calculation unit according to another embodiment of the present application;

[0045] Figure 7 This is a diagram of the internal structure of a cosine distance calculation unit according to another embodiment of the present application;

[0046] Figure 8 This is a diagram of the internal structure of a cosine distance sorting unit according to one embodiment of the present application;

[0047] Figure 9 This is a diagram showing the internal structure of a cosine distance calculation unit and a cosine distance sorting unit according to an embodiment of the present application;

[0048] Figure 10 This is a flow chart of a cosine distance calculation method according to one embodiment of the present application;

[0049] Figure 11 is a flow chart of a matching method according to an embodiment of the present application;

[0050] Figure 12 This is a flowchart of a clustering method according to an embodiment of the present application. DETAILED DESCRIPTION

[0051] The present application is described below based on examples, but the present application is not limited to these examples. In the detailed description of the present application below, certain specific details are described in detail. Those skilled in the art can fully understand the present application without describing these details. To avoid obscuring the essence of the present application, well-known methods, processes, and procedures are not described in detail. In addition, the drawings are not necessarily drawn to scale.

[0052] The following terms are used in this document.

[0053] Cosine distance: Cosine distance, also known as cosine similarity, measures the similarity between two vectors by calculating the cosine value of the angle between them. The cosine value of the angle between two vectors determines whether the two vectors are roughly pointing in the same direction. When the two vectors have the same direction, the cosine distance is 1, when the angle between the two vectors is 90°, the cosine distance is 0, and when the two vectors point in completely opposite directions, the cosine distance is -1. The cosine distance of two vectors can be calculated using the Euclidean dot product formula. Specifically, for two vectors A and B, the cosine distance between vector A and vector B can be calculated using the following cosine distance calculation formula:

[0054]

[0055] Where similarity is the cosine distance between vector A and vector B, θ is the angle between vector A and vector B, ||A|| is the length of vector A, ||B|| is the length of vector B, n is the number of vector elements in vector A and vector B, A i is the i-th vector element in vector A, B i is the i-th vector element in vector B.

[0056] Vector element: A vector element is a component or coordinate in a vector. The number of vector elements in a vector is equal to the number of dimensions of the vector. For a two-dimensional vector a = (x, y), x and y are two vector elements of vector a. For an n-dimensional vector X = (X1, X2, ... X n ), X1, X2, ... X n is the n vector elements of vector X.

[0057] Normalized vector: A normalized vector is a vector obtained by normalizing the original vector. The modulus of the normalized vector is 1, that is, the length of the normalized vector is 1. The ratio between the vector elements in the normalized vector is equal to the ratio between the vector elements in the original vector.

[0058] Adder: An adder is a component that produces the sum of two or more numbers. The addends and the summand are the inputs to the adder, and the sum is the output, which is the sum of the addends and the summand. For example, if the adder's inputs are X1 and X2, the output is X1 + X2.

[0059] Multiplier: A multiplier is an electronic device that multiplies two unrelated analog signals, specifically two binary numbers. The multiplier and the multiplicand are the inputs to the multiplier, and the output is the product, which is equal to the product of the multiplier and the multiplicand. For example, if the inputs are X1 and X2, the output is X1 × X2.

[0060] Acceleration unit: A processing unit designed to improve the data processing speed in some special-purpose fields (for example, image processing, cosine distance calculation, etc.) in response to the low efficiency of traditional processing units. In the embodiment of the present disclosure, the processing unit is mainly designed to accelerate the cosine distance calculation speed.

[0061] Processing unit: also known as the scheduling unit, is a processing unit that schedules the acceleration unit and assigns the sequence of instructions to be executed to the acceleration unit. It can take the form of a processor (CPU), application-specific integrated circuit (ASIC), field-programmable gate array (FPGA), etc.

[0062] Data Center

[0063] Data centers are globally coordinated networks of specialized equipment used to transmit, accelerate, display, compute, and store data on the internet's network infrastructure. In the future, data centers will become a competitive asset for businesses. As data center applications expand, image processing and other techniques are increasingly being used in data centers. Cosine distance calculation, a key image processing technique, has been widely used in data center image processing.

[0064] In traditional large data centers, the network structure is usually as follows Figure 1 As shown in Figure 1, this is the hierarchical inter-networking model. This model consists of the following parts:

[0065] Server 140 : Each server 140 is a processing and storage entity of the data center. The processing and storage of a large amount of data in the data center are completed by these servers 140 .

[0066] Access switch 130: Access switches 130 connect servers 140 to the data center. One access switch 130 connects to multiple servers 140. Access switches 130 are typically located at the top of a rack, and are therefore also called top-of-rack switches. They physically connect to the servers.

[0067] Aggregation switch 120: Each aggregation switch 120 connects to multiple access switches 130 and provides other services, such as firewall, intrusion detection, network analysis, etc.

[0068] Core switch 110: Core switch 110 provides high-speed forwarding for packets entering and leaving the data center and provides connectivity for aggregation switch 120. The entire data center network is divided into an L3 routing network and an L2 routing network. Core switch 110 generally provides a resilient L3 routing network for the entire data center network.

[0069] Typically, aggregation switch 120 is the demarcation point between the L2 and L3 routing networks. The network below aggregation switch 120 is the L2 network, and the network above it is the L3 network. Each aggregation switch group manages a point of delivery (POD), each of which is an independent VLAN network. Servers can migrate within a POD without changing their IP addresses or default gateways, as each POD corresponds to a single L2 broadcast domain.

[0070] The Spanning Tree Protocol (STP) is typically used between aggregation switches 120 and access switches 130. STP ensures that only one aggregation switch 120 is available for a given VLAN network, with other aggregation switches 120 being used only in the event of a failure (dashed lines in the figure above). This means that horizontal scalability is impossible at the aggregation switch 120 level because even if multiple aggregation switches 120 are added, only one will be operational.

[0071] server

[0072] Since the server 140 is the real processing device in the data center, Figure 2 The figure shows a block diagram of the internal structure of a server 140. The server 140 includes a memory 210, a processing unit cluster 270 and an acceleration unit cluster 280 connected by a bus. The processing unit cluster is also called a scheduling unit cluster. The processing unit cluster 270 includes a plurality of processing units 220. The processing unit 220 is also called a scheduling unit. The acceleration unit cluster 280 includes a plurality of acceleration units 230. In the embodiment of the present application, the acceleration unit is a special processing unit designed mainly to accelerate the speed of cosine distance calculation, which can be embodied as a processing unit specially designed for cosine distance calculation, a graphics processing unit (GPU), an application-specific integrated circuit (ASIC) and a field-programmable gate array (FPGA). The processing unit is a unit that schedules the acceleration unit and allocates a sequence of instructions to be executed to each acceleration unit. It can take the form of a processor (CPU), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) and the like.

[0073] The architectural design of traditional processing units makes the control unit and storage unit occupy a large part of the space in the architecture, while the space occupied by the computing unit is insufficient. Therefore, it is very effective in terms of logical control, but not efficient enough in terms of large-scale parallel computing. Therefore, various specialized acceleration units have been developed to more effectively improve the computing speed of calculations for different functions and different fields. The acceleration unit proposed in this application is a processing unit specifically used to accelerate the calculation speed of cosine distance. It is a processing unit for calculating the cosine distance of different normalized vectors through a structure combining a multiplier and an adder. When calculating the cosine distance of two vectors, it is necessary to calculate the dot product of the two vectors and also to calculate the modulus of the two vectors separately. Due to the complexity of the calculation process and the large amount of calculated data, offline software is currently used to calculate the cosine distance of two vectors. By using the acceleration unit proposed in this application specifically for accelerating the cosine distance calculation speed, multiplication operations are performed on corresponding vector elements in different normalized vectors through multipliers, and addition operations are performed on the outputs of different multipliers through adders, so as to obtain the cosine distance between vectors, realize real-time calculation of the cosine distance between vectors, and thus improve the real-time performance of the cosine distance calculation.

[0074] The acceleration unit 230 must accept the scheduling of the processing unit 220. Figure 2 As shown, the memory 210 stores a first normalized vector and a second normalized vector for which cosine distance calculation is required. The first normalized vector and the second normalized vector are Figure 2 A processing unit 220 transmits the first normalized vector and the second normalized vector to an acceleration unit 230. The processing unit 220 can send the addresses of the first normalized vector and the second normalized vector in the memory 210 to the acceleration unit 230 in the form of an instruction. When performing the cosine distance calculation, the acceleration unit 230 directly addresses the first normalized vector and the second normalized vector in the memory 210, temporarily stores the first normalized vector and the second normalized vector in its on-chip memory, and then the acceleration unit 230 can calculate the cosine distance between the first normalized vector and the second normalized vector. The embodiment of the present application mainly focuses on the process of calculating the cosine distance between the first normalized vector and the second normalized vector by the acceleration unit 230, and the calculation process of the cosine distance will be described in detail later.

[0075] Internal structure of the processing unit and acceleration unit

[0076] The following combination Figure 3 The internal structure diagram of the processing unit 220 and the acceleration unit 230 specifically illustrates how the processing unit 220 schedules the acceleration unit 230 to work.

[0077] like Figure 3As shown, the processing unit 220 includes multiple processor cores 222 and a cache 221 shared by the multiple processor cores 222. Each processor core 222 includes an instruction fetch unit 203, an instruction decoding unit 224, an instruction issuing unit 225, and an instruction executing unit 226.

[0078] The instruction fetch unit 203 is used to move the instruction to be executed from the memory 210 to the instruction register (which may be Figure 3 The register file 229 shown is used to store a register for instructions) and receives the next instruction fetch address or calculates the next instruction fetch address according to an instruction fetch algorithm. The instruction fetch algorithm includes, for example, increasing or decreasing the address according to the instruction length.

[0079] After fetching the instruction, the processing unit 220 enters the instruction decoding stage. The instruction decoding unit 224 decodes the fetched instruction according to a predetermined instruction format to obtain the operand fetch information required by the fetched instruction, thereby preparing for the operation of the instruction execution unit 226. The operand fetch information may point to an immediate value, a register, or other software / hardware that can provide a source operand.

[0080] The instruction issuing unit 225 is located between the instruction decoding unit 224 and the instruction executing unit 226 and is used for scheduling and controlling instructions to efficiently distribute each instruction to different instruction executing units 226 , thereby enabling parallel operation of multiple instructions.

[0081] After the instruction issue unit 225 sends the instruction to the instruction execution unit 226, the instruction execution unit 226 begins executing the instruction. However, if the instruction execution unit 226 determines that the instruction should be executed by an acceleration unit, it forwards the instruction to the corresponding acceleration unit for execution. For example, if the instruction is a cosine distance calculation instruction, the instruction execution unit 226 will no longer execute the instruction and instead send it via the bus to the acceleration unit 230 for execution.

[0082] The processing unit 220 normalizes the first vector into a first normalized vector, normalizes the second vector into a second normalized vector, and schedules the acceleration unit 230 to calculate the cosine distance between the first normalized vector and the second normalized vector.

[0083] The acceleration unit 230 internally includes a controller 231 , a standardized interface 232 , a cosine distance calculation unit 233 , and a cosine distance sorting unit 234 .

[0084] The controller 231 receives the first normalized vector from the processing unit 220 and sends the first normalized vector to the cosine distance calculation unit 233. The controller 231 also sends control instructions to the standardization interface 232, the cosine distance calculation unit 233, and the cosine distance sorting unit 234 to control the operation of the standardization interface 232, the cosine distance calculation unit 233, and the cosine distance sorting unit 234. The controller 231 also receives the cosine distance sorting result fed back by the cosine distance sorting unit 234 and sends the cosine distance sorting result to the processing unit 220. The controller 231 and the processing unit 220 can exchange control information with each other through the Advanced Peripheral Bus (APB) interface, and send the cosine distance sorting result to the processing unit 220 through the APB interface.

[0085] The normalization interface 232 reads the second normalized vectors one by one from the memory 210 and sends the second normalized vectors to the cosine distance calculation unit 233. The normalization interface 232 can read the second normalized vectors one by one from the memory 210 via an Advanced eXtensible Interface (AXI) interface bus.

[0086] The cosine distance calculation unit 233 calculates the cosine distances between the first normalized vector and the plurality of second normalized vectors one by one, and inputs the calculation results of the cosine distances to the cosine distance sorting unit 234 .

[0087] The cosine distance sorting unit 234 sorts the cosine distances respectively determined by the cosine distance calculating unit 233 for the first normalized vector and the plurality of second normalized vectors, and inputs the result of sorting the cosine distances to the controller 231 .

[0088] Cosine distance calculation unit

[0089] Figure 4 This is a diagram of the internal structure of a cosine distance calculation unit according to an embodiment of the present application.

[0090] In one embodiment, Figure 4 As shown, the cosine distance calculation unit 233 includes 2 L There are multipliers 410 and L adder layers, the i-th adder layer C i Includes 2 L-i Each multiplier 410 receives as input a vector element of the first normalized vector and a corresponding vector element of the second normalized vector. LEvery two multipliers 410 in the multipliers 410 are grouped into one group, corresponding to one first adder 421. The outputs of the two multipliers 410 in the group serve as the inputs of the first adder 421. i , every two i-th adders 42i are grouped together, corresponding to the i+1-th adder layer C i+1 The outputs of the two i-th adders 42i in the group serve as the inputs of the i+1-th adder 42(i+1). The cosine distance between the first normalized vector and the second normalized vector is calculated according to the L-th adder layer C. L The output is determined.

[0091] The multiplier in the cosine distance calculation unit 233 receives the corresponding vector elements in the first normalized vector and the second normalized vector as input, and performs a multiplication operation on the two input vector elements. For example, the first normalized vector A=(A1, A2...A M ), the second normalized vector B=(B1,B2…B M ), the first normalized vector A and the second normalized vector each include M vector elements, the vector element A in the first normalized vector A i and the vector element B in the second normalized vector B i Correspondingly, 1≤i≤M, that is, vector element A i and vector element B i Corresponding to the same dimension. Vector element A i and vector element B i As the input of a multiplier, the multiplier performs the multiplication of the vector elements A i and vector element B i Perform multiplication and output the result of the multiplication.

[0092] The cosine distance calculation unit 233 includes L adder layers, the i-th adder layer C i Includes 2 L-i The i-th adder 42i, when i is 1, the first adder layer C1 includes 2 L-1 A first adder 421. The cosine distance calculation unit 233 includes 2 L In the multipliers 410, every two multipliers 410 form a group, and different multiplier groups include different multipliers 410. Therefore, the cosine distance calculation unit 233 includes 2 L-1 Multiplier groups. 2 L-1Each multiplier group in a multiplier group corresponds to a first adder 421. Different multiplier groups correspond to different first adders 421. The outputs of the two multipliers 410 in a multiplier group serve as inputs to the first adder 421 corresponding to that multiplier group. The first adder 421 performs an addition operation on the two inputs. For example, multiplier 1 and multiplier 2 form a group. The first adder corresponding to the multiplier group is first adder 1. The two outputs of multiplier 1 and multiplier 2 serve as inputs to first adder 1. First adder 1 adds the output of multiplier 1 and the output of second multiplier 2 and outputs the result of the addition operation.

[0093] For the i-th adder layer C in the 1st to L-1th adder layers i , the i-th adder layer C i In the example, every two i-th adders 42i form a group, and different adder groups include different adders. Therefore, the i-th adder layer C i Includes 2 L-i-1 adder groups. The i-th adder layer C i Each adder group and the i+1 adder layer C i+1 The i+1 adder 42(i+1) corresponds to a different adder group and a different i+1 adder 42(i+1). The i-th adder layer C i The outputs of the two i-th adders 42i in an adder group serve as inputs to the i+1-th adder 42(i+1) corresponding to that adder group. For example, adders 1 and 2 in the first adder layer C1 form a group. This adder group corresponds to adder 3 in the second adder layer C2. The outputs of adders 1 and 2 serve as inputs to adder 3. Adder 3 adds the outputs of adders 1 and 2 and outputs the result.

[0094] L-1 adder layer C L-1 Includes 2 adders 42 (L-1), Lth adder layer C L Includes 1 adder 42L, L-1 adder layer C L-1 The two adders 42 (L-1) in the L-th adder layer C are grouped together. L The outputs of the two adders 42(L-1) serve as inputs to the adder 42L, which adds the outputs of the two adders 42(L-1) and outputs the result of the addition.

[0095] In the embodiment of the present application, since the modulus of the first normalized vector and the second normalized vector are both 1, according to the cosine distance calculation formula, it is only necessary to calculate the product of the corresponding vector elements in the first normalized vector and the second normalized vector, and then sum the products of the corresponding vector elements to obtain the cosine distance between the first normalized vector and the second normalized vector. L Multipliers and L adder layers. Each multiplier receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input, and calculates the product of the corresponding vector elements in the first normalized vector and the second normalized vector. After the products of the corresponding vector elements in the first normalized vector and the second normalized vector are obtained, the products need to be summed. However, the adder can generally only sum two inputs. Therefore, the products are summed through L adder layers to obtain the sum of the products. The adder pair in the first adder layer is used to sum the outputs of the corresponding two multipliers. The product of the outputs of the two multipliers included in each multiplier pair is uniquely reflected in the input of one first adder. The adders in the other adder layers sum the outputs of the corresponding two adders in the previous adder layer. The output of any adder in the previous adder layer is also uniquely reflected in the input of an adder in the current adder layer. In this way, the output of the only adder in the Lth adder layer is the result of summing the outputs of each multiplier. Therefore, after the multiplier multiplies the corresponding vector elements in the first normalized vector and the second normalized vector, and each adder sums the input, the cosine distance between the first normalized vector and the second normalized vector can be determined based on one or more outputs of the adders in the Lth adder layer, thereby realizing real-time calculation of the cosine distance between vectors, thereby improving the real-time performance of the cosine distance calculation without relying on high-performance cloud resources.

[0096] Figure 5 This is a diagram of the internal structure of a cosine distance calculation unit according to another embodiment of the present application.

[0097] like Figure 5As shown, the cosine distance calculation unit 233 also includes a first normalized vector cache unit 430, a second normalized vector cache unit 440, and an accumulator 450. The first normalized vector cache unit 430 is used to cache the first normalized vector, and the second normalized vector cache unit 440 is used to cache the second normalized vector. The first normalized vector cache unit 430 obtains the first normalized vector from the processing unit 220 for cache, and the second normalized vector cache unit 440 obtains the second normalized vector from the memory 210 for cache. The first normalized vector cache unit 430 inputs the vector elements included in the cached first normalized vector into the multiplier 410 through its data channel, and the second normalized vector cache unit 440 inputs the vector elements included in the cached second normalized vector into the multiplier 410 through its data channel.

[0098] The first normalized vector and the second normalized vector both include M vector elements, each of which is 2 N bits, M and N are both positive integers. M does not have to be an integer power of 2, because in face recognition, fingerprint matching, etc., the length of the vector formed by the obtained features is not fixed. N is an integer power of 2, because the number of bits of vector elements is generally an integer power of 2, which is determined by the binary counting method commonly used in code operations. The bit width of the data channel from the first normalized vector cache unit 430 and the second normalized vector cache unit 440 to transmit vector elements to the multiplier 410 is 2 P , that is, the maximum number of bits of a binary number transmitted to the multiplier 410 through the data channel of the first normalized vector cache unit 430 in each clock cycle is 2 P The maximum number of bits of a binary number transmitted to the multiplier 410 through the data channel of the second normalized vector cache unit 440 in each clock cycle is also 2 P , where P is a positive integer and P>N. The bit width of the data channel is an integer power of 2, which is determined by the binary counting method commonly used in code operations. If M is less than or equal to 2 L (2 L multipliers are sufficient to process the multiplication of M vector elements and corresponding vector elements), and M·2 N Less than or equal to 2 P (The number of bits that can be transmitted by the data channel of the first normalized vector cache unit 430 or the second normalized vector cache unit 440 in one clock cycle is sufficient to transmit the bits of all vector elements in a normalized vector), then only one clock cycle is required to input the vector elements included in the first normalized vector and the second normalized vector into M multipliers, that is, only one round of vector element input is required to input the vector elements included in the first normalized vector and the second normalized vector into each multiplier. If M is greater than 2 L(Because the number of multipliers is insufficient to process the multiplication of M vector elements and corresponding vector elements in one clock cycle), or M·2 N Greater than 2 P (The number of bits that can be transmitted by the data channel of the first normalized vector cache unit 430 or the second normalized vector cache unit 440 in one clock cycle is not enough to input the bits of all vector elements in a normalized vector), then multiple clock cycles are required to input the vector elements included in the first normalized vector and the second normalized vector into each multiplier, that is, multiple rounds of vector element input are required to input the vector elements included in the first normalized vector and the second normalized vector into each multiplier.

[0099] For each round of vector element input, the Lth adder in the Lth adder layer outputs a summation result. If only one round of vector element input is required, the vector elements included in the first normalized vector and the second normalized vector can be input into each multiplier, and the summation result output by the Lth adder is the cosine distance between the first normalized vector and the second normalized vector. If multiple rounds of vector element input are required to input the vector elements included in the first normalized vector and the second normalized vector into each multiplier, the Lth adder outputs multiple summation results, and the summation results output by the Lth adder are accumulated to obtain the cosine distance between the first normalized vector and the second normalized vector.

[0100] like Figure 5 As shown, the accumulator 450 takes the output of the Lth adder as input, and the accumulator 450 accumulates the input received in K consecutive clock cycles as the calculation result of the cosine distance between the first normalized vector and the second normalized vector, where K=roundup(M / min(2 P-N ,2 L )), roundup() is a rounding function, and min() is a minimum value function. Since the maximum number of bits that the first normalized vector cache unit 430 or the second normalized vector cache unit 440 can transfer to the multiplier 410 in one clock cycle is 2 P , and the number of bits per vector element in the first normalized vector or the second normalized vector is 2 N Therefore, the number of vector elements that the first normalized vector cache unit 430 or the second normalized vector cache unit 440 can transfer to the multiplier 410 in one clock cycle is at most 2 P / 2 N =2 P-N .

[0101] In 2 P-N ≤2 LWhen the number of vector elements input to each multiplier 410 through the data channel in each clock cycle is less than the total number of multipliers 410, only part of the multipliers 410 are occupied in each clock cycle, and the number of vector elements actually input to the multiplier 410 in one clock cycle is 2 P-N , then the number of input rounds of the vector element is roundup(M / 2 P-N ), the Lth adder will output roundup(M / 2 P-N ) summation results, the accumulator 450 rounds up (M / 2 P-N ) summation results are accumulated as the cosine distance calculation result between the first normalized vector and the second normalized vector.

[0102] In 2 P-N >2 L When each clock cycle can input vector elements whose number is greater than the total number of multipliers 410 to each multiplier 410 through the data channel, 2 P-N -2 L The vector elements will be retained and cannot be input into the multiplier 410 in this clock cycle, but can be retained and enter the multiplier 410 in the next clock cycle. The number of vector elements entering the multiplier 410 in one clock cycle is 2 L , then the number of input rounds of the vector element is roundup(M / 2 L ), the Lth adder will output roundup(M / 2 L ) summation results, the accumulator 450 rounds up the output of the Lth adder (M / 2 L ) summation results are accumulated as the cosine distance calculation result between the first normalized vector and the second normalized vector.

[0103] It should be noted that accumulator 450 is essentially an adder, and its two inputs are the output of the Lth adder and the temporary sum of the output of accumulator 450 in the previous round of calculation. In the first round of accumulation operation of accumulator 450, the output of accumulator 450 in the previous round of calculation is empty, that is, accumulator 450 takes as input the first sum result of the first normalized vector and the second normalized vector output by the Lth adder and zero.

[0104] In one example, L=5, M=128, N=2, P=7, that is, the cosine distance calculation unit includes 2 5 multipliers, the first normalized vector and the second normalized vector each have 128 vector elements, each vector element is 2 2 The data channel bit width of the first normalized vector cache unit and the second normalized vector cache unit is 2 7. Through the data channel of the first normalized vector cache unit, 32 vector elements of the first normalized vector can be input into the multiplier in each clock cycle, and through the data channel of the second normalized vector cache unit, 32 vector elements of the second normalized vector can be input into the multiplier in each clock cycle, wherein one vector element of the first normalized vector and a corresponding vector element of the second normalized vector are input into each multiplier, and different multipliers receive different vector elements as input. It takes 4 clock cycles to input the first normalized vector and the second normalized vector into the multiplier, and the fifth adder outputs 4 addition results. The accumulator accumulates the 4 addition results output by the fifth adder as the calculation result of the cosine distance between the first normalized vector and the second normalized vector.

[0105] In another example, L=5, M=125, N=3, P=6, that is, the cosine distance calculation unit includes 2 5 multipliers, the first normalized vector and the second normalized vector each have 125 vector elements, each vector element is 2 3 The data channel bit width of the first normalized vector cache unit and the second normalized vector cache unit is 2 6 . Through the data channel of the first normalized vector cache unit, 8 vector elements of the first normalized vector can be input into the multiplier in each clock cycle. Through the data channel of the second normalized vector cache unit, 8 vector elements of the second normalized vector can be input into the multiplier in each clock cycle. 8 multipliers among the 32 multipliers will receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. Different multipliers receive different vector elements as input. At this time, roundup (125 / 8) = 16, and it takes 16 clock cycles to input the first normalized vector and the second normalized vector into the multiplier. The fifth adder outputs 16 addition results. The accumulator accumulates the 16 addition results output by the fifth adder as the calculation result of the cosine distance between the first normalized vector and the second normalized vector.

[0106] In the embodiments of the present application, the data types of the vector elements in the normalized vector for which the cosine distance is to be calculated may be different. For example, the data type of the vector elements may be INT2, INT4, INT8, INT16, INT32, etc., where INT is an integer type. The number of binary digits of vector elements of different data types is different. The number of binary digits of INT2 is 2, the number of binary digits of INT4 is 4, the number of binary digits of INT8 is 8, the number of binary digits of INT16 is 16, and the number of binary digits of INT32 is 32.

[0107] Depending on the number of binary digits that can be processed, multipliers have different specifications. For example, a multiplier with an INT2 specification can only input data with a binary digit less than 2 for multiplication, a multiplier with an INT4 specification can only input data with a binary digit less than 4 for multiplication, a multiplier with an INT8 specification can only input data with a binary digit less than 8 for multiplication, a multiplier with an INT16 specification can only input data with a binary digit less than 16 for multiplication, and a multiplier with an INT32 specification can only input data with a binary digit less than 32 for multiplication. The larger the number of binary digits that can be processed, the larger the size of the multiplier and the higher the cost. Since a multiplier with a larger number of binary digits can multiply data with a smaller number of binary digits, the cosine distance calculation unit can include multipliers of different specifications, reducing the size of the cosine distance calculation unit through multiplexing of the multipliers.

[0108] The number of binary digits of the vector elements in the first normalized vector and the second normalized vector may be 2 N , where N can be any positive integer. However, in practice, the number of commonly used binary digits is limited, such as INT2, INT4, INT8, INT16, etc. In some scenarios, only a subset of the commonly used binary digit identifiers may be used. In one embodiment, N consists of k increasing positive integers N1 to N k Select from the collection.

[0109] The following discusses the 2 L The number of bits allowed at the input of the multiplier is as follows: k Select from the set where the maximum value of N is N k , so 2 L The multiplier with the largest number of bits allowed at its input should be able to accept Since the maximum number of bits that the first normalized vector cache unit 430 or the second normalized vector cache unit 440 can transfer to the multiplier 410 in one clock cycle is 2 P , when the number of bits of the vector elements is When the first normalized vector cache unit 430 or the second normalized vector cache unit 440 is able to transmit to the multiplier 410 in one clock cycle vector elements. Therefore, the cosine distance calculation unit includes 2 L Among the multipliers, at least Has bit inputs of the multiplier.

[0110] However, when the number of bits of the vector elements is When the first normalized vector cache unit 430 or the second normalized vector cache unit 440 is able to transmit to the multiplier 410 in one clock cycle vector elements. At this time, we need have at least However, the previously determined Has The multiplier with bit input can also be used as To use the multiplier at the bit input, just leave some of the bits unused (e.g. fill them with 0 or 1, etc.). Has bit inputs of the multiplier.

[0111] As a general case, for any i in 1≤i≤k-1, the cosine distance calculation unit includes 2L multipliers, at least Has Specifically, the number of binary digits input through the data channel with a bit width of 2P is When the vector element is vector elements. Since the number of input bits in the cosine distance calculation unit is greater than The number of multipliers is The number of input terminals is greater than The multiplier can be used to multiply binary digits by The data is added (one point is filled with 0 or 1, etc.), and on this basis, the cosine distance calculation unit only needs to include at least Has The multiplier at the bit input makes the number of bits at the input greater than or equal to The number of multipliers is greater than or equal to

[0112] In an embodiment of the present application, when multiplication operation is performed on data with a smaller number of binary bits through the cosine distance calculation unit, the operation is performed using a multiplier whose input end bit number is equal to or greater than the number of binary bits of the data. By reusing the multipliers with a larger input end bit number, the number of multipliers with a larger input end bit number in the cosine distance calculation unit can be reduced, thereby reducing the volume of the cosine distance calculation unit and reducing the cost of the cosine distance calculation unit.

[0113] In one example, the possible values ​​of N are 2 and 3, and P is equal to 7, that is, the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4 or 8. At this time, the cosine distance calculation unit includes at least 16 multipliers with 8-bit input terminals and at least 16 multipliers with 4-bit input terminals.

[0114] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4, in each clock cycle, 32 vector elements are input through the data channel of the first normalized vector cache unit, and 32 vector elements are input through the data channel of the second normalized vector cache unit. The 16 multipliers with 8-bit input terminals and the 16 multipliers with 4-bit input terminals both receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the 16 multipliers with 8-bit input terminals are reused for multiplication operations of 4-bit vector elements.

[0115] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 8, in each clock cycle, 16 vector elements are input through the data channel of the first normalized vector cache unit, and 16 vector elements are input through the data channel of the second normalized vector cache unit. 16 multipliers with 8-bit input terminals respectively receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the 16 multipliers with 4-bit input terminals are idle.

[0116] In another example, the possible values ​​of N are 2, 3, and 5, and P is equal to 7, that is, the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4, 8, or 32. In this case, the cosine distance calculation unit includes at least 4 multipliers with 32-bit input terminals, at least 12 multipliers with 8-bit input terminals, and at least 16 multipliers with 4-bit input terminals.

[0117] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 32, in each clock cycle, 4 vector elements are input through the data channel of the first normalized vector cache unit, and 4 vector elements are input through the data channel of the second normalized vector cache unit. The 4 multipliers with 32-bit input terminals respectively receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the 12 multipliers with 8-bit input terminals and the 16 multipliers with 4-bit input terminals are idle.

[0118] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 8, in each clock cycle, 16 vector elements are input through the data channel of the first normalized vector cache unit, and 16 vector elements are input through the data channel of the second normalized vector cache unit. The 12 multipliers with 8-bit input terminals and the 4 multipliers with 32-bit input terminals all receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the 4 multipliers with 32-bit input terminals are reused for the multiplication operation of the 8-bit vector elements, and the 16 multipliers with 4-bit input terminals are idle.

[0119] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4, in each clock cycle, 32 vector elements are input through the data channel of the first normalized vector cache unit, and 32 vector elements are input through the data channel of the second normalized vector cache unit. The four multipliers with 32-bit input terminals, the 12 multipliers with 8-bit input terminals, and the 16 multipliers with 4-bit input terminals all receive a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the four multipliers with 32-bit input terminals and the 12 multipliers with 8-bit input terminals are reused for multiplication operations of 4-bit vector elements.

[0120] In one example, the possible values ​​of N are 2, 3, 5 and 6, and P is equal to 7, that is, the binary bit numbers of the vector elements in the first normalized vector and the second normalized vector are 4, 8, 32 and 64. At this time, the cosine distance calculation unit includes at least 2 multipliers with 64-bit input terminals, at least 2 multipliers with 32-bit input terminals, at least 12 multipliers with 8-bit input terminals and at least 16 multipliers with 4-bit input terminals.

[0121] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 64, in each clock cycle, two vector elements are input through the data channel of the first normalized vector cache unit, and two vector elements are input through the data channel of the second normalized vector cache unit. Two multipliers with 64-bit input terminals respectively receive one vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the two multipliers with 32-bit input terminals, the 12 multipliers with 8-bit input terminals, and the 16 multipliers with 4-bit input terminals are idle.

[0122] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 32, in each clock cycle, 4 vector elements are input through the data channel of the first normalized vector cache unit, and 4 vector elements are input through the data channel of the second normalized vector cache unit. Two multipliers with 64-bit input ends and two multipliers with 32-bit input ends both receive one vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the two multipliers with 64-bit input ends are reused for the multiplication operation of the 32-bit vector elements, and the 12 multipliers with 8-bit input ends and the 16 multipliers with 4-bit input ends are idle.

[0123] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 8, in each clock cycle, 8 vector elements are input through the data channel of the first normalized vector cache unit, and 8 vector elements are input through the data channel of the second normalized vector cache unit. Two multipliers with 64-bit input ends, two multipliers with 32-bit input ends, and 12 multipliers with 8-bit input ends all receive one vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the two multipliers with 64-bit input ends and the two multipliers with 32-bit input ends are reused for the multiplication operation of the 8-bit vector elements, and the 16 multipliers with 4-bit input ends are idle.

[0124] When the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4, in each clock cycle, 32 vector elements are input through the data channel of the first normalized vector cache unit, and 32 vector elements are input through the data channel of the second normalized vector cache unit. Two multipliers with 64-bit input terminals, two multipliers with 32-bit input terminals, 12 multipliers with 8-bit input terminals, and 16 multipliers with 4-bit input terminals all receive one vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input. At this time, the two multipliers with 64-bit input terminals, the two multipliers with 32-bit input terminals, and the 12 multipliers with 8-bit input terminals are reused for multiplication operations of 4-bit vector elements.

[0125] Figure 6 This is a diagram of the internal structure of a cosine distance calculation unit according to another embodiment of the present application.

[0126] like Figure 6 As shown, the cosine distance calculation unit 233 includes 32 multipliers, wherein the multiplier layer D1 includes 16 multipliers with 8-bit input bits, and the multiplier layer D2 includes 16 multipliers with 4-bit input bits. The data channel bit width of the first normalized vector cache unit 430 and the second normalized vector cache unit 440 are both 128 bits.

[0127] When the number of binary bits of each vector element in the first normalized vector and the second normalized vector is 4 bits, in each clock cycle, the first normalized vector cache unit 430 outputs 32 vector elements through the data channel, and the second normalized vector cache unit 440 outputs 32 vector elements through the data channel. Each of the 32 multipliers receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input.

[0128] When the number of binary bits of each vector element in the first normalized vector and the second normalized vector is 8 bits, in each clock cycle, the first normalized vector cache unit 430 outputs 16 vector elements through the data channel, and the second normalized vector cache unit 440 outputs 16 vector elements through the data channel. Each of the 16 multipliers with an input end bit number of 8 receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input, and the 16 multipliers with an input end bit number of 4 are idle.

[0129] Figure 7 This is a diagram of the internal structure of a cosine distance calculation unit according to another embodiment of the present application.

[0130] like Figure 7 As shown, the cosine distance calculation unit 233 includes 32 multipliers, wherein the multiplier layer D1 includes 8 multipliers with 16-bit input bits, the multiplier layer D2 includes 8 multipliers with 8-bit input bits, and the multiplier layer D3 includes 16 multipliers with 4-bit input bits. The data channel bit width of the first normalized vector cache unit 430 and the second normalized vector cache unit 440 are both 128 bits.

[0131] When the number of binary bits of each vector element in the first normalized vector and the second normalized vector is 4 bits, in each clock cycle, the first normalized vector cache unit 430 outputs 32 vector elements through the data channel, and the second normalized vector cache unit 440 outputs 32 vector elements through the data channel. Each of the 32 multipliers receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input.

[0132] When the number of binary bits of each vector element in the first normalized vector and the second normalized vector is 8 bits, in each clock cycle, the first normalized vector cache unit 430 outputs 16 vector elements through the data channel, and the second normalized vector cache unit 440 outputs 16 vector elements through the data channel. Each of the 8 multipliers with an input bit number of 16 and the 8 multipliers with an input bit number of 8 receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input, and the 16 multipliers with an input bit number of 4 are idle.

[0133] When the number of binary bits of each vector element in the first normalized vector and the second normalized vector is 16 bits, in each clock cycle, the first normalized vector cache unit 430 outputs 8 vector elements through the data channel, and the second normalized vector cache unit 440 outputs 8 vector elements through the data channel. Each of the 8 multipliers with an input end bit number of 16 receives a vector element in the first normalized vector and the corresponding vector element in the second normalized vector as input, and the 16 multipliers with an input end bit number of 4 and the 8 multipliers with an input end bit number of 8 are idle.

[0134] In one possible implementation, That is, on the basis of satisfying the multiplication operation of data with different binary digits, the cosine distance calculation unit does not include redundant multipliers, ensuring that the cosine distance calculation unit has a smaller volume and lower cost. Has bit input multiplier, also includes Has A multiplier with a plurality of input bits, wherein 1≤i≤k-1, when multiplication operation is performed on data with different binary digits, a multiplier with a smaller input bit number is used as much as possible, and a multiplier with a smaller input bit number has a smaller volume and lower cost, thereby further reducing the cost of the cosine distance calculation unit.

[0135] In the embodiment of the present application, when the binary digits of the vector elements in the first normalized vector and the second normalized vector are When the number of input bits is greater than or equal to bit multiplier bits are used for the input vector elements. When the number of bits at the input of the multiplier is equal to When the binary digits of the vector elements in the first normalized vector and the second normalized vector are equal to the number of bits at the input of the multiplier, the number of bits at the input of the multiplier is equal to the number of bits at the input of the multiplier. All bits are used for input vector elements. When the binary digits of the vector elements in the first normalized vector and the second normalized vector are less than the digits of the input terminal of the multiplier, the back of the input terminal of the multiplier is The first bit is the sign bit, which is used to indicate the sign of the vector element.

[0136] For example, when the number of binary bits of the vector elements in the first normalized vector and the second normalized vector is 4, if the number of bits of the multiplier input is 4, all 4 bits of the multiplier input are used to input the vector elements; if the number of bits of the multiplier input is 8, the last 4 bits of the multiplier input are used to input the vector elements, and the first 4 bits are sign bits; if the number of bits of the multiplier input is 16, the first 4 bits of the multiplier input are used to input the vector elements, and the last 12 bits are sign bits.

[0137] If the vector elements in the first normalized vector and the second normalized vector have signs, when a multiplier whose input terminal has a bit number greater than the binary bit number of the vector element receives the vector element as input, if the vector element as the input of the multiplier is a positive number, the sign bit of the multiplier input terminal is all 0; if the vector element as the input of the multiplier is a negative number, the sign bit of the multiplier input terminal is all 1.

[0138] For example, the binary number of a vector element is 4 bits, and the number of bits of the input end of the multiplier with the vector element as input is 8 bits. If the vector element is a positive number, the first 4 bits of the input end of the multiplier are all 0s, and the last four bits are used to input the vector element. If the vector element is a negative number, the first 4 bits of the input end of the multiplier are all 1s, and the last four bits are used to input the vector element.

[0139] In the embodiment of the present application, when the binary number is The vector elements of the input are greater than The multiplier input is the The bit is used to input the vector element, and the remaining bits in the multiplier input are sign bits. If the vector element is a positive number, the sign bits are all 0, and if the vector element is a negative number, the sign bits are all 1. Setting the sign bits to all 0 or all 1 according to the sign of the vector element not only ensures that each bit at the multiplier input has a binary number, ensuring that the multiplier can perform multiplication normally, but also inputs the sign of the vector element into the multiplier, allowing the multiplier to perform multiplication operations on vector elements with different signs, thereby improving the applicability of the cosine distance calculation unit.

[0140] If the vector elements in the first normalized vector and the second normalized vector are unsigned, and a multiplier whose input has a number of bits greater than the binary digits of the vector elements receives the vector elements as input, the last several bits of the multiplier input are used to input the vector elements, and the remaining bits of the multiplier input are all 0s, so that each bit of the multiplier input has a binary digit, ensuring that the multiplier can perform multiplication normally. For example, if the binary digits of the unsigned vector elements are 4 and the number of bits of the multiplier input is 8, then the last 4 bits of the multiplier input are used to input the vector elements, and the first 4 bits of the multiplier input are all 0s.

[0141] Cosine distance sorting unit

[0142] Figure 8 This is a diagram of the internal structure of a cosine distance sorting unit according to an embodiment of the present application.

[0143] Figure 8 As shown, the cosine distance sorting unit 234 includes at least two serially connected cache units 810, each of which includes a cache space and a comparator. The cache space is used to cache at least one cosine distance and the identifier of the corresponding second normalized vector. The comparator is used to compare the cosine distance received by the cache unit with the cosine distances cached in the cache space according to a predetermined rule, retain the cosine distances and the identifiers of the corresponding second normalized vectors that meet the predetermined rule in the cache space, and remove the cosine distances and the identifiers of the corresponding second normalized vectors that do not meet the predetermined rule from the cache space and transmit them to the next serially connected cache unit.

[0144] When the cosine distance calculation unit 233 calculates the cosine distances between the first normalized vector and multiple second normalized vectors, the cosine distance calculation unit 233 inputs the calculated cosine distances and the corresponding second normalized vector identifiers into the cosine distance sorting unit 234. The cosine distances and the corresponding second normalized vector identifiers input into the cosine distance sorting unit 234 are then stored in the first cache unit 810. After a cache unit 810 receives the cosine distances and the corresponding second normalized vector identifiers, a comparator within the cache unit 810 compares the cosine distances received by the cache unit 810 with the cosine distances stored in the cache space of the cache unit 810 according to a predetermined rule. The cosine distances and the corresponding second normalized vector identifiers that meet the predetermined rule are retained in the cache space. The cosine distances and the corresponding second normalized vector identifiers that do not meet the predetermined rule are removed from the cache unit 810 and transmitted to the next cache unit 810 in the series.

[0145] In each cache unit 810, a comparator compares the cosine distance received by the cache unit 810 with the cosine distances cached in the cache space according to a predetermined rule. If the cosine distance received by the cache unit 810 meets the predetermined rule, the cosine distance received by the cache unit 810 and the identifier of the corresponding second normalized vector are cached in the cache space, the cosine distance and the identifier of the corresponding second normalized vector originally in the cache space are squeezed out of the cache unit 810, and the squeezed-out cosine distance and the identifier of the corresponding second normalized vector are transmitted to the next cache unit 810 in the series. If the cosine distance cached in the cache space of the cache unit 810 meets the predetermined rule, the cosine distance received by the cache unit 810 and the identifier of the corresponding second normalized vector are squeezed out of the cache unit 810, and the squeezed-out cosine distance and the identifier of the corresponding second normalized vector are transmitted to the next cache unit 810 in the series, while the cosine distance and the identifier of the corresponding second normalized vector cached in the cache unit 810 remain unchanged.

[0146] It should be noted that since there are no more cache units 810 in series after the last cache unit 810, after the last cache unit 810 squeezes the cosine distance and the corresponding second normalized vector identifier that do not meet the predetermined rules out of the cache unit 810, there is no need to transmit the squeezed-out cosine distance and the corresponding second normalized vector identifier. In addition, each cache unit 810 includes a cache space that caches a default cosine distance, wherein, according to the predetermined rule, the default cosine distance is less than the cosine distance between the first normalized vector and any second normalized vector, such as the default cosine distance is any value less than -1, or the default cosine distance is greater than the cosine distance between the first normalized vector and any second normalized vector, such as the default cosine distance is any value greater than 1. When the cosine distance calculated by the cosine distance calculation unit 233 is first input to the cache unit 810, the comparator in the cache unit 810 compares the cosine distance received by the cache unit 810 with the default cosine distance.

[0147] For each cache unit 810, the cosine distance that meets the predetermined rules and the identifier of the corresponding second normalized vector are retained in the cache space, while the cosine distance that does not meet the predetermined rules and the identifier of the corresponding second normalized vector are squeezed out of the cache unit 810, and the squeezed cosine distance and the identifier of the corresponding second normalized vector are transmitted to the next cache unit 810, so that the cosine distance finally retained in each cache unit 810 is a larger or smaller cosine distance among the cosine distances output by the cosine distance calculation unit 233, thereby completing the screening of larger or smaller cosine distances while calculating the cosine distance. After the cosine distance sorting unit 234 inputs the screening result of the cosine distance into the controller 231, the controller 231 can determine the matching result of the first normalized vector and multiple second normalized vectors based on the screening result of the cosine distance, thereby quickly realizing feature matching.

[0148] In an embodiment of the present application, the predetermined rule is that the larger cosine distance between the cosine distance received by the cache unit and the cosine distance cached in the cache space is retained in the cache unit, while the smaller cache distance and the corresponding second normalized vector identifier are squeezed out of the cache unit and transmitted to the next cache unit in the series. Therefore, after the cosine distance calculation unit sequentially inputs the cosine distance calculation results of the first normalized vector and each second normalized vector into the cosine distance sorting unit, each cache unit in the cosine distance sorting unit retains several larger cosine distances and the corresponding second normalized vector identifiers, among which the cosine distance retained in the first cache unit is the largest, and the cosine distances retained in subsequent cache units decrease in sequence. After the cosine distance sorting unit inputs the pre-selected distance sorting result into the controller, the controller can determine several second normalized vectors with a higher degree of matching with the first normalized vector.

[0149] Figure 9 This is a diagram of the internal structure of a cosine distance calculation unit and a cosine distance sorting unit according to an embodiment of the present application.

[0150] like Figure 9 As shown, the cosine distance calculation unit 233 includes 32 multipliers, of which the multiplier layer D1 includes 16 multipliers with 8-bit inputs, and the multiplier layer D2 includes 16 multipliers with 4-bit inputs. The cosine distance sorting unit 234 includes 8 cache units 810 connected in series. The data channel bit width of the first normalized vector cache unit 430 and the second normalized vector cache unit 440 are both 128 bits, and the vector elements in the first normalized vector and the second normalized vector each include 128 vector elements.

[0151] The cosine distance calculation unit 233 sequentially calculates the cosine distances between the first normalized vector and each second normalized vector, and sequentially inputs the calculated cosine distance results into the cosine distance sorting unit 234. The cache unit 810 in the cosine distance sorting unit 234 retains the larger cosine distance and the corresponding second normalized vector identifier in the cache space according to a predetermined rule, while expels the smaller cosine distance and the corresponding second normalized vector identifier from the cache unit 810 and transmits the expelled cosine distance and the corresponding second normalized vector identifier to the next cache unit 810 in the series. After the cosine distance calculation unit 233 inputs the calculated cosine distance results into the cosine distance sorting unit 234, and the cosine distance sorting unit 234 screens the cosine distances, the eight larger cosine distances and the corresponding second normalized vector identifiers remain in the eight cache units 810.

[0152] Cosine distance calculation method

[0153] Figure 10 Flowchart of a cosine distance calculation method according to an embodiment of the present application, which is used for the cosine distance calculation unit in any of the above embodiments, such as Figure 10 As shown, the cosine distance calculation method includes:

[0154] Step 1001: Receive a vector element of a first normalized vector and a corresponding vector element in a second normalized vector as input through each multiplier;

[0155] Step 1002: L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder;

[0156] Step 1003: For the i-th adder layer in the 1st to L-1th adder layers, group every two i-th adders into a group, corresponding to an i+1th adder in the i+1th adder layer, and use the outputs of the two i-th adders in the group as the input of the i+1th adder.

[0157] Step 1004: Determine the cosine distance according to the output of the Lth adder layer.

[0158] Since the details of the above-mentioned cosine distance calculation method have been described in detail in the cosine distance calculation unit and cosine distance sorting unit parts of the above-mentioned embodiment of the present application in combination with the structural diagram, the specific process can be referred to the description in the above-mentioned cosine distance calculation unit embodiment and cosine distance sorting unit embodiment, which will not be repeated here.

[0159] Matching method

[0160] Regarding the application of the solution provided in the embodiment of the present application in scenarios such as image, audio or text matching, the embodiment of the present application provides a matching method, such as Figure 11 As shown, the matching method includes the following steps:

[0161] Step 1101: Acquire first data and second data to be matched, wherein the first data and the second data are one of the following: image data, text data, and voice data.

[0162] In application scenarios such as face recognition, voice recognition, and machine translation, it is necessary to determine whether a match occurs based on the difference between the two matching objects, and the cosine distance can characterize the difference between the two matching objects. When the first data and the second data are image data, the cosine distance calculation solution provided in the embodiment of the present application is applicable to image matching application scenarios, such as face recognition, face unlocking, face payment, license plate recognition, etc. When the first data and the second data are voice data, the cosine distance calculation solution provided in the embodiment of the present application is applicable to voice recognition application scenarios, such as smart speakers with voice interaction functions, vehicle control systems, smart home appliances, etc., as well as meeting records realized through voice recognition, etc. When the first data and the second data are text data, the cosine distance calculation solution provided in the embodiment of the present application is applicable to text translation application scenarios, such as Chinese-English translation, etc.

[0163] Step 1102: Obtain a first vector generated after feature extraction is performed on the first data, and obtain a second vector generated after feature extraction is performed on the second data.

[0164] By using a neural network model or the like, features are extracted from the first data and the second data respectively to generate a first vector that can represent the features of the first data and a second vector that can represent the features of the second data.

[0165] Step 1103: Normalize the first vector into a first normalized vector, and normalize the second vector into a second normalized vector.

[0166] Normalization processing of the first vector and the second vector is implemented through a software program to obtain a first normalized vector and a second normalized vector, wherein the lengths of the first normalized vector and the second normalized vector are both 1.

[0167] Step 1104 : Receive a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input through each multiplier included in the cosine distance calculation unit.

[0168] Among them, the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L -ian i-th adder, where 1≤i≤L, and L is a positive integer.

[0169] Step 1105: L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder.

[0170] Step 1106: For the i-th adder layer in the 1st to L-1th adder layers, divide every two i-th adders into a group, corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the input of the i+1th adder.

[0171] Step 1107: Determine the cosine distance between the first normalized vector and the second normalized vector according to the output of the Lth adder layer.

[0172] It should be noted that steps 1104 to 1107 are the process of calculating the cosine distance between the first normalized vector and the second normalized vector. The implementation method of the cosine distance calculation has been described in detail in the above embodiment. For details, please refer to the description in the above embodiment, and will not be repeated here.

[0173] Step 1108: When the cosine distance is less than a preset first distance threshold, determine that the first data matches the second data.

[0174] Since the cosine distance between the first normalized vector and the second normalized vector reflects the difference between the first data and the second data, the smaller the cosine distance between the first normalized vector and the second normalized vector, the higher the similarity between the first data and the second data. Therefore, when the cosine distance between the first normalized vector and the second normalized vector is less than the preset first distance threshold, it can be determined that the first data and the second data match.

[0175] In an embodiment of the present application, the cosine distance calculation unit provided in the embodiment of the present application is used to calculate the cosine distance between image data, text data or voice data to achieve matching of images, text or voice. Since real-time calculation of the cosine distance between vectors can be achieved, matching processing of images, text or voice can be achieved faster, matching efficiency is improved, and it does not rely on high-performance cloud resources.

[0176] Clustering methods

[0177] In view of the application of the solution provided in the embodiment of the present application in scenarios such as data analysis, the embodiment of the present application provides a clustering method, such as Figure 12 As shown, the clustering method includes the following steps:

[0178] Step 1201: Obtain multiple data to be clustered and cluster centers.

[0179] Clustering involves dividing a dataset into distinct classes or clusters based on specific criteria, ensuring that data objects within the same cluster are as similar as possible while simultaneously maximizing the differences between data objects in different clusters. During cluster analysis, cosine distance can be used as a clustering criterion, with two data objects whose cosine distance is less than a preset second distance threshold being classified as belonging to the same class. Cluster centers can be predetermined or dynamically adjusted during the clustering process. When calculating the cosine distance between two data objects, one data object serves as the data to be clustered, and the other serves as the cluster center.

[0180] Step 1202: Determine a first vector corresponding to the cluster center and a second vector corresponding to each data to be clustered.

[0181] Through the pre-set data conversion rules, the cluster center is converted into the first training vector, and each data to be clustered is converted into the corresponding second vector.

[0182] Step 1203: For each data to be clustered, normalize the first vector into a first normalized vector, and normalize the second vector corresponding to the data to be clustered into a second normalized vector.

[0183] Normalization processing of the first vector and the second vector is implemented through a software program to obtain a first normalized vector and a second normalized vector, wherein the lengths of the first normalized vector and the second normalized vector are both 1.

[0184] Step 1204: Receive a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input through each multiplier included in the cosine distance calculation unit.

[0185] Among them, the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L -i an i-th adder, where 1≤i≤L, and L is a positive integer.

[0186] Step 1205: L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder.

[0187] Step 1206: For the i-th adder layer in the 1st to L-1th adder layers, divide every two i-th adders into a group, corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the input of the i+1th adder.

[0188] Step 1207: Determine the cosine distance between the first normalized vector and the second normalized vector according to the output of the Lth adder layer.

[0189] It should be noted that steps 1204 to 1207 are the process of calculating the cosine distance between the first normalized vector and the second normalized vector. The implementation method of the cosine distance calculation has been described in detail in the above embodiment. For details, please refer to the description in the above embodiment, and will not be repeated here.

[0190] Step 1208: If the cosine distance is less than a preset second distance threshold, it is determined that the data to be clustered belongs to the cluster group corresponding to the cluster center.

[0191] Since the cosine distance between the first normalized vector and the second normalized vector reflects the difference between the first data and the second data, the smaller the cosine distance between the first normalized vector and the second normalized vector, the higher the similarity between the data to be clustered and the cluster center. Therefore, when the cosine distance between the first normalized vector and the second normalized vector is less than the preset second distance threshold, the data to be clustered can be divided into the cluster group corresponding to the cluster center.

[0192] In an embodiment of the present application, the cosine distance calculation unit provided in the embodiment of the present application is used to calculate the cosine distance between the data to be clustered and the cluster center to realize clustering analysis of the data. Since the real-time calculation of the cosine distance between vectors can be realized, data clustering can be realized faster, the efficiency of data clustering analysis can be improved, and it does not rely on high-performance cloud resources.

[0193] It should be noted that, in each of the above embodiments, the normalization calculation of the vector is completed by software, and the cosine distance calculation and cosine distance sorting of the normalized vector are completed by hardware.

[0194] The commercial value of the embodiments of this application

[0195] In solving the technical problem of low real-time performance of cosine distance calculation, the embodiment of the present application multiplies the corresponding vector elements in the normalized vector through a multiplier, and adds the outputs of each multiplier through multiple adders to obtain the cosine distance between the normalized vectors, thereby realizing real-time calculation of the cosine distance. There is no need to calculate the cosine distance through offline software, thereby improving the real-time performance of the cosine distance calculation, and there is no need to occupy more network resources to upload the vector to the cloud. The cosine distance is calculated through high-performance cloud computing, thereby reducing the cost and security of the cosine distance calculation, and therefore has good market prospects.

[0196] It should be understood that the various embodiments in this specification are described in a progressive manner. References to the same or similar parts of the various embodiments will be sufficient. Each embodiment focuses on the differences from the other embodiments. In particular, the method embodiments are generally similar to the methods described in the device and system embodiments, so their description is relatively simple. For relevant details, references to the descriptions of the other embodiments will suffice.

[0197] It should be understood that the foregoing description of this specification is based on specific embodiments. Other embodiments are within the scope of the claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0198] It should be understood that an element described herein in the singular or shown in the drawings as only one does not limit the number of the element to one. In addition, modules or elements described or shown herein as separate may be combined into a single module or element, and modules or elements described or shown herein as single may be split into multiple modules or elements.

[0199] It should also be understood that the terms and expressions used herein are for descriptive purposes only, and the one or more embodiments of this specification should not be limited to these terms and expressions. The use of these terms and expressions does not mean to exclude any equivalent features of the illustrations and descriptions (or portions thereof), and it should be recognized that various modifications that may exist should also be included in the scope of the claims. Other modifications, variations, and substitutions may also exist. Accordingly, the claims should be deemed to cover all such equivalents.

Claims

1. A cosine distance calculation unit for calculating the cosine distance between a first normalized vector and a second normalized vector, comprising 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, wherein 1≤i≤L, L is a positive integer, each multiplier receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input; the 2 L Every two multipliers in the multipliers are grouped together, corresponding to a first adder, and the outputs of the two multipliers in the group serve as inputs to the first adder; for the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped together, corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as inputs to the i+1th adder; the cosine distance is determined based on the output of the L-th adder layer; The cosine distance calculation unit further includes a first normalized vector cache unit and a second normalized vector cache unit, wherein: The first normalized vector cache unit is used to cache the first normalized vector, and the second normalized vector cache unit is used to cache the second normalized vector; the first normalized vector and the second normalized vector each have M vector elements, each vector element is 2 N The data channel bit width of each of the first normalized vector cache unit and the second normalized vector cache unit is 2 P ; N includes k increasing positive integers N1~N k Select from the set; L The multipliers include at least Has bit input terminal of the multiplier; the 2 L The multipliers include at least Has A multiplier with 1-bit inputs, where 1≤i≤k-1.

2. The cosine distance calculation unit according to claim 1, further comprising an accumulator, wherein: The output of the Lth adder is used as the input of the accumulator, and the accumulator accumulates the input received in K consecutive clock cycles as the calculation result of the cosine distance between the first normalized vector and the second normalized vector, where K=roundup(M / min(2 P-N ,2 L )), roundup() is the rounding up function.

3. The cosine distance calculation unit according to claim 1, wherein: In N=N i When the number of input bits is greater than or equal to bit multiplier bits are used for input vector elements where the number of bits at the input is greater than The post-multiplier bits are used for input vector elements, with the leading bit being the sign bit.

4. The cosine distance calculation unit according to claim 3, wherein: If the input vector element is a positive number, the sign bit is all 0s; if the input vector element is a negative number, the sign bit is all 1s.

5. An acceleration unit comprising: The cosine distance calculation unit according to any one of claims 1 to 4; The controller is used to control the operation of the cosine distance calculation unit.

6. The acceleration unit according to claim 5, further comprising: The standardized interface is controlled by the controller and can obtain the second normalized vector in different storage formats.

7. The acceleration unit according to claim 5, further comprising: The cosine distance sorting unit is used to sort the cosine distances determined by the cosine distance calculation unit for the first normalized vector and the multiple second normalized vectors.

8. The acceleration unit according to claim 7, wherein: The cosine distance sorting unit includes at least two cache units connected in series, each cache unit includes a cache space and a comparator, the cache space is used to cache at least one cosine distance and the identifier of the corresponding second normalized vector, and the comparator is used to compare the size relationship between the cosine distance received by the cache unit and the cosine distance cached in the cache space according to a predetermined rule, retain the cosine distance and the identifier of the corresponding second normalized vector that meet the predetermined rule in the cache space, and squeeze the cosine distance and the identifier of the corresponding second normalized vector that do not meet the predetermined rule out of the cache unit and transmit them to the next cache unit in series.

9. The acceleration unit according to claim 8, wherein: The predetermined rule includes: a larger cosine distance between a cosine distance received by the cache unit and a cosine distance cached in the cache space.

10. An electronic device comprising: The acceleration unit according to any one of claims 5 to 9; The processing unit is configured to normalize the first vector into the first normalized vector, normalize the second vector into the second normalized vector, and schedule the acceleration unit to perform a calculation of a cosine distance between the first normalized vector and the second normalized vector.

11. A cosine distance calculation method, comprising a cosine distance calculation unit for calculating the cosine distance between a first normalized vector and a second normalized vector, wherein the cosine distance calculation unit comprises 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i an i-th adder, wherein 1≤i≤L, L is a positive integer, and the cosine distance calculation unit further includes a first normalized vector cache unit and a second normalized vector cache unit, wherein, The first normalized vector cache unit is used to cache the first normalized vector, and the second normalized vector cache unit is used to cache the second normalized vector; the first normalized vector and the second normalized vector each have M vector elements, each vector element is 2 N The data channel bit width of each of the first normalized vector cache unit and the second normalized vector cache unit is 2 P ; N includes k increasing positive integers N1~N k Select from the set; L The multipliers include at least Has bit input terminal of the multiplier; the 2 L The multipliers include at least Has A multiplier having a plurality of bit input terminals, wherein 1≤i≤k-1, the method comprising: caching the first normalized vector by the first normalized vector cache unit, and caching the second normalized vector by the second normalized vector cache unit; receiving as input, via each multiplier, a vector element of the first normalized vector and a corresponding vector element of the second normalized vector; The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder; For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder; The cosine distance is determined according to the output of the Lth adder layer.

12. A matching method comprising: Acquire first data and second data to be matched, wherein the first data and the second data are one of the following: image data, text data, and voice data; Obtaining a first vector generated by performing feature extraction on the first data, and obtaining a second vector generated by performing feature extraction on the second data; Normalizing the first vector into a first normalized vector, and normalizing the second vector into a second normalized vector; The first normalized vector is cached by a first normalized vector cache unit included in the cosine distance calculation unit, and the second normalized vector is cached by a second normalized vector cache unit included in the cosine distance calculation unit; wherein the first normalized vector and the second normalized vector each have M vector elements, each vector element is 2 N The data channel bit width of each of the first normalized vector cache unit and the second normalized vector cache unit is 2 P ; Each multiplier included in the cosine distance calculation unit receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input, wherein the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i The i-th adder, where 1≤i≤L, L is a positive integer, and N is a positive integer N1 to N1. k Select from the set; L The multipliers include at least Has bit input terminal of the multiplier; the 2 L The multipliers include at least Has A multiplier with a bit input, where 1≤i≤k-1; The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder; For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder; determining a cosine distance between the first normalized vector and the second normalized vector according to an output of the Lth adder layer; When the cosine distance is less than a preset first distance threshold, it is determined that the first data matches the second data.

13. A clustering method comprising: Obtain multiple data to be clustered and cluster centers; Determine a first vector corresponding to the cluster center and a second vector corresponding to each of the data to be clustered; For each data to be clustered, execute: Normalizing the first vector into a first normalized vector, and normalizing the second vector corresponding to the data to be clustered into a second normalized vector; The first normalized vector is cached by a first normalized vector cache unit included in the cosine distance calculation unit, and the second normalized vector is cached by a second normalized vector cache unit included in the cosine distance calculation unit; wherein the first normalized vector and the second normalized vector each have M vector elements, each vector element is 2 N The data channel bit width of each of the first normalized vector cache unit and the second normalized vector cache unit is 2 P ; Each multiplier included in the cosine distance calculation unit receives a vector element of the first normalized vector and a corresponding vector element in the second normalized vector as input, wherein the cosine distance calculation unit includes 2 L multipliers, L adder layers, where the i-th adder layer includes 2 L-i The i-th adder, where 1≤i≤L, L is a positive integer, and N is a positive integer N1 to N1. k Select from the set; L The multipliers include at least Has bit input terminal of the multiplier; the 2 L The multipliers include at least Has A multiplier with a bit input, where 1≤i≤k-1; The 2 L Every two multipliers in the multipliers are grouped into one group, corresponding to one first adder, and the outputs of the two multipliers in the group serve as inputs of the first adder; For the i-th adder layer in the 1st to L-1th adder layers, every two i-th adders are grouped into a group corresponding to an i+1th adder in the i+1th adder layer, and the outputs of the two i-th adders in the group serve as the inputs of the i+1th adder; determining a cosine distance between the first normalized vector and the second normalized vector according to an output of the Lth adder layer; If the cosine distance is less than a preset second distance threshold, it is determined that the data to be clustered belongs to the cluster group corresponding to the cluster center.

Citation Information

Patent Citations

  • Fusion vector multiplier and operating method using same

    CN108733348A