https://www.boundaryml.com logo
🟡 Does BAML have an internal HTTP connection pool...
# questions
c
does BAML has an internal http connection pool that limit how many how many calls i make in parallel ?
b
If you’re using the sync client, we can’t parallelize stuff; if you’re using the async client, we can and do.
There is a TCP connection pool a few layers deep, but I haven’t heard of anyone having issues with it.
Can I ask what you’re seeing / which language you’re using (python, TS, something else)?
c
i am using the baml_runtime create in Elixir directly using Rustler
doing some parallel call to LLM, but increasing the parallel call doesn't improve the performance , trying to debug whether this is llm provider rate limiting or what
i am having multiple api key from open router and create multiple clients and use a round robin on all of the clients
the idea is that i can call model in parallel without hitting rate limit
b
Ah- yeah, this is a limitation of the Elixir client implementation, because it calls stuff synchronously
Roughly how it would need to change under the hood is that that
call_function_sync
would need to become
tokio::spawn(call_function.await)
under the hood, and then it would need to pass the result to Elixir
Perhaps @emil has thoughts on how hard this would be? None of us are familiar with Elixir, unfortunately
e
thanks for the report, i'll take a look!
c
interesting 🙂 I would guess tokio is an async runtime that somehow spawn a thread to call the function ? perhaps it is similar to use different process in Elixir?
e
@cat_ethos i wrote this to test if doing calls from multiple elixir processes are indeed serialized in baml, but doesn't seem to be the case? 🤔 . am i doing something wrong here
Copy code
test "concurrent calls" do
    string = """
    Twinkle, twinkle, little star,
    How I wonder what you are!
    Up above the world so high,
    Like a diamond in the sky.

    Twinkle, twinkle, little star,
    How I wonder what you are!

    When the blazing sun is gone,
    When he nothing shines upon,
    Then you show your little light,
    Twinkle, twinkle, all the night.

    Twinkle, twinkle, little star,
    How I wonder what you are!
    """

    # Time single call
    {single_time, {:ok, _}} =
      :timer.tc(fn ->
        BamlElixirTest.Echo.call(%{message: "1 #{DateTime.utc_now()} #{string}"})
      end)

    # Time parallel calls
    {parallel_time, _} =
      :timer.tc(fn ->
        jobs =
          1..5
          |> Enum.map(fn _ ->
            Task.async(fn ->
              BamlElixirTest.Echo.call(%{message: "2 #{DateTime.utc_now()} #{string}"})
            end)
          end)

        Task.await_many(jobs, :infinity)
      end)

    # Time parallel calls 2x
    {parallel_time_2x, _} =
      :timer.tc(fn ->
        jobs =
          1..10
          |> Enum.map(fn _ ->
            Task.async(fn ->
              BamlElixirTest.Echo.call(%{message: "3 #{DateTime.utc_now()} #{string}"})
            end)
          end)

        Task.await_many(jobs, :infinity)
      end)

    IO.inspect(single_time / 1_000_000, label: "single_time")
    # => 2.565963
    IO.inspect(parallel_time / 1_000_000, label: "parallel_time")
    # => 3.510661
    IO.inspect(parallel_time_2x / 1_000_000, label: "parallel_time_2x")
    # => 3.085659
  end
b
> I would guess tokio is an async runtime that somehow spawn a thread to call the function ? perhaps it is similar to use different process in Elixir? tokio is the async scheduler we use in Rust. I'm not sure how exactly Elixir/BEAM does scheduling, but
call_function_sync
will block the caller until it returns, so if the above works then
Task.async
is doing something to force the Elixir caller to yield the current execution context somehow
The
tokio::spawn
strategy I described above is how we do it in each FFI's bridge, to be able to hand off from Rust's async to the host language's async handling
e
Task.async will run the function inside a newly spawned elixir process which is scheduled by the BEAM scheduler. From my understanding, what should happen is: - Running many BAML calls in multiple elixir processes should call the function in the FFI inside separate processes and scheduled by BEAM - since we've marked the FFI function as "DirtyIO", erlang will allow it to be long running and should block like a regular IO blocked function in elixir
b
Is “process” here a process at the kernel level, or some kind of BEAM virtual thread?
c
it is some kind virtual thread to my knowledge
e
elixir/erlang process is not a process at the kernel level. it's the unit of concurrency in BEAM and it schedules the processes using its scheduler