• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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_IR_KERNEL_TENSOR_VALUE_H_
18 #define MINDSPORE_CORE_IR_KERNEL_TENSOR_VALUE_H_
19 
20 #include <variant>
21 #include <utility>
22 #include <vector>
23 #include <string>
24 #include <memory>
25 #include "ir/value.h"
26 #include "ir/tensor.h"
27 
28 namespace mindspore {
29 // KernelTensorValue stores values in continuous memory and supports values of the types ValueSequence, Tensor, Scalar,
30 // and String.
31 class MS_CORE_API KernelTensorValue : public Value {
32  public:
33   // This constructor is used to construct the KernelTensorValue to hold the Tensor.
34   KernelTensorValue(const tensor::TensorDataPtr &tensor_data, const TypePtr &t);
35 
36   // This constructor is used to construct the KernelTensorValue to hold the ValueSequence.
37   KernelTensorValue(std::vector<uint8_t> &&array_data, const TypePtr &t);
38 
39   // This constructor is used to construct the KernelTensorValue to hold the String.
40   KernelTensorValue(const StringImmPtr &string, const TypePtr &t);
41 
42   // This constructor is used to construct the KernelTensorValue to hold the Scalar.
43   template <typename T, typename std::enable_if<std::is_scalar<std::decay_t<T>>::value>::type * = nullptr>
KernelTensorValue(T scalar,const TypePtr & t)44   KernelTensorValue(T scalar, const TypePtr &t) : Value(t) {
45     auto scalar_data = std::vector<uint8_t>(sizeof(T));
46     *reinterpret_cast<T *>(scalar_data.data()) = scalar;
47     const_data_ = std::move(scalar_data);
48     obj_type_id_ = kObjectTypeNumber;
49   }
50 
51   // This constructor is used to construct the mutable KernelTensorValue to hold any data type (such as Tensor,
52   // ValueSequence, String, Scalar) use continuous memory. This constructor only malloc raw memory to prepare store
53   // value data.
54   KernelTensorValue(size_t size, const TypePtr &t);
55 
56   // This constructor is used to construct the mutable KernelTensorValue to hold any data type (such as Tensor,
57   // ValueSequence, String, Scalar) use external const continuous memory(const void *).
58   KernelTensorValue(const void *data, size_t size, const TypePtr &t);
59 
60   ~KernelTensorValue() = default;
61 
62   MS_DECLARE_PARENT(KernelTensorValue, Value)
63 
64   bool operator==(const Value &other) const override;
65 
66   bool operator==(const KernelTensorValue &other) const;
67 
68   // Get the const address of the value stored in KernelTensorValue.
69   const void *GetDataPtr() const;
70 
71   // Get the mutable address of the value stored in KernelTensorValue.
72   void *GetMutableDataPtr();
73 
74   // Set external data to KernelTensorValue.
75   // Note: The caller needs to manage the life cycle of external values. This interface is used with the
76   // KernelTensorValue(const void *data, size_t size, const TypePtr &t) constructor. There is no need to copy external
77   // values to KernelTensorValue to improve performance.
78   void SetDataPtr(const void *data_ptr);
79 
80   // Get the buffer size in bytes of the value stored in KernelTensorValue.
81   size_t GetDataSize() const;
82 
83   // Change the continuous memory size.
84   void Resize(size_t size);
85 
86  private:
87   // Used to store values in continuous memory for the types ValueSequence, Tensor, Scalar and String.
88   // The const_data_ is read-only after assignment.
89   std::variant<std::vector<uint8_t>, tensor::TensorDataPtr, StringImmPtr> const_data_;
90 
91   // The object type id for the types ValueSequence, Tensor, Scalar and String.
92   TypeId obj_type_id_{TypeId::kTypeUnknown};
93 
94   // Used to store values in continuous memory for the types ValueSequence, Tensor, Scalar and String.
95   // The mutable_data_ that uses shared pointer one can be read, written and changed size after assignment.
96   std::variant<std::shared_ptr<uint8_t[]>, const void *> mutable_data_;
97 
98   // The value size in bytes.
99   size_t size_{0};
100 
101   // Whether use mutable_data_ to store any type value.
102   bool use_mutable_storage_{false};
103 };
104 
105 using KernelTensorValuePtr = std::shared_ptr<KernelTensorValue>;
106 }  // namespace mindspore
107 
108 #endif  // MINDSPORE_CORE_IR_KERNEL_TENSOR_VALUE_H_
109