Update NN VTS callback objects

am: 051cf39f99

Change-Id: Ibab8657b851d56af18965766303e0f26bc51bded
This commit is contained in:
Michael Butler
2019-07-19 13:25:15 -07:00
committed by android-build-merger
5 changed files with 442 additions and 723 deletions

View File

@@ -14,130 +14,78 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "Callbacks"
#include "1.0/Callbacks.h" #include "1.0/Callbacks.h"
#include <android-base/logging.h> #include <android-base/logging.h>
namespace android { namespace android::hardware::neuralnetworks::V1_0::implementation {
namespace hardware {
namespace neuralnetworks {
namespace V1_0 {
namespace implementation {
CallbackBase::CallbackBase() : mNotified(false) {} // PreparedModelCallback methods begin here
CallbackBase::~CallbackBase() {
// Note that we cannot call CallbackBase::join_thread from here:
// CallbackBase is intended to be reference counted, and it is possible that
// the reference count drops to zero in the bound thread, causing the
// bound thread to call this destructor. If a thread tries to join
// itself, it throws an exception, producing a message like the
// following:
//
// terminating with uncaught exception of type std::__1::system_error:
// thread::join failed: Resource deadlock would occur
}
void CallbackBase::wait() {
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this]{return mNotified;});
join_thread_locked();
}
bool CallbackBase::on_finish(std::function<bool(void)> post_work) {
std::lock_guard<std::mutex> lock(mMutex);
if (mPostWork != nullptr) {
LOG(ERROR) << "CallbackBase::on_finish -- a post-work function has already been bound to "
"this callback object";
return false;
}
if (post_work == nullptr) {
LOG(ERROR) << "CallbackBase::on_finish -- the new post-work function is invalid";
return false;
}
mPostWork = std::move(post_work);
return true;
}
bool CallbackBase::bind_thread(std::thread&& asyncThread) {
std::lock_guard<std::mutex> lock(mMutex);
if (mThread.joinable()) {
LOG(ERROR) << "CallbackBase::bind_thread -- a thread has already been bound to this "
"callback object";
return false;
}
if (!asyncThread.joinable()) {
LOG(ERROR) << "CallbackBase::bind_thread -- the new thread is not joinable";
return false;
}
mThread = std::move(asyncThread);
return true;
}
void CallbackBase::join_thread() {
std::lock_guard<std::mutex> lock(mMutex);
join_thread_locked();
}
void CallbackBase::notify() {
{
std::lock_guard<std::mutex> lock(mMutex);
mNotified = true;
if (mPostWork != nullptr) {
bool success = mPostWork();
if (!success) {
LOG(ERROR) << "CallbackBase::notify -- post work failed";
}
}
}
mCondition.notify_all();
}
void CallbackBase::join_thread_locked() {
if (mThread.joinable()) {
mThread.join();
}
}
PreparedModelCallback::PreparedModelCallback() :
mErrorStatus(ErrorStatus::GENERAL_FAILURE), mPreparedModel(nullptr) {}
PreparedModelCallback::~PreparedModelCallback() {}
Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus, Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus,
const sp<V1_0::IPreparedModel>& preparedModel) { const sp<IPreparedModel>& preparedModel) {
mErrorStatus = errorStatus; {
mPreparedModel = preparedModel; std::lock_guard<std::mutex> hold(mMutex);
CallbackBase::notify();
// quick-return if object has already been notified
if (mNotified) {
return Void();
}
// store results and mark as notified
mErrorStatus = errorStatus;
mPreparedModel = preparedModel;
mNotified = true;
}
mCondition.notify_all();
return Void(); return Void();
} }
ErrorStatus PreparedModelCallback::getStatus() { void PreparedModelCallback::wait() const {
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this] { return mNotified; });
}
ErrorStatus PreparedModelCallback::getStatus() const {
wait(); wait();
return mErrorStatus; return mErrorStatus;
} }
sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() { sp<IPreparedModel> PreparedModelCallback::getPreparedModel() const {
wait(); wait();
return mPreparedModel; return mPreparedModel;
} }
ExecutionCallback::ExecutionCallback() : mErrorStatus(ErrorStatus::GENERAL_FAILURE) {} // ExecutionCallback methods begin here
ExecutionCallback::~ExecutionCallback() {}
Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) { Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) {
mErrorStatus = errorStatus; {
CallbackBase::notify(); std::lock_guard<std::mutex> hold(mMutex);
// quick-return if object has already been notified
if (mNotified) {
return Void();
}
mErrorStatus = errorStatus;
mNotified = true;
}
mCondition.notify_all();
return Void(); return Void();
} }
ErrorStatus ExecutionCallback::getStatus() { void ExecutionCallback::wait() const {
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this] { return mNotified; });
}
ErrorStatus ExecutionCallback::getStatus() const {
wait(); wait();
return mErrorStatus; return mErrorStatus;
} }
} // namespace implementation } // namespace android::hardware::neuralnetworks::V1_0::implementation
} // namespace V1_0
} // namespace neuralnetworks
} // namespace hardware
} // namespace android

View File

@@ -17,268 +17,160 @@
#ifndef ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H #ifndef ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H
#define ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H #define ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H
#include <android-base/thread_annotations.h>
#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h> #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h> #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
#include <hidl/Status.h> #include <hidl/Status.h>
#include <chrono>
#include <condition_variable> #include <condition_variable>
#include <functional>
#include <mutex> #include <mutex>
#include <thread>
namespace android { /*
namespace hardware { * The Callback classes are used internally by the NeuralNetworks runtime to
namespace neuralnetworks {
namespace V1_0 {
namespace implementation {
/**
* The CallbackBase class is used internally by the NeuralNetworks runtime to
* synchronize between different threads. An asynchronous task is launched * synchronize between different threads. An asynchronous task is launched
* paired with a callback object. When a client thread requires the output being * paired with a callback object. When a client thread requires the output being
* generated by the asynchronous task, the client thread can wait for the result * generated by the asynchronous task, the client thread can wait for the result
* and be blocked until it has completed or a timeout condition has been * and be blocked until it has completed. Any wait may safely be called
* reached. Any wait* may safely be called concurrently, even on the same * concurrently, even on the same callback object. When the asynchronous task
* callback object. When the asynchronous task has finished its workload, it * has finished its workload, it must immediately call "notify". If the
* must immediately call "notify". If the asynchronous task has failed to launch, * asynchronous task has failed to launch, the function that tried to launch the
* the function that tried to launch the asynchronous task must immediately call * asynchronous task must immediately call "notify". This "notify" call
* "notify". This "notify" call awakens any client threads waiting on the * awakens any client threads waiting on the callback object.
* callback object.
* *
* The CallbackBase class implements some of the base synchronization common to * These classes exist to enable synchronization across HIDL. When
* both PrepareModelCallback and ExecutionCallback. For consistency, any HIDL * synchronization is only required in the same process, consider using
* callback class must inherit from CallbackBase as well as the HIDL callback * std::future, std::mutex, std::condition_variable, or std::experimental::latch
* interface it implements. * instead.
*
* This class exists to enable synchronization across HIDL. When synchronization
* is only required in the same process, consider using std::future, std::mutex,
* std::condition_variable, or std::experimental::latch instead.
*/ */
class CallbackBase {
public:
CallbackBase();
~CallbackBase();
/** namespace android::hardware::neuralnetworks::V1_0::implementation {
* CallbackBase::wait blocks until notify has been called on the callback
* object.
*/
void wait();
/**
* CallbackBase::wait_for blocks until notify has been called on the
* callback object or the time duration from the time the wait_for function
* was called has expired, whichever comes first.
*
* @return Status std::cv_status::no_timeout if the callback was notified
* before the time duration expired, std::cv_status::timeout
* otherwise.
*/
template <class Rep, class Period>
std::cv_status wait_for(const std::chrono::duration<Rep, Period>& timeout_duration);
/**
* CallbackBase::on_finish binds a function to the callback object. This
* bound function will be executed when CallbackBase::notify is called,
* before any calls to wait* return. (Note that CallbackBase::wait_for can
* return std::cv_status::timeout before CallbackBase::notify is called for
* the first time, and hence before the bound function is executed.)
*
* The bound function must not synchronize with or otherwise access the
* callback object it is bound to, as this could cause a deadlock.
*
* CallbackBase::on_finish can be called at most once on a given callback
* object, and the call to CallbackBase::on_finish must finish before
* CallbackBase::notify is called.
*
* @param post_work Function to be invoked the first time
* CallbackBase::notify is called. Must have a target --
* i.e., must not compare equal to nullptr. post_work
* returns true if it successfully completes, false if it
* fails.
* @return bool True if the function was successfully bound, false if
* unsuccessful.
*
* TODO: Why does the return value of the callback matter?
*/
bool on_finish(std::function<bool(void)> post_work);
/**
* CallbackBase::bind_thread binds a thread to the event for later use by
* CallbackBase::join_thread.
*
* The thread must be passed using std::move.
*
* Once a thread is bound with CallbackBase::bind_thread, the client code
* should ensure that one of the following occurs before the event is
* destroyed:
* - CallbackBase::join_thread has been called.
* - CallbackBase::wait has been called.
* - CallbackBase::wait_for has been called and returned other than
* std::cv_status::no_timeout.
*
* The bound thread shall not call any CallbackBase method with the
* exception of CallbackBase::notify, which it must call when the thread has
* finished its computation.
*
* CallbackBase::bind_thread can be called at most once on a given callback
* object.
*
* @param asyncThread Thread to be bound to the callback object. The thread
* object must represent a thread of execution -- i.e.,
* asyncThread.joinable() must be true.
* @return bool True if successful, false if thread was not properly bound.
*/
bool bind_thread(std::thread&& asyncThread);
/**
* CallbackBase::join_thread ensures that the thread (if any) bound to this
* event with CallbackBase::bind_thread has fully finished and cleaned its
* resources. It is legal to call this function multiple times, concurrently
* or sequentially.
*/
void join_thread();
protected:
/**
* CallbackBase::notify enables all prior and future wait* calls on the
* callback object to proceed. The call to CallbackBase::notify happens
* before any wait* calls on this callback object return (except in the case
* of wait_for timing out). The asynchronous call the callback object is
* paired with must ensure that any update to state that should be visible
* to the caller of wait* happens before the call to CallbackBase::notify.
*
* CallbackBase::notify must be called exactly once on a given callback
* object.
*/
void notify();
private:
// Same as CallbackBase::join_thread but assumes we already hold a lock on
// mMutex.
void join_thread_locked();
bool mNotified;
std::mutex mMutex;
std::condition_variable mCondition;
std::function<bool(void)> mPostWork;
std::thread mThread;
};
/** /**
* The PreparedModelCallback class is used to receive the error status of * The PreparedModelCallback class is used to receive the error status of
* preparing a model as well as the prepared model from a task executing * preparing a model as well as the prepared model from a task executing
* asynchronously with respect to the runtime. If a calling thread calls wait* * asynchronously with respect to the runtime. If a calling thread calls wait
* or get* on a PreparedModelCallback object and the corresponding asynchronous * or get* on a PreparedModelCallback object and the corresponding asynchronous
* task has not finished preparing the model, the calling thread will block * task has not finished preparing the model, the calling thread will block
* until the asynchronous task has called notify. For more information on the * until the asynchronous task has called notify.
* synchronization behavior, refer to the CallbackBase class.
* *
* This class inherits the basic blocking and signaling calls from * If the callback object is notified more than once, only the results of the
* CallbackBase, and implements the HIDL notify call from * first call to notify are used, and the results from subsequent calls are
* IPreparedModelCallback. This callback object is passed as an argument to * discarded.
* IDevice::prepareModel. *
* This callback object is passed as an argument to IDevice::prepareModel*.
*/ */
class PreparedModelCallback : public CallbackBase, public IPreparedModelCallback { class PreparedModelCallback : public IPreparedModelCallback {
public: public:
PreparedModelCallback();
~PreparedModelCallback() override;
/** /**
* IPreparedModelCallback::notify marks the callback object with the return * IPreparedModelCallback::notify marks the callback object with the return
* status of the asynchronous model preparation along with the prepared * status of the asynchronous model preparation along with the prepared
* model and calls CallbackBase::notify, enabling all prior and future * model, and allows all prior and future wait calls on the
* wait* calls on the PreparedModelCallback object to proceed. * PreparedModelCallback object to proceed.
* For more information on the synchronization behavior, refer to the
* CallbackBase class.
* *
* IPreparedModelCallback::notify must be called exactly once on a given * IPreparedModelCallback::notify must be called on a given
* PreparedModelCallback object. * PreparedModelCallback object.
* *
* If the callback object is notified more than once, only the results of
* the first call to notify are used, and the results from subsequent calls
* are discarded.
*
* @param status Error status returned from asynchronously preparing the * @param status Error status returned from asynchronously preparing the
* model; will be: * model; will be:
* - NONE if the asynchronous preparation was successful * - NONE if the asynchronous preparation was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - INVALID_ARGUMENT if the input model is invalid * - INVALID_ARGUMENT if the input model is invalid
* @param preparedModel Returned model that has been prepared for execution, * @param preparedModel Returned model that has been prepared for execution,
* nullptr if the model was unable to be prepared. * nullptr if the model was unable to be prepared.
*/ */
Return<void> notify(ErrorStatus status, const sp<V1_0::IPreparedModel>& preparedModel) override; Return<void> notify(ErrorStatus status, const sp<IPreparedModel>& preparedModel) override;
/**
* PreparedModelCallback::wait blocks until notify has been called on the
* callback object.
*/
void wait() const;
/** /**
* Retrieves the error status returned from the asynchronous task launched * Retrieves the error status returned from the asynchronous task launched
* by IDevice::prepareModel. If IDevice::prepareModel has not finished * by IDevice::prepareModel*. If IDevice::prepareModel* has not finished
* asynchronously preparing the model, this call will block until the * asynchronously preparing the model, this call will block until the
* asynchronous task notifies the object. * asynchronous task notifies the object.
* *
* @return status Error status returned from asynchronously preparing the * @return status Error status returned from asynchronously preparing the
* model; will be: * model; will be:
* - NONE if the asynchronous preparation was successful * - NONE if the asynchronous preparation was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - INVALID_ARGUMENT if the input model is invalid * - INVALID_ARGUMENT if the input model is invalid
*/ */
ErrorStatus getStatus(); ErrorStatus getStatus() const;
/** /**
* Retrieves the model that has been prepared for execution from the * Retrieves the model that has been prepared for execution from the
* asynchronous task launched by IDevice::prepareModel. If * asynchronous task launched by IDevice::prepareModel*. If
* IDevice::prepareModel has not finished asynchronously preparing the * IDevice::prepareModel* has not finished asynchronously preparing the
* model, this call will block until the asynchronous task notifies the * model, this call will block until the asynchronous task notifies the
* object. * object.
* *
* @return preparedModel Returned model that has been prepared for * @return preparedModel Returned model that has been prepared for
* execution, nullptr if the model was unable to be * execution, nullptr if the model was unable to be prepared.
* prepared.
*/ */
sp<V1_0::IPreparedModel> getPreparedModel(); sp<IPreparedModel> getPreparedModel() const;
private: private:
ErrorStatus mErrorStatus; mutable std::mutex mMutex;
sp<V1_0::IPreparedModel> mPreparedModel; mutable std::condition_variable mCondition;
bool mNotified GUARDED_BY(mMutex) = false;
ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE;
sp<IPreparedModel> mPreparedModel;
}; };
/** /**
* The ExecutionCallback class is used to receive the error status of the * The ExecutionCallback class is used to receive the results of the execution
* execution from a task executing asynchronously with respect to the runtime. * from a task executing asynchronously with respect to the runtime. If a
* If a calling thread calls wait* or get* on a PreparedModelCallback object and * calling thread calls wait or get* on a ExecutionCallback object and the
* the corresponding asynchronous task has not finished the execution, the * corresponding asynchronous task has not finished the execution, the calling
* calling thread will block until the asynchronous task has called notify. * thread will block until the asynchronous task has called notify.
* For more information on the synchronization behavior, refer to the
* CallbackBase class.
* *
* This class inherits the basic blocking and signaling calls from * If the callback object is notified more than once, only the results of the
* CallbackBase, and implements the HIDL notify call from IExecutionCallback. * first call to notify are used, and the results from subsequent calls are
* This callback object is passed as an argument to IPreparedModel::execute. * discarded.
*
* This callback object is passed as an argument to IPreparedModel::execute*.
*/ */
class ExecutionCallback : public CallbackBase, public IExecutionCallback { class ExecutionCallback : public IExecutionCallback {
public: public:
ExecutionCallback();
~ExecutionCallback() override;
/** /**
* IExecutionCallback::notify marks the callback object with the return * IExecutionCallback::notify marks the callback object with the return
* status of the asynchronous execution that held this callback and enable * status of the asynchronous execution that held this callback and enables
* all prior and future wait* calls on the ExecutionCallback object to * all prior and future wait calls on the ExecutionCallback object to
* proceed. For more information on the synchronization behavior, refer to * proceed.
* the CallbackBase class.
* *
* IExecutionCallback::notify must be called exactly once on a given * IExecutionCallback::notify must be called on a given ExecutionCallback
* ExecutionCallback object. * object.
*
* If the callback object is notified more than once, only the results of
* the first call to notify are used, and the results from subsequent calls
* are discarded.
* *
* @param status Error status returned from launching the asynchronous task * @param status Error status returned from launching the asynchronous task
* (if the launch fails) or from the asynchronous task itself * (if the launch fails) or from the asynchronous task itself (if the
* (if the launch succeeds). Must be: * launch succeeds). Must be:
* - NONE if the asynchronous execution was successful * - NONE if the asynchronous execution was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - OUTPUT_INSUFFICIENT_SIZE if provided output buffer is * - OUTPUT_INSUFFICIENT_SIZE if provided output buffer is not large
* not large enough to store the resultant values * enough to store the resultant values
* - INVALID_ARGUMENT if the input request is invalid * - INVALID_ARGUMENT if the input request is invalid
*/ */
Return<void> notify(ErrorStatus status) override; Return<void> notify(ErrorStatus status) override;
/**
* ExecutionCallback::wait blocks until notify has been called on the
* callback object.
*/
void wait() const;
/** /**
* Retrieves the error status returned from the asynchronous task launched * Retrieves the error status returned from the asynchronous task launched
* by IPreparedModel::execute. If IPreparedModel::execute has not finished * by IPreparedModel::execute. If IPreparedModel::execute has not finished
@@ -286,41 +178,26 @@ class ExecutionCallback : public CallbackBase, public IExecutionCallback {
* task notifies the object. * task notifies the object.
* *
* @return status Error status returned from launching the asynchronous task * @return status Error status returned from launching the asynchronous task
* (if the launch fails) or from the asynchronous task itself * (if the launch fails) or from the asynchronous task itself (if the
* (if the launch succeeds). Must be: * launch succeeds). Must be:
* - NONE if the asynchronous execution was successful * - NONE if the asynchronous execution was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if the asynchronous task resulted in an * - GENERAL_FAILURE if the asynchronous task resulted in an unspecified
* unspecified error * error
* - OUTPUT_INSUFFICIENT_SIZE if at least one output * - OUTPUT_INSUFFICIENT_SIZE if at least one output operand buffer is
* operand buffer is not large enough to store the * not large enough to store the corresponding output
* corresponding output * - INVALID_ARGUMENT if one of the input arguments to prepareModel is
* - INVALID_ARGUMENT if one of the input arguments to * invalid
* prepareModel is invalid
*/ */
ErrorStatus getStatus(); ErrorStatus getStatus() const;
private: private:
mutable std::mutex mMutex;
mutable std::condition_variable mCondition;
bool mNotified GUARDED_BY(mMutex) = false;
ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE; ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE;
}; };
// template function implementation(s) below this point } // namespace android::hardware::neuralnetworks::V1_0::implementation
template <class Rep, class Period>
std::cv_status CallbackBase::wait_for(const std::chrono::duration<Rep, Period>& timeout_duration) {
std::unique_lock<std::mutex> lock(mMutex);
std::cv_status status =
mCondition.wait_for(lock, timeout_duration, [this] { return mNotified; });
if (status != std::cv_status::timeout) {
join_thread_locked();
}
return status;
}
} // namespace implementation
} // namespace V1_0
} // namespace neuralnetworks
} // namespace hardware
} // namespace android
#endif // ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H #endif // ANDROID_HARDWARE_NEURALNETWORKS_V1_0_CALLBACKS_H

View File

@@ -14,160 +14,128 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "Callbacks"
#include "1.2/Callbacks.h" #include "1.2/Callbacks.h"
#include <android-base/logging.h> #include <android-base/logging.h>
namespace android { #include <limits>
namespace hardware {
namespace neuralnetworks {
namespace V1_2 {
namespace implementation {
CallbackBase::CallbackBase() : mNotified(false) {} namespace android::hardware::neuralnetworks::V1_2::implementation {
CallbackBase::~CallbackBase() { constexpr Timing kNoTiming = {.timeOnDevice = std::numeric_limits<uint64_t>::max(),
// Note that we cannot call CallbackBase::join_thread from here: .timeInDriver = std::numeric_limits<uint64_t>::max()};
// CallbackBase is intended to be reference counted, and it is possible that
// the reference count drops to zero in the bound thread, causing the
// bound thread to call this destructor. If a thread tries to join
// itself, it throws an exception, producing a message like the
// following:
//
// terminating with uncaught exception of type std::__1::system_error:
// thread::join failed: Resource deadlock would occur
}
void CallbackBase::wait() { // PreparedModelCallback methods begin here
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this] { return mNotified; });
join_thread_locked();
}
bool CallbackBase::on_finish(std::function<bool(void)> post_work) {
std::lock_guard<std::mutex> lock(mMutex);
if (mPostWork != nullptr) {
LOG(ERROR) << "CallbackBase::on_finish -- a post-work function has already been bound to "
"this callback object";
return false;
}
if (post_work == nullptr) {
LOG(ERROR) << "CallbackBase::on_finish -- the new post-work function is invalid";
return false;
}
mPostWork = std::move(post_work);
return true;
}
bool CallbackBase::bind_thread(std::thread&& asyncThread) {
std::lock_guard<std::mutex> lock(mMutex);
if (mThread.joinable()) {
LOG(ERROR) << "CallbackBase::bind_thread -- a thread has already been bound to this "
"callback object";
return false;
}
if (!asyncThread.joinable()) {
LOG(ERROR) << "CallbackBase::bind_thread -- the new thread is not joinable";
return false;
}
mThread = std::move(asyncThread);
return true;
}
void CallbackBase::join_thread() {
std::lock_guard<std::mutex> lock(mMutex);
join_thread_locked();
}
void CallbackBase::notify() {
{
std::lock_guard<std::mutex> lock(mMutex);
mNotified = true;
if (mPostWork != nullptr) {
bool success = mPostWork();
if (!success) {
LOG(ERROR) << "CallbackBase::notify -- post work failed";
}
}
}
mCondition.notify_all();
}
void CallbackBase::join_thread_locked() {
if (mThread.joinable()) {
mThread.join();
}
}
PreparedModelCallback::PreparedModelCallback()
: mErrorStatus(ErrorStatus::GENERAL_FAILURE), mPreparedModel(nullptr) {}
PreparedModelCallback::~PreparedModelCallback() {}
Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus, Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus,
const sp<V1_0::IPreparedModel>& preparedModel) { const sp<V1_0::IPreparedModel>& preparedModel) {
mErrorStatus = errorStatus; {
mPreparedModel = preparedModel; std::lock_guard<std::mutex> hold(mMutex);
CallbackBase::notify();
// quick-return if object has already been notified
if (mNotified) {
return Void();
}
// store results and mark as notified
mErrorStatus = errorStatus;
mPreparedModel = preparedModel;
mNotified = true;
}
mCondition.notify_all();
return Void(); return Void();
} }
Return<void> PreparedModelCallback::notify_1_2(ErrorStatus errorStatus, Return<void> PreparedModelCallback::notify_1_2(ErrorStatus errorStatus,
const sp<V1_2::IPreparedModel>& preparedModel) { const sp<V1_2::IPreparedModel>& preparedModel) {
mErrorStatus = errorStatus; return notify(errorStatus, preparedModel);
mPreparedModel = preparedModel;
CallbackBase::notify();
return Void();
} }
ErrorStatus PreparedModelCallback::getStatus() { void PreparedModelCallback::wait() const {
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this] { return mNotified; });
}
ErrorStatus PreparedModelCallback::getStatus() const {
wait(); wait();
return mErrorStatus; return mErrorStatus;
} }
sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() { sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() const {
wait(); wait();
return mPreparedModel; return mPreparedModel;
} }
ExecutionCallback::ExecutionCallback() : mErrorStatus(ErrorStatus::GENERAL_FAILURE) {} // ExecutionCallback methods begin here
ExecutionCallback::~ExecutionCallback() {}
Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) { Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) {
mErrorStatus = errorStatus; notifyInternal(errorStatus, {}, kNoTiming);
mOutputShapes = {};
mTiming = {.timeOnDevice = UINT64_MAX, .timeInDriver = UINT64_MAX};
CallbackBase::notify();
return Void(); return Void();
} }
Return<void> ExecutionCallback::notify_1_2(ErrorStatus errorStatus, Return<void> ExecutionCallback::notify_1_2(ErrorStatus errorStatus,
const hidl_vec<OutputShape>& outputShapes, const hidl_vec<OutputShape>& outputShapes,
const Timing& timing) { const Timing& timing) {
mErrorStatus = errorStatus; if (errorStatus == ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
mOutputShapes = outputShapes; // outputShapes must not be empty if OUTPUT_INSUFFICIENT_SIZE.
mTiming = timing; if (outputShapes.size() == 0) {
CallbackBase::notify(); LOG(ERROR) << "Notified with empty output shape vector when OUTPUT_INSUFFICIENT_SIZE";
notifyInternal(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
return Void();
}
} else if (errorStatus != ErrorStatus::NONE) {
// outputShapes must be empty if errorStatus is neither NONE nor OUTPUT_INSUFFICIENT_SIZE.
if (outputShapes.size() != 0) {
LOG(ERROR) << "Notified with non-empty output shape vector when error status is "
"neither NONE nor OUTPUT_INSUFFICIENT_SIZE";
notifyInternal(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
return Void();
}
}
notifyInternal(errorStatus, outputShapes, timing);
return Void(); return Void();
} }
ErrorStatus ExecutionCallback::getStatus() { void ExecutionCallback::wait() const {
std::unique_lock<std::mutex> lock(mMutex);
mCondition.wait(lock, [this] { return mNotified; });
}
ErrorStatus ExecutionCallback::getStatus() const {
wait(); wait();
return mErrorStatus; return mErrorStatus;
} }
const std::vector<OutputShape>& ExecutionCallback::getOutputShapes() { const std::vector<OutputShape>& ExecutionCallback::getOutputShapes() const {
wait(); wait();
return mOutputShapes; return mOutputShapes;
} }
Timing ExecutionCallback::getTiming() { Timing ExecutionCallback::getTiming() const {
wait(); wait();
return mTiming; return mTiming;
} }
} // namespace implementation void ExecutionCallback::notifyInternal(ErrorStatus errorStatus,
} // namespace V1_2 const hidl_vec<OutputShape>& outputShapes,
} // namespace neuralnetworks const Timing& timing) {
} // namespace hardware {
} // namespace android std::lock_guard<std::mutex> hold(mMutex);
// quick-return if object has already been notified
if (mNotified) {
return;
}
mErrorStatus = errorStatus;
mOutputShapes = outputShapes;
mTiming = timing;
mNotified = true;
}
mCondition.notify_all();
}
} // namespace android::hardware::neuralnetworks::V1_2::implementation

View File

@@ -26,6 +26,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>
#include <thread>
#include "1.2/Callbacks.h" #include "1.2/Callbacks.h"
#include "GeneratedTestHarness.h" #include "GeneratedTestHarness.h"

View File

@@ -17,299 +17,218 @@
#ifndef ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H #ifndef ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H
#define ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H #define ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H
#include <android-base/thread_annotations.h>
#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h> #include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h> #include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
#include <hidl/Status.h> #include <hidl/Status.h>
#include <chrono>
#include <condition_variable> #include <condition_variable>
#include <functional>
#include <mutex> #include <mutex>
#include <thread>
namespace android { /*
namespace hardware { * The Callback classes are used internally by the NeuralNetworks runtime to
namespace neuralnetworks { * synchronize between different threads. An asynchronous task is launched
namespace V1_2 { * paired with a callback object. When a client thread requires the output being
namespace implementation { * generated by the asynchronous task, the client thread can wait for the result
* and be blocked until it has completed. Any wait may safely be called
* concurrently, even on the same callback object. When the asynchronous task
* has finished its workload, it must immediately call "notify*". If the
* asynchronous task has failed to launch, the function that tried to launch the
* asynchronous task must immediately call "notify*". This "notify*" call
* awakens any client threads waiting on the callback object.
*
* These classes exist to enable synchronization across HIDL. When
* synchronization is only required in the same process, consider using
* std::future, std::mutex, std::condition_variable, or std::experimental::latch
* instead.
*/
namespace android::hardware::neuralnetworks::V1_2::implementation {
using V1_0::ErrorStatus; using V1_0::ErrorStatus;
/**
* The CallbackBase class is used internally by the NeuralNetworks runtime to
* synchronize between different threads. An asynchronous task is launched
* paired with a callback object. When a client thread requires the output being
* generated by the asynchronous task, the client thread can wait for the result
* and be blocked until it has completed or a timeout condition has been
* reached. Any wait* may safely be called concurrently, even on the same
* callback object. When the asynchronous task has finished its workload, it
* must immediately call "notify". If the asynchronous task has failed to launch,
* the function that tried to launch the asynchronous task must immediately call
* "notify". This "notify" call awakens any client threads waiting on the
* callback object.
*
* The CallbackBase class implements some of the base synchronization common to
* both PrepareModelCallback and ExecutionCallback. For consistency, any HIDL
* callback class must inherit from CallbackBase as well as the HIDL callback
* interface it implements.
*
* This class exists to enable synchronization across HIDL. When synchronization
* is only required in the same process, consider using std::future, std::mutex,
* std::condition_variable, or std::experimental::latch instead.
*/
class CallbackBase {
public:
CallbackBase();
~CallbackBase();
/**
* CallbackBase::wait blocks until notify has been called on the callback
* object.
*/
void wait();
/**
* CallbackBase::wait_for blocks until notify has been called on the
* callback object or the time duration from the time the wait_for function
* was called has expired, whichever comes first.
*
* @return Status std::cv_status::no_timeout if the callback was notified
* before the time duration expired, std::cv_status::timeout
* otherwise.
*/
template <class Rep, class Period>
std::cv_status wait_for(const std::chrono::duration<Rep, Period>& timeout_duration);
/**
* CallbackBase::on_finish binds a function to the callback object. This
* bound function will be executed when CallbackBase::notify is called,
* before any calls to wait* return. (Note that CallbackBase::wait_for can
* return std::cv_status::timeout before CallbackBase::notify is called for
* the first time, and hence before the bound function is executed.)
*
* The bound function must not synchronize with or otherwise access the
* callback object it is bound to, as this could cause a deadlock.
*
* CallbackBase::on_finish can be called at most once on a given callback
* object, and the call to CallbackBase::on_finish must finish before
* CallbackBase::notify is called.
*
* @param post_work Function to be invoked the first time
* CallbackBase::notify is called. Must have a target --
* i.e., must not compare equal to nullptr. post_work
* returns true if it successfully completes, false if it
* fails.
* @return bool True if the function was successfully bound, false if
* unsuccessful.
*
* TODO: Why does the return value of the callback matter?
*/
bool on_finish(std::function<bool(void)> post_work);
/**
* CallbackBase::bind_thread binds a thread to the event for later use by
* CallbackBase::join_thread.
*
* The thread must be passed using std::move.
*
* Once a thread is bound with CallbackBase::bind_thread, the client code
* should ensure that one of the following occurs before the event is
* destroyed:
* - CallbackBase::join_thread has been called.
* - CallbackBase::wait has been called.
* - CallbackBase::wait_for has been called and returned other than
* std::cv_status::no_timeout.
*
* The bound thread shall not call any CallbackBase method with the
* exception of CallbackBase::notify, which it must call when the thread has
* finished its computation.
*
* CallbackBase::bind_thread can be called at most once on a given callback
* object.
*
* @param asyncThread Thread to be bound to the callback object. The thread
* object must represent a thread of execution -- i.e.,
* asyncThread.joinable() must be true.
* @return bool True if successful, false if thread was not properly bound.
*/
bool bind_thread(std::thread&& asyncThread);
/**
* CallbackBase::join_thread ensures that the thread (if any) bound to this
* event with CallbackBase::bind_thread has fully finished and cleaned its
* resources. It is legal to call this function multiple times, concurrently
* or sequentially.
*/
void join_thread();
protected:
/**
* CallbackBase::notify enables all prior and future wait* calls on the
* callback object to proceed. The call to CallbackBase::notify happens
* before any wait* calls on this callback object return (except in the case
* of wait_for timing out). The asynchronous call the callback object is
* paired with must ensure that any update to state that should be visible
* to the caller of wait* happens before the call to CallbackBase::notify.
*
* CallbackBase::notify must be called exactly once on a given callback
* object.
*/
void notify();
private:
// Same as CallbackBase::join_thread but assumes we already hold a lock on
// mMutex.
void join_thread_locked();
bool mNotified;
std::mutex mMutex;
std::condition_variable mCondition;
std::function<bool(void)> mPostWork;
std::thread mThread;
};
/** /**
* The PreparedModelCallback class is used to receive the error status of * The PreparedModelCallback class is used to receive the error status of
* preparing a model as well as the prepared model from a task executing * preparing a model as well as the prepared model from a task executing
* asynchronously with respect to the runtime. If a calling thread calls wait* * asynchronously with respect to the runtime. If a calling thread calls wait
* or get* on a PreparedModelCallback object and the corresponding asynchronous * or get* on a PreparedModelCallback object and the corresponding asynchronous
* task has not finished preparing the model, the calling thread will block * task has not finished preparing the model, the calling thread will block
* until the asynchronous task has either called notify or notify_1_2. For more * until the asynchronous task has either called notify or notify_1_2.
* information on the synchronization behavior, refer to the CallbackBase class.
* *
* This class inherits the basic blocking and signaling calls from * If the callback object is notified more than once, only the results of the
* CallbackBase, and implements the HIDL notify and notify_1_2 calls from * first call to notify* are used, and the results from subsequent calls are
* IPreparedModelCallback. This callback object is passed as an argument to * discarded.
* IDevice::prepareModel. *
* This callback object is passed as an argument to IDevice::prepareModel*.
*/ */
class PreparedModelCallback : public CallbackBase, public IPreparedModelCallback { class PreparedModelCallback : public IPreparedModelCallback {
public: public:
PreparedModelCallback();
~PreparedModelCallback() override;
/** /**
* IPreparedModelCallback::notify and IPreparedModelCallback::notify_1_2 * IPreparedModelCallback::notify marks the callback object with the return
* mark the callback object with the return status of the asynchronous * status of the asynchronous model preparation along with the prepared
* model preparation along with the prepared model, and call * model, and allows all prior and future wait calls on the
* CallbackBase::notify, enabling all prior and future wait* calls on the * PreparedModelCallback object to proceed.
* PreparedModelCallback object to proceed. For more information on the
* synchronization behavior, refer to the CallbackBase class.
* *
* Either IPreparedModelCallback::notify or IPreparedModelCallback::notify_1_2 * Either IPreparedModelCallback::notify or
* must be called exactly once on a given PreparedModelCallback object. * IPreparedModelCallback::notify_1_2 must be called on a given
* PreparedModelCallback object.
*
* If the callback object is notified more than once, only the results of
* the first call to notify* are used, and the results from subsequent calls
* are discarded.
* *
* @param status Error status returned from asynchronously preparing the * @param status Error status returned from asynchronously preparing the
* model; will be: * model; will be:
* - NONE if the asynchronous preparation was successful * - NONE if the asynchronous preparation was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - INVALID_ARGUMENT if the input model is invalid * - INVALID_ARGUMENT if the input model is invalid
* @param preparedModel Returned model that has been prepared for execution, * @param preparedModel Returned model that has been prepared for execution,
* nullptr if the model was unable to be prepared. * nullptr if the model was unable to be prepared.
*/ */
Return<void> notify(ErrorStatus status, const sp<V1_0::IPreparedModel>& preparedModel) override; Return<void> notify(ErrorStatus status, const sp<V1_0::IPreparedModel>& preparedModel) override;
/**
* IPreparedModelCallback::notify_1_2 marks the callback object with the
* return status of the asynchronous model preparation along with the
* prepared model, and allows all prior and future wait calls on the
* PreparedModelCallback object to proceed.
*
* Either IPreparedModelCallback::notify or
* IPreparedModelCallback::notify_1_2 must be called on a given
* PreparedModelCallback object.
*
* If the callback object is notified more than once, only the results of
* the first call to notify* are used, and the results from subsequent calls
* are discarded.
*
* @param status Error status returned from asynchronously preparing the
* model; will be:
* - NONE if the asynchronous preparation was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error
* - INVALID_ARGUMENT if the input model is invalid
* @param preparedModel Returned model that has been prepared for execution,
* nullptr if the model was unable to be prepared.
*/
Return<void> notify_1_2(ErrorStatus status, Return<void> notify_1_2(ErrorStatus status,
const sp<V1_2::IPreparedModel>& preparedModel) override; const sp<V1_2::IPreparedModel>& preparedModel) override;
/**
* PreparedModelCallback::wait blocks until notify* has been called on the
* callback object.
*/
void wait() const;
/** /**
* Retrieves the error status returned from the asynchronous task launched * Retrieves the error status returned from the asynchronous task launched
* by IDevice::prepareModel. If IDevice::prepareModel has not finished * by IDevice::prepareModel*. If IDevice::prepareModel* has not finished
* asynchronously preparing the model, this call will block until the * asynchronously preparing the model, this call will block until the
* asynchronous task notifies the object. * asynchronous task notifies the object.
* *
* @return status Error status returned from asynchronously preparing the * @return status Error status returned from asynchronously preparing the
* model; will be: * model; will be:
* - NONE if the asynchronous preparation was successful * - NONE if the asynchronous preparation was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - INVALID_ARGUMENT if the input model is invalid * - INVALID_ARGUMENT if the input model is invalid
*/ */
ErrorStatus getStatus(); ErrorStatus getStatus() const;
/** /**
* Retrieves the model that has been prepared for execution from the * Retrieves the model that has been prepared for execution from the
* asynchronous task launched by IDevice::prepareModel. If * asynchronous task launched by IDevice::prepareModel*. If
* IDevice::prepareModel has not finished asynchronously preparing the * IDevice::prepareModel* has not finished asynchronously preparing the
* model, this call will block until the asynchronous task notifies the * model, this call will block until the asynchronous task notifies the
* object. * object.
* *
* @return preparedModel Returned model that has been prepared for * @return preparedModel Returned model that has been prepared for
* execution, nullptr if the model was unable to be * execution, nullptr if the model was unable to be prepared.
* prepared.
*/ */
sp<V1_0::IPreparedModel> getPreparedModel(); sp<V1_0::IPreparedModel> getPreparedModel() const;
private: private:
ErrorStatus mErrorStatus; mutable std::mutex mMutex;
mutable std::condition_variable mCondition;
bool mNotified GUARDED_BY(mMutex) = false;
ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE;
sp<V1_0::IPreparedModel> mPreparedModel; sp<V1_0::IPreparedModel> mPreparedModel;
}; };
/** /**
* The ExecutionCallback class is used to receive the error status of the * The ExecutionCallback class is used to receive the results of the execution
* execution from a task executing asynchronously with respect to the runtime. * from a task executing asynchronously with respect to the runtime. If a
* If a calling thread calls wait* or get* on a PreparedModelCallback object and * calling thread calls wait or get* on a ExecutionCallback object and the
* the corresponding asynchronous task has not finished the execution, the * corresponding asynchronous task has not finished the execution, the calling
* calling thread will block until the asynchronous task has either called notify * thread will block until the asynchronous task has either called notify or
* or notify_1_2. For more information on the synchronization behavior, refer to * notify_1_2.
* the CallbackBase class.
* *
* This class inherits the basic blocking and signaling calls from * If the callback object is notified more than once, only the results of the
* CallbackBase, and implements the HIDL notify and notify_1_2 calls from * first call to notify* are used, and the results from subsequent calls are
* IExecutionCallback. This callback object is passed as an argument to * discarded.
* IPreparedModel::execute. *
* This callback object is passed as an argument to IPreparedModel::execute*.
*/ */
class ExecutionCallback : public CallbackBase, public IExecutionCallback { class ExecutionCallback : public IExecutionCallback {
public: public:
ExecutionCallback();
~ExecutionCallback() override;
/** /**
* IExecutionCallback::notify and IExecutionCallback::notify_1_2 mark the * IExecutionCallback::notify marks the callback object with the return
* callback object with the return status of the asynchronous execution that * status of the asynchronous execution that held this callback and enables
* held this callback and enable all prior and future wait* calls on the * all prior and future wait calls on the ExecutionCallback object to
* ExecutionCallback object to proceed. For more information on the * proceed.
* synchronization behavior, refer to the CallbackBase class.
* *
* Either IExecutionCallback::notify or IExecutionCallback::notify_1_2 must * Either IExecutionCallback::notify or IExecutionCallback::notify_1_2 must
* be called exactly once on a given ExecutionCallback object. * be called on a given ExecutionCallback object.
*
* If the callback object is notified more than once, only the results of
* the first call to notify* are used, and the results from subsequent calls
* are discarded.
* *
* @param status Error status returned from launching the asynchronous task * @param status Error status returned from launching the asynchronous task
* (if the launch fails) or from the asynchronous task itself * (if the launch fails) or from the asynchronous task itself (if the
* (if the launch succeeds). Must be: * launch succeeds). Must be:
* - NONE if the asynchronous execution was successful * - NONE if the asynchronous execution was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if there is an unspecified error * - GENERAL_FAILURE if there is an unspecified error
* - OUTPUT_INSUFFICIENT_SIZE if provided output buffer is * - OUTPUT_INSUFFICIENT_SIZE if provided output buffer is not large
* not large enough to store the resultant values * enough to store the resultant values
* - INVALID_ARGUMENT if the input request is invalid * - INVALID_ARGUMENT if the input request is invalid
*/ */
Return<void> notify(ErrorStatus status) override; Return<void> notify(ErrorStatus status) override;
/** /**
* Similar to IExecutionCallback::notify, but for V1_2::IPreparedModel to * IExecutionCallback::notify_1_2 marks the callback object with the results
* also notify output shapes along with error status. * (error status, dynamic output shapes, and timing information) of the
* asynchronous execution that held this callback and enables all prior and
* future wait calls on the ExecutionCallback object to proceed.
*
* Either IExecutionCallback::notify or IExecutionCallback::notify_1_2 must
* be called on a given ExecutionCallback object.
*
* If the callback object is notified more than once, only the results of
* the first call to notify* are used, and the results from subsequent calls
* are discarded.
* *
* @param status Error status returned from launching the asynchronous task * @param status Error status returned from launching the asynchronous task
* (if the launch fails) or from the asynchronous task itself * (if the launch fails) or from the asynchronous task itself (if the
* (if the launch succeeds). Must be: * launch succeeds). Must be:
* - NONE if the asynchronous execution was successful * - NONE if the asynchronous execution was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if the asynchronous task resulted in an * - GENERAL_FAILURE if the asynchronous task resulted in an unspecified
* unspecified error * error
* - OUTPUT_INSUFFICIENT_SIZE if at least one output * - OUTPUT_INSUFFICIENT_SIZE if at least one output operand buffer is
* operand buffer is not large enough to store the * not large enough to store the corresponding output
* corresponding output * - INVALID_ARGUMENT if one of the input arguments to prepareModel is
* - INVALID_ARGUMENT if one of the input arguments to * invalid
* prepareModel is invalid
* @param outputShapes A list of shape information of model output operands. * @param outputShapes A list of shape information of model output operands.
* The index into "outputShapes" corresponds to the index * The index into "outputShapes" corresponds to the index of the output
* of the output operand in the Request outputs vector. * operand in the Request outputs vector. outputShapes must be empty
* outputShapes must be empty unless the status is either * unless the status is either NONE or OUTPUT_INSUFFICIENT_SIZE.
* NONE or OUTPUT_INSUFFICIENT_SIZE. * @param Timing Duration of execution. Unless MeasureTiming::YES was passed
* @return Timing Duration of execution. Unless MeasureTiming::YES was passed when * when launching the execution and status is NONE, all times must be
* launching the execution and status is NONE, all times must * reported as UINT64_MAX. A driver may choose to report any time as
* be reported as UINT64_MAX. A driver may choose to report * UINT64_MAX, indicating that particular measurement is not available.
* any time as UINT64_MAX, indicating that particular measurement is
* not available.
*/ */
Return<void> notify_1_2(ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, Return<void> notify_1_2(ErrorStatus status, const hidl_vec<OutputShape>& outputShapes,
const Timing& timing) override; const Timing& timing) override;
@@ -320,82 +239,88 @@ class ExecutionCallback : public CallbackBase, public IExecutionCallback {
return notify_1_2(status, outputShapes, timing); return notify_1_2(status, outputShapes, timing);
} }
/**
* ExecutionCallback::wait blocks until notify* has been called on the
* callback object.
*/
void wait() const;
/** /**
* Retrieves the error status returned from the asynchronous task launched * Retrieves the error status returned from the asynchronous task launched
* by either IPreparedModel::execute or IPreparedModel::execute_1_2. If * by either IPreparedModel::execute or IPreparedModel::execute_1_2. If
* IPreparedModel::execute or IPreparedModel::execute_1_2 has not finished * IPreparedModel::execute or IPreparedModel::execute_1_2 has not finished
* asynchronously executing, this call will block until the asynchronous task * asynchronously executing, this call will block until the asynchronous
* notifies the object. * task notifies the object.
* *
* @return status Error status returned from launching the asynchronous task * @return status Error status returned from launching the asynchronous task
* (if the launch fails) or from the asynchronous task itself * (if the launch fails) or from the asynchronous task itself (if the
* (if the launch succeeds). Must be: * launch succeeds). Must be:
* - NONE if the asynchronous execution was successful * - NONE if the asynchronous execution was successful
* - DEVICE_UNAVAILABLE if driver is offline or busy * - DEVICE_UNAVAILABLE if driver is offline or busy
* - GENERAL_FAILURE if the asynchronous task resulted in an * - GENERAL_FAILURE if the asynchronous task resulted in an unspecified
* unspecified error * error
* - OUTPUT_INSUFFICIENT_SIZE if at least one output * - OUTPUT_INSUFFICIENT_SIZE if at least one output operand buffer is
* operand buffer is not large enough to store the * not large enough to store the corresponding output
* corresponding output * - INVALID_ARGUMENT if one of the input arguments to prepareModel is
* - INVALID_ARGUMENT if one of the input arguments to * invalid
* prepareModel is invalid
*/ */
ErrorStatus getStatus(); ErrorStatus getStatus() const;
/** /**
* Retrieves the output shapes returned from the asynchronous task launched * Retrieves the output shapes returned from the asynchronous task launched
* by IPreparedModel::execute_1_2. If IPreparedModel::execute_1_2 has not finished * by IPreparedModel::execute_1_2. If IPreparedModel::execute_1_2 has not
* asynchronously executing, this call will block until the asynchronous task * finished asynchronously executing, this call will block until the
* notifies the object. * asynchronous task notifies the object.
* *
* If the asynchronous task was launched by IPreparedModel::execute, an empty vector * If the asynchronous task was launched by IPreparedModel::execute, an
* will be returned. * empty vector will be returned.
* *
* @return outputShapes A list of shape information of model output operands. * @return outputShapes A list of shape information of model output
* The index into "outputShapes" corresponds to the index * operands. The index into "outputShapes" corresponds to the index of
* of the output operand in the Request outputs vector. * the output operand in the Request outputs vector. outputShapes must
* outputShapes must be empty unless the status is either * be empty unless the status is either NONE or
* NONE or OUTPUT_INSUFFICIENT_SIZE. * OUTPUT_INSUFFICIENT_SIZE. outputShaps may be empty if the status is
* NONE and all model output operands are fully-specified at execution
* time. outputShapes must have the same number of elements as the
* number of model output operands if the status is
* OUTPUT_INSUFFICIENT_SIZE, or if the status is NONE and the model has
* at least one output operand that is not fully-specified.
*/ */
const std::vector<OutputShape>& getOutputShapes(); const std::vector<OutputShape>& getOutputShapes() const;
/** /**
* Retrieves the duration of execution ofthe asynchronous task launched * Retrieves the duration of execution of the asynchronous task launched by
* by IPreparedModel::execute_1_2. If IPreparedModel::execute_1_2 has not finished * IPreparedModel::execute_1_2. If IPreparedModel::execute_1_2 has not
* asynchronously executing, this call will block until the asynchronous task * finished asynchronously executing, this call will block until the
* notifies the object. * asynchronous task notifies the object.
* *
* If the asynchronous task was launched by IPreparedModel::execute, every time * If the asynchronous task was launched by IPreparedModel::execute, every
* must be UINT64_MAX. * time must be UINT64_MAX.
* *
* @return timing Duration of the execution. Every time must be UINT64_MAX unless * @return timing Duration of the execution. Every time must be UINT64_MAX
* the status is NONE. * unless the status is NONE.
*/ */
Timing getTiming(); Timing getTiming() const;
private: private:
/*
* ExecutionCallback::notifyInternal stores the results of the execution
* (status, output shapes, and timing information) in the ExecutionCallback
* object before any call to wait or get* return. It then enables all prior
* and future wait calls on the ExecutionCallback object to proceed.
*/
void notifyInternal(ErrorStatus errorStatus, const hidl_vec<OutputShape>& outputShapes,
const Timing& timing);
// members
mutable std::mutex mMutex;
mutable std::condition_variable mCondition;
bool mNotified GUARDED_BY(mMutex) = false;
ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE; ErrorStatus mErrorStatus = ErrorStatus::GENERAL_FAILURE;
std::vector<OutputShape> mOutputShapes = {}; std::vector<OutputShape> mOutputShapes = {};
Timing mTiming = {}; Timing mTiming = {};
}; };
// template function implementation(s) below this point } // namespace android::hardware::neuralnetworks::V1_2::implementation
template <class Rep, class Period>
std::cv_status CallbackBase::wait_for(const std::chrono::duration<Rep, Period>& timeout_duration) {
std::unique_lock<std::mutex> lock(mMutex);
std::cv_status status =
mCondition.wait_for(lock, timeout_duration, [this] { return mNotified; });
if (status != std::cv_status::timeout) {
join_thread_locked();
}
return status;
}
} // namespace implementation
} // namespace V1_2
} // namespace neuralnetworks
} // namespace hardware
} // namespace android
#endif // ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H #endif // ANDROID_HARDWARE_NEURALNETWORKS_V1_2_CALLBACKS_H