AI prompts
base on A JAX research toolkit for building, editing, and visualizing neural networks. # Penzai
> **盆 ("pen", tray) 栽 ("zai", planting)** - *an ancient Chinese art of forming
trees and landscapes in miniature, also called penjing and an ancestor of the
Japanese art of bonsai.*
Penzai is a JAX library for writing models as legible, functional pytree data
structures, along with tools for visualizing, modifying, and analyzing them.
Penzai focuses on **making it easy to do stuff with models after they have been
trained**, making it a great choice for research involving reverse-engineering
or ablating model components, inspecting and probing internal activations,
performing model surgery, debugging architectures, and more. (But if you just
want to build and train a model, you can do that too!)
With Penzai, your neural networks could look like this:
![Screenshot of the Gemma model in Penzai](docs/_static/readme_teaser.png)
Penzai is structured as a collection of modular tools, designed together but
each useable independently:
* A superpowered interactive Python pretty-printer:
* [Treescope](https://treescope.readthedocs.io/en/stable/) (`pz.ts`):
A drop-in replacement for the ordinary IPython/Colab renderer, originally
a part of Penzai but now available as a standalone package. It's designed to
help understand Penzai models and other deeply-nested JAX pytrees, with
built-in support for visualizing arbitrary-dimensional NDArrays.
* A set of JAX tree and array manipulation utilities:
* `penzai.core.selectors` (`pz.select`): A pytree swiss-army-knife,
generalizing JAX's `.at[...].set(...)` syntax to arbitrary type-driven
pytree traversals, and making it easy to do complex rewrites or
on-the-fly patching of Penzai models and other data structures.
* `penzai.core.named_axes` (`pz.nx`): A lightweight named axis system which
lifts ordinary JAX functions to vectorize over named axes, and allows you to
seamlessly switch between named and positional programming styles without
having to learn a new array API.
* A declarative combinator-based neural network library, where models are
represented as easy-to-modify data structures:
* `penzai.nn` (`pz.nn`): An alternative to other neural network libraries like
Flax, Haiku, Keras, or Equinox, which exposes the full structure of your model's
forward pass using declarative combinators. Like Equinox, models are
represented as JAX PyTrees, which means you can see everything your model
does by pretty printing it, and inject new runtime logic with `jax.tree_util`.
However, `penzai.nn` models may also contain mutable variables at the leaves
of the tree, allowing them to keep track of mutable state and parameter
sharing.
* A modular implementation of common Transformer architectures, to support
research into interpretability, model surgery, and training dynamics:
* `penzai.models.transformer`: A reference Transformer implementation that
can load the pre-trained weights for the Gemma, Llama, Mistral, and
GPT-NeoX / Pythia architectures. Built using modular components and named
axes, to simplify complex model-manipulation workflows.
Documentation on Penzai can be found at
[https://penzai.readthedocs.io](https://penzai.readthedocs.io).
> [!IMPORTANT]
> Penzai 0.2 includes a number of breaking changes to the neural network API.
> These changes are intended to simplify common workflows
> by introducing first-class support for mutable state and parameter sharing
> and removing unnecessary boilerplate. You can read about the differences
> between the old "V1" API and the current "V2" API in the
> ["Changes in the V2 API"][v2_differences] overview.
>
> If you are currently using the V1 API and have not yet converted to the V2
> system, you can instead keep the old behavior by importing from the
> `penzai.deprecated.v1` submodule, e.g. ::
>
> ```python
> from penzai.deprecated.v1 import pz
> from penzai.deprecated.v1.example_models import simple_mlp
> ```
[v2_differences]: https://penzai.readthedocs.io/en/stable/guides/v2_differences.html
## Getting Started
If you haven't already installed JAX, you should do that first, since the
installation process depends on your platform. You can find instructions in the
[JAX documentation](https://jax.readthedocs.io/en/latest/installation.html).
Afterward, you can install Penzai using
```python
pip install penzai
```
and import it using
```python
import penzai
from penzai import pz
```
(`penzai.pz` is an *alias namespace*, which makes it easier to reference
common Penzai objects.)
When working in an Colab or IPython notebook, we recommend also configuring
Treescope (Penzai's companion pretty-printer) as the default pretty printer, and
enabling some utilities for interactive use:
```python
import treescope
treescope.basic_interactive_setup(autovisualize_arrays=True)
```
Here's how you could initialize and visualize a simple neural network:
```python
from penzai.models import simple_mlp
mlp = simple_mlp.MLP.from_config(
name="mlp",
init_base_rng=jax.random.key(0),
feature_sizes=[8, 32, 32, 8]
)
# Models and arrays are visualized automatically when you output them from a
# Colab/IPython notebook cell:
mlp
```
Here's how you could capture and extract the activations after the elementwise
nonlinearities:
```python
@pz.pytree_dataclass
class AppendIntermediate(pz.nn.Layer):
saved: pz.StateVariable[list[Any]]
def __call__(self, x: Any, **unused_side_inputs) -> Any:
self.saved.value = self.saved.value + [x]
return x
var = pz.StateVariable(value=[], label="my_intermediates")
# Make a copy of the model that saves its activations:
saving_model = (
pz.select(mlp)
.at_instances_of(pz.nn.Elementwise)
.insert_after(AppendIntermediate(var))
)
output = saving_model(pz.nx.ones({"features": 8}))
intermediates = var.value
```
To learn more about how to build and manipulate neural networks with Penzai,
we recommend starting with the ["How to Think in Penzai" tutorial][how_to_think]
or one of the other tutorials in the [Penzai documentation][].
[how_to_think]: https://penzai.readthedocs.io/en/stable/notebooks/how_to_think_in_penzai.html
[Penzai documentation]: https://penzai.readthedocs.io
## Citation
If you have found Penzai to be useful for your research, please consider
citing the following writeup (also avaliable on [arXiv](https://arxiv.org/abs/2408.00211)):
```
@article{johnson2024penzai,
author={Daniel D. Johnson},
title={{Penzai} + {Treescope}: A Toolkit for Interpreting, Visualizing, and Editing Models As Data},
year={2024},
journal={ICML 2024 Workshop on Mechanistic Interpretability}
}
```
---
*This is not an officially supported Google product.*
", Assign "at most 3 tags" to the expected json: {"id":"9524","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"