dumpsterpanda 0.2.0__tar.gz → 0.2.2__tar.gz

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.
Files changed (46) hide show
  1. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/PKG-INFO +18 -1
  2. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/README.md +17 -0
  3. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/pyproject.toml +1 -1
  4. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/__init__.py +1 -1
  5. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/cli/app.py +5 -1
  6. dumpsterpanda-0.2.2/src/trash_panda/cli/config.py +68 -0
  7. dumpsterpanda-0.2.2/src/trash_panda/cli/trace.py +117 -0
  8. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/checkpoint.py +5 -0
  9. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/config.py +1 -0
  10. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/coordinator.py +6 -0
  11. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/hooks.py +110 -21
  12. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/loader.py +27 -4
  13. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/stage.py +40 -11
  14. dumpsterpanda-0.2.2/src/trash_panda/offload/trace.py +459 -0
  15. dumpsterpanda-0.2.2/src/trash_panda/paths.py +86 -0
  16. dumpsterpanda-0.2.2/src/trash_panda/ui/__init__.py +1 -0
  17. dumpsterpanda-0.2.2/src/trash_panda/ui/load.py +253 -0
  18. dumpsterpanda-0.2.2/src/trash_panda/ui/server.py +99 -0
  19. dumpsterpanda-0.2.2/src/trash_panda/ui/static/app.js +169 -0
  20. dumpsterpanda-0.2.2/src/trash_panda/ui/static/bars.js +66 -0
  21. dumpsterpanda-0.2.2/src/trash_panda/ui/static/data.js +109 -0
  22. dumpsterpanda-0.2.2/src/trash_panda/ui/static/files.js +84 -0
  23. dumpsterpanda-0.2.2/src/trash_panda/ui/static/index.html +100 -0
  24. dumpsterpanda-0.2.2/src/trash_panda/ui/static/memory.js +333 -0
  25. dumpsterpanda-0.2.2/src/trash_panda/ui/static/memorychart.js +96 -0
  26. dumpsterpanda-0.2.2/src/trash_panda/ui/static/package.json +4 -0
  27. dumpsterpanda-0.2.2/src/trash_panda/ui/static/panels.js +324 -0
  28. dumpsterpanda-0.2.2/src/trash_panda/ui/static/style.css +209 -0
  29. dumpsterpanda-0.2.2/src/trash_panda/ui/static/svg.js +25 -0
  30. dumpsterpanda-0.2.2/src/trash_panda/ui/static/timeline.js +235 -0
  31. dumpsterpanda-0.2.2/src/trash_panda/ui/static/usage.js +131 -0
  32. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/LICENSE +0 -0
  33. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/cli/__init__.py +0 -0
  34. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/cli/info.py +0 -0
  35. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/cli/names.py +0 -0
  36. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/cli/plan.py +0 -0
  37. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/__init__.py +0 -0
  38. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/blocks.py +0 -0
  39. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/depth.py +0 -0
  40. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/early.py +0 -0
  41. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/formats.py +0 -0
  42. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/hardware.py +0 -0
  43. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/measure.py +0 -0
  44. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/order_file.py +0 -0
  45. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/planner.py +0 -0
  46. {dumpsterpanda-0.2.0 → dumpsterpanda-0.2.2}/src/trash_panda/offload/report.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dumpsterpanda
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Run models bigger than your GPU: layer-by-layer weight streaming across GPU, RAM and disk.
5
5
  License: AGPL-3.0-only
6
6
  License-File: LICENSE
@@ -177,6 +177,23 @@ GPU budget: 400.0 MB allowed by --max-vram, less 50.0 MB kept for activations
177
177
  RAM budget: 150.0 MB allowed by --max-ram, less 67.1 MB kept for the system and copy buffers
178
178
  ```
179
179
 
180
+ ### Trace viewer
181
+
182
+ To see what a run really did, switch recording on and open the recording afterwards. With `TRASH_PANDA_TRACE=1` set, a
183
+ trace file is written when the `stage()` or `Coordinator` block ends, into `TRASH_PANDA_TRACE_DIR` (by default
184
+ `~/.cache/trash_panda/traces`):
185
+
186
+ ```
187
+ trash-panda trace # open the newest recording in the browser
188
+ trash-panda trace --list # list the recordings
189
+ ```
190
+
191
+ <p align="center">
192
+ <img src="https://raw.githubusercontent.com/yla382/trashpanda/main/assets/trace-viewer-demo.gif" width="800" alt="The trace viewer replaying a pipeline of three models: blocks moving between disk, RAM and the GPU, and a timeline">
193
+ </p>
194
+
195
+ The recording above is an example trace (made-up data, not a measurement).
196
+
180
197
  ## Documentation
181
198
 
182
199
  * [Quick start](https://yla382.github.io/trashpanda/quickstart/): from install to a first run
@@ -149,6 +149,23 @@ GPU budget: 400.0 MB allowed by --max-vram, less 50.0 MB kept for activations
149
149
  RAM budget: 150.0 MB allowed by --max-ram, less 67.1 MB kept for the system and copy buffers
150
150
  ```
151
151
 
152
+ ### Trace viewer
153
+
154
+ To see what a run really did, switch recording on and open the recording afterwards. With `TRASH_PANDA_TRACE=1` set, a
155
+ trace file is written when the `stage()` or `Coordinator` block ends, into `TRASH_PANDA_TRACE_DIR` (by default
156
+ `~/.cache/trash_panda/traces`):
157
+
158
+ ```
159
+ trash-panda trace # open the newest recording in the browser
160
+ trash-panda trace --list # list the recordings
161
+ ```
162
+
163
+ <p align="center">
164
+ <img src="https://raw.githubusercontent.com/yla382/trashpanda/main/assets/trace-viewer-demo.gif" width="800" alt="The trace viewer replaying a pipeline of three models: blocks moving between disk, RAM and the GPU, and a timeline">
165
+ </p>
166
+
167
+ The recording above is an example trace (made-up data, not a measurement).
168
+
152
169
  ## Documentation
153
170
 
154
171
  * [Quick start](https://yla382.github.io/trashpanda/quickstart/): from install to a first run
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dumpsterpanda"
3
- version = "0.2.0"
3
+ version = "0.2.2"
4
4
  description = "Run models bigger than your GPU: layer-by-layer weight streaming across GPU, RAM and disk."
5
5
  authors = ["yla382"]
6
6
  readme = "README.md"
@@ -1,6 +1,6 @@
1
1
  """trash-panda: run models bigger than your GPU by streaming weights across GPU, RAM and disk."""
2
2
 
3
- __version__ = "0.2.0"
3
+ __version__ = "0.2.2"
4
4
 
5
5
  # The offload API is imported lazily so that `import trash_panda` works even where torch is not installed yet.
6
6
  _OFFLOAD_NAMES = (
@@ -5,19 +5,23 @@ try:
5
5
  except ImportError:
6
6
  raise SystemExit("The command line needs extra packages: pip install 'dumpsterpanda[cli]'")
7
7
 
8
+ from .config import config_command
8
9
  from .info import info_command
9
10
  from .plan import plan_command
11
+ from .trace import trace_command
10
12
 
11
13
  app = typer.Typer(no_args_is_help=True, add_completion=False, help="trash-panda: plan and inspect offloaded models.")
12
14
 
13
15
 
14
16
  @app.callback()
15
17
  def _root() -> None:
16
- """The root of the commands: `trash-panda plan ...`, `trash-panda info ...`."""
18
+ """The root of the commands: `trash-panda plan ...`, `trash-panda info ...`, `trash-panda trace ...`, `trash-panda config`."""
17
19
 
18
20
 
19
21
  app.command("plan")(plan_command)
20
22
  app.command("info")(info_command)
23
+ app.command("trace")(trace_command)
24
+ app.command("config")(config_command)
21
25
 
22
26
 
23
27
  def main() -> None:
@@ -0,0 +1,68 @@
1
+ """`trash-panda config`: every setting and environment variable, with the value it has now."""
2
+
3
+ import os
4
+ from dataclasses import fields
5
+
6
+ import typer
7
+ from rich.console import Console
8
+ from rich.table import Table
9
+ from rich.text import Text
10
+
11
+ from .. import paths
12
+
13
+ console = Console()
14
+
15
+
16
+ def _show(value) -> str:
17
+ return str(value).lower() if isinstance(value, bool) else str(value)
18
+
19
+
20
+ def rows() -> list:
21
+ """(variable, value, state) for every variable, the folders first. The state is "default" (not set), "set" or "invalid"."""
22
+ from ..offload import config
23
+
24
+ out = []
25
+ for var, value in (("TRASH_PANDA_HOME", paths.cache_dir()), ("TRASH_PANDA_TRACE_DIR", paths.trace_dir())):
26
+ out.append((var, str(value), "set" if os.environ.get(var, "").strip() else "default"))
27
+ for field in fields(config.DEFAULTS):
28
+ var = config.ENV_PREFIX + field.name.upper()
29
+ default = getattr(config.DEFAULTS, field.name)
30
+ raw = os.environ.get(var)
31
+ if not raw: # an empty variable counts as not set, as in config.current()
32
+ out.append((var, _show(default), "default"))
33
+ continue
34
+ try:
35
+ out.append((var, _show(config._parse(field.name, default, raw)), "set"))
36
+ except ValueError:
37
+ out.append((var, raw, "invalid"))
38
+ return out
39
+
40
+
41
+ def _unknown(known) -> list:
42
+ """TRASH_PANDA_ variables in the environment that nothing reads, which is usually a typo."""
43
+ return sorted(name for name in os.environ if name.upper().startswith("TRASH_PANDA_") and name.upper() not in known)
44
+
45
+
46
+ def config_command() -> None:
47
+ """Show every setting and environment variable with its current value. A value nobody has set is marked (default)."""
48
+ shown = rows()
49
+ table = Table()
50
+ table.add_column("Variable", no_wrap=True)
51
+ table.add_column("Value", overflow="fold")
52
+ for var, value, state in shown:
53
+ cell = Text(value)
54
+ if state == "default":
55
+ cell.append(" (default)", style="dim")
56
+ elif state == "set":
57
+ cell.stylize("bold")
58
+ else:
59
+ cell.append(" (invalid)", style="red")
60
+ table.add_row(var, cell)
61
+ console.print(table)
62
+
63
+ unknown = _unknown({var for var, _, _ in shown})
64
+ if unknown:
65
+ console.print("Not a trash-panda variable, so it is ignored: " + ", ".join(unknown), markup=False)
66
+ if any(state == "invalid" for _, _, state in shown):
67
+ console.print("A value marked invalid cannot be read, and the library raises an error when it runs.", markup=False)
68
+ raise typer.Exit(1)
@@ -0,0 +1,117 @@
1
+ """`trash-panda trace`: open a recorded run in the browser, list the recorded runs, or remove old ones."""
2
+
3
+ import time
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+ from typing import Optional
7
+
8
+ import typer
9
+
10
+ from .. import paths
11
+
12
+
13
+ def _models(path):
14
+ """(the names of the models, the duration in seconds) of a trace file, or None if it cannot be read as one."""
15
+ import json
16
+
17
+ try:
18
+ data = json.loads(path.read_text(encoding="utf-8"))
19
+ plan = data["trashPanda"]
20
+ names = ", ".join(m["name"] for m in plan.get("models", [])) or "-"
21
+ return names, plan["duration_us"] / 1e6
22
+ except (OSError, ValueError, KeyError, TypeError):
23
+ return None
24
+
25
+
26
+ def _list(directory) -> None:
27
+ from rich.console import Console
28
+ from rich.table import Table
29
+
30
+ files = paths.trace_files(directory)
31
+ console = Console()
32
+ if not files:
33
+ console.print(f"No trace files in {directory}. Record one with TRASH_PANDA_TRACE=1.", markup=False)
34
+ return
35
+ table = Table(title=str(directory), title_justify="left")
36
+ for name, justify in (("File", "left"), ("When", "left"), ("Size", "right"), ("Duration", "right"), ("Models", "left")):
37
+ table.add_column(name, justify=justify, no_wrap=name != "Models", overflow="fold")
38
+ for path in files:
39
+ info = path.stat()
40
+ read = _models(path)
41
+ table.add_row(path.name, datetime.fromtimestamp(info.st_mtime).strftime("%Y-%m-%d %H:%M"), paths.fmt_size(info.st_size),
42
+ f"{read[1]:.1f} s" if read else "-", read[0] if read else "invalid")
43
+ console.print(table)
44
+
45
+
46
+ def _clean(directory, keep, older_than, yes) -> None:
47
+ files = paths.trace_files(directory)
48
+ try:
49
+ age = paths.parse_age(older_than) if older_than is not None else None
50
+ except ValueError as exc:
51
+ typer.echo(str(exc), err=True)
52
+ raise typer.Exit(2)
53
+ if keep is not None and keep < 0:
54
+ typer.echo("--keep must be 0 or more.", err=True)
55
+ raise typer.Exit(2)
56
+ doomed = paths.to_clean(files, keep=keep, older_than=age, now=time.time())
57
+ if not doomed:
58
+ typer.echo(f"Nothing to remove in {directory}.")
59
+ return
60
+ total = sum(p.stat().st_size for p in doomed)
61
+ for path in doomed:
62
+ typer.echo(f" {path.name} {paths.fmt_size(path.stat().st_size)}")
63
+ if not yes and not typer.confirm(f"Remove these {len(doomed)} files ({paths.fmt_size(total)}) from {directory}? This cannot be undone"):
64
+ typer.echo("Nothing removed.")
65
+ raise typer.Exit(1)
66
+ for path in doomed:
67
+ path.unlink()
68
+ typer.echo(f"Removed {len(doomed)} files ({paths.fmt_size(total)}).")
69
+
70
+
71
+ def trace_command(
72
+ trace: Optional[Path] = typer.Argument(None, exists=True, readable=True, dir_okay=False,
73
+ help="A trace file written by trash-panda. Without one, the newest in the trace folder."),
74
+ port: int = typer.Option(0, "--port", help="The port to serve on. 0 picks a free one."),
75
+ no_open: bool = typer.Option(False, "--no-open", help="Do not open the browser; only print the address."),
76
+ list_files: bool = typer.Option(False, "--list", help="List the trace files in the trace folder and stop."),
77
+ clean: bool = typer.Option(False, "--clean", help="Remove trace files from the trace folder (it asks first)."),
78
+ keep: Optional[int] = typer.Option(None, "--keep", help="With --clean: keep the newest N files and remove the others."),
79
+ older_than: Optional[str] = typer.Option(None, "--older-than", help="With --clean: remove files older than this, such as 30d, 12h or 2w."),
80
+ yes: bool = typer.Option(False, "--yes", help="With --clean: do not ask first."),
81
+ ) -> None:
82
+ """Show a recorded run as a replay in the browser: blocks moving between disk, RAM and the GPU, and a timeline.
83
+
84
+ Recording is switched on with TRASH_PANDA_TRACE=1; the files go to TRASH_PANDA_TRACE_DIR (default: the trace folder in
85
+ ~/.cache/trash_panda). A small server runs on this machine only, until you press Ctrl+C.
86
+ """
87
+ directory = paths.trace_dir()
88
+ if (list_files or clean) and trace is not None:
89
+ typer.echo("--list and --clean work on the trace folder: give no file.", err=True)
90
+ raise typer.Exit(2)
91
+ if list_files and clean:
92
+ typer.echo("Use --list or --clean, not both.", err=True)
93
+ raise typer.Exit(2)
94
+ if (keep is not None or older_than is not None or yes) and not clean:
95
+ typer.echo("--keep, --older-than and --yes belong to --clean.", err=True)
96
+ raise typer.Exit(2)
97
+ if list_files:
98
+ _list(directory)
99
+ return
100
+ if clean:
101
+ _clean(directory, keep, older_than, yes)
102
+ return
103
+
104
+ from ..ui.load import TraceError, load_trace
105
+ from ..ui.server import serve
106
+
107
+ if trace is None:
108
+ trace = paths.latest_trace(directory)
109
+ if trace is None:
110
+ typer.echo(f"No trace files in {directory}. Record one with TRASH_PANDA_TRACE=1, or give a file.", err=True)
111
+ raise typer.Exit(1)
112
+ try:
113
+ load_trace(trace) # a bad file fails here, not as an empty page
114
+ except TraceError as exc:
115
+ typer.echo(f"{trace.name}: {exc}", err=True)
116
+ raise typer.Exit(1)
117
+ serve(trace, port=port, open_browser=not no_open, echo=typer.echo, directory=directory)
@@ -150,6 +150,11 @@ class Checkpoint:
150
150
  self._stack.close()
151
151
  self._stack, self._handles = None, {}
152
152
 
153
+ @property
154
+ def mapped(self) -> bool:
155
+ """True inside `with checkpoint:`, when tensors are read through a memory map."""
156
+ return bool(self._handles)
157
+
153
158
  @staticmethod
154
159
  def _read_one(fh, info: TensorInfo) -> torch.Tensor:
155
160
  fh.seek(info.start)
@@ -53,6 +53,7 @@ class Defaults:
53
53
 
54
54
  # diagnostics
55
55
  profile: bool = False # print where the prefetching spent its time
56
+ trace: bool = False # record the run in a trace file (docs/design/trace-format.md), in TRASH_PANDA_TRACE_DIR
56
57
 
57
58
 
58
59
  DEFAULTS = Defaults()
@@ -8,6 +8,7 @@ from contextlib import contextmanager
8
8
  from typing import Optional, Tuple
9
9
 
10
10
  from . import config
11
+ from . import trace as _trace
11
12
  from .hooks import _PinnedPool, _empty_host_cache
12
13
  from .stage import PendingStage, preload
13
14
  from .stage import stage as open_stage
@@ -81,11 +82,14 @@ class Coordinator:
81
82
  self.pool: Optional[_PinnedPool] = None
82
83
  self._pending: Optional[Tuple[str, PendingStage]] = None # (name of the step, the model loading for it)
83
84
  self._busy = False
85
+ self._recording = False
84
86
 
85
87
  def __enter__(self) -> "Coordinator":
86
88
  if self.pool is not None:
87
89
  raise RuntimeError("this Coordinator is already in a `with` block")
88
90
  self.pool = _PinnedPool()
91
+ self.pool.trace_kind = "shared"
92
+ self._recording = _trace.open_session(coordinated=True) # one recording for the whole `with` block
89
93
  return self
90
94
 
91
95
  def __exit__(self, exc_type, exc, tb) -> bool:
@@ -95,6 +99,7 @@ class Coordinator:
95
99
  self.pool = None
96
100
  gc.collect()
97
101
  _empty_host_cache()
102
+ _trace.close_session(self._recording)
98
103
  return False
99
104
 
100
105
  def _step(self, name: str) -> Step:
@@ -162,4 +167,5 @@ class Coordinator:
162
167
  cycles through all the buffers there are, so extra ones would be pinned for nothing. The pool keeps the
163
168
  configured number of buffers, at the size they grew to."""
164
169
  if self.pool is not None and self.pool.trim(config.current().pinned_buffers):
170
+ _trace.pool_changed(self.pool)
165
171
  _empty_host_cache()