npu_bridge.estimator.npu.util
set_iteration_per_loop
Prototype
def set_iteration_per_loop(sess, train_op, iterations_per_loop=1)
Description
Sets the number of iterations per training loop in sess.run mode, that is, the number of training iterations executed on the device side in each sess.run() call. This API can save unnecessary interactions between the host and device and reduce the training time consumption.
Restrictions
The preceding API involves graph modification. If a graph cannot be modified (for example, the graph is frozen or a session is created using tf.train.Supervisor), you cannot use the set_iteration_per_loop API to set the loops and iterations per loop. In this case, use create_iteration_per_loop_var and load_iteration_per_loop_var.
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
sess |
Input |
Created TensorFlow session |
train_op |
Input |
Operation that updates a variable or gradient |
iterations_per_loop |
Input |
Number of iterations per training loop per sess.run() call on the device side. Defaults to 1. The total number of iterations per training loop must be an integer multiple of iterations_per_loop. In mixed computing mode (mix_compile_mode is set to True), this parameter must be set to 1. |
Returns
An operator for the user to call by using sess.run(op)
create_iteration_per_loop_var
Prototype
def create_iteration_per_loop_var(self, train_op)
Description
This API is used in conjunction with load_iteration_per_loop_var to set the number of iterations per training loop every sess.run() call on the device side. This API is used to modify a graph and set the number of iterations per loop using load_iteration_per_loop_var.
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
train_op |
Input |
Operation that updates a variable or gradient |
Returns
An operator for the user to call by using sess.run(op)
Example
# Train a model. with tf.Session(config=config) as sess: sess.run(init) # Set the number of iterations per loop to 10 in sess.run mode. iteration = util.IterationPerLoop() train_op = iteration.create_iteration_per_loop_var(optimizer) # Modify the graph. tf.train.Supervisor(logdir="/home/xxxx",init_op=init) # Freeze the graph. iteration.load_iteration_per_loop_var(sess, 10) # Set the number of iterations per loop. for epoch in range(training_epochs): avg_cost = 0 total_batch = int(mnist.train.num_examples / batch_size) for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) _, c = sess.run([train_op, cost], feed_dict={x: batch_xs, y: batch_ys}) avg_cost += c / total_batch
load_iteration_per_loop_var
Prototype
def load_iteration_per_loop_var(self, sess, iterations_per_loop=1)
Description
This API is used in conjunction with create_iteration_per_loop_var to set the number of iterations per training loop every sess.run() call on the device side.
Restrictions
In mixed computing mode (mix_compile_mode is set to True), this parameter must be set to 1.
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
sess |
Input |
Created TensorFlow session |
iterations_per_loop |
Input |
Number of iterations per training loop per sess.run() call on the device side. Defaults to 1. The total number of iterations per training loop must be an integer multiple of iterations_per_loop. |
Returns
None
Example
# Train a model. with tf.Session(config=config) as sess: sess.run(init) # Set the number of iterations per loop to 10 in sess.run mode. iteration = util.IterationPerLoop() train_op = iteration.create_iteration_per_loop_var(optimizer) # Modify the graph. tf.train.Supervisor(logdir="/home/xxxx",init_op=init) # Freeze the graph. iteration.load_iteration_per_loop_var(sess, 10) # Set the number of iterations per loop. for epoch in range(training_epochs): avg_cost = 0 total_batch = int(mnist.train.num_examples / batch_size) for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) _, c = sess.run([train_op, cost], feed_dict={x: batch_xs, y: batch_ys}) avg_cost += c / total_batch