forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from __future__ import print_function | ||
import numpy as np | ||
import tensorflow.compat.v1 as tf | ||
import random | ||
import copy | ||
|
||
if int(tf.__version__[0]) >= 2: | ||
tf.disable_v2_behavior() | ||
|
||
|
||
def CompareCpuAndGpu(): | ||
# [batch, seqlen, nh, dim] = [80, 128, 8, 64] | ||
[batch, seqlen, nh, dim] = [1, 16, 1, 2] | ||
shape_input = [batch, seqlen, nh, dim] | ||
shape_in_grad = [batch, nh, seqlen, seqlen] | ||
|
||
inputs_cpu = {} | ||
inputs_gpu = {} | ||
|
||
q_placeholder = tf.placeholder(tf.float32, shape_input) | ||
inputs_cpu[q_placeholder] = [ | ||
[ | ||
[[random.uniform(-2, 2) for i in range(dim)] for j in range(nh)] | ||
for k in range(seqlen) | ||
] | ||
for t in range(batch) | ||
] | ||
inputs_gpu[q_placeholder] = copy.deepcopy(inputs_cpu[q_placeholder]) | ||
|
||
k_placeholder = tf.placeholder(tf.float32, shape_input) | ||
inputs_cpu[k_placeholder] = [ | ||
[ | ||
[[random.uniform(-2, 2) for i in range(dim)] for j in range(nh)] | ||
for k in range(seqlen) | ||
] | ||
for t in range(batch) | ||
] | ||
inputs_gpu[k_placeholder] = copy.deepcopy(inputs_cpu[k_placeholder]) | ||
|
||
in_grad_placeholder = tf.placeholder(tf.float32, shape_in_grad) | ||
inputs_cpu[in_grad_placeholder] = [ | ||
[ | ||
[[random.uniform(-2, 2) for i in range(seqlen)] for j in range(seqlen)] | ||
for k in range(nh) | ||
] | ||
for t in range(batch) | ||
] | ||
inputs_gpu[in_grad_placeholder] = copy.deepcopy(inputs_cpu[in_grad_placeholder]) | ||
|
||
def T(t): | ||
return tf.transpose(t, [0, 2, 1, 3]) | ||
|
||
def calc_grad_cpu(q, k, in_grad): | ||
with tf.xla.experimental.jit_scope(separate_compiled_gradients=True): | ||
with tf.device("/CPU:0"): | ||
qk = tf.matmul(T(q), T(k), transpose_b=True) | ||
grad_q, grad_k = tf.gradients(qk, [q, k], in_grad) | ||
return [qk, grad_q, grad_k] | ||
|
||
def calc_grad_gpu(q, k, in_grad): | ||
with tf.xla.experimental.jit_scope(separate_compiled_gradients=True): | ||
with tf.device("/GPU:0"): | ||
qk = tf.matmul(T(q), T(k), transpose_b=True) | ||
grad_q, grad_k = tf.gradients(qk, [q, k], in_grad) | ||
return [qk, grad_q, grad_k] | ||
|
||
sess_config = tf.ConfigProto(allow_soft_placement=False, log_device_placement=False) | ||
sess_config.gpu_options.allow_growth = True | ||
with tf.Session(config=sess_config) as sess: | ||
res1 = sess.run(calc_grad_cpu(q_placeholder, k_placeholder, in_grad_placeholder), feed_dict=inputs_cpu) | ||
res2 = sess.run(calc_grad_gpu(q_placeholder, k_placeholder, in_grad_placeholder), feed_dict=inputs_gpu) | ||
|
||
return res1, res2 | ||
|
||
|
||
if __name__ == "__main__": | ||
res1, res2 = CompareCpuAndGpu() | ||
for i in range(len(res1)): | ||
print(np.allclose(res1[i], res2[i], rtol=4e-2, atol=4e-2)) | ||
|