onnx / keras-onnx

Convert tf.keras/Keras models to ONNX
Apache License 2.0
379 stars 109 forks source link

UnboundLocalError in _make_range_non_const #640

Closed tdp2110 closed 3 years ago

tdp2110 commented 3 years ago

Hello!

Thanks again for the super useful library! I was just using your tool and hit the following error:

... builtin.py in _make_range_non_const
UnboundLocalError: local variable 'diff_output' referenced before assignment

We can see the source

def _make_range_non_const(scope, operator, container, start, limit, delta, onnx_type):
    oopb = OnnxOperatorBuilder(container, scope)
    diff_node = oopb.apply_sub([limit.name, start.name],
                               name=operator.full_name + '_diff')
    delta_cast = delta.name
    if onnx_type in [oopb.int32, oopb.int64]:                    <== if we don't enter ...
        diff_output = oopb.apply_cast(diff_node,
                                      to=oopb.float,
                                      name=operator.full_name + '_cast_diff')
        delta_cast = oopb.apply_cast(delta.name,
                                     to=oopb.float,
                                     name=operator.full_name + '_cast_delta')

    div_node = oopb.apply_div(diff_output + delta_cast,          <== then we never assign to diff_output but we read it
                              name=operator.full_name + '_div')

If onnx_type is not in [oopb.int32, oopb.int64], then we never assign to diff_output which is unconditionally used after the if-statement.

Maybe this should be a different kind of error which gives meaning to the user, or maybe it shouldn't be an error at all. I don't know!

jiafatom commented 3 years ago

Just checked in the fix, please pull keras2onnx master (see Readme) and retry.

tdp2110 commented 3 years ago

Thanks for that! That fixes the UnboundLocalError, but I quickly hit another error:

    div_node = oopb.apply_div(diff_output + delta_cast,
TypeError: can only concatenate list (not "str") to list

I hacked this by changing line 1198 from

delta_cast = delta.name

to

delta_cast = [delta.name]

and I was able to get my model to convert (to something!). Not sure if this is correct though 😃

jiafatom commented 3 years ago

Good catch, it is a good fix. I just checked in the fix you mentioned, please pull keras2onnx master again. Thank you!