Skip to content

Commit

Permalink
Add default config as json file (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikvadla authored Apr 15, 2019
1 parent 059dc96 commit 759608d
Show file tree
Hide file tree
Showing 70 changed files with 423 additions and 67 deletions.
24 changes: 19 additions & 5 deletions Contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ required:
![Benchmarks Directory Structure](benchmarks_directory_structure.png)

2. Next, in the leaf folder that was created in the previous step, you
will need to create a `model_init.py` file:
will need to create `config.json` and `model_init.py` files:

![Add model init](add_model_init.png)
![Add model init](add_model_init_and_config.png)

This file is used to initialize the best known configuration for the
The `config.json` file contains the best known KMP environment variable
settings to get optimal performance for the model. Below default settings are recommended for most of
the models in Model Zoo.

```
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
```
The `model_init.py` file is used to initialize the best known configuration for the
model, and then start executing inference or training. When the
[launch script](/docs/general/tensorflow/LaunchBenchmark.md) is run,
it will look for the appropriate `model_init.py` file to use
Expand All @@ -33,7 +47,7 @@ required:
[base model init class](/benchmarks/common/base_model_init.py) that
includes functions for doing common tasks such as setting up the best
known environment variables (like `KMP_BLOCKTIME`, `KMP_SETTINGS`,
`KMP_AFFINITY`, and `OMP_NUM_THREADS`), num intra threads, and num
`KMP_AFFINITY` by loading **config.json** and `OMP_NUM_THREADS`), num intra threads, and num
inter threads. The `model_init.py` file also sets up the string that
will ultimately be used to run inference or model training, which
normally includes the use of `numactl` and sending all of the
Expand Down Expand Up @@ -93,7 +107,7 @@ Optional step:
the original repository, then it can be added to the
[models](/models) directory in the zoo repo. As with the first step
in the previous section, the directory structure should be setup like:
`/models/<use case>/<framework>/<model name>/<mode>/<precision>`:
`/models/<use case>/<framework>/<model name>/<mode>/<precision>`.

![Models Directory Structure](models_directory_structure.png)

Expand Down
Binary file removed add_model_init.png
Binary file not shown.
Binary file added add_model_init_and_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified add_readme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1,
"KMP_HW_SUBSET": "1T"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def __init__(self, args, custom_args=[], platform_util=None):
self.set_num_inter_intra_threads()

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
set_env_var("KMP_HW_SUBSET", "1T")
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

benchmark_script = os.path.join(
self.args.intelai_models, args.mode, args.precision,
Expand Down
25 changes: 20 additions & 5 deletions benchmarks/common/base_model_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# SPDX-License-Identifier: EPL-2.0
#

import json
import os


Expand Down Expand Up @@ -135,14 +136,28 @@ def set_num_inter_intra_threads(self, num_inter_threads=None, num_intra_threads=
print("num_inter_threads: {}\nnum_intra_threads: {}".format(
self.args.num_inter_threads, self.args.num_intra_threads))

def set_kmp_vars(self, kmp_settings="1", kmp_blocktime="1", kmp_affinity="granularity=fine,verbose,compact,1,0"):
def set_kmp_vars(self, config_file_path, kmp_settings=None, kmp_blocktime=None, kmp_affinity=None):
"""
Sets KMP_* environment variables to the specified value, if the environment variable has not already been set.
The default values for this function's args are the most common values that we have seen in the model zoo.
The default values in the json file are the best known settings for the model.
"""
if os.path.exists(config_file_path):
with open(config_file_path, 'r') as config:
config_object = json.load(config)

# First sets default from config file
for param in config_object.keys():
for env in config_object[param].keys():
set_env_var(env, config_object[param][env])

else:
print("Warning: File {} does not exist and \
cannot be used to set KMP environment variables".format(config_file_path))

# Override user provided envs
if kmp_settings:
set_env_var("KMP_SETTINGS", kmp_settings)
set_env_var("KMP_SETTINGS", kmp_settings, overwrite_existing=True)
if kmp_blocktime:
set_env_var("KMP_BLOCKTIME", kmp_blocktime)
set_env_var("KMP_BLOCKTIME", kmp_blocktime, overwrite_existing=True)
if kmp_affinity:
set_env_var("KMP_AFFINITY", kmp_affinity)
set_env_var("KMP_AFFINITY", kmp_affinity, overwrite_existing=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1,
"KMP_HW_SUBSET": "1T"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import os
import sys
from common.base_model_init import BaseModelInitializer
from common.base_model_init import set_env_var


class ModelInitializer(BaseModelInitializer):
Expand All @@ -32,8 +31,8 @@ def __init__(self, args, custom_args=[], platform_util=None):
super(ModelInitializer, self).__init__(args, custom_args, platform_util)

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
set_env_var("KMP_HW_SUBSET", "1T")
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

if self.args.accuracy_only:
print("Accuracy testing for DRAW inference is not supported yet.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self, args, custom_args=[], platform_util=None):
self.python_exe + " "

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

pairs_file = os.path.join(self.args.model_source_dir,
"data/pairs.txt")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def __init__(self, args, custom_args, platform_util=None):
self.set_num_inter_intra_threads()

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def __init__(self, args, custom_args=[], platform_util=None):
self.cmd = self.get_numactl_command(self.args.socket_id) + self.python_exe + " "

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

# use default batch size if -1
if self.args.batch_size == -1:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class ModelInitializer(BaseModelInitializer):

def __init__(self, args, custom_args=[], platform_util=None):
super(ModelInitializer, self).__init__(args, custom_args, platform_util)
self.set_kmp_vars()

# Set KMP env vars, if they haven't already been set
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

self.cmd = self.get_numactl_command(self.args.socket_id) + "{} ".format(self.python_exe)

# use default batch size if -1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def __init__(self, args, custom_args=[], platform_util=None):

self.args = arg_parser.parse_args(self.custom_args, namespace=self.args)

# Use default KMP variable values, but override the default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime=str(self.args.kmp_blocktime))
# Set KMP env vars, if they haven't already been set, but override the default KMP_BLOCKTIME value
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path, kmp_blocktime=str(self.args.kmp_blocktime))

set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads)

benchmark_script = os.path.join(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def parse_args(self):

self.args = parser.parse_args(self.custom_args,
namespace=self.args)
# Use default KMP variable values, but override the default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime=str(self.args.kmp_blocktime))
# Set KMP env vars, if they haven't already been set, but override the default KMP_BLOCKTIME value
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path, kmp_blocktime=str(self.args.kmp_blocktime))

def run_benchmark(self):
benchmark_script = os.path.join(self.args.intelai_models,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 0,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def __init__(self, args, custom_args=[], platform_util=None):
# Environment variables
set_env_var("OMP_NUM_THREADS", platform_util.num_cores_per_socket
if self.args.num_cores == -1 else self.args.num_cores)
self.set_kmp_vars(kmp_blocktime="0")

# Set KMP env vars, if they haven't already been set
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

self.set_num_inter_intra_threads(num_inter_threads=platform_util.num_threads_per_core,
num_intra_threads=platform_util.num_cores_per_socket)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def __init__(self, args, custom_args=[], platform_util=None):
if self.args.batch_size == -1:
self.args.batch_size = 128

# Set KMP env vars (except KMP_SETTINGS is not set)
self.set_kmp_vars(kmp_settings=None)
# Set KMP env vars, if they haven't already been set
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

# set num_inter_threads and num_intra_threads (override inter threads to 2)
self.set_num_inter_intra_threads(num_inter_threads=2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def __init__(self, args, custom_args=[], platform_util=None):
self.cmd = self.get_numactl_command(self.args.socket_id) + "python "

# Set KMP env vars, if they haven't already been set
self.set_kmp_vars()
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

# Set the num_inter_threads and num_intra_threads
self.set_num_inter_intra_threads()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ def __init__(self, args, custom_args=[], platform_util=None):

self.args = arg_parser.parse_args(self.custom_args, namespace=self.args)

# Use default KMP variable values, but override the default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime=str(self.args.kmp_blocktime))
# Set KMP env vars, if they haven't already been set, but override the default KMP_BLOCKTIME value
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path, kmp_blocktime=str(self.args.kmp_blocktime))

set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads)

benchmark_script = os.path.join(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 0,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ def __init__(self, args, custom_args=[], platform_util=None):
set_env_var("OMP_NUM_THREADS",
platform_util.num_cores_per_socket if args.num_cores == -1 else args.num_cores)

# Set KMP env vars, but override default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime="0")
# Set KMP env vars, if they haven't already been set
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path)

def parse_args(self):
parser = argparse.ArgumentParser()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def __init__(self, args, custom_args=[], platform_util=None):

self.args = arg_parser.parse_args(self.custom_args, namespace=self.args)

# Use default KMP variable values, but override the default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime=str(self.args.kmp_blocktime))
# Set KMP env vars, if they haven't already been set, but override the default KMP_BLOCKTIME value
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path, kmp_blocktime=str(self.args.kmp_blocktime))

set_env_var("OMP_NUM_THREADS", self.args.num_intra_threads)

benchmark_script = os.path.join(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"optimization_parameters": {
"KMP_AFFINITY": "granularity=fine,verbose,compact,1,0",
"KMP_BLOCKTIME": 1,
"KMP_SETTINGS": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def parse_args(self):

self.args = parser.parse_args(self.custom_args,
namespace=self.args)
# Use default KMP variable values, but override the default KMP_BLOCKTIME value
self.set_kmp_vars(kmp_blocktime=str(self.args.kmp_blocktime))
# Set KMP env vars, if they haven't already been set, but override the default KMP_BLOCKTIME value
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
self.set_kmp_vars(config_file_path, kmp_blocktime=str(self.args.kmp_blocktime))

def run_benchmark_or_accuracy(self):
cmd = os.path.join(
Expand Down
Loading

0 comments on commit 759608d

Please sign in to comment.