/* * 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace tvm; using namespace tvm::relay; TEST(Relay, OutOfStack_add) { auto foo = [] { auto add_op = relay::Op::Get("add"); auto c_data = tvm::runtime::NDArray::Empty({1, 2, 3}, {kDLFloat, 32, 1}, {kDLCPU, 0}); auto c1 = relay::Constant(c_data); Call y1 = relay::Call(add_op, {c1, c1}); for (int i = 0; i < 1e6; i++) { y1 = relay::Call(add_op, {c1, y1}); } relay::Function func = relay::Function({}, y1, relay::Type(), {}); }; ASSERT_EXIT((foo(), exit(0)), ::testing::ExitedWithCode(0), ".*"); } TEST(Relay, OutOfStack_cast) { auto foo = [] { auto cast_op = relay::Op::Get("cast"); auto c_data = tvm::runtime::NDArray::Empty({1, 2, 3}, {kDLFloat, 32, 1}, {kDLCPU, 0}); auto c1 = relay::Constant(c_data); Call y1 = relay::Call(cast_op, {c1}); for (int i = 0; i < 1e6; i++) { y1 = relay::Call(cast_op, {y1}); } relay::Function func = relay::Function({}, y1, relay::Type(), {}); }; ASSERT_EXIT((foo(), exit(0)), ::testing::ExitedWithCode(0), ".*"); } TEST(Relay, OutOfStack_packed_func) { constexpr int len = 1e6; auto foo = [] { auto x = relay::Var("x", relay::TensorType({3, 2}, DataType::Float(32))); auto one = relay::Constant(tvm::runtime::NDArray::Empty({1}, {kDLFloat, 32, 1}, {kDLCPU, 0})); auto add_func = tvm::runtime::Registry::Get("relay.op._make.add"); auto y = (*add_func)(x, one); for (int i = 0; i < len; ++i) { y = (*add_func)(y, one); } // check if still reachable int k = 0; Expr e = y; while (e.defined() && e.as() != nullptr) { e = e.as()->args[0]; ++k; } ASSERT_EQ(len + 1, k); }; ASSERT_EXIT((foo(), exit(0)), ::testing::ExitedWithCode(0), ".*"); } TEST(Relay, CallNodeSharedArgs) { auto x = relay::Var("x", relay::TensorType({3, 2}, DataType::Float(32))); auto one = relay::Constant(tvm::runtime::NDArray::Empty({1}, {kDLFloat, 32, 1}, {kDLCPU, 0})); auto relu_op = relay::Op::Get("nn.relu"); Call y = relay::Call(relu_op, {x}, Attrs(), {}); y = relay::Call(relu_op, {y}, Attrs(), {}); ASSERT_EQ(1, y.get()->args[0].as()->args.size()); y = relay::Call(y.get()->op, y.get()->args, y.get()->attrs, y.get()->type_args); ASSERT_EQ(1, y.get()->args[0].as()->args.size()); } TEST(Relay, TupleSharedFields) { auto x = relay::Var("x", relay::TensorType({3, 2}, DataType::Float(32))); auto one = relay::Constant(tvm::runtime::NDArray::Empty({1}, {kDLFloat, 32, 1}, {kDLCPU, 0})); auto relu_op = relay::Op::Get("nn.relu"); Expr y = relay::Call(relu_op, {x}, Attrs(), {}); y = relay::Call(relu_op, {y}, Attrs(), {}); { Expr y1 = relay::Tuple(y.as()->args); Expr y2 = relay::Tuple(y.as()->args); y1 = relay::Call(relu_op, {y1}); y2 = relay::Call(relu_op, {y2}); y = y1; } ASSERT_EQ(1, y.as()->args[0].as()->fields[0].as()->args.size()); } TEST(Relay, TupleiGetItemSharedTuple) { auto x = relay::Var("x", relay::TensorType({3, 2}, DataType::Float(32))); auto one = relay::Constant(tvm::runtime::NDArray::Empty({1}, {kDLFloat, 32, 1}, {kDLCPU, 0})); auto relu_op = relay::Op::Get("nn.relu"); Expr y = relay::Call(relu_op, {x}, Attrs(), {}); y = relay::Tuple({y}); { Expr y1 = relay::TupleGetItem(y, 0); Expr y2 = relay::TupleGetItem(y, 0); y1 = relay::Call(relu_op, {y1}); y2 = relay::Call(relu_op, {y2}); y = y1; } ASSERT_EQ(1, y.as() ->args[0] .as() ->tuple.as() ->fields[0] .as() ->args.size()); } TEST(Relay, OutOfStackLet) { auto foo = [] { auto add_op = relay::Op::Get("add"); auto p = relay::Var("p", relay::TensorType({3, 2}, DataType::Float(32))); int size = 1e6 - 1; std::vector vars; for (int i = 0; i < size; ++i) { vars.emplace_back("x_" + std::to_string(i), relay::TensorType({3, 2}, DataType::Float(32))); } Expr body = vars[size - 1]; for (int i = size - 1; i >= 0; --i) { Var v = i == 0 ? p : vars[i - 1]; body = relay::Let(vars[i], relay::Call(add_op, {v, v}), body); } relay::Function func = relay::Function({p}, body, relay::Type(), {}); }; ASSERT_EXIT((foo(), exit(0)), ::testing::ExitedWithCode(0), ".*"); }