File size: 11,047 Bytes
1bfc3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

#include <algorithm>
#include <cstdint>
#include <vector>

#ifdef _OPENMP
#include <omp.h>
#endif

#include "common.hpp"

namespace py = pybind11;

namespace {

inline uint64_t pair_key(int32_t left, int32_t right) {
  uint32_t low = static_cast<uint32_t>(left < right ? left : right);
  uint32_t high = static_cast<uint32_t>(left < right ? right : left);
  return (static_cast<uint64_t>(low) << 32) | high;
}

struct Corpus {
  int64_t records = 0;
  int32_t width = 0;                 // slots per record == action dimension
  std::vector<int32_t> member;       // [records * width], -1 when free
  std::vector<int64_t> weights;      // [records]
};

}  // namespace

py::tuple train_spatial_native(py::array_t<int32_t, py::array::c_style | py::array::forcecast> codes,
                               py::array_t<int64_t, py::array::c_style | py::array::forcecast> weights,
                               const std::vector<int32_t>& bins, int64_t vocab_size,
                               int threads) {
  auto codes_view = codes.unchecked<2>();
  auto weights_view = weights.unchecked<1>();
  const int64_t records = codes_view.shape(0);
  const int32_t dimension = static_cast<int32_t>(codes_view.shape(1));
  if (dimension != static_cast<int32_t>(bins.size())) {
    throw std::invalid_argument("codes must have one column per bin count");
  }
  if (weights_view.shape(0) != records) {
    throw std::invalid_argument("weights must have one entry per record");
  }

  std::vector<int32_t> offsets(dimension + 1, 0);
  for (int32_t index = 0; index < dimension; ++index) offsets[index + 1] = offsets[index] + bins[index];
  const int32_t atom_count = offsets[dimension];
  if (vocab_size < atom_count) {
    throw std::invalid_argument("vocab_size is below the number of primitive atoms");
  }

#ifdef _OPENMP
  const int thread_count = threads > 0 ? threads : omp_get_max_threads();
#else
  (void)threads;
  const int thread_count = 1;
#endif

  Corpus corpus;
  corpus.records = records;
  corpus.width = dimension;
  corpus.member.resize(static_cast<size_t>(records) * dimension);
  corpus.weights.resize(records);
  int64_t total_weight = 0;
  int64_t initial_length = 0;
  for (int64_t record = 0; record < records; ++record) {
    corpus.weights[record] = weights_view(record);
    total_weight += weights_view(record);
    int32_t active = 0;
    for (int32_t slot = 0; slot < dimension; ++slot) {
      const int32_t value = codes_view(record, slot);
      if (value < -1 || value >= bins[slot]) {
        throw std::invalid_argument("a cell index is outside its coordinate's bin count");
      }
      if (value < 0) {
        corpus.member[static_cast<size_t>(record) * dimension + slot] = -1;
      } else {
        corpus.member[static_cast<size_t>(record) * dimension + slot] = offsets[slot] + value;
        ++active;
      }
    }
    if (active == 0) throw std::invalid_argument("every record needs an active coordinate");
    initial_length += weights_view(record) * active;
  }

  // Occurrence sets, kept sorted so intersections and differences are linear.
  std::vector<std::vector<int32_t>> occurrence(atom_count);
  for (int32_t slot = 0; slot < dimension; ++slot) {
    for (int64_t record = 0; record < records; ++record) {
      const int32_t token = corpus.member[static_cast<size_t>(record) * dimension + slot];
      if (token >= 0) occurrence[token].push_back(static_cast<int32_t>(record));
    }
  }
  for (auto& members : occurrence) std::sort(members.begin(), members.end());

  // --- initial census -------------------------------------------------------
  ac2::CounterMap counts(static_cast<size_t>(records) * 4 + 64);
  {
    std::vector<ac2::CounterMap> shards;
    shards.reserve(thread_count);
    for (int index = 0; index < thread_count; ++index) shards.emplace_back(1024);
#ifdef _OPENMP
#pragma omp parallel num_threads(thread_count)
#endif
    {
#ifdef _OPENMP
      const int thread = omp_get_thread_num();
#else
      const int thread = 0;
#endif
      ac2::CounterMap& local = shards[thread];
#ifdef _OPENMP
#pragma omp for schedule(static)
#endif
      for (int64_t record = 0; record < records; ++record) {
        const int32_t* row = &corpus.member[static_cast<size_t>(record) * dimension];
        const int64_t weight = corpus.weights[record];
        for (int32_t a = 0; a < dimension; ++a) {
          if (row[a] < 0) continue;
          for (int32_t b = a + 1; b < dimension; ++b) {
            if (row[b] < 0) continue;
            local.add(pair_key(row[a], row[b]), weight);
          }
        }
      }
    }
    for (auto& shard : shards) {
      shard.for_each([&](uint64_t key, int64_t value) { counts.add(key, value); });
    }
  }

  std::vector<ac2::HeapEntry> heap;
  heap.reserve(counts.size());
  counts.for_each([&](uint64_t key, int64_t value) { heap.push_back({value, key, 0}); });
  std::make_heap(heap.begin(), heap.end(), ac2::HeapOrder{});
  const size_t initial_pair_types = heap.size();

  // --- greedy merging -------------------------------------------------------
  std::vector<std::pair<int32_t, int32_t>> merges;
  merges.reserve(static_cast<size_t>(vocab_size - atom_count));
  int64_t length = initial_length;  // total active tokens over the corpus
  int64_t heap_pops = 0;
  int64_t counter_updates = 0;

  std::vector<int32_t> matched;
  std::vector<int32_t> remainder;
  std::vector<int32_t> affected;
  std::vector<int32_t> stamp(static_cast<size_t>(vocab_size) + 16, 0);
  int32_t generation = 0;
  std::vector<ac2::CounterMap> deltas;
  deltas.reserve(thread_count);
  for (int index = 0; index < thread_count; ++index) deltas.emplace_back(1024);

  while (static_cast<int64_t>(merges.size()) + atom_count < vocab_size) {
    int32_t left = -1;
    int32_t right = -1;
    int64_t frequency = 0;
    while (!heap.empty()) {
      const ac2::HeapEntry top = ac2::heap_pop(heap);
      ++heap_pops;
      const int64_t current = counts.get(top.key);
      if (current <= 0) continue;
      if (current != top.count) continue;  // a fresher entry for this key exists
      left = static_cast<int32_t>(top.key >> 32);
      right = static_cast<int32_t>(top.key & 0xffffffffu);
      frequency = current;
      break;
    }
    if (left < 0) break;  // every record has collapsed to a single token

    matched.clear();
    std::set_intersection(occurrence[left].begin(), occurrence[left].end(),
                          occurrence[right].begin(), occurrence[right].end(),
                          std::back_inserter(matched));
    if (matched.empty()) {  // cannot happen while counters are exact
      counts.add(pair_key(left, right), -frequency);
      continue;
    }

    const int32_t child = static_cast<int32_t>(occurrence.size());
    occurrence.emplace_back();

    const bool parallel = matched.size() > 4096 && thread_count > 1;
    if (parallel) {
      for (auto& shard : deltas) shard = ac2::CounterMap(1024);
    }
#ifdef _OPENMP
#pragma omp parallel if (parallel) num_threads(thread_count)
#endif
    {
#ifdef _OPENMP
      const int thread = parallel ? omp_get_thread_num() : 0;
#else
      const int thread = 0;
#endif
      ac2::CounterMap& local = parallel ? deltas[thread] : counts;
#ifdef _OPENMP
#pragma omp for schedule(static)
#endif
      for (int64_t index = 0; index < static_cast<int64_t>(matched.size()); ++index) {
        const int32_t record = matched[index];
        const int32_t* row = &corpus.member[static_cast<size_t>(record) * dimension];
        const int64_t weight = corpus.weights[record];
        for (int32_t slot = 0; slot < dimension; ++slot) {
          const int32_t other = row[slot];
          if (other < 0 || other == left || other == right) continue;
          local.add(pair_key(left, other), -weight);
          local.add(pair_key(right, other), -weight);
          local.add(pair_key(child, other), weight);
        }
      }
    }
    if (parallel) {
      for (auto& shard : deltas) {
        shard.for_each([&](uint64_t key, int64_t value) { counts.add(key, value); });
      }
    }

    // Rewrite the touched rows: the child takes the left parent's slot.
    for (const int32_t record : matched) {
      int32_t* row = &corpus.member[static_cast<size_t>(record) * dimension];
      for (int32_t slot = 0; slot < dimension; ++slot) {
        if (row[slot] == left) {
          row[slot] = child;
        } else if (row[slot] == right) {
          row[slot] = -1;
        }
      }
    }

    counts.add(pair_key(left, right), -frequency);
    counter_updates += static_cast<int64_t>(matched.size()) * dimension;

    occurrence[child] = matched;
    for (const int32_t parent : {left, right}) {
      remainder.clear();
      std::set_difference(occurrence[parent].begin(), occurrence[parent].end(), matched.begin(),
                          matched.end(), std::back_inserter(remainder));
      occurrence[parent].swap(remainder);
    }

    affected.clear();
    if (static_cast<size_t>(child) >= stamp.size()) stamp.resize(child + 1024, 0);
    ++generation;
    for (const int32_t record : matched) {
      const int32_t* row = &corpus.member[static_cast<size_t>(record) * dimension];
      for (int32_t slot = 0; slot < dimension; ++slot) {
        const int32_t other = row[slot];
        if (other < 0 || other == child) continue;
        if (stamp[other] == generation) continue;
        stamp[other] = generation;
        affected.push_back(other);
      }
    }
    for (const int32_t other : affected) {
      for (const int32_t parent : {left, right, child}) {
        const uint64_t key = pair_key(parent, other);
        const int64_t value = counts.get(key);
        if (value > 0) ac2::heap_push(heap, {value, key, 0});
      }
    }

    merges.emplace_back(left, right);
    length -= frequency;

    if (heap.size() > 4 * counts.size() + 65536) {
      heap.clear();
      counts.for_each([&](uint64_t key, int64_t value) { heap.push_back({value, key, 0}); });
      std::make_heap(heap.begin(), heap.end(), ac2::HeapOrder{});
    }
  }

  py::list merge_list;
  for (const auto& merge : merges) {
    merge_list.append(py::make_tuple(merge.first, merge.second));
  }
  py::dict report;
  report["records"] = records;
  report["weight"] = total_weight;
  report["atoms"] = atom_count;
  report["merges"] = static_cast<int64_t>(merges.size());
  report["vocab_size"] = atom_count + static_cast<int64_t>(merges.size());
  report["tokens_per_action_initial"] = static_cast<float>(initial_length) / total_weight;
  report["tokens_per_action"] = static_cast<float>(length) / static_cast<float>(total_weight);
  report["compression"] = static_cast<float>(initial_length) / static_cast<float>(length);
  report["initial_pair_types"] = static_cast<int64_t>(initial_pair_types);
  report["heap_pops"] = heap_pops;
  report["counter_updates"] = counter_updates;
  report["threads"] = thread_count;
  report["backend"] = "native_sparse_exact";
  return py::make_tuple(merge_list, report);
}