forked from NVIDIA/TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NvInfer.h
8780 lines (8177 loc) · 285 KB
/
NvInfer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* 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 NV_INFER_H
#define NV_INFER_H
#include "NvInferLegacyDims.h"
#include "NvInferRuntime.h"
//!
//! \mainpage
//!
//! This is the API documentation for the NVIDIA TensorRT library. It provides information on individual
//! functions, classes and methods. Use the index on the left to navigate the documentation.
//!
//! Please see the accompanying user guide and samples for higher-level information and general advice on
//! using TensorRT.
//
//! TensorRT Versioning follows Semantic Versioning Guidelines specified here: https://semver.org/
//!
//!
//! \file NvInfer.h
//!
//! This is the top-level API file for TensorRT.
//!
//!
//! \namespace nvinfer1
//!
//! \brief The TensorRT API version 1 namespace.
//!
namespace nvinfer1
{
//!
//! \enum LayerType
//!
//! \brief The type values of layer classes.
//!
//! \see ILayer::getType()
//!
enum class LayerType : int32_t
{
kCONVOLUTION = 0, //!< Convolution layer.
kFULLY_CONNECTED = 1, //!< Fully connected layer.
kACTIVATION = 2, //!< Activation layer.
kPOOLING = 3, //!< Pooling layer.
kLRN = 4, //!< LRN layer.
kSCALE = 5, //!< Scale layer.
kSOFTMAX = 6, //!< SoftMax layer.
kDECONVOLUTION = 7, //!< Deconvolution layer.
kCONCATENATION = 8, //!< Concatenation layer.
kELEMENTWISE = 9, //!< Elementwise layer.
kPLUGIN = 10, //!< Plugin layer.
kUNARY = 11, //!< UnaryOp operation Layer.
kPADDING = 12, //!< Padding layer.
kSHUFFLE = 13, //!< Shuffle layer.
kREDUCE = 14, //!< Reduce layer.
kTOPK = 15, //!< TopK layer.
kGATHER = 16, //!< Gather layer.
kMATRIX_MULTIPLY = 17, //!< Matrix multiply layer.
kRAGGED_SOFTMAX = 18, //!< Ragged softmax layer.
kCONSTANT = 19, //!< Constant layer.
kRNN_V2 = 20, //!< RNNv2 layer.
kIDENTITY = 21, //!< Identity layer.
kPLUGIN_V2 = 22, //!< PluginV2 layer.
kSLICE = 23, //!< Slice layer.
kSHAPE = 24, //!< Shape layer.
kPARAMETRIC_RELU = 25, //!< Parametric ReLU layer.
kRESIZE = 26, //!< Resize Layer.
kTRIP_LIMIT = 27, //!< Loop Trip limit layer
kRECURRENCE = 28, //!< Loop Recurrence layer
kITERATOR = 29, //!< Loop Iterator layer
kLOOP_OUTPUT = 30, //!< Loop output layer
kSELECT = 31, //!< Select layer.
kFILL = 32, //!< Fill layer
kQUANTIZE = 33, //!< Quantize layer
kDEQUANTIZE = 34, //!< Dequantize layer
kCONDITION = 35, //!< Condition layer
kCONDITIONAL_INPUT = 36, //!< Conditional Input layer
kCONDITIONAL_OUTPUT = 37, //!< Conditional Output layer
kSCATTER = 38, //!< Scatter layer
kEINSUM = 39, //!< Einsum layer
kASSERTION = 40, //!< Assertion layer
};
//! Maximum number of elements in LayerType enum. \see LayerType
template <>
constexpr inline int32_t EnumMax<LayerType>() noexcept
{
return 41;
}
//!
//! \brief It is capable of representing one or more TensorFormat by binary OR
//! operations, e.g., 1U << TensorFormat::kCHW4 | 1U << TensorFormat::kCHW32.
//!
//! \see ITensor::getAllowedFormats(), ITensor::setAllowedFormats(),
//!
using TensorFormats = uint32_t;
//!
//! \enum ActivationType
//!
//! \brief Enumerates the types of activation to perform in an activation layer.
//!
enum class ActivationType : int32_t
{
kRELU = 0, //!< Rectified linear activation.
kSIGMOID = 1, //!< Sigmoid activation.
kTANH = 2, //!< TanH activation.
kLEAKY_RELU = 3, //!< LeakyRelu activation: x>=0 ? x : alpha * x.
kELU = 4, //!< Elu activation: x>=0 ? x : alpha * (exp(x) - 1).
kSELU = 5, //!< Selu activation: x>0 ? beta * x : beta * (alpha*exp(x) - alpha)
kSOFTSIGN = 6, //!< Softsign activation: x / (1+|x|)
kSOFTPLUS = 7, //!< Parametric softplus activation: alpha*log(exp(beta*x)+1)
kCLIP = 8, //!< Clip activation: max(alpha, min(beta, x))
kHARD_SIGMOID = 9, //!< Hard sigmoid activation: max(0, min(1, alpha*x+beta))
kSCALED_TANH = 10, //!< Scaled tanh activation: alpha*tanh(beta*x)
kTHRESHOLDED_RELU = 11 //!< Thresholded ReLU activation: x>alpha ? x : 0
};
namespace impl
{
//! Maximum number of elements in ActivationType enum. \see ActivationType
template <>
struct EnumMaxImpl<ActivationType>
{
static constexpr int32_t kVALUE = 12;
};
} // namespace impl
//!
//! \class ITensor
//!
//! \brief A tensor in a network definition.
//!
//! To remove a tensor from a network definition, use INetworkDefinition::removeTensor().
//!
//! When using the DLA, the cumulative size of all Tensors that are not marked as Network Input or Output tensors,
//! must be less than 1GB in size to fit into a single subgraph. If the build option kGPU_FALLBACK is specified, then
//! multiple subgraphs can be created, with each subgraph limited to less than 1GB of internal tensors data.
//!
//! \warning The volume of the tensor must be less than 2^31 elements.
//! \warning Do not inherit from this class, as doing so will break forward-compatibility of the API and
//! ABI.
//!
class ITensor : public INoCopy
{
public:
//!
//! \brief Set the tensor name.
//!
//! For a network input, the name is assigned by the application. For tensors which are layer outputs,
//! a default name is assigned consisting of the layer name followed by the index of the output in brackets.
//!
//! This method copies the name string.
//!
//! \param name The name.
//!
//! \see getName()
//!
void setName(const char* name) noexcept
{
mImpl->setName(name);
}
//!
//! \brief Get the tensor name.
//!
//! \return The name as a null-terminated C-style string.
//!
//! \see setName()
//!
const char* getName() const noexcept
{
return mImpl->getName();
}
//!
//! \brief Set the dimensions of a tensor.
//!
//! For a network input, the dimensions are assigned by the application. For a network output, the dimensions are
//! computed based on the layer parameters and the inputs to the layer. If a tensor size or a parameter is modified
//! in the network, the dimensions of all dependent tensors will be recomputed.
//!
//! This call is only legal for network input tensors, since the dimensions of layer output tensors are inferred
//! based on layer inputs and parameters. The volume must be less than 2^31 elements.
//!
//! \param dimensions The dimensions of the tensor.
//!
//! \see getDimensions()
//!
void setDimensions(Dims dimensions) noexcept
{
mImpl->setDimensions(dimensions);
}
//!
//! \brief Get the dimensions of a tensor.
//!
//! \return The dimensions of the tensor.
//!
//! \warning getDimensions() returns a -1 for dimensions that are derived from a wildcard dimension.
//! \see setDimensions()
//!
Dims getDimensions() const noexcept
{
return mImpl->getDimensions();
}
//!
//! \brief Set the data type of a tensor.
//!
//! \param type The data type of the tensor.
//!
//! The type is unchanged if the tensor is not a network input tensor, or marked as an output tensor or shape
//! output tensor.
//!
//! \see getType()
//!
void setType(DataType type) noexcept
{
mImpl->setType(type);
}
//!
//! \brief Get the data type of a tensor.
//!
//! \return The data type of the tensor.
//!
//! \see setType()
//!
DataType getType() const noexcept
{
return mImpl->getType();
}
//!
//! \brief Set dynamic range for the tensor
//!
//! Currently, only symmetric ranges are supported.
//! Therefore, the larger of the absolute values of the provided bounds is used.
//!
//! \return Whether the dynamic range was set successfully.
//!
//! Requires that min and max be finite, and min <= max.
//!
bool setDynamicRange(float min, float max) noexcept
{
return mImpl->setDynamicRange(min, max);
}
//!
//! \brief Whether the tensor is a network input.
//!
bool isNetworkInput() const noexcept
{
return mImpl->isNetworkInput();
}
//!
//! \brief Whether the tensor is a network output.
//!
bool isNetworkOutput() const noexcept
{
return mImpl->isNetworkOutput();
}
//!
//! \brief Set whether to enable broadcast of tensor across the batch.
//!
//! When a tensor is broadcast across a batch, it has the same value for every member in the batch.
//! Memory is only allocated once for the single member.
//!
//! This method is only valid for network input tensors, since the flags of layer output tensors are inferred based
//! on layer inputs and parameters.
//! If this state is modified for a tensor in the network, the states of all dependent tensors will be recomputed.
//! If the tensor is for an explicit batch network, then this function does nothing.
//!
//! \warning The broadcast flag is ignored when using explicit batch network mode.
//!
//! \param broadcastAcrossBatch Whether to enable broadcast of tensor across the batch.
//!
//! \see getBroadcastAcrossBatch()
//!
void setBroadcastAcrossBatch(bool broadcastAcrossBatch) noexcept
{
mImpl->setBroadcastAcrossBatch(broadcastAcrossBatch);
}
//!
//! \brief Check if tensor is broadcast across the batch.
//!
//! When a tensor is broadcast across a batch, it has the same value for every member in the batch.
//! Memory is only allocated once for the single member. If the network is in explicit batch mode,
//! this function returns true if the leading dimension is 1.
//!
//! \return True if tensor is broadcast across the batch, false otherwise.
//!
//! \see setBroadcastAcrossBatch()
//!
bool getBroadcastAcrossBatch() const noexcept
{
return mImpl->getBroadcastAcrossBatch();
}
//!
//! \brief Get the storage location of a tensor.
//! \return The location of tensor data.
//! \see setLocation()
//!
TensorLocation getLocation() const noexcept
{
return mImpl->getLocation();
}
//!
//! \brief Set the storage location of a tensor
//! \param location the location of tensor data
//!
//! Only network input tensors for storing sequence lengths for RNNv2 are supported.
//! Using host storage for layers that do not support it will generate
//! errors at build time.
//!
//! \see getLocation()
//!
void setLocation(TensorLocation location) noexcept
{
mImpl->setLocation(location);
}
//!
//! \brief Query whether dynamic range is set.
//!
//! \return True if dynamic range is set, false otherwise.
//!
bool dynamicRangeIsSet() const noexcept
{
return mImpl->dynamicRangeIsSet();
}
//!
//! \brief Undo effect of setDynamicRange.
//!
void resetDynamicRange() noexcept
{
mImpl->resetDynamicRange();
}
//!
//! \brief Get minimum of dynamic range.
//!
//! \return Minimum of dynamic range, or quiet NaN if range was not set.
//!
float getDynamicRangeMin() const noexcept
{
return mImpl->getDynamicRangeMin();
}
//!
//! \brief Get maximum of dynamic range.
//!
//! \return Maximum of dynamic range, or quiet NaN if range was not set.
//!
float getDynamicRangeMax() const noexcept
{
return mImpl->getDynamicRangeMax();
}
//!
//! \brief Set allowed formats for this tensor. By default all formats are allowed.
//! Shape tensors (for which isShapeTensor() returns true) may only have row major linear format.
//!
//! When running network on DLA and the build option kGPU_FALLBACK is not specified, if DLA format(kCHW4 with Int8,
//! kCHW4 with FP16, kCHW16 with FP16, kCHW32 with Int8) is set, the input format is treated as native DLA format with
//! line stride requirement. Input/output binding with these format should have correct layout during
//! inference.
//!
//! \param formats A bitmask of TensorFormat values that are supported for this tensor.
//!
//! \see ITensor::getAllowedFormats()
//! \see TensorFormats
//!
void setAllowedFormats(TensorFormats formats) noexcept
{
mImpl->setAllowedFormats(formats);
}
//!
//! \brief Get a bitmask of TensorFormat values that the tensor supports.
//! For a shape tensor, only row major linear format is allowed.
//!
//! \return The value specified by setAllowedFormats or all possible formats.
//!
//! \see ITensor::setAllowedFormats()
//!
TensorFormats getAllowedFormats() const noexcept
{
return mImpl->getAllowedFormats();
}
//!
//! \brief Whether the tensor is a shape tensor.
//!
//! A shape tensor is a tensor that is related to shape calculations.
//! It must be 0D or 1D, have type Int32 or Bool, and its shape must be determinable at build time.
//! Furthermore, it must be needed as a shape tensor, either marked as a network shape
//! output via markOutputForShapes(), or as an input that is required to be a shape
//! tensor, such as the second input to IShuffleLayer. Some layers are "polymorphic" in
//! this respect. For example, the inputs to IElementWiseLayer must be shape tensors
//! if the output is a shape tensor.
//!
//! The TensorRT Developer Guide give the formal rules for what tensors are shape tensors.
//!
//! The result of isShapeTensor() is reliable only when network construction is complete.
//! For example, if a partially built network sums two tensors T1 and T2 to create
//! tensor T3, and none are yet needed as shape tensors, isShapeTensor() returns false
//! for all three tensors. Setting the second input of IShuffleLayer to be T3 would
//! cause all three tensors to be shape tensors, because IShuffleLayer requires that its
//! second optional input be a shape tensor, and IElementWiseLayer is "polymorphic".
//!
//! If a tensor is a shape tensor and becomes an engine input or output,
//! then ICudaEngine::isShapeBinding will be true for that tensor.
//!
//! It is possible for a tensor to be both a shape tensor and an execution tensor.
//!
//! \return True if tensor is a shape tensor, false otherwise.
//!
//! \see INetworkDefinition::markOutputForShapes(), ICudaEngine::isShapeBinding()
//!
bool isShapeTensor() const noexcept
{
return mImpl->isShapeTensor();
}
//!
//! \brief Whether the tensor is an execution tensor.
//!
//! Tensors are usually execution tensors. The exceptions are tensors used
//! solely for shape calculations or whose contents not needed to compute the outputs.
//!
//! The result of isExecutionTensor() is reliable only when network construction is complete.
//! For example, if a partially built network has no path from a tensor to a network output,
//! isExecutionTensor() returns false. Completing the path would cause it to become true.
//!
//! If a tensor is an execution tensor and becomes an engine input or output,
//! then ICudaEngine::isExecutionBinding will be true for that tensor.
//!
//! A tensor with isShapeTensor() == false and isExecutionTensor() == false
//! can still show up as an input to the engine if its dimensions are required.
//! In that case, only its dimensions need to be set at runtime and a nullptr
//! can be passed instead of a pointer to its contents.
//!
bool isExecutionTensor() const noexcept
{
return mImpl->isExecutionTensor();
}
protected:
apiv::VTensor* mImpl;
virtual ~ITensor() noexcept = default;
};
//!
//! \class ILayer
//!
//! \brief Base class for all layer classes in a network definition.
//!
//! \warning Do not inherit from this class, as doing so will break forward-compatibility of the API and ABI.
//!
class ILayer : public INoCopy
{
public:
//!
//! \brief Return the type of a layer.
//!
//! \see LayerType
//!
LayerType getType() const noexcept
{
return mLayer->getType();
}
//!
//! \brief Set the name of a layer.
//!
//! This method copies the name string.
//!
//! \see getName()
//!
void setName(const char* name) noexcept
{
mLayer->setName(name);
}
//!
//! \brief Return the name of a layer.
//!
//! \see setName()
//!
const char* getName() const noexcept
{
return mLayer->getName();
}
//!
//! \brief Get the number of inputs of a layer.
//!
int32_t getNbInputs() const noexcept
{
return mLayer->getNbInputs();
}
//!
//! \brief Get the layer input corresponding to the given index.
//!
//! \param index The index of the input tensor.
//!
//! \return The input tensor, or nullptr if the index is out of range or the tensor is optional
//! (\ref ISliceLayer and \ref IRNNv2Layer).
//!
ITensor* getInput(int32_t index) const noexcept
{
return mLayer->getInput(index);
}
//!
//! \brief Get the number of outputs of a layer.
//!
int32_t getNbOutputs() const noexcept
{
return mLayer->getNbOutputs();
}
//!
//! \brief Get the layer output corresponding to the given index.
//!
//! \return The indexed output tensor, or nullptr if the index is out of range or the tensor is optional
//! (\ref IRNNv2Layer).
//!
ITensor* getOutput(int32_t index) const noexcept
{
return mLayer->getOutput(index);
}
//!
//! \brief Replace an input of this layer with a specific tensor.
//!
//! \param index the index of the input to modify.
//! \param tensor the new input tensor
//!
//! Except for IFillLayer, ILoopOutputLayer, IResizeLayer, IShuffleLayer, and ISliceLayer,
//! this method cannot change the number of inputs to a layer. The index argument must be
//! less than the value of getNbInputs().
//!
//! See comments for overloads of setInput() for layers with special behavior.
//!
void setInput(int32_t index, ITensor& tensor) noexcept
{
return mLayer->setInput(index, tensor);
}
//!
//! \brief Set the computational precision of this layer
//!
//! Setting the precision allows TensorRT to choose an implementation which run at this computational precision.
//! Layer input type would also get inferred from layer computational precision. TensorRT could still choose a
//! non-conforming fastest implementation that ignores the requested precision. To force choosing an implementation
//! with the requested precision, set exactly one of the following flags, which differ in what happens
//! if no such implementation exists:
//!
//! * BuilderFlag::kOBEY_PRECISION_CONSTRAINTS - build fails with an error message.
//!
//! * BuilderFlag::kPREFER_PRECISION_CONSTRAINTS - TensorRT falls back to an
//! implementation without the requested precision.
//!
//! If precision is not set, or falling back, TensorRT will select the layer computational precision
//! and layer input type based on global performance considerations and the flags specified to the builder.
//!
//! \param dataType the computational precision.
//!
//! \see getPrecision() precisionIsSet() resetPrecision()
//!
void setPrecision(DataType dataType) noexcept
{
mLayer->setPrecision(dataType);
}
//!
//! \brief get the computational precision of this layer
//!
//! \return the computational precision
//!
//! \see setPrecision() precisionIsSet() resetPrecision()
//!
DataType getPrecision() const noexcept
{
return mLayer->getPrecision();
}
//!
//! \brief whether the computational precision has been set for this layer
//!
//! \return whether the computational precision has been explicitly set
//!
//! \see setPrecision() getPrecision() resetPrecision()
//!
bool precisionIsSet() const noexcept
{
return mLayer->precisionIsSet();
}
//!
//! \brief reset the computational precision for this layer
//!
//! \see setPrecision() getPrecision() precisionIsSet()
//!
void resetPrecision() noexcept
{
mLayer->resetPrecision();
}
//!
//! \brief Set the output type of this layer
//!
//! Setting the output type constrains TensorRT to choose implementations which generate output data with the
//! given type. If it is not set, TensorRT will select output type based on layer computational precision. TensorRT
//! could still choose non-conforming output type based on fastest implementation. To force choosing the requested
//! output type, set exactly one of the following flags, which differ in what happens if no such implementation exists:
//!
//! * BuilderFlag::kOBEY_PRECISION_CONSTRAINTS - build fails with an error message.
//!
//! * BuilderFlag::kPREFER_PRECISION_CONSTRAINTS - TensorRT falls back to an
//! implementation with a non-conforming output type.
//!
//! In case layer precision is not specified, or falling back, the output type depends on the
//! chosen implementation, based on performance considerations and the flags specified to the builder.
//!
//! This method cannot be used to set the data type of the second output tensor of the TopK layer. The data type of
//! the second output tensor of the topK layer is always Int32. Also the output type of all layers that are shape
//! operations must be DataType::kINT32, and all attempts to set the output type to some other data type will be
//! ignored except for issuing an error message.
//!
//! Note that the layer output type is generally not identical to the data type of the output tensor, as TensorRT
//! may insert implicit reformatting operations to convert the former to the latter. Calling layer->setOutputType(i,
//! type) has no effect on the data type of the i-th output tensor of layer, and users need to call
//! layer->getOutput(i)->setType(type) to change the tensor data type. This is particularly relevant if the tensor
//! is marked as a network output, since only setType() [but not setOutputType()] will affect the data
//! representation in the corresponding output binding.
//!
//! \param index the index of the output to set
//! \param dataType the type of the output
//!
//! \see getOutputType() outputTypeIsSet() resetOutputType()
//!
void setOutputType(int32_t index, DataType dataType) noexcept
{
mLayer->setOutputType(index, dataType);
}
//!
//! \brief get the output type of this layer
//!
//! \param index the index of the output
//! \return the output precision. If no precision has been set, DataType::kFLOAT will be returned,
//! unless the output type is inherently DataType::kINT32.
//!
//! \see getOutputType() outputTypeIsSet() resetOutputType()
//!
DataType getOutputType(int32_t index) const noexcept
{
return mLayer->getOutputType(index);
}
//!
//! \brief whether the output type has been set for this layer
//!
//! \param index the index of the output
//! \return whether the output type has been explicitly set
//!
//! \see setOutputType() getOutputType() resetOutputType()
//!
bool outputTypeIsSet(int32_t index) const noexcept
{
return mLayer->outputTypeIsSet(index);
}
//!
//! \brief reset the output type for this layer
//!
//! \param index the index of the output
//!
//! \see setOutputType() getOutputType() outputTypeIsSet()
//!
void resetOutputType(int32_t index) noexcept
{
return mLayer->resetOutputType(index);
}
protected:
virtual ~ILayer() noexcept = default;
apiv::VLayer* mLayer;
};
//!
//! \enum PaddingMode
//!
//! \brief Enumerates the modes of padding to perform in convolution, deconvolution and pooling layer,
//! padding mode takes precedence if setPaddingMode() and setPrePadding() are also used.
//!
//! There are three padding styles, EXPLICIT, SAME, and CAFFE, with each style having two variants.
//! The EXPLICIT and CAFFE styles determine if the final sampling location is used or not.
//! The SAME style determine if the asymmetry in the padding is on the pre or post padding.
//!
//! \code
//! Shorthand:
//! I = dimensions of input image.
//! B = prePadding, before the image data. For deconvolution, prePadding is set before output.
//! A = postPadding, after the image data. For deconvolution, postPadding is set after output.
//! P = delta between input and output
//! S = stride
//! F = filter
//! O = output
//! D = dilation
//! M = I + B + A ; The image data plus any padding
//! DK = 1 + D * (F - 1)
//! \endcode
//!
//! Formulas for Convolution:
//! - EXPLICIT_ROUND_DOWN:
//! \code
//! O = floor((M - DK) / S) + 1
//! \endcode
//! - CAFFE_ROUND_DOWN:
//! \code
//! O = floor((I + B * 2 - DK) / S) + 1
//! \endcode
//! - EXPLICIT_ROUND_UP:
//! \code
//! O = ceil((M - DK) / S) + 1
//! \endcode
//! - CAFFE_ROUND_UP:
//! \code
//! O = ceil((I + B * 2 - DK) / S) + 1
//! \endcode
//! - SAME_UPPER:
//! \code
//! O = ceil(I / S)
//! P = floor((I - 1) / S) * S + DK - I;
//! B = floor(P / 2)
//! A = P - B
//! \endcode
//! - SAME_LOWER:
//! \code
//! O = ceil(I / S)
//! P = floor((I - 1) / S) * S + DK - I;
//! A = floor(P / 2)
//! B = P - A
//! \endcode
//!
//! Formulas for Deconvolution:
//! - EXPLICIT_ROUND_DOWN:
//! - CAFFE_ROUND_DOWN:
//! - EXPLICIT_ROUND_UP:
//! - CAFFE_ROUND_UP:
//! \code
//! O = (I - 1) * S + DK - (B + A)
//! \endcode
//! - SAME_UPPER:
//! \code
//! O = min(I * S, (I - 1) * S + DK)
//! P = max(DK - S, 0)
//! B = floor(P / 2)
//! A = P - B
//! \endcode
//! - SAME_LOWER:
//! \code
//! O = min(I * S, (I - 1) * S + DK)
//! P = max(DK - S, 0)
//! A = floor(P / 2)
//! B = P - A
//! \endcode
//!
//! Formulas for Pooling:
//! - EXPLICIT_ROUND_DOWN:
//! \code
//! O = floor((M - F) / S) + 1
//! \endcode
//! - EXPLICIT_ROUND_UP:
//! \code
//! O = ceil((M - F) / S) + 1
//! \endcode
//! - SAME_UPPER:
//! \code
//! O = ceil(I / S)
//! P = floor((I - 1) / S) * S + F - I;
//! B = floor(P / 2)
//! A = P - B
//! \endcode
//! - SAME_LOWER:
//! \code
//! O = ceil(I / S)
//! P = floor((I - 1) / S) * S + F - I;
//! A = floor(P / 2)
//! B = P - A
//! \endcode
//! - CAFFE_ROUND_DOWN:
//! \code
//! EXPLICIT_ROUND_DOWN - ((EXPLICIT_ROUND_DOWN - 1) * S >= I + B)
//! \endcode
//! - CAFFE_ROUND_UP:
//! \code
//! EXPLICIT_ROUND_UP - ((EXPLICIT_ROUND_UP - 1) * S >= I + B)
//! \endcode
//!
//! Pooling Example 1:
//! \code
//! Given I = {6, 6}, B = {3, 3}, A = {2, 2}, S = {2, 2}, F = {3, 3}. What is O?
//! (B, A can be calculated for SAME_UPPER and SAME_LOWER mode)
//! \endcode
//!
//! - EXPLICIT_ROUND_DOWN:
//! \code
//! Computation:
//! M = {6, 6} + {3, 3} + {2, 2} ==> {11, 11}
//! O ==> floor((M - F) / S) + 1
//! ==> floor(({11, 11} - {3, 3}) / {2, 2}) + {1, 1}
//! ==> floor({8, 8} / {2, 2}) + {1, 1}
//! ==> {5, 5}
//! \endcode
//! - EXPLICIT_ROUND_UP:
//! \code
//! Computation:
//! M = {6, 6} + {3, 3} + {2, 2} ==> {11, 11}
//! O ==> ceil((M - F) / S) + 1
//! ==> ceil(({11, 11} - {3, 3}) / {2, 2}) + {1, 1}
//! ==> ceil({8, 8} / {2, 2}) + {1, 1}
//! ==> {5, 5}
//! \endcode
//! The sample points are {0, 2, 4, 6, 8} in each dimension.
//!
//! - SAME_UPPER:
//! \code
//! Computation:
//! I = {6, 6}
//! S = {2, 2}
//! O = ceil(I / S) = {3, 3}
//! P = floor((I - 1) / S) * S + F - I
//! ==> floor(({6, 6} - {1, 1}) / {2, 2}) * {2, 2} + {3, 3} - {6, 6}
//! ==> {4, 4} + {3, 3} - {6, 6}
//! ==> {1, 1}
//! B = floor({1, 1} / {2, 2})
//! ==> {0, 0}
//! A = {1, 1} - {0, 0}
//! ==> {1, 1}
//! \endcode
//! - SAME_LOWER:
//! \code
//! Computation:
//! I = {6, 6}
//! S = {2, 2}
//! O = ceil(I / S) = {3, 3}
//! P = floor((I - 1) / S) * S + F - I
//! ==> {1, 1}
//! A = floor({1, 1} / {2, 2})
//! ==> {0, 0}
//! B = {1, 1} - {0, 0}
//! ==> {1, 1}
//! \endcode
//! The sample pointers are {0, 2, 4} in each dimension.
//! SAMPLE_UPPER has {O0, O1, O2, pad} in output in each dimension.
//! SAMPLE_LOWER has {pad, O0, O1, O2} in output in each dimension.
//!
//! Pooling Example 2:
//! \code
//! Given I = {6, 6}, B = {3, 3}, A = {3, 3}, S = {2, 2}, F = {3, 3}. What is O?
//! \endcode
//!
//! - CAFFE_ROUND_DOWN:
//! \code
//! Computation:
//! M = {6, 6} + {3, 3} + {3, 3} ==> {12, 12}
//! EXPLICIT_ROUND_DOWN ==> floor((M - F) / S) + 1
//! ==> floor(({12, 12} - {3, 3}) / {2, 2}) + {1, 1}
//! ==> {5, 5}
//! DIFF = (((EXPLICIT_ROUND_DOWN - 1) * S >= I + B) ? {1, 1} : {0, 0})
//! ==> ({5, 5} - {1, 1}) * {2, 2} >= {6, 6} + {3, 3} ? {1, 1} : {0,0}
//! ==> {0, 0}
//! O ==> EXPLICIT_ROUND_DOWN - DIFF
//! ==> {5, 5} - {0, 0}
//! ==> {5, 5}
//! \endcode
//! - CAFFE_ROUND_UP:
//! \code
//! Computation:
//! M = {6, 6} + {3, 3} + {3, 3} ==> {12, 12}
//! EXPLICIT_ROUND_UP ==> ceil((M - F) / S) + 1
//! ==> ceil(({12, 12} - {3, 3}) / {2, 2}) + {1, 1}
//! ==> {6, 6}
//! DIFF = (((EXPLICIT_ROUND_UP - 1) * S >= I + B) ? {1, 1} : {0, 0})
//! ==> ({6, 6} - {1, 1}) * {2, 2} >= {6, 6} + {3, 3} ? {1, 1} : {0,0}
//! ==> {1, 1}
//! O ==> EXPLICIT_ROUND_UP - DIFF
//! ==> {6, 6} - {1, 1}
//! ==> {5, 5}
//! \endcode
//!
//! The sample points are {0, 2, 4, 6, 8} in each dimension. <br>
//! CAFFE_ROUND_DOWN and CAFFE_ROUND_UP have two restrictions each on usage with pooling operations.
//! This will cause getDimensions to return an empty dimension and also to reject the network
//! at validation time. <br>
//! For more information on original reference code, see
//! https://github.com/BVLC/caffe/blob/master/src/caffe/layers/pooling_layer.cpp
//!
//! - Restriction 1:
//! \code
//! CAFFE_ROUND_DOWN: B >= F is an error if (B - S) < F
//! CAFFE_ROUND_UP: (B + S) >= (F + 1) is an error if B < (F + 1)
//! \endcode
//!
//! - Restriction 2:
//! \code
//! CAFFE_ROUND_DOWN: (B - S) >= F is an error if B >= F
//! CAFFE_ROUND_UP: B >= (F + 1) is an error if (B + S) >= (F + 1)
//! \endcode
//!
enum class PaddingMode : int32_t
{
kEXPLICIT_ROUND_DOWN = 0, //!< Use explicit padding, rounding output size down.
kEXPLICIT_ROUND_UP = 1, //!< Use explicit padding, rounding output size up.
kSAME_UPPER = 2, //!< Use SAME padding, with prePadding <= postPadding.
kSAME_LOWER = 3, //!< Use SAME padding, with prePadding >= postPadding.
kCAFFE_ROUND_DOWN = 4, //!< Use CAFFE padding, rounding output size down, uses prePadding value.
kCAFFE_ROUND_UP = 5 //!< Use CAFFE padding, rounding output size up, uses prePadding value.
};
namespace impl
{
//! Maximum number of elements in PaddingMode enum. \see PaddingMode
template <>
struct EnumMaxImpl<PaddingMode>
{
static constexpr int32_t kVALUE = 6;
};
} // namespace impl
//!
//! \class IConvolutionLayer
//!
//! \brief A convolution layer in a network definition.
//!
//! This layer performs a correlation operation between 3-dimensional filter with a 4-dimensional tensor to produce
//! another 4-dimensional tensor.
//!
//! An optional bias argument is supported, which adds a per-channel constant to each value in the output.
//!
//! \warning Do not inherit from this class, as doing so will break forward-compatibility of the API and ABI.
//!
class IConvolutionLayer : public ILayer
{
public:
//!
//! \brief Set the HW kernel size of the convolution.
//!
//! If executing this layer on DLA, both height and width of kernel size must be in the range [1,32].
//!
//! \see getKernelSize()
//!
//! \deprecated Superseded by setKernelSizeNd and will be removed in TensorRT 9.0.
//!
TRT_DEPRECATED void setKernelSize(DimsHW kernelSize) noexcept
{
mImpl->setKernelSize(kernelSize);
}
//!
//! \brief Get the HW kernel size of the convolution.
//!
//! \see setKernelSize()
//!
//! \deprecated Superseded by getKernelSizeNd and will be removed in TensorRT 9.0.
//!
TRT_DEPRECATED DimsHW getKernelSize() const noexcept
{