heheda12345 / MagPy

Apache License 2.0
30 stars 1 forks source link

Do magpy support print in compiled model? #46

Open hanwen-sun opened 3 days ago

hanwen-sun commented 3 days ago

Hello, I want to know whether magpy support side effect function like print. Dynamo will break the graph when meeting print. I write a simple demo to test magpy, but it seems that magpy do not support print. The test demo is:

import torch
from frontend.compile import compile

def builtin_function(x):
    x = x * 2
    print(x)
    x = x / 2

    return x

x = torch.randn(1024)
compiled = compile(builtin_function)
y = compiled(x)
print(y)

And the output is:

(<built-in function print>, [tensor([-1.2829,  2.9794, -3.1826,  ...,  0.3809,  0.3648, -0.0356])], {}) <class 'NotImplementedError'>
Traceback (most recent call last):
  File "/root/shw/graph_capture/MAGPY/MagPy/examples/magpy_test_builtin_functions.py", line 14, in <module>
    y = compiled(x)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/compile.py", line 77, in _fn
    raise e
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/compile.py", line 74, in _fn
    return fn(*args, **kwargs)
  File "/root/shw/graph_capture/MAGPY/MagPy/examples/magpy_test_builtin_functions.py", line 7, in builtin_function
    print(x)
  File "/root/shw/graph_capture/MAGPY/MagPy/examples/magpy_test_builtin_functions.py", line 7, in builtin_function
    print(x)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/tracer.py", line 58, in trace_func
    raise e
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/tracer.py", line 34, in trace_func
    record(frame, frame_id)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/guard_tracker.py", line 2827, in record
    trackers[-1].record(frame, frame_id)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/guard_tracker.py", line 979, in record
    raise e
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/guard_tracker.py", line 974, in record
    getattr(self, inst.opname)(inst)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/guard_tracker.py", line 2318, in CALL_FUNCTION
    self.call_function(func, args, kwargs)
  File "/root/shw/graph_capture/MAGPY/MagPy/frontend/guard_tracker.py", line 1996, in call_function
    raise NotImplementedError(func, args, kwargs)
NotImplementedError: (<built-in function print>, [tensor([-1.2829,  2.9794, -3.1826,  ...,  0.3809,  0.3648, -0.0356])], {})

There's no graph and guard function output.

heheda12345 commented 3 days ago

This error indicates that MagPy detects print has side effect, and throws an error to generate graph break. But the handling of graph break in the middle of user code is not very stable. You can try this flag to fall back to pure pytorch execution without compilation when MagPy meets some problem https://github.com/heheda12345/MagPy/blob/9819fc3242baa6509d7056bff70ba1d2126219dd/frontend/config.py#L9