-
Notifications
You must be signed in to change notification settings - Fork 105
Configurable activations #194
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13208e2
Updated how activations are initialized from config. Passing an objec…
52023ef
Removed broadcasting from PReLU
273d53f
Restored build/.gitignore
ed9569b
Converted the output type of get_activation to a shared_ptr so caller…
9c64e51
Updated get_activation to take an ActivationConfig instead of a JSON,…
87ea576
Deleted tests for a function that was removed from the codebase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,58 @@ | ||
| #pragma once | ||
|
|
||
| #include <cassert> | ||
| #include <string> | ||
| #include <cmath> // expf | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include <Eigen/Dense> | ||
| #include <functional> | ||
|
|
||
| #include "json.hpp" | ||
|
|
||
| namespace nam | ||
| { | ||
| namespace activations | ||
| { | ||
|
|
||
| // Forward declaration | ||
| class Activation; | ||
|
|
||
| // Strongly-typed activation type enum | ||
| enum class ActivationType | ||
| { | ||
| Tanh, | ||
| Hardtanh, | ||
| Fasttanh, | ||
| ReLU, | ||
| LeakyReLU, | ||
| PReLU, | ||
| Sigmoid, | ||
| SiLU, // aka Swish | ||
| Hardswish, | ||
| LeakyHardtanh | ||
| }; | ||
|
|
||
| // Strongly-typed activation configuration | ||
| struct ActivationConfig | ||
| { | ||
| ActivationType type; | ||
|
|
||
| // Optional parameters (used by specific activation types) | ||
| std::optional<float> negative_slope; // LeakyReLU, PReLU (single) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL |
||
| std::optional<std::vector<float>> negative_slopes; // PReLU (per-channel) | ||
| std::optional<float> min_val; // LeakyHardtanh | ||
| std::optional<float> max_val; // LeakyHardtanh | ||
| std::optional<float> min_slope; // LeakyHardtanh | ||
| std::optional<float> max_slope; // LeakyHardtanh | ||
|
|
||
| // Convenience constructors | ||
| static ActivationConfig simple(ActivationType t); | ||
| static ActivationConfig from_json(const nlohmann::json& j); | ||
| }; | ||
| inline float relu(float x) | ||
| { | ||
| return x > 0.0f ? x : 0.0f; | ||
|
|
@@ -91,6 +133,9 @@ inline float hardswish(float x) | |
| class Activation | ||
| { | ||
| public: | ||
| // Type alias for shared pointer to Activation | ||
| using Ptr = std::shared_ptr<Activation>; | ||
|
|
||
| Activation() = default; | ||
| virtual ~Activation() = default; | ||
| virtual void apply(Eigen::MatrixXf& matrix) { apply(matrix.data(), matrix.rows() * matrix.cols()); } | ||
|
|
@@ -101,15 +146,17 @@ class Activation | |
| } | ||
| virtual void apply(float* data, long size) {} | ||
|
|
||
| static Activation* get_activation(const std::string name); | ||
| static Ptr get_activation(const std::string name); | ||
| static Ptr get_activation(const ActivationConfig& config); | ||
| static Ptr get_activation(const nlohmann::json& activation_config); | ||
| static void enable_fast_tanh(); | ||
| static void disable_fast_tanh(); | ||
| static bool using_fast_tanh; | ||
| static void enable_lut(std::string function_name, float min, float max, std::size_t n_points); | ||
| static void disable_lut(std::string function_name); | ||
|
|
||
| protected: | ||
| static std::unordered_map<std::string, Activation*> _activations; | ||
| static std::unordered_map<std::string, Ptr> _activations; | ||
| }; | ||
|
|
||
| // identity function activation | ||
|
|
@@ -226,20 +273,21 @@ class ActivationPReLU : public Activation | |
| void apply(Eigen::MatrixXf& matrix) override | ||
| { | ||
| // Matrix is organized as (channels, time_steps) | ||
| int n_channels = negative_slopes.size(); | ||
| int actual_channels = matrix.rows(); | ||
|
|
||
| // NOTE: check not done during runtime on release builds | ||
| // model loader should make sure dimensions match | ||
| assert(actual_channels == n_channels); | ||
|
|
||
| unsigned long actual_channels = static_cast<unsigned long>(matrix.rows()); | ||
|
|
||
| // Prepare the slopes for the current matrix size | ||
| std::vector<float> slopes_for_channels = negative_slopes; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worried that this might not be real-time safe. |
||
|
|
||
| // Fail loudly if input has more channels than activation | ||
| assert(actual_channels == negative_slopes.size()); | ||
|
|
||
| // Apply each negative slope to its corresponding channel | ||
| for (int channel = 0; channel < std::min(n_channels, actual_channels); channel++) | ||
| for (unsigned long channel = 0; channel < actual_channels; channel++) | ||
| { | ||
| // Apply the negative slope to all time steps in this channel | ||
| for (int time_step = 0; time_step < matrix.rows(); time_step++) | ||
| for (int time_step = 0; time_step < matrix.cols(); time_step++) | ||
| { | ||
| matrix(channel, time_step) = leaky_relu(matrix(channel, time_step), negative_slopes[channel]); | ||
| matrix(channel, time_step) = leaky_relu(matrix(channel, time_step), slopes_for_channels[channel]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This enum is good for now.
In the future, I may think about reworking this so that other libraries can build on & extend it.