pytorch/test/cpp/api/jit.cpp
Zachary DeVito f3a588fede add len to nativeResolver (#15488)
Summary:
(otherwise len is not resolvable using torch::jit::compile)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15488

Differential Revision: D13539991

Pulled By: zdevito

fbshipit-source-id: 3ba85fa7b1adb163f9229c568f7997d22321903d
2018-12-21 16:47:15 -08:00

36 lines
907 B
C++

#include <gtest/gtest.h>
#include <torch/jit.h>
#include <torch/types.h>
#include <string>
TEST(TorchScriptTest, CanCompileMultipleFunctions) {
auto module = torch::jit::compile(R"JIT(
def test_mul(a, b):
return a * b
def test_relu(a, b):
return torch.relu(a + b)
def test_while(a, i):
while bool(i < 10):
a += a
i += 1
return a
def test_len(a : List[int]):
return len(a)
)JIT");
auto a = torch::ones(1);
auto b = torch::ones(1);
ASSERT_EQ(1, module->run_method("test_mul", a, b).toTensor().item<int64_t>());
ASSERT_EQ(2, module->run_method("test_relu", a, b).toTensor().item<int64_t>());
ASSERT_TRUE(
0x200 == module->run_method("test_while", a, b).toTensor().item<int64_t>());
at::IValue list = std::vector<int64_t>({3, 4});
ASSERT_EQ(2, module->run_method("test_len", list).toInt());
}