Skip to content

Commit

Permalink
Add CMake target for EmitC TTNN dylibs (#1241)
Browse files Browse the repository at this point in the history
* Add cmake target for emitc ttnn dylibs

* tiny adjustments to c++ examples
  • Loading branch information
svuckovicTT authored Nov 14, 2024
1 parent 6dd5eba commit edc964b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 25 deletions.
58 changes: 47 additions & 11 deletions tools/ttnn-standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ endif()

message($ENV{TT_METAL_HOME}/tt_metal/third_party/src/firmware/riscv/$ENV{ARCH_NAME})

add_executable(ttnn-standalone ttnn-standalone.cpp)
set_property(TARGET ttnn-standalone PROPERTY CXX_STANDARD 20)

target_include_directories(ttnn-standalone PRIVATE
# Directories to search for headers
#
set(INCLUDE_DIRS
# TODO: Remove these when ttmetal removes the dependencies from public facing headers
$ENV{TT_METAL_HOME}/.cpmcache/reflect/e75434c4c5f669e4a74e4d84e0a30d7249c1e66f
$ENV{TT_METAL_HOME}/.cpmcache/fmt/73b5ec45edbd92babfd91c3777a9e1ab9cac8238/include
Expand All @@ -78,18 +77,22 @@ target_include_directories(ttnn-standalone PRIVATE
$ENV{TT_METAL_HOME}/ttnn/cpp/ttnn/deprecated
)

target_link_directories(ttnn-standalone PRIVATE
# Link directories
#
set(LINK_DIRS
$ENV{TT_METAL_HOME}-build/lib
)

target_link_libraries(ttnn-standalone PRIVATE
# Metalium
# Libraries to link
#
set(LINK_LIBS
tt_metal
device
yaml-cpp
pthread
# The below libs have been added to tt-metal repo at some point, but are
# not currently needed by the standalone - leaving them commented here for

# The below libs have been added to tt-metal repo at some point, but are not
# currently needed by the targets here - leaving them commented here for
# reference
#
# nng
Expand All @@ -100,8 +103,41 @@ target_link_libraries(ttnn-standalone PRIVATE
$ENV{TT_METAL_HOME}-build/lib/_ttnn.so
)

target_compile_definitions(ttnn-standalone PRIVATE
FMT_HEADER_ONLY
# Compile definitions
#
set(COMPILE_DEFS
FMT_HEADER_ONLY # needed for fmt lib
)

# _ _ _
# ___ | |_ __ _ _ __ __| | __ _ | | ___ _ __ ___
# / __|| __|/ _` || '_ \ / _` | / _` || | / _ \ | '_ \ / _ \
# \__ \| |_| (_| || | | || (_| || (_| || || (_) || | | || __/
# |___/ \__|\__,_||_| |_| \__,_| \__,_||_| \___/ |_| |_| \___|

add_executable(ttnn-standalone ttnn-standalone.cpp)
set_property(TARGET ttnn-standalone PROPERTY CXX_STANDARD 20)

target_include_directories(ttnn-standalone PRIVATE ${INCLUDE_DIRS})
target_link_directories(ttnn-standalone PRIVATE ${LINK_DIRS})
target_link_libraries(ttnn-standalone PRIVATE ${LINK_LIBS})
target_compile_definitions(ttnn-standalone PRIVATE ${COMPILE_DEFS})

target_precompile_headers(ttnn-standalone PRIVATE ttnn-precompiled.hpp)

# _ _ _ _
# __| | _ _ | |(_)| |__
# / _` || | | || || || '_ \
# | (_| || |_| || || || |_) |
# \__,_| \__, ||_||_||_.__/
# |___/

add_library(ttnn-dylib SHARED ttnn-dylib.cpp)
set_property(TARGET ttnn-dylib PROPERTY CXX_STANDARD 20)

target_include_directories(ttnn-dylib PRIVATE ${INCLUDE_DIRS})
target_link_directories(ttnn-dylib PRIVATE ${LINK_DIRS})
target_link_libraries(ttnn-dylib PRIVATE ${LINK_LIBS})
set_target_properties(ttnn-dylib PROPERTIES PUBLIC_HEADER ttnn-dylib.h)

target_precompile_headers(ttnn-dylib PRIVATE ttnn-dylib.hpp)
34 changes: 27 additions & 7 deletions tools/ttnn-standalone/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
# WIP
## Table of contents

- [TTNN Standalone](#ttnn-standalone)
- [Usage](#usage)
- [TTNN Dylib](#ttnn-dylib)

## TTNN Standalone

TTNN Standalone is a post-compile tuning tool.

Third party ML models (PyTorch, Jax, ONNX, ...) can be compiled to a set of TTNN library calls in C++. This generated code can then be manually fine-tuned outside of the compiler environment. TTNN Standalone tool offers all the scaffolding needed to run the C++ code on device (build & run scripts).

### Usage

Following script compiles and runs the ttnn standalone
```bash
cd tools/ttnn-standalone
# Compile a model to C++ code
./build/bin/ttmlir-opt --ttir-load-system-desc --ttir-implicit-device --ttir-layout --convert-ttir-to-ttnn --ttnn-decompose-layouts --ttnn-deallocate --convert-ttnn-to-emitc test/ttmlir/Silicon/TTNN/emitc/simple_add.mlir | ./build/bin/ttmlir-translate --mlir-to-cpp -allow-unregistered-dialect

# Copy paste the generated function into `ttnn-standalone.cpp`.

# Adapt the `main()` function in `ttnn-standalone.cpp` to feed tensors needed for the model

# Run the following script from within this folder (`tools/ttnn-standalone`) to compile and run the ttnn standalone:

./run
```

Note: if you receive this error
```bash
-bash: ./run: Permission denied
```
you may run
```bash
chmod +x run
```
running `chmod +x run` will allow the execution of the script.

## TTNN Dylib

Similarly to the Standalone, this tool offers the ability to compile third party ML models, but to dylibs. Initial intent for compiled dylibs is to be used in testing infrastructure, but sky's the limit :)
2 changes: 1 addition & 1 deletion tools/ttnn-standalone/run
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e # exit on error
echo "==================== BUILDING TTNN STANDALONE ==================="
cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
cmake --build build
cmake --build build -- ttnn-standalone
echo "================ FINISH BUILDING TTNN STANDALONE ================"
set +e # unset exit on error

Expand Down
45 changes: 45 additions & 0 deletions tools/ttnn-standalone/ttnn-dylib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC
//
// SPDX-License-Identifier: Apache-2.0

#include "ttnn-dylib.hpp"

// Forward function example
//
std::vector<ttnn::Tensor> forward(std::vector<ttnn::Tensor> inputs) {
ttnn::Tensor v1 = inputs[0];
ttnn::Tensor v2 = inputs[1];
ttnn::Device *v3 = ttnn::DeviceGetter::getInstance();
ttnn::MemoryConfig v4 = ttnn::MemoryConfig(
ttnn::TensorMemoryLayout::INTERLEAVED, ttnn::BufferType::DRAM);
ttnn::Tensor v5 = ttnn::to_device(v1, v3, v4);
ttnn::Tensor v6 =
ttnn::to_layout(v5, ttnn::Layout::TILE, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::deallocate(v5, false);
ttnn::MemoryConfig v7 = ttnn::MemoryConfig(
ttnn::TensorMemoryLayout::INTERLEAVED, ttnn::BufferType::DRAM);
ttnn::Tensor v8 = ttnn::to_device(v2, v3, v7);
ttnn::Tensor v9 =
ttnn::to_layout(v8, ttnn::Layout::TILE, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::deallocate(v8, false);
ttnn::Shape v10 = ttnn::Shape(tt::tt_metal::LegacyShape({
32,
32,
}));
ttnn::MemoryConfig v11 = ttnn::MemoryConfig(
ttnn::TensorMemoryLayout::INTERLEAVED, ttnn::BufferType::DRAM);
ttnn::Tensor v12 =
ttnn::empty(v10, ttnn::DataType::BFLOAT16, ttnn::Layout::TILE, v3, v11);
ttnn::Tensor v13 = ttnn::add(v6, v9, std::nullopt, std::nullopt, v12);
ttnn::deallocate(v9, false);
ttnn::deallocate(v6, false);
ttnn::Tensor v14 = ttnn::from_device(v13);
ttnn::deallocate(v12, false);
ttnn::Tensor v15 =
ttnn::to_layout(v14, ttnn::Layout::ROW_MAJOR, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::deallocate(v14, false);
return std::vector<ttnn::Tensor>{v15};
}
7 changes: 7 additions & 0 deletions tools/ttnn-standalone/ttnn-dylib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC
//
// SPDX-License-Identifier: Apache-2.0

#include "ttnn-precompiled.hpp"

std::vector<ttnn::Tensor> forward(std::vector<ttnn::Tensor> inputs);
12 changes: 6 additions & 6 deletions tools/ttnn-standalone/ttnn-standalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ ttnn::Tensor forward(ttnn::Tensor v1, ttnn::Tensor v2) {
ttnn::Tensor v6 =
ttnn::to_layout(v5, ttnn::Layout::TILE, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::operations::core::deallocate(v5);
ttnn::deallocate(v5, false);
ttnn::MemoryConfig v7 = ttnn::MemoryConfig(
ttnn::TensorMemoryLayout::INTERLEAVED, ttnn::BufferType::DRAM);
ttnn::Tensor v8 = ttnn::to_device(v2, v3, v7);
ttnn::Tensor v9 =
ttnn::to_layout(v8, ttnn::Layout::TILE, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::operations::core::deallocate(v8);
ttnn::deallocate(v8, false);
ttnn::Shape v10 = ttnn::Shape(tt::tt_metal::LegacyShape({
32,
32,
Expand All @@ -36,14 +36,14 @@ ttnn::Tensor forward(ttnn::Tensor v1, ttnn::Tensor v2) {
ttnn::Tensor v12 =
ttnn::empty(v10, ttnn::DataType::BFLOAT16, ttnn::Layout::TILE, v3, v11);
ttnn::Tensor v13 = ttnn::add(v6, v9, std::nullopt, std::nullopt, v12);
ttnn::operations::core::deallocate(v9);
ttnn::operations::core::deallocate(v6);
ttnn::deallocate(v9, false);
ttnn::deallocate(v6, false);
ttnn::Tensor v14 = ttnn::from_device(v13);
ttnn::operations::core::deallocate(v12);
ttnn::deallocate(v12, false);
ttnn::Tensor v15 =
ttnn::to_layout(v14, ttnn::Layout::ROW_MAJOR, std::nullopt, std::nullopt,
static_cast<::ttnn::Device *>(nullptr));
ttnn::operations::core::deallocate(v14);
ttnn::deallocate(v14, false);
return v15;
}

Expand Down

0 comments on commit edc964b

Please sign in to comment.