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

feat[cartesian]: debug backend #1686

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _local
/src/__init__.py
/tests/__init__.py
.gt_cache/
.gt_cache*/
.gt_cache_pytest*/

# DaCe
Expand Down
2 changes: 2 additions & 0 deletions src/gt4py/cartesian/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
pass

from .cuda_backend import CudaBackend
from .debug_backend import DebugBackend
from .gtcpp_backend import GTCpuIfirstBackend, GTCpuKfirstBackend, GTGpuBackend
from .module_generator import BaseModuleGenerator
from .numpy_backend import NumpyBackend
Expand All @@ -43,6 +44,7 @@
"BasePyExtBackend",
"CLIBackendMixin",
"CudaBackend",
"DebugBackend",
"GTGpuBackend",
"GTCpuIfirstBackend",
"GTCpuKfirstBackend",
Expand Down
4 changes: 0 additions & 4 deletions src/gt4py/cartesian/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,6 @@ def generate_computation(self) -> Dict[str, Union[str, Dict]]:
source = self.make_module_source(ir=self.builder.gtir)
return {str(file_name): source}

def generate_bindings(self, language_name: str) -> Dict[str, Union[str, Dict]]:
"""Pure python backends typically will not support bindings."""
return super().generate_bindings(language_name)


class BasePyExtBackend(BaseBackend):
@property
Expand Down
80 changes: 80 additions & 0 deletions src/gt4py/cartesian/backend/debug_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# GT4Py - GridTools Framework
#
# Copyright (c) 2014-2023, ETH Zurich
# All rights reserved.
#
# This file is part of the GT4Py project and the GridTools framework.
# GT4Py is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or any later
# version. See the LICENSE.txt file at the top-level directory of this
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, Type, Union

from gt4py import storage
from gt4py.cartesian.backend.base import BaseBackend, CLIBackendMixin, register
from gt4py.cartesian.backend.numpy_backend import ModuleGenerator
from gt4py.cartesian.gtc.debug.debug_codegen import DebugCodeGen
from gt4py.cartesian.gtc.gtir_to_oir import GTIRToOIR
from gt4py.cartesian.gtc.passes.oir_pipeline import OirPipeline
from gt4py.eve.codegen import format_source


if TYPE_CHECKING:
from gt4py.cartesian.stencil_object import StencilObject


def recursive_write(root_path: Path, tree: dict[str, Union[str, dict]]):
root_path.mkdir(parents=True, exist_ok=True)
for key, value in tree.items():
if isinstance(value, dict):
recursive_write(root_path / key, value)
else:
src_path = root_path / key
src_path.write_text(value)


@register
class DebugBackend(BaseBackend, CLIBackendMixin):
"""Debug backend using plain python loops."""

name = "debug"
options: ClassVar[dict[str, Any]] = {
"oir_pipeline": {"versioning": True, "type": OirPipeline},
# TODO: Implement this option in source code
"ignore_np_errstate": {"versioning": True, "type": bool},
}
storage_info = storage.layout.NaiveCPULayout
languages = {"computation": "python", "bindings": ["python"]}
MODULE_GENERATOR_CLASS = ModuleGenerator

def generate_computation(self) -> dict[str, Union[str, dict]]:
computation_name = (
self.builder.caching.module_prefix
+ "computation"
+ self.builder.caching.module_postfix
+ ".py"
)
oir = GTIRToOIR().visit(self.builder.gtir)
source_code = DebugCodeGen().visit(oir)

if self.builder.options.format_source:
source_code = format_source("python", source_code)

return {computation_name: source_code}

def generate_bindings(self, language_name: str) -> dict[str, Union[str, dict]]:
super().generate_bindings(language_name)
return {self.builder.module_path.name: self.make_module_source()}

def generate(self) -> Type["StencilObject"]:
self.check_options(self.builder.options)
src_dir = self.builder.module_path.parent
if not self.builder.options._impl_opts.get("disable-code-generation", False):
src_dir.mkdir(parents=True, exist_ok=True)
recursive_write(src_dir, self.generate_computation())
return self.make_module()
3 changes: 3 additions & 0 deletions src/gt4py/cartesian/gtc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ def zero(cls) -> "CartesianOffset":
def to_dict(self) -> Dict[str, int]:
return {"i": self.i, "j": self.j, "k": self.k}

def to_str(self) -> str:
return f"i + {self.i}, j + {self.j}, k + {self.k}"


class VariableKOffset(eve.GenericNode, Generic[ExprT]):
k: ExprT
Expand Down
13 changes: 13 additions & 0 deletions src/gt4py/cartesian/gtc/debug/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# GT4Py - GridTools Framework
#
# Copyright (c) 2014-2023, ETH Zurich
# All rights reserved.
#
# This file is part of the GT4Py project and the GridTools framework.
# GT4Py is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or any later
# version. See the LICENSE.txt file at the top-level directory of this
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
Loading