{{ message }}
Repository navigation
Expand file tree
/
Copy pathNvInfer.h
More file actions
11966 lines (11109 loc) · 409 KB
/
Copy pathNvInfer.h
File metadata and controls
11966 lines (11109 loc) · 409 KB
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
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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" // IWYU pragma: export
//!
//! \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.
kCAST = 1, //!< Cast 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.
kIDENTITY = 20, //!< Identity layer.
kPLUGIN_V2 = 21, //!< PluginV2 layer.
kSLICE = 22, //!< Slice layer.
kSHAPE = 23, //!< Shape layer.
kPARAMETRIC_RELU = 24, //!< Parametric ReLU layer.
kRESIZE = 25, //!< Resize Layer.
kTRIP_LIMIT = 26, //!< Loop Trip limit layer
kRECURRENCE = 27, //!< Loop Recurrence layer
kITERATOR = 28, //!< Loop Iterator layer
kLOOP_OUTPUT = 29, //!< Loop output layer
kSELECT = 30, //!< Select layer.
kFILL = 31, //!< Fill layer
kQUANTIZE = 32, //!< Quantize layer
kDEQUANTIZE = 33, //!< Dequantize layer
kCONDITION = 34, //!< Condition layer
kCONDITIONAL_INPUT = 35, //!< Conditional Input layer
kCONDITIONAL_OUTPUT = 36, //!< Conditional Output layer
kSCATTER = 37, //!< Scatter layer
kEINSUM = 38, //!< Einsum layer
kASSERTION = 39, //!< Assertion layer
kONE_HOT = 40, //!< OneHot layer
kNON_ZERO = 41, //!< NonZero layer
kGRID_SAMPLE = 42, //!< Grid sample layer
kNMS = 43, //!< NMS layer
kREVERSE_SEQUENCE = 44, //!< Reverse sequence layer
kNORMALIZATION = 45, //!< Normalization layer
kPLUGIN_V3 = 46, //!< PluginV3 layer.
kSQUEEZE = 47, //!< Squeeze Layer.
kUNSQUEEZE = 48, //!< Unsqueeze Layer.
kCUMULATIVE = 49, //!< Cumulative layer.
kDYNAMIC_QUANTIZE = 50, //!< Dynamic Quantize layer.
kATTENTION_INPUT = 51, //!< Attention Input.
kATTENTION_OUTPUT = 52, //!< Attention Output.
kROTARY_EMBEDDING = 53, //!< Rotary Embedding layer.
kKVCACHE_UPDATE = 54, //!< KV Cache Update layer.
kMOE = 55, //!< MoE layer.
kDIST_COLLECTIVE = 56, //!< DistCollective layer.
};
//!
//! Maximum number of elements in LayerType enum.
//!
//! \see LayerType
//!
template <>
struct impl::EnumMaxImpl<LayerType>
{
static constexpr int32_t kVALUE = 57;
};
//!
//! \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
kGELU_ERF = 12, //!< GELU erf activation: 0.5 * x * (1 + erf(sqrt(0.5) * x))
kGELU_TANH = 13 //!< GELU tanh activation: 0.5 * x * (1 + tanh(sqrt(2/pi) * (0.044715F * pow(x, 3) + x)))
};
//!
//! Maximum number of elements in ActivationType enum.
//!
//! \see ActivationType
//!
template <>
struct impl::EnumMaxImpl<ActivationType>
{
static constexpr int32_t kVALUE = 14;
};
//!
//! \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 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.
//! Each input and output tensor must have a unique name.
//!
//! This method copies the name string.
//!
//! \param name The name.
//!
//! \warning The string name must be null-terminated, and be at most 4096 bytes including the terminator.
//!
//! \see getName()
//!
void setName(char const* name) noexcept
{
mImpl->setName(name);
}
//!
//! \brief Get the tensor name.
//!
//! \return The name as a null-terminated C-style string.
//!
//! \see setName()
//!
char const* 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.
//!
//! \param dimensions The dimensions of the tensor.
//!
//! \see getDimensions()
//!
void setDimensions(Dims const& 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 Get the data type of a tensor.
//!
//! \return The data type of the tensor.
//!
//! The type is the inferred type.
//!
//! \see setType()
//!
DataType getType() const noexcept
{
return mImpl->getType();
}
//!
//! \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 implicit batch dimension.
//!
//! \warning This method has no effect other than issuing a warning.
//!
//! \param broadcastAcrossBatch Whether to broadcast the tensor across the implicit
//! batch dimension that was a feature of TensorRT 9.x and prior.
//!
//! \see getBroadcastAcrossBatch()
//!
//! \deprecated Deprecated in TensorRT 10.0. Implicit batch is not supported since TensorRT 10.0.
//!
TRT_DEPRECATED void setBroadcastAcrossBatch(bool broadcastAcrossBatch) noexcept
{
mImpl->setBroadcastAcrossBatch(broadcastAcrossBatch);
}
//!
//! \brief Check if tensor is broadcast across the implicit batch dimension.
//!
//! \return Always false since TensorRT 10.0 does not support an implicit batch dimension.
//!
//! \see setBroadcastAcrossBatch()
//!
//! \deprecated Deprecated in TensorRT 10.0. Implicit batch is not supported since TensorRT 10.0.
//!
TRT_DEPRECATED 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()
//!
//! \deprecated Deprecated in TensorRT 10.0. RNNv2 is not supported and the location must
//! always be TensorLocation::kDEVICE since TensorRT 10.0.
//!
TRT_DEPRECATED void setLocation(TensorLocation location) noexcept
{
mImpl->setLocation(location);
}
//!
//! \brief Set allowed formats for an input or output 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.
//!
//! Tensor formats are determined at build time by TensorRT for tensors not marked as input or output.
//!
//! \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 have type Int32, Int64, Bool, or Float, 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 a layer 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 gives 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".
//!
//! 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()
//!
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 are 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.
//!
//!
//! 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();
}
//!
//! \brief Name a dimension of an input tensor.
//!
//! Associate a runtime dimension of an input tensor with a symbolic name.
//! Dimensions with the same non-empty name must be equal at runtime.
//! Knowing this equality for runtime dimensions may help the TensorRT optimizer.
//! Both runtime and build-time dimensions can be named.
//!
//! For example, setDimensionName(0, "n") associates the symbolic name "n" with the leading dimension.
//!
//! This method copies the name string.
//! If the function is called again, with the same index, it will overwrite the previous name.
//! If nullptr is passed as name, it will clear the name of the dimension.
//!
//! \param index index of the dimension
//! \param name of the dimension, as a pointer to a null-terminated character sequence.
//!
//! \warning The string name must be null-terminated, and be at most 4096 bytes including the terminator.
//!
//! \see getDimensionName()
//!
void setDimensionName(int32_t index, char const* name) noexcept
{
mImpl->setDimensionName(index, name);
}
//!
//! \brief Get the name of an input dimension.
//!
//! \param index index of the dimension
//!
//! \return The name of the input dimension, or nullptr if the dimension has no name.
//! The name is a pointer to a null-terminated character sequence.
//!
//! \see setDimensionName()
//!
char const* getDimensionName(int32_t index) const noexcept
{
return mImpl->getDimensionName(index);
}
protected:
apiv::VTensor* mImpl;
virtual ~ITensor() noexcept = 0;
};
inline ITensor::~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.
//!
//! \warning The string name must be null-terminated, and be at most 4096 bytes including the terminator.
//!
//! \see getName()
//!
void setName(char const* name) noexcept
{
mLayer->setName(name);
}
//!
//! \brief Return the name of a layer.
//!
//! \see setName()
//!
char const* 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).
//!
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.
//!
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, INMSLayer, 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 get the output type of this layer
//!
//! \param index the index of the output
//!
//! \return the inferred output type.
//!
//! \see getOutputType() outputTypeIsSet() resetOutputType()
//!
DataType getOutputType(int32_t index) const noexcept
{
return mLayer->getOutputType(index);
}
//!
//! \brief Set the metadata for this layer.
//!
//! The metadata is emitted in the JSON returned by IEngineInspector with
//! ProfilingVerbosity set to kDETAILED.
//!
//! \param metadata The per-layer metadata.
//!
//! \warning The string name must be null-terminated and be at most 4096 bytes including the terminator.
//!
//! \see getMetadata()
//! \see getLayerInformation()
//!
void setMetadata(char const* metadata) noexcept
{
mLayer->setMetadata(metadata);
}
//!
//! \brief Get the metadata of the layer.
//!
//! \return The metadata as a null-terminated C-style string. If setMetadata() has not been called,
//! an empty string "" will be returned as a default value.
//!
//! \see setMetadata()
//!
char const* getMetadata() const noexcept
{
return mLayer->getMetadata();
}
//!
//! \brief Set the number of ranks for multi-device execution.
//!
//! Setting nbRanks > 1 is only allowed for specific layer types that support multi-device execution:
//! - IDistCollectiveLayer: Determines output shape for kALL_GATHER and kREDUCE_SCATTER operations.
//!
//! For attention layers, use IAttention::setNbRanks() instead.
//! For all other layer types, nbRanks must be 1.
//!
//! \param nbRanks The number of ranks for multi-device execution. Must be >= 1.
//!
//! \return true if successful, false if the layer type does not support the specified nbRanks value.
//!
//! \see getNbRanks()
//! \see IAttention::setNbRanks()
//!
bool setNbRanks(int32_t nbRanks) noexcept
{
return mLayer->setNbRanks(nbRanks);
}
//!
//! \brief Get the number of ranks for multi-device execution.
//!
//! \return The number of ranks configured for multi-device execution. Default is 1.
//!
//! \see setNbRanks()
//!
int32_t getNbRanks() const noexcept
{
return mLayer->getNbRanks();
}
protected:
virtual ~ILayer() noexcept = 0;
apiv::VLayer* mLayer;
};
inline ILayer::~ILayer() noexcept = default;
//!
//! \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 two padding styles, EXPLICIT and SAME with each style having two variants.
//! The EXPLICIT style 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.
//! A = postPadding, after the image data.
//! 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
//! - EXPLICIT_ROUND_UP:
//! \code
//! O = ceil((M - 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:
//! - EXPLICIT_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
//!
//! 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
//!
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.
};
//!
//! Maximum number of elements in PaddingMode enum.
//!
//! \see PaddingMode
//!
template <>
struct impl::EnumMaxImpl<PaddingMode>
{
static constexpr int32_t kVALUE = 4;
};
//!
//! \class IConvolutionLayer
//!
//! \brief A convolution layer in a network definition.
//!
//! This layer performs a correlation operation between 3 or 4 dimensional filter with a 4 or 5 dimensional tensor to
//! produce another 4 or 5 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 number of output maps for the convolution.
//!
//! If executing this layer on DLA, the number of output maps must be in the range [1,8192].
//!
//! \see getNbOutputMaps()
//!
void setNbOutputMaps(int64_t nbOutputMaps) noexcept
{
mImpl->setNbOutputMaps(nbOutputMaps);
}
//!
//! \brief Get the number of output maps for the convolution.
//!
//! \see setNbOutputMaps()
//!
int64_t getNbOutputMaps() const noexcept
{
return mImpl->getNbOutputMaps();
}
//!
//! \brief Set the number of groups for a convolution.
//!
//! The input tensor channels are divided into \p nbGroups groups, and a convolution is executed for each group,
//! using a filter per group. The results of the group convolutions are concatenated to form the output.
//!
//! \note When using groups in int8 mode, the size of the groups (i.e. the channel count divided by the group
//! count) must be a multiple of 4 for both input and output.
//!
//! Default: 1
//!
//! If executing this layer on DLA, the max number of groups is 8192.
//!
//! \see getNbGroups()
//!
void setNbGroups(int64_t nbGroups) noexcept
{
mImpl->setNbGroups(nbGroups);
}
//!
//! \brief Get the number of groups of the convolution.
//!
//! \see setNbGroups()
//!
int64_t getNbGroups() const noexcept
{
return mImpl->getNbGroups();
}
//!
//! \brief Set the kernel weights for the convolution.
//!
//! The weights are specified as a contiguous array in \p GKCRS order, where \p G is the number of groups, \p K
//! the number of output feature maps, \p C the number of input channels, and \p R and \p S are the height and
//! width of the filter.
//!
//! \see getKernelWeights()
//!
void setKernelWeights(Weights weights) noexcept
{
mImpl->setKernelWeights(weights);
}
//!
//! \brief Get the kernel weights of the convolution.
//!
//! \see setKernelWeights()
//!
Weights getKernelWeights() const noexcept
{
return mImpl->getKernelWeights();
}
//!
//! \brief Set the bias weights for the convolution.
//!
//! Bias is optional. To omit bias, set the count value of the weights structure to zero.
//!
//! The bias is applied per-channel, so the number of weights (if non-zero) must be equal to the number of output
//! feature maps.
//!
//! \see getBiasWeights()
//!
void setBiasWeights(Weights weights) noexcept
{
mImpl->setBiasWeights(weights);
}
//!
//! \brief Get the bias weights for the convolution.
//!
//! \see setBiasWeights()
//!
Weights getBiasWeights() const noexcept
{
return mImpl->getBiasWeights();
}
//!
//! \brief Set the multi-dimension pre-padding of the convolution.
//!
//! The start of the input will be zero-padded by this number of elements in each dimension.
//!
//! Default: (0, 0, ..., 0)
//!
//! If executing this layer on DLA, only support 2D padding, both height and width of padding must be in the range
//! [0,31], and the padding must be less than the kernel size.
//!
//! \see getPrePadding()
//!
void setPrePadding(Dims const& padding) noexcept
{
mImpl->setPrePadding(padding);
}
//!
//! \brief Get the pre-padding.
//!
//! \see setPrePadding()
//!
Dims getPrePadding() const noexcept
{
return mImpl->getPrePadding();
}
//!
//! \brief Set the multi-dimension post-padding of the convolution.
//!
//! The end of the input will be zero-padded by this number of elements in each dimension.
//!
//! Default: (0, 0, ..., 0)
//!
//! If executing this layer on DLA, only support 2D padding, both height and width of padding must be in the range
//! [0,31], and the padding must be less than the kernel size.
//!
//! \see getPostPadding()
//!
void setPostPadding(Dims const& padding) noexcept
{
You can’t perform that action at this time.
