Skip to content

To numpy array - #142

Open
benikm91 wants to merge 2 commits into
dimwit-dev:mainfrom
benikm91:to-numpy-array
Open

To numpy array#142
benikm91 wants to merge 2 commits into
dimwit-dev:mainfrom
benikm91:to-numpy-array

Conversation

@benikm91

@benikm91 benikm91 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

toPyTree and fromPyTree are good for casting a TensorTree to a Python tree for execution of operations.

However, when serializing a tensor tree to disc (e.g., TensorTreeFormat.Pickle), the function fails for a TensorTree with a Random.Key. Instead of a custom logic in TensorTreeFormat.Pickle I propose adding a toNumpyTree and fromNumPyTree for serialization.

case class TrainState(params: UNet.Params, lastCost: Tensor0[Float32], key: Random.Key)

Key needs custom logic to be cast from DimWit/JAX to Numpy. We may could put that into TensorTreeFormat.Pickle toHost, however this is a more general solution if we need it elsewhere or if other special cases appear.

given TensorTree[Key] with
     ...
      def toNumpyTree(p: Key): Jax.PyAny =
        // Extract key data for numpy serialization using key_data
        Jax.np.asarray(Jax.jax.device_get(Jax.jax.random.key_data(p.jaxKey)))

      def fromNumpyTree(pyVal: Jax.PyAny): Key =
        // Reconstruct key from numpy array using wrap_key_data
        Key(Jax.jax.random.wrap_key_data(Jax.jnp.asarray(pyVal.as[Jax.PyDynamic])))

FYI: I tried storing a checkpoint and restarting from it. With same data samples the exact same train trajectory was followed, so the key write/read works successfully :)

@marcelluethi marcelluethi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me.

@benikm91

benikm91 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

Should be merged after #141

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

Successfully merging this pull request may close these issues.

2 participants