jcandane commited on
Commit
1c256d9
·
verified ·
1 Parent(s): f592d69

Delete src/dima/dimax.py

Browse files
Files changed (1) hide show
  1. src/dima/dimax.py +0 -597
src/dima/dimax.py DELETED
@@ -1,597 +0,0 @@
1
- # src/dima/dima.py
2
- from __future__ import annotations
3
-
4
- import json
5
- import time
6
- from dataclasses import asdict, dataclass
7
- from typing import Any, Dict, Optional, Union
8
-
9
- import numpy as np
10
-
11
- import jax
12
- import jax.numpy as jnp
13
- from jax import random
14
-
15
- from flax import serialization as flax_ser
16
-
17
- from .dmap import DMAP
18
- from .gplm import GPLM
19
- from .ddpm import DDPM
20
- from .ann import ANNBackend, make_ann
21
-
22
-
23
- # ----------------------------
24
- # Optional: Hugging Face Hub
25
- # ----------------------------
26
- try:
27
- from huggingface_hub import HfApi, HfFolder, upload_file, hf_hub_download
28
- _HAS_HF = True
29
- except Exception:
30
- _HAS_HF = False
31
-
32
-
33
- def _select_device(prefer: str = "auto"):
34
- """
35
- Safe JAX device selection.
36
- prefer: "auto" | "gpu" | "cpu"
37
- """
38
- prefer = (prefer or "auto").lower()
39
- devs = jax.devices()
40
- gpu = [d for d in devs if d.platform == "gpu"]
41
- cpu = [d for d in devs if d.platform == "cpu"]
42
-
43
- if prefer in ("auto", "gpu"):
44
- return gpu[0] if gpu else (cpu[0] if cpu else devs[0])
45
- if prefer == "cpu":
46
- return cpu[0] if cpu else devs[0]
47
- # unknown -> auto
48
- return gpu[0] if gpu else (cpu[0] if cpu else devs[0])
49
-
50
-
51
- def _np_dtype_str(x) -> str:
52
- try:
53
- return str(np.dtype(x))
54
- except Exception:
55
- return "float32"
56
-
57
-
58
- # ----------------------------
59
- # Frozen inference-only models
60
- # ----------------------------
61
- class FrozenDMAP:
62
- """
63
- Inference-only Nyström DMAP embedder built from saved DMAP state.
64
- Uses kNN in ambient space against reference points.
65
- """
66
-
67
- def __init__(
68
- self,
69
- state: Dict[str, Any],
70
- *,
71
- ann_backend: ANNBackend = "auto",
72
- ann_params: Optional[Dict[str, Any]] = None,
73
- n_jobs: int = -1,
74
- ):
75
- self.k = int(state["k"])
76
- self.beta = float(state["beta"])
77
- self.alpha = float(state["alpha"])
78
- self.eps = float(state["eps"])
79
- self.dtype = np.dtype(state.get("dtype", "float32"))
80
-
81
- self.R_iX = np.ascontiguousarray(np.asarray(state["R_iX"]).astype(self.dtype, copy=False))
82
- self.qalpha_i = np.asarray(state["qalpha_i"], dtype=np.float64)
83
- self.R_over_lam_ix = np.asarray(state["R_over_lam_ix"], dtype=np.float64) # (Nref,d)
84
-
85
- self.ann, self.ann_backend = make_ann(ann_backend, ann_params=ann_params, n_jobs=n_jobs)
86
- self.ann.build(self.R_iX)
87
-
88
- def __call__(self, R_aX: Union[np.ndarray, list], *, batch_size: Optional[int] = None) -> np.ndarray:
89
- R_aX = np.asarray(R_aX)
90
- single = (R_aX.ndim == 1)
91
- if single:
92
- R_aX = R_aX[None, :]
93
-
94
- R_aX = np.ascontiguousarray(R_aX.astype(self.dtype, copy=False))
95
-
96
- if batch_size is None:
97
- Z = self._embed(R_aX)
98
- else:
99
- out = []
100
- bs = int(batch_size)
101
- for s in range(0, R_aX.shape[0], bs):
102
- out.append(self._embed(R_aX[s : s + bs]))
103
- Z = np.vstack(out)
104
-
105
- return Z[0] if single else Z
106
-
107
- def _embed(self, R_aX: np.ndarray) -> np.ndarray:
108
- j_aK, D2_aK = self.ann.search(R_aX, self.k) # (a,k)
109
- K_ai = np.exp(-self.beta * (D2_aK.astype(np.float64) / self.eps)) # (a,k)
110
-
111
- q_a = np.maximum(K_ai.sum(axis=1), 1e-30)
112
- qalpha_a = np.maximum(np.power(q_a, self.alpha), 1e-30)
113
-
114
- qalpha_i = np.maximum(self.qalpha_i[j_aK], 1e-30)
115
- Kalpha_ai = K_ai / (qalpha_a[:, None] * qalpha_i)
116
-
117
- d_a = np.maximum(Kalpha_ai.sum(axis=1), 1e-30)
118
- P_ai = Kalpha_ai / d_a[:, None]
119
-
120
- R_over = self.R_over_lam_ix[j_aK, :] # (a,k,d)
121
- Z_ax = (P_ai[:, :, None] * R_over).sum(axis=1) # (a,d)
122
- return Z_ax
123
-
124
-
125
- class FrozenGPLM:
126
- """
127
- Inference-only GPLM decoder built from saved GPLM state.
128
- Supports optional pred_k via ANN over inducing points.
129
- """
130
-
131
- def __init__(
132
- self,
133
- state: Dict[str, Any],
134
- *,
135
- ann_backend: ANNBackend = "auto",
136
- ann_params: Optional[Dict[str, Any]] = None,
137
- n_jobs: int = -1,
138
- ):
139
- self.beta = float(state["beta"])
140
- self.eps = float(state["eps"])
141
- self.pred_k = None if state.get("pred_k", None) is None else int(state["pred_k"])
142
- self.dtype = np.dtype(state.get("dtype", "float32"))
143
-
144
- self.mean_X = np.asarray(state["mean_X"], dtype=np.float64)
145
- self.M_mX = np.asarray(state["M_mX"], dtype=np.float64)
146
-
147
- # latent whitening used at training time
148
- self.lat_mean_x = np.asarray(state["lat_mean_x"], dtype=np.float64)
149
- self.lat_std_x = np.asarray(state["lat_std_x"], dtype=np.float64)
150
-
151
- # inducing points in whitened latent space
152
- self.Z_mx_w = np.ascontiguousarray(np.asarray(state["Z_mx_w"]).astype(np.float64, copy=False))
153
- self.m = int(self.Z_mx_w.shape[0])
154
-
155
- self.ann_Z, self.ann_backend = make_ann(ann_backend, ann_params=ann_params, n_jobs=n_jobs)
156
- self.ann_Z.build(self.Z_mx_w.astype(self.dtype, copy=False))
157
-
158
- def __call__(self, R_ax: Union[np.ndarray, list], *, batch_size: Optional[int] = None) -> np.ndarray:
159
- R_ax = np.asarray(R_ax)
160
- single = (R_ax.ndim == 1)
161
- if single:
162
- R_ax = R_ax[None, :]
163
- R_ax = np.ascontiguousarray(R_ax.astype(self.dtype, copy=False))
164
-
165
- if batch_size is None:
166
- Y = self._decode(R_ax)
167
- else:
168
- out = []
169
- bs = int(batch_size)
170
- for s in range(0, R_ax.shape[0], bs):
171
- out.append(self._decode(R_ax[s : s + bs]))
172
- Y = np.vstack(out)
173
-
174
- return Y[0] if single else Y
175
-
176
- @staticmethod
177
- def _sqdist_ab(A: np.ndarray, B: np.ndarray) -> np.ndarray:
178
- A = np.asarray(A, dtype=np.float64)
179
- B = np.asarray(B, dtype=np.float64)
180
- A2 = np.sum(A * A, axis=1, keepdims=True)
181
- B2 = np.sum(B * B, axis=1, keepdims=True).T
182
- G = A @ B.T
183
- return np.maximum(A2 + B2 - 2.0 * G, 0.0)
184
-
185
- def _decode(self, R_ax: np.ndarray) -> np.ndarray:
186
- Za = R_ax.astype(np.float64)
187
- Za_w = (Za - self.lat_mean_x[None, :]) / self.lat_std_x[None, :]
188
-
189
- if self.pred_k is None or self.pred_k >= self.m:
190
- D2_am = self._sqdist_ab(Za_w, self.Z_mx_w)
191
- C_am = np.exp(-self.beta * (D2_am / self.eps))
192
- Y = C_am @ self.M_mX
193
- else:
194
- j_aK, D2_aK = self.ann_Z.search(Za_w.astype(self.dtype, copy=False), self.pred_k)
195
- W = np.exp(-self.beta * (D2_aK.astype(np.float64) / self.eps)) # (a,k)
196
- M = self.M_mX[j_aK] # (a,k,D)
197
- Y = np.sum(W[:, :, None] * M, axis=1) # (a,D)
198
-
199
- return Y + self.mean_X[None, :]
200
-
201
-
202
- # ----------------------------
203
- # Config
204
- # ----------------------------
205
- @dataclass
206
- class DIMAConfig:
207
- d: int = 32
208
- ddpm_device: str = "auto" # "auto" | "cpu" | "gpu"
209
- # purely informational defaults (actual settings live in kwargs dicts)
210
- version: str = "0.1.0"
211
-
212
-
213
- # ----------------------------
214
- # Main wrapper
215
- # ----------------------------
216
- class DIMA:
217
- """
218
- DIMA: Diffusion–Intrinsic Manifold Autoencoder
219
-
220
- Pipeline:
221
- - DMAP encoder (NumPy/SciPy, CPU): ambient -> raw latent
222
- - GPLM decoder (NumPy/SciPy, CPU): raw latent -> ambient
223
- - DDPM latent model (JAX/Flax, CPU or GPU): trained on *normalized* latents
224
-
225
- API:
226
- model = DIMA(R_iX, dmap_kwargs=..., gplm_kwargs=..., ddpm_kwargs=..., ddpm_device="auto")
227
- Z = model.encode(X) # normalized latents (jnp on ddpm device)
228
- Xhat = model.decode(Z) # ambient (np on CPU), optionally with DDPM refinement
229
- Xhat2 = model(X) # polymorphic: ambient -> latent
230
- Xhat3 = model(Z) # polymorphic: latent -> ambient (refine=True default)
231
- Xsamp = model.sample(1000) # unconditional samples (ambient np)
232
- """
233
-
234
- def __init__(
235
- self,
236
- R_iX: np.ndarray,
237
- *,
238
- d: int = 32,
239
- dmap_kwargs: Optional[Dict[str, Any]] = None,
240
- gplm_kwargs: Optional[Dict[str, Any]] = None,
241
- ddpm_kwargs: Optional[Dict[str, Any]] = None,
242
- ddpm_device: str = "auto",
243
- key: Optional[jax.Array] = None ):
244
- self.config = DIMAConfig(d=int(d), ddpm_device=str(ddpm_device))
245
- self.training_time = 0.0
246
-
247
- t0 = time.time()
248
-
249
- # data
250
- self.R_iX = np.asarray(R_iX)
251
- if self.R_iX.ndim != 2:
252
- raise ValueError("R_iX must be 2D (N,D).")
253
- self.N, self.D = self.R_iX.shape
254
- self.d = int(d)
255
-
256
- dmap_kwargs = {} if dmap_kwargs is None else dict(dmap_kwargs)
257
- gplm_kwargs = {} if gplm_kwargs is None else dict(gplm_kwargs)
258
- ddpm_kwargs = {} if ddpm_kwargs is None else dict(ddpm_kwargs)
259
-
260
- # choose device for DDPM
261
- self.ddpm_device = _select_device(self.config.ddpm_device)
262
- self.cpu_device = _select_device("cpu")
263
-
264
- if key is None:
265
- key = random.PRNGKey(0)
266
- self.rng = key
267
-
268
- # -------------------------
269
- # 1) Train DMAP (CPU)
270
- # -------------------------
271
- self.enc = DMAP(self.R_iX, d=self.d, **dmap_kwargs)
272
- R_ix = np.asarray(self.enc(self.R_iX)) # raw latents (N,d) float64
273
-
274
- # -------------------------
275
- # 2) Fit latent normalization (ONCE)
276
- # -------------------------
277
- self.lat_mean_np = R_ix.mean(axis=0, keepdims=True).astype(np.float64)
278
- self.lat_std_np = (R_ix.std(axis=0, keepdims=True) + 1e-6).astype(np.float64)
279
-
280
- Z_ix = (R_ix - self.lat_mean_np) / self.lat_std_np # normalized latents (np)
281
-
282
- # keep norms on ddpm device too
283
- self.lat_mean_j = jax.device_put(jnp.asarray(self.lat_mean_np, dtype=jnp.float32), self.ddpm_device)
284
- self.lat_std_j = jax.device_put(jnp.asarray(self.lat_std_np, dtype=jnp.float32), self.ddpm_device)
285
-
286
- # -------------------------
287
- # 3) Train GPLM (CPU)
288
- # -------------------------
289
- self.dec = GPLM(R_ix, self.R_iX, **gplm_kwargs)
290
-
291
- # -------------------------
292
- # 4) Train DDPM on normalized latents (DDPM device)
293
- # -------------------------
294
- Z_ix_j = jax.device_put(jnp.asarray(Z_ix, dtype=jnp.float32), self.ddpm_device)
295
-
296
- # defaults (can be overridden by ddpm_kwargs)
297
- ddpm_init = dict(
298
- T=200,
299
- hidden_dim=128,
300
- t_embed_dim=64,
301
- learning_rate=3e-4,
302
- n_iter=200_000,
303
- ema_decay=0.999,
304
- beta_max=0.02,
305
- batch_size=256,
306
- key=self.rng,
307
- verbose_every=0,
308
- eps=1e-5,
309
- )
310
- ddpm_init.update(ddpm_kwargs)
311
-
312
- with jax.default_device(self.ddpm_device):
313
- self.dm = DDPM(Z_ix_j, **ddpm_init)
314
-
315
- self.training_time = time.time() - t0
316
-
317
- # -------------------------
318
- # Encode / Decode
319
- # -------------------------
320
- def encode(self, R_aX: Union[np.ndarray, jnp.ndarray], *, to_device: bool = True) -> jnp.ndarray:
321
- """
322
- ambient -> normalized latent (DDPM space)
323
- Returns jnp on ddpm_device by default.
324
- """
325
- X = np.asarray(R_aX)
326
- Z_raw = np.asarray(self.enc(X)) # CPU
327
- Z_norm = (Z_raw - self.lat_mean_np) / self.lat_std_np
328
- Zj = jnp.asarray(Z_norm, dtype=jnp.float32)
329
- return jax.device_put(Zj, self.ddpm_device) if to_device else Zj
330
-
331
- def decode(
332
- self,
333
- Z_ax: Union[np.ndarray, jnp.ndarray],
334
- *,
335
- refine: bool = True,
336
- t_start: int = 10,
337
- add_noise: bool = True,
338
- key: Optional[jax.Array] = None,
339
- batch_size: Optional[int] = None,
340
- ) -> np.ndarray:
341
- """
342
- normalized latent -> (optional DDPM refine) -> raw latent -> ambient
343
- Returns numpy array (CPU).
344
- """
345
- Z = jnp.asarray(Z_ax, dtype=jnp.float32)
346
- if Z.ndim == 1:
347
- Z = Z[None, :]
348
- Z = jax.device_put(Z, self.ddpm_device)
349
-
350
- if refine:
351
- Z = self.dm.refine_latents(Z, t_start=int(t_start), key=key, add_noise=bool(add_noise))
352
-
353
- # unnormalize to raw latent (still on device)
354
- R_ax = (Z * self.lat_std_j) + self.lat_mean_j
355
- R_ax_np = np.asarray(jax.device_get(R_ax)) # to CPU
356
-
357
- X_hat = self.dec(R_ax_np, batch_size=batch_size) # CPU decode
358
- return np.asarray(X_hat)
359
-
360
- def reconstruct(
361
- self,
362
- R_aX: Union[np.ndarray, jnp.ndarray],
363
- *,
364
- refine: bool = False,
365
- t_start: int = 10,
366
- add_noise: bool = True,
367
- key: Optional[jax.Array] = None,
368
- batch_size: Optional[int] = None,
369
- ) -> np.ndarray:
370
- """Convenience: decode(encode(X))."""
371
- Z = self.encode(R_aX, to_device=True)
372
- return self.decode(Z, refine=refine, t_start=t_start, add_noise=add_noise, key=key, batch_size=batch_size)
373
-
374
- def sample(self, n: int, *, decode: bool = True, batch_size: Optional[int] = None) -> Union[jnp.ndarray, np.ndarray]:
375
- """
376
- Unconditional samples from latent DDPM.
377
- If decode=True, returns ambient samples as np.ndarray.
378
- Otherwise returns normalized latents as jnp.ndarray.
379
- """
380
- with jax.default_device(self.ddpm_device):
381
- Z = self.dm.sample(int(n))
382
- if not decode:
383
- return Z
384
- return self.decode(Z, refine=False, batch_size=batch_size)
385
-
386
- def __call__(self, R_any: Union[np.ndarray, jnp.ndarray]) -> Union[jnp.ndarray, np.ndarray]:
387
- """
388
- Polymorphic call:
389
- - if trailing dim == D: ambient -> normalized latent
390
- - if trailing dim == d: normalized latent -> ambient (refine=True)
391
- """
392
- X = np.asarray(R_any)
393
- last = X.shape[-1]
394
- if last == self.D:
395
- return self.encode(X)
396
- if last == self.d:
397
- return self.decode(X, refine=True)
398
- raise ValueError(f"Trailing dim must be D={self.D} (ambient) or d={self.d} (latent), got {last}.")
399
-
400
- # -------------------------
401
- # Save / Load
402
- # -------------------------
403
- def _pack_encoder(self) -> Dict[str, Any]:
404
- # minimal state for Nyström OOS
405
- return dict(
406
- R_iX=np.asarray(self.enc.R_iX),
407
- qalpha_i=np.asarray(getattr(self.enc, "qalpha_i", getattr(self.enc, "qα_i"))),
408
- R_over_lam_ix=np.asarray(getattr(self.enc, "R_over_λ_ix", getattr(self.enc, "R_over_lam_ix"))),
409
- k=int(self.enc.k),
410
- beta=float(getattr(self.enc, "beta", getattr(self.enc, "β"))),
411
- alpha=float(getattr(self.enc, "alpha", getattr(self.enc, "α"))),
412
- eps=float(getattr(self.enc, "eps", getattr(self.enc, "ε"))),
413
- dtype=_np_dtype_str(getattr(self.enc, "dtype", np.float32)),
414
- )
415
-
416
- def _pack_decoder(self) -> Dict[str, Any]:
417
- return dict(
418
- Z_mx_w=np.asarray(getattr(self.dec, "Z_mx_w", getattr(self.dec, "Z_mx_w", None))),
419
- M_mX=np.asarray(self.dec.M_mX),
420
- mean_X=np.asarray(getattr(self.dec, "mean_X", np.zeros((self.D,), dtype=np.float64))),
421
- lat_mean_x=np.asarray(getattr(self.dec, "lat_mean_x", np.zeros((self.d,), dtype=np.float64))),
422
- lat_std_x=np.asarray(getattr(self.dec, "lat_std_x", np.ones((self.d,), dtype=np.float64))),
423
- beta=float(getattr(self.dec, "beta", getattr(self.dec, "β"))),
424
- eps=float(getattr(self.dec, "eps", getattr(self.dec, "ε"))),
425
- pred_k=getattr(self.dec, "pred_k", getattr(self.dec, "pred_κ", None)),
426
- dtype=_np_dtype_str(getattr(self.dec, "dtype", np.float32)),
427
- )
428
-
429
- def state_dict(self) -> Dict[str, Any]:
430
- """
431
- Serializable state (msgpack via flax.serialization).
432
- """
433
- dd = dict(
434
- T=int(self.dm.T),
435
- D=int(self.dm.D),
436
- hidden_dim=int(getattr(self.dm.model, "hidden", 128)),
437
- t_embed_dim=int(getattr(self.dm.model, "t_dim", 64)),
438
- ema_decay=float(getattr(self.dm, "ema_decay", 0.999)),
439
- beta_max=float(getattr(self.dm, "beta_max", 0.02)),
440
- eps=float(getattr(self.dm, "eps", 1e-5)),
441
- params=self.dm.state.params,
442
- ema_params=self.dm.state.ema_params,
443
- )
444
-
445
- state = dict(
446
- meta=dict(
447
- N=int(self.N),
448
- D=int(self.D),
449
- d=int(self.d),
450
- training_time=float(self.training_time),
451
- ),
452
- config=asdict(self.config),
453
- latent_norm=dict(
454
- mean=np.asarray(self.lat_mean_np, dtype=np.float64),
455
- std=np.asarray(self.lat_std_np, dtype=np.float64),
456
- ),
457
- encoder=self._pack_encoder(),
458
- decoder=self._pack_decoder(),
459
- ddpm=dd,
460
- )
461
- return state
462
-
463
- def save_local(self, weights_file: str = "dima.msgpack", config_file: str = "config.json") -> None:
464
- state = self.state_dict()
465
- blob = flax_ser.msgpack_serialize(state)
466
- with open(weights_file, "wb") as f:
467
- f.write(blob)
468
- with open(config_file, "w") as f:
469
- json.dump(state["config"], f, indent=2)
470
- return None
471
-
472
- @classmethod
473
- def load_local(
474
- cls,
475
- weights_file: str = "dima.msgpack",
476
- *,
477
- ddpm_device: str = "auto",
478
- ann_backend: ANNBackend = "auto",
479
- ann_params: Optional[Dict[str, Any]] = None,
480
- n_jobs: int = -1,
481
- key: Optional[jax.Array] = None,
482
- ) -> "DIMA":
483
- with open(weights_file, "rb") as f:
484
- state = flax_ser.msgpack_restore(f.read())
485
-
486
- obj = cls.__new__(cls) # bypass __init__
487
-
488
- obj.config = DIMAConfig(**state["config"])
489
- obj.ddpm_device = _select_device(ddpm_device)
490
- obj.cpu_device = _select_device("cpu")
491
- obj.training_time = float(state["meta"].get("training_time", 0.0))
492
-
493
- obj.N = int(state["meta"]["N"])
494
- obj.D = int(state["meta"]["D"])
495
- obj.d = int(state["meta"]["d"])
496
-
497
- # RNG
498
- if key is None:
499
- key = random.PRNGKey(0)
500
- obj.rng = key
501
-
502
- # latent norm
503
- obj.lat_mean_np = np.asarray(state["latent_norm"]["mean"], dtype=np.float64)
504
- obj.lat_std_np = np.asarray(state["latent_norm"]["std"], dtype=np.float64)
505
-
506
- obj.lat_mean_j = jax.device_put(jnp.asarray(obj.lat_mean_np, dtype=jnp.float32), obj.ddpm_device)
507
- obj.lat_std_j = jax.device_put(jnp.asarray(obj.lat_std_np, dtype=jnp.float32), obj.ddpm_device)
508
-
509
- # encoder/decoder frozen
510
- obj.enc = FrozenDMAP(state["encoder"], ann_backend=ann_backend, ann_params=ann_params, n_jobs=n_jobs)
511
- obj.dec = FrozenGPLM(state["decoder"], ann_backend=ann_backend, ann_params=ann_params, n_jobs=n_jobs)
512
-
513
- # rebuild DDPM skeleton with dummy data, then load params
514
- dd = state["ddpm"]
515
- T = int(dd["T"])
516
- D = int(dd["D"])
517
- hidden_dim = int(dd["hidden_dim"])
518
- t_embed_dim = int(dd["t_embed_dim"])
519
- ema_decay = float(dd.get("ema_decay", 0.999))
520
- beta_max = float(dd.get("beta_max", 0.02))
521
- eps = float(dd.get("eps", 1e-5))
522
-
523
- dummy = jnp.zeros((1, D), dtype=jnp.float32)
524
- with jax.default_device(obj.ddpm_device):
525
- obj.dm = DDPM(
526
- dummy,
527
- T=T,
528
- hidden_dim=hidden_dim,
529
- t_embed_dim=t_embed_dim,
530
- learning_rate=1e-3,
531
- n_iter=0, # skip training on load
532
- ema_decay=ema_decay,
533
- beta_max=beta_max,
534
- batch_size=1,
535
- key=obj.rng,
536
- verbose_every=0,
537
- eps=eps,
538
- )
539
- obj.dm.state = obj.dm.state.replace(params=dd["params"], ema_params=dd["ema_params"])
540
-
541
- obj.R_iX = None # training data not stored by default
542
- return obj
543
-
544
- # -------------------------
545
- # Hugging Face helpers
546
- # -------------------------
547
- def upload_to_huggingface(
548
- self,
549
- repo_id: str,
550
- *,
551
- hf_token: Optional[str] = None,
552
- weights_file: str = "dima.msgpack",
553
- config_file: str = "config.json",
554
- ) -> None:
555
- if not _HAS_HF:
556
- raise ImportError("huggingface_hub not installed. pip install dima[hf] (or huggingface_hub).")
557
- if hf_token is not None:
558
- HfFolder.save_token(hf_token)
559
- api = HfApi()
560
- _ = api.whoami(token=hf_token) # sanity check token (no-op if public)
561
-
562
- self.save_local(weights_file=weights_file, config_file=config_file)
563
- for fname in (weights_file, config_file):
564
- upload_file(path_or_fileobj=fname, path_in_repo=fname, repo_id=repo_id, token=hf_token)
565
- return None
566
-
567
- @classmethod
568
- def load_from_huggingface(
569
- cls,
570
- repo_id: str,
571
- *,
572
- hf_token: Optional[str] = None,
573
- weights_file: str = "dima.msgpack",
574
- config_file: str = "config.json",
575
- ddpm_device: str = "auto",
576
- ann_backend: ANNBackend = "auto",
577
- ann_params: Optional[Dict[str, Any]] = None,
578
- n_jobs: int = -1,
579
- key: Optional[jax.Array] = None,
580
- ) -> "DIMA":
581
- if not _HAS_HF:
582
- raise ImportError("huggingface_hub not installed. pip install dima[hf] (or huggingface_hub).")
583
-
584
- wpath = hf_hub_download(repo_id, weights_file, token=hf_token)
585
- _ = hf_hub_download(repo_id, config_file, token=hf_token) # for humans / future-proofing
586
-
587
- return cls.load_local(
588
- wpath,
589
- ddpm_device=ddpm_device,
590
- ann_backend=ann_backend,
591
- ann_params=ann_params,
592
- n_jobs=n_jobs,
593
- key=key,
594
- )
595
-
596
-
597
- __all__ = ["DIMA", "DIMAConfig"]