Skip to content

native cpp extension with cmake #1006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 110 commits into
base: main
Choose a base branch
from
Open

Conversation

philei-tt
Copy link
Contributor

@philei-tt philei-tt commented May 7, 2025

Ticket

Original PR: #806

Problem description

This branch is based on https://github.com/tenstorrent/pytorch2.0_ttnn/tree/kw/cpp_extension

What's changed

On top of points mentioned in #806

  • Add instructions to compile torch to make it compatible with ttnn
  • Using cmake to link torch and ttnn
  • Optional git submodule to compile ttnn inside this repo
  • Restructure C++ source files
  • Fix a few minor bugs (lack of stides in TtnnTensorImpl, lack of ttnn->cpu copy)
  • Add .clang-format
  • Update docs with instructions on how to make native extension

Ideally we won't be recompiling torch, but compile ttnn in a compatible with torch variant (gcc + c++17 + some other flags used in torch), but that will be the next PR

@philei-tt philei-tt self-assigned this May 7, 2025
@philei-tt philei-tt requested review from ayerofieiev-tt, jmalone-tt, kevinwuTT and dgomezTT and removed request for ayerofieiev-tt May 7, 2025 11:46
Copy link
Contributor

@kevinwuTT kevinwuTT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done a thorough review yet, but this first set of comments include some of the changes I had to make to get the cpp extension compiled.

tt-smi 3.0.12 requires pre-commit==3.5.0, but you have pre-commit 3.0.4 which is incompatible.
```

## Building cpp extension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these mutually exclusive options? If so I think we mention that in writing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the instructions to separate the two paths here: 21fdf9c

```bash
git submodule sync
git submodule update --init --recursive
CMAKE_FLAGS="-DENABLE_SUBMODULE_TT_METAL_BUILD=ON" python3 setup.py develop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rewrite the steps when following the submodule path since we don't need to build metal separately, but we still need to create a new environment and build pytorch first. Or can we actually build pytorch as a third-party and metal at the same time with this path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can build pytorch as third-party, but then we will have to try to figure out how to install python part of it from cmake. I think it's kind of logical that you don't need to build tt-metal, but I will add a comment. But if you won't build tt-metal, you still need to use ttnn from wheel, or we should figure out how to install python part of ttnn from built submodule 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote the instructions here: https://github.com/tenstorrent/pytorch2.0_ttnn/blob/21fdf9c5fd2d3c8bb5417e5296491cdeaf66d148/docs/OpenRegistrationAPI.md. It doesn't seem like pytorch needs to be rebuild for the submodule path? Granted, I've only tested the simple unit test, and I haven't encountered any errors yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, but it's hard to be 100% sure that it won't break in the future. I think it will work as long as we guarantee that submodule version == ttnn wheel version. Ideally we should install ttnn as python package from submodule, but we can do that later in the future

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a change to this PR in workflows/update-ttnn-wheel.yaml to update the metal submodule commit to match the updated wheel release? This should automatically keep our wheel and metal submodule in sync.

Copy link
Collaborator

@jmalone-tt jmalone-tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested this change on my machine, but the code looks good overall. I left couple of comments and a couple of questions to make sure I'm understanding things

@philei-tt philei-tt marked this pull request as ready for review May 9, 2025 11:36
@philei-tt philei-tt requested review from kevinwuTT and jmalone-tt May 9, 2025 11:37

### Build torchvision (Optional)
Optionally, you can build torchvision, which might be required for some computer-vision models:
```bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to remove the indents, because it looks like this in the preview:
Screenshot 2025-05-20 at 11 51 52 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants