File size: 12,509 Bytes
685e018
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""Distilled-trace preparation: reasoning traces packed with chained thought slots.

Streams a distilled-reasoning dataset (traces carrying a ``<think>...</think>`` region
followed by a final answer), segments each think region into a chain of thought units, and
renders every example through the shared slot geometry so a block-diffusion model denoises
one thought at a time. The number of thoughts varies per example, so slot count stands in
for how much the model chose to think.

Segmentation splits on paragraph breaks first, then on reasoning-marker sentence boundaries
inside long paragraphs, so a thought is a coherent reasoning move rather than a fixed-size
cut.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import re
from pathlib import Path
from typing import Iterator

import numpy as np

from diffusion_lm.reasoning import (
    ADAPTIVE_SPECIAL_TOKENS,
    ExampleEncoder,
    LayoutSpec,
    REASONING_SPECIAL_TOKENS,
    ReasoningExample,
    size_token_ids,
    _write_packed,
)
from diffusion_lm.tokenizer import load_tokenizer, train_tokenizer_from_iterator

_THINK_RE = re.compile(r'<think>(.*?)</think>\s*', re.DOTALL)
_MARKER_BREAK = re.compile(
    r'(?<=[.!?])\s+(?=(?:Wait|Alternatively|Hmm|Okay|Now|But wait|Let me|First|Second|'
    r'Next|Then|Finally|So,|Also|Another|Actually|Therefore|Thus)\b)'
)
_LONG_PARAGRAPH_CHARS = 900


def segment_thoughts(think: str) -> list[str]:
    """Split a think region into a chain of coherent thought units."""

    thoughts: list[str] = []
    for paragraph in re.split(r'\n\s*\n', think):
        paragraph = ' '.join(paragraph.split())
        if not paragraph:
            continue
        if len(paragraph) > _LONG_PARAGRAPH_CHARS:
            thoughts.extend(part.strip() for part in _MARKER_BREAK.split(paragraph) if part.strip())
        else:
            thoughts.append(paragraph)
    return thoughts


def parse_glaive(row: dict[str, object]) -> ReasoningExample | None:
    """Render one glaive ``reasoning-v1`` row into a chained-thought example."""

    prompt = ' '.join(str(row.get('prompt') or '').split())
    response = str(row.get('response') or '')
    match = _THINK_RE.search(response)
    if not prompt or not match:
        return None
    answer = ' '.join(response[match.end():].split())
    thoughts = segment_thoughts(match.group(1))
    if not answer or not thoughts:
        return None
    return ReasoningExample(prompt, tuple(thoughts), answer, expected_answer='')


PARSERS = {'glaive': parse_glaive}


def _stream_examples(
    dataset: str, split: str, parser_name: str, limit: int
) -> Iterator[ReasoningExample]:
    from datasets import load_dataset

    parser = PARSERS[parser_name]
    kept = 0
    for row in load_dataset(dataset, split=split, streaming=True):
        example = parser(row)
        if example is None:
            continue
        yield example
        kept += 1
        if limit and kept >= limit:
            return


def _is_validation(problem: str, val_fraction: float) -> bool:
    digest = hashlib.sha256(problem.encode('utf-8')).digest()
    return int.from_bytes(digest[:4], 'big') / 2**32 < val_fraction


def _resolve_tokenizer(
    examples: list[ReasoningExample],
    args: argparse.Namespace,
    special_tokens: tuple[str, ...] = REASONING_SPECIAL_TOKENS,
):
    from pathlib import Path

    tokenizer_path = Path(args.tokenizer)
    if tokenizer_path.is_file():
        print(f'reusing tokenizer {tokenizer_path}')
        return load_tokenizer(tokenizer_path), tokenizer_path

    def _texts() -> Iterator[str]:
        for example in examples:
            yield f'{example.problem}\n{" ".join(example.steps)}\n{example.answer}'

    tokenizer = train_tokenizer_from_iterator(
        _texts(),
        tokenizer_path,
        vocab_size=args.vocab_size,
        min_frequency=4,
        length=len(examples),
        extra_special_tokens=special_tokens,
    )
    print(f'trained tokenizer {tokenizer_path} (vocab {args.vocab_size}) on {len(examples):,} traces')
    return tokenizer, tokenizer_path


def prepare(args: argparse.Namespace) -> None:
    adaptive = bool(getattr(args, 'sizes', None))
    spec = LayoutSpec(
        seq_len=args.seq_len,
        block=args.block,
        max_slots=args.max_slots,
        sizes=tuple(args.sizes) if adaptive else (),
    )
    output_dir = Path(args.output_dir)

    # Buffer unique parsed examples so a native tokenizer can be trained in the same
    # streaming pass the corpus is packed from, avoiding a second dataset download.
    examples: list[ReasoningExample] = []
    seen: set[str] = set()
    scanned = 0
    for example in _stream_examples(args.dataset, args.split, args.parser, args.scan_limit):
        scanned += 1
        key = hashlib.sha256(example.problem.encode('utf-8')).hexdigest()
        if key in seen:
            continue
        seen.add(key)
        examples.append(example)
        if args.limit and len(examples) >= args.limit:
            break
    if not examples:
        raise ValueError('no usable examples; check dataset, parser, and seq_len')
    print(f'scanned {scanned:,}, buffered {len(examples):,} unique examples')

    special_tokens = ADAPTIVE_SPECIAL_TOKENS if adaptive else REASONING_SPECIAL_TOKENS
    tokenizer, tokenizer_path = _resolve_tokenizer(examples, args, special_tokens)
    encoder = ExampleEncoder(tokenizer, spec)

    if adaptive:
        _pack_adaptive(examples, encoder, spec, tokenizer, tokenizer_path, output_dir, args)
        return

    split: dict[str, dict[str, list[np.ndarray]]] = {
        'train': {'flat': [], 'flat_regions': [], 'slotted': [], 'slotted_regions': []},
        'validation': {'flat': [], 'flat_regions': [], 'slotted': [], 'slotted_regions': []},
    }
    val_prompts: list[dict[str, str]] = []
    dropped = 0
    for example in examples:
        encoded = encoder.encode_example(example)
        if encoded is None:
            dropped += 1
            continue
        is_val = _is_validation(example.problem, args.val_fraction)
        bucket = split['validation' if is_val else 'train']
        bucket['flat'].append(encoded.flat)
        bucket['flat_regions'].append(encoded.flat_regions)
        bucket['slotted'].append(encoded.slotted)
        bucket['slotted_regions'].append(encoded.slotted_regions)
        if is_val and len(val_prompts) < 500:
            val_prompts.append({'problem': example.problem, 'expected_answer': ''})

    total = sum(len(bucket['flat']) for bucket in split.values())
    if not total:
        raise ValueError('no examples survived encoding; raise seq_len or max_slots')
    print(f'encoded {total:,} examples ({dropped:,} dropped at encode)')
    for split_name, bucket in split.items():
        if not bucket['flat']:
            raise ValueError(f'no examples in the {split_name} split; lower val_fraction or scan more')
        for layout in ('flat', 'slotted'):
            _write_packed(
                output_dir / f'{split_name}-{layout}.bin',
                np.stack(bucket[layout]),
                np.stack(bucket[f'{layout}_regions']),
                layout=layout,
                spec=spec,
                tokenizer_path=tokenizer_path,
                tokenizer=tokenizer,
            )
        print(f'{split_name}: {len(bucket["flat"]):,} examples -> {output_dir}')

    problems_path = output_dir / 'validation-problems.jsonl'
    with problems_path.open('w', encoding='utf-8') as handle:
        for record in val_prompts:
            handle.write(json.dumps(record, ensure_ascii=False) + '\n')
    print(f'wrote {len(val_prompts):,} validation prompts to {problems_path}')


def _pack_adaptive(
    examples: list[ReasoningExample],
    encoder: ExampleEncoder,
    spec: LayoutSpec,
    tokenizer,
    tokenizer_path: Path,
    output_dir: Path,
    args: argparse.Namespace,
) -> None:
    """Encode the adaptive layout and report the block-size distribution it produces."""

    split: dict[str, dict[str, list[np.ndarray]]] = {
        'train': {'tokens': [], 'regions': []},
        'validation': {'tokens': [], 'regions': []},
    }
    val_prompts: list[dict[str, str]] = []
    size_histogram: dict[int, int] = {size: 0 for size in spec.sizes}
    blocks_per_example: list[int] = []
    dropped = 0
    for example in examples:
        encoded = encoder.encode_adaptive(example)
        if encoded is None:
            dropped += 1
            continue
        is_val = _is_validation(example.problem, args.val_fraction)
        bucket = split['validation' if is_val else 'train']
        bucket['tokens'].append(encoded.tokens)
        bucket['regions'].append(encoded.regions)
        blocks_per_example.append(len(encoded.block_sizes))
        for size in encoded.block_sizes:
            size_histogram[size] += 1
        if is_val and len(val_prompts) < 500:
            val_prompts.append({'problem': example.problem, 'expected_answer': ''})

    total = sum(len(bucket['tokens']) for bucket in split.values())
    if not total:
        raise ValueError('no examples survived encoding; raise seq_len, max_slots, or sizes')

    total_blocks = sum(size_histogram.values())
    fractions = {
        size: round(count / max(1, total_blocks), 4) for size, count in size_histogram.items()
    }
    sorted_blocks = sorted(blocks_per_example)

    def percentile(fraction: float) -> int:
        return sorted_blocks[min(len(sorted_blocks) - 1, int(len(sorted_blocks) * fraction))]

    print(f'encoded {total:,} examples ({dropped:,} dropped at encode)')
    print(f'block-size counts {size_histogram} fractions {fractions}')
    print(
        f'blocks/example p50 {percentile(0.5)} p90 {percentile(0.9)} '
        f'max {sorted_blocks[-1]}'
    )

    extra_metadata = {
        'sizes': list(spec.sizes),
        'size_token_ids': size_token_ids(tokenizer),
    }
    for split_name, bucket in split.items():
        if not bucket['tokens']:
            raise ValueError(f'no examples in the {split_name} split; lower val_fraction')
        _write_packed(
            output_dir / f'{split_name}-adaptive.bin',
            np.stack(bucket['tokens']),
            np.stack(bucket['regions']),
            layout='adaptive',
            spec=spec,
            tokenizer_path=tokenizer_path,
            tokenizer=tokenizer,
            extra_metadata=extra_metadata,
        )
        print(f'{split_name}: {len(bucket["tokens"]):,} examples -> {output_dir}')

    problems_path = output_dir / 'validation-problems.jsonl'
    with problems_path.open('w', encoding='utf-8') as handle:
        for record in val_prompts:
            handle.write(json.dumps(record, ensure_ascii=False) + '\n')
    print(f'wrote {len(val_prompts):,} validation prompts to {problems_path}')


def _build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__)
    subparsers = parser.add_subparsers(dest='command', required=True)
    prepare_parser = subparsers.add_parser(
        'prepare', help='stream a distilled-reasoning dataset into chained-thought slots'
    )
    prepare_parser.add_argument('--dataset', default='glaiveai/reasoning-v1-20m')
    prepare_parser.add_argument('--split', default='train')
    prepare_parser.add_argument('--parser', choices=sorted(PARSERS), default='glaive')
    prepare_parser.add_argument(
        '--tokenizer', required=True,
        help='tokenizer path; trained from the buffered traces when the file is absent'
    )
    prepare_parser.add_argument('--vocab-size', type=int, default=16384)
    prepare_parser.add_argument('--output-dir', required=True)
    prepare_parser.add_argument('--seq-len', type=int, default=2048)
    prepare_parser.add_argument('--block', type=int, default=32)
    prepare_parser.add_argument('--max-slots', type=int, default=64)
    prepare_parser.add_argument(
        '--sizes', type=int, nargs='+',
        help='activate the adaptive layout with these ascending block sizes (e.g. 64 256)'
    )
    prepare_parser.add_argument('--val-fraction', type=float, default=0.02)
    prepare_parser.add_argument(
        '--limit', type=int, default=0, help='stop after this many unique kept examples'
    )
    prepare_parser.add_argument(
        '--scan-limit', type=int, default=0, help='stop streaming after this many parsed rows'
    )
    return parser


def main() -> None:
    args = _build_parser().parse_args()
    if args.command == 'prepare':
        prepare(args)


if __name__ == '__main__':
    main()