pytorch/test/cpp/jit/test_schema_matching.cpp
Elias Ellison a8073f34af fix schema matching of tuples to vartype lists (#25944)
Summary:
In schema matching we allow a homogenous tuple to be matched to list arguments. This logic wasn't yet extended for vartype lists, causing stuff like `len((1, 2, 3))` to fail.

Fix for https://github.com/pytorch/pytorch/issues/20500
Pull Request resolved: https://github.com/pytorch/pytorch/pull/25944

Differential Revision: D17431514

Pulled By: eellison

fbshipit-source-id: 2ad98bab15eaa496471df651572735eb35183323
2019-09-17 13:47:46 -07:00

80 lines
2.0 KiB
C++

#include <ATen/test/test_assert.h>
#include <torch/csrc/jit/ir.h>
#include <torch/csrc/jit/testing/file_check.h>
#include <torch/jit.h>
#include "test/cpp/jit/test_base.h"
#include "torch/csrc/jit/custom_operator.h"
#include <sstream>
#include <string>
namespace torch {
namespace jit {
void testSchemaMatching() {
{
RegisterOperators reg({
Operator(
"aten::test_vartype(t[] a, t b) -> (t)",
[](const Node* node) {
return [](Stack& stack) {
c10::List<double> list;
double a;
pop(stack, list, a);
push(stack, a);
return 0;
};
}),
});
script::Module m("m");
m.define(R"(
def test(self):
a = (1.0, 2.0)
return torch.test_vartype(a, 2.0)
)");
auto result = m.run_method("test");
TORCH_INTERNAL_ASSERT(result.toDouble() == 2.0);
const std::string error_example = R"JIT(
def test_2(self):
a = (1.0, 2.0)
non_float = (1, 1)
return torch.test_vartype(a, non_float)
)JIT";
ASSERT_THROWSM(m.define(error_example), "previously matched to type");
}
{
RegisterOperators reg({
Operator(
"aten::test_vartype2(t a, t[] b) -> (t[])",
[](const Node* node) {
return [](Stack& stack) {
double a;
c10::List<double> list;
pop(stack, a, list);
push(stack, a);
return 0;
};
}),
});
script::Module m("m");
m.define(R"JIT(
def test(self):
a = (1.0, 2.0)
return torch.test_vartype2(3.0, a)
)JIT");
auto result = m.run_method("test");
TORCH_INTERNAL_ASSERT(result.toDouble() == 3.0);
static const auto error_exam2 = R"JIT(
def test_2(self):
a = (1, 2)
return torch.test_vartype2(3.0, a)
)JIT";
ASSERT_THROWSM(m.define(error_exam2), "previously matched to type");
}
}
} // namespace jit
} // namespace torch