Implementation
Writing Test Cases
The following uses the TensorFlow network as an example.
- Go to the tbe/testcases/tf_test/Operator name directory and write a test case called tf_add.py.
- Import the Python libraries.
import logging # Log module of the Python standard library import tensorflow as tf # Imports the TensorFlow open-source library. from tensorflow.python.ops import state_ops # Imports the state_ops module in the TensorFlow open-source library. from npu_bridge.estimator import npu_ops # Imports the npu_ops module in the TensorFlow open-source library. import numpy as np # Imports the basic mathematics library of Python.
- Sets the tolerance parameter of the np.allclose comparison function.
# Relative tolerance parameter of the np.allclose comparison function atol = 0.001 # Absolute tolerance parameter of the np.allclose comparison function rtol = 0.001
- Use config() to define the running parameters of the Ascend AI processor and CPU.
If excute_type is set to ai_core and use_off_line is set to True, a single-operator network is running on the Ascend AI processor and a TBE operator is called.
When excute_type is set to cpu and use_off_line is not configured, the default value False is used, indicating that a single-operator network runs on the host CPU and a TensorFlow operator is called.def config(excute_type): if excute_type == 'ai_core': session_config = tf.ConfigProto( allow_soft_placement=True, log_device_placement=False, min_group_size=1, use_off_line=True) elif excute_type == 'cpu': session_config = tf.ConfigProto( allow_soft_placement=True, log_device_placement=False) return session_config
- The main function of the single-operator network test case is as follows:
def main(unused_argv): shape_params = (2, 2, 2) dtype_params = "float16" # Construct the first input data of the Add operator. shape_params indicates the shape, which is random number in the range [–2, 2]. x = np.random.uniform(-2, 2, size=shape_params).astype(dtype_params) data_params = np.reshape(x, shape_params) # Construct the second input data of the Add operator. shape_params indicates the shape, which is random number in the range [–2, 2]. y = np.random.uniform(-2, 2, size=shape_params).astype(dtype_params) data_params = np.reshape(y, shape_params) # Compute the output of the operator. out = tf.math.add(x, y) # Run a single operator on the host CPU to obtain the expected running result. with tf.Session(config=config('cpu')) as session: result_cpu = session.run(out) # Run a single operator on the Ascend AI processor to obtain the actual running result. with tf.Session(config=config('ai_core')) as session: result_ai_core = session.run(out) np.array(result_ai_core).astype(dtype_params) np.array(result_cpu).astype(dtype_params) print('====================================') #Compare the actual running result on the Ascend AI processor with the expected running result on the CPU by running the np.allclose command. atol and rtol are the relative tolerance and absolute tolerance parameters of the np.allclose comparison function, respectively. For details, see Step 3. cmp_result = np.allclose(result_ai_core, result_cpu, atol, rtol) print(cmp_result) print('====================================')
- Construct the operator input based on the actual input number and shape of the operator.
- Compute the operator output by calling related TensorFlow APIs based on the operator logic.
- Run a single-operator network.
if __name__ == "__main__": tf.app.run()