Skip to content

Commit

Permalink
[cherry-pick][RKNPU[NNA][MtkAPU]remove enable int8 in opbridge (#5591) (
Browse files Browse the repository at this point in the history
  • Loading branch information
yingshengBD authored Mar 8, 2021
1 parent e76d603 commit a991471
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 165 deletions.
5 changes: 0 additions & 5 deletions lite/core/mir/quantized_op_attributes_inference_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ void QuantizedOpAttributesInferencePass::Apply(
is_quantized = false;
}
}

// Fix the missing of the attribute 'enable_int8'.
if (is_quantized) {
inst.mutable_op_info()->SetAttr("enable_int8", true);
}
}
VLOG(5) << "\n" << Visualize(graph.get());
}
Expand Down
10 changes: 4 additions & 6 deletions lite/kernels/apu/bridges/conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ int ConvConverter(void* ctx, OpLite* op, KernelBase* kernel) {
int neuron_errCode;
VLOG(3) << "[APU] Converting [" << op_type << "]";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto input_name = op_info->Input("Input").front();
auto input_scale_name = "Input0_scale";
Expand All @@ -54,6 +51,10 @@ int ConvConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto output = scope->FindMutableTensor(output_name);
auto output_dims = output->dims();

CHECK(op_info->HasInputScale(input_scale_name, true) &&
op_info->HasInputScale(filter_scale_name, true) &&
op_info->HasOutputScale(output_scale_name, true));

auto bs = input_dims[0];
auto ic = input_dims[1];
auto oc = filter_dims[0];
Expand Down Expand Up @@ -100,11 +101,8 @@ int ConvConverter(void* ctx, OpLite* op, KernelBase* kernel) {
input_dims,
filter_dims);

CHECK(op_info->HasInputScale(input_scale_name, true));
auto input_scale = op_info->GetInputScale(input_scale_name, true)[0];
CHECK(op_info->HasInputScale(filter_scale_name, true));
auto filter_scale = op_info->GetInputScale(filter_scale_name, true);
CHECK(op_info->HasOutputScale(output_scale_name, true));
auto output_scale = op_info->GetOutputScale(output_scale_name, true)[0];
auto orig_output_scale = op_info->GetOutputScale(output_scale_name, true)[0];

Expand Down
10 changes: 4 additions & 6 deletions lite/kernels/apu/bridges/conv_transpose_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ int ConvTransposeConverter(void *ctx, OpLite *op, KernelBase *kernel) {
int neuron_errCode;
VLOG(3) << "[APU] Converting [" << op_type << "]";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input, output and op attributes
auto input_name = op_info->Input("Input").front();
auto input_scale_name = "Input0_scale";
Expand All @@ -54,6 +51,10 @@ int ConvTransposeConverter(void *ctx, OpLite *op, KernelBase *kernel) {
auto output_name = op_info->Output("Output").front();
auto output_scale_name = "Output0_scale";

CHECK(op_info->HasInputScale(input_scale_name, true) &&
op_info->HasInputScale(filter_scale_name, true) &&
op_info->HasOutputScale(output_scale_name, true));

auto strides = op_info->GetAttr<std::vector<int>>("strides");
CHECK_EQ(strides.size(), 2L);
std::vector<int> paddings = op_info->GetAttr<std::vector<int>>("paddings");
Expand Down Expand Up @@ -121,11 +122,8 @@ int ConvTransposeConverter(void *ctx, OpLite *op, KernelBase *kernel) {
}
output_dims.push_back(filter_dims[1]);

CHECK(op_info->HasInputScale(input_scale_name, true));
auto input_scale = op_info->GetInputScale(input_scale_name, true)[0];
CHECK(op_info->HasInputScale(filter_scale_name, true));
auto filter_scale = op_info->GetInputScale(filter_scale_name, true);
CHECK(op_info->HasOutputScale(output_scale_name, true));
auto output_scale = op_info->GetOutputScale(output_scale_name, true)[0];

VLOG(3) << "strides.size(): " << strides.size() << " ,groups: " << groups
Expand Down
10 changes: 4 additions & 6 deletions lite/kernels/apu/bridges/fc_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[APU] Converting [" + op_type + "]";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto input_name = op_info->Input("Input").front();
auto input_scale_name = "Input0_scale";
Expand All @@ -50,6 +47,10 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto out = scope->FindMutableTensor(out_name);
auto out_dims = out->dims();

CHECK(op_info->HasInputScale(input_scale_name, true) &&
op_info->HasInputScale(w_scale_name, true) &&
op_info->HasOutputScale(out_scale_name, true));

int in_num_col_dims = op_info->GetAttr<int>("in_num_col_dims");
int m = input_dims.Slice(0, in_num_col_dims).production();
int k = input_dims.Slice(in_num_col_dims, input_dims.size()).production();
Expand All @@ -59,11 +60,8 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
<< " out_dims: " << out_dims << " m: " << m << " k: " << k
<< " n: " << n;

CHECK(op_info->HasInputScale(input_scale_name, true));
auto input_scale = op_info->GetInputScale(input_scale_name, true)[0];
CHECK(op_info->HasInputScale(w_scale_name, true));
auto w_scale = op_info->GetInputScale(w_scale_name, true);
CHECK(op_info->HasOutputScale(out_scale_name, true));
auto out_scale = op_info->GetOutputScale(out_scale_name, true)[0];

// Add input tensor type
Expand Down
7 changes: 2 additions & 5 deletions lite/kernels/apu/bridges/pool_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ int PoolConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[APU] Converting [" + op_type + "] ";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto x_name = op_info->Input("X").front();
auto x_scale_name = "X0_scale";
Expand All @@ -49,6 +46,8 @@ int PoolConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto ksize = op_info->GetAttr<std::vector<int>>("ksize");
std::vector<int> paddings = op_info->GetAttr<std::vector<int>>("paddings");

CHECK(op_info->HasInputScale(x_scale_name, true) &&
op_info->HasOutputScale(out_scale_name, true));
// Check pool mode
if ((pooling_type == "max") || (pooling_type == "avg")) {
} else {
Expand Down Expand Up @@ -92,9 +91,7 @@ int PoolConverter(void* ctx, OpLite* op, KernelBase* kernel) {
ksize);

// Add x tensor type
CHECK(op_info->HasInputScale(x_scale_name, true));
auto x_scale = op_info->GetInputScale(x_scale_name, true)[0];
CHECK(op_info->HasOutputScale(out_scale_name, true));
auto out_scale = op_info->GetOutputScale(out_scale_name, true)[0];

NeuronOperandType xType;
Expand Down
8 changes: 3 additions & 5 deletions lite/kernels/apu/bridges/softmax_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ int SoftmaxConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[APU] Converting [" + op_type + "]";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto x_name = op_info->Input("X").front();
auto x = scope->FindMutableTensor(x_name);
Expand All @@ -44,15 +41,16 @@ int SoftmaxConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto out_name = op_info->Output("Out").front();
auto out_scale_name = "Out0_scale";

CHECK(op_info->HasInputScale(x_scale_name, true) &&
op_info->HasOutputScale(out_scale_name, true));

// Check output shape
auto axis = op_info->GetAttr<int>("axis");
if (axis < 0) {
axis += x_rank;
}

CHECK(op_info->HasInputScale(x_scale_name, true));
auto input_scale = op_info->GetInputScale(x_scale_name, true)[0];
CHECK(op_info->HasOutputScale(out_scale_name, true));
auto out_scale = op_info->GetOutputScale(out_scale_name, true)[0];

// Check output scale
Expand Down
10 changes: 4 additions & 6 deletions lite/kernels/imagination_nna/bridges/conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ int ConvConverter(void *ctx, OpLite *op, KernelBase *kernel) {
auto scope = op->scope();
VLOG(3) << "[NNA] Converting " << op_type << "... ";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto input_name = op_info->Input("Input").front();
auto input_scale_name = "Input0_scale";
Expand All @@ -50,6 +47,10 @@ int ConvConverter(void *ctx, OpLite *op, KernelBase *kernel) {
auto output = scope->FindMutableTensor(output_name);
auto output_dims = output->dims();

CHECK(op_info->HasInputScale(input_scale_name, true) &&
op_info->HasInputScale(filter_scale_name, true) &&
op_info->HasOutputScale(output_scale_name, true));

auto bs = input_dims[0];
auto ic = input_dims[1];
auto oc = filter_dims[0];
Expand All @@ -73,12 +74,9 @@ int ConvConverter(void *ctx, OpLite *op, KernelBase *kernel) {
CHECK_EQ(strides.size(), 2L);
CHECK_EQ(dilations.size(), 2L);

CHECK(op_info->HasInputScale(input_scale_name, true));
float input_scale = op_info->GetInputScale(input_scale_name, true)[0];
CHECK(op_info->HasInputScale(filter_scale_name, true));
std::vector<float> weight_scale =
op_info->GetInputScale(filter_scale_name, true);
CHECK(op_info->HasOutputScale(output_scale_name, true));
float output_scale = op_info->GetOutputScale(output_scale_name, true)[0];

TensorInfo qnt;
Expand Down
10 changes: 4 additions & 6 deletions lite/kernels/imagination_nna/bridges/fc_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[NNA] Converting " + op_type + "...";

CHECK(op_info->HasAttr("enable_int8"));
CHECK(op_info->GetAttr<bool>("enable_int8"));

auto input_name = op_info->Input("Input").front();
auto input_scale_name = "Input0_scale";
auto input = scope->FindTensor(input_name);
Expand All @@ -48,6 +45,10 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto out = scope->FindTensor(out_name);
auto out_dims = out->dims();

CHECK(op_info->HasInputScale(input_scale_name, true) &&
op_info->HasInputScale(weight_scale_name, true) &&
op_info->HasOutputScale(out_scale_name, true));

// notes : m, input row
// k, input col
// n, weight col
Expand All @@ -63,12 +64,9 @@ int FCConverter(void* ctx, OpLite* op, KernelBase* kernel) {
VLOG(3) << "[NNA] input dims: " << input_dims << " w dims: " << w_dims
<< " m: " << m << " k: " << k << " n: " << n;

CHECK(op_info->HasInputScale(input_scale_name, true));
float input_scale = op_info->GetInputScale(input_scale_name, true)[0];
CHECK(op_info->HasInputScale(weight_scale_name, true));
std::vector<float> weight_scale =
op_info->GetInputScale(weight_scale_name, true);
CHECK(op_info->HasOutputScale(out_scale_name, true));
float output_scale = op_info->GetOutputScale(out_scale_name, true)[0];

// Create input node and reshape it to (m, k, 1, 1)
Expand Down
3 changes: 0 additions & 3 deletions lite/kernels/imagination_nna/bridges/pool_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ int PoolConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[NNA] Converting " + op_type + "...";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto x_name = op_info->Input("X").front();
auto x_scale_name = "X0_scale";
Expand Down
9 changes: 2 additions & 7 deletions lite/kernels/imagination_nna/bridges/softmax_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ int SoftmaxConverter(void* ctx, OpLite* op, KernelBase* kernel) {
auto scope = op->scope();
VLOG(3) << "[NNA] Converting " + op_type + "...";

CHECK(op_info->HasAttr("enable_int8") &&
op_info->GetAttr<bool>("enable_int8"));

// Get input and output vars and op attributes
auto x_name = op_info->Input("X").front();
auto x_scale_name = "X0_scale";
Expand All @@ -45,10 +42,8 @@ int SoftmaxConverter(void* ctx, OpLite* op, KernelBase* kernel) {
axis += x_rank;
}

if (op_info->HasAttr("enable_int8")) {
CHECK(op_info->HasOutputScale(out_scale_name, true));
if (op_info->HasOutputScale(out_scale_name, true)) {
float output_scale = op_info->GetOutputScale(out_scale_name, true)[0];

// X node
std::shared_ptr<Node> x_node = nullptr;
if (graph->Has(x_name)) {
Expand All @@ -65,7 +60,7 @@ int SoftmaxConverter(void* ctx, OpLite* op, KernelBase* kernel) {

graph->Add(out_name, softmax_out_tensor, IMGDNN_TYPE_Q_U8);
} else {
LOG(FATAL) << "[NNA] Softmax: has no enable_int8 attribute.";
LOG(FATAL) << "[NNA] Softmax: is not quantized.";
}

return REBUILD_WHEN_SHAPE_CHANGED;
Expand Down
22 changes: 14 additions & 8 deletions lite/kernels/rknpu/bridges/act_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ int ActConverter(void* ctx, OpLite* op, KernelBase* kernel) {
int bit_length = 8;
PrecisionType precision = PRECISION(kFloat);

if (op_info->HasAttr("enable_int8")) {
enable_int8 = op_info->GetAttr<bool>("enable_int8");
CHECK(op_info->HasInputScale(x_scale_name, true));
if (op_info->HasInputScale(x_scale_name, true) &&
op_info->HasOutputScale(out_scale_name, true)) {
enable_int8 = true;
input_scale = op_info->GetInputScale(x_scale_name, true)[0];
bit_length = op_info->GetAttr<int>("bit_length");
CHECK(op_info->HasOutputScale(out_scale_name, true));
output_scale = op_info->GetOutputScale(out_scale_name, true)[0];

if (enable_int8) {
precision = PRECISION(kInt8);
}
precision = PRECISION(kInt8);
} else {
enable_int8 = false;
LOG(WARNING) << "[RK-NPU] the op is float-type " << op_type;
precision = PRECISION(kFloat);
}

for (size_t i = 0; i < x_dims.size(); i++) {
Expand Down Expand Up @@ -116,6 +116,9 @@ int ActConverter(void* ctx, OpLite* op, KernelBase* kernel) {
} else if (op_type == "sigmoid") {
auto sigmoid = rGraph->AddOperator(
rk::nn::OperatorType::SIGMOID, inputs, outputs, nullptr);
} else if (op_type == "relu6") {
auto relu6 = rGraph->AddOperator(
rk::nn::OperatorType::RELU6, inputs, outputs, nullptr);
} else {
LOG(WARNING) << "[RK-NPU] only support relu and sigmod, "
"but the activation type is "
Expand All @@ -136,3 +139,6 @@ REGISTER_SUBGRAPH_BRIDGE(relu,
REGISTER_SUBGRAPH_BRIDGE(sigmoid,
kRKNPU,
paddle::lite::subgraph::rknpu::ActConverter);
REGISTER_SUBGRAPH_BRIDGE(relu6,
kRKNPU,
paddle::lite::subgraph::rknpu::ActConverter);
16 changes: 8 additions & 8 deletions lite/kernels/rknpu/bridges/batch_norm_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ int BatchNormConverter(void* ctx, OpLite* op, KernelBase* kernel) {
DataLayoutType layout = DATALAYOUT(kNCHW);
PrecisionType precision = PRECISION(kFloat);

if (op_info->HasAttr("enable_int8")) {
enable_int8 = op_info->GetAttr<bool>("enable_int8");
CHECK(op_info->HasInputScale(x_scale_name, true));
if (op_info->HasInputScale(x_scale_name, true) &&
op_info->HasOutputScale(y_scale_name, true)) {
enable_int8 = true;
input_scale = op_info->GetInputScale(x_scale_name, true)[0];
bit_length = op_info->GetAttr<int>("bit_length");
CHECK(op_info->HasOutputScale(y_scale_name, true));
output_scale = op_info->GetOutputScale(y_scale_name, true)[0];
if (enable_int8) {
precision = PRECISION(kInt8);
}
precision = PRECISION(kInt8);
} else {
enable_int8 = false;
LOG(WARNING) << "[RK-NPU] the op is float-type " << op_type;
precision = PRECISION(kFloat);
}

// X node
std::shared_ptr<Node> x_node = nullptr;
if (graph->Has(x_name)) {
Expand Down
15 changes: 7 additions & 8 deletions lite/kernels/rknpu/bridges/concat_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ int ConcatConverter(void* ctx, OpLite* op, KernelBase* kernel) {
DataLayoutType layout = DATALAYOUT(kNCHW);
PrecisionType precision = PRECISION(kFloat);

if (op_info->HasAttr("enable_int8")) {
enable_int8 = op_info->GetAttr<bool>("enable_int8");
if (op_info->HasOutputScale(out_scale_name, true)) {
enable_int8 = true;
bit_length = op_info->GetAttr<int>("bit_length");
CHECK(op_info->HasOutputScale(out_scale_name, true));
output_scale = op_info->GetOutputScale(out_scale_name, true)[0];

if (enable_int8) {
precision = PRECISION(kInt8);
}
precision = PRECISION(kInt8);
} else {
enable_int8 = false;
LOG(WARNING) << "[RK-NPU] the op is float-type " << op_type;
precision = PRECISION(kFloat);
}

// Traverse all of input nodes which are added into the new created concat
// node
std::vector<std::shared_ptr<rk::nn::Tensor>> inputs;
Expand Down
Loading

0 comments on commit a991471

Please sign in to comment.