kosslab-kr / Tizen-NN-Runtime

Apache License 2.0
6 stars 2 forks source link

tf-lite 모델의 NN API 호출 과정 #13

Open mojunsang26 opened 5 years ago

mojunsang26 commented 5 years ago

nnfw/docs/howto.md를 참고하시면 tflite 모델이 NN API 호출하는 과정을 출력할 수 있습니다. https://developer.android.com/ndk/guides/neuralnetworks/ 위의 문서와 함께 보시면 어렵지 않게 NN API의 작동 방식을 이해할 수 있을 것 같습니다.

문제는 Xor.tflite의 구조를 알 수 없으니 tensorflow 모델의 코드와 NN API를 매칭시키기 어렵습니다. 따라서 nnfw runtimes의 frontend부터 backend까지의 호출 과정 외에 tensorflow의 frontend부터 NN API호출까지 어떻게 이루어지는지 공부가 필요할 것 같습니다.

yuntk commented 5 years ago

추가로 오드로이드에서 직접 Xor 샘플앱과 tensorflow-lite, nnfw를 빌드하여 실행한 결과 Xor앱이 이상없이 실행이 되었으나 debug용으로 추가한 fprintf 구문이 출력되지 않았습니다.

tensorflow-lite의 경우 gbs로 빌드하면 컴파일 결과가 Active(.a) 파일 형식으로 제공되어 컴파일타임에 Xor 샘플앱과 링킹이 되어지고, nnfw의경우 Shared object(.so) 형식으로 결과물이 나오게 되어 런타임에 링킹이 이루어집니다. lite가 오드로이드에 설치가 안되어있어도 실행이 되는 것은 이상이 없으나, nnfw의 경우 런타임에러가 발생해야 하는데 nnfw package를 삭제하고 Xor를 실행해도 문제가 없이 실행이 되었습니다.

lite에서 nnapi를 부르는 부분을 더 찾아 봐야할 것 같습니다.

timedilation commented 5 years ago

Tensorflow Lite에서 NNapi를 호출하는 과정은 다음과 같습니다.

tensorflow/contrib/lite/interpreter.h

296   // Invoke the interpreter (run the whole graph in dependency order).
301   // Returns status of success or failure.
302   TfLiteStatus Invoke();

tensorflow/contrib/lite/interpreter.cc

513 TfLiteStatus Interpreter::Invoke() {
526       TF_LITE_ENSURE_OK(&context_, nnapi_delegate_->Invoke(this));

tensorflow/contrib/lite/nnapi_delegate.cc

636 TfLiteStatus NNAPIDelegate::Invoke(Interpreter* interpreter) {
680   ANeuralNetworksEvent* event = nullptr;
681   CHECK_NN(ANeuralNetworksExecution_startCompute(execution, &event));
timedilation commented 5 years ago

어플리케이션에서 nnapi를 사용하기 위해서는 해당 라인 과 같은 셋팅이 필요합니다.