From a56ace3be2cb8f385c8b0dc0ac31f32e7b3341d8 Mon Sep 17 00:00:00 2001 From: shira zaloshinki Date: Mon, 20 Feb 2023 14:25:23 +0000 Subject: [PATCH] add vsl_compress files --- .../tensorrt/op/vsl_compress_tensorrt.cc | 84 +++++++++++++++++++ .../tensorrt/op/vsl_compress_tensorrt.h | 62 ++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.cc create mode 100644 mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.h diff --git a/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.cc b/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.cc new file mode 100644 index 00000000000..1f067b0b300 --- /dev/null +++ b/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.cc @@ -0,0 +1,84 @@ +/** + * Copyright 2022 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.h" +#include +#include +#include +#include +#include +#include +#include +#include "NvInferRuntimeCommon.h" +#include "src/extendrt/delegate/tensorrt/tensorrt_utils.h" +#include "src/fastertransformer/utils/cuda_utils.h" +#include "src/fastertransformer/utils/allocator.h" +#include "src/fastertransformer/kernels/bert_preprocess_kernels.h" + +namespace mindspore::lite { +namespace { +constexpr std::size_t kTwo = 2; +} // namespace + +REGISTER_TENSORRT_PLUGIN(VslCompressPluginCreater); +template class TensorRTPluginCreater; +template +nvinfer1::PluginFieldCollection TensorRTPluginCreater::field_collection_{}; +template +std::vector TensorRTPluginCreater::fields_; + +int VslCompressPlugin::enqueue(const nvinfer1::PluginTensorDesc *inputDesc, + const nvinfer1::PluginTensorDesc *outputDesc, const void *const *inputs, + void *const *outputs, void *workspace, cudaStream_t stream) noexcept { + fastertransformer::invokeBuildSequenceLength(static_cast(inputs[0]), batch_size_, + static_cast(outputs[0]), seq_len_, stream); + return RET_OK; +} + +nvinfer1::IPluginV2DynamicExt *VslCompressPlugin::clone() const noexcept { + auto *plugin = new VslCompressPlugin(*this); + if (plugin == nullptr) { + MS_LOG(ERROR) << "plugin is null"; + return nullptr; + } + plugin->setPluginNamespace(name_space_.c_str()); + return plugin; +} + +nvinfer1::DimsExprs VslCompressPlugin::getOutputDimensions(int index, const nvinfer1::DimsExprs *inputs, + int nbInputDims, + nvinfer1::IExprBuilder &exprBuilder) noexcept { + nvinfer1::DimsExprs dims; + dims.nbDims = 1; + dims.d[0] = exprBuilder.constant(static_cast(inputs[0].d[0]->getConstantValue())); + return dims; +} + +void VslCompressPlugin::configurePlugin(const nvinfer1::DynamicPluginTensorDesc *in, int nbInputs, + const nvinfer1::DynamicPluginTensorDesc *out, int nbOutputs) noexcept { + batch_size_ = static_cast(in[0].desc.dims.d[0]); + seq_len_ = static_cast(in[0].desc.dims.d[1]); +} + +size_t VslCompressPlugin::getSerializationSize() const noexcept { + return 2*sizeof(int); +} + +void VslCompressPlugin::serialize(void *buffer) const noexcept { + SerializeValue(&buffer, &batch_size_, sizeof(int)); + SerializeValue(&buffer, &seq_len_, sizeof(int)); +} +} // namespace mindspore::lite diff --git a/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.h b/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.h new file mode 100644 index 00000000000..991e7c3a4a5 --- /dev/null +++ b/mindspore/lite/src/extendrt/delegate/tensorrt/op/vsl_compress_tensorrt.h @@ -0,0 +1,62 @@ +// * Copyright 2022 Huawei Technologies Co., Ltd +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ + +#ifndef MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_VSL_COMPRESS_TENSORRT_H_ +#define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_VSL_COMPRESS_TENSORRT_H_ + +#include +#include +#include "src/extendrt/delegate/tensorrt/op/tensorrt_op.h" +#include "src/extendrt/delegate/tensorrt/op/tensorrt_plugin.h" +#include "src/extendrt/delegate/tensorrt/cuda_impl/cudnn_utils.h" + +namespace mindspore::lite { + +constexpr auto VSL_COMPRESS_PLUGIN_NAME{"VslCompressPlugin"}; +class VslCompressPlugin : public TensorRTPlugin { + public: + VslCompressPlugin(const char *name, uint32_t device_id) + : TensorRTPlugin(name, std::string(VSL_COMPRESS_PLUGIN_NAME), device_id) {} + VslCompressPlugin(const char *name, const nvinfer1::PluginFieldCollection *fc) + : TensorRTPlugin(std::string(name), std::string(VSL_COMPRESS_PLUGIN_NAME)) {} + + VslCompressPlugin(const char *name, const void *serialData, size_t serialLength) + : TensorRTPlugin(std::string(name), std::string(VSL_COMPRESS_PLUGIN_NAME)) { + DeserializeValue(&serialData, &serialLength, &seq_len_, sizeof(int)); + DeserializeValue(&serialData, &serialLength, &batch_size_, sizeof(int)); + } + VslCompressPlugin() = delete; + + ~VslCompressPlugin() override {} + + int enqueue(const nvinfer1::PluginTensorDesc *inputDesc, const nvinfer1::PluginTensorDesc *outputDesc, + const void *const *inputs, void *const *outputs, void *workspace, cudaStream_t stream) noexcept override; + nvinfer1::DimsExprs getOutputDimensions(int index, const nvinfer1::DimsExprs *inputs, int nbInputDims, + nvinfer1::IExprBuilder &exprBuilder) noexcept override; + void configurePlugin(const nvinfer1::DynamicPluginTensorDesc *in, int nbInputs, + const nvinfer1::DynamicPluginTensorDesc *out, int nbOutputs) noexcept override; + nvinfer1::IPluginV2DynamicExt *clone() const noexcept override; + size_t getSerializationSize() const noexcept override; + void serialize(void *buffer) const noexcept override; + private: + int batch_size_; + int seq_len_; +}; +class VslCompressPluginCreater : public TensorRTPluginCreater { + public: + VslCompressPluginCreater() : TensorRTPluginCreater(std::string(VSL_COMPRESS_PLUGIN_NAME)) {} +}; +} // namespace mindspore::lite +#endif // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_OP_VSL_COMPRESS_TENSORRT_H_ -- Gitee