1 /** 2 * Copyright 2021-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 #ifndef MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_CAST_PLUGIN_H_ 17 #define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_CAST_PLUGIN_H_ 18 #include <string> 19 #include <vector> 20 #include "src/extendrt/delegate/tensorrt/op/tensorrt_plugin.h" 21 22 namespace mindspore::lite { 23 constexpr auto CAST_PLUGIN_NAME{"CastPluginCreater"}; 24 class CastPlugin : public TensorRTPlugin { 25 public: 26 CastPlugin(const std::string &name, nvinfer1::DataType dest_datatype, uint32_t device_id = 0) TensorRTPlugin(name,std::string (CAST_PLUGIN_NAME),device_id)27 : TensorRTPlugin(name, std::string(CAST_PLUGIN_NAME), device_id), dest_datatype_(dest_datatype) {} 28 CastPlugin(const char * name,const nvinfer1::PluginFieldCollection * fc)29 CastPlugin(const char *name, const nvinfer1::PluginFieldCollection *fc) 30 : TensorRTPlugin(std::string(name), std::string(CAST_PLUGIN_NAME)) { 31 const nvinfer1::PluginField *fields = fc->fields; 32 dest_datatype_ = static_cast<const nvinfer1::DataType *>(fields[0].data)[0]; 33 } 34 CastPlugin(const char * name,const void * serialData,size_t serialLength)35 CastPlugin(const char *name, const void *serialData, size_t serialLength) 36 : TensorRTPlugin(std::string(name), std::string(CAST_PLUGIN_NAME)) { 37 DeserializeValue(&serialData, &serialLength, &dest_datatype_, sizeof(nvinfer1::DataType)); 38 } 39 40 CastPlugin() = delete; 41 42 nvinfer1::IPluginV2DynamicExt *clone() const noexcept override; 43 44 int enqueue(const nvinfer1::PluginTensorDesc *inputDesc, const nvinfer1::PluginTensorDesc *outputDesc, 45 const void *const *inputs, void *const *outputs, void *workspace, cudaStream_t stream) noexcept override; 46 47 nvinfer1::DataType getOutputDataType(int, const nvinfer1::DataType *, int) const noexcept override; 48 49 size_t getSerializationSize() const noexcept override; 50 void serialize(void *buffer) const noexcept override; 51 52 private: 53 nvinfer1::DataType dest_datatype_; 54 }; 55 class CastPluginCreater : public TensorRTPluginCreater<CastPlugin> { 56 public: CastPluginCreater()57 CastPluginCreater() : TensorRTPluginCreater(std::string(CAST_PLUGIN_NAME)) {} 58 }; 59 } // namespace mindspore::lite 60 #endif // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_CAST_PLUGIN_H_ 61