You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/03/02 21:16:12 UTC

[GitHub] [tvm] tkonolige opened a new pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

tkonolige opened a new pull request #10455:
URL: https://github.com/apache/tvm/pull/10455


   Allow users to directly extract features from a PrimFunc. Extracted features can be used to get an estimate of flops, memory load size, or arithmetic intensity of a PrimFunc. I've improved support to handle PrimFuncs created from a full lowering pass in addition to the limited lowering that auto_scheduler uses.
   
   Also fix feature extraction to correctly measure the number of arithmetic operations width vector datatypes.
   
   This change will not change features used by auto_scheduler's cost models.
   
   @junrushao1994 @merrymercy @masahi 


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#issuecomment-1058483664


   cc @comaniac 


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tkonolige commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
tkonolige commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822959339



##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -252,3 +253,78 @@ def get_per_store_feature_names(max_n_bufs: Optional[int] = None) -> List[str]:
         The names of elements in the flatten feature vector
     """
     return _ffi_api.GetPerStoreFeatureNames(max_n_bufs or DEFAULT_MAX_N_BUFS)
+
+
+def primfunc_features(

Review comment:
       Renamed to `features_from_primfunc`

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -252,3 +253,78 @@ def get_per_store_feature_names(max_n_bufs: Optional[int] = None) -> List[str]:
         The names of elements in the flatten feature vector
     """
     return _ffi_api.GetPerStoreFeatureNames(max_n_bufs or DEFAULT_MAX_N_BUFS)
+
+
+def primfunc_features(
+    func: PrimFunc,
+    cache_line_bytes: int = 64,
+    max_n_bufs: Optional[int] = None,
+    log_scale: bool = False,
+) -> np.ndarray:
+    """Extract performance features from a PrimFunc.
+
+    Parameters
+    ----------
+    func: PrimFunc
+        PrimFunc from which features will be extracted. Each store operation to
+        a unique buffer in the function will result in one row of features in
+        the output.
+
+    cache_line_bytes: int, optional
+        Size of a cache line in bytes. Defaults to 64 which is the size for
+        most x86 processors.
+
+    max_n_bufs: int, optional
+        Maximum number of buffers in generated features. This determines the
+        length of the resulting feature vector.
+
+    log_scale: bool
+        Should entries in the feature vector be scaled by log2(x + 1). Defaults
+        to False. Use True if using features with a cost model.
+
+    Returns
+    -------
+    np.ndarray
+        Output features, one row per store into a unique buffer statement in `func`.
+    """
+    return _ffi_api.FeaturesFromPrimFunc(
+        func, cache_line_bytes, max_n_bufs or DEFAULT_MAX_N_BUFS, log_scale
+    ).numpy()
+
+
+def named_primfunc_features(

Review comment:
       Renamed to `named_features_from_primfunc`




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] Lunderberg commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
Lunderberg commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r823784663



##########
File path: tests/python/unittest/test_auto_scheduler_feature.py
##########
@@ -200,6 +201,35 @@ def test_gpu_feature():
         assert fequal(fea_dicts[0]["is_gpu"], 1.0)
 
 
+@T.prim_func
+def tir_matmul(
+    A: T.Buffer[(128, 128), "float32"],
+    B: T.Buffer[(128, 128), "float32"],
+    C: T.Buffer[(128, 128), "float32"],
+) -> None:
+    T.func_attr({"from_legacy_te_schedule": True, "global_symbol": "main", "tir.noalias": True})
+    for y in T.serial(128):
+        T.store(C.data, T.ramp(y, 128, 128), T.broadcast(T.float32(0), 128), T.broadcast(True, 128))
+        for k in T.serial(128):
+            T.store(
+                C.data,
+                T.ramp(y, 128, 128),
+                T.load("float32x128", C.data, T.ramp(y, 128, 128), T.broadcast(True, 128))
+                + T.load("float32x128", A.data, T.ramp(k, 128, 128), T.broadcast(True, 128))
+                * T.broadcast(T.load("float32", B.data, y * 128 + k), 128),
+                T.broadcast(True, 128),
+            )

Review comment:
       :+1: 




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tkonolige commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
tkonolige commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822958745



##########
File path: src/auto_scheduler/feature.cc
##########
@@ -659,38 +697,86 @@ class PerStoreFeatureExtractor : public StmtExprVisitor {
     }
   }
 
+  void VisitExpr_(const BufferLoadNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+    StmtExprVisitor::VisitExpr_(node);
+  }
+
   void VisitStmt_(const BufferStoreNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+
     MathOpCounter math_op_counter;
     math_op_counter(node->value);
     std::vector<float> mem_bytes_list;
     std::vector<float> compute_ops_list;
     double cur_compute_ops;
 
     // Group 1: Computation related features
-    ExtractComputationFeature(node, math_op_counter);
+    ExtractComputationFeature(node->buffer->data, node->indices, math_op_counter);
 
     // Group 2: Buffer access related features (per buffer)
-    ExtractBufferAccessFeature(node, math_op_counter, &cur_compute_ops, &compute_ops_list,
-                               &mem_bytes_list);
+    ExtractBufferAccessFeature(node->buffer->data, node->indices, node->value, math_op_counter,
+                               &cur_compute_ops, &compute_ops_list, &mem_bytes_list);
 
     // Group 3: Arithmetic intensity related features
-    ExtractArithmeticIntensityFeature(node, cur_compute_ops, compute_ops_list, mem_bytes_list);
+    ExtractArithmeticIntensityFeature(node->buffer->data, cur_compute_ops, compute_ops_list,
+                                      mem_bytes_list);
 
     // Group 4: Allocation related features
-    ExtractOuterScopeFeature(node);
+    ExtractOuterScopeFeature(node->buffer->data);
   }
 
   void VisitStmt_(const BufferRealizeNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
     StmtExprVisitor::VisitStmt_(node);
 
     // Group 5: Outer scope related features
     ExtractAllocationFeature(node);
   }
 
+  void VisitStmt_(const AllocateNode* node) final {
+    buffer_dtypes[node->buffer_var] = node->dtype;
+    buffer_shapes[node->buffer_var] = node->extents;
+    StmtExprVisitor::VisitStmt_(node);
+
+    // Group 5: Outer scope related features
+    ExtractAllocationFeature(node);
+  }
+
+  void VisitStmt_(const StoreNode* node) final {
+    MathOpCounter math_op_counter;

Review comment:
       No, we don't hit this. I've removed it.

##########
File path: tests/python/unittest/test_auto_scheduler_feature.py
##########
@@ -200,6 +201,35 @@ def test_gpu_feature():
         assert fequal(fea_dicts[0]["is_gpu"], 1.0)
 
 
+@T.prim_func
+def tir_matmul(
+    A: T.Buffer[(128, 128), "float32"],
+    B: T.Buffer[(128, 128), "float32"],
+    C: T.Buffer[(128, 128), "float32"],
+) -> None:
+    T.func_attr({"from_legacy_te_schedule": True, "global_symbol": "main", "tir.noalias": True})
+    for y in T.serial(128):
+        T.store(C.data, T.ramp(y, 128, 128), T.broadcast(T.float32(0), 128), T.broadcast(True, 128))
+        for k in T.serial(128):
+            T.store(
+                C.data,
+                T.ramp(y, 128, 128),
+                T.load("float32x128", C.data, T.ramp(y, 128, 128), T.broadcast(True, 128))
+                + T.load("float32x128", A.data, T.ramp(k, 128, 128), T.broadcast(True, 128))
+                * T.broadcast(T.load("float32", B.data, y * 128 + k), 128),
+                T.broadcast(True, 128),
+            )

Review comment:
       I rewrote this to use buffer store.




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi merged pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi merged pull request #10455:
URL: https://github.com/apache/tvm/pull/10455


   


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822188815



##########
File path: src/auto_scheduler/feature.cc
##########
@@ -659,38 +697,86 @@ class PerStoreFeatureExtractor : public StmtExprVisitor {
     }
   }
 
+  void VisitExpr_(const BufferLoadNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+    StmtExprVisitor::VisitExpr_(node);
+  }
+
   void VisitStmt_(const BufferStoreNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+
     MathOpCounter math_op_counter;
     math_op_counter(node->value);
     std::vector<float> mem_bytes_list;
     std::vector<float> compute_ops_list;
     double cur_compute_ops;
 
     // Group 1: Computation related features
-    ExtractComputationFeature(node, math_op_counter);
+    ExtractComputationFeature(node->buffer->data, node->indices, math_op_counter);
 
     // Group 2: Buffer access related features (per buffer)
-    ExtractBufferAccessFeature(node, math_op_counter, &cur_compute_ops, &compute_ops_list,
-                               &mem_bytes_list);
+    ExtractBufferAccessFeature(node->buffer->data, node->indices, node->value, math_op_counter,
+                               &cur_compute_ops, &compute_ops_list, &mem_bytes_list);
 
     // Group 3: Arithmetic intensity related features
-    ExtractArithmeticIntensityFeature(node, cur_compute_ops, compute_ops_list, mem_bytes_list);
+    ExtractArithmeticIntensityFeature(node->buffer->data, cur_compute_ops, compute_ops_list,
+                                      mem_bytes_list);
 
     // Group 4: Allocation related features
-    ExtractOuterScopeFeature(node);
+    ExtractOuterScopeFeature(node->buffer->data);
   }
 
   void VisitStmt_(const BufferRealizeNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
     StmtExprVisitor::VisitStmt_(node);
 
     // Group 5: Outer scope related features
     ExtractAllocationFeature(node);
   }
 
+  void VisitStmt_(const AllocateNode* node) final {
+    buffer_dtypes[node->buffer_var] = node->dtype;
+    buffer_shapes[node->buffer_var] = node->extents;
+    StmtExprVisitor::VisitStmt_(node);
+
+    // Group 5: Outer scope related features
+    ExtractAllocationFeature(node);
+  }
+
+  void VisitStmt_(const StoreNode* node) final {
+    MathOpCounter math_op_counter;

Review comment:
       Do we hit this function now? Note that Load / Store node are deprecated after eric PR




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822188815



##########
File path: src/auto_scheduler/feature.cc
##########
@@ -659,38 +697,86 @@ class PerStoreFeatureExtractor : public StmtExprVisitor {
     }
   }
 
+  void VisitExpr_(const BufferLoadNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+    StmtExprVisitor::VisitExpr_(node);
+  }
+
   void VisitStmt_(const BufferStoreNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
+
     MathOpCounter math_op_counter;
     math_op_counter(node->value);
     std::vector<float> mem_bytes_list;
     std::vector<float> compute_ops_list;
     double cur_compute_ops;
 
     // Group 1: Computation related features
-    ExtractComputationFeature(node, math_op_counter);
+    ExtractComputationFeature(node->buffer->data, node->indices, math_op_counter);
 
     // Group 2: Buffer access related features (per buffer)
-    ExtractBufferAccessFeature(node, math_op_counter, &cur_compute_ops, &compute_ops_list,
-                               &mem_bytes_list);
+    ExtractBufferAccessFeature(node->buffer->data, node->indices, node->value, math_op_counter,
+                               &cur_compute_ops, &compute_ops_list, &mem_bytes_list);
 
     // Group 3: Arithmetic intensity related features
-    ExtractArithmeticIntensityFeature(node, cur_compute_ops, compute_ops_list, mem_bytes_list);
+    ExtractArithmeticIntensityFeature(node->buffer->data, cur_compute_ops, compute_ops_list,
+                                      mem_bytes_list);
 
     // Group 4: Allocation related features
-    ExtractOuterScopeFeature(node);
+    ExtractOuterScopeFeature(node->buffer->data);
   }
 
   void VisitStmt_(const BufferRealizeNode* node) final {
+    // Store buffer shape/dtype. It may already be stored.
+    buffer_shapes[node->buffer->data] = node->buffer->shape;
+    buffer_dtypes[node->buffer->data] = node->buffer->dtype;
     StmtExprVisitor::VisitStmt_(node);
 
     // Group 5: Outer scope related features
     ExtractAllocationFeature(node);
   }
 
+  void VisitStmt_(const AllocateNode* node) final {
+    buffer_dtypes[node->buffer_var] = node->dtype;
+    buffer_shapes[node->buffer_var] = node->extents;
+    StmtExprVisitor::VisitStmt_(node);
+
+    // Group 5: Outer scope related features
+    ExtractAllocationFeature(node);
+  }
+
+  void VisitStmt_(const StoreNode* node) final {
+    MathOpCounter math_op_counter;

Review comment:
       Do we hit this function now? Note that Load / Store node are deprecated after eric PR cc @Lunderberg 




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822196505



##########
File path: tests/python/unittest/test_auto_scheduler_feature.py
##########
@@ -200,6 +201,35 @@ def test_gpu_feature():
         assert fequal(fea_dicts[0]["is_gpu"], 1.0)
 
 
+@T.prim_func
+def tir_matmul(
+    A: T.Buffer[(128, 128), "float32"],
+    B: T.Buffer[(128, 128), "float32"],
+    C: T.Buffer[(128, 128), "float32"],
+) -> None:
+    T.func_attr({"from_legacy_te_schedule": True, "global_symbol": "main", "tir.noalias": True})
+    for y in T.serial(128):
+        T.store(C.data, T.ramp(y, 128, 128), T.broadcast(T.float32(0), 128), T.broadcast(True, 128))
+        for k in T.serial(128):
+            T.store(
+                C.data,
+                T.ramp(y, 128, 128),
+                T.load("float32x128", C.data, T.ramp(y, 128, 128), T.broadcast(True, 128))
+                + T.load("float32x128", A.data, T.ramp(k, 128, 128), T.broadcast(True, 128))
+                * T.broadcast(T.load("float32", B.data, y * 128 + k), 128),
+                T.broadcast(True, 128),
+            )

Review comment:
       cc @Lunderberg @vinx13 @junrushao1994 on recommendation on using `T.load / store` in new tests.




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#issuecomment-1062420710


   @tkonolige can you kick another job now that eric PR has landed.


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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] comaniac commented on a change in pull request #10455: [AUTO_SCHEDULER] Add feature extraction directly from PrimFunc

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #10455:
URL: https://github.com/apache/tvm/pull/10455#discussion_r822227670



##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -252,3 +253,78 @@ def get_per_store_feature_names(max_n_bufs: Optional[int] = None) -> List[str]:
         The names of elements in the flatten feature vector
     """
     return _ffi_api.GetPerStoreFeatureNames(max_n_bufs or DEFAULT_MAX_N_BUFS)
+
+
+def primfunc_features(

Review comment:
       Seems like `get_features_from_primfunc` or something in this direction would be more precise.

##########
File path: python/tvm/auto_scheduler/feature.py
##########
@@ -252,3 +253,78 @@ def get_per_store_feature_names(max_n_bufs: Optional[int] = None) -> List[str]:
         The names of elements in the flatten feature vector
     """
     return _ffi_api.GetPerStoreFeatureNames(max_n_bufs or DEFAULT_MAX_N_BUFS)
+
+
+def primfunc_features(
+    func: PrimFunc,
+    cache_line_bytes: int = 64,
+    max_n_bufs: Optional[int] = None,
+    log_scale: bool = False,
+) -> np.ndarray:
+    """Extract performance features from a PrimFunc.
+
+    Parameters
+    ----------
+    func: PrimFunc
+        PrimFunc from which features will be extracted. Each store operation to
+        a unique buffer in the function will result in one row of features in
+        the output.
+
+    cache_line_bytes: int, optional
+        Size of a cache line in bytes. Defaults to 64 which is the size for
+        most x86 processors.
+
+    max_n_bufs: int, optional
+        Maximum number of buffers in generated features. This determines the
+        length of the resulting feature vector.
+
+    log_scale: bool
+        Should entries in the feature vector be scaled by log2(x + 1). Defaults
+        to False. Use True if using features with a cost model.
+
+    Returns
+    -------
+    np.ndarray
+        Output features, one row per store into a unique buffer statement in `func`.
+    """
+    return _ffi_api.FeaturesFromPrimFunc(
+        func, cache_line_bytes, max_n_bufs or DEFAULT_MAX_N_BUFS, log_scale
+    ).numpy()
+
+
+def named_primfunc_features(

Review comment:
       ditto. Maybe `get_named_features_from_primfunc`?




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

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org