diff --git a/examples/BuddyBert/CMakeLists.txt b/examples/BuddyBert/CMakeLists.txt index 95c98dfa96..419c695c6e 100644 --- a/examples/BuddyBert/CMakeLists.txt +++ b/examples/BuddyBert/CMakeLists.txt @@ -1,33 +1,38 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyBert/forward.mlir ${BUDDY_EXAMPLES_DIR}/BuddyBert/subgraph0.mlir ${BUDDY_EXAMPLES_DIR}/BuddyBert/arg0.data ${BUDDY_EXAMPLES_DIR}/BuddyBert/arg1.data - COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyBert/import-bert.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + ${CMAKE_CURRENT_BINARY_DIR}/arg1.data + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/import-bert.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files" ) + add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyBert/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, func.func(linalg-bufferize, tensor-bufferize), func-bufferize)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -pass-pipeline "builtin.module(func.func(buffer-deallocation-simplification, convert-linalg-to-loops), eliminate-empty-tensors, func.func(llvm-request-c-wrappers),convert-math-to-llvm, convert-math-to-libm, convert-scf-to-cf, convert-arith-to-llvm, expand-strided-metadata, finalize-memref-to-llvm, convert-func-to-llvm, reconcile-unrealized-casts)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyBert/forward.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyBert/forward.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o" VERBATIM) add_custom_command( OUTPUT subgraph0.o - COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyBert/subgraph0.mlir + COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, func-bufferize-dynamic-offset, arith-bufferize, func.func(linalg-bufferize, tensor-bufferize))" | ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -pass-pipeline "builtin.module(func.func(buffer-deallocation-simplification, convert-linalg-to-loops), eliminate-empty-tensors, func.func(llvm-request-c-wrappers),convert-math-to-llvm, convert-math-to-libm, convert-scf-to-cf, convert-arith-to-llvm, expand-strided-metadata, finalize-memref-to-llvm, convert-func-to-llvm, reconcile-unrealized-casts)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyBert/subgraph0.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyBert/subgraph0.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir COMMENT "Building subgraph0.o" VERBATIM) @@ -36,6 +41,15 @@ add_library(BERT STATIC forward.o subgraph0.o) SET_TARGET_PROPERTIES(BERT PROPERTIES LINKER_LANGUAGE C) add_executable(buddy-bert-run bert-main.cpp) + +set(BERT_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(BERT_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-bert-run PRIVATE + BERT_EXAMPLE_PATH="${BERT_EXAMPLE_PATH}" + BERT_EXAMPLE_BUILD_PATH="${BERT_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-bert-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_BERT_LIBS BERT mlir_c_runner_utils) diff --git a/examples/BuddyBert/bert-main.cpp b/examples/BuddyBert/bert-main.cpp index d3f0075491..c9d3073527 100644 --- a/examples/BuddyBert/bert-main.cpp +++ b/examples/BuddyBert/bert-main.cpp @@ -68,10 +68,12 @@ int main() { std::cout << "\033[33;1m" << title << "\033[0m" << std::endl; /// Load weights to MemRef container. + std::string bertDir = BERT_EXAMPLE_PATH; + std::string bertBuildDir = BERT_EXAMPLE_BUILD_PATH; MemRef arg0({109486854}); MemRef arg1({512}); - loadParameters("../../examples/BuddyBert/arg0.data", - "../../examples/BuddyBert/arg1.data", arg0, arg1); + loadParameters(bertBuildDir + "/arg0.data", + bertBuildDir + "/arg1.data", arg0, arg1); /// Get user message and build Text container. std::cout << "What sentence do you want to say to BERT?" << std::endl; @@ -81,7 +83,7 @@ int main() { Text pureStrContainer(pureStr); /// Define vacabulary and tokenize the - std::string vocabDir = "../../examples/BuddyBert/vocab.txt"; + std::string vocabDir = bertDir + "/vocab.txt"; pureStrContainer.tokenizeBert(vocabDir, 5); /// Initialize data containers. diff --git a/examples/BuddyBert/import-bert.py b/examples/BuddyBert/import-bert.py index 8c408b1fb7..8543eecf21 100644 --- a/examples/BuddyBert/import-bert.py +++ b/examples/BuddyBert/import-bert.py @@ -19,6 +19,7 @@ # ===--------------------------------------------------------------------------- import os +import argparse from pathlib import Path import numpy as np @@ -30,6 +31,20 @@ from torch._inductor.decomposition import decompositions as inductor_decomp from transformers import BertForSequenceClassification, BertTokenizer +# Parse command-line arguments +parser = argparse.ArgumentParser(description="BERT model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files" +) +args = parser.parse_args() + +# Ensure output directory exists +output_dir = os.path.abspath(args.output_dir) +os.makedirs(output_dir, exist_ok=True) + model = BertForSequenceClassification.from_pretrained( "bhadresh-savani/bert-base-uncased-emotion" ) @@ -57,20 +72,19 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: + +# Write the MLIR module and forward graph to the specified output directory +with open(os.path.join(output_dir, "subgraph0.mlir"), "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward.mlir"), "w") as module_file: print(driver.construct_main_graph(True), file=module_file) params = dynamo_compiler.imported_params[graph] -current_path = os.path.dirname(os.path.abspath(__file__)) float32_param = np.concatenate( [param.detach().numpy().reshape([-1]) for param in params[:-1]] ) - -float32_param.tofile(Path(current_path) / "arg0.data") +float32_param.tofile(Path(output_dir) / "arg0.data") int64_param = params[-1].detach().numpy().reshape([-1]) -int64_param.tofile(Path(current_path) / "arg1.data") +int64_param.tofile(Path(output_dir) / "arg1.data") diff --git a/examples/BuddyLeNet/CMakeLists.txt b/examples/BuddyLeNet/CMakeLists.txt index d460d0e345..646f12699c 100644 --- a/examples/BuddyLeNet/CMakeLists.txt +++ b/examples/BuddyLeNet/CMakeLists.txt @@ -1,25 +1,28 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/forward.mlir ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/subgraph0.mlir ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/arg0.data - COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/buddy-lenet-import.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/buddy-lenet-import.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files" ) add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, func.func(linalg-bufferize, tensor-bufferize), func-bufferize)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -pass-pipeline "builtin.module(func.func(buffer-deallocation-simplification, convert-linalg-to-loops), eliminate-empty-tensors, func.func(llvm-request-c-wrappers),convert-math-to-llvm, convert-math-to-libm, convert-scf-to-cf, convert-arith-to-llvm, expand-strided-metadata, finalize-memref-to-llvm, convert-func-to-llvm, reconcile-unrealized-casts)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyLeNet/forward.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/forward.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o" VERBATIM) add_custom_command( OUTPUT subgraph0.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/subgraph0.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -eliminate-empty-tensors @@ -45,11 +48,14 @@ add_custom_command( -reconcile-unrealized-casts | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyLeNet/subgraph0.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/subgraph0.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir COMMENT "Building subgraph0.o" VERBATIM) +set(LENET_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(LENET_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + add_library(LENET STATIC subgraph0.o forward.o) SET_TARGET_PROPERTIES(LENET PROPERTIES LINKER_LANGUAGE C) @@ -64,6 +70,11 @@ set(BUDDY_LENET_LIBS LENET mlir_c_runner_utils mlir_async_runtime mlir_runner_ut target_link_libraries(buddy-lenet-run ${BUDDY_LENET_LIBS}) +target_compile_definitions(buddy-lenet-run PRIVATE + LENET_EXAMPLE_PATH="${LENET_EXAMPLE_PATH}" + LENET_EXAMPLE_BUILD_PATH="${LENET_EXAMPLE_BUILD_PATH}" +) + set(ONE_SHOT_BUFFERIZE_OPTION "bufferize-function-boundaries=1 function-boundary-type-conversion=identity-layout-map") set(LOWER_TO_NVVM_OPTION "cubin-chip=sm_80 cubin-features=+ptx71 cubin-format=fatbin") set(CONVERT_MEMCPY_TO_GPU_OPTION "process-args=1") @@ -71,20 +82,20 @@ set(CONVERT_MEMCPY_TO_GPU_OPTION_DISABLE_PROCESS_ARG "process-args=0") add_custom_command( OUTPUT forward_gpu.o - COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/forward.mlir + COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -buffer-deallocation -canonicalize -cse -expand-strided-metadata -convert-memcpy-to-gpu -gpu-async-region | ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -llvm-request-c-wrappers --gpu-to-llvm | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyLeNet/forward_gpu.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/forward.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/forward_gpu.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward_gpu.o" VERBATIM) add_custom_command( OUTPUT subgraph0_gpu.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/subgraph0.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -one-shot-bufferize @@ -101,8 +112,8 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -llvm-request-c-wrappers --test-lower-to-nvvm=${LOWER_TO_NVVM_OPTION} | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyLeNet/subgraph0_gpu.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyLeNet/subgraph0.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_gpu.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir COMMENT "Building subgraph0_gpu.o" VERBATIM) @@ -116,3 +127,8 @@ target_link_directories(buddy-lenet-run-gpu PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_LENET_LIBS_GPU LENET_GPU mlir_c_runner_utils mlir_async_runtime mlir_runner_utils mlir_cuda_runtime ${PNG_LIBRARIES}) target_link_libraries(buddy-lenet-run-gpu ${BUDDY_LENET_LIBS_GPU}) + +target_compile_definitions(buddy-lenet-run-gpu PRIVATE + LENET_EXAMPLE_PATH="${LENET_EXAMPLE_PATH}" + LENET_EXAMPLE_BUILD_PATH="${LENET_EXAMPLE_BUILD_PATH}" +) diff --git a/examples/BuddyLeNet/README.md b/examples/BuddyLeNet/README.md index 8ddab714b4..1552845177 100644 --- a/examples/BuddyLeNet/README.md +++ b/examples/BuddyLeNet/README.md @@ -77,12 +77,6 @@ $ export LLVM_MLIR_BUILD_DIR=$PWD/../llvm/build $ export PYTHONPATH=${LLVM_MLIR_BUILD_DIR}/tools/mlir/python_packages/mlir_core:${BUDDY_MLIR_BUILD_DIR}/python_packages:${PYTHONPATH} ``` -### Set the `LENET_EXAMPLE_PATH` environment variable. - -```bash -$ export LENET_EXAMPLE_PATH=${BUDDY_MLIR_BUILD_DIR}/../examples/BuddyLeNet/ -``` - ### Build and run the LeNet example ```bash diff --git a/examples/BuddyLeNet/buddy-lenet-import.py b/examples/BuddyLeNet/buddy-lenet-import.py index c787061a55..54700bcd53 100644 --- a/examples/BuddyLeNet/buddy-lenet-import.py +++ b/examples/BuddyLeNet/buddy-lenet-import.py @@ -20,6 +20,7 @@ import os from pathlib import Path +import argparse import numpy as np import torch @@ -30,12 +31,22 @@ from buddy.compiler.ops import tosa from model import LeNet -# Retrieve the LeNet model path from environment variables. -model_path = os.environ.get("LENET_EXAMPLE_PATH") -if model_path is None: - raise EnvironmentError( - "The environment variable 'LENET_MODEL_PATH' is not set or is invalid." - ) +# Parse command-line arguments. +parser = argparse.ArgumentParser(description="LeNet model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files." +) +args = parser.parse_args() + +# Ensure output directory exists. +output_dir = Path(args.output_dir) +output_dir.mkdir(parents=True, exist_ok=True) + +# Retrieve the LeNet model path. +model_path = os.path.dirname(os.path.abspath(__file__)) model = LeNet() model = torch.load(model_path + "/lenet-model.pth", weights_only=False) @@ -58,17 +69,15 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: +with open(output_dir / "subgraph0.mlir", "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(output_dir / "forward.mlir", "w") as module_file: print(driver.construct_main_graph(True), file=module_file) params = dynamo_compiler.imported_params[graph] -current_path = os.path.dirname(os.path.abspath(__file__)) float32_param = np.concatenate( [param.detach().numpy().reshape([-1]) for param in params] ) -float32_param.tofile(Path(current_path) / "arg0.data") +float32_param.tofile(output_dir / "arg0.data") diff --git a/examples/BuddyLeNet/buddy-lenet-main.cpp b/examples/BuddyLeNet/buddy-lenet-main.cpp index 2fc8b0fbe3..23b95480d2 100644 --- a/examples/BuddyLeNet/buddy-lenet-main.cpp +++ b/examples/BuddyLeNet/buddy-lenet-main.cpp @@ -99,13 +99,14 @@ int main() { intptr_t sizesOutput[2] = {1, 10}; // Create input and output containers for the image and model output. - std::string lenetDir = getenv("LENET_EXAMPLE_PATH"); + std::string lenetDir = LENET_EXAMPLE_PATH; + std::string lenetBuildDir = LENET_EXAMPLE_BUILD_PATH; std::string imgPath = lenetDir + "/images/" + ImgName; dip::Image input(imgPath, dip::DIP_GRAYSCALE, true /* norm */); MemRef output(sizesOutput); // Load model parameters from the specified file. - std::string paramsDir = lenetDir + "/arg0.data"; + std::string paramsDir = lenetBuildDir + "/arg0.data"; MemRef paramsContainer({ParamsSize}); loadParameters(paramsDir, paramsContainer); diff --git a/examples/BuddyLlama/CMakeLists.txt b/examples/BuddyLlama/CMakeLists.txt index 6953b7de7d..e7d313ad90 100644 --- a/examples/BuddyLlama/CMakeLists.txt +++ b/examples/BuddyLlama/CMakeLists.txt @@ -1,12 +1,16 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyLlama/forward.mlir ${BUDDY_EXAMPLES_DIR}/BuddyLlama/subgraph0.mlir ${BUDDY_EXAMPLES_DIR}/BuddyLlama/arg0.data - COMMAND ${Python3_EXECUTABLE} ${BUDDY_EXAMPLES_DIR}/BuddyLlama/import-llama2.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/import-llama2.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and arg0.data..." ) + add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyLlama/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -arith-expand @@ -43,14 +47,14 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyLlama/forward.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyLlama/forward.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o " VERBATIM) add_custom_command( OUTPUT subgraph.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyLlama/subgraph0.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -convert-elementwise-to-linalg @@ -89,8 +93,8 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyLlama/subgraph.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyLlama/subgraph0.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir COMMENT "Building subgraph.o " VERBATIM) @@ -108,6 +112,15 @@ SET_TARGET_PROPERTIES( LINKER_LANGUAGE C) add_executable(buddy-llama-run llama-main.cpp) + +set(LLAMA_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(LLAMA_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-llama-run PRIVATE + LLAMA_EXAMPLE_PATH="${LLAMA_EXAMPLE_PATH}" + LLAMA_EXAMPLE_BUILD_PATH="${LLAMA_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-llama-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_LLAMA_LIBS diff --git a/examples/BuddyLlama/import-llama2.py b/examples/BuddyLlama/import-llama2.py index d893ee87f6..b1a896c30a 100644 --- a/examples/BuddyLlama/import-llama2.py +++ b/examples/BuddyLlama/import-llama2.py @@ -19,6 +19,7 @@ # ===--------------------------------------------------------------------------- import os +import argparse import torch import torch._dynamo as dynamo from transformers import LlamaForCausalLM, LlamaTokenizer @@ -30,6 +31,20 @@ from buddy.compiler.graph import GraphDriver from buddy.compiler.graph.transform import simply_fuse +# Add argument parser to allow custom output directory. +parser = argparse.ArgumentParser(description="LLaMA2 model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files." +) +args = parser.parse_args() + +# Ensure the output directory exists. +output_dir = args.output_dir +os.makedirs(output_dir, exist_ok=True) + # Retrieve the LLaMA model path from environment variables. model_path = os.environ.get("LLAMA_MODEL_PATH") if model_path is None: @@ -60,12 +75,13 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: + +# Save the generated files to the specified output directory. +with open(os.path.join(output_dir, "subgraph0.mlir"), "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward.mlir"), "w") as module_file: print(driver.construct_main_graph(True), file=module_file) all_param = numpy.concatenate( [param.detach().numpy().reshape([-1]) for param in params] ) -all_param.tofile(os.path.join(path_prefix, "arg0.data")) +all_param.tofile(os.path.join(output_dir, "arg0.data")) diff --git a/examples/BuddyLlama/llama-main.cpp b/examples/BuddyLlama/llama-main.cpp index 61c42f0db2..3e73dc37a3 100644 --- a/examples/BuddyLlama/llama-main.cpp +++ b/examples/BuddyLlama/llama-main.cpp @@ -114,8 +114,10 @@ int main() { std::cout << "\033[33;1m" << title << "\033[0m" << std::endl; /// Define directories of vacabulary and parameter file. - const std::string vocabDir = "../../examples/BuddyLlama/vocab.txt"; - const std::string paramsDir = "../../examples/BuddyLlama/arg0.data"; + std::string llamaDir = LLAMA_EXAMPLE_PATH; + std::string llamaBuildDir = LLAMA_EXAMPLE_BUILD_PATH; + const std::string vocabDir = llamaDir + "/vocab.txt"; + const std::string paramsDir = llamaBuildDir + "/arg0.data"; /// Get user message. std::string inputStr; diff --git a/examples/BuddyMobileNetV3/CMakeLists.txt b/examples/BuddyMobileNetV3/CMakeLists.txt index ef60c7e931..20cc51c4b0 100644 --- a/examples/BuddyMobileNetV3/CMakeLists.txt +++ b/examples/BuddyMobileNetV3/CMakeLists.txt @@ -1,15 +1,16 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/arg0.data - ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/forward.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/subgraph0.mlir - COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/buddy-mobilenetv3-import.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/buddy-mobilenetv3-import.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files" ) add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), \ empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, \ @@ -24,15 +25,15 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyMobileNetV3/forward.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/forward.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o" VERBATIM) add_custom_command( OUTPUT subgraph0.o - COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/subgraph0.mlir + COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-arith, tosa-to-linalg, tosa-to-tensor))" | ${BUDDY_BINARY_DIR}/buddy-opt @@ -57,8 +58,8 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyMobileNetV3/subgraph0.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/subgraph0.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir buddy-opt COMMENT "Building subgraph0.o" VERBATIM) @@ -68,6 +69,15 @@ add_library(MOBILENETV3 STATIC subgraph0.o forward.o) SET_TARGET_PROPERTIES(MOBILENETV3 PROPERTIES LINKER_LANGUAGE C) add_executable(buddy-mobilenetv3-run buddy-mobilenetv3-main.cpp) + +set(MOBILENETV3_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(MOBILENETV3_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-mobilenetv3-run PRIVATE + MOBILENETV3_EXAMPLE_PATH="${MOBILENETV3_EXAMPLE_PATH}" + MOBILENETV3_EXAMPLE_BUILD_PATH="${MOBILENETV3_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-mobilenetv3-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_MOBILENETV3_LIBS MOBILENETV3 mlir_c_runner_utils BuddyLibDIP ${PNG_LIBRARIES}) diff --git a/examples/BuddyMobileNetV3/README.md b/examples/BuddyMobileNetV3/README.md index a55cd74304..0d50dd34e4 100644 --- a/examples/BuddyMobileNetV3/README.md +++ b/examples/BuddyMobileNetV3/README.md @@ -32,13 +32,7 @@ $ export LLVM_MLIR_BUILD_DIR=$PWD/../llvm/build $ export PYTHONPATH=${LLVM_MLIR_BUILD_DIR}/tools/mlir/python_packages/mlir_core:${BUDDY_MLIR_BUILD_DIR}/python_packages:${PYTHONPATH} ``` -3. Set the `MOBILENETV3_EXAMPLE_PATH` environment variable. - -```bash -$ export MOBILENETV3_EXAMPLE_PATH=${BUDDY_MLIR_BUILD_DIR}/../examples/BuddyMobileNetV3/ -``` - -4. Build and run the MobileNetV3 example +3. Build and run the MobileNetV3 example ```bash $ cmake -G Ninja .. -DBUDDY_MOBILENETV3_EXAMPLES=ON diff --git a/examples/BuddyMobileNetV3/buddy-mobilenetv3-import.py b/examples/BuddyMobileNetV3/buddy-mobilenetv3-import.py index 062d7cb653..84b541b663 100644 --- a/examples/BuddyMobileNetV3/buddy-mobilenetv3-import.py +++ b/examples/BuddyMobileNetV3/buddy-mobilenetv3-import.py @@ -19,11 +19,10 @@ # ===--------------------------------------------------------------------------- import os - from pathlib import Path +import argparse import numpy as np import torch -import torch._inductor.lowering import torchvision.models as models from torch._inductor.decomposition import decompositions as inductor_decomp @@ -32,12 +31,22 @@ from buddy.compiler.graph.transform import simply_fuse from buddy.compiler.ops import tosa -# Retrieve the MobileNet V3 model path from environment variables. -model_path = os.environ.get("MOBILENETV3_EXAMPLE_PATH") -if model_path is None: - raise EnvironmentError( - "The environment variable 'MOBILENETV3_MODEL_PATH' is not set or is invalid." - ) +# Parse command-line arguments. +parser = argparse.ArgumentParser(description="MobileNetV3 model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files." +) +args = parser.parse_args() + +# Ensure output directory exists. +output_dir = Path(args.output_dir) +output_dir.mkdir(parents=True, exist_ok=True) + +# Retrieve the MobileNet V3 model path. +model_path = os.path.dirname(os.path.abspath(__file__)) model = models.mobilenet_v3_small( weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1, pretrained=True @@ -66,16 +75,13 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: +# Write generated files to the specified output directory. +with open(output_dir / "subgraph0.mlir", "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(output_dir / "forward.mlir", "w") as module_file: print(driver.construct_main_graph(True), file=module_file) -params = dynamo_compiler.imported_params[graph] -current_path = os.path.dirname(os.path.abspath(__file__)) - - +# Export parameters. float32_param = np.concatenate( [ param.detach().numpy().reshape([-1]) @@ -83,4 +89,4 @@ if param.dtype == torch.float32 ] ) -float32_param.tofile(Path(current_path) / "arg0.data") +float32_param.tofile(output_dir / "arg0.data") diff --git a/examples/BuddyMobileNetV3/buddy-mobilenetv3-main.cpp b/examples/BuddyMobileNetV3/buddy-mobilenetv3-main.cpp index 90defb895e..9b2a57fcde 100644 --- a/examples/BuddyMobileNetV3/buddy-mobilenetv3-main.cpp +++ b/examples/BuddyMobileNetV3/buddy-mobilenetv3-main.cpp @@ -93,8 +93,8 @@ void softmax(float *input, size_t size) { } std::string getLabel(int idx) { - std::string mobilenetDir = getenv("MOBILENETV3_EXAMPLE_PATH"); - std::ifstream in(mobilenetDir + "Labels.txt"); + std::string mobilenetDir = MOBILENETV3_EXAMPLE_PATH; + std::ifstream in(mobilenetDir + "/Labels.txt"); assert(in.is_open() && "Could not read the label file."); std::string label; for (int i = 0; i < idx; ++i) @@ -113,7 +113,8 @@ int main() { intptr_t sizesOutput[2] = {1, 1000}; // Create input and output containers for the image and model output. - std::string mobilenetDir = getenv("MOBILENETV3_EXAMPLE_PATH"); + std::string mobilenetDir = MOBILENETV3_EXAMPLE_PATH; + std::string mobilenetBuildDir = MOBILENETV3_EXAMPLE_BUILD_PATH; std::string imgPath = mobilenetDir + "/images/" + ImgName; dip::Image input(imgPath, dip::DIP_RGB, true /* norm */); MemRef inputResize = dip::Resize4D_NCHW( @@ -123,7 +124,7 @@ int main() { MemRef output(sizesOutput); // Load model parameters from the specified file. - std::string paramsDir = mobilenetDir + "/arg0.data"; + std::string paramsDir = mobilenetBuildDir + "/arg0.data"; MemRef paramsContainer({ParamsSize}); loadParameters(paramsDir, paramsContainer); // Call the forward function of the model. diff --git a/examples/BuddyResNet18/CMakeLists.txt b/examples/BuddyResNet18/CMakeLists.txt index 034012c9ef..f298f864e2 100644 --- a/examples/BuddyResNet18/CMakeLists.txt +++ b/examples/BuddyResNet18/CMakeLists.txt @@ -1,15 +1,16 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/arg0.data - ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir - COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/buddy-resnet-import.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/buddy-resnet-import.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files" ) add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), \ empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, \ @@ -24,15 +25,15 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyResNet18/forward.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o" VERBATIM) add_custom_command( OUTPUT subgraph0.o - COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir + COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-arith, tosa-to-linalg, tosa-to-tensor))" | ${BUDDY_BINARY_DIR}/buddy-opt @@ -57,8 +58,8 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyResNet18/subgraph0.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir buddy-opt COMMENT "Building subgraph0.o" VERBATIM) @@ -68,6 +69,15 @@ add_library(RESNET STATIC subgraph0.o forward.o) SET_TARGET_PROPERTIES(RESNET PROPERTIES LINKER_LANGUAGE C) add_executable(buddy-resnet-run buddy-resnet-main.cpp) + +set(RESNET_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(RESNET_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-resnet-run PRIVATE + RESNET_EXAMPLE_PATH="${RESNET_EXAMPLE_PATH}" + RESNET_EXAMPLE_BUILD_PATH="${RESNET_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-resnet-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_RESNET_LIBS RESNET mlir_c_runner_utils BuddyLibDIP ${PNG_LIBRARIES}) diff --git a/examples/BuddyResNet18/README.md b/examples/BuddyResNet18/README.md index 7813e98255..5374dc4707 100644 --- a/examples/BuddyResNet18/README.md +++ b/examples/BuddyResNet18/README.md @@ -58,12 +58,6 @@ $ export LLVM_MLIR_BUILD_DIR=$PWD/../llvm/build $ export PYTHONPATH=${LLVM_MLIR_BUILD_DIR}/tools/mlir/python_packages/mlir_core:${BUDDY_MLIR_BUILD_DIR}/python_packages:${PYTHONPATH} ``` -4.Set the `RESNET_EXAMPLE_PATH` environment variable. - -```bash -$ export RESNET_EXAMPLE_PATH=${BUDDY_MLIR_BUILD_DIR}/../examples/BuddyResNet18/ -``` - 4. Build and run the ResNet example ```bash diff --git a/examples/BuddyResNet18/buddy-resnet-import.py b/examples/BuddyResNet18/buddy-resnet-import.py index 725c1ab3a1..364ba44a85 100644 --- a/examples/BuddyResNet18/buddy-resnet-import.py +++ b/examples/BuddyResNet18/buddy-resnet-import.py @@ -19,7 +19,7 @@ # ===--------------------------------------------------------------------------- import os - +import argparse from pathlib import Path import numpy as np import torch @@ -33,12 +33,22 @@ from buddy.compiler.graph.transform import simply_fuse from buddy.compiler.ops import tosa -# Retrieve the ResNet18 model path from environment variables. -model_path = os.environ.get("RESNET_EXAMPLE_PATH") -if model_path is None: - raise EnvironmentError( - "The environment variable 'RESNET_MODEL_PATH' is not set or is invalid." - ) +# Parse command-line arguments +parser = argparse.ArgumentParser(description="ResNet18 model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files." +) +args = parser.parse_args() + +# Ensure output directory exists +output_dir = os.path.abspath(args.output_dir) +os.makedirs(output_dir, exist_ok=True) + +# Retrieve the ResNet18 model path. +model_path = os.path.dirname(os.path.abspath(__file__)) model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) model = model.eval() @@ -71,16 +81,16 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: + +# Write the MLIR module and forward graph to the specified output directory +with open(os.path.join(output_dir, "subgraph0.mlir"), "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward.mlir"), "w") as module_file: print(driver.construct_main_graph(True), file=module_file) params = dynamo_compiler.imported_params[graph] current_path = os.path.dirname(os.path.abspath(__file__)) - float32_param = np.concatenate( [ param.detach().numpy().reshape([-1]) @@ -88,4 +98,4 @@ if param.dtype == torch.float32 ] ) -float32_param.tofile(Path(current_path) / "arg0.data") +float32_param.tofile(Path(output_dir) / "arg0.data") diff --git a/examples/BuddyResNet18/buddy-resnet-main.cpp b/examples/BuddyResNet18/buddy-resnet-main.cpp index 43933b6e49..25fa279f3c 100644 --- a/examples/BuddyResNet18/buddy-resnet-main.cpp +++ b/examples/BuddyResNet18/buddy-resnet-main.cpp @@ -93,8 +93,8 @@ void softmax(float *input, size_t size) { } std::string getLabel(int idx) { - std::string resnetDir = getenv("RESNET_EXAMPLE_PATH"); - std::ifstream in(resnetDir + "Labels.txt"); + std::string resnetDir = RESNET_EXAMPLE_PATH; + std::ifstream in(resnetDir + "/Labels.txt"); assert(in.is_open() && "Could not read the label file."); std::string label; for (int i = 0; i < idx; ++i) @@ -113,7 +113,8 @@ int main() { intptr_t sizesOutput[2] = {1, 1000}; // Create input and output containers for the image and model output. - std::string resnetDir = getenv("RESNET_EXAMPLE_PATH"); + std::string resnetDir = RESNET_EXAMPLE_PATH; + std::string resnetBuildDir = RESNET_EXAMPLE_BUILD_PATH; std::string imgPath = resnetDir + "/images/" + ImgName; dip::Image input(imgPath, dip::DIP_RGB, true /* norm */); MemRef inputResize = dip::Resize4D_NCHW( @@ -123,7 +124,7 @@ int main() { MemRef output(sizesOutput); // Load model parameters from the specified file. - std::string paramsDir = resnetDir + "/arg0.data"; + std::string paramsDir = resnetBuildDir + "/arg0.data"; MemRef paramsContainer({ParamsSize}); loadParameters(paramsDir, paramsContainer); // Call the forward function of the model. diff --git a/examples/BuddyStableDiffusion/CMakeLists.txt b/examples/BuddyStableDiffusion/CMakeLists.txt index 10ce9f1a85..d693b1c3dd 100644 --- a/examples/BuddyStableDiffusion/CMakeLists.txt +++ b/examples/BuddyStableDiffusion/CMakeLists.txt @@ -1,21 +1,22 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/arg0_text_encoder.data - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/arg1_text_encoder.data - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/arg0_unet.data - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/arg0_vae.data - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_text_encoder.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_text_encoder.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_unet.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_unet.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_vae.mlir - ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_vae.mlir - COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/import-stable-diffusion.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/arg0_text_encoder.data + ${CMAKE_CURRENT_BINARY_DIR}arg1_text_encoder.data + ${CMAKE_CURRENT_BINARY_DIR}/arg0_unet.data + ${CMAKE_CURRENT_BINARY_DIR}/arg0_vae.data + ${CMAKE_CURRENT_BINARY_DIR}/forward_text_encoder.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_text_encoder.mlir + ${CMAKE_CURRENT_BINARY_DIR}/forward_unet.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_unet.mlir + ${CMAKE_CURRENT_BINARY_DIR}/forward_vae.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_vae.mlir + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/import-stable-diffusion.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files" ) add_custom_command( OUTPUT forward_text_encoder.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_text_encoder.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_text_encoder.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -arith-expand @@ -52,14 +53,14 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/forward_text_encoder.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_text_encoder.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward_text_encoder.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_text_encoder.mlir COMMENT "Building forward_text_encoder.o " VERBATIM) add_custom_command( OUTPUT subgraph0_text_encoder.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_text_encoder.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_text_encoder.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -convert-elementwise-to-linalg @@ -67,6 +68,12 @@ add_custom_command( -eliminate-empty-tensors -empty-tensor-to-alloc-tensor -one-shot-bufferize + -func-bufferize-dynamic-offset + -tensor-bufferize + -arith-bufferize + -buffer-deallocation + -finalizing-bufferize + -conv-nhwc-fhwc-optimize -matmul-parallel-vectorization-optimize -batchmatmul-optimize -convert-linalg-to-affine-loops @@ -74,11 +81,6 @@ add_custom_command( -affine-parallelize -lower-affine -convert-scf-to-openmp - -func-bufferize-dynamic-offset - -tensor-bufferize - -arith-bufferize - -buffer-deallocation - -finalizing-bufferize -convert-vector-to-scf -expand-strided-metadata -cse @@ -92,20 +94,20 @@ add_custom_command( -convert-openmp-to-llvm -convert-arith-to-llvm -convert-math-to-llvm - -convert-math-to-libm + -convert-math-to-libm -convert-func-to-llvm -reconcile-unrealized-casts | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/subgraph0_text_encoder.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_text_encoder.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_text_encoder.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_text_encoder.mlir COMMENT "Building subgraph0_text_encoder.o " VERBATIM) add_custom_command( OUTPUT forward_unet.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_unet.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_unet.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -arith-expand @@ -142,14 +144,14 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/forward_unet.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_unet.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward_unet.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_unet.mlir COMMENT "Building forward_unet.o " VERBATIM) add_custom_command( OUTPUT subgraph0_unet.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_unet.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_unet.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -convert-elementwise-to-linalg @@ -157,6 +159,12 @@ add_custom_command( -eliminate-empty-tensors -empty-tensor-to-alloc-tensor -one-shot-bufferize + -func-bufferize-dynamic-offset + -tensor-bufferize + -arith-bufferize + -buffer-deallocation + -finalizing-bufferize + -conv-nhwc-fhwc-optimize -matmul-parallel-vectorization-optimize -batchmatmul-optimize -convert-linalg-to-affine-loops @@ -164,11 +172,6 @@ add_custom_command( -affine-parallelize -lower-affine -convert-scf-to-openmp - -func-bufferize-dynamic-offset - -tensor-bufferize - -arith-bufferize - -buffer-deallocation - -finalizing-bufferize -convert-vector-to-scf -expand-strided-metadata -cse @@ -182,20 +185,20 @@ add_custom_command( -convert-openmp-to-llvm -convert-arith-to-llvm -convert-math-to-llvm - -convert-math-to-libm + -convert-math-to-libm -convert-func-to-llvm -reconcile-unrealized-casts | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/subgraph0_unet.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_unet.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_unet.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_unet.mlir COMMENT "Building subgraph0_unet.o " VERBATIM) add_custom_command( OUTPUT forward_vae.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_vae.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_vae.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -arith-expand @@ -232,14 +235,14 @@ add_custom_command( ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/forward_vae.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/forward_vae.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/forward_vae.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/forward_vae.mlir COMMENT "Building forward_vae.o " VERBATIM) add_custom_command( OUTPUT subgraph0_vae.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_vae.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_vae.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))" | ${BUDDY_BINARY_DIR}/buddy-opt -convert-elementwise-to-linalg @@ -247,6 +250,12 @@ add_custom_command( -eliminate-empty-tensors -empty-tensor-to-alloc-tensor -one-shot-bufferize + -func-bufferize-dynamic-offset + -tensor-bufferize + -arith-bufferize + -buffer-deallocation + -finalizing-bufferize + -conv-nhwc-fhwc-optimize -matmul-parallel-vectorization-optimize -batchmatmul-optimize -convert-linalg-to-affine-loops @@ -254,11 +263,6 @@ add_custom_command( -affine-parallelize -lower-affine -convert-scf-to-openmp - -func-bufferize-dynamic-offset - -tensor-bufferize - -arith-bufferize - -buffer-deallocation - -finalizing-bufferize -convert-vector-to-scf -expand-strided-metadata -cse @@ -272,14 +276,14 @@ add_custom_command( -convert-openmp-to-llvm -convert-arith-to-llvm -convert-math-to-llvm - -convert-math-to-libm + -convert-math-to-libm -convert-func-to-llvm -reconcile-unrealized-casts | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 - -o ${BUDDY_BINARY_DIR}/../examples/BuddyStableDiffusion/subgraph0_vae.o - DEPENDS buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyStableDiffusion/subgraph0_vae.mlir + -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_vae.o + DEPENDS buddy-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0_vae.mlir COMMENT "Building subgraph0_vae.o " VERBATIM) @@ -293,16 +297,22 @@ SET_TARGET_PROPERTIES(UNET PROPERTIES LINKER_LANGUAGE C) SET_TARGET_PROPERTIES(VAE PROPERTIES LINKER_LANGUAGE C) add_executable(buddy-stable-diffusion-run buddy-stable-diffusion-main.cpp) + +set(STABLE_DIFFUSION_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(STABLE_DIFFUSION_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-stable-diffusion-run PRIVATE + STABLE_DIFFUSION_EXAMPLE_PATH="${STABLE_DIFFUSION_EXAMPLE_PATH}" + STABLE_DIFFUSION_EXAMPLE_BUILD_PATH="${STABLE_DIFFUSION_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-stable-diffusion-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_STABLE_DIFFUSION_LIBS TEXTENCODER UNET VAE mlir_c_runner_utils omp) if(BUDDY_MLIR_USE_MIMALLOC) - list(APPEND BUDDY_LLAMA_LIBS mimalloc) + list(APPEND BUDDY_STABLE_DIFFUSION_LIBS mimalloc) endif() -find_package(JPEG REQUIRED) -find_package(PNG REQUIRED) - -target_link_libraries(buddy-stable-diffusion-run ${BUDDY_STABLE_DIFFUSION_LIBS} ${JPEG_LIBRARIES} ${PNG_LIBRARIES}) +target_link_libraries(buddy-stable-diffusion-run ${BUDDY_STABLE_DIFFUSION_LIBS}) diff --git a/examples/BuddyStableDiffusion/buddy-stable-diffusion-main.cpp b/examples/BuddyStableDiffusion/buddy-stable-diffusion-main.cpp index 712bed26eb..39d37015f5 100644 --- a/examples/BuddyStableDiffusion/buddy-stable-diffusion-main.cpp +++ b/examples/BuddyStableDiffusion/buddy-stable-diffusion-main.cpp @@ -15,7 +15,7 @@ //===----------------------------------------------------------------------===// #include #include -#include +#include #include #include #include @@ -298,15 +298,17 @@ int main() { std::cout << "\033[33;1m" << title << "\033[0m" << std::endl; // Define directories of vacabulary and parameter file. - const std::string vocabDir = "../../examples/BuddyStableDiffusion/vocab.txt"; + std::string stableDiffusionDir = STABLE_DIFFUSION_EXAMPLE_PATH; + std::string stableDiffusionBuildDir = STABLE_DIFFUSION_EXAMPLE_BUILD_PATH; + const std::string vocabDir = stableDiffusionDir + "/vocab.txt"; const std::string TextEncoderParamsDir1 = - "../../examples/BuddyStableDiffusion/arg0_text_encoder.data"; + stableDiffusionBuildDir + "/arg0_text_encoder.data"; const std::string TextEncoderParamsDir2 = - "../../examples/BuddyStableDiffusion/arg1_text_encoder.data"; + stableDiffusionBuildDir + "/arg1_text_encoder.data"; const std::string UnetParamsDir = - "../../examples/BuddyStableDiffusion/arg0_unet.data"; + stableDiffusionBuildDir + "/arg0_unet.data"; const std::string VaeParamsDir = - "../../examples/BuddyStableDiffusion/arg0_vae.data"; + stableDiffusionBuildDir + "/arg0_vae.data"; // Get user message. std::string inputStr; @@ -472,32 +474,21 @@ int main() { resultVae.getData()[i] = 1; resultVae.getData()[i] = resultVae.getData()[i] * 255; } - intptr_t sizes[4] = {512, 512, 3, 1}; - Img img(sizes); - - // Rearrange the images - for (int i = 0; i < 3 * 512 * 512; i += 3) { - img.getData()[i] = resultVae.getData()[i / 3 + 512 * 512 * 2]; - img.getData()[i + 1] = resultVae.getData()[i / 3 + 512 * 512 * 1]; - img.getData()[i + 2] = resultVae.getData()[i / 3 + 512 * 512 * 0]; - } + intptr_t sizes[4] = {1, 3, 512, 512}; + Image img(resultVae.getData(), sizes); - String Imgfilename = - "../../examples/BuddyStableDiffusion/" + image_name + ".bmp"; - // Call the imwrite function - bool success = imwrite(Imgfilename, img); + const std::string Imgfilename = + stableDiffusionBuildDir + "/" + image_name + ".bmp"; + // Call the imageWrite function + imageWrite(Imgfilename, img); printLogLabel(); std::cout << "The prompt used to generate the image:" << inputStr << std::endl; printLogLabel(); - if (success) { - std::cout << "Image saved successfully to " - << std::filesystem::canonical(Imgfilename) << std::endl; - } else { - std::cerr << "Failed to save the image." << std::endl; - } + std::cout << "Image saved successfully to " + << std::filesystem::canonical(Imgfilename) << std::endl; return 0; } diff --git a/examples/BuddyStableDiffusion/import-stable-diffusion.py b/examples/BuddyStableDiffusion/import-stable-diffusion.py index e39b08366a..ee76d33811 100644 --- a/examples/BuddyStableDiffusion/import-stable-diffusion.py +++ b/examples/BuddyStableDiffusion/import-stable-diffusion.py @@ -17,8 +17,9 @@ # This is the Stable Diffusion model AOT importer. # # ===--------------------------------------------------------------------------- -import os +import os +import argparse from pathlib import Path import numpy import torch @@ -31,6 +32,20 @@ from buddy.compiler.ops import tosa from diffusers import StableDiffusionPipeline +# Parse command-line arguments for output directory +parser = argparse.ArgumentParser(description="Stable Diffusion model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save the output files.", +) +args = parser.parse_args() +output_dir = args.output_dir + +# Ensure the output directory exists +os.makedirs(output_dir, exist_ok=True) + device = torch.device("cpu") model_id = "stabilityai/stable-diffusion-2-1-base" @@ -46,7 +61,6 @@ unet = pipe.unet.forward vae = pipe.vae.decode - # Initialize Dynamo Compiler with specific configurations as an importer. dynamo_compiler_text_encoder = DynamoCompiler( primary_registry=tosa.ops_registry, @@ -76,7 +90,6 @@ } data_vae = torch.ones((1, 4, 64, 64), dtype=torch.float32).to(device) - # Import the model into MLIR module and parameters. with torch.no_grad(): graphs_text_encoder = dynamo_compiler_text_encoder.importer( @@ -85,7 +98,6 @@ graphs_unet = dynamo_compiler_unet.importer(unet, **data_unet) graphs_vae = dynamo_compiler_vae.importer(vae, data_vae, return_dict=False) - assert len(graphs_text_encoder) == 1 assert len(graphs_unet) == 1 assert len(graphs_vae) == 1 @@ -144,25 +156,20 @@ driver_unet.subgraphs[0].lower_to_top_level_ir() driver_vae.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) - -with open( - os.path.join(path_prefix, "subgraph0_text_encoder.mlir"), "w" -) as module_file: +# Save output files to specified directory +with open(os.path.join(output_dir, "subgraph0_text_encoder.mlir"), "w") as module_file: print(driver_text_encoder.subgraphs[0]._imported_module, file=module_file) -with open( - os.path.join(path_prefix, "forward_text_encoder.mlir"), "w" -) as module_file: +with open(os.path.join(output_dir, "forward_text_encoder.mlir"), "w") as module_file: print(driver_text_encoder.construct_main_graph(True), file=module_file) -with open(os.path.join(path_prefix, "subgraph0_unet.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "subgraph0_unet.mlir"), "w") as module_file: print(driver_unet.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward_unet.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward_unet.mlir"), "w") as module_file: print(driver_unet.construct_main_graph(True), file=module_file) -with open(os.path.join(path_prefix, "subgraph0_vae.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "subgraph0_vae.mlir"), "w") as module_file: print(driver_vae.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward_vae.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward_vae.mlir"), "w") as module_file: print(driver_vae.construct_main_graph(True), file=module_file) float32_param_text_encoder = numpy.concatenate( @@ -172,23 +179,22 @@ ] ) float32_param_text_encoder.tofile( - os.path.join(path_prefix, "arg0_text_encoder.data") + os.path.join(output_dir, "arg0_text_encoder.data") ) int64_param_text_encoder = ( params_text_encoder[-1].detach().cpu().numpy().reshape([-1]) ) int64_param_text_encoder.tofile( - os.path.join(path_prefix, "arg1_text_encoder.data") + os.path.join(output_dir, "arg1_text_encoder.data") ) param_unet = numpy.concatenate( [param.detach().cpu().numpy().reshape([-1]) for param in params_unet] ) -param_unet.tofile(os.path.join(path_prefix, "arg0_unet.data")) +param_unet.tofile(os.path.join(output_dir, "arg0_unet.data")) param_vae = numpy.concatenate( [param.detach().cpu().numpy().reshape([-1]) for param in params_vae] ) -param_vae.tofile(os.path.join(path_prefix, "arg0_vae.data")) - +param_vae.tofile(os.path.join(output_dir, "arg0_vae.data")) diff --git a/examples/BuddyWhisper/CMakeLists.txt b/examples/BuddyWhisper/CMakeLists.txt index 756d6db081..e65a8943c4 100644 --- a/examples/BuddyWhisper/CMakeLists.txt +++ b/examples/BuddyWhisper/CMakeLists.txt @@ -1,25 +1,28 @@ add_custom_command( - OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/forward.mlir ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/subgraph0.mlir ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/arg0.data - COMMAND ${Python3_EXECUTABLE} ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/import-whisper.py + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir + ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir + ${CMAKE_CURRENT_BINARY_DIR}/arg0.data + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/import-whisper.py + --output-dir ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating forward.mlir, subgraph0.mlir and arg0.data..." ) set(PATTERN_ARG "test-generalize-pad-tensor") add_custom_command( OUTPUT forward.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/forward.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, func.func(linalg-bufferize, tensor-bufferize), func-bufferize)" | ${BUDDY_BINARY_DIR}/buddy-opt -pass-pipeline "builtin.module( func.func(buffer-deallocation-simplification, convert-linalg-to-loops),matmul-parallel-vectorization-optimize, batchmatmul-optimize, eliminate-empty-tensors,func-bufferize-dynamic-offset, func.func(llvm-request-c-wrappers),convert-scf-to-openmp, convert-openmp-to-llvm, convert-math-to-llvm, convert-math-to-libm, convert-scf-to-cf, convert-arith-to-llvm, expand-strided-metadata, finalize-memref-to-llvm, convert-func-to-llvm, reconcile-unrealized-casts)" | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${BUDDY_BINARY_DIR}/../examples/BuddyWhisper/forward.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/forward.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O0 -o ${CMAKE_CURRENT_BINARY_DIR}/forward.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/forward.mlir COMMENT "Building forward.o" VERBATIM) add_custom_command( OUTPUT subgraph0.o - COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/subgraph0.mlir + COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir -pass-pipeline "builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith))" | ${LLVM_TOOLS_BINARY_DIR}/mlir-opt -test-linalg-transform-patterns=${PATTERN_ARG} | @@ -57,8 +60,8 @@ add_custom_command( -reconcile-unrealized-casts | ${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir | ${LLVM_TOOLS_BINARY_DIR}/llvm-as | - ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 -o ${BUDDY_BINARY_DIR}/../examples/BuddyWhisper/subgraph0.o - DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyWhisper/subgraph0.mlir + ${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3 -o ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.o + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/subgraph0.mlir COMMENT "Building subgraph0.o " VERBATIM) @@ -80,6 +83,15 @@ set(BUDDY_WHISPER_FILES ) add_executable(buddy-whisper-run ${BUDDY_WHISPER_FILES}) + +set(WHISPER_EXAMPLE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(WHISPER_EXAMPLE_BUILD_PATH ${CMAKE_CURRENT_BINARY_DIR}) + +target_compile_definitions(buddy-whisper-run PRIVATE + WHISPER_EXAMPLE_PATH="${WHISPER_EXAMPLE_PATH}" + WHISPER_EXAMPLE_BUILD_PATH="${WHISPER_EXAMPLE_BUILD_PATH}" +) + target_link_directories(buddy-whisper-run PRIVATE ${LLVM_LIBRARY_DIR}) set(BUDDY_WHISPER_LIBS diff --git a/examples/BuddyWhisper/import-whisper.py b/examples/BuddyWhisper/import-whisper.py index 449646a676..1fcde42c63 100644 --- a/examples/BuddyWhisper/import-whisper.py +++ b/examples/BuddyWhisper/import-whisper.py @@ -19,6 +19,7 @@ # ===--------------------------------------------------------------------------- import os +import argparse import torch import torch._dynamo as dynamo from torch._inductor.decomposition import decompositions as inductor_decomp @@ -30,6 +31,19 @@ from buddy.compiler.graph import GraphDriver from buddy.compiler.graph.transform import simply_fuse +# Parse command-line arguments for output directory. +parser = argparse.ArgumentParser(description="Whisper model AOT importer") +parser.add_argument( + "--output-dir", + type=str, + default="./", + help="Directory to save output files.", +) +args = parser.parse_args() + +output_dir = args.output_dir +os.makedirs(output_dir, exist_ok=True) + # Retrieve the Whisper model path from environment variables. model_path = os.environ.get("WHISPER_MODEL_PATH") if model_path is None: @@ -64,16 +78,15 @@ graphs[0].fuse_ops(pattern_list) driver = GraphDriver(graphs[0]) driver.subgraphs[0].lower_to_top_level_ir() -path_prefix = os.path.dirname(os.path.abspath(__file__)) - -with open(os.path.join(path_prefix, "subgraph0.mlir"), "w") as module_file: +# Save the MLIR files and parameter data to the specified output directory. +with open(os.path.join(output_dir, "subgraph0.mlir"), "w") as module_file: print(driver.subgraphs[0]._imported_module, file=module_file) -with open(os.path.join(path_prefix, "forward.mlir"), "w") as module_file: +with open(os.path.join(output_dir, "forward.mlir"), "w") as module_file: print(driver.construct_main_graph(True), file=module_file) all_param = numpy.concatenate( [param.detach().numpy().reshape([-1]) for param in params] ) -all_param.tofile(os.path.join(path_prefix, "arg0.data")) +all_param.tofile(os.path.join(output_dir, "arg0.data")) diff --git a/examples/BuddyWhisper/whisper-main.cpp b/examples/BuddyWhisper/whisper-main.cpp index 011b5c847e..70b39379f6 100644 --- a/examples/BuddyWhisper/whisper-main.cpp +++ b/examples/BuddyWhisper/whisper-main.cpp @@ -115,15 +115,17 @@ int main() { std::cout << "\033[33;1m" << title << "\033[0m" << std::endl; /// Define directories of vacabulary and parameter file. - const std::string vocabDir = "../../examples/BuddyWhisper/vocab.txt"; - const std::string paramsDir = "../../examples/BuddyWhisper/arg0.data"; + std::string whisperDir = WHISPER_EXAMPLE_PATH; + std::string whisperBuildDir = WHISPER_EXAMPLE_BUILD_PATH; + const std::string vocabDir = whisperDir + "/vocab.txt"; + const std::string paramsDir = whisperBuildDir + "/arg0.data"; /// Initialize data containers // - Result container // - Output container. // - Parameters container. Text outputContainer; - Audio rawAudioContainer("../../examples/BuddyWhisper/audio.wav"); + Audio rawAudioContainer(whisperDir + "/audio.wav"); MemRef audioInput({1, 80, 3000}); MemRef resultContainer[2] = { MemRef({1, 1500, 512}, false, 0),