-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[ONNX] Add Einsum converter #8985
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a62bae
einsum
anwang2009 1dae1c3
address review
anwang2009 1d03f10
move files around
anwang2009 653a9aa
use generic topi op
anwang2009 64a4f4a
TODO comment
anwang2009 3481e56
jostle ci
anwang2009 69974b6
jostle ci
anwang2009 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 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 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 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 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,22 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Backend compiler related feature registration""" | ||
from . import op as _reg | ||
from . import strategy | ||
|
||
# einsum | ||
_reg.register_strategy("einsum", strategy.einsum_strategy) |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -39,3 +39,4 @@ | |
from .sort import * | ||
from .search import * | ||
from .image import * | ||
from .math import * |
This file contains 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,34 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Generic math operators""" | ||
from .default import default_schedule as _default_schedule | ||
|
||
|
||
def schedule_einsum(outs): | ||
"""Schedule for einsum operator. | ||
|
||
Parameters | ||
---------- | ||
outs: Array of Tensor | ||
The computation graph description of einsum. | ||
|
||
Returns | ||
------- | ||
s: Schedule | ||
The computation schedule for the op. | ||
""" | ||
return _default_schedule(outs, False) |
This file contains 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,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file math.cc | ||
* \brief Math operators. | ||
*/ | ||
#include <tvm/relay/expr.h> | ||
#include <tvm/relay/op.h> | ||
#include <tvm/topi/einsum.h> | ||
|
||
#include "../make_op.h" | ||
#include "../op_common.h" | ||
#include "../type_relations.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
// relay.einsum | ||
TVM_REGISTER_NODE_TYPE(EinsumAttrs); | ||
|
||
bool EinsumRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
// Check attrs | ||
const EinsumAttrs* param = attrs.as<EinsumAttrs>(); | ||
if (param == nullptr) { | ||
reporter->GetDiagCtx().EmitFatal(Diagnostic::Error(reporter->GetSpan()) | ||
<< "the call attributes are not defined"); | ||
return false; | ||
} | ||
|
||
// types: [data, result] | ||
ICHECK_EQ(types.size(), 2) << "the arity of einsum is 2, not " << types.size(); | ||
|
||
// Check input type is a tuple. | ||
const auto* tensor_tuple = types[0].as<TupleTypeNode>(); | ||
if (tensor_tuple == nullptr) { | ||
reporter->GetDiagCtx().EmitFatal( | ||
Diagnostic::Error(reporter->GetSpan()) | ||
<< "einsum requires a tuple of tensors as the first argument, found " | ||
<< PrettyPrint(types[0])); | ||
return false; | ||
} | ||
|
||
// Check the input tuple consists of tensors with consistent dtype. | ||
const auto& first = Downcast<TensorType>(tensor_tuple->fields[0]); | ||
const DataType dtype = first->dtype; | ||
std::vector<Array<PrimExpr>> input_shapes; | ||
for (const Type& ele : tensor_tuple->fields) { | ||
if (ele.as<IncompleteTypeNode>()) { | ||
return false; | ||
} | ||
|
||
const auto& e = Downcast<TensorType>(ele); | ||
|
||
const DataType& e_dtype = e->dtype; | ||
if (e_dtype != dtype) { | ||
throw Error("relay.einsum requires all tensors have the same dtype"); | ||
} | ||
input_shapes.push_back(e->shape); | ||
} | ||
|
||
// Calculate output shape | ||
Array<IndexExpr> oshape = topi::NumpyEinsumShape(param->equation, input_shapes); | ||
|
||
auto rtype = TensorType(oshape, dtype); | ||
reporter->Assign(types[1], rtype); | ||
return true; | ||
} | ||
|
||
Array<te::Tensor> EinsumCompute(const Attrs& attrs, const Array<te::Tensor>& inputs, | ||
const Type& out_type) { | ||
const EinsumAttrs* param = attrs.as<EinsumAttrs>(); | ||
ICHECK(param != nullptr); | ||
return Array<te::Tensor>{topi::einsum(param->equation, inputs)}; | ||
} | ||
|
||
Expr MakeEinsum(Expr data, String equation) { | ||
auto attrs = make_object<EinsumAttrs>(); | ||
attrs->equation = std::move(equation); | ||
static const Op& op = Op::Get("einsum"); | ||
return Call(op, {data}, Attrs(attrs), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.op._make.einsum").set_body_typed(MakeEinsum); | ||
|
||
RELAY_REGISTER_OP("einsum") | ||
.describe(R"doc(Evaluates the Einstein summation convention | ||
on the operands)doc" TVM_ADD_FILELINE) | ||
.set_attrs_type<EinsumAttrs>() | ||
.set_num_inputs(1) | ||
.add_argument("data", "Tuple of Tensors", "The input list of tensors.") | ||
.set_support_level(11) | ||
.add_type_rel("Einsum", EinsumRel) | ||
.set_attr<FTVMCompute>("FTVMCompute", EinsumCompute) | ||
.set_attr<TOpPattern>("TOpPattern", kInjective); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
This file contains 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
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.
This would lead to an error if you try to run it on cuda right? It is better to remove this strategy until we have a CUDA schedule ready. The error message would be clearer than the one from incorrect scheduling.
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.
The
test_onnx_nodes
tests run on CUDA and have passed. I will remove if you think it's necessary to remove despite that?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.
Oh interesting. I didn't know that
topi.generic.schedule_extern
somehow generates a valid schedule.