-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from cyLi-Tiger/main
feat: add onnx to MegCC
- Loading branch information
Showing
16 changed files
with
1,275 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <vector> | ||
|
||
#include "compiler/Common/Logger.h" | ||
#include "compiler/Common/MemoryStatus.h" | ||
|
||
#include "megdnn/basic_types.h" | ||
#include "mlir/IR/AffineMap.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
|
||
#include "llvm/Support/raw_ostream.h" | ||
|
||
#include "compiler/Dialect/MGB/IR/MGBDialect.h" | ||
|
||
#include "mlir/Dialect/StandardOps/IR/Ops.h" | ||
#include "onnx/common/ir.h" | ||
|
||
namespace mlir { | ||
namespace ONNX { | ||
static inline mlir::Type elemTypeToType( | ||
mlir::MLIRContext* context, const int32_t& elem_type) { | ||
switch (elem_type) { | ||
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT: | ||
return mlir::FloatType::getF32(context); | ||
case ONNX_NAMESPACE::TensorProto_DataType_UINT8: | ||
return mlir::IntegerType::get(context, 8, mlir::IntegerType::Unsigned); | ||
case ONNX_NAMESPACE::TensorProto_DataType_INT32: | ||
return mlir::IntegerType::get(context, 32, mlir::IntegerType::Signed); | ||
default: | ||
CC_ABORT << "Unsupported dtype " << elem_type << "\n"; | ||
break; | ||
} | ||
return mlir::Type(); | ||
} | ||
|
||
static inline mlir::ShapedType valueToShapedType( | ||
mlir::MLIRContext* context, ONNX_NAMESPACE::Value* value) { | ||
std::vector<int64_t> dims; | ||
for (auto dim : value->sizes()) { | ||
dims.emplace_back(dim.dim); | ||
} | ||
LOG_DEBUG << "Create RankedTensorType in Value with shape= " << dims << "\n"; | ||
mlir::ShapedType res; | ||
if (dims.size() > 0) { | ||
res = mlir::RankedTensorType::get( | ||
dims, elemTypeToType(context, value->elemType())); | ||
} else { | ||
LOG_WARN << "Shape is unknown, compiler just make 1 dim dynamic tensor " | ||
"type\n"; | ||
res = mlir::RankedTensorType::get( | ||
{-1}, elemTypeToType(context, value->elemType())); | ||
} | ||
return res; | ||
} | ||
|
||
} // namespace ONNX | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <vector> | ||
|
||
#include "megdnn/basic_types.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
|
||
namespace mlir { | ||
namespace ONNX { | ||
|
||
struct ONNXImporterOptions { | ||
std::string module_name; | ||
std::string model_path; | ||
std::string input_shape_str; | ||
}; | ||
|
||
mlir::LogicalResult import_onnx(mlir::ModuleOp module, std::string model_path); | ||
|
||
} // namespace ONNX | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_subdirectory(MGB) | ||
add_subdirectory(TinyNN) | ||
add_subdirectory(Hako) | ||
add_subdirectory(onnx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
set(LLVM_OPTIONAL_SOURCES onnx_importer.cpp) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DONNX_ML=1 -DONNX_NAMESPACE=onnx") | ||
add_compile_definitions("ONNX_NO_EXCEPTIONS") | ||
|
||
add_mlir_translation_library( | ||
MLIRONNXImporter | ||
importer.cpp | ||
DEPENDS | ||
MLIRMGBIncGen | ||
LINK_LIBS | ||
PUBLIC | ||
MLIRIR | ||
MLIRMGB | ||
MLIRStandard) | ||
# detail obj library created in llvm_add_library | ||
target_include_directories( | ||
obj.MLIRONNXImporter PRIVATE ${MGB_INCLUDE_DIR} ${ONNX_INCLUDE_DIR} | ||
${PROTOBUF_INCLUDE_DIR}) | ||
# add onnx-imported | ||
target_link_libraries(MLIRONNXImporter PUBLIC $<BUILD_INTERFACE:onnx_imported>) | ||
# target_compile_options(MLIRONNXImporter PUBLIC -fexceptions) |
Oops, something went wrong.