MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / parseReshape

Function parseReshape

parsers/caffe/caffeParser/opParsers/parseReshape.cpp:24–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22namespace nvcaffeparser1
23{
24ILayer* parseReshape(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::ReshapeParameter& p = msg.reshape_param();
32 Dims bottomDims = tensors[msg.bottom(0)]->getDimensions();
33 int axis = p.has_axis() ? p.axis() : 0;
34
35 const ::trtcaffe::BlobShape& shape = p.shape();
36 // Check that N (batch dim) is 0. TensorRT does not support reshape in batch dimension
37 if (network.hasImplicitBatchDimension() && (axis == 0) && (shape.dim(0) != 0))
38 {
39 std::cout << "Caffe Parser: Invalid reshape param. TensorRT does not support reshape in N (batch) dimension"
40 << std::endl;
41 return nullptr;
42 }
43
44 // Handle axis and dims parameters
45 int axStart = std::max(0, axis - 1);
46 int axEnd = p.has_num_axes()
47 ? std::max(0, axis - static_cast<int>(network.hasImplicitBatchDimension()) + p.num_axes())
48 : bottomDims.nbDims;
49 std::vector<int> reshapeDims;
50
51 reshapeDims.reserve(axStart);
52 for (int i = 0; i < axStart; i++)
53 {
54 reshapeDims.push_back(bottomDims.d[i]);
55 }
56
57 for (int i = 0; i < shape.dim_size(); i++)
58 {
59 // skip first 0 (batch)
60 if (network.hasImplicitBatchDimension() && axis == 0 && i == 0)
61 {
62 continue;
63 }
64 if (shape.dim(i) == 0)
65 {
66 // If there is no bottom dimension corresponding to the current axis, then the params are invalid
67 assert(static_cast<int>(reshapeDims.size()) <= bottomDims.nbDims);
68 reshapeDims.push_back(bottomDims.d[reshapeDims.size()]);
69 }
70 else
71 {
72 reshapeDims.push_back(shape.dim(i));
73 }
74 }
75
76 for (int i = axEnd; i < bottomDims.nbDims; i++)
77 {
78 reshapeDims.push_back(bottomDims.d[i]);
79 }
80
81 Dims topDims{};

Callers

nothing calls this directly

Calls 10

checkBlobsFunction · 0.85
maxFunction · 0.85
shapeMethod · 0.80
addShuffleMethod · 0.80
setReshapeDimensionsMethod · 0.80
volumeFunction · 0.50
getDimensionsMethod · 0.45
axisMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected