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

Replace if-else chain with switch statement. #1638

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 22 additions & 31 deletions src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ void Network::benchmark(const GameState* const state, const int iterations) {
runcount.load(), elapsed, int(runcount.load() / elapsed));
}

void Network::process_bn_var(std::vector<float>& weights, const float epsilon) {
template<class container>
void process_bn_var(container& weights) {
constexpr float epsilon = 1e-5f;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you would want that to be auto.

for (auto&& w : weights) {
w = 1.0f / std::sqrt(w + epsilon);
}
Expand Down Expand Up @@ -206,39 +208,28 @@ std::pair<int, int> Network::load_v1_network(std::istream& wtfile) {
process_bn_var(weights);
m_batchnorm_stddevs.emplace_back(weights);
}
} else if (linecount == plain_conv_wts) {
m_conv_pol_w = std::move(weights);
} else if (linecount == plain_conv_wts + 1) {
m_conv_pol_b = std::move(weights);
} else if (linecount == plain_conv_wts + 2) {
std::copy(cbegin(weights), cend(weights), begin(m_bn_pol_w1));
} else if (linecount == plain_conv_wts + 3) {
process_bn_var(weights);
std::copy(cbegin(weights), cend(weights), begin(m_bn_pol_w2));
} else if (linecount == plain_conv_wts + 4) {
std::copy(cbegin(weights), cend(weights), begin(m_ip_pol_w));
} else if (linecount == plain_conv_wts + 5) {
std::copy(cbegin(weights), cend(weights), begin(m_ip_pol_b));
} else if (linecount == plain_conv_wts + 6) {
m_conv_val_w = std::move(weights);
} else if (linecount == plain_conv_wts + 7) {
m_conv_val_b = std::move(weights);
} else if (linecount == plain_conv_wts + 8) {
std::copy(cbegin(weights), cend(weights), begin(m_bn_val_w1));
} else if (linecount == plain_conv_wts + 9) {
process_bn_var(weights);
std::copy(cbegin(weights), cend(weights), begin(m_bn_val_w2));
} else if (linecount == plain_conv_wts + 10) {
std::copy(cbegin(weights), cend(weights), begin(m_ip1_val_w));
} else if (linecount == plain_conv_wts + 11) {
std::copy(cbegin(weights), cend(weights), begin(m_ip1_val_b));
} else if (linecount == plain_conv_wts + 12) {
std::copy(cbegin(weights), cend(weights), begin(m_ip2_val_w));
} else if (linecount == plain_conv_wts + 13) {
std::copy(cbegin(weights), cend(weights), begin(m_ip2_val_b));
} else {
switch (linecount - plain_conv_wts) {
case 0: m_conv_pol_w = std::move(weights); break;
case 1: m_conv_pol_b = std::move(weights); break;
case 2: std::copy(cbegin(weights), cend(weights), begin(m_bn_pol_w1)); break;
case 3: std::copy(cbegin(weights), cend(weights), begin(m_bn_pol_w2)); break;
case 4: std::copy(cbegin(weights), cend(weights), begin(m_ip_pol_w)); break;
case 5: std::copy(cbegin(weights), cend(weights), begin(m_ip_pol_b)); break;
case 6: m_conv_val_w = std::move(weights); break;
case 7: m_conv_val_b = std::move(weights); break;
case 8: std::copy(cbegin(weights), cend(weights), begin(m_bn_val_w1)); break;
case 9: std::copy(cbegin(weights), cend(weights), begin(m_bn_val_w2)); break;
case 10: std::copy(cbegin(weights), cend(weights), begin(m_ip1_val_w)); break;
case 11: std::copy(cbegin(weights), cend(weights), begin(m_ip1_val_b)); break;
case 12: std::copy(cbegin(weights), cend(weights), begin(m_ip2_val_w)); break;
case 13: std::copy(cbegin(weights), cend(weights), begin(m_ip2_val_b)); break;
}
}
linecount++;
}
process_bn_var(m_bn_pol_w2);
process_bn_var(m_bn_val_w2);

return {channels, static_cast<int>(residual_blocks)};
}
Expand Down
2 changes: 0 additions & 2 deletions src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class Network {
private:
std::pair<int, int> load_v1_network(std::istream& wtfile);
std::pair<int, int> load_network_file(const std::string& filename);
static void process_bn_var(std::vector<float>& weights,
const float epsilon = 1e-5f);

static std::vector<float> winograd_transform_f(const std::vector<float>& f,
const int outputs, const int channels);
Expand Down