Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit d789370

Browse files
authored
Cyphers/exec can create tensors (#3445)
* Add method to check if an Executable can create tensors * Add a Backend check to see if Executable can create tensors * Update per review comment * style * Possible fix so that backend does not keep holding on to the dummy ng_exec
1 parent 005c118 commit d789370

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/ngraph/runtime/backend.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,24 @@ bool runtime::Backend::set_config(const map<string, string>& config, string& err
151151
error = "set_config not supported";
152152
return false;
153153
}
154+
155+
bool runtime::Backend::executable_can_create_tensors()
156+
{
157+
auto A = make_shared<op::Parameter>(element::f32, Shape());
158+
auto function = make_shared<Function>(NodeVector{A}, ParameterVector{A});
159+
auto exec = compile(function);
160+
bool exec_can_create_tensors = false;
161+
try
162+
{
163+
auto t0 = exec->create_input_tensor(0);
164+
auto t1 = exec->create_input_tensor(0, 1);
165+
auto t2 = exec->create_output_tensor(0);
166+
auto t3 = exec->create_output_tensor(0, 1);
167+
exec_can_create_tensors = true;
168+
}
169+
catch (...)
170+
{
171+
}
172+
remove_compiled_function(exec);
173+
return exec_can_create_tensors;
174+
}

src/ngraph/runtime/backend.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ class ngraph::runtime::Backend
172172
/// \param ptr pointer to the memory to determine if its in device memory or not
173173
virtual bool is_device_memory(void* ptr);
174174

175+
virtual bool executable_can_create_tensors();
176+
175177
private:
176178
// mutex to modify s_backend_shared_library_search_directory thread safe
177179
static std::mutex m_mtx;

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ if (NGRAPH_CPU_ENABLE)
365365
# The INTERPRETER backend is required for convolution, and backwards unit tests
366366
target_link_libraries(unit-test PRIVATE cpu_backend interpreter_backend)
367367
target_link_libraries(unit-test PRIVATE libmkldnn)
368+
target_compile_definitions(unit-test PRIVATE NGRAPH_CPU_ENABLE)
368369
endif()
369370

370371
if (NGRAPH_TOOLS_ENABLE)

test/backend_api.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,14 @@ TEST(backend_api, save_load)
9191
}
9292
}
9393
#endif
94+
95+
#if defined(NGRAPH_INTERPRETER_ENABLE) && defined(NGRAPH_CPU_ENABLE)
96+
TEST(backend_api, executable_can_create_tensor)
97+
{
98+
auto interpreter = runtime::Backend::create("INTERPRETER");
99+
auto cpu = runtime::Backend::create("CPU");
100+
101+
EXPECT_TRUE(interpreter->executable_can_create_tensors());
102+
EXPECT_FALSE(cpu->executable_can_create_tensors());
103+
}
104+
#endif

0 commit comments

Comments
 (0)