1 /*
2  * Copyright (c) 2023 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_NNTENSOR_H
17 #define NEURAL_NETWORK_RUNTIME_NNTENSOR_H
18 
19 #include <memory>
20 #include "tensor.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 class NNTensor2_0 : public Tensor {
25 public:
NNTensor2_0(size_t backendID)26     explicit NNTensor2_0(size_t backendID) : m_backendID(backendID) {}
27     ~NNTensor2_0() override;
28 
29     OH_NN_ReturnCode SetTensorDesc(const TensorDesc* tensorDesc) override;
30     OH_NN_ReturnCode CreateData() override;
31     OH_NN_ReturnCode CreateData(size_t size) override;
32     OH_NN_ReturnCode CreateData(int fd, size_t size, size_t offset) override;
33 
34     TensorDesc* GetTensorDesc() const override;
35     void* GetData() const override;
36     int GetFd() const override;
37     size_t GetSize() const override;
38     size_t GetOffset() const override;
39     void SetData(void* data);
40     void SetFd(int fd);
41     void SetSize(size_t size);
42     void SetOffset(size_t offset);
43     size_t GetBackendID() const override;
44 
45     bool CheckTensorData() const;
46 
47     OH_NN_ReturnCode CheckDimRanges(const std::vector<uint32_t>& minDimRanges,
48                                     const std::vector<uint32_t>& maxDimRanges) const;
49 
50 private:
51     OH_NN_ReturnCode AllocateMemory(size_t length);
52     OH_NN_ReturnCode ReleaseMemory();
53 
54 private:
55     size_t m_backendID {0};
56     TensorDesc* m_tensorDesc {nullptr};
57     void* m_data {nullptr};
58     int m_fd {0};
59     size_t m_size {0};
60     size_t m_offset {0};
61     bool m_isUserData {false};
62 };
63 }  // namespace NeuralNetworkRuntime
64 }  // namespace OHOS
65 #endif  // NEURAL_NETWORK_RUNTIME_NNTENSOR_H