preciz

preciz

Axon memory usage high

I’m training simple feed forward Neural Networks on CPU and I often see the memory usage go up over 128GB of RAM.

Is this normal? The training data is 70000x4000 tensor, when this gets made I see a bump in memory usage. But then when training loop starts it keeps increasing until out of memory is reached. Should memory keep rapidly increasing during training?

My code seems simple:

      {train_data, test_data} =
        data
        |> Nx.tensor(type: :u8)
        |> Nx.divide(255.0)
        |> Nx.reshape({count, 4000})
        |> Nx.to_batched(batch_size, leftover: :discard)
        |> Enum.split(train_batches_count)

      {train_labels, test_labels} =
        series["label"]
        |> Explorer.Series.to_tensor()
        |> Nx.new_axis(-1)
        |> Nx.equal(Nx.tensor([0, 1]))
        |> Nx.to_batched(batch_size, leftover: :discard)
        |> Enum.split(train_batches_count)

      optimizer = Polaris.Optimizers.adamw(learning_rate: learning_rate)

      model_params =
        model
        |> Axon.Loop.trainer(:categorical_cross_entropy, optimizer)
        |> Axon.Loop.run(Stream.zip(train_data, train_labels), %{}, epochs: 5, compiler: EXLA)

Marked As Solved

preciz

preciz

Thank you @polvalente for mentioning garbage collection.

If I set the :garbage_collect option to true with Axon.Loop.run then the memory usage is low and it doesn’t increase. If I don’t set it, then it grows continuously during training.

This solves the issue.

Also Liked

polvalente

polvalente

Nx Core Team

I believe you might be missing Nx.default_backend(EXLA.Backend)

Your data occupies around 280MB, if I calculated this right. If you didn’t set the default backend, that’ll be allocated in Nx.BinaryBackend. Then, at each iteration of the loop, Nx will copy the data over to EXLA due to your choice of compiler, at least doubling the memory usage.

Then, you might be running into the GC not doing its work fast enough.

polvalente

polvalente

Nx Core Team

Ah right. I thought that was the whole Nx code.

You could add a Stream.map function that calls :erlang.gc before every epoch at least, and that will eliminate the possibility of gc being too slow.

Also, which EXLA version are you using?

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement