Dataset Lineage and Provenance › Packing Sequences and Preserving Spans

Michael BrenndoerferNovember 26, 20263 min read
Language AI HandbookAll chapters
Part 21 of 67Data Governance, Provenance, and Synthetic Ecosystems
Chapter 149Dataset Lineage and Provenance › Packing Sequences and Preserving Spans
Reading section 10 of 17 · Packing Sequences and Preserving Spans · 3 minSection 10 of 17 · 3 min

Section 10 of 17 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Sequence length and document-boundary handling depend on the packing and training setup. In this example the packer uses fixed-length windows, combines documents to reduce padding, and retains a map from positions to document owners.

In this example, we read each retained document once and pack its tokens into windows of length eight. Content positions carry a document ID. Padding positions carry no owner. A window can span several documents, and the owner at each position identifies the document that supplied that input token.

PythonIn [20]
CollapseExpand
packed = []
buffer: list[str] = []
owners: list[str | None] = []

for doc in kept:
    for token in doc_tokens[doc["doc_id"]]:
        buffer.append(token)
        owners.append(doc["doc_id"])
        if len(buffer) == SEQ_LEN:
            packed.append(
                {
                    "seq_id": f"seq-{len(packed):03d}",
                    "tokens": tuple(buffer),
                    "owners": tuple(owners),
                }
            )
            buffer, owners = [], []

if buffer:
    while len(buffer) < SEQ_LEN:
        buffer.append("<pad>")
        owners.append(None)
    packed.append(
        {
            "seq_id": f"seq-{len(packed):03d}",
            "tokens": tuple(buffer),
            "owners": tuple(owners),
        }
    )
ConsoleOut [21]
CollapseExpand
Packed sequences: 13
Packed slots: 104
Content tokens: 97
Padding slots: 7
seq-005 ('by', 'nearly', 'two', 'degrees', '.', 'def', 'add', '(')
  owners: ('doc-dd10f119f2fb', 'doc-dd10f119f2fb', 'doc-dd10f119f2fb', 'doc-dd10f119f2fb', 'doc-dd10f119f2fb', 'doc-bcb940d2e6f2', 'doc-bcb940d2e6f2', 'doc-bcb940d2e6f2')
seq-007 ('b', 'Contact', 'Jane', 'Doe', 'at', '[', 'EMAIL', ']')
  owners: ('doc-bcb940d2e6f2', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34')
seq-009 ('transit', 'schedule', '.', 'Section', '4', '.', '2', 'requires')
  owners: ('doc-90510a22cf34', 'doc-90510a22cf34', 'doc-90510a22cf34', 'doc-a86ae901cca2', 'doc-a86ae901cca2', 'doc-a86ae901cca2', 'doc-a86ae901cca2', 'doc-a86ae901cca2')

The output lists windows that cross document boundaries. A window can begin at the end of one document and continue into the next. The owners tuple identifies which document contributed each content position. Character offsets can identify the corresponding span when they specify the text version and coordinate system. A training record can also preserve the associated attention masks and position IDs.

The toy assigns sequence IDs by position. Changing the packing policy can change the tokens and source spans under the same label, such as seq-000. Treat that ID as local to the packing run. Keep the packing policy, tokenizer version or digest, and span map with the packed artifact so readers can interpret its input positions.

The packed sequence span map can be visualized as a token-position grid. Each cell identifies the document that owned the token, with padding left blank.

PythonIn [22]
CollapseExpand
import numpy as np

owner_ids = []
for seq in packed:
    for owner in seq["owners"]:
        if owner is not None and owner not in owner_ids:
            owner_ids.append(owner)
owner_to_code = {owner: index for index, owner in enumerate(owner_ids)}
owner_matrix = np.full((len(packed), SEQ_LEN), np.nan)
for row, seq in enumerate(packed):
    for col, owner in enumerate(seq["owners"]):
        if owner is not None:
            owner_matrix[row, col] = owner_to_code[owner]
owner_matrix_masked = np.ma.masked_invalid(owner_matrix)
VisualizationOut [23]
CollapseExpand
Heatmap of packed sequence token positions colored by owning document.
Document ownership of token positions in each packed sequence. Sequences that cross document boundaries show cells from multiple documents, while padding positions remain blank, illustrating how packing blends content from different sources.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy