# TopKLayer's two outputs looks the same, and when I mark the second output as network's output,the en...

**URL:** <https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793>\
**Category:** TensorRT\
**Created:** [March 21, 2019, 8:49am UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793 "2019-03-21T08:49:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![275218173](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@275218173](https://forums.developer.nvidia.com/u/275218173)\
**Post date:** [March 21, 2019, 8:49am UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/1 "2019-03-21T08:49:52Z")

</div>

TensorRT version:5.0.2.6  
cuda version:9.0  
cudnn version:7.3  
python version:3.5

```auto
def build_engine(deploy_file, model_file, chars_top_k=5):
    with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.CaffeParser() as parser:
        builder.max_workspace_size = common.MB(200)
        builder.max_batch_size = ModelData.MAX_BATCH_SIZE
        # Set the parser's plugin factory. Note that we bind the factory to a reference so
        # that we can destroy it later. (parser.plugin_factory_ext is a write-only attribute)
        parser.plugin_factory_ext = fc_factory

        # Parse the model and build the engine.
        model_tensors = parser.parse(deploy=deploy_file, model=model_file, network=network, dtype=ModelData.DTYPE)

        prob_tensor = model_tensors.find(ModelData.OUTPUT_NAME)

        topk = network.add_topk(prob_tensor, trt.TopKOperation.MAX, chars_top_k, 2)
        topk.get_output(0).name = 'top%d_prob' % chars_top_k
        topk.get_output(1).name = 'top%d_chars' % chars_top_k

print("output1",topk.get_output(0))
        print("output2",topk.get_output(1))
        # topk.get_output(1).name = 'top%d_chari' % chars_top_k
        network.mark_output(topk.get_output(0))
        network.mark_output(topk.get_output(1))
        # network.mark_output(topk.get_output(1))
        # network.mark_output(prob_tensor)
        return builder.build_cuda_engine(network)

```

[TensorRT] ERROR: Output tensor top5\_chars of type Float produced from output of incompatible type Int32  
[TensorRT] ERROR: Could not compute dimensions for top5\_prob, because the network is not valid  
output1 \<tensorrt.tensorrt.ITensor object at 0x7fde7408bc70\>  
output2 \<tensorrt.tensorrt.ITensor object at 0x7fde7408bc70\>  
Traceback (most recent call last):  
File “/home/jiachx/gitcode/ThunisoftOCR/tensorrt/sample.py”, line 192, in   
main()  
File “/home/jiachx/gitcode/ThunisoftOCR/tensorrt/sample.py”, line 161, in main  
with build\_engine(deploy\_file, model\_file, chars\_top\_k=top\_k) as engine:  
AttributeError: **exit**

---

<div class="post-metadata">

**Author:** ![NVES](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves/32/14043_2.png) [@NVES](https://forums.developer.nvidia.com/u/NVES)\
**Post date:** [March 22, 2019, 5:14pm UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/2 "2019-03-22T17:14:47Z")

</div>

Hello,

to help us debug, can you please share a small repro that contains the model and build source that exhibit the error you are seeing?

---

<div class="post-metadata">

**Author:** ![275218173](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@275218173](https://forums.developer.nvidia.com/u/275218173)\
**Post date:** [March 22, 2019, 11:34pm UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/3 "2019-03-22T23:34:21Z")

</div>

here is the code，the deploy\_file and model\_file is in the tensorrt data/mnist

```auto
import tensorrt as trt

TRT_LOGGER = trt.Logger(trt.Logger.WARNING)

def build_engine(deploy_file, model_file):
    with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.CaffeParser() as parser:
        builder.max_workspace_size = 200 * 1 << 20
        builder.max_batch_size = 128
        # Set the parser's plugin factory. Note that we bind the factory to a reference so
        # that we can destroy it later. (parser.plugin_factory_ext is a write-only attribute)

        # Parse the model and build the engine.
        model_tensors = parser.parse(deploy=deploy_file, model=model_file, network=network, dtype=trt.float32)

        prob_tensor = model_tensors.find("prob")
        network.mark_output(prob_tensor)
        return builder.build_cuda_engine(network)

def build_engine_with_topk(deploy_file, model_file, chars_top_k=5):
    with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.CaffeParser() as parser:
        builder.max_workspace_size = 200 * 1 << 20
        builder.max_batch_size = 128
        # Set the parser's plugin factory. Note that we bind the factory to a reference so
        # that we can destroy it later. (parser.plugin_factory_ext is a write-only attribute)

        # Parse the model and build the engine.
        model_tensors = parser.parse(deploy=deploy_file, model=model_file, network=network, dtype=trt.float32)

        prob_tensor = model_tensors.find("prob")

        topk = network.add_topk(prob_tensor, trt.TopKOperation.MAX, chars_top_k, 1)
        topk.get_output(0).name = 'top%d_prob' % chars_top_k
        topk.get_output(1).name = 'top%d_chars' % chars_top_k

        print("output1", topk.get_output(0))
        print("output2", topk.get_output(1))
        network.mark_output(topk.get_output(0))
        network.mark_output(topk.get_output(1)) # if you comment this line,no error produce
        return builder.build_cuda_engine(network)

deploy_file = '/home/jiachx/driver/TensorRT-5.0.2.6/data/mnist/deploy.prototxt'
model_file = '/home/jiachx/driver/TensorRT-5.0.2.6/data/mnist/mnist.caffemodel'

engine = build_engine(deploy_file, model_file)
# build_engine_with_topk produce error
engine_with_topk = build_engine_with_topk(deploy_file, model_file, chars_top_k=5)

```

---

<div class="post-metadata">

**Author:** ![275218173](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@275218173](https://forums.developer.nvidia.com/u/275218173)\
**Post date:** [March 27, 2019, 2:08am UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/4 "2019-03-27T02:08:46Z")

</div>

Is anybody solving this problem?

---

<div class="post-metadata">

**Author:** ![NVES](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves/32/14043_2.png) [@NVES](https://forums.developer.nvidia.com/u/NVES)\
**Post date:** [May 21, 2019, 5:51pm UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/5 "2019-05-21T17:51:06Z")

</div>

the case is being reviewed. thank you for your patience.

---

<div class="post-metadata">

**Author:** ![NVES](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves/32/14043_2.png) [@NVES](https://forums.developer.nvidia.com/u/NVES)\
**Post date:** [June 20, 2019, 4:21pm UTC](https://forums.developer.nvidia.com/t/topklayers-two-outputs-looks-the-same-and-when-i-mark-the-second-output-as-networks-output-the-en/71793/6 "2019-06-20T16:21:37Z")

</div>

Per engineering, this is fixed in TensorRT-5.1.5 and Container-19.05.
