Skip to content
Open
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
22 changes: 17 additions & 5 deletions backends/xnnpack/runtime/XNNExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ ET_NODISCARD Error XNNExecutor::initialize(
* delegate->execute()
*/
ET_NODISCARD Error XNNExecutor::prepare_args(Span<EValue*> args) {
ET_DCHECK_MSG(
!destroyed_.load(std::memory_order_acquire),
"XNNExecutor::prepare_args called after destroy");
ET_DCHECK_MSG(
!in_use_.exchange(true, std::memory_order_acquire),
"XNNExecutor::prepare_args called concurrently");

ET_CHECK_OR_RETURN_ERROR(
runtime_ != nullptr,
Internal,
Expand Down Expand Up @@ -160,11 +167,14 @@ ET_NODISCARD Error XNNExecutor::forward(BackendExecutionContext& context) {
xnn_status status = xnn_setup_runtime_v2(
runtime_.get(), externals_.size(), externals_.data());

ET_CHECK_OR_RETURN_ERROR(
status == xnn_status_success,
Internal,
"Internal Error: Setting up the runtime failed with code: %s",
xnn_status_to_string(status));
if (status != xnn_status_success) {
in_use_.store(false, std::memory_order_release);
ET_LOG(
Error,
"Internal Error: Setting up the runtime failed with code: %s",
xnn_status_to_string(status));
return Error::Internal;
}

auto error = profiler_.start(context.event_tracer());
if (error != Error::Ok) {
Expand All @@ -184,6 +194,8 @@ ET_NODISCARD Error XNNExecutor::forward(BackendExecutionContext& context) {
static_cast<unsigned int>(error));
}

in_use_.store(false, std::memory_order_release);

ET_CHECK_OR_RETURN_ERROR(
status == xnn_status_success,
Internal,
Expand Down
10 changes: 10 additions & 0 deletions backends/xnnpack/runtime/XNNExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>

#include <xnnpack.h>
#include <atomic>
#include <memory>
#include <vector>

Expand All @@ -36,11 +37,20 @@ class XNNExecutor {
std::vector<xnn_external_value> externals_;
std::vector<std::string> packed_data_names_;
std::shared_ptr<XNNWorkspace> workspace_;
std::atomic<bool> in_use_{false};
std::atomic<bool> destroyed_{false};

public:
XNNExecutor(std::shared_ptr<XNNWorkspace> workspace)
: workspace_(workspace) {}

~XNNExecutor() {
ET_DCHECK_MSG(
!in_use_.load(std::memory_order_acquire),
"XNNExecutor destroyed while in use");
destroyed_.store(true, std::memory_order_release);
}

inline size_t getNumInputs() {
return input_ids_.size();
}
Expand Down
2 changes: 2 additions & 0 deletions backends/xnnpack/runtime/XNNWorkspaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ XNNWorkspaceManager::get_or_create_workspace(
return create_result.error();
}

#ifndef XNNPACK_WORKSPACE_ALWAYS_LOCK
create_result.get()->disable_locking();
#endif
return create_result.get();
} else if (mode == WorkspaceSharingMode::PerModel) {
return get_or_create_model_workspace(program_id);
Expand Down
2 changes: 2 additions & 0 deletions backends/xnnpack/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def _get_preprocessor_flags():
if native.read_config("executorch", "xnnpack_weights_cache", "0") != "0":
preprocessor_flags.append("-DENABLE_XNNPACK_WEIGHTS_CACHE")

preprocessor_flags.append("-DXNNPACK_WORKSPACE_ALWAYS_LOCK")

# Enable if not disabled through config
return preprocessor_flags

Expand Down
Loading