-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathconstant_test.cc
147 lines (126 loc) · 5.65 KB
/
constant_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2023 Ant Group Co., Ltd.
//
// Licensed 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 "engine/operator/constant.h"
#include "gtest/gtest.h"
#include "engine/core/tensor_constructor.h"
#include "engine/operator/test_util.h"
#include "engine/util/spu_io.h"
#include "engine/util/tensor_util.h"
namespace scql::engine::op {
struct ConstantTestCase {
test::NamedTensor scalar;
pb::TensorStatus output_status;
};
class ConstantTest
: public ::testing::TestWithParam<
std::tuple<test::SpuRuntimeTestCase, ConstantTestCase>> {
protected:
static pb::ExecNode MakeConstantExecNode(const ConstantTestCase& tc);
};
INSTANTIATE_TEST_SUITE_P(
ConstantPrivateTest, ConstantTest,
testing::Combine(
test::SpuTestValuesMultiPC,
testing::Values(
ConstantTestCase{.scalar = {test::NamedTensor(
"x", TensorFrom(arrow::int64(), "[3]"))},
.output_status = pb::TENSORSTATUS_PRIVATE},
ConstantTestCase{.scalar = {test::NamedTensor(
"y", TensorFrom(arrow::large_utf8(),
R"json(["2022-11-22"])json"))},
.output_status = pb::TENSORSTATUS_PRIVATE},
ConstantTestCase{.scalar = {test::NamedTensor(
"z", TensorFrom(arrow::boolean(), "[true]"))},
.output_status = pb::TENSORSTATUS_PRIVATE},
ConstantTestCase{
.scalar = {test::NamedTensor("zz", TensorFrom(arrow::float32(),
"[3.1415]"))},
.output_status = pb::TENSORSTATUS_PRIVATE})),
TestParamNameGenerator(ConstantTest));
INSTANTIATE_TEST_SUITE_P(
ConstantPublicTest, ConstantTest,
testing::Combine(
test::SpuTestValuesMultiPC,
testing::Values(
ConstantTestCase{.scalar = {test::NamedTensor(
"x", TensorFrom(arrow::int64(), "[3]"))},
.output_status = pb::TENSORSTATUS_PUBLIC},
ConstantTestCase{.scalar = {test::NamedTensor(
"y", TensorFrom(arrow::large_utf8(),
R"json(["2022-11-22"])json"))},
.output_status = pb::TENSORSTATUS_PUBLIC},
ConstantTestCase{.scalar = {test::NamedTensor(
"z", TensorFrom(arrow::boolean(), "[true]"))},
.output_status = pb::TENSORSTATUS_PUBLIC},
ConstantTestCase{
.scalar = {test::NamedTensor("zz", TensorFrom(arrow::float32(),
"[3.1415]"))},
.output_status = pb::TENSORSTATUS_PUBLIC})),
TestParamNameGenerator(ConstantTest));
TEST_P(ConstantTest, Works) {
// Given
auto parm = GetParam();
auto tc = std::get<1>(parm);
auto node = MakeConstantExecNode(tc);
auto sessions = test::MakeMultiPCSession(std::get<0>(parm));
std::vector<ExecContext> exec_ctxs;
exec_ctxs.reserve(sessions.size());
for (auto& session : sessions) {
exec_ctxs.emplace_back(node, session.get());
}
std::vector<ExecContext*> ctx_ptrs;
ctx_ptrs.reserve(exec_ctxs.size());
for (auto& exec_ctx : exec_ctxs) {
ctx_ptrs.emplace_back(&exec_ctx);
}
// When
EXPECT_NO_THROW(test::RunAsync<Constant>(ctx_ptrs));
// check alice output
auto expect_arr = tc.scalar.tensor->ToArrowChunkedArray();
TensorPtr out;
if (tc.output_status == pb::TENSORSTATUS_PRIVATE) {
out = exec_ctxs[0].GetTensorTable()->GetTensor(tc.scalar.name);
} else {
auto* sctx = exec_ctxs[0].GetSession()->GetSpuContext();
auto* device_symbols = exec_ctxs[0].GetSession()->GetDeviceSymbols();
util::SpuOutfeedHelper outfeed_helper(sctx, device_symbols);
out = outfeed_helper.DumpPublic(tc.scalar.name);
// convert hash to string for string tensor in spu
if (tc.scalar.tensor->Type() == pb::PrimitiveDataType::STRING) {
out = exec_ctxs[0].GetSession()->HashToString(*out);
}
}
ASSERT_TRUE(out);
EXPECT_TRUE(out->ToArrowChunkedArray()->ApproxEquals(*expect_arr))
<< "expect type = " << expect_arr->type()->ToString()
<< ", got type = " << out->ToArrowChunkedArray()->type()->ToString()
<< "\nexpect result = " << expect_arr->ToString()
<< "\nbut actual got result = " << out->ToArrowChunkedArray()->ToString();
}
/// ===========================
/// ConstantTest impl
/// ===========================
pb::ExecNode ConstantTest::MakeConstantExecNode(const ConstantTestCase& tc) {
test::ExecNodeBuilder builder(Constant::kOpType);
builder.SetNodeName("constant-test");
auto pb_tensor = std::make_shared<pb::Tensor>();
pb_tensor->set_elem_type(tc.scalar.tensor->Type());
util::CopyValuesToProto(tc.scalar.tensor, pb_tensor.get());
builder.AddAttr(Constant::kScalarAttr, *pb_tensor);
auto t = test::MakeTensorReference(tc.scalar.name, pb_tensor->elem_type(),
tc.output_status);
builder.AddOutput(Constant::kOut, {t});
return builder.Build();
}
} // namespace scql::engine::op