From 2673e8045b9d832dee48c9c2471b97a06d42249e Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Wed, 27 Jul 2022 13:40:46 -0400 Subject: [PATCH 01/13] Replace CHECK_EQ with TORCH_CHECK_EQ --- build_tools/autogen_ltc_backend.py | 2 +- .../csrc/base_lazy_backend/mlir_lowering_context.cpp | 2 +- .../torch_mlir/csrc/base_lazy_backend/mlir_node_lowering.cpp | 4 ++-- python/torch_mlir/csrc/base_lazy_backend/ops/to_copy.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build_tools/autogen_ltc_backend.py b/build_tools/autogen_ltc_backend.py index 44c56b6454f5..bfe51c5ec1a7 100644 --- a/build_tools/autogen_ltc_backend.py +++ b/build_tools/autogen_ltc_backend.py @@ -86,7 +86,7 @@ def lowering_function(self, schema: LazyIrSchema): {emplace_arguments_str} {emplace_kwarguments} torch::lazy::TorchMlirOpVector {schema.aten_name}_out = torch::lazy::LowerTorchMlirBuiltin(function, op().op, shapes(), arguments, kwarguments); - CHECK_EQ({schema.aten_name}_out.size(), {len(schema.returns)}); + TORCH_CHECK_EQ({schema.aten_name}_out.size(), {len(schema.returns)}); return {schema.aten_name}_out; }} diff --git a/python/torch_mlir/csrc/base_lazy_backend/mlir_lowering_context.cpp b/python/torch_mlir/csrc/base_lazy_backend/mlir_lowering_context.cpp index a2d58145aa20..fdef6271965a 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/mlir_lowering_context.cpp +++ b/python/torch_mlir/csrc/base_lazy_backend/mlir_lowering_context.cpp @@ -64,7 +64,7 @@ void TorchMlirLoweringContext::Lower(const Node* node) { dynamic_cast(node)) { TorchMlirOpVector ops = torch_mlir_node->Lower(function_, this); CHECK(!ops.empty()) << "Failed to lower: " << *node; - CHECK_EQ(node->num_outputs(), ops.size()); + TORCH_CHECK_EQ(node->num_outputs(), ops.size()); for (size_t i = 0; i < ops.size(); ++i) { AssignOutputOp(torch::lazy::Output(node, i), ops[i]); } diff --git a/python/torch_mlir/csrc/base_lazy_backend/mlir_node_lowering.cpp b/python/torch_mlir/csrc/base_lazy_backend/mlir_node_lowering.cpp index a18a20e789db..e3d4fab862f4 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/mlir_node_lowering.cpp +++ b/python/torch_mlir/csrc/base_lazy_backend/mlir_node_lowering.cpp @@ -205,7 +205,7 @@ GenerateClone(torch::jit::Value* val, TorchMlirFunction function) { // Type of cloned value should be identical to the original one. TorchMlirOpVector cloned = LowerBuiltin(at::aten::clone, {val->type()}, function, clone_arguments); - CHECK_EQ(cloned.size(), 1); + TORCH_CHECK_EQ(cloned.size(), 1); return cloned.front(); } @@ -235,7 +235,7 @@ torch::jit::Value* GenerateSlice( c10::ArrayRef( compute_shape_slice(base->type(), dim, start, end, step)), function, arguments); - CHECK_EQ(selected.size(), 1); + TORCH_CHECK_EQ(selected.size(), 1); return selected.front(); } diff --git a/python/torch_mlir/csrc/base_lazy_backend/ops/to_copy.h b/python/torch_mlir/csrc/base_lazy_backend/ops/to_copy.h index 311d97f90aa6..c6b75baaf8f3 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/ops/to_copy.h +++ b/python/torch_mlir/csrc/base_lazy_backend/ops/to_copy.h @@ -84,7 +84,7 @@ class ToCopy : public torch::lazy::TorchMlirNode { kwarguments.emplace_back("non_blocking", non_blocking); kwarguments.emplace_back("memory_format", memory_format); torch::lazy::TorchMlirOpVector _to_copy_out = torch::lazy::LowerTorchMlirBuiltin(function, op().op, shapes(), arguments, kwarguments); - CHECK_EQ(_to_copy_out.size(), 1); + TORCH_CHECK_EQ(_to_copy_out.size(), 1); return _to_copy_out; From 65f94dd5e37c6b1451e3251111b87a5a3322bd4a Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Mon, 25 Jul 2022 20:53:38 -0400 Subject: [PATCH 02/13] Check value of TORCH_MLIR_USE_INSTALLED_PYTORCH during LTC build --- python/CMakeLists.txt | 43 +++++++++++-------- .../csrc/base_lazy_backend/CMakeLists.txt | 8 +++- .../reference_lazy_backend/CMakeLists.txt | 9 +++- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 86e222beb85d..d7e19d912a21 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -13,6 +13,30 @@ set(TORCH_MLIR_PYTHON_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/torch_mlir") # We vendor our own MLIR instance in the `torch_mlir` namespace. add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=torch_mlir.") +################################################################################ +# PyTorch +################################################################################ + +option(TORCH_MLIR_USE_INSTALLED_PYTORCH "Build from local PyTorch in environment" ON) + +if (NOT TORCH_MLIR_USE_INSTALLED_PYTORCH) + # Source builds + set(ENV{PYTORCH_REPO} ${PYTORCH_REPO}) + set(ENV{PYTORCH_BRANCH} ${PYTORCH_BRANCH}) + set(ENV{MACOSX_DEPLOYMENT_TARGET} ${MACOSX_DEPLOYMENT_TARGET}) + set(ENV{CMAKE_OSX_ARCHITECTURES} ${CMAKE_OSX_ARCHITECTURES}) + set(ENV{CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER_LAUNCHER}) + set(ENV{CMAKE_CXX_COMPILER_LAUNCHER} ${CMAKE_CXX_COMPILER_LAUNCHER}) + execute_process( + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../build_tools/build_libtorch.sh + RESULT_VARIABLE _result + ) + if(_result) + message(FATAL_ERROR "Failed to run `build_libtorch.sh`") + endif() + set(TORCH_INSTALL_PREFIX "libtorch") +endif() + ################################################################################ # Sources ################################################################################ @@ -67,26 +91,7 @@ add_subdirectory(torch_mlir/csrc/reference_lazy_backend) # Optionally handle JIT IR importer. ################################################################################ -option(TORCH_MLIR_USE_INSTALLED_PYTORCH "Build from local PyTorch in environment" ON) - if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER) - if (NOT TORCH_MLIR_USE_INSTALLED_PYTORCH) - # Source builds - set(ENV{PYTORCH_REPO} ${PYTORCH_REPO}) - set(ENV{PYTORCH_BRANCH} ${PYTORCH_BRANCH}) - set(ENV{MACOSX_DEPLOYMENT_TARGET} ${MACOSX_DEPLOYMENT_TARGET}) - set(ENV{CMAKE_OSX_ARCHITECTURES} ${CMAKE_OSX_ARCHITECTURES}) - set(ENV{CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER_LAUNCHER}) - set(ENV{CMAKE_CXX_COMPILER_LAUNCHER} ${CMAKE_CXX_COMPILER_LAUNCHER}) - execute_process( - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../build_tools/build_libtorch.sh - RESULT_VARIABLE _result - ) - if(_result) - message(FATAL_ERROR "Failed to run `build_libtorch.sh`") - endif() - set(TORCH_INSTALL_PREFIX "libtorch") - endif() add_subdirectory(torch_mlir/dialects/torch/importer/jit_ir) add_subdirectory(torch_mlir_e2e_test) endif() diff --git a/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt index 4b0df62337a0..033edbc77563 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt @@ -5,10 +5,16 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/python/torch_mlir/cmake/modules") include(TorchMLIRPyTorch) + TorchMLIRProbeForPyTorchInstall() +if(TORCH_MLIR_USE_INSTALLED_PYTORCH) + TorchMLIRConfigurePyTorch() +else() + set(Torch_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../libtorch/share/cmake/Torch") +endif() + find_package(Torch 1.11 REQUIRED) -TorchMLIRConfigurePyTorch() set(TORCHGEN_DIR ${Torch_ROOT}/../../../torchgen) include_directories(BEFORE diff --git a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt index 8585aaf73c59..453e8009eeb1 100644 --- a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt @@ -4,10 +4,15 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/python/torch_mlir/cmake/modules") include(TorchMLIRPyTorch) + TorchMLIRProbeForPyTorchInstall() -find_package(Torch 1.11 REQUIRED) +if(TORCH_MLIR_USE_INSTALLED_PYTORCH) + TorchMLIRConfigurePyTorch() +else() + set(Torch_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../libtorch/share/cmake/Torch") +endif() -TorchMLIRConfigurePyTorch() +find_package(Torch 1.11 REQUIRED) ########################################################################### # Setup Python development From 94ecb23343a4d55453cefcb4b51cba78aba14ad1 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Wed, 27 Jul 2022 15:01:51 -0400 Subject: [PATCH 03/13] Update LTC XFAIL with NewZerosModule ops --- e2e_testing/torchscript/xfail_sets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/e2e_testing/torchscript/xfail_sets.py b/e2e_testing/torchscript/xfail_sets.py index 8856ee0afd77..b91d6de38f8f 100644 --- a/e2e_testing/torchscript/xfail_sets.py +++ b/e2e_testing/torchscript/xfail_sets.py @@ -288,6 +288,12 @@ "NewOnesModuleFloat3D_basic", "NewOnesModuleInt2D_basic", "NewOnesModuleInt3D_basic", + "NewZerosModuleDefaultDtype_basic", + "NewZerosModuleFalsePinMemory_basic", + "NewZerosModuleFloat2D_basic", + "NewZerosModuleFloat3D_basic", + "NewZerosModuleInt2D_basic", + "NewZerosModuleInt3D_basic", "OnesLikeModule_defaultDtype", "OnesLikeModule_falsePinMemory", "OnesLikeModule_float", From 5ca5f636ebbec7ae7f1552173c8b2596d7b9595f Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Wed, 27 Jul 2022 17:15:17 -0400 Subject: [PATCH 04/13] Explicitly blacklist _like ops --- build_tools/autogen_ltc_backend.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build_tools/autogen_ltc_backend.yaml b/build_tools/autogen_ltc_backend.yaml index ff6c7f5eeaba..50bf505f8915 100644 --- a/build_tools/autogen_ltc_backend.yaml +++ b/build_tools/autogen_ltc_backend.yaml @@ -14,6 +14,13 @@ blacklist: - size - where - copy_ +- empty_like +- ones_like +- full_like +- zeros_like +- rand_like +- randn_like +- randint_like # Disabled for consistency with TS backend - new_empty From f0006dcc07293d1401c8b25690d7e9aa05a9b430 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Wed, 27 Jul 2022 17:21:41 -0400 Subject: [PATCH 05/13] Automatically blacklist new_/_like ops --- build_tools/autogen_ltc_backend.py | 3 +++ build_tools/autogen_ltc_backend.yaml | 7 ------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/build_tools/autogen_ltc_backend.py b/build_tools/autogen_ltc_backend.py index bfe51c5ec1a7..ff31b95b411e 100644 --- a/build_tools/autogen_ltc_backend.py +++ b/build_tools/autogen_ltc_backend.py @@ -236,6 +236,9 @@ def get_opnames(ops): continue if base in supported or op in supported: continue + # Blacklist new_/_like ops since they are non-differentiable. + if any(o.startswith("new_") or o.endswith("_like") for o in (base, op)): + continue if func.has_composite_implicit_autograd_kernel: composite_implicit.add(op) diff --git a/build_tools/autogen_ltc_backend.yaml b/build_tools/autogen_ltc_backend.yaml index 50bf505f8915..ff6c7f5eeaba 100644 --- a/build_tools/autogen_ltc_backend.yaml +++ b/build_tools/autogen_ltc_backend.yaml @@ -14,13 +14,6 @@ blacklist: - size - where - copy_ -- empty_like -- ones_like -- full_like -- zeros_like -- rand_like -- randn_like -- randint_like # Disabled for consistency with TS backend - new_empty From d14c82c1712e60a1d750372fbec9305e6b5bf1c5 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Wed, 27 Jul 2022 19:06:38 -0400 Subject: [PATCH 06/13] Prune away unused Python dependencies from LTC --- python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt | 2 -- python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt | 2 -- 2 files changed, 4 deletions(-) diff --git a/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt index 033edbc77563..db34a8e1206a 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/base_lazy_backend/CMakeLists.txt @@ -82,8 +82,6 @@ target_link_libraries(torch_mlir_ltc_backend TorchMLIRAggregateCAPI TorchMLIRJITIRImporter ${TORCH_LIBRARIES} - ${Python3_LIBRARIES} - torch_python ) message(STATUS "TORCH_CXXFLAGS=${TORCH_CXXFLAGS} -Wno-pedantic") diff --git a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt index 453e8009eeb1..58b54ea40718 100644 --- a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt @@ -48,8 +48,6 @@ add_dependencies(reference_lazy_backend ) target_link_libraries(reference_lazy_backend ${TORCH_LIBRARIES} - ${Python3_LIBRARIES} - torch_python torch_mlir_ltc_backend ) From bbcff6096a7e4065bc622777aed560a521b7a670 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Thu, 28 Jul 2022 18:11:07 -0400 Subject: [PATCH 07/13] Add flag to disable LTC --- .github/workflows/buildAndTest.yml | 1 + CMakeLists.txt | 2 ++ python/CMakeLists.txt | 14 +++++++++----- .../csrc/reference_lazy_backend/CMakeLists.txt | 3 +-- .../torch/importer/jit_ir/csrc/CMakeLists.txt | 9 ++++++++- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.github/workflows/buildAndTest.yml b/.github/workflows/buildAndTest.yml index 38943aedb54b..913014f503ef 100644 --- a/.github/workflows/buildAndTest.yml +++ b/.github/workflows/buildAndTest.yml @@ -96,6 +96,7 @@ jobs: -DMLIR_ENABLE_BINDINGS_PYTHON=OFF \ -DTORCH_MLIR_ENABLE_MHLO=ON \ -DTORCH_MLIR_USE_INSTALLED_PYTORCH=${{ matrix.torch-binary }} \ + -DTORCH_MLIR_ENABLE_LTC=OFF \ -DPython3_EXECUTABLE=$(which python) \ . diff --git a/CMakeLists.txt b/CMakeLists.txt index 10432d1e9c8f..00340141c835 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,8 @@ if(TORCH_MLIR_ENABLE_MHLO) endif() endif() +option(TORCH_MLIR_ENABLE_LTC "Enables LTC backend" ON) + torch_mlir_add_llvm_external_project( torch-mlir-dialects TORCH_MLIR_DIALECTS diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index d7e19d912a21..4601d8e8cf61 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -84,8 +84,10 @@ declare_mlir_python_extension(TorchMLIRPythonExtensions.Main # Lazy Tensor Core ################################################################################ -add_subdirectory(torch_mlir/csrc/base_lazy_backend) -add_subdirectory(torch_mlir/csrc/reference_lazy_backend) +if(TORCH_MLIR_ENABLE_LTC) + add_subdirectory(torch_mlir/csrc/base_lazy_backend) + add_subdirectory(torch_mlir/csrc/reference_lazy_backend) +endif() ################################################################################ # Optionally handle JIT IR importer. @@ -159,8 +161,10 @@ endif() # TODO: Add after macOS builds are fixed #add_dependencies(TorchMLIRPythonModules torch_mlir_custom_op_example) -# Add Torch-MLIR LTC backend as dependency -add_dependencies(TorchMLIRPythonModules torch_mlir_ltc_backend) -add_dependencies(TorchMLIRPythonModules reference_lazy_backend) +if(TORCH_MLIR_ENABLE_LTC) + # Add Torch-MLIR LTC backend as dependency + add_dependencies(TorchMLIRPythonModules torch_mlir_ltc_backend) + add_dependencies(TorchMLIRPythonModules reference_lazy_backend) +endif() add_subdirectory(test) diff --git a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt index 58b54ea40718..a3290212b54e 100644 --- a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt @@ -38,11 +38,10 @@ link_directories("${TORCH_INSTALL_PREFIX}/lib") link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) add_link_options(-Wl,-rpath,$ORIGIN/lib) -set(REFERENCE_LAZY_BACKEND_CSRC +add_library(reference_lazy_backend SHARED backend_impl.cpp reference_lazy_backend_pybind.cpp ) -add_library(reference_lazy_backend SHARED ${REFERENCE_LAZY_BACKEND_CSRC}) add_dependencies(reference_lazy_backend torch_mlir_ltc_backend ) diff --git a/python/torch_mlir/dialects/torch/importer/jit_ir/csrc/CMakeLists.txt b/python/torch_mlir/dialects/torch/importer/jit_ir/csrc/CMakeLists.txt index 3dfaf67bee51..043975990197 100644 --- a/python/torch_mlir/dialects/torch/importer/jit_ir/csrc/CMakeLists.txt +++ b/python/torch_mlir/dialects/torch/importer/jit_ir/csrc/CMakeLists.txt @@ -10,7 +10,14 @@ include_directories(BEFORE ) link_directories("${TORCH_INSTALL_PREFIX}/lib") -add_library(TorchMLIRJITIRImporter SHARED +# Hack! Currently out-of-tree build fails when this is set to SHARED, so we have this toggle +if(TORCH_MLIR_ENABLE_LTC) + set(LIBRARY_TYPE "SHARED") +else() + set(LIBRARY_TYPE "MODULE") +endif() + +add_library(TorchMLIRJITIRImporter ${LIBRARY_TYPE} class_annotator.cpp class_annotator_pybind.cpp get_registered_ops.cpp From cdeb50dac45d966383db1c2aa990139098583da3 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Thu, 28 Jul 2022 19:45:50 -0400 Subject: [PATCH 08/13] Autogen dummy _REFERENCE_LAZY_BACKEND library when LTC is disabled --- python/CMakeLists.txt | 3 +- .../reference_lazy_backend/CMakeLists.txt | 77 +++++++++++-------- .../reference_lazy_backend/gen_dummy_lib.py | 23 ++++++ 3 files changed, 69 insertions(+), 34 deletions(-) create mode 100755 python/torch_mlir/csrc/reference_lazy_backend/gen_dummy_lib.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 4601d8e8cf61..304f4a11cdd5 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -86,8 +86,9 @@ declare_mlir_python_extension(TorchMLIRPythonExtensions.Main if(TORCH_MLIR_ENABLE_LTC) add_subdirectory(torch_mlir/csrc/base_lazy_backend) - add_subdirectory(torch_mlir/csrc/reference_lazy_backend) endif() +# Reference backend has a separate check for TORCH_MLIR_ENABLE_LTC. +add_subdirectory(torch_mlir/csrc/reference_lazy_backend) ################################################################################ # Optionally handle JIT IR importer. diff --git a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt index a3290212b54e..7340890d90fc 100644 --- a/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt +++ b/python/torch_mlir/csrc/reference_lazy_backend/CMakeLists.txt @@ -26,36 +26,47 @@ mlir_configure_python_dev_packages() # Library definition ########################################################################### -include_directories(BEFORE - ${TORCH_INCLUDE_DIRS} - ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_BINARY_DIR} - ${Python3_INCLUDE_DIRS} - ${PYTHON_H_DIR} - ${PROJECT_SOURCE_DIR}/python - ) -link_directories("${TORCH_INSTALL_PREFIX}/lib") -link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) -add_link_options(-Wl,-rpath,$ORIGIN/lib) - -add_library(reference_lazy_backend SHARED - backend_impl.cpp - reference_lazy_backend_pybind.cpp - ) -add_dependencies(reference_lazy_backend - torch_mlir_ltc_backend - ) -target_link_libraries(reference_lazy_backend - ${TORCH_LIBRARIES} - torch_mlir_ltc_backend - ) - -message(STATUS "TORCH_CXXFLAGS=${TORCH_CXXFLAGS} -Wno-pedantic") -set_target_properties(reference_lazy_backend PROPERTIES - LIBRARY_OUTPUT_DIRECTORY "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir/reference_lazy_backend" - OUTPUT_NAME _REFERENCE_LAZY_BACKEND - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" - CXX_VISIBILITY_PRESET "hidden" - COMPILE_FLAGS "${TORCH_CXXFLAGS} -Wno-pedantic" - ) +set(LIBRARY_OUTPUT_PATH "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir/reference_lazy_backend") +set(OUTPUT_NAME "_REFERENCE_LAZY_BACKEND") + +if(TORCH_MLIR_ENABLE_LTC) + include_directories(BEFORE + ${TORCH_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${Python3_INCLUDE_DIRS} + ${PYTHON_H_DIR} + ${PROJECT_SOURCE_DIR}/python + ) + link_directories("${TORCH_INSTALL_PREFIX}/lib") + link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) + add_link_options(-Wl,-rpath,$ORIGIN/lib) + + add_library(reference_lazy_backend SHARED + backend_impl.cpp + reference_lazy_backend_pybind.cpp + ) + add_dependencies(reference_lazy_backend + torch_mlir_ltc_backend + ) + target_link_libraries(reference_lazy_backend + ${TORCH_LIBRARIES} + torch_mlir_ltc_backend + ) + + message(STATUS "TORCH_CXXFLAGS=${TORCH_CXXFLAGS} -Wno-pedantic") + set_target_properties(reference_lazy_backend PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} + OUTPUT_NAME ${OUTPUT_NAME} + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" + CXX_VISIBILITY_PRESET "hidden" + COMPILE_FLAGS "${TORCH_CXXFLAGS} -Wno-pedantic" + ) +else() + # To avoid import errors when LTC is disabled (and a bunch of checks + # associated with that), we will generate a dummy placeholder library. + execute_process( + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gen_dummy_lib.py ${LIBRARY_OUTPUT_PATH} ${OUTPUT_NAME} + ) +endif() diff --git a/python/torch_mlir/csrc/reference_lazy_backend/gen_dummy_lib.py b/python/torch_mlir/csrc/reference_lazy_backend/gen_dummy_lib.py new file mode 100755 index 000000000000..34c9e61907b6 --- /dev/null +++ b/python/torch_mlir/csrc/reference_lazy_backend/gen_dummy_lib.py @@ -0,0 +1,23 @@ +# When LTC is disabled in Torch-MLIR build, we will generate a dummy module to +# ensure that no import errors occur. + +import sys +import os + +if __name__ == '__main__': + path = sys.argv[1] # dummy script path + file_name = sys.argv[2] # dummy script + + contents = ''' +# This file was automatically generated due to LTC being disabled in build. + +class LazyTensorCoreTestConfig: + def __init__(self): + assert False, "LTC is not enabled. Check the value of `TORCH_MLIR_ENABLE_LTC`" + ''' + + if not os.path.exists(path): + os.makedirs(path) + + with open(os.path.join(path, file_name + '.py'), 'w') as file: + file.write(contents) From 97b63941fd7761b345bf5897c5dfae9388c7d9c1 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Fri, 29 Jul 2022 13:52:44 -0400 Subject: [PATCH 09/13] Implement compute_shape_var --- .../torch_mlir/csrc/base_lazy_backend/shape_inference.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/torch_mlir/csrc/base_lazy_backend/shape_inference.cpp b/python/torch_mlir/csrc/base_lazy_backend/shape_inference.cpp index 2ad1c962d165..48004d9d34eb 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/shape_inference.cpp +++ b/python/torch_mlir/csrc/base_lazy_backend/shape_inference.cpp @@ -29,5 +29,12 @@ compute_shape_mul(const at::Tensor& self, const at::Scalar& other) { return {Shape(self.scalar_type(), self.sizes().vec())}; } +std::vector compute_shape_var( + const at::Tensor& self, at::OptionalIntArrayRef dim, + c10::optional correction, bool keepdim) { + // Result of variance is scalar tensor. + return {Shape(self.scalar_type(), {})}; +} + } // namespace lazy } // namespace torch From 46653a050178f29306de13c0db326694159e4e71 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Fri, 29 Jul 2022 13:56:14 -0400 Subject: [PATCH 10/13] Removed Var tests from XFAIL Set --- e2e_testing/torchscript/xfail_sets.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/e2e_testing/torchscript/xfail_sets.py b/e2e_testing/torchscript/xfail_sets.py index b91d6de38f8f..33966e531de1 100644 --- a/e2e_testing/torchscript/xfail_sets.py +++ b/e2e_testing/torchscript/xfail_sets.py @@ -323,15 +323,5 @@ "UniformModule_basic", "UniformStaticModule_basic", "UnsafeViewCollapseDynamicWithAtenSizeIntModule_basic", - "VarBiasedModule_basic", - "VarDimAllDimReduceModule_basic", - "VarDimBiasedModule_basic", - "VarDimKeepDimFalseModule_basic", - "VarDimModule_basic", - "VarDimMultiDimModule_basic", - "VarDimNegativeModule_basic", - "VarDimSingleDimModule_basic", - "VarDimUnbiasedModule_basic", - "VarUnbiasedModule_basic", "ViewCollapseDynamicWithAtenSizeIntModule_basic", } From 4401da9006c3f4984dfcc061dd7b4961cd49d8c6 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Fri, 29 Jul 2022 14:03:35 -0400 Subject: [PATCH 11/13] XFAIL tests using _local_scalar_dense or index.Tensor --- e2e_testing/torchscript/xfail_sets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/e2e_testing/torchscript/xfail_sets.py b/e2e_testing/torchscript/xfail_sets.py index 33966e531de1..902f89dc1e6d 100644 --- a/e2e_testing/torchscript/xfail_sets.py +++ b/e2e_testing/torchscript/xfail_sets.py @@ -227,6 +227,7 @@ "FullLikeModuleInt3D_basic", "GeFloatIntModule_basic", "GeFloatModule_basic", + "GeIntModule_basic", "GtFloatIntModule_basic", "GtIntModule_basic", "HBC_basic", @@ -266,6 +267,11 @@ "IndexPutImpl3DFloatNonAccumulateModule_basic", "IndexTensorModule3dInput_basic", "IndexTensorModule_basic", + "IndexTensorMultiInputContiguousCenter_basic", + "IndexTensorMultiInputNonContiguous_basic", + "IndexTensorMultiInputOneDim_basic", + "IndexTensorMultiInputThreeIndexers_basic", + "IndexTensorMultiInput_basic", "IndexTensorSelectDimModule_basic", "Matmul_dot", "Matmul_matvec", From a9e45f57b58f740b7117a2a76b2252a36c316b28 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Fri, 29 Jul 2022 16:23:18 -0400 Subject: [PATCH 12/13] Add StdDim tests to XFAIL set --- e2e_testing/torchscript/xfail_sets.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/e2e_testing/torchscript/xfail_sets.py b/e2e_testing/torchscript/xfail_sets.py index 902f89dc1e6d..7e0e43918795 100644 --- a/e2e_testing/torchscript/xfail_sets.py +++ b/e2e_testing/torchscript/xfail_sets.py @@ -314,6 +314,9 @@ "SliceStartEqEndModule_basic", "SqrtIntModule_basic", "StdBiasedModule_basic", + "StdDimBiasedModule_basic", + "StdDimKeepDimFalseModule_basic", + "StdDimKeepDimTrueModule_basic", "StdUnbiasedModule_basic", "SubFloatModule_basic", "SubIntModule_basic", From 27d20b918b47376599b7c36e5edcaf130cec85f2 Mon Sep 17 00:00:00 2001 From: Henry Tu Date: Sat, 30 Jul 2022 01:48:12 -0400 Subject: [PATCH 13/13] Autogen aten::cat --- build_tools/autogen_ltc_backend.yaml | 1 - .../mlir_native_functions.cpp | 19 ------------------- 2 files changed, 20 deletions(-) diff --git a/build_tools/autogen_ltc_backend.yaml b/build_tools/autogen_ltc_backend.yaml index ff6c7f5eeaba..505701eaa0dc 100644 --- a/build_tools/autogen_ltc_backend.yaml +++ b/build_tools/autogen_ltc_backend.yaml @@ -43,7 +43,6 @@ supported: # - bernoulli # - bernoulli_ - _to_copy -- cat - clone - empty.memory_format - empty_strided diff --git a/python/torch_mlir/csrc/base_lazy_backend/mlir_native_functions.cpp b/python/torch_mlir/csrc/base_lazy_backend/mlir_native_functions.cpp index eb793615b86d..e197af3e569d 100644 --- a/python/torch_mlir/csrc/base_lazy_backend/mlir_native_functions.cpp +++ b/python/torch_mlir/csrc/base_lazy_backend/mlir_native_functions.cpp @@ -154,25 +154,6 @@ void copy_(torch::lazy::LazyTensorPtr& input, torch::lazy::LazyTensorPtr& src) { // // return self; // } -at::Tensor LazyNativeFunctions::cat(at::TensorList tensors, int64_t dim) { - TORCH_LAZY_FN_COUNTER("lazy::"); - auto lazy_tensors = torch::lazy::GetLtcTensors(tensors); - std::vector values; - values.reserve(lazy_tensors.size()); - for (auto& tensor : lazy_tensors) { - values.emplace_back(tensor->GetIrValue()); - } - - auto shapes = torch::lazy::compute_shape_cat(tensors, dim); - UNIMPLEMENTED_FUNCTION_ERROR(); - // auto node = - // torch::lazy::MakeNode(values, dim, std::move(shapes)); - // auto result = torch::lazy::CreateAtenFromLtcTensor( - // torch::lazy::LazyTensor::Create(torch::lazy::Value(node, 0), - // lazy_tensors[0]->GetDevice())); - // return result; -} - // clone is special in LT because we make it a no-op. // This should be safe to do, because every operator in the LT is functional. at::Tensor LazyNativeFunctions::clone(