Skip to content
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

Runtime gtest and linker fix #209

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Options
option(TTMLIR_ENABLE_RUNTIME_TESTS "Enable runtime tests" OFF)

add_subdirectory(lib)
add_subdirectory(tools)
if (TTMLIR_ENABLE_RUNTIME_TESTS)
add_subdirectory(test)
endif()
5 changes: 2 additions & 3 deletions runtime/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ if (TTMLIR_ENABLE_RUNTIME)
${PROJECT_BINARY_DIR}/include/ttmlir/Target/Common
)
target_include_directories(TTRuntimeTTNN PUBLIC "$<BUILD_INTERFACE:${TTMETAL_INCLUDE_DIRS}>")
target_link_directories(TTRuntimeTTNN PUBLIC ${TTMETAL_LIBRARY_DIR})
target_link_libraries(TTRuntimeTTNN PUBLIC ${TTNN_LIBRARY})
add_dependencies(TTRuntimeTTNN tt-metal FBS_GENERATION)
target_link_libraries(TTRuntimeTTNN PUBLIC TTNN_LIBRARY)
add_dependencies(TTRuntimeTTNN TTNN_LIBRARY tt-metal FBS_GENERATION)
else()
add_library(TTRuntimeTTNN INTERFACE)
endif()
Expand Down
53 changes: 53 additions & 0 deletions runtime/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
enable_testing()
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
FetchContent_MakeAvailable(googletest)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
target_compile_options(gtest PRIVATE -Wno-covered-switch-default)
endif()

include(GoogleTest)

find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
if (NOT Python3_LIBRARIES)
message(FATAL_ERROR "python libraries not found")
endif()

find_library(FLATBUFFERS_LIB flatbuffers PATHS ${TTMLIR_TOOLCHAIN_DIR}/lib)
if (NOT FLATBUFFERS_LIB)
message(FATAL_ERROR "flatbuffers library not found")
endif()

add_library(TTRuntimeTEST INTERFACE)
add_dependencies(TTRuntimeTEST TTRuntimeTTNN TTRuntimeTTMetal TTRuntime TTEAGER_LIBRARY TTMETAL_LIBRARY)
target_include_directories(TTRuntimeTEST INTERFACE
${PROJECT_SOURCE_DIR}/runtime/include
${PROJECT_BINARY_DIR}/include/ttmlir/Target/Common
${TTMLIR_TOOLCHAIN}/include
)

target_link_libraries(TTRuntimeTEST INTERFACE
TTEAGER_LIBRARY
TTMETAL_LIBRARY
TTRuntime
TTRuntimeTTNN
TTRuntimeTTMetal
${Python3_LIBRARIES}
${FLATBUFFERS_LIB}
GTest::gtest_main
)

function(add_runtime_gtest test_name)
add_executable(${test_name} ${ARGN})
add_dependencies(${test_name} TTRuntimeTEST)
target_link_libraries(${test_name} PRIVATE TTRuntimeTEST)
gtest_discover_tests(${test_name})
endfunction()

add_subdirectory(ttnn)
add_subdirectory(ttmetal)
Empty file.
1 change: 1 addition & 0 deletions runtime/test/ttnn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_runtime_gtest(subtract_test test_subtract.cpp)
51 changes: 51 additions & 0 deletions runtime/test/ttnn/test_subtract.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC
//
// SPDX-License-Identifier: Apache-2.0
#include "tt/runtime/detail/ttnn.h"
#include "tt/runtime/runtime.h"
#include "tt/runtime/utils.h"
#include <cstring>
#include <filesystem>
#include <gtest/gtest.h>
#include <memory>
#include <string>
TEST(TTNNSubtract, Equal) {
const char *fbPath = std::getenv("TTMLIR_SUBTRACT_FB_PATH");
assert(fbPath && "Path to subtract flatbuffer must be provided");
::tt::runtime::Binary fbb = ::tt::runtime::Binary::loadFromPath(fbPath);
EXPECT_EQ(fbb.getFileIdentifier(), "TTNN");
std::vector<::tt::runtime::TensorDesc> inputDescs = fbb.getProgramInputs(0);
std::vector<::tt::runtime::TensorDesc> outputDescs = fbb.getProgramOutputs(0);
std::vector<::tt::runtime::Tensor> inputTensors, outputTensors;

std::uint32_t tensorSize = inputDescs[0].itemsize;
for (const int dim : inputDescs[0].shape) {
tensorSize *= dim;
}

for (const auto &desc : inputDescs) {
std::shared_ptr<void> data =
::tt::runtime::utils::malloc_shared(tensorSize);
std::memset(data.get(), 1, tensorSize);
inputTensors.emplace_back(::tt::runtime::createTensor(data, desc));
}
for (const auto &desc : outputDescs) {
std::shared_ptr<void> data =
::tt::runtime::utils::malloc_shared(tensorSize);
// Set to wrong value on purpose here
std::memset(data.get(), 1, tensorSize);
outputTensors.emplace_back(::tt::runtime::createTensor(data, desc));
}

auto device = ::tt::runtime::openDevice();
auto ev = ::tt::runtime::submit(device, fbb, 0, inputTensors, outputTensors);
::tt::runtime::closeDevice(device);

std::shared_ptr<void> expected =
::tt::runtime::utils::malloc_shared(tensorSize);
std::memset(expected.get(), 0, tensorSize);
for (const auto &outputTensor : outputTensors) {
EXPECT_EQ(std::memcmp(outputTensor.data.get(), expected.get(), tensorSize),
0);
}
}
29 changes: 21 additions & 8 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ set(TTMETAL_INCLUDE_DIRS
${PROJECT_SOURCE_DIR}/third_party/tt-metal/src/tt-metal/tt_eager
PARENT_SCOPE
)
set(TTMETAL_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/third_party/tt-metal/src/tt-metal-build/lib PARENT_SCOPE)
set(TTNN_LIBRARY ${TTMETAL_LIBRARY_DIR}/_ttnn.so PARENT_SCOPE)
set(TTMETAL_LIBRARY -ltt_metal PARENT_SCOPE)

if(EXISTS ${TTNN_LIBRARY})
message(STATUS "Found TTNN library at ${TTNN_LIBRARY}")
add_custom_target(tt-metal)
else()
set(TTMETAL_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/third_party/tt-metal/src/tt-metal-build/lib)
set(TTNN_LIBRARY_PATH ${TTMETAL_LIBRARY_DIR}/_ttnn.so)
set(TTMETAL_LIBRARY_PATH ${TTMETAL_LIBRARY_DIR}/libtt_metal.so)
set(TTEAGER_LIBRARY_PATH ${TTMETAL_LIBRARY_DIR}/libtt_eager.so)

set(TTMETAL_LIBRARY_DIR ${TTMETAL_LIBRARY_DIR} PARENT_SCOPE)
set(TTNN_LIBRARY_PATH ${TTNN_LIBRARY_PATH} PARENT_SCOPE)
set(TTMETAL_LIBRARY_PATH ${TTMETAL_LIBRARY_PATH} PARENT_SCOPE)
set(TTEAGER_LIBRARY_PATH ${TTEAGER_LIBRARY_PATH} PARENT_SCOPE)


ExternalProject_Add(
tt-metal
PREFIX ${TTMLIR_SOURCE_DIR}/third_party/tt-metal
Expand All @@ -46,7 +50,16 @@ ExternalProject_Add(
GIT_REPOSITORY https://github.com/tenstorrent/tt-metal.git
GIT_TAG v0.49.0
GIT_PROGRESS ON
BUILD_BYPRODUCTS ${TTNN_LIBRARY_PATH} ${TTMETAL_LIBRARY_PATH} ${TTEAGER_LIBRARY_PATH}
)
endif()

set_target_properties(tt-metal PROPERTIES EXCLUDE_FROM_ALL TRUE)

list(APPEND library_names TTNN_LIBRARY TTEAGER_LIBRARY TTMETAL_LIBRARY)
list(APPEND library_paths ${TTNN_LIBRARY_PATH} ${TTMETAL_LIBRARY_PATH} ${TTEAGER_LIBRARY_PATH})

foreach(lib_name lib_path IN ZIP_LISTS library_names library_paths)
add_library(${lib_name} SHARED IMPORTED GLOBAL)
set_target_properties(${lib_name} PROPERTIES EXCLUDE_FROM_ALL TRUE IMPORTED_LOCATION ${lib_path})
add_dependencies(${lib_name} tt-metal)
endforeach()
Loading