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

Deepcopy of pjit functions failing (or not supported) #24838

Open
jlperla opened this issue Nov 11, 2024 · 0 comments
Open

Deepcopy of pjit functions failing (or not supported) #24838

jlperla opened this issue Nov 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jlperla
Copy link

jlperla commented Nov 11, 2024

Description

While trying to get jsonargparse to work with defaults on activation functions, I ran into omni-us/jsonargparse#619 (comment)

The preferred MWE from the developer there is repeated below

>>> from copy import deepcopy
>>> from dataclasses import dataclass, asdict
>>> from flax import nnx
>>> from typing import Callable
>>> @dataclass
... class Settings:
...     width: int = 64
...     activation: Callable = nnx.relu
...     final_activation: Callable = nnx.softplus
>>> deepcopy(nnx.softplus) is nnx.softplus
False
>>> nnx.softplus
<PjitFunction of <function softplus at 0x7d73a9f0fb50>>
>>> deepcopy(nnx.softplus)
<PjitFunction>

It seems like the issue is that a deepcopy and perhaps a asdict of the jit'd functions is failing or not supported.

As said in that post, the workaround shows there is no intrinsic reason deepcopy can't be supported. If we put

from types import MethodType
nnx.softplus.__deepcopy__ = MethodType(lambda self, memo: self, nnx.softplus)

Then things work great.

System info (python version, jaxlib version, accelerator, etc.)

This applies to all versions.

@jlperla jlperla added the bug Something isn't working label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant