File size: 596 Bytes
4c5d1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""Score padded embeddings using the local experimental MaxSim provider."""

from __future__ import annotations

from pathlib import Path

import torch
import torch.nn.functional as F
from kernels import get_local_kernel

kernel = get_local_kernel(Path(__file__).resolve().parent)
a = F.normalize(torch.randn(4, 32, 128, device="cuda"), dim=-1).half().requires_grad_()
b = F.normalize(torch.randn(8, 180, 128, device="cuda"), dim=-1).half().requires_grad_()
scores = kernel.layers.MaxSim()(a, b, None, None)
scores.sum().backward()
print(scores.shape, a.grad.norm().item(), b.grad.norm().item())