AMDMIGraphX icon indicating copy to clipboard operation
AMDMIGraphX copied to clipboard

Convert MIGX IR to Linalg

Open causten opened this issue 1 year ago • 1 comments

Now that the MIGX IR can be extracted in to files PR (https://github.com/ROCm/AMDMIGraphX/pull/3550) , the next step is to convert the IR to Linalg. The rocMLIR team has a tool that should be able to do that already. Use the tool and generate linalg for each model's winning MLIR config

causten avatar Nov 19 '24 21:11 causten

MLIR IR to Linalg Conversion

This guide provides step-by-step instructions for converting MLIR IR to Linalg using RocMLIR and LLVM.

# Step 1: Clone RocMLIR Repository
git clone https://github.com/ROCm/rocMLIR.git
cd rocMLIR

# Step 2: Install `ninja-build`
apt install ninja-build

# Step 3: Build RocMLIR
cmake -G Ninja .. \
  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
  -DCMAKE_C_COMPILER=/opt/rocm/llvm/bin/clang \
  -DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++
ninja check-rocmlir

# Step 4: Clone LLVM Repository
RUN echo 'install llvm ${LLVM_VERSION}' && \
    wget --no-verbose https://apt.llvm.org/llvm.sh && \
    chmod +x llvm.sh && \
    ./llvm.sh ${LLVM_VERSION} && \
    apt-get update && \
    apt-get install -y clang-${LLVM_VERSION} clang-format-${LLVM_VERSION} clang-tidy-${LLVM_VERSION} lld-${LLVM_VERSION} && \
    ln -s /usr/bin/clang-${LLVM_VERSION} /usr/bin/clang && \
    ln -s /usr/bin/clang++-${LLVM_VERSION} /usr/bin/clang++ && \
    ln -s /usr/bin/clang-tidy-${LLVM_VERSION} /usr/bin/clang-tidy && \
    ln -s /usr/bin/clang-tidy-diff-${LLVM_VERSION}.py /usr/bin/clang-tidy-diff && \
    ln -s /usr/bin/clang-format-${LLVM_VERSION} /usr/bin/clang-format && \
    ln -s /usr/bin/git-clang-format-${LLVM_VERSION} /usr/bin/git-clang-format && \
    ln -s /usr/bin/clang-format-diff-${LLVM_VERSION} /usr/bin/clang-format-diff && \
    ln -s /usr/bin/lld-${LLVM_VERSION} /usr/bin/lld && \
    ln -s /usr/bin/lldb-${LLVM_VERSION} /usr/bin/lldb && \
    ln -s /usr/bin/ld.lld-${LLVM_VERSION} /usr/bin/ld.lld && \
    ln -s /usr/bin/llvm-profdata-${LLVM_VERSION} /usr/bin/llvm-profdata && \
    ln -s /usr/bin/llvm-cov-${LLVM_VERSION} /usr/bin/llvm-cov && \
    ln -s /usr/bin/llvm-symbolizer-${LLVM_VERSION} /usr/bin/llvm-symbolizer && \
    ln -s /usr/bin/llvm-cxxfilt-${LLVM_VERSION} /usr/bin/llvm-cxxfilt && \
    clang --version

# Step 5: Modify `TosaToLinalgPass.cpp`
# Navigate to `external/llvm-project/mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp`
# and delete the following line:
# validationOptions.profile = tosa::TosaProfileEnum::BaseInference;

# Step 6: Build `mlir-opt` from Source
git clone https://github.com/llvm/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build

cmake -G Ninja ../llvm \
  -DLLVM_ENABLE_PROJECTS=mlir \
  -DLLVM_BUILD_EXAMPLES=ON \
  -DLLVM_TARGETS_TO_BUILD="Native;NVPTX;AMDGPU" \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_ENABLE_ASSERTIONS=ON

cmake --build . --target check-mlir

# Step 7: Run the Conversion Script with .mlir file
./build/bin/rocmlir-driver \
  --kernel-pipeline=migraphx \
  bert_base_cased_1.onnx/2.11.0.2f9757/fill1_input_ids_input-dim_\@input_ids_1_32/fp32/mlir/mlir_dot_add_1x32x768_1x768x2304.mlir | \
  ./build/bin/rocmlir-opt \
  --rocmlir-custom-tosa-to-linalg | \
  ./external/llvm-project/build/bin/mlir-opt \
  --tosa-to-linalg-pipeline \
  --tosa-to-tensor \
  --tosa-to-scf \
  --tosa-to-arith \
  --linalg-fuse-elementwise-ops \
  --linalg-fold-unit-extent-dims \
  --canonicalize \
  --convert-tensor-to-linalg \
  --cse

richagadgil avatar Nov 23 '24 02:11 richagadgil

https://github.com/ROCm/FusionZoo/blob/main/src/linalg_gen.sh

richagadgil avatar Apr 24 '25 02:04 richagadgil