Skip to content

Commit a6f2b94

Browse files
ChinChangYangclaude
andcommitted
Replace WeightEntry raw ptr+count with a local FloatView
Introduce a KataGo-local non-owning FloatView for WeightEntry::data instead of a raw const float*/size_t pair; convert to MILBlob::Util::Span only inside WeightSerializer, keeping the MILBlob dependency out of Operations.hpp. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 434f975 commit a6f2b94

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

cpp/external/katagocoreml/src/builder/Operations.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ std::string KataGoOps::registerWeight(const std::string& name,
1717
const std::vector<int64_t>& shape) {
1818
WeightEntry entry;
1919
entry.name = name;
20-
entry.data = data.data();
21-
entry.count = data.size();
20+
entry.data = FloatView{data.data(), data.size()};
2221
entry.shape = shape;
2322
entry.blob_offset = 0;
2423
m_weights.push_back(std::move(entry));
@@ -32,8 +31,7 @@ std::string KataGoOps::registerOwnedWeight(const std::string& name,
3231
const std::vector<float>& stored = m_owned.back();
3332
WeightEntry entry;
3433
entry.name = name;
35-
entry.data = stored.data();
36-
entry.count = stored.size();
34+
entry.data = FloatView{stored.data(), stored.size()};
3735
entry.shape = shape;
3836
entry.blob_offset = 0;
3937
m_weights.push_back(std::move(entry));

cpp/external/katagocoreml/src/builder/Operations.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111

1212
namespace katagocoreml {
1313

14-
/// Weight entry for blob file storage. `data`/`count` are a NON-OWNING view into
15-
/// the live KataGoModelDesc (or into KataGoOps::m_owned for derived tensors).
14+
/// Minimal non-owning view over a contiguous float buffer. KataGo-local on
15+
/// purpose: keeps the MILBlob dependency out of this header (conversion to
16+
/// MILBlob::Util::Span happens only at the serializer boundary).
17+
struct FloatView {
18+
const float* ptr = nullptr;
19+
size_t len = 0;
20+
const float* data() const { return ptr; }
21+
size_t size() const { return len; }
22+
bool empty() const { return len == 0; }
23+
float operator[](size_t i) const { return ptr[i]; }
24+
};
25+
26+
/// Weight entry for blob file storage. `data` is a NON-OWNING view into the live
27+
/// KataGoModelDesc (or into KataGoOps::m_owned for derived tensors).
1628
struct WeightEntry {
1729
std::string name;
18-
const float* data = nullptr;
19-
size_t count = 0;
30+
FloatView data; // non-owning view (replaces raw ptr + count)
2031
std::vector<int64_t> shape;
2132
uint64_t blob_offset = 0; // Set during serialization
2233
};

cpp/external/katagocoreml/src/serializer/WeightSerializer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ size_t WeightSerializer::serialize(std::vector<WeightEntry>& weights,
1515
size_t total_bytes = 0;
1616

1717
for (auto& entry : weights) {
18+
const size_t count = entry.data.size();
1819
if (use_fp16) {
1920
// Convert FP32 weights to FP16
20-
std::vector<MILBlob::Fp16> fp16_data(entry.count);
21-
for (size_t i = 0; i < entry.count; ++i) {
21+
std::vector<MILBlob::Fp16> fp16_data(count);
22+
for (size_t i = 0; i < count; ++i) {
2223
fp16_data[i] = MILBlob::Fp16::FromFloat(entry.data[i]);
2324
}
2425
MILBlob::Util::Span<const MILBlob::Fp16> span(fp16_data.data(), fp16_data.size());
2526
entry.blob_offset = writer.WriteData(span);
26-
total_bytes += entry.count * sizeof(MILBlob::Fp16);
27+
total_bytes += count * sizeof(MILBlob::Fp16);
2728
} else {
28-
// Write FP32 weights
29-
MILBlob::Util::Span<const float> span(entry.data, entry.count);
29+
// Write FP32 weights — convert the KataGo-local view to a MILBlob span here.
30+
MILBlob::Util::Span<const float> span(entry.data.data(), count);
3031
entry.blob_offset = writer.WriteData(span);
31-
total_bytes += entry.count * sizeof(float);
32+
total_bytes += count * sizeof(float);
3233
}
3334
}
3435

0 commit comments

Comments
 (0)