tensor.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "ngraph/descriptor/tensor.hpp"
23 #include "ngraph/shape.hpp"
24 #include "ngraph/strides.hpp"
25 #include "ngraph/type/element_type.hpp"
26 
27 namespace ngraph
28 {
29  namespace runtime
30  {
31  class NGRAPH_API Tensor
32  {
33  protected:
34  Tensor(const std::shared_ptr<ngraph::descriptor::Tensor>& descriptor)
35  : m_descriptor(descriptor)
36  , m_stale(true)
37  {
38  }
39 
40  public:
41  virtual ~Tensor() {}
42  Tensor& operator=(const Tensor&) = default;
43 
44  /// \brief Get tensor shape
45  /// \return const reference to a Shape
46  virtual const ngraph::Shape& get_shape() const;
47 
48  /// \brief Get tensor partial shape
49  /// \return const reference to a PartialShape
51 
52  /// \brief Get tensor element type
53  /// \return element::Type
54  virtual const element::Type& get_element_type() const;
55 
56  /// \brief Get number of elements in the tensor
57  /// \return number of elements in the tensor
58  virtual size_t get_element_count() const;
59 
60  /// \brief Get the size in bytes of the tensor
61  /// \return number of bytes in tensor's allocation
62  virtual size_t get_size_in_bytes() const;
63 
64  /// \brief Get tensor's unique name
65  /// \return tensor's name
66  const std::string& get_name() const;
67 
68  /// \brief Get the stale value of the tensor. A tensor is stale if its data is
69  /// changed.
70  /// \return true if there is new data in this tensor
71  bool get_stale() const;
72 
73  /// \brief Set the stale value of the tensor. A tensor is stale if its data is
74  /// changed.
75  void set_stale(bool val);
76 
77  /// \brief Write bytes directly into the tensor
78  /// \param p Pointer to source of data
79  /// \param n Number of bytes to write, must be integral number of elements.
80  virtual void write(const void* p, size_t n) = 0;
81 
82  /// \brief Read bytes directly from the tensor
83  /// \param p Pointer to destination for data
84  /// \param n Number of bytes to read, must be integral number of elements.
85  virtual void read(void* p, size_t n) const = 0;
86 
87  /// \brief check tensor for new data, call may block.
88  /// backends may use this to ensure tensor is updated (eg: lazy eval).
89  virtual void wait_for_read_ready() {}
90  /// \brief notify tensor of new data, call may block.
91  /// backends may use this as indication of new data in tensor.
92  virtual void wait_for_write_ready() {}
93  protected:
94  std::shared_ptr<ngraph::descriptor::Tensor> m_descriptor;
95  bool m_stale;
96  };
97  }
98 }
ngraph::runtime::Tensor::get_element_type
virtual const element::Type & get_element_type() const
Get tensor element type.
ngraph::runtime::Tensor::wait_for_write_ready
virtual void wait_for_write_ready()
notify tensor of new data, call may block. backends may use this as indication of new data in tensor.
Definition: tensor.hpp:92
ngraph::runtime::Tensor::get_shape
virtual const ngraph::Shape & get_shape() const
Get tensor shape.
ngraph::PartialShape
Class representing a shape that may be partially or totally dynamic.
Definition: partial_shape.hpp:46
ngraph::runtime::Tensor::wait_for_read_ready
virtual void wait_for_read_ready()
check tensor for new data, call may block. backends may use this to ensure tensor is updated (eg: laz...
Definition: tensor.hpp:89
ngraph::runtime::Tensor::read
virtual void read(void *p, size_t n) const =0
Read bytes directly from the tensor.
ngraph::runtime::Tensor::get_name
const std::string & get_name() const
Get tensor's unique name.
ngraph::runtime::Tensor::get_stale
bool get_stale() const
Get the stale value of the tensor. A tensor is stale if its data is changed.
ngraph::element::Type
Definition: element_type.hpp:61
ngraph::Shape
Shape for a tensor.
Definition: shape.hpp:31
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::runtime::Tensor::get_element_count
virtual size_t get_element_count() const
Get number of elements in the tensor.
ngraph::runtime::Tensor
Definition: tensor.hpp:32
ngraph::runtime::Tensor::get_size_in_bytes
virtual size_t get_size_in_bytes() const
Get the size in bytes of the tensor.
ngraph::runtime::Tensor::write
virtual void write(const void *p, size_t n)=0
Write bytes directly into the tensor.
ngraph::runtime::Tensor::set_stale
void set_stale(bool val)
Set the stale value of the tensor. A tensor is stale if its data is changed.
ngraph::runtime::Tensor::get_partial_shape
const ngraph::PartialShape & get_partial_shape() const
Get tensor partial shape.