-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typo
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
orttraining/orttraining/test/python/orttraining_test_aggressive_cpu_fallback.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import onnx | ||
import onnxscript | ||
from onnxscript.onnx_opset import opset18 | ||
from onnxscript.onnx_types import FLOAT, INT64 | ||
|
||
import onnxruntime | ||
|
||
|
||
class TestAggressiveCpuFallback(unittest.TestCase): | ||
def test_reshape_matmul(self): | ||
@onnxscript.script(default_opset=opset18) | ||
def foo(x: FLOAT[12], w: FLOAT[6, 2], dim0: INT64[1], dim1: INT64[1]): | ||
# This should be computed by CPU but is placed | ||
# on CUDA (i.e., all inputs and outputs are GPU tensors). | ||
dim2 = dim1 + 1 | ||
# Same as `dim2 = dim1 + 1`. Another GPU node. | ||
dim3 = dim2 - 1 | ||
# Same as `dim2 = dim1 + 1`. Another GPU node. | ||
new_shape = opset18.Concat(dim0, dim3, axis=0) | ||
# A memcpy node will be inserted to copy GPU output | ||
# to CPU since Reshape's 2nd input is a CPU tensor | ||
# per schema definition. | ||
# | ||
# Use ORT_AGGRESSIVE_CPU_FALLBACK=1 to | ||
# 1. remove memcpy node. | ||
# 2. fallback all computation above this line to CPU. | ||
new_x = opset18.Reshape(x, new_shape) | ||
y = opset18.MatMul(new_x, w) | ||
return y | ||
|
||
model = foo.to_model_proto() | ||
|
||
temp_file_name = tempfile.mktemp(prefix="cpu_fallback_test", suffix=".onnx") | ||
Check failure Code scanning / CodeQL Insecure temporary file High test
Call to deprecated function tempfile.mktemp may be insecure.
|
||
session_options = onnxruntime.SessionOptions() | ||
session_options.optimized_model_filepath = temp_file_name | ||
# This call should trigger GetCpuPreferredNodes and then GetShapeRelatedNodes | ||
# when environment variable ORT_AGGRESSIVE_CPU_FALLBACK=1 is set. | ||
# As a result, no memcopy node should be observed in optimized graph. | ||
# | ||
# See comments inside `foo`. | ||
onnxruntime.InferenceSession( | ||
path_or_bytes=model.SerializeToString(), sess_options=session_options, providers=["CUDAExecutionProvider"] | ||
) | ||
optimized = onnx.load(temp_file_name) | ||
|
||
self.assertTrue(all(node.op_type != "MemcpyToHost" for node in optimized.graph.node)) | ||
os.remove(temp_file_name) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |