Skip to content
Snippets Groups Projects
Commit 1bcdc628 authored by Stephan Seitz's avatar Stephan Seitz
Browse files

Fix tf -> Field and include pycuda headers

parent 7e2aa52f
No related branches found
No related tags found
No related merge requests found
Pipeline #19340 failed
......@@ -53,10 +53,18 @@ def create_field_from_array_like(field_name, maybe_array, annotations=None):
except ImportError:
torch = None
if torch:
# Torch tensors don't have t.strides but t.stride(dim). Let's fix that!
if isinstance(maybe_array, torch.Tensor):
maybe_array = _torch_tensor_to_numpy_shim(maybe_array)
if 'tensorflow.python.' in str(type(maybe_array)) and 'Tensor' in str(type(maybe_array)):
try:
# This fails on eager execution
return Field.create_fixed_size(maybe_array.name or field_name,
maybe_array.shape,
index_dimensions=index_dimensions,
dtype=maybe_array.dtype.as_numpy_dtype())
except Exception:
return Field.create_fixed_size(field_name,
maybe_array.shape,
index_dimensions=index_dimensions,
dtype=maybe_array.dtype.as_numpy_dtype())
field = Field.create_from_numpy_array(field_name, maybe_array, index_dimensions)
field.field_type = field_type
......
......@@ -17,7 +17,8 @@ import p_tqdm
import pystencils
import pystencils.gpucuda
from pystencils.cpu.cpujit import get_cache_config, get_compiler_config, get_pystencils_include_path
from pystencils.cpu.cpujit import get_cache_config, get_compiler_config
from pystencils.include import get_pycuda_include_path, get_pystencils_include_path
from pystencils_autodiff._file_io import read_file, write_file
_hash = hashlib.md5
......@@ -31,7 +32,9 @@ else:
if get_compiler_config()['os'] != 'windows':
_shared_object_flag = '-shared'
_output_flag = '-o'
_include_flags = ['-I' + sysconfig.get_paths()['include'], '-I' + get_pystencils_include_path()]
_include_flags = ['-I' + sysconfig.get_paths()['include'],
'-I' + get_pystencils_include_path(),
'-I' + get_pycuda_include_path()]
_do_not_link_flag = "-c"
_position_independent_flag = "-fPIC"
_compile_env = os.environ.copy()
......@@ -41,7 +44,9 @@ else:
_do_not_link_flag = '-c'
_output_flag = '-o'
_shared_object_flag = '/DLL'
_include_flags = ['-I' + sysconfig.get_paths()['include'], '-I' + get_pystencils_include_path()]
_include_flags = ['-I' + sysconfig.get_paths()['include'],
'-I' + get_pystencils_include_path(),
'-I' + get_pycuda_include_path()]
_position_independent_flag = "/DTHIS_FLAG_DOES_NOTHING"
get_compiler_config()['command'] = 'cl.exe'
config_env = get_compiler_config()['env'] if 'env' in get_compiler_config() else {}
......
......@@ -184,7 +184,7 @@ def test_valid_boundary_handling_torch_native():
@pytest.mark.parametrize('with_offsets', (False, True))
@pytest.mark.parametrize('with_cuda',
(False, pytest.param(True, marks=pytest.mark.skipif('CI' in os.environ, reason='SEGFAULT on CI')))) # noqa
(False, pytest.param(True, marks=pytest.mark.skipif('CI' in os.environ, reason='PYTORCH does not like pycuda')))) # noqa
def test_tfmad_gradient_check_torch_native(with_offsets, with_cuda):
torch = pytest.importorskip('torch')
import torch
......@@ -233,12 +233,12 @@ def test_tfmad_gradient_check_torch_native(with_offsets, with_cuda):
@pytest.mark.parametrize('gradient_check', (False, 'with_gradient_check'))
@pytest.mark.parametrize('with_cuda', (False, pytest.param('with_cuda', marks=pytest.mark.xfail)))
@pytest.mark.parametrize('with_offsets', (False, 'with_offsets'))
@pytest.mark.xfail(reason="", strict=False)
# @pytest.mark.xfail(reason="", strict=False)
def test_tfmad_gradient_check_tensorflow_native(with_offsets, with_cuda, gradient_check):
pytest.importorskip('tensorflow')
import tensorflow as tf
a, b, out = ps.fields("a, b, out: double[21,13]")
a, b, out = ps.fields("a, b, out: double[21,13]", layout='fzyx')
print(a.shape)
if with_offsets:
......@@ -269,22 +269,20 @@ def test_tfmad_gradient_check_tensorflow_native(with_offsets, with_cuda, gradien
with tf.Graph().as_default():
a_tensor = tf.Variable(np.zeros(a.shape, a.dtype.numpy_dtype))
b_tensor = tf.Variable(np.zeros(a.shape, a.dtype.numpy_dtype))
out_tensor = auto_diff.create_tensorflow_op(use_cuda=with_cuda,
backend='tensorflow_native')(a=a_tensor,
b=b_tensor)
op = auto_diff.create_tensorflow_op(use_cuda=with_cuda, backend='tensorflow_native')
out_tensor = op(a=a_tensor, b=b_tensor)
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(out_tensor)
if gradient_check:
gradient_error = compute_gradient_error_without_border(
[a_tensor, b_tensor], [a.shape, b.shape],
out_tensor,
out.shape,
num_border_pixels=2,
num_border_pixels=0,
ndim=2,
debug=False)
debug=True)
print('error: %s' % gradient_error.max_error)
print('avg error: %s' % gradient_error.avg_error)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment