|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2024 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import time |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import dpctl |
| 22 | +import dpctl.tensor as dpt |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def profiling_queue(): |
| 27 | + try: |
| 28 | + q = dpctl.SyclQueue(property="enable_profiling") |
| 29 | + except dpctl.SyclQueueCreationError: |
| 30 | + pytest.skip( |
| 31 | + "Could not created profiling queue " "for default-selected device" |
| 32 | + ) |
| 33 | + return q |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "device_timer", [None, "queue_barrier", "order_manager"] |
| 38 | +) |
| 39 | +def test_sycl_timer_queue_barrier(profiling_queue, device_timer): |
| 40 | + dev = dpt.Device.create_device(profiling_queue) |
| 41 | + |
| 42 | + timer = dpctl.SyclTimer( |
| 43 | + host_timer=time.perf_counter, device_timer=device_timer, time_scale=1e3 |
| 44 | + ) |
| 45 | + |
| 46 | + with timer(dev.sycl_queue): |
| 47 | + x = dpt.linspace(0, 1, num=10**6, device=dev) |
| 48 | + y = 3.0 - dpt.square(x - 0.5) |
| 49 | + z = dpt.sort(y) |
| 50 | + res1 = z[-1] |
| 51 | + res2 = dpt.max(y) |
| 52 | + |
| 53 | + host_dt, device_dt = timer.dt |
| 54 | + |
| 55 | + assert dpt.all(res1 == res2) |
| 56 | + assert host_dt > 0 |
| 57 | + assert device_dt > 0 |
| 58 | + |
| 59 | + |
| 60 | +def test_sycl_timer_accumulation(profiling_queue): |
| 61 | + q = profiling_queue |
| 62 | + |
| 63 | + timer = dpctl.SyclTimer( |
| 64 | + host_timer=time.perf_counter, |
| 65 | + device_timer="order_manager", |
| 66 | + time_scale=1e3, |
| 67 | + ) |
| 68 | + |
| 69 | + # initial condition |
| 70 | + x = dpt.linspace(0, 1, num=10**6, sycl_queue=q) |
| 71 | + |
| 72 | + aitkens_data = [ |
| 73 | + x, |
| 74 | + ] |
| 75 | + |
| 76 | + # 16 iterations of Aitken's accelerated Newton's method |
| 77 | + # x <- x - f(x)/f'(x) for f(x) = x - cos(x) |
| 78 | + for _ in range(16): |
| 79 | + # only time Newton step |
| 80 | + with timer(q): |
| 81 | + s = dpt.sin(x) |
| 82 | + x = (dpt.cos(x) + x * s) / (1 + s) |
| 83 | + aitkens_data.append(x) |
| 84 | + aitkens_data = aitkens_data[-3:] |
| 85 | + if len(aitkens_data) == 3: |
| 86 | + # apply Aitkens acceleration |
| 87 | + d1 = aitkens_data[-1] - aitkens_data[-2] |
| 88 | + d2 = aitkens_data[-2] - aitkens_data[-3] |
| 89 | + if not dpt.any(d1 == d2): |
| 90 | + x = aitkens_data[-1] - dpt.square(d1) / (d1 - d2) |
| 91 | + |
| 92 | + # Total time for 16 iterations |
| 93 | + dev_dt = timer.dt.device_dt |
| 94 | + assert dev_dt > 0 |
| 95 | + |
| 96 | + # check convergence |
| 97 | + assert dpt.max(x) - dpt.min(x) < 1e-5 |
| 98 | + |
| 99 | + |
| 100 | +def test_sycl_timer_validation(): |
| 101 | + with pytest.raises(ValueError): |
| 102 | + dpctl.SyclTimer(device_timer="invalid") |
| 103 | + |
| 104 | + timer = dpctl.SyclTimer() |
| 105 | + mock_queue = Ellipsis |
| 106 | + |
| 107 | + with pytest.raises(TypeError): |
| 108 | + timer(mock_queue) |
0 commit comments