Skip to content

Conversation

@chilo-ms
Copy link
Contributor

Description: Enable TRT provider option configuration for C#

Use key-value strings to configure TRT EP provider options and create session options with TRT EP specific configurations:

        var trtProviderOptions = new OrtTensorRTProviderOptions();
        var providerOptionsDict = new Dictionary<string, string>();
        providerOptionsDict["device_id"] = "0";
        providerOptionsDict["trt_int8_enable"] = "1";
        providerOptionsDict["trt_int8_calibration_table_name"] = "C:\calibration.flatbuffers";
        trtProviderOptions.UpdateOptions(providerOptionsDict);
        ....
        options.AppendExecutionProvider_Tensorrt(trtProviderOptions);

After configuration, call GetOptions() to get provider options as c# string to check

        var resultProviderOptionsDict = new Dictionary<string, string>();
        ProviderOptionsValueHelper.StringToDict(trtProviderOptions.GetOptions(), resultProviderOptionsDict);

Note: This PR is the modification of #7179

@chilo-ms chilo-ms requested a review from a team as a code owner May 24, 2021 09:47
@chilo-ms
Copy link
Contributor Author

/azp run orttraining-linux-ci-pipeline,orttraining-mac-ci-pipeline,orttraining-linux-gpu-ci-pipeline,centos7_cpu,Linux CPU Minimal Build E2E CI Pipeline,Linux Nuphar CI Pipeline,MacOS NoContribops CI Pipeline,Linux OpenVINO CI Pipeline,orttraining-distributed, orttraining-amd-gpu-ci-pipeline

@azure-pipelines
Copy link

Pull request contains merge conflicts.


virtual void* GetInfo() { return nullptr; } // Returns a provider specific information interface if it exists

virtual const ProviderOptions GetProviderOptions() { return {}; } // Returns a provider specific information interface if it exists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
#include "core/framework/provider_options.h"
Copy link
Member

@yuslepukhin yuslepukhin May 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you forward declare it here? As a reminder, you can use forward declarations in the folowing cases:

  1. pointer 2) reference 3) return by value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it forward declare here?
In provider_options.h, I think they are all complete type:

  using ProviderOptions = std::unordered_map<std::string, std::string>;
  using ProviderOptionsVector = std::vector;
  using ProviderOptionsMap = std::unordered_map<std::string, ProviderOptions>;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProviderOptions can be forward declared in this header. Under onnxruntime you can say
struct/class ProviderOptions.

/**
* Use this API to create the configuration of a TensorRT Execution Provider.
*/
ORT_API2_STATUS(CreateTensorRTProviderOptions, _Outptr_ OrtTensorRTProviderOptions** out);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, document all the params and state how to destroy the instance when done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. and move the definitions to tensorrt_provider_factory.h

* Get configuration of a TensorRT Execution Provider.
* \param allocator - a ptr to an instance of OrtAllocator obtained with CreateAllocator() or GetAllocatorWithDefaultOptions()
* the specified allocator will be used to allocate continuous buffers for output strings and lengths.
* \param ptr - is a null terminated string allocated using 'allocator'. The caller is responsible for freeing it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a null terminated string allocated using 'allocator'. The caller is responsible for freeing it.

UTF-8 string, state that one must use the same allocator to free it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. and move the definitions to tensorrt_provider_factory.h

jywu-msft
jywu-msft previously approved these changes Jun 10, 2021
RyanUnderhill
RyanUnderhill previously approved these changes Jun 11, 2021
Copy link
Contributor

@pranavsharma pranavsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment.

@chilo-ms chilo-ms dismissed stale reviews from RyanUnderhill and jywu-msft via b312090 June 15, 2021 09:48
@chilo-ms chilo-ms requested a review from pranavsharma June 22, 2021 03:40
trt_options.trt_int8_calibration_table_name = new char[internal_options.int8_calibration_table_name.size() + 1];
strcpy((char*)trt_options.trt_int8_calibration_table_name, internal_options.int8_calibration_table_name.c_str());
trt_options.trt_int8_calibration_table_name = new char[str_size + 1];
strncpy((char*)trt_options.trt_int8_calibration_table_name, internal_options.int8_calibration_table_name.c_str(), str_size + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strncpy won't add the null char. I would change this to strncy(dest, src, str_size); and then dest[str_size] = '\0';

Copy link
Contributor Author

@chilo-ms chilo-ms Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why strncpy won't add the null char?
In this case, the size of src is always same as str_size, so strncpy will always copy the null char of src and then stop due to str_size + 1 is reached. (Also, the c_str() returns char array with null char, so src contains null char at the end)

https://en.cppreference.com/w/c/string/byte/strncpy

Copy link
Contributor

@pranavsharma pranavsharma Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to be explicit.

Copy link
Contributor Author

@chilo-ms chilo-ms Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree to be explicit in order to avoid potential null termination bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants