Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of const_cast in nnue_feature_transformer.h #5740

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/nnue/nnue_feature_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,39 +256,39 @@ class FeatureTransformer {
#endif
}

void permute_weights([[maybe_unused]] void (*order_fn)(uint64_t*)) const {
static void permute_weights([[maybe_unused]] WeightType* weights,
[[maybe_unused]] BiasType* biases,
[[maybe_unused]] void (*order_fn)(uint64_t*)) {
#if defined(USE_AVX2)
#if defined(USE_AVX512)
constexpr IndexType di = 16;
#else
constexpr IndexType di = 8;
#endif
uint64_t* b = reinterpret_cast<uint64_t*>(const_cast<BiasType*>(&biases[0]));
uint64_t* b = reinterpret_cast<uint64_t*>(&biases[0]);
for (IndexType i = 0; i < HalfDimensions * sizeof(BiasType) / sizeof(uint64_t); i += di)
order_fn(&b[i]);

for (IndexType j = 0; j < InputDimensions; ++j)
{
uint64_t* w =
reinterpret_cast<uint64_t*>(const_cast<WeightType*>(&weights[j * HalfDimensions]));
uint64_t* w = reinterpret_cast<uint64_t*>(&weights[j * HalfDimensions]);
for (IndexType i = 0; i < HalfDimensions * sizeof(WeightType) / sizeof(uint64_t);
i += di)
order_fn(&w[i]);
}
#endif
}

inline void scale_weights(bool read) const {
static void scale_weights(WeightType* weights, BiasType* biases, bool read) {
for (IndexType j = 0; j < InputDimensions; ++j)
{
WeightType* w = const_cast<WeightType*>(&weights[j * HalfDimensions]);
WeightType* w = &weights[j * HalfDimensions];
for (IndexType i = 0; i < HalfDimensions; ++i)
w[i] = read ? w[i] * 2 : w[i] / 2;
}

BiasType* b = const_cast<BiasType*>(biases);
for (IndexType i = 0; i < HalfDimensions; ++i)
b[i] = read ? b[i] * 2 : b[i] / 2;
biases[i] = read ? biases[i] * 2 : biases[i] / 2;
}

// Read network parameters
Expand All @@ -298,23 +298,26 @@ class FeatureTransformer {
read_leb_128<WeightType>(stream, weights, HalfDimensions * InputDimensions);
read_leb_128<PSQTWeightType>(stream, psqtWeights, PSQTBuckets * InputDimensions);

permute_weights(inverse_order_packs);
scale_weights(true);
permute_weights(weights, biases, inverse_order_packs);
scale_weights(weights, biases, true);
return !stream.fail();
}

// Write network parameters
bool write_parameters(std::ostream& stream) const {
auto biasesToWrite = std::make_unique<BiasType[]>(HalfDimensions);
auto weightsToWrite = std::make_unique<WeightType[]>(HalfDimensions * InputDimensions);

permute_weights(order_packs);
scale_weights(false);
std::copy(std::begin(biases), std::end(biases), biasesToWrite.get());
std::copy(std::begin(weights), std::end(weights), weightsToWrite.get());

write_leb_128<BiasType>(stream, biases, HalfDimensions);
write_leb_128<WeightType>(stream, weights, HalfDimensions * InputDimensions);
permute_weights(weightsToWrite.get(), biasesToWrite.get(), order_packs);
scale_weights(weightsToWrite.get(), biasesToWrite.get(), false);

write_leb_128<BiasType>(stream, biasesToWrite.get(), HalfDimensions);
write_leb_128<WeightType>(stream, weightsToWrite.get(), HalfDimensions * InputDimensions);
write_leb_128<PSQTWeightType>(stream, psqtWeights, PSQTBuckets * InputDimensions);

permute_weights(inverse_order_packs);
scale_weights(true);
return !stream.fail();
}

Expand Down
Loading