executorch icon indicating copy to clipboard operation
executorch copied to clipboard

Error Code 32 on module.load_method("forward")

Open sorenjmadsen opened this issue 1 year ago • 2 comments

I have compiled and linked ExecuTorch release 0.6 with a project I'm working on in C++. However, I get an error code 32 when I attempt to load my forward function. Currently, I export my model like this.

import torch
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower
from torch.export import Dim, export
from model import Classifier
import os

model = Classifier(num_in_types=2, n_blocks=2, num_devices=7)
model.eval()
model_path = os.environ.get("MODEL_PATH") or "./model.pth"
model.load_state_dict(torch.load(model_path, weights_only=True))


inputs = (torch.randn(1,96,11,11, dtype=torch.float32),)
with torch.no_grad():
    exported_program = export(model, inputs)
    executorch_program = to_edge_transform_and_lower(
        exported_program,
        partitioner=[XnnpackPartitioner()]
    ).to_executorch()

with open("model.pte", "wb") as file:
    file.write(executorch_program.buffer)

I defined a class for performing inference with my .pte model:

#include <stdlib.h>
#include <stdexcept>
#include <array>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>

using namespace::executorch::extension;

class AIClassifier {
  public:
    AIClassifier() {
        LoadModel();
    }

  private:

    void LoadModel() {
        model_module = std::make_unique<Module>(model_path);
        const auto load_model_error = model_module->load();
        const auto load_forward_error = model_module->load_method("forward");
        if (!model_module->is_loaded()){
            throw std::runtime_error((std::string) "AI client error on load model: " + strerror(errno));
        }
        else {
            std::cout << "Successfully loaded model module!" << std::endl;
        }
        const auto load_forward_error = model_module->load_method("forward");
        if (!model_module->is_method_loaded("forward")){
            std::cout << "Failed to load forward method: ";
            std::cout << static_cast<uint32_t>(load_forward_error) << std::endl;
        }
        else {
            std::cout << "Successfully loaded forward method!" << std::endl;
        }
    }
    inline static const char* model_path = "/path/to/classifier/model.pte";
    std::unique_ptr<Module> model_module;
};

I can load the model successfully, but I am consistently getting error 32 (error.h: resource not found) on force-load of the forward method. I've tested the existence of forward by using this C++ snippet in my class:

auto method_names_result = model_module->method_names();
if (method_names_result.ok()) {
    auto method_names = method_names_result.get();
    std::cout << "Available methods (" << method_names.size() << "):" << std::endl;
    for (const auto& name : method_names) {
        std::cout << "  - '" << name << "'" << std::endl;
    }
} else {
    std::cout << "Failed to get method names" << std::endl;
}

I get the output:

Available methods (1):
  - 'forward'

Before even trying to load the model in C++, I tested my export with this code snippet from a tutorial, and I have no issues except for a warning: [program.cpp:135] InternalConsistency verification requested but not available

from executorch.runtime import Runtime
import torch
from typing import List
runtime = Runtime.get()

input_tensor = torch.randn(1,96,11,11, dtype=torch.float32)
program = runtime.load_program("model.pte")
method = program.load_method("forward")
output: List[torch.Tensor] = method.execute([input_tensor])
print(output)

I've scoured the docs, and I can't find anything that addresses this. Does anyone know what could be causing this?

cc @larryliu0820 @jathu

sorenjmadsen avatar Jun 27 '25 07:06 sorenjmadsen

Update: Including my CMAKE command and build information

cmake -DCMAKE_BUILD_TYPE=Release -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON -DEXECUTORCH_BUILD_ARM_BAREMETAL=ON

Compiler: aarch64-xilinx-linux-g++ -mcpu=cortex-a72.cortex-a53 -march=armv8-a+crc

sorenjmadsen avatar Jun 27 '25 08:06 sorenjmadsen

Resolved by adding -Wl,--whole-archive -lportable_ops_lib -Wl,--no-whole-archive the_rest_of_the_libs... in my ld command.

sorenjmadsen avatar Jun 27 '25 16:06 sorenjmadsen