Mojo (programming language) explained

Mojo
Family:Python
Designer:Chris Lattner[1]
Developer:Modular Inc.
Latest Preview Version:24.4 [2]
Operating System:Cross-platform
License:Proprietary
Influenced By:Python, Cython, C, C++, Rust, Swift, Zig
File Ext: (the fire emoji/U+1F525 Unicode character), alternatively

Mojo is a programming language in the Python family that is currently under development.[3] [4] [5] It is available both in browsers via Jupyter notebooks,[6] and locally on Linux and macOS.[7] [8] Mojo aims to combine the usability of higher level programming languages, specifically Python, with the performance of lower level programming languages like C++, Rust, and Zig.[9] The Mojo compiler is currently closed source with an open source standard library, although Modular, the company behind Mojo, has stated their intent to eventually open source the Mojo programming language itself as it matures.[10]

Mojo builds upon the MLIR compiler framework instead of directly on the lower level LLVM compiler framework that many languages like Julia, Swift, clang and Rust do.[11] [12] MLIR is a newer compiler framework that allows Mojo to take advantage of higher level compiler passes not available in LLVM alone and allows Mojo to compile down and target more than just CPUs, including producing code that can run on GPUs, TPUs, ASICs and other accelerators. It can also often more effectively use certain types of CPU optimizations directly, like SIMD without direct intervention by the developer like in many other languages.[13] [14] According to Jeremy Howard of fast.ai, Mojo can be seen as "syntax sugar for MLIR" and for that reason Mojo is well optimized for applications like AI.[15]

Origin and Development History

The Mojo programming language was created by Modular Inc, which was founded by Chris Lattner, the original architect of the Swift programming language and LLVM, and Tim Davis, a former Google employee.[16] Intention behind Mojo is to bridge the gap between Python’s ease of use and the fast performance required for cutting-edge AI applications.[17]

According to public change logs, Mojo development goes back to 2022.[18] In May of 2023, the first publicly testable version was made available online via a hosted playground.[19] By September 2023 Mojo was available for local download for Linux[20] and by October 2023 it was also made available for download on Apple's macOS. [21]

In March of 2024, Modular open sourced the Mojo standard library and started accepting community contributions under the Apache 2.0 license.[22] [23]

Features

Mojo was created for an easy transition from Python. The language has syntax similar to Python's, with inferred static typing,[24] and allows users to import Python modules.[25] It uses LLVM and MLIR as its compilation backend.[26] [27] The language also intends to add a foreign function interface to call C/C++ and Python code. The language is not source-compatible with Python 3, only providing a subset of its syntax, e.g. missing the keyword, list and dictionary comprehensions, and support for classes. Further, Mojo also adds features that enable performant low-level programming: for creating typed, compiled functions and "struct" for memory-optimized alternatives to classes. Mojo structs support methods, fields, operator overloading, and decorators.

The language also provides a borrow checker, an influence from Rust.[28] Mojo functions use value semantics by default (functions receive a copy of all arguments and any modifications are not visible outside the function), while Python functions use reference semantics (functions receive a reference on their arguments and any modification of a mutable argument inside the function is visible outside).[29]

The language is not open source, but it is planned to be made open source in the future.[30]

Programming examples

In Mojo, functions can be declared using both (for performant functions) or (for Python compatibility).

Basic arithmetic operations in Mojo with a function:

def sub(x, y): """A pythonic subtraction.""" res = x - y return res

and with an function:

fn add(x: Int, y: Int) -> Int: """A rustacean addition.""" let res: Int = x + y return res

The manner in which Mojo employs and for mutable and immutable variable declarations respectively mirrors the syntax found in Swift. In Swift, is used for mutable variables, while is designated for constants or immutable variables.

Variable declaration and usage in Mojo:fn main: let x = 1 let y: Int y = 1

var z = 0 z += 1

Usage

The Mojo SDK allows Mojo programmers to compile and execute Mojo source files locally from the command line and currently supports Ubuntu and macOS.[31] Additionally, there is a Mojo extension for Visual Studio Code which provides code completion and tooltips.

In January 2024, an inference model of LLaMA2 written in Mojo was released to the public.[32]

See also

External links

Notes and References

  1. Web site: How Modular simplified AI software infrastructure . Fast Company . 2024-08-19.
  2. Web site: Mojo Changelog. Modular . 2024-04-14.
  3. Web site: Mojo programming manual . 2023 . docs.modular.com . Modular . 2023-09-26 . Mojo is a programming language that is as easy to use as Python but with the performance of C++ and Rust. Furthermore, Mojo provides the ability to leverage the entire Python library ecosystem..
  4. Web site: Why Mojo - A language for next-generation compiler technology . 2023 . docs.modular.com . Modular . 2023-09-26 . While many other projects now use MLIR, Mojo is the first major language designed expressly for MLIR, which makes Mojo uniquely powerful when writing systems-level code for AI workloads..
  5. Web site: Krill . Paul . Mojo language marries Python and MLIR for AI development . InfoWorld . en . 4 May 2023.
  6. News: Yegulalp . Serdar . A first look at the Mojo language . InfoWorld . 7 June 2023 . en.
  7. News: Deutscher . Maria . Modular makes its AI-optimized Mojo programming language generally available . Silicon Angle . 7 September 2023 . 2023-09-11 . en.
  8. Web site: Mojo for Mac OS . Modular . 2023-10-19.
  9. Web site: Mojo : Programming language for all of AI . 2024-02-28 . www.modular.com . en.
  10. Web site: Modular: The Next Big Step in Mojo Open Source . 2024-05-28 . www.modular.com . en.
  11. Web site: Krill . Paul . 2023-05-04 . Mojo language marries Python and MLIR for AI development . 2024-05-28 . InfoWorld . en.
  12. Web site: 2024-02-20 . Should Julia use MLIR in the future? . 2024-05-28 . Julia Programming Language . en.
  13. Web site: Why Mojo Modular Docs . 2024-05-28 . docs.modular.com . en.
  14. https://llvm.org/devmtg/2023-10/slides/keynote/Mojo.pdf
  15. Web site: Howard . Jeremy . 2023-05-04 . fast.ai - Mojo may be the biggest programming language advance in decades . 2024-05-28 . fast.ai . en.
  16. Web site: Modular finds its Mojo, a Python superset with C-level speed . Thomas . Claburn . 2023-05-05 . 2023-08-08 . The Register .
  17. https://venturebeat.com/ai/mojo-rising-the-resurgence-of-ai-first-programming-languages/
  18. Web site: Mojo changelog.
  19. Web site: Modular: A unified, extensible platform to superpower your AI . 2024-04-14 . www.modular.com . en.
  20. Web site: Modular: Mojo - It's finally here! . 2024-04-14 . www.modular.com . en.
  21. Web site: Modular: Mojo is now available on Mac . 2024-04-14 . www.modular.com . en.
  22. Web site: 2024-03-28 . Modular open-sources its Mojo AI programming language’s core components . 2024-05-28 . SiliconANGLE . en-US.
  23. Web site: mojo/stdlib/README.md at nightly · modularml/mojo . 2024-05-28 . GitHub . en.
  24. Web site: Modular Docs - Mojo programming manual . 2023-10-19 . docs.modular.com . en.
  25. Web site: Modular Docs - Mojo programming manual . 2023-10-31 . docs.modular.com . en.
  26. Lattner . Chris . MLIR Primer: A Compiler Infrastructure for the End of Moore's Law . 2019 . 2022-09-30 . Pienaar . Jacques.
  27. Lattner . Chris . Amini . Mehdi . Bondhugula . Uday . Cohen . Albert . Davis . Andy . Pienaar . Jacques . Riddle . River . Shpeisman . Tatiana . Vasilache . Nicolas . Zinenko . Oleksandr . 2020-02-29 . MLIR: A Compiler Infrastructure for the End of Moore's Law . cs.PL . 2002.11054.
  28. Web site: Ownership and borrowing Modular Docs . 2024-02-29 . Modular.
  29. Web site: Mojo programming manual . 2023-06-11 . Modular . https://web.archive.org/web/20230611225929/https://docs.modular.com/mojo/programming-manual.html . 2023-06-11 . All values passed into a Python def function use reference semantics. This means the function can modify mutable objects passed into it and those changes are visible outside the function. However, the behavior is sometimes surprising for the uninitiated, because you can change the object that an argument points to and that change is not visible outside the function. All values passed into a Mojo function use value semantics by default. Compared to Python, this is an important difference: A Mojo def function receives a copy of all arguments— it can modify arguments inside the function, but the changes are not visible outside the function..
  30. Web site: Welcome to Mojo . 2023-10-31 . 2023-10-31 . Modular . .
  31. Web site: Modular Docs - Mojo roadmap & sharp edges . 2023-10-31 . docs.modular.com . en.
  32. Web site: llama2.mojo changelog. .