Quick Start
This section uses a simple example to help you quickly understand how to port and execute TensorFlow scripts to the Ascend platform.
- Script Porting
- Script before porting:
import tensorflow as tf a = tf.random_normal([2, 3], dtype=tf.float32) b = tf.random_uniform([2, 3], dtype=tf.float32) c = tf.add(a, b) with tf.Session() as sess: result = sess.run(c) print("result: ", result) - Script after porting:
import tensorflow as tf # Import NPU-related libraries. from npu_bridge.npu_init import * a = tf.random_normal([2, 3], dtype=tf.float32) b = tf.random_uniform([2, 3], dtype=tf.float32) c = tf.add(a, b) # Add allow_soft_placement=True for the session configurations to allow TensorFlow to automatically allocate devices. config = tf.ConfigProto(allow_soft_placement=True) # Add an NPU optimizer named NpuOptimizer. During network compilation, the NPU traverses only the session configurations under NpuOptimizer. custom_op = config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" # Explicitly disable the remapping and memory_optimization functions of TensorFlow to avoid conflicts with the functions of the NPU. config.graph_options.rewrite_options.remapping = RewriterConfig.OFF # Explicitly disable the function. config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF # Explicitly disable the function. with tf.Session(config=config) as sess: result = sess.run(c) print("result: ", result)
- Script before porting:
- Execute the script.
- Set environment variables.
1 2 3 4 5 6 7 8 9 10
# Set one of the following environment variables for the installation path of the infrastructure software on which training depends. The following assumes that the installation user is HwHiAiUser: # Method 1: Install Ascend-CANN-Toolkit for training on an Ascend AI device, which serves as the development environment. . /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh # Method 2: Install Ascend-CANN-NNAE on an Ascend AI device. # . /home/HwHiAiUser/Ascend/nnae/set_env.sh # TF Adapter Python library. ${TFPLUGIN_INSTALL_PATH} indicates the installation path of the TF Adapter package. export PYTHONPATH=${TFPLUGIN_INSTALL_PATH}:$PYTHONPATH export JOB_ID=10087 # User-defined training job ID. Only letters, digits, hyphens (-), and underscores (_) are supported. You are advised not to use a number starting with 0. export ASCEND_DEVICE_ID=0 # Logical ID of the Ascend AI Processor, optional in single-device training and defaulted to 0, indicating that training is performed on device 0.
- Execute the script.
Assume that the ported script is named tf_quickstart.py. The command example is as follows:
python3 tf_quickstart.py
- Set environment variables.