Unverified Commit f430eac7 authored by Robert Kimball's avatar Robert Kimball Committed by GitHub

Move runtime::Manager functionality into runtime::Backend (#875)

* Move runtime::Manager functionality into runtime::Backend

* Remove unused files

* remove obsolete function
parent ec26acf2
......@@ -30,7 +30,6 @@ void regclass_pyngraph_runtime_Backend(py::module m)
backend.doc() = "ngraph.impl.runtime.Backend wraps ngraph::runtime::Backend";
backend.def_static("create", &ngraph::runtime::Backend::create);
backend.def_static("get_registered_devices", &ngraph::runtime::Backend::get_registered_devices);
backend.def_static("get_subdevices", &ngraph::runtime::Backend::get_subdevices);
backend.def("create_tensor",
(std::shared_ptr<ngraph::runtime::TensorView>(ngraph::runtime::Backend::*)(
const ngraph::element::Type&, const ngraph::Shape&)) &
......
......@@ -126,9 +126,6 @@ set (SRC
runtime/interpreter/int_backend.cpp
runtime/interpreter/int_call_frame.cpp
runtime/interpreter/int_external_function.cpp
runtime/interpreter/int_kernels.cpp
runtime/interpreter/int_manager.cpp
runtime/manager.cpp
runtime/tensor_view.cpp
serializer.cpp
shape.cpp
......@@ -194,7 +191,6 @@ if (NGRAPH_CPU_ENABLE AND LLVM_INCLUDE_DIR AND
codegen/execution_engine.cpp
runtime/cpu/cpu_call_frame.cpp
runtime/cpu/cpu_backend.cpp
runtime/cpu/cpu_manager.cpp
runtime/cpu/cpu_kernels.cpp
runtime/cpu/cpu_kernel_emitters.cpp
runtime/cpu/cpu_kernel_utils.cpp
......@@ -269,7 +265,6 @@ endif()
runtime/gpu/gpu_emitter.cpp
runtime/gpu/gpu_external_function.cpp
runtime/gpu/gpu_kernel_emitters.cpp
runtime/gpu/gpu_manager.cpp
runtime/gpu/gpu_tensor_view.cpp
runtime/gpu/gpu_tensor_view_wrapper.cpp
runtime/gpu/gpu_util.cpp
......
......@@ -18,38 +18,47 @@
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/util.hpp"
using namespace std;
using namespace ngraph;
bool runtime::Backend::register_backend(const string& name, shared_ptr<Backend> backend)
{
get_backend_map().insert({name, backend});
return true;
}
unordered_map<string, shared_ptr<runtime::Backend>>& runtime::Backend::get_backend_map()
{
static unordered_map<string, shared_ptr<Backend>> backend_map;
return backend_map;
}
runtime::Backend::~Backend()
{
}
shared_ptr<runtime::Backend> runtime::Backend::create(const string& type)
{
shared_ptr<Manager> manager = runtime::Manager::get(type);
return manager->allocate_backend();
auto it = get_backend_map().find(type);
if (it == get_backend_map().end())
{
throw runtime_error("Backend '" + type + "' not found in registered backends.");
}
return it->second;
}
vector<string> runtime::Backend::get_registered_devices()
{
vector<string> rc;
for (const pair<string, runtime::Manager::Factory>& p : runtime::Manager::get_factory_map())
for (const auto& p : get_backend_map())
{
rc.push_back(p.first);
}
return rc;
}
vector<size_t> runtime::Backend::get_subdevices(const string& type)
{
shared_ptr<Manager> manager = runtime::Manager::get(type);
return manager->get_subdevices();
}
void runtime::Backend::remove_compiled_function(shared_ptr<Function> func)
{
}
......
......@@ -48,11 +48,6 @@ namespace ngraph
/// @returns A vector of all registered devices.
static std::vector<std::string> get_registered_devices();
/// @brief Query the list of available subdevices of a particular device.
/// @param type The name of a registered backend, such as "CPU" or "GPU"
/// @returns A vector of available devices of the specified type.
static std::vector<size_t> get_subdevices(const std::string& type);
virtual std::shared_ptr<ngraph::runtime::TensorView>
create_tensor(const ngraph::element::Type& element_type, const Shape& shape) = 0;
......@@ -80,10 +75,15 @@ namespace ngraph
virtual std::vector<PerformanceCounter>
get_performance_data(std::shared_ptr<Function> func) const;
static bool register_backend(const std::string& name, std::shared_ptr<Backend>);
protected:
void validate_call(std::shared_ptr<const Function> func,
const std::vector<std::shared_ptr<runtime::TensorView>>& outputs,
const std::vector<std::shared_ptr<runtime::TensorView>>& inputs);
private:
static std::unordered_map<std::string, std::shared_ptr<Backend>>& get_backend_map();
};
}
}
......@@ -24,6 +24,14 @@
using namespace ngraph;
using namespace std;
static bool static_init()
{
runtime::Backend::register_backend("CPU", make_shared<runtime::cpu::CPU_Backend>());
return true;
};
bool runtime::cpu::CPU_Backend::init = static_init();
shared_ptr<runtime::cpu::CPU_CallFrame> runtime::cpu::CPU_Backend::make_call_frame(
const shared_ptr<runtime::cpu::CPU_ExternalFunction>& external_function)
{
......
......@@ -66,6 +66,7 @@ namespace ngraph
};
std::map<std::shared_ptr<Function>, FunctionInstance> m_function_map;
static bool init;
};
}
}
......
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 <memory>
#include "ngraph/runtime/cpu/cpu_backend.hpp"
#include "ngraph/runtime/cpu/cpu_external_function.hpp"
#include "ngraph/runtime/cpu/cpu_manager.hpp"
using namespace std;
using namespace ngraph;
std::shared_ptr<ngraph::runtime::Backend> runtime::cpu::CPU_Manager::allocate_backend()
{
return std::make_shared<CPU_Backend>();
}
vector<size_t> runtime::cpu::CPU_Manager::get_subdevices() const
{
throw runtime_error("unimplemented");
}
ngraph::runtime::Manager::Factory runtime::cpu::CPU_Manager::factory =
ngraph::runtime::Manager::register_factory(
"CPU", [](const std::string& name) -> std::shared_ptr<ngraph::runtime::Manager> {
return std::make_shared<CPU_Manager>();
});
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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.
*******************************************************************************/
#pragma once
#include <memory>
#include "ngraph/codegen/execution_engine.hpp"
#include "ngraph/runtime/manager.hpp"
namespace ngraph
{
class Function;
namespace runtime
{
class ExternalFunction;
namespace cpu
{
/// @brief Transformer for the interpreted backend
class CPU_Manager : public Manager
{
protected:
ngraph::codegen::ExecutionEngine exec_state;
public:
virtual std::shared_ptr<Backend> allocate_backend() override;
virtual std::vector<size_t> get_subdevices() const override;
static Factory factory;
};
};
}
}
......@@ -23,6 +23,14 @@
using namespace ngraph;
using namespace std;
static bool static_init()
{
runtime::Backend::register_backend("GPU", make_shared<runtime::gpu::GPU_Backend>());
return true;
};
bool runtime::gpu::GPU_Backend::init = static_init();
shared_ptr<runtime::gpu::GPU_CallFrame> runtime::gpu::GPU_Backend::make_call_frame(
const shared_ptr<GPU_ExternalFunction>& external_function)
{
......
......@@ -63,6 +63,7 @@ namespace ngraph
};
std::map<std::shared_ptr<Function>, FunctionInstance> m_function_map;
static bool init;
};
}
}
......
......@@ -63,6 +63,7 @@ namespace ngraph
EntryPoint m_compiled_function;
cublasHandle_t m_cublas_handle;
cudnnHandle_t m_cudnn_handle;
static bool init;
};
}
}
......
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 "ngraph/runtime/gpu/gpu_manager.hpp"
#include "ngraph/runtime/gpu/gpu_backend.hpp"
#include "ngraph/runtime/gpu/gpu_external_function.hpp"
using namespace ngraph;
std::shared_ptr<ngraph::runtime::Backend> runtime::gpu::GPU_Manager::allocate_backend()
{
return std::make_shared<GPU_Backend>();
}
std::vector<size_t> runtime::gpu::GPU_Manager::get_subdevices() const
{
throw std::runtime_error("Unimplemented method");
}
ngraph::runtime::Manager::Factory runtime::gpu::GPU_Manager::factory =
ngraph::runtime::Manager::register_factory(
"GPU", [](const std::string& name) -> std::shared_ptr<ngraph::runtime::Manager> {
return std::make_shared<GPU_Manager>();
});
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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.
*******************************************************************************/
#pragma once
#include <memory>
#include "ngraph/runtime/manager.hpp"
namespace ngraph
{
namespace runtime
{
namespace gpu
{
class GPU_Manager : public Manager
{
public:
virtual std::shared_ptr<Backend> allocate_backend() override;
virtual std::vector<size_t> get_subdevices() const override;
static Factory factory;
};
};
}
}
......@@ -22,6 +22,15 @@
using namespace ngraph;
using namespace std;
static bool static_init()
{
runtime::Backend::register_backend("INTERPRETER",
make_shared<runtime::interpreter::INT_Backend>());
return true;
};
bool runtime::interpreter::INT_Backend::init = static_init();
shared_ptr<runtime::interpreter::INT_CallFrame> runtime::interpreter::INT_Backend::make_call_frame(
const shared_ptr<runtime::interpreter::ExternalFunction>& external_function)
{
......
......@@ -69,6 +69,7 @@ namespace ngraph
};
std::map<std::shared_ptr<Function>, FunctionInstance> m_function_map;
static bool init;
};
}
}
......
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 "ngraph/runtime/interpreter/int_kernels.hpp"
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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.
*******************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
// CBLAS types and wrappers
namespace cblas
{
enum class Layout
{
RowMajor = 101,
ColMajor = 102
};
enum class Transpose
{
None = 111,
Transpose = 112,
ConjTrans = 113
};
enum class UpperLower
{
Upper = 121,
Lower = 122
};
enum class Diag
{
NonUnit = 131,
Unit = 132
};
enum class Side
{
Left = 141,
Right = 142
};
enum class Storage
{
Packed = 151
};
enum class Ident
{
AMatrix = 161,
BMatrix = 162
};
enum class Offset
{
RowOffset = 171,
ColOffset = 172,
FixOffset = 173
};
extern "C" {
void cblas_sgemm(const Layout layout,
const Transpose TransA,
const Transpose TransB,
const int64_t M,
const int64_t N,
const int64_t K,
const float alpha,
const float* A,
const int64_t lda,
const float* B,
const int64_t ldb,
const float beta,
float* C,
const int64_t ldc);
}
}
namespace mkl
{
extern "C" {
void MKL_Somatcopy(char ordering,
char trans,
size_t rows,
size_t cols,
const float alpha,
const float* A,
size_t lda,
float* B,
size_t ldb);
}
}
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 <memory>
#include "ngraph/runtime/interpreter/int_backend.hpp"
#include "ngraph/runtime/interpreter/int_external_function.hpp"
#include "ngraph/runtime/interpreter/int_manager.hpp"
using namespace ngraph;
using namespace std;
shared_ptr<runtime::Backend> runtime::interpreter::INT_Manager::allocate_backend()
{
return make_shared<INT_Backend>();
}
std::vector<size_t> runtime::interpreter::INT_Manager::get_subdevices() const
{
vector<size_t> rc;
return rc;
}
runtime::Manager::Factory runtime::interpreter::INT_Manager::factory =
runtime::Manager::register_factory("INTERPRETER",
[](const string& name) -> shared_ptr<runtime::Manager> {
return make_shared<INT_Manager>();
});
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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.
*******************************************************************************/
#pragma once
#include <memory>
#include <vector>
#include "ngraph/runtime/manager.hpp"
namespace ngraph
{
class Function;
namespace runtime
{
class ExternalFunction;
namespace interpreter
{
/// @brief Transformer for the interpreted backend
class INT_Manager : public Manager
{
public:
virtual std::shared_ptr<Backend> allocate_backend() override;
virtual std::vector<size_t> get_subdevices() const override;
static Factory factory;
};
};
}
}
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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 <dlfcn.h>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "ngraph/except.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/util.hpp"
using namespace ngraph;
using namespace std;
// Put all runtime plugin here. Plugins are statically linked, and a known function need to be
// called to register the backend.
#ifdef NGRAPH_NNP_ENABLE
bool REGISTER_NNP_RUNTIME();
static bool nnp_runtime_initialized = REGISTER_NNP_RUNTIME();
#endif
runtime::Manager::FactoryMap& runtime::Manager::get_factory_map()
{
// Stores Manager Factories
static FactoryMap factory_map;
return factory_map;
}
shared_ptr<runtime::Manager> runtime::Manager::get(const string& name)
{
auto iter = get_factory_map().find(name);
if (iter == get_factory_map().end())
{
throw ngraph_error("No nGraph runtime with name '" + name + "' has been registered.");
}
Factory& f = iter->second;
return f(name);
}
runtime::Manager::Factory runtime::Manager::register_factory(const string& name, Factory factory)
{
get_factory_map()[name] = factory;
return factory;
}
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* 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.
*******************************************************************************/
#pragma once
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace ngraph
{
class Function;
namespace runtime
{
class Backend;
class ExternalFunction;
/// @brief Interface to a generic manager.
///
/// A manager provides access to compilation for a backend, and a means to obtain
/// a backed for execution and allocation.
class Manager
{
friend class runtime::Backend;
public:
virtual ~Manager() {}
/// @brief Allocate a backend for this transformer.
///
/// Specific transformers may provide addtional methods for allocating customized backends.
virtual std::shared_ptr<Backend> allocate_backend() = 0;
/// @brief Query the list of available subdevices of this device.
/// @returns A vector of available devices of the specified type.
virtual std::vector<size_t> get_subdevices() const = 0;
using Factory = std::function<std::shared_ptr<Manager>(const std::string&)>;
static std::shared_ptr<Manager> get(const std::string& name);
static Factory register_factory(const std::string& name, Factory factory);
private:
using FactoryMap = std::map<std::string, Factory>;
static FactoryMap& get_factory_map();
};
}
}
......@@ -26,7 +26,6 @@
#include <ngraph/pass/manager.hpp>
#include <ngraph/pass/visualize_tree.hpp>
#include <ngraph/runtime/backend.hpp>
#include <ngraph/runtime/manager.hpp>
#include <ngraph/util.hpp>
#include "util/benchmark.hpp"
......
......@@ -45,7 +45,6 @@ set (SRC
pass_liveness.cpp
pass_manager.cpp
pass_memory_layout.cpp
runtime_manager.cpp
serialize.cpp
pattern.cpp
shape.cpp
......
......@@ -29,3 +29,8 @@ TEST(backend_api, registered_devices)
EXPECT_TRUE(contains(devices, "INTERPRETER"));
}
TEST(backend_api, invalid_name)
{
ASSERT_ANY_THROW(ngraph::runtime::Backend::create("COMPLETELY-BOGUS-NAME"));
}
......@@ -27,7 +27,6 @@
#include "ngraph/op/concat.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/cpu/cpu_call_frame.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
#include "util/benchmark.hpp"
......
......@@ -191,7 +191,6 @@ TEST(DISABLED_include, complete)
"ngraph/runtime/interpreter/int_backend.hpp",
"ngraph/runtime/interpreter/int_call_frame.hpp",
"ngraph/runtime/interpreter/int_external_function.hpp",
"ngraph/runtime/interpreter/int_kernels.hpp",
"ngraph/runtime/interpreter/int_manager.hpp",
"ngraph/runtime/reference/abs.hpp",
"ngraph/runtime/reference/acos.hpp",
......
/*******************************************************************************
* Copyright 2018 Intel Corporation
*
* 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 "gtest/gtest.h"
#include "ngraph/ngraph.hpp"
using namespace std;
TEST(runtime_manager, invalidName)
{
ASSERT_THROW(ngraph::runtime::Backend::create("COMPLETELY-BOGUS-MANAGER-NAME"),
ngraph::ngraph_error);
}
......@@ -15,7 +15,6 @@
*******************************************************************************/
#include "ngraph/log.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/type/element_type.hpp"
#include "util/all_close.hpp"
#include "util/autodiff/backprop_derivative.hpp"
......
......@@ -20,7 +20,6 @@
#include <vector>
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/type/element_type.hpp"
namespace ngraph
......
......@@ -19,7 +19,6 @@
#include "benchmark.hpp"
#include "ngraph/graph_util.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/manager.hpp"
#include "ngraph/runtime/tensor_view.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment