1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NEURAL_NETWORK_RUNTIME_NN_TENSOR_H 17 #define NEURAL_NETWORK_RUNTIME_NN_TENSOR_H 18 19 #include <string> 20 #include <vector> 21 22 #include "cpp_type.h" 23 #include "tensor_desc.h" 24 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime_type.h" 25 #include "interfaces/innerkits/c/neural_network_runtime_inner.h" 26 27 namespace OHOS { 28 namespace NeuralNetworkRuntime { 29 using LiteGraphTensorPtr = std::unique_ptr<void, void(*)(void*)>; 30 31 void DestroyLiteGraphTensor(void* tensor); 32 33 class NNTensor { 34 public: 35 NNTensor() = default; 36 ~NNTensor(); 37 NNTensor(NNTensor&& tensor) noexcept; 38 NNTensor& operator=(NNTensor&& tensor) noexcept; 39 // Copy construction and assignment is not allowed in case of double-free of m_buffer 40 NNTensor(const NNTensor& tensor) = delete; 41 NNTensor& operator=(const NNTensor& tensor) = delete; 42 43 OH_NN_ReturnCode BuildFromOHNNTensor(const OH_NN_Tensor& nnTensor); 44 OH_NN_ReturnCode BuildFromOHNNTensorInfo(const OH_NN_TensorInfo& nnTensorInfo); 45 OH_NN_ReturnCode BuildFromTensorDesc(const NN_TensorDesc* tensorDesc); 46 47 OH_NN_ReturnCode Build(OH_NN_DataType dataType, 48 const std::vector<int32_t>& dimensions, 49 const std::vector<QuantParam>& quantParams, 50 OH_NN_TensorType type); 51 void IdentifyOpParameter(); 52 53 void SetName(const std::string& name); 54 void SetBuffer(const void* buffer, size_t length); 55 void SetFormat(const OH_NN_Format& format); 56 OH_NN_ReturnCode SetDimensions(const std::vector<int32_t>& dimensions); 57 OH_NN_ReturnCode SetQuantParam(const NN_QuantParam* quantParam); 58 OH_NN_ReturnCode SetTensorType(OH_NN_TensorType tensorType); 59 60 std::string GetName() const; 61 OH_NN_TensorType GetType() const; 62 void* GetBuffer() const; 63 // Return complete buffer length 64 size_t GetBufferLength() const; 65 // Return actual data length, since the data can be store in a larger buffer 66 size_t GetDataLength() const; 67 OH_NN_DataType GetDataType() const; 68 uint32_t GetElementCount() const; 69 std::vector<int32_t> GetDimensions() const; 70 OH_NN_Format GetFormat() const; 71 std::vector<QuantParam> GetQuantParam() const; 72 LiteGraphTensorPtr ConvertToLiteGraphTensor() const; 73 void ConvertToIOTensor(IOTensor& tensor) const; 74 void ConvertToTensorDesc(TensorDesc& desc) const; 75 76 bool IsDynamicShape() const; 77 bool IsQuantTensor() const; 78 bool IsScalar() const; 79 bool IsOpParameter() const; 80 bool CompareAttribute(const NNTensor& tensor) const; 81 82 private: 83 OH_NN_ReturnCode ParseQuantParams(const OH_NN_QuantParam* quantParams); 84 OH_NN_ReturnCode ParseDimensions(const int32_t* dimensions, uint32_t dimensionCount); 85 OH_NN_ReturnCode ValidateQuantParams(const std::vector<QuantParam>& quantParams); 86 OH_NN_ReturnCode ValidateDimensions(const std::vector<int32_t>& dimensions); 87 88 private: 89 OH_NN_TensorType m_type {OH_NN_TENSOR}; 90 OH_NN_DataType m_dataType {OH_NN_FLOAT32}; 91 OH_NN_Format m_format {OH_NN_FORMAT_NHWC}; 92 std::string m_name; 93 std::vector<int32_t> m_dimensions; 94 std::vector<QuantParam> m_quantParams; 95 uint32_t m_elementCount {0}; 96 bool m_isDynamicShape {false}; 97 bool m_isOpParameter {false}; 98 void* m_buffer {nullptr}; 99 size_t m_bufferLength {0}; 100 size_t m_dataLength {0}; 101 }; 102 } // namespace NeuralNetworkRuntime 103 } // namespace OHOS 104 #endif // NEURAL_NETWORK_RUNTIME_NN_TENSOR_H