• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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_LITE_SRC_COMMON_MUTABLE_TESNOR_IMPL_H_
18 #define MINDSPORE_LITE_SRC_COMMON_MUTABLE_TESNOR_IMPL_H_
19 
20 #include <string>
21 #include <memory>
22 #include <vector>
23 #include "ir/api_tensor_impl.h"
24 
25 namespace mindspore {
26 class MutableTensorImpl : public MSTensor::Impl {
27  public:
28   virtual void SetName(const std::string &name) = 0;
29   virtual void SetDataType(mindspore::DataType data_type) = 0;
30   virtual void SetShape(const std::vector<int64_t> &shape) = 0;
31   virtual mindspore::Format Format() const = 0;
32   virtual void SetFormat(mindspore::Format format) = 0;
33   virtual void SetData(void *data, bool own_data) = 0;
34   virtual bool IsConst() const = 0;
35   virtual void SetAllocator(const std::shared_ptr<Allocator> &allocator) = 0;
36   virtual std::shared_ptr<Allocator> GetAllocator() const = 0;
37   virtual std::vector<QuantParam> GetQuantParams() const = 0;
38   virtual void SetQuantParams(const std::vector<QuantParam> &quant_param) = 0;
39   virtual void SetDeviceData(void *data) = 0;
40   virtual void *GetDeviceData() = 0;
41   virtual std::string GetDevice() const = 0;
42   virtual int GetDeviceId() const = 0;
43   virtual void SetDeviceId(int device_id) = 0;
44   virtual void SetDevice(const std::string &device) = 0;
ElementNum()45   virtual int64_t ElementNum() const {
46     const auto &shape = Shape();
47     int64_t ele_num = 1;
48     for (auto &dim : shape) {
49       if (dim < 0) {
50         return 0;
51       }
52 #if defined(ENABLE_CLOUD_FUSION_INFERENCE) || defined(ENABLE_CLOUD_INFERENCE)
53       if (INT64_MAX / ele_num < dim) {
54         MS_LOG(ERROR) << "The shape " << shape << " is invalid";
55         return 0;
56       }
57 #else
58       if (INT32_MAX / ele_num < dim) {
59         MS_LOG(ERROR) << "The shape " << shape << " is invalid";
60         return 0;
61       }
62 #endif
63       ele_num *= dim;
64     }
65     return ele_num;
66   }
67 };
68 using MutableTensorImplPtr = std::shared_ptr<MutableTensorImpl>;
69 }  // namespace mindspore
70 #endif  // MINDSPORE_LITE_SRC_COMMON_MUTABLE_TESNOR_IMPL_H_
71