Unverified Commit 8caa2717 authored by Robert Kimball's avatar Robert Kimball Committed by GitHub

Major cleanup of runtime::TensorView (#1682)

* remove get_tensor method

* remove get_element_count as it is redundant to get_size

* fix a few issues with examples and python wrapper

* rename get_size to get_element_count and add get_size_in_bytes method
parent 35a4a32f
...@@ -37,7 +37,7 @@ template <typename T> ...@@ -37,7 +37,7 @@ template <typename T>
void randomize(std::function<T()> rand, void randomize(std::function<T()> rand,
const std::shared_ptr<ngraph::runtime::TensorView>& t) const std::shared_ptr<ngraph::runtime::TensorView>& t)
{ {
if (t->get_tensor().get_element_type().bitwidth() != 8 * sizeof(T)) if (t->get_element_type().bitwidth() != 8 * sizeof(T))
{ {
throw std::invalid_argument( throw std::invalid_argument(
"Randomize generator size is not the same as tensor " "Randomize generator size is not the same as tensor "
......
...@@ -37,6 +37,6 @@ void regclass_pyngraph_runtime_TensorView(py::module m) ...@@ -37,6 +37,6 @@ void regclass_pyngraph_runtime_TensorView(py::module m)
tensorView.def_property_readonly("element_count", tensorView.def_property_readonly("element_count",
&ngraph::runtime::TensorView::get_element_count); &ngraph::runtime::TensorView::get_element_count);
tensorView.def_property_readonly("element_type", [](const ngraph::runtime::TensorView& self) { tensorView.def_property_readonly("element_type", [](const ngraph::runtime::TensorView& self) {
return self.get_tensor().get_element_type(); return self.get_element_type();
}); });
} }
...@@ -71,10 +71,10 @@ void runtime::Backend::validate_call(shared_ptr<const Function> function, ...@@ -71,10 +71,10 @@ void runtime::Backend::validate_call(shared_ptr<const Function> function,
for (size_t i = 0; i < input_parameters.size(); i++) for (size_t i = 0; i < input_parameters.size(); i++)
{ {
if (input_parameters[i]->get_element_type() != inputs[i]->get_tensor().get_element_type()) if (input_parameters[i]->get_element_type() != inputs[i]->get_element_type())
{ {
stringstream ss; stringstream ss;
ss << "Input " << i << " type '" << inputs[i]->get_tensor().get_element_type() ss << "Input " << i << " type '" << inputs[i]->get_element_type()
<< "' does not match Parameter type '" << input_parameters[i]->get_element_type() << "' does not match Parameter type '" << input_parameters[i]->get_element_type()
<< "'"; << "'";
throw runtime_error(ss.str()); throw runtime_error(ss.str());
...@@ -91,10 +91,10 @@ void runtime::Backend::validate_call(shared_ptr<const Function> function, ...@@ -91,10 +91,10 @@ void runtime::Backend::validate_call(shared_ptr<const Function> function,
for (size_t i = 0; i < function->get_output_size(); i++) for (size_t i = 0; i < function->get_output_size(); i++)
{ {
if (function->get_output_element_type(i) != outputs[i]->get_tensor().get_element_type()) if (function->get_output_element_type(i) != outputs[i]->get_element_type())
{ {
stringstream ss; stringstream ss;
ss << "Output " << i << " type '" << outputs[i]->get_tensor().get_element_type() ss << "Output " << i << " type '" << outputs[i]->get_element_type()
<< "' does not match Result type '" << function->get_output_element_type(i) << "'"; << "' does not match Result type '" << function->get_output_element_type(i) << "'";
throw runtime_error(ss.str()); throw runtime_error(ss.str());
} }
......
...@@ -95,7 +95,7 @@ void runtime::cpu::CPU_CallFrame::propagate_layouts( ...@@ -95,7 +95,7 @@ void runtime::cpu::CPU_CallFrame::propagate_layouts(
throw ngraph_error( throw ngraph_error(
"Error propagating layouts - layout information missing from tensor view"); "Error propagating layouts - layout information missing from tensor view");
} }
tvs[i]->get_descriptor()->set_tensor_layout(layouts[i]); tvs[i]->set_tensor_layout(layouts[i]);
} }
} }
......
...@@ -131,7 +131,7 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_ ...@@ -131,7 +131,7 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_
return false; return false;
} }
auto native_md = mkldnn_utils::create_blocked_mkldnn_md( auto native_md = mkldnn_utils::create_blocked_mkldnn_md(
this->get_shape(), cpu_tvl->get_strides(), this->get_descriptor()->get_element_type()); this->get_shape(), cpu_tvl->get_strides(), this->get_element_type());
if (mkldnn_utils::compare_mkldnn_mds(cpu_tvl->get_mkldnn_md(), native_md)) if (mkldnn_utils::compare_mkldnn_mds(cpu_tvl->get_mkldnn_md(), native_md))
{ {
return false; return false;
...@@ -144,7 +144,7 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_ ...@@ -144,7 +144,7 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_
auto tensor_shape = this->get_shape(); auto tensor_shape = this->get_shape();
auto input_desc = cpu_tvl->get_mkldnn_md(); auto input_desc = cpu_tvl->get_mkldnn_md();
auto output_desc = mkldnn_utils::create_blocked_mkldnn_md( auto output_desc = mkldnn_utils::create_blocked_mkldnn_md(
this->get_shape(), cpu_tvl->get_strides(), this->get_descriptor()->get_element_type()); this->get_shape(), cpu_tvl->get_strides(), this->get_element_type());
memory input{{input_desc, mkldnn_utils::global_cpu_engine}, aligned_buffer}; memory input{{input_desc, mkldnn_utils::global_cpu_engine}, aligned_buffer};
memory output{{output_desc, mkldnn_utils::global_cpu_engine}, target}; memory output{{output_desc, mkldnn_utils::global_cpu_engine}, target};
...@@ -158,8 +158,3 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_ ...@@ -158,8 +158,3 @@ void runtime::cpu::CPUTensorView::read(void* target, size_t tensor_offset, size_
memcpy(target, &source[tensor_offset], n); memcpy(target, &source[tensor_offset], n);
} }
} }
size_t runtime::cpu::CPUTensorView::get_size() const
{
return get_element_count();
}
...@@ -45,9 +45,6 @@ namespace ngraph ...@@ -45,9 +45,6 @@ namespace ngraph
char* get_data_ptr(); char* get_data_ptr();
const char* get_data_ptr() const; const char* get_data_ptr() const;
size_t get_size() const;
const element::Type& get_element_type() const;
/// \brief Write bytes directly into the tensor /// \brief Write bytes directly into the tensor
/// \param p Pointer to source of data /// \param p Pointer to source of data
/// \param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned. /// \param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned.
......
...@@ -99,13 +99,3 @@ void runtime::HostTensorView::read(void* target, size_t tensor_offset, size_t n) ...@@ -99,13 +99,3 @@ void runtime::HostTensorView::read(void* target, size_t tensor_offset, size_t n)
const char* source = get_data_ptr(); const char* source = get_data_ptr();
memcpy(target, &source[tensor_offset], n); memcpy(target, &source[tensor_offset], n);
} }
size_t runtime::HostTensorView::get_size() const
{
return get_tensor_layout()->get_size();
}
const element::Type& runtime::HostTensorView::get_element_type() const
{
return get_tensor_layout()->get_element_type();
}
...@@ -58,9 +58,6 @@ public: ...@@ -58,9 +58,6 @@ public:
return reinterpret_cast<T*>(get_data_ptr()); return reinterpret_cast<T*>(get_data_ptr());
} }
size_t get_size() const;
const element::Type& get_element_type() const;
/// \brief Write bytes directly into the tensor /// \brief Write bytes directly into the tensor
/// \param p Pointer to source of data /// \param p Pointer to source of data
/// \param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned. /// \param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned.
......
...@@ -209,7 +209,7 @@ bool runtime::interpreter::INTBackend::call(shared_ptr<Function> function, ...@@ -209,7 +209,7 @@ bool runtime::interpreter::INTBackend::call(shared_ptr<Function> function,
{ {
for (auto it = tensor_map.begin(); it != tensor_map.end(); ++it) for (auto it = tensor_map.begin(); it != tensor_map.end(); ++it)
{ {
if (it->second->get_tensor().get_name() == t->get_name()) if (it->second->get_name() == t->get_name())
{ {
tensor_map.erase(it); tensor_map.erase(it);
break; break;
......
...@@ -21,16 +21,6 @@ ...@@ -21,16 +21,6 @@
using namespace ngraph; using namespace ngraph;
using namespace std; using namespace std;
shared_ptr<const descriptor::TensorView> runtime::TensorView::get_descriptor() const
{
return m_descriptor;
}
shared_ptr<descriptor::TensorView> runtime::TensorView::get_descriptor()
{
return m_descriptor;
}
const Shape& runtime::TensorView::get_shape() const const Shape& runtime::TensorView::get_shape() const
{ {
return m_descriptor->get_shape(); return m_descriptor->get_shape();
...@@ -41,22 +31,43 @@ Strides runtime::TensorView::get_strides() const ...@@ -41,22 +31,43 @@ Strides runtime::TensorView::get_strides() const
return m_descriptor->get_tensor_layout()->get_strides(); return m_descriptor->get_tensor_layout()->get_strides();
} }
const element::Type& runtime::TensorView::get_element_type() const
{
return m_descriptor->get_element_type();
}
shared_ptr<descriptor::layout::TensorLayout> runtime::TensorView::get_tensor_layout() const shared_ptr<descriptor::layout::TensorLayout> runtime::TensorView::get_tensor_layout() const
{ {
return m_descriptor->get_tensor_layout(); return m_descriptor->get_tensor_layout();
} }
void runtime::TensorView::set_tensor_layout(
const shared_ptr<descriptor::layout::TensorLayout>& layout)
{
m_descriptor->set_tensor_layout(layout);
}
size_t runtime::TensorView::get_element_count() const size_t runtime::TensorView::get_element_count() const
{ {
size_t rc = 1; return get_tensor_layout()->get_size();
for (size_t s : get_shape()) }
{
rc *= s; size_t runtime::TensorView::get_size_in_bytes() const
} {
return rc; return get_tensor_layout()->get_size() * get_element_type().size();
}
const std::string& runtime::TensorView::get_name() const
{
return m_descriptor->get_name();
}
bool runtime::TensorView::get_stale() const
{
return m_stale;
} }
const descriptor::Tensor& runtime::TensorView::get_tensor() const void runtime::TensorView::set_stale(bool val)
{ {
return *get_descriptor(); m_stale = val;
} }
...@@ -46,30 +46,58 @@ namespace ngraph ...@@ -46,30 +46,58 @@ namespace ngraph
virtual ~TensorView() {} virtual ~TensorView() {}
TensorView& operator=(const TensorView&) = default; TensorView& operator=(const TensorView&) = default;
virtual std::shared_ptr<const ngraph::descriptor::Tensor> get_descriptor() const; /// \brief Get tensor shape
/// \return const reference to a Shape
virtual std::shared_ptr<descriptor::Tensor> get_descriptor();
const ngraph::Shape& get_shape() const; const ngraph::Shape& get_shape() const;
/// \brief Get tensor strides
/// \return Strides
ngraph::Strides get_strides() const; ngraph::Strides get_strides() const;
/// \brief Get tensor element type
/// \return element::Type
const element::Type& get_element_type() const;
/// \brief Get number of elements in the tensor
/// \return number of elements in the tensor
size_t get_element_count() const; size_t get_element_count() const;
const ngraph::descriptor::Tensor& get_tensor() const;
std::shared_ptr<ngraph::descriptor::layout::TensorLayout> get_tensor_layout() const; /// \brief Get the size in bytes of the tensor
/// \return number of bytes in tensor's allocation
size_t get_size_in_bytes() const;
/// \brief Get tensor's unique name
/// \return tensor's name
const std::string& get_name() const;
/// \brief Get tensor layout
/// \return tensor layout
std::shared_ptr<descriptor::layout::TensorLayout> get_tensor_layout() const;
/// \brief Set tensor layout
/// \param layout Layout to set
void set_tensor_layout(const std::shared_ptr<descriptor::layout::TensorLayout>& layout);
/// \brief Get the stale value of the tensor. A tensor is stale if it's data is
/// changed.
/// \return true if there is new data in this tensor
bool get_stale() const;
/// \brief Set the stale value of the tensor. A tensor is stale if it's data is
/// changed.
void set_stale(bool val);
bool get_stale() { return m_stale; }
void set_stale(bool val) { m_stale = val; }
/// \brief Write bytes directly into the tensor /// \brief Write bytes directly into the tensor
/// \param p Pointer to source of data /// \param p Pointer to source of data
/// \param tensor_offset Offset into tensor storage to begin writing. Must be element-aligned. /// \param 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. /// \param n Number of bytes to write, must be integral number of elements.
virtual void write(const void* p, size_t tensor_offset, size_t n) = 0; virtual void write(const void* p, size_t offset, size_t n) = 0;
/// \brief Read bytes directly from the tensor /// \brief Read bytes directly from the tensor
/// \param p Pointer to destination for data /// \param p Pointer to destination for data
/// \param tensor_offset Offset into tensor storage to begin reading. Must be element-aligned. /// \param offset Offset into tensor storage to begin writing. Must be element-aligned.
/// \param n Number of bytes to read, must be integral number of elements. /// \param n Number of bytes to read, must be integral number of elements.
virtual void read(void* p, size_t tensor_offset, size_t n) const = 0; virtual void read(void* p, size_t offset, size_t n) const = 0;
protected: protected:
std::shared_ptr<ngraph::descriptor::Tensor> m_descriptor; std::shared_ptr<ngraph::descriptor::Tensor> m_descriptor;
......
...@@ -97,7 +97,7 @@ void init_real_tv(shared_ptr<runtime::TensorView> tv, T min, T max) ...@@ -97,7 +97,7 @@ void init_real_tv(shared_ptr<runtime::TensorView> tv, T min, T max)
static void random_init(shared_ptr<runtime::TensorView> tv) static void random_init(shared_ptr<runtime::TensorView> tv)
{ {
element::Type et = tv->get_tensor().get_element_type(); element::Type et = tv->get_element_type();
if (et == element::boolean) if (et == element::boolean)
{ {
init_int_tv<char>(tv, 0, 1); init_int_tv<char>(tv, 0, 1);
...@@ -218,7 +218,7 @@ vector<runtime::PerformanceCounter> run_benchmark(shared_ptr<Function> f, ...@@ -218,7 +218,7 @@ vector<runtime::PerformanceCounter> run_benchmark(shared_ptr<Function> f,
const shared_ptr<runtime::HostTensorView>& data = arg_data[arg_index]; const shared_ptr<runtime::HostTensorView>& data = arg_data[arg_index];
arg->write(data->get_data_ptr(), arg->write(data->get_data_ptr(),
0, 0,
data->get_size() * data->get_element_type().size()); data->get_element_count() * data->get_element_type().size());
} }
} }
} }
...@@ -229,8 +229,9 @@ vector<runtime::PerformanceCounter> run_benchmark(shared_ptr<Function> f, ...@@ -229,8 +229,9 @@ vector<runtime::PerformanceCounter> run_benchmark(shared_ptr<Function> f,
{ {
const shared_ptr<runtime::HostTensorView>& data = result_data[result_index]; const shared_ptr<runtime::HostTensorView>& data = result_data[result_index];
const shared_ptr<runtime::TensorView>& result = results[result_index]; const shared_ptr<runtime::TensorView>& result = results[result_index];
result->read( result->read(data->get_data_ptr(),
data->get_data_ptr(), 0, data->get_size() * data->get_element_type().size()); 0,
data->get_element_count() * data->get_element_type().size());
} }
} }
} }
......
...@@ -40,11 +40,11 @@ bool autodiff_numeric_compare(const std::shared_ptr<ngraph::runtime::Backend>& b ...@@ -40,11 +40,11 @@ bool autodiff_numeric_compare(const std::shared_ptr<ngraph::runtime::Backend>& b
std::vector<std::shared_ptr<ngraph::runtime::TensorView>> interpreter_args; std::vector<std::shared_ptr<ngraph::runtime::TensorView>> interpreter_args;
for (auto arg : args) for (auto arg : args)
{ {
auto interpreter_arg = interpreter_backend->create_tensor( auto interpreter_arg =
arg->get_tensor().get_element_type(), arg->get_shape()); interpreter_backend->create_tensor(arg->get_element_type(), arg->get_shape());
// TODO: copy_data should not require T. Quick fix here for bool used in `Select` // TODO: copy_data should not require T. Quick fix here for bool used in `Select`
if (arg->get_tensor().get_element_type() == ngraph::element::boolean) if (arg->get_element_type() == ngraph::element::boolean)
{ {
copy_data(interpreter_arg, read_vector<char>(arg)); copy_data(interpreter_arg, read_vector<char>(arg));
} }
...@@ -113,11 +113,11 @@ bool autodiff_numeric_compare_selective( ...@@ -113,11 +113,11 @@ bool autodiff_numeric_compare_selective(
std::vector<std::shared_ptr<ngraph::runtime::TensorView>> interpreter_args; std::vector<std::shared_ptr<ngraph::runtime::TensorView>> interpreter_args;
for (auto arg : args) for (auto arg : args)
{ {
auto interpreter_arg = interpreter_backend->create_tensor( auto interpreter_arg =
arg->get_tensor().get_element_type(), arg->get_shape()); interpreter_backend->create_tensor(arg->get_element_type(), arg->get_shape());
// TODO: copy_data should not require T. Quick fix here for bool used in `Select` // TODO: copy_data should not require T. Quick fix here for bool used in `Select`
if (arg->get_tensor().get_element_type() == ngraph::element::boolean) if (arg->get_element_type() == ngraph::element::boolean)
{ {
copy_data(interpreter_arg, read_vector<char>(arg)); copy_data(interpreter_arg, read_vector<char>(arg));
} }
......
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