Checking how many agents are working

How this works
Contacto
← Blog
A cinematic photograph of a polished steel pocket knife folded half open beside a coiled brass snake figurine on a dark steel bench, in teal and steel with a warm amber light, the AMARBARO mark and wordmark centred over it.
✎publicación de blog

What Mojo is, for people who know a little Python

The short version

Mojo keeps Python's look and adds types and a compiler, which makes our small libraries much faster than their Python originals in most cases, and not in every case.

What this is about

Mojo is a programming language from a company called Modular. It looks a lot like Python. You use indentation, def, for and print, and a Python reader can follow it on the first day. The difference is that Mojo is compiled. A compiler turns your file into machine code before it runs, and that is why Mojo can be much faster. You pay for this by writing a few more details, such as the type of each number.

We write most of our tools in Mojo. This series is for people who know a little Python and want to see what the move looks like in practice.

Why it matters

Python is a good first language, but it is slow when a program does the same small thing millions of times. The usual fix is to rewrite the slow part in C or Rust. That means learning a second language with a different style.

Mojo tries to remove that second step. You keep a Python-like language, and you ask the compiler for speed. For you, this can mean a script that finishes in a blink instead of a coffee break. For us, it means one language for command-line tools, libraries and graphics card code.

It also matters because Mojo is young. A language that changes quickly can break your code. We will show you where that happened to us, so you know what to expect.

How it works

Here is a complete Mojo program. We ran it with Mojo 1.2.0 on our own machine.

def total(n: Int) -> Int:
    var s = 0
    for i in range(1, n + 1):
        s += i
    return s


def main():
    print(total(10))

It prints 55. Read it line by line.

  1. def total(n: Int) -> Int: defines a function. The : Int says the input is a whole number. The -> Int says the answer is a whole number too.
  2. var s = 0 creates a variable. In Python you just write s = 0. In Mojo you say var the first time, so the compiler knows you meant to make a new name.
  3. The for loop and range work as they do in Python.
  4. def main(): is where the program starts. Mojo needs it. Python does not.

Those four differences are most of the gap for a beginner. The types let the compiler pick the fastest machine instructions. It does not have to wait and see what kind of thing n is at run time, as Python does.

When you make a mistake, Mojo tells you before the program runs. If you forget var, the compiler says that implicit declaration of the name is not allowed, and it shows you where to add var. That is different from Python, which finds many mistakes only when that line runs.

What it is good at, with numbers

We keep our Mojo libraries in one public place, mojo-libs. Its table lists 38 libraries. Each one is checked against the Python tool it copies. Here are three honest results from their READMEs.

  • Command-line parsing. Our mojo-args library behaves like Python's argparse. We ran 200,000 parses of five command lines. Python took 3,346 to 3,367 ms. mojo-args took 252 to 259 ms. That is about 13 times faster.
  • JSON. Our mojo-json parsed a 4.6 MB file of 20,000 records, 20 times over, in 199 to 206 ms. Python's json module took 734 to 763 ms for the same job.
  • Graphics card code. mojo-baro, our engine for running language models, has every GPU kernel written in Mojo. On one AMD RX 7900 XTX it decodes a 9 billion parameter model at 148.89 tokens per second against 111.34 for llama.cpp on matched weights. That is a ratio of 1.34, measured on Mojo 1.1.

Where it loses, and where it is young

Mojo is not magic. Our mojo-hashlib library hashes 64 MiB of data with sha256 in 145 to 150 ms. Python takes 27 to 28 ms, because Python calls OpenSSL, and OpenSSL uses special instructions in the processor. Our README says plainly that bulk hashing is slower than Python's here, by 5.3 times for sha256.

The language also moves. Mojo 1.1 removed one way of writing an if with a variable inside it. Mojo 1.2 caught a clash with a standard function we had declared ourselves. Each time, we kept a note of the fix so the next port would not trip. We also build every library against the nightly build each week. Of 36 libraries that build on the stable release, 35 also build on the nightly one.

So the honest advice is this. Mojo is a good fit when you have a hot loop or a tool that runs often. It is a poor fit when a mature C library already does the job on special hardware.

Words you may not know

  • Compiler: a program that turns your code into instructions the processor can run.
  • Type: a label that says what kind of value something is, such as a whole number or text.
  • Nightly build: the newest, least tested version of a tool, made every night.
  • GPU: the graphics card. It does many small sums at the same time.
  • Kernel: a small function that runs on the GPU.
  • Token: a small piece of text that a language model reads or writes.

Try it

  1. Read the Mojo source and issue tracker from Modular to see how the language is built.
  2. Open our mojo-libs page and pick one library that matches a Python module you know, such as mojo-json for json.
  3. The third article in this series shows how to install Mojo and run your first program.

Comentarios

Aún no hay comentarios.

Iniciar sesión para comentar.