char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d30500b83
char/aliceai-t5-moe
1"""Shared helpers for QDC on-device test runners.""" 2 3from __future__import annotations 4 5import logging 6import os 7import subprocess 8import sys 9import tempfile 10 11from appium .options .common import AppiumOptions 12 13log = logging .getLogger (__name__ ) 14 15# --------------------------------------------------------------------------- 16# On-device paths 17# --------------------------------------------------------------------------- 18 19BUNDLE_PATH = "/data/local/tmp/llama.cpp" 20BIN_PATH = f" { BUNDLE_PATH } /bin" 21LIB_PATH = f" { BUNDLE_PATH } /lib" 22QDC_LOGS_PATH = "/data/local/tmp/QDC_logs" 23SCRIPTS_DIR = "/qdc/appium" 24MODEL_NAME = "model.gguf" 25MODEL_DEVICE_PATH = "/data/local/tmp/gguf/model.gguf" 26PROMPT_DIR = "/data/local/tmp/scorecard_prompts" 27 28# --------------------------------------------------------------------------- 29# Appium session options 30# --------------------------------------------------------------------------- 31 32options = AppiumOptions () 33options .set_capability ("automationName" ,"UiAutomator2" ) 34options .set_capability ("platformName" ,"Android" ) 35options .set_capability ("deviceName" ,os .getenv ("ANDROID_DEVICE_VERSION" )) 36 37# --------------------------------------------------------------------------- 38# Shell / process helpers 39# --------------------------------------------------------------------------- 40 41 42def write_qdc_log (filename :str ,content :str )-> None : 43"""Write content as a log file for QDC log collection.""" 44subprocess .run ( 45 ["adb" ,"shell" ,f"mkdir -p { QDC_LOGS_PATH } " ], 46stdout = subprocess .PIPE , 47stderr = subprocess .STDOUT , 48 ) 49with tempfile .NamedTemporaryFile (mode = "w" ,suffix = ".log" ,delete = False )as f : 50f .write (content ) 51tmp_path = f .name 52try : 53subprocess .run ( 54 ["adb" ,"push" ,tmp_path ,f" { QDC_LOGS_PATH } / { filename } " ], 55stdout = subprocess .PIPE , 56stderr = subprocess .STDOUT , 57 ) 58finally : 59os .unlink (tmp_path ) 60 61 62def ensure_bundle (check_binary :str | None = None )-> None : 63"""Ensure the llama_cpp_bundle is available on the target device.""" 64push_bundle_if_needed (check_binary or f" { BIN_PATH } /llama-cli" ) 65 66 67# --------------------------------------------------------------------------- 68# Android / Linux host helpers 69# --------------------------------------------------------------------------- 70 71 72def run_adb_command (cmd :str ,* ,check :bool = True )-> subprocess .CompletedProcess : 73"""Run a command on-device via ``adb shell`` with exit-code sentinel.""" 74raw = subprocess .run ( 75 ["adb" ,"shell" ,f" { cmd } ; echo __RC__:$?" ], 76text = True , 77stdout = subprocess .PIPE , 78stderr = subprocess .STDOUT , 79 ) 80stdout = raw .stdout 81returncode = raw .returncode 82if stdout : 83lines = stdout .rstrip ("\n" ).split ("\n" ) 84if lines and lines [- 1 ].startswith ("__RC__:" ): 85try : 86returncode = int (lines [- 1 ][7 :]) 87stdout = "\n" .join (lines [:- 1 ])+ "\n" 88except ValueError : 89pass 90log .info (stdout ) 91result = subprocess .CompletedProcess (raw .args ,returncode ,stdout = stdout ) 92if check : 93assert returncode == 0 ,f"Command failed (exit { returncode } )" 94return result 95 96 97def run_snapdragon ( 98cmd_args :list [str ], 99* , 100device :str | None = None , 101extra_run_args :list [str ]| None = None , 102extra_env :dict [str ,str ]| None = None , 103)-> subprocess .CompletedProcess : 104"""Run a tool via scripts/snapdragon/run.py targeting android.""" 105env = os .environ .copy () 106env ["GGML_HEXAGON_EXPERIMENTAL" ]= "1" 107if extra_env : 108env .update (extra_env ) 109cmd = [sys .executable ,f" { SCRIPTS_DIR } /run.py" ,"--target" ,"android" ] 110if device is not None : 111cmd .extend (["-d" ,device ]) 112if extra_run_args : 113cmd .extend (extra_run_args ) 114cmd .append ("--" ) 115cmd .extend (cmd_args ) 116result = subprocess .run ( 117cmd ,env = env , 118text = True ,stdout = subprocess .PIPE ,stderr = subprocess .STDOUT , 119 ) 120log .info (result .stdout ) 121return result 122 123 124def adb_shell (cmd :str )-> None : 125"""Run a command via adb shell (fire-and-forget, no error check).""" 126subprocess .run ( 127 ["adb" ,"shell" ,"sh" ,"-c" ,cmd ], 128capture_output = True ,encoding = "utf-8" ,errors = "replace" ,check = False , 129 ) 130 131 132def push_bundle_if_needed (check_binary :str )-> None : 133"""Push llama_cpp_bundle to the device if check_binary is not already present.""" 134result = subprocess .run ( 135 ["adb" ,"shell" ,f"ls { check_binary } " ], 136text = True , 137stdout = subprocess .PIPE , 138stderr = subprocess .STDOUT , 139 ) 140if result .returncode != 0 : 141subprocess .run ( 142 ["adb" ,"push" ,"/qdc/appium/llama_cpp_bundle/" ,BUNDLE_PATH ], 143text = True , 144stdout = subprocess .PIPE , 145stderr = subprocess .STDOUT , 146 ) 147subprocess .run ( 148 ["adb" ,"shell" ,f"find { BUNDLE_PATH } /bin -type f -exec chmod 755 {{}} +" ], 149text = True , 150stdout = subprocess .PIPE , 151stderr = subprocess .STDOUT , 152 )