TensorTonic
Problems
Cosine Similarity as a CUDA Kernel
Linear AlgebraGPU Programming
Medium

Compute the cosine similarity between two equal-length vectors AAA and BBB. This is the cosine of the angle between them: their dot product divided by the product of their L2 norms.

sim(A,B)=∑i=0N−1A[i] B[i]∑i=0N−1A[i]2 ∑i=0N−1B[i]2\text{sim}(A, B) = \frac{\sum_{i=0}^{N-1} A[i]\,B[i]}{\sqrt{\sum_{i=0}^{N-1} A[i]^2}\,\sqrt{\sum_{i=0}^{N-1} B[i]^2}}sim(A,B)=∑i=0N−1​A[i]2​∑i=0N−1​B[i]2​∑i=0N−1​A[i]B[i]​

The result is a single scalar in [−1,1][-1, 1][−1,1] written to a 1-element device buffer. Computing it needs three reductions over the array: the dot product and both squared norms.

Loading visualization...

Examples

Input: N = 3, A = [1, 2, 3], B = [4, 5, 6]

Output: 0.97460.97460.9746

Dot product 323232 over norms 14 77≈32.83\sqrt{14}\,\sqrt{77} \approx 32.8314​77​≈32.83.

Input: N = 3, A = [1, 0, 0], B = [0, 1, 0]

Output: 0.00.00.0

Orthogonal vectors have zero cosine similarity.

Hint 1

All three reductions can share one pass: each thread accumulates A[i]B[i]A[i]B[i]A[i]B[i], A[i]2A[i]^2A[i]2, and B[i]2B[i]^2B[i]2 at once.

Hint 2

Use shared-memory partial sums plus atomicAdd\texttt{atomicAdd}atomicAdd into three scratch accumulators, then a tiny finalize step combines them.

Requirements

  • Reduce three quantities across the array: A⋅BA \cdot BA⋅B, ∥A∥2\lVert A \rVert^2∥A∥2, and ∥B∥2\lVert B \rVert^2∥B∥2
  • Combine them as dot over the product of norms
  • Write the scalar result to result[0]\texttt{result[0]}result[0]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • Neither input vector is all zeros (the similarity would be undefined)
  • AAA, BBB, and the result are device pointers
  • Values fit in 32-bit float
Try Similar Problems
Scaled Dot-Product Attention in CUDAHardRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumCross-Entropy Loss as a CUDA KernelMediumOuter Product as a CUDA KernelEasyTiled Matrix Multiplication as a CUDA KernelHardMatrix Addition as a CUDA KernelEasy

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Cosine Similarity as a CUDA Kernel

Cosine Similarity as a CUDA Kernel

Linear AlgebraGPU Programming
Medium

Compute the cosine similarity between two equal-length vectors AAA and BBB. This is the cosine of the angle between them: their dot product divided by the product of their L2 norms.

sim(A,B)=∑i=0N−1A[i] B[i]∑i=0N−1A[i]2 ∑i=0N−1B[i]2\text{sim}(A, B) = \frac{\sum_{i=0}^{N-1} A[i]\,B[i]}{\sqrt{\sum_{i=0}^{N-1} A[i]^2}\,\sqrt{\sum_{i=0}^{N-1} B[i]^2}}sim(A,B)=∑i=0N−1​A[i]2​∑i=0N−1​B[i]2​∑i=0N−1​A[i]B[i]​

The result is a single scalar in [−1,1][-1, 1][−1,1] written to a 1-element device buffer. Computing it needs three reductions over the array: the dot product and both squared norms.

Loading visualization...

Examples

Input: N = 3, A = [1, 2, 3], B = [4, 5, 6]

Output: 0.97460.97460.9746

Dot product 323232 over norms 14 77≈32.83\sqrt{14}\,\sqrt{77} \approx 32.8314​77​≈32.83.

Input: N = 3, A = [1, 0, 0], B = [0, 1, 0]

Output: 0.00.00.0

Orthogonal vectors have zero cosine similarity.

Hint 1

All three reductions can share one pass: each thread accumulates A[i]B[i]A[i]B[i]A[i]B[i], A[i]2A[i]^2A[i]2, and B[i]2B[i]^2B[i]2 at once.

Hint 2

Use shared-memory partial sums plus atomicAdd\texttt{atomicAdd}atomicAdd into three scratch accumulators, then a tiny finalize step combines them.

Requirements

  • Reduce three quantities across the array: A⋅BA \cdot BA⋅B, ∥A∥2\lVert A \rVert^2∥A∥2, and ∥B∥2\lVert B \rVert^2∥B∥2
  • Combine them as dot over the product of norms
  • Write the scalar result to result[0]\texttt{result[0]}result[0]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤1071 \le N \le 10^71≤N≤107
  • Neither input vector is all zeros (the similarity would be undefined)
  • AAA, BBB, and the result are device pointers
  • Values fit in 32-bit float
Try Similar Problems
Scaled Dot-Product Attention in CUDAHardRMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix Multiplication as a CUDA KernelMediumL1 Normalization as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumCross-Entropy Loss as a CUDA KernelMediumOuter Product as a CUDA KernelEasyTiled Matrix Multiplication as a CUDA KernelHardMatrix Addition as a CUDA KernelEasy

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1