keypadml 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keypadml/__init__.py +20 -0
- keypadml/_runtime/desktop/kp/desktop/DesktopRun$1.class +0 -0
- keypadml/_runtime/desktop/kp/desktop/DesktopRun.class +0 -0
- keypadml/_runtime/desktop/kp/desktop/PhotoRun$1.class +0 -0
- keypadml/_runtime/desktop/kp/desktop/PhotoRun.class +0 -0
- keypadml/_runtime/icon.png +0 -0
- keypadml/_runtime/phone/kp/Bench$Listener.class +0 -0
- keypadml/_runtime/phone/kp/Bench.class +0 -0
- keypadml/_runtime/phone/kp/BenchMIDlet$1.class +0 -0
- keypadml/_runtime/phone/kp/BenchMIDlet$2.class +0 -0
- keypadml/_runtime/phone/kp/BenchMIDlet$3.class +0 -0
- keypadml/_runtime/phone/kp/BenchMIDlet.class +0 -0
- keypadml/_runtime/phone/kp/Camera.class +0 -0
- keypadml/_runtime/phone/kp/Card$1.class +0 -0
- keypadml/_runtime/phone/kp/Card.class +0 -0
- keypadml/_runtime/phone/kp/Inputs.class +0 -0
- keypadml/_runtime/phone/kp/Jpeg$Huffman.class +0 -0
- keypadml/_runtime/phone/kp/Jpeg.class +0 -0
- keypadml/_runtime/phone/kp/MathX.class +0 -0
- keypadml/_runtime/phone/kp/Net.class +0 -0
- keypadml/_runtime/phone/kp/Ops.class +0 -0
- keypadml/_runtime/phone/kp/Parts.class +0 -0
- keypadml/_runtime/phone/kp/Photo$Rows.class +0 -0
- keypadml/_runtime/phone/kp/Photo.class +0 -0
- keypadml/_runtime/phone/kp/ResultsCanvas.class +0 -0
- keypadml/_runtime/phone/kp/Saver.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$1.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$2.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$3.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$4.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$5.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$6.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$7.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$8.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$Files.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$Job.class +0 -0
- keypadml/_runtime/phone/kp/TryIt$Snapper.class +0 -0
- keypadml/_runtime/phone/kp/TryIt.class +0 -0
- keypadml/_tools.py +88 -0
- keypadml/_version.py +1 -0
- keypadml/cli.py +69 -0
- keypadml/convert.py +576 -0
- keypadml/export.py +311 -0
- keypadml/graph.py +211 -0
- keypadml/jarfile.py +68 -0
- keypadml/photo.py +80 -0
- keypadml/py.typed +0 -0
- keypadml/reference.py +109 -0
- keypadml/report.py +433 -0
- keypadml/runner.py +111 -0
- keypadml/serialize.py +228 -0
- keypadml-0.3.0.dist-info/METADATA +155 -0
- keypadml-0.3.0.dist-info/RECORD +57 -0
- keypadml-0.3.0.dist-info/WHEEL +5 -0
- keypadml-0.3.0.dist-info/entry_points.txt +2 -0
- keypadml-0.3.0.dist-info/licenses/LICENSE +202 -0
- keypadml-0.3.0.dist-info/top_level.txt +1 -0
keypadml/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""keypadml: benchmark PyTorch CNNs on old Java ME (J2ME) phones.
|
|
2
|
+
|
|
3
|
+
import keypadml
|
|
4
|
+
result = keypadml.export(model, inputs, labels, out_dir="bench") # -> bench/<name>.jar
|
|
5
|
+
keypadml.verify(result) # optional, needs Java
|
|
6
|
+
# ...run the jar on the phone, then copy the results file back:
|
|
7
|
+
print(keypadml.report("kp_<name>_<n>.json", ref="bench"))
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from ._version import __version__
|
|
11
|
+
from .convert import UnsupportedModelError
|
|
12
|
+
from .export import ConversionCheckError, ExportResult, ModelTooBigError, export, sample
|
|
13
|
+
from .photo import prepare_photo
|
|
14
|
+
from .report import Report, ResultsFormatError, report
|
|
15
|
+
from .runner import JavaNotFoundError, emulate, verify
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"__version__", "export", "sample", "prepare_photo", "verify", "emulate", "report", "Report", "ExportResult",
|
|
19
|
+
"UnsupportedModelError", "ConversionCheckError", "ModelTooBigError", "ResultsFormatError", "JavaNotFoundError",
|
|
20
|
+
]
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
keypadml/_tools.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Downloads (and caches) the third-party jars keypadml needs, checked against pinned SHA-256s.
|
|
2
|
+
|
|
3
|
+
None of these are bundled with keypadml: they are fetched from Maven Central on first use.
|
|
4
|
+
|
|
5
|
+
* MicroEmulator (LGPL-2.1+ or Apache-2.0) and its JSR-75 extension: the optional emulator.
|
|
6
|
+
* CLDC / MIDP / JSR-75 / JSR-135 (camera) API jars (from MicroEmulator) and ProGuard (GPL-2.0, run as a separate
|
|
7
|
+
program) are only used by ``scripts/build_runtime.py`` when developing keypadml itself.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import hashlib
|
|
13
|
+
import os
|
|
14
|
+
import shutil
|
|
15
|
+
import tempfile
|
|
16
|
+
import urllib.request
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
MAVEN = "https://repo1.maven.org/maven2"
|
|
20
|
+
|
|
21
|
+
# name -> (path on Maven Central, sha256)
|
|
22
|
+
JARS = {
|
|
23
|
+
"microemulator": ("org/microemu/microemulator/2.0.4/microemulator-2.0.4.jar",
|
|
24
|
+
"dbd5f3eb8365d3e839d6a203149e0e3776fc1a0585e16ac1fc23f76c9fcae1c6"),
|
|
25
|
+
"microemu-jsr-75": ("org/microemu/microemu-jsr-75/2.0.4/microemu-jsr-75-2.0.4.jar",
|
|
26
|
+
"d4372cf344ee03f0261ae58bd3d2becb4aed98272bca7c6bb98749efd11b4601"),
|
|
27
|
+
"microemu-jsr-135": ("org/microemu/microemu-jsr-135/2.0.4/microemu-jsr-135-2.0.4.jar",
|
|
28
|
+
"9de8726d16d559da98bd36b30e83c992ced946200e80d93a9f4c61f71ceda6af"),
|
|
29
|
+
"cldcapi11": ("org/microemu/cldcapi11/2.0.4/cldcapi11-2.0.4.jar",
|
|
30
|
+
"f86f41bfb4097e4bd9cdfeca528960f6977cbda0f5785342adef9909a6a3baef"),
|
|
31
|
+
"midpapi20": ("org/microemu/midpapi20/2.0.4/midpapi20-2.0.4.jar",
|
|
32
|
+
"b7f6ed5e0344c7000643ecc114e6edff3587e8e77face546af0e12ffd6dbe826"),
|
|
33
|
+
"proguard-base": ("com/guardsquare/proguard-base/7.10.0/proguard-base-7.10.0.jar",
|
|
34
|
+
"022854eb4c6f4bd761b2e3ff19d832e98227ff498fd8fcc788b313f55810a555"),
|
|
35
|
+
"proguard-core": ("com/guardsquare/proguard-core/9.4.0/proguard-core-9.4.0.jar",
|
|
36
|
+
"7af7c47c173711aca9edee7482ec27b33eb0a20345279d88022cb34f71710407"),
|
|
37
|
+
"kotlin-stdlib": ("org/jetbrains/kotlin/kotlin-stdlib/2.4.0/kotlin-stdlib-2.4.0.jar",
|
|
38
|
+
"ccb14ff83fabcb11458b798dbc9824748ccdffeec79c9aba789e6ed1cda86b1c"),
|
|
39
|
+
"gson": ("com/google/code/gson/gson/2.11.0/gson-2.11.0.jar",
|
|
40
|
+
"57928d6e5a6edeb2abd3770a8f95ba44dce45f3b23b7a9dc2b309c581552a78b"),
|
|
41
|
+
"log4j-api": ("org/apache/logging/log4j/log4j-api/2.26.1/log4j-api-2.26.1.jar",
|
|
42
|
+
"f1810a4704ccce019d02bba029dc02a4f2ac0c997f647b465e7d409c1927f822"),
|
|
43
|
+
"log4j-core": ("org/apache/logging/log4j/log4j-core/2.26.1/log4j-core-2.26.1.jar",
|
|
44
|
+
"2ec8c3bf5a6d7c89be8620116a2fc973f7dda1ad0261ad393c4d4726edfd3dcb"),
|
|
45
|
+
"json": ("org/json/json/20231013/json-20231013.jar",
|
|
46
|
+
"0f18192df289114e17aa1a0d0a7f8372cc9f5c7e4f7e39adcf8906fe714fa7d3"),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
PROGUARD = ["proguard-base", "proguard-core", "kotlin-stdlib", "gson", "log4j-api", "log4j-core", "json"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def cache_dir() -> Path:
|
|
53
|
+
"""$KEYPADML_CACHE, else ~/.cache/keypadml (or $XDG_CACHE_HOME/keypadml)."""
|
|
54
|
+
env = os.environ.get("KEYPADML_CACHE")
|
|
55
|
+
if env:
|
|
56
|
+
return Path(env)
|
|
57
|
+
base = os.environ.get("XDG_CACHE_HOME") or os.path.join(os.path.expanduser("~"), ".cache")
|
|
58
|
+
return Path(base) / "keypadml"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def fetch(name: str) -> Path:
|
|
62
|
+
"""Returns the local path of a pinned jar, downloading it once."""
|
|
63
|
+
rel, sha = JARS[name]
|
|
64
|
+
dest = cache_dir() / "jars" / os.path.basename(rel)
|
|
65
|
+
if dest.exists() and _sha256(dest) == sha:
|
|
66
|
+
return dest
|
|
67
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
68
|
+
url = f"{MAVEN}/{rel}"
|
|
69
|
+
fd, tmp = tempfile.mkstemp(dir=dest.parent, suffix=".part")
|
|
70
|
+
try:
|
|
71
|
+
with os.fdopen(fd, "wb") as f, urllib.request.urlopen(url, timeout=60) as r:
|
|
72
|
+
shutil.copyfileobj(r, f)
|
|
73
|
+
got = _sha256(Path(tmp))
|
|
74
|
+
if got != sha:
|
|
75
|
+
raise RuntimeError(f"checksum mismatch for {url}: expected {sha}, got {got}")
|
|
76
|
+
os.replace(tmp, dest)
|
|
77
|
+
finally:
|
|
78
|
+
if os.path.exists(tmp):
|
|
79
|
+
os.remove(tmp)
|
|
80
|
+
return dest
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _sha256(path: Path) -> str:
|
|
84
|
+
h = hashlib.sha256()
|
|
85
|
+
with open(path, "rb") as f:
|
|
86
|
+
for chunk in iter(lambda: f.read(1 << 16), b""):
|
|
87
|
+
h.update(chunk)
|
|
88
|
+
return h.hexdigest()
|
keypadml/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.0"
|
keypadml/cli.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Command line: keypadml report | verify | emulate | info."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from ._version import __version__
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main(argv=None) -> int:
|
|
14
|
+
ap = argparse.ArgumentParser(prog="keypadml",
|
|
15
|
+
description="Benchmark PyTorch CNNs on old Java ME phones.")
|
|
16
|
+
ap.add_argument("--version", action="version", version=f"keypadml {__version__}")
|
|
17
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
18
|
+
|
|
19
|
+
r = sub.add_parser("report", help="summarise a results file saved by the phone")
|
|
20
|
+
r.add_argument("results", help="results file (kp_<name>_<n>.json, or .txt from older jars) copied from the phone")
|
|
21
|
+
r.add_argument("--ref", help="export folder (adds parity check and MACs per layer)")
|
|
22
|
+
r.add_argument("--json", help="also write the full report as JSON here")
|
|
23
|
+
r.add_argument("--layers-csv", help="also write the per-layer timings as CSV here")
|
|
24
|
+
|
|
25
|
+
v = sub.add_parser("verify", help="run the exported model on this computer and check it against PyTorch")
|
|
26
|
+
v.add_argument("export_dir")
|
|
27
|
+
v.add_argument("--java", help="java executable")
|
|
28
|
+
|
|
29
|
+
e = sub.add_parser("emulate", help="open the benchmark in MicroEmulator (downloads it once)")
|
|
30
|
+
e.add_argument("export_dir")
|
|
31
|
+
e.add_argument("--java", help="java executable")
|
|
32
|
+
e.add_argument("--size", default="240x320", help="screen size, e.g. 240x320")
|
|
33
|
+
|
|
34
|
+
i = sub.add_parser("info", help="show what an export contains")
|
|
35
|
+
i.add_argument("export_dir")
|
|
36
|
+
|
|
37
|
+
a = ap.parse_args(argv)
|
|
38
|
+
if a.cmd == "report":
|
|
39
|
+
from .report import Report
|
|
40
|
+
rep = Report.load(a.results, a.ref)
|
|
41
|
+
print(rep)
|
|
42
|
+
if a.json:
|
|
43
|
+
rep.to_json(a.json)
|
|
44
|
+
if a.layers_csv:
|
|
45
|
+
rep.layers_csv(a.layers_csv)
|
|
46
|
+
p = rep.parity()
|
|
47
|
+
return 1 if p is not None and not p.get("ok") else 0
|
|
48
|
+
if a.cmd == "verify":
|
|
49
|
+
from .runner import verify
|
|
50
|
+
rep = verify(a.export_dir, a.java)
|
|
51
|
+
print(rep)
|
|
52
|
+
p = rep.parity()
|
|
53
|
+
return 0 if p and p.get("ok") else 1
|
|
54
|
+
if a.cmd == "emulate":
|
|
55
|
+
from .runner import emulate
|
|
56
|
+
w, h = (int(s) for s in a.size.lower().split("x"))
|
|
57
|
+
print("Results are saved in ~/.microemulator/filesystem/ (the emulator's memory card).")
|
|
58
|
+
return emulate(a.export_dir, a.java, w, h)
|
|
59
|
+
if a.cmd == "info":
|
|
60
|
+
info = json.loads((Path(a.export_dir) / "export.json").read_text())
|
|
61
|
+
from .export import ExportResult
|
|
62
|
+
d = Path(a.export_dir)
|
|
63
|
+
print(ExportResult(d, d / info["jar"], d / info["jad"], d / "reference.npz", d / "export.json", info))
|
|
64
|
+
return 0
|
|
65
|
+
return 2
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if __name__ == "__main__":
|
|
69
|
+
sys.exit(main())
|