Skip to content

Commit

Permalink
Add memory hint & calculate times
Browse files Browse the repository at this point in the history
Change-Id: I0dbcc6ea3110e2a9bab2716409aa01cd56ddbf12
  • Loading branch information
Jeffwhen committed Aug 2, 2022
1 parent dce191c commit ce5c6a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion python/tpu_perf/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def build(tree, path, config):
fp32_pool.wait()
logging.info(f'FP32 bmodel {name} done.')

int8_pool = CommandExecutor(workdir, env)
int8_pool = CommandExecutor(
workdir, env,
memory_hint=config.get('memory_hint'))

cali_key = 'time_only_cali' if option_time_only else 'cali'

Expand Down
12 changes: 10 additions & 2 deletions python/tpu_perf/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def run(tree, path, config, stat_f):
pool = CommandExecutor(workdir, env)
rounds = config.get('time_rounds', 2000)
rt_cmp = config.get('runtime_cmp')
iter_opt = tree.global_config.get('iter_opt', '--loopnum')
if 'iter_opt' in config:
iter_opt = config['iter_opt']
for b in config['bmnetu_batch_sizes']:
name = f'{b}b.compilation'
bmodel_dir = os.path.join(workdir, name)
Expand All @@ -84,6 +87,9 @@ def run(tree, path, config, stat_f):
info = parse_profile(profile_path)
rounds = int(1200 / info['runtime'])
logging.info(f'Run {rounds} times for {name}.{b}b')
max_rounds = 500000
if rounds > max_rounds:
rounds = max_rounds

title = f'run.{b}'
ref_fn = os.path.join(bmodel_dir, 'output_ref_data.dat')
Expand All @@ -92,13 +98,13 @@ def run(tree, path, config, stat_f):
logging.info(f'Runtime test {name}')
pool.put(
title,
['bmrt_test', '--loopnum', str(rounds), '--context', bmodel_dir],
['bmrt_test', iter_opt, str(rounds), '--context', bmodel_dir],
env=env, shell=False)
else:
logging.info(f'Runtime test {name} without reference')
pool.put(
title,
['bmrt_test', '--loopnum', str(rounds), '--bmodel', bmodel],
['bmrt_test', iter_opt, str(rounds), '--bmodel', bmodel],
env=env, shell=False)
try:
pool.fire()
Expand All @@ -115,6 +121,8 @@ def run(tree, path, config, stat_f):
stats = parse_stats(f.read())
from math import nan
real_time = stats['calculate'] * 1000 if 'calculate' in stats else nan
if 'calculate_times' in iter_opt:
real_time /= rounds
row = [
config['name'],
stats['shape'],
Expand Down
6 changes: 4 additions & 2 deletions python/tpu_perf/subp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ def sys_memory_size():
return int(m.group(1))

class CommandExecutor:
def __init__(self, cwd, env):
def __init__(self, cwd, env, memory_hint = None):
if memory_hint is None:
memory_hint = 1024 * 1024 * 7
import os
self.env = os.environ.copy()
for v in env:
pair = v.split('=')
self.env[pair[0].strip()] = pair[1].strip() if len(pair) > 1 else ""
mem_size = sys_memory_size()
max_threads = max(1, int(mem_size / 1024 / 1024 / 4))
max_threads = max(1, int(mem_size / memory_hint))
self.threads = 4
if self.threads > max_threads:
self.threads = max_threads
Expand Down

0 comments on commit ce5c6a3

Please sign in to comment.