Thursday, 14 November 2019

Different Methods of Turning a Python Function into C-based Code

Python code must be converted into C-based code for performance and utilising CPU together with GPU. These are some methods:

1. Use @tf.function decorator
Recommended method in TensorFlow 2

2. Use tf.function directly over some 'def' (pure Python function)
Not recommended, although this yields the same result as method 1.

3. Use tf.autograph.to_graph to create Autograph
Not recommended, although this creates a callable similar to tf.function

4. Use tf.Graph().as_default() to create Graph
Not recommended, legacy of TensorFlow 1, must run in a tf.compat.v1.Session

Example code:
%tensorflow_version 2.x
%reset -f

#libs
import tensorflow as tf;

#global constants
T1 = tf.constant(1, tf.float32);

#use decoration to convert to c-based function
#this decorator makes: f1 = tf.function(f1);
@tf.function
def f1(T1):
  T2 = tf.constant(2, tf.float32);
  return T1+T2;

def f2(T1):
  T2 = tf.constant(2, tf.float32);
  return T1+T2;

print("Use decorator:");
print(f1); #TensorFlow function!
print(f1(T1));

#the same like using @tf.function decorator
print("\nUse tf.function directly:");
f2 = tf.function(f2);
print(f2); #TensorFlow function!
print(f2(T1));

#create autograph from function
def f3(T1):
  T2 = tf.constant(2, tf.float32);
  return T1+T2;

g3 = tf.autograph.to_graph(f3);
print("\nUse Autograph as function:");
print(g3); #Python function!
print(g3(T1));

#create graph using 'with'
G = tf.Graph();
with G.as_default(): 
  Inp = tf.compat.v1.placeholder(tf.float32);
  T2  = tf.constant(2, tf.float32);
  Out = Inp+T2;

print("\nUse Graph in session:");
S = tf.compat.v1.Session(graph=G);
R = S.run(Out, feed_dict={Inp:T1.numpy()});
print(G);
print(R); #numpy values, not raw python values
#eof

Result:
Use decorator: <tensorflow.python.eager.def_function.Function object at 0x7efe62ad32b0> tf.Tensor(3.0, shape=(), dtype=float32) Use tf.function directly: <tensorflow.python.eager.def_function.Function object at 0x7efe62b29278> tf.Tensor(3.0, shape=(), dtype=float32) Use Autograph as function: <function create_converted_entity_factory.<locals>.create_converted_entity.<locals>.tf__f3 at 0x7efe62903e18> tf.Tensor(3.0, shape=(), dtype=float32) Use Graph in session: <tensorflow.python.framework.ops.Graph object at 0x7efe696155c0> 3.0

No comments:

Post a Comment