Skip to content

Commit 5cff257

Browse files
committed
chore: Apply liniting
Signed-off-by: Naren Dasan <[email protected]> Signed-off-by: Naren Dasan <[email protected]>
1 parent 9403f88 commit 5cff257

24 files changed

+389
-394
lines changed

core/compiler.cpp

+65-56
Original file line numberDiff line numberDiff line change
@@ -308,70 +308,78 @@ void MapInputsAndDetermineDTypes(
308308
std::shared_ptr<torch::jit::Graph>& g,
309309
ir::StaticParams& static_params,
310310
ir::CollectionTypeMap& first_use_type_map) {
311-
cfg.convert_info.collection_input_spec_map = std::move(ir::associate_specs_with_collection_inputs(g, cfg.graph_inputs, static_params));
311+
cfg.convert_info.collection_input_spec_map =
312+
std::move(ir::associate_specs_with_collection_inputs(g, cfg.graph_inputs, static_params));
312313

313-
auto collection_inputs = ir::get_collection_inputs(g, static_params);
314-
LOG_DEBUG("In MapInputsAndDetermineDTypes, the g->inputs() size is " << g->inputs().size() << ", CollectionInputSpecMap size is" << collection_inputs.size());
314+
auto collection_inputs = ir::get_collection_inputs(g, static_params);
315+
LOG_DEBUG(
316+
"In MapInputsAndDetermineDTypes, the g->inputs() size is "
317+
<< g->inputs().size() << ", CollectionInputSpecMap size is" << collection_inputs.size());
315318

316-
for (auto in : collection_inputs) {
317-
std::vector<ir::Input>& spec = cfg.convert_info.collection_input_spec_map.find(in)->second;
318-
std::vector<c10::optional<at::ScalarType>> est_type_opt;
319+
for (auto in : collection_inputs) {
320+
std::vector<ir::Input>& spec = cfg.convert_info.collection_input_spec_map.find(in)->second;
321+
std::vector<c10::optional<at::ScalarType>> est_type_opt;
319322

320-
auto est_it = first_use_type_map.find(in);
321-
if (est_it != first_use_type_map.end()) {
322-
est_type_opt = first_use_type_map.find(in)->second;
323-
}
324-
// traverse elements in est_type_out and spec
325-
for (size_t i = 0; i < est_type_opt.size(); i++) {
326-
if (est_type_opt[i] && !spec[i].dtype_is_user_defined) {
327-
// If we can calculate the type from the graph and the type was not defined by the user then use the calculated
328-
// type
329-
LOG_INFO(
330-
"Since input type is not explicitly defined, infering using first tensor calculation\n Inferred input "
331-
<< in->debugName() << " has type " << est_type_opt[i].value());
332-
spec[i].dtype = util::ScalarTypeToTRTDataType(est_type_opt[i].value());
333-
} else if (!est_type_opt[i] && !spec[i].dtype_is_user_defined) {
334-
// If we cannot calculate the type and the user did not define the type, then default to FP32
335-
LOG_WARNING(
336-
"Cannot infer input type from calcuations in graph for input "
337-
<< in->debugName() << ". Assuming it is Float32. If not, specify input type explicity");
338-
spec[i].dtype = nvinfer1::DataType::kFLOAT;
339-
} else if (spec[i].dtype_is_user_defined && cfg.partition_info.enabled) {
340-
if (!est_type_opt[i]) {
341-
LOG_INFO("Cannot infer input tensor dtype in graph, compiler is going to use the user setting");
323+
auto est_it = first_use_type_map.find(in);
324+
if (est_it != first_use_type_map.end()) {
325+
est_type_opt = first_use_type_map.find(in)->second;
326+
}
327+
// traverse elements in est_type_out and spec
328+
for (size_t i = 0; i < est_type_opt.size(); i++) {
329+
if (est_type_opt[i] && !spec[i].dtype_is_user_defined) {
330+
// If we can calculate the type from the graph and the type was not defined by the user then use the calculated
331+
// type
332+
LOG_INFO(
333+
"Since input type is not explicitly defined, infering using first tensor calculation\n Inferred input "
334+
<< in->debugName() << " has type " << est_type_opt[i].value());
335+
spec[i].dtype = util::ScalarTypeToTRTDataType(est_type_opt[i].value());
336+
} else if (!est_type_opt[i] && !spec[i].dtype_is_user_defined) {
337+
// If we cannot calculate the type and the user did not define the type, then default to FP32
338+
LOG_WARNING(
339+
"Cannot infer input type from calcuations in graph for input "
340+
<< in->debugName() << ". Assuming it is Float32. If not, specify input type explicity");
341+
spec[i].dtype = nvinfer1::DataType::kFLOAT;
342+
} else if (spec[i].dtype_is_user_defined && cfg.partition_info.enabled) {
343+
if (!est_type_opt[i]) {
344+
LOG_INFO("Cannot infer input tensor dtype in graph, compiler is going to use the user setting");
345+
std::stringstream ss;
346+
ss << "For input " << in->debugName() << ", found user specified input dtype as ";
347+
ss << cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
348+
ss << ". The compiler is going to use the user setting "
349+
<< cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
350+
auto warn_str = ss.str();
351+
LOG_WARNING(warn_str);
352+
// Overwrite type map with user settings
353+
first_use_type_map[in][i] = {
354+
util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype)};
355+
356+
} else {
357+
if (util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype) !=
358+
est_type_opt[i].value()) {
342359
std::stringstream ss;
343360
ss << "For input " << in->debugName() << ", found user specified input dtype as ";
344361
ss << cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
345-
ss << ". The compiler is going to use the user setting " << cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
362+
ss << ", however when inspecting the graph, the input type expected was inferred to be ";
363+
ss << est_type_opt[i].value() << std::endl;
364+
ss << "The compiler is going to use the user setting "
365+
<< cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
366+
ss << "\nThis conflict may cause an error at runtime due to partial compilation being enabled and therefore\n";
367+
ss << "compatibility with PyTorch's data type convention is required.\n";
368+
ss << "If you do indeed see errors at runtime either:\n";
369+
ss << "- Remove the dtype spec for " << in->debugName() << std::endl;
370+
ss << "- Disable partial compilation by setting require_full_compilation to True";
346371
auto warn_str = ss.str();
347372
LOG_WARNING(warn_str);
348373
// Overwrite type map with user settings
349-
first_use_type_map[in][i] = {util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype)};
350-
351-
} else {
352-
if (util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype) != est_type_opt[i].value()) {
353-
std::stringstream ss;
354-
ss << "For input " << in->debugName() << ", found user specified input dtype as ";
355-
ss << cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
356-
ss << ", however when inspecting the graph, the input type expected was inferred to be ";
357-
ss << est_type_opt[i].value() << std::endl;
358-
ss << "The compiler is going to use the user setting " << cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype;
359-
ss << "\nThis conflict may cause an error at runtime due to partial compilation being enabled and therefore\n";
360-
ss << "compatibility with PyTorch's data type convention is required.\n";
361-
ss << "If you do indeed see errors at runtime either:\n";
362-
ss << "- Remove the dtype spec for " << in->debugName() << std::endl;
363-
ss << "- Disable partial compilation by setting require_full_compilation to True";
364-
auto warn_str = ss.str();
365-
LOG_WARNING(warn_str);
366-
// Overwrite type map with user settings
367-
first_use_type_map[in][i] = {util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype)};
368-
}
374+
first_use_type_map[in][i] = {
375+
util::TRTDataTypeToScalarType(cfg.convert_info.collection_input_spec_map.find(in)->second[i].dtype)};
369376
}
370-
} else {
371-
// The user defined the type so no changes are necessary
372377
}
378+
} else {
379+
// The user defined the type so no changes are necessary
373380
}
374381
}
382+
}
375383
// }
376384
}
377385

@@ -425,12 +433,13 @@ torch::jit::Module CompileGraph(const torch::jit::Module& mod, CompileSpec cfg)
425433

426434
if (cfg.partition_info.enabled &&
427435
(!(cfg.lower_info.forced_fallback_modules.size() == 0 &&
428-
cfg.partition_info.forced_fallback_operators.size() == 0 && isBlockConvertible)
429-
|| outputIsCollection)) {
430-
436+
cfg.partition_info.forced_fallback_operators.size() == 0 && isBlockConvertible) ||
437+
outputIsCollection)) {
431438
std::unordered_map<torch::jit::Node*, int> fallback_nodes;
432-
auto collection_input_ivalues_map = partitioning::generateRandomInputs(cfg.convert_info.collection_input_spec_map, first_use_types);
433-
auto graph_and_mapping = ConstructFallbackGraph(new_mod, g->block(), collection_input_ivalues_map, cfg, static_params, fallback_nodes);
439+
auto collection_input_ivalues_map =
440+
partitioning::generateRandomInputs(cfg.convert_info.collection_input_spec_map, first_use_types);
441+
auto graph_and_mapping = ConstructFallbackGraph(
442+
new_mod, g->block(), collection_input_ivalues_map, cfg, static_params, fallback_nodes);
434443
new_g = graph_and_mapping.first;
435444
// renaming the input name of graph after fallback to ensure pytorch deserialize it correctly
436445
for (size_t i = 0; i < new_g->inputs().size(); ++i) {

core/conversion/conversion.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ void AddLayer(ConversionCtx* ctx, const torch::jit::Node* n) {
135135
<< "please report this error to https://www.github.com/NVIDIA/Torch-TensorRT/issues");
136136
}
137137

138-
void AddInputs(
139-
ConversionCtx* ctx,
140-
c10::ArrayRef<const torch::jit::Value*> inputs,
141-
ConversionInfo& conversion_info) {
138+
void AddInputs(ConversionCtx* ctx, c10::ArrayRef<const torch::jit::Value*> inputs, ConversionInfo& conversion_info) {
142139
std::unordered_map<const torch::jit::Value*, ir::Input>& input_specs = conversion_info.inputs;
143-
std::unordered_map<const torch::jit::Value*, std::vector<ir::Input>> collection_input_spec = conversion_info.collection_input_spec_map;
140+
std::unordered_map<const torch::jit::Value*, std::vector<ir::Input>> collection_input_spec =
141+
conversion_info.collection_input_spec_map;
144142

145143
std::vector<const torch::jit::Value*> input_tensors;
146144
for (auto in : inputs) {
@@ -173,7 +171,7 @@ void AddInputs(
173171
"Cannot find an input spec associated with input: " << in->debugName());
174172
ir::Input spec;
175173
if (input_specs.find(in) != input_specs.end()) {
176-
spec = input_specs.find(in)->second;
174+
spec = input_specs.find(in)->second;
177175
} else {
178176
spec = collection_input_spec.find(in)->second[0]; // assume input is tensor
179177
}
@@ -559,8 +557,9 @@ std::set<std::string> ConvertableOpsInBlock(const torch::jit::Block* b) {
559557
}
560558

561559
bool OutputIsCollection(const torch::jit::Block* b) {
562-
for (auto out: b->outputs()) {
563-
if(out->type()->kind() == torch::jit::TypeKind::TupleType || out->type()->kind() == torch::jit::TypeKind::ListType) {
560+
for (auto out : b->outputs()) {
561+
if (out->type()->kind() == torch::jit::TypeKind::TupleType ||
562+
out->type()->kind() == torch::jit::TypeKind::ListType) {
564563
return true;
565564
}
566565
}

core/conversion/conversionctx/ConversionCtx.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ConversionCtx::ConversionCtx(BuilderSettings build_settings)
107107
}
108108

109109
cfg->setAvgTimingIterations(settings.num_avg_timing_iters);
110-
if (settings.workspace_size != 0){
110+
if (settings.workspace_size != 0) {
111111
cfg->setMemoryPoolLimit(nvinfer1::MemoryPoolType::kWORKSPACE, settings.workspace_size);
112112
}
113113

@@ -124,13 +124,13 @@ ConversionCtx::ConversionCtx(BuilderSettings build_settings)
124124
settings.enabled_precisions.find(nvinfer1::DataType::kFLOAT) == settings.enabled_precisions.end(),
125125
"DLA supports only fp16 or int8 precision");
126126
cfg->setDLACore(settings.device.dla_core);
127-
if (settings.dla_sram_size != 1048576){
127+
if (settings.dla_sram_size != 1048576) {
128128
cfg->setMemoryPoolLimit(nvinfer1::MemoryPoolType::kDLA_MANAGED_SRAM, settings.dla_sram_size);
129129
}
130-
if (settings.dla_local_dram_size != 1073741824){
130+
if (settings.dla_local_dram_size != 1073741824) {
131131
cfg->setMemoryPoolLimit(nvinfer1::MemoryPoolType::kDLA_LOCAL_DRAM, settings.dla_local_dram_size);
132132
}
133-
if (settings.dla_global_dram_size != 536870912){
133+
if (settings.dla_global_dram_size != 536870912) {
134134
cfg->setMemoryPoolLimit(nvinfer1::MemoryPoolType::kDLA_GLOBAL_DRAM, settings.dla_global_dram_size);
135135
}
136136
}

core/conversion/converters/converter_util.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ nvinfer1::ITensor* clamp(
207207
nvinfer1::ITensor* lower_bound,
208208
nvinfer1::ITensor* upper_bound,
209209
std::string const& name) {
210-
211210
auto max_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kMAX, x, lower_bound, "max layer for " + name);
212211
TORCHTRT_CHECK(max_layer, "Unable to create max layer for clamp");
213212
LOG_DEBUG(ctx->logger, "Create " << max_layer->getName() << " for clamp");
214213
auto max_itensor = max_layer->getOutput(0);
215214

216-
auto min_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kMIN, max_itensor, upper_bound, "min layer for " + name);
215+
auto min_layer =
216+
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kMIN, max_itensor, upper_bound, "min layer for " + name);
217217
TORCHTRT_CHECK(min_layer, "Unable to create min layer for clamp");
218218
LOG_DEBUG(ctx->logger, "Create " << min_layer->getName() << " for clamp");
219219
auto min_itensor = min_layer->getOutput(0);
@@ -227,13 +227,13 @@ nvinfer1::ITensor* clamp_to_input_dim(
227227
nvinfer1::ITensor* input_dim,
228228
int nbdims,
229229
std::string const& name) {
230-
231230
auto zero = torch::zeros({nbdims}).to(torch::kI32);
232231
auto zero_itensor = tensor_to_const(ctx, zero);
233232
auto one = torch::ones({nbdims}).to(torch::kI32);
234233
auto one_itensor = tensor_to_const(ctx, one);
235234

236-
auto upper_bound_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kSUB, input_dim, one_itensor, "sub layer for " + name);
235+
auto upper_bound_layer =
236+
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kSUB, input_dim, one_itensor, "sub layer for " + name);
237237
TORCHTRT_CHECK(upper_bound_layer, "Unable to create sub layer for clamp to inputDim");
238238
LOG_DEBUG(ctx->logger, "Create " << upper_bound_layer->getName() << " for clamp to inputDim");
239239
auto upper_bound = upper_bound_layer->getOutput(0);
@@ -243,7 +243,8 @@ nvinfer1::ITensor* clamp_to_input_dim(
243243
LOG_DEBUG(ctx->logger, "Create " << max_layer->getName() << " for clamp to inputDim");
244244
auto max_itensor = max_layer->getOutput(0);
245245

246-
auto min_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kMIN, max_itensor, upper_bound, "min layer for " + name);
246+
auto min_layer =
247+
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kMIN, max_itensor, upper_bound, "min layer for " + name);
247248
TORCHTRT_CHECK(min_layer, "Unable to create min_layer for clamp to inputDim");
248249
LOG_DEBUG(ctx->logger, "Create " << min_layer->getName() << " for clamp to inputDim");
249250
auto min_itensor = min_layer->getOutput(0);
@@ -257,7 +258,6 @@ nvinfer1::ITensor* normalize_indices(
257258
nvinfer1::ITensor* indices,
258259
int nbdims,
259260
std::string const& name) {
260-
261261
auto zero = torch::zeros({nbdims}).to(torch::kI32);
262262
auto neg = -torch::ones({nbdims}).to(torch::kI32);
263263
auto zero_itensor = tensor_to_const(ctx, zero);
@@ -307,17 +307,20 @@ nvinfer1::ITensor* get_slice_size(
307307
at::Tensor one_tensor = torch::ones({nbdims}).to(torch::kI32);
308308
auto one_itensor = tensor_to_const(ctx, one_tensor);
309309

310-
auto sub_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kSUB, end, start, "get_slice_size sub layer for " + name);
310+
auto sub_layer =
311+
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kSUB, end, start, "get_slice_size sub layer for " + name);
311312
TORCHTRT_CHECK(sub_layer, "Unable to create sub layer in calculate_output_size");
312313
LOG_DEBUG(ctx->logger, "Create " << sub_layer->getName() << " for calculate_output_size");
313314
auto sub_itensor = sub_layer->getOutput(0);
314315

315-
auto div_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kDIV, sub_itensor, stride, "get_slice_size div layer for " + name);
316+
auto div_layer = add_elementwise(
317+
ctx, nvinfer1::ElementWiseOperation::kDIV, sub_itensor, stride, "get_slice_size div layer for " + name);
316318
TORCHTRT_CHECK(div_layer, "Unable to create div layer in calculate_output_size");
317319
LOG_DEBUG(ctx->logger, "Create " << div_layer->getName() << " for calculate_output_size");
318320
auto div_itensor = div_layer->getOutput(0);
319321

320-
auto add_layer = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kSUM, div_itensor, one_itensor, "get_slice_size sum layer for " + name);
322+
auto add_layer = add_elementwise(
323+
ctx, nvinfer1::ElementWiseOperation::kSUM, div_itensor, one_itensor, "get_slice_size sum layer for " + name);
321324
TORCHTRT_CHECK(add_layer, "Unable to create add layer in calculate_output_size");
322325
LOG_DEBUG(ctx->logger, "Create " << add_layer->getName() << " for calculate_output_size");
323326
auto size_itensor = add_layer->getOutput(0);

core/conversion/converters/converter_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3+
#include <limits>
34
#include <map>
45
#include <string>
5-
#include <limits>
66

77
#include "core/conversion/conversionctx/ConversionCtx.h"
88
#include "core/conversion/converters/Weights.h"

0 commit comments

Comments
 (0)