forked from modular/mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add blog and video content to examples folder (modular#1031)
Add blog and video content to examples folder
- Loading branch information
Showing
6 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Mojo Plotter | ||
|
||
## Installation | ||
### Conda | ||
If you don't have `conda`, install [miniconda here](https://docs.conda.io/projects/miniconda/en/latest/#quick-command-line-install) | ||
|
||
### Create conda environment | ||
Create and acivate conda enironment: | ||
|
||
#### General | ||
```bash | ||
conda env create -f environment.yaml | ||
conda activate mojo-plotter | ||
``` | ||
|
||
### Auto Set Mojo Environment | ||
To automatically set Mojo to use the python environment when you activate it: | ||
|
||
#### Macos/Linux | ||
```bash | ||
mkdir -p $CONDA_PREFIX/etc/conda/activate.d | ||
export MOJO_PYTHON_LIBRARY="$(find $CONDA_PREFIX/lib -iname 'libpython*.[s,d]*' | sort -r | head -n 1)" | ||
echo "export MOJO_PYTHON_LIBRARY=\"$MOJO_PYTHON_LIBRARY\"" > $CONDA_PREFIX/etc/conda/activate.d/export-mojo.sh | ||
|
||
mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d | ||
echo "unset MOJO_PYTHON_LIBRARY" > $CONDA_PREFIX/etc/conda/deactivate.d/unset-mojo.sh | ||
``` | ||
|
||
## Usage | ||
Simply activate the environment and run the program: | ||
|
||
```bash | ||
conda activate mojo-plotter | ||
mojo main.mojo | ||
``` |
69 changes: 69 additions & 0 deletions
69
examples/blogs-videos/mojo-plotter/environment-macos-arm.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: mojo-plotter-mac | ||
channels: | ||
- pytorch | ||
- anaconda | ||
dependencies: | ||
- blas=1.0=openblas | ||
- brotlipy=0.7.0=py311h80987f9_1002 | ||
- bzip2=1.0.8=h620ffc9_4 | ||
- ca-certificates=2023.08.22=hca03da5_0 | ||
- certifi=2023.7.22=py311hca03da5_0 | ||
- cffi=1.15.1=py311h80987f9_3 | ||
- charset-normalizer=2.0.4=pyhd3eb1b0_0 | ||
- cryptography=41.0.3=py311hd4332d6_0 | ||
- filelock=3.9.0=py311hca03da5_0 | ||
- freetype=2.12.1=h1192e45_0 | ||
- giflib=5.2.1=h80987f9_3 | ||
- gmp=6.2.1=hc377ac9_3 | ||
- gmpy2=2.1.2=py311h40f64dc_0 | ||
- idna=3.4=py311hca03da5_0 | ||
- jinja2=3.1.2=py311hca03da5_0 | ||
- jpeg=9e=h80987f9_1 | ||
- lcms2=2.12=hba8e193_0 | ||
- lerc=3.0=hc377ac9_0 | ||
- libcxx=14.0.6=h848a8c0_0 | ||
- libdeflate=1.17=h80987f9_1 | ||
- libffi=3.4.4=hca03da5_0 | ||
- libgfortran=5.0.0=11_3_0_hca03da5_28 | ||
- libgfortran5=11.3.0=h009349e_28 | ||
- libopenblas=0.3.21=h269037a_0 | ||
- libpng=1.6.39=h80987f9_0 | ||
- libtiff=4.5.1=h313beb8_0 | ||
- libwebp=1.3.2=ha3663a8_0 | ||
- libwebp-base=1.3.2=h80987f9_0 | ||
- llvm-openmp=14.0.6=hc6e5704_0 | ||
- lz4-c=1.9.4=h313beb8_0 | ||
- markupsafe=2.1.1=py311h80987f9_0 | ||
- mpc=1.1.0=h8c48613_1 | ||
- mpfr=4.0.2=h695f6f0_1 | ||
- mpmath=1.3.0=py311hca03da5_0 | ||
- ncurses=6.4=h313beb8_0 | ||
- networkx=3.1=py311hca03da5_0 | ||
- numpy=1.26.0=py311he598dae_0 | ||
- numpy-base=1.26.0=py311hfbfe69c_0 | ||
- openjpeg=2.3.0=h7a6adac_2 | ||
- openssl=3.0.11=h1a28f6b_2 | ||
- pillow=10.0.1=py311h3b245a6_0 | ||
- pip=23.2.1=py311hca03da5_0 | ||
- pycparser=2.21=pyhd3eb1b0_0 | ||
- pyopenssl=23.2.0=py311hca03da5_0 | ||
- pysocks=1.7.1=py311hca03da5_0 | ||
- python=3.11.5=hb885b13_0 | ||
- pytorch=2.1.0=py3.11_0 | ||
- pyyaml=6.0=py311h80987f9_1 | ||
- readline=8.2=h1a28f6b_0 | ||
- requests=2.31.0=py311hca03da5_0 | ||
- setuptools=68.0.0=py311hca03da5_0 | ||
- sqlite=3.41.2=h80987f9_0 | ||
- sympy=1.11.1=py311hca03da5_0 | ||
- tk=8.6.12=hb8d0fd4_0 | ||
- torchaudio=2.1.0=py311_cpu | ||
- torchvision=0.15.2=cpu_py311he74fb5d_0 | ||
- typing_extensions=4.7.1=py311hca03da5_0 | ||
- tzdata=2023c=h04d1e81_0 | ||
- urllib3=1.26.16=py311hca03da5_0 | ||
- wheel=0.41.2=py311hca03da5_0 | ||
- xz=5.4.2=h80987f9_0 | ||
- yaml=0.2.5=h1a28f6b_0 | ||
- zlib=1.2.13=h5a0b063_0 | ||
- zstd=1.5.5=hd90d995_0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: mojo-plotter | ||
channels: | ||
- anaconda | ||
dependencies: | ||
- python=3.11 | ||
- matplotlib | ||
- pytorch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2023, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
|
||
from python import Python | ||
|
||
|
||
fn main() raises: | ||
let torch = Python.import_module("torch") | ||
let x = torch.linspace(0, 10, 100) | ||
let y = torch.sin(x) | ||
plot(x, y) | ||
|
||
def plot(x: PythonObject, y: PythonObject) -> None: | ||
let plt = Python.import_module("matplotlib.pyplot") | ||
plt.plot(x.numpy(), y.numpy()) | ||
plt.xlabel('x') | ||
plt.ylabel('y') | ||
plt.title("Plot of y = sin(x)") | ||
plt.grid(True) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2023, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
|
||
from .tensorutils import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2023, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
|
||
from tensor import Tensor | ||
from math import trunc, mod | ||
fn tensorprint[type: DType](t: Tensor[type])->None: | ||
let rank = t.rank() | ||
var dim0:Int=0 | ||
var dim1:Int=0 | ||
var dim2:Int=0 | ||
if rank==0 or rank>3: | ||
print("Error: Tensor rank should be: 1,2, or 3. Tensor rank is ", rank) | ||
return | ||
if rank==1: | ||
dim0 = 1 | ||
dim1 = 1 | ||
dim2 = t.dim(0) | ||
if rank==2: | ||
dim0 = 1 | ||
dim1 = t.dim(0) | ||
dim2 = t.dim(1) | ||
if rank==3: | ||
dim0 = t.dim(0) | ||
dim1 = t.dim(1) | ||
dim2 = t.dim(2) | ||
var val:SIMD[type, 1]=0.0 | ||
for i in range(dim0): | ||
if i==0 and rank==3: | ||
print("[") | ||
else: | ||
if i>0: | ||
print() | ||
for j in range(dim1): | ||
if rank!=1: | ||
if j==0: | ||
print_no_newline(" [") | ||
else: | ||
print_no_newline("\n ") | ||
print_no_newline("[") | ||
for k in range(dim2): | ||
if rank==1: | ||
val = t[k] | ||
if rank==2: | ||
val = t[j,k] | ||
if rank==3: | ||
val = t[i,j,k] | ||
let int_str = String(trunc(val).cast[DType.int32]()) | ||
let float_str = String(mod(val,1)) | ||
let s = int_str+"."+float_str[2:6] | ||
if k==0: | ||
print_no_newline(s) | ||
else: | ||
print_no_newline(" ",s) | ||
print_no_newline("]") | ||
if rank>1: | ||
print_no_newline("]") | ||
print() | ||
if rank==3: | ||
print("]") | ||
print("Tensor shape:",t.shape().__str__(),", Tensor rank:",rank,",","DType:", type.__str__()) | ||
print() |