forked from secretflow/scql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor_batch_reader_test.cc
132 lines (121 loc) · 4.5 KB
/
tensor_batch_reader_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
// Copyright 2024 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/core/tensor_batch_reader.h"
#include <filesystem>
#include "arrow/array.h"
#include "arrow/testing/random.h"
#include "arrow/type.h"
#include "gtest/gtest.h"
#include "engine/core/tensor_constructor.h"
#include "engine/util/filepath_helper.h"
namespace scql::engine {
class TensorBatchReaderTest : public ::testing::Test {
public:
TensorBatchReaderTest()
: tmp_dir_(util::ScopedDir(util::CreateDirWithRandSuffix(
std::filesystem::temp_directory_path(), "test"))) {}
protected:
util::ScopedDir tmp_dir_;
};
TEST_F(TensorBatchReaderTest, ReadDisk) {
constexpr arrow::random::SeedType randomSeed = 0x0ff1ce;
arrow::ArrayVector arrays;
constexpr size_t array_num = 3;
constexpr size_t array_num_rows = 1000;
auto field = std::make_shared<arrow::Field>("a", arrow::int64());
for (size_t i = 0; i < array_num; ++i) {
arrays.push_back(
arrow::random::GenerateArray(*field, array_num_rows, randomSeed));
}
arrow::FieldVector fields = {field};
auto schema = std::make_shared<arrow::Schema>(fields);
auto chunked_array = std::make_shared<arrow::ChunkedArray>(arrays);
TensorPtr ptr;
{
TensorWriter writer(schema, tmp_dir_.path().string());
auto write_num = writer.WriteBatch(*chunked_array);
ASSERT_EQ(array_num * array_num_rows, write_num);
writer.Finish(&ptr);
}
size_t batch_size = 400;
auto reader = ptr->CreateBatchReader(batch_size);
size_t offset = 0;
while (true) {
auto array = reader->Next();
if (!array) break;
offset += array->length();
ASSERT_TRUE(batch_size == static_cast<size_t>(array->length()) ||
offset == array_num * array_num_rows);
}
ASSERT_TRUE(offset == array_num * array_num_rows);
}
TEST_F(TensorBatchReaderTest, ReadDiskOnlyOneBatch) {
constexpr arrow::random::SeedType randomSeed = 0x0ff1ce;
arrow::ArrayVector arrays;
constexpr size_t array_num = 1;
constexpr size_t array_num_rows = 1000;
auto field = std::make_shared<arrow::Field>("a", arrow::int64());
for (size_t i = 0; i < array_num; ++i) {
arrays.push_back(
arrow::random::GenerateArray(*field, array_num_rows, randomSeed));
}
arrow::FieldVector fields = {field};
auto schema = std::make_shared<arrow::Schema>(fields);
auto chunked_array = std::make_shared<arrow::ChunkedArray>(arrays);
TensorPtr ptr;
{
TensorWriter writer(schema, tmp_dir_.path().string());
auto write_num = writer.WriteBatch(*chunked_array);
ASSERT_EQ(array_num * array_num_rows, write_num);
writer.Finish(&ptr);
}
size_t batch_size = 1000;
auto reader = ptr->CreateBatchReader(batch_size);
size_t offset = 0;
while (true) {
auto array = reader->Next();
if (!array) break;
offset += array->length();
ASSERT_TRUE(batch_size == static_cast<size_t>(array->length()) ||
offset == array_num * array_num_rows);
}
ASSERT_TRUE(offset == array_num * array_num_rows);
}
TEST_F(TensorBatchReaderTest, ReadMemory) {
constexpr arrow::random::SeedType randomSeed = 0x0ff1ce;
arrow::ArrayVector arrays;
constexpr size_t array_num = 3;
constexpr size_t array_num_rows = 1000;
auto field = std::make_shared<arrow::Field>("a", arrow::int64());
for (size_t i = 0; i < array_num; ++i) {
arrays.push_back(
arrow::random::GenerateArray(*field, array_num_rows, randomSeed));
}
arrow::FieldVector fields = {field};
auto schema = std::make_shared<arrow::Schema>(fields);
auto chunked_array = std::make_shared<arrow::ChunkedArray>(arrays);
TensorPtr ptr = std::make_shared<MemTensor>(chunked_array);
size_t batch_size = 400;
auto reader = ptr->CreateBatchReader(batch_size);
size_t offset = 0;
while (true) {
auto array = reader->Next();
if (!array) break;
offset += array->length();
ASSERT_TRUE(batch_size == static_cast<size_t>(array->length()) ||
offset == array_num * array_num_rows);
}
ASSERT_TRUE(offset == array_num * array_num_rows);
}
} // namespace scql::engine