@@ -654,6 +654,31 @@ static OpType _getRealOpType(OpType opType) {
654654 return opType;
655655 }
656656}
657+
658+ // A Conv / DepthwiseConv may be promoted to Int8 execution purely based on the
659+ // int8 quantAttr of its input/output tensors. Guard against promoting an op that
660+ // carries no quantized weight data (e.g. a Conv listed in skip_quant_op_names but
661+ // still surrounded by int8 tensors): the ConvInt8 executor would later dereference
662+ // a missing quan weight (null symmetricQuan) and crash. Non-conv ops are unaffected.
663+ static bool _convHasQuantWeight (const MNN ::Op* op) {
664+ auto opType = op->type ();
665+ if (opType != OpType_Convolution && opType != OpType_ConvolutionDepthwise) {
666+ return true ;
667+ }
668+ auto conv2d = op->main_as_Convolution2D ();
669+ if (nullptr == conv2d) {
670+ return false ;
671+ }
672+ auto quan = conv2d->quanParameter ();
673+ if (nullptr != quan && (nullptr != quan->buffer () || nullptr != conv2d->external ())) {
674+ return true ;
675+ }
676+ auto symmetric = conv2d->symmetricQuan ();
677+ if (nullptr != symmetric && nullptr != symmetric->weight ()) {
678+ return true ;
679+ }
680+ return false ;
681+ }
657682void * CPUBackend::onMapTensor (Tensor::MapType mtype, Tensor::DimensionType dtype, const Tensor* srcTensor) {
658683 if (static_cast <int >(getBytes (this , srcTensor)) != srcTensor->getType ().bytes ()) {
659684 return nullptr ;
@@ -732,7 +757,7 @@ Execution* CPUBackend::onCreate(const std::vector<Tensor*>& inputs, const std::v
732757 if (outputs.size () > 0 && inputs.size () > 0 ) {
733758 bool outputQuant = TensorUtils::getDescribe (outputs[0 ])->quantAttr != nullptr && TensorUtils::getDescribe (outputs[0 ])->quantAttr ->type == DataType_DT_INT8;
734759 bool inputQuant = TensorUtils::getDescribe (inputs[0 ])->quantAttr != nullptr && TensorUtils::getDescribe (inputs[0 ])->quantAttr ->type == DataType_DT_INT8;
735- if (inputQuant && outputQuant) {
760+ if (inputQuant && outputQuant && _convHasQuantWeight (op) ) {
736761 opType = _getRealOpType (opType);
737762 }
738763 }
0 commit comments