1
0
Fork 0
tinygrab/README-upstream.md

160 lines
5.3 KiB
Markdown
Raw Permalink Normal View History

<div align="center">
2020-10-26 11:12:49 -06:00
[![logo](https://raw.githubusercontent.com/tinygrad/tinygrad/master/docs/logo.png)](https://tinygrad.org)
2020-10-18 12:27:37 -06:00
tinygrad: For something between [PyTorch](https://github.com/pytorch/pytorch) and [karpathy/micrograd](https://github.com/karpathy/micrograd). Maintained by [tiny corp](https://tinygrad.org).
2020-10-18 14:41:51 -06:00
<h3>
2023-01-28 12:36:15 -07:00
[Homepage](https://github.com/tinygrad/tinygrad) | [Documentation](/docs) | [Examples](/examples) | [Showcase](/docs/showcase.md) | [Discord](https://discord.gg/ZjZadyC7PK)
2020-10-17 23:57:01 -06:00
</h3>
2022-11-08 20:13:11 -07:00
[![GitHub Repo stars](https://img.shields.io/github/stars/tinygrad/tinygrad)](https://github.com/tinygrad/tinygrad/stargazers)
[![Unit Tests](https://github.com/tinygrad/tinygrad/actions/workflows/test.yml/badge.svg)](https://github.com/tinygrad/tinygrad/actions/workflows/test.yml)
[![Discord](https://img.shields.io/discord/1068976834382925865)](https://discord.gg/ZjZadyC7PK)
2022-11-08 20:13:11 -07:00
</div>
2020-10-18 13:48:17 -06:00
---
2020-10-18 13:48:17 -06:00
This may not be the best deep learning framework, but it is a deep learning framework.
2020-10-18 13:48:17 -06:00
2023-06-05 13:20:14 -06:00
Due to its extreme simplicity, it aims to be the easiest framework to add new accelerators to, with support for both inference and training. If XLA is CISC, tinygrad is RISC.
2020-10-18 13:48:17 -06:00
2023-06-05 13:20:14 -06:00
tinygrad is still alpha software, but we [raised some money](https://geohot.github.io/blog/jekyll/update/2023/05/24/the-tiny-corp-raised-5M.html) to make it good. Someday, we will tape out chips.
2020-10-18 13:48:17 -06:00
## Features
2020-10-18 13:48:17 -06:00
### LLaMA and Stable Diffusion
2020-10-18 13:48:17 -06:00
tinygrad can run [LLaMA](/docs/showcase.md#llama) and [Stable Diffusion](/docs/showcase.md#stable-diffusion)!
2020-10-17 23:57:01 -06:00
### Laziness
2023-03-06 09:25:13 -07:00
Try a matmul. See how, despite the style, it is fused into one kernel with the power of laziness.
2023-03-06 09:25:13 -07:00
```sh
2023-11-23 15:54:52 -07:00
DEBUG=3 python3 -c "from tinygrad import Tensor;
2023-06-04 09:52:13 -06:00
N = 1024; a, b = Tensor.rand(N, N), Tensor.rand(N, N);
2023-03-06 09:25:13 -07:00
c = (a.reshape(N, 1, N) * b.permute(1,0).reshape(1, N, N)).sum(axis=2);
print((c.numpy() - (a.numpy() @ b.numpy())).mean())"
```
And we can change `DEBUG` to `4` to see the generated code.
### Neural networks
2020-10-18 17:40:42 -06:00
As it turns out, 90% of what you need for neural networks are a decent autograd/tensor library.
Throw in an optimizer, a data loader, and some compute, and you have all you need.
2020-10-18 17:40:42 -06:00
```py
2023-11-23 15:54:52 -07:00
from tinygrad import Tensor, nn
2020-10-18 15:32:45 -06:00
2023-11-23 15:54:52 -07:00
class LinearNet:
2020-10-18 15:32:45 -06:00
def __init__(self):
2023-11-23 15:54:52 -07:00
self.l1 = Tensor.kaiming_uniform(784, 128)
self.l2 = Tensor.kaiming_uniform(128, 10)
def __call__(self, x:Tensor) -> Tensor:
return x.flatten(1).dot(self.l1).relu().dot(self.l2)
2020-10-18 15:32:45 -06:00
2023-11-23 15:54:52 -07:00
model = LinearNet()
optim = nn.optim.Adam([model.l1, model.l2], lr=0.001)
2020-10-18 15:32:45 -06:00
2023-11-23 15:54:52 -07:00
x, y = Tensor.rand(4, 1, 28, 28), Tensor([2,4,3,7]) # replace with real mnist dataloader
2020-10-18 15:32:45 -06:00
2023-11-23 15:54:52 -07:00
for i in range(10):
optim.zero_grad()
loss = model(x).sparse_categorical_crossentropy(y).backward()
optim.step()
print(i, loss.item())
2020-10-18 15:32:45 -06:00
```
2020-10-18 14:08:14 -06:00
2023-11-23 15:58:22 -07:00
See [examples/beautiful_mnist.py](examples/beautiful_mnist.py) for the full version that gets 98% in ~5 seconds
## Accelerators
2020-11-02 09:33:48 -07:00
tinygrad already supports numerous accelerators, including:
2020-11-02 09:33:48 -07:00
- [x] [CPU](tinygrad/runtime/ops_cpu.py)
- [x] [GPU (OpenCL)](tinygrad/runtime/ops_gpu.py)
- [x] [C Code (Clang)](tinygrad/runtime/ops_clang.py)
- [x] [LLVM](tinygrad/runtime/ops_llvm.py)
- [x] [METAL](tinygrad/runtime/ops_metal.py)
- [x] [CUDA](tinygrad/runtime/ops_cuda.py)
- [x] [Triton](extra/triton/triton.py)
- [x] [PyTorch](tinygrad/runtime/ops_torch.py)
- [x] [HIP](tinygrad/runtime/ops_hip.py)
- [x] [WebGPU](tinygrad/runtime/ops_webgpu.py)
2020-11-02 09:33:48 -07:00
And it is easy to add more! Your accelerator of choice only needs to support a total of 26 (optionally 27) low level ops.
More information can be found in the [documentation for adding new accelerators](/docs/adding_new_accelerators.md).
2022-06-08 12:46:09 -06:00
## Installation
2022-06-08 12:46:09 -06:00
The current recommended way to install tinygrad is from source.
2020-12-13 22:32:20 -07:00
### From source
2020-12-13 22:32:20 -07:00
```sh
git clone https://github.com/tinygrad/tinygrad.git
cd tinygrad
python3 -m pip install -e .
2020-12-13 22:32:20 -07:00
```
Don't forget the `.` at the end!
2020-12-13 22:32:20 -07:00
## Documentation
2022-06-08 12:41:19 -06:00
Documentation along with a quick start guide can be found in the [docs/](/docs) directory.
2022-06-08 12:41:19 -06:00
### Quick example comparing to PyTorch
2022-06-08 12:41:19 -06:00
```py
2023-11-23 15:54:52 -07:00
from tinygrad import Tensor
2020-11-02 09:30:43 -07:00
x = Tensor.eye(3, requires_grad=True)
y = Tensor([[2.0,0,-2.0]], requires_grad=True)
z = y.matmul(x).sum()
z.backward()
2020-11-07 13:26:57 -07:00
print(x.grad.numpy()) # dz/dx
print(y.grad.numpy()) # dz/dy
2020-11-07 13:26:57 -07:00
```
The same thing but in PyTorch:
```py
import torch
2023-05-26 00:10:41 -06:00
x = torch.eye(3, requires_grad=True)
y = torch.tensor([[2.0,0,-2.0]], requires_grad=True)
z = y.matmul(x).sum()
z.backward()
2023-05-26 00:10:41 -06:00
print(x.grad.numpy()) # dz/dx
print(y.grad.numpy()) # dz/dy
2023-05-26 00:10:41 -06:00
```
## Contributing
There has been a lot of interest in tinygrad lately. Here are some basic guidelines for contributing:
2021-10-30 20:47:34 -06:00
- Bug fixes are the best and always welcome! Like [this one](https://github.com/tinygrad/tinygrad/pull/421/files).
- If you don't understand the code you are changing, don't change it!
- All code golf PRs will be closed, but [conceptual cleanups](https://github.com/tinygrad/tinygrad/pull/372/files) are great.
- Features are welcome. Though if you are adding a feature, you need to include tests.
- Improving test coverage is great, with reliable non-brittle tests.
2021-10-30 20:47:34 -06:00
Additional guidelines can be found in [CONTRIBUTING.md](/CONTRIBUTING.md).
2022-06-05 13:13:05 -06:00
2020-10-27 09:10:51 -06:00
### Running tests
For more examples on how to run the full test suite please refer to the [CI workflow](.github/workflows/test.yml).
Some examples:
```sh
python3 -m pip install -e '.[testing]'
2020-11-28 07:20:02 -07:00
python3 -m pytest
python3 -m pytest -v -k TestTrain
python3 ./test/models/test_train.py TestTrain.test_efficientnet
2020-10-27 09:10:51 -06:00
```