Skip to content

Commit

Permalink
race condition + clang fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
svuckovicTT committed Dec 27, 2024
1 parent c6da33c commit 09d1349
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
20 changes: 11 additions & 9 deletions runtime/lib/ttnn/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ std::vector<Tensor> 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<ForwardFunction>(dlsym(so, func_name.c_str()));
std::cout << "after" << std::endl;

const char *dlsym_error = dlerror();
Expand Down Expand Up @@ -643,16 +644,16 @@ bool compareOuts(std::vector<Tensor> &lhs, std::vector<Tensor> &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<int>(lhsTensor->get_dtype()) << ", "
<< static_cast<int>(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<int>(lhsTensor->get_layout()) << ", "
<< static_cast<int>(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;
Expand Down Expand Up @@ -681,8 +682,9 @@ bool compareOuts(std::vector<Tensor> &lhs, std::vector<Tensor> &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<int>(lhsData[i])
<< " != " << static_cast<int>(rhsData[i]) << std::endl;
return false;
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
96 changes: 56 additions & 40 deletions tools/ttnn-standalone/compile_dylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ def main():
print("Usage: script.py <path-to-cpp-file> <path-to-output-dir>")
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]

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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__":
Expand Down

0 comments on commit 09d1349

Please sign in to comment.