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 2021/11/20 21:31:11 UTC

[GitHub] [tvm] comaniac commented on a change in pull request #9544: [Target] decouple set_cuda_target_arch with autotvm

comaniac commented on a change in pull request #9544:
URL: https://github.com/apache/tvm/pull/9544#discussion_r753715309



##########
File path: python/tvm/contrib/nvcc.py
##########
@@ -72,10 +73,12 @@ def compile_cuda(code, target="ptx", arch=None, options=None, path_target=None):
 
     file_target = path_target if path_target else temp_target
     cmd = ["nvcc"]
-    cmd += ["--%s" % target, "-O3"]
+    cmd += ["--%s" % compile_target, "-O3"]
     if isinstance(arch, list):
         cmd += arch
-    else:
+    if isinstance(arch, tvm.ir.container.Array):

Review comment:
       Better to be "elif".

##########
File path: python/tvm/target/cuda_scope.py
##########
@@ -0,0 +1,98 @@
+# 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.
+"""Global configuration/variable scope for cuda target"""
+import tvm
+from tvm._ffi import register_func as _register_func
+from tvm.contrib import nvcc
+
+from .. import nd as _nd
+
+
+class CudaGlobalScope(object):
+    current = None
+
+    def __init__(self):
+        self._old = CudaGlobalScope.current
+        CudaGlobalScope.current = self
+
+        self._old_cuda_target_arch = None
+        self.cuda_target_arch = None
+
+
+CUDA_GLOBAL_SCOPE = CudaGlobalScope()
+CUDA_DEVICE_TYPE = _nd.device("cuda").device_type
+
+
+@_register_func
+def tvm_callback_cuda_compile(code):
+    """use nvcc to generate ptx code for better optimization"""
+    curr_cuda_target_arch = get_cuda_target_arch()
+    # e.g., target arch could be [
+    #   "-gencode", "arch=compute_52,code=sm_52",
+    #   "-gencode", "arch=compute_70,code=sm_70"
+    # ]
+    compile_target = "fatbin" if isinstance(curr_cuda_target_arch, list) else "ptx"
+    ptx = nvcc.compile_cuda(code, compile_target=compile_target, arch=curr_cuda_target_arch)
+    return ptx
+
+
+@_register_func("target.set_cuda_target_arch")
+def set_cuda_target_arch(arch, gencode=True):
+    """set target architecture of nvcc compiler
+
+    Parameters
+    ----------
+    arch: str or list
+        The argument of nvcc -arch. (e.g. "sm_51", "sm_62")
+        it can also be a count of gencode arguments pass to nvcc command line,
+        e.g., ["-gencode", "arch=compute_52,code=sm_52", "-gencode", "arch=compute_70,code=sm_70"]
+    """
+    if arch is None:
+        cuda_arch = None
+    elif isinstance(arch, str):
+        if gencode:
+            compute_version = arch.split("_")[1]
+            cuda_arch = ["-gencode", f"arch=compute_{compute_version},code=sm_{compute_version}"]
+        else:
+            cuda_arch = arch
+    elif isinstance(arch, (list, tvm.ir.container.Array)):
+        cuda_arch = list(arch)
+        assert "-gencode" in cuda_arch
+    else:
+        raise TypeError(
+            "arch is expected to be str or "
+            + "a list or NoneType, but received "
+            + "{}".format(type(arch))
+        )
+
+    CudaGlobalScope.current._old_cuda_target_arch = get_cuda_target_arch()
+    CudaGlobalScope.current.cuda_target_arch = cuda_arch
+
+
+@_register_func("target.get_cuda_target_arch")
+def get_cuda_target_arch(target=None):
+    """get target architecture of nvcc compiler"""
+    if target is None:
+        return CudaGlobalScope.current.cuda_target_arch
+
+    return tvm.target.Target(target=target).arch

Review comment:
       Need to check whether the target is CUDA and the target has the arch attribute.

##########
File path: python/tvm/target/target.py
##########
@@ -139,6 +142,14 @@ def current(allow_none=True):
         """
         return _ffi_api.TargetCurrent(allow_none)
 
+    def enter_cuda_scope(self):
+        if self.arch:
+            set_cuda_target_arch(self.arch)
+
+    @property
+    def arch(self):

Review comment:
       Again, you should not use target attributes in this way. You have to register this attribute to the CIDA target in the C++ side and use it here like others. Otherwise we cannot access this attribute in C++.

##########
File path: python/tvm/target/cuda_scope.py
##########
@@ -0,0 +1,98 @@
+# 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.
+"""Global configuration/variable scope for cuda target"""
+import tvm
+from tvm._ffi import register_func as _register_func
+from tvm.contrib import nvcc
+
+from .. import nd as _nd
+
+
+class CudaGlobalScope(object):
+    current = None
+
+    def __init__(self):
+        self._old = CudaGlobalScope.current
+        CudaGlobalScope.current = self
+
+        self._old_cuda_target_arch = None
+        self.cuda_target_arch = None
+
+
+CUDA_GLOBAL_SCOPE = CudaGlobalScope()
+CUDA_DEVICE_TYPE = _nd.device("cuda").device_type

Review comment:
       What happen to this line if CUDA is disabled?

##########
File path: python/tvm/target/target.py
##########
@@ -113,6 +114,8 @@ def __init__(self, target, host=None):
 
     def __enter__(self):
         _ffi_api.TargetEnterScope(self)
+        if self.kind.name == "cuda":
+            self.enter_cuda_scope()

Review comment:
       It is too as-hoc to add a CUDA specific process in a general target method.




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