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

move cpu tensor view to runtime dir and change cpu and interpreter to use it (#399)

parent ad6b0f07
......@@ -88,12 +88,12 @@ set (SRC
pass/visualize_tree.cpp
pattern/matcher.cpp
runtime/aligned_buffer.cpp
runtime/host_tensor_view.cpp
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/interpreter/int_tensor_view.cpp
runtime/manager.cpp
runtime/tensor_view.cpp
serializer.cpp
......@@ -168,7 +168,6 @@ if (NGRAPH_CPU_ENABLE AND LLVM_INCLUDE_DIR AND
runtime/cpu/cpu_kernel_utils.cpp
runtime/cpu/cpu_emitter.cpp
runtime/cpu/cpu_external_function.cpp
runtime/cpu/cpu_tensor_view.cpp
runtime/cpu/cpu_tensor_view_wrapper.cpp
)
# LLVM binary builds are typically built without RTTI
......
......@@ -14,8 +14,8 @@
#include "ngraph/runtime/cpu/cpu_backend.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/runtime/external_function.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
using namespace ngraph;
using namespace std;
......@@ -30,6 +30,6 @@ std::shared_ptr<ngraph::runtime::TensorView>
runtime::cpu::CPU_Backend::make_primary_tensor_view(const ngraph::element::Type& element_type,
const Shape& shape)
{
auto rc = make_shared<runtime::cpu::CPU_TensorView>(element_type, shape);
auto rc = make_shared<runtime::HostTensorView>(element_type, shape);
return dynamic_pointer_cast<runtime::TensorView>(rc);
}
......@@ -22,8 +22,6 @@ namespace ngraph
{
namespace cpu
{
static size_t alignment = 64;
class CPU_Backend : public runtime::Backend
{
public:
......
......@@ -16,7 +16,7 @@
#include "ngraph/runtime/cpu/cpu_call_frame.hpp"
#include "ngraph/runtime/cpu/cpu_external_function.hpp"
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
using namespace std;
using namespace ngraph;
......@@ -36,14 +36,14 @@ void runtime::cpu::CPU_CallFrame::tensor_call(
vector<void*> outputs;
for (size_t i = 0; i < input_tvs.size(); i++)
{
shared_ptr<runtime::cpu::CPU_TensorView> tv =
static_pointer_cast<runtime::cpu::CPU_TensorView>(input_tvs[i]);
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(input_tvs[i]);
inputs.push_back(tv->get_data_ptr());
}
for (size_t i = 0; i < output_tvs.size(); i++)
{
shared_ptr<runtime::cpu::CPU_TensorView> tv =
static_pointer_cast<runtime::cpu::CPU_TensorView>(output_tvs[i]);
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(output_tvs[i]);
outputs.push_back(tv->get_data_ptr());
}
......
......@@ -92,6 +92,7 @@
#include "ngraph/runtime/cpu/cpu_call_frame.hpp"
#include "ngraph/runtime/cpu/cpu_emitter.hpp"
#include "ngraph/runtime/cpu/cpu_external_function.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
using namespace std;
using namespace ngraph;
......@@ -499,7 +500,7 @@ using namespace ngraph::runtime;
writer << "// Memory pool size is " << temp_pool_size << " bytes\n";
writer << "// Worst case size is " << worst_case_tmp_size << " bytes\n";
writer << "ngraph::runtime::AlignedBuffer memory_handler(" << temp_pool_size << ", "
<< ngraph::runtime::cpu::alignment << ");\n";
<< ngraph::runtime::alignment << ");\n";
writer << "size_t pool_base_ptr = (size_t)memory_handler.get_ptr();\n";
writer << "\n";
......
......@@ -30,5 +30,6 @@ std::shared_ptr<ngraph::runtime::TensorView>
runtime::gpu::GPU_Backend::make_device_tensor(const ngraph::element::Type& element_type,
const Shape& shape)
{
return make_shared<runtime::TensorView>(element_type, shape);
auto rc = make_shared<runtime::HostTensorView>(element_type, shape);
return dynamic_pointer_cast<runtime::TensorView>(rc);
}
......@@ -41,14 +41,14 @@ void runtime::gpu::GPU_CallFrame::tensor_call(
for (size_t i = 0; i < input_tvs.size(); i++)
{
shared_ptr<runtime::cpu::CPU_TensorView> tv =
static_pointer_cast<runtime::cpu::CPU_TensorView>(input_tvs[i]);
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(input_tvs[i]);
inputs.push_back(tv->get_data_ptr());
}
for (size_t i = 0; i < output_tvs.size(); i++)
{
shared_ptr<runtime::cpu::CPU_TensorView> tv =
static_pointer_cast<runtime::cpu::CPU_TensorView>(output_tvs[i]);
shared_ptr<runtime::HostTensorView> tv =
static_pointer_cast<runtime::HostTensorView>(output_tvs[i]);
outputs.push_back(tv->get_data_ptr());
}
......
......@@ -17,20 +17,16 @@
#include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp"
#include "ngraph/descriptor/primary_tensor_view.hpp"
#include "ngraph/runtime/cpu/cpu_backend.hpp"
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
using namespace ngraph;
using namespace std;
runtime::cpu::CPU_TensorView::CPU_TensorView(const ngraph::element::Type& element_type,
const Shape& shape)
runtime::HostTensorView::HostTensorView(const ngraph::element::Type& element_type,
const Shape& shape,
const string& name)
: runtime::TensorView(std::make_shared<ngraph::descriptor::PrimaryTensorView>(
std::make_shared<ngraph::TensorViewType>(element_type, shape),
"external",
true,
true,
false))
std::make_shared<ngraph::TensorViewType>(element_type, shape), name, true, true, false))
, m_allocated_buffer_pool(nullptr)
, m_aligned_buffer_pool(nullptr)
......@@ -41,7 +37,7 @@ runtime::cpu::CPU_TensorView::CPU_TensorView(const ngraph::element::Type& elemen
m_buffer_size = m_descriptor->get_tensor_view_layout()->get_size() * element_type.size();
if (m_buffer_size > 0)
{
size_t allocation_size = m_buffer_size + runtime::cpu::alignment;
size_t allocation_size = m_buffer_size + runtime::alignment;
m_allocated_buffer_pool = static_cast<char*>(malloc(allocation_size));
m_aligned_buffer_pool = m_allocated_buffer_pool;
size_t mod = size_t(m_aligned_buffer_pool) % alignment;
......@@ -52,7 +48,7 @@ runtime::cpu::CPU_TensorView::CPU_TensorView(const ngraph::element::Type& elemen
}
}
runtime::cpu::CPU_TensorView::~CPU_TensorView()
runtime::HostTensorView::~HostTensorView()
{
if (m_allocated_buffer_pool != nullptr)
{
......@@ -60,17 +56,17 @@ runtime::cpu::CPU_TensorView::~CPU_TensorView()
}
}
char* runtime::cpu::CPU_TensorView::get_data_ptr()
char* runtime::HostTensorView::get_data_ptr()
{
return m_aligned_buffer_pool;
}
const char* runtime::cpu::CPU_TensorView::get_data_ptr() const
const char* runtime::HostTensorView::get_data_ptr() const
{
return m_aligned_buffer_pool;
}
void runtime::cpu::CPU_TensorView::write(const void* source, size_t tensor_offset, size_t n)
void runtime::HostTensorView::write(const void* source, size_t tensor_offset, size_t n)
{
if (tensor_offset + n > m_buffer_size)
{
......@@ -80,7 +76,7 @@ void runtime::cpu::CPU_TensorView::write(const void* source, size_t tensor_offse
memcpy(&target[tensor_offset], source, n);
}
void runtime::cpu::CPU_TensorView::read(void* target, size_t tensor_offset, size_t n) const
void runtime::HostTensorView::read(void* target, size_t tensor_offset, size_t n) const
{
if (tensor_offset + n > m_buffer_size)
{
......@@ -90,12 +86,12 @@ void runtime::cpu::CPU_TensorView::read(void* target, size_t tensor_offset, size
memcpy(target, &source[tensor_offset], n);
}
size_t runtime::cpu::CPU_TensorView::get_size() const
size_t runtime::HostTensorView::get_size() const
{
return get_tensor_view_layout()->get_size();
}
const element::Type& runtime::cpu::CPU_TensorView::get_element_type() const
const element::Type& runtime::HostTensorView::get_element_type() const
{
return get_tensor_view_layout()->get_element_type();
}
......@@ -23,18 +23,19 @@ namespace ngraph
{
namespace runtime
{
namespace cpu
{
class CPU_TensorView;
}
static size_t alignment = 64;
class HostTensorView;
}
}
class ngraph::runtime::cpu::CPU_TensorView : public ngraph::runtime::TensorView
class ngraph::runtime::HostTensorView : public ngraph::runtime::TensorView
{
public:
CPU_TensorView(const ngraph::element::Type& element_type, const Shape& shape);
virtual ~CPU_TensorView();
HostTensorView(const ngraph::element::Type& element_type,
const Shape& shape,
const std::string& name = "external");
virtual ~HostTensorView();
char* get_data_ptr();
const char* get_data_ptr() const;
......
......@@ -15,7 +15,7 @@
#include "ngraph/runtime/interpreter/int_backend.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/external_function.hpp"
#include "ngraph/runtime/interpreter/int_tensor_view.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
using namespace ngraph;
using namespace std;
......@@ -30,6 +30,6 @@ shared_ptr<runtime::TensorView>
runtime::interpreter::INT_Backend::make_primary_tensor_view(const element::Type& element_type,
const Shape& shape)
{
auto rc = make_shared<runtime::interpreter::INT_TensorView>(element_type, shape, "external");
auto rc = make_shared<runtime::HostTensorView>(element_type, shape, "external");
return static_pointer_cast<runtime::TensorView>(rc);
}
......@@ -22,8 +22,6 @@ namespace ngraph
{
namespace interpreter
{
static size_t alignment = 64;
class INT_Backend : public runtime::Backend
{
public:
......
......@@ -16,8 +16,8 @@
#include <cstdlib>
#include <iomanip>
#include "ngraph/runtime/host_tensor_view.hpp"
#include "ngraph/runtime/interpreter/int_call_frame.hpp"
#include "ngraph/runtime/interpreter/int_tensor_view.hpp"
using namespace std;
using namespace ngraph;
......@@ -33,15 +33,14 @@ runtime::interpreter::INT_CallFrame::INT_CallFrame(shared_ptr<ExternalFunction>
void runtime::interpreter::INT_CallFrame::call(
std::shared_ptr<Function> function,
const vector<shared_ptr<runtime::interpreter::INT_TensorView>>& input_tvs,
const vector<shared_ptr<runtime::interpreter::INT_TensorView>>& output_tvs)
const vector<shared_ptr<runtime::HostTensorView>>& input_tvs,
const vector<shared_ptr<runtime::HostTensorView>>& output_tvs)
{
if (m_nan_check)
{
perform_nan_check(input_tvs);
}
unordered_map<descriptor::TensorView*, shared_ptr<runtime::interpreter::INT_TensorView>>
tensor_map;
unordered_map<descriptor::TensorView*, shared_ptr<runtime::HostTensorView>> tensor_map;
size_t arg_index = 0;
for (shared_ptr<op::Parameter> param : function->get_parameters())
......@@ -95,8 +94,8 @@ void runtime::interpreter::INT_CallFrame::call(
continue;
}
vector<shared_ptr<runtime::interpreter::INT_TensorView>> inputs;
vector<shared_ptr<runtime::interpreter::INT_TensorView>> outputs;
vector<shared_ptr<runtime::HostTensorView>> inputs;
vector<shared_ptr<runtime::HostTensorView>> outputs;
for (const descriptor::Input& input : op->get_inputs())
{
descriptor::TensorView* tv = input.get_output().get_tensor_view().get();
......@@ -107,15 +106,14 @@ void runtime::interpreter::INT_CallFrame::call(
{
descriptor::TensorView* tv = op->get_output_tensor_view(i).get();
string name = tv->get_tensor().get_name();
shared_ptr<runtime::interpreter::INT_TensorView> itv;
shared_ptr<runtime::HostTensorView> itv;
if (!contains_key(tensor_map, tv))
{
// The output tensor is not in the tensor map so create a new tensor
const Shape& shape = op->get_output_shape(i);
const element::Type& element_type = op->get_output_element_type(i);
string tensor_name = op->get_output_tensor(i).get_name();
itv = make_shared<runtime::interpreter::INT_TensorView>(
element_type, shape, tensor_name);
itv = make_shared<runtime::HostTensorView>(element_type, shape, tensor_name);
tensor_map.insert({tv, itv});
}
else
......@@ -179,7 +177,7 @@ void runtime::interpreter::INT_CallFrame::call(
void runtime::interpreter::INT_CallFrame::handle_output_alias(
const Node& node,
const unordered_map<descriptor::TensorView*, vector<size_t>>& output_alias_map,
const vector<shared_ptr<runtime::interpreter::INT_TensorView>>& output_tvs)
const vector<shared_ptr<runtime::HostTensorView>>& output_tvs)
{
for (size_t i = 0; i < node.get_output_size(); ++i)
{
......@@ -205,8 +203,8 @@ void runtime::interpreter::INT_CallFrame::generate_calls(
const element::Type& base_type,
const element::Type& secondary_type,
ngraph::Node& op,
const std::vector<std::shared_ptr<INT_TensorView>>& args,
const std::vector<std::shared_ptr<INT_TensorView>>& out)
const std::vector<std::shared_ptr<HostTensorView>>& args,
const std::vector<std::shared_ptr<HostTensorView>>& out)
{
if (base_type == element::boolean)
{
......@@ -261,8 +259,8 @@ void runtime::interpreter::INT_CallFrame::generate_calls(
}
void runtime::interpreter::INT_CallFrame::tensor_call(
const vector<shared_ptr<runtime::interpreter::INT_TensorView>>& input_tvs,
const vector<shared_ptr<runtime::interpreter::INT_TensorView>>& output_tvs)
const vector<shared_ptr<runtime::HostTensorView>>& input_tvs,
const vector<shared_ptr<runtime::HostTensorView>>& output_tvs)
{
call(m_function, input_tvs, output_tvs);
}
......@@ -271,15 +269,15 @@ void runtime::interpreter::INT_CallFrame::tensor_call(
const vector<shared_ptr<runtime::TensorView>>& input_tvs,
const vector<shared_ptr<runtime::TensorView>>& output_tvs)
{
vector<shared_ptr<runtime::interpreter::INT_TensorView>> args;
vector<shared_ptr<runtime::interpreter::INT_TensorView>> out;
vector<shared_ptr<runtime::HostTensorView>> args;
vector<shared_ptr<runtime::HostTensorView>> out;
for (auto tv : input_tvs)
{
args.push_back(static_pointer_cast<runtime::interpreter::INT_TensorView>(tv));
args.push_back(static_pointer_cast<runtime::HostTensorView>(tv));
}
for (auto tv : output_tvs)
{
out.push_back(static_pointer_cast<runtime::interpreter::INT_TensorView>(tv));
out.push_back(static_pointer_cast<runtime::HostTensorView>(tv));
}
tensor_call(args, out);
}
......@@ -317,10 +315,10 @@ vector<runtime::PerformanceCounter>
}
void runtime::interpreter::INT_CallFrame::perform_nan_check(
const vector<shared_ptr<INT_TensorView>>& tvs, const Node* op)
const vector<shared_ptr<HostTensorView>>& tvs, const Node* op)
{
size_t arg_number = 1;
for (shared_ptr<INT_TensorView> tv : tvs)
for (shared_ptr<HostTensorView> tv : tvs)
{
const element::Type& type = tv->get_tensor().get_element_type();
if (type == element::f32)
......
......@@ -39,8 +39,7 @@
#include "ngraph/ops/slice.hpp"
#include "ngraph/ops/sum.hpp"
#include "ngraph/runtime/call_frame.hpp"
#include "ngraph/runtime/interpreter/int_tensor_view.hpp"
#include "ngraph/runtime/interpreter/int_tensor_view.hpp"
#include "ngraph/runtime/host_tensor_view.hpp"
#include "ngraph/runtime/kernel/abs.hpp"
#include "ngraph/runtime/kernel/acos.hpp"
#include "ngraph/runtime/kernel/add.hpp"
......@@ -130,17 +129,17 @@ private:
/// tensor views.
void tensor_call(const std::vector<std::shared_ptr<TensorView>>& inputs,
const std::vector<std::shared_ptr<TensorView>>& outputs) override;
void tensor_call(const std::vector<std::shared_ptr<INT_TensorView>>& inputs,
const std::vector<std::shared_ptr<INT_TensorView>>& outputs);
void tensor_call(const std::vector<std::shared_ptr<HostTensorView>>& inputs,
const std::vector<std::shared_ptr<HostTensorView>>& outputs);
void call(std::shared_ptr<Function> function,
const std::vector<std::shared_ptr<runtime::interpreter::INT_TensorView>>& input_tvs,
const std::vector<std::shared_ptr<runtime::interpreter::INT_TensorView>>& output_tvs);
const std::vector<std::shared_ptr<runtime::HostTensorView>>& input_tvs,
const std::vector<std::shared_ptr<runtime::HostTensorView>>& output_tvs);
void handle_output_alias(
const Node& node,
const std::unordered_map<descriptor::TensorView*, std::vector<size_t>>& output_alias_map,
const std::vector<std::shared_ptr<runtime::interpreter::INT_TensorView>>& output_tvs);
const std::vector<std::shared_ptr<runtime::HostTensorView>>& output_tvs);
static void perform_nan_check(const std::vector<std::shared_ptr<INT_TensorView>>&,
static void perform_nan_check(const std::vector<std::shared_ptr<HostTensorView>>&,
const Node* op = nullptr);
std::shared_ptr<ExternalFunction> m_external_function;
......@@ -152,14 +151,14 @@ private:
void generate_calls(const element::Type& base_type,
const element::Type& secondary_type,
ngraph::Node& op,
const std::vector<std::shared_ptr<INT_TensorView>>& args,
const std::vector<std::shared_ptr<INT_TensorView>>& out);
const std::vector<std::shared_ptr<HostTensorView>>& args,
const std::vector<std::shared_ptr<HostTensorView>>& out);
template <typename BASE>
void generate_calls(const element::Type& type,
ngraph::Node& op,
const std::vector<std::shared_ptr<INT_TensorView>>& args,
const std::vector<std::shared_ptr<INT_TensorView>>& out)
const std::vector<std::shared_ptr<HostTensorView>>& args,
const std::vector<std::shared_ptr<HostTensorView>>& out)
{
if (type == element::boolean)
{
......@@ -215,8 +214,8 @@ private:
template <typename T, typename S>
void op_engine(ngraph::Node& node,
const std::vector<std::shared_ptr<INT_TensorView>>& args,
const std::vector<std::shared_ptr<INT_TensorView>>& out)
const std::vector<std::shared_ptr<HostTensorView>>& args,
const std::vector<std::shared_ptr<HostTensorView>>& out)
{
std::string node_op = node.description();
if (node_op == "Abs")
......@@ -286,7 +285,7 @@ private:
const op::Concat* concat = static_cast<const op::Concat*>(&node);
std::vector<T*> in_args;
std::vector<Shape> in_shapes;
for (std::shared_ptr<INT_TensorView> arg : args)
for (std::shared_ptr<HostTensorView> arg : args)
{
in_args.push_back(reinterpret_cast<T*>(arg->get_data_ptr()));
in_shapes.push_back(arg->get_shape());
......@@ -504,11 +503,11 @@ private:
std::shared_ptr<ngraph::Function> reduction_function = reduce->get_functions()[0];
std::function<T(T, T)> f = [this, &node, reduction_function](T x, T y) -> T {
auto tx = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tx = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(0).get_element_type(), Shape{}, "reduce_temp_x");
auto ty = std::make_shared<runtime::interpreter::INT_TensorView>(
auto ty = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(1).get_element_type(), Shape{}, "reduce_temp_y");
auto tr = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tr = std::make_shared<runtime::HostTensorView>(
node.get_output_element_type(0), Shape{}, "reduce_temp_r");
*(reinterpret_cast<T*>(tx->get_data_ptr())) = x;
*(reinterpret_cast<T*>(ty->get_data_ptr())) = y;
......@@ -532,11 +531,11 @@ private:
reduce_window->get_functions()[0];
std::function<T(T, T)> f = [this, &node, reduction_function](T x, T y) -> T {
auto tx = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tx = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(0).get_element_type(), Shape{}, "reduce_window_temp_x");
auto ty = std::make_shared<runtime::interpreter::INT_TensorView>(
auto ty = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(1).get_element_type(), Shape{}, "reduce_window_temp_y");
auto tr = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tr = std::make_shared<runtime::HostTensorView>(
node.get_output_element_type(0), Shape{}, "reduce_window_temp_r");
*(reinterpret_cast<T*>(tx->get_data_ptr())) = x;
*(reinterpret_cast<T*>(ty->get_data_ptr())) = y;
......@@ -604,11 +603,11 @@ private:
select_and_scatter->get_functions()[0];
std::function<bool(T, T)> f_selection = [this, &node, selection_function](T x,
T y) -> bool {
auto tx = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tx = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(0).get_element_type(), Shape{}, "selection_temp_x");
auto ty = std::make_shared<runtime::interpreter::INT_TensorView>(
auto ty = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(1).get_element_type(), Shape{}, "selection_temp_y");
auto tr = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tr = std::make_shared<runtime::HostTensorView>(
element::boolean, Shape{}, "selection_temp_r");
*(reinterpret_cast<T*>(tx->get_data_ptr())) = x;
*(reinterpret_cast<T*>(ty->get_data_ptr())) = y;
......@@ -619,11 +618,11 @@ private:
std::shared_ptr<ngraph::Function> scatter_function =
select_and_scatter->get_functions()[1];
std::function<T(T, T)> f_scatter = [this, &node, scatter_function](T x, T y) -> T {
auto tx = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tx = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(0).get_element_type(), Shape{}, "scatter_temp_x");
auto ty = std::make_shared<runtime::interpreter::INT_TensorView>(
auto ty = std::make_shared<runtime::HostTensorView>(
node.get_inputs().at(1).get_element_type(), Shape{}, "scatter_temp_y");
auto tr = std::make_shared<runtime::interpreter::INT_TensorView>(
auto tr = std::make_shared<runtime::HostTensorView>(
node.get_output_element_type(0), Shape{}, "scatter_temp_r");
*(reinterpret_cast<T*>(tx->get_data_ptr())) = x;
*(reinterpret_cast<T*>(ty->get_data_ptr())) = y;
......
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// 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
// ----------------------------------------------------------------------------
#include <cstring>
#include <memory>
#include "ngraph/descriptor/layout/dense_tensor_view_layout.hpp"
#include "ngraph/descriptor/primary_tensor_view.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/interpreter/int_backend.hpp"
#include "ngraph/runtime/interpreter/int_tensor_view.hpp"
using namespace ngraph;
using namespace std;
runtime::interpreter::INT_TensorView::INT_TensorView(const element::Type& element_type,
const Shape& shape,
const string& name)
: runtime::TensorView(std::make_shared<descriptor::PrimaryTensorView>(
std::make_shared<TensorViewType>(element_type, shape), name, true, true, false))
, m_allocated_buffer_pool(nullptr)
, m_aligned_buffer_pool(nullptr)
{
m_descriptor->set_tensor_view_layout(
std::make_shared<descriptor::layout::DenseTensorViewLayout>(*m_descriptor));
m_buffer_size = m_descriptor->get_tensor_view_layout()->get_size() * element_type.size();
if (m_buffer_size > 0)
{
size_t allocation_size = m_buffer_size + runtime::interpreter::alignment;
m_allocated_buffer_pool = static_cast<char*>(malloc(allocation_size));
m_aligned_buffer_pool = m_allocated_buffer_pool;
size_t mod = size_t(m_aligned_buffer_pool) % alignment;
if (mod != 0)
{
m_aligned_buffer_pool += (alignment - mod);
}
}
}
runtime::interpreter::INT_TensorView::~INT_TensorView()
{
if (m_allocated_buffer_pool != nullptr)
{
free(m_allocated_buffer_pool);
}
}
char* runtime::interpreter::INT_TensorView::get_data_ptr()
{
return m_aligned_buffer_pool;
}
const char* runtime::interpreter::INT_TensorView::get_data_ptr() const
{
return m_aligned_buffer_pool;
}
void runtime::interpreter::INT_TensorView::write(const void* source, size_t tensor_offset, size_t n)
{
if (tensor_offset + n > m_buffer_size)
{
throw out_of_range("write access past end of tensor");
}
char* target = get_data_ptr();
memcpy(&target[tensor_offset], source, n);
}
void runtime::interpreter::INT_TensorView::read(void* target, size_t tensor_offset, size_t n) const
{
if (tensor_offset + n > m_buffer_size)
{
throw out_of_range("read access past end of tensor");
}
const char* source = get_data_ptr();
memcpy(target, &source[tensor_offset], n);
}
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// 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
// ----------------------------------------------------------------------------
#pragma once
#include <memory>
#include "ngraph/runtime/tensor_view.hpp"
#include "ngraph/types/element_type.hpp"
namespace ngraph
{
namespace runtime
{
namespace interpreter
{
class INT_TensorView;
}
}
}
class ngraph::runtime::interpreter::INT_TensorView : public ngraph::runtime::TensorView
{
public:
INT_TensorView(const ngraph::element::Type& element_type,
const Shape& shape,
const std::string& name);
virtual ~INT_TensorView();
char* get_data_ptr();
const char* get_data_ptr() const;
/// @brief Write bytes directly into the tensor
/// @param p Pointer to source of data
/// @param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned.
/// @param n Number of bytes to write, must be integral number of elements.
void write(const void* p, size_t tensor_offset, size_t n) override;
/// @brief Read bytes directly from the tensor
/// @param p Pointer to destination for data
/// @param tensor_offset Offset into tensor storage to begin reading. Must be element-aligned.
/// @param n Number of bytes to read, must be integral number of elements.
void read(void* p, size_t tensor_offset, size_t n) const override;
private:
char* m_allocated_buffer_pool;
char* m_aligned_buffer_pool;
size_t m_buffer_size;
};
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