Spaces:
Sleeping
Sleeping
MaduRox commited on
Commit ·
abbc3ec
1
Parent(s): ef0b4f4
fix: bridge KalpanaRIFTensor to native kalpana.core kernel without kwargs mismatch
Browse files- kalpana/__pycache__/__init__.cpython-312.pyc +0 -0
- kalpana/__pycache__/core.cpython-312.pyc +0 -0
- kalpana/__pycache__/integrations.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/__pycache__/__init__.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/__pycache__/attention.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/__pycache__/core.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/__pycache__/extractor.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/__pycache__/kv_cache.cpython-312.pyc +0 -0
- kalpana_embed_to_kv/core.py +106 -8
- kalpana_embed_to_kv/core.py.metadata.json +4 -0
kalpana/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (465 Bytes). View file
|
|
|
kalpana/__pycache__/core.cpython-312.pyc
ADDED
|
Binary file (3.41 kB). View file
|
|
|
kalpana/__pycache__/integrations.cpython-312.pyc
ADDED
|
Binary file (3.42 kB). View file
|
|
|
kalpana_embed_to_kv/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (882 Bytes). View file
|
|
|
kalpana_embed_to_kv/__pycache__/attention.cpython-312.pyc
ADDED
|
Binary file (8.81 kB). View file
|
|
|
kalpana_embed_to_kv/__pycache__/core.cpython-312.pyc
ADDED
|
Binary file (7.86 kB). View file
|
|
|
kalpana_embed_to_kv/__pycache__/extractor.cpython-312.pyc
ADDED
|
Binary file (5.38 kB). View file
|
|
|
kalpana_embed_to_kv/__pycache__/kv_cache.cpython-312.pyc
ADDED
|
Binary file (25.4 kB). View file
|
|
|
kalpana_embed_to_kv/core.py
CHANGED
|
@@ -8,17 +8,115 @@ import torch
|
|
| 8 |
import torch.nn as nn
|
| 9 |
|
| 10 |
try:
|
| 11 |
-
from kalpana.core import
|
| 12 |
except ImportError:
|
| 13 |
try:
|
| 14 |
-
from kalpana.core import
|
| 15 |
except ImportError:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class EmbedToKVMatrix(nn.Module):
|
|
|
|
| 8 |
import torch.nn as nn
|
| 9 |
|
| 10 |
try:
|
| 11 |
+
from kalpana.core import KalpanaRIFTensor as _NativeKernel
|
| 12 |
except ImportError:
|
| 13 |
try:
|
| 14 |
+
from kalpana.core import KalpanaEngineTensor as _NativeKernel
|
| 15 |
except ImportError:
|
| 16 |
+
_NativeKernel = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class KalpanaRIFTensor(nn.Module):
|
| 20 |
+
"""
|
| 21 |
+
Standard interface adapter for the Kalpanā Native Kernel.
|
| 22 |
+
"""
|
| 23 |
+
def __init__(
|
| 24 |
+
self,
|
| 25 |
+
batch_size: int = 1,
|
| 26 |
+
num_heads: int = 8,
|
| 27 |
+
bands: int = 512,
|
| 28 |
+
dim: int = 128,
|
| 29 |
+
kappa: float = 1.0,
|
| 30 |
+
min_freq: float = 0.1,
|
| 31 |
+
max_freq: float = 10.0,
|
| 32 |
+
device: Union[str, torch.device] = 'cpu',
|
| 33 |
+
dtype: torch.dtype = torch.float32,
|
| 34 |
+
):
|
| 35 |
+
super().__init__()
|
| 36 |
+
self.batch_size = batch_size
|
| 37 |
+
self.num_heads = num_heads
|
| 38 |
+
self.bands = bands
|
| 39 |
+
self.dim = dim
|
| 40 |
+
self.kappa = kappa
|
| 41 |
+
self.device = device
|
| 42 |
+
self.dtype = dtype
|
| 43 |
+
self.seen_tokens = 0
|
| 44 |
+
|
| 45 |
+
if _NativeKernel is None:
|
| 46 |
+
raise ImportError(
|
| 47 |
+
"Kalpanā Native Kernel is required to initialize KalpanaRIFTensor. "
|
| 48 |
+
"Please install kalpana_sdk_enterprise."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Initialize native kernel without kwargs that native __init__ doesn't accept
|
| 52 |
+
try:
|
| 53 |
+
self._kernel = _NativeKernel(
|
| 54 |
+
batch_size=batch_size,
|
| 55 |
+
num_heads=num_heads,
|
| 56 |
+
bands=bands,
|
| 57 |
+
dim=dim,
|
| 58 |
+
kappa=kappa,
|
| 59 |
+
min_freq=min_freq,
|
| 60 |
+
max_freq=max_freq,
|
| 61 |
+
device=device,
|
| 62 |
+
)
|
| 63 |
+
except TypeError:
|
| 64 |
+
self._kernel = _NativeKernel(
|
| 65 |
+
batch_size=batch_size,
|
| 66 |
+
num_heads=num_heads,
|
| 67 |
+
bands=bands,
|
| 68 |
+
dim=dim,
|
| 69 |
+
device=device,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def write(self, t: Union[int, float, torch.Tensor], vector: torch.Tensor) -> None:
|
| 73 |
+
"""Writes token vector into native RIF kernel."""
|
| 74 |
+
# Ensure vector shape is [batch, heads, seq_len, dim] or [batch, heads, dim]
|
| 75 |
+
if vector.dim() == 3:
|
| 76 |
+
# [batch, heads, dim] -> [batch, heads, 1, dim]
|
| 77 |
+
vec_4d = vector.unsqueeze(2)
|
| 78 |
+
elif vector.dim() == 2:
|
| 79 |
+
# [batch, dim] -> [batch, num_heads, 1, dim]
|
| 80 |
+
vec_4d = vector.unsqueeze(1).unsqueeze(2).expand(-1, self.num_heads, -1, -1)
|
| 81 |
+
else:
|
| 82 |
+
vec_4d = vector
|
| 83 |
+
|
| 84 |
+
start_t = int(t) if not isinstance(t, torch.Tensor) else int(t.item())
|
| 85 |
+
|
| 86 |
+
if hasattr(self._kernel, "write_rif"):
|
| 87 |
+
self._kernel.write_rif(start_t, vec_4d.to(self.device, dtype=torch.float32))
|
| 88 |
+
elif hasattr(self._kernel, "write"):
|
| 89 |
+
self._kernel.write(start_t, vec_4d)
|
| 90 |
+
|
| 91 |
+
self.seen_tokens += vec_4d.shape[2]
|
| 92 |
+
|
| 93 |
+
def batch_reconstruct(self, t_range: torch.Tensor) -> torch.Tensor:
|
| 94 |
+
"""Reconstructs past key/value vectors from native RIF kernel."""
|
| 95 |
+
max_t = len(t_range)
|
| 96 |
+
if hasattr(self._kernel, "reconstruct_all"):
|
| 97 |
+
out = self._kernel.reconstruct_all(max_t)
|
| 98 |
+
return out.to(self.device, dtype=self.dtype)
|
| 99 |
+
elif hasattr(self._kernel, "batch_reconstruct"):
|
| 100 |
+
return self._kernel.batch_reconstruct(t_range).to(self.device, dtype=self.dtype)
|
| 101 |
+
elif hasattr(self._kernel, "reconstruct"):
|
| 102 |
+
# reconstruct per step
|
| 103 |
+
recons = [self._kernel.reconstruct(i) for i in range(max_t)]
|
| 104 |
+
return torch.stack(recons, dim=2).to(self.device, dtype=self.dtype)
|
| 105 |
+
raise AttributeError("Native kernel has no reconstruction method")
|
| 106 |
+
|
| 107 |
+
def memory_footprint_mb(self) -> float:
|
| 108 |
+
"""Calculates constant O(1) memory footprint."""
|
| 109 |
+
if hasattr(self._kernel, "memory_footprint_mb"):
|
| 110 |
+
return self._kernel.memory_footprint_mb()
|
| 111 |
+
# Fallback calculation based on tensor dimensions: real + imag buffers
|
| 112 |
+
total_elements = self.batch_size * self.num_heads * self.bands * self.dim * 2
|
| 113 |
+
element_size = 4 # float32
|
| 114 |
+
return (total_elements * element_size) / (1024 * 1024)
|
| 115 |
+
|
| 116 |
+
def reset(self) -> None:
|
| 117 |
+
if hasattr(self._kernel, "reset"):
|
| 118 |
+
self._kernel.reset()
|
| 119 |
+
self.seen_tokens = 0
|
| 120 |
|
| 121 |
|
| 122 |
class EmbedToKVMatrix(nn.Module):
|
kalpana_embed_to_kv/core.py.metadata.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"summary": "Bridge KalpanaRIFTensor from kalpana.core to kv_cache interface seamlessly",
|
| 3 |
+
"updatedAt": "2026-08-21T08:37:30.581175900Z"
|
| 4 |
+
}
|