def do_register(command: Register, module: CurrentModule, all_modules: AllModules, runtime: Runtime) -> None: if command.name is None: if module is None: raise Exception("Invariant") runtime.register_module(command.as_, module) else: runtime.register_module(command.as_, all_modules[command.name])
def do_recursive_keccak(depth): runtime = Runtime() keccak_file = BASE_DIR / f"recursive_keccak_{depth}_wasm.wasm" if not keccak_file.exists(): raise Exception( f"recursive keccak {depth} is not supported: {keccak_file} not found" ) module, _ = runtime.instantiate_module(runtime.load_module(keccak_file)) logging.info(f"BENCHMARKING: {depth} recursive keccak hashes") delta = time_fn(runtime, module, 'main', (), 1) logging.info(f"took: {delta:.4f}")
def do_factorization_benchmarks(prime): runtime = Runtime() factorization_file = BASE_DIR / f"factorization_{prime}_reikna_wasm.wasm" if not factorization_file.exists(): raise Exception( f"factorization {prime} is not supported: {factorization_file} not found" ) module, _ = runtime.instantiate_module( runtime.load_module(factorization_file)) logging.info(f"BENCHMARKING: factorization of {prime}") delta = time_fn(runtime, module, 'main', (), 1) logging.info(f"took: {delta:.4f}")
def do_fibonacci_bigint_benchmarks(order): runtime = Runtime() fib_file = BASE_DIR / f"fibonacci_{order}_bigint_wasm.wasm" if not fib_file.exists(): raise Exception( f"fibonacci {order} is not supported: {fib_file} not found") module, _ = runtime.instantiate_module(runtime.load_module(fib_file)) logging.info( f"BENCHMARKING: recursive computation of fibonacci number {order} (wasm)" ) delta = time_fn(runtime, module, 'main', (), 1) logging.info(f"took: {delta:.4f}")
def do_basic_benchmarks(): runtime = Runtime() module, _ = runtime.instantiate_module(runtime.load_module(BASIC_WASM)) runtime.register_module('bench', module) thunk_delta = _run_basic_bench(runtime, module, 'thunk', (), 10000) add_delta = _run_basic_bench(runtime, module, 'add', (numpy.uint32(1), numpy.uint32(2)), 10000) call_thunk_delta = _run_basic_bench(runtime, module, 'call_thunk', (numpy.uint32(10000), ), 1) call_add_delta = _run_basic_bench( runtime, module, 'call_add', (numpy.uint32(10000), numpy.uint32(1), numpy.uint32(2)), 1, ) logging.info(f"wrapped thunk: {call_thunk_delta / 10000:.8f} per call") logging.info(f"wrapped add : {call_add_delta / 10000:.8f} per call") thunk_overhead = (call_thunk_delta - thunk_delta) add_overhead = call_add_delta - add_delta logging.info( f"call overhead for thunk: {thunk_overhead:.4f} ({100 * thunk_overhead / thunk_delta:.2f}%) | {thunk_overhead / 10000:.8f} per call" ) # noqa: E501 logging.info( f"call overhead for add : {add_overhead:.4f} ({100 * add_overhead / add_delta:.2f}%) | {add_overhead / 10000:.8f} per call" ) # noqa: E501
def instantiate_module_from_wasm_file(file_path: Path, runtime: Runtime) -> ModuleInstance: logger.debug("Loading wasm module from file: %s", file_path.name) if file_path.suffix != ".wasm": logger.debug("Unsupported file type for wasm module: %s", file_path.suffix) raise Exception("Unsupported file type: {file_path.suffix}") with file_path.open("rb") as wasm_module_file: # memoryview doesn't make copy, bytearray may require copy wasmbytes = memoryview(wasm_module_file.read()) module = wasm.decode_module(wasmbytes) wasm.validate_module(module) module_instance, _ = runtime.instantiate_module(module) return module_instance
def test_json_fixture(fixture_path): runtime = Runtime() # module "spectest" is imported from by many tests runtime.register_module( "spectest", instantiate_spectest_module(runtime.store), ) # module "test" is imported from by many tests runtime.register_module( "test", instantiate_test_module(runtime.store), ) run_fixture_test(fixture_path, runtime)
def test_json_fixture(fixture_path, pytestconfig): # ensure that all numpy flags are set to raise exceptions numpy.seterr(all='raise') stop_after_command_line = pytestconfig.getoption('stop_after_command_line') runtime = Runtime() # module "spectest" is imported from by many tests runtime.register_module( "spectest", instantiate_spectest_module(runtime.store), ) # module "test" is imported from by many tests runtime.register_module( "test", instantiate_test_module(runtime.store), ) run_fixture_test(fixture_path, runtime, stop_after_command_line)
def run_opcode_action_invoke(action: Action, module: ModuleInstance, all_modules: AllModules, runtime: Runtime) -> Tuple[TValue, ...]: # get function name, which could include unicode bytes like \u001b which # must be converted to unicode string function_name = action.field.encode('latin1').decode('utf8') # get function address function_address = None for export in module.exports: if export.is_function and export.name == function_name: function_address = export.function_address logger.debug("function_address: %s", function_address) break else: raise Exception(f"No function found by name: {function_name}") args = tuple(arg.value for arg in action.args) # invoke func ret = runtime.invoke_function(function_address, tuple(args)) return ret
def instantiate_module_from_wasm_file(file_path: Path, runtime: Runtime) -> ModuleInstance: module = runtime.load_module(file_path) module_instance, _ = runtime.instantiate_module(module) return module_instance