Skip to content
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

[tmva][sofie] Add TopK operator #15886

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lmoneta
Copy link
Member

@lmoneta lmoneta commented Jun 19, 2024

This Pull request adds the support for TopK operator in SOFIE

Implementation provided by GSOC student from Vedant Mehra.

This PR is based on #15837 and should be merged after that one and eventually rebased

@lmoneta lmoneta requested a review from bellenot as a code owner June 19, 2024 16:36
@lmoneta lmoneta requested a review from sanjibansg June 19, 2024 16:36
Comment on lines 111 to 112
//std::cout << "Reduce operator - axis = " << fAttrAxes[0] << " shape x " << ConvertShapeToString(fShapeX)
// << " output shape " << ConvertShapeToString(fShapeY) << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can remove this commented code?

Comment on lines +82 to +95
// if(fK>fShapeX[fAttrAxis]){
// throw
// std::runtime_error("TMVA::SOFIE ONNX TopK op k = "+ std::to_string(fK) +" value exeeds value of tensor " +fNX+" of size "+fShapeX.size()+" at axis= "+std::to_string(fAttrAxis)+".");
// }
// fShapeX = model.GetTensorShape(fNX); // [ m x n x o x p ... ]
// if(k[0]>=fShapeX.size()){
// throw
// std::runtime_error("TMVA::SOFIE ONNX TopK op k = "+ std::to_string(k[0]) +"value exeeds size of tensor " +fNX+" of size "+fShapeX.size()+" .");
// }
// fShapeY.push_back(2);
// for (auto i : fShapeX)
// fShapeY.push_back(i); // [ 2 x m x n x o x p ... ]
// size_t axis = fAttrAxis < 0 ? fShapeX.size() + fAttrAxis : fAttrAxis;
// fShapeY[axis] = k[0]; // [ 2 x m x n x K x p ... ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with the commented code here

@@ -797,7 +833,7 @@ long RModel::WriteInitializedTensorsToFile(std::string filename) {
void RModel::PrintRequiredInputTensors() {
std::cout << "Model requires following inputs:\n";
for (auto& inputInfo: fInputTensorInfos) {
std::cout << "Parameterised Tensor name: " << inputInfo.first << "\t";
std::cout << "Parametraised Tensor name: " << inputInfo.first << "\t";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::cout << "Parametraised Tensor name: " << inputInfo.first << "\t";
std::cout << "Parameterised Tensor name: " << inputInfo.first << "\t";

Copy link

github-actions bot commented Jun 19, 2024

Test Results

    13 files      13 suites   2d 13h 35m 1s ⏱️
 2 650 tests  2 649 ✅ 0 💤 1 ❌
32 634 runs  32 633 ✅ 0 💤 1 ❌

For more details on these failures, see this check.

Results for commit 449baf1.

♻️ This comment has been updated with latest results.

@dpiparo dpiparo self-requested a review June 20, 2024 13:00
Copy link
Member

@dpiparo dpiparo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. The code seems to do what it's supposed to, i.e. add an operator. However the code seems to need some refactoring, for example avoiding to pass by value potentially large objects, by not having commented code, unclear functions.

@@ -60,6 +60,9 @@ public:
}
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
std::shared_ptr<void> data);
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps strings have to be passed by const ref?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment valid for all signatures featuring string.

public:
ROperator_Constant(){}

ROperator_Constant(const std::string & type, const std::vector<T> & values, const std::vector<size_t> & shape, std::string nameX, std::string nameY):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here vectors are passed properly, but not strings. see above.

fAttrType(type)
{ }

std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ref?

return input;
}

std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the goal of this function to make a copy of the input? The implementation also is strange, right? Why not just making a copy instead of calling it?

}//TMVA


#endif //TMVA_SOFIE_ROPERATOR_Constant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing trailing carriage return.

@@ -56,102 +58,120 @@ public:
// shape of output tensors given input tensors
std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, this syntax is misleading. Why not passing a const ref?


std::string Generate(std::string OpName){
std::string Generate(std::string OpName){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ref?

fNVal(UTILITY::Clean_name(nameVal)),
fNInd(UTILITY::Clean_name(nameInd)){}

std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again const ref.

size_t length = ConvertShapeToLength(i.second.shape());
// in case we are not using weight files or for tensor created from Constant operator
if (!fUseWeightFile || i.second.IsConstantTensor() ) {
//std::cout << "write tensor " << i.first << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes contain commented code, should it be removed?

strs << "float tensor_" << i.first << "[" << length << "] = {";
float const *data = i.second.data<float>();
for (size_t idx = 0; idx < length; idx++) {
strs << std::setprecision(std::numeric_limits<float>::max_digits10) << data[idx];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a general comment: should the fp numbers be written in hex format not to loose any precision?

Implement TopK operator (work from GSOC student Vedant Mehra)
Fix the output type when parsing TopK

Clean up the code in TopK impelmentation and in the generated code.
Fix also the compilation warnings
@lmoneta lmoneta force-pushed the tmva_sofie_add_topk_operator branch from 3d54e72 to 449baf1 Compare June 26, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants