<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="370361" data-post-id="370361">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jonatanklosko" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jonatanklosko/120/37940_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jonatanklosko
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Livebook</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>it uses 20 G of GPU memory</p>
</blockquote>
<p>By default XLA preallocates 80% of the GPU memory upfront, so <code>nvidia-smi</code> will always show such high usage.</p>
<p>You can disable this behaviour by configuring <code>preallocate: false</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">config :exla, :clients,
  host: [platform: :host],
  cuda: [platform: :cuda, preallocate: false],
  rocm: [platform: :rocm],
  tpu: [platform: :tpu]
</code></pre>
<p>Still, once XLA allocates some memory it won’t give it back, so the overall usage may still be inflated.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="370361" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/bumblebee-slow-load-model-in-genserver-slow-nx-serving-run-in-exs-file/71888/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-370361" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="370361"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="370485" data-post-id="370485">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mgwidmann" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mgwidmann/120/7249_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mgwidmann
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I built a GenServer which communicates with a simple python script over stdin/stdout using <code>Exile.Process</code> in order to run the LLM model. It is not the preferable solution but its all I have at the moment. I’m still experiencing the slow model download speed to be abysmal when my application is booted. For document ingest I’m using <code>thenlper/gte-small</code> for embeddings, which is only 66.75 MB but its terribly slow to download from an EC2 machine which makes no sense. When the python LLM model booted up, it downloaded quickly, but the Elixir one is super slow and I don’t understand why. I estimate the speed of download to be in the few KB per second, no more than 10 KB/s at most. Yet, when I booted the <code>openai--whisper-tiny</code> model, it downloaded (from the exact same machine while this is going on) 151.09 MB nearly instantly. It would be nice to be able to understand the format of the cache or to have a mix task which will prime the cache with a certain model (useful for infra setup too!) to avoid having to load the entire application. When I do this with <code>iex -S mix run --no-start</code> then <code>Application.ensure_all_started(:bumblebee)</code> and <code>Bumblebee.load_model({:hf, thenlper/gte-small})</code> it is also a nearly instant downloaded. I’m not sure what is different about my project that is slowing it down like this.</p>
<p>Once I got past that manually, I still see infinite hanging on the <code>batched_run</code> call, even with smaller and simpler models like <code>thenlper/gte-small</code>. For example, within my GenServer when I add a document:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    # Chunk up
    chunks =
      text
      |&gt; String.codepoints()
      |&gt; Enum.chunk_every(@chunk_size)
      |&gt; Enum.map(&amp;Enum.join/1)


    Logger.info("Created #{length(chunks)} chunks")

    # Run tokenizer
    results = Nx.Serving.batched_run(DocsServing, chunks) |&gt; IO.inspect()

    Logger.info("Vectorized #{length(results)} chunks")
</code></pre>
<p>I don’t see the second logger line, only:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(processing@127.0.0.1)1&gt; Docs.add_doc("test/assets/doc.txt")

14:42:16.697 [info] Created 74 chunks
</code></pre>
<p>Is there some kind of deadlock occurring? I am creating the model in a <code>handle_cast</code> as the first message after <code>init</code> like so:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    # Load model
    repo = {:hf, "thenlper/gte-small"}

    {:ok, model_info} = Bumblebee.load_model(repo)
    {:ok, tokenizer} = Bumblebee.load_tokenizer(repo)

    serving =
      Bumblebee.Text.TextEmbedding.text_embedding(model_info, tokenizer,
        compile: [batch_size: 64, sequence_length: 512],
        output_attribute: :hidden_state,
        output_pool: :mean_pooling
      )

    # Start serving
    {:ok, _server} = Nx.Serving.start_link(serving: serving, name: DocsServing, batch_timeout: 100)
</code></pre>
<p>I set <code>preallocate: false</code> like you said, and I only see 362 MB of GPU memory being used while the process is running and I switched from using <code>batched_run</code> to <code>run</code> and I get a response now. It runs out of memory but that makes no sense.</p>
<p>I see that XLA has allocated its 13 G out of 15 G available.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Every 2.0s: nvidia-smi                                                                                                          ip-172-31-42-52: Fri Aug  1 15:07:32 2025

Fri Aug  1 15:07:32 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 570.133.20             Driver Version: 570.133.20     CUDA Version: 12.8     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA T4G                     On  |   00000000:00:1F.0 Off |                    0 |
| N/A   57C    P0             37W /   70W |   13545MiB /  15360MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A          194428      C   ...g/28.0/erts-16.0/bin/beam.smp      13542MiB |
+-----------------------------------------------------------------------------------------+
</code></pre>
<p>But still I get this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">15:07:12.370 [info] Created 74 chunks

15:07:29.172 [warning] Allocator (GPU_0_bfc) ran out of memory trying to allocate 192.00MiB (rounded to 201326592)requested by op
If the cause is memory fragmentation maybe the environment variable 'TF_GPU_ALLOCATOR=cuda_malloc_async' will improve the situation.
Current allocation summary follows.
Current allocation summary follows.

15:07:29.173 [info] BFCAllocator dump for GPU_0_bfc

...


15:07:29.179 [error] GenServer {OfflineRadioLlm.Registry, OfflineRadioLlm.Ingest.Docs} terminating
** (RuntimeError) Out of memory while trying to allocate 201326592 bytes.
    (exla 0.10.0) EXLA.NIF.run_io(#Reference&lt;0.2779148131.354025504.5762&gt;, [[#Reference&lt;0.2779148131.354025510.8544&gt;, #Reference&lt;0.2779148131.354025510.8743&gt;]], 0)
    (exla 0.10.0) lib/exla/executable.ex:31: EXLA.Executable.run/3
    (exla 0.10.0) lib/exla/defn.ex:128: EXLA.Defn.maybe_outfeed/7
    (stdlib 7.0) timer.erl:599: :timer.tc/2
    (exla 0.10.0) lib/exla/defn.ex:60: anonymous fn/7 in EXLA.Defn.__compile__/4
    (nx 0.10.0) lib/nx/defn/compiler.ex:134: Nx.Defn.Compiler.__jit__/4
    (nx 0.10.0) lib/nx/defn.ex:452: Nx.Defn.do_jit_apply/3
    (nx 0.10.0) lib/nx/defn/evaluator.ex:461: Nx.Defn.Evaluator.eval_apply/4
Last message (from #PID&lt;0.255.0&gt;): {:ingest, "test/assets/doc.txt"}
State: {%Nx.Serving{module: Nx.Serving.Default, arg: #Function&lt;1.7074203/2 in Bumblebee.Text.TextEmbedding.text_embedding/3&gt;, client_preprocessing: #Function&lt;2.7074203/1 in Bumblebee.Text.TextEmbedding.text_embedding/3&gt;, client_postprocessing: #Function&lt;3.7074203/2 in Bumblebee.Text.TextEmbedding.text_embedding/3&gt;, streaming: nil, batch_size: 64, distributed_postprocessing: &amp;Function.identity/1, process_options: [batch_keys: [sequence_length: 512]], defn_options: []}, %HNSWLib.Index{space: :cosine, dim: 384, reference: #Reference&lt;0.2779148131.354025473.10498&gt;}, %{}}
Client #PID&lt;0.255.0&gt; is alive

...

15:07:29.211 [info] Sum Total of in-use chunks: 12.94GiB


15:07:29.211 [info] Total bytes in pool: 14074321152 memory_limit_: 14074321305 available bytes: 153 curr_region_allocation_bytes_: 17179869184


15:07:29.211 [info] Stats:
Limit:                     14074321305
InUse:                     13892561408
MaxInUse:                  13892561408
NumAllocs:                        2196
MaxAllocSize:                922746880
Reserved:                            0
PeakReserved:                        0
LargestFreeBlock:                    0
</code></pre>
<p>It looks like it tried to allocate another 2 GB but it was not available. Why does it try to use so much for this small embeddings model? It doesn’t make any sense to me whats happening here. I tried running a single chunk to see if that helps, like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">results = Nx.Serving.run(serving, [Enum.at(chunks, 0)]) |&gt; IO.inspect()
</code></pre>
<p>That worked, it still grabs the full 13 G of GPU memory though. Perhaps like you said just getting 80% but I don’t understand why it would need more for this model with only 74 chunks, its not very much I don’t think. 74 chunks * 384 dimensional vector * 32 bits per vector (<code>f32</code> type) is 909 thousand bytes, barely a megabyte. When I chunk by 10, 2, 1 items at a time, it always succeeds on the first loop then fails on the second loop.</p>
<p>I know thats a lot… Any thoughts on what to try? I am at a loss for how to make a small simple model like this work in Elixir and that doesn’t bode well for my proposal to my team to try it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="370485" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/bumblebee-slow-load-model-in-genserver-slow-nx-serving-run-in-exs-file/71888/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-370485" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="370485"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="371419" data-post-id="371419">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mgwidmann" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mgwidmann/120/7249_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mgwidmann
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t know what happened but I’m able to run models that previously did not work and I didn’t change anything.</p>
<p>I am however still running into out of memory problems. Normally, I would just say, ok it needs more GPU memory to run, duh. But I’m running into issues running much smaller models than I can with python. For example, the <code>meta-llama/Llama-3.2-1B-Instruct</code> runs out of memory in Elixir yet (obviously when shut down) running the <code>meta-llama/Meta-Llama-3-8B-Instruct</code> model in python works perfectly fine on the exact same machine (an amazon Graviton machine, ARM64). Perhaps there are still some optimizations that are not yet implemented in EXLA?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="371419" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/bumblebee-slow-load-model-in-genserver-slow-nx-serving-run-in-exs-file/71888/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-371419" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="371419"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>