1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15"""test cpu profiler""" 16import os 17import shutil 18import sys 19 20import numpy as np 21import pytest 22 23import mindspore.context as context 24import mindspore.nn as nn 25from mindspore import Tensor 26from mindspore.ops import operations as P 27from mindspore.profiler import Profiler 28from tests.security_utils import security_off_wrap 29 30 31class Net(nn.Cell): 32 def __init__(self): 33 super(Net, self).__init__() 34 self.add = P.Add() 35 36 def construct(self, x_, y_): 37 return self.add(x_, y_) 38 39 40@pytest.mark.level0 41@pytest.mark.platform_x86_cpu 42@pytest.mark.env_onecard 43@security_off_wrap 44def test_cpu_profiling(): 45 if sys.platform != 'linux': 46 return 47 data_path = os.path.join(os.getcwd(), 'data_cpu_profiler') 48 if os.path.isdir(data_path): 49 shutil.rmtree(data_path) 50 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 51 device_id = context.get_context("device_id") 52 profiler = Profiler(output_path="data_cpu_profiler") 53 x = np.random.randn(1, 3, 3, 4).astype(np.float32) 54 y = np.random.randn(1, 3, 3, 4).astype(np.float32) 55 add = Net() 56 add(Tensor(x), Tensor(y)) 57 profiler.analyse() 58 59 assert os.path.isdir(data_path) 60 assert len(os.listdir(data_path)) == 1 61 62 profiler_dir = os.path.join(data_path, f"{os.listdir(data_path)[0]}/") 63 op_detail_file = f"{profiler_dir}cpu_op_detail_info_{device_id}.csv" 64 op_type_file = f"{profiler_dir}cpu_op_type_info_{device_id}.csv" 65 timeline_file = f"{profiler_dir}cpu_op_execute_timestamp_{device_id}.txt" 66 cpu_profiler_files = (op_detail_file, op_type_file, timeline_file) 67 for file in cpu_profiler_files: 68 assert os.path.isfile(file) 69 70 if os.path.isdir(data_path): 71 shutil.rmtree(data_path) 72