setsuna 1.0.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.
setsuna/__init__.py ADDED
@@ -0,0 +1,12 @@
1
+ """SETSUNA: spike inference with temporal super-resolution for calcium imaging.
2
+
3
+ import setsuna
4
+ dff = setsuna.dff_from_fluorescence(F, Fneu, fps=30.0)
5
+ out = setsuna.infer(dff, fps=30.0, indicator="jGCaMP8s", tolerance_ms=10)
6
+
7
+ See docs/USER_GUIDE.md.
8
+ """
9
+ from .api import CELL_TYPES, INDICATORS, Session, dff_from_fluorescence, infer
10
+
11
+ __version__ = "1.0.0"
12
+ __all__ = ["dff_from_fluorescence", "infer", "Session", "INDICATORS", "CELL_TYPES", "__version__"]
setsuna/_cuda_rt.py ADDED
@@ -0,0 +1,251 @@
1
+ """Runtime-compiled CUDA kernels for SETSUNA, without a CUDA toolkit: NVRTC (the DLL PyTorch ships) compiles the
2
+ source to a cubin for this GPU, the driver API loads it, and launches go on PyTorch's current stream, so they are
3
+ ordered with torch work and captured by CUDA graphs like any other kernel. Compiled images are cached on disk by
4
+ source + options + GPU, so only the first use compiles.
5
+
6
+ k = CudaKernel(src, "name", options=("-default-device",))
7
+ k.launch(grid, block, shared_bytes, (tensor_a, tensor_b, (int(n), "i"), (1.5, "f")))
8
+
9
+ Arguments: tensors (their data pointers), (value, code) pairs with code "i" (int32), "q" (int64), "f" (float32),
10
+ "d" (float64), or
11
+ ctypes Structure / Array instances (by-value struct parameters, laid out as the kernel declares them).
12
+ Any failure raises CudaRtError; callers keep their Triton path then."""
13
+ from __future__ import annotations
14
+
15
+ import ctypes
16
+ import hashlib
17
+ import os
18
+ import sys
19
+ import threading
20
+ from pathlib import Path
21
+
22
+ import torch
23
+
24
+
25
+ class CudaRtError(RuntimeError):
26
+ pass
27
+
28
+
29
+ _LOCK = threading.Lock()
30
+ _LIBS: dict = {}
31
+
32
+
33
+ def _nvrtc():
34
+ lib = _LIBS.get("nvrtc")
35
+ if lib is None:
36
+ torch_lib = Path(torch.__file__).resolve().parent / "lib"
37
+ names = sorted(torch_lib.glob("nvrtc64_*.dll")) if os.name == "nt" else sorted(torch_lib.glob("libnvrtc*.so*"))
38
+ names = [n for n in names if ".alt." not in n.name] or names
39
+ if not names:
40
+ raise CudaRtError("no NVRTC library next to torch")
41
+ lib = ctypes.CDLL(str(names[0]))
42
+ lib.nvrtcGetErrorString.restype = ctypes.c_char_p
43
+ _LIBS["nvrtc"] = lib
44
+ return lib
45
+
46
+
47
+ def _cuda():
48
+ lib = _LIBS.get("cuda")
49
+ if lib is None:
50
+ lib = ctypes.WinDLL("nvcuda.dll") if os.name == "nt" else ctypes.CDLL("libcuda.so.1")
51
+ lib.cuLaunchKernel.argtypes = [ctypes.c_void_p, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint,
52
+ ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint,
53
+ ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p]
54
+ _LIBS["cuda"] = lib
55
+ return lib
56
+
57
+
58
+ def _cuda_held():
59
+ """The driver through ctypes.PyDLL, whose calls keep the interpreter lock: for launches, which return at once,
60
+ so that a thread does not have to win the lock back from busy threads after each one."""
61
+ lib = _LIBS.get("cuda_held")
62
+ if lib is None:
63
+ lib = ctypes.PyDLL("nvcuda.dll") if os.name == "nt" else ctypes.PyDLL("libcuda.so.1")
64
+ lib.cuLaunchKernel.argtypes = _cuda().cuLaunchKernel.argtypes
65
+ _LIBS["cuda_held"] = lib
66
+ return lib
67
+
68
+
69
+ def _make_current(device):
70
+ """Makes ``device``'s primary context current on this thread when none is. PyTorch makes it current on its own
71
+ CUDA calls, and a thread whose PyTorch work so far came from its caches has made none."""
72
+ lib = _cuda_held()
73
+ ctx = ctypes.c_void_p()
74
+ _check_cu(lib.cuCtxGetCurrent(ctypes.byref(ctx)), "cuCtxGetCurrent")
75
+ if ctx.value:
76
+ return
77
+ dev = ctypes.c_int()
78
+ _check_cu(lib.cuDeviceGet(ctypes.byref(dev), device.index if device.index is not None else torch.cuda.current_device()),
79
+ "cuDeviceGet")
80
+ _check_cu(lib.cuDevicePrimaryCtxRetain(ctypes.byref(ctx), dev), "cuDevicePrimaryCtxRetain")
81
+ try:
82
+ _check_cu(lib.cuCtxSetCurrent(ctx), "cuCtxSetCurrent")
83
+ finally:
84
+ lib.cuDevicePrimaryCtxRelease(dev) # PyTorch's own reference keeps the context
85
+
86
+
87
+ def _check_nvrtc(res, what):
88
+ if res != 0:
89
+ raise CudaRtError(f"{what}: {_nvrtc().nvrtcGetErrorString(res).decode(errors='replace')}")
90
+
91
+
92
+ def _check_cu(res, what):
93
+ if res != 0:
94
+ s = ctypes.c_char_p()
95
+ _cuda().cuGetErrorString(res, ctypes.byref(s))
96
+ raise CudaRtError(f"{what}: CUDA error {res} ({(s.value or b'?').decode(errors='replace')})")
97
+
98
+
99
+ def _cache_dir() -> Path:
100
+ base = os.environ.get("JSCOPE_CUDA_RT_CACHE") or os.path.join(os.environ.get("LOCALAPPDATA") or os.path.expanduser("~"),
101
+ "jupiterscope", "cuda_rt")
102
+ p = Path(base)
103
+ p.mkdir(parents=True, exist_ok=True)
104
+ return p
105
+
106
+
107
+ def compile_cubin(src: str, name: str, arch: str, options=()) -> bytes:
108
+ """NVRTC: CUDA C++ source -> a loadable image for ``arch``, cached on disk: a cubin for a real architecture
109
+ ("sm_120"), PTX (null-terminated; the driver compiles it for the GPU on load) for a virtual one ("compute_75")."""
110
+ opts = [f"--gpu-architecture={arch}"] + list(options)
111
+ ptx = arch.startswith("compute_")
112
+ key = hashlib.sha256(("\0".join([src, name] + opts) + "\0v1").encode()).hexdigest()[:32]
113
+ f = _cache_dir() / f"{name}_{arch}_{key}.{'ptx' if ptx else 'cubin'}"
114
+ if f.exists():
115
+ return f.read_bytes()
116
+ lib = _nvrtc()
117
+ prog = ctypes.c_void_p()
118
+ _check_nvrtc(lib.nvrtcCreateProgram(ctypes.byref(prog), src.encode(), (name + ".cu").encode(), 0, None, None),
119
+ "nvrtcCreateProgram")
120
+ try:
121
+ c_opts = (ctypes.c_char_p * len(opts))(*[o.encode() for o in opts])
122
+ res = lib.nvrtcCompileProgram(prog, len(opts), c_opts)
123
+ if res != 0:
124
+ n = ctypes.c_size_t()
125
+ lib.nvrtcGetProgramLogSize(prog, ctypes.byref(n))
126
+ log = ctypes.create_string_buffer(n.value + 1)
127
+ lib.nvrtcGetProgramLog(prog, log)
128
+ raise CudaRtError(f"NVRTC compile of {name} failed:\n{log.value.decode(errors='replace')}")
129
+ n = ctypes.c_size_t()
130
+ if ptx:
131
+ _check_nvrtc(lib.nvrtcGetPTXSize(prog, ctypes.byref(n)), "nvrtcGetPTXSize")
132
+ buf = ctypes.create_string_buffer(n.value)
133
+ _check_nvrtc(lib.nvrtcGetPTX(prog, buf), "nvrtcGetPTX")
134
+ else:
135
+ _check_nvrtc(lib.nvrtcGetCUBINSize(prog, ctypes.byref(n)), "nvrtcGetCUBINSize")
136
+ buf = ctypes.create_string_buffer(n.value)
137
+ _check_nvrtc(lib.nvrtcGetCUBIN(prog, buf), "nvrtcGetCUBIN")
138
+ img = buf.raw
139
+ finally:
140
+ lib.nvrtcDestroyProgram(ctypes.byref(prog))
141
+ tmp = f.with_suffix(".tmp%d" % os.getpid())
142
+ tmp.write_bytes(img)
143
+ os.replace(tmp, f)
144
+ return img
145
+
146
+
147
+ def supported_archs() -> tuple:
148
+ """The architectures (as 75, 80, ..., 120) this NVRTC compiles for; () when it cannot say."""
149
+ got = _LIBS.get("archs")
150
+ if got is None:
151
+ try:
152
+ lib = _nvrtc()
153
+ n = ctypes.c_int()
154
+ _check_nvrtc(lib.nvrtcGetNumSupportedArchs(ctypes.byref(n)), "nvrtcGetNumSupportedArchs")
155
+ arr = (ctypes.c_int * n.value)()
156
+ _check_nvrtc(lib.nvrtcGetSupportedArchs(arr), "nvrtcGetSupportedArchs")
157
+ got = tuple(sorted(int(v) for v in arr))
158
+ except Exception:
159
+ got = ()
160
+ _LIBS["archs"] = got
161
+ return got
162
+
163
+
164
+ #: JSCOPE_CUDA_RT_ARCH=compute_75 (a test setting): every kernel compiled as PTX for that virtual architecture, which
165
+ #: the driver compiles for the GPU on load -- an older architecture's code paths (__CUDA_ARCH__) run on a newer GPU.
166
+ ARCH_ENV = "JSCOPE_CUDA_RT_ARCH"
167
+
168
+
169
+ def device_arch(device) -> str:
170
+ """NVRTC's target for ``device``: its own architecture ("sm_89") when NVRTC knows it; else PTX for the newest
171
+ architecture NVRTC knows below it ("compute_120" on a cc 12.1 GPU with NVRTC 12.8), which the driver compiles for
172
+ the GPU on load. JSCOPE_CUDA_RT_ARCH overrides it (tests)."""
173
+ forced = os.environ.get(ARCH_ENV, "").strip()
174
+ if forced:
175
+ return forced
176
+ major, minor = torch.cuda.get_device_capability(device)
177
+ cc = major * 10 + minor
178
+ archs = supported_archs()
179
+ if not archs or cc in archs:
180
+ return f"sm_{major}{minor}"
181
+ lower = [a for a in archs if a <= cc]
182
+ if not lower:
183
+ return f"sm_{major}{minor}" # (older than NVRTC knows: the compile fails, callers fall back)
184
+ return f"compute_{lower[-1]}"
185
+
186
+
187
+ def device_cc(device) -> int:
188
+ """The compute capability the kernels are compiled for, as 75, 89, 120: JSCOPE_CUDA_RT_ARCH's when set, else the
189
+ device's -- for sources that pick a variant by architecture before compiling."""
190
+ forced = os.environ.get(ARCH_ENV, "").strip()
191
+ if forced:
192
+ return int(forced.split("_")[-1])
193
+ major, minor = torch.cuda.get_device_capability(device)
194
+ return major * 10 + minor
195
+
196
+
197
+ _ARG_TYPES = {"i": ctypes.c_int32, "q": ctypes.c_int64, "f": ctypes.c_float, "d": ctypes.c_double}
198
+ # (absent from torch builds without CUDA -- macOS's -- where no kernel is ever launched)
199
+ _current_raw_stream = getattr(torch._C, "_cuda_getCurrentRawStream", None)
200
+
201
+
202
+ class CudaKernel:
203
+ """One kernel of a runtime-compiled module, loaded in the current CUDA context of ``device``."""
204
+
205
+ def __init__(self, src: str, name: str, device=None, options=(), max_dynamic_smem: int = 0):
206
+ dev = torch.device("cuda" if device is None else device)
207
+ with torch.cuda.device(dev):
208
+ torch.cuda.current_stream(dev)
209
+ # PyTorch's primary context, current on this thread even when PyTorch has made no CUDA call here
210
+ _make_current(dev if dev.index is not None else torch.device("cuda", torch.cuda.current_device()))
211
+ img = compile_cubin(src, name, device_arch(dev), options)
212
+ cu = _cuda()
213
+ self._mod = ctypes.c_void_p()
214
+ self._img = ctypes.create_string_buffer(img, len(img))
215
+ _check_cu(cu.cuModuleLoadData(ctypes.byref(self._mod), self._img), f"cuModuleLoadData({name})")
216
+ self._fn = ctypes.c_void_p()
217
+ _check_cu(cu.cuModuleGetFunction(ctypes.byref(self._fn), self._mod, name.encode()),
218
+ f"cuModuleGetFunction({name})")
219
+ if max_dynamic_smem > 48 * 1024:
220
+ # CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES = 8
221
+ _check_cu(cu.cuFuncSetAttribute(self._fn, 8, int(max_dynamic_smem)), "cuFuncSetAttribute")
222
+ self.name = name
223
+ self.device = dev
224
+ self._what = f"cuLaunchKernel({name})"
225
+
226
+ def launch(self, grid, block, shared_bytes: int, args, stream=None, hold=False) -> None:
227
+ """``hold`` keeps the interpreter lock through the launch call."""
228
+ holders = []
229
+ params = (ctypes.c_void_p * len(args))()
230
+ for k, a in enumerate(args):
231
+ if isinstance(a, torch.Tensor):
232
+ h = ctypes.c_void_p(a.data_ptr())
233
+ elif isinstance(a, (ctypes.Structure, ctypes.Array)):
234
+ h = a # a by-value struct / array parameter
235
+ else:
236
+ v, code = a
237
+ h = _ARG_TYPES[code](v)
238
+ holders.append(h)
239
+ params[k] = ctypes.addressof(h)
240
+ if stream is not None:
241
+ handle = stream.cuda_stream
242
+ else: # the current stream's handle, as current_stream gives it
243
+ index = self.device.index
244
+ handle = _current_raw_stream(index if index is not None else torch.cuda.current_device())
245
+ g = tuple(grid) + (1,) * (3 - len(grid))
246
+ b = tuple(block) + (1,) * (3 - len(block))
247
+ if hold:
248
+ _make_current(self.device)
249
+ _check_cu((_cuda_held() if hold else _cuda()).cuLaunchKernel(
250
+ self._fn, g[0], g[1], g[2], b[0], b[1], b[2], int(shared_bytes), ctypes.c_void_p(handle), params, None),
251
+ self._what)