Apollo  6.0
Open source self driving car software
softmax_plugin.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 #pragma once
18 
20 
21 namespace apollo {
22 namespace perception {
23 namespace inference {
24 
25 class SoftmaxPlugin : public nvinfer1::IPlugin {
26  public:
27  SoftmaxPlugin(const SoftmaxParameter &param, nvinfer1::Dims in_dims) {
28  input_dims_.nbDims = in_dims.nbDims;
29  for (int i = 0; i < in_dims.nbDims; i++) {
30  input_dims_.d[i] = in_dims.d[i];
31  input_dims_.type[i] = in_dims.type[i];
32  }
33  axis_ = param.axis() - 1;
34  CHECK_GE(axis_, 0);
35  CHECK_LE(axis_ + 1, input_dims_.nbDims);
36 
37  inner_num_ = 1;
38  for (int i = axis_ + 1; i < input_dims_.nbDims; i++) {
39  inner_num_ *= input_dims_.d[i];
40  }
41  outer_num_ = 1;
42  for (int i = 0; i < axis_; i++) {
43  outer_num_ *= input_dims_.d[i];
44  }
45  cudnnCreateTensorDescriptor(&input_desc_);
46  cudnnCreateTensorDescriptor(&output_desc_);
47  }
48 
50 
52  cudnnDestroyTensorDescriptor(input_desc_);
53  cudnnDestroyTensorDescriptor(output_desc_);
54  }
55  virtual int initialize() {
56  cudnnCreate(&cudnn_); // initialize cudnn and cublas
57  cublasCreate(&cublas_);
58  return 0;
59  }
60  virtual void terminate() {
61  cublasDestroy(cublas_);
62  cudnnDestroy(cudnn_);
63  }
64  int getNbOutputs() const override { return 1; }
65 
66  nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
67  int nbInputDims) override {
68  nvinfer1::Dims out_dims = inputs[0];
69  return out_dims;
70  }
71 
72  void configure(const nvinfer1::Dims *inputDims, int nbInputs,
73  const nvinfer1::Dims *outputDims, int nbOutputs,
74  int maxBatchSize) override {
75  input_dims_ = inputDims[0];
76  }
77 
78  size_t getWorkspaceSize(int maxBatchSize) const override { return 0; }
79 
80  int enqueue(int batchSize, const void *const *inputs, void **outputs,
81  void *workspace, cudaStream_t stream) override;
82 
83  size_t getSerializationSize() override { return 0; }
84 
85  void serialize(void *buffer) override {
86  char *d = reinterpret_cast<char *>(buffer), *a = d;
87  size_t size = getSerializationSize();
88  CHECK_EQ(d, a + size);
89  }
90 
91  private:
92  cudnnHandle_t cudnn_;
93  cublasHandle_t cublas_;
94  nvinfer1::Dims input_dims_;
95  int axis_;
96  int inner_num_;
97  int outer_num_;
98  cudnnTensorDescriptor_t input_desc_;
99  cudnnTensorDescriptor_t output_desc_;
100 };
101 
102 } // namespace inference
103 } // namespace perception
104 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: softmax_plugin.h:25
SoftmaxPlugin()
Definition: softmax_plugin.h:49
nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs, int nbInputDims) override
Definition: softmax_plugin.h:66
size_t getWorkspaceSize(int maxBatchSize) const override
Definition: softmax_plugin.h:78
virtual void terminate()
Definition: softmax_plugin.h:60
int getNbOutputs() const override
Definition: softmax_plugin.h:64
size_t getSerializationSize() override
Definition: softmax_plugin.h:83
void configure(const nvinfer1::Dims *inputDims, int nbInputs, const nvinfer1::Dims *outputDims, int nbOutputs, int maxBatchSize) override
Definition: softmax_plugin.h:72
SoftmaxPlugin(const SoftmaxParameter &param, nvinfer1::Dims in_dims)
Definition: softmax_plugin.h:27
void serialize(void *buffer) override
Definition: softmax_plugin.h:85
int enqueue(int batchSize, const void *const *inputs, void **outputs, void *workspace, cudaStream_t stream) override
~SoftmaxPlugin()
Definition: softmax_plugin.h:51
virtual int initialize()
Definition: softmax_plugin.h:55