From b1dc16e37ab3251f71b6dab384fb3ffa3138b183 Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Thu, 12 Sep 2019 13:47:26 +0200 Subject: [PATCH] Un-prefix: functions from pystencils_autodiff._file_io --- src/pystencils_autodiff/_file_io.py | 8 ++++---- .../backends/_torch_native.py | 16 ++++++++-------- src/pystencils_autodiff/backends/astnodes.py | 6 +++--- tests/backends/test_torch_native_compilation.py | 6 +++--- tests/test_native_tensorflow_compilation.py | 8 ++++---- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/pystencils_autodiff/_file_io.py b/src/pystencils_autodiff/_file_io.py index 3da195a..0b7f443 100644 --- a/src/pystencils_autodiff/_file_io.py +++ b/src/pystencils_autodiff/_file_io.py @@ -10,15 +10,15 @@ import jinja2 -def _read_template_from_file(file): - return jinja2.Template(_read_file(file)) +def read_template_from_file(file): + return jinja2.Template(read_file(file)) -def _read_file(file): +def read_file(file): with open(file, 'r') as f: return f.read() -def _write_file(filename, content): +def write_file(filename, content): with open(filename, 'w') as f: return f.write(content) diff --git a/src/pystencils_autodiff/backends/_torch_native.py b/src/pystencils_autodiff/backends/_torch_native.py index fcd1ac9..d63db1d 100644 --- a/src/pystencils_autodiff/backends/_torch_native.py +++ b/src/pystencils_autodiff/backends/_torch_native.py @@ -22,12 +22,12 @@ except ImportError: pass -def _read_file(file): +def read_file(file): with open(file, 'r') as f: return f.read() -def _write_file(filename, content): +def write_file(filename, content): with open(filename, 'w') as f: return f.write(content) @@ -106,22 +106,22 @@ def generate_torch(destination_folder, } if is_cuda: - template_string_cpp = _read_file(join(dirname(__file__), + template_string_cpp = read_file(join(dirname(__file__), 'torch_native_cuda.tmpl.cpp')) template = jinja2.Template(template_string_cpp) output = template.render(render_dict) - _write_file(join(destination_folder, operation_string + '.cpp'), output) + write_file(join(destination_folder, operation_string + '.cpp'), output) - template_string = _read_file(join(dirname(__file__), 'torch_native_cuda.tmpl.cu')) + template_string = read_file(join(dirname(__file__), 'torch_native_cuda.tmpl.cu')) template = jinja2.Template(template_string) output = template.render(render_dict) - _write_file(join(destination_folder, operation_string + '.cu'), output) + write_file(join(destination_folder, operation_string + '.cu'), output) else: - template_string_cpp = _read_file(join(dirname(__file__), + template_string_cpp = read_file(join(dirname(__file__), 'torch_native_cpu.tmpl.cpp')) template = jinja2.Template(template_string_cpp) output = template.render(render_dict) - _write_file(join(destination_folder, operation_string + '.cpp'), output) + write_file(join(destination_folder, operation_string + '.cpp'), output) from torch.utils.cpp_extension import load compiled_operation = load(operation_string, required_files, verbose=True, diff --git a/src/pystencils_autodiff/backends/astnodes.py b/src/pystencils_autodiff/backends/astnodes.py index 922bcbe..f507fa6 100644 --- a/src/pystencils_autodiff/backends/astnodes.py +++ b/src/pystencils_autodiff/backends/astnodes.py @@ -12,7 +12,7 @@ from collections.abc import Iterable from os.path import dirname, join from pystencils.astnodes import FieldPointerSymbol, FieldShapeSymbol, FieldStrideSymbol -from pystencils_autodiff._file_io import _read_template_from_file +from pystencils_autodiff._file_io import read_template_from_file from pystencils_autodiff.backends.python_bindings import ( PybindFunctionWrapping, PybindPythonBindings, TensorflowFunctionWrapping, TensorflowPythonBindings, TorchPythonBindings) @@ -61,7 +61,7 @@ class PybindArrayDestructuring(DestructuringBindingsForFieldClass): class TorchModule(JinjaCppFile): - TEMPLATE = _read_template_from_file(join(dirname(__file__), 'module.tmpl.cpp')) + TEMPLATE = read_template_from_file(join(dirname(__file__), 'module.tmpl.cpp')) DESTRUCTURING_CLASS = TorchTensorDestructuring PYTHON_BINDINGS_CLASS = TorchPythonBindings PYTHON_FUNCTION_WRAPPING_CLASS = PybindFunctionWrapping @@ -103,7 +103,7 @@ class TensorflowModule(TorchModule): :param backward_kernel_ast: """ if use_cuda: - self.TEMPLATE = _read_template_from_file(join(dirname(__file__), 'tensorflow.cuda.tmpl.cu')) + self.TEMPLATE = read_template_from_file(join(dirname(__file__), 'tensorflow.cuda.tmpl.cu')) super().__init__(module_name, kernel_asts) diff --git a/tests/backends/test_torch_native_compilation.py b/tests/backends/test_torch_native_compilation.py index 1868689..9a1b906 100644 --- a/tests/backends/test_torch_native_compilation.py +++ b/tests/backends/test_torch_native_compilation.py @@ -13,7 +13,7 @@ import tempfile import pystencils from pystencils_autodiff import create_backward_assignments -from pystencils_autodiff._file_io import _write_file +from pystencils_autodiff._file_io import write_file from pystencils_autodiff.backends.astnodes import TorchModule torch = pytest.importorskip('torch') @@ -66,7 +66,7 @@ def test_torch_native_compilation_cpu(): temp_file = tempfile.NamedTemporaryFile(suffix='.cu' if target == 'gpu' else '.cpp') print(temp_file.name) - _write_file(temp_file.name, str(module)) + write_file(temp_file.name, str(module)) torch_extension = load(module_name, [temp_file.name]) assert torch_extension is not None assert 'call_forward' in dir(torch_extension) @@ -99,7 +99,7 @@ def test_torch_native_compilation_gpu(): temp_file = tempfile.NamedTemporaryFile(suffix='.cu' if target == 'gpu' else '.cpp') print(temp_file.name) - _write_file(temp_file.name, str(module)) + write_file(temp_file.name, str(module)) torch_extension = load(module_name, [temp_file.name]) assert torch_extension is not None assert 'call_forward' in dir(torch_extension) diff --git a/tests/test_native_tensorflow_compilation.py b/tests/test_native_tensorflow_compilation.py index 273fc9f..864c4a0 100644 --- a/tests/test_native_tensorflow_compilation.py +++ b/tests/test_native_tensorflow_compilation.py @@ -20,7 +20,7 @@ import sympy import pystencils from pystencils.include import get_pystencils_include_path from pystencils_autodiff import create_backward_assignments -from pystencils_autodiff._file_io import _write_file +from pystencils_autodiff._file_io import write_file from pystencils_autodiff.backends.astnodes import TensorflowModule @@ -71,8 +71,8 @@ def test_native_tensorflow_compilation_cpu(): temp_file = tempfile.NamedTemporaryFile(suffix='.cu' if target == 'gpu' else '.cpp') print(temp_file.name) - _write_file(temp_file.name, str(module)) - _write_file('/tmp/foo.cpp', str(module)) + write_file(temp_file.name, str(module)) + write_file('/tmp/foo.cpp', str(module)) command = ['c++', '-fPIC', temp_file.name, '-O2', '-shared', '-o', 'foo.so'] + compile_flags + link_flags + extra_flags @@ -115,7 +115,7 @@ def test_native_tensorflow_compilation_gpu(): temp_file = tempfile.NamedTemporaryFile(suffix='.cu' if target == 'gpu' else '.cpp') print(temp_file.name) - _write_file(temp_file.name, str(module)) + write_file(temp_file.name, str(module)) # on my machine g++-6 and clang-7 are working command = ['nvcc', -- GitLab