ssapling 0.1.0__tar.gz → 0.2.0__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 (150) hide show
  1. {ssapling-0.1.0 → ssapling-0.2.0}/CHANGELOG.md +27 -1
  2. {ssapling-0.1.0 → ssapling-0.2.0}/PKG-INFO +1 -1
  3. {ssapling-0.1.0 → ssapling-0.2.0}/docs/data.md +129 -3
  4. {ssapling-0.1.0 → ssapling-0.2.0}/docs/extending.md +11 -2
  5. {ssapling-0.1.0 → ssapling-0.2.0}/docs/train.md +7 -2
  6. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/__init__.py +1 -1
  7. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/__init__.py +2 -1
  8. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/encoding.py +49 -2
  9. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/storage.py +231 -33
  10. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_storage.py +403 -3
  11. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/collators.py +46 -0
  12. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/test_runs.py +35 -0
  13. {ssapling-0.1.0 → ssapling-0.2.0}/.gitignore +0 -0
  14. {ssapling-0.1.0 → ssapling-0.2.0}/LICENSE +0 -0
  15. {ssapling-0.1.0 → ssapling-0.2.0}/README.md +0 -0
  16. {ssapling-0.1.0 → ssapling-0.2.0}/docs/core.md +0 -0
  17. {ssapling-0.1.0 → ssapling-0.2.0}/docs/development.md +0 -0
  18. {ssapling-0.1.0 → ssapling-0.2.0}/docs/environments.md +0 -0
  19. {ssapling-0.1.0 → ssapling-0.2.0}/docs/evaluate.md +0 -0
  20. {ssapling-0.1.0 → ssapling-0.2.0}/docs/interaction.md +0 -0
  21. {ssapling-0.1.0 → ssapling-0.2.0}/docs/models.md +0 -0
  22. {ssapling-0.1.0 → ssapling-0.2.0}/docs/overview.md +0 -0
  23. {ssapling-0.1.0 → ssapling-0.2.0}/docs/tasks.md +0 -0
  24. {ssapling-0.1.0 → ssapling-0.2.0}/docs/teachers.md +0 -0
  25. {ssapling-0.1.0 → ssapling-0.2.0}/docs/usage.md +0 -0
  26. {ssapling-0.1.0 → ssapling-0.2.0}/examples/arithmetic/README.md +0 -0
  27. {ssapling-0.1.0 → ssapling-0.2.0}/examples/arithmetic/components.py +0 -0
  28. {ssapling-0.1.0 → ssapling-0.2.0}/examples/arithmetic/run.py +0 -0
  29. {ssapling-0.1.0 → ssapling-0.2.0}/examples/arithmetic/run.yaml +0 -0
  30. {ssapling-0.1.0 → ssapling-0.2.0}/pyproject.toml +0 -0
  31. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/__init__.py +0 -0
  32. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/_declarations.py +0 -0
  33. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/_execution.py +0 -0
  34. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/_plain.py +0 -0
  35. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/_text.py +0 -0
  36. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/errors.py +0 -0
  37. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/loading.py +0 -0
  38. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/registry.py +0 -0
  39. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/settings.py +0 -0
  40. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/core/spec.py +0 -0
  41. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/_matching.py +0 -0
  42. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/batching.py +0 -0
  43. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/collections.py +0 -0
  44. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/errors.py +0 -0
  45. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/provenance.py +0 -0
  46. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/records.py +0 -0
  47. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/data/sampling.py +0 -0
  48. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/environments/__init__.py +0 -0
  49. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/environments/contracts.py +0 -0
  50. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/environments/errors.py +0 -0
  51. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/environments/runner.py +0 -0
  52. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/environments/types.py +0 -0
  53. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/evaluate/__init__.py +0 -0
  54. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/evaluate/aggregation.py +0 -0
  55. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/evaluate/assessment.py +0 -0
  56. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/evaluate/measurement.py +0 -0
  57. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/interaction/__init__.py +0 -0
  58. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/interaction/episode.py +0 -0
  59. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/interaction/errors.py +0 -0
  60. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/interaction/hosting.py +0 -0
  61. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/models/__init__.py +0 -0
  62. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/models/buffering.py +0 -0
  63. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/models/contracts.py +0 -0
  64. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/models/errors.py +0 -0
  65. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/models/types.py +0 -0
  66. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/py.typed +0 -0
  67. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/registries.py +0 -0
  68. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/tasks/__init__.py +0 -0
  69. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/tasks/contracts.py +0 -0
  70. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/tasks/errors.py +0 -0
  71. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/tasks/types.py +0 -0
  72. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/teachers/__init__.py +0 -0
  73. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/teachers/contracts.py +0 -0
  74. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/teachers/errors.py +0 -0
  75. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/teachers/runner.py +0 -0
  76. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/teachers/types.py +0 -0
  77. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/__init__.py +0 -0
  78. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/components.py +0 -0
  79. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/context.py +0 -0
  80. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/errors.py +0 -0
  81. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/procedure.py +0 -0
  82. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/runner.py +0 -0
  83. {ssapling-0.1.0 → ssapling-0.2.0}/src/ssapling/train/stages.py +0 -0
  84. {ssapling-0.1.0 → ssapling-0.2.0}/tests/__init__.py +0 -0
  85. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/__init__.py +0 -0
  86. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_construction.py +0 -0
  87. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_declarations.py +0 -0
  88. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_execution.py +0 -0
  89. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_loading.py +0 -0
  90. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_plain.py +0 -0
  91. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_public_helpers.py +0 -0
  92. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_registry.py +0 -0
  93. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_settings.py +0 -0
  94. {ssapling-0.1.0 → ssapling-0.2.0}/tests/core/test_spec.py +0 -0
  95. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/__init__.py +0 -0
  96. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_batching.py +0 -0
  97. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_builder.py +0 -0
  98. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_collections.py +0 -0
  99. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_encoding.py +0 -0
  100. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_instances.py +0 -0
  101. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_provenance.py +0 -0
  102. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_records.py +0 -0
  103. {ssapling-0.1.0 → ssapling-0.2.0}/tests/data/test_sampling.py +0 -0
  104. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/__init__.py +0 -0
  105. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/test_adapters.py +0 -0
  106. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/test_contracts.py +0 -0
  107. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/test_registry.py +0 -0
  108. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/test_runner.py +0 -0
  109. {ssapling-0.1.0 → ssapling-0.2.0}/tests/environments/test_types.py +0 -0
  110. {ssapling-0.1.0 → ssapling-0.2.0}/tests/evaluate/__init__.py +0 -0
  111. {ssapling-0.1.0 → ssapling-0.2.0}/tests/evaluate/test_aggregation.py +0 -0
  112. {ssapling-0.1.0 → ssapling-0.2.0}/tests/evaluate/test_assessment.py +0 -0
  113. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/__init__.py +0 -0
  114. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/environments.py +0 -0
  115. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/hosting.py +0 -0
  116. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/models.py +0 -0
  117. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/stages.py +0 -0
  118. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/tasks.py +0 -0
  119. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/teachers.py +0 -0
  120. {ssapling-0.1.0 → ssapling-0.2.0}/tests/fixtures/training.py +0 -0
  121. {ssapling-0.1.0 → ssapling-0.2.0}/tests/interaction/__init__.py +0 -0
  122. {ssapling-0.1.0 → ssapling-0.2.0}/tests/interaction/test_episode.py +0 -0
  123. {ssapling-0.1.0 → ssapling-0.2.0}/tests/interaction/test_hosting.py +0 -0
  124. {ssapling-0.1.0 → ssapling-0.2.0}/tests/interaction/test_timing.py +0 -0
  125. {ssapling-0.1.0 → ssapling-0.2.0}/tests/models/__init__.py +0 -0
  126. {ssapling-0.1.0 → ssapling-0.2.0}/tests/models/test_buffering.py +0 -0
  127. {ssapling-0.1.0 → ssapling-0.2.0}/tests/models/test_contracts.py +0 -0
  128. {ssapling-0.1.0 → ssapling-0.2.0}/tests/models/test_registry.py +0 -0
  129. {ssapling-0.1.0 → ssapling-0.2.0}/tests/packaging/__init__.py +0 -0
  130. {ssapling-0.1.0 → ssapling-0.2.0}/tests/packaging/check_install.py +0 -0
  131. {ssapling-0.1.0 → ssapling-0.2.0}/tests/packaging/test_install.py +0 -0
  132. {ssapling-0.1.0 → ssapling-0.2.0}/tests/tasks/__init__.py +0 -0
  133. {ssapling-0.1.0 → ssapling-0.2.0}/tests/tasks/test_contracts.py +0 -0
  134. {ssapling-0.1.0 → ssapling-0.2.0}/tests/tasks/test_registry.py +0 -0
  135. {ssapling-0.1.0 → ssapling-0.2.0}/tests/tasks/test_types.py +0 -0
  136. {ssapling-0.1.0 → ssapling-0.2.0}/tests/teachers/__init__.py +0 -0
  137. {ssapling-0.1.0 → ssapling-0.2.0}/tests/teachers/test_contracts.py +0 -0
  138. {ssapling-0.1.0 → ssapling-0.2.0}/tests/teachers/test_registry.py +0 -0
  139. {ssapling-0.1.0 → ssapling-0.2.0}/tests/teachers/test_runner.py +0 -0
  140. {ssapling-0.1.0 → ssapling-0.2.0}/tests/teachers/test_types.py +0 -0
  141. {ssapling-0.1.0 → ssapling-0.2.0}/tests/test_docs.py +0 -0
  142. {ssapling-0.1.0 → ssapling-0.2.0}/tests/test_errors.py +0 -0
  143. {ssapling-0.1.0 → ssapling-0.2.0}/tests/test_examples.py +0 -0
  144. {ssapling-0.1.0 → ssapling-0.2.0}/tests/test_immutability.py +0 -0
  145. {ssapling-0.1.0 → ssapling-0.2.0}/tests/test_layering.py +0 -0
  146. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/__init__.py +0 -0
  147. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/test_controllers.py +0 -0
  148. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/test_learn.py +0 -0
  149. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/test_procedures.py +0 -0
  150. {ssapling-0.1.0 → ssapling-0.2.0}/tests/train/test_stages.py +0 -0
@@ -7,6 +7,31 @@ change the public interface; every such change is listed here.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2026-11-29
11
+
12
+ ### Added
13
+
14
+ - **Binary parts in documents** (`ssapling.data`): a codec with the
15
+ optional methods `encode_document` and `decode_document` (the
16
+ `DocumentCodec` protocol) keeps a document's large binary values, such as
17
+ a model's weights in a checkpoint, as raw bytes in files of their own
18
+ (`documents/<name>.parts/`), not as base64. `save_document` and
19
+ `load_document` are called as before, so the `checkpoint` stage and
20
+ `Runner.resume` use parts whenever the run's store has such a codec
21
+ (`output=RecordStore(path, codec=...)`). Parts are written before the
22
+ document's lines, so an interrupted save never leaves a document that
23
+ reads as complete, and a missing or wrong-size part raises `StoreError`.
24
+ Records and collections do not have parts.
25
+
26
+ ### Changed
27
+
28
+ - `STORE_VERSION` is 2. A store is version 1 until a document is first
29
+ written with `encode_document`, and version 2 from then on. Stores written
30
+ by 0.1.0 read as before, and existing codecs write the same bytes as
31
+ before. SSAPling 0.1.0 refuses a version 2 store and asks for an upgrade.
32
+ - A document file whose second line is not the document's body raises
33
+ `StoreError`, as other damaged files do, where it raised `KeyError`.
34
+
10
35
  ## [0.1.0] - 2026-11-17
11
36
 
12
37
  The first release. Every area is built, tested and documented.
@@ -60,5 +85,6 @@ The first release. Every area is built, tested and documented.
60
85
  - `examples/arithmetic/`: a complete small project, in which a learner learns
61
86
  addition from a tutor.
62
87
 
63
- [Unreleased]: https://github.com/KobeKnowles/SSAPling/compare/v0.1.0...HEAD
88
+ [Unreleased]: https://github.com/KobeKnowles/SSAPling/compare/v0.2.0...HEAD
89
+ [0.2.0]: https://github.com/KobeKnowles/SSAPling/compare/v0.1.0...v0.2.0
64
90
  [0.1.0]: https://github.com/KobeKnowles/SSAPling/releases/tag/v0.1.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ssapling
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Infrastructure, interfaces and scaffolding for System-Specification Alignment Problem projects.
5
5
  Project-URL: Repository, https://github.com/KobeKnowles/SSAPling
6
6
  Project-URL: Documentation, https://github.com/KobeKnowles/SSAPling/blob/main/docs/overview.md
@@ -17,7 +17,7 @@ know what a problem means, how it is scored, or how a model encodes it.
17
17
  | [Datasets and streams](#datasets-and-streams) | A collection is a tuple, the remainder an iterator: both iterate the same way. |
18
18
  | [Provenance](#provenance) | Where an item came from, in plain values: enough to trace it or make it again. |
19
19
  | [Storage](#storage) | `RecordStore`: records written step by step and collections saved, as JSON-lines files. |
20
- | [Codecs](#codecs) | How payloads are written: plain values by default, your own codec for anything else. |
20
+ | [Codecs](#codecs) | How payloads are written: plain values by default, your own codec for anything else, binary parts for large values. |
21
21
  | [Batching](#batching) | `batches(items, collate, ...)`, with collation optionally in worker processes. |
22
22
 
23
23
  ## Records
@@ -440,11 +440,12 @@ store.record_ids() # every record's id, sorted
440
440
 
441
441
  ```text
442
442
  runs/baseline/
443
- manifest.json {"format": "ssapling-store", "version": 1}
443
+ manifest.json {"format": "ssapling-store", "version": 1}, or 2 with binary parts
444
444
  records/<record id>.jsonl a header line, one line per step, a summary line
445
445
  records/<record id>.jsonl.gz the same, compressed, with compress=True, once finished
446
446
  collections/<name>.jsonl a header line, one line per item
447
447
  documents/<name>.jsonl a header line, then the document
448
+ documents/<name>.parts/ the document's binary parts, if its codec makes any
448
449
  ```
449
450
 
450
451
  - **Every line is written as it happens**, so a record survives an
@@ -470,6 +471,12 @@ runs/baseline/
470
471
  the directory is and its layout version; a newer version, or a non-empty
471
472
  directory that is not a store, is refused. Each record's header holds its
472
473
  record schema version.
474
+ - **The layout version is 1 or 2.** A store is version 1 until a document
475
+ is first saved with a codec that has `encode_document`, whether or not it
476
+ returns any [binary parts](#binary-parts), and version 2 from then on.
477
+ This SSAPling reads both (`STORE_VERSION` is 2). SSAPling 0.1.0 reads
478
+ version 1 only, and refuses a version 2 store with a `StoreError` that
479
+ says to upgrade.
473
480
 
474
481
  ### Saving and loading collections
475
482
 
@@ -501,6 +508,12 @@ checkpoints (see [Train](train.md#the-output)). A saved name is never
501
508
  overwritten, and loading a name that was never saved raises
502
509
  `UnknownDocumentError`.
503
510
 
511
+ A document is written as one JSON line, so with the `"json"` codec any
512
+ `bytes` in it are written as base64. For a document with large binary
513
+ values, such as a checkpoint of a model's weights, use a codec that keeps
514
+ them as [binary parts](#binary-parts): `save_document` and `load_document`
515
+ are called the same way.
516
+
504
517
  ## Codecs
505
518
 
506
519
  The store writes the structure; a **codec** writes the payloads inside it:
@@ -537,6 +550,118 @@ r.codecs.register("arrays", ArrayCodec)
537
550
  store = RecordStore("runs/baseline", codec="arrays")
538
551
  ```
539
552
 
553
+ ### Binary parts
554
+
555
+ Base64 makes binary data a third larger, and a value inside a JSON line is
556
+ held in memory several times over while it is written and read. That is
557
+ fine for a small array in a record, and costly for a checkpoint of a
558
+ model's weights. So a codec may keep a **document's** large binary values
559
+ out of its JSON, as **binary parts**, or parts for short: raw bytes that
560
+ the store writes to files of their own.
561
+
562
+ Such a codec has two more methods, which the store uses for documents in
563
+ place of `encode` and `decode`:
564
+
565
+ | Method | Is given | Returns |
566
+ | --- | --- | --- |
567
+ | `encode_document(document)` | The whole document. | A pair: the JSON-ready value, and the parts, a list or tuple of `bytes`, `bytearray` or `memoryview` objects. |
568
+ | `decode_document(value, parts)` | The value, and the parts as `bytes`, in the same order. | The document. |
569
+
570
+ - A codec needs both methods or neither: a store refuses, with a
571
+ `TypeError`, a codec that has only one.
572
+ - Records and collections always use `encode` and `decode`, so the codec
573
+ still needs them.
574
+ - `encode_document` is given every document the store saves, a run's
575
+ settings and results as well as its checkpoints, so it returns no parts
576
+ for a document with nothing to keep that way.
577
+ - The value it returns is written as it is, without `encode`, so it must be
578
+ JSON-ready already.
579
+
580
+ The codec decides which values become parts, how many parts there are, and
581
+ how the value refers to them. The store never looks inside either: it
582
+ writes each part as it is, and gives it back as it was. SSAPling has no
583
+ such codec built in. A codec for tensors would put all of a document's
584
+ tensors in one part, in a format such as safetensors, and leave a tag where
585
+ each one was. Here `Tensor`, `to_bytes` and `from_bytes` stand for the
586
+ array library's type and the format's own functions:
587
+
588
+ ```python
589
+ class TensorCodec:
590
+ def __init__(self):
591
+ self.json = JsonCodec()
592
+
593
+ def encode(self, value): ... # records and collections: tensors inline
594
+ def decode(self, value): ...
595
+
596
+ def encode_document(self, document):
597
+ tensors = {}
598
+
599
+ def tagged(value): # each tensor becomes {"__tensor__": "<key>"}
600
+ if isinstance(value, Tensor):
601
+ key = str(len(tensors))
602
+ tensors[key] = value
603
+ return {"__tensor__": key}
604
+ ... # recurse into lists, tuples and dicts
605
+ return value
606
+
607
+ value = self.json.encode(tagged(document))
608
+ return value, [to_bytes(tensors)] if tensors else []
609
+
610
+ def decode_document(self, value, parts):
611
+ tensors = from_bytes(parts[0]) if parts else {}
612
+ return ... # self.json.decode(value), then each tag back to its tensor
613
+
614
+ r.codecs.register("tensors", TensorCodec)
615
+ store = RecordStore("runs/baseline", codec="tensors")
616
+ store.save_document("checkpoints/120", {"step": 120, "weights": weights})
617
+ ```
618
+
619
+ The store then holds:
620
+
621
+ ```text
622
+ documents/checkpoints%2F120.jsonl
623
+ documents/checkpoints%2F120.parts/0-3f9a1c2e.bin the first part, as raw bytes
624
+ ```
625
+
626
+ and `checkpoints%2F120.jsonl` reads:
627
+
628
+ ```text
629
+ {"kind":"document","codec":"tensors","name":"checkpoints/120","parts":[{"file":"0-3f9a1c2e.bin","size":1048576}]}
630
+ {"document":{"step":120,"weights":{"__tensor__":"0"}}}
631
+ ```
632
+
633
+ - **The header line lists the parts**, in order: each one's file and its
634
+ size in bytes. A part's file name is its index and a random token, so no
635
+ two saves ever write the same file.
636
+ - **A document is never read half-written.** The parts are written first,
637
+ each flushed to the disk, and the document's lines last. A save
638
+ interrupted before the lines leaves part files and no document: the name
639
+ is not listed, loading it raises `UnknownDocumentError`, and it can be
640
+ saved again. A save interrupted while the lines are written leaves a
641
+ `.jsonl` file that loading refuses with `StoreError`, as for a document
642
+ without parts.
643
+ - **Part files left by an interrupted save are never read.** The store
644
+ never deletes them, because it cannot tell them from a save in progress.
645
+ They only take up space. Once nothing is writing to the store, any file
646
+ in a `.parts` directory that its document's header does not name, and
647
+ any `.parts` directory with no document, can be deleted by hand.
648
+ - **A missing or truncated part is reported.** A part that is missing, or
649
+ not the size the header says, raises `StoreError` naming the file. The
650
+ store does not checksum parts: use a format that checks itself if that
651
+ matters.
652
+ - **A part is held in memory whole**, when it is saved and when it is
653
+ loaded, where it arrives as `bytes`. The codec does not get the file's
654
+ path, so it cannot memory-map it.
655
+ - **A document and its `.parts` directory go together.** Copy or move
656
+ both. Part file names are random, so two stores holding the same
657
+ documents are not byte for byte the same.
658
+ - **Each document is read as it was written.** A document whose header
659
+ lists parts, even none, is read with `decode_document`; any other, such as
660
+ one from before the codec had these methods, is read with `decode`.
661
+
662
+ `DocumentCodec`, in `ssapling.data`, is the `Protocol` for these two
663
+ methods, for type checking. A codec need not inherit from it.
664
+
540
665
  ## Batching
541
666
 
542
667
  `batches` groups items (records, drawn instances, anything) into batches
@@ -588,11 +713,12 @@ where that batch would have been. Closing the iterator stops the workers.
588
713
  | `SourceExhaustedError` (a `LookupError`) | `max_draws` draws in a row were rejected while building a collection or drawing the remainder. Names the sampler and the reserved collections. |
589
714
  | `HeldOutError` (a `LookupError`) | A held-out collection was asked for without `purpose="evaluation"`. |
590
715
  | `UnknownCollectionError` (a `LookupError`) | No collection has that name; the error lists those that exist. |
591
- | `StoreError` (a `ValueError`) | A store's files are not what SSAPling writes: a non-empty directory that is not a store, a newer store version, an invalid line (named with its file and line), or a file whose first line names no codec. |
716
+ | `StoreError` (a `ValueError`) | A store's files are not what SSAPling writes: a non-empty directory that is not a store, a newer store version, an invalid line (named with its file and line), a file whose first line names no codec, a document's [binary part](#binary-parts) that is missing or the wrong size, or a document with parts whose codec has no `decode_document`. |
592
717
  | `UnknownRecordError` (a `LookupError`) | The store holds no record with that id. |
593
718
  | `UnknownDocumentError` (a `LookupError`) | The store holds no document with that name. |
594
719
  | `ValueError` from `RecordStore` | A record id, collection name or document name already in the store, a step that breaks a [builder check](#building-a-record), or a finished record changed. |
595
720
  | `TypeError` from a codec | A payload the codec cannot encode, named with where it is. Nothing is written. |
721
+ | `TypeError` from `RecordStore` | A codec with only one of `encode_document` and `decode_document`, or an `encode_document` that does not return a JSON-ready value and a list or tuple of `bytes`, `bytearray` or `memoryview` [parts](#binary-parts). Nothing is written. |
596
722
  | `UnknownComponentError` from `RecordStore` or `batches` | A codec or collator name that is not registered. |
597
723
  | `ValueError`, `TypeError` from `batches` | `batch_size` below 1, negative `workers`, or a `collate` or `setup` of the wrong type. |
598
724
  | `ValueError`, `TypeError` from `Collections` | A taken or empty name, a negative size, `max_draws` below 1, a seed that is not an integer, a sampler drawing from another source, a sampler whose `draw` does not return a `Sample`, or a source that neither generates nor lists. |
@@ -19,6 +19,7 @@ area guides have the details.
19
19
  | Model | `models` | `act` | [Models](models.md) |
20
20
  | Sampler | `samplers` | `draw(seed=...)` | [Data: samplers](data.md#samplers) |
21
21
  | Collator | `collators` | a call that turns a list of items into a batch | [Data: batching](data.md#batching) |
22
+ | Codec | `codecs` | `encode`, `decode` | [Data: codecs](data.md#codecs) |
22
23
  | Measurement | `measurements` | a function of a `Trial` | [Evaluate: measurements](evaluate.md#measurements) |
23
24
  | Aggregator | `aggregators` | a function of a list of values | [Evaluate: aggregation](evaluate.md#aggregation) |
24
25
  | Assessment | `assessments` | `async run(model, *, run_id, seed, model_info=None)` | [Evaluate: assessments](evaluate.md#assessments) |
@@ -69,7 +70,8 @@ thread so it does not hold up the rest (see
69
70
  [Interaction: settings](interaction.md#settings)).
70
71
 
71
72
  **State.** A component whose state should survive a checkpoint has
72
- `export_state()`, returning plain data, and `restore_state(state)`. Without
73
+ `export_state()`, returning plain data or data the store's codec can
74
+ save, and `restore_state(state)`. Without
73
75
  them it starts afresh when a run resumes, and a warning names it.
74
76
 
75
77
  ## Tasks
@@ -125,7 +127,7 @@ there is no environment.
125
127
 
126
128
  - [ ] A model that learns lists `learning_operations` and has `learn(operation, **payload)`, returning statistics, which are published.
127
129
  - [ ] It lists the learning `signals` it takes, such as `{"supervision"}` or `{"reward"}`. Only those reach it; teacher feedback never becomes a reward unless the model asks for feedback and treats it as one.
128
- - [ ] Its parameters or memory survive a checkpoint through `export_state`/`restore_state`. For large weights, save them to a file of your own and return the path: a checkpoint is a document, and nothing in a store is overwritten, so use a new file each time.
130
+ - [ ] Its parameters or memory survive a checkpoint through `export_state`/`restore_state`. For large weights, return the tensors or bytes themselves, and run with a store whose codec keeps them as [binary parts](data.md#binary-parts). You write that codec. The checkpoint then holds the weights as raw bytes in a file of their own, not as base64.
129
131
 
130
132
  Optional: `perceive`, `poll` and `advance` for a model that takes its own
131
133
  time; a `key=None` parameter for one model serving several participants or
@@ -158,6 +160,13 @@ of what it accepts.
158
160
  items, returning a batch, such as padded tensors. To collate in worker
159
161
  processes, give it as a registered name, which each worker builds again
160
162
  after running your `register_components`.
163
+ - **A codec** has `encode(value)`, returning JSON-ready values, and
164
+ `decode(value)`, returning what `encode` was given. It usually tags its
165
+ own types, as in `{"__ndarray__": ...}`, and passes the rest to
166
+ `JsonCodec`. Optional `encode_document` and `decode_document` keep a
167
+ document's large values as [binary parts](data.md#binary-parts). Keep it
168
+ registered for as long as its files are read, and give a changed format a
169
+ new name.
161
170
 
162
171
  ## Changing SSAPling's defaults
163
172
 
@@ -513,8 +513,13 @@ The **`checkpoint`** stage saves the run, as the document
513
513
  `checkpoints/<stage>`: the state of every component with `export_state()`,
514
514
  the controller's, and the run context's (its counters, running time,
515
515
  published values and decision log). What each component saves is its own
516
- business; the store's codec writes it. Checkpoints are taken between stages, never in the
517
- middle of an episode:
516
+ business; the store's codec writes it. A codec can keep large binary
517
+ state, such as a model's weights, as raw bytes in files beside the
518
+ checkpoint (see [Data: binary parts](data.md#binary-parts)). Give the run a
519
+ store with that codec, `output=RecordStore("runs/baseline",
520
+ codec="tensors")`, and give `Runner.resume` one too: a directory alone is
521
+ opened with the `"json"` codec. Checkpoints are taken between stages, never
522
+ in the middle of an episode:
518
523
 
519
524
  ```yaml
520
525
  - stage: checkpoint
@@ -7,6 +7,6 @@ downstream projects. Each package area is described in its own ``__init__``.
7
7
 
8
8
  from .core.errors import SSAPlingError
9
9
 
10
- __version__ = "0.1.0"
10
+ __version__ = "0.2.0"
11
11
 
12
12
  __all__ = ["SSAPlingError", "__version__"]
@@ -11,7 +11,7 @@ Enforced by tests/test_layering.py.
11
11
 
12
12
  from .batching import batches, collators
13
13
  from .collections import Collections, Drawn
14
- from .encoding import Codec, JsonCodec, codecs
14
+ from .encoding import Codec, DocumentCodec, JsonCodec, codecs
15
15
  from .errors import (
16
16
  HeldOutError,
17
17
  SourceExhaustedError,
@@ -55,6 +55,7 @@ __all__ = [
55
55
  "Clock",
56
56
  "Codec",
57
57
  "Collections",
58
+ "DocumentCodec",
58
59
  "Drawn",
59
60
  "GeneratedSampler",
60
61
  "GeneratedSettings",
@@ -16,15 +16,19 @@ handling those types and passing everything else to ``JsonCodec``::
16
16
  if isinstance(value, np.ndarray):
17
17
  return {"__ndarray__": value.tolist(), "dtype": str(value.dtype)}
18
18
  ...
19
+
20
+ A codec may also keep a document's large binary values, such as a model's
21
+ weights, out of its JSON, as raw bytes in files of their own: see
22
+ ``DocumentCodec``.
19
23
  """
20
24
 
21
25
  import base64
22
- from collections.abc import Callable, Mapping
26
+ from collections.abc import Callable, Mapping, Sequence
23
27
  from typing import Any, Protocol
24
28
 
25
29
  from ..core import Registry
26
30
 
27
- __all__ = ["Codec", "JsonCodec", "codecs"]
31
+ __all__ = ["Codec", "DocumentCodec", "JsonCodec", "codecs"]
28
32
 
29
33
 
30
34
  class Codec(Protocol):
@@ -39,6 +43,49 @@ class Codec(Protocol):
39
43
  ...
40
44
 
41
45
 
46
+ class DocumentCodec(Codec, Protocol):
47
+ """A codec that keeps a document's large binary values out of its JSON, as **parts**.
48
+
49
+ The two methods are optional. A store whose codec has them uses them for
50
+ documents, in place of ``encode`` and ``decode``, and writes each part as
51
+ raw bytes to a file of its own, with no base64. Records and collections
52
+ always use ``encode`` and ``decode``. A codec needs both methods or
53
+ neither, and need not inherit from this class. ``RecordStore`` uses
54
+ them; whether another ``Store`` does is its own choice.
55
+
56
+ The codec chooses which values become parts, how many parts there are,
57
+ and how the JSON-ready value refers to them; the store never looks inside
58
+ either. A codec for tensors would put all of a document's tensors in one
59
+ part::
60
+
61
+ class TensorCodec:
62
+ def encode_document(self, document):
63
+ tensors = {} # filled by replacing each tensor in
64
+ value = ... # the document with a tag naming it
65
+ return value, [to_bytes(tensors)]
66
+
67
+ def decode_document(self, value, parts):
68
+ tensors = from_bytes(parts[0])
69
+ return ... # each tag replaced by its tensor
70
+ """
71
+
72
+ def encode_document(
73
+ self, document: Mapping[str, Any]
74
+ ) -> tuple[Any, Sequence[bytes | bytearray | memoryview]]:
75
+ """``document`` as a JSON-ready value, and the parts to keep as raw bytes, in order.
76
+
77
+ The parts are a list or a tuple; a ``memoryview`` must be
78
+ contiguous. The value is written as it is, without ``encode``. Return
79
+ no parts for a document with nothing to keep that way. Raise
80
+ ``TypeError`` for a value it cannot encode.
81
+ """
82
+ ...
83
+
84
+ def decode_document(self, value: Any, parts: Sequence[bytes]) -> dict[str, Any]:
85
+ """The document ``encode_document`` was given, from what it returned."""
86
+ ...
87
+
88
+
42
89
  _TUPLE, _BYTES, _DICT = "__tuple__", "__bytes__", "__dict__"
43
90
  _TAGS = (_TUPLE, _BYTES, _DICT)
44
91