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