tensor.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 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  NGRAPH_DEPRECATED("Only output ports have names")
67  const std::string& get_name() const;
68 
69  /// \brief Get the stale value of the tensor. A tensor is stale if its data is
70  /// changed.
71  /// \return true if there is new data in this tensor
72  bool get_stale() const;
73 
74  /// \brief Set the stale value of the tensor. A tensor is stale if its data is
75  /// changed.
76  void set_stale(bool val);
77 
78  /// \brief Write bytes directly into the tensor
79  /// \param p Pointer to source of data
80  /// \param n Number of bytes to write, must be integral number of elements.
81  virtual void write(const void* p, size_t n) = 0;
82 
83  /// \brief Read bytes directly from the tensor
84  /// \param p Pointer to destination for data
85  /// \param n Number of bytes to read, must be integral number of elements.
86  virtual void read(void* p, size_t n) const = 0;
87 
88  /// \brief check tensor for new data, call may block.
89  /// backends may use this to ensure tensor is updated (eg: lazy eval).
90  virtual void wait_for_read_ready() {}
91  /// \brief notify tensor of new data, call may block.
92  /// backends may use this as indication of new data in tensor.
93  virtual void wait_for_write_ready() {}
94  protected:
95  std::shared_ptr<ngraph::descriptor::Tensor> m_descriptor;
96  bool m_stale;
97  };
98  }
99 }
Class representing a shape that may be partially or totally dynamic.
Definition: partial_shape.hpp:46
Shape for a tensor.
Definition: shape.hpp:31
Definition: element_type.hpp:61
Definition: tensor.hpp:32
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:93
const ngraph::PartialShape & get_partial_shape() const
Get tensor partial shape.
virtual size_t get_size_in_bytes() const
Get the size in bytes of the tensor.
virtual const element::Type & get_element_type() const
Get tensor element type.
virtual size_t get_element_count() const
Get number of elements in the tensor.
virtual const ngraph::Shape & get_shape() const
Get tensor shape.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28