Collecting Profile Data Locally
To collect profile data locally, you can use the compat.v1 module to call the Profiler class in TF Adapter 1.x. That is, profile data sampling can be enabled only by running commands in the Profiler class.
For details about the Profiler class, see Profiler Constructor.
The following describes how to use the compat.v1 module to call the Profiler class of TF Adapter 1.x to collect profile data locally.
- Import the Profiler class.
1 2 3
import npu_device from npu_device.compat.v1.npu_init import * npu_device.compat.enable_v1()
- Use the with statement to call the Profiler class and include the operations that require profile data collection in the Profiler class.In the following simple code snippet, a graph containing the Add operator is implemented and executed in a session. As sess.run (add, ...) is within the Profiler class, the L1 profile data and the ratios of compute metrics are collected. The profile data is stored in the current script execution path.
1 2 3 4 5 6 7 8
a = tf.placeholder(tf.int32, (None,None)) b = tf.constant([[1,2],[2,3]], dtype=tf.int32, shape=(2,2)) c = tf.placeholder(tf.int32, (None,None)) add = tf.add(a, b) with tf.compat.v1.Session(config=session_config, graph=g) as sess: with profiler.Profiler(level="L1", aic_metrics="ArithmeticUtilization", output_path = "./"): result=sess.run(add, feed_dict={a: [[-20, 2],[1,3]],c: [[1],[-21]]})
Currently, you can collect the profile data of a specified step by defining the specified step operation in the corresponding Profiler, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
a=tf.placeholder(tf.int32, (None,None)) b=tf.constant([[1,2],[2,3]], dtype=tf.int32, shape=(2,2)) c = tf.placeholder(tf.int32, (None,None)) d = tf.constant([[1,2],[2,3]], dtype=tf.int32, shape=(2,2)) add1 = tf.add(a, b) add2 = tf.add(c, d) add3 = tf.add(add1, add2) with tf.compat.v1.Session(config=session_config, graph=g) as sess: with profiler.Profiler(level="L1", aic_metrics="PipeUtilization", output_path = "/home/test/profiling_data"): for i in range(2): result=sess.run(add1, feed_dict={a: [[-20],[1]]}) with profiler.Profiler(level="L1", aic_metrics="ArithmeticUtilization", output_path = "/home/test/profiling_data"): for i in range(4): result=sess.run(add3, feed_dict={a: [[-20, 2],[1,3]],c: [[1],[-21]]})
Pay attention to the following restrictions when using the Profiler class:
- The Profiler class needs to be called using the with statement, and the profile data collection function takes effect in the corresponding scope.
- The Profiler class can be invoked only in session mode.
- The Profiler class cannot be nested.
The following is an incorrect invoking example:
1 2 3
with profiler.Profiler(level="L1", aic_metrics="ArithmeticUtilization", output_path = "./"): with profiler.Profiler(level="L1", aic_metrics="ArithmeticUtilization", output_path = "./"): sess.run(add)
- The Profiler class cannot be used together with the configuration items enable_profiling and profiling_config in "global_options > Profiling", and the environment variables PROFILING_MODE and PROFILING_OPTIONS. For details about the environment variables, see Environment Variables.
- The Profiler class does not support multi-thread calling.
Parent topic: Profile Data Collection and Analysis