memorysafe 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
memorysafe/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ __version__ = "0.1.0"
2
+
3
+ from .buffer import TasteMemorySafeBuffer
4
+
5
+ __all__ = ["__version__", "TasteMemorySafeBuffer"]
memorysafe/buffer.py ADDED
@@ -0,0 +1,91 @@
1
+ from dataclasses import dataclass
2
+ from typing import List, Optional, Tuple, Dict
3
+ import numpy as np
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+
8
+ @dataclass
9
+ class MemoryItem:
10
+ x: torch.Tensor
11
+ y: int
12
+ protect_score: float
13
+ seen_step: int
14
+
15
+
16
+ class TasteMemorySafeBuffer:
17
+ """
18
+ Public demo buffer (non-IP). Not the proprietary MemorySafe policy/MVI.
19
+ Keeps top protect_score items under fixed capacity and replays samples.
20
+ """
21
+
22
+ def __init__(self, capacity: int = 300):
23
+ self.capacity = int(capacity)
24
+ self.items: List[MemoryItem] = []
25
+ self.step = 0
26
+
27
+ @torch.no_grad()
28
+ def compute_protect_score(self, y: torch.Tensor, logits: torch.Tensor) -> torch.Tensor:
29
+ probs = F.softmax(logits, dim=1)
30
+ p_true = probs.gather(1, y.view(-1, 1)).squeeze(1)
31
+
32
+ loss_proxy = (-torch.log(p_true + 1e-8))
33
+ top2 = torch.topk(probs, k=2, dim=1).values
34
+ margin = (top2[:, 0] - top2[:, 1]).clamp(min=0.0)
35
+
36
+ loss_n = (loss_proxy / (loss_proxy.max() + 1e-8)).clamp(0, 1)
37
+ margin_n = (1.0 - (margin / (margin.max() + 1e-8))).clamp(0, 1)
38
+
39
+ score = 0.6 * loss_n + 0.4 * margin_n
40
+ return score.clamp(0, 1)
41
+
42
+ def add_batch(self, x: torch.Tensor, y: torch.Tensor, score: torch.Tensor) -> None:
43
+ x = x.detach().cpu()
44
+ y = y.detach().cpu()
45
+ score = score.detach().cpu()
46
+
47
+ for i in range(x.size(0)):
48
+ self.items.append(
49
+ MemoryItem(
50
+ x=x[i],
51
+ y=int(y[i]),
52
+ protect_score=float(score[i].item()),
53
+ seen_step=self.step,
54
+ )
55
+ )
56
+ self.step += 1
57
+
58
+ if len(self.items) > self.capacity:
59
+ self.items.sort(key=lambda it: it.protect_score, reverse=True)
60
+ self.items = self.items[: self.capacity]
61
+
62
+ def sample(
63
+ self,
64
+ n: int = 64,
65
+ device: Optional[torch.device] = None,
66
+ ) -> Optional[Tuple[torch.Tensor, torch.Tensor]]:
67
+ if not self.items:
68
+ return None
69
+
70
+ n = min(int(n), len(self.items))
71
+ idx = np.random.choice(len(self.items), size=n, replace=False)
72
+
73
+ xs = torch.stack([self.items[i].x for i in idx])
74
+ ys = torch.tensor([self.items[i].y for i in idx], dtype=torch.long)
75
+
76
+ if device is not None:
77
+ xs = xs.to(device)
78
+ ys = ys.to(device)
79
+
80
+ return xs, ys
81
+
82
+ def stats(self) -> Dict[str, float]:
83
+ if not self.items:
84
+ return {"size": 0.0, "avg_score": 0.0, "p90_score": 0.0}
85
+
86
+ scores = np.array([it.protect_score for it in self.items], dtype=float)
87
+ return {
88
+ "size": float(len(self.items)),
89
+ "avg_score": float(scores.mean()),
90
+ "p90_score": float(np.percentile(scores, 90)),
91
+ }
memorysafe/demo.py ADDED
File without changes
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.4
2
+ Name: memorysafe
3
+ Version: 0.1.0
4
+ Summary: MemorySafe: MVI-driven memory governance for continual learning systems.
5
+ Author: Carla P. Centeno
6
+ License: MIT
7
+ Requires-Python: >=3.10
8
+ License-File: LICENSE
9
+ Requires-Dist: mvi-metrics
10
+ Requires-Dist: numpy
11
+ Dynamic: license-file
@@ -0,0 +1,8 @@
1
+ memorysafe/__init__.py,sha256=KcSUwIvlc0YX4mh67CsZsyuQA0CoaGZgQeoouGMzBo8,117
2
+ memorysafe/buffer.py,sha256=Vs720tsYK84AjUreHbZaN84tPuAEGwh51nPIjWbRg2c,2830
3
+ memorysafe/demo.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ memorysafe-0.1.0.dist-info/licenses/LICENSE,sha256=IcOH5RGd5Sd4PIdCEkPpG-ibb5qoP_R4DMTdKjoZ2bk,634
5
+ memorysafe-0.1.0.dist-info/METADATA,sha256=FJBfwHJ5_N55B5XwJRbksJXTa9Oh1yGwLNMKZEKdkw0,290
6
+ memorysafe-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
7
+ memorysafe-0.1.0.dist-info/top_level.txt,sha256=8vY8F97skgmHRq2AfHyyp9NUuyO8mBoD6RZLB-nf7_c,11
8
+ memorysafe-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,17 @@
1
+ # Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ Copyright 2026 MemorySafe Labs
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
@@ -0,0 +1 @@
1
+ memorysafe