• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-2022 Huawei Technologies Co., Ltd
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 #ifndef MINDSPORE_CORE_MINDAPI_IR_TENSOR_H_
18 #define MINDSPORE_CORE_MINDAPI_IR_TENSOR_H_
19 
20 #include <cstdint>
21 #include "mindapi/base/base.h"
22 #include "mindapi/base/shape_vector.h"
23 #include "mindapi/base/type_id.h"
24 #include "mindapi/ir/common.h"
25 #include "mindapi/ir/value.h"
26 
27 namespace mindspore::api {
28 /// \brief Tensor represents a multi-dimensional array of elements.
29 class MIND_API Tensor : public Value {
30  public:
31   MIND_API_BASE_MEMBER(Tensor);
32 
33   /// \brief Create a lazy allocated tensor.
34   ///
35   /// \param[in] data_type [TypeId] Data type of the tensor.
36   /// \param[in] shape The shape represented by ShapeVector of the tensor.
37   Tensor(TypeId data_type, const ShapeVector &shape);
38 
39   /// \brief Create a tensor with input data buffer.
40   ///
41   /// \param[in] data_type [TypeId] Data type of the tensor.
42   /// \param[in] shape The shape represented by ShapeVector of the tensor.
43   /// \param[in] data The input data to be copied into tensor.
44   /// \param[in] data_len The length of data in bytes.
45   Tensor(TypeId data_type, const ShapeVector &shape, void *data, size_t data_len);
46 
47   /// \brief Get the shape of the tensor.
48   /// The shape of a tensor is stored in a vector<int64_t>. Each element of the
49   /// vector represents the size of a dimension of the tensor. The order of each
50   /// element in the vector is the same as the the dimension's order it represents.
51   ///
52   /// \return A vector<int64_t> which represents the shape of the tensor.
53   const ShapeVector &shape() const;
54 
55   /// \brief Set the shape of tensor.
56   ///
57   /// \param[in] shape The shape to be set.
58   void set_shape(const ShapeVector &shape);
59 
60   /// \brief Get the data type of the tensor.
61   ///
62   /// \return The data type of the tensor.
63   TypeId data_type() const;
64 
65   /// \brief Set the data type of the tensor.
66   ///
67   /// \param[in] data_type The data type to be set.
68   void set_data_type(const TypeId data_type);
69 
70   /// \brief Get The pointer to the underlying memory block for data storage.
71   ///
72   /// \return The pointer to the underlying data.
73   const void *data() const;
74 
75   /// \brief Get The pointer to the underlying memory block for data storage.
76   ///
77   /// \return The pointer to the underlying data.
78   void *data();
79 
80   /// \brief Get tensor data size.
81   ///
82   /// \return The total number of elements in the tensor.
83   size_t DataSize() const;
84 
85   /// \brief Get tensor data size in bytes.
86   ///
87   /// \return The total number of bytes for the tensor data.
88   std::size_t Size() const;
89 };
90 
91 using TensorPtr = SharedPtr<Tensor>;
92 }  // namespace mindspore::api
93 #endif  // MINDSPORE_CORE_MINDAPI_IR_TENSOR_H_
94