1 /** 2 * Copyright 2022 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_MINDSPORE_CCSRC_PIPELINE_PYNATIVE_FORWARD_CAST_BASE_H_H_ 18 #define MINDSPORE_MINDSPORE_CCSRC_PIPELINE_PYNATIVE_FORWARD_CAST_BASE_H_H_ 19 20 #include <string> 21 #include <vector> 22 #include <utility> 23 #include "pipeline/pynative/base.h" 24 #include "pipeline/pynative/pynative_cache.h" 25 26 namespace mindspore { 27 namespace pynative { 28 class CastBaseOperation { 29 public: CastBaseOperation()30 CastBaseOperation() { 31 type_prim_cache_.reserve(kDefaultContainerSize); 32 implicit_cast_map_.reserve(kDefaultContainerSize); 33 } 34 ~CastBaseOperation() = default; 35 36 protected: 37 PrimitivePtr GetPrimByTypeId(const TypeId &type_id) const; 38 bool GetSignatureType(const std::vector<Signature> &signatures, std::vector<SignatureEnumDType> *dtypes) const; 39 // Modify tensor data type, when op input source dtype is not tensor without dispatch cast op. 40 tensor::BaseTensorPtr TensorToDstDtypeValue(const ValuePtr &src_value, const TypeId &dst_type_id) const; 41 ValuePtr ScalarToDstDtypeValue(const ValuePtr &src_value, const std::pair<TypeId, bool> &dst_type) const; 42 43 mutable mindspore::HashMap<TypeId, PrimitivePtr> type_prim_cache_; 44 mutable ImplicitCastCache implicit_cast_map_; 45 }; 46 } // namespace pynative 47 } // namespace mindspore 48 49 #endif // MINDSPORE_MINDSPORE_CCSRC_PIPELINE_PYNATIVE_FORWARD_CAST_BASE_H_H_ 50