| 22 | namespace nvcaffeparser1 |
| 23 | { |
| 24 | ILayer* parseSoftMax(INetworkDefinition& network, const trtcaffe::LayerParameter& msg, CaffeWeightFactory& /*weightFactory*/, BlobNameToTensor& tensors) |
| 25 | { |
| 26 | if (!checkBlobs(msg, 1, 1)) |
| 27 | { |
| 28 | return nullptr; |
| 29 | } |
| 30 | |
| 31 | const trtcaffe::SoftmaxParameter& p = msg.softmax_param(); |
| 32 | |
| 33 | // Caffe supports negative axis, indexing from the last dimension |
| 34 | // However, there is a discrepancy in the internal tensor dimension in some cases. |
| 35 | // For example. InnerProduct produces flat 1D blob in Caffe, while TensorRT still |
| 36 | // produces CHW format. MNIST sample generates input to Softmax as, |
| 37 | // Caffe = n x 10 |
| 38 | // TensorRT = n x 10 x 1 x 1 |
| 39 | // To make sure we do not run into issues, negative axis won't be supported in TensorRT |
| 40 | int nbDims = tensors[msg.bottom(0)]->getDimensions().nbDims; |
| 41 | bool hasAxis = p.has_axis(); // optional parameter |
| 42 | int axis = hasAxis ? p.axis() : 1; // default is 1 |
| 43 | |
| 44 | if (network.hasImplicitBatchDimension() && axis == 0) |
| 45 | { |
| 46 | std::cout << "Caffe Parser: Invalid axis in softmax layer - TensorRT does not support softmax across the batch " |
| 47 | "axis with implicit batch dimensions networks." |
| 48 | << std::endl; |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | if (axis < 0 || axis > 3 || (axis > nbDims)) |
| 53 | { |
| 54 | std::cout << "Caffe Parser: Invalid axis in softmax layer - TensorRT expects NCHW input. Negative axis is not " |
| 55 | "supported in TensorRT, please use positive axis indexing" |
| 56 | << std::endl; |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | auto softmax = network.addSoftMax(*tensors[msg.bottom(0)]); |
| 61 | // Do this so that setAxes is not used when the default axis is needed |
| 62 | // This is necessary to preserve correct roll-into-the-batch dimension behaviour for samples like FasterRCNN |
| 63 | // NCHW -> default axis when setAxes is not called will be 1 (the C dimension) |
| 64 | // NPCHW -> default axis when setAxes is not called will be 2 (the C dimension) |
| 65 | if (hasAxis) |
| 66 | { |
| 67 | uint32_t axes = 1u << (axis - static_cast<int>(network.hasImplicitBatchDimension())); |
| 68 | softmax->setAxes(axes); |
| 69 | } |
| 70 | return softmax; |
| 71 | } |
| 72 | } //namespace nvcaffeparser1 |
nothing calls this directly
no test coverage detected