comaniac commented on a change in pull request #6187:
URL: https://github.com/apache/incubator-tvm/pull/6187#discussion_r464690412



##########
File path: include/tvm/auto_scheduler/cost_model.h
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file tvm/auto_scheduler/cost_model.h
+ * \brief Cost models that estimate the performance of programs
+ */
+
+#ifndef TVM_AUTO_SCHEDULER_COST_MODEL_H_
+#define TVM_AUTO_SCHEDULER_COST_MODEL_H_
+
+#include <tvm/auto_scheduler/compute_dag.h>
+#include <tvm/auto_scheduler/measure.h>
+#include <tvm/node/node.h>
+#include <tvm/runtime/packed_func.h>
+
+#include <vector>
+
+namespace tvm {
+namespace auto_scheduler {
+
+using runtime::PackedFunc;
+using runtime::TypedPackedFunc;
+
+/*! \brief The base class for cost model */
+class CostModelNode : public Object {
+ public:
+  /*!
+   * \brief Update the cost model according to new measurement results 
(training data).
+   * \param inputs The measure inputs
+   * \param results The measure results
+   */
+  virtual void Update(const Array<MeasureInput>& inputs, const 
Array<MeasureResult>& results) = 0;
+
+  /*!
+   * \brief Predict the scores of states
+   * \param task The search task of states
+   * \param states The input states
+   * \param scores The predicted scores for all states
+   */
+  virtual void Predict(const SearchTask& task, const std::vector<State>& 
states,
+                       std::vector<float>* scores) = 0;
+
+  /*!
+   * \brief Predict the scores of all stages in states
+   * \param task The search task
+   * \param states The input states
+   * \param state_scores The predicted scores for all states
+   * \param stage_scores The predicted scores for all stages in all stages
+   */
+  virtual void PredictStages(const SearchTask& task, const std::vector<State>& 
states,

Review comment:
       @merrymercy would that be better to also make this as a virtual 
function, or setup a flag or something to indicate if this cost model supports 
`PredictStages` or not instead of just throwing an error?

##########
File path: python/tvm/auto_scheduler/cost_model/cost_model.py
##########
@@ -0,0 +1,142 @@
+# 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.
+
+""" Cost model that estimates the performance of programs """
+import ctypes
+import numpy as np
+
+import tvm._ffi
+from tvm.runtime import Object
+from .. import _ffi_api
+
+
+@tvm._ffi.register_object("auto_scheduler.CostModel")
+class CostModel(Object):
+    """The base class for cost model"""
+
+@tvm._ffi.register_object("auto_scheduler.RandomModel")
+class RandomModel(CostModel):
+    """A model returns random estimation for all inputs"""
+    def __init__(self):
+        self.__init_handle_by_constructor__(_ffi_api.RandomModel)
+
+    def update(self, inputs, results):
+        """Update the cost model according to new measurement results 
(training data).
+
+        Parameters
+        ----------
+        inputs : List[MeasureInput]
+            The measurement inputs
+        results : List[MeasureResult]
+            The measurement results
+        """
+        _ffi_api.CostModelUpdate(self, inputs, results)
+
+    def predict(self, search_task, states):
+        """Predict the scores of states
+
+        Parameters
+        ----------
+        search_task : SearchTask
+            The search task of states
+        statse : List[State]
+            The input states
+
+        Returns
+        -------
+        scores: List[float]
+            The predicted scores for all states
+        """
+        return [x.value for x in _ffi_api.CostModelPredict(self, search_task, 
states)]
+
+
+@tvm._ffi.register_func("auto_scheduler.cost_model.random_number")
+def random_number(n, return_ptr):
+    """ A random number generator func for c++'s RandomModel """
+    if n == 0:
+        return
+    return_ptr = ctypes.cast(return_ptr, ctypes.POINTER(ctypes.c_float))
+    array_wrapper = np.ctypeslib.as_array(return_ptr, shape=(n,))
+    array_wrapper[:] = np.random.uniform(0, 1, (n,))
+
+
+@tvm._ffi.register_object("auto_scheduler.PythonBasedModel")
+class PythonBasedModel(CostModel):
+    """Base class for cost models implemented in python"""
+    def __init__(self):
+        def update_func(inputs, results):
+            self.update(inputs, results)
+
+        def predict_func(task, states, return_ptr):
+            return_ptr = ctypes.cast(return_ptr, 
ctypes.POINTER(ctypes.c_float))
+            array_wrapper = np.ctypeslib.as_array(return_ptr, 
shape=(len(states),))
+            array_wrapper[:] = self.predict(task, states)
+
+        def predict_stage_func(task, states, return_ptr):
+            ret = self.predict_stages(task, states)
+            return_ptr = ctypes.cast(return_ptr, 
ctypes.POINTER(ctypes.c_float))
+            array_wrapper = np.ctypeslib.as_array(return_ptr, shape=ret.shape)
+            array_wrapper[:] = ret
+
+        self.__init_handle_by_constructor__(_ffi_api.PythonBasedModel, 
update_func,
+                                            predict_func, predict_stage_func)
+
+    def update(self, inputs, results):
+        """Update the cost model according to new measurement results 
(training data).
+
+        Parameters
+        ----------
+        inputs : List[MeasureInput]
+            The measurement inputs
+        results : List[MeasureResult]
+            The measurement results
+        """
+        raise NotImplementedError
+
+    def predict(self, task, states):
+        """Predict the scores of states
+
+        Parameters
+        ----------
+        search_task : SearchTask
+            The search task of states
+        statse : List[State]
+            The input states
+
+        Returns
+        -------
+        scores: List[float]
+            The predicted scores for all states
+        """
+        raise NotImplementedError
+
+    def predict_stages(self, task, states):
+        """Predict the scores of states

Review comment:
       ```suggestion
           """Predict the scores of states. Different from "predict", this 
function returns score breakdown for each stage.
   ```

##########
File path: python/tvm/auto_scheduler/cost_model/cost_model.py
##########
@@ -0,0 +1,142 @@
+# 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.
+
+""" Cost model that estimates the performance of programs """
+import ctypes
+import numpy as np
+
+import tvm._ffi
+from tvm.runtime import Object
+from .. import _ffi_api
+
+
+@tvm._ffi.register_object("auto_scheduler.CostModel")
+class CostModel(Object):
+    """The base class for cost model"""
+
+@tvm._ffi.register_object("auto_scheduler.RandomModel")
+class RandomModel(CostModel):
+    """A model returns random estimation for all inputs"""
+    def __init__(self):
+        self.__init_handle_by_constructor__(_ffi_api.RandomModel)
+
+    def update(self, inputs, results):
+        """Update the cost model according to new measurement results 
(training data).
+
+        Parameters
+        ----------
+        inputs : List[MeasureInput]
+            The measurement inputs
+        results : List[MeasureResult]
+            The measurement results
+        """
+        _ffi_api.CostModelUpdate(self, inputs, results)
+
+    def predict(self, search_task, states):
+        """Predict the scores of states
+
+        Parameters
+        ----------
+        search_task : SearchTask
+            The search task of states
+        statse : List[State]
+            The input states
+
+        Returns
+        -------
+        scores: List[float]
+            The predicted scores for all states
+        """
+        return [x.value for x in _ffi_api.CostModelPredict(self, search_task, 
states)]
+
+
+@tvm._ffi.register_func("auto_scheduler.cost_model.random_number")
+def random_number(n, return_ptr):
+    """ A random number generator func for c++'s RandomModel """
+    if n == 0:

Review comment:
       Better to improve the naming of `n`.




----------------------------------------------------------------
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


Reply via email to