Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

MXNet Amalgamation iOS compilation #1914

Closed
RishabGargeya opened this issue Apr 20, 2016 · 7 comments
Closed

MXNet Amalgamation iOS compilation #1914

RishabGargeya opened this issue Apr 20, 2016 · 7 comments

Comments

@RishabGargeya
Copy link

Hi all,

I am using MXNet amalgamation for prediction on iOS and am running into an issue -- here is the situation:
My goal is to build a prediction model for iOS using the c_predict_api.h and mxnet_predict-all.cc files downloaded from the demo: https://github.com/pppoe/WhatsThis-iOS/tree/master/WhatIsThis/MXNet

When building the project in xcode, I am able to successfully load a trained mxnet model and predict when building for the iPhone 6, but when building for the iPhone 5, I am getting this error:

Undefined symbols for architecture i386:
  "__tlv_bootstrap", referenced from:
      mxnet::common::ThreadLocalStore<ErrorEntry>::Get()::ptr in mxnet_predict-all.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am kind of confused as to why this error would show up when building for certain devices and not for others. Any ideas?

Thanks so much for your help!

@Piyush3dB
Copy link
Contributor

Are you building for the simulator or hardware targets? Have you managed to get it working on the simulator to begin with?

@RishabGargeya
Copy link
Author

@Piyush3dB Thanks for the reply! I can successfully build for the iPhone 6 simulator. However, building for the iPhone 5 simulators and below give me the aforementioned error. I get the same error when trying a hardware install on an iPhone 5.

@Piyush3dB
Copy link
Contributor

Ok, looks like an llvm issue. We need to establish what the differences are.

Are you able to post the commands Xcode uses during linking for the iPhone 5 and 6 simulators?

Fortunately when I tried it I didn't run into this issue as I was targeting the iPhone 6. I'll give it a go with other devices soon.

@RishabGargeya
Copy link
Author

Would this be it? I'm copying this from the "View -> Show Report Navigator -> Build" page, under the Link dropdown menu.

iPhone 5:

Ld /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator/emr.app/emr normal i386
    cd /Users/rishabgargeya/Desktop/test/EMR2/emr_test
    export IPHONEOS_DEPLOYMENT_TARGET=9.2
    export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk -L/Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator -L/Users/rishabgargeya/Desktop/test/EMR2/emr_test/opencv2.framework -L/Users/rishabgargeya/Desktop/test/EMR2/emr_test/opencv2.framework/Versions/A -F/Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test/build/Debug-iphoneos -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test/emr_test -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test -filelist /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/i386/emr.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -rpath -Xlinker @executable_path/Frameworks -mios-simulator-version-min=9.2 -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -framework Agrume -framework Alamofire -framework Haneke -framework IQKeyboardManagerSwift -framework ImageViewer -framework PureLayout -framework SQLite -framework SwiftyButton -framework SwiftyDropbox -framework SwiftyJSON -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator -Xlinker -add_ast_path -Xlinker /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/i386/emr.swiftmodule -framework Foundation -framework CoreImage -framework opencv2 -framework AssetsLibrary -framework AVFoundation -framework CoreGraphics -framework Accelerate -framework Pods -Xlinker -dependency_info -Xlinker /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/i386/emr_dependency_info.dat -o /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator/emr.app/emr

iPhone 6:

Ld /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator/emr.app/emr normal x86_64
    cd /Users/rishabgargeya/Desktop/test/EMR2/emr_test
    export IPHONEOS_DEPLOYMENT_TARGET=9.2
    export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk -L/Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator -L/Users/rishabgargeya/Desktop/test/EMR2/emr_test/opencv2.framework -L/Users/rishabgargeya/Desktop/test/EMR2/emr_test/opencv2.framework/Versions/A -F/Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test/build/Debug-iphoneos -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test/emr_test -F/Users/rishabgargeya/Desktop/test/EMR2/emr_test -filelist /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/x86_64/emr.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -rpath -Xlinker @executable_path/Frameworks -mios-simulator-version-min=9.2 -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -framework Agrume -framework Alamofire -framework Haneke -framework IQKeyboardManagerSwift -framework ImageViewer -framework PureLayout -framework SQLite -framework SwiftyButton -framework SwiftyDropbox -framework SwiftyJSON -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator -Xlinker -add_ast_path -Xlinker /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/x86_64/emr.swiftmodule -framework Foundation -framework CoreImage -framework opencv2 -framework AssetsLibrary -framework AVFoundation -framework CoreGraphics -framework Accelerate -framework Pods -Xlinker -dependency_info -Xlinker /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Intermediates/EMR.build/Debug-iphonesimulator/emr.build/Objects-normal/x86_64/emr_dependency_info.dat -o /Users/rishabgargeya/Library/Developer/Xcode/DerivedData/EMR-fcfhyiqcnpscotepubgkbokrncab/Build/Products/Debug-iphonesimulator/emr.app/emr

The difference seems to be i386 vs x86_64 during compilation for iPhone 5 and iPhone 6 respectively.

@Piyush3dB
Copy link
Contributor

This might be a can of worms to debug. Anyway to get you going I propose the following:

The the bottom of mxnet_predict-all.cc you have the definition of the function MXGetLastError() where the error appears. This function doesn't get used during MXNet predict so it is safe to comment out the offending line like so

const char *MXGetLastError() {
//return MXAPIErrorStore::Get()->last_error.c_str();
return "thisIsAHackJob lol";
}

Thereafter you should be able to build and run without any problems. Does this work? I'll try and give it a go myself later today.

@Piyush3dB
Copy link
Contributor

@RishabGargeya FYI I'm able to build and run the app on iPhone 5 simulator by making the following mods to mxnet_predict-all.cc (at the bottom of the file).

//typedef mxnet::common::ThreadLocalStore MXAPIErrorStore;
const char MXGetLastError() {
//return MXAPIErrorStore::Get()->last_error.c_str();
return "hack job ";
}
void MXAPISetLastError(const char
msg) {
//MXAPIErrorStore::Get()->last_error = msg;
(void) msg;
}

Hopefully this solves your issue?

@RishabGargeya
Copy link
Author

@Piyush3dB Thank you so much! This workaround fixes the issues I have been having.

Here are the changes I have made in total to the amalgamation file (mxnet_predict-all.cc) found here:
https://github.com/pppoe/WhatsThis-iOS/tree/master/WhatIsThis/MXNet
to get it working locally, in case others have these problems as well.

define MX_TREAD_LOCAL __declspec(thread)
#ifndef MX_TREAD_LOCAL
#message("Warning: Threadlocal is not enabled");
#endif

change to:

//#define MX_TREAD_LOCAL __declspec(thread)
#define MX_TREAD_LOCAL __thread
#ifndef MX_TREAD_LOCAL
#message("Warning: Threadlocal is not enabled");
#endif

as well --

//===== EXPANDED: ../src/common/thread_local.h =====


struct ErrorEntry {
  std::string last_error;
};

typedef mxnet::common::ThreadLocalStore<ErrorEntry> MXAPIErrorStore;

const char *MXGetLastError() {
  return MXAPIErrorStore::Get()->last_error.c_str();
}

void MXAPISetLastError(const char* msg) {
  MXAPIErrorStore::Get()->last_error = msg;
}
//===== EXPANDED: ../src/c_api/c_api_error.cc =====

change to:

//===== EXPANDED: ../src/common/thread_local.h =====


struct ErrorEntry {
  std::string last_error;
};

//typedef mxnet::common::ThreadLocalStore<ErrorEntry> MXAPIErrorStore;

const char *MXGetLastError() {
//  return MXAPIErrorStore::Get()->last_error.c_str();
    return "temp";
}

void MXAPISetLastError(const char* msg) {
  //MXAPIErrorStore::Get()->last_error = msg;
}
//===== EXPANDED: ../src/c_api/c_api_error.cc =====

Once again, thanks for all the help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants