Mostafa86

Mostafa86

Does Nx has an equivalent for JAX tree_map jax.tree_util.tree_map — JAX documentation?

Thank you!

/nx

Showing Posts 1 to 9

Mostafa86

Mostafa86 OP

Couldn’t find any so I hacked a quick one that fits my use case. Any feedback or alternatives is more than welcome as I am pretty new to Elixir.

defmodule TreeMap do
  import Nx.Defn
  import Nx, only: [is_tensor: 1]

  defn fun({a, b, c}) do
    Nx.multiply(c, Nx.add(a, b))
  end

  def process_list_of_arbitrary_nested_maps(list_of_arbitrary_nested_maps) do
    list_of_arbitrary_nested_maps
    |> Enum.map(&Map.to_list/1)
    |> Enum.map(&Enum.sort/1)
    |> Enum.zip()
    |> Enum.map(&Tuple.to_list/1)
    |> Enum.map(fn elem ->
      elem |> Enum.map_reduce("", fn x, _ -> {x |> elem(1), x |> elem(0)} end)
    end)
  end

  def traverse(list_of_arbitrary_nested_maps, fun) do
    condition =
      list_of_arbitrary_nested_maps
      |> Enum.map(&Map.values/1)
      |> Enum.map(fn x -> x |> Enum.all?(&is_tensor/1) end)
      |> Enum.all?()

    cond do
      condition ->
        list_of_arbitrary_nested_maps
        |> process_list_of_arbitrary_nested_maps()
        |> Enum.map(fn {l, k} -> %{"#{k}" => fun.(l |> List.to_tuple())} end)
        |> Enum.reduce(&Map.merge/2)

      true ->
        list_of_arbitrary_nested_maps
        |> process_list_of_arbitrary_nested_maps()
        |> Enum.map(fn {l, k} ->
          %{
            "#{k}" => traverse(l, &fun/1)
          }
        end)
        |> Enum.reduce(&Map.merge/2)
    end
  end
end
polvalente

polvalente

Nx Core Team

Maybe Nx.Defn.Composite.traverse?

Mostafa86

Mostafa86 OP

Thank you. I checked polaris/lib/polaris/shared.ex at ad85df596966548b7c38a89e2032263d0a0b4527 · elixir-nx/polaris · GitHub and I can see that it can be used for 2 arguments but not sure how to generalize to more than 2 arguments :thinking:

polvalente

polvalente

Nx Core Team

What do you mean more than 2 args?

polvalente

polvalente

Nx Core Team

Maybe you want to use Nx.Container.traverse together with Nx.Defn.Composite.flatten_list in a way that your accumulator yields the corresponding “zipped” values for each container.

polvalente

polvalente

Nx Core Team

Yet another possibility is to flatten each container and zip them together

Mostafa86

Mostafa86 OP

I mean more than 2 Nx.Containers

Mostafa86

Mostafa86 OP

Thanks !

This immensely simplified my implementation :smiley:

defmodule TreeMap do
  import Nx.Defn

  defn fun({a, b, c}) do
    Nx.multiply(c, Nx.add(a, b))
  end

  def traverse(list_of_arbitrary_nested_maps, fun) when is_list(list_of_arbitrary_nested_maps) do
    list_of_arbitrary_nested_maps
    |> Enum.map(&List.wrap/1)
    |> Enum.map(&Nx.Defn.Composite.flatten_list/1)
    |> Enum.zip()
    |> Enum.map(fn t -> fun.(t) end)
    |> Kernel.then(fn v ->
      {v, _} =
        Nx.Defn.Composite.traverse(list_of_arbitrary_nested_maps |> Enum.at(0), 0, fn _, acc ->
          {v |> Enum.at(acc), acc + 1}
        end)

      v
    end)
  end
end
polvalente

polvalente

Nx Core Team

Here’s my suggestion:

  def traverse([template_container | _] = list_of_arbitrary_nested_maps, fun) when is_list(list_of_arbitrary_nested_maps) do
    zipped_containers = 
      list_of_arbitrary_nested_maps
      |> Enum.map(&Nx.Defn.Composite.flatten_list[&1])
      |> Enum.zip_with(fun)
    
    {v, []} =
        Nx.Defn.Composite.traverse(template_container, zipped_containers, fn _, [h | t] ->
          {h, t}
        end)

   v
  end
— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
AstonJ
This might be a bit disturbing for some but it’s happening - computers running on living human neurons. They’ve made them smart enough t...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews