Skip to content

Commit 21f5b79

Browse files
authored
[Tensorrt] update features and bug fixes (#8935)
#8738 #8764 #8756 #8761 #8800 #8813 #8845 #8859 #8885 #8882 #8865 #8912
1 parent d1c0885 commit 21f5b79

File tree

153 files changed

+1889
-1096
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+1889
-1096
lines changed

lite/api/cxx_api_impl.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ void CxxPaddleApiImpl::Init(const lite_api::CxxConfig &config) {
9191
raw_predictor_->scope(), config.nnadapter_device_names());
9292
Context<TargetType::kNNAdapter>::SetNNAdapterContextProperties(
9393
raw_predictor_->scope(), config.nnadapter_context_properties());
94+
Context<TargetType::kNNAdapter>::SetNNAdapterContextCallback(
95+
raw_predictor_->scope(), config.nnadapter_context_callback());
9496
Context<TargetType::kNNAdapter>::SetNNAdapterModelCacheDir(
9597
raw_predictor_->scope(), config.nnadapter_model_cache_dir());
9698
Context<TargetType::kNNAdapter>::SetNNAdapterModelCacheBuffers(

lite/api/light_api_impl.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
7272
raw_predictor_->scope(), config.nnadapter_device_names());
7373
Context<TargetType::kNNAdapter>::SetNNAdapterContextProperties(
7474
raw_predictor_->scope(), config.nnadapter_context_properties());
75+
Context<TargetType::kNNAdapter>::SetNNAdapterContextCallback(
76+
raw_predictor_->scope(), config.nnadapter_context_callback());
7577
Context<TargetType::kNNAdapter>::SetNNAdapterModelCacheDir(
7678
raw_predictor_->scope(), config.nnadapter_model_cache_dir());
7779
Context<TargetType::kNNAdapter>::SetNNAdapterModelCacheBuffers(

lite/api/paddle_api.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#ifndef PADDLE_LITE_API_H_ // NOLINT
2121
#define PADDLE_LITE_API_H_
22+
#include <functional>
2223
#include <map>
2324
#include <memory>
2425
#include <string>
@@ -165,6 +166,8 @@ class LITE_API ConfigBase {
165166
// The NNAdapter context properties for device configuration, model
166167
// compilation and execution
167168
std::string nnadapter_context_properties_{};
169+
int (*nnadapter_context_callback_)(int event_id,
170+
void* user_data){nullptr}; // NOLINT
168171
// The directory to find and store the compiled NNAdapter models.
169172
std::string nnadapter_model_cache_dir_{""};
170173
// Dynamic shapes of the NNAdapter model
@@ -274,6 +277,26 @@ class LITE_API ConfigBase {
274277
const std::string& nnadapter_context_properties() const {
275278
return nnadapter_context_properties_;
276279
}
280+
// Set nnadapter_context_callback for NNAdapter device to get runtime
281+
// parameters.
282+
// For example:
283+
// cudaStream_t cuda_stream;
284+
// cudaStreamCreate(&cuda_stream);
285+
// int nnadapter_context_callback(int event_id, void* user_data) {
286+
// if (event_id == 0x0100) {
287+
// *(std::reinterpret_cast<cudaStream_t*>(user_data)) = cuda_stream;
288+
// }
289+
// return 0;
290+
// }
291+
void set_nnadapter_context_callback(
292+
int (*nnadapter_context_callback)(int event_id, void* user_data)) {
293+
nnadapter_context_callback_ = nnadapter_context_callback;
294+
}
295+
int (*nnadapter_context_callback() const)(int event_id, // NOLINT
296+
void* user_data) {
297+
return nnadapter_context_callback_;
298+
}
299+
277300
// Enable caching and set the directory to search and store the compiled
278301
// NNAdapter models in the file system.
279302
void set_nnadapter_model_cache_dir(const std::string& model_cache_dir) {

lite/backends/nnadapter/nnadapter/include/nnadapter/core/types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ typedef struct Operand {
3838
typedef struct Argument {
3939
int index;
4040
void* memory;
41-
void* (*access)(void* memory, NNAdapterOperandType* type);
41+
void* (*access)(void* memory,
42+
NNAdapterOperandType* type,
43+
void* device_buffer);
4244
} Argument;
4345

4446
typedef struct Operation {

lite/backends/nnadapter/nnadapter/include/nnadapter/driver/device.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ typedef struct Device {
2828
// Interfaces
2929
int (*open_device)(void** device);
3030
void (*close_device)(void* device);
31-
int (*create_context)(void* device, const char* properties, void** context);
31+
int (*create_context)(void* device,
32+
const char* properties,
33+
int (*callback)(int event_id, void* user_data),
34+
void** context);
3235
void (*destroy_context)(void* context);
3336
int (*validate_program)(void* context,
3437
const core::Model* model,

lite/backends/nnadapter/nnadapter/include/nnadapter/nnadapter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,7 @@ int NNAdapterDevice_getVersion(const NNAdapterDevice* device, int32_t* version);
23162316
int NNAdapterContext_create(NNAdapterDevice** devices,
23172317
uint32_t num_devices,
23182318
const char* properties,
2319+
int (*callback)(int event_id, void* user_data),
23192320
NNAdapterContext** context);
23202321
/**
23212322
* Release the context.
@@ -2489,7 +2490,8 @@ int NNAdapterExecution_setInput(NNAdapterExecution* execution,
24892490
int32_t index,
24902491
void* memory,
24912492
void* (*access)(void* memory,
2492-
NNAdapterOperandType* type));
2493+
NNAdapterOperandType* type,
2494+
void* device_buffer));
24932495
/**
24942496
* Set the output memory and the function used to access it.
24952497
*
@@ -2518,7 +2520,8 @@ int NNAdapterExecution_setOutput(NNAdapterExecution* execution,
25182520
int32_t index,
25192521
void* memory,
25202522
void* (*access)(void* memory,
2521-
NNAdapterOperandType* type));
2523+
NNAdapterOperandType* type,
2524+
void* device_buffer));
25222525
/**
25232526
* Start to run the execution synchronously.
25242527
*

lite/backends/nnadapter/nnadapter/include/nnadapter/operation/reduce.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616

17+
#include <iostream>
1718
namespace nnadapter {
1819
namespace operation {
1920

@@ -26,12 +27,15 @@ namespace operation {
2627
NNADAPTER_CHECK_EQ(output_count, 1); \
2728
/* Input */ \
2829
auto input_operand = input_operands[0]; \
30+
auto input_dimension_count = input_operand->type.dimensions.count; \
2931
NNADAPTER_VLOG(5) << "input_operand: " << OperandToString(input_operand); \
3032
/* Axes */ \
3133
auto axes_operand = input_operands[1]; \
3234
int axes_size = axes_operand->length / sizeof(int32_t); \
3335
auto axes_data = reinterpret_cast<int32_t*>(axes_operand->buffer); \
3436
for (int i = 0; i < axes_size; i++) { \
37+
axes_data[i] = axes_data[i] < 0 ? axes_data[i] + input_dimension_count \
38+
: axes_data[i]; \
3539
NNADAPTER_VLOG(5) << "axes[" << i << "]: " << axes_data[i]; \
3640
} \
3741
/* Keep_dim */ \

lite/backends/nnadapter/nnadapter/src/driver/amlogic_npu/driver.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ void CloseDevice(void* device) {
3838
}
3939
}
4040

41-
int CreateContext(void* device, const char* properties, void** context) {
41+
int CreateContext(void* device,
42+
const char* properties,
43+
int (*callback)(int event_id, void* user_data),
44+
void** context) {
4245
if (!device || !context) {
4346
return NNADAPTER_INVALID_PARAMETER;
4447
}

lite/backends/nnadapter/nnadapter/src/driver/amlogic_npu/engine.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int Program::CheckInputsAndOutputs(uint32_t input_count,
173173
// Get actual type
174174
auto& arg = input_arguments[i];
175175
NNAdapterOperandType type;
176-
arg.access(arg.memory, &type);
176+
arg.access(arg.memory, &type, nullptr);
177177
// Check dimensions count
178178
uint32_t count = type.dimensions.count;
179179
int32_t* data = type.dimensions.data;
@@ -210,7 +210,7 @@ int Program::Execute(uint32_t input_count,
210210
NNADAPTER_CHECK(arg.memory);
211211
NNADAPTER_CHECK(arg.access);
212212
auto type = input_types_[arg.index];
213-
auto buffer = arg.access(arg.memory, &type);
213+
auto buffer = arg.access(arg.memory, &type, nullptr);
214214
NNADAPTER_CHECK(buffer);
215215
auto length = GetOperandTypeBufferLength(type);
216216
if (IsUInt8AsymmPerLayerQuantType(type.precision)) {
@@ -243,7 +243,7 @@ int Program::Execute(uint32_t input_count,
243243
// TODO(hong19860320) Get the dimensions of the outputs from amlnpu_ddk
244244
// according to the dynamic dimensions of the inputs, fill them to 'type'
245245
// and call the 'access' function to re-allocate the host output memory
246-
auto buffer = arg.access(arg.memory, type);
246+
auto buffer = arg.access(arg.memory, type, nullptr);
247247
NNADAPTER_CHECK(buffer);
248248
auto length = GetOperandTypeBufferLength(*type);
249249
// Initialize the output info for the execution

lite/backends/nnadapter/nnadapter/src/driver/android_nnapi/driver.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ void CloseDevice(void* device) {
3838
}
3939
}
4040

41-
int CreateContext(void* device, const char* properties, void** context) {
41+
int CreateContext(void* device,
42+
const char* properties,
43+
int (*callback)(int event_id, void* user_data),
44+
void** context) {
4245
if (!device || !context) {
4346
return NNADAPTER_INVALID_PARAMETER;
4447
}

0 commit comments

Comments
 (0)