Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef __CIRCLE_MLIR_DIALECT_CIRCLE_DIALECT_H__
#define __CIRCLE_MLIR_DIALECT_CIRCLE_DIALECT_H__

#include <mlir/Bytecode/BytecodeOpInterface.h> // from @llvm-project
#include <mlir/Dialect/Traits.h> // from @llvm-project
#include <mlir/IR/Dialect.h> // from @llvm-project
#include <mlir/IR/DialectImplementation.h> // from @llvm-project
Expand All @@ -46,6 +47,7 @@ class ControlType : public Type::TypeBase<ControlType, Type, TypeStorage>
{
public:
using Base::Base;
static constexpr StringLiteral name = "cir.control";
};

#include "mlir/CircleOpInterface.h.inc"
Expand Down
1 change: 0 additions & 1 deletion circle-mlir/circle-mlir/lib/dialect/mlir/CircleOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef CIRCLE_OPS
#define CIRCLE_OPS

include "mlir/IR/FunctionInterfaces.td"
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
Expand Down
2 changes: 1 addition & 1 deletion circle-mlir/circle-mlir/lib/dialect/src/NameUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ std::string GetNameFromLoc(Location loc)
// in functions where the op's name is first.
auto name = name_loc.getName().strref().split('@').first;
// Skip if the name is for op type.
if (!name.endswith(":"))
if (!name.ends_with(":"))
{
loc_names.push_back(name);
if (!name.empty())
Expand Down
12 changes: 5 additions & 7 deletions circle-mlir/circle-mlir/lib/import/src/CircleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ inline constexpr unsigned int flatbuffer_size_max = 2147483648;
#include <llvm/Support/FormatVariadic.h>
#include <mlir/IR/Matchers.h> // m_Constant
#include <mlir/Dialect/Func/IR/FuncOps.h>
#include <mlir/Dialect/Quant/QuantOps.h>
#include <mlir/Dialect/Quant/QuantTypes.h>
#include <mlir/Dialect/Quant/IR/QuantTypes.h>

#include <circle_schema/schema_generated.h>

Expand Down Expand Up @@ -226,8 +225,8 @@ template <typename T> llvm::SmallVector<mlir::APInt> ReadAsHostEndian(llvm::Arra
const char *data_ptr = reinterpret_cast<const char *>(bytes.data());
for (size_t i = 0; i < elem_count; i++)
{
T val = llvm::support::endian::readNext<T, llvm::support::endianness::native,
llvm::support::unaligned>(data_ptr);
T val = llvm::support::endian::readNext<T, llvm::endianness::native, llvm::support::unaligned>(
data_ptr);
ret.push_back(mlir::APInt(sizeof(T) * 8, val));
}
return ret;
Expand All @@ -254,9 +253,8 @@ std::optional<mlir::ElementsAttr> ConvertFloatBuffer(mlir::RankedTensorType shap

for (int i = 0; i < elem_count; i++)
{
uint32_t bit_repr =
llvm::support::endian::readNext<uint32_t, llvm::support::endianness::native,
llvm::support::unaligned>(data);
uint32_t bit_repr = llvm::support::endian::readNext<uint32_t, llvm::endianness::native,
llvm::support::unaligned>(data);
values.push_back(absl::bit_cast<float>(bit_repr));
}
auto num = shaped_type.getNumElements();
Expand Down
2 changes: 1 addition & 1 deletion circle-mlir/circle-mlir/lib/pass/src/RewriteCirclePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void RewriteCirclePass::applyActivationFusion()

// TODO add more patterns

(void)applyPatternsAndFoldGreedily(func, std::move(patterns));
(void)applyPatternsGreedily(func, std::move(patterns));
}

std::unique_ptr<mlir::Pass> createRewriteCirclePass(void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct ConvertMirrorPadPad32 : public OpRewritePattern<MirrorPadOp>
auto intattr = DenseIntElementsAttr::get(si32stype, values);
mlir::Value shape32 = rewriter.create<ConstOp>(opLoc, intattr);

auto pad_mutable = mirrorpad_op.getPadMutable();
auto &pad_mutable = mirrorpad_op.getPadMutable();
pad_mutable.assign(shape32);

return mlir::success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct ConvertReshapeShape32 : public OpRewritePattern<ReshapeOp>
mlir::Value shape32 =
rewriter.create<ConstOp>(opLoc, DenseIntElementsAttr::get(si32stype, values));

auto shape_mutable = reshape_op.getShapeMutable();
auto &shape_mutable = reshape_op.getShapeMutable();
shape_mutable.assign(shape32);

return mlir::success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct ConvertResizeNearestSize32 : public OpRewritePattern<ResizeNearestNeighbo
mlir::Value size32 =
rewriter.create<ConstOp>(opLoc, DenseIntElementsAttr::get(si32stype, values));

auto resize_mutable = resize_op.getSizeMutable();
auto &resize_mutable = resize_op.getSizeMutable();
resize_mutable.assign(size32);

return mlir::success();
Expand Down
39 changes: 20 additions & 19 deletions circle-mlir/circle-mlir/lib/tools/converter-gen/converter_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ llvm::cl::opt<ActionType> action(

// Returns the associated option name for the given op definition.
static inline std::string GetOperatorOptionName(const Record &def) {
assert(def.getName().startswith("CIR_") && "unexpected op prefix");
assert(def.getName().endswith("Op") && "unexpected op suffix");
assert(def.getName().starts_with("CIR_") && "unexpected op prefix");
assert(def.getName().ends_with("Op") && "unexpected op suffix");

auto *custom_option = dyn_cast<StringInit>(def.getValueInit("customOption"));
std::ostringstream oss;
Expand All @@ -78,8 +78,8 @@ static inline std::string GetOperatorOptionName(const Record &def) {

// Returns the builder function name for the given op definition.
static inline std::string GetOperatorBuilderName(StringRef op_name) {
assert(op_name.startswith("CIR_") && "unexpected op prefix");
assert(op_name.endswith("Op") && "unexpected op suffix");
assert(op_name.starts_with("CIR_") && "unexpected op prefix");
assert(op_name.ends_with("Op") && "unexpected op suffix");

// E.g., AddOp -> CreateAddOperator
std::ostringstream oss;
Expand All @@ -92,7 +92,7 @@ static inline bool IsLstmOp(const StringRef op_name) {
}

static void EmitOptionBuilders(const RecordKeeper &record_keeper,
const std::vector<Record *> &defs,
const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;

Expand All @@ -117,7 +117,7 @@ static void EmitOptionBuilders(const RecordKeeper &record_keeper,
mlir::tblgen::Operator op(*def);
for (unsigned i = 0, e = arg_values->getNumArgs(); i != e; ++i) {
auto arg = arg_values->getArg(i);
DefInit *arg_def = dyn_cast<DefInit>(arg);
const auto *arg_def = dyn_cast<DefInit>(arg);
if (!arg_def) continue;
if (arg_def->getDef()->isSubClassOf(attr_type)) {
// This binds the name of the attribute in the TD file with the name
Expand Down Expand Up @@ -175,7 +175,7 @@ static void EmitOptionBuilders(const RecordKeeper &record_keeper,
// arguments that depend on op definitions should be auto-generated and then
// operator should be built by the caller because it does not require
// auto-generation.
static void EmitOperatorBuilders(const std::vector<Record *> &defs,
static void EmitOperatorBuilders(const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;

Expand Down Expand Up @@ -240,7 +240,7 @@ static inline std::string GetOperatorName(const Record &def) {
//
// TODO(hinsu): Consider converting this to a static constant associative
// container instead of a series of if conditions, if required.
static void EmitGetBuiltinOpCode(const std::vector<Record *> &defs,
static void EmitGetBuiltinOpCode(const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;

Expand Down Expand Up @@ -279,7 +279,7 @@ static void EmitGetBuiltinOpCode(const std::vector<Record *> &defs,
// return {0, 0};
// }
static void EmitOperandNumbers(const RecordKeeper &record_keeper,
const std::vector<Record *> &defs,
const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;
const auto attr_type = record_keeper.getClass("Attr");
Expand Down Expand Up @@ -324,7 +324,7 @@ static void EmitOperandNumbers(const RecordKeeper &record_keeper,
// const std::vector<int32_t>& results,
// const std::vector<int32_t>& intermediates,
// flatbuffers::FlatBufferBuilder *fbb);
static void EmitBuildOperator(const std::vector<Record *> &defs,
static void EmitBuildOperator(const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;

Expand Down Expand Up @@ -360,7 +360,7 @@ static void EmitBuildOperator(const std::vector<Record *> &defs,
// mlir::Builder builder,
// llvm::SmallVectorImpl<mlir::NamedAttribute> &attributes);
static void EmitBuiltinOptionsToAttributes(const RecordKeeper &record_keeper,
const std::vector<Record *> &defs,
const std::vector<const Record *> &defs,
raw_ostream *ostream) {
raw_ostream &os = *ostream;

Expand All @@ -385,7 +385,7 @@ static void EmitBuiltinOptionsToAttributes(const RecordKeeper &record_keeper,
auto *arg_values = def->getValueAsDag("arguments");
for (unsigned i = 0, e = arg_values->getNumArgs(); i != e; ++i) {
auto arg = arg_values->getArg(i);
DefInit *arg_def = dyn_cast<DefInit>(arg);
const auto *arg_def = dyn_cast<DefInit>(arg);
if (!arg_def) continue;
if (arg_def->getDef()->isSubClassOf(attr_type)) {
StringRef arg_name = arg_values->getArgNameStr(i);
Expand All @@ -411,11 +411,11 @@ static void EmitBuiltinOptionsToAttributes(const RecordKeeper &record_keeper,
// The function below has a non-constant reference as that is required by LLVM's
// TableGenMain.
// NOLINTNEXTLINE
static bool OperatorWritersMain(raw_ostream &os, RecordKeeper &records) {
static bool OperatorWritersMain(raw_ostream &os, const RecordKeeper &records) {
emitSourceFileHeader("MLIR Circle FlatBuffer Builders", os);

// Retrieve all the definitions derived from CIR_Op and sort by record name.
std::vector<Record *> defs = records.getAllDerivedDefinitions("CIR_Op");
std::vector<const Record *> defs = records.getAllDerivedDefinitions("CIR_Op");
llvm::sort(defs, LessRecord());

for (const auto *def : defs) {
Expand All @@ -424,10 +424,10 @@ static bool OperatorWritersMain(raw_ostream &os, RecordKeeper &records) {
// The generated Circle op C++ class should be circle::<OpName>Op.
// The generated operator's options should be circle::<OpName>Options.
// The option builder should be Create<OpName>Options.
if (!def->getName().startswith("CIR_"))
if (!def->getName().starts_with("CIR_"))
PrintFatalError(def->getLoc(),
"unexpected op name format: 'CIR_' prefix missing");
if (!def->getName().endswith("Op"))
if (!def->getName().ends_with("Op"))
PrintFatalError(def->getLoc(),
"unexpected op name format: 'Op' suffix missing");
}
Expand All @@ -448,7 +448,7 @@ static bool OperatorWritersMain(raw_ostream &os, RecordKeeper &records) {
}

static void GenOperandResultVerifier(raw_ostream &os,
llvm::ArrayRef<llvm::Init *> values,
llvm::ArrayRef<const llvm::Init *> values,
StringRef valueKind) {
mlir::tblgen::FmtContext fctx;

Expand Down Expand Up @@ -496,11 +496,12 @@ static void GenOperandResultVerifier(raw_ostream &os,
}

// NOLINTNEXTLINE
static bool RuntimeVerifierWriterMain(raw_ostream &os, RecordKeeper &records) {
static bool RuntimeVerifierWriterMain(raw_ostream &os,
const RecordKeeper &records) {
emitSourceFileHeader("MLIR Circle Runtime Verifiers", os);

// Retrieve all the definitions derived from CIR_Op and sort by record name.
std::vector<Record *> defs = records.getAllDerivedDefinitions("Op");
std::vector<const Record *> defs = records.getAllDerivedDefinitions("Op");
llvm::sort(defs, LessRecord());

// Iterate through all the ops defined.
Expand Down
4 changes: 2 additions & 2 deletions circle-mlir/circle-mlir/tools/onnx2circle/src/onnx2circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int loadONNX(const std::string &onnx_path, mlir::MLIRContext &context,
{
llvm::StringRef inputFilename(onnx_path);
std::string errorMessage;
if (inputFilename.endswith(".mlir"))
if (inputFilename.ends_with(".mlir"))
{
auto input = mlir::openInputFile(inputFilename, &errorMessage);
if (!input)
Expand All @@ -95,7 +95,7 @@ int loadONNX(const std::string &onnx_path, mlir::MLIRContext &context,
return -1;
}
}
else if (inputFilename.endswith(".onnx"))
else if (inputFilename.ends_with(".onnx"))
{
onnx_mlir::ImportOptions options;
options.useOnnxModelTypes = onnx_mlir::useOnnxModelTypes;
Expand Down
18 changes: 10 additions & 8 deletions circle-mlir/externals/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Match the minimum required version of LLVM and MLIR
cmake_minimum_required(VERSION 3.13.4)
cmake_minimum_required(VERSION 3.20.0)

project(circle-mlir-externals)

Expand Down Expand Up @@ -105,7 +105,7 @@ ExternalProject_Add(externals-abseil-cpp
# from https://github.com/onnx/onnx-mlir/blob/main/docs/BuildOnLinuxOSX.md
ExternalProject_Add(externals-llvm-project
GIT_REPOSITORY "https://github.com/llvm/llvm-project.git"
GIT_TAG "91088978d712cd7b33610c59f69d87d5a39e3113"
GIT_TAG "b270525f730be6e7196667925f5a9bfa153262e9"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/llvm-project"
SOURCE_SUBDIR "llvm"
BINARY_DIR "${LP_BUILD_DIR}"
Expand Down Expand Up @@ -145,22 +145,24 @@ list(APPEND CMAKE_PREFIX_PATH ${PB_INSTALL_DIR})
# build ONNX-MLIR
ExternalProject_Add(externals-onnx-mlir
GIT_REPOSITORY "https://github.com/onnx/onnx-mlir.git"
GIT_TAG "40615b362fe474d6cce4fd9c31a7ec51acd8f88a" # v0.4.1.2
GIT_TAG "660bd8efa664bd2d05801a7a793fe29925313b4b" # v0.5.0.0
GIT_SUBMODULES_RECURSE True
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir"
BINARY_DIR "${OM_BUILD_DIR}"
INSTALL_DIR "${OM_INSTALL_DIR}"
PATCH_COMMAND patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/onnx_quantdequant_i16.diff -N || true
PATCH_COMMAND patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/onnx_mlir_0_5_0_0.diff -N || true
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
-DCMAKE_PREFIX_PATH='${CMAKE_PREFIX_PATH}'
-DCMAKE_INSTALL_PREFIX='${OM_INSTALL_DIR}'
${CIRCLE_MLIR_CLANG_OPTION}
-DMLIR_DIR='${LP_BUILD_DIR}/lib/cmake/mlir'
-DPython3_ROOT_DIR='$ENV{Python3_ROOT_DIR}'
-DONNX_MLIR_BUILD_TESTS=OFF
-DONNX_MLIR_ENABLE_MHLO=OFF
-DONNX_MLIR_DECOMP_ONNX_CONVTRANSPOSE=OFF
-DONNX_MLIR_ENABLE_STABLEHLO=OFF
-DONNX_MLIR_ENABLE_JAVA=OFF
-DLLVM_ENABLE_ASSERTIONS=ON
DEPENDS externals-llvm-project externals-protobuf
)
# NOTE onnx_quantdequant_i16.diff is to provide I16 type in
# DequantizeLinear/QuantizeLinear Ops
# NOTE onnx_mlir_0_5_0_0.diff is to provide I16 type in DequantizeLinear/QuantizeLinear Ops
# and disable decompose for InstanceNormalization and ConvTranspose
# and not necessary test build.
10 changes: 5 additions & 5 deletions circle-mlir/infra/overlay/prepare-venv
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ fi
# NOTE version
# - https://github.com/onnx/onnx/blob/master/docs/Versioning.md

VER_TORCH=2.1.0
VER_ONNX=1.16.0
VER_ONNXRUNTIME=1.18.0
VER_TORCH=2.6.0
VER_ONNX=1.17.0
VER_ONNXRUNTIME=1.21.0

PIP_TRUSTED_HOST="--trusted-host pypi.org "
PIP_TRUSTED_HOST+="--trusted-host pypi.python.org "
Expand All @@ -61,5 +61,5 @@ ${VENV_PYTHON} -m pip ${PIP_OPTIONS} install torch==${VER_TORCH}
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install onnx==${VER_ONNX}
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install onnxruntime==${VER_ONNXRUNTIME}

${VENV_PYTHON} -m pip ${PIP_OPTIONS} install h5py einops
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install numpy==1.24.4
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install h5py==3.8.0 einops
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install numpy==1.24.3