Docs

What does each network component do?

Each component is one file that you can use independently. This page gives, for each: the lines to add, why it exists, what was measured and on which link, and the output of its example. The examples were run on a Mac with Python 3.12, so the components that read the Linux kernel return None or False there.

What a component saves depends on your link, and every number below is given with the link it was measured on. The inference components (sequence_ai.engine) are described in the README.

Client side

window: the congestion window, kept through the pauses

python
from sequence_ai.network import window

transport_user = httpx.HTTPTransport(socket_options=window.socket_options())    # BBR and TCP keepalive, where it can
window.keep(sock_user)                                                          # or BBR on your own socket

warm_user = window.Warm(ping=lambda: client.get("/ping"))    # a ping while the connection is idle
warm_user.used()                                              # each time you send on it

window.check()                                           # warns once: what this machine does after a pause

Why. On Linux a connection that sends nothing for longer than its retransmission timeout has its congestion window restarted, so the next upload takes one extra round trip. A robot that sends a request every half second incurs it on every request.

Measured.

  • BBR. A robot sending a request every 0.53 s with 24 KB of frames, on a round-trip time (RTT) of about 21 ms: 40 of 40 requests took one more round trip without it (21.5 ms), and none with it (0.5 to 3.0 ms).
  • Where the pause cost nothing. On RTTs of about 101 and 241 ms the same robot took no extra round trip with the default settings (0 or 1 of 40 requests), until the link lost packets (38 to 40 of 40 at 1% loss) or the size of the request varied.
  • A ping while idle. On an RTT of 226 ms, 162 KB uploads 3 s apart took 303.6 ms to arrive without the pings, and under 1 ms with them.

A ping cannot keep a window that the pings do not fill; BBR, or tcp_slow_start_after_idle=0, keeps that one.

window.check() issues a warning once per process, with both ways to keep the window, and changes nothing on the machine. Why does my robot’s upload get slow after a pause? has the full table.

python examples/components/window.py printed:

output
socket options this machine accepts: [(65535, 8, 1), (6, 16, 20), (6, 257, 5), (6, 258, 3)]
restarts a window after a pause: False
BBR on your own socket: False; its congestion control: None
pings in 0.3 s of idle time: 5

frames: a frame resized before upload, and decoded on the server

python
from sequence_ai.network import frames

frame_user = frames.resize_image(camera_user.read(), 224, 224, mode="pad", filter="bilinear")
data_user = frames.encode(frame_user)            # a base64 JPEG
image_user = frames.decode(data_user)            # on the server: the HxWx3 uint8 array its policy reads

"pad" is openpi’s resize_with_pad, byte for byte. The mode and the filter have no default and must be passed: a policy sees what it was trained on. frames.encode(frame) gives a base64 JPEG for an input that carries its frames as text, and frames.image_size(data) reads a frame’s width and height from its header without Pillow. On the server, frames.decode(data) turns that JPEG, or a PNG, back into the array, and returns an array it is given unchanged. It opens no other format, since a picture comes over the network.

python examples/components/frames.py printed:

output
pad: (224, 224, 3) with black rows at the top: True
stretch: (224, 224, 3)
encoded: 17,056 characters for 921,600 bytes; size from its header: (224, 224)
decoded: (224, 224, 3) uint8, 2.0 levels from the frame on average

wifi_power: Wi-Fi power saving, detected

python
from sequence_ai.network import wifi_power

wifi_power.check()    # the interface whose power saving is on, after warning how to disable it; else None

With power saving enabled, a Wi-Fi adapter sleeps between the access point’s beacons, and a policy’s response is delayed until the adapter wakes. check() reads the machine’s settings and changes nothing. Linux only.

python examples/components/wifi_power.py printed, on a Mac:

output
Wi-Fi power saving is on: no (or this is not Linux, or not Wi-Fi)

connect: the robot’s own connection

python
import sequence_ai

with sequence_ai.connect("http://your-gpu:8000") as policy_user:       # seq serve at that address
    policy_user.wait_until_ready()
    policy_user.observe({"image": sequence_ai.Video(frame_user), "state": state_user})
    actions_user = policy_user.act()["actions"]
    print(policy_user.stats()["latency_us"]["act_us"])                 # the acts' p50, p95 and p99

The same policy a deployed one’s name gives, on the robot’s stream: one WebSocket to the server, each observation sent as it is made, and each act after them naming how many observations came before it. The server answers once those are in, from the newest. What it does by default:

What it doesTo change it
the streamone WebSocket, kept open, with window’s socket options; a ping after each 100 ms of idle time, for 60 s after the last request-
videoa sequence_ai.Video item goes as low-latency video (H.264), a keyframe every second and after anything lost-
picturesa picture you did not mark (H×W×3 uint8, each side 32 pixels or more) goes as a video keyframe of its ownpictures="exact"
large arrayscompressed where that saves upload time-
lost and refusedan observation the server lost is sent again; one it refused is left out, and the video’s next frame is a keyframe-
linesone line, by the default routelines=["eth0", "wwan0"]
tokennonetoken="..."

Your server is seq serve: it takes the robot’s stream, and says so on every answer (X-Seq-Act-Formats). python -m sequence_ai.check http://your-gpu:8000 reaches it as the robot does and says what is on.

One connection, kept open. This is one line in any client: create one httpx.Client and reuse it. Measured: a new connection for each call took a median 305.9 ms; one reused connection, 121.0 ms. The handshake was 184.9 ms per call. connect() keeps one.

Server side

ping: a server-side ping

python
import websockets
from sequence_ai.network import ping

# websockets' own server, with a ping every 100 ms
async with ping.serve(handler_user, host_user, port_user) as server_user:
    await server_user.serve_forever()

# the same, as its options
async with websockets.serve(handler_user, host_user, port_user, **ping.websockets_options()):
    ...

uvicorn.run(app_user, **ping.uvicorn_options())                                         # the same, under uvicorn

A WebSocket library answers a ping with a pong by itself. A server that pings therefore makes the robot send a few bytes each interval, and the robot’s kernel never counts the connection idle. Nothing changes on the robot.

Measured. Between machines of one cloud provider, on RTTs of 11.7 and 69.2 ms, an upload after 500 ms of silence took 3.0 round trips; with the server pinging every 100 ms it took 1.1 (100 KB and 500 KB) and 1.25 (1 MB).

A server that answers one HTTP request with one response cannot send anything unsolicited. There the ping comes from the robot: window.Warm, or the same pings connect() sends on its own stream. How do I keep a WebSocket policy server’s connection warm? has the rest.

python examples/components/ping.py printed:

output
response: observation
the server measured the connection's RTT from its pings: 0.17 ms

one_write: a response in one write

python
import uvicorn
from sequence_ai.network.one_write import WholeResponse, one_write_http

uvicorn.run(WholeResponse(app_user), http=one_write_http())

app_user is any ASGI app: FastAPI’s and Starlette’s too.

uvicorn writes a response in pieces. A tunnel or proxy in front of the server can delay every piece after the first until the client has acknowledged the first, so each request takes one more round trip than necessary.

Measured. Behind such a tunnel, with a TCP connect of p50 89.5 ms: headers then body, GET p50 189.7 ms; the same response in one write, 106.7 ms.

python examples/components/one_write.py printed response: b'ok'.

python
from sequence_ai.network import tcp_link

tcp_link.read(sock_user)             # any connected socket
tcp_link.of(scope_user["client"])    # in an ASGI app served with one_write_http(): this request's own connection

Each returns rtt_ms, rttvar_ms, retrans, total_retrans, cwnd and, where the kernel provides it, delivery_kbps; or None where the kernel provides none, as on macOS. A robot’s own OS may not expose these to the program that runs on it, and the server reads every robot the same way.

python examples/components/tcp_link.py printed, on a Mac:

output
this connection, as the server's kernel reports it: None

Both sides

msgpack_io

python
from sequence_ai.network import msgpack_io

body_user = msgpack_io.pack({"image": frame_user, "state": joints_user})
observation_user = msgpack_io.unpack(body_user)

Bytes are sent as bytes and numpy arrays as their raw data, whereas JSON carries bytes as base64, a third larger on the wire. It is plain Python, so neither side installs a msgpack package. It writes arrays as msgpack-numpy does and reads openpi’s layout as well. An object array is rejected: decoding one would run code chosen by the sender.

python examples/components/msgpack_io.py printed:

output
150,670 bytes for an image of 150,528; content type application/x-seq-msgpack
arrays returned as arrays: (224, 224, 3) float32

latency_stats

python
from sequence_ai.network.latency_stats import LatencyStats, now_us

stats_user = LatencyStats(latencies=["request_us"])
sent_user = now_us(); ...; stats_user.since("request_us", sent_user)
stats_user.snapshot()["latency_us"]["request_us"]    # n, mean, max, p50, p95, p99

A percentile is None until its window contains enough samples for it: the p99 of 20 samples is only the largest of the

  1. So p50 needs 2 samples, p95 needs 20 and p99 needs 100.

python examples/components/latency_stats.py printed:

output
200 requests: p50 248 ms, p95 281 ms, p99 290 ms

openpi and LeRobot

openpi. Keep openpi’s client and server. On the GPU, openpi.patch() in openpi’s server script, before it makes the server: it then pings each robot every 100 ms while no inference runs, and hands its policy each picture sent as JPEG as the array it reads. openpi’s server runs its policy on its event loop, which sends and answers no ping while an inference runs, so it waits for each pong as long as that takes. On the robot, import image_tools from here, so its pictures go as JPEG, and add the client-side components to the connection the client holds.

python
from sequence_ai.network import openpi

openpi.patch()                                        # the GPU: in openpi's scripts/serve_policy.py, before the server
python
from sequence_ai.network import openpi
from sequence_ai.network.openpi import image_tools     # in place of openpi_client's: pictures go as JPEG

policy_user = openpi.connect(host_user, port_user)    # openpi_client's own client, once its server answers
kept_user = openpi.keep(policy_user)  # BBR and TCP keepalive on its socket; a WebSocket ping while it is idle
observation_user["observation/image"] = image_tools.resize_with_pad(frame_user, 224, 224)
action_user = policy_user.infer(observation_user)     # as before

keep() is verified against openpi-client 0.1.2 on a local connection, and the server side and image_tools against a server module built as openpi’s websocket_policy_server.py is. Measured on a WebSocket connection of the kind openpi’s client holds, between two cloud machines: a 100 KB upload 0.53 s after each response took two to three round trips without keep() and one with it (Measurements). With openpi’s own server and client, keep() was measured in one closed-loop run (the table), and the pictures as JPEG with the server’s pings in another (the table). How do I run π0 (openpi) on a remote GPU?

LeRobot. LeRobot 0.7.0’s remote inference talks over Zenoh, which sends a frame on an idle link only every few seconds. One setting makes it every 100 ms:

python
from sequence_ai.network import lerobot

path_user = lerobot.keep_alive_file("zenoh-keep-alive.json5")

Give the file to both sides: the policy server reads it from zenoh.config_file, and the robot’s remote inference from zenoh_config_path.

Checked: Zenoh 1.10.2 takes the setting, and two idle peers on one machine then sent a frame every 102 ms each way. Measured between two Zenoh peers on two cloud machines: a 100 KB request 0.53 s after each response took two to three round trips without the file and one with it (Measurements). How do I reduce the latency of LeRobot remote inference?