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

[Examples]Add ResNet18 E2E Example #404

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion examples/BuddyMobileNetV3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
add_custom_command(
OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/arg0.data
${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/arg1.data
${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/forward.mlir
${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/subgraph0.mlir
COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyMobileNetV3/buddy-mobilenetv3-import.py
Expand Down
21 changes: 14 additions & 7 deletions examples/BuddyMobileNetV3/buddy-mobilenetv3-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@
"The environment variable 'MOBILENETV3_MODEL_PATH' is not set or is invalid."
)

model = models.mobilenet_v3_small(weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1, pretrained=True)
model = models.mobilenet_v3_small(
weights=models.MobileNet_V3_Small_Weights.IMAGENET1K_V1, pretrained=True
)
model = model.eval()

# Remove the num_batches_tracked attribute.
for layer in model.modules():
if isinstance(layer, torch.nn.BatchNorm2d):
if hasattr(layer, "num_batches_tracked"):
del layer.num_batches_tracked

# Initialize Dynamo Compiler with specific configurations as an importer.
dynamo_compiler = DynamoCompiler(
primary_registry=tosa.ops_registry,
Expand Down Expand Up @@ -68,11 +76,10 @@


float32_param = np.concatenate(
[param.detach().numpy().reshape([-1]) for param in params if param.dtype == torch.float32]
[
param.detach().numpy().reshape([-1])
for param in params
if param.dtype == torch.float32
]
)
float32_param.tofile(Path(current_path) / "arg0.data")

int64_param = np.concatenate(
[param.detach().numpy().reshape([-1]) for param in params if param.dtype == torch.int64]
)
int64_param.tofile(Path(current_path) / "arg1.data")
64 changes: 30 additions & 34 deletions examples/BuddyMobileNetV3/buddy-mobilenetv3-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,40 @@ const std::string ImgName = "dog-224*224.png";
// Declare the mobilenet C interface.
extern "C" void _mlir_ciface_forward(MemRef<float, 2> *output,
MemRef<float, 1> *arg0,
MemRef<long long, 1> *arg1,
dip::Image<float, 4> *input);

/// Print [Log] label in bold blue format.
void printLogLabel() { std::cout << "\033[34;1m[Log] \033[0m"; }

void loadParameters(const std::string &floatParamPath,
const std::string &int64ParamPath,
MemRef<float, 1> &floatParam,
MemRef<long long, 1> &int64Param) {
std::ifstream floatParamFile(floatParamPath, std::ios::in | std::ios::binary);
if (!floatParamFile.is_open()) {
std::string errMsg = "Failed to open float param file: " +
std::filesystem::canonical(floatParamPath).string();
throw std::runtime_error(errMsg);
/// Load parameters into data container.
void loadParameters(const std::string &paramFilePath,
MemRef<float, 1> &params) {
const auto loadStart = std::chrono::high_resolution_clock::now();
// Open the parameter file in binary mode.
std::ifstream paramFile(paramFilePath, std::ios::in | std::ios::binary);
if (!paramFile.is_open()) {
throw std::runtime_error("[Error] Failed to open params file!");
}
floatParamFile.read(reinterpret_cast<char *>(floatParam.getData()),
floatParam.getSize() * sizeof(float));
if (floatParamFile.fail()) {
throw std::runtime_error("Failed to read float param file");
printLogLabel();
std::cout << "Loading params..." << std::endl;
printLogLabel();
// Print the canonical path of the parameter file.
std::cout << "Params file: " << std::filesystem::canonical(paramFilePath)
<< std::endl;
// Read the parameter data into the provided memory reference.
paramFile.read(reinterpret_cast<char *>(params.getData()),
sizeof(float) * (params.getSize()));
if (paramFile.fail()) {
throw std::runtime_error("Error occurred while reading params file!");
}
floatParamFile.close();

std::ifstream int64ParamFile(int64ParamPath, std::ios::in | std::ios::binary);
if (!int64ParamFile.is_open()) {
std::string errMsg = "Failed to open int64 param file: " +
std::filesystem::canonical(int64ParamPath).string();
throw std::runtime_error(errMsg);
}
int64ParamFile.read(reinterpret_cast<char *>(int64Param.getData()),
int64Param.getSize() * sizeof(long long));
if (int64ParamFile.fail()) {
throw std::runtime_error("Failed to read int64 param file");
}
int64ParamFile.close();
paramFile.close();
const auto loadEnd = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> loadTime =
loadEnd - loadStart;
printLogLabel();
std::cout << "Params load time: " << (double)(loadTime.count()) / 1000
<< "s\n"
<< std::endl;
}

// Softmax function.
Expand Down Expand Up @@ -120,13 +119,10 @@ int main() {

// Load model parameters from the specified file.
std::string paramsDir = mobilenetDir + "/arg0.data";
std::string intDir = mobilenetDir + "/arg1.data";
MemRef<float, 1> paramsContainerf32({ParamsSize});
MemRef<long long, 1> ParamsContainerInt64({34});
loadParameters(paramsDir, intDir, paramsContainerf32, ParamsContainerInt64);
MemRef<float, 1> paramsContainer({ParamsSize});
loadParameters(paramsDir, paramsContainer);
// Call the forward function of the model.
_mlir_ciface_forward(&output, &paramsContainerf32, &ParamsContainerInt64,
&input);
_mlir_ciface_forward(&output, &paramsContainer, &input);

auto out = output.getData();
softmax(out, 1000);
Expand Down
74 changes: 74 additions & 0 deletions examples/BuddyResNet18/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
add_custom_command(
OUTPUT ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/arg0.data
${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir
${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir
COMMAND python3 ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/buddy-resnet-import.py
COMMENT "Generating forward.mlir, subgraph0.mlir and parameter files"
)


add_custom_command(
OUTPUT forward.o
COMMAND ${LLVM_TOOLS_BINARY_DIR}/mlir-opt ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir
-pass-pipeline
"builtin.module(func.func(tosa-to-linalg-named, tosa-to-linalg, tosa-to-tensor, tosa-to-arith), \
empty-tensor-to-alloc-tensor, convert-elementwise-to-linalg, arith-bufferize, \
func.func(linalg-bufferize, tensor-bufferize), func-bufferize)" |
${LLVM_TOOLS_BINARY_DIR}/mlir-opt
-pass-pipeline
"builtin.module(func.func(buffer-deallocation-simplification, convert-linalg-to-loops), \
eliminate-empty-tensors, func.func(llvm-request-c-wrappers), \
convert-math-to-llvm, convert-math-to-libm, convert-scf-to-cf, \
convert-arith-to-llvm, expand-strided-metadata, finalize-memref-to-llvm, \
convert-func-to-llvm, reconcile-unrealized-casts)" |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llvm-as |
${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3
-o ${BUDDY_BINARY_DIR}/../examples/BuddyResNet18/forward.o
DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/forward.mlir
COMMENT "Building forward.o"
VERBATIM)


add_custom_command(
OUTPUT subgraph0.o
COMMAND ${BUDDY_BINARY_DIR}/buddy-opt ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir
-pass-pipeline
"builtin.module(func.func(tosa-to-linalg-named, tosa-to-arith, tosa-to-linalg, tosa-to-tensor))" |
${BUDDY_BINARY_DIR}/buddy-opt
-convert-elementwise-to-linalg
-func-bufferize-dynamic-offset
-arith-bufferize
-func-bufferize
-tensor-bufferize
-linalg-bufferize
-finalizing-bufferize
-convert-linalg-to-loops
-lower-affine
-convert-scf-to-cf
-llvm-request-c-wrappers
-convert-math-to-llvm
-convert-math-to-libm
-convert-arith-to-llvm
-convert-func-to-llvm
-expand-strided-metadata
-finalize-memref-to-llvm
-reconcile-unrealized-casts |
${LLVM_TOOLS_BINARY_DIR}/mlir-translate -mlir-to-llvmir |
${LLVM_TOOLS_BINARY_DIR}/llvm-as |
${LLVM_TOOLS_BINARY_DIR}/llc -filetype=obj -relocation-model=pic -O3
-o ${BUDDY_BINARY_DIR}/../examples/BuddyResNet18/subgraph0.o
DEPENDS ${BUDDY_EXAMPLES_DIR}/BuddyResNet18/subgraph0.mlir
buddy-opt
COMMENT "Building subgraph0.o"
VERBATIM)

add_library(RESNET STATIC subgraph0.o forward.o)

SET_TARGET_PROPERTIES(RESNET PROPERTIES LINKER_LANGUAGE C)

add_executable(buddy-resnet-run buddy-resnet-main.cpp)
target_link_directories(buddy-resnet-run PRIVATE ${LLVM_LIBRARY_DIR})

set(BUDDY_RESNET_LIBS RESNET mlir_c_runner_utils ${PNG_LIBRARIES})
target_link_libraries(buddy-resnet-run ${BUDDY_RESNET_LIBS})
Loading