File size: 5,612 Bytes
146a370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared schemas and bounded-memory Parquet writing for dataset releases."""

import hashlib
import json
import logging
from pathlib import Path
import time

import pyarrow as pa
import pyarrow.parquet as pq


LOG = logging.getLogger(__name__)
RELEASE_FORMAT_VERSION = 2
# Input compatibility for releases exported before the public terminology change.
LEGACY_SCORE_COLUMNS = {
    "negative_mining_scores": "negative_retrieval_scores",
    "skipped_mining_scores": "skipped_retrieval_scores",
    "mining_score": "retrieval_score",
}


def retrieval_score_schema(schema):
    names = [LEGACY_SCORE_COLUMNS.get(name, name) for name in schema.names]
    if len(names) != len(set(names)):
        raise ValueError("Both legacy and retrieval score columns are present")
    return pa.schema([field.with_name(name) for field, name in zip(schema, names)], metadata=schema.metadata)


GENERATION_ORIGIN = pa.struct([
    ("source_query_id", pa.string()),
    ("query_type", pa.string()),
    ("difficulty", pa.string()),
    ("character", pa.string()),
    ("query_length_target", pa.int64()),
    ("metadata_json", pa.string()),
])
OTHER_QUERY = pa.struct([
    ("query_id", pa.int64()),
    ("mining_query_id", pa.int64()),
    ("query", pa.string()),
    ("query_word_count", pa.int64()),
    ("generation_metadata", pa.list_(GENERATION_ORIGIN)),
])
TRAIN_SCHEMA = pa.schema([
    ("query_id", pa.int64()),
    ("mining_query_id", pa.int64()),
    ("query", pa.string()),
    ("query_word_count", pa.int64()),
    ("generation_metadata", pa.list_(GENERATION_ORIGIN)),
    ("other_queries", pa.list_(OTHER_QUERY)),
    ("positive_idx", pa.int64()),
    ("negative_idxs", pa.list_(pa.int64())),
    ("negative_retrieval_scores", pa.list_(pa.float64())),
    ("negative_reranker_scores", pa.list_(pa.float64())),
    ("negative_reranker_raw_scores", pa.list_(pa.float64())),
    ("positive_reranker_score", pa.float64()),
    ("positive_reranker_raw_score", pa.float64()),
    ("skipped_idxs", pa.list_(pa.int64())),
    ("skipped_retrieval_scores", pa.list_(pa.float64())),
])
REF_SCHEMA = pa.schema([
    ("finepdf_idx", pa.int64()), ("finepdf_id", pa.string()),
    ("source_shard", pa.string()), ("row_in_shard", pa.int64()),
])
NEGATIVE_ALIAS_SCHEMA = pa.schema([
    ("query_id", pa.int64()), ("mining_query_id", pa.int64()),
    ("positive_mining_id", pa.int64()), ("positive_idx", pa.int64()),
    ("removed_mining_id", pa.int64()), ("canonical_idx", pa.int64()),
    ("retained_mining_id", pa.int64()), ("reason", pa.string()),
    ("retrieval_score", pa.float64()), ("reranker_score", pa.float64()),
    ("reranker_raw_score", pa.float64()),
])


def write_json(path, value):
    Path(path).write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
    LOG.info("Wrote %s", path)


def sha256(path):
    digest = hashlib.sha256()
    with Path(path).open("rb") as stream:
        for chunk in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


class ShardedWriter:
    """Write fixed-size shards without buffering an entire output shard."""

    def __init__(self, root, config, schema, rows_per_shard=100_000):
        self.root = Path(root)
        self.config = config
        self.directory = self.root / config
        self.directory.mkdir(parents=True)
        self.schema = schema
        self.capacity = rows_per_shard
        self.writer = None
        self.in_shard = 0
        self.rows = 0
        self.shards = []
        self.started = time.monotonic()
        self.last_report = self.started

    def write(self, table):
        table = table.cast(self.schema)
        offset = 0
        while offset < table.num_rows:
            if self.writer is None:
                self.path = self.directory / "part-{:05d}.parquet".format(len(self.shards))
                self.writer = pq.ParquetWriter(self.path, self.schema, compression="zstd")
                LOG.info("Opening output shard: %s", self.path)
            count = min(self.capacity - self.in_shard, table.num_rows - offset)
            self.writer.write_table(table.slice(offset, count), row_group_size=8192)
            self.rows += count
            self.in_shard += count
            offset += count
            if self.in_shard == self.capacity:
                self._close_shard()
        now = time.monotonic()
        if now - self.last_report >= 60:
            LOG.info("Writing %s: %s rows, %d completed shards, %.1f min elapsed",
                     self.config, format(self.rows, ",d"), len(self.shards), (now - self.started) / 60)
            self.last_report = now

    def _close_shard(self):
        if self.writer is not None:
            self.writer.close()
            LOG.info("Closed %s (%s rows); computing SHA256", self.path, format(self.in_shard, ",d"))
            self.shards.append({
                "path": self.path.relative_to(self.root).as_posix(),
                "start_row": self.rows - self.in_shard,
                "num_rows": self.in_shard,
                "sha256": sha256(self.path),
            })
            LOG.info("Output shard ready: %s (%.1f MiB; %s total %s rows)", self.path,
                     self.path.stat().st_size / (1024 * 1024), format(self.rows, ",d"), self.config)
            self.writer = None
            self.in_shard = 0

    def close(self):
        self._close_shard()
        LOG.info("Finished %s: %s rows in %d shards (%.1f s)", self.config,
                 format(self.rows, ",d"), len(self.shards), time.monotonic() - self.started)
        return {"num_rows": self.rows, "shards": self.shards}