Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 12 additions & 18 deletions NAM/activations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,33 @@ nam::activations::ActivationReLU _RELU = nam::activations::ActivationReLU();
nam::activations::ActivationLeakyReLU _LEAKY_RELU = nam::activations::ActivationLeakyReLU();
nam::activations::ActivationSigmoid _SIGMOID = nam::activations::ActivationSigmoid();

bool nam::activations::Activation::using_fast_tanh = false;
std::atomic<bool> nam::activations::Activation::using_fast_tanh{false};

std::unordered_map<std::string, nam::activations::Activation*> nam::activations::Activation::_activations = {
{"Tanh", &_TANH}, {"Hardtanh", &_HARD_TANH}, {"Fasttanh", &_FAST_TANH},
{"ReLU", &_RELU}, {"LeakyReLU", &_LEAKY_RELU}, {"Sigmoid", &_SIGMOID}};

nam::activations::Activation* tanh_bak = nullptr;

nam::activations::Activation* nam::activations::Activation::get_activation(const std::string name)
{
if (_activations.find(name) == _activations.end())
// Return FastTanh when Tanh is requested and fast_tanh mode is enabled
if (name == "Tanh" && using_fast_tanh.load(std::memory_order_relaxed))
Copy link
Owner

Choose a reason for hiding this comment

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

Does this change make the WaveNet's getting of the sigmoid activation for its gating activation no longer real-time safe?

activations::Activation::get_activation("Sigmoid")->apply(this->_z.block(channels, i, channels, 1));

{
return _activations.at("Fasttanh");
}

auto it = _activations.find(name);
if (it == _activations.end())
return nullptr;

return _activations[name];
return it->second;
}

void nam::activations::Activation::enable_fast_tanh()
{
nam::activations::Activation::using_fast_tanh = true;

if (_activations["Tanh"] != _activations["Fasttanh"])
{
tanh_bak = _activations["Tanh"];
_activations["Tanh"] = _activations["Fasttanh"];
}
using_fast_tanh.store(true, std::memory_order_relaxed);
}

void nam::activations::Activation::disable_fast_tanh()
{
nam::activations::Activation::using_fast_tanh = false;

if (_activations["Tanh"] == _activations["Fasttanh"])
{
_activations["Tanh"] = tanh_bak;
}
using_fast_tanh.store(false, std::memory_order_relaxed);
}
5 changes: 3 additions & 2 deletions NAM/activations.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include <string>
#include <atomic>
#include <cmath> // expf
#include <string>
#include <unordered_map>
#include <Eigen/Dense>

Expand Down Expand Up @@ -63,7 +64,7 @@ class Activation
static Activation* get_activation(const std::string name);
static void enable_fast_tanh();
static void disable_fast_tanh();
static bool using_fast_tanh;
static std::atomic<bool> using_fast_tanh;

protected:
static std::unordered_map<std::string, Activation*> _activations;
Expand Down