From 09d1349a43e44e0612968289999dc2bcca15fb9a Mon Sep 17 00:00:00 2001 From: svuckovic Date: Fri, 27 Dec 2024 15:21:01 +0000 Subject: [PATCH] race condition + clang fixes --- runtime/lib/ttnn/runtime.cpp | 20 +++--- test/lit.cfg.py | 5 +- tools/ttnn-standalone/compile_dylib.py | 96 +++++++++++++++----------- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/runtime/lib/ttnn/runtime.cpp b/runtime/lib/ttnn/runtime.cpp index eeee8270d..98dde70e6 100644 --- a/runtime/lib/ttnn/runtime.cpp +++ b/runtime/lib/ttnn/runtime.cpp @@ -561,7 +561,8 @@ std::vector do_stuff(void *so, std::string func_name, using ForwardFunction = std::vector<::ttnn::Tensor> (*)( std::vector<::ttnn::Tensor>, ::ttnn::Device *); std::cout << "before" << std::endl; - ForwardFunction forwardFunc = (ForwardFunction)dlsym(so, func_name.c_str()); + ForwardFunction forwardFunc = + reinterpret_cast(dlsym(so, func_name.c_str())); std::cout << "after" << std::endl; const char *dlsym_error = dlerror(); @@ -643,16 +644,16 @@ bool compareOuts(std::vector &lhs, std::vector &rhs) { LOG_ASSERT(lhsTensors.size() == rhsTensors.size()); for (size_t i = 0; i < lhsTensors.size(); i++) { - auto lhsTensor = lhsTensors[i]; - auto rhsTensor = rhsTensors[i]; - std::cout << "Dtype: " << (int)lhsTensor->get_dtype() << ", " - << (int)rhsTensor->get_dtype() << std::endl; + auto *lhsTensor = lhsTensors[i]; + auto *rhsTensor = rhsTensors[i]; + std::cout << "Dtype: " << static_cast(lhsTensor->get_dtype()) << ", " + << static_cast(rhsTensor->get_dtype()) << std::endl; LOG_ASSERT(lhsTensor->get_dtype() == rhsTensor->get_dtype()); std::cout << "Shape: " << lhsTensor->get_shape() << ", " << rhsTensor->get_shape() << std::endl; LOG_ASSERT(lhsTensor->get_shape() == rhsTensor->get_shape()); - std::cout << "Layout: " << (int)lhsTensor->get_layout() << ", " - << (int)rhsTensor->get_layout() << std::endl; + std::cout << "Layout: " << static_cast(lhsTensor->get_layout()) << ", " + << static_cast(rhsTensor->get_layout()) << std::endl; LOG_ASSERT(lhsTensor->get_layout() == rhsTensor->get_layout()); std::cout << "Logical shape: " << lhsTensor->get_logical_shape() << ", " << rhsTensor->get_logical_shape() << std::endl; @@ -681,8 +682,9 @@ bool compareOuts(std::vector &lhs, std::vector &rhs) { for (size_t i = 0; i < lhsTensor->volume() * lhsTensor->element_size(); i++) { if (lhsData[i] != rhsData[i]) { - std::cout << "Mismatch at byte number: " << i << ": " << (int)lhsData[i] - << " != " << (int)rhsData[i] << std::endl; + std::cout << "Mismatch at byte number: " << i << ": " + << static_cast(lhsData[i]) + << " != " << static_cast(rhsData[i]) << std::endl; return false; } } diff --git a/test/lit.cfg.py b/test/lit.cfg.py index a039ddb57..926d882b6 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -49,7 +49,10 @@ def set_system_desc_features(system_desc): # system_desc_path: The system desc that is to be used to generate the binary files. config.system_desc_path = os.getenv("SYSTEM_DESC_PATH", "") -# Add `TT_METAL_HOME` to the lit environment. +# In emitc dylib testing path, we want to build dylibs but their CMake expects proper TT_METAL_HOME in order to locate +# sources/libs from TT Metal. However, importing TTRT can change TT_METAL_HOME env to `/opt/ttmlir-toolchain/...`. In +# order to combat this, we save the original TT_METAL_HOME variable TT_METAL_HOME_ORIGINAL and adjust the dylib CMake +# file to read TT_METAL_HOME_ORIGINAL if it exists. llvm_config.with_environment( "TT_METAL_HOME_ORIGINAL", os.getenv("TT_METAL_HOME"), diff --git a/tools/ttnn-standalone/compile_dylib.py b/tools/ttnn-standalone/compile_dylib.py index 1fbf4cbdb..370d3ba8d 100755 --- a/tools/ttnn-standalone/compile_dylib.py +++ b/tools/ttnn-standalone/compile_dylib.py @@ -16,9 +16,6 @@ def main(): print("Usage: script.py ") sys.exit(1) - for name, value in os.environ.items(): - print("{0}: {1}".format(name, value)) - cpp_file_path = sys.argv[1] output_dir = sys.argv[2] @@ -32,50 +29,63 @@ def main(): print(f"Error: Directory '{output_dir}' does not exist.") sys.exit(1) - # Define the path to the target file + # Define the path to the target tt_mlir_home = os.environ.get("TT_MLIR_HOME") if not tt_mlir_home: print("Error: TT_MLIR_HOME environment variable is not set.") sys.exit(1) - target_file_path = os.path.join( - tt_mlir_home, "tools/ttnn-standalone/ttnn-dylib.cpp" + # Create unique source and build directories + cpp_base_name = os.path.basename(cpp_file_path).split(".")[0] + temp_source_dir = os.path.join( + tt_mlir_home, f"tools/ttnn-standalone/temp_source_{cpp_base_name}" + ) + temp_build_dir = os.path.join( + tt_mlir_home, f"tools/ttnn-standalone/temp_build_{cpp_base_name}" ) try: - # Read contents of the input file - with open(cpp_file_path, "r") as source_file: - cpp_content = source_file.read() - - # Overwrite the target file - with open(target_file_path, "w") as target_file: - target_file.write(cpp_content) - - print( - f"Successfully updated {target_file_path} with contents from {cpp_file_path}." + # Create the temporary source directory and copy necessary files + os.makedirs(temp_source_dir, exist_ok=True) + shutil.copy2( + os.path.join(tt_mlir_home, "tools/ttnn-standalone/CMakeLists.txt"), + temp_source_dir, ) - except Exception as e: - print(f"Error while handling files: {e}") - sys.exit(1) - - # Define the commands to be executed - build_dir = os.path.join(tt_mlir_home, "tools/ttnn-standalone/build") - cmake_command = [ - "cmake", - "-G", - "Ninja", - "-B", - build_dir, - "-S", - os.path.join(tt_mlir_home, "tools/ttnn-standalone"), - "-DCMAKE_BUILD_TYPE=Release", - "-DCMAKE_C_COMPILER=clang", - "-DCMAKE_CXX_COMPILER=clang++", - ] - - build_command = ["cmake", "--build", build_dir, "--", "ttnn-dylib"] + shutil.copy2( + os.path.join(tt_mlir_home, "tools/ttnn-standalone/ttnn-precompiled.hpp"), + temp_source_dir, + ) + shutil.copy2( + os.path.join(tt_mlir_home, "tools/ttnn-standalone/ttnn-dylib.hpp"), + temp_source_dir, + ) + shutil.copy2( + os.path.join(tt_mlir_home, "tools/ttnn-standalone/ttnn-standalone.cpp"), + temp_source_dir, + ) # TODO(svuckovic): remove the need for this + + # Copy the input .cpp file to the temporary source directory + temp_cpp_path = os.path.join(temp_source_dir, "ttnn-dylib.cpp") + shutil.copy2(cpp_file_path, temp_cpp_path) + + print(f"Temporary source directory created: {temp_source_dir}") + + # Define the commands to be executed + cmake_command = [ + "cmake", + "-G", + "Ninja", + "-B", + temp_build_dir, + "-S", + temp_source_dir, + "-DCMAKE_BUILD_TYPE=Release", + "-DCMAKE_C_COMPILER=clang", + "-DCMAKE_CXX_COMPILER=clang++", + ] + + build_command = ["cmake", "--build", temp_build_dir, "--", "ttnn-dylib"] - try: # Run the cmake command print("Running cmake command...") subprocess.run(cmake_command, check=True, cwd=tt_mlir_home) @@ -87,14 +97,13 @@ def main(): print("Build completed successfully.") # Determine the output .so file - compiled_so_path = os.path.join(build_dir, "libttnn-dylib.so") + compiled_so_path = os.path.join(temp_build_dir, "libttnn-dylib.so") if not os.path.isfile(compiled_so_path): print(f"Error: Compiled file '{compiled_so_path}' not found.") sys.exit(1) # Define the destination path with renamed file - output_file_name = os.path.basename(cpp_file_path) - output_file_name = os.path.splitext(output_file_name)[0] + ".so" + output_file_name = cpp_base_name + ".so" destination_path = os.path.join(output_dir, output_file_name) # Copy and rename the .so file @@ -106,6 +115,13 @@ def main(): except Exception as e: print(f"Error during file operations: {e}") sys.exit(1) + finally: + # Cleanup temporary directories + shutil.rmtree(temp_source_dir, ignore_errors=True) + shutil.rmtree(temp_build_dir, ignore_errors=True) + print( + f"Cleaned up temporary directories: {temp_source_dir} and {temp_build_dir}" + ) if __name__ == "__main__":