Skip to content

Commit

Permalink
Fix some buffers having uninitialized contents after resizing (#51)
Browse files Browse the repository at this point in the history
* Fix some buffers having uninitialized contents after resizing

* Switch to native way of initing matrices

---------

Co-authored-by: Steven Atkinson <[email protected]>
  • Loading branch information
daleonov and sdatkinson authored Sep 5, 2023
1 parent 0ee7c02 commit ca5b3a2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion NAM/convnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ convnet::ConvNet::ConvNet(const double loudness, const int channels, const std::
for (size_t i = 0; i < dilations.size(); i++)
this->_blocks[i].set_params_(i == 0 ? 1 : channels, channels, dilations[i], batchnorm, activation, it);
this->_block_vals.resize(this->_blocks.size() + 1);
for (auto& matrix : this->_block_vals)
matrix.setZero();
std::fill(this->_input_buffer.begin(), this->_input_buffer.end(), 0.0f);
this->_head = _Head(channels, it);
if (it != params.end())
throw std::runtime_error("Didn't touch all the params when initializing wavenet");
Expand Down Expand Up @@ -147,9 +150,20 @@ void convnet::ConvNet::_update_buffers_()
{
this->Buffer::_update_buffers_();
const size_t buffer_size = this->_input_buffer.size();
this->_block_vals[0].resize(1, buffer_size);

if (this->_block_vals[0].rows() != 1 || this->_block_vals[0].cols() != buffer_size)
{
this->_block_vals[0].resize(1, buffer_size);
this->_block_vals[0].setZero();
}

for (size_t i = 1; i < this->_block_vals.size(); i++)
{
if (this->_block_vals[i].rows() == this->_blocks[i - 1].get_out_channels() && this->_block_vals[i].cols() == buffer_size)
continue; // Already has correct size
this->_block_vals[i].resize(this->_blocks[i - 1].get_out_channels(), buffer_size);
this->_block_vals[i].setZero();
}
}

void convnet::ConvNet::_rewind_buffers_()
Expand Down
3 changes: 3 additions & 0 deletions NAM/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void Buffer::_set_receptive_field(const int new_receptive_field, const int input
{
this->_receptive_field = new_receptive_field;
this->_input_buffer.resize(input_buffer_size);
std::fill (this->_input_buffer.begin(), this->_input_buffer.end(), 0.0f);
this->_reset_input_buffer();
}

Expand All @@ -132,6 +133,7 @@ void Buffer::_update_buffers_()
while (new_buffer_size < minimum_input_buffer_size)
new_buffer_size *= 2;
this->_input_buffer.resize(new_buffer_size);
std::fill (this->_input_buffer.begin(), this->_input_buffer.end(), 0.0f);
}
}

Expand All @@ -144,6 +146,7 @@ void Buffer::_update_buffers_()
this->_input_buffer[i] = this->_input_post_gain[j];
// And resize the output buffer:
this->_output_buffer.resize(num_frames);
std::fill (this->_output_buffer.begin(), this->_output_buffer.end(), 0.0f);
}

void Buffer::_rewind_buffers_()
Expand Down
2 changes: 1 addition & 1 deletion NAM/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ std::string util::lowercase(const std::string& s)
std::string out(s);
std::transform(s.begin(), s.end(), out.begin(), [](unsigned char c) { return std::tolower(c); });
return out;
}
}
1 change: 1 addition & 0 deletions NAM/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Utilities

#include <string>
#include <Eigen/Dense> // Eigen::MatrixXf

namespace util
{
Expand Down
11 changes: 11 additions & 0 deletions NAM/wavenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Eigen/Dense>

#include "wavenet.h"
#include "util.h"

wavenet::_DilatedConv::_DilatedConv(const int in_channels, const int out_channels, const int kernel_size,
const int bias, const int dilation)
Expand Down Expand Up @@ -48,7 +49,11 @@ void wavenet::_Layer::process_(const Eigen::MatrixXf& input, const Eigen::Matrix

void wavenet::_Layer::set_num_frames_(const long num_frames)
{
if (this->_z.rows() == this->_conv.get_out_channels() && this->_z.cols() == num_frames)
return; // Already has correct size

this->_z.resize(this->_conv.get_out_channels(), num_frames);
this->_z.setZero();
}

// LayerArray =================================================================
Expand Down Expand Up @@ -211,7 +216,12 @@ void wavenet::_Head::process_(Eigen::MatrixXf& inputs, Eigen::MatrixXf& outputs)
void wavenet::_Head::set_num_frames_(const long num_frames)
{
for (size_t i = 0; i < this->_buffers.size(); i++)
{
if (this->_buffers[i].rows() == this->_channels && this->_buffers[i].cols() == num_frames)
continue; // Already has correct size
this->_buffers[i].resize(this->_channels, num_frames);
this->_buffers[i].setZero();
}
}

void wavenet::_Head::_apply_activation_(Eigen::MatrixXf& x)
Expand Down Expand Up @@ -371,6 +381,7 @@ void wavenet::WaveNet::_set_num_frames_(const long num_frames)
for (size_t i = 0; i < this->_layer_array_outputs.size(); i++)
this->_layer_array_outputs[i].resize(this->_layer_array_outputs[i].rows(), num_frames);
this->_head_output.resize(this->_head_output.rows(), num_frames);
this->_head_output.setZero();

for (size_t i = 0; i < this->_layer_arrays.size(); i++)
this->_layer_arrays[i].set_num_frames_(num_frames);
Expand Down

0 comments on commit ca5b3a2

Please sign in to comment.