Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android binary #321

Open
acristescu opened this issue Sep 25, 2020 · 76 comments
Open

Android binary #321

acristescu opened this issue Sep 25, 2020 · 76 comments

Comments

@acristescu
Copy link

Now that we have a CPU-only version of the engine, would it be possible to add arm7 and/or arm64 to the compiled binary of the release? I couldn't get it going, but then I haven't compiled any C++ since I finished uni many years ago :) It should be possible though as there are several of these floating around for both Leela Zero and SAI (for example https://github.com/Grant-Tao/compiled-leelaz-0.17-for-android-phones and https://github.com/evdwerf/leela-zero/tree/android ).

The second repo above even has the modifications to the Makefile for Android (see this comparison leela-zero/leela-zero@next...evdwerf:android ).

Is that something that could be achieved?

@simonguoxm
Copy link

this will be a great feature for the phone.

@l1t1
Copy link

l1t1 commented Sep 27, 2020

@cryptsport
Copy link

cryptsport commented Sep 27, 2020

@l1t1 I seem to have the wrong site. do you have another link? is this program paid?

@portkata
Copy link

portkata commented Sep 27, 2020

@cryptpark
acristescu will probably have one in a few months, until then we have https://github.com/aki65/aki65.github.io/releases
version .1 and .2 are opencl for qualcomm phones, .4 and .5 are eigen cpu for any phone, tablet or chromebook. Speed is good but i wish the katrain calibrated ai equation could be applied and distributed as a paid app. Everyone is doing great work with katago, it is a dream come true

@acristescu
Copy link
Author

@portkata could you please describe what do you mean by the "calibrated ai equation"? FYI after I posted this, somebody on reddit pointed the aki65 stuff to me and I manage to reverse engineer the binary out of it. It's still not great as the guy did not put the scripts online, just the resulting APK and he or she might not build a newer version. Not having an official build is a big miss IMHO.

I am planning to include the AI into the free app OnlineGo (https://play.google.com/store/apps/details?id=io.zenandroid.onlinego&hl=en). I have solved most technical problems I think, I just need to tweak some things so that it doesn't beat us mere mortals too badly :)

@portkata
Copy link

portkata commented Sep 27, 2020

@acristescu Thank you for your work :)
Sanderbaduk uses an equation to weaken katago on one visit to play at various ranks 15k to low dan, but weaker than the policy. It is explained in this issue thread in his katrain software sanderland/katrain#74 and also referred to in passing here sanderland/katrain#73
Incorporating this into a katago android gui if possible would let users of any rank play an even match against katago. It gives the user the ability to set katago's rank to weaker than 3d (1 visit). Your gui is the best android go experience so far, allowing the user to play against katago at >1500 visits/move, but it is not possible to be competitive against such a strong bot. It is still such great joy to play.

@portkata
Copy link

portkata commented Sep 28, 2020

ahq weiqi pro includes it.

http://www.flygo.net/bbs/forum.php?mod=viewthread&tid=112497&extra=page%3D1&mobile=2

link to download http://aqgo.ezandroid.cn/AQGoPro_1.3.0_2020-09-27.apk
Paypal ($15 US): 397093109@qq.com
activation code arrives in one business day.

@acristescu
Copy link
Author

What?!?! $15 for just integrating the hard work of other people into an android app? That is highway robbery... Now I feel like a schmuck for giving the app for free :)

As for Katrain, yes, I am aware of that (see for example this reddit thread in which both me and @sanderland are discussing it). Thank you for pointing out the equation in the Katrain code, I'm going to give it a honest go at implementing it in the app, but @sanderland put a lot of work and thought into this I'm pretty sure I will not be able to do nearly as good a job as him/her. Perhaps @sanderland could be persuaded to contribute that aspect to my open-source app once I manage to get the integration going? :)

@sanderland
Copy link
Contributor

@acristescu wait until you see things like this - when you pick a license you know what people can do with it, and I can't get worked up about it.
AI code is here and is quite simple overall. KaTrain being based on kivy should be able to run on android, but no luck so far from my end.

@portkata
Copy link

@sanderland would it be very difficult for you to write the code for the calibrated ai in @acristescu's apk if he does the gui and katago implementation? Thanks for the time.

@acristescu
Copy link
Author

acristescu commented Sep 29, 2020

That would be awesome, otherwise I'll have to "git gud" at magic such as this:

elt = [(math.log(random.random()) / (item[1] + 1e-18), item) for item in items]  # magic
    return [e[1] for e in heapq.nlargest(pick_n, elt)]  # NB fine if too small

@sanderland
Copy link
Contributor

well if you ignore the name of the function, which is weighted_selection_without_replacement....
I'm hesitant to get pulled into other projects as my own is already taking far more than I ever imagined. Please try it yourself first, I bet the main work is mapping things onto your own data structures, which I wouldn't know about anyway.

@portkata
Copy link

portkata commented Sep 29, 2020

Maybe the calibrated ai was too much to ask. Does this zip file have all the files necessary to run katago in android in the command line? https://drive.google.com/file/d/12QNGdFx0e0GHfpzB6GFWcsHoq1TRJklg/view?usp=drivesdk
what files are absolutely necessary to play a game? Like do i only need the katago file, gtp.cfg, and the tensorflow file, or just the first two, etc? Thanks!

@acristescu
Copy link
Author

acristescu commented Sep 29, 2020

@portkata all that you need is available in the APKs you've initially posted ( https://github.com/aki65/aki65.github.io/releases ). You just have to unzip the APK, then locate a file called private.mp3 then rename that as .tar.gz then unzip that, then find the katago executable. What you need are the binary (which is the one that this issue asks for as an official release) + any net + an appropriate cfg file. I have all that and I am now migrating my app from using GTP to using the katago JSON analysis engine (which IMHO is a lot nicer). I should have that done shortly, and then I can release a version without scaling. I expect that to play at 3-4d. Once that is done, I can, in a second iteration, try to reverse engineer @sanderland 's work (for which I am grateful, please don't misinterpret my "magic" comment as dismissive).

That being said, I still think the initial request of this issue is a valid one: a katago binary that I don't have to basically steal from somebody that is ripping people off with $15 price tags would be great (but beyond my skill).

@portkata
Copy link

portkata commented Sep 29, 2020

(edited) Thanks for all the detailed replies and hard work towards this. Sorry, probably stupid, but just one last question. So if I'm understanding correctly the 3 files in the zip file attached below
64-bit_katago_android.zip
32bit_katago_android.zip
(android binary, 10b net, and gtp.cfg 14mb) are the only ones needed for katago on android command line on any phone? no dll file equivalents are needed? Thanks so much!

@acristescu
Copy link
Author

I'm not sure where that zip file is coming from, but the binary in the aki65 repository above does not need any .so files (the equivalent of DLL). It just worked on my S9+ with Android Q.

@portkata
Copy link

portkata commented Sep 29, 2020

That being said, I still think the initial request of this issue is a valid one: a katago binary that I don't have to basically steal from somebody that is ripping people off with $15 price tags would be great (but beyond my skill).

Maybe @lightvector can ask akigo how he compiled the android binary? Akigo is fluent in English. Then if lightvector puts it on his github page, the community can update it if needed in the future if the run restarts.

edit: wow, there is a new update by akigo to badukai with the calibrated ai included. You guys are geniuses. His calibrated rank is based on Sanderland's algorithm but better in that he somehow gets in to go all the way up to 9d. I'm guessing you have to use the 15b net? He must have found a way to use multiple playouts as well. His program has definitely moved the katago project forward in a great way.

@aki65
Copy link

aki65 commented Sep 29, 2020

From issue #74 in KaTrain it seemed to me, that the calibration was recently adapted to 20b, but I may be wrong. Surely, @sanderland can clarify here.

And no, I did not change anything from KaTrain's algorithm. I just allowed entering up to 9-dan for experimental purposes to see how the formulas behave when the rank is extended beyond 4-dan. Should I cut this off ?

@acristescu
Copy link
Author

Hi @aki65 and welcome to the party. I think the issue here is not with Sanderland's formula (although we got a bit side-tracked here), the request was to basically integrate whatever you did to create an android binary into the main release so that there is an official android binary. Is that something that can be achieved?

@portkata
Copy link

portkata commented Sep 30, 2020

@akigo I had the impression it was based on the 20b too, but he said 15b when asked a couple days ago on reddit, so not 100% sure. Either will give you good results. The policy network at full strength is roughly 4 dan or so, so higher numbers, won't do much, as the algorithm is meant to weaken the policy. A better thing would be to set all the integers above his highest "kyu-rank" value to not use the algorithm but instead run katago using only 1 thread and x number for "maxPlayouts" in the config file instead of maxvisits. If it was me, I'd set it based on the 20b (which does work well with his algorithm as well) and do 6 playouts for the 4 dan equivalent kyu_rank, 8 for 5 dan, 10 for 6 dan, 12 for 7 dan, 14 for 8 dan, and 16 for 9 dan. Using 1 thread only and maxPlayouts ensures the same strength on any hardware. I know 1 thread 16 playouts can easily beat the gtp4zen 9d zen7 setting. Thanks to everyone for all these great projects, it's so exciting!
Even with no further changes, as it stands bringing sanderland's equation to android for free is a great acheivement!

Lastly if it is too much work to alter the kyu_rank but you still want to change it to include the high dan ranks, a simpler adjustment might be to just add 3 boxes to adjust maxPlayouts, maxVisits, and numSearchThreads. That would give the end user very precise control of high dan strength. Or if there is a way to place the config file in a location where that the user has access to it would accomplish the same thing.

@sanderland
Copy link
Contributor

sanderland commented Sep 30, 2020

From issue #74 in KaTrain it seemed to me, that the calibration was recently adapted to 20b, but I may be wrong. Surely, @sanderland can clarify here.

it's still on 15b since that's the default, but the strengths are approximate anyway and they don't differ that much between 15b and 20b due to the weirdness of the algorithm.

And no, I did not change anything from KaTrain's algorithm. I just allowed entering up to 9-dan for experimental purposes to see how the formulas behave when the rank is extended beyond 4-dan. Should I cut this off ?

It doesn't make much sense, as it converges to pure policy-based play for strength to infinity, which is around ogs 5 dan. At these levels the lack of reading becomes clearer as well, and players get a bit annoyed at how the AI seems to die.

I'm working on some new ais which may fill this gap as well (see https://online-go.com/player/767655/ and #322)

@acristescu
Copy link
Author

acristescu commented Sep 30, 2020

Since we're on the subject, which net should I bake into the app? So far I've been using the 10b one (g170e-b10c128-s1141046784-d204142634.bin) and it's reasonably fast, however it seems to me weaker than LeelaZero (15b-270 0c4ade) running on the same phone. It's kinda hard to say since I am about 10k and both of them beat me to a pulp, but Kata does make some weird moves such as putting both his first two moves in the same corner to enclose instead of grabbing an empty one.

So the bottom line is, which net should I choose, keeping in mind that APK size is a concern (these nets are bigger than my entire APK!!!)?

@sanderland
Copy link
Contributor

sanderland commented Sep 30, 2020

The 6b and 10b are really stepping stones in early phase training and not so suitable to distribute, the 15b went a lot longer, and the 20b/30b/40b to the end.
I use 15b by default for size concerns, it's superhuman, reasonably fast and decent at play and analysis. When analysing on desktop I use 20b myself, and I've barely touched the other nets.

@acristescu
Copy link
Author

acristescu commented Sep 30, 2020

Thank you, 15b it is...

Is there a way to force kata to accept a model file that is not ending with .gz? The way Android resources are packed in an APK destroys archives, and kata is just throwing a fit if I rename the file:

terminating with uncaught exception of type StringError: Error loading or parsing model file katago.net.so: Model file should end with .txt, .bin, .txt.gz, .bin.gz, or possibly just .gz. (If it doesn't
 have one of these extensions already, it's probably the wrong file, renaming will probably NOT help).

@portkata
Copy link

portkata commented Sep 30, 2020

would it be possible for you to be able to package the .gz net file as contained in a folder inside of an archive with an extension you change to .mp3, then have the .mp3 file extension be changed and unpacked upon installation of the apk? Like private.mp3? May be a crazy idea...

@acristescu
Copy link
Author

I don't have write permissions in that folder so I can't rename it (crazy Android 10 restrictions). I can copy them manually to another temp folder under the right name if they don't already exist... That's the workaround I'm doing right now, but it's a hassle, I was hoping there's a -model-i-know-what-im-doing-just-take-the-bloody-file parameter :)

@sanderland
Copy link
Contributor

@acristescu : there is not, since it needs the extension to figure out what to do with the file. See desc.cpp
Easy enough to edit in an extra extension if you're compiling it though.

@lightvector
Copy link
Owner

@acristescu
https://github.com/lightvector/KataGo/blob/master/cpp/neuralnet/desc.cpp#L1111

Take a look at the code starting at that line and below. KataGo uses the extension to figure out whether it should try to load the file as compressed or uncompressed, and what format it is, because there are multiple possible formats that it tries to accept, for legacy reasons. As Sander said, if you're already compiling it yourself, a quick workaround would just be to edit the code here a little to just make it so that the extension that you want to use falls into the proper case instead of hitting the else at the very end that reports the failure.

@aki65
Copy link

aki65 commented Sep 30, 2020

@acristescu

... the request was to basically integrate whatever you did to create an android binary into the main release so that there is an official android binary. Is that something that can be achieved?

I think so, but there is still work to be done. Keep in mind that my goal was just to create a working binary within my personal development environment in order to get BadukAI up and running. Therefore I had no qualms to spread machine-dependent settings and paths all over my scripts. This has to be factored out as far as possible before sharing them makes any sense. Furthermore, I just made every code syntax change in KataGo that was necessary to make the android cross-compiler happy. Some of these changes will probably break builds for other platforms. So I have to refine this (probably using some #ifdef directives) before the code changes can be merged into the KataGo repository.
All this is on my TODO list, but I am very busy at the moment with my day job and feature requests for BadukAI, so it will take some time ...

@acristescu
Copy link
Author

acristescu commented Oct 22, 2020

Is there a new version of the android binary please? I spent way too much time figuring out that the parameter includeMovesOwnership that I'm trying to use has only been added 24 days ago, meaning after @aki65 compiled his one-time binary...

_Edit: sorry, found it, I was confusing lightvector with aki65 and looking for the release in the wrong place 🤦‍♂️ _

@cryptsport
Copy link

network remains the same, its use is optimized

@acristescu
Copy link
Author

Not quite true, what it looks like he did is converted the model to Google's TensorFlow lite format and somehow made KataGo work with tensorflow. This is quite impressive, as the new format should be both faster (and importantly for mobile) quite compact as well. I'm not sure if @lightvector knows about this, but I think this would be something really cool to include back in the main project.

However, I could not get it to run, probably because the command line parameters are different. @aki65 could you kindly share what the new command lines parameters are (in the spirit of open-source)?

@cryptsport
Copy link

cryptsport commented Dec 7, 2020

unclear. if unpack the apk, then unpack "private.mp3", like a zip, there is a part of the code that completely coincides with the official network g170e-b20c256x2-s5303129600-d1228401921.bin.gz. (7ED1600h arm64-v8a-rel-0.21)

@acristescu
Copy link
Author

The old network is still there, but now there's a new file called 20b.tflite. If you try to just run the katago binary included in 0.21 you also get an error saying that it's missing libtensorflowlite.so. This did not happen with the old 0.16. From this I have deduced that he somehow made katago work with TensorFlow.

@cryptsport
Copy link

this is very interesting, but it doesn't work for me v64. so is there a new network or not? or is it not clear?

@acristescu
Copy link
Author

acristescu commented Dec 7, 2020

only @aki65 can answer these questions for certain.

it doesn't work for me v64

Do you mean LazyBaduk does not work for you or that you cannot run the new katago binary? I can't get it to work either...

@cryptsport
Copy link

only 32bit version works on my devices. for some reason also on the nox emulator (probably there is no 64bit support, I will find out)

@cryptsport
Copy link

do you mean "BadukAI"? and which network? Can this network be pulled out of the apk? how to find?

@cryptsport
Copy link

the sizes of unpacked files "private" v17 and v18 - 198MB and 225MB. little space for another network

@portkata
Copy link

portkata commented Dec 7, 2020

imagine after distributed training when the 40b policy reaches 9 dan and he optimizes that network. The kyu rank mode will be awesome. turning on the opening book, they play well all the way down to kyu rank 10. they go up to kyu rank -8. Cryptpark has tested against crazystone zero, i think it can already beat the 7d setting?

@acristescu
Copy link
Author

All of that is great, too bad is not open source so that other open source projects could use it... :|

@cryptsport
Copy link

@l1t1
Copy link

l1t1 commented Dec 8, 2020

what are the meaning of these files in the apk

10b.bin.gz
20b.bin.gz
15b.tflite
20b.tflite
40b.tflite

@portkata
Copy link

portkata commented Dec 8, 2020

the 15b and 40b are for leela zero, they 20b is for the optimized katago, which is now faster than leela zero, setting numsearchthreads to 1.

@portkata
Copy link

portkata commented Dec 8, 2020

answer @aki65
aki65/aki65.github.io#8 (comment)

to summarize for this thread, aki65 said there should be no change in invoking katago to use the tensorflow optimized net. Can someone run the optimized net in android command line and confirm? This net is really great for calibrated ai because it is so much faster. One can run the kyu rank bots on very old mobile processors now with instant moves; together with his opening book for the first moves, the kyu rank bot plays great and fast from kyu rank 10 and stronger.
aki65's binary w/optimized net, .so, .cfg files:
https://easyupload.io/yza296

@acristescu
Copy link
Author

Can someone run the optimized net in android command line and confirm?

I just tried by unzipping the files provided, using adb push to upload them to an S9+, then doing adb shell and running the binary with LD_LIBRARY_PATH=. ./katago_binary_android. When providing no parameter it does work (prints the usage info), but as soon as I add any parameter (for example LD_LIBRARY_PATH=. ./katago_binary_android version) it stops working. It just exits without any error.

I have spent some 3 hours during the weekend trying every which way, with different versions of the libraries command lines, etc. The same method worked for the old katago, leela zero and SAI. I must be missing something...

@portkata
Copy link

portkata commented Dec 13, 2020

just saw the new icon in your app, it looks great! I still wish katago used half as many playouts though. or maybe have easy, medium and hard settings? Thanks for putting in all the time!

@acristescu
Copy link
Author

Plan A was to put in a scaling mechanism as the one in katrain, but I just can't find the time for such an undertaking.

Plan B was to basically have the app compute the number of playouts per second it gets and then scale the number of playouts for the next moves so as to keep the time per move to 1s on any device. This requires less development, but would make the AI be inconsistent.

Plan C would be to at least temporarily have a global setting in the settings page where you can tweak this to your heart's desire. Might go with this in the next version.

@portkata
Copy link

oh wow this is going to be great, thanks!

@portkata
Copy link

portkata commented Jan 2, 2021

aki65 added support for the new distributed kata1 weights on android. https://github.com/aki65/aki65.github.io/releases

@l1t1
Copy link

l1t1 commented Jan 2, 2021

aki65 added support for the new distributed kata1 weights on android. https://github.com/aki65/aki65.github.io/releases

thanks

@portkata
Copy link

portkata commented Jan 31, 2021

aki65 released optimized s580 40b distributed training weight. It is extremely, extremely fast. https://github.com/aki65/aki65.github.io/releases/tag/v1.4.1
In a quick test, the optimized s580 policy seems to be atleast as strong as the non optimized last 40b s509 net of the non distributed run. 6 games policy (t1 p1 nncache=2) against 20b 5 playouts (t1 p5), the optimized net was 4 wins - 2 losses, the non optimized s509 was 2 wins - 4 losses. The optimized net is almost 4 times faster and almost 1/4 the size of the s509, which is a really incredible accomplishment by lightvector, akigo, sanderland and all the people who contributed gpus to play 3,000,000 training games. wow.

Imagine if in 2018, when Leela Zero was first starting, if someone would have told you that 3 years later there would be an app that was able to play from 10kyu - 9d on a $100 smartphone, making it's moves almost instantly. And it could play with variable komi and no ladder weaknesses.

@acristescu
Copy link
Author

If we could also have that open-source, that would be a dream...

@HackYardo
Copy link

Sorry to bother you all after so long! I'm an amateur in programming and I'm not sure whether this Issue is discussing a full apk or something that just works? Because I can't stand LeelaZero only can play Go games on a 19x19 board, which is too big on a phone screen. Today, I try to compile KataGo in Ubuntu in Termux on Android, it works and works fine, all you need to do is to read the guide here:
https://github.com/lightvector/KataGo/blob/master/Compiling.md#linux

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

No branches or pull requests

10 participants