Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagation Performance #27

Closed
GlaserN opened this issue Jan 29, 2021 · 3 comments
Closed

Propagation Performance #27

GlaserN opened this issue Jan 29, 2021 · 3 comments
Assignees
Labels
can-wait Not working, not urgent enhancement New feature or request tensorflow Tensorflow Performance and Implementation
Milestone

Comments

@GlaserN
Copy link
Collaborator

GlaserN commented Jan 29, 2021

Is your feature request related to a problem? Please describe.
In the propagation and creation of the Us everything is written by using python for loops. These should be replaced by the tensorflow equivalents to increase performance and actually make use of tensorflow workload distribution.

c3/c3/utils/tf_utils.py

Lines 219 to 224 in ef95330

for ii in range(cflds[0].shape[0]):
cf_t = []
for fields in cflds:
cf_t.append(tf.cast(fields[ii], tf.complex128))
dUs.append(tf_dU_of_t(h0, hks, cf_t, dt))
return dUs

c3/c3/utils/tf_utils.py

Lines 140 to 145 in ef95330

ii = 0
while ii < len(hks):
h += cflds_t[ii] * hks[ii]
ii += 1
terms = int(1e12 * dt) + 2
dU = tf_expm(-1j * h * dt, terms)

Currently every du is calculated by itself and no parallelization is done, which could/should be improved.

Describe the solution you'd like
Make the progapagtion with native tensorflow functions.

Describe alternatives you've considered

Additional context

@shaimach
Copy link
Collaborator

shaimach commented Jan 29, 2021 via email

@fedroy fedroy added the enhancement New feature or request label Jan 29, 2021
@nwittler
Copy link
Collaborator

One thing to keep in mind is that the python structure is not necessarily the tensorflow structure. The examples above use decorated @tf.functions which means they will be parsed as a for loop but not executed that way. It all comes back to the fact that we need a proper software engineer to find the best tensorflow network representation for the propagation.

Below is an example notebook but that's about as far as I got investigating this.

import numpy as np
import tensorflow as tf
import time

slices = 3000
var = tf.Variable(np.random.rand(100,100))

def expm(var):
    return tf.linalg.expm(var)

@tf.function
def tf_expm(var):
    return tf.linalg.expm(var)
start_time = time.time()
res = []
for ii in range(slices):
    res.append(expm(var))
print(time.time() - start_time, "seconds")
19.600555658340454 seconds
start_time = time.time()
res2 = []
for ii in range(slices):
    res2.append(tf_expm(var))
print(time.time() - start_time, "seconds")
4.85532021522522 seconds
var_vec = tf.Variable(np.random.rand(slices, 100,100))
def expm_vec():
    return tf.vectorized_map(expm, var_vec)
start_time = time.time()
res_vec = expm_vec()
print(time.time() - start_time, "seconds")
WARNING:tensorflow:Using a while_loop for converting ResourceGather
6.617619276046753 seconds
@tf.function
def expm_tf_vec():
    return tf.vectorized_map(expm, var_vec)
start_time = time.time()
res_vec_2 = expm_tf_vec()
print(time.time() - start_time, "seconds")
WARNING:tensorflow:Using a while_loop for converting ResourceGather
6.34708309173584 seconds

@lazyoracle lazyoracle self-assigned this Feb 2, 2021
@lazyoracle lazyoracle added can-wait Not working, not urgent tensorflow Tensorflow Performance and Implementation labels Feb 2, 2021
@lazyoracle lazyoracle removed their assignment Feb 4, 2021
@GlaserN GlaserN self-assigned this Feb 4, 2021
@lazyoracle
Copy link
Member

@GlaserN Is this issue fixed in #34?

@GlaserN GlaserN closed this as completed Mar 26, 2021
@lazyoracle lazyoracle added this to the 1.3 milestone May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can-wait Not working, not urgent enhancement New feature or request tensorflow Tensorflow Performance and Implementation
Projects
None yet
Development

No branches or pull requests

5 participants