Skip to content

Commit ebc652d

Browse files
authored
Add python build option for cuda-archs (#672)
* Add python build option for cuda-archs * ignore the nvidia-smi error * apply multi-archs more places * fix the semicolon issue * Update ci_optional.yml * Update ci.yml
1 parent 01e3a63 commit ebc652d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

.pipelines/ci.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ stages:
488488
- stage: WindowsCUDABuilds
489489
dependsOn: []
490490
jobs:
491-
- job: WindowsC
491+
- job: WindowsCUDABoth
492492
pool:
493493
name: 'onnxruntime-extensions-Win2022-GPU-A10'
494494
variables:
@@ -526,7 +526,8 @@ stages:
526526

527527
- script: |
528528
set CUDA_PATH=$(Agent.TempDirectory)\v11.8
529-
call .\build.bat -T cuda="%CUDA_PATH%" -DOCOS_ENABLE_CTEST=ON -DOCOS_USE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=86 -DOCOS_ONNXRUNTIME_VERSION="$(ORT_VERSION)" -DONNXRUNTIME_PKG_DIR=.\onnxruntime-win-x64-gpu-$(ORT_VERSION)
529+
call .\build.bat -T cuda="%CUDA_PATH%" -DOCOS_ENABLE_CTEST=ON -DOCOS_USE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=70;86^
530+
-DOCOS_ONNXRUNTIME_VERSION="$(ORT_VERSION)" -DONNXRUNTIME_PKG_DIR=.\onnxruntime-win-x64-gpu-$(ORT_VERSION)
530531
displayName: build the customop library with onnxruntime
531532
532533
- script: |
@@ -547,7 +548,7 @@ stages:
547548
python -m pip install --upgrade setuptools pip
548549
python -m pip install numpy coloredlogs flatbuffers packaging protobuf sympy
549550
python -m pip install onnxruntime-gpu==$(ORT_VERSION)
550-
python -m pip install -v --config-settings "ortx-user-option=use-cuda" .
551+
python -m pip install -v --config-settings "ortx-user-option=use-cuda,cuda_archs=70;86" .
551552
displayName: Build and install onnxruntime-extensions CUDA package.
552553
553554
- script: |
@@ -665,7 +666,7 @@ stages:
665666
python3 -m pip install --upgrade pip; \
666667
python3 -m pip install --upgrade setuptools; \
667668
python3 -m pip install onnxruntime-gpu==$(ORT_VERSION); \
668-
python3 -m pip install -v --config-settings "ortx-user-option=use-cuda" . ; \
669+
python3 -m pip install -v --config-settings 'ortx-user-option=use-cuda,cuda_archs=70;86' . ; \
669670
python3 -m pip install $(TORCH_VERSION) ; \
670671
python3 -m pip install -r requirements-dev.txt; \
671672
cd test && python -m pytest . --verbose; \
@@ -864,4 +865,4 @@ stages:
864865
-scheme OrtExtensionsUsage \
865866
-destination "platform=iOS Simulator,id=${ORT_EXTENSIONS_SIMULATOR_DEVICE_ID}" \
866867
test CODE_SIGNING_ALLOWED=NO
867-
displayName: "Build and test OrtExtensionsUsage"
868+
displayName: "Build and test OrtExtensionsUsage"

.pipelines/ci_optional.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ stages:
181181
python -m pip install --upgrade setuptools pip
182182
python -m pip install numpy coloredlogs flatbuffers packaging protobuf sympy
183183
python -m pip install -U --index-url https://pkgs.dev.azure.com/aiinfra/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ ort-nightly-gpu
184-
python -m pip install -v --config-settings "ortx-user-option=use-cuda" .
184+
python -m pip install -v --config-settings "ortx-user-option=use-cuda,cuda_archs=70;86" .
185185
displayName: Build and install onnxruntime-extensions CUDA package.
186186
187187
- script: |

.pyproject/cmdclass.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ def _load_cuda_version():
3030
match = re.search(pattern, output)
3131
if match:
3232
return match.group(1)
33-
except subprocess.CalledProcessError:
33+
except (subprocess.CalledProcessError, OSError):
34+
pass
35+
36+
return None
37+
38+
39+
def _load_nvidia_smi():
40+
try:
41+
output = subprocess.check_output(
42+
["nvidia-smi", "--query-gpu=compute_cap", "--format=csv,noheader,nounits"],
43+
stderr=subprocess.STDOUT).decode("utf-8")
44+
arch = output.strip().replace('.', '')
45+
return arch if arch.isdigit() else None
46+
except (subprocess.CalledProcessError, OSError):
3447
pass
3548

3649
return None
@@ -133,6 +146,7 @@ def initialize_options(self):
133146
self.no_azure = None
134147
self.no_opencv = None
135148
self.cc_debug = None
149+
self.cuda_archs = None
136150

137151
def _parse_options(self, options):
138152
for segment in options.split(','):
@@ -211,6 +225,14 @@ def build_cmake(self, extension):
211225
with f_ver.open('a') as _f:
212226
_f.writelines(["\n", f"cuda = \"{cuda_ver}\"", "\n"])
213227

228+
if self.cuda_archs is not None:
229+
cmake_args += ['-DCMAKE_CUDA_ARCHITECTURES=' + self.cuda_archs]
230+
else:
231+
smi = _load_nvidia_smi()
232+
if not smi:
233+
raise RuntimeError(f"Cannot detect the CUDA archs from your machine, please specify it by yourself.")
234+
cmake_args += ['-DCMAKE_CUDA_ARCHITECTURES=' + smi]
235+
214236
# CMake lets you override the generator - we need to check this.
215237
# Can be set with Conda-Build, for example.
216238
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")

0 commit comments

Comments
 (0)