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



##########
File path: include/tvm/auto_scheduler/feature.h
##########
@@ -0,0 +1,122 @@
+/*
+ * 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 auto_scheduler/feature.h
+ * \brief Feature extraction for the cost model.
+ * We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+ * so we call this feature as "Per Store" feature.
+ * The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+ * the predictions as the whole score for a TVM IR (Stmt).
+ *
+ * The feature specification is defined by `src/auto_scheduler/feature.cc:: 
FeatureSet`
+ */
+
+#ifndef TVM_AUTO_SCHEDULER_FEATURE_H_
+#define TVM_AUTO_SCHEDULER_FEATURE_H_
+
+#include <tvm/auto_scheduler/compute_dag.h>
+#include <tvm/auto_scheduler/measure.h>
+
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace auto_scheduler {
+
+/*!
+ * \brief Get PerStore feature from a TIR Stmt
+ * \param stmt The input lowered TIR statement
+ * \param cache_line_size The size of cache line in bytes
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned feature vector
+ */
+void GetPerStoreFeature(const Stmt& stmt, int cache_line_size, int max_n_bufs,
+                        std::vector<float>* ret);
+
+/*
+ * \brief Get the names of elements in the feature vector. Use this for debug 
and inspection.
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned names.
+ */
+void GetPerStoreFeatureName(int max_n_bufs, std::vector<std::string>* ret);
+
+/*!
+ * \brief Get PerStore feature from states and the same task
+ * \param states The input states
+ * \param task The same search task for all states
+ * \param skip_first_n_feature_extraction Skip feature extraction for the 
first n states
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param features The returned feature vector. The innermost vector contains 
the
+ * feature vectors for all BufferStoreNode statements
+ */
+void GetPerStoreFeaturesFromStates(const Array<State>& states, const 
SearchTask& task,
+                                   int skip_first_n_feature_extraction, int 
max_n_bufs,
+                                   std::vector<std::vector<float> >* features);
+
+/*!
+ * \brief Get PerStore feature from states and different tasks

Review comment:
       s/and/of/

##########
File path: include/tvm/auto_scheduler/feature.h
##########
@@ -0,0 +1,122 @@
+/*
+ * 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 auto_scheduler/feature.h
+ * \brief Feature extraction for the cost model.
+ * We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+ * so we call this feature as "Per Store" feature.
+ * The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+ * the predictions as the whole score for a TVM IR (Stmt).
+ *
+ * The feature specification is defined by `src/auto_scheduler/feature.cc:: 
FeatureSet`
+ */
+
+#ifndef TVM_AUTO_SCHEDULER_FEATURE_H_
+#define TVM_AUTO_SCHEDULER_FEATURE_H_
+
+#include <tvm/auto_scheduler/compute_dag.h>
+#include <tvm/auto_scheduler/measure.h>
+
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace auto_scheduler {
+
+/*!
+ * \brief Get PerStore feature from a TIR Stmt
+ * \param stmt The input lowered TIR statement
+ * \param cache_line_size The size of cache line in bytes
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned feature vector
+ */
+void GetPerStoreFeature(const Stmt& stmt, int cache_line_size, int max_n_bufs,
+                        std::vector<float>* ret);
+
+/*
+ * \brief Get the names of elements in the feature vector. Use this for debug 
and inspection.
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned names.
+ */
+void GetPerStoreFeatureName(int max_n_bufs, std::vector<std::string>* ret);
+
+/*!
+ * \brief Get PerStore feature from states and the same task

Review comment:
       s/and/of/

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -0,0 +1,241 @@
+# 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.
+
+""""
+Python API for Feature extraction. The extracted features vector are used by 
cost models.
+
+We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+so we call this feature as "Per Store" feature.
+The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+the predictions as the whole score for a TVM IR (Stmt).
+
+The feature specification is defined by 
`src/auto_scheduler/feature.cc::FeatureSet`
+"""
+
+from typing import List, Tuple
+import struct
+
+import numpy as np
+
+from .loop_state import State, StateObject
+from .measure import MeasureInput, MeasureResult
+from . import _ffi_api
+
+# The maximum number of extracted buffers for one statement
+DEFAULT_MAX_N_BUFS = 5
+
+# The length of the feature vector
+DEFAULT_FEATURE_VEC_LEN = 164
+
+
+def unpack_feature(byte_arr: bytearray) -> Tuple[np.ndarray, np.ndarray, 
np.ndarray]:
+    """Unpack the flatten feature (in byte array format) from c++
+
+    Parameters
+    ----------
+    byte_arr: bytearray
+        The two-dimensional feature vector in serialized byte array format
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    size_of_int = 4
+    size_of_float = 4

Review comment:
       Shuld those be globally static?

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -0,0 +1,241 @@
+# 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.
+
+""""
+Python API for Feature extraction. The extracted features vector are used by 
cost models.
+
+We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+so we call this feature as "Per Store" feature.
+The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+the predictions as the whole score for a TVM IR (Stmt).
+
+The feature specification is defined by 
`src/auto_scheduler/feature.cc::FeatureSet`
+"""
+
+from typing import List, Tuple
+import struct
+
+import numpy as np
+
+from .loop_state import State, StateObject
+from .measure import MeasureInput, MeasureResult
+from . import _ffi_api
+
+# The maximum number of extracted buffers for one statement
+DEFAULT_MAX_N_BUFS = 5
+
+# The length of the feature vector
+DEFAULT_FEATURE_VEC_LEN = 164
+
+
+def unpack_feature(byte_arr: bytearray) -> Tuple[np.ndarray, np.ndarray, 
np.ndarray]:
+    """Unpack the flatten feature (in byte array format) from c++
+
+    Parameters
+    ----------
+    byte_arr: bytearray
+        The two-dimensional feature vector in serialized byte array format
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    size_of_int = 4
+    size_of_float = 4
+
+    # The format for n records is:
+    # {
+    #   int n;
+    #   int[n+2] sizes
+
+    #   float[sizes[0]]    feature for record 1
+    #   float[sizes[1]]    feature for record 2
+    #   ...                feature for record i...
+    #   float[sizes[n-1]]  feature for record n
+
+    #   float[sizes[n]]    normalized throughput for n records
+    #   int[sizes[n+1]]    task id for n records
+    # }
+
+    vec_len = DEFAULT_FEATURE_VEC_LEN
+
+    # unpack sizes
+    offset = 0
+    n = struct.unpack_from("1i", byte_arr, offset=offset)[0]
+    offset += size_of_int
+
+    sizes = struct.unpack_from("%di" % (n+2), byte_arr, offset=offset)
+    offset += size_of_int * (n+2)
+
+    # unpack features
+    features = []
+    for size in sizes[:-2]:
+        row = []
+
+        # Now, we need to unpack the feature for multiple statements.
+        # The format is:
+        # {
+        #     int n_stmts
+        #     float[n_stmt][vec_len] feature_vecs
+        # }
+        # where vec_len can be calculated by `(size - 1) / n_stmts`
+
+        if size == 0:
+            # failed during lowering
+            features.append(np.zeros((1, vec_len)))
+        else:
+            n_stmts = struct.unpack_from("f", byte_arr, offset=offset)
+            offset += size_of_float
+
+            n_stmts = int(n_stmts[0] + 0.5)
+            tmp_vec_len = (size - 1) // n_stmts
+            assert tmp_vec_len == vec_len, "The lenght of feature vector is 
wrong. " \
+                                           "Expected %d but got %d." % 
(vec_len, tmp_vec_len)
+            assert (size - 1) % n_stmts == 0
+            for _ in range(n_stmts):
+                x = struct.unpack_from("%df" % vec_len, byte_arr, 
offset=offset)
+                offset += vec_len * size_of_float
+                row.append(x)
+
+            features.append(np.array(row))
+
+    # unpack normalized_throughputs
+    m = sizes[-2]
+    normalized_throughputs = struct.unpack_from("%df" % m, byte_arr, 
offset=offset)
+    offset += m * size_of_int
+
+    # unpack task_ids
+    m = sizes[-1]
+    task_ids = struct.unpack_from("%di" % m, byte_arr, offset=offset)
+    offset += m * size_of_int
+
+    assert offset == len(byte_arr), "%d vs %d" % (offset, len(byte_arr))
+    return np.array(features, dtype=object), np.array(normalized_throughputs), 
np.array(task_ids)
+
+
+def get_per_store_features_from_file(filename: str,
+                                     n_lines: int,
+                                     max_n_bufs: int = None) \

Review comment:
       The type of `max_n_bufs` should be `Optional[int]`. Ditto to the rest 
functions in this file.

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -0,0 +1,241 @@
+# 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.
+
+""""
+Python API for Feature extraction. The extracted features vector are used by 
cost models.
+
+We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+so we call this feature as "Per Store" feature.
+The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+the predictions as the whole score for a TVM IR (Stmt).

Review comment:
       ```suggestion
   the predicted score of each BufferStoreNode as the score of a TIR Stmt.
   ```

##########
File path: include/tvm/auto_scheduler/feature.h
##########
@@ -0,0 +1,122 @@
+/*
+ * 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 auto_scheduler/feature.h
+ * \brief Feature extraction for the cost model.
+ * We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+ * so we call this feature as "Per Store" feature.
+ * The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+ * the predictions as the whole score for a TVM IR (Stmt).
+ *
+ * The feature specification is defined by `src/auto_scheduler/feature.cc:: 
FeatureSet`
+ */
+
+#ifndef TVM_AUTO_SCHEDULER_FEATURE_H_
+#define TVM_AUTO_SCHEDULER_FEATURE_H_
+
+#include <tvm/auto_scheduler/compute_dag.h>
+#include <tvm/auto_scheduler/measure.h>
+
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace auto_scheduler {
+
+/*!
+ * \brief Get PerStore feature from a TIR Stmt
+ * \param stmt The input lowered TIR statement
+ * \param cache_line_size The size of cache line in bytes
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned feature vector
+ */
+void GetPerStoreFeature(const Stmt& stmt, int cache_line_size, int max_n_bufs,
+                        std::vector<float>* ret);
+
+/*
+ * \brief Get the names of elements in the feature vector. Use this for debug 
and inspection.
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned names.
+ */
+void GetPerStoreFeatureName(int max_n_bufs, std::vector<std::string>* ret);
+
+/*!
+ * \brief Get PerStore feature from states and the same task
+ * \param states The input states
+ * \param task The same search task for all states
+ * \param skip_first_n_feature_extraction Skip feature extraction for the 
first n states
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param features The returned feature vector. The innermost vector contains 
the
+ * feature vectors for all BufferStoreNode statements
+ */
+void GetPerStoreFeaturesFromStates(const Array<State>& states, const 
SearchTask& task,
+                                   int skip_first_n_feature_extraction, int 
max_n_bufs,
+                                   std::vector<std::vector<float> >* features);
+
+/*!
+ * \brief Get PerStore feature from states and different tasks
+ * \param states The input states
+ * \param tasks The search tasks for different states
+ * \param skip_first_n_feature_extraction Skip feature extraction for the 
first n states
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param features The returned feature vector. The innermost vector contains 
the
+ * feature vectors for all BufferStoreNode statements
+ */
+void GetPerStoreFeaturesFromStates(const Array<State>& states, const 
std::vector<SearchTask>& tasks,
+                                   int skip_first_n_feature_extraction, int 
max_n_bufs,
+                                   std::vector<std::vector<float> >* features);
+
+/*!
+ * \brief Get PerStore features from a log file
+ * \param filename The name of log file
+ * \param n_lines Only read the first n lines

Review comment:
       Maybe `max_line_num` or `max_lines` would be better?

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -0,0 +1,241 @@
+# 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.
+
+""""
+Python API for Feature extraction. The extracted features vector are used by 
cost models.
+
+We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+so we call this feature as "Per Store" feature.
+The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+the predictions as the whole score for a TVM IR (Stmt).
+
+The feature specification is defined by 
`src/auto_scheduler/feature.cc::FeatureSet`
+"""
+
+from typing import List, Tuple
+import struct
+
+import numpy as np
+
+from .loop_state import State, StateObject
+from .measure import MeasureInput, MeasureResult
+from . import _ffi_api
+
+# The maximum number of extracted buffers for one statement
+DEFAULT_MAX_N_BUFS = 5
+
+# The length of the feature vector
+DEFAULT_FEATURE_VEC_LEN = 164
+
+
+def unpack_feature(byte_arr: bytearray) -> Tuple[np.ndarray, np.ndarray, 
np.ndarray]:
+    """Unpack the flatten feature (in byte array format) from c++
+
+    Parameters
+    ----------
+    byte_arr: bytearray
+        The two-dimensional feature vector in serialized byte array format
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    size_of_int = 4
+    size_of_float = 4
+
+    # The format for n records is:
+    # {
+    #   int n;
+    #   int[n+2] sizes
+
+    #   float[sizes[0]]    feature for record 1
+    #   float[sizes[1]]    feature for record 2
+    #   ...                feature for record i...
+    #   float[sizes[n-1]]  feature for record n
+
+    #   float[sizes[n]]    normalized throughput for n records
+    #   int[sizes[n+1]]    task id for n records
+    # }
+
+    vec_len = DEFAULT_FEATURE_VEC_LEN
+
+    # unpack sizes
+    offset = 0
+    n = struct.unpack_from("1i", byte_arr, offset=offset)[0]
+    offset += size_of_int
+
+    sizes = struct.unpack_from("%di" % (n+2), byte_arr, offset=offset)
+    offset += size_of_int * (n+2)
+
+    # unpack features
+    features = []
+    for size in sizes[:-2]:
+        row = []
+
+        # Now, we need to unpack the feature for multiple statements.
+        # The format is:
+        # {
+        #     int n_stmts
+        #     float[n_stmt][vec_len] feature_vecs
+        # }
+        # where vec_len can be calculated by `(size - 1) / n_stmts`
+
+        if size == 0:
+            # failed during lowering
+            features.append(np.zeros((1, vec_len)))
+        else:
+            n_stmts = struct.unpack_from("f", byte_arr, offset=offset)
+            offset += size_of_float
+
+            n_stmts = int(n_stmts[0] + 0.5)
+            tmp_vec_len = (size - 1) // n_stmts
+            assert tmp_vec_len == vec_len, "The lenght of feature vector is 
wrong. " \
+                                           "Expected %d but got %d." % 
(vec_len, tmp_vec_len)
+            assert (size - 1) % n_stmts == 0
+            for _ in range(n_stmts):
+                x = struct.unpack_from("%df" % vec_len, byte_arr, 
offset=offset)
+                offset += vec_len * size_of_float
+                row.append(x)
+
+            features.append(np.array(row))
+
+    # unpack normalized_throughputs
+    m = sizes[-2]
+    normalized_throughputs = struct.unpack_from("%df" % m, byte_arr, 
offset=offset)
+    offset += m * size_of_int
+
+    # unpack task_ids
+    m = sizes[-1]
+    task_ids = struct.unpack_from("%di" % m, byte_arr, offset=offset)
+    offset += m * size_of_int
+
+    assert offset == len(byte_arr), "%d vs %d" % (offset, len(byte_arr))
+    return np.array(features, dtype=object), np.array(normalized_throughputs), 
np.array(task_ids)
+
+
+def get_per_store_features_from_file(filename: str,
+                                     n_lines: int,
+                                     max_n_bufs: int = None) \
+        -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
+    """Get per_store features from a log file
+
+    Parameters
+    ----------
+    filename: str
+        The input filename
+    n_lines: int

Review comment:
       ditto

##########
File path: include/tvm/auto_scheduler/feature.h
##########
@@ -0,0 +1,122 @@
+/*
+ * 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 auto_scheduler/feature.h
+ * \brief Feature extraction for the cost model.
+ * We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+ * so we call this feature as "Per Store" feature.
+ * The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+ * the predictions as the whole score for a TVM IR (Stmt).
+ *
+ * The feature specification is defined by `src/auto_scheduler/feature.cc:: 
FeatureSet`
+ */
+
+#ifndef TVM_AUTO_SCHEDULER_FEATURE_H_
+#define TVM_AUTO_SCHEDULER_FEATURE_H_
+
+#include <tvm/auto_scheduler/compute_dag.h>
+#include <tvm/auto_scheduler/measure.h>
+
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace auto_scheduler {
+
+/*!
+ * \brief Get PerStore feature from a TIR Stmt
+ * \param stmt The input lowered TIR statement
+ * \param cache_line_size The size of cache line in bytes
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned feature vector
+ */
+void GetPerStoreFeature(const Stmt& stmt, int cache_line_size, int max_n_bufs,
+                        std::vector<float>* ret);
+
+/*
+ * \brief Get the names of elements in the feature vector. Use this for debug 
and inspection.
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param ret The returned names.
+ */
+void GetPerStoreFeatureName(int max_n_bufs, std::vector<std::string>* ret);
+
+/*!
+ * \brief Get PerStore feature from states and the same task
+ * \param states The input states
+ * \param task The same search task for all states
+ * \param skip_first_n_feature_extraction Skip feature extraction for the 
first n states
+ * \param max_n_bufs The maximum number of extracted buffers for one statement
+ * \param features The returned feature vector. The innermost vector contains 
the
+ * feature vectors for all BufferStoreNode statements
+ */
+void GetPerStoreFeaturesFromStates(const Array<State>& states, const 
SearchTask& task,
+                                   int skip_first_n_feature_extraction, int 
max_n_bufs,
+                                   std::vector<std::vector<float> >* features);
+
+/*!
+ * \brief Get PerStore feature from states and different tasks
+ * \param states The input states
+ * \param tasks The search tasks for different states

Review comment:
       ```suggestion
    * \param tasks The search tasks corresponding to the input states
   ```

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -0,0 +1,241 @@
+# 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.
+
+""""
+Python API for Feature extraction. The extracted features vector are used by 
cost models.
+
+We extract one feature vector per BufferStoreNode statement in a TIR Stmt,
+so we call this feature as "Per Store" feature.
+The cost model also does prediction for each BufferStoreNode statement and 
aggregates
+the predictions as the whole score for a TVM IR (Stmt).
+
+The feature specification is defined by 
`src/auto_scheduler/feature.cc::FeatureSet`
+"""
+
+from typing import List, Tuple
+import struct
+
+import numpy as np
+
+from .loop_state import State, StateObject
+from .measure import MeasureInput, MeasureResult
+from . import _ffi_api
+
+# The maximum number of extracted buffers for one statement
+DEFAULT_MAX_N_BUFS = 5
+
+# The length of the feature vector
+DEFAULT_FEATURE_VEC_LEN = 164
+
+
+def unpack_feature(byte_arr: bytearray) -> Tuple[np.ndarray, np.ndarray, 
np.ndarray]:
+    """Unpack the flatten feature (in byte array format) from c++
+
+    Parameters
+    ----------
+    byte_arr: bytearray
+        The two-dimensional feature vector in serialized byte array format
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    size_of_int = 4
+    size_of_float = 4
+
+    # The format for n records is:
+    # {
+    #   int n;
+    #   int[n+2] sizes
+
+    #   float[sizes[0]]    feature for record 1
+    #   float[sizes[1]]    feature for record 2
+    #   ...                feature for record i...
+    #   float[sizes[n-1]]  feature for record n
+
+    #   float[sizes[n]]    normalized throughput for n records
+    #   int[sizes[n+1]]    task id for n records
+    # }
+
+    vec_len = DEFAULT_FEATURE_VEC_LEN
+
+    # unpack sizes
+    offset = 0
+    n = struct.unpack_from("1i", byte_arr, offset=offset)[0]
+    offset += size_of_int
+
+    sizes = struct.unpack_from("%di" % (n+2), byte_arr, offset=offset)
+    offset += size_of_int * (n+2)
+
+    # unpack features
+    features = []
+    for size in sizes[:-2]:
+        row = []
+
+        # Now, we need to unpack the feature for multiple statements.
+        # The format is:
+        # {
+        #     int n_stmts
+        #     float[n_stmt][vec_len] feature_vecs
+        # }
+        # where vec_len can be calculated by `(size - 1) / n_stmts`
+
+        if size == 0:
+            # failed during lowering
+            features.append(np.zeros((1, vec_len)))
+        else:
+            n_stmts = struct.unpack_from("f", byte_arr, offset=offset)
+            offset += size_of_float
+
+            n_stmts = int(n_stmts[0] + 0.5)
+            tmp_vec_len = (size - 1) // n_stmts
+            assert tmp_vec_len == vec_len, "The lenght of feature vector is 
wrong. " \
+                                           "Expected %d but got %d." % 
(vec_len, tmp_vec_len)
+            assert (size - 1) % n_stmts == 0
+            for _ in range(n_stmts):
+                x = struct.unpack_from("%df" % vec_len, byte_arr, 
offset=offset)
+                offset += vec_len * size_of_float
+                row.append(x)
+
+            features.append(np.array(row))
+
+    # unpack normalized_throughputs
+    m = sizes[-2]
+    normalized_throughputs = struct.unpack_from("%df" % m, byte_arr, 
offset=offset)
+    offset += m * size_of_int
+
+    # unpack task_ids
+    m = sizes[-1]
+    task_ids = struct.unpack_from("%di" % m, byte_arr, offset=offset)
+    offset += m * size_of_int
+
+    assert offset == len(byte_arr), "%d vs %d" % (offset, len(byte_arr))
+    return np.array(features, dtype=object), np.array(normalized_throughputs), 
np.array(task_ids)
+
+
+def get_per_store_features_from_file(filename: str,
+                                     n_lines: int,
+                                     max_n_bufs: int = None) \
+        -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
+    """Get per_store features from a log file
+
+    Parameters
+    ----------
+    filename: str
+        The input filename
+    n_lines: int
+        Only extract the first n lines in the file
+    max_n_bufs: int
+        The maximum number of extracted buffers for one statement
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    byte_arr = _ffi_api.GetPerStoreFeaturesFromFile(
+        filename, n_lines, max_n_bufs or DEFAULT_MAX_N_BUFS)
+    return unpack_feature(byte_arr)
+
+
+def get_per_store_features_from_measure_pairs(inputs: List[MeasureInput],
+                                              results: List[MeasureResult],
+                                              skip_first_n_feature_extraction: 
int = 0,
+                                              max_n_bufs: int = None) \
+        -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
+    """Get per_store features from measurement input/result pairs
+
+    Parameters
+    ----------
+    inputs: List[MeasureInput]
+        The measure inputs
+    results: List[MeasureResult]
+        The measure results
+    skip_first_n_feature_extraction: int
+        Skip feature extraction for the first n states
+    max_n_bufs: int
+        The maximum number of extracted buffers for one statement
+
+    Returns
+    -------
+    features: np.ndarray
+        Feature vectors
+    normalized_throughputs: np.ndarray
+        Normalized throughputs
+    task_ids: np.ndarray
+        Task ids
+    """
+    byte_arr = _ffi_api.GetPerStoreFeaturesFromMeasurePairs(
+        inputs, results, skip_first_n_feature_extraction, max_n_bufs or 
DEFAULT_MAX_N_BUFS)
+    return unpack_feature(byte_arr)
+
+
+def get_per_store_features_from_states(states,

Review comment:
       Is that any reason of not annotating the type of `states` here?




----------------------------------------------------------------
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:
[email protected]


Reply via email to