Skip to content

Commit 7050f78

Browse files
authored
[FEATURE] Grouped 1x1 convolutions in FiLM modules (#211)
* Add group processing tests to FiLM functionality - Introduced two new test functions: `test_process_with_groups` and `test_process_with_groups_scale_only` to validate the behavior of the FiLM class with grouped convolution parameters. - Updated `run_tests.cpp` to include these new tests, ensuring comprehensive coverage of group processing scenarios. - Enhanced the FiLM constructor to accept a groups parameter, allowing for flexible configuration of grouped convolutions in the processing logic. - Adjusted relevant header files to support the new groups parameter in the FiLM class and its associated structures. * update wavenet_a2_max.nam
1 parent 4063824 commit 7050f78

7 files changed

Lines changed: 265 additions & 746 deletions

File tree

NAM/film.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class FiLM
2323
/// \param condition_dim Size of the conditioning input
2424
/// \param input_dim Size of the input to be modulated
2525
/// \param shift Whether to apply both scale and shift (true) or only scale (false)
26-
FiLM(const int condition_dim, const int input_dim, const bool shift)
27-
: _cond_to_scale_shift(condition_dim, (shift ? 2 : 1) * input_dim, /*bias=*/true)
26+
/// \param groups Number of groups for grouped convolution in the condition-to-scale-shift submodule (default: 1)
27+
FiLM(const int condition_dim, const int input_dim, const bool shift, const int groups = 1)
28+
: _cond_to_scale_shift(condition_dim, (shift ? 2 : 1) * input_dim, /*bias=*/true, groups)
2829
, _do_shift(shift)
2930
{
3031
}

NAM/wavenet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ std::unique_ptr<nam::DSP> nam::wavenet::Factory(const nlohmann::json& config, st
760760
const nlohmann::json& film_config = layer_config[key];
761761
bool active = film_config.value("active", true);
762762
bool shift = film_config.value("shift", true);
763-
return nam::wavenet::_FiLMParams(active, shift);
763+
int groups = film_config.value("groups", 1);
764+
return nam::wavenet::_FiLMParams(active, shift, groups);
764765
};
765766

766767
// Parse FiLM parameters

NAM/wavenet.h

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ struct _FiLMParams
6767
/// \brief Constructor
6868
/// \param active_ Whether FiLM is active at this location
6969
/// \param shift_ Whether to apply both scale and shift (true) or only scale (false)
70-
_FiLMParams(bool active_, bool shift_)
70+
/// \param groups_ Number of groups for grouped convolution in the condition-to-scale-shift submodule (default: 1)
71+
_FiLMParams(bool active_, bool shift_, int groups_ = 1)
7172
: active(active_)
7273
, shift(shift_)
74+
, groups(groups_)
7375
{
7476
}
7577
const bool active; ///< Whether FiLM is active
7678
const bool shift; ///< Whether to apply shift in addition to scale
79+
const int groups; ///< Number of groups for grouped convolution in the condition-to-scale-shift submodule
7780
};
7881

7982
/// \brief Parameters for constructing a single Layer
@@ -215,48 +218,53 @@ class _Layer
215218
// Initialize FiLM objects
216219
if (params.conv_pre_film_params.active)
217220
{
218-
_conv_pre_film =
219-
std::make_unique<FiLM>(params.condition_size, params.channels, params.conv_pre_film_params.shift);
221+
_conv_pre_film = std::make_unique<FiLM>(
222+
params.condition_size, params.channels, params.conv_pre_film_params.shift, params.conv_pre_film_params.groups);
220223
}
221224
if (params.conv_post_film_params.active)
222225
{
223226
const int conv_out_channels =
224227
(params.gating_mode != GatingMode::NONE) ? 2 * params.bottleneck : params.bottleneck;
225-
_conv_post_film =
226-
std::make_unique<FiLM>(params.condition_size, conv_out_channels, params.conv_post_film_params.shift);
228+
_conv_post_film = std::make_unique<FiLM>(params.condition_size, conv_out_channels,
229+
params.conv_post_film_params.shift, params.conv_post_film_params.groups);
227230
}
228231
if (params.input_mixin_pre_film_params.active)
229232
{
230233
_input_mixin_pre_film =
231-
std::make_unique<FiLM>(params.condition_size, params.condition_size, params.input_mixin_pre_film_params.shift);
234+
std::make_unique<FiLM>(params.condition_size, params.condition_size, params.input_mixin_pre_film_params.shift,
235+
params.input_mixin_pre_film_params.groups);
232236
}
233237
if (params.input_mixin_post_film_params.active)
234238
{
235239
const int input_mixin_out_channels =
236240
(params.gating_mode != GatingMode::NONE) ? 2 * params.bottleneck : params.bottleneck;
237-
_input_mixin_post_film = std::make_unique<FiLM>(
238-
params.condition_size, input_mixin_out_channels, params.input_mixin_post_film_params.shift);
241+
_input_mixin_post_film =
242+
std::make_unique<FiLM>(params.condition_size, input_mixin_out_channels,
243+
params.input_mixin_post_film_params.shift, params.input_mixin_post_film_params.groups);
239244
}
240245
if (params.activation_pre_film_params.active)
241246
{
242247
const int z_channels = (params.gating_mode != GatingMode::NONE) ? 2 * params.bottleneck : params.bottleneck;
243248
_activation_pre_film =
244-
std::make_unique<FiLM>(params.condition_size, z_channels, params.activation_pre_film_params.shift);
249+
std::make_unique<FiLM>(params.condition_size, z_channels, params.activation_pre_film_params.shift,
250+
params.activation_pre_film_params.groups);
245251
}
246252
if (params.activation_post_film_params.active)
247253
{
248254
_activation_post_film =
249-
std::make_unique<FiLM>(params.condition_size, params.bottleneck, params.activation_post_film_params.shift);
255+
std::make_unique<FiLM>(params.condition_size, params.bottleneck, params.activation_post_film_params.shift,
256+
params.activation_post_film_params.groups);
250257
}
251258
if (params._1x1_post_film_params.active)
252259
{
253-
_1x1_post_film =
254-
std::make_unique<FiLM>(params.condition_size, params.channels, params._1x1_post_film_params.shift);
260+
_1x1_post_film = std::make_unique<FiLM>(params.condition_size, params.channels,
261+
params._1x1_post_film_params.shift, params._1x1_post_film_params.groups);
255262
}
256263
if (params.head1x1_post_film_params.active && params.head1x1_params.active)
257264
{
258-
_head1x1_post_film = std::make_unique<FiLM>(
259-
params.condition_size, params.head1x1_params.out_channels, params.head1x1_post_film_params.shift);
265+
_head1x1_post_film =
266+
std::make_unique<FiLM>(params.condition_size, params.head1x1_params.out_channels,
267+
params.head1x1_post_film_params.shift, params.head1x1_post_film_params.groups);
260268
}
261269
};
262270

0 commit comments

Comments
 (0)