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

Import error with Pytorch optimizers (AdamW) #3975

Closed
1 task done
ap4499 opened this issue Sep 12, 2024 · 1 comment
Closed
1 task done

Import error with Pytorch optimizers (AdamW) #3975

ap4499 opened this issue Sep 12, 2024 · 1 comment

Comments

@ap4499
Copy link

ap4499 commented Sep 12, 2024

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
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
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: <function upsample at 0x1234942c0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample at 0x123494360>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x123494680>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x123494720>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x123494860>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function interpolate at 0x1234949a0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_nearest at 0x123494c20>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_nearest at 0x123494cc0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x123494f40>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x123494fe0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x123495120>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function upsample_bilinear at 0x123495260>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function RNN.forward at 0x12358c5e0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function RNN.forward at 0x12358c860>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function LSTM.forward at 0x12358cf40>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function LSTM.forward at 0x12358d120>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function GRU.forward at 0x12358d580>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function GRU.forward at 0x12358d760>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x1236405e0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x123640680>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x1236407c0>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function tensordot at 0x123640900>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x123640f40>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x123641080>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x123641120>.
/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_jit_internal.py:872: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function norm at 0x1236411c0>.
Traceback (most recent call last):
  File "<string>", line 43, in <module>
  File "<frozen runpy>", line 229, in run_module
  File "<frozen runpy>", line 88, in _run_code
  File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/main.py", line 17, in <module>
  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 <module>
  File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/_dynamo/convert_frame.py", line 39, in <module>
  File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/fx/experimental/symbolic_shapes.py", line 48, in <module>
  File "/var/folders/gv/yy0v6h9s32d63pwl8nx1mgcw0000gn/T/serious_python_tempDgq7zi/__pypackages__/torch/fx/experimental/_config.py", line 76, in <module>
  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 ap4499 changed the title Serious_Python, import error with AdamW (Pytorch Optimizer) Import error with AdamW (Pytorch Optimizer) Sep 12, 2024
@ap4499 ap4499 changed the title Import error with AdamW (Pytorch Optimizer) Import error with Pytorch optimizers (AdamW) Sep 12, 2024
@ap4499
Copy link
Author

ap4499 commented Sep 27, 2024

I have posted the solution to this on #3060.

Set to be fixed in up coming versions.

@ap4499 ap4499 closed this as completed Sep 27, 2024
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

1 participant