In [1]:
import tensorflow as tf
In [2]:
from pycalphad import Database, Model, variables as v
from pycalphad.codegen.sympydiff_utils import build_functions
from sympy import lambdify
import numpy as np
dbf = Database('Al-Cu-Zr_Zhou.tdb')
mod = Model(dbf, ['AL', 'CU', 'ZR'], 'LIQUID')
In [3]:
mod.variables
Out[3]:
In [4]:
%time bfr = build_functions(mod.GM, mod.variables, include_grad=True)
In [5]:
%%timeit
tf_func = lambdify(mod.variables, mod.GM, modules='tensorflow')
tf_grads = lambdify(mod.variables, [mod.GM.diff(x) for x in mod.variables], modules='tensorflow')
In [6]:
tf_func = lambdify(mod.variables, mod.GM, modules='tensorflow')
tf_grads = lambdify(mod.variables, [mod.GM.diff(x) for x in mod.variables], modules='tensorflow')
In [7]:
%%timeit
func_xla = tf.function(experimental_compile=True)(tf_func)
grad_xla = tf.function(experimental_compile=True)(tf_grads)
In [8]:
func_xla = tf.function(experimental_compile=True)(tf_func)
grad_xla = tf.function(experimental_compile=True)(tf_grads)
In [9]:
out = np.array([0.])
%timeit bfr.func.unsafe_real(np.array([300., 0.3, 0.3, 0.4]), out)
print(out)
In [10]:
out = np.zeros(4)
%timeit bfr.grad.unsafe_real(np.array([300., 0.3, 0.3, 0.4]), out)
print(out)
In [11]:
%timeit func_xla(300.0, 0.3, 0.3, 0.4)
In [12]:
%timeit grad_xla(300.0, 0.3, 0.3, 0.4)
In [13]:
temps = tf.constant(np.linspace(300., 2000., num=1000, dtype=np.float32))
%timeit grad_xla(temps, 0.3, 0.3, 0.4)
In [ ]: