Skip to content

Commit

Permalink
Merge pull request #1 from cms-hls4ml/cleanup
Browse files Browse the repository at this point in the history
start cleanup
  • Loading branch information
jmduarte authored Mar 14, 2023
2 parents 18a42b5 + 65705ad commit b063b44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 54 deletions.
75 changes: 22 additions & 53 deletions AXOL1TL_v1/GTADModel_emulator_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,50 @@
#include "emulator.h"
#include "NN/nnet_utils/nnet_common.h"
#include <any>
#include <array>
#include "ap_fixed.h"
#include "ap_int.h"
#include "scales.h"

//type notes
//AD_NN_OUT_SQ_T = resultsq_t = typedef ap_ufixed<18,14> resultsq_t;
//AD_NN_IN_T = input_t = typedef ap_fixed<8, 6, AP_RND_CONV, AP_SAT> input_t;
//AD_NN_OUT_T = result_t = typedef ap_fixed<10,7> result_t;
//unscaled_t = typedef ap_fixed<18,13> unscaled_t;
//resultsq_t = typedef ap_ufixed<18,14> resultsq_t;

//AD_NNNOUTPUTS = N_LAYER_6 = 13
//AD_NNNINPUTS = N_INPUT_1_1 = 57

class GTADModel_emulator_v1 : public hls4mlEmulator::Model {

private:
input_t _input[N_INPUT_1_1];
unscaled_t _unscaled_input[N_INPUT_1_1];
input_t _scaled_input[N_INPUT_1_1];
result_t _result[N_LAYER_6];
resultsq_t _loss;

public:
virtual void prepare_input(std::any input) {
// input_t *input_p = std::any_cast<input_t*>(input);
unscaled_t *unscaled_input_p = std::any_cast<unscaled_t*>(input);
unscaled_t unscaled_input[N_INPUT_1_1];
input_t scaled_input[N_INPUT_1_1];

//first get unscaled inputs
// first get unscaled inputs
for (int i = 0; i < N_INPUT_1_1; i++) {
// _input[i] = std::any_cast<input_t>(input_p[i]);
unscaled_input[i] = std::any_cast<unscaled_t>(unscaled_input_p[i]);
_unscaled_input[i] = std::any_cast<unscaled_t>(unscaled_input_p[i]);
}

//scale inputs
scaleNNInputs(unscaled_input, scaled_input);

//then fill scaled inputs
for (int i = 0; i < N_INPUT_1_1; i++) {
// _input[i] = std::any_cast<input_t>(scaled_input[i])
_input[i] = scaled_input[i]; //dont think any_cast needed here but correct me if I'm wrong
}

// scale inputs
scaleNNInputs(_unscaled_input, _scaled_input);
}



virtual void predict() {
GTADModel_v1(_input, _result);
GTADModel_v1(_scaled_input, _result);
_loss = computeLoss(_result);
}



virtual void read_result(std::any result) {
//original just read result
// result_t *result_p = std::any_cast<result_t*>(result);
// for (int i = 0; i < N_LAYER_6; i++) {
// result_p[i] = _result[i];
// }

//new: compute loss to read result
result_t result_p[N_LAYER_6]; //store preloss _result in result_p, maybe not needed
// return results as an std::pair
// first = reconstructed output
// second = loss
std::pair<std::array<result_t, N_LAYER_6>, resultsq_t> result_p = std::any_cast<std::pair<std::array<result_t, N_LAYER_6>, resultsq_t>>(result);
for (int i = 0; i < N_LAYER_6; i++) {
result_p[i] = _result[i];
}
// resultsq_t *loss = std::any_cast<resultsq_t*>(result); //causes ambiguous conversion error
resultsq_t loss = computeLoss(result_p);
result = loss; //so output loss as "result"
result_p.first[i] = _result[i];
}
result_p.second = _loss;
}



//scaleNNInputs function from https://github.com/cms-l1-globaltrigger/mp7_ugt_legacy/blob/anomaly_detection_trigger/firmware/hls/anomaly_detection/anomaly_detection.cpp#L28
// scaleNNInputs function from
// https://github.com/cms-l1-globaltrigger/mp7_ugt_legacy/blob/anomaly_detection_trigger/firmware/hls/anomaly_detection/anomaly_detection.cpp#L28
virtual void scaleNNInputs(unscaled_t unscaled[N_INPUT_1_1], input_t scaled[N_INPUT_1_1])
{
for (int i = 0; i < N_INPUT_1_1; i++)
Expand All @@ -84,17 +56,14 @@ class GTADModel_emulator_v1 : public hls4mlEmulator::Model {
}
}


// computeLoss function from https://github.com/cms-l1-globaltrigger/mp7_ugt_legacy/blob/anomaly_detection_trigger/firmware/hls/anomaly_detection/anomaly_detection.cpp#L7
// computeLoss function from
// https://github.com/cms-l1-globaltrigger/mp7_ugt_legacy/blob/anomaly_detection_trigger/firmware/hls/anomaly_detection/anomaly_detection.cpp#L7
virtual resultsq_t computeLoss(result_t result_p[N_LAYER_6]) {
// result_t *result_p = std::any_cast<result_t*>(result);
resultsq_t squares[N_LAYER_6];
resultsq_t square_sum;

for (int i = 0; i < N_LAYER_6; i++) {
// result_p[i] = _result[i]; //not needed, should already be the case
resultsq_t sq = result_p[i] * result_p[i];
squares[i] = sq;
squares[i] = result_p[i] * result_p[i];
}
nnet::Op_add<resultsq_t> op;
square_sum = nnet::reduce<resultsq_t, N_LAYER_6, nnet::Op_add<resultsq_t>>(squares, op);
Expand Down
2 changes: 1 addition & 1 deletion AXOL1TL_v1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ GTADModel_v1.so: GTADModel_emulator_v1.o GTADModel_v1.o
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
rm GTADModel_v1.so
rm GTADModel_emulator_v1.o GTADModel_v1.o GTADModel_v1.so

0 comments on commit b063b44

Please sign in to comment.