Skip to content
Merged
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
66 changes: 32 additions & 34 deletions tmva/sofie/inc/TMVA/ROperator_Elu.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,77 @@

#include <sstream>

namespace TMVA{
namespace Experimental{
namespace SOFIE{
namespace TMVA {
namespace Experimental {
namespace SOFIE {

template <typename T>
class ROperator_Elu final : public ROperator
{
class ROperator_Elu final : public ROperator {

private:

/* Attributes*/
float falpha= 1.0; //default value
float falpha = 1.0; // default value
std::string fNX;
std::string fNY;
std::vector<size_t> fShape;
std::string fType;

public:
ROperator_Elu(){}
ROperator_Elu(float alpha,std::string nameX, std::string nameY):
falpha(alpha),fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
ROperator_Elu() {}
ROperator_Elu(float alpha, std::string nameX, std::string nameY)
: falpha(alpha), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
{
fInputTensorNames = { fNX };
fOutputTensorNames = { fNY };
if(std::is_same<T, float>::value){
fInputTensorNames = {fNX};
fOutputTensorNames = {fNY};

if (std::is_same<T, float>::value) {
fType = "float";
} else {
throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Elu operator");
}
else{
throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Elu operator");
}
}

std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
return input;
}
std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override { return input; }

std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
auto ret = input; //suggest copy to compiler
std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override
{
auto ret = input; // suggest copy to compiler
return ret;
}

void Initialize(RModel& model) override {
if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
void Initialize(RModel &model) override
{
if (model.CheckIfTensorAlreadyExist(fNX) ==
false) { // input must be a graph input, or already initialized intermediate tensor
throw std::runtime_error("TMVA SOFIE Elu Op Input Tensor is not found in model");
}
fShape = model.GetTensorShape(fNX);
model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
}


std::string Generate(std::string OpName) override {
std::string Generate(std::string OpName) override
{
OpName = "op_" + OpName;
if (fShape.empty()) {
throw std::runtime_error("TMVA SOFIE Operator Elu called to Generate without being initialized first");
}
std::stringstream out;
size_t length = ConvertShapeToLength(fShape);

out << SP << "float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10) << falpha << ";\n";
out << SP << "float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10)
<< falpha << ";\n";

out << "\n//------ ELU \n";
out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX << "[id] : "<< OpName << "_alpha * std::exp(tensor_"<< fNX<<"[id]) - 1);\n";
out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX
<< "[id] : " << OpName << "_alpha * (std::exp(tensor_" << fNX << "[id]) - 1));\n";
Comment thread
guitargeek marked this conversation as resolved.
Comment thread
guitargeek marked this conversation as resolved.
out << SP << "}\n";
return out.str();
}

};

}//SOFIE
}//Experimental
}//TMVA

} // namespace SOFIE
} // namespace Experimental
} // namespace TMVA

#endif //TMVA_SOFIE_ROPERATOR_Elu
#endif // TMVA_SOFIE_ROPERATOR_Elu
17 changes: 17 additions & 0 deletions tmva/sofie/test/TestCustomModelsFromONNX.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ constexpr auto modelDataSuffix = "_FromONNX.dat";
#include "input_models/references/Log.ref.hxx"
#include "input_models/references/Elu.ref.hxx"
#include "input_models/references/Equal.ref.hxx"
#include "input_models/references/EluAlpha.ref.hxx"
#include "input_models/references/LessOrEqual.ref.hxx"
#include "input_models/references/GreaterOrEqual.ref.hxx"
#include "input_models/references/Less.ref.hxx"
Expand Down Expand Up @@ -308,6 +309,22 @@ TEST(ONNX, Elu)
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
}
}
TEST(ONNX, EluAlpha)
{
constexpr float TOLERANCE = DEFAULT_TOLERANCE;
// Regression test for alpha != 1.0 (fixes #21539)
std::vector<float> input({
1.0, -2.0, 3.0, 0.5, -1.0, 2.0
});
ASSERT_INCLUDE_AND_RUN(std::vector<float>, "EluAlpha", input);
// Checking output size
EXPECT_EQ(output.size(), sizeof(EluAlpha_ExpectedOutput::outputs) / sizeof(float));
float *correct = EluAlpha_ExpectedOutput::outputs;
// Checking every output value, one by one
for (size_t i = 0; i < output.size(); ++i) {
EXPECT_LE(std::abs(output[i] - correct[i]), TOLERANCE);
}
}

TEST(ONNX, Constant)
{
Expand Down
Binary file added tmva/sofie/test/input_models/EluAlpha.onnx
Binary file not shown.
6 changes: 6 additions & 0 deletions tmva/sofie/test/input_models/references/EluAlpha.ref.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EluAlpha_ExpectedOutput{
float outputs[] = {
1.0000, -0.4323, 3.0000,
0.5000, -0.3161, 2.0000
};
} // namespace EluAlpha_ExpectedOutput
Loading