[{"data":1,"prerenderedAt":254},["ShallowReactive",2],{"content-query-v4pcq6QezC":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":10,"date":11,"cover":12,"type":13,"category":14,"body":15,"_type":248,"_id":249,"_source":250,"_file":251,"_stem":252,"_extension":253},"/technology-blogs/en/1800","en",false,"",[9],"MindSpore Made Easy","On-demand profiler performance debugging does not support user-defined data storage paths. Therefore, after the program is complete, the profile data is saved in the data file in the current directory by default.","2022-08-12","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/15/8c6b26d6705f488b83a3c9cdc67ac422.png","technology-blogs","Basics",{"type":16,"children":17,"toc":235},"root",[18,32,38,43,54,59,71,83,88,98,117,126,131,141,149,158,166,176,181,186,205,216,225,230],{"type":19,"tag":20,"props":21,"children":23},"element","h1",{"id":22},"mindspore-made-easy-collecting-profile-data-in-a-modelarts-development-environment",[24,30],{"type":19,"tag":25,"props":26,"children":27},"span",{},[28],{"type":29,"value":9},"text",{"type":29,"value":31}," Collecting Profile Data in a ModelArts Development Environment",{"type":19,"tag":33,"props":34,"children":35},"p",{},[36],{"type":29,"value":37},"August 12, 2022",{"type":19,"tag":33,"props":39,"children":40},{},[41],{"type":29,"value":42},"This blog describes how to collect profile data in a ModelArts development environment and enable profiler for performance debugging as required.",{"type":19,"tag":44,"props":45,"children":47},"h2",{"id":46},"_1-collecting-profile-data-by-using-the-training-script-in-the-development-environment",[48],{"type":19,"tag":49,"props":50,"children":51},"strong",{},[52],{"type":29,"value":53},"1. Collecting Profile Data by Using the Training Script in the Development Environment",{"type":19,"tag":33,"props":55,"children":56},{},[57],{"type":29,"value":58},"To collect the profile data of a neural network, you need to add MindSpore Profiler APIs to the training script.",{"type":19,"tag":33,"props":60,"children":61},{},[62,64,69],{"type":29,"value":63},"(1) After ",{"type":19,"tag":49,"props":65,"children":66},{},[67],{"type":29,"value":68},"set_context",{"type":29,"value":70}," is executed and before the network and HCCL are initialized, initialize the MindSpore Profiler objects.",{"type":19,"tag":33,"props":72,"children":73},{},[74,76,81],{"type":29,"value":75},"(2) After the training is complete, call ",{"type":19,"tag":49,"props":77,"children":78},{},[79],{"type":29,"value":80},"Profiler.analyse()",{"type":29,"value":82}," to stop profile data collection and generate the profiling results.",{"type":19,"tag":33,"props":84,"children":85},{},[86],{"type":29,"value":87},"The sample code is as follows:",{"type":19,"tag":89,"props":90,"children":92},"pre",{"code":91},"from mindspore.profiler import Profilerfrom mindspore import Model\n\n\n\ncontext.set_context(mode=context.GRAPH_MODE,\n\ndevice_target=args.device_target)\n\nSAVE_PATH = \"./profile\"\n\n# Init Profiler and SummaryCollector# Data directory should be placed under SAVE_PATH.\n\nprofiler_output_path = SAVE_PATH + \"mindspore_profile\"\n\nprofiler = Profiler(output_path=profiler_output_path)# Train Model\n\nModel.train()\n\n# Profiler end\n\nprofiler.analyse()\n",[93],{"type":19,"tag":94,"props":95,"children":96},"code",{"__ignoreMap":7},[97],{"type":29,"value":91},{"type":19,"tag":33,"props":99,"children":100},{},[101,103,108,110,115],{"type":29,"value":102},"Note: ",{"type":19,"tag":49,"props":104,"children":105},{},[106],{"type":29,"value":107},"output_path",{"type":29,"value":109}," indicates the path where the profile data is generated. If this path is not specified, the profile data is automatically saved in the ",{"type":19,"tag":49,"props":111,"children":112},{},[113],{"type":29,"value":114},"data",{"type":29,"value":116}," folder (automatically generated) under the current directory.",{"type":19,"tag":44,"props":118,"children":120},{"id":119},"_2-collecting-profile-data-for-performance-debugging-as-required",[121],{"type":19,"tag":49,"props":122,"children":123},{},[124],{"type":29,"value":125},"2. Collecting Profile Data for Performance Debugging as Required",{"type":19,"tag":33,"props":127,"children":128},{},[129],{"type":29,"value":130},"Enable profile data collection as required.",{"type":19,"tag":132,"props":133,"children":135},"h4",{"id":134},"_1-the-sample-code-for-collecting-profile-data-by-step-is-as-follows",[136],{"type":19,"tag":49,"props":137,"children":138},{},[139],{"type":29,"value":140},"(1) The sample code for collecting profile data by step is as follows:",{"type":19,"tag":89,"props":142,"children":144},{"code":143},"class StopAtStep(Callback):\n\n    def __init__(self, start_step, stop_step):\n\n        super(StopAtStep, self).__init__()\n\n        self.start_step = start_step\n\n        self.stop_step = stop_step\n\n        self.profiler = Profiler(start_profile=False)\n\n\n\n    def step_begin(self, run_context):\n\n        cb_params = run_context.original_args()\n\n        step_num = cb_params.cur_step_num\n\n        if step_num == self.start_step:\n\n            self.profiler.start() # Enable profile data collection as required.\n\n\n\n    def step_end(self, run_context):\n\n        cb_params = run_context.original_args()\n\n        step_num = cb_params.cur_step_num\n\n        if step_num == self.stop_step:\n\n            self.profiler.stop() # Disable profile data collection as required.\n\n\n\n    def end(self, run_context):\n\n        self.profiler.analyse()\n\n...\n\n...\n\nstart_step = 2\n\nstop_step = 5\n\nprofiler_data = StopAtEpoch(start_step, stop_step)\n\nmodel.train(..., callbacks=[..., profiler_data])\n",[145],{"type":19,"tag":94,"props":146,"children":147},{"__ignoreMap":7},[148],{"type":29,"value":143},{"type":19,"tag":132,"props":150,"children":152},{"id":151},"_2-the-sample-code-for-collecting-profile-data-by-epoch-is-as-follows",[153],{"type":19,"tag":49,"props":154,"children":155},{},[156],{"type":29,"value":157},"(2) The sample code for collecting profile data by epoch is as follows:",{"type":19,"tag":89,"props":159,"children":161},{"code":160},"class StopAtEpoch(Callback):\n\n    def __init__(self, start_epoch, stop_epoch):\n\n        super(StopAtEpoch, self).__init__()\n\n        self.start_epoch = start_epoch\n\n        self.stop_epoch = stop_epoch\n\n        self.profiler = Profiler(start_profile=False)\n\n\n\n    def epoch_begin(self, run_context):\n\n        cb_params = run_context.original_args()\n\n        epoch_num = cb_params.cur_epoch_num\n\n        if epoch_num == self.start_epoch:\n\n            self.profiler.start() # Enable profile data collection as required.\n\n\n\n    def epoch_end(self, run_context):\n\n        cb_params = run_context.original_args()\n\n        epoch_num = cb_params.cur_epoch_num\n\n        if epoch_num == self.stop_epoch:\n\n            self.profiler.stop() # Disable profile data collection as required.\n\n\n\n    def end(self, run_context):\n\n        self.profiler.analyse()\n\n...\n\n...\n\nstart_epoch = 2\n\nstop_epoch = 5\n\nprofiler_data = StopAtEpoch(start_epoch, stop_epoch)\n\nmodel.train(..., callbacks=[..., profiler_data])\n",[162],{"type":19,"tag":94,"props":163,"children":164},{"__ignoreMap":7},[165],{"type":29,"value":160},{"type":19,"tag":167,"props":168,"children":170},"h3",{"id":169},"_3-running-the-script",[171],{"type":19,"tag":49,"props":172,"children":173},{},[174],{"type":29,"value":175},"3. Running the Script",{"type":19,"tag":33,"props":177,"children":178},{},[179],{"type":29,"value":180},"Startup command:",{"type":19,"tag":33,"props":182,"children":183},{},[184],{"type":29,"value":185},"python MindSpore_1P_profiler.py data_path=xxx",{"type":19,"tag":33,"props":187,"children":188},{},[189,191,196,198,203],{"type":29,"value":190},"Run the script in ",{"type":19,"tag":49,"props":192,"children":193},{},[194],{"type":29,"value":195},"Terminal",{"type":29,"value":197}," of the development environment. After script execution, the generated profile data is stored in ",{"type":19,"tag":49,"props":199,"children":200},{},[201],{"type":29,"value":202},"SAVE_PATH",{"type":29,"value":204},".",{"type":19,"tag":33,"props":206,"children":207},{},[208,210,214],{"type":29,"value":209},"Note: On-demand profiler performance debugging does not support user-defined data storage paths. Therefore, after the program is complete, the profile data is saved in the ",{"type":19,"tag":49,"props":211,"children":212},{},[213],{"type":29,"value":114},{"type":29,"value":215}," file in the current directory by default.",{"type":19,"tag":132,"props":217,"children":219},{"id":218},"precautions",[220],{"type":19,"tag":49,"props":221,"children":222},{},[223],{"type":29,"value":224},"Precautions:",{"type":19,"tag":33,"props":226,"children":227},{},[228],{"type":29,"value":229},"1. Currently, performance debugging is not supported during training while inference, but is supported for separate training or inference.",{"type":19,"tag":33,"props":231,"children":232},{},[233],{"type":29,"value":234},"2. Ascend performance debugging does not support the dynamic shape, multi-subgraph, and control flow scenarios.",{"title":7,"searchDepth":236,"depth":236,"links":237},4,[238,240],{"id":46,"depth":239,"text":53},2,{"id":119,"depth":239,"text":125,"children":241},[242,243,244],{"id":134,"depth":236,"text":140},{"id":151,"depth":236,"text":157},{"id":169,"depth":245,"text":175,"children":246},3,[247],{"id":218,"depth":236,"text":224},"markdown","content:technology-blogs:en:1800.md","content","technology-blogs/en/1800.md","technology-blogs/en/1800","md",1776506104641]