Skip to content

Commit

Permalink
Merge pull request #3 from cms-hls4ml/private
Browse files Browse the repository at this point in the history
make private
  • Loading branch information
jmduarte authored Mar 15, 2023
2 parents 6287a2f + d93cc7e commit a658b8a
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions AXOL1TL_v1/GTADModel_emulator_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ class GTADModel_emulator_v1 : public hls4mlEmulator::Model {
result_t _result[N_LAYER_6];
resultsq_t _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
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++)
{
unscaled_t tmp0 = unscaled[i] - ad_offsets[i];
input_t tmp1 = tmp0 >> ad_shift[i];
scaled[i] = tmp1;
}
}

// 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]) {
resultsq_t squares[N_LAYER_6];
resultsq_t square_sum;

for (int i = 0; i < N_LAYER_6; i++) {
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);
return square_sum;
}

public:
virtual void prepare_input(std::any input) {
unscaled_t *unscaled_input_p = std::any_cast<unscaled_t*>(input);
Expand All @@ -25,12 +51,12 @@ class GTADModel_emulator_v1 : public hls4mlEmulator::Model {
}

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

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

virtual void read_result(std::any result) {
Expand All @@ -44,31 +70,6 @@ class GTADModel_emulator_v1 : public hls4mlEmulator::Model {
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
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++)
{
unscaled_t tmp0 = unscaled[i] - ad_offsets[i];
input_t tmp1 = tmp0 >> ad_shift[i];
scaled[i] = tmp1;
}
}

// 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]) {
resultsq_t squares[N_LAYER_6];
resultsq_t square_sum;

for (int i = 0; i < N_LAYER_6; i++) {
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);
return square_sum;
}
};

extern "C" hls4mlEmulator::Model* create_model() {
Expand Down

0 comments on commit a658b8a

Please sign in to comment.