kendryte / nncase

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

Unable to convert tflite to kmodel for use with K210 #1229

Closed code2k13 closed 3 months ago

code2k13 commented 3 months ago

Unable to convert from tflite to kmodel I am trying to export a custom model to kmodel format using nncase for k210. I am able to convert my model to tflite successfully but conversion to kmodel fails.

To Reproduce Run following code or run this kaggle notebook:

import nncase

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

def main():
    model='sparrow.tflite'
    target = 'k210'

    # compile_options
    compile_options = nncase.CompileOptions()
    compile_options.target = target
    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()

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

    # compile
    compiler.compile()

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

if __name__ == '__main__':
    main()

Output of the above code:

1. Import graph...
....
....
8. Generate code...
WARN: Cannot find a decompiler for section .rdata
.....
SUMMARY
INPUTS
0       conv2d_input    f32[1,32,32,1]
OUTPUTS
0       dense_2/Softmax_0       f32[1,10]

MEMORY USAGES
.input     4.00 KB      (4096 B)
.output   40.00 B       (40 B)
.data     67.38 KB      (68992 B)
MODEL    111.67 KB      (114352 B)
TOTAL    183.09 KB      (187480 B)

It generates a kmodel but gives this error when running on the device

init i2c:2 freq:100000

[MAIXPY]: sensor id = 9d
[MAIXPY]: find gc0328
model size err

Traceback (most recent call last):
  File "<stdin>", line 15, in <module>
ValueError: [MAIXPY]kpu: load error:2003, ERR_KMODEL_FORMAT: layer_header.body_size <=0
>
MicroPython v0.6.2-89-gd8901fd22 on 2024-06-17; Sipeed_M1 with kendryte-k210
Type "help()" for more information.

Expected behavior Model should load without error.

Environment (please complete the following information):

curioyang commented 3 months ago

@code2k13
Your K210 board is from Sipeed(MAIXPY). You can only use ncc to compile your tflite to kmodel. It means that the nncase version needs below 1.0. Ask Sipeed to get support.

code2k13 commented 3 months ago

@curioyang my friend , you are a genius. I got it to work. Was trying since last 2 days. Thank you so much !