Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Adding TRT options/task #435

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(StablehloToExecutable)
add_subdirectory(TensorRTToExecutable)
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ optionsCreateFromArgs(mlir::MLIRContext *context,
[&errMsg](const llvm::StringError &err) { errMsg = err.getMessage(); });

if (errMsg)
return getInternalErrorStatus("failed to initialize options: %s",
errMsg->c_str());
return getInternalErrorStatus("failed to initialize options: {0}", *errMsg);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(_TABLEGEN_ARGS )
set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name TensorRTToExecutable ${_TABLEGEN_ARGS})
add_public_tablegen_target(MLIRTensorRTTensorRTToExecutableIncGen)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===- Passes.h ----------------------------------------------===//
//
// SPDX-FileCopyrightText: Copyright 2025 NVIDIA CORPORATION & AFFILIATES.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// Declarations for opt tool pipeline command-line registration for pipelines
/// related to "tensorrt-to-executable".
///
//===----------------------------------------------------------------------===//
#ifndef MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES
#define MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES

#include <memory>
#include <mlir/Pass/Pass.h>

namespace mlirtrt::compiler {

//===----------------------------------------------------------------------===//
// Add Tablegen'd pass declarations and registration methods.
//===----------------------------------------------------------------------===//
#define GEN_PASS_DECL
#define GEN_PASS_REGISTRATION
#include "mlir-tensorrt/Compiler/TensorRTToExecutable/Passes.h.inc"

//===----------------------------------------------------------------------===//
// Pipeline Registrations
//===----------------------------------------------------------------------===//

/// Register the TensorRT clustering and compilation pipelines.
void registerTensorRTToExecutablePipelines();

} // namespace mlirtrt::compiler

#endif // MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===- Passes.td ----------------------------------------------------------===//
//
// SPDX-FileCopyrightText: Copyright 2025 NVIDIA CORPORATION & AFFILIATES.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES
#define MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES

include "mlir/Pass/PassBase.td"

//===----------------------------------------------------------------------===//
// OutlineTensorRTOpPass
//===----------------------------------------------------------------------===//

def OutlineTensorRTOpPass : Pass<"outline-tensorrt-op",
"::mlir::ModuleOp"> {
let summary = "Outline all tensorrt ops into a tensorrt module";

let dependentDialects = [
"::mlir::plan::PlanDialect"
];
}

#endif // MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE_PASSES
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//===- TensorRTToExecutable.h -----------------------------------*- C++ -*-===//
//
// SPDX-FileCopyrightText: Copyright 2025 NVIDIA CORPORATION & AFFILIATES.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE
#define MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE

// TODO (pranavm): MLIR_TRT_TARGET_TENSORRT is only needed because we pull in
// the TranslateToTensorRT.h header. If we move the translation options, we
// won't need it.
#ifdef MLIR_TRT_TARGET_TENSORRT
#include "mlir-tensorrt-dialect/Target/TranslateToTensorRT.h"

#include "mlir-tensorrt-dialect/Utils/OptionsBundle.h"
#include "mlir-tensorrt/Compiler/Client.h"

namespace mlirtrt::compiler {

//===----------------------------------------------------------------------===//
// TensorRTToExecutableOptions
//===----------------------------------------------------------------------===//

class TensorRTToExecutableTask;

// TODO (pranavm): Figure out a better way to reuse TRT translation options -
// maybe move to options providers?
struct TensorRTOptions : public OptionsProvider<TensorRTOptions> {
public:
using OptionsProvider::OptionsProvider;
mlir::tensorrt::TensorRTTranslationOptions options;

TensorRTOptions(mlir::OptionsContext &ctx) : OptionsProvider(ctx) {
options.addToOptions(ctx);
}
};

struct TensorRTToExecutableOptions
: public mlir::OptionsBundle<DeviceOptions, DebugOptions, ExecutorOptions,
TensorRTOptions> {
// Default initialization does not require any extensions.
TensorRTToExecutableOptions() = default;

TensorRTToExecutableOptions(TaskExtensionRegistry extensions);

Option<std::string> entrypoint{this, "entrypoint", llvm::cl::init("main"),
llvm::cl::desc("entrypoint function name")};
};

//===----------------------------------------------------------------------===//
// TensorRTToExecutableTask
//===----------------------------------------------------------------------===//

class TensorRTToExecutableTask
: public CompilationTask<TensorRTToExecutableTask,
TensorRTToExecutableOptions> {
public:
TensorRTToExecutableTask(mlir::MLIRContext *ctx,
const TensorRTToExecutableOptions &options);

/// Build the clustering pipeline that occurs on TensorRT Ops.
static void
buildTensorRTClusteringPipeline(mlir::OpPassManager &pm,
const TensorRTToExecutableOptions &options);

/// Build the compilation pipeline that runs after clustering.
static void
buildPostClusteringPipeline(mlir::OpPassManager &pm,
const TensorRTToExecutableOptions &options);

static void populatePassManager(mlir::PassManager &pm,
const TensorRTToExecutableOptions &options);
};

/// Register the task/options with the client's registry.
void registerTensorRTToExecutableTask();

} // namespace mlirtrt::compiler

MLIR_DECLARE_EXPLICIT_TYPE_ID(mlirtrt::compiler::TensorRTToExecutableTask)

#endif
#endif // MLIR_TENSORRT_COMPILER_TENSORRTTOEXECUTABLE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifdef MLIR_TRT_ENABLE_HLO
#include "mlir-tensorrt/Compiler/StablehloToExecutable/Passes.h"
#include "mlir-tensorrt/Compiler/StablehloToExecutable/StablehloToExecutable.h"
#include "mlir-tensorrt/Compiler/TensorRTToExecutable/Passes.h"
#include "mlir-tensorrt/Dialect/Plan/Transforms/Passes.h"
#include "mlir-tensorrt/Dialect/StableHloExt/Transforms/Passes.h"
#include "mlir-tensorrt/Pipelines/StableHloInputPipelines.h"
Expand Down Expand Up @@ -63,6 +64,10 @@ inline void registerAllMlirTensorRtPasses() {
plan::registerPlanDialectPipelines();
#endif // MLIR_TRT_ENABLE_HLO

#ifdef MLIR_TRT_TARGET_TENSORRT
mlirtrt::compiler::registerTensorRTToExecutablePipelines();
#endif // MLIR_TRT_TARGET_TENSORRT

#ifdef MLIR_TRT_ENABLE_EXECUTOR
registerConvertCUDAToExecutorPass();
bufferization::registerBufferizationPasses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ add_mlir_tensorrt_public_c_api_library(MLIRTensorRTCAPIRegisterAllDialects
MLIRTensorRTRegistration
MLIRCAPIIR
MLIRTensorRTCompilerStableHloToExecutable
MLIRTensorRTCompilerTensorRTToExecutable
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "mlir-tensorrt-c/Compiler/Registration/RegisterAllDialects.h"
#include "mlir-tensorrt/Compiler/StablehloToExecutable/StablehloToExecutable.h"
#include "mlir-tensorrt/Compiler/TensorRTToExecutable/TensorRTToExecutable.h"
#include "mlir-tensorrt/Registration/RegisterMlirTensorRtDialects.h"
#include "mlir-tensorrt/Registration/RegisterMlirTensorRtPasses.h"
#include "mlir/CAPI/IR.h"
Expand All @@ -38,4 +39,5 @@ void mtrtCompilerRegisterPasses() {

void mtrtCompilerRegisterTasks() {
mlirtrt::compiler::registerStableHloToExecutableTask();
mlirtrt::compiler::registerTensorRTToExecutableTask();
}
3 changes: 2 additions & 1 deletion mlir-tensorrt/compiler/lib/Compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ add_mlir_tensorrt_library(MLIRTensorRTCompilerClient
MLIRTensorRTSupportDeviceInfo
)

add_subdirectory(StablehloToExecutable)
add_subdirectory(StablehloToExecutable)
add_subdirectory(TensorRTToExecutable)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_mlir_tensorrt_library(MLIRTensorRTCompilerTensorRTToExecutable
TensorRTToExecutable.cpp
Passes.cpp

PARTIAL_SOURCES_INTENDED

DEPENDS
MLIRTensorRTTensorRTToExecutableIncGen

LINK_LIBS PUBLIC
MLIRIR
MLIRTensorRTRegistration
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch-all library has been removed in a commit which hasn't been sync'd up yet. Just remove this and add in dependent libraries explicitly.

MLIRTensorRTTargetLua
MLIRTensorRTOptionUtils
MLIRTensorRTTargetTensorRT
MLIRTensorRTCompilerClient
)
Loading