forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 2
Load DNN from Binary #143
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
Draft
GNiendorf
wants to merge
6
commits into
master
Choose a base branch
from
load_dnn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Load DNN from Binary #143
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fdc37be
load dnn from bin initial commit
GNiendorf fda1a28
formatting fix
GNiendorf a175d1c
add code to save network weights to bin
GNiendorf f71b44c
first working version
GNiendorf f636e69
remove hard-coded weights
GNiendorf cc5c65b
more formatting fixes
GNiendorf 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_DenseLayer_h | ||
| #define RecoTracker_LSTCore_interface_DenseLayer_h | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| /** | ||
| * Represents a dense (fully connected) layer with fixed input and output sizes. | ||
| * | ||
| * IN: Number of input neurons | ||
| * OUT: Number of output neurons | ||
| */ | ||
| template <std::size_t IN, std::size_t OUT> | ||
| struct DenseLayer { | ||
| /** | ||
| * Biases: one float per output neuron. | ||
| */ | ||
| std::array<float, OUT> biases{}; | ||
|
|
||
| /** | ||
| * Weights: stored as IN rows of OUT columns. | ||
| */ | ||
| std::array<std::array<float, OUT>, IN> weights{}; | ||
|
|
||
| /** | ||
| * Returns the weight from input neuron index `in` to output neuron index `out`. | ||
| */ | ||
| float getWeight(std::size_t in, std::size_t out) const { return weights[in][out]; } | ||
|
|
||
| static constexpr std::size_t inputSize = IN; | ||
| static constexpr std::size_t outputSize = OUT; | ||
| }; | ||
|
|
||
| #endif |
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 |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_Dnn_h | ||
| #define RecoTracker_LSTCore_interface_Dnn_h | ||
|
|
||
| #include <tuple> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| /** | ||
| * A general Dnn class that holds a sequence (tuple) of DenseLayer<T> types, | ||
| * each with compile-time fixed dimensions. | ||
| * | ||
| * Layers: A parameter pack of layer types (e.g. DenseLayer<23,32>, DenseLayer<32,1>, etc.) | ||
| */ | ||
| template <class... Layers> | ||
| class Dnn { | ||
| public: | ||
| Dnn() = default; | ||
| explicit Dnn(const std::string& filename) { load(filename); } | ||
|
|
||
| /** | ||
| * Loads biases and weights for each layer in the tuple from a binary file. | ||
| */ | ||
| void load(const std::string& filename) { | ||
| std::ifstream file(filename, std::ios::binary); | ||
| if (!file) { | ||
| throw std::runtime_error("Failed to open file: " + filename); | ||
| } | ||
|
|
||
| loadLayers<0>(file); | ||
|
|
||
| if (!file.good()) { | ||
| throw std::runtime_error("Error reading from file: " + filename); | ||
| } | ||
| file.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Prints the biases and weights of each layer to stdout. | ||
| */ | ||
| void print() const { printLayers<0>(); } | ||
|
|
||
| /** | ||
| * A const reference to the underlying tuple of layers. | ||
| */ | ||
| const std::tuple<Layers...>& getLayers() const { return layers_; } | ||
|
|
||
| /** | ||
| * A reference to the underlying tuple of layers. | ||
| */ | ||
| std::tuple<Layers...>& getLayers() { return layers_; } | ||
|
|
||
| private: | ||
| // Store all layers in a compile-time tuple | ||
| std::tuple<Layers...> layers_; | ||
|
|
||
| /** | ||
| * Internal compile-time recursion for loading each layer from file | ||
| */ | ||
| template <std::size_t I> | ||
| typename std::enable_if<I == sizeof...(Layers), void>::type loadLayers(std::ifstream&) { | ||
| // Base case: no more layers to load | ||
| } | ||
|
|
||
| template <std::size_t I> | ||
| typename std::enable_if < I<sizeof...(Layers), void>::type loadLayers(std::ifstream& file) { | ||
| auto& layer = std::get<I>(layers_); | ||
|
|
||
| // Read and verify header information | ||
| uint32_t layer_id, num_inputs, num_outputs; | ||
| file.read(reinterpret_cast<char*>(&layer_id), sizeof(layer_id)); | ||
| file.read(reinterpret_cast<char*>(&num_inputs), sizeof(num_inputs)); | ||
| file.read(reinterpret_cast<char*>(&num_outputs), sizeof(num_outputs)); | ||
|
|
||
| // Verify the dimensions match our template parameters | ||
| if (num_inputs != layer.inputSize || num_outputs != layer.outputSize) { | ||
| throw std::runtime_error("Layer " + std::to_string(I) + | ||
| " dimension mismatch: " | ||
| "expected " + | ||
| std::to_string(layer.inputSize) + "x" + std::to_string(layer.outputSize) + ", got " + | ||
| std::to_string(num_inputs) + "x" + std::to_string(num_outputs)); | ||
| } | ||
|
|
||
| // Verify layer index matches | ||
| if (layer_id != I + 1) { // Assumes 1-based layer IDs | ||
| throw std::runtime_error("Layer index mismatch: expected " + std::to_string(I + 1) + ", got " + | ||
| std::to_string(layer_id)); | ||
| } | ||
|
|
||
| // Read biases | ||
| file.read(reinterpret_cast<char*>(layer.biases.data()), layer.biases.size() * sizeof(float)); | ||
|
|
||
| // Read weights row by row | ||
| for (auto& row : layer.weights) { | ||
| file.read(reinterpret_cast<char*>(row.data()), row.size() * sizeof(float)); | ||
| } | ||
|
|
||
| if (!file.good()) { | ||
| throw std::runtime_error("Failed to read parameters for layer " + std::to_string(I)); | ||
| } | ||
|
|
||
| // Recurse to next layer | ||
| loadLayers<I + 1>(file); | ||
| } | ||
|
|
||
| /** | ||
| * Internal compile-time recursion for printing each layer | ||
| */ | ||
| template <std::size_t I> | ||
| typename std::enable_if<I == sizeof...(Layers), void>::type printLayers() const { | ||
| // Base case: no more layers to print | ||
| } | ||
|
|
||
| template <std::size_t I> | ||
| typename std::enable_if < I<sizeof...(Layers), void>::type printLayers() const { | ||
| const auto& layer = std::get<I>(layers_); | ||
| std::cout << "\n=== Layer " << I + 1 << " ===\nInputs=" << layer.inputSize << ", Outputs=" << layer.outputSize | ||
| << "\n\nBiases:\n"; | ||
|
|
||
| for (float b : layer.biases) { | ||
| std::cout << b << " "; | ||
| } | ||
| std::cout << "\n\nWeights:\n"; | ||
|
|
||
| for (std::size_t in = 0; in < layer.inputSize; ++in) { | ||
| std::cout << " [ "; | ||
| for (std::size_t out = 0; out < layer.outputSize; ++out) { | ||
| std::cout << layer.getWeight(in, out) << " "; | ||
| } | ||
| std::cout << "]\n"; | ||
| } | ||
|
|
||
| // Recurse to next layer | ||
| printLayers<I + 1>(); | ||
| } | ||
| }; | ||
|
|
||
| #endif | ||
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
| #define RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
|
|
||
| #include "RecoTracker/LSTCore/interface/DenseLayer.h" | ||
|
|
||
| namespace lst { | ||
|
|
||
| /** | ||
| * Data structure holding multiple dense layers for the DNN weights. | ||
| */ | ||
| struct DnnWeightsDevData { | ||
| DenseLayer<23, 32> layer1; | ||
| DenseLayer<32, 32> layer2; | ||
| DenseLayer<32, 1> layer3; | ||
|
Comment on lines
+12
to
+14
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. don't we have named constants for these 23 and 32? |
||
| }; | ||
|
|
||
| } // namespace lst | ||
|
|
||
| #endif // RecoTracker_LSTCore_interface_DnnWeightsDevSoA_h | ||
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
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
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include "RecoTracker/LSTCore/interface/MiniDoubletsSoA.h" | ||
|
|
||
| #include "NeuralNetworkWeights.h" | ||
| #include "RecoTracker/LSTCore/interface/DnnWeightsDevSoA.h" | ||
|
|
||
| namespace ALPAKA_ACCELERATOR_NAMESPACE::lst::t5dnn { | ||
|
|
||
|
|
@@ -24,10 +25,11 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst::t5dnn { | |
| } | ||
|
|
||
| template <int IN_FEATURES, int OUT_FEATURES> | ||
| ALPAKA_FN_ACC ALPAKA_FN_INLINE void linear_layer(const float (&input)[IN_FEATURES], | ||
| float (&output)[OUT_FEATURES], | ||
| const float (&weights)[IN_FEATURES][OUT_FEATURES], | ||
| const float (&biases)[OUT_FEATURES]) { | ||
| ALPAKA_FN_ACC ALPAKA_FN_INLINE void linear_layer( | ||
| const float (&input)[IN_FEATURES], | ||
| float (&output)[OUT_FEATURES], | ||
| const std::array<std::array<float, OUT_FEATURES>, IN_FEATURES>& weights, | ||
|
Member
Author
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. |
||
| const std::array<float, OUT_FEATURES>& biases) { | ||
| CMS_UNROLL_LOOP | ||
| for (unsigned int i = 0; i < OUT_FEATURES; ++i) { | ||
| output[i] = biases[i]; | ||
|
|
@@ -52,6 +54,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst::t5dnn { | |
|
|
||
| template <typename TAcc> | ||
| ALPAKA_FN_ACC ALPAKA_FN_INLINE bool runInference(TAcc const& acc, | ||
| lst::DnnWeightsDevData const* dnnPtr, | ||
| MiniDoubletsConst mds, | ||
| const unsigned int mdIndex1, | ||
| const unsigned int mdIndex2, | ||
|
|
@@ -126,15 +129,15 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst::t5dnn { | |
| float x_3[1]; // Layer 3 linear output | ||
|
|
||
| // Layer 1: Linear + Relu | ||
| linear_layer<kinputFeatures, khiddenFeatures>(x, x_1, wgtT_layer1, bias_layer1); | ||
| linear_layer<kinputFeatures, khiddenFeatures>(x, x_1, dnnPtr->layer1.weights, dnnPtr->layer1.biases); | ||
| relu_activation<khiddenFeatures>(x_1); | ||
|
|
||
| // Layer 2: Linear + Relu | ||
| linear_layer<khiddenFeatures, khiddenFeatures>(x_1, x_2, wgtT_layer2, bias_layer2); | ||
| linear_layer<khiddenFeatures, khiddenFeatures>(x_1, x_2, dnnPtr->layer2.weights, dnnPtr->layer2.biases); | ||
| relu_activation<khiddenFeatures>(x_2); | ||
|
|
||
| // Layer 3: Linear + Sigmoid | ||
| linear_layer<khiddenFeatures, 1>(x_2, x_3, wgtT_output_layer, bias_output_layer); | ||
| linear_layer<khiddenFeatures, 1>(x_2, x_3, dnnPtr->layer3.weights, dnnPtr->layer3.biases); | ||
| float x_5 = sigmoid_activation(acc, x_3[0]); | ||
|
|
||
| // Get the bin index based on abs(eta) of first hit and t5_pt | ||
|
|
||
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.
perhaps keep everything in
lstnamespace for now