kendryte / nncase

Open deep learning compiler stack for Kendryte AI accelerators ✨
Apache License 2.0
752 stars 183 forks source link

量化类型int8与int16设置无效 #807

Closed TheOnlyoe closed 1 year ago

TheOnlyoe commented 1 year ago

问题 使用例程中的“编译uint8量化tflite模型”,模型使用的样例中的‘examples/20classes_yolo/model/20classes_yolo.tflite’,修改量化类型为int16,输出模型大小无任何变化,Netron查看ir_target_dep_after_quant下的pb文件,卷积核等各项参数仍然是uint8类型 compile_options.quant_type = 'int16' # or 'int8' 'int16' compile_options.w_quant_type = 'int16' # or 'int8' 'int16'

To Reproduce 使用的转换代码

import numpy as np

def read_model_file(model_file):
    with open(model_file, 'rb') as f:
        model_content = f.read()
    return model_content

def generate_data(shape, batch):
    shape[0] *= batch
    data = np.random.rand(*shape).astype(np.float32)
    return data

def main():
    model='examples/20classes_yolo/model/20classes_yolo.tflite'
    input_shape = [1,240,320,3]
    target = 'k210'

    # compile_options
    compile_options = nncase.CompileOptions()
    compile_options.target = target
    compile_options.input_type = 'float32'
    compile_options.input_layout = 'NHWC'
    compile_options.output_layout = 'NHWC'
    compile_options.dump_ir = True
    compile_options.dump_asm = True
    compile_options.dump_dir = 'tmp'

    # compiler
    compiler = nncase.Compiler(compile_options)

    # import_options
    import_options = nncase.ImportOptions()

    # quantize model
    compile_options.quant_type = 'int16' # or 'int8' 'int16'
    compile_options.w_quant_type = 'int16' # or 'int8' 'int16'

    # ptq_options
    ptq_options = nncase.PTQTensorOptions()
    ptq_options.samples_count = 10
    ptq_options.set_tensor_data(generate_data(input_shape, ptq_options.samples_count).tobytes())

    # import
    model_content = read_model_file(model)
    compiler.import_tflite(model_content, import_options)

    # compile
    compiler.use_ptq(ptq_options)
    compiler.compile()

    # kmodel
    kmodel = compiler.gencode_tobytes()
    with open('test.kmodel', 'wb') as f:
        f.write(kmodel)

if __name__ == '__main__':
    main()

转换出来的结果 image

Expected behavior 将量化类型更改为int16

Origin model and code If applicable, add orgin model and code to help explain your problem.

Environment (please complete the following information):

curioyang commented 1 year ago

image 顺序错了

TheOnlyoe commented 1 year ago

image 顺序错了

我尝试将这两句移到前面,但是最终的结果似乎并没有改变

# compile_options
    compile_options = nncase.CompileOptions()
    compile_options.target = target
    compile_options.input_type = 'float32'
    compile_options.input_layout = 'NHWC'
    compile_options.output_layout = 'NHWC'
    compile_options.dump_ir = True
    compile_options.dump_asm = True
    compile_options.dump_dir = 'tmp'
    # quantize model
    compile_options.quant_type = 'int16' # or 'int8' 'int16'
    compile_options.w_quant_type = 'int16' # or 'int8' 'int16'
    # compiler
    compiler = nncase.Compiler(compile_options)

    # import_options
    import_options = nncase.ImportOptions()

image

curioyang commented 1 year ago

@TheOnlyoe k210只支持uint8量化