Comments (2)
To anyone reading this, I solved this problem by defining my own metaclass:
metaclass_of_struct = type(pz.Struct)
class PytreeMeta(metaclass_of_struct):
def __new__(cls, name, bases, namespace):
if pz.Struct not in bases and not any(
issubclass(base, pz.Struct) for base in bases
):
bases = (pz.Struct,) + bases
new_cls = type.__new__(cls, name, bases, namespace)
return pz.pytree_dataclass(new_cls)
which my "overwrite" class for Struct
declares as its metaclass.
from penzai.
Thanks for the question! As the error message suggests, this was actually an explicit design choice in Penzai to not automatically use @pz.pytree_dataclass
for all subclasses. The main reasons:
- If you have multiple levels of Python object inheritance, it can be non-obvious which ones are dataclasses and which ones aren't. For instance, if you subclass an ordinary Python dataclass, you don't get a dataclass back. Requiring the explicit decorator makes it clear which classes are dataclasses and which aren't.
- Inheritance between dataclasses is really tricky; it can silently reorder the attributes that you've declared, or overwrite methods in unexpected ways. The
@pz.pytree_dataclass
decorator takes extra arguments that make sure you (and readers of your code) can tell what the decorator is going to do.
I'd advise against directly subclassing the metaclass of Struct
in this way, and I can't guarantee that this will always work. Instead I'd recommend just adding the one-line @pz.pytree_dataclass
decorator for each of the classes you want to define. Or, if you really want automatic inheritance of "pytree-dataclassness", you can just write your own metaclass from scratch, and not inherit from Struct
at all. (Or you could use something like eqx.Module
, which has implicit inheritance semantics.)
from penzai.
Related Issues (8)
- Customized `selector` functions for user-defined `Pytree` classes HOT 3
- Synchronize sliders across array data in Pytree HOT 3
- Penzai as a training framework HOT 1
- importing model from other jax-based libraries for weight visualization HOT 1
- Keep axes in nmap HOT 5
- Auto unwrap for full reductions. HOT 2
- Feature request: Animate button
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from penzai.