Skip to content

Commit 3e341d1

Browse files
authored
[onert] Add model verification for I/O consistency (#16402)
This commit adds verification step to ensure loaded models have matching input/output counts with the pipeline configuration. ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <jonghwa3.lee@samsung.com>
1 parent b2f7ae1 commit 3e341d1

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

runtime/onert/backend/trix/ops/BulkPipelineLayer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void BulkPipelineLayer::configure(const std::vector<const IPortableTensor *> &in
4444
config.model_paths = binary_path;
4545
config.device_id = 0; // default device id = 0
4646
config.n_owner_models = 2; // Use 2 owner models for buffer sharing
47+
config.n_inputs = inputs.size();
48+
config.n_outputs = outputs.size();
4749

4850
_pipeline_manager = std::make_unique<BulkPipelineManager>(config);
4951

runtime/onert/backend/trix/ops/BulkPipelineManager.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ bool BulkPipelineManager::initialize()
4343
try
4444
{
4545
createModels();
46+
verifyModels();
4647
prepareModels();
4748
linkModels();
4849

@@ -207,6 +208,19 @@ void BulkPipelineManager::linkModels()
207208
}
208209
}
209210

211+
void BulkPipelineManager::verifyModels()
212+
{
213+
for (auto &model : _models)
214+
{
215+
if ((static_cast<uint32_t>(model->metadata()->input_seg_num) != _config.n_inputs) ||
216+
(static_cast<uint32_t>(model->metadata()->output_seg_num) != _config.n_outputs))
217+
{
218+
throw std::runtime_error("Model " + model->modelPath() +
219+
" has different number of inputs/outputs");
220+
}
221+
}
222+
}
223+
210224
void BulkPipelineManager::prepareModels()
211225
{
212226
for (auto &model : _models)

runtime/onert/backend/trix/ops/BulkPipelineManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class BulkPipelineManager
3838
std::vector<std::string> model_paths;
3939
int device_id{0};
4040
int n_owner_models{2}; // number of models that share the buffers
41+
uint32_t n_inputs{1};
42+
uint32_t n_outputs{1};
4143
};
4244

4345
public:
@@ -59,6 +61,7 @@ class BulkPipelineManager
5961
void createModels();
6062
void linkModels();
6163
void prepareModels();
64+
void verifyModels();
6265

6366
private:
6467
PipelineConfig _config;

runtime/onert/backend/trix/ops/test/BulkPipelineManager.test.cc

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,23 @@ class BulkPipelineManagerTest : public ::testing::Test
2929
{
3030
BulkPipelineManager::PipelineConfig config;
3131
config.device_id = 0;
32-
config.model_paths.push_back("model_path");
32+
config.model_paths.push_back("model_path_0");
33+
config.model_paths.push_back("model_path_1");
34+
config.n_inputs = 0;
35+
config.n_outputs = 0;
3336
manager = std::make_unique<BulkPipelineManager>(config);
3437

3538
// Reset all mock syscalls before each test
3639
MockSyscallsManager::getInstance().resetAll();
3740

41+
MockSyscallsManager::getInstance().setFopenHook([](const char *path, const char *) -> FILE * {
42+
if (strcmp(path, "model_path_0") == 0)
43+
{
44+
return (FILE *)1;
45+
}
46+
return (FILE *)2;
47+
});
48+
3849
MockSyscallsManager::getInstance().setFreadHook(
3950
[](void *ptr, size_t size, size_t, FILE *) -> int {
4051
if (size == NPUBIN_META_SIZE)
@@ -43,6 +54,8 @@ class BulkPipelineManagerTest : public ::testing::Test
4354
meta->program_size = 1024;
4455
meta->weight_size = 1024;
4556
meta->size = 4096;
57+
meta->input_seg_num = 0;
58+
meta->output_seg_num = 0;
4659
}
4760
return 1;
4861
});
@@ -61,7 +74,7 @@ class BulkPipelineManagerTest : public ::testing::Test
6174
void TearDown() override {}
6275

6376
std::unique_ptr<BulkPipelineManager> manager;
64-
const int nr_models = 1;
77+
const int nr_models = 2;
6578
};
6679

6780
TEST_F(BulkPipelineManagerTest, test_initilize)
@@ -92,3 +105,29 @@ TEST_F(BulkPipelineManagerTest, test_execute)
92105
std::vector<onert::backend::IPortableTensor *> outputs;
93106
EXPECT_NO_THROW(manager->execute(inputs, outputs));
94107
}
108+
109+
TEST_F(BulkPipelineManagerTest, test_verify_models)
110+
{
111+
MockSyscallsManager::getInstance().clearFreadHook();
112+
MockSyscallsManager::getInstance().setFreadHook(
113+
[](void *ptr, size_t size, size_t, FILE *fp) -> int {
114+
if (size == NPUBIN_META_SIZE)
115+
{
116+
auto meta = reinterpret_cast<npubin_meta *>(ptr);
117+
meta->program_size = 1024;
118+
meta->weight_size = 1024;
119+
meta->size = 4096;
120+
meta->input_seg_num = 0;
121+
if (fp == (FILE *)1)
122+
{
123+
meta->output_seg_num = 1;
124+
}
125+
else
126+
{
127+
meta->output_seg_num = 0;
128+
}
129+
}
130+
return 1;
131+
});
132+
EXPECT_FALSE(manager->initialize());
133+
}

0 commit comments

Comments
 (0)