flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.62k stars 455 forks source link

Import error with Pytorch optimizers (AdamW) #3975

Closed ap4499 closed 2 months ago

ap4499 commented 2 months ago

Duplicate Check

Describe the bug

When using the optimizer AdamW within Pytorch, and using Flet Build macos, the application errors with:

"OSError: could not get source code"

It also occurs with NAdam as well.

Note, that Flet Build is still stripping off all bin directories (0.24.1), and these have to be manually inserted to get the code to the failure point, else it will fail due to lack of the bin directory. (see #3060 )

To manually get the bin folder working: Take the bin folder from the development environment, location: /.venv/lib/python3.12/site-packages/torch/bin

and copy it to: /Users/[USER]/Library/Caches/flet_demo-1.0.0-1/app/pypackages/torch

Code sample

Code ```python import flet as ft import torch import torch.nn as nn import torch.optim as optim # Simple model (replace with your actual model) class SimpleModel(nn.Module): def __init__(self): super(SimpleModel, self).__init__() self.fc = nn.Linear(10, 1) # Adjust input/output sizes as needed def forward(self, x): return self.fc(x) # Initialize model and optimizer model = SimpleModel() optimizer = optim.AdamW(model.parameters(), lr=0.001) def main(page: ft.Page): page.title = "PyTorch Model with Flet" # Sample input data input_data = torch.randn(1, 10) def train_click(e): # Sample training loop output = model(input_data) loss = torch.mean(output) # Simple loss for illustration optimizer.zero_grad() loss.backward() optimizer.step() page.snack_bar = ft.SnackBar(ft.Text(f"Loss: {loss.item()}")) page.snack_bar.open = True page.update() page.add( ft.ElevatedButton("Train", on_click=train_click), ) ft.app(target=main) ```

To reproduce

To reproduce this error:

Environment Python 3.12.5 (the issue is also present in 3.11.9) Flet 0.24.1 Pytorch 2.3.1

Command: Flet CLI = flet build macos

Expected behavior

The application should operate correctly.

Screenshots / Videos

Captures [Upload media here]

Operating System

macOS

Operating system details

MacOS 14.6.1

Flet version

0.24.1

Regression

No, it isn't

Suggestions

No response

Logs

Logs ```console The full error log is contained below: /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . /var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: . Traceback (most recent call last): File "", line 43, in File "", line 229, in run_module File "", line 88, in _run_code File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/main.py", line 17, in File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/optim/adamw.py", line 72, in __init__ File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/optim/optimizer.py", line 367, in __init__ File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_compile.py", line 26, in inner File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_dynamo/__init__.py", line 2, in File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_dynamo/convert_frame.py", line 39, in File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/fx/experimental/symbolic_shapes.py", line 48, in File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/fx/experimental/_config.py", line 76, in File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/utils/_config_module.py", line 58, in install_config_module File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/utils/_config_module.py", line 75, in get_assignments_with_compile_ignored_comments File "./inspect.py", line 1262, in getsource File "./inspect.py", line 1244, in getsourcelines File "./inspect.py", line 1081, in findsource OSError: could not get source code ```

Additional details

No response

ap4499 commented 2 months ago

I have posted the solution to this on #3060.

Set to be fixed in up coming versions.