jcf94 commented on a change in pull request #7635:
URL: https://github.com/apache/tvm/pull/7635#discussion_r601975937



##########
File path: tutorials/auto_scheduler/tune_network_x86.py
##########
@@ -126,6 +129,46 @@ def get_network(name, batch_size, layout="NHWC", 
dtype="float32"):
             net.params, relay.nn.softmax(net.body), None, net.type_params, 
net.attrs
         )
         mod = tvm.IRModule.from_expr(net)
+    elif name == "mlp":
+        mod, params = relay.testing.mlp.get_workload(
+            batch_size=batch_size, dtype=dtype, image_shape=image_shape, 
num_classes=1000
+        )
+    else:
+        raise ValueError("Network not found.")
+
+    if use_sparse:
+        # This is a test workload that manually transforms a dense model to 
sparse
+        # Check `tutorials/frontend/deploy_sparse.py` for more examples on how 
to import a
+        # pretrained model.
+
+        def random_sparse_dense_params(func, params, density, BS_R, BS_C):
+            def deepcopy(param_dic):
+                ret = {}
+                for k, v in param_dic.items():
+                    ret[k] = tvm.nd.array(v.asnumpy())
+                return ret
+
+            new_params = deepcopy(params)
+            dense_weight_names = 
relay.analysis.sparse_dense._search_dense_op_weight(func)
+            for item in dense_weight_names:
+                name = str(item)
+                shape = new_params[name].shape
+                if shape[0] % BS_R == 0 and shape[1] % BS_C == 0:
+                    new_w = random_bsr_matrix(
+                        shape[0], shape[1], BS_R, BS_C, density, "float32"
+                    ).todense()
+                    new_params[name] = tvm.nd.array(new_w)
+            return new_params
+
+        bs_r = 1
+        sparsity = 0.85
+
+        # Currently we only support to conver dense matmul to sparse dense 
matmul
+        mod, params = ddo.simplify_fc_transpose.convert(mod["main"], params)
+        params = random_sparse_dense_params(mod, params, BS_R=bs_r, BS_C=1, 
density=1 - sparsity)
+        mod, params = ddo.bsr_dense.convert(mod, params, (bs_r, 1), 
sparsity_threshold=0.8)
+
+        mod = tvm.IRModule.from_expr(mod)

Review comment:
       > Wrap this as a function. Do not let this huge block of code confuse 
readers who only want to know how to use auto-scheduler for regular networks.
   
   Simplified this part, and moved the big block to `sparse.utils`.




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