lightvector / KataGo

GTP engine and self-play learning in Go
https://katagotraining.org/
Other
3.46k stars 561 forks source link

Android binary #321

Open acristescu opened 3 years ago

acristescu commented 3 years ago

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 https://github.com/leela-zero/leela-zero/compare/next...evdwerf:android ).

Is that something that could be achieved?

simonguoxm commented 3 years ago

this will be a great feature for the phone.

l1t1 commented 3 years ago

ahq weiqi pro includes it.

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

cryptsport commented 3 years ago

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

portkata commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 https://github.com/sanderland/katrain/issues/74 and also referred to in passing here https://github.com/sanderland/katrain/issues/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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

(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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 🤦‍♂️

acristescu commented 3 years ago

On second thought, the release is new, but the katago binary seems to be the same (old) version (7,942,024 bytes). Is there a way to get a newer version with all the new stuff by any chance?

Here's how it looks so far btw: Screenshot_20201022-162704_Online Go

portkata commented 3 years ago

Wow that looks so good.

acristescu commented 3 years ago

I've released a first version of it: https://play.google.com/store/apps/details?id=io.zenandroid.onlinego using aki65's binary.

For now it has not scaling down functionality, it just plays at 30 visits with the 15b network which makes it WAY above my level. I will try my hand at dumbing it down, as it seems like a missed opportunity given the fact that the app knows your OGS rank and could thus scale katago down to your level automatically.

Thank you guys for your help.

PS: it would still be great to have an official android build with the latest and greatest though

portkata commented 3 years ago

The ui looks great but I can't figure out how to play katago online. I'm wondering if the new apk is not being hosted by google play? the version that downloads is 285. Since you mentioned you wanted the ai to play at 3-4 dan, the 15b net can easily beat the 4d Golaxy bot by using max 4 playouts (1 thread). Thanks for the great app!

acristescu commented 3 years ago

285 doesn't have it, 301 and 304 do. Sometimes play store is slow to suggest updates, but for me it did update. Try it again later pls.

lightvector commented 3 years ago

I don't build android (had someone else been doing this?) But you are right that the latest official release is lagging a bit behind master branch in features. I'll make a new release (for windows+linux) before too long. Thanks for the reminder.

acristescu commented 3 years ago

Hi @lightvector thank you for answering. Indeed, an android official build was the initial ask of this issue before it got completely derailed. In summary, yes, somebody has done it... as a one-time thing: @aki65 but as he explains here he didn't get the build scripts in a good enough shape to raise a PR or even to make public.

Anyway, this is the binary I managed to bake into my app, however it is a bit out of date already (and it will get more so as time passes). I can confirm that his build does work quite nicely though and the performance is adequate for casual use. I feel it would be a great addition to the project to add android as an officially supported platform.

sanderland commented 3 years ago

@acristescu @portkata what kind of visits are you getting? I'm getting around 1/s on my phone and 0.5/s on my tablet on 15b.

acristescu commented 3 years ago

My Samsung Galaxy S9+ (2018 flagship) gets about 7 visits/s. Other users of my app are reporting around 10 visits/s on S10 (2019 flagship) and 4-5 visits/s on A51 (midrange phone). If you don't mind me asking, what phone and tabled are you getting those results on?

sanderland commented 3 years ago

S10 for the 1 visit/sec. Specifically using the analysis engine with an 8s timeout I get ~10 visits.

acristescu commented 3 years ago

Hmm... weird... I am also using the analysis engine (excellent work btw, that's much nicer than GTP), but with maxVisits = 20. What app are you using? Are you getting better speeds from mine?

sanderland commented 3 years ago

Your app does move in about 2 seconds. Maybe my settings are off. Is your numSearchThreads high?

My contributions to the analysis engine are quite small, by the way, but I agree it's a nice thing to have. :)

image

portkata commented 3 years ago

Wow i can't believe you were able to bring katrain to android, what a great thing :) . I get less than 2 visits/second with 1 thread 64 bit snapdragon 625 eigen. That is good enough for me I think. No signicant speed difference for me between 3 threads and 8 threads on octacore, but obviously significantly faster than 1 thread. if you have snapdragon/adreno, i can get faster performance by using opencl binary by aki65, but i dont use it day to day, just to test out speed difference out of curiosity -

katago_64bit_opencl_snapdragon_adreno.zip

now we just eventually need someone to do one for exynos

acristescu commented 3 years ago

Wow, katrain on Android... that's going to be stiff competition :) Anyway, here's the CFG I use: https://github.com/acristescu/OnlineGo/blob/master/app/src/main/assets/katago.cfg

I haven't actually done any tests for speed, as for playing against katago visits / s are not important, it will beat 99% of the users in a couple of seconds anyway.

Btw, katrain was python, right? maybe it's the python platform that slowing things down?

sanderland commented 3 years ago

i suspect it's numSearchThreadsPerAnalysisThread = 8, the opencl is irrelevant right as it's eigen? katrain is python, and python-for-android is a bit slow to start, but after that no amount of button drawing is going to compete with neural network evaluations in terms of turning your phone into a handwarmer.

acristescu commented 3 years ago

i suspect it's numSearchThreadsPerAnalysisThread = 8, the opencl is irrelevant right as it's eigen?

I have absolutely no idea, I figured my phone has 8 cores, it can't hurt, right? If you do experiment and find some interesting settings for the CFG, please do share though...

neural network evaluations in terms of turning your phone into a handwarmer.

Leela Zero did warm the phone right up, katago doesn't so much. Whatever magic aki65 did to create that build worked like a charm...

isty2e commented 3 years ago

You might be interested in using ONNXRuntime backend, which supports NNAPI.

sanderland commented 3 years ago

are there binaries for 1.7.0 out yet?

acristescu commented 3 years ago

are there binaries for 1.7.0 out yet?

Not that I know of. No includeMovesOwnership for us... :(

portkata commented 3 years ago

aki65 optimized the newest version of his app so when it runs with his optimized 20b net, the net is 5 times faster :)