[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-22 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349726681
 
 

 ##
 File path: python/tvm/autotvm/config_library.py
 ##
 @@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.import datetime
+#pylint: disable=arguments-differ, method-hidden, 
inconsistent-return-statements
+"""Config Library to store tuning configs"""
+import json
+import os
+from _ctypes import PyObj_FromPtr
+from shutil import copyfile
+from pathlib import Path
+
+import numpy as np
+
+from . import record
+from .task import ApplyHistoryBest
+
+
+class ConfigLibraryException(Exception):
+pass
+
+
+class ConfigLibrary:
+"""A library to store auto-tuning results for any number of 
targets/workloads.
+
+Parameters
+--
+library_dir: str
+Path to the config library directory. If the library does not already
+exist, a new library will be initialised in the directory. This will
+create an 'index.json' file in the directory which contains the 
location
+of all other files used to store configs in the library.
+
+"""
+
+LIBRARY_INDEX_FILE_NAME = "index.json"
+JOBS_INDEX_FILE_NAME = "jobs.json"
+JOBS_DIRECTORY_NAME = "jobs"
+
+def __init__(self, library_dir):
+# Handle if the directory doesn't exist
+if not os.path.isdir(library_dir):
+os.mkdir(library_dir)
+
+index_file = os.path.join(library_dir, self.LIBRARY_INDEX_FILE_NAME)
+if not os.path.isfile(index_file):
+with open(index_file, "w") as f:
+full_index_path = os.path.abspath(f.name)
+index = {
+"root": os.path.dirname(full_index_path),
+"targets": {},
+"jobs_index": self.JOBS_INDEX_FILE_NAME,
+"jobs_dir": self.JOBS_DIRECTORY_NAME,
+}
+json.dump(index, f, indent=4)
+
+with open(os.path.join(library_dir, self.JOBS_INDEX_FILE_NAME), 
"w") as f:
+json.dump({}, f)
+
+self.library_dir = library_dir
+self.library_index = index_file
+self.jobs_dir = os.path.join(self.library_dir, 
self.JOBS_DIRECTORY_NAME)
+self.jobs_index = os.path.join(self.library_dir, 
self.JOBS_INDEX_FILE_NAME)
+if not os.path.isdir(self.jobs_dir):
+os.makedirs(self.jobs_dir)
+
+def load(self, target):
+"""Load the configs for a given TVM target string.
+
+Returns a DispatchContext with the appropriate configs loaded."""
+target_configs = self._load_target_configs(target)
+return ApplyHistoryBest(target_configs)
+
+def _load_target_configs(self, target):
+"""Yield the configs in the library for a given target."""
+target_file = self.get_config_file(target, create=False)
+if target_file:
+with open(target_file) as f:
+configs = json.load(f)
+for config in configs.values():
+row = json.dumps(config)
+yield record.decode(row)
+
+else:
+yield from []
+
+def save_job(self, job, save_history=True):
+"""Save the results of an auto-tuning job to the library.
+
+Parameters
+--
+job: TuningJob
+The auto-tuning job to save.
+save_history: bool
+Whether to save the history log of the job.
+
+"""
+with open(self.jobs_index, 'r+') as f:
+job_index = json.load(f)
+highest_job_id = 0
+for job_id in job_index:
+highest_job_id = max(highest_job_id, 
int(job_index[job_id]["id"]))
+
+job_id = str(highest_job_id + 1)
+job_log = None
+if save_history:
+job_log = self._create_job_log(job_id, job.target)
+copyfile(job.log, job_log)
+
+job_entry = {
+"id": job_id,
+"log": job_log,
+"target": job.target,
+"platform": job.platform,
+  

[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-21 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349295422
 
 

 ##
 File path: tests/python/unittest/test_autotvm_tuning_job.py
 ##
 @@ -0,0 +1,94 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test Tuning Job"""
+import copy
+import numpy as np
+import random
+import tempfile
+
+from tvm.autotvm.tuner.tuning_job import TuningJob
+
+from test_autotvm_common import get_sample_records
+
+
+class MockTuner:
+pass
+
+
+def test_log_configs():
+r = random.Random()
+r.seed(42)
 
 Review comment:
   ditto


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-21 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349293137
 
 

 ##
 File path: python/tvm/autotvm/tuner/tuner.py
 ##
 @@ -86,8 +87,7 @@ def update(self, inputs, results):
 result for measurement
 """
 
-
-def tune(self, n_trial, measure_option, early_stopping=None, callbacks=()):
+def tune(self, n_trial, measure_option, early_stopping=None, callbacks=(), 
allow_skipping=True):
 
 Review comment:
   I think the default value of `allow_skipping` should be `False`, which means 
users have to intentionally skip tasks.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-21 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349291485
 
 

 ##
 File path: python/tvm/autotvm/config_library.py
 ##
 @@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.import datetime
+#pylint: disable=arguments-differ, method-hidden, 
inconsistent-return-statements
+"""Config Library to store tuning configs"""
+import json
+import os
+from _ctypes import PyObj_FromPtr
+from shutil import copyfile
+from pathlib import Path
+
+import numpy as np
+
+from . import record
+from .task import ApplyHistoryBest
+
+
+class ConfigLibraryException(Exception):
+pass
 
 Review comment:
   Why we need this?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-21 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349291108
 
 

 ##
 File path: python/tvm/autotvm/config_library.py
 ##
 @@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.import datetime
+#pylint: disable=arguments-differ, method-hidden, 
inconsistent-return-statements
+"""Config Library to store tuning configs"""
+import json
+import os
+from _ctypes import PyObj_FromPtr
+from shutil import copyfile
+from pathlib import Path
+
+import numpy as np
+
+from . import record
+from .task import ApplyHistoryBest
+
+
+class ConfigLibraryException(Exception):
+pass
+
+
+class ConfigLibrary:
+"""A library to store auto-tuning results for any number of 
targets/workloads.
+
+Parameters
+--
+library_dir: str
+Path to the config library directory. If the library does not already
+exist, a new library will be initialised in the directory. This will
+create an 'index.json' file in the directory which contains the 
location
+of all other files used to store configs in the library.
+
+"""
+
+LIBRARY_INDEX_FILE_NAME = "index.json"
+JOBS_INDEX_FILE_NAME = "jobs.json"
+JOBS_DIRECTORY_NAME = "jobs"
+
+def __init__(self, library_dir):
+# Handle if the directory doesn't exist
+if not os.path.isdir(library_dir):
+os.mkdir(library_dir)
+
+index_file = os.path.join(library_dir, self.LIBRARY_INDEX_FILE_NAME)
+if not os.path.isfile(index_file):
+with open(index_file, "w") as f:
+full_index_path = os.path.abspath(f.name)
+index = {
+"root": os.path.dirname(full_index_path),
+"targets": {},
+"jobs_index": self.JOBS_INDEX_FILE_NAME,
+"jobs_dir": self.JOBS_DIRECTORY_NAME,
+}
+json.dump(index, f, indent=4)
+
+with open(os.path.join(library_dir, self.JOBS_INDEX_FILE_NAME), 
"w") as f:
+json.dump({}, f)
+
+self.library_dir = library_dir
+self.library_index = index_file
+self.jobs_dir = os.path.join(self.library_dir, 
self.JOBS_DIRECTORY_NAME)
+self.jobs_index = os.path.join(self.library_dir, 
self.JOBS_INDEX_FILE_NAME)
+if not os.path.isdir(self.jobs_dir):
+os.makedirs(self.jobs_dir)
+
+def load(self, target):
+"""Load the configs for a given TVM target string.
+
+Returns a DispatchContext with the appropriate configs loaded."""
+target_configs = self._load_target_configs(target)
+return ApplyHistoryBest(target_configs)
+
+def _load_target_configs(self, target):
+"""Yield the configs in the library for a given target."""
+target_file = self.get_config_file(target, create=False)
+if target_file:
+with open(target_file) as f:
+configs = json.load(f)
+for config in configs.values():
+row = json.dumps(config)
+yield record.decode(row)
+
+else:
+yield from []
+
+def save_job(self, job, save_history=True):
+"""Save the results of an auto-tuning job to the library.
+
+Parameters
+--
+job: TuningJob
+The auto-tuning job to save.
+save_history: bool
+Whether to save the history log of the job.
+
+"""
+with open(self.jobs_index, 'r+') as f:
+job_index = json.load(f)
+highest_job_id = 0
+for job_id in job_index:
+highest_job_id = max(highest_job_id, 
int(job_index[job_id]["id"]))
+
+job_id = str(highest_job_id + 1)
+job_log = None
+if save_history:
+job_log = self._create_job_log(job_id, job.target)
+copyfile(job.log, job_log)
+
+job_entry = {
+"id": job_id,
+"log": job_log,
+"target": job.target,
+"platform": job.platform,
+  

[GitHub] [incubator-tvm] comaniac commented on a change in pull request #4151: [AutoTVM] Added an autotuning Config Library to store autotune results

2019-11-21 Thread GitBox
comaniac commented on a change in pull request #4151: [AutoTVM] Added an 
autotuning Config Library to store autotune results
URL: https://github.com/apache/incubator-tvm/pull/4151#discussion_r349294111
 
 

 ##
 File path: tests/python/unittest/test_autotvm_config_library.py
 ##
 @@ -0,0 +1,252 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Test Config Library"""
+
+import copy
+import datetime
+import filecmp
+import json
+import os
+import random
+import tempfile
+
+from tvm.autotvm.config_library import ConfigLibrary
+from tvm.autotvm.task.dispatcher import ApplyHistoryBest
+from tvm.autotvm.tuner.callback import log_to_file
+from tvm.autotvm.record import encode
+
+from test_autotvm_common import get_sample_records
+
+
+def get_random_configs(n):
+records = get_sample_records(n)
+random_configs = []
+r = random.Random()
+r.seed(42)
 
 Review comment:
   You can hard code random configs, but do not use random generator in the 
unit tests (fixing the seed number doesn't guarantee the result is 
deterministic on different machines) sicne it is hard to catch the problem by 
all means.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services