masahi commented on a change in pull request #10638:
URL: https://github.com/apache/tvm/pull/10638#discussion_r836905364



##########
File path: src/tir/schedule/primitive/reduction.cc
##########
@@ -284,7 +284,22 @@ StmtSRef DecomposeReduction(ScheduleState self, const 
StmtSRef& block_sref,
                /*body=*/body);
   }
   body = Substitute(body, loop_var_map);
-  // Step 6. Mutate IR
+  // Step 6. Add write regions back to read regions in update block.
+  Array<BufferRegion> new_reads;
+  std::unordered_set<const BufferNode*> read_bufs;
+  for (const BufferRegion& read_access : block->reads) {
+    read_bufs.insert(read_access->buffer.get());
+  }
+  for (const BufferRegion& write_access : block->writes) {
+    if (read_bufs.find(write_access->buffer.get()) == read_bufs.end()) {
+      new_reads.push_back(write_access);
+    }
+  }
+  for (const BufferRegion& read_access : block->reads) {
+    new_reads.push_back(read_access);
+  }
+  (const_cast<BlockNode*>(block))->reads = std::move(new_reads);

Review comment:
       ok the repro is ready: 
https://gist.github.com/masahi/591078723d26f09ece3430af95835c99
   
   The test works with the current `main`. When I run it, I get this output:
   ```
   With decompose_reduction=True
   One or more operators have not been tuned. Please tune your model for better 
performance. Use DEBUG logging level to see more details.
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_nn_contrib_dense_pack
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_expand_dims
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast_1
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_nn_batch_matmul
   
   With decompose_reduction=False
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_expand_dims
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast
   [07:20:13] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast_1
   ```
   
   This means, if `decompose_reduction` is applied during scheduling, 
`ApplyHistoryBest` fails to match the structural hash of the query mod, 
corresponding to `nn_contrib_dense` and `nn_batch_matmul`, against the ones in 
database.
   
   If I revert this commit, I get the following expected output
   ```
   With decompose_reduction=True
   One or more operators have not been tuned. Please tune your model for better 
performance. Use DEBUG logging level to see more details.
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_expand_dims
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast_1
   
   With decompose_reduction=False
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_expand_dims
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast
   [07:24:21] /home/masa/projects/dev/tvm/src/meta_schedule/integration.cc:146: 
Warning: Cannot find workload: tvmgen_default_fused_cast_1
   ```
   
   You can dump the contents of the database at 
https://github.com/apache/tvm/blob/b08e8e49b5ea0e406c3ca41610a131dd271f0d41/src/meta_schedule/database/json_database.cc#L72-L74
 to see that `reads` region for dense and bmm is modified if 
`decompose_reduction` is enabled. This causes the structual hash mismatch 
between the modules in the database and the query mod.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to