1 /** 2 * Copyright 2024 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_CCSRC_COMMON_PYNATIVE_ABSTRACT_CONVERTER_H 18 #define MINDSPORE_CCSRC_COMMON_PYNATIVE_ABSTRACT_CONVERTER_H 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include <utility> 24 #include "include/common/visible.h" 25 #include "include/common/pynative/ring_buffer.h" 26 #include "mindspore/core/ir/base_tensor.h" 27 28 namespace mindspore { 29 namespace pynative { 30 using BaseTensor = tensor::BaseTensor; 31 using BaseTensorPtr = tensor::BaseTensorPtr; 32 // For get abstract from value and cache abstract 33 constexpr size_t kAbstractCacheSize = 8192; 34 class COMMON_EXPORT AbstractConverter { 35 public: 36 using AbstractCache = kernel::pyboost::RingBuffer<AbstractBasePtr, kAbstractCacheSize>; 37 void CacheAbstract(const AbstractBasePtr &abstract); 38 AbstractBasePtr ConvertAbstract(const ValuePtr &t); 39 // Tensor is held by Abstract, may lead to memory leak. 40 AbstractBasePtr ConvertAbstract(const BaseTensorPtr &t); 41 AbstractBasePtr ConvertAbstract(const ValueTuplePtr &t); 42 43 template <typename T> ConvertAbstract(const std::optional<T> & t)44 AbstractBasePtr ConvertAbstract(const std::optional<T> &t) { 45 if (!t.has_value()) { 46 return kNone->ToAbstract(); 47 } 48 return ConvertAbstract(t.value()); 49 } 50 51 private: 52 AbstractCache abstract_cache_; 53 }; 54 } // namespace pynative 55 } // namespace mindspore 56 57 #endif // MINDSPORE_CCSRC_COMMON_PYNATIVE_ABSTRACT_CONVERTER_H 58