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

Fix einsum operator for empty inputs #23379

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <algorithm>

#include "einsum_typed_compute_processor.h"
#include "core/common/narrow.h"
#include "core/common/span_utils.h"
Expand Down Expand Up @@ -357,6 +359,25 @@ Status EinsumTypedComputeProcessor<T>::Run() {

auto num_inputs = context_->InputCount();

{
bool has_empty_input = std::any_of(raw_inputs.begin(), raw_inputs.end(), [](const auto& input) {
return input->Shape().Size() == 0;
});
// Skip all the work, fill with zeros if needed
if (has_empty_input) {
const auto output_dims = einsum_compute_preprocessor_.GetOutputDims();
Tensor& output = *context_->Output(0, output_dims);

if constexpr (std::is_integral<T>::value) {
std::fill_n(reinterpret_cast<T*>(output.MutableDataRaw()), output.Shape().Size(), T(0));
Copy link
Member

Choose a reason for hiding this comment

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

This logic needs a device abstraction. The output data could on CUDA for example and we need to populate it appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the quick review!

Can you look over the changes again and tell me whether they make sense to you, and if so run the test suite again?

} else {
std::fill_n(reinterpret_cast<T*>(output.MutableDataRaw()), output.Shape().Size(), T(0.f));
}

return Status::OK();
}
}

// Pre-process the first input so as to reduce any dims that only it has
std::unique_ptr<const Tensor> result;

Expand Down
46 changes: 46 additions & 0 deletions onnxruntime/test/providers/cpu/math/einsum_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,52 @@ TEST(Einsum, ExplicitEinsumAsTensorContraction_Half) {
test.Run();
}

// Theme: Empty inputs
TEST(Einsum, EinsumEmptyInputOuterProduct) {
OpTester test("Einsum", 12, onnxruntime::kOnnxDomain);
test.AddAttribute<std::string>("equation", "i,j->ij");
test.AddInput<float>("x", {0}, {});
test.AddInput<float>("y", {0}, {});
test.AddOutput<float>("o", {0, 0}, {});
test.Run();
}

TEST(Einsum, EinsumEmptyInputTranspose) {
OpTester test("Einsum", 12, onnxruntime::kOnnxDomain);
test.AddAttribute<std::string>("equation", "ab,ba->ab");
test.AddInput<float>("x", {0, 1}, {});
test.AddInput<float>("y", {1, 0}, {});
test.AddOutput<float>("o", {0, 1}, {});
test.Run();
}

TEST(Einsum, EinsumEmptyInputVanish) {
OpTester test("Einsum", 12, onnxruntime::kOnnxDomain);
test.AddAttribute<std::string>("equation", "ab,ba->a");
test.AddInput<float>("x", {1, 0}, {});
test.AddInput<float>("y", {0, 1}, {});
test.AddOutput<float>("o", {1}, {0.f});
test.Run();
}

TEST(Einsum, EinsumEmptyInputVanish3d) {
OpTester test("Einsum", 12, onnxruntime::kOnnxDomain);
test.AddAttribute<std::string>("equation", "abc,bad->ad");
test.AddInput<float>("x", {10, 0, 10}, {});
test.AddInput<float>("y", {0, 10, 1}, {});
test.AddOutput<float>("o", {10, 1}, std::vector<float>(10, 0.f));
test.Run();
}

TEST(Einsum, EinsumEmptyInputVanish3d2empty) {
OpTester test("Einsum", 12, onnxruntime::kOnnxDomain);
test.AddAttribute<std::string>("equation", "abc,bcd->ad");
test.AddInput<float>("x", {0, 0, 0}, {});
test.AddInput<float>("y", {0, 0, 1}, {});
test.AddOutput<float>("o", {0, 1}, {});
test.Run();
}

// Theme: Tests involving MatMul(s) interleaved with Transpose(s)
// for two and three inputs (most common use-case of Einsum operator)

Expand Down
Loading