Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime Error: Decorator script does not work dynamically compiled function #1969

Open
tuanle0910 opened this issue Nov 27, 2024 · 10 comments
Open

Comments

@tuanle0910
Copy link

Hi everyone, I get the Runtime Error when running the function below in Poetry environment with Python 3.11 and Pycharm IDE. I was able to run it successfully from Virtual Environment (the .venv). Can someone offer help on how to fix it?

import numpy as np
import onnx
import onnxruntime
import pandas as pd
from onnxscript import FLOAT
from onnxscript import opset20 as op

x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13])
y = np.array(x[len(x) - 3: len(x)], dtype=np.float32)
z = np.array(x[len(x)-8: len(x)-3], dtype=np.float32)

x_coef = np.array([1.1, 1.5, 1.7, 1.9, 1.2, 3.1, 4.5, 5.2, 8.5, 9.0, 11.0]
y_coef = np.array([8.5, 9.0, 11.0])
z_coef = np.array([5,7,9,9,10,11])

t1 = len(x)
t2 = len(y)
t3 = len(z)

const_term = 2.37698
h1 = 1.5689
h2 = 1.799

@script()
def conv(a1: FLOAT, a2: FLOAT, a3: FLOAT, a4: FLOAT, a5: FLOAT) -> FLOAT:
    const = op.Constant(value_float=const_term)
    # Define constants
    coeff1 = op.Constant(value_float=h1)
    coeff2 = op.Constant(value_float=h2)
    s = coeff1 * a1  + coeff2 * a2 - const

    for inx in range(t1):
        if a4 == x[inx]:
            s = s + x_coef[inx]
    for inx2 in range(t2):
        if a5 == y[inx2]:
            s = s + y_coef[inx2]
    for inx3 in range(t3):
        if a6 == z[inx3]:
            s = s + z_coef[inx3]
    
    return op.Exp(s)

Error Message
Traceback (most recent call last):
File "[File_path]/lib/python3.11/site-packages/onnxscript/_internal/ast_utils.py", line 18, in get_src_and_ast
src = inspect.getsource(func)
^^^^^^^^^^^^^^^^^^^^^^^
File "[file_path]/.asdf/python/3.11.9/lib/python3.11/inspect.py", line 1258, in getsource
lines, lnum = getsourcelines(object)
^^^^^^^^^^^^^^^^^^^^^^
File "[file_path]/.asdf/installs/python/3.11.9/lib/python3.11/inspect.py", line 1240, in getsourcelines
lines, lnum = findsource(object)
^^^^^^^^^^^^^^^^^^
File "[file_path]/.asdf/installs/python/3.11.9/lib/python3.11/inspect.py", line 1077, in findsource
raise OSError('could not get source code')
OSError: could not get source code

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevconsole.py", line 364, in runcode
coro = func()
^^^^^^
File "", line 1, in
File "", line 67, in [module_name]
File "[path]/lib/python3.11/site-packages/onnxscript/main.py", line 82, in transform
src, f_ast = ast_utils.get_src_and_ast(f)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "[path]/lib/python3.11/site-packages/onnxscript/_internal/ast_utils.py", line 20, in get_src_and_ast
raise RuntimeError(
RuntimeError: Decorator script does not work on dynamically compiled function conv.

@tuanle0910
Copy link
Author

@justinchuby @gramalingam @xadupre @titaiwangms Could either of you please help take a look at this problem?

@xadupre
Copy link
Member

xadupre commented Nov 27, 2024

x, x_coef, ... appear to be global variables. That's not allowed. They should become inputs or constant declared in the function body.

@tuanle0910
Copy link
Author

@xadupre Thank you very much for your quick reply. I tried both ways but the same error still appeared. I am not sure if the actual cause is because the @script() decorator was placed inside another function (e.g. def far()) ? The script above runs fine when it was placed outside of anything. However, I just check and even the given example here (https://onnxscript.ai/api/decorator.html) get the same error as above, so perhaps the actual cause was due to package version?

  • onnx: 1.16.0
  • onnxconverter-common: 1.13.0
  • onnxmltools: 1.12.0
  • onnxruntime: 1.16.3
  • onnxscript: 0.1.0dev20241126

@xadupre
Copy link
Member

xadupre commented Nov 28, 2024

I created PR #1970. I fixed your example to make it work. The conversion to onnx works with the main branch. I suggest you install onnxscript from github. I fixed a bug when your function is called in eager mode. onnxscript is very strict about types. In your example, x is an array of float32 but numpy uses int because all values are integer and dtype is not specifed. For onnx, that creates a type issue.

x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13])  # missing: , dtype=np.float32

@tle4336
Copy link

tle4336 commented Nov 28, 2024

@xadupre Thank you very much for your input. Regarding the installation of onnxscript: I used Pycharm as my IDE, and I install onnxscript via poetry install. Which version of onnxscript and onnxruntime are you using to make it work? Because I tried it with even a simple example like the following and still incur the Runtime Error like above.

@script()
def f() -> FLOAT:
    return op.Constant(value_float=0.0)

But when I tried the same script in another environment except the version of onnxscript is 1.17.0, and it works (no errors appeared) --- Does this mean something is going on between the two versions of onnxscript? But I will run the above code again with the dtype=np.float32 and let you know if the same issue still occurs. Something more subtle than missing the dtype=np.float32 is what I feel like missing/going awry.

From that same environment with the same script above with python run mypy, I encountered another errors like Unsupported operand type left for * [FLOAT/BOOL/INT] and the guy from mypy is blaming that you guys are doing the type exporting incorrectly (python/mypy#18200). Do you agree with him?

Also, the onnxscript=1.17.0 does not work with the latest version of OpsML (the 2.3.0 version). Any thought on when this compatibility issue can be fixed, because the environment that currently has RunTime error is using OpsML=2.3.0?

@xadupre
Copy link
Member

xadupre commented Nov 28, 2024

I used onnxscript from github, main branch. I did not check mypy. That's another topic. About opsml? I need more details. Onnxscript==1.17, you mean onnx? I don't how it is used in opset but i don't see why a more recent version would not work.

@tuanle0910
Copy link
Author

tuanle0910 commented Nov 29, 2024

@xadupre Thank you very much. I guess the topic related to mypy should be asked in onnx repo, rather than onnxscript?

When I looked over the problem that I encountered, it was a bit different from the above: the above script worked well when I put it into a new fresh environment in Pycharm, while it did not work when I put it into an existing environment (even when all the package versions of onnx, onnxscript, onnxconverter-common, onnxmltools and their package dependencies matched exactly the same between two environments). When I looked more closely into the error above, I realized that under the module inspect.py of onnxscript, the function getsourcelines(object) contains this particular line filename = getfile(object). Its output was shown to be <input> for the non-working environment. This caused the OSError: could not get source code as above because lines = linecache.getlines(file, module.__dict__) resulted in empty list []. Do you have any insights into why the filename was returned as <input>, especially how it started with < and ended with >?

@tuanle0910
Copy link
Author

@xadupre I hope your weekend went great! I'm sorry for having to touch base with you again. Just wonder if you have any idea on the root cause of the issue I described in the previous comment? I noticed that the filename= getfile(object) in the successful environment (i.e., the environment where the @script() for the function above worked well) was as lengthy as <ipython-input-69-13062711b1c1>. This led to lines being every lines within the function conv and much more, rather than the empty list as in the case of the environment where Runtime Error appears.

@xadupre
Copy link
Member

xadupre commented Dec 2, 2024

Would it be possible to run the command line from the the terminal and not inside PyCharm? Sometimes, those editors run your script from another script to intercept the standard output and it may alter the filenames.

@gramalingam
Copy link
Collaborator

A quick response: I haven't had a chance to look into the details yet. But I remember not being able to use the script decorator in a REPL interpreter. The onnxscript translator needs the source, and it can't get it (as the above error messages imply). You could try putting the script function inside a python file and importing it. (Not sure about your IDE however)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants