|
1 |
| -#include <sstream> |
2 |
| - |
3 | 1 | #include "core/conversion/conversion.h"
|
| 2 | +#include <torch/torch.h> |
| 3 | +#include <sstream> |
4 | 4 | #include "core/conversion/conversionctx/ConversionCtx.h"
|
5 | 5 | #include "core/conversion/converters/converters.h"
|
6 | 6 | #include "core/conversion/evaluators/evaluators.h"
|
@@ -234,10 +234,28 @@ void MarkOutputs(ConversionCtx* ctx, at::ArrayRef<const torch::jit::Value*> outp
|
234 | 234 | }
|
235 | 235 | }
|
236 | 236 | } else {
|
237 |
| - std::string name = std::string("output_") + std::to_string(ctx->num_outputs); |
| 237 | + bool setOutput = false; |
| 238 | + auto num_inputs = ctx->net->getNbInputs(); |
238 | 239 | auto out_tensor = it->second;
|
239 |
| - out_tensor->setName(name.c_str()); |
240 |
| - ctx->net->markOutput(*out_tensor); |
| 240 | + std::string name = std::string("output_") + std::to_string(ctx->num_outputs); |
| 241 | + |
| 242 | + // Check if the output tensor is one of the inputs to the network. If so, apply an identity layer to it. |
| 243 | + for (int64_t i = 0; i < num_inputs; i++) { |
| 244 | + if (out_tensor == ctx->net->getInput(i)) { |
| 245 | + LOG_DEBUG( |
| 246 | + "One of the inputs named " |
| 247 | + << ctx->net->getInput(i)->getName() |
| 248 | + << " to the network is marked as an output tensor. Applying an identity layer and marking this tensor as output"); |
| 249 | + auto id_out_tensor = converters::applyIdentityOp(ctx, out_tensor, name); |
| 250 | + ctx->net->markOutput(*id_out_tensor); |
| 251 | + setOutput = true; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + if (!setOutput) { |
| 256 | + out_tensor->setName(name.c_str()); |
| 257 | + ctx->net->markOutput(*out_tensor); |
| 258 | + } |
241 | 259 | LOG_INFO(
|
242 | 260 | ctx->logger, "Marking Output " << out->debugName() << " named " << name << " in engine (ctx.MarkOutput)");
|
243 | 261 | ctx->num_outputs += 1;
|
|
0 commit comments