runyard 0.3.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.
@@ -0,0 +1,52 @@
1
+ """Lightning: Trainer(logger=RunyardLogger(project="mnist")). Works with lightning.pytorch and pytorch_lightning."""
2
+ from argparse import Namespace
3
+
4
+ try:
5
+ from lightning.pytorch.loggers.logger import Logger, rank_zero_experiment
6
+ from lightning.pytorch.utilities import rank_zero_only
7
+ except ImportError:
8
+ from pytorch_lightning.loggers.logger import Logger, rank_zero_experiment
9
+ from pytorch_lightning.utilities import rank_zero_only
10
+
11
+ from .. import init
12
+
13
+
14
+ class RunyardLogger(Logger):
15
+ """One run per Trainer call (fit, then test, are two runs), on the global-zero process only. Config is the
16
+ hyperparameters (save_hyperparameters); metrics are keyed by global step. Other keyword arguments go to runyard.init.
17
+ """
18
+
19
+ def __init__(self, project=None, name=None, **init_kwargs):
20
+ super().__init__()
21
+ self.project, self._name, self.init_kwargs = project, name, init_kwargs
22
+ self.run = None
23
+
24
+ @property
25
+ def name(self):
26
+ return self._name
27
+
28
+ @property
29
+ def version(self):
30
+ return self.run.id if self.run else None
31
+
32
+ @property
33
+ @rank_zero_experiment
34
+ def experiment(self):
35
+ """The runyard Run: trainer.logger.experiment.image("samples", grid)."""
36
+ if self.run is None:
37
+ self.run = init(self.project, self._name, **self.init_kwargs)
38
+ return self.run
39
+
40
+ @rank_zero_only
41
+ def log_hyperparams(self, params, *args, **kwargs):
42
+ self.experiment.config.update(vars(params) if isinstance(params, Namespace) else params)
43
+
44
+ @rank_zero_only
45
+ def log_metrics(self, metrics, step=None):
46
+ self.experiment.log(metrics, step=step)
47
+
48
+ @rank_zero_only
49
+ def finalize(self, status):
50
+ if self.run is not None:
51
+ self.run.finish("failed" if status == "failed" else "finished") # Lightning says success, failed or finished
52
+ self.run = None # a later trainer.test() on this logger starts its own run instead of writing to a closed one
@@ -0,0 +1,61 @@
1
+ """scikit-learn: runyard.integrations.sklearn.autolog(project="tabular"), then every fit() is a run."""
2
+ import functools
3
+ import time
4
+
5
+ from sklearn.utils import all_estimators
6
+
7
+ from .. import init, warn
8
+
9
+ _patched = {} # class -> its own fit, put back by autolog(disable=True)
10
+ _depth = 0 # fits in progress, so a meta-estimator's inner fits stay inside its run
11
+
12
+
13
+ def autolog(project=None, disable=False, **init_kwargs):
14
+ """Each outer fit() of a scikit-learn estimator becomes a run named after its class: config is
15
+ get_params(deep=False), metrics are fit_seconds and, when y is given, train/score. A fit that raises marks its
16
+ run failed. autolog(disable=True) puts the original fit methods back. Other keyword arguments go to runyard.init.
17
+ """
18
+ for cls, fit in _patched.items():
19
+ cls.fit = fit
20
+ _patched.clear()
21
+ if disable:
22
+ return
23
+ # shortcut: only fit() is wrapped, so a fit_transform that skips fit() (PCA, TruncatedSVD) is not a run, and fits
24
+ # running at once in several threads make one run; wrap those too if anyone uses them that way.
25
+ for _, cls in all_estimators():
26
+ # the class that defines fit: GridSearchCV and the forests inherit it from abstract bases all_estimators skips
27
+ owner = next((c for c in cls.__mro__ if "fit" in vars(c)), None)
28
+ if owner is not None and owner not in _patched:
29
+ _patched[owner] = vars(owner)["fit"]
30
+ owner.fit = _wrap(owner.fit, project, init_kwargs)
31
+
32
+
33
+ def _wrap(fit, project, init_kwargs):
34
+ @functools.wraps(fit)
35
+ def wrapped(self, *args, **kwargs):
36
+ global _depth
37
+ if _depth:
38
+ return fit(self, *args, **kwargs)
39
+ run = init(project, type(self).__name__, self.get_params(deep=False), **init_kwargs)
40
+ _depth += 1
41
+ try:
42
+ t = time.time()
43
+ out = fit(self, *args, **kwargs)
44
+ values = {"fit_seconds": time.time() - t}
45
+ X = args[0] if args else kwargs.get("X")
46
+ y = args[1] if len(args) > 1 else kwargs.get("y")
47
+ if y is not None and hasattr(self, "score"):
48
+ try:
49
+ values["train/score"] = self.score(X, y)
50
+ except Exception as e: # scoring is ours: it must not break the user's fit
51
+ warn(f"{type(self).__name__}.score failed: {e!r}")
52
+ run.log(values, step=0)
53
+ run.finish()
54
+ return out
55
+ except BaseException:
56
+ run.finish("failed")
57
+ raise
58
+ finally:
59
+ _depth -= 1
60
+
61
+ return wrapped
@@ -0,0 +1,33 @@
1
+ """torchtune: in the recipe config,
2
+ metric_logger: {_component_: runyard.integrations.torchtune.RunyardLogger, project: sft}."""
3
+ from omegaconf import OmegaConf
4
+ from torchtune.training import get_world_size_and_rank
5
+ from torchtune.training.metric_logging import MetricLoggerInterface
6
+
7
+ from .. import init
8
+
9
+
10
+ class RunyardLogger(MetricLoggerInterface):
11
+ """One run per recipe, on rank 0 only. Config is the resolved recipe config; metrics are keyed by the recipe's step.
12
+ Other keyword arguments go to runyard.init.
13
+ """
14
+
15
+ # log_dir: torchtune's configs give every metric logger one; runyard writes nothing there
16
+ def __init__(self, project=None, name=None, log_dir=None, **init_kwargs):
17
+ self.run = init(project, name, **init_kwargs) if get_world_size_and_rank()[1] == 0 else None
18
+
19
+ def log(self, name, data, step):
20
+ if self.run:
21
+ self.run.log({name: data}, step=step)
22
+
23
+ def log_dict(self, payload, step):
24
+ if self.run:
25
+ self.run.log(payload, step=step)
26
+
27
+ def log_config(self, config):
28
+ if self.run:
29
+ self.run.config.update(OmegaConf.to_container(config, resolve=True))
30
+
31
+ def close(self):
32
+ if self.run:
33
+ self.run.finish()
@@ -0,0 +1,45 @@
1
+ """XGBoost: xgb.train(params, dtrain, evals=[(dval, "val")], callbacks=[RunyardCallback(project="tabular")])."""
2
+ import json
3
+
4
+ from xgboost.callback import TrainingCallback
5
+
6
+ from .. import init
7
+
8
+
9
+ def _train_params(c):
10
+ """The "*_train_param" sections of the booster's JSON config, merged: objective, eta, max_depth and the rest."""
11
+ out = {}
12
+ for k, v in c.items():
13
+ if isinstance(v, dict):
14
+ out.update(v if k.endswith("_train_param") else _train_params(v))
15
+ return out
16
+
17
+
18
+ class RunyardCallback(TrainingCallback):
19
+ """One run per training (also XGBClassifier(callbacks=[...])). Config is the booster's training parameters;
20
+ every eval metric is logged as data/metric at step=iteration. Other keyword arguments go to runyard.init.
21
+ """
22
+
23
+ def __init__(self, project=None, name=None, **init_kwargs):
24
+ super().__init__()
25
+ self.project, self.name, self.init_kwargs = project, name, init_kwargs
26
+ self.run = None
27
+
28
+ def before_training(self, model):
29
+ self.run = init(self.project, self.name, **self.init_kwargs)
30
+ if hasattr(model, "save_config"): # xgb.cv passes a bundle of fold boosters instead
31
+ self.run.config.update(_train_params(json.loads(model.save_config())))
32
+ return model
33
+
34
+ def after_iteration(self, model, epoch, evals_log):
35
+ values = {}
36
+ for data, metrics in evals_log.items():
37
+ for metric, history in metrics.items():
38
+ v = history[-1]
39
+ values[f"{data}/{metric}"] = v[0] if isinstance(v, tuple) else v # xgb.cv logs (mean, std)
40
+ self.run.log(values, step=epoch)
41
+ return False
42
+
43
+ def after_training(self, model):
44
+ self.run.finish()
45
+ return model
runyard/media.py ADDED
@@ -0,0 +1,95 @@
1
+ """Turns images and videos into files. numpy, Pillow and imageio are optional: without Pillow, PNGs are written by hand."""
2
+ import os
3
+ import shutil
4
+ import struct
5
+ import zlib
6
+ from pathlib import Path
7
+
8
+
9
+ def _copy(src, path):
10
+ src = Path(src)
11
+ dst = path.with_suffix(src.suffix.lower() or path.suffix)
12
+ shutil.copyfile(src, dst)
13
+ return dst
14
+
15
+
16
+ def to_array(obj):
17
+ """HxW or HxWxC uint8 array from a numpy array or torch tensor; CHW, [0, 1], [-1, 1] and [0, 255] floats are handled."""
18
+ import numpy as np
19
+
20
+ if hasattr(obj, "detach"): # torch
21
+ obj = obj.detach().float().cpu().numpy()
22
+ a = np.asarray(obj)
23
+ if a.ndim == 3 and a.shape[0] in (1, 3, 4) and a.shape[-1] not in (1, 3, 4):
24
+ a = a.transpose(1, 2, 0)
25
+ if a.ndim == 3 and a.shape[-1] == 1:
26
+ a = a[..., 0]
27
+ if a.dtype != np.uint8:
28
+ a = a.astype(np.float32)
29
+ if a.size and a.max() > 1.0 + 1e-6:
30
+ a = a / 255.0
31
+ elif a.size and a.min() < 0:
32
+ a = (a + 1) / 2
33
+ a = (np.clip(a, 0, 1) * 255).round().astype(np.uint8)
34
+ return a
35
+
36
+
37
+ def write_png(a, path):
38
+ """Encodes an HxW, HxWx3 or HxWx4 uint8 array as PNG with zlib alone."""
39
+ h, w = a.shape[:2]
40
+ channels = 1 if a.ndim == 2 else a.shape[-1]
41
+ ctype = {1: 0, 3: 2, 4: 6}.get(channels) if a.ndim in (2, 3) else None # gray, RGB, RGBA
42
+ if ctype is None:
43
+ raise ValueError(f"cannot make an image of shape {a.shape}")
44
+ raw = b"".join(b"\x00" + a[y].tobytes() for y in range(h))
45
+
46
+ def chunk(t, d):
47
+ return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d))
48
+
49
+ Path(path).write_bytes(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, ctype, 0, 0, 0))
50
+ + chunk(b"IDAT", zlib.compress(raw, 6)) + chunk(b"IEND", b""))
51
+
52
+
53
+ def save_image(obj, path):
54
+ """Writes a PIL image, array, tensor or image file to path (a .png); returns the path actually written."""
55
+ path.parent.mkdir(parents=True, exist_ok=True)
56
+ if isinstance(obj, (str, os.PathLike)):
57
+ return _copy(obj, path)
58
+ if hasattr(obj, "save") and hasattr(obj, "mode"): # PIL
59
+ obj.save(path, format="PNG")
60
+ return path
61
+ a = to_array(obj)
62
+ try:
63
+ from PIL import Image
64
+ except ImportError:
65
+ write_png(a, path)
66
+ else:
67
+ Image.fromarray(a).save(path)
68
+ return path
69
+
70
+
71
+ def save_video(obj, path, fps):
72
+ """A video file is copied; frames (arrays, tensors or PIL images) become an H.264 mp4 with imageio and imageio-ffmpeg,
73
+ else an animated GIF, which needs Pillow."""
74
+ path.parent.mkdir(parents=True, exist_ok=True)
75
+ if isinstance(obj, (str, os.PathLike)):
76
+ return _copy(obj, path)
77
+ try:
78
+ import imageio.v2 as imageio
79
+ import imageio_ffmpeg # noqa: F401 the backend imageio writes mp4 with
80
+ except ImportError:
81
+ from PIL import Image
82
+
83
+ frames = [f if hasattr(f, "mode") else Image.fromarray(to_array(f)) for f in obj]
84
+ path = path.with_suffix(".gif")
85
+ frames[0].save(path, save_all=True, append_images=frames[1:], duration=max(1, round(1000 / fps)), loop=0)
86
+ return path
87
+ import numpy as np
88
+
89
+ path = path.with_suffix(".mp4")
90
+ with imageio.get_writer(path, fps=fps, codec="libx264", pixelformat="yuv420p", macro_block_size=1) as w:
91
+ for f in obj:
92
+ a = np.asarray(f.convert("RGB")) if hasattr(f, "mode") else to_array(f)
93
+ # yuv420p needs even sides (Safari and iOS play nothing else): repeat the last row or column
94
+ w.append_data(np.pad(a, ((0, a.shape[0] % 2), (0, a.shape[1] % 2)) + ((0, 0),) * (a.ndim - 2), mode="edge"))
95
+ return path
runyard/wandb_hook.py ADDED
@@ -0,0 +1,55 @@
1
+ """Makes `import wandb` load runyard.wandb_shim. The runyard_wandb.pth file installed with the package calls install()
2
+ at interpreter start when RUNYARD_WANDB=1, so tools that only support W&B write to runyard without code changes."""
3
+ import importlib.abc
4
+ import importlib.machinery
5
+ import importlib.metadata
6
+ import sys
7
+ import types
8
+
9
+ VERSION = "0.21.0"
10
+
11
+
12
+ class _Dist(importlib.metadata.Distribution):
13
+ """Package metadata for `wandb`: accelerate and diffusers check it before they use W&B."""
14
+
15
+ def read_text(self, filename):
16
+ if filename == "METADATA":
17
+ return f"Metadata-Version: 2.1\nName: wandb\nVersion: {VERSION}\n"
18
+ return None
19
+
20
+ def locate_file(self, path):
21
+ return path
22
+
23
+
24
+ class _Finder(importlib.abc.MetaPathFinder, importlib.abc.Loader):
25
+ def find_spec(self, name, path=None, target=None):
26
+ if name == "wandb" or name.startswith("wandb."):
27
+ return importlib.machinery.ModuleSpec(name, self, is_package=True)
28
+ return None
29
+
30
+ def create_module(self, spec):
31
+ if spec.name == "wandb":
32
+ from . import wandb_shim
33
+
34
+ return wandb_shim
35
+ from .wandb_shim import stub_attr
36
+
37
+ m = types.ModuleType(spec.name)
38
+ m.__path__ = []
39
+ m.__getattr__ = lambda attr: stub_attr(spec.name, attr)
40
+ return m
41
+
42
+ def exec_module(self, module):
43
+ pass
44
+
45
+ def find_distributions(self, context=importlib.metadata.DistributionFinder.Context()):
46
+ if context.name and context.name.lower().replace("_", "-") == "wandb":
47
+ return [_Dist()]
48
+ return []
49
+
50
+
51
+ def install():
52
+ if not any(isinstance(f, _Finder) for f in sys.meta_path):
53
+ sys.meta_path.insert(0, _Finder())
54
+ for name in [m for m in sys.modules if m == "wandb" or m.startswith("wandb.")]:
55
+ del sys.modules[name] # a real wandb imported before us would win otherwise
runyard/wandb_shim.py ADDED
@@ -0,0 +1,256 @@
1
+ """Enough of the wandb API for tools that only speak W&B (ai-toolkit, diffusers examples, HF report_to="wandb")
2
+ to write to runyard instead, without changing their code. Turned on by RUNYARD_WANDB=1; see wandb_hook.
3
+
4
+ Supported: init, log (step/commit), finish, config(.update), summary, run, define_metric (its step_metric is honoured),
5
+ Image, Video, Table, alert. Anything else warns once and does nothing.
6
+ """
7
+ import os
8
+ import sys
9
+
10
+ from . import core
11
+
12
+ __version__ = "0.21.0" # what version checks in callers see
13
+ __path__ = [] # a package, so `import wandb.sdk...` reaches the stub finder in wandb_hook
14
+
15
+ run = None
16
+ config = None
17
+ summary = None
18
+
19
+
20
+ class Config(dict):
21
+ """wandb.config: a dict with attribute access whose changes go to the runyard run."""
22
+
23
+ def __init__(self, ry_run, data=None):
24
+ super().__init__(data or {})
25
+ object.__setattr__(self, "_ry", ry_run)
26
+
27
+ def update(self, d=None, allow_val_change=None, **kw):
28
+ d = dict(vars(d) if hasattr(d, "__dict__") and not isinstance(d, dict) else (d or {}), **kw)
29
+ super().update(d)
30
+ self._ry.config.update(d)
31
+
32
+ def setdefaults(self, d):
33
+ self.update({k: v for k, v in dict(d).items() if k not in self})
34
+
35
+ def __setitem__(self, k, v):
36
+ self.update({k: v})
37
+
38
+ def __getattr__(self, k):
39
+ try:
40
+ return self[k]
41
+ except KeyError:
42
+ raise AttributeError(k) from None
43
+
44
+ def __setattr__(self, k, v):
45
+ self.update({k: v})
46
+
47
+ def as_dict(self):
48
+ return dict(self)
49
+
50
+
51
+ class Image:
52
+ def __init__(self, data_or_path, mode=None, caption=None, **kwargs):
53
+ self.data, self.caption = data_or_path, caption
54
+
55
+
56
+ class Video:
57
+ def __init__(self, data_or_path, caption=None, fps=4, format=None, **kwargs):
58
+ self.data, self.caption, self.fps = data_or_path, caption, fps
59
+
60
+
61
+ class Table:
62
+ def __init__(self, columns=None, data=None, rows=None, dataframe=None, **kwargs):
63
+ if dataframe is not None:
64
+ columns, data = list(dataframe.columns), dataframe.values.tolist()
65
+ self.columns = list(columns or [])
66
+ self.data = [list(r) for r in (data or rows or [])]
67
+
68
+ def add_data(self, *row):
69
+ self.data.append(list(row))
70
+
71
+ def rows(self):
72
+ cols = self.columns or [f"c{i}" for i in range(len(self.data[0]) if self.data else 0)]
73
+ return [dict(zip(cols, r)) for r in self.data]
74
+
75
+
76
+ class AlertLevel:
77
+ INFO, WARN, ERROR = "INFO", "WARN", "ERROR"
78
+
79
+
80
+ class Run:
81
+ def __init__(self, ry_run):
82
+ self._ry = ry_run
83
+ self.id, self.name, self.project, self.url = ry_run.id, ry_run.name, ry_run.project, ry_run.url
84
+ self.config = Config(ry_run, ry_run.config)
85
+ self.summary = ry_run.summary
86
+ self.dir = os.getcwd()
87
+ self._pending = {}
88
+ self._pstep = None # step the pending values were logged with, if any
89
+ self._auto = 0 # wandb's own step counter
90
+ self._step_metric = None
91
+
92
+ @property
93
+ def step(self):
94
+ return self._auto
95
+
96
+ def log(self, data, step=None, commit=None, sync=None):
97
+ if step is not None and self._pending and self._pstep is not None and step != self._pstep:
98
+ self._flush() # an earlier step logged with commit=False and never committed
99
+ self._pending.update(data or {})
100
+ if step is not None:
101
+ self._pstep = int(step)
102
+ # wandb would hold uncommitted values until the step moves on; sending now is equivalent
103
+ # because the server merges everything logged for the same step.
104
+ if commit is not False:
105
+ self._flush()
106
+
107
+ def _flush(self):
108
+ data, self._pending = self._pending, {}
109
+ step, self._pstep = self._pstep, None
110
+ if not data:
111
+ return
112
+ if step is None and self._step_metric in data:
113
+ step = int(data[self._step_metric])
114
+ if step is None:
115
+ step = self._auto
116
+ self._auto = max(self._auto, step + 1)
117
+ numbers = {}
118
+ for k, v in data.items():
119
+ items = v if isinstance(v, (list, tuple)) and v and isinstance(v[0], (Image, Video)) else None
120
+ if isinstance(v, Image):
121
+ self._ry.image(k, v.data, step=step, caption=v.caption)
122
+ elif items and isinstance(items[0], Image):
123
+ self._ry.image(k, [i.data for i in items], step=step, caption=[i.caption for i in items])
124
+ elif isinstance(v, Video) or items:
125
+ for vid in items or [v]:
126
+ self._ry.video(k, vid.data, fps=vid.fps, step=step, caption=vid.caption)
127
+ elif isinstance(v, Table):
128
+ self._ry.text(k, v.rows(), step=step)
129
+ elif isinstance(v, dict):
130
+ numbers.update({f"{k}/{kk}": vv for kk, vv in v.items()})
131
+ else:
132
+ numbers[k] = v
133
+ if numbers:
134
+ self._ry.log(numbers, step=step)
135
+
136
+ def define_metric(self, name, step_metric=None, **kwargs):
137
+ if step_metric:
138
+ self._step_metric = step_metric
139
+
140
+ def alert(self, title, text="", level=None, wait_duration=None):
141
+ self._ry.alert(title, text, level={"WARN": "warn", "ERROR": "error"}.get(str(level).split(".")[-1], "info"))
142
+
143
+ def finish(self, exit_code=None, quiet=None):
144
+ global run
145
+ self._flush()
146
+ self._ry.finish("finished" if exit_code in (None, 0) else "failed")
147
+ if run is self:
148
+ run = None
149
+ _set_globals()
150
+
151
+ def __getattr__(self, name):
152
+ if name.startswith("__"):
153
+ raise AttributeError(name)
154
+ if name.startswith("_"): # private wandb calls such as run._label(): silently nothing
155
+ return _Stub(name, quiet=True)
156
+ return _Stub("run." + name)
157
+
158
+
159
+ def init(project=None, name=None, config=None, tags=None, notes=None, id=None, reinit=None, **kwargs):
160
+ global run
161
+ if run is not None and not reinit:
162
+ return run
163
+ if run is not None:
164
+ run.finish()
165
+ if config is not None and not isinstance(config, dict):
166
+ config = vars(config) # argparse.Namespace and the like
167
+ project = project or os.environ.get("WANDB_PROJECT")
168
+ name = name or os.environ.get("WANDB_NAME") or os.environ.get("WANDB_RUN_NAME")
169
+ tags = list(tags or []) + ([kwargs["group"]] if kwargs.get("group") else [])
170
+ ry_run = core.Run(project, name, dict(config or {}), tags=tags, notes=notes or "")
171
+ run = Run(ry_run)
172
+ _set_globals()
173
+ return run
174
+
175
+
176
+ def _set_globals():
177
+ global config, summary
178
+ config = run.config if run else None
179
+ summary = run.summary if run else None
180
+
181
+
182
+ def log(data, step=None, commit=None, sync=None):
183
+ if run is None:
184
+ core.warn("wandb.log() before wandb.init(), ignored", once=True)
185
+ return
186
+ run.log(data, step=step, commit=commit)
187
+
188
+
189
+ def define_metric(name, step_metric=None, **kwargs):
190
+ if run:
191
+ run.define_metric(name, step_metric=step_metric)
192
+
193
+
194
+ def finish(exit_code=None, quiet=None):
195
+ if run:
196
+ run.finish(exit_code)
197
+
198
+
199
+ def alert(title, text="", level=None, wait_duration=None):
200
+ if run:
201
+ run.alert(title, text, level)
202
+
203
+
204
+ def login(*args, **kwargs):
205
+ return True
206
+
207
+
208
+ def watch(*args, **kwargs):
209
+ pass
210
+
211
+
212
+ def termlog(msg="", *a, **kw):
213
+ print(f"wandb: {msg}", file=sys.stderr)
214
+
215
+
216
+ termwarn = termerror = termlog
217
+
218
+
219
+ class _Stub:
220
+ """Stands in for an unsupported part of wandb: warns once when used, then behaves as a no-op."""
221
+
222
+ def __init__(self, name, quiet=False):
223
+ object.__setattr__(self, "_name", name)
224
+ if not quiet:
225
+ core.warn(f"wandb.{name} is not supported by the runyard bridge; ignored", once=True)
226
+
227
+ def __call__(self, *args, **kwargs):
228
+ return self
229
+
230
+ def __getattr__(self, name):
231
+ return self
232
+
233
+ def __setattr__(self, name, value):
234
+ pass
235
+
236
+ def __iter__(self):
237
+ return iter(())
238
+
239
+ def __bool__(self):
240
+ return False
241
+
242
+
243
+ def stub_attr(module_name, name):
244
+ """Module __getattr__ for the shim and its stub submodules: exception classes stay usable in except clauses."""
245
+ if name.startswith("__"):
246
+ raise AttributeError(name)
247
+ full = f"{module_name}.{name}".removeprefix("wandb.")
248
+ if name[:1].isupper() and name.endswith(("Error", "Exception")):
249
+ return type(name, (Exception,), {})
250
+ if name[:1].isupper(): # a class, so isinstance() checks and subclassing work
251
+ return type(name, (_Stub,), {"__init__": lambda self, *a, **kw: _Stub.__init__(self, full)})
252
+ return _Stub(full)
253
+
254
+
255
+ def __getattr__(name):
256
+ return stub_attr("wandb", name)