eye icon indicating copy to clipboard operation
eye copied to clipboard

Running eye in a browser?

Open rybesh opened this issue 3 years ago • 17 comments

Have there been any experiments with running eye in a browser, using something like swipl-wasm, or maybe Tau Prolog?

rybesh avatar Nov 11 '21 16:11 rybesh

Not yet like that but see https://github.com/MellonScholarlyCommunication/POrchestrator/issues/3 and one day it will work out ;-)

josd avatar Nov 11 '21 17:11 josd

For the moment I think that the best solution is mentioned in https://github.com/MellonScholarlyCommunication/POrchestrator/issues/3#issuecomment-960831148

In the browser, we could also host a Prolog engine via WebAssembly if we wanted to.

So @RubenVerborgh do you want to?

josd avatar Nov 11 '21 17:11 josd

we could also host a Prolog engine via WebAssembly if we wanted to.

That’s what I was most interested in… I’m envisioning browser-based tools that would allow people to pull in small-to-medium triplesets from various sources, store them locally and reason over them offline.

rybesh avatar Nov 11 '21 19:11 rybesh

So @RubenVerborgh do you want to?

Oh very much 🙂 Maybe @DylanVanAssche is the right person?

RubenVerborgh avatar Nov 11 '21 20:11 RubenVerborgh

I have no experience with getting such things running in browsers...

DylanVanAssche avatar Nov 12 '21 09:11 DylanVanAssche

I thought your *nix skills might be a good match for this; but let's keep looking then 🙂

RubenVerborgh avatar Nov 12 '21 19:11 RubenVerborgh

I tried my *nix skills and was able to run EYE on my Android phone 🙂

image

Having a reasoner in your hand feels good and thanks to the UserLAnd app it should be straightforward to integrate it further.

josd avatar Nov 13 '21 14:11 josd

That is beyond amazing!

RubenVerborgh avatar Nov 13 '21 18:11 RubenVerborgh

I was also surprised and it was working out quite well and all tests in the 81 subprojects at https://github.com/josd/eye/tree/master/reasoning work perfectly. The speed is a bit slower than in the cloud but is much better than I would have expected for instance the deep taxonomy example at https://github.com/josd/eye/tree/master/reasoning/dt with 30000 classes runs in about 18 sec versus 4 sec in the cloud. The particular phone is a Samsung Galaxy A40 from last year. What I did was installing the UserLAnd app and going step by step through https://github.com/josd/josd.github.io/blob/master/temp/userland.txt

josd avatar Nov 14 '21 00:11 josd

I don't know whether it is easier or harder. Termux is an alternative for running SWI-Prolog on Android. Eventually WASM should do the job reliably. I'm afraid that is still merely a demonstrator.

JanWielemaker avatar Nov 14 '21 09:11 JanWielemaker

It is about the same difficulty/effort @JanWielemaker 🙂 I tried it as described in https://github.com/josd/josd.github.io/blob/master/temp/termux.txt

I was also trying to build swipl wasm following https://github.com/SWI-Prolog/swipl-devel/blob/master/CMAKE.md#wasm-emscripten and ~/github.com/SWI-Prolog/swipl-devel/build.wasm/build.ninja was nicely generated but then when running ninja we got https://github.com/josd/josd.github.io/blob/0b4538045c908d0f3b024166c95098a0a92c9697/temp/stdout.txt and https://github.com/josd/josd.github.io/blob/0b4538045c908d0f3b024166c95098a0a92c9697/temp/.ninja_log

josd avatar Nov 14 '21 20:11 josd

Today @JanWielemaker and @RubenVerborgh we were able to run eye on swipl.wasm by doing

sudo apt install cmake
sudo apt install ninja-build

cd ~
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh

cd ~
wget https://zlib.net/zlib-1.2.11.tar.gz -O "$HOME/zlib-1.2.11.tar.gz"
tar -xf "$HOME/zlib-1.2.11.tar.gz" -C "$HOME"
cd "$HOME/zlib-1.2.11"
emconfigure ./configure
emmake make

cd ~/github.com/SWI-Prolog/swipl-devel
mkdir build.wasm
cd build.wasm
source ~/emsdk/emsdk_env.sh
export EMSCRIPTEN=~/emsdk/upstream/emscripten/
cmake -DCMAKE_TOOLCHAIN_FILE=$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake \
  -DCMAKE_BUILD_TYPE=Release \
  -DZLIB_LIBRARY=$HOME/zlib-1.2.11/libz.a \
  -DZLIB_INCLUDE_DIR=$HOME/zlib-1.2.11 \
  -DMULTI_THREADED=OFF \
  -DUSE_SIGNALS=OFF \
  -DUSE_GMP=OFF \
  -DBUILD_SWIPL_LD=OFF \
  -DSWIPL_PACKAGES=OFF \
  -DINSTALL_DOCUMENTATION=OFF \
  -DSWIPL_NATIVE_FRIEND=build \
  -G Ninja ..
ninja

and we just needed a small tweak

~/github.com/SWI-Prolog/swipl-devel$ git diff
diff --git a/src/pl-setup.c b/src/pl-setup.c
index 82a21ee36..9b9f3a4dd 100644
--- a/src/pl-setup.c
+++ b/src/pl-setup.c
@@ -501,7 +501,7 @@ dispatch_signal(int sig, int sync)

   if ( (LD->critical || (true(sh, PLSIG_SYNC) && !sync)) &&
        !is_fatal_signal(sig) &&
-       sig != SIGINT )
+       sig != 2 )
   { PL_raise(sig);                     /* wait for better times! */
     return;
   }

to let it run for the following cases in https://github.com/josd/josd.github.io/tree/master/temp/eye-wasm

$ node ~/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js -f eye.pl -g main -- --nope socrates.n3 --query socrates-query.n3
eye --nope socrates.n3 --query socrates-query.n3
EYE v22.0203.1955 josd
SWI-Prolog version 8.5.7-26-g4a0a2091e-DIRTY
starting 0 [msec cputime] 167 [msec walltime]
#Processed by EYE v22.0203.1955 josd
#eye --nope socrates.n3 --query socrates-query.n3

GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/socrates.n3 SC=3
GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/socrates-query.n3 SC=1
networking 0 [msec cputime] 22 [msec walltime]
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://example.org/socrates#>

:Socrates a :Human.
:Socrates a :Mortal.

reasoning 0 [msec cputime] 1 [msec walltime]
#2022-02-18T01:01:27.938Z in=4 out=2 ent=3 step=6 brake=2 inf=624804 sec=0.000 inf/sec=
#ENDS

2022-02-18T01:01:27.938Z in=4 out=2 ent=3 step=6 brake=2 inf=624804 sec=0.000 inf/sec=

and

$ node ~/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js -f eye.pl -g main -- --nope gps-plugin.n3 gps-example1.n3 --query gps-query1.n3
eye --nope gps-plugin.n3 gps-example1.n3 --query gps-query1.n3
EYE v22.0203.1955 josd
SWI-Prolog version 8.5.7-26-g4a0a2091e-DIRTY
starting 0 [msec cputime] 174 [msec walltime]
#Processed by EYE v22.0203.1955 josd
#eye --nope gps-plugin.n3 gps-example1.n3 --query gps-query1.n3

GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/gps-plugin.n3 SC=7
GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/gps-example1.n3 SC=5
GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/gps-query1.n3 SC=1
networking 0 [msec cputime] 32 [msec walltime]
PREFIX math: <http://www.w3.org/2000/10/swap/math#>
PREFIX list: <http://www.w3.org/2000/10/swap/list#>
PREFIX log: <http://www.w3.org/2000/10/swap/log#>
PREFIX e: <http://eulersharp.sourceforge.net/2003/03swap/log-rules#>
PREFIX gps: <http://josd.github.io/eye/reasoning/gps/gps-schema#>
PREFIX : <http://josd.github.io/eye/reasoning#>

:i1 gps:path ((:drive_gent_brugge :drive_brugge_oostende) 2400.0 0.01 0.9408 0.99).
:i1 gps:path ((:drive_gent_kortrijk :drive_kortrijk_brugge :drive_brugge_oostende) 4100.0 0.018000000000000002 0.903168 0.9801).

reasoning 0 [msec cputime] 6 [msec walltime]
#2022-02-18T01:01:42.420Z in=13 out=2 ent=2 step=2 brake=2 inf=685807 sec=0.000 inf/sec=
#ENDS

2022-02-18T01:01:42.420Z in=13 out=2 ent=2 step=2 brake=2 inf=685807 sec=0.000 inf/sec=

but get an unexpected OOM (Out Of Memory) for

$ node ~/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js -f eye.pl -g main -- --nope test-dt.n3 test-facts.n3 --query test-query.n3
eye --nope test-dt.n3 test-facts.n3 --query test-query.n3
EYE v22.0203.1955 josd
SWI-Prolog version 8.5.7-26-g4a0a2091e-DIRTY
starting 0 [msec cputime] 165 [msec walltime]
#Processed by EYE v22.0203.1955 josd
#eye --nope test-dt.n3 test-facts.n3 --query test-query.n3

GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/test-dt.n3 Aborted(OOM)
exiting due to exception: RuntimeError: Aborted(OOM). Build with -s ASSERTIONS=1 for more info.

josd avatar Feb 18 '22 01:02 josd

@josd - really cool. I'd love to add this as a reasoning actor to comunica once I release a stable version of the base reasoning components.

Are there plans to make a npm package of the wasm distro at any point?

jeswr avatar Feb 18 '22 01:02 jeswr

Maybe yes @jeswr once the OOM is resolved. If not we remain with @RubenVerborgh 's excellent https://www.npmjs.com/package/eyeserver

josd avatar Feb 18 '22 01:02 josd

Now that's progress! Super news, @josd! Definitely a great start.

Have tried playing with the Node.js command-line flags for memory?

RubenVerborgh avatar Feb 18 '22 07:02 RubenVerborgh

That is a better state than I feared! It is also new that the node version really works. It used not to due to a bug in the POSIX directory API of emscripten :cry: So, that seems fixed :smile:

  •   sig != 2 )
    

Pushed SWI-Prolog/swipl-devel@1c0c6729cef8604e5723e01f668db3d69485abf5 to address this.

If there are no bugs in emscripten and SWI-Prolog you should end up with a pretty decent system. By default without GMP (bigint) support, signal handling and threads. The main limitation might be that WASM is 32-bits and that limits the Prolog stacks to 128Mb (each). I see some hints that a wasm64 might be coming.

Any data on performance yet?

Note that @dmchurch is looking into the WASM port. You can also reach out on the SWI-Prolog Discourse.

JanWielemaker avatar Feb 18 '22 08:02 JanWielemaker

Have tried playing with the Node.js command-line flags for memory?

Very good suggestion @RubenVerborgh and when building with -s ALLOW_MEMORY_GROWTH=1 it now works much better!

Still the 100000 deep taxonomy benchmark gives

$ node ~/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js -f eye.pl -g main -- --nope test-dt.n3 test-facts.n3 --query test-query.n3
eye --nope test-dt.n3 test-facts.n3 --query test-query.n3
EYE v22.0203.1955 josd
SWI-Prolog version 8.5.7-26-g4a0a2091e-DIRTY
starting 0 [msec cputime] 420 [msec walltime]
#Processed by EYE v22.0203.1955 josd
#eye --nope test-dt.n3 test-facts.n3 --query test-query.n3

GET file:///home/jdroo/github.com/josd/josd.github.io/temp/eye-wasm/test-dt.n3 [FATAL ERROR: at Fri Feb 18 11:42:28 2022
        Cannot report error: no memory]
Aborted(native code called abort())
exiting due to exception: RuntimeError: Aborted(native code called abort()),RuntimeError: Aborted(native code called abort())
    at abort (/home/jdroo/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js:1:21763)
    at _abort (/home/jdroo/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js:1:101328)
    at <anonymous>:wasm-function[1497]:0xc7d51
    at <anonymous>:wasm-function[988]:0x9c2f9
    at <anonymous>:wasm-function[472]:0x30a56
    at <anonymous>:wasm-function[70]:0x3e4d
    at <anonymous>:wasm-function[1602]:0xcd05f
    at <anonymous>:wasm-function[1601]:0xccfc7
    at invoke_iiii (/home/jdroo/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js:1:122082)
    at <anonymous>:wasm-function[732]:0x5c2e3

but when running the same benchmark on Webeye it works fine

$ /usr/bin/time -f "sec=%e0 kB=%M cpu=%P" node ~/github.com/SWI-Prolog/swipl-devel/build.wasm/src/swipl.js -g run,halt webeye.pl dt.pl
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'('http://example.org/ns#z','http://example.org/ns#N100000') => true.
sec=29.830 kB=504168 cpu=108%

Any data on performance yet?

@JanWielemaker when compared with

$ /usr/bin/time -f "sec=%e0 kB=%M cpu=%P" swipl -g run,halt webeye.pl dt.pl
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'('http://example.org/ns#z','http://example.org/ns#N100000') => true.
sec=8.380 kB=190200 cpu=114%

swipl.js is about 3.5 times slower and uses about 2.5 times more memory, but that really looks nice when compared with https://twitter.com/josderoo/status/1494097945767161862 🙂

josd avatar Feb 18 '22 11:02 josd

Running eye in the brower is now realized in https://github.com/eyereasoner/eye-js

josd avatar Feb 05 '23 22:02 josd