Closed
[master]test: add NPU test for ONNXProgram.release #43594
Zky0803Add on Aug 2Closed at Aug 21
[master]test: add NPU test for ONNXProgram.release #43594
Closed
Merged fromZky0803/pytorchpr/onnx-program-releaseintoAscend/pytorchmaster
Total: 1 files changed+79-0
| @@ -0,0 +1,79 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# | ||
| 4 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 5 | +# you may not use this file except in compliance with the License. | ||
| 6 | +# You may obtain a copy of the License at | ||
| 7 | +# | ||
| 8 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 9 | +# | ||
| 10 | +# Unless required by applicable law or agreed to in writing, software | ||
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | +# See the License for the specific language governing permissions and | ||
| 14 | +# limitations under the License. | ||
| 15 | + | ||
| 16 | +"""Direct tests for torch.onnx.ONNXProgram.release.""" | ||
| 17 | + | ||
| 18 | +import gc | ||
| 19 | +import weakref | ||
| 20 | + | ||
| 21 | +import torch | ||
| 22 | +from torch_npu.testing.testcase import TestCase, run_tests | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +class _InferenceSession: | ||
| 26 | + pass | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +class _TemporaryDirectory: | ||
| 30 | + def __init__(self): | ||
| 31 | + self.cleanup_called = False | ||
| 32 | + | ||
| 33 | + def cleanup(self): | ||
| 34 | + self.cleanup_called = True | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +class TestONNXProgramRelease(TestCase): | ||
| 38 | + def test_release_clears_inference_session(self): | ||
| 39 | + program = object.__new__(torch.onnx.ONNXProgram) | ||
| 40 | + session = _InferenceSession() | ||
| 41 | + session_ref = weakref.ref(session) | ||
| 42 | + program._inference_session = session | ||
| 43 | + program._tempdir = None | ||
| 44 | + del session | ||
| 45 | + | ||
| 46 | + self.assertIsNotNone(session_ref()) | ||
| 47 | + self.assertIsNone(program.release()) | ||
| 48 | + self.assertIsNone(program._inference_session) | ||
| 49 | + self.assertIsNone(program._tempdir) | ||
| 50 | + | ||
| 51 | + gc.collect() | ||
| 52 | + self.assertIsNone(session_ref()) | ||
| 53 | + | ||
| 54 | + def test_release_cleans_temporary_directory(self): | ||
| 55 | + program = object.__new__(torch.onnx.ONNXProgram) | ||
| 56 | + tempdir = _TemporaryDirectory() | ||
| 57 | + program._inference_session = None | ||
| 58 | + program._tempdir = tempdir | ||
| 59 | + | ||
| 60 | + self.assertIsNone(program.release()) | ||
| 61 | + self.assertTrue(tempdir.cleanup_called) | ||
| 62 | + self.assertIsNone(program._inference_session) | ||
| 63 | + self.assertIsNone(program._tempdir) | ||
| 64 | + | ||
| 65 | + def test_release_is_idempotent(self): | ||
| 66 | + program = object.__new__(torch.onnx.ONNXProgram) | ||
| 67 | + program._inference_session = None | ||
| 68 | + program._tempdir = None | ||
| 69 | + | ||
| 70 | + self.assertIsNone(program.release()) | ||
| 71 | + self.assertIsNone(program._inference_session) | ||
| 72 | + self.assertIsNone(program._tempdir) | ||
| 73 | + self.assertIsNone(program.release()) | ||
| 74 | + self.assertIsNone(program._inference_session) | ||
| 75 | + self.assertIsNone(program._tempdir) | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +if __name__ == "__main__": | ||
| 79 | + run_tests() | ||