-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0d4819
commit 7d75598
Showing
8 changed files
with
504 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2023 The OpenXLA Authors | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
import argparse | ||
import requests | ||
import socket | ||
import struct | ||
import time | ||
|
||
ADB_SERVER_ADDR = ("localhost", 5037) | ||
|
||
|
||
def adb_download_and_push_file(source_url: str, | ||
destination: str, | ||
verbose: bool = False): | ||
"""Fetch file from the URL and stream to the device. | ||
In the case of fetching, this method avoids the temporary file on the host | ||
and reduces the overhead when the file is large. | ||
Args: | ||
source_url: URL to fetch the file. | ||
destination: the full destination path on the device. | ||
verbose: output verbose message. | ||
Returns: | ||
File path on the device. | ||
""" | ||
|
||
if verbose: | ||
print(f"Streaming file {source_url} to {destination}.") | ||
|
||
req = requests.get(source_url, stream=True, timeout=60) | ||
if not req.ok: | ||
raise RuntimeError( | ||
f"Failed to fetch {source_url}: {req.status_code} - {req.text}") | ||
|
||
# Implement the ADB sync protocol to stream file chunk to the device, since | ||
# the adb client tool doesn't support it. | ||
# | ||
# Alternatively we can use thrid-party library such as | ||
# https://github.com/JeffLIrion/adb_shell. But the protocol we need is | ||
# simple and fairly stable. This part can be replaced with other solutions | ||
# if needed. | ||
# | ||
# To understand the details of the protocol, see | ||
# https://cs.android.com/android/_/android/platform/packages/modules/adb/+/93c8e3c26e4de3a2b767a2394200bc0721bb1e24:OVERVIEW.TXT | ||
|
||
def wait_ack_ok(sock: socket.socket): | ||
buf = bytearray() | ||
while len(buf) < 4: | ||
data = sock.recv(4 - len(buf)) | ||
if not data: | ||
break | ||
buf += data | ||
|
||
if buf.decode("utf-8") != "OKAY": | ||
raise RuntimeError(f"ADB communication error: {buf}") | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | ||
sock.connect(ADB_SERVER_ADDR) | ||
# Connect to any device (the first 4 hexadecimals is the following text | ||
# command length). | ||
sock.sendall(b"0012host:transport-any") | ||
wait_ack_ok(sock) | ||
# Switch to sync mode. | ||
sock.sendall(b"0005sync:") | ||
wait_ack_ok(sock) | ||
# Send the destination file path and file permissions 0755 (rwxr-xr-x). | ||
file_attr = f"{destination},{0o755}".encode("utf-8") | ||
sock.sendall(b"SEND" + struct.pack("I", len(file_attr)) + file_attr) | ||
# Stream the file chunks. 64k bytes is the max chunk size for adb. | ||
for data in req.iter_content(chunk_size=64 * 1024): | ||
sock.sendall(b"DATA" + struct.pack("I", len(data)) + data) | ||
# End the file stream and set the creation time. | ||
sock.sendall(b"DONE" + struct.pack("I", int(time.time()))) | ||
wait_ack_ok(sock) | ||
|
||
return destination | ||
|
||
|
||
def _parse_arguments() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser(description="Runs benchmarks.") | ||
parser.add_argument("-s", | ||
"--source_url", | ||
type=str, | ||
required=True, | ||
help="The url of file to download.") | ||
parser.add_argument("-d", | ||
"--destination", | ||
type=str, | ||
required=True, | ||
help="The path on the device to stream the file to.") | ||
parser.add_argument("--verbose", | ||
action="store_true", | ||
help="Show verbose messages.") | ||
return parser.parse_args() | ||
|
||
|
||
if __name__ == "__main__": | ||
adb_download_and_push_file(**vars(_parse_arguments())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright 2023 The OpenXLA Authors | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# | ||
# Environment variables: | ||
# PYTHON: Python interpreter, default: /usr/bin/python3 | ||
# OOBI_TARGET_DEVICE: target benchmark device, can also be specified the first | ||
# argument. | ||
# OOBI_VENV_DIR: name of the virtual environment. | ||
# OOBI_IREE_BENCHMARK_MODULE_PATH: the path to `iree-benchmark-module`. | ||
# OOBI_IREE_RUN_MODULE_PATH: the path to `iree-run-module`. | ||
# OOBI_IREE_COMPILED_ARTIFACTS_PATH: the path to the IREE vmfb files to benchmark. | ||
# OOBI_ANDROID_BENCHMARK_DIR: the on-device directory where benchmark artifacts are copied to. | ||
# | ||
# Example usage: | ||
# ./benchmark_iree.sh <target-device> <path-to-compiled-artifacts> <results-path> | ||
|
||
set -xeuo pipefail | ||
|
||
VENV_DIR="${OOBI_VENV_DIR:-iree-benchmarks.venv}" | ||
PYTHON="${PYTHON:-/usr/bin/python3}" | ||
ROOT_DIR="${OOBI_ANDROID_BENCHMARK_DIR:-/data/local/tmp/oobi_benchmarks}" | ||
IREE_BENCHMARK_MODULE_PATH="${OOBI_IREE_BENCHMARK_MODULE_PATH:-/tmp/iree-build-android/tools/iree-benchmark-module}" | ||
IREE_RUN_MODULE_PATH="${OOBI_IREE_RUN_MODULE_PATH:-/tmp/iree-build-android/tools/iree-run-module}" | ||
TARGET_DEVICE="${1:-"${OOBI_TARGET_DEVICE}"}" | ||
COMPILED_ARTIFACTS_PATH="${2:-"${OOBI_IREE_COMPILED_ARTIFACTS_PATH}"}" | ||
OUTPUT_PATH="${3:-"${OOBI_OUTPUT}"}" | ||
|
||
TD="$(cd $(dirname $0) && pwd)" | ||
|
||
# Setup benchmarking environment. | ||
adb shell "rm -rf ${ROOT_DIR}" | ||
adb shell "mkdir ${ROOT_DIR}" | ||
|
||
adb push "${TD}/../../comparative_benchmark/scripts/set_android_scaling_governor.sh" "${ROOT_DIR}" | ||
adb shell "chmod +x ${ROOT_DIR}/set_android_scaling_governor.sh" | ||
adb shell "su root sh ${ROOT_DIR}/set_android_scaling_governor.sh performance" | ||
#adb shell "su root sendhint -m DISPLAY_INACTIVE -e 0" | ||
adb shell "su root setprop persist.vendor.disable.thermal.control 1" | ||
|
||
adb push "${TD}/benchmark_lib.py" "${ROOT_DIR}" | ||
adb shell "chmod +x ${ROOT_DIR}/benchmark_lib.py" | ||
|
||
adb push "${IREE_RUN_MODULE_PATH}" "${ROOT_DIR}" | ||
IREE_RUN_MODULE_PATH="${ROOT_DIR}/iree-run-module" | ||
|
||
adb push "${IREE_BENCHMARK_MODULE_PATH}" "${ROOT_DIR}" | ||
IREE_BENCHMARK_MODULE_PATH="${ROOT_DIR}/iree-benchmark-module" | ||
|
||
VENV_DIR="${VENV_DIR}" PYTHON="${PYTHON}" source "${TD}/setup_venv.sh" | ||
|
||
DEVICE_ARTIFACT_DIR="${ROOT_DIR}/artifacts" | ||
adb shell mkdir "${DEVICE_ARTIFACT_DIR}" | ||
|
||
if [[ "${COMPILED_ARTIFACTS_PATH}" = https* ]]; then | ||
archive_name=$(basename "${COMPILED_ARTIFACTS_PATH}") | ||
|
||
"${TD}/../../comparative_benchmark/scripts/adb_fetch_and_push.py" \ | ||
--source_url="${COMPILED_ARTIFACTS_PATH}" \ | ||
--destination="${ROOT_DIR}/${archive_name}" \ | ||
--verbose | ||
|
||
adb shell "tar -xf ${ROOT_DIR}/${archive_name} --strip-components=1 -C ${DEVICE_ARTIFACT_DIR}" | ||
else | ||
adb push "${COMPILED_ARTIFACTS_PATH}/." "${DEVICE_ARTIFACT_DIR}/" | ||
fi | ||
|
||
"${TD}/../../comparative_benchmark/scripts/create_results_json.sh" "${OUTPUT_PATH}" | ||
|
||
# A num_threads to cpu_ids map. We use the biggest cores for each configuration. | ||
THREAD_CONFIG="{1: '0', 4: '0,1,2,3'}" | ||
|
||
"${TD}/run_benchmarks.py" \ | ||
--target_device="${TARGET_DEVICE}" \ | ||
--output="${OUTPUT_PATH}" \ | ||
--artifact_dir="${DEVICE_ARTIFACT_DIR}" \ | ||
--iree_run_module_path="${IREE_RUN_MODULE_PATH}" \ | ||
--iree_benchmark_module_path="${IREE_BENCHMARK_MODULE_PATH}" \ | ||
--thread_config="${THREAD_CONFIG}" \ | ||
--verbose | ||
|
||
# Cleanup. | ||
adb shell rm -rf "${ROOT_DIR}" |
Oops, something went wrong.