Commit c67892af authored by Chris Sullivan's avatar Chris Sullivan Committed by Scott Cyphers

Change CPUTensorRole -> TensorRole in ngraph core and utilize it in GPU backend…

Change CPUTensorRole -> TensorRole in ngraph core and utilize it in GPU backend instead of a local enum. This is also now use in NNP backend. (#2900)
parent 3c7ca424
......@@ -653,7 +653,7 @@ using namespace ngraph::runtime;
for (auto& ele_t : tensor_set)
{
NGRAPH_CHECK(ele_t->get_pool_offset() == 0, "no offset set for constants");
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::CONSTANT;
m_tensor_roles[ele_t->get_name()] = TensorRole::CONSTANT;
m_variable_name_map[ele_t->get_name()] = output_tensor->get_name();
}
}
......@@ -795,7 +795,7 @@ using namespace ngraph::runtime;
ss << "(((" << type << "*)(inputs[" << arg_index << "])) + "
<< ele_t->get_pool_offset() / et.size() << ")";
m_variable_name_map[ele_t->get_name()] = ss.str();
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::INPUT;
m_tensor_roles[ele_t->get_name()] = TensorRole::INPUT;
}
arg_index++;
}
......@@ -806,7 +806,7 @@ using namespace ngraph::runtime;
{
for (auto& ele : bufferID_to_tensorSets)
{
if (ele.second.first == CPUTensorRole::INTERMEDIATE)
if (ele.second.first == TensorRole::INTERMEDIATE)
{
for (auto& ele_t : ele.second.second)
{
......@@ -814,7 +814,7 @@ using namespace ngraph::runtime;
ss << "((" << ele_t->get_element_type().c_type_string()
<< "*)(pool_base_ptr + " << ele_t->get_pool_offset() << "))";
m_variable_name_map[ele_t->get_name()] = ss.str();
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::INTERMEDIATE;
m_tensor_roles[ele_t->get_name()] = TensorRole::INTERMEDIATE;
}
}
}
......@@ -835,7 +835,7 @@ using namespace ngraph::runtime;
ss << "(((" << type << "*)(outputs[" << i << "])) + "
<< ele_t->get_pool_offset() / et.size() << ")";
m_variable_name_map[ele_t->get_name()] = ss.str();
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::OUTPUT;
m_tensor_roles[ele_t->get_name()] = TensorRole::OUTPUT;
}
}
......@@ -1220,7 +1220,7 @@ bool runtime::cpu::CPU_ExternalFunction::computes_result(Node* node)
{
auto& output_tensor = node->get_output_tensor(i);
if (m_tensor_roles.find(output_tensor.get_name()) != m_tensor_roles.end() &&
m_tensor_roles[output_tensor.get_name()] == CPUTensorRole::OUTPUT)
m_tensor_roles[output_tensor.get_name()] == TensorRole::OUTPUT)
{
return true;
}
......@@ -1294,14 +1294,14 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
m_memory_buffer_sizes.push_back(m_function->get_temporary_pool_size());
for (auto& ele : bufferID_to_tensorSets)
{
if (ele.second.first == CPUTensorRole::INTERMEDIATE)
if (ele.second.first == TensorRole::INTERMEDIATE)
{
for (auto& ele_t : ele.second.second)
{
m_buffer_indices[ele_t->get_name()] = buffer_index;
intermediates_offsets.emplace_back(m_buffer_indices[ele_t->get_name()],
ele_t->get_pool_offset());
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::INTERMEDIATE;
m_tensor_roles[ele_t->get_name()] = TensorRole::INTERMEDIATE;
buffer_index++;
}
}
......@@ -1323,7 +1323,7 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
for (auto& ele_t : tensor_set)
{
NGRAPH_CHECK(ele_t->get_pool_offset() == 0, "no offset set for constants");
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::CONSTANT;
m_tensor_roles[ele_t->get_name()] = TensorRole::CONSTANT;
if (ele_t->get_name() != output_tensor->get_name())
{
tensor_alias[ele_t->get_name()] = output_tensor->get_name();
......@@ -1346,7 +1346,7 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
// process all tensors in the set containing the output tensor of the parameter
for (auto& ele_t : tensor_set)
{
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::INPUT;
m_tensor_roles[ele_t->get_name()] = TensorRole::INPUT;
m_buffer_indices[ele_t->get_name()] = buffer_index;
function_input_index_offset.emplace_back(m_buffer_indices[ele_t->get_name()],
arg_index,
......@@ -1368,7 +1368,7 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
// process all tensors in the set containing the output tensor of the result
for (auto& ele_t : tensor_set)
{
m_tensor_roles[ele_t->get_name()] = CPUTensorRole::OUTPUT;
m_tensor_roles[ele_t->get_name()] = TensorRole::OUTPUT;
m_buffer_indices[ele_t->get_name()] = buffer_index;
function_output_index_offset.emplace_back(
m_buffer_indices[ele_t->get_name()], i, ele_t->get_pool_offset());
......@@ -1495,13 +1495,13 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
{
string filename = file_util::path_join(s_debug_dir, m_function_name + "_debug.txt");
std::stringstream strm;
auto find_role = [](CPUTensorRole tensor_role) -> string {
auto find_role = [](TensorRole tensor_role) -> string {
switch (tensor_role)
{
case CPUTensorRole::INPUT: return string("CPUTensorRole::INPUT");
case CPUTensorRole::INTERMEDIATE: return string("CPUTensorRole::INTERMEDIATE");
case CPUTensorRole::CONSTANT: return string("CPUTensorRole::CONSTANT");
case CPUTensorRole::OUTPUT: return string("CPUTensorRole::OUTPUT");
case TensorRole::INPUT: return string("TensorRole::INPUT");
case TensorRole::INTERMEDIATE: return string("TensorRole::INTERMEDIATE");
case TensorRole::CONSTANT: return string("TensorRole::CONSTANT");
case TensorRole::OUTPUT: return string("TensorRole::OUTPUT");
}
throw runtime_error("unhandled CPU tensor role");
};
......
......@@ -264,7 +264,7 @@ namespace ngraph
std::unordered_map<std::string, std::pair<std::size_t, std::size_t>>
m_variable_output_index_offset_map;
std::unordered_map<std::string, ngraph::CPUTensorRole> m_tensor_roles;
std::unordered_map<std::string, ngraph::TensorRole> m_tensor_roles;
LayoutDescriptorPtrs parameter_layout_descriptors;
LayoutDescriptorPtrs result_layout_descriptors;
......@@ -287,12 +287,12 @@ namespace ngraph
std::unordered_map<std::string, bool> tensor_stale;
// Each tensor is put into one buffer set.
// All the tensors in the same buffer set share the same memory buffer.
// bufferID_to_tensorSets maps bufferID to the pair of CPUTensorRole and buffer set.
// CPUTensorRole is INPUT, CONSTANT, OUTPUT, or INTERMEDIATE,
// bufferID_to_tensorSets maps bufferID to the pair of TensorRole and buffer set.
// TensorRole is INPUT, CONSTANT, OUTPUT, or INTERMEDIATE,
// which tells from where the memory buffer comes.
std::unordered_map<
size_t,
std::pair<ngraph::CPUTensorRole, std::unordered_set<descriptor::Tensor*>>>
std::pair<ngraph::TensorRole, std::unordered_set<descriptor::Tensor*>>>
bufferID_to_tensorSets;
// tensor_to_bufferID maps tensor to the ID of the buffer set it belongs to.
std::unordered_map<descriptor::Tensor*, size_t> tensor_to_bufferID;
......
......@@ -42,8 +42,7 @@ class ngraph::runtime::cpu::pass::CPUMemoryAssignment : public ngraph::pass::Fun
{
public:
CPUMemoryAssignment(
std::unordered_map<size_t,
std::pair<CPUTensorRole, std::unordered_set<descriptor::Tensor*>>>&,
std::unordered_map<size_t, std::pair<TensorRole, std::unordered_set<descriptor::Tensor*>>>&,
std::unordered_map<descriptor::Tensor*, size_t>&,
size_t alignment = 1,
bool disable_memory_sharing = false);
......@@ -74,7 +73,7 @@ private:
bool m_disable_memory_sharing;
std::set<descriptor::Tensor*> m_tensor_caching;
std::unordered_map<size_t,
std::pair<ngraph::CPUTensorRole, std::unordered_set<descriptor::Tensor*>>>&
std::pair<ngraph::TensorRole, std::unordered_set<descriptor::Tensor*>>>&
m_bufferID_to_tensorSets;
std::unordered_map<descriptor::Tensor*, size_t>& m_tensor_to_bufferID;
};
......@@ -64,7 +64,7 @@ void runtime::gpu::GPUCallFrame::resolve_outputs(void** outputs, size_t num_outp
}
}
// returns pointers of any GPUTensorWrapper::TensorType
// returns pointers of any TensorRole
std::vector<void*>
runtime::gpu::GPUCallFrame::get_tensor_io(const std::vector<GPUTensorWrapper>& tensors)
{
......@@ -78,18 +78,18 @@ std::vector<void*>
return ptrs;
}
void* runtime::gpu::GPUCallFrame::get_pointer(const TensorType& type,
void* runtime::gpu::GPUCallFrame::get_pointer(const TensorRole& type,
const size_t& offset,
const std::string& name)
{
switch (type)
{
case TensorType::CONSTANT:
case TensorType::INTERMEDIATE:
case TensorRole::CONSTANT:
case TensorRole::INTERMEDIATE:
return static_cast<void*>(m_memory_reservations.at(name) + offset);
case TensorType::INPUT: return static_cast<void*>(m_inputs.at(offset));
case TensorType::OUTPUT: return static_cast<void*>(m_outputs.at(offset));
case TensorType::UNKNOWN:
case TensorRole::INPUT: return static_cast<void*>(m_inputs.at(offset));
case TensorRole::OUTPUT: return static_cast<void*>(m_outputs.at(offset));
case TensorRole::UNKNOWN:
default: throw ngraph_error("GPUCallFrame encountered unknown or uninitialized tensor type");
};
}
......@@ -32,8 +32,6 @@ namespace ngraph
class GPUCallFrame
{
public:
using TensorType = GPUTensorWrapper::TensorType;
GPUCallFrame(const size_t& num_inputs, const size_t& num_outputs);
void resolve_reservations(
const GPUCompiledFunction* compiled_function,
......@@ -43,7 +41,7 @@ namespace ngraph
std::vector<void*> get_tensor_io(const std::vector<GPUTensorWrapper>& tensors);
private:
void* get_pointer(const TensorType& type,
void* get_pointer(const TensorRole& type,
const size_t& offset,
const std::string& name = "");
......
......@@ -259,8 +259,8 @@ void runtime::gpu::GPUInternalFunction::build_functions()
string type = et.c_type_string();
stringstream ss;
ss << "((" << type << "*)(inputs[" << arg_index << "]))";
m_variable_name_map[tv->get_name()] = std::make_tuple(
runtime::gpu::GPUTensorWrapper::TensorType::INPUT, arg_index, ss.str());
m_variable_name_map[tv->get_name()] =
std::make_tuple(TensorRole::INPUT, arg_index, ss.str());
// propagate_in_place_input(&param->get_outputs().at(i), ss.str());
arg_index++;
}
......@@ -274,8 +274,7 @@ void runtime::gpu::GPUInternalFunction::build_functions()
string type = tv->get_element_type().c_type_string();
stringstream ss;
ss << "((" << type << "*)(outputs[" << i << "]))";
m_variable_name_map[tv->get_name()] =
std::make_tuple(runtime::gpu::GPUTensorWrapper::TensorType::OUTPUT, i, ss.str());
m_variable_name_map[tv->get_name()] = std::make_tuple(TensorRole::OUTPUT, i, ss.str());
auto res = dynamic_pointer_cast<ngraph::op::Result>(op);
//keep assigning different outputs to a result descriptor
......@@ -287,8 +286,8 @@ void runtime::gpu::GPUInternalFunction::build_functions()
shared_ptr<descriptor::Tensor> itv =
res->get_inputs().at(0).get_output().get_tensor_ptr();
auto output_name = ss.str();
m_variable_name_map[itv->get_name()] = std::make_tuple(
runtime::gpu::GPUTensorWrapper::TensorType::OUTPUT, i, ss.str());
m_variable_name_map[itv->get_name()] =
std::make_tuple(TensorRole::OUTPUT, i, ss.str());
//propagate_in_place_output(&(res->get_inputs().at(0).get_output()), output_name);
}
}
......@@ -310,7 +309,7 @@ void runtime::gpu::GPUInternalFunction::build_functions()
for (descriptor::Tensor* tensor : node->liveness_new_list)
{
m_variable_name_map[tensor->get_name()] =
std::make_tuple(runtime::gpu::GPUTensorWrapper::TensorType::INTERMEDIATE,
std::make_tuple(TensorRole::INTERMEDIATE,
tensor->get_pool_offset(),
current_function->get_name());
}
......@@ -323,8 +322,8 @@ void runtime::gpu::GPUInternalFunction::build_functions()
if (auto c = std::dynamic_pointer_cast<op::Constant>(node))
{
shared_ptr<descriptor::Tensor> tv = node->get_outputs()[0].get_tensor_ptr();
m_variable_name_map[tv->get_name()] = std::make_tuple(
runtime::gpu::GPUTensorWrapper::TensorType::CONSTANT, 0, node->get_name());
m_variable_name_map[tv->get_name()] =
std::make_tuple(TensorRole::CONSTANT, 0, node->get_name());
}
}
......
......@@ -87,9 +87,7 @@ namespace ngraph
// internal ops
virtual void propagate_in_place_output(ngraph::descriptor::Output* res_src_output,
const std::string& output_name) override;
std::unordered_map<
std::string,
std::tuple<runtime::gpu::GPUTensorWrapper::TensorType, size_t, std::string>>
std::unordered_map<std::string, std::tuple<TensorRole, size_t, std::string>>
m_variable_name_map;
std::unique_ptr<GPURuntimeConstructor> m_runtime_constructor;
std::shared_ptr<CodeWriter> m_trace;
......
......@@ -26,13 +26,12 @@ runtime::gpu::GPUTensorWrapper::GPUTensorWrapper(const shared_ptr<descriptor::Te
const string& alias)
: m_tensor(tv)
, m_alias(alias)
, m_offset(std::make_pair(runtime::gpu::GPUTensorWrapper::TensorType::UNKNOWN,
std::numeric_limits<size_t>::max()))
, m_offset(std::make_pair(TensorRole::UNKNOWN, std::numeric_limits<size_t>::max()))
{
}
runtime::gpu::GPUTensorWrapper::GPUTensorWrapper(const std::shared_ptr<descriptor::Tensor>& tv,
runtime::gpu::GPUTensorWrapper::TensorType type,
TensorRole type,
size_t offset,
const std::string& alias)
: m_tensor(tv)
......@@ -73,8 +72,7 @@ const std::string& runtime::gpu::GPUTensorWrapper::get_name() const
}
}
const std::pair<runtime::gpu::GPUTensorWrapper::TensorType, size_t>&
runtime::gpu::GPUTensorWrapper::get_offset() const
const std::pair<TensorRole, size_t>& runtime::gpu::GPUTensorWrapper::get_offset() const
{
return m_offset;
}
......
......@@ -21,6 +21,7 @@
#include "ngraph/descriptor/tensor.hpp"
#include "ngraph/type/element_type.hpp"
#include "ngraph/util.hpp"
namespace ngraph
{
......@@ -38,17 +39,9 @@ namespace ngraph
class ngraph::runtime::gpu::GPUTensorWrapper
{
public:
enum TensorType : std::size_t
{
CONSTANT,
INTERMEDIATE,
INPUT,
OUTPUT,
UNKNOWN
};
GPUTensorWrapper(const std::shared_ptr<descriptor::Tensor>&, const std::string& alias = "");
GPUTensorWrapper(const std::shared_ptr<descriptor::Tensor>&,
TensorType,
TensorRole,
size_t,
const std::string& alias);
......@@ -58,12 +51,12 @@ public:
const element::Type& get_element_type() const;
const std::string& get_name() const;
const std::string& get_type() const;
const std::pair<TensorType, size_t>& get_offset() const;
const std::pair<TensorRole, size_t>& get_offset() const;
friend std::ostream& ngraph::runtime::gpu::
operator<<(std::ostream& out, const ngraph::runtime::gpu::GPUTensorWrapper& obj);
private:
std::shared_ptr<descriptor::Tensor> m_tensor;
std::string m_alias;
std::pair<TensorType, size_t> m_offset;
std::pair<TensorRole, size_t> m_offset;
};
......@@ -237,12 +237,13 @@ namespace ngraph
using BuildNodeExecutorMap = std::unordered_map<std::type_index, BuildNodeExecutor>;
enum class CPUTensorRole
enum class TensorRole
{
INPUT,
CONSTANT,
OUTPUT,
INTERMEDIATE
INTERMEDIATE,
UNKNOWN
};
/**
......
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