跳转至

Mojo

Mojo 是一种面向 AI 开发人员的新编程语言,它已经支持与任意 Python 代码无缝集成,目标是拥有python的简洁性、C++的速度、Rust的安全性。Mojo 由LLVM编译器和Swift语言开发大佬Lattner发起开发。

语言特性:

  • 用一种语言编写所有内容:Mojo 可满足 AI 开发人员的需求,将 Python 的易用性与系统编程功能相结合。这使研究和部署团队可以在一个共同的代码库中工作,从而简化工作流程。

  • 释放 Python 性能:Python 无处不在,但对于需要高性能或特殊硬件的任务来说,Python 并非最适合的工具。Mojo 可在 CPU 上实现高性能,并支持 GPU 和 ASIC 等特殊加速器,提供与 C++ 和 CUDA 不相上下的性能。

  • 访问整个 Python 生态系统:Mojo 提供了与 Python 生态系统的完全互操作性,可以无缝使用 Python 库,同时利用 Mojo 的功能和性能优势。

官方给的例子,曼德勃罗分形图计算,经过Mojo的向量化和并行化之后,相比python版本有6.8万倍的加速,具体实现可以参考 How Mojo🔥 gets a 35,000x speedup over Python

目前官方开放了Windows、Mac、Ubuntu的SDK下载。

官方文档 https://docs.modular.com/mojo/manual/get-started/

博客 https://www.modular.com/blog

调用集群mojo SDK

$ module load Singularity/3.7.3

# 第一次使用需要拷贝这个文件
$ cp -r $IMAGE/mojo/.modular ~

# 运行
$ singularity exec  -B $HOME:/root/ $IMAGE/mojo/mojo.sif mojo
Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1> print("Hello, world!")
  2.
Hello, world!
  2>

跑hello world

$ cat hello.mojo
fn main():
   print("Hello, world!")
# 直接运行脚本
$ singularity exec  -B $HOME:/root/ $IMAGE/mojo/mojo.sif mojo hello.mojo
Hello, world!
# 编译成二进制文件运行
$ singularity exec  -B $HOME:/root/ $IMAGE/mojo/mojo.sif mojo build hello.mojo
$ ls hello
hello
$ singularity exec $IMAGE/mojo/mojo.sif hello
Hello, world!

测试llama2.mojo

代码:https://github.com/tairov/llama2.mojo

How I built llama2.🔥 by Aydyn Tairov

$ git clone https://github.com/tairov/llama2.mojo.git
$ cd llama2.mojo/

# 模型下载,如果集群上下载不了可以用自己PC的迅雷下载,然后传到集群上,不到60MB
$ wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin

$ module load Singularity/3.7.3

$ singularity exec -B $HOME:/root $IMAGE/mojo/mojo.sif mojo  llama2.mojo stories15M.bin -s 100 -n 256 -t 0.5 -i "Mojo is a language"
num parallel workers: 31  SIMD width: 32
checkpoint size:  60816028 [ 57 MB ] | n layers: 6 | vocab size: 32000
Mojo is a language that people like to talk. Hephones are very different from other people. He has a big book with many pictures and words. He likes to look at the pictures and learn new things.
One day, Mojo was playing with his friends in the park. They were running and laughing and having fun. Mojo told them about his book and his friends. They listened and looked at the pictures. Then, they saw a picture of a big, scary monster. They were very scared and ran away.
Mojo was sad that his book was gone. He told his friends about the monster and they all felt very sad. Mojo's friends tried to make him feel better, but nothing worked. Mojo never learned his language again.
achieved tok/s:  203.51758793969847

singularity安装 mojo SDK

参考:

How to install Mojo🔥locally using Docker

如果想在自己的服务器上安装,可以参考如下步骤

# singularity pull docker://ubuntu:latest

$ singularity build --sandbox mojo/ docker://ubuntu

$ singularity  shell --fakeroot  --writable mojo/

$ Singularity> apt-get -y update; apt-get -y install curl vim libedit-dev
$ Singularity> apt install python3.10-venv
$ Singularity> curl https://get.modular.com | sh - 

# 这里需要在官网注册获取授权码 mut_2f3675eaf13640a5a3c49aaaaaaaaaaa,每个人不一样
$ Singularity> modular auth mut_2f3675eaf13640a5a3c49aaaaaaaaaaa
$ Singularity> modular install mojo
$ Singularity> echo 'export MODULAR_HOME="/root/.modular"' >> /environment
$ Singularity> echo 'export PATH="/root/.modular/pkg/packages.modular.com_mojo/bin:$PATH"' >> /environment
$ Singularity> exit

$ singularity build mojo.sif mojo/
$ singularity exec -B $HOME:/root mojo.sif mojo
Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

  1> print("hello, world")
  2.
hello, world
  2>


$ singularity exec -B $HOME:/root mojo.sif mojo build benchmark_mojo.mojo
$ singularity exec  mojo.sif ./benchmark_mojo
本文阅读量  次
本站总访问量  次