Skip to content

⭐️ TTNN Compiler for PyTorch 2.0 ⭐️ This project serves as an example of building a compiler on top of TTNN. It enables running PyTorch2.0 models on Tenstorrent hardware

Notifications You must be signed in to change notification settings

mcw-zwakeelTT/pytorch2.0_ttnn

 
 

Repository files navigation

PyTorch 2.0 TTNN Compiler

This project allows to run PyTorch code on Tenstorrent hardware.

Supported Models

The table below summarizes the results of running various ML models through our TTNN compiler. For each model, we track whether the run was successful, the number of operations before and after conversion, the number of to_device and from_device operations, performance metrics, and accuracy.

Model Run Success Torch Ops Before (Unique Ops) Torch Ops Remain (Unique Ops) To/From Device Ops Original Run Time (ms) Compiled Run Time (ms) Accuracy (%)
Autoencoder (conv) 9 (3) N/A N/A 2820.6 N/A N/A
Autoencoder (linear) 22 (3) N/A N/A 1805.49 N/A N/A
BERT 1393 (21) 100 (3) 610 64018.1 55212.51 98.64
Bloom 1407 (29) 180 (10) 683 28901.2 68217.04 45.77
CLIP 1397 (30) N/A N/A 5898.12 N/A N/A
DETR 1685 (42) N/A N/A 2504.78 N/A N/A
DPR 720 (22) N/A N/A 753.9 N/A N/A
FLAN-T5 19680 (38) N/A N/A 6989.62 N/A N/A
Falcon 2698 (30) N/A N/A 60905 N/A N/A
GLPN-KITTI 3064 (30) N/A N/A 3852.85 N/A N/A
GPT-2 748 (31) N/A N/A 2670.77 N/A N/A
GPTNeo 2652 (36) N/A N/A 11909.7 N/A N/A
HardNet 245 (10) 241 (6) 4 1322.55 19959.26 99.99
Llama 104 (5) 35 (3) 35 172105 167269.96 100.0
MLPMixer 255 (11) N/A N/A 195.61 N/A N/A
Mnist (Eval) 14 (8) 4 (3) 8 37.51 534.9 90.48
Mnist (Train) 46 (15) 22 (9) 14 139.85 8243.7 88.63
MobileNetSSD 575 (34) N/A N/A 635.41 N/A N/A
OPT 4072 (32) N/A N/A 6271.93 N/A N/A
OpenPose 155 (7) N/A N/A 406.9 N/A N/A
Perceiver IO 1532 (21) N/A N/A 1589.95 N/A N/A
ResNet18 70 (9) 42 (4) 20 2102.84 13314.96 99.99
ResNet50 176 (9) 108 (4) 52 4594.02 17987.31 99.98
RoBERTa 719 (21) N/A N/A 5500.24 N/A N/A
SegFormer 760 (27) N/A N/A 907.49 N/A N/A
SqueezeBERT 16 (9) 5 (2) 7 1047.77 4680.07 100.0
Stable Diffusion 1762 (29) N/A N/A 6016.54 N/A N/A
U-Net 86 (7) N/A N/A 841.59 N/A N/A
Unet-brain 86 (7) N/A N/A 691.79 N/A N/A
Unet-carvana 85 (6) N/A N/A 482.3 N/A N/A
ViLT 781 (30) N/A N/A 1576.88 N/A N/A
Whisper 4294 (19) N/A N/A 8577.57 N/A N/A
XGLM 1458 (30) N/A N/A 4050.87 N/A N/A
YOLOS 964 (28) 144 (10) 377 784.94 56322.18 71.71
YOLOv3 264 (10) N/A N/A 1266.72 N/A N/A
YOLOv5 236 (13) N/A N/A 11650.9 N/A N/A
albert/albert-base-v2 779 (21) N/A N/A 346.45 N/A N/A
albert/albert-large-v2 1547 (21) N/A N/A 654.68 N/A N/A
albert/albert-xlarge-v2 1547 (21) N/A N/A 1008.08 N/A N/A
albert/albert-xxlarge-v2 791 (21) N/A N/A 1462.02 N/A N/A
codegen 9114 (37) N/A N/A 3804.21 N/A N/A
densenet121 432 (10) N/A N/A 659.19 N/A N/A
densenet161 572 (10) N/A N/A 5641.99 N/A N/A
densenet169 600 (10) N/A N/A 759.44 N/A N/A
densenet201 712 (10) N/A N/A 1475.14 N/A N/A
distilbert-base-uncased 367 (17) N/A N/A 822.69 N/A N/A
dla34.in1k 135 (9) N/A N/A 630.17 N/A N/A
ese_vovnet19b_dw.ra_in1k 111 (12) N/A N/A 426.94 N/A N/A
facebook/deit-base-patch16-224 685 (17) 100 (5) 330 1173.91 27003.7 96.01
ghostnet_100.in1k 515 (14) N/A N/A 408.09 N/A N/A
ghostnetv2_100.in1k 781 (20) N/A N/A 543.94 N/A N/A
googlenet 214 (15) N/A N/A 716.95 N/A N/A
hrnet_w18.ms_aug_in1k 1426 (14) N/A N/A 1069.34 N/A N/A
inception_v4.tf_in1k 495 (11) N/A N/A 1054.2 N/A N/A
microsoft/beit-base-patch16-224 793 (21) N/A N/A 1079.27 N/A N/A
microsoft/beit-large-patch16-224 1573 (21) N/A N/A 2252.97 N/A N/A
mixer_b16_224.goog_in21k 356 (11) 26 (2) 210 1735.14 25218.07 41.52
mobilenet_v2 154 (9) N/A N/A 517.11 N/A N/A
mobilenet_v3_large 188 (11) N/A N/A 542.86 N/A N/A
mobilenet_v3_small 158 (11) N/A N/A 400.69 N/A N/A
mobilenetv1_100.ra4_e3600_r224_in1k 85 (7) 82 (4) 3 449.36 7487.2 99.14
regnet_x_16gf 235 (8) N/A N/A 2072.13 N/A N/A
regnet_x_1_6gf 195 (8) N/A N/A 536.23 N/A N/A
regnet_x_32gf 245 (8) N/A N/A 3138.26 N/A N/A
regnet_x_3_2gf 265 (8) N/A N/A 759.96 N/A N/A
regnet_x_400mf 235 (8) N/A N/A 483.66 N/A N/A
regnet_x_800mf 175 (8) N/A N/A 467.31 N/A N/A
regnet_x_8gf 245 (8) N/A N/A 1269.97 N/A N/A
regnet_y_128gf 447 (10) N/A N/A 16158.1 N/A N/A
regnet_y_16gf 303 (10) N/A N/A 2436.44 N/A N/A
regnet_y_1_6gf 447 (10) N/A N/A 722.44 N/A N/A
regnet_y_32gf 335 (10) N/A N/A 4557.42 N/A N/A
regnet_y_3_2gf 351 (10) N/A N/A 927.68 N/A N/A
regnet_y_400mf 271 (10) N/A N/A 530.75 N/A N/A
regnet_y_800mf 239 (10) N/A N/A 506.09 N/A N/A
regnet_y_8gf 287 (10) N/A N/A 1330.38 N/A N/A
resnet101 346 (9) N/A N/A 1249.91 N/A N/A
resnet152 516 (9) N/A N/A 1697.12 N/A N/A
resnet18 70 (9) N/A N/A 609.9 N/A N/A
resnet34 126 (9) N/A N/A 739.07 N/A N/A
resnet50 176 (9) N/A N/A 816.02 N/A N/A
resnext101_32x8d 346 (9) N/A N/A 2647.5 N/A N/A
resnext101_64x4d 346 (9) N/A N/A 15655.6 N/A N/A
resnext50_32x4d 176 (9) N/A N/A 1178.88 N/A N/A
retinanet_resnet50_fpn 1107 (32) N/A N/A 2301.66 N/A N/A
retinanet_resnet50_fpn_v2 617 (33) N/A N/A 2239.23 N/A N/A
speecht5-tts 6940 (38) N/A N/A 4303.55 N/A N/A
ssd300_vgg16 387 (32) N/A N/A 3381.07 N/A N/A
ssdlite320_mobilenet_v3_large 575 (34) N/A N/A 564.65 N/A N/A
swin_b 1898 (30) N/A N/A 2776.24 N/A N/A
swin_s 1898 (30) N/A N/A 3168.2 N/A N/A
swin_t 968 (30) N/A N/A 1296.93 N/A N/A
swin_v2_b 2474 (37) N/A N/A 3511.28 N/A N/A
swin_v2_s 2474 (37) N/A N/A 2090.86 N/A N/A
swin_v2_t 1256 (37) N/A N/A 1363.67 N/A N/A
t5-base 13550 (38) N/A N/A 4353.55 N/A N/A
t5-large 20891 (38) N/A N/A 4574.86 N/A N/A
t5-small 5681 (38) N/A N/A 2208.99 N/A N/A
textattack/albert-base-v2-imdb 782 (22) N/A N/A 414.59 N/A N/A
tf_efficientnet_lite0.in1k 149 (9) N/A N/A 734.51 N/A N/A
tf_efficientnet_lite1.in1k 194 (9) N/A N/A 455.98 N/A N/A
tf_efficientnet_lite2.in1k 194 (9) N/A N/A 487.19 N/A N/A
tf_efficientnet_lite3.in1k 221 (9) N/A N/A 523.98 N/A N/A
tf_efficientnet_lite4.in1k 275 (9) N/A N/A 691.26 N/A N/A
twmkn9/albert-base-v2-squad2 783 (23) N/A N/A 544.2 N/A N/A
vgg11 33 (8) N/A N/A 2063.18 N/A N/A
vgg11_bn 41 (9) N/A N/A 1816.49 N/A N/A
vgg13 37 (8) N/A N/A 1964.88 N/A N/A
vgg13_bn 47 (9) N/A N/A 1882.64 N/A N/A
vgg16 43 (8) N/A N/A 1899.6 N/A N/A
vgg16_bn 56 (9) N/A N/A 2081.79 N/A N/A
vgg19 49 (8) N/A N/A 1985.53 N/A N/A
vgg19_bn 65 (9) N/A N/A 2031.69 N/A N/A
vit_b_16 552 (17) N/A N/A 1952.1 N/A N/A
vit_b_32 552 (17) N/A N/A 1725.55 N/A N/A
vit_h_14 1452 (17) N/A N/A 25062.9 N/A N/A
vit_l_16 1092 (17) N/A N/A 5792.03 N/A N/A
vit_l_32 1092 (17) N/A N/A 6193.11 N/A N/A
wide_resnet101_2 346 (9) N/A N/A 2940.82 N/A N/A
wide_resnet50_2 176 (9) N/A N/A 1696.58 N/A N/A
xception71.tf_in1k 393 (9) N/A N/A 1133.26 N/A N/A

Explanation of Metrics

Model: Name of the model.
Run Success: Indicates whether the model runs successfully after conversion.
Torch Ops Before (Unique Ops): The total number of operations used by the model in the original Torch implementation. The number in parenthesis represents the total unique ops.
Torch Ops Remain (Unique Ops): The total number of operations used after conversion to TTNN. The number in parenthesis represents the total unique ops.
To/From Device Ops: The number of to/from_device operations (data transfer to/from the device).
Original Run Time (ms): Execution time (in seconds) of the model before conversion.
Compiled Run Time (ms): Execution time (in seconds) of the model after conversion.
Accuracy (%): Model accuracy on a predefined test dataset after conversion.


Quickstart

The torch_ttnn module has a backend function, which can be used with the torch.compile().

import torch
import torch_ttnn

# A torch Module
class FooModule(torch.nn.Module):
    ...
# Create a module
module = FooModule()

# Compile the module, with ttnn backend
device = ttnn.open_device(device_id=0)
option = torch_ttnn.TorchTtnnOption(device=self.device)
ttnn_module = torch.compile(module, backend=torch_ttnn.backend, options=option)

# Running inference / training
ttnn_module(input_data)

Tracer

The tracer dump the information of fx graph such as node's op_name and shape.

For example, you can run this script to parse the information

PYTHONPATH=$(pwd) python3 tools/stat_models.py --trace_orig --backward --profile
ls stat/raw

By default, the raw result will be stored at stat/raw, and you can run this script to generate the report

python3 tools/generate_report.py
ls stat/

Now the stat/ folder have these report

  • fw_node_count.csv
  • bw_node_count.csv
  • fw_total_input_size_dist/
  • bw_total_input_size_dist/
  • fw_total_output_size_dist/
  • bw_total_output_size_dist/
  • profile/

The node_count.csv show the node with op_type appear in the fx graph. This report can help analyze the frequency of op type appear in the graph.

The *_total_*_size_dist/ statistics the op_type's input/output_size distribution from all fx graph recored in stat/raw. This report can help analyze the memory footprint durning the calculation of op_type.

  • Notice: the default input_shapes in tools/stat_torchvision.py is [1,3,224,224], which has dependency with *_total_*_size_dist/ report.

  • Notice: the aten ir interface is in there

The profile/ is the tools provided by pytorch, you can open it by the url: chrome://tracing

For developers

Install torch-ttnn with editable mode

During development, you may want to use the torch-ttnn package for testing. In order to do that, you can install the torch-ttnn package in "editable" mode with

pip install -e .

Now, you can utilize torch_ttnn in your Python code. Any modifications you make to the torch_ttnn package will take effect immediately, eliminating the need for constant reinstallation via pip.

Build wheel file

For developers want to deploy the wheel, you can build the wheel file with

python -m build

Then you can upload the .whl file to the PyPI (Python Package Index).

Run transformer models

To run transformer model with ttnn backend, run:

PYTHONPATH="$TT_METAL_HOME:$(pwd)" python3 tools/run_transformers.py --model "phiyodr/bert-large-finetuned-squad2" --backend torch_ttnn

You can also substitute the backend with torch_stat to run a reference comparison.

Add a model test

If you want to record run time metrics for a model or test, include a Pytest fixture named record_property as a parameter and set the "model_name" key.
If you also want to compile the model with torch_ttnn backend, set the "torch_ttnn" key to a tuple in this order (model, test_inputs, outputs). "model_name" still needs to be set. See the example code snippet below. torch.nn.Module models with generate method is supported.

def Model(torch.nn.Module):
    def forward(self, x):
        # ...
        return outputs

# Add compilation_xfail marker if torch/CPU runs, but compiled version is xfail
@pytest.mark.compilation_xfail
# Add "record_property" parameter
def test_model_name(record_property):
    # Should be set as early as possible
    record_property("model_name", "Model Name")

    model = Model()
    # ...
    outputs = model(test_input)
    # outputs = model(**test_inputs) # dictionary inputs are also supported
    # ...

    # Can be set once all three objects for the tuple are defined
    record_property("torch_ttnn", (model, test_input(s), outputs))

If model.generate(inputs) is used, pass in model.generate instead of model to record_property.

About

⭐️ TTNN Compiler for PyTorch 2.0 ⭐️ This project serves as an example of building a compiler on top of TTNN. It enables running PyTorch2.0 models on Tenstorrent hardware

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%